package net.geforcemods.securitycraft.network.server;

import java.util.function.Supplier;

import net.geforcemods.securitycraft.api.IPasswordProtected;
import net.geforcemods.securitycraft.tileentity.KeypadChestTileEntity;
import net.geforcemods.securitycraft.util.BlockUtils;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.network.NetworkEvent;

public class SetPassword {

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

	public SetPassword(){

	}

	public SetPassword(int x, int y, int z, String code){
		this.x = x;
		this.y = y;
		this.z = z;
		password = code;
	}

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

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

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

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

			if(getWorld(player).getTileEntity(pos) instanceof IPasswordProtected){
				((IPasswordProtected) getWorld(player).getTileEntity(pos)).setPassword(password);
				checkForAdjacentChest(pos, password, player);
			}
		});

		ctx.get().setPacketHandled(true);
	}

	private static void checkForAdjacentChest(BlockPos pos, String codeToSet, PlayerEntity player) {
		if(getWorld(player).getTileEntity(pos) instanceof KeypadChestTileEntity)
			if(getWorld(player).getTileEntity(pos.east()) instanceof KeypadChestTileEntity)
				((IPasswordProtected) getWorld(player).getTileEntity(pos.east())).setPassword(codeToSet);
			else if(getWorld(player).getTileEntity(pos.west()) instanceof KeypadChestTileEntity)
				((IPasswordProtected) getWorld(player).getTileEntity(pos.west())).setPassword(codeToSet);
			else if(getWorld(player).getTileEntity(pos.south()) instanceof KeypadChestTileEntity)
				((IPasswordProtected) getWorld(player).getTileEntity(pos.south())).setPassword(codeToSet);
			else if(getWorld(player).getTileEntity(pos.north()) instanceof KeypadChestTileEntity)
				((IPasswordProtected) getWorld(player).getTileEntity(pos.north())).setPassword(codeToSet);
	}

	private static World getWorld(PlayerEntity player) //i'm lazy
	{
		return player.world;
	}
}

@@ -81,24 +81,75 @@
 .Supplier;%0A%0A
+import net.geforcemods.securitycraft.api.IOwnable;%0A
 import net.g
@@ -414,16 +414,60 @@
 Buffer;%0A
+import net.minecraft.tileentity.TileEntity;%0A
 import n
@@ -1548,39 +1548,71 @@
 nder();%0A
-%0A
 %09%09%09
-if(getWorld(player)
+World world = player.world;%0A%09%09%09TileEntity te = world
 .getTile
@@ -1614,32 +1614,43 @@
 tTileEntity(pos)
+;%0A%0A%09%09%09if(te
  instanceof IPas
@@ -1667,97 +1667,185 @@
 cted
-)%7B%0A%09%09%09%09((IPasswordProtected) getWorld(player).getTileEntity(pos)).setPassword(password);%0A
+ && (!(te instanceof IOwnable) %7C%7C ((IOwnable)te).getOwner().isOwner(player)))%7B%0A%09%09%09%09((IPasswordProtected)te).setPassword(password);%0A%0A%09%09%09%09if(te instanceof KeypadChestTileEntity)%0A%09
 %09%09%09%09
@@ -1866,16 +1866,23 @@
 ntChest(
+world, 
 pos, pas
@@ -1992,16 +1992,29 @@
 ntChest(
+World world, 
 BlockPos
@@ -2069,100 +2069,13 @@
 %09if(
-getWorld(player).getTileEntity(pos) instanceof KeypadChestTileEntity)%0A%09%09%09if(getWorld(player)
+world
 .get
@@ -2126,33 +2126,32 @@
 tTileEntity)%0A%09%09%09
-%09
 ((IPasswordProte
@@ -2156,32 +2156,21 @@
 tected) 
-getWorld(player)
+world
 .getTile
@@ -2211,33 +2211,32 @@
 deToSet);%0A%09%09
-%09
 else if(
 getWorld(pla
@@ -2223,32 +2223,21 @@
 else if(
-getWorld(player)
+world
 .getTile
@@ -2284,33 +2284,32 @@
 tTileEntity)%0A%09%09%09
-%09
 ((IPasswordProte
@@ -2314,32 +2314,21 @@
 tected) 
-getWorld(player)
+world
 .getTile
@@ -2369,33 +2369,32 @@
 deToSet);%0A%09%09
-%09
 else if(
 getWorld(pla
@@ -2381,32 +2381,21 @@
 else if(
-getWorld(player)
+world
 .getTile
@@ -2443,33 +2443,32 @@
 tTileEntity)%0A%09%09%09
-%09
 ((IPasswordProte
@@ -2473,32 +2473,21 @@
 tected) 
-getWorld(player)
+world
 .getTile
@@ -2533,25 +2533,24 @@
 Set);%0A%09%09
-%09
 else if(
 getWorld
@@ -2541,32 +2541,21 @@
 else if(
-getWorld(player)
+world
 .getTile
@@ -2607,25 +2607,24 @@
 eEntity)%0A%09%09%09
-%09
 ((IPasswordP
@@ -2637,24 +2637,13 @@
 ed) 
-getWorld(player)
+world
 .get
@@ -2697,100 +2697,7 @@
 ;%0A%09%7D
-%0A%0A%09private static World getWorld(PlayerEntity player) //i'm lazy%0A%09%7B%0A%09%09return player.world;%0A%09%7D
 %0A%7D%0A

add validation checks to SetPassword