-
Notifications
You must be signed in to change notification settings - Fork 2
Remove Ponder dependency and implement equivalent classes locally #296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
96f8468
866023a
c24469f
abcc21a
2c710c3
78b1973
6d657cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| name: CI | ||
|
|
||
| on: | ||
| push: | ||
| branches: [ main, master ] | ||
| pull_request: | ||
| branches: [ main, master ] | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up JDK 8 | ||
| uses: actions/setup-java@v4 | ||
| with: | ||
| java-version: '8' | ||
| distribution: 'temurin' | ||
|
|
||
| - name: Cache Maven packages | ||
| uses: actions/cache@v4 | ||
| with: | ||
| path: ~/.m2/repository | ||
| key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} | ||
| restore-keys: ${{ runner.os }}-m2 | ||
|
|
||
| - name: Compile | ||
| run: mvn compile --batch-mode | ||
|
|
||
| - name: Run tests | ||
| run: mvn test --batch-mode |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package dansplugins.wildpets.commands; | ||
|
|
||
| import org.bukkit.ChatColor; | ||
| import org.bukkit.command.CommandSender; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
|
|
||
| /** | ||
| * Abstract base class for plugin commands. | ||
| */ | ||
| public abstract class AbstractPluginCommand { | ||
| private final ArrayList<String> names; | ||
| private final ArrayList<String> permissions; | ||
|
|
||
| public AbstractPluginCommand(ArrayList<String> names, ArrayList<String> permissions) { | ||
| this.names = names; | ||
| this.permissions = permissions; | ||
| } | ||
|
|
||
| /** | ||
| * Method to execute the command with no arguments. | ||
| * @param sender The sender of the command. | ||
| * @return Whether the execution of the command was successful. | ||
| */ | ||
| public abstract boolean execute(CommandSender sender); | ||
|
|
||
| /** | ||
| * @param sender The sender of the command. | ||
| * @param args The arguments of the command. | ||
| * @return Whether the execution of the command was successful. | ||
| */ | ||
| public abstract boolean execute(CommandSender sender, String[] args); | ||
|
|
||
| /** | ||
| * @param message to send. | ||
| * @param args to check. | ||
| * @param sender to send message to. | ||
| * @param color of the message. | ||
| * @return Boolean signifying whether there were no arguments. | ||
| */ | ||
| public boolean sendMessageIfNoArguments(String message, String[] args, CommandSender sender, ChatColor color) { | ||
| if (args.length == 0) { | ||
| sender.sendMessage(ChatColor.RED + message); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * @param line to convert into an Integer. | ||
| * @param orElse if the conversion fails. | ||
| * @return {@link Integer} numeric. | ||
| */ | ||
| public int getIntSafe(String line, int orElse) { | ||
| try { | ||
| return Integer.parseInt(line); | ||
| } catch (Exception e) { | ||
| return orElse; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Method to test if something matches any goal string. | ||
| * | ||
| * @param matchCase for the comparison (or not) | ||
| * @param what to test | ||
| * @param goals to compare with | ||
| * @return {@code true} if something in goals matches what. | ||
| */ | ||
| public boolean safeEquals(boolean matchCase, String what, String... goals) { | ||
| return Arrays.stream(goals).anyMatch(goal -> | ||
| matchCase && goal.equals(what) || !matchCase && goal.equalsIgnoreCase(what) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return A list of names of the command. | ||
| */ | ||
| public ArrayList<String> getNames() { | ||
| return names; | ||
| } | ||
|
|
||
| /** | ||
| * @return A list of permissions of the command. | ||
| */ | ||
| public ArrayList<String> getPermissions() { | ||
| return permissions; | ||
| } | ||
|
|
||
| public ArrayList<String> extractArgumentsInsideDoubleQuotes(String[] args) throws Exception { | ||
| ArgumentParser argumentParser = new ArgumentParser(); | ||
| ArrayList<String> doubleQuoteArgs = argumentParser.getArgumentsInsideDoubleQuotes(args); | ||
| if (doubleQuoteArgs.size() < 2) { | ||
| throw new Exception(); | ||
| } | ||
| return doubleQuoteArgs; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package dansplugins.wildpets.commands; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * Utility class for parsing command arguments. | ||
| */ | ||
| public class ArgumentParser { | ||
|
|
||
| /** | ||
| * @param args to modify. | ||
| * @return Modified Array of Strings with the first argument dropped. | ||
| * @throws IllegalArgumentException if the arguments given are invalid. | ||
| */ | ||
| public String[] dropFirstArgument(String[] args) { | ||
| ensureArgumentsExist(args); | ||
| String[] toReturn = new String[args.length - 1]; | ||
| System.arraycopy(args, 1, toReturn, 0, args.length - 1); | ||
| return toReturn; | ||
| } | ||
|
|
||
| /** | ||
| * @param args to compile and scan. | ||
| * @return {@link ArrayList} of {@link String} which were surrounded by double quotes. | ||
| * @throws IllegalArgumentException if the arguments given are invalid. | ||
| */ | ||
| public ArrayList<String> getArgumentsInsideDoubleQuotes(String[] args) { | ||
| ensureArgumentsExist(args); | ||
| return parseForArguments(args); | ||
| } | ||
|
|
||
| private ArrayList<String> parseForArguments(String[] args) { | ||
| ArrayList<String> toReturn = new ArrayList<>(); | ||
| final String argumentString = String.join(" ", args); | ||
| final Matcher matcher = Pattern.compile("\"[^\"]*\"").matcher(argumentString); | ||
| while (matcher.find()) { | ||
| toReturn.add(matcher.group().replace("\"", "")); | ||
| } | ||
| return toReturn; | ||
| } | ||
|
|
||
| private void ensureArgumentsExist(String[] args) { | ||
| if (args == null || args.length == 0) { | ||
| throw new IllegalArgumentException("Arguments not valid."); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,58 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
| package dansplugins.wildpets.commands; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| import org.bukkit.ChatColor; | ||||||||||||||||||||||||||||||||||||||||||||||
| import org.bukkit.command.CommandSender; | ||||||||||||||||||||||||||||||||||||||||||||||
| import org.bukkit.plugin.java.JavaPlugin; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.ArrayList; | ||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Set; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||
| * Service for managing and executing plugin commands. | ||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||
| public class CommandService { | ||||||||||||||||||||||||||||||||||||||||||||||
| private ArrayList<AbstractPluginCommand> commands = new ArrayList<>(); | ||||||||||||||||||||||||||||||||||||||||||||||
| private final Set<String> coreCommands; | ||||||||||||||||||||||||||||||||||||||||||||||
| private String notFoundMessage; | ||||||||||||||||||||||||||||||||||||||||||||||
| private final ArgumentParser parser = new ArgumentParser(); | ||||||||||||||||||||||||||||||||||||||||||||||
| private final PermissionChecker permissionChecker = new PermissionChecker(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public CommandService(JavaPlugin plugin) { | ||||||||||||||||||||||||||||||||||||||||||||||
| coreCommands = plugin.getDescription().getCommands().keySet(); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public void initialize(ArrayList<AbstractPluginCommand> commands, String notFoundMessage) { | ||||||||||||||||||||||||||||||||||||||||||||||
| this.commands = commands; | ||||||||||||||||||||||||||||||||||||||||||||||
| this.notFoundMessage = notFoundMessage; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public boolean interpretAndExecuteCommand(CommandSender sender, String label, String[] args) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (!coreCommands.contains(label)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (args.length == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| String subCommand = args[0]; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| String[] arguments = parser.dropFirstArgument(args); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| for (AbstractPluginCommand command : commands) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (command.getNames().contains(subCommand)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (!permissionChecker.checkPermission(sender, command.getPermissions())) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| if (arguments.length == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return command.execute(sender); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| else { | ||||||||||||||||||||||||||||||||||||||||||||||
| return command.execute(sender, arguments); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (command.getNames().contains(subCommand)) { | |
| if (!permissionChecker.checkPermission(sender, command.getPermissions())) { | |
| return false; | |
| } | |
| if (arguments.length == 0) { | |
| return command.execute(sender); | |
| } | |
| else { | |
| return command.execute(sender, arguments); | |
| } | |
| for (String name : command.getNames()) { | |
| if (name.equalsIgnoreCase(subCommand)) { | |
| if (!permissionChecker.checkPermission(sender, command.getPermissions())) { | |
| return false; | |
| } | |
| if (arguments.length == 0) { | |
| return command.execute(sender); | |
| } | |
| else { | |
| return command.execute(sender, arguments); | |
| } | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sendMessageIfNoArgumentsignores itscolorparameter and always prefixes messages withChatColor.RED, so callers cannot control the color as the signature suggests. Use the providedcolorwhen sending the message (or remove the parameter if color should be fixed).