diff --git a/mcmmo-api/src/main/java/com/gmail/nossr50/mcmmo/api/platform/PlatformProvider.java b/mcmmo-api/src/main/java/com/gmail/nossr50/mcmmo/api/platform/PlatformProvider.java index 8824c47f7..f47b014a0 100644 --- a/mcmmo-api/src/main/java/com/gmail/nossr50/mcmmo/api/platform/PlatformProvider.java +++ b/mcmmo-api/src/main/java/com/gmail/nossr50/mcmmo/api/platform/PlatformProvider.java @@ -1,6 +1,6 @@ package com.gmail.nossr50.mcmmo.api.platform; -import com.gmail.nossr50.mcmmo.api.platform.schedular.PlatformScheduler; +import com.gmail.nossr50.mcmmo.api.platform.scheduler.PlatformScheduler; import com.gmail.nossr50.mcmmo.api.platform.util.MetadataStore; import java.io.File; @@ -33,4 +33,6 @@ public interface PlatformProvider { void printUnsupported(); PlatformScheduler getScheduler(); + + void checkMetrics(); } diff --git a/mcmmo-api/src/main/java/com/gmail/nossr50/mcmmo/api/platform/schedular/PlatformScheduler.java b/mcmmo-api/src/main/java/com/gmail/nossr50/mcmmo/api/platform/schedular/PlatformScheduler.java deleted file mode 100644 index fc1c154d7..000000000 --- a/mcmmo-api/src/main/java/com/gmail/nossr50/mcmmo/api/platform/schedular/PlatformScheduler.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.gmail.nossr50.mcmmo.api.platform.schedular; - -public interface PlatformScheduler { - - -} diff --git a/mcmmo-api/src/main/java/com/gmail/nossr50/mcmmo/api/platform/scheduler/PlatformScheduler.java b/mcmmo-api/src/main/java/com/gmail/nossr50/mcmmo/api/platform/scheduler/PlatformScheduler.java new file mode 100644 index 000000000..cb78552ec --- /dev/null +++ b/mcmmo-api/src/main/java/com/gmail/nossr50/mcmmo/api/platform/scheduler/PlatformScheduler.java @@ -0,0 +1,55 @@ +package com.gmail.nossr50.mcmmo.api.platform.scheduler; + +import java.util.function.Consumer; + +public interface PlatformScheduler { + + TaskBuilder getTaskBuilder(); + + Task scheduleTask(TaskBuilder taskBuilder); + + + class TaskBuilder { + Integer delay; + Integer repeatTime; + + public boolean isAsync() { + return isAsync; + } + + public TaskBuilder setAsync(boolean async) { + isAsync = async; + return this; + } + + boolean isAsync = false; + Consumer task; + + public Integer getDelay() { + return delay; + } + + public TaskBuilder setDelay(Integer delay) { + this.delay = delay; + return this; + } + + public Integer getRepeatTime() { + return repeatTime; + } + + public TaskBuilder setRepeatTime(Integer repeatTime) { + this.repeatTime = repeatTime; + return this; + } + + public Consumer getTask() { + return task; + } + + public TaskBuilder setTask(Consumer task) { + this.task = task; + return this; + } + } +} diff --git a/mcmmo-api/src/main/java/com/gmail/nossr50/mcmmo/api/platform/scheduler/Task.java b/mcmmo-api/src/main/java/com/gmail/nossr50/mcmmo/api/platform/scheduler/Task.java new file mode 100644 index 000000000..e5b8e3c43 --- /dev/null +++ b/mcmmo-api/src/main/java/com/gmail/nossr50/mcmmo/api/platform/scheduler/Task.java @@ -0,0 +1,9 @@ +package com.gmail.nossr50.mcmmo.api.platform.scheduler; + +public interface Task { + + void cancel(); + + void run(); + +} diff --git a/mcmmo-bukkit/src/main/java/com/gmail/nossr50/mcmmo/bukkit/BukkitBoostrap.java b/mcmmo-bukkit/src/main/java/com/gmail/nossr50/mcmmo/bukkit/BukkitBoostrap.java index e4827b128..ff10dad9d 100644 --- a/mcmmo-bukkit/src/main/java/com/gmail/nossr50/mcmmo/bukkit/BukkitBoostrap.java +++ b/mcmmo-bukkit/src/main/java/com/gmail/nossr50/mcmmo/bukkit/BukkitBoostrap.java @@ -9,8 +9,11 @@ import com.gmail.nossr50.listeners.WorldListener; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcmmo.api.platform.PlatformProvider; import com.gmail.nossr50.mcmmo.api.platform.ServerSoftwareType; +import com.gmail.nossr50.mcmmo.api.platform.scheduler.PlatformScheduler; import com.gmail.nossr50.mcmmo.api.platform.util.MetadataStore; +import com.gmail.nossr50.mcmmo.bukkit.platform.scheduler.BukkitPlatformScheduler; +import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; import org.bukkit.event.HandlerList; import org.bukkit.plugin.PluginManager; @@ -23,6 +26,7 @@ import java.util.logging.Logger; public class BukkitBoostrap extends JavaPlugin implements PlatformProvider { private mcMMO core = new mcMMO(this); + private final BukkitPlatformScheduler scheduler = new BukkitPlatformScheduler(this); @Override public @NotNull Logger getLogger() { @@ -89,6 +93,30 @@ public class BukkitBoostrap extends JavaPlugin implements PlatformProvider { } + @Override + public PlatformScheduler getScheduler() { + return scheduler; + } + + @Override + public void checkMetrics() { + //If anonymous statistics are enabled then use them + if (core.getConfigManager().getConfigMetrics().isAllowAnonymousUsageStatistics()) { + Metrics metrics; + metrics = new Metrics(this); + metrics.addCustomChart(new Metrics.SimplePie("version", this::getVersion)); + + int levelScaleModifier = core.getConfigManager().getConfigLeveling().getConfigSectionLevelingGeneral().getConfigSectionLevelScaling().getCosmeticLevelScaleModifier(); + + if (levelScaleModifier == 10) + metrics.addCustomChart(new Metrics.SimplePie("scaling", () -> "Standard")); + else if (levelScaleModifier == 1) + metrics.addCustomChart(new Metrics.SimplePie("scaling", () -> "Retro")); + else + metrics.addCustomChart(new Metrics.SimplePie("scaling", () -> "Custom")); + } + } + private void registerEvents() { PluginManager pluginManager = getServer().getPluginManager(); diff --git a/mcmmo-bukkit/src/main/java/com/gmail/nossr50/mcmmo/bukkit/platform/scheduler/BukkitPlatformScheduler.java b/mcmmo-bukkit/src/main/java/com/gmail/nossr50/mcmmo/bukkit/platform/scheduler/BukkitPlatformScheduler.java new file mode 100644 index 000000000..85d9b0421 --- /dev/null +++ b/mcmmo-bukkit/src/main/java/com/gmail/nossr50/mcmmo/bukkit/platform/scheduler/BukkitPlatformScheduler.java @@ -0,0 +1,60 @@ +package com.gmail.nossr50.mcmmo.bukkit.platform.scheduler; + +import com.gmail.nossr50.mcmmo.api.platform.scheduler.PlatformScheduler; +import com.gmail.nossr50.mcmmo.api.platform.scheduler.Task; +import com.gmail.nossr50.mcmmo.bukkit.BukkitBoostrap; + +import org.bukkit.Bukkit; +import org.bukkit.scheduler.BukkitScheduler; +import org.bukkit.scheduler.BukkitTask; + +import java.util.function.Consumer; + +public class BukkitPlatformScheduler implements PlatformScheduler { + + private final BukkitBoostrap bukkitBoostrap; + + public BukkitPlatformScheduler(BukkitBoostrap bukkitBoostrap) { + + this.bukkitBoostrap = bukkitBoostrap; + } + + @Override + public TaskBuilder getTaskBuilder() { + return new TaskBuilder(); + } + + @Override + public Task scheduleTask(TaskBuilder taskBuilder) { + final Integer repeatTime = taskBuilder.getRepeatTime(); + final Integer delay = taskBuilder.getDelay(); + final boolean isAsync = taskBuilder.isAsync(); + final Consumer taskConsumer = taskBuilder.getTask(); + + final MMOBukkitTask task = new MMOBukkitTask(taskConsumer); + final BukkitScheduler bukkitScheduler = Bukkit.getScheduler(); + + final BukkitTask bukkitTask; + if (!isAsync) { + if (delay == null && repeatTime == null) { + bukkitTask = bukkitScheduler.runTask(bukkitBoostrap, task); + } else if (delay != null && repeatTime == null) { + bukkitTask = bukkitScheduler.runTaskLater(bukkitBoostrap, task, delay); + } else { + bukkitTask = bukkitScheduler.runTaskTimer(bukkitBoostrap, task, delay != null ? delay : 0, repeatTime); + } + } else { + if (delay == null && repeatTime == null) { + bukkitTask = bukkitScheduler.runTaskAsynchronously(bukkitBoostrap, task); + } else if (delay != null && repeatTime == null) { + bukkitTask = bukkitScheduler.runTaskLaterAsynchronously(bukkitBoostrap, task, delay); + } else { + bukkitTask = bukkitScheduler.runTaskTimerAsynchronously(bukkitBoostrap, task, delay != null ? delay : 0, repeatTime); + } + } + + task.setBukkitTask(bukkitTask); + + return task; + } +} diff --git a/mcmmo-bukkit/src/main/java/com/gmail/nossr50/mcmmo/bukkit/platform/scheduler/MMOBukkitTask.java b/mcmmo-bukkit/src/main/java/com/gmail/nossr50/mcmmo/bukkit/platform/scheduler/MMOBukkitTask.java new file mode 100644 index 000000000..0b4f5e581 --- /dev/null +++ b/mcmmo-bukkit/src/main/java/com/gmail/nossr50/mcmmo/bukkit/platform/scheduler/MMOBukkitTask.java @@ -0,0 +1,32 @@ +package com.gmail.nossr50.mcmmo.bukkit.platform.scheduler; + +import com.gmail.nossr50.mcmmo.api.platform.scheduler.Task; +import com.google.common.base.Preconditions; + +import org.bukkit.scheduler.BukkitTask; + +import java.util.function.Consumer; + +public class MMOBukkitTask implements Task, Runnable { + Consumer task; + private BukkitTask bukkitTask; + + public MMOBukkitTask(Consumer task) { + this.task = task; + } + + @Override + public void cancel() { + Preconditions.checkState(bukkitTask != null, "Cannot cancel an an unscheduled task!"); + bukkitTask.cancel(); + } + + @Override + public void run() { + task.accept(this); + } + + public void setBukkitTask(BukkitTask bukkitTask) { + this.bukkitTask = bukkitTask; + } +} diff --git a/mcmmo-core/src/main/java/com/gmail/nossr50/mcMMO.java b/mcmmo-core/src/main/java/com/gmail/nossr50/mcMMO.java index 76468beea..2e1e5f5a7 100644 --- a/mcmmo-core/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/mcmmo-core/src/main/java/com/gmail/nossr50/mcMMO.java @@ -219,21 +219,7 @@ public class mcMMO implements McMMOApi { rankTools.populateRanks(); } - //If anonymous statistics are enabled then use them - if (getConfigManager().getConfigMetrics().isAllowAnonymousUsageStatistics()) { - Metrics metrics; - metrics = new Metrics(this); - metrics.addCustomChart(new Metrics.SimplePie("version", this::getVersion)); - - int levelScaleModifier = configManager.getConfigLeveling().getConfigSectionLevelingGeneral().getConfigSectionLevelScaling().getCosmeticLevelScaleModifier(); - - if (levelScaleModifier == 10) - metrics.addCustomChart(new Metrics.SimplePie("scaling", () -> "Standard")); - else if (levelScaleModifier == 1) - metrics.addCustomChart(new Metrics.SimplePie("scaling", () -> "Retro")); - else - metrics.addCustomChart(new Metrics.SimplePie("scaling", () -> "Custom")); - } + platformProvider.checkMetrics(); } catch (Throwable t) { getLogger().severe("There was an error while enabling mcMMO!"); t.printStackTrace();