command adjustments
This commit is contained in:
@@ -4,11 +4,12 @@ import com.jonahseguin.drink.Drink;
|
||||
import com.jonahseguin.drink.annotation.Command;
|
||||
import com.jonahseguin.drink.command.DrinkCommandService;
|
||||
import com.jonahseguin.drink.utils.ChatUtils;
|
||||
import net.nuggetmc.ai.TerminatorPlus;
|
||||
import net.nuggetmc.ai.command.commands.AICommand;
|
||||
import net.nuggetmc.ai.command.commands.BotCommand;
|
||||
import net.nuggetmc.ai.command.commands.MainCommand;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
@@ -21,12 +22,15 @@ import java.util.stream.Collectors;
|
||||
|
||||
public class CommandHandler {
|
||||
|
||||
private final TerminatorPlus plugin;
|
||||
|
||||
private static final String MANAGE_PERMISSION = "terminatorplus.manage";
|
||||
|
||||
private final DrinkCommandService drink;
|
||||
private final Map<Class<? extends CommandInstance>, List<String>> help;
|
||||
|
||||
public CommandHandler(JavaPlugin plugin) {
|
||||
public CommandHandler(TerminatorPlus plugin) {
|
||||
this.plugin = plugin;
|
||||
this.drink = (DrinkCommandService) Drink.get(plugin);
|
||||
this.help = new HashMap<>();
|
||||
this.registerCommands();
|
||||
@@ -34,32 +38,35 @@ public class CommandHandler {
|
||||
}
|
||||
|
||||
private void registerCommands() {
|
||||
registerCommand(new MainCommand(this, drink), "terminatorplus", "terminator");
|
||||
registerCommand(new MainCommand(this, drink), "terminatorplus");
|
||||
registerCommand(new BotCommand(this), "bot", "npc");
|
||||
registerCommand(new AICommand(this), "ai");
|
||||
}
|
||||
|
||||
private void registerCommand(@Nonnull CommandInstance handler, @Nonnull String name, @Nullable String... aliases) {
|
||||
handler.setName(name);
|
||||
drink.register(handler, MANAGE_PERMISSION, name, aliases);
|
||||
setHelp(handler.getClass());
|
||||
}
|
||||
|
||||
public void sendRootInfo(CommandInstance commandInstance, CommandSender sender) {
|
||||
sender.sendMessage(ChatUtils.LINE);
|
||||
sender.sendMessage(ChatColor.GOLD + plugin.getName() + ChatUtils.BULLET_FORMATTED + ChatColor.GRAY
|
||||
+ "[" + ChatColor.YELLOW + "/" + commandInstance.getName() + ChatColor.GRAY + "]");
|
||||
help.get(commandInstance.getClass()).forEach(sender::sendMessage);
|
||||
sender.sendMessage(ChatUtils.LINE);
|
||||
}
|
||||
|
||||
private void setHelp(Class<? extends CommandInstance> cls) {
|
||||
help.put(cls, getUsage(cls));
|
||||
}
|
||||
|
||||
public List<String> getHelp(Class<? extends CommandInstance> cls) {
|
||||
return help.get(cls);
|
||||
}
|
||||
|
||||
private List<String> getUsage(Class<? extends CommandInstance> cls) {
|
||||
String rootName = getRootName(cls);
|
||||
|
||||
return getSubCommands(cls).stream().map(c -> {
|
||||
Command command = c.getAnnotation(Command.class);
|
||||
return ChatUtils.BULLET_FORMATTED + ChatColor.YELLOW + "/" + rootName + " " + command.name() + ChatUtils.BULLET_FORMATTED
|
||||
+ ChatColor.RESET + command.desc();
|
||||
}).sorted().collect(Collectors.toList());
|
||||
return getSubCommands(cls).stream().map(c -> c.getAnnotation(Command.class)).filter(Command::visible).map(c ->
|
||||
ChatUtils.BULLET_FORMATTED + ChatColor.YELLOW + "/" + rootName + " " + c.name() + ChatUtils.BULLET_FORMATTED
|
||||
+ ChatColor.RESET + c.desc()).sorted().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private String getRootName(Class<? extends CommandInstance> cls) {
|
||||
|
||||
@@ -2,12 +2,22 @@ package net.nuggetmc.ai.command;
|
||||
|
||||
public abstract class CommandInstance {
|
||||
|
||||
private String name;
|
||||
|
||||
protected final CommandHandler commandHandler;
|
||||
|
||||
public CommandInstance(CommandHandler commandHandler) {
|
||||
this.commandHandler = commandHandler;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public CommandHandler getCommandHandler() {
|
||||
return commandHandler;
|
||||
}
|
||||
|
||||
@@ -3,11 +3,11 @@ package net.nuggetmc.ai.command.commands;
|
||||
import com.jonahseguin.drink.annotation.Command;
|
||||
import com.jonahseguin.drink.annotation.OptArg;
|
||||
import com.jonahseguin.drink.annotation.Sender;
|
||||
import com.jonahseguin.drink.utils.ChatUtils;
|
||||
import net.nuggetmc.ai.TerminatorPlus;
|
||||
import net.nuggetmc.ai.bot.BotManager;
|
||||
import net.nuggetmc.ai.command.CommandHandler;
|
||||
import net.nuggetmc.ai.command.CommandInstance;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class AICommand extends CommandInstance {
|
||||
@@ -23,10 +23,8 @@ public class AICommand extends CommandInstance {
|
||||
@Command(
|
||||
desc = "The root command for bot AI training."
|
||||
)
|
||||
public void root(@Sender Player sender) {
|
||||
sender.sendMessage(ChatUtils.LINE);
|
||||
commandHandler.getHelp(getClass()).forEach(sender::sendMessage);
|
||||
sender.sendMessage(ChatUtils.LINE);
|
||||
public void root(@Sender CommandSender sender) {
|
||||
commandHandler.sendRootInfo(this, sender);
|
||||
}
|
||||
|
||||
@Command(
|
||||
|
||||
@@ -11,6 +11,7 @@ import net.nuggetmc.ai.bot.BotManager;
|
||||
import net.nuggetmc.ai.command.CommandHandler;
|
||||
import net.nuggetmc.ai.command.CommandInstance;
|
||||
import net.nuggetmc.ai.utils.Debugger;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
@@ -21,6 +22,8 @@ import org.bukkit.util.Vector;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class BotCommand extends CommandInstance {
|
||||
@@ -42,10 +45,8 @@ public class BotCommand extends CommandInstance {
|
||||
@Command(
|
||||
desc = "The root command for bot management."
|
||||
)
|
||||
public void root(@Sender Player sender) {
|
||||
sender.sendMessage(ChatUtils.LINE);
|
||||
commandHandler.getHelp(getClass()).forEach(sender::sendMessage);
|
||||
sender.sendMessage(ChatUtils.LINE);
|
||||
public void root(@Sender CommandSender sender) {
|
||||
commandHandler.sendRootInfo(this, sender);
|
||||
}
|
||||
|
||||
@Command(
|
||||
@@ -66,15 +67,6 @@ public class BotCommand extends CommandInstance {
|
||||
manager.createBots(sender, name, skin, n);
|
||||
}
|
||||
|
||||
@Command(
|
||||
name = "debug",
|
||||
desc = "Debug plugin code.",
|
||||
usage = "<expression>"
|
||||
)
|
||||
public void debug(@Sender CommandSender sender, @Text String cmd) {
|
||||
new Debugger(sender).execute(cmd);
|
||||
}
|
||||
|
||||
@Command(
|
||||
name = "info",
|
||||
desc = "Information about loaded bots.",
|
||||
@@ -142,4 +134,40 @@ public class BotCommand extends CommandInstance {
|
||||
String formatted = NumberFormat.getNumberInstance(Locale.US).format(size);
|
||||
sender.sendMessage("Removed " + ChatColor.RED + formatted + ChatColor.RESET + " entit" + (size == 1 ? "y" : "ies") + ".");
|
||||
}
|
||||
|
||||
@Command(
|
||||
name = "options",
|
||||
desc = "Make changes to the global configuration file and bot-specific settings.",
|
||||
aliases = "settings",
|
||||
autofill = "optionsAutofill"
|
||||
)
|
||||
public void options(@Sender CommandSender sender) {
|
||||
sender.sendMessage(ChatColor.YELLOW + "This feature is coming soon!");
|
||||
}
|
||||
|
||||
public List<String> optionsAutofill(CommandSender sender, String[] args) {
|
||||
List<String> output = new ArrayList<>();
|
||||
|
||||
if (args.length == 2) {
|
||||
output.add("setgoal");
|
||||
output.add("setitem");
|
||||
output.add("tpall");
|
||||
output.add("tprandom");
|
||||
output.add("hidenametags");
|
||||
output.add("sitall");
|
||||
output.add("lookall");
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
@Command(
|
||||
name = "debug",
|
||||
desc = "Debug plugin code.",
|
||||
usage = "<expression>",
|
||||
visible = false
|
||||
)
|
||||
public void debug(@Sender CommandSender sender, @Text String cmd) {
|
||||
new Debugger(sender).execute(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import net.nuggetmc.ai.command.CommandHandler;
|
||||
import net.nuggetmc.ai.command.CommandInstance;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class MainCommand extends CommandInstance {
|
||||
|
||||
@@ -33,7 +33,11 @@ public class MainCommand extends CommandInstance {
|
||||
@Command(
|
||||
desc = "The TerminatorPlus main command."
|
||||
)
|
||||
public void root(@Sender Player sender) {
|
||||
public void root(@Sender CommandSender sender) {
|
||||
if (rootInfo == null) {
|
||||
rootInfoSetup();
|
||||
}
|
||||
|
||||
sender.spigot().sendMessage(rootInfo);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user