From aea26ef2f3ae67744380702d244d2fe3f98840f3 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Tue, 26 Jan 2021 21:47:08 -0600 Subject: [PATCH] a --- .idea/vcs.xml | 6 ++ .../paldiu/simplexcore/SimplexCore.java | 7 +- .../simplexcore/command/CommandBase.java | 41 +++++++++++ .../simplexcore/command/CommandInfo.java | 4 ++ .../simplexcore/command/CommandLoader.java | 69 +++++++++++++++---- .../command/defaults/Command_info.java | 16 +++++ .../paldiu/simplexcore/math/Cuboid.java | 27 ++++++++ .../github/paldiu/simplexcore/math/Size.java | 30 ++++++++ .../{registry => plugin}/Addon.java | 10 +-- .../simplexcore/plugin/AddonManager.java | 21 ++++++ .../simplexcore/plugin/AddonRegistry.java | 24 +++++++ .../simplexcore/registry/AddonRegistry.java | 19 ----- .../github/paldiu/simplexcore/utils/Bean.java | 17 +++++ .../simplexcore/{ => utils}/Constants.java | 13 ++-- .../github/paldiu/simplexcore/utils/Trio.java | 25 +++++++ .../paldiu/simplexcore/utils/Utilities.java | 32 +++++++++ 16 files changed, 317 insertions(+), 44 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 src/main/java/io/github/paldiu/simplexcore/command/CommandBase.java create mode 100644 src/main/java/io/github/paldiu/simplexcore/command/defaults/Command_info.java create mode 100644 src/main/java/io/github/paldiu/simplexcore/math/Cuboid.java create mode 100644 src/main/java/io/github/paldiu/simplexcore/math/Size.java rename src/main/java/io/github/paldiu/simplexcore/{registry => plugin}/Addon.java (79%) create mode 100644 src/main/java/io/github/paldiu/simplexcore/plugin/AddonManager.java create mode 100644 src/main/java/io/github/paldiu/simplexcore/plugin/AddonRegistry.java delete mode 100644 src/main/java/io/github/paldiu/simplexcore/registry/AddonRegistry.java create mode 100644 src/main/java/io/github/paldiu/simplexcore/utils/Bean.java rename src/main/java/io/github/paldiu/simplexcore/{ => utils}/Constants.java (73%) create mode 100644 src/main/java/io/github/paldiu/simplexcore/utils/Trio.java create mode 100644 src/main/java/io/github/paldiu/simplexcore/utils/Utilities.java diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/io/github/paldiu/simplexcore/SimplexCore.java b/src/main/java/io/github/paldiu/simplexcore/SimplexCore.java index 024e1f8..fcda473 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.registry.Addon; +import io.github.paldiu.simplexcore.command.defaults.Command_info; +import io.github.paldiu.simplexcore.plugin.Addon; +import io.github.paldiu.simplexcore.utils.Constants; public final class SimplexCore extends Addon { protected SimplexCore(SimplexCore plugin) { @@ -9,7 +11,8 @@ public final class SimplexCore extends Addon { @Override public void start() { - + Constants.getRegistry().register(this); + Constants.getCommandLoader().classpath(Command_info.class).load(); } @Override diff --git a/src/main/java/io/github/paldiu/simplexcore/command/CommandBase.java b/src/main/java/io/github/paldiu/simplexcore/command/CommandBase.java new file mode 100644 index 0000000..82fa42e --- /dev/null +++ b/src/main/java/io/github/paldiu/simplexcore/command/CommandBase.java @@ -0,0 +1,41 @@ +package io.github.paldiu.simplexcore.command; + +import io.github.paldiu.simplexcore.utils.Constants; +import io.github.paldiu.simplexcore.utils.Utilities; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public abstract class CommandBase implements CommandExecutor, TabCompleter { + public boolean checkSender(CommandSender sender) { + return sender instanceof Player; + } + + public Player getPlayer(String name) { + return Constants.getServer().getPlayer(name); + } + + public Player getPlayer(UUID uuid) { + return Constants.getServer().getPlayer(uuid); + } + + public void playerMsg(Player player, String... message) { + StringBuilder builder = new StringBuilder(); + Utilities.primitiveFE(message, builder::append); + player.sendMessage(builder.toString()); + } + + @Nullable + @Override + public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) { + return new ArrayList<>(); + } +} diff --git a/src/main/java/io/github/paldiu/simplexcore/command/CommandInfo.java b/src/main/java/io/github/paldiu/simplexcore/command/CommandInfo.java index 905e5e2..e52dd43 100644 --- a/src/main/java/io/github/paldiu/simplexcore/command/CommandInfo.java +++ b/src/main/java/io/github/paldiu/simplexcore/command/CommandInfo.java @@ -1,5 +1,7 @@ package io.github.paldiu.simplexcore.command; +import org.bukkit.ChatColor; + import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -14,4 +16,6 @@ public @interface CommandInfo { String aliases() default ""; String permission() default "simplex.core"; + + String permissionMessage() default "You do not have permission to use this command."; } diff --git a/src/main/java/io/github/paldiu/simplexcore/command/CommandLoader.java b/src/main/java/io/github/paldiu/simplexcore/command/CommandLoader.java index d7c6551..6e5ae59 100644 --- a/src/main/java/io/github/paldiu/simplexcore/command/CommandLoader.java +++ b/src/main/java/io/github/paldiu/simplexcore/command/CommandLoader.java @@ -1,22 +1,25 @@ package io.github.paldiu.simplexcore.command; -import io.github.paldiu.simplexcore.Constants; +import io.github.paldiu.simplexcore.utils.Constants; import org.bukkit.Bukkit; import org.bukkit.command.*; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.SimplePluginManager; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.reflections.Reflections; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; -import java.util.*; +import java.util.Arrays; +import java.util.Map; +import java.util.MissingResourceException; public class CommandLoader { - private final Reflections reflections; + private Reflections reflections; - public CommandLoader(Class clazz) { + public CommandLoader classpath(Class clazz) { if (!clazz.isAnnotationPresent(CommandInfo.class)) { throw new MissingResourceException("Cannot register this class as the main resource location!", clazz.getSimpleName(), "@CommandInfo"); } @@ -26,6 +29,7 @@ public class CommandLoader { } reflections = new Reflections(clazz); + return this; } public void load() { @@ -33,21 +37,30 @@ public class CommandLoader { CommandInfo info = annotated.getDeclaredAnnotation(CommandInfo.class); if (info == null) return; - if (!CommandExecutor.class.isAssignableFrom(annotated)) return; + if (!CommandBase.class.isAssignableFrom(annotated)) return; - PluginCommand objectToRegister = Registry.create(Constants.getPlugin(), info.name()); - objectToRegister.setDescription(info.description()); - objectToRegister.setUsage(info.usage()); + PluginCommand objectToRegister = Registry.create(Constants.getPlugin(), info.name().toLowerCase()); objectToRegister.setAliases(Arrays.asList(info.aliases().split(","))); - objectToRegister.setPermission(info.permission()); + objectToRegister.setDescription(info.description()); objectToRegister.setExecutor(getFromSetName(info.name())); + objectToRegister.setLabel(info.name().toLowerCase()); + objectToRegister.setPermission(info.permission()); + objectToRegister.setPermissionMessage(info.permissionMessage()); + objectToRegister.setTabCompleter(getTabFromName(info.name())); + objectToRegister.setUsage(info.usage()); Registry.registerCommand(objectToRegister); }); } public CommandExecutor getFromSetName(String name) { for (Class obj : reflections.getSubTypesOf(CommandExecutor.class)) { - if (name.equalsIgnoreCase(obj.getSimpleName())) { + if (!obj.isAnnotationPresent(CommandInfo.class)) { + throw new RuntimeException("Missing annotation CommandInfo!"); + } + + CommandInfo info = obj.getDeclaredAnnotation(CommandInfo.class); + + if (name.equalsIgnoreCase(info.name())) { try { Constructor constr = obj.getDeclaredConstructor(); return constr.newInstance(); @@ -59,7 +72,26 @@ public class CommandLoader { throw new RuntimeException("Unable to get a command executor! Terminating!"); } - @SuppressWarnings("unchecked") + @Nullable + public TabCompleter getTabFromName(String name) { + for (Class obj : reflections.getSubTypesOf(TabCompleter.class)) { + if (!obj.isAnnotationPresent(CommandInfo.class)) { + throw new RuntimeException("Missing annotation CommandInfo!"); + } + + CommandInfo info = obj.getDeclaredAnnotation(CommandInfo.class); + if (name.equalsIgnoreCase(info.name())) { + try { + Constructor constr = obj.getDeclaredConstructor(); + return constr.newInstance(); + } catch (NoSuchMethodException | InstantiationException | InvocationTargetException | IllegalAccessException e) { + e.printStackTrace(); + } + } + } + return null; + } + private static class Registry { private static final Constructor constructor; private static final Field cmdMapField; @@ -107,14 +139,23 @@ public class CommandLoader { CommandMap map = (CommandMap) cmdMapField.get(Bukkit.getPluginManager()); Map knownCommands = map.getKnownCommands(); - if (knownCommands.containsKey(command.getName())) { - knownCommands.replace(command.getName(), command); + if (knownCommands.containsKey(command.getName().toLowerCase())) { + knownCommands.replace(command.getName().toLowerCase(), command); } - map.register(command.getName(), command); + map.register(command.getName().toLowerCase(), command); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } } + + private static final CommandLoader instance = new CommandLoader(); + + protected CommandLoader() { + } + + public static CommandLoader getInstance() { + return instance; + } } diff --git a/src/main/java/io/github/paldiu/simplexcore/command/defaults/Command_info.java b/src/main/java/io/github/paldiu/simplexcore/command/defaults/Command_info.java new file mode 100644 index 0000000..e2e5b8a --- /dev/null +++ b/src/main/java/io/github/paldiu/simplexcore/command/defaults/Command_info.java @@ -0,0 +1,16 @@ +package io.github.paldiu.simplexcore.command.defaults; + +import io.github.paldiu.simplexcore.command.CommandBase; +import io.github.paldiu.simplexcore.command.CommandInfo; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +@CommandInfo(name = "Info", description = "Gets info on this API / Library.", usage = "/", permission = "simplex.core.info") +public class Command_info extends CommandBase { + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + sender.sendMessage("This is an API!"); + return true; + } +} diff --git a/src/main/java/io/github/paldiu/simplexcore/math/Cuboid.java b/src/main/java/io/github/paldiu/simplexcore/math/Cuboid.java new file mode 100644 index 0000000..a80d08f --- /dev/null +++ b/src/main/java/io/github/paldiu/simplexcore/math/Cuboid.java @@ -0,0 +1,27 @@ +package io.github.paldiu.simplexcore.math; + +import org.bukkit.Location; + +public class Cuboid { + private final int x, y, z; + + public Cuboid() { + this(3, 3, 3); + } + + public Cuboid(int xSize, int ySize, int zSize) { + this.x = xSize; + this.y = ySize; + this.z = zSize; + } + + public Cuboid(Size size) { + this(size.getX(), size.getY(), size.getZ()); + } + + public void generate(Location location) { + int t1 = location.getBlockX(); + int t2 = location.getBlockY(); + int t3 = location.getBlockZ(); + } +} diff --git a/src/main/java/io/github/paldiu/simplexcore/math/Size.java b/src/main/java/io/github/paldiu/simplexcore/math/Size.java new file mode 100644 index 0000000..4638d1e --- /dev/null +++ b/src/main/java/io/github/paldiu/simplexcore/math/Size.java @@ -0,0 +1,30 @@ +package io.github.paldiu.simplexcore.math; + +public enum Size { + SMALL(1, 1, 1), + MEDIUM(3, 3, 3), + LARGE(5, 5, 5), + EXTRA_LARGE(10, 10, 10), + HUGE(20, 20, 20), + ENORMOUS(50, 50, 50); + + int x, y, z; + + Size(int x, int y, int z) { + this.x = x; + this.y = y; + this.z = z; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + public int getZ() { + return z; + } +} diff --git a/src/main/java/io/github/paldiu/simplexcore/registry/Addon.java b/src/main/java/io/github/paldiu/simplexcore/plugin/Addon.java similarity index 79% rename from src/main/java/io/github/paldiu/simplexcore/registry/Addon.java rename to src/main/java/io/github/paldiu/simplexcore/plugin/Addon.java index fbd400c..2f7969f 100644 --- a/src/main/java/io/github/paldiu/simplexcore/registry/Addon.java +++ b/src/main/java/io/github/paldiu/simplexcore/plugin/Addon.java @@ -1,4 +1,4 @@ -package io.github.paldiu.simplexcore.registry; +package io.github.paldiu.simplexcore.plugin; import org.bukkit.plugin.java.JavaPlugin; @@ -10,9 +10,9 @@ public abstract class Addon> extends JavaPlugin { } /** - * Gets your plugin as an addon. + * Gets your plugin as an addon. * - * @return The addon. + * @return The addon. */ public T getPlugin() { return plugin; @@ -34,12 +34,12 @@ public abstract class Addon> extends JavaPlugin { } /** - * Plugin startup logic. + * Plugin startup logic. */ public abstract void start(); /** - * Plugin shutdown logic. + * Plugin shutdown logic. */ public abstract void stop(); diff --git a/src/main/java/io/github/paldiu/simplexcore/plugin/AddonManager.java b/src/main/java/io/github/paldiu/simplexcore/plugin/AddonManager.java new file mode 100644 index 0000000..4060a5f --- /dev/null +++ b/src/main/java/io/github/paldiu/simplexcore/plugin/AddonManager.java @@ -0,0 +1,21 @@ +package io.github.paldiu.simplexcore.plugin; + +import io.github.paldiu.simplexcore.utils.Constants; +import io.github.paldiu.simplexcore.utils.Utilities; + +public class AddonManager { + public AddonManager() { } + + public void disable(Addon addon) { + Constants.getManager().disablePlugin(addon); + } + + public void enable(Addon addon) { + Constants.getManager().enablePlugin(addon); + } + + public void reload(Addon addon) { + disable(addon); + enable(addon); + } +} diff --git a/src/main/java/io/github/paldiu/simplexcore/plugin/AddonRegistry.java b/src/main/java/io/github/paldiu/simplexcore/plugin/AddonRegistry.java new file mode 100644 index 0000000..1b5b20c --- /dev/null +++ b/src/main/java/io/github/paldiu/simplexcore/plugin/AddonRegistry.java @@ -0,0 +1,24 @@ +package io.github.paldiu.simplexcore.plugin; + +import java.util.HashSet; +import java.util.Set; + +public class AddonRegistry { + private final Set> components = new HashSet<>(); + private static final AddonRegistry instance = new AddonRegistry(); + + protected AddonRegistry() { + } + + public static AddonRegistry getInstance() { + return instance; + } + + public > void register(T addon) { + getComponents().add(addon); + } + + public Set> getComponents() { + return components; + } +} \ No newline at end of file diff --git a/src/main/java/io/github/paldiu/simplexcore/registry/AddonRegistry.java b/src/main/java/io/github/paldiu/simplexcore/registry/AddonRegistry.java deleted file mode 100644 index 264ca6a..0000000 --- a/src/main/java/io/github/paldiu/simplexcore/registry/AddonRegistry.java +++ /dev/null @@ -1,19 +0,0 @@ -package io.github.paldiu.simplexcore.registry; - -import java.util.HashSet; -import java.util.Set; - -public class AddonRegistry { - private final Set> components = new HashSet<>(); - - public AddonRegistry() { - } - - public void register(Addon addon) { - getComponents().add(addon); - } - - public Set> getComponents() { - return components; - } -} \ No newline at end of file diff --git a/src/main/java/io/github/paldiu/simplexcore/utils/Bean.java b/src/main/java/io/github/paldiu/simplexcore/utils/Bean.java new file mode 100644 index 0000000..589eb8a --- /dev/null +++ b/src/main/java/io/github/paldiu/simplexcore/utils/Bean.java @@ -0,0 +1,17 @@ +package io.github.paldiu.simplexcore.utils; + +public class Bean { + protected T bean; + + public Bean(T bean) { + this.bean = bean; + } + + public void setBean(T bean) { + this.bean = bean; + } + + public T getBean() { + return bean; + } +} diff --git a/src/main/java/io/github/paldiu/simplexcore/Constants.java b/src/main/java/io/github/paldiu/simplexcore/utils/Constants.java similarity index 73% rename from src/main/java/io/github/paldiu/simplexcore/Constants.java rename to src/main/java/io/github/paldiu/simplexcore/utils/Constants.java index ab58a75..9ae154b 100644 --- a/src/main/java/io/github/paldiu/simplexcore/Constants.java +++ b/src/main/java/io/github/paldiu/simplexcore/utils/Constants.java @@ -1,6 +1,8 @@ -package io.github.paldiu.simplexcore; +package io.github.paldiu.simplexcore.utils; -import io.github.paldiu.simplexcore.registry.AddonRegistry; +import io.github.paldiu.simplexcore.SimplexCore; +import io.github.paldiu.simplexcore.command.CommandLoader; +import io.github.paldiu.simplexcore.plugin.AddonRegistry; import org.bukkit.Server; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; @@ -14,7 +16,6 @@ public final class Constants { private static final Logger logger = plugin.getLogger(); private static final PluginManager manager = server.getPluginManager(); private static final BukkitScheduler scheduler = server.getScheduler(); - private static final AddonRegistry registry = new AddonRegistry(); public static SimplexCore getPlugin() { return plugin; @@ -37,6 +38,10 @@ public final class Constants { } public static AddonRegistry getRegistry() { - return registry; + return AddonRegistry.getInstance(); + } + + public static CommandLoader getCommandLoader() { + return CommandLoader.getInstance(); } } diff --git a/src/main/java/io/github/paldiu/simplexcore/utils/Trio.java b/src/main/java/io/github/paldiu/simplexcore/utils/Trio.java new file mode 100644 index 0000000..3a59ce3 --- /dev/null +++ b/src/main/java/io/github/paldiu/simplexcore/utils/Trio.java @@ -0,0 +1,25 @@ +package io.github.paldiu.simplexcore.utils; + +public class Trio { + private final A primary; + private final B secondary; + private final C tertiary; + + public Trio(A primary, B secondary, C tertiary) { + this.primary = primary; + this.secondary = secondary; + this.tertiary = tertiary; + } + + public A getPrimary() { + return primary; + } + + public B getSecondary() { + return secondary; + } + + public C getTertiary() { + return tertiary; + } +} diff --git a/src/main/java/io/github/paldiu/simplexcore/utils/Utilities.java b/src/main/java/io/github/paldiu/simplexcore/utils/Utilities.java new file mode 100644 index 0000000..e8af07b --- /dev/null +++ b/src/main/java/io/github/paldiu/simplexcore/utils/Utilities.java @@ -0,0 +1,32 @@ +package io.github.paldiu.simplexcore.utils; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Map; +import java.util.Set; +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class Utilities { + private static Stream feStr(T[] array) { + return Arrays.stream(array); + } + + private static Set feCol(Stream stream) { + return stream.collect(Collectors.toSet()); + } + + public static void primitiveFE(T[] array, Consumer action) { + feCol(feStr(array)).forEach(action); + } + + public static void smartFE(Collection collection, Consumer action) { + collection.forEach(action); + } + + public static void mapFE(Map map, BiConsumer actions) { + map.forEach(actions); + } +}