Skip to content

Commit

Permalink
Make the task remember how much time it has remaining.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShroomAgent27 committed Jan 9, 2020
1 parent 32e79f3 commit 9959aa7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = 'town.championsofequestria'
version = '2'
version = '3'

repositories {
jcenter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
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 {

private CommandHandler ch;
private Settings s;
private Data d;
private static BlockRegenPlugin p;
static BlockRegenPlugin p;
public static HashMap<Integer, BlockRegenTask> tasks;

@Override
Expand All @@ -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);
Expand All @@ -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")
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 9959aa7

Please sign in to comment.