Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/ci.yml
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
10 changes: 0 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -75,12 +71,6 @@
<version>1.17.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.Preponderous-Software</groupId>
<artifactId>ponder</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/dansplugins/wildpets/WildPets.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@
import dansplugins.wildpets.storage.StorageService;
import dansplugins.wildpets.scheduler.Scheduler;
import org.bukkit.ChatColor;
import preponderous.ponder.minecraft.bukkit.abs.AbstractPluginCommand;
import preponderous.ponder.minecraft.bukkit.abs.PonderBukkitPlugin;
import preponderous.ponder.minecraft.bukkit.services.CommandService;
import preponderous.ponder.minecraft.bukkit.tools.EventHandlerRegistry;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.util.ArrayList;
Expand All @@ -27,10 +24,10 @@
/**
* @author Daniel McCoy Stephenson
*/
public final class WildPets extends PonderBukkitPlugin {
public final class WildPets extends JavaPlugin {
private final String pluginVersion = "v" + getDescription().getVersion();

private final CommandService commandService = new CommandService(getPonder());
private final CommandService commandService = new CommandService(this);
private final EphemeralData ephemeralData = new EphemeralData();
private final EntityConfigService entityConfigService = new EntityConfigService(this);
private final ConfigService configService = new ConfigService(this, entityConfigService);
Expand Down Expand Up @@ -140,7 +137,7 @@ private void handlebStatsIntegration() {
}

/**
* Registers the event handlers of the plugin using Ponder.
* Registers the event handlers of the plugin.
*/
private void registerEventHandlers() {
ArrayList<Listener> listeners = new ArrayList<>();
Expand All @@ -154,7 +151,7 @@ private void registerEventHandlers() {
}

/**
* Initializes Ponder's command service with the plugin's commands.
* Initializes the command service with the plugin's commands.
*/
private void initializeCommandService() {
ArrayList<AbstractPluginCommand> commands = new ArrayList<>(Arrays.asList(
Expand All @@ -168,4 +165,4 @@ private void initializeCommandService() {
));
commandService.initialize(commands, "That command wasn't found.");
}
}
}
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(color + 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;
}
}
49 changes: 49 additions & 0 deletions src/main/java/dansplugins/wildpets/commands/ArgumentParser.java
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.");
}
}
}
3 changes: 1 addition & 2 deletions src/main/java/dansplugins/wildpets/commands/CallCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import dansplugins.wildpets.data.EphemeralData;
import dansplugins.wildpets.pet.Pet;
import preponderous.ponder.minecraft.bukkit.abs.AbstractPluginCommand;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -55,4 +54,4 @@ public boolean execute(CommandSender sender) {
public boolean execute(CommandSender commandSender, String[] strings) {
return execute(commandSender);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dansplugins.wildpets.commands;

import dansplugins.wildpets.data.EphemeralData;
import preponderous.ponder.minecraft.bukkit.abs.AbstractPluginCommand;

import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -37,4 +36,4 @@ public boolean execute(CommandSender sender) {
public boolean execute(CommandSender commandSender, String[] strings) {
return execute(commandSender);
}
}
}
60 changes: 60 additions & 0 deletions src/main/java/dansplugins/wildpets/commands/CommandService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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) {
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);
}
}
}
}
sender.sendMessage(ChatColor.RED + notFoundMessage);
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dansplugins.wildpets.commands;

import preponderous.ponder.minecraft.bukkit.abs.AbstractPluginCommand;

import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -51,4 +50,4 @@ else if (args[0].equalsIgnoreCase("set")) {
return false;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dansplugins.wildpets.commands;

import dansplugins.wildpets.WildPets;
import preponderous.ponder.minecraft.bukkit.abs.AbstractPluginCommand;

import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -32,4 +31,4 @@ public boolean execute(CommandSender commandSender) {
public boolean execute(CommandSender commandSender, String[] strings) {
return execute(commandSender);
}
}
}
Loading