support reloads by stopping cleanuptimer

This commit is contained in:
Govindas 2021-04-09 12:56:18 +03:00
parent b18fc1f8cf
commit b1e078493a
2 changed files with 10 additions and 2 deletions

View File

@ -10,10 +10,12 @@ import net.govindas.skooldown.expressions.ExprCooldown;
import net.govindas.skooldown.utilities.CleanupTimer; import net.govindas.skooldown.utilities.CleanupTimer;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.util.Timer;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
public final class Skooldown extends JavaPlugin { public final class Skooldown extends JavaPlugin {
public static ConcurrentHashMap<String, Long> cooldowns = new ConcurrentHashMap<String, Long>(); public static ConcurrentHashMap<String, Long> cooldowns = new ConcurrentHashMap<String, Long>();
private Timer cleanupTimer;
@Override @Override
public void onEnable() { public void onEnable() {
@ -27,12 +29,16 @@ public final class Skooldown extends JavaPlugin {
Skript.registerExpression(ExprCooldown.class, Timespan.class, ExpressionType.PROPERTY, "cooldown %string%"); Skript.registerExpression(ExprCooldown.class, Timespan.class, ExpressionType.PROPERTY, "cooldown %string%");
getLogger().info("Skript addon enabled!"); getLogger().info("Skript addon enabled!");
new CleanupTimer();
cleanupTimer = new CleanupTimer().start();
} }
@Override @Override
public void onDisable() { public void onDisable() {
cooldowns = null; cooldowns = null;
getLogger().info("Skript addon disabled!"); getLogger().info("Skript addon disabled!");
cleanupTimer.cancel();
cleanupTimer = null;
} }
} }

View File

@ -11,7 +11,8 @@ import java.util.logging.Level;
public class CleanupTimer { public class CleanupTimer {
//Regular Cleanup Timer to prevent memory leaks //Regular Cleanup Timer to prevent memory leaks
public CleanupTimer() { public CleanupTimer() {}
public Timer start() {
Timer timer = new Timer(); Timer timer = new Timer();
TimerTask regularTask = new TimerTask() { TimerTask regularTask = new TimerTask() {
int i = 0; int i = 0;
@ -39,5 +40,6 @@ public class CleanupTimer {
//regular schedule //regular schedule
int minutes = 30; int minutes = 30;
timer.schedule(regularTask, 100, 1000 * 60 * minutes); timer.schedule(regularTask, 100, 1000 * 60 * minutes);
return timer;
} }
} }