From 9b60b20a0fa051cd109170d22263a07d8eefd482 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Fri, 12 Mar 2021 01:24:11 -0600 Subject: [PATCH] BLEEDING EDGE 1.3_09 - CommandLoader fix in which it was incorrectly attempting to assign a subclass to a superclass. - Added a friendly comment to Yaml which states the constructor is package private. - Fixed a problem in YamlFactory where fileName and resourcePath were swapped, resulting in the resourcePath to possibly be the fileName and the fileName to possibly be null. - Moved over some methods from SimplexCorePlugin so it is accessible by default to all modules. - A Timer instance and two methods operating on that timer instance were added to SimplexTask --- .../simplexcore/SimplexCorePlugin.java | 30 ++--------------- .../simplexcore/command/CommandLoader.java | 4 +-- .../simplexdev/simplexcore/config/Yaml.java | 1 + .../simplexcore/config/YamlFactory.java | 2 +- .../simplexcore/plugin/SimplexAddon.java | 32 +++++++++++++++++++ .../simplexcore/task/SimplexTask.java | 19 ++++++++++- 6 files changed, 57 insertions(+), 31 deletions(-) diff --git a/src/main/java/io/github/simplexdev/simplexcore/SimplexCorePlugin.java b/src/main/java/io/github/simplexdev/simplexcore/SimplexCorePlugin.java index 8145aab..4e1f92a 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/SimplexCorePlugin.java +++ b/src/main/java/io/github/simplexdev/simplexcore/SimplexCorePlugin.java @@ -25,10 +25,10 @@ public final class SimplexCorePlugin extends SimplexAddon { private TimeValues time; private Yaml internals; - protected static SimplexCorePlugin instance; // = getPlugin(SimplexCorePlugin.class); + protected static SimplexCorePlugin instance; public static SimplexCorePlugin getInstance() { return instance; - } + } // I understand this could be an issue. @Override public SimplexCorePlugin getPlugin() { @@ -37,7 +37,7 @@ public final class SimplexCorePlugin extends SimplexAddon { @Override public void init() { - instance = this; + instance = this; // However, if the module is written correctly, this wont be an issue. this.dpm = new DependencyManagement(); this.config = new YamlFactory(this).setDefaultPathways(); this.time = new TimeValues(); @@ -80,30 +80,6 @@ public final class SimplexCorePlugin extends SimplexAddon { return suspended; } - public Logger getLogger() { - return this.getServer().getLogger(); - } - - public PluginManager getManager() { - return this.getServer().getPluginManager(); - } - - public BukkitScheduler getScheduler() { - return this.getServer().getScheduler(); - } - - public File getParentFolder() { - return this.getDataFolder(); - } - - public synchronized AddonRegistry getRegistry() { - return AddonRegistry.getInstance(); - } - - public synchronized CommandLoader getCommandLoader() { - return CommandLoader.getInstance(); - } - public DependencyManagement getDependencyManager() { return dpm; } diff --git a/src/main/java/io/github/simplexdev/simplexcore/command/CommandLoader.java b/src/main/java/io/github/simplexdev/simplexcore/command/CommandLoader.java index b543499..a2011e3 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/command/CommandLoader.java +++ b/src/main/java/io/github/simplexdev/simplexcore/command/CommandLoader.java @@ -48,8 +48,8 @@ public final class CommandLoader { throw new MissingResourceException("Cannot register this class as the main resource location!", clazz.getSimpleName(), "@CommandInfo"); } - if (!clazz.isAssignableFrom(SimplexCommand.class)) { - throw new RuntimeException("Unable to assign an executor!"); + if (!SimplexCommand.class.isAssignableFrom(clazz)) { + throw new RuntimeException("Your command must extend SimplexCommand.class for it to be used as the reference point for loading commands."); } reflections = new Reflections(clazz); diff --git a/src/main/java/io/github/simplexdev/simplexcore/config/Yaml.java b/src/main/java/io/github/simplexdev/simplexcore/config/Yaml.java index 8be8003..f79a941 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/config/Yaml.java +++ b/src/main/java/io/github/simplexdev/simplexcore/config/Yaml.java @@ -21,6 +21,7 @@ public final class Yaml implements IConfig { private FileConfiguration config; private File file; + // Package private ;) Yaml(SimplexAddon plugin, String fileName, File directory, String resourcePath) { if (!fileName.endsWith(".yml")) { fileName += ".yml"; diff --git a/src/main/java/io/github/simplexdev/simplexcore/config/YamlFactory.java b/src/main/java/io/github/simplexdev/simplexcore/config/YamlFactory.java index 259d06a..6dc2748 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/config/YamlFactory.java +++ b/src/main/java/io/github/simplexdev/simplexcore/config/YamlFactory.java @@ -21,7 +21,7 @@ public final class YamlFactory { this.resourcePath = resourcePath; this.directory = directory; this.fileName = fileName; - return new Yaml(plugin, resourcePath, directory, fileName); + return new Yaml(plugin, fileName, directory, resourcePath); } public FileConfiguration load(File yamlFile) { diff --git a/src/main/java/io/github/simplexdev/simplexcore/plugin/SimplexAddon.java b/src/main/java/io/github/simplexdev/simplexcore/plugin/SimplexAddon.java index 2b69800..564e052 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/plugin/SimplexAddon.java +++ b/src/main/java/io/github/simplexdev/simplexcore/plugin/SimplexAddon.java @@ -1,7 +1,19 @@ package io.github.simplexdev.simplexcore.plugin; +import io.github.simplexdev.simplexcore.command.CommandLoader; +import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitScheduler; +import org.jetbrains.annotations.NotNull; +import java.io.File; +import java.util.logging.Logger; + +/** + * This class represents a SimplexCore module. + * You should extend this instead of JavaPlugin if you are using the core as a dependency. + * @param + */ public abstract class SimplexAddon> extends JavaPlugin { /** * Gets your plugin as an addon. @@ -40,4 +52,24 @@ public abstract class SimplexAddon> extends JavaPlugin */ public void init() { } + + public PluginManager getManager() { + return this.getServer().getPluginManager(); + } + + public BukkitScheduler getScheduler() { + return this.getServer().getScheduler(); + } + + public File getParentFolder() { + return this.getDataFolder(); + } + + public synchronized AddonRegistry getRegistry() { + return AddonRegistry.getInstance(); + } + + public synchronized CommandLoader getCommandLoader() { + return CommandLoader.getInstance(); + } } diff --git a/src/main/java/io/github/simplexdev/simplexcore/task/SimplexTask.java b/src/main/java/io/github/simplexdev/simplexcore/task/SimplexTask.java index 6592098..648fab5 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/task/SimplexTask.java +++ b/src/main/java/io/github/simplexdev/simplexcore/task/SimplexTask.java @@ -1,10 +1,13 @@ package io.github.simplexdev.simplexcore.task; +import io.github.simplexdev.api.func.VoidSupplier; import io.github.simplexdev.simplexcore.SimplexCorePlugin; import io.github.simplexdev.simplexcore.plugin.SimplexAddon; import org.bukkit.scheduler.BukkitTask; import java.util.Date; +import java.util.Timer; +import java.util.TimerTask; import java.util.function.Consumer; // TODO: Rewrite this entire class and the task system to have more control over tasks. @@ -12,12 +15,13 @@ public abstract class SimplexTask implements Consumer { protected final long DELAY; protected final long INTERVAL; protected Date lastRan = new Date(); + protected Timer timer = new Timer(); protected SimplexTask(long initialDelay, long interval) { DELAY = initialDelay; INTERVAL = interval; } - + protected SimplexTask(long interval) { DELAY = 0L; INTERVAL = interval; @@ -55,4 +59,17 @@ public abstract class SimplexTask implements Consumer { public void setLastRan(Date lastRan) { this.lastRan = lastRan; } + + protected Timer getTimer() { + return timer; + } + + protected TimerTask newTimer(VoidSupplier supplier) { + return new TimerTask() { + @Override + public void run() { + supplier.get(); + } + }; + } }