memory optimization with command help
This commit is contained in:
@@ -44,5 +44,4 @@ public class BotManager implements Listener {
|
||||
bot.render(connection, true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,28 +8,44 @@ import net.nuggetmc.ai.commands.commands.PlayerAICommand;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CommandHandler {
|
||||
|
||||
private final DrinkCommandService drink;
|
||||
private final Map<Class<? extends CommandInstance>, List<String>> help;
|
||||
|
||||
public CommandHandler(PlayerAI plugin) {
|
||||
drink = (DrinkCommandService) Drink.get(plugin);
|
||||
drink.register(new PlayerAICommand(this), "bot", "playerai", "pai", "ai", "npc");
|
||||
drink.registerCommands();
|
||||
|
||||
help = new HashMap<>();
|
||||
|
||||
setHelps(new Class[] {
|
||||
PlayerAICommand.class
|
||||
});
|
||||
}
|
||||
|
||||
public List<String> getUsage(Class<? extends CommandInstance> clazz) {
|
||||
private void setHelps(Class<? extends CommandInstance>[] cls) {
|
||||
for (Class<? extends CommandInstance> clazz : cls) {
|
||||
help.put(clazz, getUsage(clazz));
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> getHelp(Class<? extends CommandInstance> clazz) {
|
||||
return help.get(clazz);
|
||||
}
|
||||
|
||||
private List<String> getUsage(Class<? extends CommandInstance> clazz) {
|
||||
String rootName = getRootName(clazz);
|
||||
|
||||
return getSubCommands(clazz).stream().map(c -> {
|
||||
Command command = c.getAnnotation(Command.class);
|
||||
return ChatColor.GRAY + " ▪ " + ChatColor.YELLOW + "/" + rootName + " " + command.name() + ChatColor.GRAY + " ▪ "
|
||||
+ ChatColor.RESET + command.desc();
|
||||
}).collect(Collectors.toList());
|
||||
}).sorted().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private String getRootName(Class<? extends CommandInstance> clazz) {
|
||||
|
||||
@@ -5,10 +5,10 @@ import com.jonahseguin.drink.annotation.OptArg;
|
||||
import com.jonahseguin.drink.annotation.Require;
|
||||
import com.jonahseguin.drink.annotation.Sender;
|
||||
import net.nuggetmc.ai.PlayerAI;
|
||||
import net.nuggetmc.ai.commands.CommandHandler;
|
||||
import net.nuggetmc.ai.commands.CommandInstance;
|
||||
import net.nuggetmc.ai.bot.Bot;
|
||||
import net.nuggetmc.ai.bot.BotManager;
|
||||
import net.nuggetmc.ai.commands.CommandHandler;
|
||||
import net.nuggetmc.ai.commands.CommandInstance;
|
||||
import net.nuggetmc.ai.utils.ChatUtils;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -28,8 +28,8 @@ public class PlayerAICommand extends CommandInstance {
|
||||
sender.sendMessage(ChatUtils.LINE);
|
||||
sender.sendMessage(ChatColor.GOLD + "PlayerAI" + ChatColor.GRAY + " [" + ChatColor.RED + "v" + PlayerAI.VERSION + ChatColor.GRAY + "]");
|
||||
|
||||
for (String s : this.getCommandHandler().getUsage(PlayerAICommand.class)) {
|
||||
sender.sendMessage(s);
|
||||
for (String line : getCommandHandler().getHelp(getClass())) {
|
||||
sender.sendMessage(line);
|
||||
}
|
||||
|
||||
sender.sendMessage(ChatUtils.LINE);
|
||||
@@ -65,5 +65,4 @@ public class PlayerAICommand extends CommandInstance {
|
||||
String formatted = NumberFormat.getNumberInstance(Locale.US).format(size);
|
||||
sender.sendMessage("Removed " + ChatColor.RED + formatted + ChatColor.RESET + " entit" + (size == 1 ? "y" : "ies") + ".");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user