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

@ -56,8 +56,8 @@ 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);
}
@ -286,9 +286,8 @@ 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(

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);
}
}
}