Fix for CITIZENS-31

This commit is contained in:
fullwall 2012-03-15 19:38:10 +08:00
parent 2614b07208
commit 7b624765bf
3 changed files with 107 additions and 105 deletions

View File

@ -43,12 +43,12 @@ public class NPCCommands {
} }
@Command( @Command(
aliases = { "npc" }, aliases = { "npc" },
usage = "character [character]", usage = "character [character]",
desc = "Set the character of a NPC", desc = "Set the character of a NPC",
modifiers = { "character" }, modifiers = { "character" },
min = 2, min = 2,
max = 2) max = 2)
public void character(CommandContext args, Player player, NPC npc) throws CommandException { public void character(CommandContext args, Player player, NPC npc) throws CommandException {
String name = args.getString(1).toLowerCase(); String name = args.getString(1).toLowerCase();
Character character = characterManager.getCharacter(name); Character character = characterManager.getCharacter(name);
@ -66,19 +66,19 @@ public class NPCCommands {
Messaging.sendError(player, "This NPC cannot be given the character '" + character.getName() + "'."); Messaging.sendError(player, "This NPC cannot be given the character '" + character.getName() + "'.");
return; return;
} }
Messaging.send(player, StringHelper.wrap(npc.getName() + "'s") + " character is now '" Messaging.send(player,
+ StringHelper.wrap(name) + "'."); StringHelper.wrap(npc.getName() + "'s") + " character is now '" + StringHelper.wrap(name) + "'.");
npc.setCharacter(character); npc.setCharacter(character);
} }
@Command( @Command(
aliases = { "npc" }, aliases = { "npc" },
usage = "create [name] (--type (type) --char (char))", usage = "create [name] (--type (type) --char (char))",
desc = "Create a new NPC", desc = "Create a new NPC",
modifiers = { "create" }, modifiers = { "create" },
min = 2, min = 2,
max = 5, max = 5,
permission = "npc.create") permission = "npc.create")
@Requirements @Requirements
public void create(CommandContext args, Player player, NPC npc) { public void create(CommandContext args, Player player, NPC npc) {
String name = args.getString(1); String name = args.getString(1);
@ -94,8 +94,8 @@ public class NPCCommands {
Messaging.sendError(player, "'" + args.getFlag("type") Messaging.sendError(player, "'" + args.getFlag("type")
+ "' is not a valid mob type. Using default NPC."); + "' is not a valid mob type. Using default NPC.");
} }
NPC create = npcManager.createNPC(type, name); npc = npcManager.createNPC(type, name);
String msg = ChatColor.GREEN + "You created " + StringHelper.wrap(create.getName()); String msg = ChatColor.GREEN + "You created " + StringHelper.wrap(npc.getName());
if (args.hasValueFlag("char")) { if (args.hasValueFlag("char")) {
String character = args.getFlag("char").toLowerCase(); String character = args.getFlag("char").toLowerCase();
if (characterManager.getCharacter(character) == null) { if (characterManager.getCharacter(character) == null) {
@ -106,34 +106,34 @@ public class NPCCommands {
if (!set.getValidTypes().isEmpty() && !set.getValidTypes().contains(type)) { if (!set.getValidTypes().isEmpty() && !set.getValidTypes().contains(type)) {
Messaging.sendError(player, "The character '" + set.getName() + "' cannot be given the mob type '" Messaging.sendError(player, "The character '" + set.getName() + "' cannot be given the mob type '"
+ type.name().toLowerCase() + "'."); + type.name().toLowerCase() + "'.");
create.remove(); npc.remove();
return; return;
} }
create.setCharacter(characterManager.getCharacter(character)); npc.setCharacter(characterManager.getCharacter(character));
msg += " with the character " + StringHelper.wrap(character); msg += " with the character " + StringHelper.wrap(character);
} }
} }
msg += " at your location."; msg += " at your location.";
// Initialize necessary traits // Initialize necessary traits
create.getTrait(Owner.class).setOwner(player.getName()); npc.getTrait(Owner.class).setOwner(player.getName());
create.getTrait(MobType.class).setType(type.toString()); npc.getTrait(MobType.class).setType(type.toString());
create.addTrait(traitManager.getTrait(LookClose.class, create)); npc.addTrait(traitManager.getTrait(LookClose.class, npc));
create.addTrait(traitManager.getTrait(Text.class, create)); npc.addTrait(traitManager.getTrait(Text.class, npc));
create.spawn(player.getLocation()); npc.spawn(player.getLocation());
npcManager.selectNPC(player, create); npcManager.selectNPC(player, npc);
Messaging.send(player, msg); Messaging.send(player, msg);
} }
@Command( @Command(
aliases = { "npc" }, aliases = { "npc" },
usage = "despawn", usage = "despawn",
desc = "Despawn a NPC", desc = "Despawn a NPC",
modifiers = { "despawn" }, modifiers = { "despawn" },
min = 1, min = 1,
max = 1, max = 1,
permission = "npc.despawn") permission = "npc.despawn")
public void despawn(CommandContext args, Player player, NPC npc) { public void despawn(CommandContext args, Player player, NPC npc) {
npc.getTrait(Spawned.class).setSpawned(false); npc.getTrait(Spawned.class).setSpawned(false);
npc.despawn(); npc.despawn();
@ -141,14 +141,14 @@ public class NPCCommands {
} }
@Command( @Command(
aliases = { "npc" }, aliases = { "npc" },
usage = "list (page) ((-a) --owner (owner) --type (type) --char (char))", usage = "list (page) ((-a) --owner (owner) --type (type) --char (char))",
desc = "List NPCs", desc = "List NPCs",
flags = "a", flags = "a",
modifiers = { "list" }, modifiers = { "list" },
min = 1, min = 1,
max = 2, max = 2,
permission = "npc.list") permission = "npc.list")
@Requirements @Requirements
public void list(CommandContext args, Player player, NPC npc) throws CommandException { public void list(CommandContext args, Player player, NPC npc) throws CommandException {
List<NPC> npcs = new ArrayList<NPC>(); List<NPC> npcs = new ArrayList<NPC>();
@ -213,13 +213,13 @@ public class NPCCommands {
} }
@Command( @Command(
aliases = { "npc" }, aliases = { "npc" },
usage = "lookclose", usage = "lookclose",
desc = "Toggle whether a NPC will look when a player is near", desc = "Toggle whether a NPC will look when a player is near",
modifiers = { "lookclose", "look", "rotate" }, modifiers = { "lookclose", "look", "rotate" },
min = 1, min = 1,
max = 1, max = 1,
permission = "npc.lookclose") permission = "npc.lookclose")
public void lookClose(CommandContext args, Player player, NPC npc) { public void lookClose(CommandContext args, Player player, NPC npc) {
String msg = StringHelper.wrap(npc.getName()) + " will " String msg = StringHelper.wrap(npc.getName()) + " will "
+ (npc.getTrait(LookClose.class).toggle() ? "now rotate" : "no longer rotate"); + (npc.getTrait(LookClose.class).toggle() ? "now rotate" : "no longer rotate");
@ -236,13 +236,13 @@ public class NPCCommands {
} }
@Command( @Command(
aliases = { "npc" }, aliases = { "npc" },
usage = "owner [name]", usage = "owner [name]",
desc = "Set the owner of an NPC", desc = "Set the owner of an NPC",
modifiers = { "owner" }, modifiers = { "owner" },
min = 2, min = 2,
max = 2, max = 2,
permission = "npc.owner") permission = "npc.owner")
public void owner(CommandContext args, Player player, NPC npc) throws CommandException { public void owner(CommandContext args, Player player, NPC npc) throws CommandException {
String name = args.getString(1); String name = args.getString(1);
if (npc.getTrait(Owner.class).getOwner().equals(name)) if (npc.getTrait(Owner.class).getOwner().equals(name))
@ -253,12 +253,12 @@ public class NPCCommands {
} }
@Command( @Command(
aliases = { "npc" }, aliases = { "npc" },
usage = "remove (all)", usage = "remove (all)",
desc = "Remove a NPC", desc = "Remove a NPC",
modifiers = { "remove" }, modifiers = { "remove" },
min = 1, min = 1,
max = 2) max = 2)
@Requirements @Requirements
public void remove(CommandContext args, Player player, NPC npc) throws CommandException { public void remove(CommandContext args, Player player, NPC npc) throws CommandException {
if (args.argsLength() == 2) { if (args.argsLength() == 2) {
@ -281,13 +281,13 @@ public class NPCCommands {
} }
@Command( @Command(
aliases = { "npc" }, aliases = { "npc" },
usage = "rename [name]", usage = "rename [name]",
desc = "Rename a NPC", desc = "Rename a NPC",
modifiers = { "rename" }, modifiers = { "rename" },
min = 2, min = 2,
max = 2, max = 2,
permission = "npc.rename") permission = "npc.rename")
public void rename(CommandContext args, Player player, NPC npc) { public void rename(CommandContext args, Player player, NPC npc) {
String oldName = npc.getName(); String oldName = npc.getName();
String newName = args.getString(1); String newName = args.getString(1);
@ -296,18 +296,19 @@ public class NPCCommands {
newName = newName.substring(0, 15); newName = newName.substring(0, 15);
} }
npc.setName(newName); npc.setName(newName);
Messaging.send(player, ChatColor.GREEN + "You renamed " + StringHelper.wrap(oldName) + " to " Messaging.send(player,
+ StringHelper.wrap(newName) + "."); ChatColor.GREEN + "You renamed " + StringHelper.wrap(oldName) + " to " + StringHelper.wrap(newName)
+ ".");
} }
@Command( @Command(
aliases = { "npc" }, aliases = { "npc" },
usage = "select [id]", usage = "select [id]",
desc = "Select a NPC with the given ID", desc = "Select a NPC with the given ID",
modifiers = { "select" }, modifiers = { "select" },
min = 2, min = 2,
max = 2, max = 2,
permission = "npc.select") permission = "npc.select")
@Requirements(ownership = true) @Requirements(ownership = true)
public void select(CommandContext args, Player player, NPC npc) throws CommandException { public void select(CommandContext args, Player player, NPC npc) throws CommandException {
NPC toSelect = npcManager.getNPC(args.getInteger(1)); NPC toSelect = npcManager.getNPC(args.getInteger(1));
@ -320,13 +321,13 @@ public class NPCCommands {
} }
@Command( @Command(
aliases = { "npc" }, aliases = { "npc" },
usage = "spawn [id]", usage = "spawn [id]",
desc = "Spawn an existing NPC", desc = "Spawn an existing NPC",
modifiers = { "spawn" }, modifiers = { "spawn" },
min = 2, min = 2,
max = 2, max = 2,
permission = "npc.spawn") permission = "npc.spawn")
@Requirements @Requirements
public void spawn(CommandContext args, Player player, NPC npc) throws CommandException { public void spawn(CommandContext args, Player player, NPC npc) throws CommandException {
NPC respawn = npcManager.getNPC(args.getInteger(1)); NPC respawn = npcManager.getNPC(args.getInteger(1));
@ -346,13 +347,13 @@ public class NPCCommands {
} }
@Command( @Command(
aliases = { "npc" }, aliases = { "npc" },
usage = "tp", usage = "tp",
desc = "Teleport to a NPC", desc = "Teleport to a NPC",
modifiers = { "tp", "teleport" }, modifiers = { "tp", "teleport" },
min = 1, min = 1,
max = 1, max = 1,
permission = "npc.tp") permission = "npc.tp")
public void tp(CommandContext args, Player player, NPC npc) { public void tp(CommandContext args, Player player, NPC npc) {
// Spawn the NPC if it isn't spawned to prevent NPEs // Spawn the NPC if it isn't spawned to prevent NPEs
if (!npc.isSpawned()) if (!npc.isSpawned())
@ -362,13 +363,13 @@ public class NPCCommands {
} }
@Command( @Command(
aliases = { "npc" }, aliases = { "npc" },
usage = "tphere", usage = "tphere",
desc = "Teleport a NPC to your location", desc = "Teleport a NPC to your location",
modifiers = { "tphere" }, modifiers = { "tphere" },
min = 1, min = 1,
max = 1, max = 1,
permission = "npc.tphere") permission = "npc.tphere")
public void tphere(CommandContext args, Player player, NPC npc) { public void tphere(CommandContext args, Player player, NPC npc) {
// Spawn the NPC if it isn't spawned to prevent NPEs // Spawn the NPC if it isn't spawned to prevent NPEs
if (!npc.isSpawned()) if (!npc.isSpawned())
@ -378,13 +379,13 @@ public class NPCCommands {
} }
@Command( @Command(
aliases = { "npc" }, aliases = { "npc" },
usage = "power", usage = "power",
desc = "Toggle a creeper NPC as powered", desc = "Toggle a creeper NPC as powered",
modifiers = { "power" }, modifiers = { "power" },
min = 1, min = 1,
max = 1, max = 1,
permission = "npc.power") permission = "npc.power")
@Requirements(selected = true, ownership = true, types = { EntityType.CREEPER }) @Requirements(selected = true, ownership = true, types = { EntityType.CREEPER })
public void power(CommandContext args, Player player, NPC npc) { public void power(CommandContext args, Player player, NPC npc) {
String msg = StringHelper.wrap(npc.getName()) + " will " String msg = StringHelper.wrap(npc.getName()) + " will "

View File

@ -146,6 +146,7 @@ public abstract class CitizensNPC extends AbstractNPC {
return false; return false;
mcEntity = createHandle(loc); mcEntity = createHandle(loc);
mcEntity.world.addEntity(mcEntity); mcEntity.world.addEntity(mcEntity);
mcEntity.world.players.remove(mcEntity); mcEntity.world.players.remove(mcEntity);

View File

@ -60,7 +60,7 @@ public class CitizensHumanNPC extends CitizensNPC implements Equipable {
@Override @Override
public void update() { public void update() {
super.update(); super.update();
if (mcEntity != null) { if (isSpawned() && getBukkitEntity().getLocation().getChunk().isLoaded()) {
mcEntity.move(0, -0.1, 0); mcEntity.move(0, -0.1, 0);
// gravity! also works around an entity.onGround not updating issue // gravity! also works around an entity.onGround not updating issue
// (onGround is normally updated by the client) // (onGround is normally updated by the client)