Jump to content

[1.8.9] Automatically open fence gates


Raycoms

Recommended Posts

Is there a way to get custom npcs automatically go through fence gates like doors?

 

It is easy for doors where I only have to set canOpenDoors to true but there seems no easy way for fence gates.

I copied the openDoorAI classes from minecraft and simulated them for fence gates, my pathfinder also pathes my entities through the gates, but they still won't open the fence gates?

Is there some special trick?

Link to comment
Share on other sites

Highlight canOpenDoors

Right click

References

Find in Workspace

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

The classes:

 

EntityAIGateInteract

package com.minecolonies.entity.ai;

import net.minecraft.block.Block;
import net.minecraft.block.BlockFenceGate;
import net.minecraft.block.material.Material;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.pathfinding.PathEntity;
import net.minecraft.pathfinding.PathNavigateGround;
import net.minecraft.pathfinding.PathPoint;
import net.minecraft.util.BlockPos;

public class EntityAIGateInteract extends EntityAIBase {
    /**
     * Our citizen
     */
    protected EntityLiving theEntity;
    /**
     * The gate position
     */
    protected BlockPos gatePosition;
    /**
     * The gate block
     */
    protected BlockFenceGate gateBlock;
    /**
     * Check if the interaction with the fenceGate stopped already.
     */
    private boolean hasStoppedFenceInteraction;
    /**
     * The entities x and z position
     */
    private float entityPositionX;
    private float entityPositionZ;

    /**
     * Constructor called to register the AI class with an entity
     * @param entityIn the registering entity
     */
    public EntityAIGateInteract(EntityLiving entityIn) {
        this.gatePosition = BlockPos.ORIGIN;
        this.theEntity = entityIn;
        if (!(entityIn.getNavigator() instanceof PathNavigateGround)) {
            throw new IllegalArgumentException("Unsupported mob type for DoorInteractGoal");
        }
    }

    /**
     * Checks if the Interaction should be executed
     * @return true or false depending on the conditions
     */
    public boolean shouldExecute() {
        if (!this.theEntity.isCollidedHorizontally) {
            return false;
        } else {
            PathNavigateGround pathnavigateground = (PathNavigateGround) this.theEntity.getNavigator();
            PathEntity pathentity = pathnavigateground.getPath();
            if (pathentity != null && !pathentity.isFinished() && pathnavigateground.getEnterDoors()) {
                for (int i = 0; i < Math.min(pathentity.getCurrentPathIndex() + 2, pathentity.getCurrentPathLength()); ++i) {
                    PathPoint pathpoint = pathentity.getPathPointFromIndex(i);
                    this.gatePosition = new BlockPos(pathpoint.xCoord, pathpoint.yCoord + 1, pathpoint.zCoord);
                    if (this.theEntity.getDistanceSq((double) this.gatePosition.getX(), this.theEntity.posY, (double) this.gatePosition.getZ()) <= 2.25D) {
                        this.gateBlock = this.getBlockFence(this.gatePosition);
                        if (this.gateBlock != null) {
                            return true;
                        }
                    }
                }

                this.gatePosition = (new BlockPos(this.theEntity)).up();
                this.gateBlock = this.getBlockFence(this.gatePosition);
                return this.gateBlock != null;
            } else {
                return false;
            }
        }
    }

    /**
     * Checks if the execution is still ongoing
     * @return true or false
     */
    public boolean continueExecuting() {
        return !this.hasStoppedFenceInteraction;
    }

    /**
     * Starts the execution
     */
    public void startExecuting() {
        this.hasStoppedFenceInteraction = false;
        this.entityPositionX = (float) ((double) ((float) this.gatePosition.getX() + 0.5F) - this.theEntity.posX);
        this.entityPositionZ = (float) ((double) ((float) this.gatePosition.getZ() + 0.5F) - this.theEntity.posZ);
    }

    /**
     * Updates the task
     */
    public void updateTask() {
        float f = (float) ((double) ((float) this.gatePosition.getX() + 0.5F) - this.theEntity.posX);
        float f1 = (float) ((double) ((float) this.gatePosition.getZ() + 0.5F) - this.theEntity.posZ);
        float f2 = this.entityPositionX * f + this.entityPositionZ * f1;
        if (f2 < 0.0F) {
            this.hasStoppedFenceInteraction = true;
        }

    }

    /**
     * Returns a fenceBlock if available
     * @param pos the position to be searched
     * @return fenceBlock or null
     */
    private BlockFenceGate getBlockFence(BlockPos pos) {
        Block block = this.theEntity.worldObj.getBlockState(pos).getBlock();
        return block instanceof BlockFenceGate && block.getMaterial() == Material.wood ? (BlockFenceGate) block : null;
    }
}

 

EntityAIOpenFenceGate

package com.minecolonies.entity.ai;

import net.minecraft.block.BlockFenceGate;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;

public class EntityAIOpenFenceGate extends EntityAIGateInteract {

    /**
     * Checks if the gate should be closed
     */
    private boolean closeDoor;
    /**
     * Ticks until the gate should be closed
     */
    private int closeDoorTemporisation;

    /**
     * Constructor called to register the AI class with an entity
     * @param entityLivingIn the registering entity
     * @param shouldClose should the entity close the gate?
     */
    public EntityAIOpenFenceGate(EntityLiving entityLivingIn, boolean shouldClose) {
        super(entityLivingIn);
        this.theEntity = entityLivingIn;
        this.closeDoor = shouldClose;
    }

    /**
     * Should the AI continue to execute?
     * @return true or false
     */
    public boolean continueExecuting() {
        return this.closeDoor && this.closeDoorTemporisation > 0 && super.continueExecuting();
    }

    /**
     * Start the execution
     */
    public void startExecuting() {
        this.closeDoorTemporisation = 20;
        toggleDoor(true);
    }

    /**
     * Reset the action
     */
    public void resetTask() {
        if (this.closeDoor) {
            toggleDoor(false);
        }
    }

    /**
     * Toggles the door(Opens or closes)
     * @param open if open or close
     */
    private void toggleDoor(boolean open) {
        IBlockState iblockstate = this.theEntity.worldObj.getBlockState(this.gatePosition);
        if (iblockstate.getBlock() == this.gateBlock) {
            if ((iblockstate.getValue(BlockFenceGate.OPEN)) != open) {
                this.theEntity.worldObj.setBlockState(this.gatePosition, iblockstate.withProperty(BlockFenceGate.OPEN, open), 2);
                this.theEntity.worldObj.playAuxSFXAtEntity((EntityPlayer) null, open ? 1003 : 1006, this.gatePosition, 0);
            }
        }
    }

    /**
     * Updates the task.
     */
    public void updateTask() {
        --this.closeDoorTemporisation;
        super.updateTask();
    }
}

 

and how I add them:

 

   private void initTasks()
    {
        this.tasks.addTask(0, new EntityAISwimming(this));
        this.tasks.addTask(1, new EntityAICitizenAvoidEntity(this, EntityMob.class, 8.0F, 0.6D, 1.6D));
        this.tasks.addTask(2, new EntityAIGoHome(this));
        this.tasks.addTask(3, new EntityAISleep(this));
        this.tasks.addTask(4, new EntityAIOpenDoor(this, true));
        this.tasks.addTask(4, new EntityAIOpenFenceGate(this, true));
        this.tasks.addTask(5, new EntityAIWatchClosest2(this, EntityPlayer.class, 3.0F, 1.0F));
        this.tasks.addTask(6, new EntityAIWatchClosest2(this, EntityCitizen.class, 5.0F, 0.02F));
        this.tasks.addTask(7, new EntityAICitizenWander(this, 0.6D));
        this.tasks.addTask(8, new EntityAIWatchClosest(this, EntityLiving.class, 6.0F));
        onJobChanged(getColonyJob());
    }

Link to comment
Share on other sites

Thanks, now the EntityAIGateInteraction is being called.

 

But strangely he still will not open the fence gate. He stuck at shouldExecute because the pathEntity is null which is strange.

I marked the fenceGate as a passable block and the pathfinder clearly is trying to path the NPC through it.

It works perfectly for doors but doesn't for fenceGates...

 

/**
     * Checks if the Interaction should be executed
     * @return true or false depending on the conditions
     */
    public boolean shouldExecute()
    {
        if (!this.theEntity.isCollidedHorizontally)
        {
            return false;
        }
        else
        {
            PathNavigateGround pathnavigateground = (PathNavigateGround) this.theEntity.getNavigator();
            PathEntity pathentity = pathnavigateground.getPath();
            if (pathentity != null && !pathentity.isFinished()  && pathnavigateground.getEnterDoors())
            {
                for (int i = 0; i < Math.min(pathentity.getCurrentPathIndex() + 2, pathentity.getCurrentPathLength()); ++i)
                {
                    PathPoint pathpoint = pathentity.getPathPointFromIndex(i);
                    this.gatePosition = new BlockPos(pathpoint.xCoord, pathpoint.yCoord + 1, pathpoint.zCoord);
                    if (this.theEntity.getDistanceSq((double) this.gatePosition.getX(), this.theEntity.posY, (double) this.gatePosition.getZ()) <= 2.25D)
                    {
                        this.gateBlock = this.getBlockFence(this.gatePosition);
                        if (this.gateBlock != null)
                        {
                            return true;
                        }
                    }
                }

                this.gatePosition = (new BlockPos(this.theEntity)).up();
                this.gateBlock = this.getBlockFence(this.gatePosition);
                return this.gateBlock != null;
            } else {
                return false;
            }
        }
    }

Link to comment
Share on other sites

You have this:

this.gatePosition = new BlockPos(pathpoint.xCoord, pathpoint.yCoord + 1, pathpoint.zCoord);

 

You could add:

this.gatePosition = new BlockPos(pathpoint.xCoord, pathpoint.yCoord + 0, pathpoint.zCoord);

 

as a separate check.

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

Thank you it is working a bit better now.

 

But still. He only passes sometimes.

 

PathNavigateGround pathnavigateground = (PathNavigateGround) this.theEntity.getNavigator();
            PathEntity pathentity = pathnavigateground.getPath();
            if (pathentity != null && !pathentity.isFinished()  && pathnavigateground.getEnterDoors())
            {

 

Is stopping the execution mostly because pathentity is almost everytime null.

Link to comment
Share on other sites

Is the pathEntity set and then wiped, or is it never set? If wiped, then what's wiping it?

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

And the winner is....

 

private BlockFenceGate getBlockFence(BlockPos pos)
    {
        Block block  = this.theEntity.worldObj.getBlockState(pos.up()).getBlock();
        return (block instanceof BlockFenceGate && block.getMaterial() == Material.wood ? (BlockFenceGate) block : null);
    }

 

Edit it's not about the hovering. The problem is that the entity already stands inside the gate position and therefore tries to path one more to the front. But at this point there is no gate anymore so the above check returns null.

Link to comment
Share on other sites

The solution:

 

    /**
     * Returns a fenceBlock if available
     * @param pos the position to be searched
     * @return fenceBlock or null
     */
    private BlockFenceGate getBlockFence(BlockPos pos)
    {
        Block block  = this.theEntity.worldObj.getBlockState(pos).getBlock();
        if(!(block instanceof BlockFenceGate && block.getMaterial() == Material.wood))
        {
            block = this.theEntity.worldObj.getBlockState(this.theEntity.getPosition()).getBlock();
            gatePosition = this.theEntity.getPosition();
        }
        return (block instanceof BlockFenceGate && block.getMaterial() == Material.wood ? (BlockFenceGate) block : null);
    }

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.



×
×
  • Create New...

Important Information

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