Last active
November 13, 2019 18:56
-
-
Save GameplayJDK/84e4e4abe855c21fe2df to your computer and use it in GitHub Desktop.
package de.GameplayJDK.Code.Util;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// --Snip-- | |
new de.GameplayJDK.Code.Util.Timer(owner) | |
.setTimerHandler(new de.GameplayJDK.Code.Util.Timer.TimerHandler() { // Start, Stop and Tick actions | |
@Override | |
public void onStart(de.GameplayJDK.Code.Util.Timer parent) { | |
// Do something when it starts.. | |
} | |
@Override | |
public void onFinish(de.GameplayJDK.Code.Util.Timer parent) { | |
// Or when it's finished.. | |
} | |
@Override | |
public void onCount(de.GameplayJDK.Code.Util.Timer parent) { | |
// And do something when it is counting! | |
} | |
}) | |
.setInterval(20L) // count -1 down every 20 ticks (servers mainly run with 20 TPS) | |
.setLength(60) // count from 60 to 0 --> one minute | |
.addTimerAction(30, new de.GameplayJDK.Code.Util.Timer.TimerActionHandler() { // TimerAction at 30 seconds | |
@Override | |
public void onAction(de.GameplayJDK.Code.Util.Timer parent) { | |
// Now 30s are over.. Time to do something! | |
} | |
}) | |
.addTimerAction(5, new de.GameplayJDK.Code.Util.Timer.TimerActionHandler() { // TimerAction at 5 seconds | |
@Override | |
public void onAction(de.GameplayJDK.Code.Util.Timer parent) { | |
// Oh! Only 5 seconds left! Hurry up!!! | |
} | |
}) | |
.start(); | |
// --Snip-- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Published through <a href="https://gist.github.com/">Github gist service</a> by GameplayJDK (gpn) | |
* Working with all known CraftBukkit server versions (05/21/2014;MC-1.7.9;CB-1.7.9-R0.1) | |
*/ | |
package de.GameplayJDK.Code.Util; | |
import java.util.HashMap; | |
import org.bukkit.plugin.Plugin; | |
import org.bukkit.scheduler.BukkitRunnable; | |
import org.bukkit.scheduler.BukkitTask; | |
/** | |
* @author GameplayJDK | |
* No License. Open Source. For Free. | |
* Usage examples: <a href="">Bukkit Forums</a> | |
* (This class uses MethodChaining <a href="http://en.wikipedia.org/wiki/Method_chaining">Wikipedia</a>) | |
*/ | |
public class Timer { | |
/* Public interfaces */ | |
/** | |
* @author GameplayJDK | |
* Interface to store an action at a specific time | |
*/ | |
public static interface TimerActionHandler { | |
public void onAction(Timer parent); | |
} | |
/** | |
* @author GameplayJDK | |
* Interface to store actions on start, finish and count | |
*/ | |
public static interface TimerHandler { | |
public void onStart(Timer parent); | |
public void onFinish(Timer parent); | |
public void onCount(Timer parent); | |
} | |
/* Public enums */ | |
/** | |
* @author GameplayJDK | |
* Describes the state of the timer | |
*/ | |
public static enum TimerState { | |
PREINIT, | |
RUNNING, | |
PAUSED, | |
FINISHED | |
} | |
/* Private variables */ | |
private Plugin plugin; | |
private TimerHandler handler; | |
private Long interval; | |
private Integer length; | |
private HashMap<Integer, TimerActionHandler> actions; | |
private Integer count; | |
private BukkitTask task; | |
private TimerState state; | |
/* Constructor */ | |
/** | |
* Default constructor | |
* @param plugin The plugin which is running the timer | |
*/ | |
public Timer(Plugin plugin) { this.state = TimerState.PREINIT; | |
this.actions = new HashMap<Integer, TimerActionHandler>(); | |
this.plugin = plugin; | |
} | |
/* Public methods */ | |
/** | |
* Sets the TimerHandler of the Timer | |
* @param handler A TimerHandler which stores actions on start, finish and count {@link TimerHandler} | |
* @return The timer itself with changes applied | |
*/ | |
public Timer setTimerHandler(TimerHandler handler) { this.handler = handler; | |
return this; } | |
/** | |
* Sets the interval of the timer | |
* @param interval A Long which equals the interval (in ticks - default: 20L tps = 1s) | |
* @return The timer itself with changes applied | |
*/ | |
public Timer setInterval(Long interval) { this.interval = interval; | |
return this; } | |
/** | |
* Sets the length and count of the timer | |
* @param length An Integer which equals the length (in seconds) | |
* @return The timer itself with changes applied | |
*/ | |
public Timer setLength(Integer length) { this.length = length; this.count = length; | |
return this; } | |
/** | |
* Adds a new TimerActionHandler to the timer | |
* @param at Run the specified action at this count value | |
* @param action A TimeActionHandler which stores an action at a specific time {@link TimerActionHandler} | |
* @return The timer itself with changes applied | |
*/ | |
public Timer addTimerAction(Integer at, TimerActionHandler action) { this.actions.put(at, action); | |
return this; } | |
/** | |
* Removes an existing TimerActionHandler interval of the timer | |
* @param at Remove the TimerActionHandler with this count value | |
* @return The timer itself with changes applied | |
*/ | |
public Timer removeTimerAction(Integer at) { if (this.actions.containsKey(at)) { this.actions.remove(at); } | |
return this; } | |
/** | |
* Sets the state of the timer to {@link TimerState.RUNNING} and starts the BukkitTask of the timer | |
* @return The timer itself with changes applied | |
*/ | |
public Timer start() { | |
this.state = TimerState.RUNNING; | |
handler.onStart(self()); | |
this.task = new BukkitRunnable() { | |
@Override | |
public void run() { | |
if (state == TimerState.PAUSED) { return; } else if (state == TimerState.PREINIT) { task.cancel(); return; } else if (state == TimerState.FINISHED) { task.cancel(); return; } | |
if (count <1) { | |
state = TimerState.FINISHED; | |
handler.onFinish(self()); | |
task.cancel(); | |
} | |
handler.onCount(self()); | |
if (actions.containsKey(count)) { actions.get(count).onAction(self()); } | |
count--; | |
} | |
}.runTaskTimer(this.plugin, this.interval, this.interval); | |
return this; } | |
/** | |
* Sets the state of the timer to {@link TimerState.PAUSED} | |
* @return The timer itself with changes applied | |
*/ | |
public Timer pause() { this.state = TimerState.PAUSED; | |
return this; } | |
/** | |
* Sets the state of the timer to {@link TimerState.FINISHED} and cancels the BukkitTask of it | |
* @return The timer itself with changes applied | |
*/ | |
public Timer stop() { if (this.state != TimerState.FINISHED || this.task != null) { this.state = TimerState.FINISHED; this.task.cancel(); } | |
return this; } | |
/* Private methods */ | |
/** | |
* Represents the Timer object (to allow usage of "this") | |
* @return The timer itself | |
*/ | |
private Timer self() { | |
return this; } | |
/* Getters */ | |
/** | |
* The Plugin that owns the timer | |
* @return The timers Plugin | |
*/ | |
public Plugin getPlugin() { | |
return this.plugin; } | |
/** | |
* The TimerHandler of the timer | |
* @return The timers TimerHandler | |
*/ | |
public final TimerHandler getTimerHandler() { | |
return this.handler; } | |
/** | |
* The value of the interval of the timer | |
* @return The timers interval (L) | |
*/ | |
public final Long getInterval() { | |
return this.interval; } | |
/** | |
* The start length of the timer | |
* @return The timers length | |
*/ | |
public final Integer getLength() { | |
return this.length; } | |
/** | |
* All actions which are applied to the timer | |
* @return The timers actions | |
*/ | |
public final HashMap<Integer, TimerActionHandler> getActions() { | |
return this.actions; } | |
/** | |
* The current count of the timer | |
* @return The timers count | |
*/ | |
public final Integer getCount() { | |
return this.count; } | |
/** | |
* The BukkitTask object of the timer | |
* @return The timers BukkitTask | |
*/ | |
public final BukkitTask getTask() { | |
return this.task; } | |
/** | |
* The current state of the timer | |
* @return The timers state | |
*/ | |
public final TimerState getState() { | |
return this.state; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment