mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2024-11-25 06:36:45 +01:00
Create BukkitPlatformScheduler
This commit is contained in:
parent
48e547e51f
commit
c916070cd1
@ -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();
|
||||
}
|
||||
|
@ -1,6 +0,0 @@
|
||||
package com.gmail.nossr50.mcmmo.api.platform.schedular;
|
||||
|
||||
public interface PlatformScheduler {
|
||||
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.gmail.nossr50.mcmmo.api.platform.scheduler;
|
||||
|
||||
public interface Task {
|
||||
|
||||
void cancel();
|
||||
|
||||
void run();
|
||||
|
||||
}
|
@ -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();
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user