package net.geforcemods.securitycraft.network.server;

import java.util.function.Supplier;

import net.geforcemods.securitycraft.api.IExplosive;
import net.geforcemods.securitycraft.api.IOwnable;
import net.geforcemods.securitycraft.util.BlockUtils;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.network.NetworkEvent;

public class RemoteControlMine{

	private int x, y, z;
	private String state;

	public RemoteControlMine(){

	}

	public RemoteControlMine(int x, int y, int z, String state){
		this.x = x;
		this.y = y;
		this.z = z;
		this.state = state;
	}

	public static void encode(RemoteControlMine message, PacketBuffer buf)
	{
		buf.writeInt(message.x);
		buf.writeInt(message.y);
		buf.writeInt(message.z);
		buf.writeString(message.state);
	}

	public static RemoteControlMine decode(PacketBuffer buf)
	{
		RemoteControlMine message = new RemoteControlMine();

		message.x = buf.readInt();
		message.y = buf.readInt();
		message.z = buf.readInt();
		message.state = buf.readString(Integer.MAX_VALUE / 4);
		return message;
	}

	public static void onMessage(RemoteControlMine message, Supplier<NetworkEvent.Context> ctx)
	{
		ctx.get().enqueueWork(() -> {
			PlayerEntity player = ctx.get().getSender();
			BlockPos pos = new BlockPos(message.x, message.y, message.z);

			if(player.world.getBlockState(pos) instanceof IExplosive)
			{
				TileEntity te = player.world.getTileEntity(pos);

				if(te instanceof IOwnable && ((IOwnable)te).getOwner().isOwner(player))
				{
					if(message.state.equalsIgnoreCase("activate"))
						((IExplosive) BlockUtils.getBlock(player.world, message.x, message.y, message.z)).activateMine(player.world, BlockUtils.toPos(message.x, message.y, message.z));
					else if(message.state.equalsIgnoreCase("defuse"))
						((IExplosive) BlockUtils.getBlock(player.world, message.x, message.y, message.z)).defuseMine(player.world, BlockUtils.toPos(message.x, message.y, message.z));
					else if(message.state.equalsIgnoreCase("detonate"))
						((IExplosive) BlockUtils.getBlock(player.world, message.x, message.y, message.z)).explode(player.world, BlockUtils.toPos(message.x, message.y, message.z));
				}
			}
		});

		ctx.get().setPacketHandled(true);

	}

}

@@ -204,49 +204,34 @@
 net.
-geforcemods.securitycraft.util
+minecraft.block
 .Block
-Utils
+State
 ;%0Aim
@@ -405,16 +405,50 @@
 ockPos;%0A
+import net.minecraft.world.World;%0A
 import n
@@ -1388,16 +1388,47 @@
 nder();%0A
+%09%09%09World world = player.world;%0A
 %09%09%09Block
@@ -1488,22 +1488,30 @@
 z);%0A
-%0A
 %09%09%09
-if(player.
+BlockState state = 
 worl
@@ -1530,16 +1530,41 @@
 ate(pos)
+;%0A%0A%09%09%09if(state.getBlock()
  instanc
@@ -1580,24 +1580,84 @@
 osive)%0A%09%09%09%7B%0A
+%09%09%09%09IExplosive explosive = ((IExplosive) state.getBlock());%0A
 %09%09%09%09TileEnti
@@ -1664,23 +1664,16 @@
 ty te = 
-player.
 world.ge
@@ -1827,36 +1827,33 @@
 te%22))%0A%09%09%09%09%09%09
-((IE
+e
 xplosive
 ) BlockUtils
@@ -1844,154 +1844,31 @@
 sive
-) BlockUtils.getBlock(player.world, message.x, message.y, message.z)).activateMine(player.world, BlockUtils.toPos(message.x, message.y, message.z)
+.activateMine(world,pos
 );%0A%09
@@ -1923,36 +1923,33 @@
 se%22))%0A%09%09%09%09%09%09
-((IE
+e
 xplosive
 ) BlockUtils
@@ -1940,152 +1940,30 @@
 sive
-) BlockUtils.getBlock(player.world, message.x, message.y, message.z)).defuseMine(player.world, BlockUtils.toPos(message.x, message.y, message.z)
+.defuseMine(world, pos
 );%0A%09
@@ -2024,28 +2024,25 @@
 )%0A%09%09%09%09%09%09
-((IE
+e
 xplosive
 ) BlockU
@@ -2037,149 +2037,27 @@
 sive
-) BlockUtils.getBlock(player.world, message.x, message.y, message.z)).explode(player.world, BlockUtils.toPos(message.x, message.y, message.z)
+.explode(world, pos
 );%0A%09

improve code of RemoteControMine packet