Advertisement
Guest User

Event listener

a guest
Oct 5th, 2022
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | Source Code | 0 0
  1. package net.mcreator.modid;
  2.  
  3. import net.minecraftforge.fml.common.Mod;
  4. import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
  5. import net.minecraftforge.fml.network.NetworkRegistry;
  6. import net.minecraftforge.eventbus.api.SubscribeEvent;
  7. import net.minecraftforge.api.distmarker.OnlyIn;
  8. import net.minecraftforge.api.distmarker.Dist;
  9. import net.minecraftforge.client.event.InputEvent;
  10.  
  11. import net.minecraft.client.Minecraft;
  12. import net.minecraft.client.gui.widget.TextFieldWidget;
  13. import net.minecraft.client.gui.screen.inventory.ContainerScreen;
  14. import net.minecraft.entity.player.PlayerEntity;
  15.  
  16. import net.mcreator.modid.gui.YourGuiWindow;
  17. import net.mcreator.modid.CustomPacketHandler;
  18. import net.mcreator.modid.YourMod;
  19.  
  20.  
  21. @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
  22. public class OnKeyPressed {
  23.     public OnKeyPressed() {
  24.     }
  25.  
  26.     @SubscribeEvent
  27.     public static void init(FMLCommonSetupEvent event) {
  28.         new OnKeyPressed();
  29.     }
  30.     @Mod.EventBusSubscriber
  31.     private static class ForgeBusEvents {
  32.         @OnlyIn(Dist.CLIENT)
  33.         @SubscribeEvent
  34.         public static void keyPressed(InputEvent.KeyInputEvent e) {
  35.             if (Minecraft.getInstance().currentScreen != null) {
  36.                         if (Minecraft.getInstance().currentScreen instanceof YourGuiWindow) {
  37.                             if (Minecraft.getInstance().currentScreen != null) {
  38.                                 YourGuiWindow win = (YourGuiWindow)((ContainerScreen)Minecraft.getInstance().currentScreen);
  39.                                 String tmpText = (String) (new Object() {
  40.                                     public String getText() {
  41.                                         TextFieldWidget _tf = (TextFieldWidget) win.Number;
  42.                                         if (_tf != null) {
  43.                                             return _tf.getText();
  44.                                         }
  45.                                     return "";
  46.                                     }
  47.                                 }.getText());
  48.                                 YourMod.PACKET_HANDLER.sendToServer(new CustomPacketHandler.TextFieldDataSyncMessage(tmpText));
  49.                         }
  50.                     }
  51.                 }
  52.             }
  53.  
  54.         @OnlyIn(Dist.CLIENT)
  55.         @SubscribeEvent
  56.         public static void onClick(InputEvent.MouseInputEvent e) {
  57.             if (Minecraft.getInstance().currentScreen != null) {
  58.                     if (Minecraft.getInstance().currentScreen instanceof YourGuiWindow) {
  59.                         if (Minecraft.getInstance().currentScreen != null) {
  60.                             YourGuiWindow win = (YourGuiWindow)((ContainerScreen)Minecraft.getInstance().currentScreen);
  61.                             String tmpText = (String) (new Object() {
  62.                                 public String getText() {
  63.                                     TextFieldWidget _tf = (TextFieldWidget) win.Number;
  64.                                     if (_tf != null) {
  65.                                         return _tf.getText();
  66.                                     }
  67.                                 return "";
  68.                                 }
  69.                             }.getText());
  70.                             YourMod.PACKET_HANDLER.sendToServer(new CustomPacketHandler.TextFieldDataSyncMessage(tmpText));
  71.                         }
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.     }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement