dynamic help menus

This commit is contained in:
aPunch 2012-02-08 02:26:14 -06:00
parent ba8ee44769
commit efaa462894
8 changed files with 136 additions and 33 deletions

View File

@ -3,5 +3,7 @@ authors: [aPunch, fullwall]
version: 2.0
main: net.citizensnpcs.Citizens
commands:
citizens:
description: Administration commands
npc:
description: Basic commands for all NPC-related things

View File

@ -14,13 +14,13 @@ import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.trait.Character;
import net.citizensnpcs.api.npc.trait.DefaultInstanceFactory;
import net.citizensnpcs.api.npc.trait.InstanceFactory;
import net.citizensnpcs.api.npc.trait.Trait;
import net.citizensnpcs.api.npc.trait.trait.Owner;
import net.citizensnpcs.api.npc.trait.trait.SpawnLocation;
import net.citizensnpcs.api.npc.trait.trait.Spawned;
import net.citizensnpcs.command.CommandManager;
import net.citizensnpcs.command.Injector;
import net.citizensnpcs.command.command.HelpCommands;
import net.citizensnpcs.command.command.NPCCommands;
import net.citizensnpcs.command.exception.CommandUsageException;
import net.citizensnpcs.command.exception.MissingNestedCommandException;
@ -59,9 +59,9 @@ public class Citizens extends JavaPlugin {
LookClose.class, SpawnLocation.class, Inventory.class);
private volatile CitizensNPCManager npcManager;
private final InstanceFactory<Character> characterManager = new DefaultInstanceFactory<Character>();
private final InstanceFactory<Trait> traitManager = new DefaultInstanceFactory<Trait>();
private CommandManager cmdManager;
private final DefaultInstanceFactory<Character> characterManager = new DefaultInstanceFactory<Character>();
private final DefaultInstanceFactory<Trait> traitManager = new DefaultInstanceFactory<Trait>();
private final CommandManager cmdManager = new CommandManager();
private Settings config;
private Storage saves;
private boolean compatible;
@ -213,11 +213,24 @@ public class Citizens extends JavaPlugin {
}
}
private void registerCommands() {
cmdManager = new CommandManager();
cmdManager.setInjector(new Injector(npcManager, characterManager));
public CitizensNPCManager getNPCManager() {
return npcManager;
}
public DefaultInstanceFactory<Character> getCharacterManager() {
return characterManager;
}
public CommandManager getCommandManager() {
return cmdManager;
}
private void registerCommands() {
cmdManager.setInjector(new Injector(this));
// Register command classes
cmdManager.register(NPCCommands.class);
cmdManager.register(HelpCommands.class);
}
private void registerPermissions() {

View File

@ -16,8 +16,7 @@ public class NPCUpdater implements Runnable {
for (NPC npc : npcManager) {
if (!npc.isSpawned())
continue;
CitizensNPC handle = (CitizensNPC) npc;
handle.update();
((CitizensNPC) npc).update();
}
}
}

View File

@ -22,6 +22,7 @@ package net.citizensnpcs.command;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
@ -51,8 +52,6 @@ import net.citizensnpcs.util.Messaging;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import com.google.common.collect.Lists;
public class CommandManager {
/*
@ -264,7 +263,7 @@ public class CommandManager {
public List<Command> getCommands(String command) {
if (subCommands.containsKey(command))
return subCommands.get(command);
List<Command> cmds = Lists.newArrayList();
List<Command> cmds = new ArrayList<Command>();
for (Entry<CommandIdentifier, Method> entry : commands.get(null).entrySet()) {
if (!entry.getKey().getCommand().equalsIgnoreCase(command)
|| !entry.getValue().isAnnotationPresent(Command.class))
@ -362,16 +361,6 @@ public class CommandManager {
instances.put(method, obj);
}
/*
* // Build a list of commands and their usage details, at least for
* // root level commands if (parent == null) if
* (cmd.usage().length() == 0) descs.put(new
* CommandIdentifier(cmd.aliases()[0], cmd.modifiers()[0]),
* cmd.desc()); else descs.put(new
* CommandIdentifier(cmd.aliases()[0], cmd.modifiers()[0]),
* cmd.usage() + " - " + cmd.desc());
*/
// Look for nested commands -- if there are any, those have
// to be cached too so that they can be quickly looked
// up when processing commands

View File

@ -1,15 +1,9 @@
package net.citizensnpcs.command.command;
import net.citizensnpcs.api.npc.trait.Character;
import net.citizensnpcs.api.npc.trait.DefaultInstanceFactory;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.Citizens;
public class AdminCommands {
private final CitizensNPCManager npcManager;
private final DefaultInstanceFactory<Character> characterManager;
public AdminCommands(CitizensNPCManager npcManager, DefaultInstanceFactory<Character> characterManager) {
this.npcManager = npcManager;
this.characterManager = characterManager;
public AdminCommands(Citizens plugin) {
}
}

View File

@ -0,0 +1,95 @@
package net.citizensnpcs.command.command;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.bukkit.entity.Player;
import net.citizensnpcs.Citizens;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.command.CommandContext;
import net.citizensnpcs.command.CommandManager;
import net.citizensnpcs.command.annotation.Command;
import net.citizensnpcs.command.annotation.Requirements;
import net.citizensnpcs.util.Messaging;
import net.citizensnpcs.util.StringHelper;
@Requirements
public class HelpCommands {
private static final int LINES_PER_PAGE = 9;
private final CommandManager cmdManager;
public HelpCommands(Citizens plugin) {
cmdManager = plugin.getCommandManager();
}
@Command(
aliases = { "citizens" },
usage = "help (page)",
desc = "Citizens help menu",
modifiers = { "help" },
min = 1,
max = 2,
permission = "citizens.help")
@Requirements
public void citizensHelp(CommandContext args, Player player, NPC npc) {
int page = args.argsLength() == 2 ? args.getInteger(1) : 1;
if (!sendPage(player, args.getCommand(), page))
Messaging.sendError(player, "The page '" + page + "' does not exist.");
}
@Command(
aliases = { "npc" },
usage = "help (page)",
desc = "NPC help menu",
modifiers = { "help" },
min = 1,
max = 2,
permission = "npc.help")
@Requirements
public void npcHelp(CommandContext args, Player player, NPC npc) {
int page = args.argsLength() == 2 ? args.getInteger(1) : 1;
if (!sendPage(player, args.getCommand(), page))
Messaging.sendError(player, "The page '" + page + "' does not exist.");
}
private boolean sendPage(Player player, String baseCommand, int page) {
List<String> lines = getLines(player, baseCommand);
int pages = (lines.size() / LINES_PER_PAGE == 0) ? 1 : lines.size() / LINES_PER_PAGE;
if (page < 0 || page > pages)
return false;
int startIndex = LINES_PER_PAGE * page - LINES_PER_PAGE;
int endIndex = page * LINES_PER_PAGE;
Messaging.send(
player,
StringHelper.parseColors("<a>=====[ <e>"
+ (baseCommand.equalsIgnoreCase("npc") ? "NPC" : StringHelper.capitalize(baseCommand
.toLowerCase())) + " Help <f>" + page + "/" + pages + " <a>]====="));
if (lines.size() < endIndex)
endIndex = lines.size() - 1;
for (String line : lines.subList(startIndex, endIndex))
Messaging.send(player, line);
return true;
}
private List<String> getLines(Player player, String baseCommand) {
// Ensures that commands with multiple modifiers are only added once
Set<Command> cmds = new HashSet<Command>();
List<String> lines = new ArrayList<String>();
for (Command cmd : cmdManager.getCommands(baseCommand)) {
if (cmds.contains(cmd) || !player.hasPermission("citizens." + cmd.permission()))
continue;
lines.add(StringHelper.parseColors("<7>/<c>" + cmd.aliases()[0] + " " + cmd.usage() + " <7>- <e>"
+ cmd.desc()));
if (cmd.modifiers().length > 1)
cmds.add(cmd);
}
return lines;
}
}

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.command.command;
import net.citizensnpcs.Citizens;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.trait.Character;
@ -25,9 +26,9 @@ public class NPCCommands {
private final CitizensNPCManager npcManager;
private final DefaultInstanceFactory<Character> characterManager;
public NPCCommands(CitizensNPCManager npcManager, DefaultInstanceFactory<Character> characterManager) {
this.npcManager = npcManager;
this.characterManager = characterManager;
public NPCCommands(Citizens plugin) {
npcManager = plugin.getNPCManager();
characterManager = plugin.getCharacterManager();
}
@Command(

View File

@ -57,10 +57,20 @@ public class StringHelper {
return ChatColor.YELLOW + string.toString() + ChatColor.GREEN;
}
public static String wrap(Object string, ChatColor after) {
return ChatColor.YELLOW + string.toString() + after;
}
public static String parseColors(Object string) {
String parsed = string.toString();
for (ChatColor color : ChatColor.values())
parsed = parsed.replace("<" + color.getChar() + ">", color.toString());
return parsed;
}
public static String capitalize(Object string) {
String capitalize = string.toString();
return capitalize.replaceFirst(String.valueOf(capitalize.charAt(0)),
String.valueOf(Character.toUpperCase(capitalize.charAt(0))));
}
}