Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LootFruitPacketS2C {
- private static final Codec<Map<Integer, List<LootFruitCodec>>> MAPPER = Codec.unboundedMap(Codec.INT, LootFruitCodec.CODEC.listOf())
- .xmap(LootFruitCodec::convertToMap, LootFruitCodec::convertFromMap)
- .orElse(e -> {UnusualPrehistory.LOGGER.info("Error " + e);},
- new HashMap<>());
- public static Map<Integer, List<LootFruitCodec>> SYNCED_DATA = new HashMap<>();
- private final Map<Integer, List<LootFruitCodec>> map;
- public LootFruitPacketS2C(Map<Integer, List<LootFruitCodec>> map) {
- this.map = map;
- }
- public void encode(FriendlyByteBuf buffer) {
- CompoundTag result = (CompoundTag) (MAPPER.encodeStart(NbtOps.INSTANCE, this.map).result().orElse(new CompoundTag()));
- UnusualPrehistory.LOGGER.error("Error encoding data: " + result);
- buffer.writeNbt(result);
- UnusualPrehistory.LOGGER.info("Encoded Tag: " + result);
- }
- public static LootFruitPacketS2C decode(FriendlyByteBuf buffer) {
- CompoundTag receivedTag = buffer.readNbt();
- UnusualPrehistory.LOGGER.info("Received Tag: " + receivedTag);
- Map<Integer, List<LootFruitCodec>> decodedMap = MAPPER.parse(NbtOps.INSTANCE, receivedTag).result().orElse(new HashMap<>());
- UnusualPrehistory.LOGGER.info("Decoded Map: " + decodedMap);
- return new LootFruitPacketS2C(decodedMap);
- }
- public void onPacketReceived(Supplier<NetworkEvent.Context> contextGetter) {
- NetworkEvent.Context context = contextGetter.get();
- context.enqueueWork(this::handlePacketOnMainThread);
- context.setPacketHandled(true);
- }
- private void handlePacketOnMainThread() {
- SYNCED_DATA = this.map;
- LootFruitJsonManager.setTierTrades(SYNCED_DATA);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement