From 3398d8c1e2631b273986291823d254b35837700c Mon Sep 17 00:00:00 2001 From: Reece Mackie <20544390+Rover656@users.noreply.github.com> Date: Mon, 9 Dec 2024 18:50:03 +0000 Subject: [PATCH] fix: Filter items not persisting when dragged in from JEI/EMI. Fixes: GH-906 --- .../enderio/base/common/init/EIOPackets.java | 4 ++ .../jei/FilterGhostIngredientHandler.java | 23 ++++--- .../common/network/C2SSetFluidFilterSlot.java | 61 +++++++++++++++++++ .../common/network/C2SSetItemFilterSlot.java | 61 +++++++++++++++++++ 4 files changed, 142 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/enderio/base/common/network/C2SSetFluidFilterSlot.java create mode 100644 src/main/java/com/enderio/base/common/network/C2SSetItemFilterSlot.java diff --git a/src/main/java/com/enderio/base/common/init/EIOPackets.java b/src/main/java/com/enderio/base/common/init/EIOPackets.java index 60e469b118..a4cf0caca1 100644 --- a/src/main/java/com/enderio/base/common/init/EIOPackets.java +++ b/src/main/java/com/enderio/base/common/init/EIOPackets.java @@ -1,6 +1,8 @@ package com.enderio.base.common.init; import com.enderio.base.common.network.AddTravelTargetPacket; +import com.enderio.base.common.network.C2SSetFluidFilterSlot; +import com.enderio.base.common.network.C2SSetItemFilterSlot; import com.enderio.base.common.network.FilterUpdatePacket; import com.enderio.base.common.network.RemoveTravelTargetPacket; import com.enderio.base.common.network.RequestTravelPacket; @@ -20,6 +22,8 @@ public static void register() { CoreNetwork.registerPacket(new RemoveTravelTargetPacket.Handler(), RemoveTravelTargetPacket.class); CoreNetwork.registerPacket(new RequestTravelPacket.Handler(), RequestTravelPacket.class); CoreNetwork.registerPacket(new FilterUpdatePacket.Handler(), FilterUpdatePacket.class); + CoreNetwork.registerPacket(new C2SSetItemFilterSlot.Handler(), C2SSetItemFilterSlot.class); + CoreNetwork.registerPacket(new C2SSetFluidFilterSlot.Handler(), C2SSetFluidFilterSlot.class); } } diff --git a/src/main/java/com/enderio/base/common/integrations/jei/FilterGhostIngredientHandler.java b/src/main/java/com/enderio/base/common/integrations/jei/FilterGhostIngredientHandler.java index 1f34c3e536..a33b484e2e 100644 --- a/src/main/java/com/enderio/base/common/integrations/jei/FilterGhostIngredientHandler.java +++ b/src/main/java/com/enderio/base/common/integrations/jei/FilterGhostIngredientHandler.java @@ -1,5 +1,7 @@ package com.enderio.base.common.integrations.jei; +import com.enderio.base.common.network.C2SSetFluidFilterSlot; +import com.enderio.base.common.network.C2SSetItemFilterSlot; import com.enderio.core.common.menu.FilterSlot; import com.enderio.core.common.menu.FluidFilterSlot; import com.enderio.core.common.menu.ItemFilterSlot; @@ -8,6 +10,7 @@ import java.util.ArrayList; import java.util.List; +import com.enderio.core.common.network.CoreNetwork; import mezz.jei.api.constants.VanillaTypes; import mezz.jei.api.forge.ForgeTypes; import mezz.jei.api.gui.handlers.IGhostIngredientHandler; @@ -21,7 +24,10 @@ public class FilterGhostIngredientHandler implements IGhostIngredientHandler List> getTargetsTyped(EIOScreen gui, ITypedIngredient ingredient, boolean doStart) { List> targets = new ArrayList<>(); - for (var slot : gui.getMenu().slots) { + + var menu = gui.getMenu(); + for (int i = 0; i < menu.slots.size(); i++) { + var slot = menu.getSlot(i); if (!slot.isActive()) { continue; } @@ -32,16 +38,16 @@ public List> getTargetsTyped(EIOScreen gui, ITypedIngredient in ItemStack currentIngredient = (ItemStack) ingredient.getIngredient(); if (slot instanceof ItemFilterSlot itemFilterSlot) { - targets.add(new ItemStackTarget<>(bounds, itemFilterSlot)); + targets.add(new ItemStackTarget<>(bounds, menu.containerId, i, itemFilterSlot)); } else if (slot instanceof FilterSlot otherFilterSlot) { // If the item can be converted to the resource, allow it to be dragged too. if (otherFilterSlot.getResourceFrom(currentIngredient).isPresent()) { - targets.add(new IndirectItemStackTarget<>(bounds, otherFilterSlot)); + targets.add(new IndirectItemStackTarget<>(bounds, menu.containerId, i, otherFilterSlot)); } } } else if (ingredient.getType() == ForgeTypes.FLUID_STACK) { if (slot instanceof FluidFilterSlot fluidFilterSlot) { - targets.add(new FluidStackTarget<>(bounds, fluidFilterSlot)); + targets.add(new FluidStackTarget<>(bounds, menu.containerId, i, fluidFilterSlot)); } } } @@ -53,7 +59,7 @@ public List> getTargetsTyped(EIOScreen gui, ITypedIngredient in public void onComplete() { } - private record ItemStackTarget(Rect2i bounds, ItemFilterSlot slot) implements Target { + private record ItemStackTarget(Rect2i bounds, int containerId, int slotIndex, ItemFilterSlot slot) implements Target { @Override public Rect2i getArea() { return bounds; @@ -62,10 +68,11 @@ public Rect2i getArea() { @Override public void accept(I ingredient) { slot.setResource((ItemStack) ingredient); + CoreNetwork.sendToServer(new C2SSetItemFilterSlot(containerId, slotIndex, (ItemStack) ingredient)); } } - private record IndirectItemStackTarget(Rect2i bounds, FilterSlot slot) implements Target { + private record IndirectItemStackTarget(Rect2i bounds, int containerId, int slotIndex, FilterSlot slot) implements Target { @Override public Rect2i getArea() { return bounds; @@ -74,10 +81,11 @@ public Rect2i getArea() { @Override public void accept(I ingredient) { slot.safeInsert((ItemStack) ingredient); + CoreNetwork.sendToServer(new C2SSetItemFilterSlot(containerId, slotIndex, (ItemStack) ingredient)); } } - private record FluidStackTarget(Rect2i bounds, FluidFilterSlot slot) implements Target { + private record FluidStackTarget(Rect2i bounds, int containerId, int slotIndex, FluidFilterSlot slot) implements Target { @Override public Rect2i getArea() { return bounds; @@ -86,6 +94,7 @@ public Rect2i getArea() { @Override public void accept(I ingredient) { slot.setResource((FluidStack) ingredient); + CoreNetwork.sendToServer(new C2SSetFluidFilterSlot(containerId, slotIndex, (FluidStack) ingredient)); } } diff --git a/src/main/java/com/enderio/base/common/network/C2SSetFluidFilterSlot.java b/src/main/java/com/enderio/base/common/network/C2SSetFluidFilterSlot.java new file mode 100644 index 0000000000..2b76fa1b08 --- /dev/null +++ b/src/main/java/com/enderio/base/common/network/C2SSetFluidFilterSlot.java @@ -0,0 +1,61 @@ +package com.enderio.base.common.network; + +import com.enderio.core.common.menu.FluidFilterSlot; +import com.enderio.core.common.network.Packet; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.network.NetworkDirection; +import net.minecraftforge.network.NetworkEvent; + +import java.util.Optional; + +public record C2SSetFluidFilterSlot(int containerId, int slotIndex, FluidStack fluidStack) implements Packet { + public C2SSetFluidFilterSlot(FriendlyByteBuf buf) { + this(buf.readInt(), buf.readInt(), buf.readFluidStack()); + } + + @Override + public boolean isValid(NetworkEvent.Context context) { + if (context.getSender() != null) { + return false; + } + + var menu = context.getSender().containerMenu; + if (menu == null || menu.containerId != containerId || slotIndex >= menu.slots.size()) { + return false; + } + + return true; + } + + @Override + public void handle(NetworkEvent.Context context) { + if (context.getSender().containerMenu.getSlot(slotIndex) instanceof FluidFilterSlot filterSlot) { + filterSlot.setResource(fluidStack); + } + } + + protected void write(FriendlyByteBuf writeInto) { + writeInto.writeInt(containerId); + writeInto.writeInt(slotIndex); + writeInto.writeFluidStack(fluidStack); + } + + public static class Handler extends PacketHandler { + + @Override + public C2SSetFluidFilterSlot fromNetwork(FriendlyByteBuf buf) { + return new C2SSetFluidFilterSlot(buf); + } + + @Override + public void toNetwork(C2SSetFluidFilterSlot packet, FriendlyByteBuf buf) { + packet.write(buf); + } + + @Override + public Optional getDirection() { + return Optional.of(NetworkDirection.PLAY_TO_SERVER); + } + } +} diff --git a/src/main/java/com/enderio/base/common/network/C2SSetItemFilterSlot.java b/src/main/java/com/enderio/base/common/network/C2SSetItemFilterSlot.java new file mode 100644 index 0000000000..82de1843ca --- /dev/null +++ b/src/main/java/com/enderio/base/common/network/C2SSetItemFilterSlot.java @@ -0,0 +1,61 @@ +package com.enderio.base.common.network; + +import com.enderio.core.common.menu.FilterSlot; +import com.enderio.core.common.network.Packet; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.world.item.ItemStack; +import net.minecraftforge.network.NetworkDirection; +import net.minecraftforge.network.NetworkEvent; + +import java.util.Optional; + +public record C2SSetItemFilterSlot(int containerId, int slotIndex, ItemStack itemStack) implements Packet { + public C2SSetItemFilterSlot(FriendlyByteBuf buf) { + this(buf.readInt(), buf.readInt(), buf.readItem()); + } + + @Override + public boolean isValid(NetworkEvent.Context context) { + if (context.getSender() != null) { + return false; + } + + var menu = context.getSender().containerMenu; + if (menu == null || menu.containerId != containerId || slotIndex >= menu.slots.size()) { + return false; + } + + return true; + } + + @Override + public void handle(NetworkEvent.Context context) { + if (context.getSender().containerMenu.getSlot(slotIndex) instanceof FilterSlot filterSlot) { + filterSlot.safeInsert(itemStack); + } + } + + protected void write(FriendlyByteBuf writeInto) { + writeInto.writeInt(containerId); + writeInto.writeInt(slotIndex); + writeInto.writeItem(itemStack); + } + + public static class Handler extends Packet.PacketHandler { + + @Override + public C2SSetItemFilterSlot fromNetwork(FriendlyByteBuf buf) { + return new C2SSetItemFilterSlot(buf); + } + + @Override + public void toNetwork(C2SSetItemFilterSlot packet, FriendlyByteBuf buf) { + packet.write(buf); + } + + @Override + public Optional getDirection() { + return Optional.of(NetworkDirection.PLAY_TO_SERVER); + } + } +}