|
3 | 3 | import com.mojang.datafixers.util.Function3; |
4 | 4 | import it.unimi.dsi.fastutil.objects.Object2LongMap; |
5 | 5 | import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap; |
| 6 | +import java.util.Optional; |
| 7 | +import mekanism.common.lib.inventory.HashedItem; |
6 | 8 | import mekanism.common.lib.inventory.HashedItem.UUIDAwareHashedItem; |
7 | 9 | import mekanism.common.network.IMekanismPacket; |
8 | | -import mekanism.common.network.PacketUtils; |
9 | | -import net.minecraft.network.FriendlyByteBuf; |
| 10 | +import net.minecraft.core.UUIDUtil; |
10 | 11 | import net.minecraft.network.RegistryFriendlyByteBuf; |
11 | 12 | import net.minecraft.network.codec.ByteBufCodecs; |
12 | 13 | import net.minecraft.network.codec.StreamCodec; |
|
17 | 18 | // we can also send a smaller packet to each client until they disconnect and then we clear what packets they know |
18 | 19 | public abstract class PacketQIOItemViewerGuiSync implements IMekanismPacket { |
19 | 20 |
|
| 21 | + private static final StreamCodec<RegistryFriendlyByteBuf, Object2LongMap<UUIDAwareHashedItem>> ITEM_MAP_CODEC = ByteBufCodecs.map(Object2LongOpenHashMap::new, |
| 22 | + StreamCodec.composite( |
| 23 | + ItemStack.STREAM_CODEC, HashedItem::getInternalStack, |
| 24 | + ByteBufCodecs.optional(UUIDUtil.STREAM_CODEC), item -> Optional.ofNullable(item.getUUID()), |
| 25 | + (stack, uuid) -> new UUIDAwareHashedItem(stack, uuid.orElse(null)) |
| 26 | + ), ByteBufCodecs.VAR_LONG |
| 27 | + ); |
| 28 | + |
20 | 29 | protected static <PKT extends PacketQIOItemViewerGuiSync> StreamCodec<RegistryFriendlyByteBuf, PKT> streamCodec( |
21 | 30 | Function3<Long, Integer, Object2LongMap<UUIDAwareHashedItem>, PKT> constructor) { |
22 | 31 | return StreamCodec.composite( |
23 | 32 | ByteBufCodecs.VAR_LONG, pkt -> pkt.countCapacity, |
24 | 33 | ByteBufCodecs.VAR_INT, pkt -> pkt.typeCapacity, |
25 | | - StreamCodec.of( |
26 | | - (buffer, itemMap) -> buffer.writeMap(itemMap, (buf, item) -> { |
27 | | - ItemStack.STREAM_CODEC.encode(buffer, item.getInternalStack()); |
28 | | - //Shouldn't be null unless something failed, but if it does try to handle it relatively gracefully |
29 | | - buf.writeNullable(item.getUUID(), (b, u) -> b.writeUUID(u)); |
30 | | - }, FriendlyByteBuf::writeVarLong), |
31 | | - buffer -> buffer.readMap(Object2LongOpenHashMap::new, buf -> new UUIDAwareHashedItem(ItemStack.STREAM_CODEC.decode(buffer), |
32 | | - buf.readNullable(b -> b.readUUID())), FriendlyByteBuf::readVarLong) |
33 | | - ), pkt -> pkt.itemMap, |
| 34 | + ITEM_MAP_CODEC, pkt -> pkt.itemMap, |
34 | 35 | constructor |
35 | 36 | ); |
36 | 37 | } |
|
0 commit comments