Skip to content

Commit

Permalink
Fixed and tested command system
Browse files Browse the repository at this point in the history
  • Loading branch information
IllusionTheDev committed Sep 11, 2023
1 parent 3387112 commit 997ae0c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,22 @@ public class SimpleSkyblockCommandBuilder<T extends SkyblockAudience> implements
private SkyblockCommandHandler<T> handler;
private String permission;

public SimpleSkyblockCommandBuilder(AbstractSkyblockCommandManager manager, String name, Class<T> audienceClass, List<CommandArgument> arguments) {
public SimpleSkyblockCommandBuilder(AbstractSkyblockCommandManager manager, String name, Class<T> audienceClass, List<CommandArgument> arguments,
String permission) {
this.manager = manager;
this.name = name;
this.audienceClass = audienceClass;
this.arguments = arguments;

arguments.add(new LiteralArgument(name));
this.permission = permission;
}

public SimpleSkyblockCommandBuilder(AbstractSkyblockCommandManager manager, String name, Class<T> audienceClass) {
this(
manager,
name,
audienceClass,
new LinkedList<>()
new LinkedList<>(),
null
);
}

Expand All @@ -64,12 +65,17 @@ public SkyblockCommandBuilder<T> permission(String permission) {

@Override
public <V extends SkyblockAudience> SkyblockCommandBuilder<V> audience(Class<V> audience) {
return new SimpleSkyblockCommandBuilder<>(manager, name, audience, arguments);
return new SimpleSkyblockCommandBuilder<>(manager, name, audience, arguments, permission);
}

@Override
public SkyblockCommand<T> build() {
SkyblockCommand<T> command = new SimpleSkyblockCommand<>(arguments, handler, audienceClass, permission);
List<CommandArgument> realArguments = new LinkedList<>();

realArguments.add(new LiteralArgument(name));
realArguments.addAll(arguments);

SkyblockCommand<T> command = new SimpleSkyblockCommand<>(realArguments, handler, audienceClass, permission);
manager.registerCommand(command);

return command;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import me.illusion.skyblockcore.common.command.node.CommandNode;
import me.illusion.skyblockcore.common.command.structure.CommandTree;
import me.illusion.skyblockcore.common.command.structure.CommandTree.TargetResult;
import me.illusion.skyblockcore.common.config.SkyblockMessagesFile;
import me.illusion.skyblockcore.common.platform.SkyblockPlatform;

/**
Expand Down Expand Up @@ -44,8 +45,10 @@ protected void handle(SkyblockAudience audience, String label, String[] args) {

protected <T extends SkyblockAudience> void handle(SkyblockAudience audience, String input) {
TargetResult result = commandTree.getTargetNode(input);
SkyblockMessagesFile messages = platform.getMessagesFile();

if (result == null) {
messages.sendMessage(audience, "invalid-command");
return;
}

Expand All @@ -64,24 +67,20 @@ protected <T extends SkyblockAudience> void handle(SkyblockAudience audience, St
boolean hasPermission = permission == null || audience.hasPermission(permission);

if (!hasPermission) {
platform.getMessagesFile().sendMessage(audience, "no-permission");
messages.sendMessage(audience, "no-permission");
return;
}

if (!audienceClass.isAssignableFrom(audience.getClass())) {
platform.getMessagesFile().sendMessage(audience, "invalid-audience");
messages.sendMessage(audience, "invalid-audience");
return;
}

command.getHandler().handle((T) audience, context);
}

protected List<String> tabComplete(SkyblockAudience audience, String label, String[] args) {
return tabComplete(audience, createInput(label, args));
}

protected List<String> tabComplete(SkyblockAudience audience, String input) {
return commandTree.tabComplete(audience, input);
return commandTree.tabComplete(audience, label, args);
}

public abstract void registerRoot(String name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,19 @@ public TargetResult getTargetNode(String fullInput) {
return null;
}

if (node.getChildren().isEmpty() && fullInput.equalsIgnoreCase(node.getName())) {
return new TargetResult(node, new MutatingCommandContext(fullInput));
}

CommandNode target = node;
MutatingCommandContext context = new MutatingCommandContext(fullInput);

boolean valid = false;

for (int index = 1; index < split.length; index++) {
String word = split[index];
List<? extends CommandNode> children = target.getChildren();
boolean isLast = index == split.length - 1;

if (children == null) {
return null;
Expand All @@ -70,6 +77,10 @@ public TargetResult getTargetNode(String fullInput) {

if (context.addArgument(word, argument)) {
child = nodeChild;

if (isLast && child.getChildren().isEmpty()) {
valid = true;
}
break;
}
}
Expand All @@ -81,18 +92,23 @@ public TargetResult getTargetNode(String fullInput) {
target = child;
}

if (!valid) {
return null;
}

return new TargetResult(target, context);
}

/**
* Tab completes a command.
* @param audience The audience to tab complete for.
* @param fullInput The full input of the command.
* @param commandName The name of the command.
* @param split The argument array.
* @return The tab completions.
*/
public List<String> tabComplete(SkyblockAudience audience, String fullInput) {
String[] split = fullInput.split(" ");
CommandNode node = getRoot(split[0]);
public List<String> tabComplete(SkyblockAudience audience, String commandName, String[] split) {
CommandNode node = getRoot(commandName);
String fullInput = commandName + " " + String.join(" ", split);

if (node == null) {
return Collections.emptyList();
Expand All @@ -103,7 +119,7 @@ public List<String> tabComplete(SkyblockAudience audience, String fullInput) {

MutatingCommandContext context = new MutatingCommandContext(fullInput);

for (int index = 1; index < split.length; index++) {
for (int index = 0; index < split.length; index++) {
String s = split[index];
List<? extends CommandNode> children = target.getChildren();

Expand All @@ -127,7 +143,9 @@ public List<String> tabComplete(SkyblockAudience audience, String fullInput) {

CommandArgument argument = child.getArgument();

if (!audience.hasPermission(child.getPermission())) {
String permission = child.getPermission();

if (permission != null && !audience.hasPermission(child.getPermission())) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
if (sender instanceof ConsoleCommandSender) {
return tabComplete(new SkyblockBukkitConsoleAudience(), s);
return tabComplete(new SkyblockBukkitConsoleAudience(), s, strings);
}

if (!(sender instanceof Player player)) {
return null; // Not quite supported, should add support for this
}

SkyblockAudience audience = platform.getPlayerManager().getPlayer(player.getUniqueId());
return tabComplete(audience, s);
return tabComplete(audience, s, strings);
}
}
}

0 comments on commit 997ae0c

Please sign in to comment.