Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package puppy.hanii.bukkit.library.misc;
- import org.bukkit.entity.Player;
- import org.bukkit.event.entity.PotionSplashEvent;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.scheduler.BukkitRunnable;
- // NOTE: There are:
- // 20 ticks per second.
- // 1,200 ticks per minute.
- // 72,000 ticks per hour.
- /**
- * Generic count-down class the performs a specified action at every decrement and upon completion.
- * @author Hanii Puppy <[email protected]>
- */
- public abstract class CountDown extends BukkitRunnable
- {
- //<editor-fold defaultstate="collapsed" desc="Initialisation">
- /**
- * Constructs the countdown.
- * @param plugin The instance of the plugin this countdown is being used in.
- * @param Count The current decrement of the countdown.
- * @param TicksPerCount The number of ticks per decrement. One second is 20 ticks.
- */
- public CountDown(JavaPlugin plugin, int Count, int TicksPerCount)
- {
- this.plugin = plugin;
- this.Max = Count;
- this.Current = Count;
- this.TicksPerCount = TicksPerCount;
- this.started = false;
- this.paused = false;
- }
- /**
- * Constructs the countdown. Defaults decrement time to one second.
- * @param plugin The instance of the plugin this countdown is being used in.
- * @param Count The current decrement of the countdown.
- */
- public CountDown(JavaPlugin plugin, int Count)
- { this(plugin, Count, 20); }
- /**
- * Constructs the countdown. Defaults decrement time to one second, and the count-down to ten.
- * @param plugin The instance of the plugin this countdown is being used in.
- */
- public CountDown(JavaPlugin plugin)
- { this(plugin, 10, 20); }
- //</editor-fold>
- //<editor-fold defaultstate="collapsed" desc="Variables">
- int Current;
- int Max;
- int TicksPerCount;
- boolean started;
- JavaPlugin plugin;
- boolean paused;
- //</editor-fold>
- //<editor-fold defaultstate="collapsed" desc="Methods">
- //<editor-fold defaultstate="collapsed" desc="Getters/Setters">
- /**
- * Gets the current decrement of the countdown.
- * @return The current decrement.
- */
- public int getCurrentCount()
- { return Current; }
- /**
- * Gets the number the countdown started at.
- * @return The start number.
- */
- public int getMaxCount()
- { return Max; }
- /**
- * Gets the number of ticks per count.
- * @return The ticks per count.
- */
- public int getTicksPerCount()
- { return TicksPerCount; }
- /**
- * Gets whether or not the countdown has started.
- * @return True if it has, else false.
- */
- public boolean hasStarted()
- { return started; }
- /**
- * Gets the instance for the plugin this countdown is being used in.
- * @return
- */
- public JavaPlugin getPlugin()
- { return plugin; }
- /**
- * Sets a new number to start the countdown from.
- * @param newMax The new number.
- * @return Whether or not the operation was completed successfully. False if the countdown had already started and this was now immutable.
- */
- public boolean setMaxCount(int newMax)
- {
- if(!started)
- {
- Max = newMax;
- Current = newMax;
- return true;
- }
- else
- return false;
- }
- /**
- * Gets the number of ticks per decrement.
- * @param ticks The number of ticks.
- * @return Whether or not the operation was completed successfully. False if the countdown had already started and this was now immutable.
- */
- public boolean setTicksPerCount(int ticks)
- {
- if(!started)
- {
- TicksPerCount = ticks;
- return true;
- }
- else
- return false;
- }
- //</editor-fold>
- /**
- * Starts the countdown, and renders the object immutable.
- */
- public void start()
- {
- started = true;
- onCount(this, Current);
- decrement();
- }
- public boolean pause()
- {
- if(!paused)
- {
- paused = true;
- return true;
- }
- else
- return false;
- }
- public boolean unpause()
- {
- if(paused)
- {
- paused = false;
- decrement();
- return true;
- }
- else
- return false;
- }
- /**
- * Starts the countdown, and renders the object immutable.
- */
- @Override
- public void run()
- { start(); }
- /**
- * Reduces the countdown's current value by one, and finishes it if the new current value is 0. If it's not, calls it again after the specified number of ticks has passed.
- */
- void decrement()
- {
- if(!paused)
- new BukkitRunnable()
- {
- @Override
- public void run()
- {
- Current--;
- onCount(CountDown.this, Current);
- if(Current <= 0)
- onFinish(CountDown.this);
- else
- decrement();
- }
- }.runTaskLater(plugin, TicksPerCount);
- }
- //<editor-fold defaultstate="collapsed" desc="Abstract Methods">
- /**
- * The action to perform each time the countdown is decremented.
- * @param countDown The countdown decremented.
- * @param currentCount The new count.
- */
- protected abstract void onCount(CountDown countDown, int currentCount);
- /**
- * The action to perform when the countdown finishes.
- * @param countDown The countdown finished.
- */
- protected abstract void onFinish(CountDown countDown);
- //</editor-fold>
- //</editor-fold>
- }
Advertisement
Add Comment
Please, Sign In to add comment