From d8b8bedc98009c71052d4c664b987a3121aa1118 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Fri, 29 Jan 2021 20:51:15 -0600 Subject: [PATCH] Slowly adding more shit --- .../paldiu/simplexcore/SimplexCore.java | 3 ++ .../simplexcore/config/JsonFactory.java | 33 +++++++++++++++ .../simplexcore/config/YamlFactory.java | 32 +++++++++++++++ .../listener/ServerPluginListener.java | 17 ++++++++ .../simplexcore/listener/SimplexListener.java | 41 +++++++++++++++++++ .../listeners/SimplexListener.java | 29 ------------- .../github/paldiu/simplexcore/utils/Trio.java | 37 +++++++++++++++++ .../paldiu/simplexcore/utils/Utilities.java | 3 ++ 8 files changed, 166 insertions(+), 29 deletions(-) create mode 100644 src/main/java/io/github/paldiu/simplexcore/config/JsonFactory.java create mode 100644 src/main/java/io/github/paldiu/simplexcore/config/YamlFactory.java create mode 100644 src/main/java/io/github/paldiu/simplexcore/listener/ServerPluginListener.java create mode 100644 src/main/java/io/github/paldiu/simplexcore/listener/SimplexListener.java delete mode 100644 src/main/java/io/github/paldiu/simplexcore/listeners/SimplexListener.java diff --git a/src/main/java/io/github/paldiu/simplexcore/SimplexCore.java b/src/main/java/io/github/paldiu/simplexcore/SimplexCore.java index 2066e18..c6baf24 100644 --- a/src/main/java/io/github/paldiu/simplexcore/SimplexCore.java +++ b/src/main/java/io/github/paldiu/simplexcore/SimplexCore.java @@ -1,6 +1,8 @@ package io.github.paldiu.simplexcore; import io.github.paldiu.simplexcore.command.defaults.Command_info; +import io.github.paldiu.simplexcore.listener.ServerPluginListener; +import io.github.paldiu.simplexcore.listener.SimplexListener; import io.github.paldiu.simplexcore.plugin.SimplexAddon; import io.github.paldiu.simplexcore.utils.Constants; @@ -14,6 +16,7 @@ public final class SimplexCore extends SimplexAddon { public void start() { Constants.getRegistry().register(this); Constants.getCommandLoader().classpath(Command_info.class).load(); + SimplexListener.register(new ServerPluginListener(), this); } @Override diff --git a/src/main/java/io/github/paldiu/simplexcore/config/JsonFactory.java b/src/main/java/io/github/paldiu/simplexcore/config/JsonFactory.java new file mode 100644 index 0000000..978accf --- /dev/null +++ b/src/main/java/io/github/paldiu/simplexcore/config/JsonFactory.java @@ -0,0 +1,33 @@ +package io.github.paldiu.simplexcore.config; + +import io.github.paldiu.simplexcore.plugin.SimplexAddon; +import io.github.paldiu.simplexcore.utils.Trio; + +import java.io.File; +import java.util.Map; + +public final class JsonFactory { + private final SimplexAddon plugin; + private String resourcePath; + private File directory; + private String fileName; + + public JsonFactory(SimplexAddon plugin) { + this.plugin = plugin; + } + + public JsonFactory setPathways(String resourcePath, File directory, String fileName) { + this.resourcePath = resourcePath; + this.directory = directory; + this.fileName = fileName; + return this; + } + + public JsonFactory setDefaultPathways() { + return setPathways("config.yml", plugin.getDataFolder(), "config.yml"); + } + + public Trio getPathways() { + return new Trio<>(resourcePath, directory, fileName); + } +} \ No newline at end of file diff --git a/src/main/java/io/github/paldiu/simplexcore/config/YamlFactory.java b/src/main/java/io/github/paldiu/simplexcore/config/YamlFactory.java new file mode 100644 index 0000000..b7f6004 --- /dev/null +++ b/src/main/java/io/github/paldiu/simplexcore/config/YamlFactory.java @@ -0,0 +1,32 @@ +package io.github.paldiu.simplexcore.config; + +import io.github.paldiu.simplexcore.plugin.SimplexAddon; +import io.github.paldiu.simplexcore.utils.Trio; + +import java.io.File; + +public final class YamlFactory { + private final SimplexAddon plugin; + private String resourcePath; + private File directory; + private String fileName; + + public YamlFactory(SimplexAddon plugin) { + this.plugin = plugin; + } + + public YamlFactory setPathways(String resourcePath, File directory, String fileName) { + this.resourcePath = resourcePath; + this.directory = directory; + this.fileName = fileName; + return this; + } + + public YamlFactory setDefaultPathways() { + return setPathways("config.yml", plugin.getDataFolder(), "config.yml"); + } + + public Trio getPathways() { + return new Trio<>(resourcePath, directory, fileName); + } +} diff --git a/src/main/java/io/github/paldiu/simplexcore/listener/ServerPluginListener.java b/src/main/java/io/github/paldiu/simplexcore/listener/ServerPluginListener.java new file mode 100644 index 0000000..6b9d2ff --- /dev/null +++ b/src/main/java/io/github/paldiu/simplexcore/listener/ServerPluginListener.java @@ -0,0 +1,17 @@ +package io.github.paldiu.simplexcore.listener; + +import io.github.paldiu.simplexcore.plugin.SimplexAddon; +import io.github.paldiu.simplexcore.utils.Constants; +import org.bukkit.event.EventHandler; +import org.bukkit.event.server.PluginEnableEvent; + +public class ServerPluginListener extends SimplexListener { + @EventHandler + public void pluginRegister(PluginEnableEvent event) { + if (SimplexAddon.class.isAssignableFrom(event.getPlugin().getClass())) { + if (!Constants.getRegistry().getComponents().contains(event.getPlugin())) { + Constants.getRegistry().getComponents().add((SimplexAddon) event.getPlugin()); + } + } + } +} diff --git a/src/main/java/io/github/paldiu/simplexcore/listener/SimplexListener.java b/src/main/java/io/github/paldiu/simplexcore/listener/SimplexListener.java new file mode 100644 index 0000000..1fee786 --- /dev/null +++ b/src/main/java/io/github/paldiu/simplexcore/listener/SimplexListener.java @@ -0,0 +1,41 @@ +package io.github.paldiu.simplexcore.listener; + +import io.github.paldiu.simplexcore.plugin.SimplexAddon; +import io.github.paldiu.simplexcore.utils.Constants; +import org.bukkit.event.Listener; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +public abstract class SimplexListener implements Listener { + /** + * This will register your listener by its class. This requires your class to have a single constructor which takes no parameters. + * This also requires the class in question to extend SimplexListener. + * + * @param cls The class to register. + */ + public static void registerFromClass(Class cls, SimplexAddon plugin) { + if (!SimplexListener.class.isAssignableFrom(cls)) { + Constants.getLogger().severe("You can only register subclasses of SimplexListener with this method."); + return; + } + + try { + Constructor constr = cls.getDeclaredConstructor(); + register(constr.newInstance(), plugin); + } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) { + Constants.getLogger().severe("Could not register this listener!"); + } + } + + /** + * Registers your listener with the server plugin manager. + * + * @param listener The listener instance + * @param plugin Your plugin instance + * @param Type of which extends SimplexListener. + */ + public static void register(T listener, SimplexAddon plugin) { + Constants.getManager().registerEvents(listener, plugin); + } +} diff --git a/src/main/java/io/github/paldiu/simplexcore/listeners/SimplexListener.java b/src/main/java/io/github/paldiu/simplexcore/listeners/SimplexListener.java deleted file mode 100644 index 53aaec2..0000000 --- a/src/main/java/io/github/paldiu/simplexcore/listeners/SimplexListener.java +++ /dev/null @@ -1,29 +0,0 @@ -package io.github.paldiu.simplexcore.listeners; - -import io.github.paldiu.simplexcore.plugin.SimplexAddon; -import io.github.paldiu.simplexcore.utils.Constants; -import io.github.paldiu.simplexcore.utils.Utilities; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.server.PluginEnableEvent; - -public abstract class SimplexListener implements Listener { - @EventHandler - public void pluginRegister(PluginEnableEvent event) { - if (SimplexAddon.class.isAssignableFrom(event.getPlugin().getClass())) { - if (!Constants.getRegistry().getComponents().contains(event.getPlugin())) { - Constants.getRegistry().getComponents().add((SimplexAddon) event.getPlugin()); - } - } - } - - public static void registerAll(T[] sl, SimplexAddon plugin) { - Utilities.primitiveFE(sl, action -> { - SimplexListener.register(action, plugin); - }); - } - - public static void register(T listener, SimplexAddon plugin) { - Constants.getManager().registerEvents(listener, plugin); - } -} diff --git a/src/main/java/io/github/paldiu/simplexcore/utils/Trio.java b/src/main/java/io/github/paldiu/simplexcore/utils/Trio.java index 3a59ce3..b1bd234 100644 --- a/src/main/java/io/github/paldiu/simplexcore/utils/Trio.java +++ b/src/main/java/io/github/paldiu/simplexcore/utils/Trio.java @@ -1,5 +1,8 @@ package io.github.paldiu.simplexcore.utils; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; + public class Trio { private final A primary; private final B secondary; @@ -22,4 +25,38 @@ public class Trio { public C getTertiary() { return tertiary; } + + @Override + public int hashCode() { + return new HashCodeBuilder().append(getPrimary()) + .append(getSecondary()) + .append(getTertiary()) + .toHashCode(); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Trio)) { + return false; + } + + if (obj.equals(this)) { + return true; + } + + Trio trio = (Trio) obj; + return new EqualsBuilder().append(getPrimary(), trio.getPrimary()) + .append(getSecondary(), trio.getSecondary()) + .append(getTertiary(), trio.getTertiary()) + .isEquals(); + } + + @Override + public String toString() { + return getPrimary().toString() + + "\n" + + getSecondary().toString() + + "\n" + + getTertiary().toString(); + } } diff --git a/src/main/java/io/github/paldiu/simplexcore/utils/Utilities.java b/src/main/java/io/github/paldiu/simplexcore/utils/Utilities.java index e8af07b..49a7307 100644 --- a/src/main/java/io/github/paldiu/simplexcore/utils/Utilities.java +++ b/src/main/java/io/github/paldiu/simplexcore/utils/Utilities.java @@ -1,5 +1,8 @@ package io.github.paldiu.simplexcore.utils; +import io.github.paldiu.simplexcore.listener.SimplexListener; +import io.github.paldiu.simplexcore.plugin.SimplexAddon; + import java.util.Arrays; import java.util.Collection; import java.util.Map;