Create BukkitPlatformScheduler

This commit is contained in:
Shane Freeder 2020-02-01 02:29:02 +00:00
parent 48e547e51f
commit c916070cd1
No known key found for this signature in database
GPG Key ID: A3F61EA5A085289C
8 changed files with 188 additions and 22 deletions

View File

@ -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();
}

View File

@ -1,6 +0,0 @@
package com.gmail.nossr50.mcmmo.api.platform.schedular;
public interface PlatformScheduler {
}

View File

@ -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> 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<Task> getTask() {
return task;
}
public TaskBuilder setTask(Consumer<Task> task) {
this.task = task;
return this;
}
}
}

View File

@ -0,0 +1,9 @@
package com.gmail.nossr50.mcmmo.api.platform.scheduler;
public interface Task {
void cancel();
void run();
}

View File

@ -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();

View File

@ -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<Task> 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;
}
}

View File

@ -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> task;
private BukkitTask bukkitTask;
public MMOBukkitTask(Consumer<Task> 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;
}
}

View File

@ -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();