package info.u_team.u_team_core.intern.network;

import java.util.Optional;
import java.util.function.Supplier;

import info.u_team.u_team_core.container.FluidContainer;
import net.minecraft.inventory.container.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.network.NetworkEvent.Context;

public class FluidClickContainerMessage {
	
	private final int id;
	private final int slot;
	private final ItemStack stack;
	
	public FluidClickContainerMessage(int id, int slot, ItemStack stack) {
		this.id = id;
		this.slot = slot;
		this.stack = stack;
	}
	
	public static void encode(FluidClickContainerMessage message, PacketBuffer sendBuffer) {
		sendBuffer.writeByte(message.id);
		sendBuffer.writeShort(message.slot);
		sendBuffer.writeItemStack(message.stack, false);
	}
	
	public static FluidClickContainerMessage decode(PacketBuffer sendBuffer) {
		final int id = sendBuffer.readByte();
		final int slot = sendBuffer.readShort();
		final ItemStack stack = sendBuffer.readItemStack();
		
		return new FluidClickContainerMessage(id, slot, stack);
	}
	
	public static class Handler {
		
		public static void handle(FluidClickContainerMessage message, Supplier<Context> contextSupplier) {
			final Context context = contextSupplier.get();
			context.enqueueWork(() -> {
				getFluidContainer(context.getSender().openContainer, message.id).ifPresent(container -> container.fluidSlotClick(message.slot, message.stack));
			});
			context.setPacketHandled(true);
		}
		
		private static final Optional<FluidContainer> getFluidContainer(Container container, int id) {
			if (container instanceof FluidContainer && container.windowId == id) {
				return Optional.of((FluidContainer) container);
			}
			return Optional.empty();
		}
	}
	
}

@@ -155,32 +155,87 @@
 FluidContainer;%0A
+import net.minecraft.entity.player.ServerPlayerEntity;%0A
 import net.minec
@@ -1396,45 +1396,91 @@
 %09%09%09%09
-getFluidContainer(context.getSender()
+final ServerPlayerEntity player = context.getSender();%0A%09%09%09%09getFluidContainer(player
 .ope
@@ -1551,16 +1551,24 @@
 otClick(
+player, 
 message.

Include the player