This commit is contained in:
Ell 2022-03-30 14:48:51 +02:00
parent e3fad2bd6a
commit 58f665452d
2 changed files with 14 additions and 2 deletions

View file

@ -14,11 +14,13 @@ public class PacketRequest {
private BlockPos pos; private BlockPos pos;
private ItemStack stack; private ItemStack stack;
private int nbtHash;
private int amount; private int amount;
public PacketRequest(BlockPos pos, ItemStack stack, int amount) { public PacketRequest(BlockPos pos, ItemStack stack, int amount) {
this.pos = pos; this.pos = pos;
this.stack = stack; this.stack = stack;
this.nbtHash = stack.getTag().hashCode();
this.amount = amount; this.amount = amount;
} }
@ -30,6 +32,7 @@ public class PacketRequest {
PacketRequest packet = new PacketRequest(); PacketRequest packet = new PacketRequest();
packet.pos = buf.readBlockPos(); packet.pos = buf.readBlockPos();
packet.stack = buf.readItemStack(); packet.stack = buf.readItemStack();
packet.nbtHash = buf.readVarInt();
packet.amount = buf.readVarInt(); packet.amount = buf.readVarInt();
return packet; return packet;
} }
@ -37,6 +40,7 @@ public class PacketRequest {
public static void toBytes(PacketRequest packet, PacketBuffer buf) { public static void toBytes(PacketRequest packet, PacketBuffer buf) {
buf.writeBlockPos(packet.pos); buf.writeBlockPos(packet.pos);
buf.writeItemStack(packet.stack); buf.writeItemStack(packet.stack);
buf.writeVarInt(packet.nbtHash);
buf.writeVarInt(packet.amount); buf.writeVarInt(packet.amount);
} }
@ -48,7 +52,7 @@ public class PacketRequest {
PlayerEntity player = ctx.get().getSender(); PlayerEntity player = ctx.get().getSender();
ItemTerminalTileEntity tile = Utility.getTileEntity(ItemTerminalTileEntity.class, player.world, message.pos); ItemTerminalTileEntity tile = Utility.getTileEntity(ItemTerminalTileEntity.class, player.world, message.pos);
message.stack.setCount(message.amount); message.stack.setCount(message.amount);
tile.requestItem(player, message.stack); tile.requestItem(player, message.stack, message.nbtHash);
} }
}); });
ctx.get().setPacketHandled(true); ctx.get().setPacketHandled(true);

View file

@ -154,10 +154,18 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
} }
} }
public void requestItem(PlayerEntity player, ItemStack stack) { public void requestItem(PlayerEntity player, ItemStack stack, int nbtHash) {
PipeNetwork network = PipeNetwork.get(this.world); PipeNetwork network = PipeNetwork.get(this.world);
network.startProfile("terminal_request_item"); network.startProfile("terminal_request_item");
this.updateItems(); this.updateItems();
if (nbtHash != 0) {
ItemStack filter = stack;
stack = this.networkItems.values().stream()
.map(NetworkItem::asStack)
// don't compare with nbt equality here or the whole hashing thing is pointless
.filter(s -> ItemEquality.compareItems(s, filter) && s.getTag().hashCode() == nbtHash)
.findFirst().orElse(filter);
}
int requested = this.requestItemImpl(stack, onItemUnavailable(player)); int requested = this.requestItemImpl(stack, onItemUnavailable(player));
if (requested > 0) { if (requested > 0) {
player.sendMessage(new TranslationTextComponent("info." + PrettyPipes.ID + ".sending", requested, stack.getDisplayName()).setStyle(Style.EMPTY.setFormatting(TextFormatting.GREEN)), UUID.randomUUID()); player.sendMessage(new TranslationTextComponent("info." + PrettyPipes.ID + ".sending", requested, stack.getDisplayName()).setStyle(Style.EMPTY.setFormatting(TextFormatting.GREEN)), UUID.randomUUID());