package com.lothrazar.cyclic.net;

import com.lothrazar.cyclic.ModCyclic;
import com.lothrazar.cyclic.base.PacketBase;
import com.lothrazar.cyclic.block.endershelf.EnderShelfItemHandler;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.network.NetworkEvent;
import net.minecraftforge.items.CapabilityItemHandler;

import java.util.function.Supplier;

public class PacketTileInventory extends PacketBase {

  private BlockPos blockPos;
  private int slot;
  private ItemStack itemStack;
  private TYPE type;

  public enum TYPE {
    CHANGE, SET
  }

  public PacketTileInventory(BlockPos blockPos, int slot, ItemStack itemStack, TYPE type) {
    this.blockPos = blockPos;
    this.slot = slot;
    this.itemStack = itemStack;
    this.type = type;
  }

  public PacketTileInventory() {}

  public static void handle(PacketTileInventory message, Supplier<NetworkEvent.Context> ctx) {
    ctx.get().enqueueWork(() -> {
      PlayerEntity player = ModCyclic.proxy.getClientPlayer();
      TileEntity tile = player.world.getTileEntity(message.blockPos);
      if (tile != null)
      tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(h -> {
        if (message.type == TYPE.SET && h instanceof EnderShelfItemHandler) {
          ItemStack was = h.getStackInSlot(message.slot);
          ItemStack extracted = ((EnderShelfItemHandler) h).emptySlot(message.slot);
        }
        h.insertItem(message.slot, message.itemStack, false);
      });
    });
    message.done(ctx);
  }

  public static PacketTileInventory decode(PacketBuffer buf) {
    PacketTileInventory p = new PacketTileInventory();
    p.blockPos = buf.readBlockPos();
    p.slot = buf.readInt();
    p.itemStack = buf.readItemStack();
    p.type = buf.readEnumValue(TYPE.class);
    return p;
  }

  public static void encode(PacketTileInventory msg, PacketBuffer buf) {
    buf.writeBlockPos(msg.blockPos);
    buf.writeInt(msg.slot);
    buf.writeItemStack(msg.itemStack);
    buf.writeEnumValue(msg.type);
  }
}

@@ -176,24 +176,63 @@
 temHandler;%0A
+import net.minecraft.client.Minecraft;%0A
 import net.m
@@ -1154,64 +1154,103 @@
     
-PlayerEntity player = ModCyclic.proxy.getClientPlayer();
+if (Minecraft.getInstance().world == null) %7B%0A        message.done(ctx);%0A        return;%0A      %7D
 %0A   
@@ -1270,22 +1270,39 @@
  tile = 
-player
+Minecraft.getInstance()
 .world.g

refactor PacketTileInventory to conform to proxy refactor