bobmarley12345

TimeTrigger.java

Nov 5th, 2021 (edited)
798
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1. package reghzy.api.utils;
  2.  
  3. import java.util.concurrent.TimeUnit;
  4.  
  5. /**
  6.  * <h2>
  7.  *     A class for helping with time-based execution of things, without requiring constant update
  8.  * </h2>
  9.  * <h3>
  10.  *     An example usage, GriefPrevention's "warning: you haven't claimed this land" when placing blocks.
  11.  * </h3>
  12.  * <p>
  13.  *     When they place a block, you called {@link TimeTrigger#canTrigger()}. If that returns true, you send them a message.
  14.  *     No matter what that returns, you always call {@link TimeTrigger#canTrigger()} after that. Example:
  15.  * <p>
  16.  * <pre>
  17.  * if ({@link TimeTrigger#canTrigger()}) {
  18.  *     sendMessage("Try claiming your land!");
  19.  * }
  20.  *
  21.  * {@link TimeTrigger#trigger()}
  22.  * </pre>
  23.  * </p>
  24.  * <p>
  25.  *     This ensures that if they keep placing blocks, they wont get spammed.
  26.  *     But if they stop placing blocks for {@link TimeTrigger#getInterval()} number of milliseconds,
  27.  *     it will message them again, and the loop continues
  28.  * </p>
  29.  *
  30.  * <p>
  31.  *     You can also use {@link TimeTrigger#getAndTrigger()} instead, like so:
  32.  * <pre>
  33.  * if ({@link TimeTrigger#getAndTrigger()}) {
  34.  *     sendMessage("Try claiming your land!");
  35.  * }
  36.  * </pre>
  37.  * </p>
  38.  * </p>
  39.  */
  40. public class TimeTrigger {
  41.     protected long interval;
  42.     protected long lastTrigger;
  43.  
  44.     /**
  45.      * Creates a time trigger with an interval of 0 (aka always trigger-able)
  46.      */
  47.     public TimeTrigger() {
  48.  
  49.     }
  50.  
  51.     /**
  52.      * Creates a time trigger with the given interval
  53.      * @param intervalMillis The minimum required time between the last execution, and the next for {@link TimeTrigger#canTrigger()} to return true.
  54.      *                       Measured in MILLISECONDS
  55.      */
  56.     public TimeTrigger(long intervalMillis) {
  57.         this.interval = intervalMillis;
  58.     }
  59.  
  60.     /**
  61.      * Creates an instance where the time is millisecond-based (1000 milliseconds in a second)
  62.      */
  63.     public static TimeTrigger fromMillis(long millis) {
  64.         return new TimeTrigger(millis);
  65.     }
  66.  
  67.     /**
  68.      * Creates an instance where the time is second-based (the seconds get converted into millis)
  69.      * @see TimeTrigger#TimeTrigger(long)
  70.      * @see TimeUnit#convert(long, TimeUnit)
  71.      */
  72.     public static TimeTrigger fromSeconds(long seconds) {
  73.         return new TimeTrigger(TimeUnit.MILLISECONDS.convert(seconds, TimeUnit.SECONDS));
  74.     }
  75.  
  76.     /**
  77.      * Creates an instance where the time is minute-based (the minutes get converted into millis)
  78.      * @see TimeTrigger#TimeTrigger(long)
  79.      * @see TimeUnit#convert(long, TimeUnit)
  80.      */
  81.     public static TimeTrigger fromMinutes(long minutes) {
  82.         return new TimeTrigger(TimeUnit.MILLISECONDS.convert(minutes, TimeUnit.MINUTES));
  83.     }
  84.  
  85.     /**
  86.      * Checks if the difference between the system time and the last trigger time is bigger than this time trigger's interval
  87.      * @return True if it is, false if not
  88.      */
  89.     public boolean canTrigger() {
  90.         return (System.currentTimeMillis() - this.lastTrigger) >= getInterval();
  91.     }
  92.  
  93.     /**
  94.      * Updates the last trigger time of this time trigger, most likely causing {@link TimeTrigger#canTrigger()} to no longer return true
  95.      */
  96.     public void trigger() {
  97.         this.lastTrigger = System.currentTimeMillis();
  98.     }
  99.  
  100.     /**
  101.      * Gets whether this can be triggered and returns it. And also triggers in the process (after getting whether this can be triggered)
  102.      * <p>
  103.      *     This essentially removes the need to use an if block on the {@link TimeTrigger#canTrigger()}
  104.      *     and then executing {@link TimeTrigger#trigger()} after the if block has exited; you can simply it in this single method
  105.      * </p>
  106.      */
  107.     public boolean getAndTrigger() {
  108.         boolean canTrigger = canTrigger();
  109.         trigger();
  110.         return canTrigger;
  111.     }
  112.  
  113.     /**
  114.      * Returns the interval that this time trigger is set to
  115.      */
  116.     public long getInterval() {
  117.         return this.interval;
  118.     }
  119.  
  120.     /**
  121.      * Sets the interval time for this time trigger
  122.      */
  123.     public void setInterval(long intervalMillis) {
  124.         this.interval = intervalMillis;
  125.     }
  126. }
Add Comment
Please, Sign In to add comment