Advertisement
Guest User

Packet Class

a guest
Jan 27th, 2022
26
213 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package net.mcreator.modid;
  2.  
  3. import net.minecraftforge.fml.network.NetworkEvent;
  4. import net.minecraftforge.common.MinecraftForge;
  5.  
  6. import net.minecraft.network.PacketBuffer;
  7. import net.minecraft.entity.player.ServerPlayerEntity;
  8.  
  9. import net.mcreator.modid.ModIdMod;
  10.  
  11. import java.util.function.Supplier;
  12. import java.util.function.BiConsumer;
  13. import java.util.function.Function;
  14.  
  15.  
  16. public class TextFieldPacketHandler {
  17.     public TextFieldPacketHandler() {
  18.         addNetworkMessage((int) 6554/*Random number :P*/, TextFieldDataSyncMessage.class, TextFieldDataSyncMessage::buffer, TextFieldDataSyncMessage::new, TextFieldDataSyncMessage::handler);
  19.         MinecraftForge.EVENT_BUS.register(this);
  20.     }
  21.  
  22.     public <T> void addNetworkMessage(int id, Class<T> messageType, BiConsumer<T, PacketBuffer> encoder, Function<PacketBuffer, T> decoder,
  23.             BiConsumer<T, Supplier<NetworkEvent.Context>> messageConsumer) {
  24.         ModIdMod.PACKET_HANDLER.registerMessage(id, messageType, encoder, decoder, messageConsumer);
  25.     }
  26.  
  27.  
  28.     public static class TextFieldDataSyncMessage {
  29.         public String data;
  30.         public TextFieldDataSyncMessage(PacketBuffer buffer) {
  31.             this.data = "";
  32.             this.data = (buffer.readString());
  33.         }
  34.  
  35.         public TextFieldDataSyncMessage(String data) {
  36.             this.data = data;
  37.         }
  38.  
  39.         public static void buffer(TextFieldDataSyncMessage message, PacketBuffer buffer) {
  40.             buffer.writeString(message.data);
  41.         }
  42.  
  43.         public static void handler(TextFieldDataSyncMessage message, Supplier<NetworkEvent.Context> contextSupplier) {
  44.             NetworkEvent.Context context = contextSupplier.get();
  45.             context.enqueueWork(() -> {
  46.                 if (context.getDirection().getReceptionSide().isServer()) {
  47.                     ServerPlayerEntity sender = context.getSender();
  48.                     if (sender != null) {
  49.                         sender.getCapability(ModIdModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
  50.                             capability.TmpTextFInput = message.data;
  51.                             capability.syncPlayerVariables(sender);
  52.                         });
  53.  
  54.                     }
  55.                 }
  56.             });
  57.             context.setPacketHandled(true);
  58.         }
  59.     }
  60. }
  61.  
Advertisement
RAW Paste Data Copied
Advertisement