From 355d04d9bad7f6ee69b783a0e353a262a6a652f2 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Sun, 6 Feb 2022 19:58:00 -0600 Subject: [PATCH] Minor Update --- .../java/io/github/simplex/CommandBase.java | 55 --------- .../io/github/simplex/SubCommandBase.java | 15 --- .../simplex/api/annotations/Permission.java | 14 --- .../simplex/api/annotations/SubCommand.java | 12 -- .../io/github/simplex/cl/CommandBase.java | 107 ++++++++++++++++++ .../simplex/{ => cl}/CommandLoader.java | 21 ++-- .../github/simplex/{ => cl}/DummyCommand.java | 2 +- .../github/simplex/{ => cl}/Permissible.java | 2 +- .../github/simplex/{ => cl}/api/ICommand.java | 0 .../io/github/simplex/cl/api/SubCommand.java | 6 + .../{ => cl}/api/annotations/Info.java | 2 +- .../simplex/cl/impl/ExampleCommand.java | 28 +++++ .../github/simplex/impl/ExampleCommand.java | 39 ------- .../simplex/msgutils/AdvancedColors.java | 46 ++++++++ .../github/simplex/msgutils/BasicColors.java | 32 ++++++ 15 files changed, 230 insertions(+), 151 deletions(-) delete mode 100644 src/main/java/io/github/simplex/CommandBase.java delete mode 100644 src/main/java/io/github/simplex/SubCommandBase.java delete mode 100644 src/main/java/io/github/simplex/api/annotations/Permission.java delete mode 100644 src/main/java/io/github/simplex/api/annotations/SubCommand.java create mode 100644 src/main/java/io/github/simplex/cl/CommandBase.java rename src/main/java/io/github/simplex/{ => cl}/CommandLoader.java (67%) rename src/main/java/io/github/simplex/{ => cl}/DummyCommand.java (97%) rename src/main/java/io/github/simplex/{ => cl}/Permissible.java (96%) rename src/main/java/io/github/simplex/{ => cl}/api/ICommand.java (100%) create mode 100644 src/main/java/io/github/simplex/cl/api/SubCommand.java rename src/main/java/io/github/simplex/{ => cl}/api/annotations/Info.java (88%) create mode 100644 src/main/java/io/github/simplex/cl/impl/ExampleCommand.java delete mode 100644 src/main/java/io/github/simplex/impl/ExampleCommand.java create mode 100644 src/main/java/io/github/simplex/msgutils/AdvancedColors.java create mode 100644 src/main/java/io/github/simplex/msgutils/BasicColors.java diff --git a/src/main/java/io/github/simplex/CommandBase.java b/src/main/java/io/github/simplex/CommandBase.java deleted file mode 100644 index b5996cc..0000000 --- a/src/main/java/io/github/simplex/CommandBase.java +++ /dev/null @@ -1,55 +0,0 @@ -package io.github.simplex; - -import io.github.simplex.api.ICommand; -import io.github.simplex.api.annotations.SubCommand; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.TextComponent; -import net.kyori.adventure.text.format.TextColor; -import org.bukkit.Bukkit; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public abstract class CommandBase extends Permissible implements ICommand { - public CommandBase(String permission, String permissionMessage, boolean allowConsole) { - super(permission, permissionMessage, allowConsole); - } - - public CommandBase(String permission, String permissionMessage) { - this(permission, permissionMessage, true); - } - - @Override - public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String lbl, String[] args) { - if (!hasPermission(sender)) { - sender.sendMessage(Component - .empty() - .content(getPermissionMessage()) - .color(TextColor.color(255, 3, 62))); - return true; - } - - if (!(sender instanceof Player) && !allowConsole()) { - sender.sendMessage(Component.empty().content("This command may only be run in game.")); - } - - execute(sender, args, allowConsole()); - return true; - } - - @Override - public @Nullable List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) { - return new ArrayList<>(); - } - - public TextComponent msg(String text) { - return Component.empty().content(text); - } -} diff --git a/src/main/java/io/github/simplex/SubCommandBase.java b/src/main/java/io/github/simplex/SubCommandBase.java deleted file mode 100644 index aef8375..0000000 --- a/src/main/java/io/github/simplex/SubCommandBase.java +++ /dev/null @@ -1,15 +0,0 @@ -package io.github.simplex; - -/* - * This class is intended to be used as a nested class inside your main command class. - * For example: - */ -public abstract class SubCommandBase extends CommandBase { - public SubCommandBase(String permission, String permissionMessage, boolean allowConsole) { - super(permission, permissionMessage, allowConsole); - } - - public SubCommandBase(String permission, String permissionMessage) { - this(permission, permissionMessage, true); - } -} diff --git a/src/main/java/io/github/simplex/api/annotations/Permission.java b/src/main/java/io/github/simplex/api/annotations/Permission.java deleted file mode 100644 index 43066a3..0000000 --- a/src/main/java/io/github/simplex/api/annotations/Permission.java +++ /dev/null @@ -1,14 +0,0 @@ -package io.github.simplex.api.annotations; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target({ElementType.METHOD, ElementType.TYPE}) -@Retention(RetentionPolicy.RUNTIME) -public @interface Permission { - String permission(); - String permissionMessage() default "You do not have permission to use this command!"; - boolean allowConsole() default true; -} diff --git a/src/main/java/io/github/simplex/api/annotations/SubCommand.java b/src/main/java/io/github/simplex/api/annotations/SubCommand.java deleted file mode 100644 index 7f090ad..0000000 --- a/src/main/java/io/github/simplex/api/annotations/SubCommand.java +++ /dev/null @@ -1,12 +0,0 @@ -package io.github.simplex.api.annotations; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.TYPE) -public @interface SubCommand { - String name(); -} diff --git a/src/main/java/io/github/simplex/cl/CommandBase.java b/src/main/java/io/github/simplex/cl/CommandBase.java new file mode 100644 index 0000000..c09abf7 --- /dev/null +++ b/src/main/java/io/github/simplex/cl/CommandBase.java @@ -0,0 +1,107 @@ +package io.github.simplex.cl; + +import io.github.simplex.api.ICommand; +import io.github.simplex.api.SubCommand; +import io.github.simplex.msgutils.AdvancedColors; +import io.github.simplex.msgutils.BasicColors; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; +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 extends Permissible implements ICommand { + public CommandBase(String permission, String permissionMessage, boolean allowConsole) { + super(permission, permissionMessage, allowConsole); + } + + public CommandBase(String permission, String permissionMessage) { + this(permission, permissionMessage, true); + } + + public CommandBase(String permission, boolean allowConsole) { + this(permission, "You do not have permission to use this command!", allowConsole); + } + + public CommandBase(String permission) { + this(permission, "You do not have permission to use this command!", true); + } + + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String lbl, String[] args) { + if (!hasPermission(sender)) { + sender.sendMessage(msg(getPermissionMessage(), BasicColors.RED)); + return true; + } + + if (!(sender instanceof Player) && !allowConsole()) { + sender.sendMessage(msg("This command can only be run in game.")); + } + + execute(sender, args, allowConsole()); + return true; + } + + @Override + public @Nullable List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) { + return new ArrayList<>(); + } + + public TextComponent msg(String text) { + return Component.empty().content(text); + } + + public TextComponent msg(String text, BasicColors color) { + return Component.empty().content(text).color(color.getColor()); + } + + public TextComponent msg(String text, AdvancedColors color) { + return Component.empty().content(text).color(color.getColor()); + } + + public void subCommand(String name, String[] args, SubCommand command) { + if (args[0].equalsIgnoreCase(name)) { + command.execute(); + } + } + + public Player getPlayer(String name) { + return Bukkit.getServer().getPlayer(name); + } + + public Player getPlayer(UUID uuid) { + return Bukkit.getServer().getPlayer(uuid); + } + + public List getOnlinePlayers() { + return Bukkit.getOnlinePlayers().stream().toList(); + } + + public void broadcast(String text) { + Bukkit.getServer().broadcast(msg(text)); + } + + public void broadcast(String text, BasicColors color) { + Bukkit.getServer().broadcast(msg(text, color)); + } + + public void broadcast(String text, AdvancedColors color) { + Bukkit.getServer().broadcast(msg(text, color)); + } + + public void enablePlugin(Plugin plugin) { + Bukkit.getServer().getPluginManager().enablePlugin(plugin); + } + + public void disablePlugin(Plugin plugin) { + Bukkit.getServer().getPluginManager().disablePlugin(plugin); + } +} diff --git a/src/main/java/io/github/simplex/CommandLoader.java b/src/main/java/io/github/simplex/cl/CommandLoader.java similarity index 67% rename from src/main/java/io/github/simplex/CommandLoader.java rename to src/main/java/io/github/simplex/cl/CommandLoader.java index b215d24..0078efc 100644 --- a/src/main/java/io/github/simplex/CommandLoader.java +++ b/src/main/java/io/github/simplex/cl/CommandLoader.java @@ -1,34 +1,29 @@ -package io.github.simplex; +package io.github.simplex.cl; -import io.github.simplex.api.annotations.*; +import io.github.simplex.cl.api.annotations.Info; import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; import org.reflections.Reflections; + import java.lang.reflect.InvocationTargetException; import java.util.Arrays; import java.util.Set; -public class CommandLoader { +public class CommandLoader { private final Plugin plugin; public CommandLoader(Plugin plugin) { this.plugin = plugin; } - public void registerCommands(Class target) { - Reflections reflections = new Reflections(target); - if (target.getDeclaredAnnotation(Info.class) != null) { + public void registerCommands(Class commandRoot) { + Reflections reflections = new Reflections(commandRoot); + if (commandRoot.getDeclaredAnnotation(Info.class) != null) { Set> classSet = reflections.getTypesAnnotatedWith(Info.class); classSet.forEach(cmd -> { Info info = cmd.getDeclaredAnnotation(Info.class); - Permission permission = cmd.getDeclaredAnnotation(Permission.class); try { - CommandBase base = (CommandBase) cmd.getConstructor(String.class, - String.class, - Boolean.class) - .newInstance(permission.permission(), - permission.permissionMessage(), - permission.allowConsole()); + CommandBase base = (CommandBase) cmd.getConstructor().newInstance(); DummyCommand dummy = new DummyCommand(plugin, base, info.name(), diff --git a/src/main/java/io/github/simplex/DummyCommand.java b/src/main/java/io/github/simplex/cl/DummyCommand.java similarity index 97% rename from src/main/java/io/github/simplex/DummyCommand.java rename to src/main/java/io/github/simplex/cl/DummyCommand.java index 901cc44..f61b422 100644 --- a/src/main/java/io/github/simplex/DummyCommand.java +++ b/src/main/java/io/github/simplex/cl/DummyCommand.java @@ -1,4 +1,4 @@ -package io.github.simplex; +package io.github.simplex.cl; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; diff --git a/src/main/java/io/github/simplex/Permissible.java b/src/main/java/io/github/simplex/cl/Permissible.java similarity index 96% rename from src/main/java/io/github/simplex/Permissible.java rename to src/main/java/io/github/simplex/cl/Permissible.java index 993a17b..ac4a1c5 100644 --- a/src/main/java/io/github/simplex/Permissible.java +++ b/src/main/java/io/github/simplex/cl/Permissible.java @@ -1,4 +1,4 @@ -package io.github.simplex; +package io.github.simplex.cl; import org.bukkit.command.CommandSender; diff --git a/src/main/java/io/github/simplex/api/ICommand.java b/src/main/java/io/github/simplex/cl/api/ICommand.java similarity index 100% rename from src/main/java/io/github/simplex/api/ICommand.java rename to src/main/java/io/github/simplex/cl/api/ICommand.java diff --git a/src/main/java/io/github/simplex/cl/api/SubCommand.java b/src/main/java/io/github/simplex/cl/api/SubCommand.java new file mode 100644 index 0000000..3da34be --- /dev/null +++ b/src/main/java/io/github/simplex/cl/api/SubCommand.java @@ -0,0 +1,6 @@ +package io.github.simplex.api; + +@FunctionalInterface +public interface SubCommand { + void execute(); +} diff --git a/src/main/java/io/github/simplex/api/annotations/Info.java b/src/main/java/io/github/simplex/cl/api/annotations/Info.java similarity index 88% rename from src/main/java/io/github/simplex/api/annotations/Info.java rename to src/main/java/io/github/simplex/cl/api/annotations/Info.java index c2f2bed..5c8c9e4 100644 --- a/src/main/java/io/github/simplex/api/annotations/Info.java +++ b/src/main/java/io/github/simplex/cl/api/annotations/Info.java @@ -1,4 +1,4 @@ -package io.github.simplex.api.annotations; +package io.github.simplex.cl.api.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/src/main/java/io/github/simplex/cl/impl/ExampleCommand.java b/src/main/java/io/github/simplex/cl/impl/ExampleCommand.java new file mode 100644 index 0000000..df29811 --- /dev/null +++ b/src/main/java/io/github/simplex/cl/impl/ExampleCommand.java @@ -0,0 +1,28 @@ +package io.github.simplex.cl.impl; + +import io.github.simplex.cl.CommandBase; +import io.github.simplex.cl.api.annotations.Info; +import io.github.simplex.msgutils.BasicColors; +import net.kyori.adventure.text.format.TextColor; +import org.bukkit.command.CommandSender; + +@Info(name = "example", description = "An example command implementation to see how this works.", usage = "/example [info]", aliases = "ex, impl") +public final class ExampleCommand extends CommandBase { + public ExampleCommand() { + super("simplexcl.example", + "You do not have permission to use this command!", + true); + } + + @Override + public void execute(CommandSender sender, String[] args, boolean allowConsole) { + if (args.length == 1) { + subCommand("info", + args, + () -> sender.sendMessage(msg("SimplexCL was created by Simplex Development Group.", BasicColors.GOLD))); + return; + } + + sender.sendMessage(msg("This is an example command.", BasicColors.BLUE)); + } +} diff --git a/src/main/java/io/github/simplex/impl/ExampleCommand.java b/src/main/java/io/github/simplex/impl/ExampleCommand.java deleted file mode 100644 index ee2efb7..0000000 --- a/src/main/java/io/github/simplex/impl/ExampleCommand.java +++ /dev/null @@ -1,39 +0,0 @@ -package io.github.simplex.impl; - -import io.github.simplex.CommandBase; -import io.github.simplex.SubCommandBase; -import io.github.simplex.api.annotations.Info; -import io.github.simplex.api.annotations.Permission; -import io.github.simplex.api.annotations.SubCommand; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.TextComponent; -import org.bukkit.command.CommandSender; - -@Info(name = "example", description = "An example command implementation to see how this works.", usage = "/example [info]", aliases = "ex, impl") -@Permission(permission = "simplexcl.example", permissionMessage = "You cannot use this command!") -public final class ExampleCommand extends CommandBase { - public ExampleCommand(String permission, String permissionMessage, boolean allowConsole) { - super(permission, permissionMessage, allowConsole); - } - - @Override - public void execute(CommandSender sender, String[] args, boolean allowConsole) { - - } - - @SubCommand(name = "info") - @Permission(permission = "simplexcl.example.info") - public class InfoSubCommand extends SubCommandBase { - public InfoSubCommand(String permission, String permissionMessage, boolean allowConsole) { - super(permission, permissionMessage, allowConsole); - } - - @Override - public void execute(CommandSender sender, String[] args, boolean allowConsole) { - TextComponent message = Component.empty(); - message = message.append(msg("SimplexCL was created by SimplexDevelopment!")) - .append(msg("https://github.com/SimplexDevelopment/")); - sender.sendMessage(message); - } - } -} diff --git a/src/main/java/io/github/simplex/msgutils/AdvancedColors.java b/src/main/java/io/github/simplex/msgutils/AdvancedColors.java new file mode 100644 index 0000000..f33433b --- /dev/null +++ b/src/main/java/io/github/simplex/msgutils/AdvancedColors.java @@ -0,0 +1,46 @@ +package io.github.simplex.msgutils; + +import net.kyori.adventure.text.format.TextColor; + +public enum AdvancedColors { + AMETHYST(TextColor.color(253, 103, 204)), + AQUAMARINE(TextColor.color(127, 255, 212)), + AZURE(TextColor.color(0, 127, 255)), + BRONZE(TextColor.color(205, 127, 50)), + BURLYWOOD(TextColor.color(222, 184, 135)), + CHARTREUSE(TextColor.color(223, 255, 0)), + CORAL(TextColor.color(255, 127, 80)), + CORNFLOWER(TextColor.color(100, 149, 237)), + CRIMSON(TextColor.color(220, 20, 60)), + FUCHSIA(TextColor.color(255, 0, 255)), + GOLDENROD(TextColor.color(218, 165, 32)), + GRAPE(TextColor.color(111, 45, 168)), + HONEYDEW(TextColor.color(240, 255, 240)), + INDIGO(TextColor.color(75, 0, 130)), + IVORY(TextColor.color(255, 255, 240)), + MAROON(TextColor.color(128, 0 , 0)), + MEDIUM_VIOLET_RED(TextColor.color(199, 21, 133)), + MOCCASIN(TextColor.color(255, 228, 181)), + OLIVE(TextColor.color(128, 128, 0)), + PALE_VIOLET_RED(TextColor.color(219, 112, 147)), + PLUM(TextColor.color(142, 69, 133)), + SALMON(TextColor.color(250, 128, 114)), + SEA_GREEN(TextColor.color(46, 139, 87)), + SILVER(TextColor.color(192, 192, 192)), + SLATE(TextColor.color(192, 194, 201)), + SLATE_BLUE(TextColor.color(106, 90, 205)), + STEEL_BLUE(TextColor.color(70, 130, 180)), + TOMATO(TextColor.color(255, 99, 71)), + TURQUOISE(TextColor.color(48, 213, 200)), + WHEAT(TextColor.color(245, 222, 179)); + + final TextColor color; + + AdvancedColors(TextColor color) { + this.color = color; + } + + public TextColor getColor() { + return color; + } +} diff --git a/src/main/java/io/github/simplex/msgutils/BasicColors.java b/src/main/java/io/github/simplex/msgutils/BasicColors.java new file mode 100644 index 0000000..bb0665a --- /dev/null +++ b/src/main/java/io/github/simplex/msgutils/BasicColors.java @@ -0,0 +1,32 @@ +package io.github.simplex.msgutils; + +import net.kyori.adventure.text.format.TextColor; + +public enum BasicColors { + RED(TextColor.color(255, 0, 0)), + DARK_RED(TextColor.color(127, 0, 0)), + ORANGE(TextColor.color(255, 165, 0)), + DARK_ORANGE(TextColor.color(255, 140, 0)), + YELLOW(TextColor.color(255, 255, 0)), + GOLD(TextColor.color(255, 215, 0)), + GREEN(TextColor.color(0, 255, 0)), + DARK_GREEN(TextColor.color(0, 127, 0)), + BLUE(TextColor.color(0, 0, 255)), + DARK_BLUE(TextColor.color(0, 0, 127)), + AQUA(TextColor.color(0, 255, 255)), + DARK_AQUA(TextColor.color(0, 127, 127)), + PURPLE(TextColor.color(255, 0, 255)), + DARK_PURPLE(TextColor.color(127, 0, 127)), + PINK(TextColor.color(255, 105, 180)), + DARK_PINK(TextColor.color(231, 84, 128)); + + final TextColor color; + + BasicColors(TextColor color) { + this.color = color; + } + + public TextColor getColor() { + return color; + } +}