Jump to content

Custom Mod Breaks Mobs in 1.15.2


JJ Revans

Recommended Posts

So I am making a mod that adds an entity that beats the game for you. When I spawn it in, all mobs in the game freeze and liquids stop working. Not only that, but chunk generation slows down considerably and the world becomes un-savable. The script for the entity's actions is in it's basic Entity script, so the script is very very lengthy. Here it is: (btw, it is incomplete)

package com.jaegalaxy.illi.entities;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.*;
import net.minecraft.entity.ai.goal.SwimGoal;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.DamageSource;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;

import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.List;

public class BotEntity extends MobEntity
{
    public List<ItemStack> itemStackList = Arrays.asList();

    private final float SPEED = 0.23f;

    private BlockPos CraftingTablePos;

    private boolean SPAWNEDYET = false;

    private boolean foundTree = false; // whether or not we have found a tree
    private BlockPos nearestTree; // where the nearest tree is
    private int tryfindtreerepeats = 5; // how many times we want to find and break a tree

    private Block mineForBlock;
    private boolean startMining = false;
    private boolean movingtomine = false;
    private BlockPos mineStart;
    private BlockPos movingToMinePos;
    private boolean afterMiningMove = false;

    private boolean smeltingIron = false;
    private int ticksSinceStartedSmeltingIron = 0;

    private boolean goingToWater = false;
    private BlockPos WaterPos;
    private boolean goingToLava = false;
    private BlockPos LavaPos;

    private boolean waitingForNPortal = false;
    private boolean goingToPortalAfterBuilt = false;
    private BlockPos OGPortalPos;
    private boolean WTSAGIP = false;
    private int WTSAGIP_ticks = 0;

    boolean findingBlazes = false;
    BlockPos blazeLocation;

    boolean tryingToKill = false;


    public BotEntity(EntityType<? extends BotEntity> type, World worldIn)
    {
        super(type, worldIn);
    }

    @Override
    protected void registerAttributes() {
        super.registerAttributes();
        this.getAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(20);
        this.getAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.23F);
    }

    @Override
    protected void registerGoals() {
        this.goalSelector.addGoal(0, new SwimGoal(this));
    }

    @Nullable
    @Override
    public ILivingEntityData onInitialSpawn(IWorld worldIn, DifficultyInstance difficultyIn, SpawnReason reason, @Nullable ILivingEntityData spawnDataIn, @Nullable CompoundNBT dataTag)
    {
        BotStart();
        return super.onInitialSpawn(worldIn, difficultyIn, reason, spawnDataIn, dataTag);
    }

    void BotStart()
    {
        SPAWNEDYET = true;
        TryFindTree();
    }

    @Override
    public void onDeath(DamageSource cause) {
        super.onDeath(cause);

        if(!world.isRemote)
        {
            for (ItemStack stacky : itemStackList)
            {
                ItemEntity stackyEntity = new ItemEntity(world, getPosX(), getPosY(), getPosZ());
                world.addEntity(stackyEntity);
            }
        }
    }

    @Nullable
    @Override
    protected SoundEvent getHurtSound(DamageSource damageSourceIn) {
        return SoundEvents.ENTITY_PLAYER_HURT;
    }

    @Nullable
    @Override
    protected SoundEvent getDeathSound() {
        return SoundEvents.ENTITY_PLAYER_DEATH;
    }

    //public boolean canAttackClass(Class par1Class)
    //{
      //  return EntityCreeper.class != par1Class && EntityGhast.class != par1Class;
    //}

    void TryFindTree() // we want to find a tree.
    {
        foundTree = false; // we have not found a tree
        nearestTree = FindBlock(Blocks.OAK_LOG, 50); // we want to find the nearest tree
        if(!nearestTree.equals(new BlockPos(getPosX(), getPosY(), getPosZ()))) // if the FindBlock doesnt give us the random thing...
            getNavigator().tryMoveToXYZ(nearestTree.getX() - 1, nearestTree.getY(), nearestTree.getZ() - 1, SPEED); // get to the tree
        else{ // otherwise...
            foundTree = true; // P.S. this is totally a lie just FYI
            TryFindTree(); // find a new tree
        }
    }

    void TryBreakTree() // we want to break a tree.
    {
        world.setBlockState(nearestTree, Blocks.AIR.getDefaultState()); // mine the wood
        itemStackList.add(new ItemStack(Items.OAK_LOG)); // mmm get that nice tasty wood
    }

    void TryCraftOakPlanks() // 4 oak planks = 1 oak log. do this for every log!
    {
        for(ItemStack stacky : itemStackList){ // repeat for each item in the bot's inventory
            if(stacky.equals(new ItemStack(Items.OAK_LOG))){ // if the bot has oak log
                itemStackList.remove(stacky); // take away the oak logs
                itemStackList.add(new ItemStack(Items.OAK_PLANKS)); // give the bot wooden planks
                itemStackList.add(new ItemStack(Items.OAK_PLANKS)); // give the bot wooden planks
                itemStackList.add(new ItemStack(Items.OAK_PLANKS)); // give the bot wooden planks
                itemStackList.add(new ItemStack(Items.OAK_PLANKS)); // give the bot wooden planks
                // these planks will be used for sticks and such
            }
        }

        TryCraftCraftingTable(); // craft a crafting table
    }

    void TryCraftCraftingTable() // crafting table = 4 oak planks.
    {
        int loops = 4; // do this once
        for(ItemStack stacky : itemStackList){ // repeat for each item in the bot's inventory
            if(stacky.equals(new ItemStack(Items.OAK_PLANKS))){ // if the bot has oak planks
                if(loops > 0) { // if we have not looped enough
                    itemStackList.remove(stacky); // take away the oak plank
                    loops--; // we have looped
                }
            }
        }
        itemStackList.add(new ItemStack(Items.CRAFTING_TABLE)); // give the bot a crafting table

        TryPlaceCraftingTable(); // place a crafting table
    }

    void TryPlaceCraftingTable() // place crafting table. do this until a crafting table is crafted
    {
        BlockPos craftpos = new BlockPos(getPosX(), getPosY(), getPosZ() +1 );
        int loops = 1; // do this once
        world.setBlockState(craftpos, Blocks.CRAFTING_TABLE.getDefaultState());
        for(ItemStack stacky : itemStackList){ // repeat for each item in the bot's inventory
            if(stacky.equals(new ItemStack(Items.CRAFTING_TABLE))){ // if the bot has crafting table
                if(loops > 0) { // if we have not looped enough
                    itemStackList.remove(stacky); // take away the crafting table
                    loops--; // we have looped
                }
            }
        }
        world.setBlockState(craftpos, Blocks.CRAFTING_TABLE.getDefaultState()); // place the crafting table
        CraftingTablePos = craftpos;
        TryCraftSomeSticks(); // craft those sticks!
    }

    void TryCraftSomeSticks() // 8 sticks = 4 oak planks
    {
        int loops = 2; // do this twice
        for(ItemStack stacky : itemStackList) { // repeat for each item in the bot's inventory
            if (stacky.equals(new ItemStack(Items.OAK_PLANKS))) { // if the bot has oak planks
                if (loops > 0) { // if we have not looped enough
                    itemStackList.remove(stacky); // take away an oak plank
                    itemStackList.remove(stacky); // take away an oak plank
                    itemStackList.add(new ItemStack(Items.CRAFTING_TABLE)); // give the bot a crafting table
                    itemStackList.add(new ItemStack(Items.CRAFTING_TABLE)); // give the bot a crafting table
                    itemStackList.add(new ItemStack(Items.CRAFTING_TABLE)); // give the bot a crafting table
                    itemStackList.add(new ItemStack(Items.CRAFTING_TABLE)); // give the bot a crafting table
                    loops--;
                }
            }
        }

        TryCraftPickaxe(); // craft a pickaxe...
    }

    void TryCraftPickaxe()
    {
        // TAKE AWAY OAK PLANKS!
        int loops = 3; // do this 3 times
        for(ItemStack stacky : itemStackList) { // repeat for each item in the bot's inventory
            if (stacky.equals(new ItemStack(Items.OAK_PLANKS))) { // if the bot has oak planks
                if (loops > 0) { // if we have not looped enough
                    itemStackList.remove(stacky); // take away an oak plank
                    loops--; // we have looped
                }
            }
        }

        // TAKE AWAY STICKS!
        int loops2 = 2; // do this twice
        for(ItemStack stacky : itemStackList) { // repeat for each item in the bot's inventory
            if (stacky.equals(new ItemStack(Items.STICK))) { // if the bot has stick
                if (loops2 > 0) { // if we have not looped enough
                    itemStackList.remove(stacky); // take away stick
                    loops2--; // we have looped
                }
            }
        }

        itemStackList.add(new ItemStack(Items.WOODEN_PICKAXE)); // give the bot a pickaxe

        TryMineForCobble();
    }

    void TryMineForCobble()
    {
        StartMine(Blocks.COBBLESTONE);
    }

    void TryCraftStonePickaxe()
    {
        // repeat for each item in the bot's inventory
        // if the bot has cobblestone
        // take it away
        itemStackList.removeIf(stacky -> stacky.equals(new ItemStack(Items.COBBLESTONE)));
        int loops = 2;
        for(ItemStack stacky : itemStackList){ // repeat for each item in the bot's inventory
            if(stacky.equals(new ItemStack(Items.STICK))){ // if the bot has sticks
                if(loops > 0) {
                    itemStackList.remove(stacky); // take it away
                    loops--;
                }
            }
        }

        itemStackList.add(new ItemStack(Items.STONE_PICKAXE)); // give the bot a pickaxe

        TryMineMoreForIron();
    }

    void TryMineMoreForIron()
    {
        StartMine(Blocks.IRON_ORE);
    }

    void SmeltIron()
    {
        // craft a furnace using cobblestone collected
        SI_CraftFurnace();
    }

    void SI_CraftFurnace()
    {
        itemStackList.removeIf(stacky -> stacky.equals(new ItemStack(Items.COBBLESTONE)));
        int loops = 8;
        for(ItemStack stacky : itemStackList){ // repeat for each item in the bot's inventory
            if(stacky.equals(new ItemStack(Items.COBBLESTONE))){ // if the bot has cobblestone
                if(loops > 0) {
                    itemStackList.remove(stacky); // take it away
                    loops--;
                }
            }
        }

        BlockPos furnacePos = new BlockPos(CraftingTablePos.getX() - 1, CraftingTablePos.getY(), CraftingTablePos.getZ());
        world.setBlockState(furnacePos, Blocks.FURNACE.getDefaultState());

        SI_StartSmeltIron();
    }

    void SI_StartSmeltIron()
    {
        smeltingIron = true;
    }

    void SI_HandleSmeltSystemAndCraftIronBucket()
    {
        // remove items used for smelting
        itemStackList.add(new ItemStack(Items.IRON_INGOT)); // give the bot an iron ingot
        itemStackList.add(new ItemStack(Items.IRON_INGOT)); // give the bot an iron ingot
        itemStackList.add(new ItemStack(Items.IRON_INGOT)); // give the bot an iron ingot

        int loops = 3;
        for(ItemStack stacky : itemStackList){ // repeat for each item in the bot's inventory
            if(stacky.equals(new ItemStack(Items.OAK_PLANKS))){ // if the bot has cobblestone
                if(loops > 0) {
                    itemStackList.remove(stacky); // take it away
                    loops--;
                }
            }
        }

        int loops2 = 3;
        for(ItemStack stacky : itemStackList){ // repeat for each item in the bot's inventory
            if(stacky.equals(new ItemStack(Items.IRON_INGOT))){ // if the bot has cobblestone
                if(loops2 > 0) {
                    itemStackList.remove(stacky); // take it away
                    loops2--;
                }
            }
        }

        itemStackList.add(new ItemStack(Items.BUCKET)); // give the bot an iron ingot

        AfterwardsCraftAStoneSwordPlease();
    }

    void AfterwardsCraftAStoneSwordPlease()
    {
        // use the crafting algorithm to make a stone sword
        // TAKE AWAY COBBLESTONE!
        int loops = 2; // do this twice
        for(ItemStack stacky : itemStackList) { // repeat for each item in the bot's inventory
            if (stacky.equals(new ItemStack(Items.COBBLESTONE))) { // if the bot has oak planks
                if (loops > 0) { // if we have not looped enough
                    itemStackList.remove(stacky); // take away an oak plank
                    loops--; // we have looped
                }
            }
        }

        // TAKE AWAY STICKS!
        int loops2 = 1; // do this once
        for(ItemStack stacky : itemStackList) { // repeat for each item in the bot's inventory
            if (stacky.equals(new ItemStack(Items.STICK))) { // if the bot has stick
                if (loops2 > 0) { // if we have not looped enough
                    itemStackList.remove(stacky); // take away stick
                    loops2--; // we have looped
                }
            }
        }

        itemStackList.add(new ItemStack(Items.STONE_SWORD)); // give the bot a sword
        FindWater();
    }

    void FindWater()
    {
        // set destination to water and wait
        BlockPos waterPos = FindBlock(Blocks.WATER, 100);
        getNavigator().tryMoveToXYZ(waterPos.getX(), waterPos.getY(), waterPos.getZ(), 0.23f);
        WaterPos = new BlockPos(waterPos.getX() - 1, waterPos.getY(), waterPos.getZ());
        goingToWater = true;
    }

    void FillBucketWithWater()
    {
        // get some water in the bucket!
        BlockPos actualwaterpos = new BlockPos(WaterPos.getX() + 1, WaterPos.getY(), WaterPos.getZ());
        world.setBlockState(actualwaterpos, Blocks.AIR.getDefaultState());

        int loops = 1;
        for(ItemStack stacky : itemStackList){ // repeat for each item in the bot's inventory
            if(stacky.equals(new ItemStack(Items.BUCKET))){ // if the bot has bucket
                if(loops > 0) {
                    itemStackList.remove(stacky); // take it away
                    itemStackList.add(new ItemStack(Items.WATER_BUCKET)); // give the bot a water bucket
                    loops--;
                }
            }
        }

        FindLava();
    }

    void FindLava()
    {
        // set destination to lava and wait
        BlockPos _lavaPos = FindBlock(Blocks.LAVA, 100);
        getNavigator().tryMoveToXYZ(_lavaPos.getX()-7, _lavaPos.getY(), _lavaPos.getZ()-7, 0.23f);
        LavaPos = new BlockPos(_lavaPos.getX()-7, _lavaPos.getY(), _lavaPos.getZ()-7);
        goingToLava = true;
    }

    void CreatePortal()
    {
        // follow the slightly complex algorithm to build a portal

        // make a hole (2x1x2)
        BlockPos pos1 = new BlockPos(LavaPos.getX(), LavaPos.getY(), LavaPos.getZ());
        BlockPos pos2 = new BlockPos(LavaPos.getX(), LavaPos.getY(), LavaPos.getZ()+1);
        BlockPos pos3 = new BlockPos(LavaPos.getX()+1, LavaPos.getY(), LavaPos.getZ());
        BlockPos pos4 = new BlockPos(LavaPos.getX()+1, LavaPos.getY(), LavaPos.getZ()+1);

        CP_MineBlock(pos1);
        CP_MineBlock(pos2);
        CP_MineBlock(pos3);
        CP_MineBlock(pos4);

        // make a wall (2x3x1)
        BlockPos PP1 = new BlockPos(LavaPos.getX()-1, LavaPos.getY()+1, LavaPos.getZ());
        BlockPos PP2 = new BlockPos(LavaPos.getX()-1, LavaPos.getY()+2, LavaPos.getZ());
        BlockPos PP3 = new BlockPos(LavaPos.getX()-1, LavaPos.getY()+3, LavaPos.getZ());
        BlockPos PP4 = new BlockPos(LavaPos.getX()-1, LavaPos.getY()+1, LavaPos.getZ()+1);
        BlockPos PP5 = new BlockPos(LavaPos.getX()-1, LavaPos.getY()+2, LavaPos.getZ()+1);
        BlockPos PP6 = new BlockPos(LavaPos.getX()-1, LavaPos.getY()+3, LavaPos.getZ()+1);

        CP_PlaceBLock(PP1, Blocks.COBBLESTONE);
        CP_PlaceBLock(PP2, Blocks.COBBLESTONE);
        CP_PlaceBLock(PP3, Blocks.COBBLESTONE);
        CP_PlaceBLock(PP4, Blocks.COBBLESTONE);
        CP_PlaceBLock(PP5, Blocks.COBBLESTONE);
        CP_PlaceBLock(PP6, Blocks.COBBLESTONE);

        // pour some water
        BlockPos WP = new BlockPos(LavaPos.getX()-1, LavaPos.getY()+4, LavaPos.getZ()+1);

        CP_EmptyWaterBucket(WP);

        // fill bucket with lava and empty it repeatedly!
        BlockPos EP1 = new BlockPos(LavaPos.getX(), LavaPos.getY()+1, LavaPos.getZ()-1);
        BlockPos EP2 = new BlockPos(LavaPos.getX(), LavaPos.getY()+2, LavaPos.getZ()-1);
        BlockPos EP3 = new BlockPos(LavaPos.getX(), LavaPos.getY()+3, LavaPos.getZ()-1);
        BlockPos EP4 = new BlockPos(LavaPos.getX(), LavaPos.getY()+4, LavaPos.getZ());
        BlockPos EP5 = new BlockPos(LavaPos.getX(), LavaPos.getY()+1, LavaPos.getZ()+2);
        BlockPos EP6 = new BlockPos(LavaPos.getX(), LavaPos.getY()+2, LavaPos.getZ()+2);
        BlockPos EP7 = new BlockPos(LavaPos.getX(), LavaPos.getY()+3, LavaPos.getZ()+2);
        BlockPos EP8 = new BlockPos(LavaPos.getX(), LavaPos.getY()+4, LavaPos.getZ()+1);
        BlockPos EP9 = new BlockPos(LavaPos.getX(), LavaPos.getY(), LavaPos.getZ());
        BlockPos EP10 = new BlockPos(LavaPos.getX(), LavaPos.getY(), LavaPos.getZ()+1);

        CP_FillLavaBucket(FindBlock(Blocks.LAVA, 25));
        CP_EmptyLavaBucket(EP1);
        CP_FillLavaBucket(FindBlock(Blocks.LAVA, 25));
        CP_EmptyLavaBucket(EP2);
        CP_FillLavaBucket(FindBlock(Blocks.LAVA, 25));
        CP_EmptyLavaBucket(EP3);
        CP_FillLavaBucket(FindBlock(Blocks.LAVA, 25));
        CP_EmptyLavaBucket(EP4);
        CP_FillLavaBucket(FindBlock(Blocks.LAVA, 25));
        CP_EmptyLavaBucket(EP5);
        CP_FillLavaBucket(FindBlock(Blocks.LAVA, 25));
        CP_EmptyLavaBucket(EP6);
        CP_FillLavaBucket(FindBlock(Blocks.LAVA, 25));
        CP_EmptyLavaBucket(EP7);
        CP_FillLavaBucket(FindBlock(Blocks.LAVA, 25));
        CP_EmptyLavaBucket(EP8);
        CP_FillLavaBucket(FindBlock(Blocks.LAVA, 25));
        CP_EmptyLavaBucket(EP9);
        CP_FillLavaBucket(FindBlock(Blocks.LAVA, 25));
        CP_EmptyLavaBucket(EP10);

        // use an algorithm to set the portal on fire - run IgnitePortal()
        IgnitePortal(EP10, EP9);
    }

    void CP_EmptyWaterBucket(BlockPos emptyPos)
    {
        // replace water bucket with regular bucket
        int loops = 1;
        for(ItemStack stacky : itemStackList){ // repeat for each item in the bot's inventory
            if(stacky.equals(new ItemStack(Items.WATER_BUCKET))){ // if the bot has water bucket
                if(loops > 0) {
                    itemStackList.remove(stacky); // take it away
                    itemStackList.add(new ItemStack(Items.BUCKET)); // give the bot a bucket
                    loops--;
                }
            }
        }

        // put down water at "emptyPos"
        world.setBlockState(emptyPos, Blocks.WATER.getDefaultState());
    }

    void CP_FillLavaBucket(BlockPos fillPos)
    {
        // replace bucket with lava bucket
        int loops = 1;
        for(ItemStack stacky : itemStackList){ // repeat for each item in the bot's inventory
            if(stacky.equals(new ItemStack(Items.BUCKET))){ // if the bot has bucket
                if(loops > 0) {
                    itemStackList.remove(stacky); // take it away
                    itemStackList.add(new ItemStack(Items.LAVA_BUCKET)); // give the bot a lava bucket
                    loops--;
                }
            }
        }

        // replace lava at "fillPos" with air
        world.setBlockState(fillPos, Blocks.AIR.getDefaultState());
    }

    void CP_EmptyLavaBucket(BlockPos emptyPos)
    {
        // replace lava bucket with regular bucket
        int loops = 1;
        for(ItemStack stacky : itemStackList){ // repeat for each item in the bot's inventory
            if(stacky.equals(new ItemStack(Items.LAVA_BUCKET))){ // if the bot has lava bucket
                if(loops > 0) {
                    itemStackList.remove(stacky); // take it away
                    itemStackList.add(new ItemStack(Items.BUCKET)); // give the bot a bucket
                    loops--;
                }
            }
        }

        // put down lava at "emptyPos"
        world.setBlockState(emptyPos, Blocks.LAVA.getDefaultState());
    }

    void CP_MineBlock(BlockPos blockPos)
    {
        // give the bot the block that will be removed
        itemStackList.add(new ItemStack(Item.getItemFromBlock(world.getBlockState(blockPos).getBlock()))); // give the bot a bucket

        // replace block at "blockPos" with air
        world.setBlockState(blockPos, Blocks.AIR.getDefaultState());
    }

    void CP_PlaceBLock(BlockPos placePos, Block blockToPlace)
    {
        // remove "blockToPlace" from the bot's inv.
        int loops = 1;
        for(ItemStack stacky : itemStackList) { // repeat for each item in the bot's inventory
            if (stacky.equals(new ItemStack(Item.getItemFromBlock(blockToPlace)))) { // if the bot has lava bucket
                if (loops > 0) {
                    itemStackList.remove(stacky); // take it away
                    loops--;
                }
            }
        }

        // place that block at "placePos"
        world.setBlockState(placePos, blockToPlace.getDefaultState());
    }

    void IgnitePortal(BlockPos portalBottomRight, BlockPos portalBottomLeft)
    {
        // replace a block behind the bottom of the portal with lava (left I guess)
        CP_MineBlock(portalBottomLeft);
        CP_FillLavaBucket(FindBlock(Blocks.LAVA, 25));
        CP_PlaceBLock(portalBottomLeft, Blocks.LAVA);

        // place wooden planks right above it
        BlockPos PBRA = new BlockPos(portalBottomRight.getX()-1, portalBottomRight.getY()+1, portalBottomRight.getZ());
        BlockPos PBLA = new BlockPos(portalBottomLeft.getX()-1, portalBottomLeft.getY()+1, portalBottomLeft.getZ());

        CP_MineBlock(PBRA);
        CP_MineBlock(PBLA);
        CP_PlaceBLock(PBRA, Blocks.OAK_PLANKS);
        CP_PlaceBLock(PBLA, Blocks.OAK_PLANKS);

        // wait for the portal to ignite
        waitingForNPortal = true;
    }

    void FindBlazes()
    {
        // set destination to a blaze spawner and wait
        BlockPos blazePos = FindBlock(Blocks.SPAWNER, 100);
        BlockPos newBlazePos = new BlockPos(blazePos.getX(), blazePos.getY() + 1, blazePos.getZ());
        getNavigator().tryMoveToXYZ(newBlazePos.getX(), newBlazePos.getY(), newBlazePos.getZ(), 0.23f);
        blazeLocation = newBlazePos;
        findingBlazes = true;
    }

    void Kill(Entity entity)
    {
        moveToKill(entity);
    }

    void moveToKill(Entity entity)
    {
        getNavigator().tryMoveToEntityLiving(entity, 0.23f); // this may not work...
    }

    @Override
    public void tick() // called every 1/20 of a second
    {
        //super.tick(); // makes sure the parent "Goal" does all it needs to do in tick TODO possibly uncomment

        if(!SPAWNEDYET) return;

        if(tryingToKill)
        {

        }

        if(waitingForNPortal)
        {
            BlockPos pos = FindBlock(Blocks.NETHER_PORTAL, 10);
            if(pos != getPosition())
            {
                waitingForNPortal = false;
                OGPortalPos = pos;
                getNavigator().tryMoveToXYZ(pos.getX(), pos.getY(), pos.getZ(), 0.23f);
                goingToPortalAfterBuilt = true;
            }
        }

        if(goingToPortalAfterBuilt)
        {
            if(getPosition() == OGPortalPos)
            {
                goingToPortalAfterBuilt = false;
                WTSAGIP = true;
            }
        }

        if(WTSAGIP)
        {
            WTSAGIP_ticks++;
            if(WTSAGIP_ticks >= 200)
            {
                WTSAGIP = false;
                FindBlazes();
            }
        }

        if(goingToWater)
        {
            if(getPosition() == WaterPos)
            {
                goingToWater = false;
                FillBucketWithWater();
            }
        }

        if(goingToLava)
        {
            if(getPosition() == LavaPos)
            {
                goingToWater = false;
                CreatePortal();
            }
        }

        if(smeltingIron) {
            ticksSinceStartedSmeltingIron++;
            if(ticksSinceStartedSmeltingIron >= 60)
            {
                smeltingIron = false;
                SI_HandleSmeltSystemAndCraftIronBucket();
            }
        }

        if(!foundTree) // updates whether or not we have found a tree
        {
            if(getPosition().equals(new BlockPos(nearestTree.getX() - 1, getPosY(), nearestTree.getZ() - 1)))
            {
                foundTree = true; // we have gotten to the tree
                tryfindtreerepeats--; // we have "repeated"
                if(tryfindtreerepeats <= 0) // if we have repeated enough...
                {
                    TryCraftOakPlanks(); // craft some oak planks
                }
                else { // otherwise...
                    TryBreakTree(); // break the tree we found
                    TryFindTree(); // find a new tree
                }
            }
        }

        if(startMining)
        {
            if(getPosition() == new BlockPos(mineStart.getX() - 1, getPosY(), mineStart.getZ() - 1))
            {
                MineInMine();
                startMining = false;
            }
        }

        if(movingtomine)
        {
            if(getPosition() == movingToMinePos)
            {
                MineInMine();
            }
        }

        if(afterMiningMove)
        {
            if(getPosition() == new BlockPos(CraftingTablePos.getX(), CraftingTablePos.getY(), CraftingTablePos.getZ() - 1))
            {
                afterMiningMove = false;
                if(mineForBlock == Blocks.COBBLESTONE)
                    TryCraftStonePickaxe();
                else if (mineForBlock == Blocks.IRON_ORE)
                    SmeltIron();
            }
        }

        CheckBlocksNearAndMine(Blocks.IRON_ORE);
        CheckBlocksNearAndMine(Blocks.DIAMOND_ORE);
    }

    void CheckBlocksNearAndMine(Block checkfordis)
    {
        BlockPos blockabove = new BlockPos(getPosX(), getPosY() +1, getPosZ());
        BlockPos blockbelow = new BlockPos(getPosX(), getPosY() -1, getPosZ());
        BlockPos blockNZ = new BlockPos(getPosX(), getPosY(), getPosZ() -1);
        BlockPos blockPZ = new BlockPos(getPosX(), getPosY(), getPosZ() +1);
        BlockPos blockNX = new BlockPos(getPosX() -1, getPosY(), getPosZ());
        BlockPos blockPX = new BlockPos(getPosX() +1, getPosY(), getPosZ());
        BlockPos blockabovetwo = new BlockPos(getPosX(), getPosY() +2, getPosZ());
        if(world.getBlockState(blockabove) == checkfordis.getDefaultState())
        {
            world.setBlockState(blockabove, Blocks.AIR.getDefaultState());
            itemStackList.add(new ItemStack(Item.getItemFromBlock(checkfordis))); // give the bot stuff
        }
        if(world.getBlockState(blockbelow) == checkfordis.getDefaultState())
        {
            world.setBlockState(blockbelow, Blocks.AIR.getDefaultState());
            itemStackList.add(new ItemStack(Item.getItemFromBlock(checkfordis))); // give the bot stuff
        }
        if(world.getBlockState(blockNZ) == checkfordis.getDefaultState())
        {
            world.setBlockState(blockNZ, Blocks.AIR.getDefaultState());
            itemStackList.add(new ItemStack(Item.getItemFromBlock(checkfordis))); // give the bot stuff
        }
        if(world.getBlockState(blockPZ) == checkfordis.getDefaultState())
        {
            world.setBlockState(blockPZ, Blocks.AIR.getDefaultState());
            itemStackList.add(new ItemStack(Item.getItemFromBlock(checkfordis))); // give the bot stuff
        }
        if(world.getBlockState(blockNX) == checkfordis.getDefaultState())
        {
            world.setBlockState(blockNX, Blocks.AIR.getDefaultState());
            itemStackList.add(new ItemStack(Item.getItemFromBlock(checkfordis))); // give the bot stuff
        }
        if(world.getBlockState(blockPX) == checkfordis.getDefaultState())
        {
            world.setBlockState(blockPX, Blocks.AIR.getDefaultState());
            itemStackList.add(new ItemStack(Item.getItemFromBlock(checkfordis))); // give the bot stuff
        }
        if(world.getBlockState(blockabovetwo) == checkfordis.getDefaultState())
        {
            world.setBlockState(blockabovetwo, Blocks.AIR.getDefaultState());
            itemStackList.add(new ItemStack(Item.getItemFromBlock(checkfordis))); // give the bot stuff
        }
    }

    BlockPos FindBlock(Block targetBlock, int radius) // finds the nearest block of a type
    {
        BlockPos closestPos = null;
        BlockPos checkPos = getPosition();

        for (int x = (int)getPosX()-radius; x < (int)getPosX()+radius; x++)
        {
            for (int y = (int)getPosY()-radius; x < (int)getPosY()+radius; y++)
            {
                for (int z = (int)getPosZ()-radius; z < (int)getPosZ()+radius; z++)
                {
                    checkPos = new BlockPos(x, y, z);
                    if (world.getBlockState(checkPos).getBlock() == targetBlock)
                    {
                        // check if it is closer than any previously found position
                        if (closestPos == null ||
                                getDistanceSq((int)getPosX() - checkPos.getX(),
                                        (int)getPosY() - checkPos.getY(),
                                        (int)getPosZ() - checkPos.getZ())
                                        < getDistanceSq((int)getPosX() - closestPos.getX(),
                                        (int)getPosY() - closestPos.getY(),
                                        (int)getPosZ() - closestPos.getZ()))
                        {
                            closestPos = checkPos;
                        }
                    }
                }
            }
        }
        if(closestPos != null)
            return closestPos;
        else
            return new BlockPos(getPosX(),getPosY(),getPosZ()); // return this random thing
    }

    void StartMine(Block thisisnewblock)
    {
        mineForBlock = thisisnewblock;
        getNavigator().tryMoveToXYZ(getPosX(), getPosY(), getPosZ() - 2, 0.23f);
        mineStart = new BlockPos(getPosX(), getPosY(), getPosZ() - 2);
        startMining = true;
    }

    void MineInMine()
    {
        movingtomine = false;
        BlockPos topblock = new BlockPos(getPosX(), getPosY() + 1, getPosZ() + 1);
        BlockPos midblock = new BlockPos(getPosX(), getPosY(), getPosZ() + 1);
        BlockPos lowblock = new BlockPos(getPosX(), getPosY() - 1, getPosZ() + 1);
        BlockState topblockOG = world.getBlockState(topblock);
        BlockState midblockOG = world.getBlockState(midblock);
        BlockState lowblockOG = world.getBlockState(lowblock);
        world.setBlockState(topblock, Blocks.AIR.getDefaultState());
        world.setBlockState(midblock, Blocks.AIR.getDefaultState());
        world.setBlockState(lowblock, Blocks.AIR.getDefaultState());
        itemStackList.add(new ItemStack(Item.getItemFromBlock(topblockOG.getBlock()))); // give the bot stuff
        itemStackList.add(new ItemStack(Item.getItemFromBlock(midblockOG.getBlock()))); // give the bot stuff
        itemStackList.add(new ItemStack(Item.getItemFromBlock(lowblockOG.getBlock()))); // give the bot stuff
        if(!HasAmountOfItem(Items.COBBLESTONE, 3) && mineForBlock == Blocks.COBBLESTONE)
        {
            getNavigator().tryMoveToXYZ(lowblock.getX(), lowblock.getY(), lowblock.getZ(), 0.23f);
            movingToMinePos = lowblock;
            movingtomine = true;
        }else if (!HasAmountOfItem(Items.IRON_ORE, 3) && mineForBlock == Blocks.IRON_ORE)
        {
            getNavigator().tryMoveToXYZ(lowblock.getX(), lowblock.getY(), lowblock.getZ(), 0.23f);
            movingToMinePos = lowblock;
            movingtomine = true;
        }
        else{
            AfterMine();
        }
    }

    void AfterMine()
    {
        getNavigator().tryMoveToXYZ(CraftingTablePos.getX(), CraftingTablePos.getY(), CraftingTablePos.getZ() - 1, 0.23f);
        afterMiningMove = true;
    }

    boolean HasAmountOfItem(Item checkForItem, int amountOfItem)
    {
        int amountfound = 0;
        for(ItemStack stacky : itemStackList){ // repeat for each item in the bot's inventory
            if(stacky.equals(new ItemStack(checkForItem))) { // if the bot has oak log
                amountfound++;
            }
        }
        if(amountfound >= amountOfItem)
            return true;
        else
            return false;
    }
}

 

 

Any help is appreciated!

Link to comment
Share on other sites

 

32 minutes ago, JJ Revans said:

    void TryFindTree() // we want to find a tree.
    {
        foundTree = false; // we have not found a tree
        nearestTree = FindBlock(Blocks.OAK_LOG, 50); // we want to find the nearest tree
        if(!nearestTree.equals(new BlockPos(getPosX(), getPosY(), getPosZ()))) // if the FindBlock doesnt give us the random thing...
            getNavigator().tryMoveToXYZ(nearestTree.getX() - 1, nearestTree.getY(), nearestTree.getZ() - 1, SPEED); // get to the tree
        else{ // otherwise...
            foundTree = true; // P.S. this is totally a lie just FYI
            TryFindTree(); // find a new tree
        }
    }

(1) Try to find a tree:
(2)  - If you fail to find a tree:

(3)  - - Goto 1

 

Plus you have a bunch of stuff that appears to indicate that you don't understand how the mob AI works, like your CreateNetherPortal method which tells the entity to find 14 lava blocks in one tick. It doesn't wait for the entity to actually DO anything, it says "find a lava blocks, place lava block, repeat 13 more times." All in the same function.

Edited by Draco18s

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

3 minutes ago, JJ Revans said:

Is the code getting stuck in an infinite loop?

Yes.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

    private int startingRadius = 9; // the radius of our block checking

void TryFindTree() // we want to find a tree.
    {
        foundTree = false; // we have not found a tree
        startingRadius+=20;
        nearestTree = FindBlock(Blocks.OAK_LOG, startingRadius); // we want to find the nearest tree
        if(!nearestTree.equals(new BlockPos(getPosX(), getPosY(), getPosZ()))) // if the FindBlock doesnt give us the random thing...
            getNavigator().tryMoveToXYZ(nearestTree.getX() - 1, nearestTree.getY(), nearestTree.getZ() - 1, SPEED); // get to the tree
        else{ // otherwise...
            foundTree = true; // P.S. this is totally a lie just FYI
            TryFindTree(); // find a new tree
        }
    }

 

 

So I changed the script to work like this and spawned the entity near a tree. The problem still occurs. I may have put another infinite loop somewhere, but I am not sure...

 

About your Mob AI statement, I may be doing this in the completely wrong way. It was just the first way of doing things that jumped to my mind. Feel free to give suggestions if I have no idea what I'm doing, and thanks for your help!

Edited by JJ Revans
Link to comment
Share on other sites

2 hours ago, JJ Revans said:

So I changed the script to work like this and spawned the entity near a tree. The problem still occurs. I may have put another infinite loop somewhere, but I am not sure...

I'm not sure either, use the debugger.

2 hours ago, JJ Revans said:

I may be doing this in the completely wrong way. It was just the first way of doing things that jumped to my mind.

Here's a hint: you need to do one thing at a time and wait for the entity to complete that thing before you have it do something else.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

20 hours ago, JJ Revans said:

I may have put another infinite loop somewhere

Yes you did in your y for loop in FindBlock.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

1 hour ago, JJ Revans said:

when I damage it it freezes like before

This sure sounds like a good place to start debugging.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Congrats! You know what function is the problem. Now debug that function.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

So I believe the problem occurs when I run

        getNavigator().tryMoveToXYZ(nearestTree.getX() - 1, nearestTree.getY(), nearestTree.getZ() - 1, SPEED); // get to the tree

Is there a different function I should use, or is there another problem?

 

should I use 

getNavigator.getPathToXYZ();
getNavigator.setPath();

?

Edited by JJ Revans
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.