package com.progwml6.natura.common.item; import gnu.trove.map.hash.TIntObjectHashMap; import net.minecraft.block.Block; import net.minecraft.block.BlockDoor; import net.minecraft.block.SoundType; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.util.EnumActionResult; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.SoundCategory; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import slimeknights.mantle.item.ItemMetaDynamic; public class ItemNaturaDoor extends ItemMetaDynamic { protected TIntObjectHashMap<IBlockState> states = new TIntObjectHashMap<>(); public ItemStack addMeta(int meta, String name, IBlockState state) { this.states.put(meta, state); ItemStack ret = this.addMeta(meta, name); return ret; } @Override public ItemStack addMeta(int meta, String name) { if (!this.states.containsKey(meta)) { throw new RuntimeException("Usage of wrong function. Use the addMeta function that has an state paired with it with this implementation"); } return super.addMeta(meta, name); } @Override public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { if (facing != EnumFacing.UP) { return EnumActionResult.FAIL; } else { int meta = stack.getMetadata(); if (this.isValid(meta)) { IBlockState iblockstate = worldIn.getBlockState(pos); Block block = iblockstate.getBlock(); if (!block.isReplaceable(worldIn, pos)) { pos = pos.offset(facing); } if (playerIn.canPlayerEdit(pos, facing, stack) && this.states.get(meta).getBlock().canPlaceBlockAt(worldIn, pos)) { EnumFacing enumfacing = EnumFacing.fromAngle(playerIn.rotationYaw); int i = enumfacing.getFrontOffsetX(); int j = enumfacing.getFrontOffsetZ(); boolean flag = i < 0 && hitZ < 0.5F || i > 0 && hitZ > 0.5F || j < 0 && hitX > 0.5F || j > 0 && hitX < 0.5F; placeDoor(worldIn, pos, enumfacing, this.states.get(meta), flag); SoundType soundtype = worldIn.getBlockState(pos).getBlock().getSoundType(worldIn.getBlockState(pos), worldIn, pos, playerIn); worldIn.playSound(playerIn, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F); --stack.stackSize; return EnumActionResult.SUCCESS; } else { return EnumActionResult.FAIL; } } else { return EnumActionResult.FAIL; } } } public static void placeDoor(World worldIn, BlockPos pos, EnumFacing facing, IBlockState door, boolean isRightHinge) { BlockPos blockpos = pos.offset(facing.rotateY()); BlockPos blockpos1 = pos.offset(facing.rotateYCCW()); int i = (worldIn.getBlockState(blockpos1).isNormalCube() ? 1 : 0) + (worldIn.getBlockState(blockpos1.up()).isNormalCube() ? 1 : 0); int j = (worldIn.getBlockState(blockpos).isNormalCube() ? 1 : 0) + (worldIn.getBlockState(blockpos.up()).isNormalCube() ? 1 : 0); boolean flag = worldIn.getBlockState(blockpos1).getBlock() == door || worldIn.getBlockState(blockpos1.up()).getBlock() == door; boolean flag1 = worldIn.getBlockState(blockpos).getBlock() == door || worldIn.getBlockState(blockpos.up()).getBlock() == door; if ((!flag || flag1) && j <= i) { if (flag1 && !flag || j < i) { isRightHinge = false; } } else { isRightHinge = true; } BlockPos blockpos2 = pos.up(); boolean flag2 = worldIn.isBlockPowered(pos) || worldIn.isBlockPowered(blockpos2); IBlockState iblockstate = door.withProperty(BlockDoor.FACING, facing).withProperty(BlockDoor.HINGE, isRightHinge ? BlockDoor.EnumHingePosition.RIGHT : BlockDoor.EnumHingePosition.LEFT).withProperty(BlockDoor.POWERED, Boolean.valueOf(flag2)).withProperty(BlockDoor.OPEN, Boolean.valueOf(flag2)); worldIn.setBlockState(pos, iblockstate.withProperty(BlockDoor.HALF, BlockDoor.EnumDoorHalf.LOWER), 2); worldIn.setBlockState(blockpos2, iblockstate.withProperty(BlockDoor.HALF, BlockDoor.EnumDoorHalf.UPPER), 2); worldIn.notifyNeighborsOfStateChange(pos, door.getBlock()); worldIn.notifyNeighborsOfStateChange(blockpos2, door.getBlock()); } }