From 91ac26714d4f78aba1b9e6c912dabf52e5bf503e Mon Sep 17 00:00:00 2001 From: ChomeNS Date: Sun, 19 Mar 2023 10:47:15 +0700 Subject: [PATCH] fix stuff and add stuff yup --- .../chomensbot_mabe/command/Command.java | 2 + .../commands/CommandBlockCommand.java | 13 ++++- .../commands/CowsayCommand.java | 11 +++- .../commands/CreatorCommand.java | 46 ++++++++++++++++ .../commands/DiscordCommand.java | 52 +++++++++++++++++++ .../chomensbot_mabe/commands/EchoCommand.java | 11 +++- .../chomensbot_mabe/commands/HelpCommand.java | 25 +++++---- .../chomensbot_mabe/commands/TestCommand.java | 9 ++++ .../commands/ThrowCommand.java | 11 +++- .../commands/ValidateCommand.java | 12 +++-- .../plugins/CommandHandlerPlugin.java | 37 ++++++------- .../util/ElementUtilities.java | 16 ++++++ 12 files changed, 210 insertions(+), 35 deletions(-) create mode 100644 src/main/java/me/chayapak1/chomensbot_mabe/commands/CreatorCommand.java create mode 100644 src/main/java/me/chayapak1/chomensbot_mabe/commands/DiscordCommand.java create mode 100644 src/main/java/me/chayapak1/chomensbot_mabe/util/ElementUtilities.java diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/command/Command.java b/src/main/java/me/chayapak1/chomensbot_mabe/command/Command.java index 81b7d42..1fbc635 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/command/Command.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/command/Command.java @@ -5,8 +5,10 @@ import net.kyori.adventure.text.Component; import java.util.List; public interface Command { + String name(); String description(); List usage(); + List alias(); int trustLevel(); Component execute(CommandContext context, String[] args, String[] fullArgs) throws Exception; diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/commands/CommandBlockCommand.java b/src/main/java/me/chayapak1/chomensbot_mabe/commands/CommandBlockCommand.java index f9d40fc..3c49fdc 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/commands/CommandBlockCommand.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/commands/CommandBlockCommand.java @@ -9,17 +9,28 @@ import java.util.ArrayList; import java.util.List; public class CommandBlockCommand implements Command { + public String name() { return "cb"; } + public String description() { return "Executes a command in the command core"; } public List usage() { final List usages = new ArrayList<>(); - usages.add(""); + usages.add("<{command}>"); return usages; } + public List alias() { + final List aliases = new ArrayList<>(); + aliases.add("cmd"); + aliases.add("commandblock"); + aliases.add("run"); + + return aliases; + } + public int trustLevel() { return 0; } diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/commands/CowsayCommand.java b/src/main/java/me/chayapak1/chomensbot_mabe/commands/CowsayCommand.java index 5456eea..5b4deb3 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/commands/CowsayCommand.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/commands/CowsayCommand.java @@ -10,17 +10,26 @@ import java.util.Arrays; import java.util.List; public class CowsayCommand implements Command { + public String name() { return "cowsay"; } + public String description() { return "Moo"; } public List usage() { final List usages = new ArrayList<>(); - usages.add(" "); + usages.add(" <{message}>"); return usages; } + public List alias() { + final List aliases = new ArrayList<>(); + aliases.add(""); + + return aliases; + } + public int trustLevel() { return 0; } diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/commands/CreatorCommand.java b/src/main/java/me/chayapak1/chomensbot_mabe/commands/CreatorCommand.java new file mode 100644 index 0000000..1f13c10 --- /dev/null +++ b/src/main/java/me/chayapak1/chomensbot_mabe/commands/CreatorCommand.java @@ -0,0 +1,46 @@ +package me.chayapak1.chomensbot_mabe.commands; + +import me.chayapak1.chomensbot_mabe.command.Command; +import me.chayapak1.chomensbot_mabe.command.CommandContext; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; + +import java.util.ArrayList; +import java.util.List; + +public class CreatorCommand implements Command { + public String name() { return "creator"; } + + public String description() { + return "Shows the bot's creator"; + } + + public List usage() { + final List usages = new ArrayList<>(); + usages.add(""); + + return usages; + } + + public List alias() { + final List aliases = new ArrayList<>(); + aliases.add(""); + + return aliases; + } + + public int trustLevel() { + return 0; + } + + public Component execute(CommandContext context, String[] args, String[] fullArgs) { + context.sendOutput( + Component.empty() + .append(Component.text("ChomeNS Bot ").color(NamedTextColor.YELLOW)) + .append(Component.text("was created by ").color(NamedTextColor.WHITE)) + .append(Component.text("chayapak").color(NamedTextColor.GREEN)) + ); + + return Component.text("success"); + } +} diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/commands/DiscordCommand.java b/src/main/java/me/chayapak1/chomensbot_mabe/commands/DiscordCommand.java new file mode 100644 index 0000000..1fc37ef --- /dev/null +++ b/src/main/java/me/chayapak1/chomensbot_mabe/commands/DiscordCommand.java @@ -0,0 +1,52 @@ +package me.chayapak1.chomensbot_mabe.commands; + +import me.chayapak1.chomensbot_mabe.command.Command; +import me.chayapak1.chomensbot_mabe.command.CommandContext; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.event.ClickEvent; +import net.kyori.adventure.text.format.NamedTextColor; + +import java.util.ArrayList; +import java.util.List; + +public class DiscordCommand implements Command { + public String name() { return "discord"; } + + public String description() { + return "Shows the Discord invite"; + } + + public List usage() { + final List usages = new ArrayList<>(); + usages.add(""); + + return usages; + } + + public List alias() { + final List aliases = new ArrayList<>(); + aliases.add(""); + + return aliases; + } + + public int trustLevel() { + return 0; + } + + public Component execute(CommandContext context, String[] args, String[] fullArgs) { + final String link = "https://discord.gg/xdgCkUyaA4"; + context.sendOutput( + Component.empty() + .append(Component.text("The Discord invite is ").color(NamedTextColor.WHITE)) + .append( + Component + .text(link) + .clickEvent(ClickEvent.openUrl(link)) + .color(NamedTextColor.BLUE) + ) + ); + + return Component.text("success"); + } +} diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/commands/EchoCommand.java b/src/main/java/me/chayapak1/chomensbot_mabe/commands/EchoCommand.java index a7e8900..e68f03f 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/commands/EchoCommand.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/commands/EchoCommand.java @@ -9,17 +9,26 @@ import java.util.ArrayList; import java.util.List; public class EchoCommand implements Command { + public String name() { return "echo"; } + public String description() { return "Says a message"; } public List usage() { final List usages = new ArrayList<>(); - usages.add(""); + usages.add("<{message}>"); return usages; } + public List alias() { + final List aliases = new ArrayList<>(); + aliases.add("say"); + + return aliases; + } + public int trustLevel() { return 0; } diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/commands/HelpCommand.java b/src/main/java/me/chayapak1/chomensbot_mabe/commands/HelpCommand.java index f59d211..df53d3b 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/commands/HelpCommand.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/commands/HelpCommand.java @@ -10,9 +10,10 @@ import net.kyori.adventure.text.format.NamedTextColor; import java.util.ArrayList; import java.util.List; -import java.util.Map; public class HelpCommand implements Command { + public String name() { return "help"; } + public String description() { return "Shows the help"; } @@ -24,6 +25,15 @@ public class HelpCommand implements Command { return usages; } + public List alias() { + final List aliases = new ArrayList<>(); + aliases.add("heko"); + aliases.add("cmds"); + aliases.add("commands"); + + return aliases; + } + public int trustLevel() { return 0; } @@ -62,9 +72,8 @@ public class HelpCommand implements Command { public List getCommandListByTrustLevel (int trustLevel) { final List list = new ArrayList<>(); - for (Map.Entry entry : CommandHandlerPlugin.commands().entrySet()) { - final String name = entry.getKey(); - final Command command = entry.getValue(); + for (Command command : CommandHandlerPlugin.commands()) { + final String name = command.name(); if (command.trustLevel() != trustLevel) continue; list.add(Component.text(name).color(getColorByTrustLevel(trustLevel))); @@ -89,13 +98,11 @@ public class HelpCommand implements Command { final String commandName = args[0]; - for (Map.Entry entry : CommandHandlerPlugin.commands().entrySet()) { - if (!entry.getKey().equals(commandName)) continue; + for (Command command : CommandHandlerPlugin.commands()) { + if (!command.name().equals(commandName)) continue; final List usages = new ArrayList<>(); - final Command command = entry.getValue(); - usages.add( Component.empty() .append(Component.text(prefix + commandName).color(NamedTextColor.GOLD)) @@ -105,7 +112,7 @@ public class HelpCommand implements Command { usages.add( Component.empty() .append(Component.text("Trust level: ").color(NamedTextColor.GREEN)) - .append(Component.text("TODO").color(NamedTextColor.YELLOW)) + .append(Component.text(command.trustLevel()).color(NamedTextColor.YELLOW)) ); for (String usage : command.usage()) { diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/commands/TestCommand.java b/src/main/java/me/chayapak1/chomensbot_mabe/commands/TestCommand.java index cd9ec5a..4835533 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/commands/TestCommand.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/commands/TestCommand.java @@ -9,6 +9,8 @@ import java.util.ArrayList; import java.util.List; public class TestCommand implements Command { + public String name() { return "test"; } + public String description() { return "Tests if the bot is working"; } @@ -20,6 +22,13 @@ public class TestCommand implements Command { return usages; } + public List alias() { + final List aliases = new ArrayList<>(); + aliases.add(""); + + return aliases; + } + public int trustLevel() { return 0; } diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/commands/ThrowCommand.java b/src/main/java/me/chayapak1/chomensbot_mabe/commands/ThrowCommand.java index a5b1e5b..4937960 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/commands/ThrowCommand.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/commands/ThrowCommand.java @@ -8,17 +8,26 @@ import java.util.ArrayList; import java.util.List; public class ThrowCommand implements Command { + public String name() { return "throw"; } + public String description() { return "A command to throw an error, kinda useless"; } public List usage() { final List usages = new ArrayList<>(); - usages.add("[message]"); + usages.add("[{message}]"); return usages; } + public List alias() { + final List aliases = new ArrayList<>(); + aliases.add("throwerror"); + + return aliases; + } + public int trustLevel() { return 0; } diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/commands/ValidateCommand.java b/src/main/java/me/chayapak1/chomensbot_mabe/commands/ValidateCommand.java index b6f5373..0143912 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/commands/ValidateCommand.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/commands/ValidateCommand.java @@ -10,13 +10,12 @@ import java.util.ArrayList; import java.util.List; public class ValidateCommand implements Command { + public String name() { return "validate"; } - @Override public String description() { return "Validates a hash"; } - @Override public List usage() { final List usages = new ArrayList<>(); usages.add(""); @@ -24,12 +23,17 @@ public class ValidateCommand implements Command { return usages; } - @Override + public List alias() { + final List aliases = new ArrayList<>(); + aliases.add("checkhash"); + + return aliases; + } + public int trustLevel() { return 1; } - @Override public Component execute(CommandContext context, String[] args, String[] fullArgs) { final Bot bot = context.bot(); final String hash = fullArgs[0]; diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/CommandHandlerPlugin.java b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/CommandHandlerPlugin.java index b14873e..25ff9c9 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/CommandHandlerPlugin.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/CommandHandlerPlugin.java @@ -4,31 +4,31 @@ import lombok.Getter; import me.chayapak1.chomensbot_mabe.command.Command; import me.chayapak1.chomensbot_mabe.command.CommandContext; import me.chayapak1.chomensbot_mabe.commands.*; +import me.chayapak1.chomensbot_mabe.util.ElementUtilities; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.format.NamedTextColor; import org.apache.commons.lang3.exception.ExceptionUtils; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; public class CommandHandlerPlugin { - @Getter private static final Map commands = new HashMap<>(); + @Getter private static final List commands = new ArrayList<>(); public CommandHandlerPlugin () { - registerCommand("cb", new CommandBlockCommand()); - registerCommand("cowsay", new CowsayCommand()); - registerCommand("echo", new EchoCommand()); - registerCommand("help", new HelpCommand()); - registerCommand("test", new TestCommand()); - registerCommand("throw", new ThrowCommand()); - registerCommand("validate", new ValidateCommand()); + registerCommand(new CommandBlockCommand()); + registerCommand(new CowsayCommand()); + registerCommand(new EchoCommand()); + registerCommand(new CreatorCommand()); + registerCommand(new DiscordCommand()); + registerCommand(new HelpCommand()); + registerCommand(new TestCommand()); + registerCommand(new ThrowCommand()); + registerCommand(new ValidateCommand()); } - public void registerCommand (String commandName, Command command) { - commands.put(commandName, command); + public void registerCommand (Command command) { + commands.add(command); } public static Component executeCommand (String input, CommandContext context, String hash, String ownerHash) { @@ -36,7 +36,7 @@ public class CommandHandlerPlugin { final String commandName = splitInput[0]; - final Command command = commands.get(commandName); + final Command command = ElementUtilities.findCommand(commands, commandName); // idea told this as "Condition 'command == null' is always 'false'" and its not true LMFAO if (command == null) return Component.text("Unknown command: " + commandName).color(NamedTextColor.RED); @@ -45,10 +45,11 @@ public class CommandHandlerPlugin { final String[] fullArgs = Arrays.copyOfRange(splitInput, 1, splitInput.length); final int longestUsageIndex = getLongestUsageIndex(command.usage()); - final int minimumArgs = getMinimumArgs(command.usage().get(longestUsageIndex)); - final int maximumArgs = getMaximumArgs(command.usage().get(longestUsageIndex)); + final String usage = command.usage().get(longestUsageIndex); + final int minimumArgs = getMinimumArgs(usage); + final int maximumArgs = getMaximumArgs(usage); if (fullArgs.length < minimumArgs) return Component.text("Excepted minimum of " + minimumArgs + " argument(s), got " + fullArgs.length).color(NamedTextColor.RED); - if (fullArgs.length > maximumArgs) return Component.text("Too much arguments, expected " + maximumArgs).color(NamedTextColor.RED); + if (fullArgs.length > maximumArgs && !usage.contains("{")) return Component.text("Too much arguments, expected " + maximumArgs).color(NamedTextColor.RED); String userHash = ""; if (trustLevel > 0 && splitInput.length >= 2) userHash = splitInput[1]; diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/util/ElementUtilities.java b/src/main/java/me/chayapak1/chomensbot_mabe/util/ElementUtilities.java new file mode 100644 index 0000000..03ccd5e --- /dev/null +++ b/src/main/java/me/chayapak1/chomensbot_mabe/util/ElementUtilities.java @@ -0,0 +1,16 @@ +package me.chayapak1.chomensbot_mabe.util; + +import me.chayapak1.chomensbot_mabe.command.Command; + +import java.util.List; + +public class ElementUtilities { + public static Command findCommand(List commands, String searchTerm) { + for (Command command : commands) { + if (command.name().equals(searchTerm) || command.alias().contains(searchTerm)) { + return command; + } + } + return null; + } +}