Door placement shenanigans

-Enabled Door opening and closing sounds (BlockDimDoorBase L66)
-Repaired Double placement of doors and Doors swinging open upon
placement (ItemDoorBase L78)
This commit is contained in:
Mathijs Riezebos 2017-05-14 16:11:33 +02:00
parent c29c9b3d23
commit 4f711e5e3f
3 changed files with 32 additions and 20 deletions

View file

@ -63,6 +63,7 @@ public abstract class BlockDimDoorBase extends BlockDoor implements IDimDoor, IT
state = state.cycleProperty(BlockDoor.OPEN); state = state.cycleProperty(BlockDoor.OPEN);
world.setBlockState(pos, state, 2); world.setBlockState(pos, state, 2);
world.markBlockRangeForRenderUpdate(pos, pos.up()); world.markBlockRangeForRenderUpdate(pos, pos.up());
world.playEvent(player, (state.getValue(OPEN)) ? this.getOpenSound() : this.getCloseSound(), pos, 0);
return true; return true;
} }
} }
@ -178,16 +179,26 @@ public abstract class BlockDimDoorBase extends BlockDoor implements IDimDoor, IT
IBlockState groundState = worldIn.getBlockState(pos.down()); IBlockState groundState = worldIn.getBlockState(pos.down());
IBlockState bottomState = worldIn.getBlockState(pos); IBlockState bottomState = worldIn.getBlockState(pos);
IBlockState topState = worldIn.getBlockState(pos.up()); IBlockState topState = worldIn.getBlockState(pos.up());
return pos.up().getY() > worldIn.getHeight() - 1 || pos.getY() < 1 ? false //top half can never be placed above buildHeight (255), (worldIn.getHeight() should return 256) and bottom half can never be placed below y=1 return !(pos.up().getY() > worldIn.getHeight() - 1 || pos.getY() < 1) //top half can never be placed above buildHeight (255), (worldIn.getHeight() should return 256) and bottom half can never be placed below y=1
: groundState.isSideSolid(worldIn, pos.down(), EnumFacing.UP) && groundState.isSideSolid(worldIn, pos.down(), EnumFacing.UP)
&& canPlaceBottomAt(worldIn, pos, bottomState) && canPlaceTopAt(worldIn, pos, topState); && canPlaceBottomAt(bottomState) && canPlaceTopAt(topState);
} }
private boolean canPlaceBottomAt(World worldIn, BlockPos pos, IBlockState state) { private boolean canPlaceBottomAt(IBlockState state) {
return (state.equals(Blocks.AIR) || state.getBlock().isReplaceable(worldIn, pos)); return (state.equals(Blocks.AIR) || state.getMaterial().isReplaceable());
} }
private boolean canPlaceTopAt(World worldIn, BlockPos pos, IBlockState state) { private boolean canPlaceTopAt(IBlockState state) {
return (state.getBlock() == ModBlocks.blockRift || state.equals(Blocks.AIR) || state.getMaterial().isReplaceable()); return (state.getBlock() == ModBlocks.blockRift || state.equals(Blocks.AIR) || state.getMaterial().isReplaceable());
} }
protected int getCloseSound()
{
return this.blockMaterial == Material.IRON ? 1011 : 1012;
}
protected int getOpenSound()
{
return this.blockMaterial == Material.IRON ? 1005 : 1006;
}
} }

View file

@ -10,7 +10,6 @@ import com.zixiken.dimdoors.shared.tileentities.TileEntityDimDoor;
import com.zixiken.dimdoors.shared.tileentities.TileEntityRift; import com.zixiken.dimdoors.shared.tileentities.TileEntityRift;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemDoor; import net.minecraft.item.ItemDoor;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.util.*; import net.minecraft.util.*;
@ -55,6 +54,7 @@ public abstract class ItemDoorBase extends ItemDoor {
*/ */
protected abstract BlockDimDoorBase getDoorBlock(); protected abstract BlockDimDoorBase getDoorBlock();
//onItemUse gets fired before onItemRightClick and if it returns "success", onItemRightClick gets skipped.
@Override @Override
public ActionResult<ItemStack> onItemRightClick(ItemStack stack, World worldIn, EntityPlayer playerIn, EnumHand hand) { public ActionResult<ItemStack> onItemRightClick(ItemStack stack, World worldIn, EntityPlayer playerIn, EnumHand hand) {
if (worldIn.isRemote) { if (worldIn.isRemote) {
@ -75,22 +75,20 @@ public abstract class ItemDoorBase extends ItemDoor {
@Override @Override
public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World world, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) { public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World world, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
if (world.isRemote) { if (world.isRemote) {
return EnumActionResult.FAIL; //@todo, is this needed, or does this always get called from the onItemRightClick(params) method? return EnumActionResult.SUCCESS;
} }
Block block = world.getBlockState(pos).getBlock(); Block block = world.getBlockState(pos).getBlock();
if (block.isReplaceable(world, pos) && block != Blocks.AIR) { //why are we even checking for Blocks.AIR here? How would one use an item on an air block. It has no hitbox, does it? if (!block.isReplaceable(world, pos)) {
if (side != EnumFacing.UP) {
return EnumActionResult.FAIL;
}
} else {
pos = pos.offset(EnumFacing.DOWN); //the bottom part of the door can replace this block, so we will try to place it on the block under it pos = pos.offset(EnumFacing.DOWN); //the bottom part of the door can replace this block, so we will try to place it on the block under it
block = world.getBlockState(pos).getBlock(); //update the block to be placed on
side = EnumFacing.UP; //make sure that the next if-statement returns true
} }
if (side != EnumFacing.UP || block == Blocks.AIR) { //only place the door if the item is "used" on the top of the block it is to be placed on and we might as well check if that block is air or not even though I do not think it is needed (isSideSolid gets checked later)
return EnumActionResult.FAIL;
} else {
return tryPlaceDoorOnTopOfBlock(stack, playerIn, world, pos, hand, hitX, hitY, hitZ); return tryPlaceDoorOnTopOfBlock(stack, playerIn, world, pos, hand, hitX, hitY, hitZ);
} }
}
//pos = position of block, the door gets placed on //pos = position of block, the door gets placed on
static EnumActionResult tryPlaceDoorOnTopOfBlock(ItemStack stack, EntityPlayer playerIn, World world, BlockPos pos, EnumHand hand, float hitX, float hitY, float hitZ) { static EnumActionResult tryPlaceDoorOnTopOfBlock(ItemStack stack, EntityPlayer playerIn, World world, BlockPos pos, EnumHand hand, float hitX, float hitY, float hitZ) {
@ -99,8 +97,10 @@ public abstract class ItemDoorBase extends ItemDoor {
// return null, just as if the item was an unrecognized door type. // return null, just as if the item was an unrecognized door type.
ItemDoorBase mappedItem = doorItemMapping.get(stack.getItem()); ItemDoorBase mappedItem = doorItemMapping.get(stack.getItem());
if (mappedItem == null) { if (mappedItem == null) {
DimDoors.warn(ItemDoorBase.class, "Item " + stack.getItem().toString() + " does not seem to have a valid mapped ItemDoor.");
return EnumActionResult.FAIL; return EnumActionResult.FAIL;
} }
pos = pos.up(); //change pos to position the bottom half of the door gets placed at pos = pos.up(); //change pos to position the bottom half of the door gets placed at
BlockDimDoorBase doorBlock = mappedItem.getDoorBlock(); BlockDimDoorBase doorBlock = mappedItem.getDoorBlock();
if (playerIn.canPlayerEdit(pos, EnumFacing.UP, stack) && playerIn.canPlayerEdit(pos.up(), EnumFacing.UP, stack) if (playerIn.canPlayerEdit(pos, EnumFacing.UP, stack) && playerIn.canPlayerEdit(pos.up(), EnumFacing.UP, stack)
@ -120,7 +120,9 @@ public abstract class ItemDoorBase extends ItemDoor {
placeDoor(world, pos, enumfacing, doorBlock, flag); placeDoor(world, pos, enumfacing, doorBlock, flag);
SoundType soundtype = world.getBlockState(pos).getBlock().getSoundType(world.getBlockState(pos), world, pos, playerIn); SoundType soundtype = world.getBlockState(pos).getBlock().getSoundType(world.getBlockState(pos), world, pos, playerIn);
world.playSound(playerIn, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F); world.playSound(playerIn, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
if (!playerIn.isCreative()) {
--stack.stackSize; --stack.stackSize;
}
//fetch the TileEntityDimDoor at the top block of where the door has just been placed //fetch the TileEntityDimDoor at the top block of where the door has just been placed
TileEntityDimDoor newTileEntityDimDoor = (TileEntityDimDoor) world.getTileEntity(pos.up()); TileEntityDimDoor newTileEntityDimDoor = (TileEntityDimDoor) world.getTileEntity(pos.up());

View file

@ -185,7 +185,6 @@ public abstract class DDTileEntityBase extends TileEntity implements ITickable {
@Override @Override
public void update() { public void update() {
if (isTeleporting && teleportingEntity != null) { if (isTeleporting && teleportingEntity != null) {
IDimDoor door = (IDimDoor) this.world.getBlockState(this.pos).getBlock();
if (tryTeleport(teleportingEntity)) { if (tryTeleport(teleportingEntity)) {
//player is succesfully teleported //player is succesfully teleported
} else { } else {