package info.u_team.u_team_core.container;

import java.util.List;

import com.google.common.collect.Lists;

import net.minecraft.inventory.container.*;
import net.minecraft.util.NonNullList;
import net.minecraftforge.fluids.FluidStack;

public abstract class FluidContainer extends Container {
	
	private final NonNullList<FluidStack> fluidStacks = NonNullList.create();
	public final List<FluidSlot> fluidSlots = Lists.newArrayList();
	
	public FluidContainer(ContainerType<?> type, int id) {
		super(type, id);
	}
	
	protected FluidSlot addFluidSlot(FluidSlot slot) {
		fluidSlots.add(slot);
		fluidStacks.add(FluidStack.EMPTY);
		return slot;
	}
	
	public FluidSlot getFluidSlot(int slot) {
		return fluidSlots.get(slot);
	}
	
	public NonNullList<FluidStack> getFluids() {
		final NonNullList<FluidStack> list = NonNullList.create();
		
		for (int index = 0; index < fluidSlots.size(); index++) {
			list.add(fluidSlots.get(index).getStack());
		}
		return list;
	}
	
	// Used for sync with the client
	
	public void setFluidStackInSlot(int slot, FluidStack stack) {
		getFluidSlot(slot).putStack(stack);
	}
	
	public void setAllFluidSlots(List<FluidStack> list) {
		for (int index = 0; index < list.size(); index++) {
			getFluidSlot(index).putStack(list.get(index));
		}
	}
}

@@ -59,16 +59,52 @@
 il.List;
+%0Aimport java.util.stream.Collectors;
 %0A%0Aimport
@@ -149,43 +149,249 @@
 ort 
-net.minecraft.inventory.container.*
+info.u_team.u_team_core.intern.init.UCoreNetwork;%0Aimport info.u_team.u_team_core.intern.network.*;%0Aimport net.minecraft.entity.player.ServerPlayerEntity;%0Aimport net.minecraft.inventory.container.*;%0Aimport net.minecraft.network.NetworkManager
 ;%0Aim
@@ -471,16 +471,73 @@
 idStack;
+%0Aimport net.minecraftforge.fml.network.PacketDistributor;
 %0A%0Apublic
@@ -1572,10 +1572,1218 @@
 %0A%09%09%7D%0A%09%7D%0A
+%09%0A%09// Send packets for client sync%0A%09%0A%09@Override%0A%09public void addListener(IContainerListener listener) %7B%0A%09%09super.addListener(listener);%0A%09%09if (listener instanceof ServerPlayerEntity) %7B%0A%09%09%09UCoreNetwork.NETWORK.send(PacketDistributor.PLAYER.with(() -%3E (ServerPlayerEntity) listener), new FluidSetAllContainerMessage(windowId, getFluids()));%0A%09%09%7D%0A%09%7D%0A%09%0A%09@Override%0A%09public void detectAndSendChanges() %7B%0A%09%09super.detectAndSendChanges();%0A%09%09for (int index = 0; index %3C fluidSlots.size(); index++) %7B%0A%09%09%09final FluidStack stackSlot = fluidSlots.get(index).getStack();%0A%09%09%09final FluidStack stackSynced = fluidStacks.get(index);%0A%09%09%09if (!stackSynced.isFluidStackIdentical(stackSlot)) %7B%0A%09%09%09%09final FluidStack stackNewSynced = stackSlot.copy();%0A%09%09%09%09fluidStacks.set(index, stackNewSynced);%0A%09%09%09%09%0A%09%09%09%09final List%3CNetworkManager%3E networkManagers = listeners.stream() //%0A%09%09%09%09%09%09.filter(listener -%3E listener instanceof ServerPlayerEntity) //%0A%09%09%09%09%09%09.map(listener -%3E ((ServerPlayerEntity) listener).connection.getNetworkManager()) //%0A%09%09%09%09%09%09.collect(Collectors.toList());%0A%09%09%09%09%0A%09%09%09%09UCoreNetwork.NETWORK.send(PacketDistributor.NMLIST.with(() -%3E networkManagers), new FluidSetSlotContainerMessage(windowId, index, stackNewSynced));%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A
 %7D%0A

Send packets in container for fluids