From 9959aa78b9d8f69ae3086af6fdee38efcb04b5ba Mon Sep 17 00:00:00 2001 From: Sydney Date: Wed, 8 Jan 2020 19:38:41 -0500 Subject: [PATCH] Make the task remember how much time it has remaining. --- build.gradle | 2 +- .../blockregen/BlockRegenPlugin.java | 29 +++++++++---------- .../blockregen/BlockRegenTask.java | 18 +++++++++++- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/build.gradle b/build.gradle index 154e528..bb99559 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group = 'town.championsofequestria' -version = '2' +version = '3' repositories { jcenter() diff --git a/src/main/java/town/championsofequestria/blockregen/BlockRegenPlugin.java b/src/main/java/town/championsofequestria/blockregen/BlockRegenPlugin.java index c7502dd..d4baab1 100644 --- a/src/main/java/town/championsofequestria/blockregen/BlockRegenPlugin.java +++ b/src/main/java/town/championsofequestria/blockregen/BlockRegenPlugin.java @@ -7,8 +7,6 @@ import org.bukkit.block.Block; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.scheduler.BukkitTask; - import net.milkbowl.vault.economy.Economy; public class BlockRegenPlugin extends JavaPlugin { @@ -16,7 +14,7 @@ public class BlockRegenPlugin extends JavaPlugin { private CommandHandler ch; private Settings s; private Data d; - private static BlockRegenPlugin p; + static BlockRegenPlugin p; public static HashMap tasks; @Override @@ -29,9 +27,9 @@ public void onEnable() { getCommand("blockregen").setExecutor(ch); boolean hasEconomy = false; RegisteredServiceProvider economy = null; - if(getServer().getPluginManager().getPlugin("Vault") != null) { + if (getServer().getPluginManager().getPlugin("Vault") != null) { economy = getServer().getServicesManager().getRegistration(Economy.class); - if(economy != null) + if (economy != null) hasEconomy = true; } getServer().getPluginManager().registerEvents(new EventManager(this, s, d, hasEconomy, economy), this); @@ -41,15 +39,14 @@ public void onEnable() { @Override public void onDisable() { - for (BukkitTask task : Bukkit.getScheduler().getPendingTasks()) { - if (task.getOwner().getName().equals(this.getName())) { - s.saveTask(task.getTaskId(), tasks.get(task.getTaskId())); - task.cancel(); - tasks.remove(task.getTaskId()); - if (s.debug) - getLogger().info("Removed task " + task.getTaskId()); - } + for (BlockRegenTask task : tasks.values()) { + task.cancelMyTask(); + s.saveTask(task.taskid, task); + Bukkit.getScheduler().cancelTask(task.taskid); + if (s.debug) + getLogger().info("Removed task " + task.taskid); } + tasks.clear(); } @SuppressWarnings("deprecation") @@ -61,13 +58,13 @@ public void scheduleTask(BlockRegenTask task) { task.setTaskId(Bukkit.getScheduler().scheduleSyncDelayedTask(this, task, task.getTimeToRegenerate() * 20L)); tasks.put(task.taskid, task); } - + public String locToString(Location loc) { return String.format("%d,%d,%d:%s", loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), loc.getWorld().getName()); } - + public static void debug(String str) { - if(p.s.debug) + if (p.s.debug) p.getLogger().info(str); } } diff --git a/src/main/java/town/championsofequestria/blockregen/BlockRegenTask.java b/src/main/java/town/championsofequestria/blockregen/BlockRegenTask.java index 5b084b5..6409e97 100644 --- a/src/main/java/town/championsofequestria/blockregen/BlockRegenTask.java +++ b/src/main/java/town/championsofequestria/blockregen/BlockRegenTask.java @@ -1,5 +1,6 @@ package town.championsofequestria.blockregen; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -11,13 +12,28 @@ public class BlockRegenTask implements Runnable { private Material type; private byte data; private int seconds; - public int taskid; + int taskid; + private int myTaskId; public BlockRegenTask(Block block, Material type, byte data, int seconds) { this.block = block; this.type = type; this.data = data; this.seconds = seconds; + this.myTaskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(BlockRegenPlugin.p, new Runnable() { + @Override + public void run() { + decrementSeconds(); + } + }, 20, seconds * 20); + } + + private void decrementSeconds() { + seconds--; + } + + public void cancelMyTask() { + Bukkit.getScheduler().cancelTask(myTaskId); } @Override