package info.sleeplessacorn.nomagi.item;

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.ItemBlock;
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;

// TODO - Move to LendingLibrary
public class ItemNomagiDoor extends ItemBlock {

    public ItemNomagiDoor(Block block) {
        super(block);
    }

    @Override
    public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
        if (facing != EnumFacing.UP)
            return EnumActionResult.FAIL;

        IBlockState state = world.getBlockState(pos);
        ItemStack held = player.getHeldItem(hand);

        if (!state.getBlock().isReplaceable(world, pos)) {
            pos = pos.offset(facing);
            state = world.getBlockState(pos);
        }

        if (player.canPlayerEdit(pos, facing, held) && block.canPlaceBlockAt(world, pos)) {
            EnumFacing enumfacing = EnumFacing.fromAngle((double) player.rotationYaw);
            int offsetX = enumfacing.getFrontOffsetX();
            int offsetZ = enumfacing.getFrontOffsetZ();
            boolean isRightHinge = offsetX < 0 && hitZ < 0.5F || offsetX > 0 && hitZ > 0.5F || offsetZ < 0 && hitX > 0.5F || offsetZ > 0 && hitX < 0.5F;

            placeDoor(world, pos, enumfacing, block, isRightHinge);
            SoundType soundtype = state.getBlock().getSoundType(state, world, pos, player);
            world.playSound(player, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
            held.shrink(1);
            return EnumActionResult.SUCCESS;
        }

        return EnumActionResult.FAIL;
    }

    public static void placeDoor(World world, BlockPos pos, EnumFacing facing, Block door, boolean isRightHinge) {
        BlockPos blockpos = pos.offset(facing.rotateY());
        BlockPos blockpos1 = pos.offset(facing.rotateYCCW());

        IBlockState state = world.getBlockState(blockpos);
        IBlockState stateUp = world.getBlockState(blockpos.up());
        IBlockState state1 = world.getBlockState(blockpos1);
        IBlockState state1Up = world.getBlockState(blockpos1.up());

        int j = (state.isNormalCube() ? 1 : 0) + (stateUp.isNormalCube() ? 1 : 0);
        int i = (state1.isNormalCube() ? 1 : 0) + (state1Up.isNormalCube() ? 1 : 0);

        boolean flag1 = state.getBlock() == door || stateUp.getBlock() == door;
        boolean flag = state1.getBlock() == door || state1Up.getBlock() == door;

        if ((!flag || flag1) && j <= i)
            isRightHinge = !(flag1 && !flag || j < i);

        boolean powered = world.isBlockPowered(pos) || world.isBlockPowered(pos.up());
        IBlockState placeSate = door.getDefaultState().withProperty(BlockDoor.FACING, facing).withProperty(BlockDoor.HINGE, isRightHinge ? BlockDoor.EnumHingePosition.RIGHT : BlockDoor.EnumHingePosition.LEFT).withProperty(BlockDoor.POWERED, powered).withProperty(BlockDoor.OPEN, powered);
        world.setBlockState(pos, placeSate.withProperty(BlockDoor.HALF, BlockDoor.EnumDoorHalf.LOWER), 2);
        world.setBlockState(pos.up(), placeSate.withProperty(BlockDoor.HALF, BlockDoor.EnumDoorHalf.UPPER), 2);
        world.notifyNeighborsOfStateChange(pos, door, false);
        world.notifyNeighborsOfStateChange(pos.up(), door, false);
    }
}

@@ -73,46 +73,8 @@
 ck;%0A
-import net.minecraft.block.BlockDoor;%0A
 impo
@@ -235,24 +235,60 @@
 .ItemBlock;%0A
+import net.minecraft.item.ItemDoor;%0A
 import net.m
@@ -1402,25 +1402,30 @@
         int 
-o
+frontO
 ffsetX = enu
@@ -1467,17 +1467,22 @@
     int 
-o
+frontO
 ffsetZ =
@@ -1547,17 +1547,22 @@
 Hinge = 
-o
+frontO
 ffsetX %3C
@@ -1578,25 +1578,30 @@
 Z %3C 0.5F %7C%7C 
-o
+frontO
 ffsetX %3E 0 &
@@ -1613,25 +1613,30 @@
 Z %3E 0.5F %7C%7C 
-o
+frontO
 ffsetZ %3C 0 &
@@ -1652,17 +1652,22 @@
 0.5F %7C%7C 
-o
+frontO
 ffsetZ %3E
@@ -1698,16 +1698,25 @@
         
+ItemDoor.
 placeDoo
@@ -2145,1656 +2145,7 @@
    %7D
-%0A%0A    public static void placeDoor(World world, BlockPos pos, EnumFacing facing, Block door, boolean isRightHinge) %7B%0A        BlockPos blockpos = pos.offset(facing.rotateY());%0A        BlockPos blockpos1 = pos.offset(facing.rotateYCCW());%0A%0A        IBlockState state = world.getBlockState(blockpos);%0A        IBlockState stateUp = world.getBlockState(blockpos.up());%0A        IBlockState state1 = world.getBlockState(blockpos1);%0A        IBlockState state1Up = world.getBlockState(blockpos1.up());%0A%0A        int j = (state.isNormalCube() ? 1 : 0) + (stateUp.isNormalCube() ? 1 : 0);%0A        int i = (state1.isNormalCube() ? 1 : 0) + (state1Up.isNormalCube() ? 1 : 0);%0A%0A        boolean flag1 = state.getBlock() == door %7C%7C stateUp.getBlock() == door;%0A        boolean flag = state1.getBlock() == door %7C%7C state1Up.getBlock() == door;%0A%0A        if ((!flag %7C%7C flag1) && j %3C= i)%0A            isRightHinge = !(flag1 && !flag %7C%7C j %3C i);%0A%0A        boolean powered = world.isBlockPowered(pos) %7C%7C world.isBlockPowered(pos.up());%0A        IBlockState placeSate = door.getDefaultState().withProperty(BlockDoor.FACING, facing).withProperty(BlockDoor.HINGE, isRightHinge ? BlockDoor.EnumHingePosition.RIGHT : BlockDoor.EnumHingePosition.LEFT).withProperty(BlockDoor.POWERED, powered).withProperty(BlockDoor.OPEN, powered);%0A        world.setBlockState(pos, placeSate.withProperty(BlockDoor.HALF, BlockDoor.EnumDoorHalf.LOWER), 2);%0A        world.setBlockState(pos.up(), placeSate.withProperty(BlockDoor.HALF, BlockDoor.EnumDoorHalf.UPPER), 2);%0A        world.notifyNeighborsOfStateChange(pos, door, false);%0A        world.notifyNeighborsOfStateChange(pos.up(), door, false);%0A    %7D
 %0A%7D%0A

Fix door placement