Jump to content

[1.14.4] Make villagers / zombies recognize new type of door?


Kenneth201998

Recommended Posts

3 hours ago, diesieben07 said:

AI checks for instanceof DoorBlock to check if something is a door. It checks for Material.WOOD to determine if it can be opened (iron doors can only be used with redstone).

DoorBlock#toggleDoor is called on your block to open / close the door. The property DoorBlock.OPEN is checked to determine if a door is open or closed.

Here is the code for my door:

package com.kenneth.hard_survival_craft.objects.blocks.construction;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.DoorBlock;
import net.minecraft.block.HorizontalBlock;
import net.minecraft.block.material.Material;
import net.minecraft.block.material.PushReaction;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.item.ItemStack;
import net.minecraft.pathfinding.PathType;
import net.minecraft.state.BooleanProperty;
import net.minecraft.state.DirectionProperty;
import net.minecraft.state.EnumProperty;
import net.minecraft.state.StateContainer.Builder;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.state.properties.DoorHingeSide;
import net.minecraft.state.properties.DoubleBlockHalf;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.Mirror;
import net.minecraft.util.Rotation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
import net.minecraft.world.IWorldReader;
import net.minecraft.world.World;

public class CabinDoor extends DoorBlock {
	public static final DirectionProperty FACING = HorizontalBlock.HORIZONTAL_FACING;
	public static final BooleanProperty OPEN = BlockStateProperties.OPEN;
	public static final EnumProperty<DoorHingeSide> HINGE = BlockStateProperties.DOOR_HINGE;
	public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
	public static final EnumProperty<DoubleBlockHalf> HALF = BlockStateProperties.DOUBLE_BLOCK_HALF;
	
	protected static final VoxelShape default_shape = Block.makeCuboidShape(0, 0, 7, 16, 16, 9);
	
	protected static final VoxelShape north_south = Block.makeCuboidShape(0, 0, 7, 16, 16, 9);
	protected static final VoxelShape east_west = Block.makeCuboidShape(7, 0, 0, 9, 16, 16);
	
	protected static final VoxelShape south_l = Block.makeCuboidShape(13, 0, 7, 16, 16, 21);
	protected static final VoxelShape south_r = Block.makeCuboidShape(0, 0, 7, 3, 16, 21);
	
	protected static final VoxelShape north_l = Block.makeCuboidShape(0, 0, 9, 3, 16, -5);
	protected static final VoxelShape north_r = Block.makeCuboidShape(13, 0, 9, 16, 16, -5);
	
	protected static final VoxelShape east_l = Block.makeCuboidShape(7, 0, 0, 21, 16, 3);
	protected static final VoxelShape east_r = Block.makeCuboidShape(7, 0, 13, 21, 16, 16);
	
	protected static final VoxelShape west_l = Block.makeCuboidShape(9, 0, 13, -5, 16, 16);
	protected static final VoxelShape west_r = Block.makeCuboidShape(9, 0, 0, -5, 16, 3);
	
	public CabinDoor(Properties properties) {
		super(properties);
		this.setDefaultState(this.stateContainer.getBaseState().with(OPEN, Boolean.valueOf(false)));
	}
	
	@Override
	public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
		Direction dir = state.get(FACING);
		DoorHingeSide side = state.get(HINGE);
		Boolean open = state.get(OPEN);
		
		if((dir == Direction.NORTH || dir == Direction.SOUTH) && open == false) {
			return north_south;
		} else if ((dir == Direction.EAST || dir == Direction.WEST) && open == false) {
			return east_west;
		} else if(dir == Direction.NORTH && side == DoorHingeSide.LEFT) {
			return north_l;
		} else if (dir == Direction.NORTH && side == DoorHingeSide.RIGHT) {
			return north_r;
		} else if(dir == Direction.SOUTH && side == DoorHingeSide.LEFT) {
			return south_l;
		} else if (dir == Direction.SOUTH && side == DoorHingeSide.RIGHT) {
			return south_r;
		} else if (dir == Direction.EAST && side == DoorHingeSide.LEFT) {
			return east_l;
		} else if (dir == Direction.EAST && side == DoorHingeSide.RIGHT) {
			return east_r;
		} else if (dir == Direction.WEST && side == DoorHingeSide.LEFT) {
			return west_l;
		} else if (dir == Direction.WEST && side == DoorHingeSide.RIGHT) {
			return west_r;
		} else {
			return default_shape;
		}
	}
	
	@Override
	public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld worldIn, BlockPos currentPos, BlockPos facingPos) {
		DoubleBlockHalf doubleblockhalf = stateIn.get(HALF);
		if (facing.getAxis() == Direction.Axis.Y && doubleblockhalf == DoubleBlockHalf.LOWER == (facing == Direction.UP)) {
			return facingState.getBlock() == this && facingState.get(HALF) != doubleblockhalf ? stateIn.with(FACING, facingState.get(FACING)).with(OPEN, facingState.get(OPEN)).with(HINGE, facingState.get(HINGE)).with(POWERED, facingState.get(POWERED)) : Blocks.AIR.getDefaultState();
		} else {
			return doubleblockhalf == DoubleBlockHalf.LOWER && facing == Direction.DOWN && !stateIn.isValidPosition(worldIn, currentPos) ? Blocks.AIR.getDefaultState() : super.updatePostPlacement(stateIn, facing, facingState, worldIn, currentPos, facingPos);
		}
	}
	@Override
	public void harvestBlock(World worldIn, PlayerEntity player, BlockPos pos, BlockState state, TileEntity te, ItemStack stack) {
		super.harvestBlock(worldIn, player, pos, Blocks.AIR.getDefaultState(), te, stack);
	}
	@Override
	public void onBlockHarvested(World worldIn, BlockPos pos, BlockState state, PlayerEntity player) {
		DoubleBlockHalf doubleblockhalf = state.get(HALF);
		BlockPos blockpos = doubleblockhalf == DoubleBlockHalf.LOWER ? pos.up() : pos.down();
		BlockState blockstate = worldIn.getBlockState(blockpos);
		if (blockstate.getBlock() == this && blockstate.get(HALF) != doubleblockhalf) {
	         worldIn.setBlockState(blockpos, Blocks.AIR.getDefaultState(), 35);
	         worldIn.playEvent(player, 2001, blockpos, Block.getStateId(blockstate));
	         ItemStack itemstack = player.getHeldItemMainhand();
	         if (!worldIn.isRemote && !player.isCreative()) {
	        	 Block.spawnDrops(state, worldIn, pos, (TileEntity)null, player, itemstack);
	        	 Block.spawnDrops(blockstate, worldIn, blockpos, (TileEntity)null, player, itemstack);
	         }
		}
		super.onBlockHarvested(worldIn, pos, state, player);
	}
	@Override
	public boolean allowsMovement(BlockState state, IBlockReader worldIn, BlockPos pos, PathType type) {
		switch(type) {
		case LAND:
			return state.get(OPEN);
		case WATER:
			return false;
		case AIR:
			return state.get(OPEN);
		default:
			return false;
		}
	}
	private int getCloseSound() {
		return this.material == Material.IRON ? 1011 : 1012;
	}
	
	private int getOpenSound() {
		return this.material == Material.IRON ? 1005 : 1006;
	}
	@Override
	public BlockState getStateForPlacement(BlockItemUseContext context) {
		BlockPos blockpos = context.getPos();
		if (blockpos.getY() < 255 && context.getWorld().getBlockState(blockpos.up()).isReplaceable(context)) {
			World world = context.getWorld();
			boolean flag = world.isBlockPowered(blockpos) || world.isBlockPowered(blockpos.up());
			return this.getDefaultState().with(FACING, context.getPlacementHorizontalFacing()).with(HINGE, this.getHingeSide(context)).with(POWERED, Boolean.valueOf(flag)).with(OPEN, Boolean.valueOf(flag)).with(HALF, DoubleBlockHalf.LOWER);
		} else {
			return null;
		}
	}
	@Override
	public void onBlockPlacedBy(World worldIn, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
		worldIn.setBlockState(pos.up(), state.with(HALF, DoubleBlockHalf.UPPER), 3);
	}
	private DoorHingeSide getHingeSide(BlockItemUseContext p_208073_1_) {
		IBlockReader iblockreader = p_208073_1_.getWorld();
		BlockPos blockpos = p_208073_1_.getPos();
		Direction direction = p_208073_1_.getPlacementHorizontalFacing();
		BlockPos blockpos1 = blockpos.up();
		Direction direction1 = direction.rotateYCCW();
		BlockPos blockpos2 = blockpos.offset(direction1);
		BlockState blockstate = iblockreader.getBlockState(blockpos2);
		BlockPos blockpos3 = blockpos1.offset(direction1);
		BlockState blockstate1 = iblockreader.getBlockState(blockpos3);
		Direction direction2 = direction.rotateY();
		BlockPos blockpos4 = blockpos.offset(direction2);
		BlockState blockstate2 = iblockreader.getBlockState(blockpos4);
		BlockPos blockpos5 = blockpos1.offset(direction2);
		BlockState blockstate3 = iblockreader.getBlockState(blockpos5);
		int i = (blockstate.func_224756_o(iblockreader, blockpos2) ? -1 : 0) + (blockstate1.func_224756_o(iblockreader, blockpos3) ? -1 : 0) + (blockstate2.func_224756_o(iblockreader, blockpos4) ? 1 : 0) + (blockstate3.func_224756_o(iblockreader, blockpos5) ? 1 : 0);
		boolean flag = blockstate.getBlock() == this && blockstate.get(HALF) == DoubleBlockHalf.LOWER;
		boolean flag1 = blockstate2.getBlock() == this && blockstate2.get(HALF) == DoubleBlockHalf.LOWER;
		if ((!flag || flag1) && i <= 0) {
			if ((!flag1 || flag) && i >= 0) {
				int j = direction.getXOffset();
	            int k = direction.getZOffset();
	            Vec3d vec3d = p_208073_1_.getHitVec();
	            double d0 = vec3d.x - (double)blockpos.getX();
	            double d1 = vec3d.z - (double)blockpos.getZ();
	            return (j >= 0 || !(d1 < 0.5D)) && (j <= 0 || !(d1 > 0.5D)) && (k >= 0 || !(d0 > 0.5D)) && (k <= 0 || !(d0 < 0.5D)) ? DoorHingeSide.LEFT : DoorHingeSide.RIGHT;
			} else {
				return DoorHingeSide.LEFT;
			}
		} else {
			return DoorHingeSide.RIGHT;
		}
	}
	@Override
	public boolean onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
		if (this.material == Material.IRON) {
			return false;
		} else {
			state = state.cycle(OPEN);
			worldIn.setBlockState(pos, state, 10);
			worldIn.playEvent(player, state.get(OPEN) ? this.getOpenSound() : this.getCloseSound(), pos, 0);
			return true;
		}
	}
	@Override
	public void toggleDoor(World worldIn, BlockPos pos, boolean open) {
		BlockState blockstate = worldIn.getBlockState(pos);
		if (blockstate.getBlock() == this && blockstate.get(OPEN) != open) {
			worldIn.setBlockState(pos, blockstate.with(OPEN, Boolean.valueOf(open)), 10);
			this.playSound(worldIn, pos, open);
		}
	}
	@Override
	public void neighborChanged(BlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos, boolean isMoving) {
		boolean flag = worldIn.isBlockPowered(pos) || worldIn.isBlockPowered(pos.offset(state.get(HALF) == DoubleBlockHalf.LOWER ? Direction.UP : Direction.DOWN));
		if (blockIn != this && flag != state.get(POWERED)) {
			if (flag != state.get(OPEN)) {
				this.playSound(worldIn, pos, flag);
			}
			worldIn.setBlockState(pos, state.with(POWERED, Boolean.valueOf(flag)).with(OPEN, Boolean.valueOf(flag)), 2);
		}
	}
	@Override
	public boolean isValidPosition(BlockState state, IWorldReader worldIn, BlockPos pos) {
		BlockPos blockpos = pos.down();
		BlockState blockstate = worldIn.getBlockState(blockpos);
		if (state.get(HALF) == DoubleBlockHalf.LOWER) {
			return blockstate.func_224755_d(worldIn, blockpos, Direction.UP);
		} else {
			return blockstate.getBlock() == this;
		}
	}
	private void playSound(World worldIn, BlockPos pos, boolean open) {
		worldIn.playEvent((PlayerEntity)null, open ? this.getOpenSound() : this.getCloseSound(), pos, 0);
	}
	@Override
	public PushReaction getPushReaction(BlockState state) {
		return PushReaction.DESTROY;
	}
	@Override
	public BlockState rotate(BlockState state, Rotation rot) {
		return state.with(FACING, rot.rotate(state.get(FACING)));
	}
	@Override
	public BlockState mirror(BlockState state, Mirror mirrorIn) {
		return mirrorIn == Mirror.NONE ? state : state.rotate(mirrorIn.toRotation(state.get(FACING))).cycle(HINGE);
	}
	@Override
	public long getPositionRandom(BlockState state, BlockPos pos) {
		return MathHelper.getCoordinateRandom(pos.getX(), pos.down(state.get(HALF) == DoubleBlockHalf.LOWER ? 0 : 1).getY(), pos.getZ());
	}
	@Override
	public boolean isSolid(BlockState state) {
		return false;
	}
	@Override
	protected void fillStateContainer(Builder<Block, BlockState> builder) {
		builder.add(HALF, FACING, OPEN, HINGE, POWERED);
	}
}

They still don't interact with it. Remember the door goes through the middle of the block instead of at the edge.

 

Refer to my mod's images to see what the door looks like:

 

https://www.curseforge.com/minecraft/mc-mods/kenneths-log-cabin-blocks

 

EDIT:

 

The villagers crowd around the door but can't open it

 

 

2019-12-02_08.36.27.png

Edited by Kenneth201998
Link to comment
Share on other sites

Ok so I have tried:

package com.kenneth.hard_survival_craft.objects.new_ai.join_handlers;

import com.kenneth.hard_survival_craft.KenmodMain;
import com.kenneth.hard_survival_craft.objects.new_ai.goal.CustomDoorGoal;

import net.minecraft.entity.merchant.villager.AbstractVillagerEntity;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;

public class CustomVillagerJoinHandler {
	@SubscribeEvent
	public void onJoinWorld(EntityJoinWorldEvent evt) {
		if(evt.getEntity() instanceof AbstractVillagerEntity) {
			AbstractVillagerEntity ent = (AbstractVillagerEntity)evt.getEntity();
			CustomDoorGoal test = new CustomDoorGoal(ent);
			ent.goalSelector.addGoal(1, test);
			KenmodMain.debugString("TEST 001");
		}
	}
}

And:

package com.kenneth.hard_survival_craft.objects.new_ai.goal;

import com.kenneth.hard_survival_craft.objects.blocks.construction.CabinDoor;

import net.minecraft.block.BlockState;
import net.minecraft.block.DoorBlock;
import net.minecraft.entity.MobEntity;
import net.minecraft.entity.ai.goal.InteractDoorGoal;
import net.minecraft.pathfinding.GroundPathNavigator;
import net.minecraft.util.math.BlockPos;

public class CustomDoorGoal extends InteractDoorGoal{
	
	protected MobEntity entity;
	protected BlockPos doorPosition = BlockPos.ZERO;
	protected boolean doorInteract;
	
	public CustomDoorGoal(MobEntity entityIn) {
		super(entityIn);
		this.entity = entityIn;
		if (!(entityIn.getNavigator() instanceof GroundPathNavigator)) {
			throw new IllegalArgumentException("Unsupported mob type for DoorInteractGoal");
		}
	}
	
	@Override
	protected void toggleDoor(boolean open) {
		if (this.doorInteract) {
			BlockState blockstate = this.entity.world.getBlockState(this.doorPosition);
			if (blockstate.getBlock() instanceof DoorBlock) {
				((DoorBlock)blockstate.getBlock()).toggleDoor(this.entity.world, this.doorPosition, open);
			} else if (blockstate.getBlock() instanceof CabinDoor){
				((CabinDoor)blockstate.getBlock()).toggleDoor(this.entity.world, this.doorPosition, open);
			}
		}
	}
}

And:

	private void clientRegistries (final FMLClientSetupEvent event) {
		
		MinecraftForge.EVENT_BUS.register(new CustomVillagerJoinHandler());
		debugString("Client registries method registered...");
		
	}

But still they don't open the doors.

Link to comment
Share on other sites

4 hours ago, diesieben07 said:

The part where they check if they have collided with a door.

Ok so I have tried moving some things from the vanilla code and got this:

package com.kenneth.hard_survival_craft.objects.new_ai.goal;

import com.kenneth.hard_survival_craft.objects.blocks.construction.CabinDoor;

import net.minecraft.block.BlockState;
import net.minecraft.block.DoorBlock;
import net.minecraft.block.material.Material;
import net.minecraft.entity.MobEntity;
import net.minecraft.entity.ai.goal.InteractDoorGoal;
import net.minecraft.pathfinding.GroundPathNavigator;
import net.minecraft.pathfinding.Path;
import net.minecraft.pathfinding.PathPoint;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class CustomDoorGoal extends InteractDoorGoal{
	
	protected MobEntity entity;
	protected BlockPos doorPosition = BlockPos.ZERO;
	protected boolean doorInteract;
	private boolean hasStoppedDoorInteraction;
	private float entityPositionX;
	private float entityPositionZ;
	   
	public CustomDoorGoal(MobEntity entityIn) {
		super(entityIn);
		this.entity = entityIn;
		if (!(entityIn.getNavigator() instanceof GroundPathNavigator)) {
			throw new IllegalArgumentException("Unsupported mob type for DoorInteractGoal");
		}
	}
	@Override
	public boolean shouldExecute() {
		if(!this.entity.collidedHorizontally) {
			return false;
		} else {
			GroundPathNavigator groundPathNavigator = (GroundPathNavigator)this.entity.getNavigator();
			Path path = groundPathNavigator.getPath();
			if(path != null && !path.isFinished() && groundPathNavigator.getEnterDoors()) {
				for(int i = 0; i < Math.min(path.getCurrentPathIndex() + 2, path.getCurrentPathLength()); ++i) {
					PathPoint pathpoint = path.getPathPointFromIndex(i);
					this.doorPosition = new BlockPos(pathpoint.x, pathpoint.y + 1, pathpoint.z);
					if (!(this.entity.getDistanceSq((double)this.doorPosition.getX(), this.entity.posY, (double)this.doorPosition.getZ()) > 2.25D)) {
						this.doorInteract = func_220695_a(this.entity.world, this.doorPosition);
						if (this.doorInteract) {
							return true;
						}
					}
				}
				this.doorPosition = (new BlockPos(this.entity)).up();
	            this.doorInteract = func_220695_a(this.entity.world, this.doorPosition);
	            return this.doorInteract;
			} else {
				return false;
			}
		}
	}
	@Override
	public boolean shouldContinueExecuting() {
		return !this.hasStoppedDoorInteraction;
	}
	@Override
	public void startExecuting() {
		this.hasStoppedDoorInteraction = false;
		this.entityPositionX = (float)((double)((float)this.doorPosition.getX() + 0.5F) - this.entity.posX);
		this.entityPositionZ = (float)((double)((float)this.doorPosition.getZ() + 0.5F) - this.entity.posZ);
	}
	@Override
	protected void toggleDoor(boolean open) {
		if (this.doorInteract) {
			BlockState blockstate = this.entity.world.getBlockState(this.doorPosition);
			if (blockstate.getBlock() instanceof DoorBlock) {
				((DoorBlock)blockstate.getBlock()).toggleDoor(this.entity.world, this.doorPosition, open);
			} else if (blockstate.getBlock() instanceof CabinDoor){
				((CabinDoor)blockstate.getBlock()).toggleDoor(this.entity.world, this.doorPosition, open);
			}
		}
	}
	@Override
	public void tick() {
		float f = (float)((double)((float)this.doorPosition.getX() + 0.5F) - this.entity.posX);
		float f1 = (float)((double)((float)this.doorPosition.getZ() + 0.5F) - this.entity.posZ);
		float f2 = this.entityPositionX * f + this.entityPositionZ * f1;
		if (f2 < 0.0F) {
			this.hasStoppedDoorInteraction = true;
		}
	}
	public static boolean func_220695_a(World p_220695_0_, BlockPos p_220695_1_) {
		BlockState blockstate = p_220695_0_.getBlockState(p_220695_1_);
		return (blockstate.getBlock() instanceof DoorBlock || blockstate.getBlock() instanceof CabinDoor) && blockstate.getMaterial() == Material.WOOD;
	}
}

They still don't open the door.

 

 

EDIT:

 

package com.kenneth.hard_survival_craft.objects.new_ai.goal;

import com.kenneth.hard_survival_craft.objects.blocks.construction.CabinDoor;

import net.minecraft.block.BlockState;
import net.minecraft.block.DoorBlock;
import net.minecraft.block.material.Material;
import net.minecraft.entity.MobEntity;
import net.minecraft.entity.ai.goal.OpenDoorGoal;
import net.minecraft.pathfinding.GroundPathNavigator;
import net.minecraft.pathfinding.Path;
import net.minecraft.pathfinding.PathPoint;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class CustomDoorGoal extends OpenDoorGoal {
	
	protected MobEntity entity;
	protected BlockPos doorPosition = BlockPos.ZERO;
	protected boolean doorInteract;
	private int closeDoorTemporisation;
	private final boolean closeDoor;
	   
	public CustomDoorGoal(MobEntity entityIn, boolean shouldClose) {
		super(entityIn, shouldClose);
		this.entity = entityIn;
		this.closeDoor = shouldClose;
		if (!(entityIn.getNavigator() instanceof GroundPathNavigator)) {
			throw new IllegalArgumentException("Unsupported mob type for DoorInteractGoal");
		}
	}
	@Override
	public boolean shouldExecute() {
		if(!this.entity.collidedHorizontally) {
			return false;
		} else {
			GroundPathNavigator groundPathNavigator = (GroundPathNavigator)this.entity.getNavigator();
			Path path = groundPathNavigator.getPath();
			if(path != null && !path.isFinished() && groundPathNavigator.getEnterDoors()) {
				for(int i = 0; i < Math.min(path.getCurrentPathIndex() + 2, path.getCurrentPathLength()); ++i) {
					PathPoint pathpoint = path.getPathPointFromIndex(i);
					this.doorPosition = new BlockPos(pathpoint.x, pathpoint.y + 1, pathpoint.z);
					if (!(this.entity.getDistanceSq((double)this.doorPosition.getX(), this.entity.posY, (double)this.doorPosition.getZ()) > 2.25D)) {
						this.doorInteract = func_220695_a(this.entity.world, this.doorPosition);
						if (this.doorInteract) {
							return true;
						}
					}
				}
				this.doorPosition = (new BlockPos(this.entity)).up();
	            this.doorInteract = func_220695_a(this.entity.world, this.doorPosition);
	            return this.doorInteract;
			} else {
				return false;
			}
		}
	}
	@Override
	public boolean shouldContinueExecuting() {
		return this.closeDoor && this.closeDoorTemporisation > 0 && super.shouldContinueExecuting();
	}
	@Override
	public void startExecuting() {
		this.closeDoorTemporisation = 60;
		this.toggleDoor(true);
	}
	@Override
	protected void toggleDoor(boolean open) {
		if (this.doorInteract) {
			BlockState blockstate = this.entity.world.getBlockState(this.doorPosition);
			if (blockstate.getBlock() instanceof DoorBlock) {
				((DoorBlock)blockstate.getBlock()).toggleDoor(this.entity.world, this.doorPosition, open);
			} else if (blockstate.getBlock() instanceof CabinDoor){
				((CabinDoor)blockstate.getBlock()).toggleDoor(this.entity.world, this.doorPosition, open);
			}
		}
	}
	@Override
	public void resetTask() {
		this.toggleDoor(false);
	}
	@Override
	public void tick() {
		--this.closeDoorTemporisation;
		super.tick();
	}
	public static boolean func_220695_a(World p_220695_0_, BlockPos p_220695_1_) {
		BlockState blockstate = p_220695_0_.getBlockState(p_220695_1_);
		return (blockstate.getBlock() instanceof DoorBlock || blockstate.getBlock() instanceof CabinDoor) && blockstate.getMaterial() == Material.WOOD;
	}
}

 

They can now open doors but the door closes almost instantly upon opening. Its like the door opens then 1 tick later it closes. How to fix?

Edited by Kenneth201998
Link to comment
Share on other sites

8 minutes ago, diesieben07 said:

Okay, then check why.

I noticed this:

	@Override
	public void tick() {
		if(this.closeDoorTemporisation > 0) {
			--this.closeDoorTemporisation;
			KenmodMain.debugString("Test 1");
		}
		super.tick();
	}

Kenmod.debugString("Test 1");

is only called once.

And there is this:

	@Override
	public void startExecuting() {
		this.closeDoorTemporisation = 60;
		KenmodMain.debugString("Test 0");
		this.toggleDoor(true);
	}

Which is meant to set closeDoorTemporisation to 60.

Does this mean that tick() is not being called properly?

Link to comment
Share on other sites

Fixed it:

package com.kenneth.hard_survival_craft.objects.new_ai.goal;

import com.kenneth.hard_survival_craft.KenmodMain;
import com.kenneth.hard_survival_craft.objects.blocks.construction.CabinDoor;

import net.minecraft.block.BlockState;
import net.minecraft.block.DoorBlock;
import net.minecraft.block.material.Material;
import net.minecraft.entity.MobEntity;
import net.minecraft.entity.ai.goal.OpenDoorGoal;
import net.minecraft.pathfinding.GroundPathNavigator;
import net.minecraft.pathfinding.Path;
import net.minecraft.pathfinding.PathPoint;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class CustomDoorGoal extends OpenDoorGoal {
	
	protected MobEntity entity;
	protected BlockPos doorPosition = BlockPos.ZERO;
	protected boolean doorInteract;
	protected boolean opened;
	private int closeDoorTemporisation;
	private final boolean closeDoor;
	   
	public CustomDoorGoal(MobEntity entityIn, boolean shouldClose) {
		super(entityIn, shouldClose);
		this.entity = entityIn;
		this.closeDoor = shouldClose;
		if (!(entityIn.getNavigator() instanceof GroundPathNavigator)) {
			throw new IllegalArgumentException("Unsupported mob type for DoorInteractGoal");
		}
	}
	@Override
	public boolean shouldExecute() {
		if(!this.entity.collidedHorizontally) {
			return false;
		} else {
			GroundPathNavigator groundPathNavigator = (GroundPathNavigator)this.entity.getNavigator();
			Path path = groundPathNavigator.getPath();
			if(path != null && !path.isFinished() && groundPathNavigator.getEnterDoors()) {
				for(int i = 0; i < Math.min(path.getCurrentPathIndex() + 2, path.getCurrentPathLength()); ++i) {
					PathPoint pathpoint = path.getPathPointFromIndex(i);
					this.doorPosition = new BlockPos(pathpoint.x, pathpoint.y + 1, pathpoint.z);
					if (!(this.entity.getDistanceSq((double)this.doorPosition.getX(), this.entity.posY, (double)this.doorPosition.getZ()) > 2.25D)) {
						this.doorInteract = func_220695_a(this.entity.world, this.doorPosition);
						if (this.doorInteract) {
							return true;
						}
					}
				}
				this.doorPosition = (new BlockPos(this.entity)).up();
	            this.doorInteract = func_220695_a(this.entity.world, this.doorPosition);
	            return this.doorInteract;
			} else {
				return false;
			}
		}
	}
	@Override
	public boolean shouldContinueExecuting() {
		boolean test = this.closeDoor && this.closeDoorTemporisation > 0;
		KenmodMain.debugString("Should Continue: " + test);
		return test;
	}
	@Override
	public void startExecuting() {
		this.closeDoorTemporisation = 60;
		KenmodMain.debugString("Test 0");
		this.toggleDoor(true);
	}
	@Override
	protected void toggleDoor(boolean open) {
		if (this.doorInteract) {
			BlockState blockstate = this.entity.world.getBlockState(this.doorPosition);
			if (blockstate.getBlock() instanceof DoorBlock) {
				((DoorBlock)blockstate.getBlock()).toggleDoor(this.entity.world, this.doorPosition, open);
			} else if (blockstate.getBlock() instanceof CabinDoor){
				((CabinDoor)blockstate.getBlock()).toggleDoor(this.entity.world, this.doorPosition, open);
			}
		}
	}
	@Override
	public void tick() {
		if(this.closeDoorTemporisation > 0) {
			--this.closeDoorTemporisation;
			KenmodMain.debugString("Test 1");
		}
		KenmodMain.debugString("Test 2");
		super.tick();
	}
	@Override
	public void resetTask() {
		this.toggleDoor(false);
	}
	public static boolean func_220695_a(World p_220695_0_, BlockPos p_220695_1_) {
		BlockState blockstate = p_220695_0_.getBlockState(p_220695_1_);
		return (blockstate.getBlock() instanceof DoorBlock || blockstate.getBlock() instanceof CabinDoor) && blockstate.getMaterial() == Material.WOOD;
	}
}

Change:

	@Override
	public boolean shouldContinueExecuting() {
		return this.closeDoor && this.closeDoorTemporisation > 0 && super.shouldContinueExecuting();
	}

To:

	@Override
	public boolean shouldContinueExecuting() {
		return this.closeDoor && this.closeDoorTemporisation > 0;
	}

 

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.