mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-12-22 09:08:03 +01:00
Commands API - execution is working. WIP
This commit is contained in:
parent
46910dd851
commit
65999aa550
@ -1,14 +1,24 @@
|
||||
package us.tastybento.bskyblock.api.commands;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class CommandArgument {
|
||||
import java.util.*;
|
||||
|
||||
public abstract class CommandArgument {
|
||||
|
||||
private String label;
|
||||
private List<String> aliases;
|
||||
private Map<String, CommandArgument> subCommands;
|
||||
|
||||
public CommandArgument(String label, String... aliases) {
|
||||
this.label = label;
|
||||
this.aliases = new ArrayList<>(Arrays.asList(aliases));
|
||||
this.subCommands = new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
public abstract boolean execute(CommandSender sender, String[] args);
|
||||
public abstract Set<String> tabComplete(CommandSender sender, String[] args);
|
||||
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
@ -17,12 +27,20 @@ public class CommandArgument {
|
||||
return aliases;
|
||||
}
|
||||
|
||||
public boolean hasSubCommmands() {
|
||||
return !subCommands.isEmpty();
|
||||
}
|
||||
|
||||
public Map<String, CommandArgument> getSubCommands() {
|
||||
return subCommands;
|
||||
}
|
||||
|
||||
public CommandArgument getSubCommand(String label) {
|
||||
return subCommands.getOrDefault(label, null);
|
||||
for (Map.Entry<String, CommandArgument> entry : subCommands.entrySet()) {
|
||||
if (entry.getKey().equalsIgnoreCase(label)) return subCommands.get(label);
|
||||
else if (entry.getValue().getAliases().contains(label)) return subCommands.get(entry.getValue().getLabel());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addSubCommand(CommandArgument subCommand) {
|
||||
|
@ -30,7 +30,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
|
||||
public CompositeCommand(String label, String description, String... aliases) {
|
||||
super(label);
|
||||
this.setDescription(description);
|
||||
this.setAliases(new ArrayList<String>(Arrays.asList(aliases)));
|
||||
this.setAliases(new ArrayList<>(Arrays.asList(aliases)));
|
||||
|
||||
this.subCommands = new LinkedHashMap<>();
|
||||
|
||||
@ -40,13 +40,18 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
|
||||
}
|
||||
|
||||
public abstract void setup();
|
||||
public abstract boolean execute(CommandSender sender, String[] args);
|
||||
|
||||
public Map<String, CommandArgument> getSubCommands() {
|
||||
return subCommands;
|
||||
}
|
||||
|
||||
public CommandArgument getSubCommand(String label) {
|
||||
return subCommands.getOrDefault(label, null);
|
||||
for (Map.Entry<String, CommandArgument> entry : subCommands.entrySet()) {
|
||||
if (entry.getKey().equalsIgnoreCase(label)) return subCommands.get(label);
|
||||
else if (entry.getValue().getAliases().contains(label)) return subCommands.get(entry.getValue().getLabel());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addSubCommand(CommandArgument subCommand) {
|
||||
@ -62,9 +67,34 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(CommandSender commandSender, String s, String[] strings) {
|
||||
commandSender.sendMessage("Hi! I'm working!");
|
||||
return false;
|
||||
public boolean execute(CommandSender sender, String label, String[] args) {
|
||||
if (args.length >= 1) {
|
||||
// Store the latest subCommand found
|
||||
CommandArgument subCommand = null;
|
||||
|
||||
for (int i = 0 ; i < args.length ; i++) {
|
||||
// get the subcommand corresponding to the label
|
||||
if (subCommand == null) subCommand = getSubCommand(args[i]);
|
||||
else subCommand = subCommand.getSubCommand(args[i]);
|
||||
|
||||
if (subCommand != null) { // check if this subcommand exists
|
||||
if (!subCommand.hasSubCommmands()) { // if it has not any subcommands
|
||||
subCommand.execute(sender, args); //TODO: "cut" the args to only send the needed ones
|
||||
}
|
||||
// else continue the loop
|
||||
// TODO: adapt this part to make it works with arguments that are not subcommands
|
||||
}
|
||||
// TODO: get the help
|
||||
else {
|
||||
//TODO: say "unknown command"
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// No args : execute the default behaviour
|
||||
this.execute(sender, args);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user