package net.geforcemods.securitycraft.items;

import net.geforcemods.securitycraft.SCContent;
import net.geforcemods.securitycraft.api.IOwnable;
import net.geforcemods.securitycraft.blocks.reinforced.IReinforcedBlock;
import net.geforcemods.securitycraft.containers.BlockReinforcerContainer;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.fml.network.NetworkHooks;

public class UniversalBlockReinforcerItem extends Item
{
	public UniversalBlockReinforcerItem(Item.Properties properties)
	{
		super(properties);
	}

	@Override
	public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand)
	{
		if(!world.isRemote && player instanceof ServerPlayerEntity)
		{
			NetworkHooks.openGui((ServerPlayerEntity)player, new INamedContainerProvider() {
				@Override
				public Container createMenu(int windowId, PlayerInventory inv, PlayerEntity player)
				{
					return new BlockReinforcerContainer(windowId, inv, UniversalBlockReinforcerItem.this == SCContent.UNIVERSAL_BLOCK_REINFORCER_LVL_1.get());
				}

				@Override
				public ITextComponent getDisplayName()
				{
					return new TranslationTextComponent(getTranslationKey());
				}
			}, data -> data.writeBoolean(this == SCContent.UNIVERSAL_BLOCK_REINFORCER_LVL_1.get()));
		}
		return super.onItemRightClick(world, player, hand);
	}

	@Override
	public boolean canPlayerBreakBlockWhileHolding(BlockState vanillaState, World world, BlockPos pos, PlayerEntity player) //gets rid of the stuttering experienced with onBlockStartBreak
	{
		if(!player.isCreative() && player.getHeldItemMainhand().getItem() == this)
		{
			Block block = vanillaState.getBlock();
			Block rb = IReinforcedBlock.VANILLA_TO_SECURITYCRAFT.get(block);

			if(rb != null)
			{
				BlockState convertedState = ((IReinforcedBlock)rb).getConvertedState(vanillaState);
				TileEntity te = world.getTileEntity(pos);

				CompoundNBT tag = new CompoundNBT();

				if(te != null)
					te.write(tag);

				if(te instanceof IInventory)
					((IInventory)te).clear();

				world.setBlockState(pos, convertedState);
				te = world.getTileEntity(pos);
				te.read(convertedState, tag);
				((IOwnable)te).getOwner().set(player.getGameProfile().getId().toString(), player.getName());
				player.getHeldItemMainhand().damageItem(1, player, p -> p.sendBreakAnimation(p.getActiveHand()));
				return false;
			}
		}

		return true;
	}
}

@@ -2631,17 +2631,16 @@
 y(pos);%0A
-%0A
 %09%09%09%09Comp
@@ -2658,24 +2658,11 @@
  = n
-ew CompoundNBT()
+ull
 ;%0A%0A%09
@@ -2687,25 +2687,52 @@
 %09%09%09%09
-%09te.write(tag
+%7B%0A%09%09%09%09%09tag = te.write(new CompoundNBT()
 );%0A%0A
+%09
 %09%09%09%09
@@ -2765,16 +2765,17 @@
 y)%0A%09%09%09%09%09
+%09
 ((IInven
@@ -2791,16 +2791,22 @@
 clear();
+%0A%09%09%09%09%7D
 %0A%0A%09%09%09%09wo
@@ -2876,24 +2876,46 @@
 ntity(pos);%0A
+%0A%09%09%09%09if(tag != null)%0A%09
 %09%09%09%09te.read(
@@ -2936,16 +2936,17 @@
 , tag);%0A
+%0A
 %09%09%09%09((IO

fix the last fix