more bugfixes

This commit is contained in:
aPunch 2012-01-28 16:40:00 -06:00
parent 6837ac11ea
commit 922cf2444b
6 changed files with 49 additions and 54 deletions

Binary file not shown.

View File

@ -79,7 +79,7 @@ public class Citizens extends JavaPlugin {
}
NPC npc = null;
if (player != null && npcManager.hasNPCSelected(player))
if (player != null)
npc = npcManager.getSelectedNPC(player);
try {
@ -190,13 +190,6 @@ public class Citizens extends JavaPlugin {
// Load the character if it exists, otherwise remove the character
if (character != null)
character.load(key.getRelative("characters." + character.getName()));
else {
if (key.keyExists("character")) {
Messaging.debug("Character '" + key.getString("character")
+ "' does not exist. Removing character from the NPC with ID '" + npc.getId() + "'.");
key.removeKey("character");
}
}
// Load traits
for (DataKey traitKey : key.getSubKeys()) {

View File

@ -6,6 +6,7 @@ import java.util.List;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.trait.trait.Owner;
import net.citizensnpcs.api.npc.trait.trait.SpawnLocation;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.util.Messaging;
@ -92,7 +93,9 @@ public class EventListen implements Listener {
return;
Player player = (Player) event.getTarget();
if (!npcManager.npcIsSelectedByPlayer(player, npc)) {
if (npcManager.canSelectNPC(player, npc)) {
if (player.hasPermission("citizens.npc.select")
&& player.getItemInHand().getTypeId() == Setting.SELECTION_ITEM.getInt()
&& npc.getTrait(Owner.class).getOwner().equals(player.getName())) {
npcManager.selectNPC(player, npc);
Messaging.sendWithNPC(player, Setting.SELECTION_MESSAGE.getString(), npc);
if (!Setting.QUICK_SELECT.getBoolean())

View File

@ -37,12 +37,20 @@ public class NPCCommands {
@Requirements
public void createNPC(CommandContext args, Player player, NPC npc) {
CitizensNPC create = (CitizensNPC) npcManager.createNPC(args.getString(1));
String msg = ChatColor.GREEN + "You created " + StringHelper.wrap(create.getName());
if (args.argsLength() == 3 && characterManager.getInstance(args.getString(2), create) != null) {
create.setCharacter(characterManager.getInstance(args.getString(2), create));
msg += " with the character " + StringHelper.wrap(args.getString(2));
String successMsg = ChatColor.GREEN + "You created " + StringHelper.wrap(create.getName());
boolean success = true;
if (args.argsLength() == 3) {
if (characterManager.getInstance(args.getString(2), create) == null) {
Messaging.sendError(player,
"The character '" + args.getString(2) + "' does not exist. " + create.getName()
+ " was created at your location without a character.");
success = false;
} else {
create.setCharacter(characterManager.getInstance(args.getString(2), create));
successMsg += " with the character " + StringHelper.wrap(args.getString(2));
}
}
msg += " at your location.";
successMsg += " at your location.";
// Set the owner
create.addTrait(new Owner(player.getName()));
@ -50,7 +58,8 @@ public class NPCCommands {
create.spawn(player.getLocation());
npcManager.selectNPC(player, create);
Messaging.send(player, msg);
if (success)
Messaging.send(player, successMsg);
}
@Command(
@ -93,7 +102,6 @@ public class NPCCommands {
@Permission("npc.despawn")
public void despawnNPC(CommandContext args, Player player, NPC npc) {
npc.despawn();
npcManager.deselectNPC(player);
Messaging.send(player, ChatColor.GREEN + "You despawned " + StringHelper.wrap(npc.getName()) + ".");
}
}

View File

@ -34,7 +34,6 @@ public class CitizensNPC extends AbstractNPC {
Bukkit.getPluginManager().callEvent(new NPCDespawnEvent(this));
manager.despawn(this);
getHandle().die();
spawned = false;
return true;
@ -78,10 +77,7 @@ public class CitizensNPC extends AbstractNPC {
if (spawnEvent.isCancelled())
return false;
if (mcEntity == null)
mcEntity = manager.spawn(this, loc);
else
manager.spawn(this, loc);
mcEntity = manager.spawn(this, loc);
// Set the location
addTrait(new SpawnLocation(loc));

View File

@ -2,17 +2,17 @@ package net.citizensnpcs.npc;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCManager;
import net.citizensnpcs.api.npc.trait.Character;
import net.citizensnpcs.api.npc.trait.trait.Owner;
import net.citizensnpcs.api.npc.trait.trait.SpawnLocation;
import net.citizensnpcs.resource.lib.CraftNPC;
import net.citizensnpcs.storage.Storage;
@ -35,7 +35,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, Integer> selected = new ConcurrentHashMap<String, Integer>();
private final Map<Integer, Set<String>> selected = new ConcurrentHashMap<Integer, Set<String>>();
private final Storage saves;
public CitizensNPCManager(Storage saves) {
@ -61,13 +61,16 @@ public class CitizensNPCManager implements NPCManager {
public void despawn(NPC npc) {
CraftNPC mcEntity = ((CitizensNPC) npc).getHandle();
for (Player player : Bukkit.getOnlinePlayers())
((CraftPlayer) player).getHandle().netServerHandler.sendPacket(new Packet29DestroyEntity(mcEntity.id));
Location loc = npc.getBukkitEntity().getLocation();
getWorldServer(loc.getWorld()).removeEntity(mcEntity);
npc.getTrait(SpawnLocation.class).setLocation(loc);
if (selected.get(npc.getId()) != null)
selected.get(npc.getId()).clear();
spawned.remove(mcEntity.getPlayer().getEntityId());
for (Player player : Bukkit.getOnlinePlayers())
((CraftPlayer) player).getHandle().netServerHandler.sendPacket(new Packet29DestroyEntity(mcEntity.id));
getWorldServer(loc.getWorld()).removeEntity(mcEntity);
mcEntity.die();
}
@Override
@ -100,12 +103,7 @@ public class CitizensNPCManager implements NPCManager {
return npcs;
}
@Override
public Iterable<NPC> getSpawnedNPCs() {
return spawned;
}
public int generateUniqueId() {
private int generateUniqueId() {
int count = 0;
while (getNPC(count++) != null)
;
@ -122,10 +120,11 @@ public class CitizensNPCManager implements NPCManager {
}
public void remove(NPC npc) {
if (spawned.contains(npc.getBukkitEntity().getEntityId()))
if (spawned.contains(((CitizensNPC) npc).getHandle().getPlayer().getEntityId()))
despawn(npc);
byID.remove(npc.getId());
saves.getKey("npc").removeKey("" + npc.getId());
selected.remove(npc.getId());
}
public CraftNPC spawn(NPC npc, Location loc) {
@ -142,34 +141,30 @@ public class CitizensNPCManager implements NPCManager {
}
public void selectNPC(Player player, NPC npc) {
selected.put(player.getName(), npc.getId());
}
// Remove existing selection if any
NPC select = getSelectedNPC(player);
if (select != null)
selected.get(select.getId()).remove(player.getName());
public void deselectNPC(Player player) {
selected.remove(player.getName());
Set<String> selectors = selected.get(npc.getId());
if (selectors == null)
selectors = new HashSet<String>();
selectors.add(player.getName());
selected.put(npc.getId(), selectors);
}
public boolean npcIsSelectedByPlayer(Player player, NPC npc) {
if (!selected.containsKey(player.getName()))
if (!selected.containsKey(npc.getId()))
return false;
return selected.get(player.getName()) == npc.getId();
}
public boolean hasNPCSelected(Player player) {
return selected.containsKey(player.getName());
}
public boolean canSelectNPC(Player player, NPC npc) {
if (player.hasPermission("citizens.npc.select"))
return player.getItemInHand().getTypeId() == Setting.SELECTION_ITEM.getInt()
&& npc.getTrait(Owner.class).getOwner().equals(player.getName());
return false;
return selected.get(npc.getId()).contains(player.getName());
}
public NPC getSelectedNPC(Player player) {
if (!selected.containsKey(player.getName()))
return null;
return getNPC(selected.get(player.getName()));
for (int id : selected.keySet()) {
if (selected.get(id).contains(player.getName()))
return getNPC(id);
}
return null;
}
public int size() {