add hashing, add maximum and minimum args length (broken)

also add other stuff but just check it yourself lol
This commit is contained in:
ChomeNS
2023-03-18 21:12:37 +07:00
parent d0f5d29092
commit fce5527abe
15 changed files with 237 additions and 40 deletions

View File

@@ -11,6 +11,7 @@ import org.apache.commons.lang3.exception.ExceptionUtils;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CommandHandlerPlugin {
@@ -23,39 +24,98 @@ public class CommandHandlerPlugin {
registerCommand("help", new HelpCommand());
registerCommand("test", new TestCommand());
registerCommand("throw", new ThrowCommand());
registerCommand("validate", new ValidateCommand());
}
public void registerCommand (String commandName, Command command) {
commands.put(commandName, command);
}
public static Component executeCommand (String input, CommandContext context) {
final String[] splitInput = input.split(" ");
public static Component executeCommand (String input, CommandContext context, String hash, String ownerHash) {
final String[] splitInput = input.split("\\s+");
final String commandName = splitInput[0];
final String[] args = Arrays.copyOfRange(splitInput, 1, splitInput.length);
final Command command = commands.get(commandName);
if (command != null) {
try {
return command.execute(context, args);
} catch (Exception exception) {
exception.printStackTrace();
// 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);
final String stackTrace = ExceptionUtils.getStackTrace(exception);
return Component
.text("An error occurred while trying to execute the command, hover here for more details", NamedTextColor.RED)
.hoverEvent(
HoverEvent.showText(
Component
.text(stackTrace)
.color(NamedTextColor.RED)
)
);
}
} else {
return Component.text("Unknown command: " + commandName).color(NamedTextColor.RED);
final int trustLevel = command.trustLevel();
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));
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);
String userHash = "";
if (trustLevel > 0 && splitInput.length >= 2) userHash = splitInput[1];
final String[] args = Arrays.copyOfRange(splitInput, (trustLevel > 0) ? 2 : 1, splitInput.length);
if (command.trustLevel() > 0) {
if (
command.trustLevel() == 1 &&
!userHash.equals(hash) &&
!userHash.equals(ownerHash)
) return Component.text("Invalid hash").color(NamedTextColor.RED);
if (
command.trustLevel() == 2 &&
!userHash.equals(ownerHash)
) return Component.text("Invalid OwnerHash").color(NamedTextColor.RED);
}
try {
return command.execute(context, args, fullArgs);
} catch (Exception exception) {
exception.printStackTrace();
final String stackTrace = ExceptionUtils.getStackTrace(exception);
return Component
.text("An error occurred while trying to execute the command, hover here for more details", NamedTextColor.RED)
.hoverEvent(
HoverEvent.showText(
Component
.text(stackTrace)
.color(NamedTextColor.RED)
)
);
}
}
private static int getLongestUsageIndex(List<String> usages) {
int longestIndex = 0;
int maxLength = 0;
for (int i = 0; i < usages.size(); i++) {
String[] args = usages.get(i).split("\\s+");
if (args.length > maxLength) {
longestIndex = i;
maxLength = args.length;
}
}
return longestIndex;
}
private static int getMinimumArgs(String usage) {
int count = 0;
for (int i = 0; i < usage.length(); i++) {
if (usage.charAt(i) == '<') {
count++;
}
}
return count;
}
private static int getMaximumArgs(String usage) {
int count = 0;
for (int i = 0; i < usage.length(); i++) {
if (usage.charAt(i) == '<' || usage.charAt(i) == '[') {
count++;
}
}
return count;
}
}