This article explains how to use the Badlion Client Timer API. Our Timer API repository has now been archived and merged with our Mod API repository, found here. To install it properly, please follow the instructions found in our other wiki article.
API Usage
Below is an example plugin with the four different timers types implemented.
public class TestPlugin extends JavaPlugin implements Listener {
private TimerApi timerApi;
private Timer tickTimer; // 1 minute tick timer
private Timer repeatingTickTimer; // 1 minute repeating tick timer
private Timer timeTimer; // 1 minute time timer
private Timer repeatingTimeTimer; // 1 minute repeating time timer
@Override
public void onEnable() {
// Get the timer api instance
this.timerApi = TimerApi.getInstance();
// Create the timers
this.tickTimer = this.timerApi.createTickTimer("Tick Timer", new ItemStack(Material.IRON_INGOT), false, 1200L);
this.repeatingTickTimer = this.timerApi.createTickTimer("Repeating Tick Timer", new ItemStack(Material.GOLD_INGOT), true, 1200L);
this.timeTimer = this.timerApi.createTimeTimer("Time Timer", new ItemStack(Material.DIAMOND), false, 1L, TimeUnit.MINUTES);
this.repeatingTimeTimer = this.timerApi.createTimeTimer("Repeating Tick Timer", new ItemStack(Material.EMERALD), true, 1L, TimeUnit.MINUTES);
// Register the listener
this.getServer().getPluginManager().registerEvents(this, this);
}
@Override
public void onDisable() {
// Remove the timers
this.timerApi.removeTimer(this.tickTimer);
this.timerApi.removeTimer(this.repeatingTickTimer);
this.timerApi.removeTimer(this.timeTimer);
this.timerApi.removeTimer(this.repeatingTimeTimer);
}
@EventHandler
public void onLogin(PlayerJoinEvent event) {
// Add the player to the timers
this.tickTimer.addReceiver(event.getPlayer());
this.repeatingTickTimer.addReceiver(event.getPlayer());
this.timeTimer.addReceiver(event.getPlayer());
this.repeatingTimeTimer.addReceiver(event.getPlayer());
}
@EventHandler
public void onLogout(PlayerQuitEvent event) {
// Remove the player from the timers
this.tickTimer.removeReceiver(event.getPlayer());
this.repeatingTickTimer.removeReceiver(event.getPlayer());
this.timeTimer.removeReceiver(event.getPlayer());
this.repeatingTimeTimer.removeReceiver(event.getPlayer());
}
}