Advertisement
Scouter456

Untitled

Aug 10th, 2023
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. public class LootFruitPacketS2C {
  2.     private static final Codec<Map<Integer, List<LootFruitCodec>>> MAPPER = Codec.unboundedMap(Codec.INT, LootFruitCodec.CODEC.listOf())
  3.             .xmap(LootFruitCodec::convertToMap, LootFruitCodec::convertFromMap)
  4.             .orElse(e -> {UnusualPrehistory.LOGGER.info("Error " + e);},
  5.                  new HashMap<>());
  6.     public static Map<Integer, List<LootFruitCodec>> SYNCED_DATA = new HashMap<>();
  7.  
  8.     private final Map<Integer, List<LootFruitCodec>> map;
  9.  
  10.     public LootFruitPacketS2C(Map<Integer, List<LootFruitCodec>> map) {
  11.         this.map = map;
  12.     }
  13.  
  14.     public void encode(FriendlyByteBuf buffer) {
  15.         CompoundTag result = (CompoundTag) (MAPPER.encodeStart(NbtOps.INSTANCE, this.map).result().orElse(new CompoundTag()));
  16.         UnusualPrehistory.LOGGER.error("Error encoding data: " + result);
  17.  
  18.  
  19.  
  20.         buffer.writeNbt(result);
  21.         UnusualPrehistory.LOGGER.info("Encoded Tag: " + result);
  22.     }
  23.  
  24.     public static LootFruitPacketS2C decode(FriendlyByteBuf buffer) {
  25.         CompoundTag receivedTag = buffer.readNbt();
  26.         UnusualPrehistory.LOGGER.info("Received Tag: " + receivedTag);
  27.         Map<Integer, List<LootFruitCodec>> decodedMap = MAPPER.parse(NbtOps.INSTANCE, receivedTag).result().orElse(new HashMap<>());
  28.         UnusualPrehistory.LOGGER.info("Decoded Map: " + decodedMap);
  29.         return new LootFruitPacketS2C(decodedMap);
  30.     }
  31.  
  32.     public void onPacketReceived(Supplier<NetworkEvent.Context> contextGetter) {
  33.         NetworkEvent.Context context = contextGetter.get();
  34.         context.enqueueWork(this::handlePacketOnMainThread);
  35.         context.setPacketHandled(true);
  36.     }
  37.  
  38.     private void handlePacketOnMainThread() {
  39.         SYNCED_DATA = this.map;
  40.         LootFruitJsonManager.setTierTrades(SYNCED_DATA);
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement