Advertisement
Exception_Prototype

Untitled

Mar 3rd, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. public final class ClientTickEvent {
  2.  
  3.     private static final Minecraft mc = Minecraft.getMinecraft();
  4.  
  5.     private CompletableFuture<InfoMessage> future;
  6.  
  7.     private final Consumer<InfoMessage> consumer;
  8.     private final int frequency;
  9.     private int ticks;
  10.  
  11.     private ClientTickEvent(int frequency, Consumer<InfoMessage> consumer) {
  12.         this.frequency = frequency * 20;
  13.         this.consumer = consumer;
  14.         this.ticks = 0;
  15.     }
  16.  
  17.     @SubscribeEvent
  18.     public void tick(TickEvent.ClientTickEvent event) {
  19.         if (mc.thePlayer != null && event.phase == TickEvent.Phase.END) {
  20.  
  21.             if (this.ticks++ == this.frequency) {
  22.                 this.future = CompletableFuture.supplyAsync(ClientProxy::findProcesses);
  23.                 this.ticks = 0;
  24.             }
  25.  
  26.             if ((this.future != null) && (this.future.isDone())) {
  27.                 try {
  28.                     InfoMessage message = this.future.get();
  29.                     if (message.isValid()) {
  30.                         this.consumer.accept(message);
  31.                     }
  32.                     this.future = null;
  33.                 } catch (InterruptedException | ExecutionException e) {
  34.                     //ignore
  35.                 }
  36.             }
  37.  
  38.         }
  39.     }
  40.  
  41.     @SubscribeEvent
  42.     public void disconnect(FMLNetworkEvent.ClientDisconnectionFromServerEvent event) {
  43.         try {
  44.             if (this.future != null)
  45.                 this.future.cancel(false);
  46.         } catch (Exception e) {
  47.             //
  48.         }
  49.         unregister(this);
  50.     }
  51.  
  52.     static void doChecks(int frequency, Consumer<InfoMessage> consumer) {
  53.         register(new ClientTickEvent(frequency, consumer));
  54.     }
  55.  
  56.     private static void register(Object object) {
  57.         if (object != null) {
  58.             MinecraftForge.EVENT_BUS.register(object);
  59.             FMLCommonHandler.instance().bus().register(object);
  60.         }
  61.     }
  62.  
  63.     private static void unregister(Object object) {
  64.         if (object != null) {
  65.             MinecraftForge.EVENT_BUS.unregister(object);
  66.             FMLCommonHandler.instance().bus().unregister(object);
  67.         }
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement