97 lines
3.2 KiB
Java
Executable file
97 lines
3.2 KiB
Java
Executable file
package game.item;
|
|
|
|
import game.block.Block;
|
|
import game.block.BlockDoor;
|
|
import game.entity.npc.EntityNPC;
|
|
import game.init.Blocks;
|
|
import game.material.Material;
|
|
import game.world.BlockPos;
|
|
import game.world.Facing;
|
|
import game.world.State;
|
|
import game.world.World;
|
|
|
|
public class ItemDoor extends Item
|
|
{
|
|
private Block block;
|
|
|
|
public ItemDoor(Block block)
|
|
{
|
|
this.block = block;
|
|
this.setTab(block.getMaterial() == Material.wood ? CheatTab.tabWood : CheatTab.tabTech);
|
|
}
|
|
|
|
public Block getBlock()
|
|
{
|
|
return this.block;
|
|
}
|
|
|
|
/**
|
|
* Called when a Block is right-clicked with this Item
|
|
*/
|
|
public boolean onItemUse(ItemStack stack, EntityNPC playerIn, World worldIn, BlockPos pos, Facing side, float hitX, float hitY, float hitZ)
|
|
{
|
|
if (side != Facing.UP)
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
State iblockstate = worldIn.getState(pos);
|
|
Block block = iblockstate.getBlock();
|
|
|
|
if (!block.isReplaceable(worldIn, pos))
|
|
{
|
|
pos = pos.offset(side);
|
|
}
|
|
|
|
if (!playerIn.canPlayerEdit(pos, side, stack))
|
|
{
|
|
return false;
|
|
}
|
|
else if (!this.block.canPlaceBlockAt(worldIn, pos))
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
placeDoor(worldIn, pos, Facing.fromAngle((double)playerIn.rotYaw), this.block, true);
|
|
--stack.stackSize;
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void placeDoor(World worldIn, BlockPos pos, Facing facing, Block door, boolean update)
|
|
{
|
|
BlockPos blockpos = pos.offset(facing.rotateY());
|
|
BlockPos blockpos1 = pos.offset(facing.rotateYCCW());
|
|
int i = (worldIn.getState(blockpos1).getBlock().isNormalCube() ? 1 : 0) + (worldIn.getState(blockpos1.up()).getBlock().isNormalCube() ? 1 : 0);
|
|
int j = (worldIn.getState(blockpos).getBlock().isNormalCube() ? 1 : 0) + (worldIn.getState(blockpos.up()).getBlock().isNormalCube() ? 1 : 0);
|
|
boolean flag = worldIn.getState(blockpos1).getBlock() == door || worldIn.getState(blockpos1.up()).getBlock() == door;
|
|
boolean flag1 = worldIn.getState(blockpos).getBlock() == door || worldIn.getState(blockpos.up()).getBlock() == door;
|
|
boolean flag2 = false;
|
|
|
|
if (flag && !flag1 || j > i)
|
|
{
|
|
flag2 = true;
|
|
}
|
|
|
|
BlockPos blockpos2 = pos.up();
|
|
State iblockstate = door.getState().withProperty(BlockDoor.FACING, facing).withProperty(BlockDoor.HINGE, flag2 ? BlockDoor.EnumHingePosition.RIGHT : BlockDoor.EnumHingePosition.LEFT);
|
|
worldIn.setState(pos, iblockstate.withProperty(BlockDoor.HALF, BlockDoor.EnumDoorHalf.LOWER), 2);
|
|
worldIn.setState(blockpos2, iblockstate.withProperty(BlockDoor.HALF, BlockDoor.EnumDoorHalf.UPPER), 2);
|
|
if(update) {
|
|
worldIn.notifyNeighborsOfStateChange(pos, door);
|
|
worldIn.notifyNeighborsOfStateChange(blockpos2, door);
|
|
}
|
|
}
|
|
|
|
public boolean isMagnetic() {
|
|
return this.block == Blocks.iron_door;
|
|
}
|
|
|
|
public String getDisplay(ItemStack stack)
|
|
{
|
|
return this.block.getDisplay();
|
|
}
|
|
}
|