fix ID bug and others

This commit is contained in:
aPunch 2012-01-23 09:42:01 -06:00
parent 7ef3df8dfc
commit 420302043a
4 changed files with 38 additions and 24 deletions

View File

@ -14,6 +14,7 @@ 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.SpawnLocation;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.storage.Storage;
import net.citizensnpcs.storage.database.DatabaseStorage;
@ -49,6 +50,7 @@ public class Citizens extends JavaPlugin {
if (args[0].equals("spawn")) {
NPC npc = npcManager.createNPC(ChatColor.GREEN + "aPunch");
npc.spawn(((Player) sender).getLocation());
((CitizensNPC) npc).save(saves);
} else if (args[0].equals("despawn")) {
for (NPC npc : npcManager.getSpawnedNPCs())
npc.despawn();
@ -102,22 +104,8 @@ public class Citizens extends JavaPlugin {
}
private void saveNPCs() {
for (NPC npc : npcManager.getAllNPCs()) {
DataKey root = saves.getKey("npc." + npc.getId());
root.setString("name", npc.getFullName());
if (root.getBoolean("spawned"))
root.setBoolean("spawned", !npc.getBukkitEntity().isDead());
// Save the character if it exists
if (npc.getCharacter() != null) {
root.setString("character", npc.getCharacter().getName());
npc.getCharacter().save(root.getRelative(npc.getCharacter().getName()));
}
// Save all existing traits
for (Trait trait : npc.getTraits())
trait.save(root.getRelative(trait.getName()));
}
for (NPC npc : npcManager.getAllNPCs())
((CitizensNPC) npc).save(saves);
saves.save();
}
@ -129,7 +117,7 @@ public class Citizens extends JavaPlugin {
if (!key.keyExists("name"))
throw new NPCLoadException("Could not find a name for the NPC with ID '" + id + "'.");
Character character = characterManager.getInstance(key.getString("character"));
NPC npc = npcManager.createNPC(key.getString("name"), character);
NPC npc = npcManager.createNPC(id, key.getString("name"), character);
// Load the character if it exists, otherwise remove the character
if (character != null)

View File

@ -1,11 +1,14 @@
package net.citizensnpcs.npc;
import net.citizensnpcs.api.DataKey;
import net.citizensnpcs.api.event.NPCDespawnEvent;
import net.citizensnpcs.api.event.NPCSpawnEvent;
import net.citizensnpcs.api.npc.AbstractNPC;
import net.citizensnpcs.api.npc.ai.Navigator;
import net.citizensnpcs.api.npc.trait.Trait;
import net.citizensnpcs.api.npc.trait.trait.SpawnLocation;
import net.citizensnpcs.resources.lib.CraftNPC;
import net.citizensnpcs.storage.Storage;
import net.citizensnpcs.util.Messaging;
import org.bukkit.Bukkit;
@ -86,4 +89,23 @@ public class CitizensNPC extends AbstractNPC {
spawned = true;
}
public void save(Storage saves) {
DataKey key = saves.getKey("npc." + getId());
key.setString("name", getFullName());
if (!key.keyExists("spawned"))
key.setBoolean("spawned", true);
if (key.getBoolean("spawned"))
key.setBoolean("spawned", !getBukkitEntity().isDead());
// Save the character if it exists
if (getCharacter() != null) {
key.setString("character", getCharacter().getName());
getCharacter().save(key.getRelative(getCharacter().getName()));
}
// Save all existing traits
for (Trait trait : getTraits())
trait.save(key.getRelative(trait.getName()));
}
}

View File

@ -33,7 +33,7 @@ import org.bukkit.entity.Player;
public class CitizensNPCManager implements NPCManager {
private final ByIdArray<NPC> spawned = new ByIdArray<NPC>();
private final ByIdArray<NPC> byID = new ByIdArray<NPC>();
private final Map<String, NPC> selected = new ConcurrentHashMap<String, NPC>();
private final Map<String, Integer> selected = new ConcurrentHashMap<String, Integer>();
@Override
public NPC createNPC(String name) {
@ -42,7 +42,11 @@ public class CitizensNPCManager implements NPCManager {
@Override
public NPC createNPC(String name, Character character) {
CitizensNPC npc = new CitizensNPC(this, getUniqueID(), name);
return createNPC(generateUniqueId(), name, character);
}
public NPC createNPC(int id, String name, Character character) {
CitizensNPC npc = new CitizensNPC(this, id, name);
npc.setCharacter(character);
byID.put(npc.getId(), npc);
return npc;
@ -93,11 +97,11 @@ public class CitizensNPCManager implements NPCManager {
return spawned;
}
public int getUniqueID() {
public int generateUniqueId() {
int count = 0;
while (getNPC(count++) != null)
;
return count;
return count - 1;
}
private WorldServer getWorldServer(World world) {
@ -129,14 +133,14 @@ public class CitizensNPCManager implements NPCManager {
}
public void selectNPC(Player player, NPC npc) {
selected.put(player.getName(), npc);
selected.put(player.getName(), npc.getId());
}
public boolean canSelect(Player player, NPC npc) {
if (player.hasPermission("citizens.npc.select")) {
if (!selected.containsKey(player.getName()))
return true;
return selected.get(player.getName()).getId() != npc.getId()
return selected.get(player.getName()) != npc.getId()
&& player.getItemInHand().getTypeId() == Setting.SELECTION_ITEM.getInt();
}
return false;

View File

@ -32,7 +32,7 @@ public class CraftNPC extends EntityPlayer {
});
netServerHandler = new NPCNetHandler(minecraftServer, netMgr, this);
netMgr.a(netServerHandler);
netMgr.a(); // this interrupts the read/write threads
netMgr.a();
try {
socket.close();