2020-10-15 06:07:06 +02:00
|
|
|
package de.ellpeck.prettypipes.packets;
|
|
|
|
|
2024-03-07 16:00:49 +01:00
|
|
|
import de.ellpeck.prettypipes.PrettyPipes;
|
2020-10-15 06:07:06 +02:00
|
|
|
import de.ellpeck.prettypipes.pipe.modules.craft.CraftingModuleContainer;
|
2021-12-02 15:25:46 +01:00
|
|
|
import net.minecraft.network.FriendlyByteBuf;
|
2024-03-07 16:00:49 +01:00
|
|
|
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
|
|
|
|
import net.minecraft.resources.ResourceLocation;
|
2021-12-02 12:31:04 +01:00
|
|
|
import net.minecraft.world.item.ItemStack;
|
2024-02-03 15:17:58 +01:00
|
|
|
import net.neoforged.neoforge.items.ItemHandlerHelper;
|
|
|
|
import net.neoforged.neoforge.items.ItemStackHandler;
|
2024-03-07 16:00:49 +01:00
|
|
|
import net.neoforged.neoforge.network.handling.PlayPayloadContext;
|
2020-10-15 06:07:06 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2024-03-07 16:00:49 +01:00
|
|
|
public class PacketCraftingModuleTransfer implements CustomPacketPayload {
|
2020-10-15 06:07:06 +02:00
|
|
|
|
2024-03-07 16:00:49 +01:00
|
|
|
public static final ResourceLocation ID = new ResourceLocation(PrettyPipes.ID, "crafting_module_transfer");
|
|
|
|
|
|
|
|
private final List<ItemStack> inputs;
|
|
|
|
private final List<ItemStack> outputs;
|
2020-10-15 06:07:06 +02:00
|
|
|
|
|
|
|
public PacketCraftingModuleTransfer(List<ItemStack> inputs, List<ItemStack> outputs) {
|
|
|
|
this.inputs = inputs;
|
|
|
|
this.outputs = outputs;
|
|
|
|
}
|
|
|
|
|
2024-03-07 16:00:49 +01:00
|
|
|
public PacketCraftingModuleTransfer(FriendlyByteBuf buf) {
|
|
|
|
this.inputs = new ArrayList<>();
|
2021-12-02 17:46:56 +01:00
|
|
|
for (var i = buf.readInt(); i > 0; i--)
|
2024-03-07 16:00:49 +01:00
|
|
|
this.inputs.add(buf.readItem());
|
|
|
|
this.outputs = new ArrayList<>();
|
2021-12-02 17:46:56 +01:00
|
|
|
for (var i = buf.readInt(); i > 0; i--)
|
2024-03-07 16:00:49 +01:00
|
|
|
this.outputs.add(buf.readItem());
|
2020-10-15 06:07:06 +02:00
|
|
|
}
|
|
|
|
|
2024-03-07 16:00:49 +01:00
|
|
|
@Override
|
|
|
|
public void write(FriendlyByteBuf buf) {
|
|
|
|
buf.writeInt(this.inputs.size());
|
|
|
|
for (var stack : this.inputs)
|
2021-12-02 15:25:46 +01:00
|
|
|
buf.writeItem(stack);
|
2024-03-07 16:00:49 +01:00
|
|
|
buf.writeInt(this.outputs.size());
|
|
|
|
for (var stack : this.outputs)
|
2021-12-02 15:25:46 +01:00
|
|
|
buf.writeItem(stack);
|
2020-10-15 06:07:06 +02:00
|
|
|
}
|
|
|
|
|
2024-03-07 16:00:49 +01:00
|
|
|
@Override
|
|
|
|
public ResourceLocation id() {
|
|
|
|
return PacketCraftingModuleTransfer.ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void onMessage(PacketCraftingModuleTransfer message, PlayPayloadContext ctx) {
|
|
|
|
ctx.workHandler().execute(() -> {
|
|
|
|
var player = ctx.player().orElseThrow();
|
|
|
|
if (player.containerMenu instanceof CraftingModuleContainer container) {
|
|
|
|
PacketCraftingModuleTransfer.copy(container.input, message.inputs);
|
|
|
|
PacketCraftingModuleTransfer.copy(container.output, message.outputs);
|
|
|
|
container.modified = true;
|
|
|
|
container.broadcastChanges();
|
2020-10-15 06:07:06 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void copy(ItemStackHandler container, List<ItemStack> contents) {
|
2021-12-02 17:46:56 +01:00
|
|
|
for (var i = 0; i < container.getSlots(); i++)
|
2020-10-15 06:07:06 +02:00
|
|
|
container.setStackInSlot(i, ItemStack.EMPTY);
|
2021-12-02 17:46:56 +01:00
|
|
|
for (var stack : contents)
|
2020-10-15 06:07:06 +02:00
|
|
|
ItemHandlerHelper.insertItem(container, stack, false);
|
|
|
|
}
|
2024-03-07 16:00:49 +01:00
|
|
|
|
2020-10-15 06:07:06 +02:00
|
|
|
}
|