Created
May 29, 2012 00:45
-
-
Save Zren/2821924 to your computer and use it in GitHub Desktop.
Task Injector for Bukkit's Scheduler
This file contains hidden or 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
| package ca.xshade.bukkit.util; | |
| import org.bukkit.Bukkit; | |
| import org.bukkit.plugin.Plugin; | |
| import java.util.TimerTask; | |
| /** | |
| * A task that can be scheduled for one-time or repeated execution by a Timer that will insert the defined task into Bukkit's schedular to | |
| * run on it's next tick. | |
| * | |
| * @author Zren (Shade / Chris) | |
| * @version 1.0 | |
| */ | |
| public class InjectIntoBukkitTask extends TimerTask { | |
| /** The plugin. */ | |
| private Plugin plugin; | |
| /** The runnable. */ | |
| private Runnable runnable; | |
| /** | |
| * Instantiates a new inject into bukkit task. | |
| * | |
| * @param plugin | |
| * the plugin | |
| * @param runnable | |
| * the runnable | |
| */ | |
| public InjectIntoBukkitTask(Plugin plugin, Runnable runnable) { | |
| this.plugin = plugin; | |
| this.runnable = runnable; | |
| } | |
| /** | |
| * Inserts task into the Bukkit schedular to run on the next tick. | |
| */ | |
| @Override | |
| public void run() { | |
| Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, runnable); | |
| } | |
| } |
This file contains hidden or 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
| package ca.xshade.bukkit.util; | |
| import org.bukkit.plugin.Plugin; | |
| import java.util.Date; | |
| import java.util.Timer; | |
| /** | |
| * A middleman manager of a Timer instance that schedules a Runnable in a InjectIntoBukkitTask. | |
| * | |
| * @see java.util.Timer | |
| * | |
| * @author Zren (Shade / Chris) | |
| * @version 2.0 | |
| * | |
| * TODO: Link all sub instances statically for a static cancelAll that kills all timers? | |
| */ | |
| public class TaskInjector { | |
| /** The instance. */ | |
| private static TaskInjector instance; | |
| /** The timer. */ | |
| private Timer timer = new Timer(); | |
| /** The plugin. */ | |
| private Plugin plugin; | |
| /** | |
| * Instantiates a new task injector. | |
| * | |
| * @param plugin | |
| * the plugin | |
| */ | |
| public TaskInjector(Plugin plugin) { | |
| this.plugin = plugin; | |
| } | |
| /** | |
| * Sets the static version of this class as a new instance with the defined plugin. | |
| * | |
| * @param plugin | |
| * the plugin | |
| */ | |
| public static void newInstance(Plugin plugin) { | |
| TaskInjector.instance = new TaskInjector(plugin); | |
| } | |
| /** | |
| * A factory method to generate new instances of TaskInjector with the same plugin. | |
| * | |
| * @return a new instance of TaskInjector with the same plugin as the static instance. | |
| */ | |
| public static TaskInjector newInstance() { | |
| if (TaskInjector.instance == null) | |
| throw new IllegalArgumentException(); | |
| if (TaskInjector.instance.getTimer() == null) | |
| throw new IllegalArgumentException(); | |
| return new TaskInjector(instance.getPlugin()); | |
| } | |
| /** | |
| * Gets the timer. | |
| * | |
| * @return the timer | |
| */ | |
| public Timer getTimer() { | |
| return timer; | |
| } | |
| /** | |
| * Gets the plugin | |
| * | |
| * @return the plugin | |
| */ | |
| public Plugin getPlugin() { | |
| return plugin; | |
| } | |
| /** | |
| * Gets the current static instance of the TaskInjector | |
| * | |
| * @return the instance | |
| */ | |
| public static TaskInjector getInstance() { | |
| return instance; | |
| } | |
| /** | |
| * Terminates this timer, discarding any currently scheduled tasks. Does not interfere with a currently executing task (if it exists). | |
| * Once a timer has been terminated, its execution thread terminates gracefully, and no more tasks may be scheduled on it. | |
| * | |
| * @see java.util.Timer#cancel() | |
| */ | |
| public void cancelAll() { | |
| timer.cancel(); | |
| timer = new Timer(); | |
| } | |
| /** | |
| * Schedules the specified task for execution at the specified time. If the time is in the past, the task is scheduled for immediate | |
| * execution. | |
| * | |
| * @see java.util.Timer#schedule(java.util.TimerTask, java.util.Date) | |
| * | |
| * @param runnable | |
| * the runnable | |
| * @param time | |
| * the time | |
| */ | |
| public void schedule(Runnable runnable, Date time) { | |
| getTimer().schedule(new InjectIntoBukkitTask(plugin, runnable), time); | |
| } | |
| /** | |
| * Schedules the specified task for repeated fixed-delay execution, beginning at the specified time. Subsequent executions take place at | |
| * approximately regular intervals, separated by the specified period. | |
| * | |
| * @see java.util.Timer#schedule(java.util.TimerTask, java.util.Date, long) | |
| * | |
| * @param runnable | |
| * the runnable | |
| * @param firstTime | |
| * the first time | |
| * @param period | |
| * the period | |
| */ | |
| public void schedule(Runnable runnable, Date firstTime, long period) { | |
| getTimer().schedule(new InjectIntoBukkitTask(plugin, runnable), firstTime, period); | |
| } | |
| /** | |
| * Schedules the specified task for execution after the specified delay. | |
| * | |
| * @see java.util.Timer#schedule(java.util.TimerTask, long) | |
| * | |
| * @param runnable | |
| * the runnable | |
| * @param delay | |
| * the delay | |
| */ | |
| public void schedule(Runnable runnable, long delay) { | |
| getTimer().schedule(new InjectIntoBukkitTask(plugin, runnable), delay); | |
| } | |
| /** | |
| * Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. Subsequent executions take | |
| * place at approximately regular intervals separated by the specified period. | |
| * | |
| * @see java.util.Timer#schedule(java.util.TimerTask, long, long) | |
| * | |
| * @param runnable | |
| * the runnable | |
| * @param delay | |
| * the delay | |
| * @param period | |
| * the period | |
| */ | |
| public void schedule(Runnable runnable, long delay, long period) { | |
| getTimer().schedule(new InjectIntoBukkitTask(plugin, runnable), delay, period); | |
| } | |
| /** | |
| * Schedules the specified task for repeated fixed-rate execution, beginning at the specified time. Subsequent executions take place at | |
| * approximately regular intervals, separated by the specified period. | |
| * | |
| * @see java.util.Timer#scheduleAtFixedRate(java.util.TimerTask, java.util.Date, long) | |
| * | |
| * @param runnable | |
| * the runnable | |
| * @param firstTime | |
| * the first time | |
| * @param period | |
| * the period | |
| */ | |
| public void scheduleAtFixedRate(Runnable runnable, Date firstTime, long period) { | |
| getTimer().scheduleAtFixedRate(new InjectIntoBukkitTask(plugin, runnable), firstTime, period); | |
| } | |
| /** | |
| * Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay. Subsequent executions take place | |
| * at approximately regular intervals, separated by the specified period. | |
| * | |
| * @see java.util.Timer#scheduleAtFixedRate(java.util.TimerTask, long, long) | |
| * | |
| * @param runnable | |
| * the runnable | |
| * @param delay | |
| * the delay | |
| * @param period | |
| * the period | |
| */ | |
| public void scheduleAtFixedRate(Runnable runnable, long delay, long period) { | |
| getTimer().scheduleAtFixedRate(new InjectIntoBukkitTask(plugin, runnable), delay, period); | |
| } | |
| } |
This file contains hidden or 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
| package ca.xshade.bukkit.test; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| import java.util.concurrent.TimeUnit; | |
| import org.bukkit.Bukkit; | |
| import org.bukkit.World; | |
| import org.bukkit.plugin.java.JavaPlugin; | |
| import ca.xshade.bukkit.util.TaskInjector; | |
| public class TestBukkitPlugin extends JavaPlugin { | |
| public void onDisable() { | |
| TaskInjector.cancelAll(); | |
| System.out.println(String.format("[%s] Disabled", getDescription().getName())); | |
| } | |
| public void onEnable() { | |
| TaskInjector.newInstance(this); | |
| final Map<String, Long> worldLastTick = new HashMap<>(); | |
| final long periodSeconds = 1; | |
| final long period = TimeUnit.MILLISECONDS.convert(periodSeconds, TimeUnit.SECONDS); | |
| TaskInjector.getInstance().scheduleAtFixedRate(new Runnable() { | |
| @Override | |
| public void run() { | |
| System.out.println(String.format("Tick:", Bukkit.getServer().getWorld(""))); | |
| for (World world : Bukkit.getWorlds()) { | |
| long thisTick = world.getTime(); | |
| Long lastTick = worldLastTick.get(world.getName()); | |
| if (lastTick == null) | |
| lastTick = thisTick; | |
| long deltaTick = thisTick - lastTick; | |
| double ticksPerSecond = deltaTick / periodSeconds; | |
| System.out.println(String.format("\t%s: %.1f ticks/s (%d)", world.getName(), ticksPerSecond, thisTick)); | |
| worldLastTick.put(world.getName(), thisTick); | |
| } | |
| } | |
| }, 0L, period); | |
| System.out.println(String.format("[%s] Enabled - Version: %s", getDescription().getName(), getDescription().getVersion())); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment