-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Filter items not persisting when dragged in from JEI/EMI.
Fixes: GH-906
- Loading branch information
Showing
4 changed files
with
142 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/com/enderio/base/common/network/C2SSetFluidFilterSlot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/com/enderio/base/common/network/C2SSetItemFilterSlot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |