Fix reload removing NPCs. This fixes CITIZENS-13.

This commit is contained in:
aPunch 2012-03-11 15:14:36 -05:00
parent 2f59dd5f07
commit 0e657d81db
7 changed files with 116 additions and 104 deletions

Binary file not shown.

Binary file not shown.

View File

@ -36,7 +36,6 @@ import net.citizensnpcs.util.Messaging;
import net.citizensnpcs.util.Metrics;
import net.citizensnpcs.util.StringHelper;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
@ -170,13 +169,13 @@ public class Citizens extends JavaPlugin {
// Register commands
registerCommands();
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new NPCUpdater(npcManager), 0, 1);
getServer().getScheduler().scheduleSyncRepeatingTask(this, new NPCUpdater(npcManager), 0, 1);
Messaging.log("v" + getDescription().getVersion() + " enabled.");
// Setup NPCs after all plugins have been enabled (allows for multiworld
// support and for NPCs to properly register external settings)
if (Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
if (getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
@Override
public void run() {
try {
@ -228,8 +227,7 @@ public class Citizens extends JavaPlugin {
public void reload() throws NPCLoadException {
Editor.leaveAll();
config.load();
saves.load();
npcManager.removeAll();
npcManager.safeRemove();
setupNPCs();
getServer().getPluginManager().callEvent(new CitizensReloadEvent());
@ -237,13 +235,14 @@ public class Citizens extends JavaPlugin {
public void save() {
config.save();
for (NPC npc : npcManager) {
for (NPC npc : npcManager)
((CitizensNPC) npc).save(saves.getKey("npc." + npc.getId()));
}
saves.save();
}
private void setupNPCs() throws NPCLoadException {
saves.load();
int created = 0, spawned = 0;
for (DataKey key : saves.getKey("npc").getIntegerSubKeys()) {
int id = Integer.parseInt(key.name());

View File

@ -46,6 +46,7 @@ public class AdminCommands {
plugin.reload();
Messaging.send(sender, "<e>Citizens reloaded.");
} catch (NPCLoadException ex) {
ex.printStackTrace();
throw new CommandException("Error occured while reloading, see console.");
}
}

View File

@ -40,12 +40,12 @@ public class NPCCommands {
}
@Command(
aliases = { "npc" },
usage = "character [character]",
desc = "Set the character of an NPC",
modifiers = { "character" },
min = 2,
max = 2)
aliases = { "npc" },
usage = "character [character]",
desc = "Set the character of an 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);
@ -56,19 +56,19 @@ public class NPCCommands {
if (!player.hasPermission("citizens.npc.character." + character.getName())
&& !player.hasPermission("citizens.npc.character.*") && !player.hasPermission("citizens.admin"))
throw new NoPermissionsException();
Messaging.send(player,
StringHelper.wrap(npc.getName() + "'s") + " character is now '" + StringHelper.wrap(name) + "'.");
Messaging.send(player, StringHelper.wrap(npc.getName() + "'s") + " character is now '"
+ StringHelper.wrap(name) + "'.");
npc.setCharacter(character);
}
@Command(
aliases = { "npc" },
usage = "create [name] (--type (type) --char (char))",
desc = "Create a new NPC",
modifiers = { "create" },
min = 2,
max = 5,
permission = "npc.create")
aliases = { "npc" },
usage = "create [name] (--type (type) --char (char))",
desc = "Create a new NPC",
modifiers = { "create" },
min = 2,
max = 5,
permission = "npc.create")
@Requirements
public void create(CommandContext args, Player player, NPC npc) {
String name = args.getString(1);
@ -117,13 +117,13 @@ public class NPCCommands {
}
@Command(
aliases = { "npc" },
usage = "despawn",
desc = "Despawn an NPC",
modifiers = { "despawn" },
min = 1,
max = 1,
permission = "npc.despawn")
aliases = { "npc" },
usage = "despawn",
desc = "Despawn an 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();
@ -131,14 +131,14 @@ public class NPCCommands {
}
@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")
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>();
@ -203,13 +203,13 @@ public class NPCCommands {
}
@Command(
aliases = { "npc" },
usage = "lookclose",
desc = "Toggle whether an NPC will look when a player is near",
modifiers = { "lookclose", "look", "rotate" },
min = 1,
max = 1,
permission = "npc.lookclose")
aliases = { "npc" },
usage = "lookclose",
desc = "Toggle whether an 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");
@ -226,13 +226,13 @@ public class NPCCommands {
}
@Command(
aliases = { "npc" },
usage = "owner [name]",
desc = "Set the owner of an NPC",
modifiers = { "owner" },
min = 2,
max = 2,
permission = "npc.owner")
aliases = { "npc" },
usage = "owner [name]",
desc = "Set the owner of an NPC",
modifiers = { "owner" },
min = 2,
max = 2,
permission = "npc.owner")
public void owner(CommandContext args, Player player, NPC npc) throws CommandException {
String name = args.getString(1);
if (npc.getTrait(Owner.class).getOwner().equals(name))
@ -243,12 +243,12 @@ public class NPCCommands {
}
@Command(
aliases = { "npc" },
usage = "remove (all)",
desc = "Remove an NPC",
modifiers = { "remove" },
min = 1,
max = 2)
aliases = { "npc" },
usage = "remove (all)",
desc = "Remove an NPC",
modifiers = { "remove" },
min = 1,
max = 2)
@Requirements
public void remove(CommandContext args, Player player, NPC npc) throws CommandException {
if (args.argsLength() == 2) {
@ -271,13 +271,13 @@ public class NPCCommands {
}
@Command(
aliases = { "npc" },
usage = "rename [name]",
desc = "Rename an NPC",
modifiers = { "rename" },
min = 2,
max = 2,
permission = "npc.rename")
aliases = { "npc" },
usage = "rename [name]",
desc = "Rename an 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);
@ -286,19 +286,18 @@ public class NPCCommands {
newName = newName.substring(0, 15);
}
npc.setName(newName);
Messaging.send(player,
ChatColor.GREEN + "You renamed " + StringHelper.wrap(oldName) + " to " + StringHelper.wrap(newName)
+ ".");
Messaging.send(player, ChatColor.GREEN + "You renamed " + StringHelper.wrap(oldName) + " to "
+ StringHelper.wrap(newName) + ".");
}
@Command(
aliases = { "npc" },
usage = "select [id]",
desc = "Select an NPC with the given ID",
modifiers = { "select" },
min = 2,
max = 2,
permission = "npc.select")
aliases = { "npc" },
usage = "select [id]",
desc = "Select an 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));
@ -311,13 +310,13 @@ public class NPCCommands {
}
@Command(
aliases = { "npc" },
usage = "spawn [id]",
desc = "Spawn an existing NPC",
modifiers = { "spawn" },
min = 2,
max = 2,
permission = "npc.spawn")
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));
@ -337,13 +336,13 @@ public class NPCCommands {
}
@Command(
aliases = { "npc" },
usage = "tp",
desc = "Teleport to an NPC",
modifiers = { "tp", "teleport" },
min = 1,
max = 1,
permission = "npc.tp")
aliases = { "npc" },
usage = "tp",
desc = "Teleport to an 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())
@ -353,13 +352,13 @@ public class NPCCommands {
}
@Command(
aliases = { "npc" },
usage = "tphere",
desc = "Teleport an NPC to your location",
modifiers = { "tphere" },
min = 1,
max = 1,
permission = "npc.tphere")
aliases = { "npc" },
usage = "tphere",
desc = "Teleport an 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())

View File

@ -7,6 +7,7 @@ import java.util.List;
import net.citizensnpcs.Citizens;
import net.citizensnpcs.api.event.NPCSelectEvent;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCManager;
import net.citizensnpcs.api.npc.character.Character;
@ -51,9 +52,9 @@ public class CitizensNPCManager implements NPCManager {
return createNPC(type, generateUniqueId(), name, character);
}
public void despawn(NPC npc, boolean deselect) {
public void despawn(NPC npc, boolean keepSelected) {
npc.getTrait(SpawnLocation.class).setLocation(npc.getBukkitEntity().getLocation());
if (!deselect)
if (!keepSelected)
npc.removeMetadata("selectors", plugin);
npc.getBukkitEntity().remove();
}
@ -100,13 +101,16 @@ public class CitizensNPCManager implements NPCManager {
public void remove(NPC npc) {
npcs.remove(npc.getId());
saves.getKey("npc").removeKey(String.valueOf(npc.getId()));
removeMetadata(npc);
}
// Remove metadata from selectors
if (npc.hasMetadata("selectors")) {
for (MetadataValue value : npc.getMetadata("selectors"))
if (Bukkit.getPlayer(value.asString()) != null)
Bukkit.getPlayer(value.asString()).removeMetadata("selected", plugin);
npc.removeMetadata("selectors", plugin);
public void safeRemove() throws NPCLoadException {
// Destroy all NPCs everywhere besides storage
while (iterator().hasNext()) {
NPC npc = iterator().next();
removeMetadata(npc);
npc.despawn();
iterator().remove();
}
}
@ -115,7 +119,6 @@ public class CitizensNPCManager implements NPCManager {
iterator().next().remove();
}
// TODO: remove
public void selectNPC(Player player, NPC npc) {
// Remove existing selection if any
if (player.hasMetadata("selected"))
@ -130,4 +133,14 @@ public class CitizensNPCManager implements NPCManager {
// Call selection event
player.getServer().getPluginManager().callEvent(new NPCSelectEvent(npc, player));
}
private void removeMetadata(NPC npc) {
// Remove metadata from selectors
if (npc.hasMetadata("selectors")) {
for (MetadataValue value : npc.getMetadata("selectors"))
if (Bukkit.getPlayer(value.asString()) != null)
Bukkit.getPlayer(value.asString()).removeMetadata("selected", plugin);
npc.removeMetadata("selectors", plugin);
}
}
}