Skip to content

Commit

Permalink
fix: Filter items not persisting when dragged in from JEI/EMI.
Browse files Browse the repository at this point in the history
Fixes: GH-906
  • Loading branch information
Rover656 committed Dec 9, 2024
1 parent 451f0c6 commit 3398d8c
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/enderio/base/common/init/EIOPackets.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);

}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -21,7 +24,10 @@ public class FilterGhostIngredientHandler implements IGhostIngredientHandler<EIO
@Override
public <I> List<Target<I>> getTargetsTyped(EIOScreen gui, ITypedIngredient<I> ingredient, boolean doStart) {
List<Target<I>> 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;
}
Expand All @@ -32,16 +38,16 @@ public <I> List<Target<I>> getTargetsTyped(EIOScreen gui, ITypedIngredient<I> 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));
}
}
}
Expand All @@ -53,7 +59,7 @@ public <I> List<Target<I>> getTargetsTyped(EIOScreen gui, ITypedIngredient<I> in
public void onComplete() {
}

private record ItemStackTarget<I>(Rect2i bounds, ItemFilterSlot slot) implements Target<I> {
private record ItemStackTarget<I>(Rect2i bounds, int containerId, int slotIndex, ItemFilterSlot slot) implements Target<I> {
@Override
public Rect2i getArea() {
return bounds;
Expand All @@ -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<I>(Rect2i bounds, FilterSlot<?> slot) implements Target<I> {
private record IndirectItemStackTarget<I>(Rect2i bounds, int containerId, int slotIndex, FilterSlot<?> slot) implements Target<I> {
@Override
public Rect2i getArea() {
return bounds;
Expand All @@ -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<I>(Rect2i bounds, FluidFilterSlot slot) implements Target<I> {
private record FluidStackTarget<I>(Rect2i bounds, int containerId, int slotIndex, FluidFilterSlot slot) implements Target<I> {
@Override
public Rect2i getArea() {
return bounds;
Expand All @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<C2SSetFluidFilterSlot> {

@Override
public C2SSetFluidFilterSlot fromNetwork(FriendlyByteBuf buf) {
return new C2SSetFluidFilterSlot(buf);
}

@Override
public void toNetwork(C2SSetFluidFilterSlot packet, FriendlyByteBuf buf) {
packet.write(buf);
}

@Override
public Optional<NetworkDirection> getDirection() {
return Optional.of(NetworkDirection.PLAY_TO_SERVER);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<C2SSetItemFilterSlot> {

@Override
public C2SSetItemFilterSlot fromNetwork(FriendlyByteBuf buf) {
return new C2SSetItemFilterSlot(buf);
}

@Override
public void toNetwork(C2SSetItemFilterSlot packet, FriendlyByteBuf buf) {
packet.write(buf);
}

@Override
public Optional<NetworkDirection> getDirection() {
return Optional.of(NetworkDirection.PLAY_TO_SERVER);
}
}
}

0 comments on commit 3398d8c

Please sign in to comment.