Minor cleanup

This commit is contained in:
fullwall 2012-03-21 17:40:40 +08:00
parent 4b83f8d956
commit 785f353bae
6 changed files with 173 additions and 51 deletions

View File

@ -160,7 +160,13 @@ public class CommandContext {
}
public int getInteger(int index, int def) throws NumberFormatException {
return index + 1 < args.length ? Integer.parseInt(args[index + 1]) : def;
if (index + 1 < args.length) {
try {
return Integer.parseInt(args[index + 1]);
} catch (NumberFormatException ex) {
}
}
return def;
}
public String getJoinedStrings(int initialIndex) {

View File

@ -5,8 +5,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.bukkit.command.CommandSender;
import net.citizensnpcs.Citizens;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.command.Command;
@ -17,6 +15,8 @@ import net.citizensnpcs.command.ServerCommand;
import net.citizensnpcs.command.exception.CommandException;
import net.citizensnpcs.util.Paginator;
import org.bukkit.command.CommandSender;
@Requirements
public class HelpCommands {
private final CommandManager cmdManager;
@ -25,28 +25,40 @@ public class HelpCommands {
cmdManager = plugin.getCommandManager();
}
@Command(aliases = { "citizens" }, usage = "help (page)", desc = "Citizens help menu", modifiers = { "help" }, min = 1, max = 2, permission = "help")
@Command(
aliases = { "citizens" },
usage = "help (page)",
desc = "Citizens help menu",
modifiers = { "help" },
min = 1,
max = 2,
permission = "help")
@Requirements
@ServerCommand
public void citizensHelp(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
int page = args.argsLength() == 2 ? args.getInteger(1) : 1;
Paginator paginator = new Paginator();
Paginator paginator = new Paginator().header("Citizens Help");
for (String line : getLines(sender, "citizens"))
paginator.addLine(line);
paginator.setHeaderText("Citizens Help");
if (!paginator.sendPage(sender, page))
throw new CommandException("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")
@Command(
aliases = { "npc" },
usage = "help (page)",
desc = "NPC help menu",
modifiers = { "help" },
min = 1,
max = 2,
permission = "npc.help")
@Requirements
@ServerCommand
public void npcHelp(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
int page = args.argsLength() == 2 ? args.getInteger(1) : 1;
Paginator paginator = new Paginator();
Paginator paginator = new Paginator().header("NPC Help");
for (String line : getLines(sender, "npc"))
paginator.addLine(line);
paginator.setHeaderText("NPC Help");
if (!paginator.sendPage(sender, page))
throw new CommandException("The page '" + page + "' does not exist.");
}

View File

@ -47,7 +47,13 @@ public class NPCCommands {
npcManager = plugin.getNPCManager();
}
@Command(aliases = { "npc" }, usage = "character [character]", desc = "Set the character of a NPC", modifiers = { "character" }, min = 2, max = 2)
@Command(
aliases = { "npc" },
usage = "character [character]",
desc = "Set the character of a NPC",
modifiers = { "character" },
min = 2,
max = 2)
public void character(CommandContext args, Player player, NPC npc) throws CommandException {
String name = args.getString(1).toLowerCase();
Character character = characterManager.getCharacter(name);
@ -70,7 +76,15 @@ public class NPCCommands {
npc.setCharacter(character);
}
@Command(aliases = { "npc" }, usage = "create [name] (--type (type) --char (char))", desc = "Create a new NPC", flags = "b", modifiers = { "create" }, min = 2, max = 5, permission = "npc.create")
@Command(
aliases = { "npc" },
usage = "create [name] (--type (type) --char (char))",
desc = "Create a new NPC",
flags = "b",
modifiers = { "create" },
min = 2,
max = 5,
permission = "npc.create")
@Requirements
public void create(CommandContext args, Player player, NPC npc) {
String name = args.getString(1);
@ -139,60 +153,77 @@ public class NPCCommands {
Messaging.send(player, msg);
}
@Command(aliases = { "npc" }, usage = "despawn", desc = "Despawn a NPC", modifiers = { "despawn" }, min = 1, max = 1, permission = "npc.despawn")
@Command(
aliases = { "npc" },
usage = "despawn",
desc = "Despawn a NPC",
modifiers = { "despawn" },
min = 1,
max = 1,
permission = "npc.despawn")
public void despawn(CommandContext args, Player player, NPC npc) {
npc.getTrait(Spawned.class).setSpawned(false);
npc.despawn();
Messaging.send(player, ChatColor.GREEN + "You despawned " + StringHelper.wrap(npc.getName()) + ".");
}
@Command(aliases = { "npc" }, usage = "list (page) ((-a) --owner (owner) --type (type) --char (char))", desc = "List NPCs", flags = "a", modifiers = { "list" }, min = 1, max = 2, permission = "npc.list")
@Command(
aliases = { "npc" },
usage = "list (page) ((-a) --owner (owner) --type (type) --char (char))",
desc = "List NPCs",
flags = "a",
modifiers = { "list" },
min = 1,
max = 2,
permission = "npc.list")
@Requirements
public void list(CommandContext args, Player player, NPC npc) throws CommandException {
List<NPC> npcs = new ArrayList<NPC>();
if (args.hasFlag('a')) {
for (NPC add : npcManager)
for (NPC add : npcManager) {
npcs.add(add);
}
} else if (args.getValueFlags().size() == 0 && args.argsLength() == 1 || args.argsLength() == 2) {
for (NPC add : npcManager)
if (add.getTrait(Owner.class).isOwner(player))
for (NPC add : npcManager) {
if (!npcs.contains(add) && add.getTrait(Owner.class).isOwner(player))
npcs.add(add);
}
} else {
if (args.hasValueFlag("owner")) {
String name = args.getFlag("owner");
for (NPC add : npcManager)
if (add.getTrait(Owner.class).getOwner().equals(name))
for (NPC add : npcManager) {
if (!npcs.contains(add) && add.getTrait(Owner.class).getOwner().equals(name))
npcs.add(add);
}
}
if (args.hasValueFlag("type")) {
String type = args.getFlag("type");
try {
EntityType.valueOf(type.toUpperCase().replace('-', '_'));
} catch (IllegalArgumentException ex) {
if (EntityType.fromName(type.replace('-', '_')) == null) {
throw new CommandException("'" + type + "' is not a valid mob type.");
}
for (NPC add : npcManager)
for (NPC add : npcManager) {
if (!npcs.contains(add) && add.getTrait(MobType.class).getType().equalsIgnoreCase(type))
npcs.add(add);
}
}
if (args.hasValueFlag("char")) {
String character = args.getFlag("char");
if (characterManager.getCharacter(character) == null)
throw new CommandException("'" + character + "' is not a valid character.");
for (NPC add : npcManager.getNPCs(characterManager.getCharacter(character).getClass()))
if (!npcs.contains(add) && add.getCharacter() != null
&& add.getCharacter().getName().equals(character.toLowerCase()))
for (NPC add : npcManager.getNPCs(characterManager.getCharacter(character).getClass())) {
if (!npcs.contains(add))
npcs.add(add);
}
}
}
Paginator paginator = new Paginator();
paginator.setHeaderText("NPCs");
Paginator paginator = new Paginator().header("NPCs");
paginator.addLine("<e>Key: <a>ID <b>Name");
for (int i = 0; i < npcs.size(); i += 2) {// 0,2,4,6,etc, size=3
String line = "<a>" + npcs.get(i).getId() + "<b> " + npcs.get(i).getName();
@ -201,21 +232,23 @@ public class NPCCommands {
paginator.addLine(line);
}
int page = 1;
try {
page = args.getInteger(1);
} catch (Exception ex) {
}
int page = args.getInteger(1, 1);
if (!paginator.sendPage(player, page))
throw new CommandException("The page '" + page + "' does not exist.");
}
@Command(aliases = { "npc" }, usage = "lookclose", desc = "Toggle whether a NPC will look when a player is near", modifiers = {
"lookclose", "look", "rotate" }, min = 1, max = 1, permission = "npc.lookclose")
@Command(
aliases = { "npc" },
usage = "lookclose",
desc = "Toggle whether a NPC will look when a player is near",
modifiers = { "lookclose", "look", "rotate" },
min = 1,
max = 1,
permission = "npc.lookclose")
public void lookClose(CommandContext args, Player player, NPC npc) {
String msg = StringHelper.wrap(npc.getName()) + " will "
+ (npc.getTrait(LookClose.class).toggle() ? "now rotate" : "no longer rotate");
Messaging.send(player, msg += " when a player is nearby.");
Messaging.send(player, msg + " when a player is nearby.");
}
@Command(aliases = { "npc" }, desc = "Show basic NPC information", max = 0)
@ -227,7 +260,14 @@ public class NPCCommands {
Messaging.send(player, " <a>Type: <e>" + npc.getTrait(MobType.class).getType());
}
@Command(aliases = { "npc" }, usage = "owner [name]", desc = "Set the owner of an NPC", modifiers = { "owner" }, min = 1, max = 2, permission = "npc.owner")
@Command(
aliases = { "npc" },
usage = "owner [name]",
desc = "Set the owner of an NPC",
modifiers = { "owner" },
min = 1,
max = 2,
permission = "npc.owner")
public void owner(CommandContext args, Player player, NPC npc) throws CommandException {
if (args.argsLength() == 1) {
Messaging.send(player, StringHelper.wrap(npc.getName() + "'s Owner: ")
@ -242,7 +282,13 @@ public class NPCCommands {
+ " is now the owner of " + StringHelper.wrap(npc.getName()) + ".");
}
@Command(aliases = { "npc" }, usage = "remove (all)", desc = "Remove a NPC", modifiers = { "remove" }, min = 1, max = 2)
@Command(
aliases = { "npc" },
usage = "remove (all)",
desc = "Remove a NPC",
modifiers = { "remove" },
min = 1,
max = 2)
@Requirements
public void remove(CommandContext args, Player player, NPC npc) throws CommandException {
if (args.argsLength() == 2) {
@ -264,7 +310,14 @@ public class NPCCommands {
Messaging.send(player, "<a>You permanently removed " + StringHelper.wrap(npc.getName()) + ".");
}
@Command(aliases = { "npc" }, usage = "rename [name]", desc = "Rename a NPC", modifiers = { "rename" }, min = 2, max = 2, permission = "npc.rename")
@Command(
aliases = { "npc" },
usage = "rename [name]",
desc = "Rename a NPC",
modifiers = { "rename" },
min = 2,
max = 2,
permission = "npc.rename")
public void rename(CommandContext args, Player player, NPC npc) {
String oldName = npc.getName();
String newName = args.getString(1);
@ -278,7 +331,14 @@ public class NPCCommands {
+ ".");
}
@Command(aliases = { "npc" }, usage = "select [id]", desc = "Select a NPC with the given ID", modifiers = { "select" }, min = 2, max = 2, permission = "npc.select")
@Command(
aliases = { "npc" },
usage = "select [id]",
desc = "Select a NPC with the given ID",
modifiers = { "select" },
min = 2,
max = 2,
permission = "npc.select")
@Requirements(ownership = true)
public void select(CommandContext args, Player player, NPC npc) throws CommandException {
NPC toSelect = npcManager.getNPC(args.getInteger(1));
@ -290,7 +350,14 @@ public class NPCCommands {
Messaging.sendWithNPC(player, Setting.SELECTION_MESSAGE.asString(), toSelect);
}
@Command(aliases = { "npc" }, usage = "spawn [id]", desc = "Spawn an existing NPC", modifiers = { "spawn" }, min = 2, max = 2, permission = "npc.spawn")
@Command(
aliases = { "npc" },
usage = "spawn [id]",
desc = "Spawn an existing NPC",
modifiers = { "spawn" },
min = 2,
max = 2,
permission = "npc.spawn")
@Requirements
public void spawn(CommandContext args, Player player, NPC npc) throws CommandException {
NPC respawn = npcManager.getNPC(args.getInteger(1));
@ -309,7 +376,14 @@ public class NPCCommands {
+ " Use '/npc tphere' to teleport the NPC to your location.");
}
@Command(aliases = { "npc" }, usage = "tp", desc = "Teleport to a NPC", modifiers = { "tp", "teleport" }, min = 1, max = 1, permission = "npc.tp")
@Command(
aliases = { "npc" },
usage = "tp",
desc = "Teleport to a NPC",
modifiers = { "tp", "teleport" },
min = 1,
max = 1,
permission = "npc.tp")
public void tp(CommandContext args, Player player, NPC npc) {
// Spawn the NPC if it isn't spawned to prevent NPEs
if (!npc.isSpawned())
@ -318,7 +392,14 @@ public class NPCCommands {
Messaging.send(player, ChatColor.GREEN + "You teleported to " + StringHelper.wrap(npc.getName()) + ".");
}
@Command(aliases = { "npc" }, usage = "tphere", desc = "Teleport a NPC to your location", modifiers = { "tphere" }, min = 1, max = 1, permission = "npc.tphere")
@Command(
aliases = { "npc" },
usage = "tphere",
desc = "Teleport a NPC to your location",
modifiers = { "tphere" },
min = 1,
max = 1,
permission = "npc.tphere")
public void tphere(CommandContext args, Player player, NPC npc) {
// Spawn the NPC if it isn't spawned to prevent NPEs
if (!npc.isSpawned())
@ -327,7 +408,14 @@ public class NPCCommands {
Messaging.send(player, StringHelper.wrap(npc.getName()) + " was teleported to your location.");
}
@Command(aliases = { "npc" }, usage = "power", desc = "Toggle a creeper NPC as powered", modifiers = { "power" }, min = 1, max = 1, permission = "npc.power")
@Command(
aliases = { "npc" },
usage = "power",
desc = "Toggle a creeper NPC as powered",
modifiers = { "power" },
min = 1,
max = 1,
permission = "npc.power")
@Requirements(selected = true, ownership = true, types = { EntityType.CREEPER })
public void power(CommandContext args, Player player, NPC npc) {
String msg = StringHelper.wrap(npc.getName()) + " will "
@ -335,7 +423,14 @@ public class NPCCommands {
Messaging.send(player, msg += " be powered.");
}
@Command(aliases = { "npc" }, usage = "profession [profession]", desc = "Set a NPC's profession", modifiers = { "profession" }, min = 2, max = 2, permission = "npc.profession")
@Command(
aliases = { "npc" },
usage = "profession [profession]",
desc = "Set a NPC's profession",
modifiers = { "profession" },
min = 2,
max = 2,
permission = "npc.profession")
@Requirements(selected = true, ownership = true, types = { EntityType.VILLAGER })
public void profession(CommandContext args, Player player, NPC npc) throws CommandException {
String profession = args.getString(1);
@ -350,7 +445,15 @@ public class NPCCommands {
}
}
@Command(aliases = { "npc" }, usage = "age [age] (-l)", desc = "Set the age of a NPC", flags = "l", modifiers = { "age" }, min = 1, max = 2, permission = "npc.age")
@Command(
aliases = { "npc" },
usage = "age [age] (-l)",
desc = "Set the age of a NPC",
flags = "l",
modifiers = { "age" },
min = 1,
max = 2,
permission = "npc.age")
@Requirements(selected = true, ownership = true, types = { EntityType.CHICKEN, EntityType.COW, EntityType.OCELOT,
EntityType.PIG, EntityType.SHEEP, EntityType.VILLAGER, EntityType.WOLF })
public void age(CommandContext args, Player player, NPC npc) throws CommandException {

View File

@ -87,9 +87,10 @@ public class CitizensNPCManager implements NPCManager {
@Override
public Collection<NPC> getNPCs(Class<? extends Character> character) {
List<NPC> npcs = new ArrayList<NPC>();
for (NPC npc : this)
for (NPC npc : this) {
if (npc.getCharacter() != null && npc.getCharacter().getClass().equals(character))
npcs.add(npc);
}
return npcs;
}

View File

@ -113,8 +113,8 @@ public class Text extends Trait implements Runnable, Toggleable, ConversationAba
public Editor getEditor(final Player player) {
final Conversation conversation = new ConversationFactory(plugin).addConversationAbandonedListener(this)
.withLocalEcho(false).withEscapeSequence("/npc text").withModality(false).withFirstPrompt(
new StartPrompt(this)).buildConversation(player);
.withLocalEcho(false).withEscapeSequence("/npc text").withModality(false)
.withFirstPrompt(new StartPrompt(this)).buildConversation(player);
return new Editor() {
@Override
@ -148,8 +148,7 @@ public class Text extends Trait implements Runnable, Toggleable, ConversationAba
}
public boolean sendPage(Player player, int page) {
Paginator paginator = new Paginator();
paginator.setHeaderText(npc.getName() + "'s Text Entries");
Paginator paginator = new Paginator().header(npc.getName() + "'s Text Entries");
for (int i = 0; i < text.size(); i++)
paginator.addLine("<a>" + i + " <7>- <e>" + text.get(i));

View File

@ -32,7 +32,8 @@ public class Paginator {
return true;
}
public void setHeaderText(String header) {
public Paginator header(String header) {
this.header = header;
return this;
}
}