mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-12-27 11:37:42 +01:00
Implement getByUniqueId
This commit is contained in:
parent
0c139a27d4
commit
cf4c6f851d
@ -10,6 +10,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import net.citizensnpcs.api.CitizensAPI;
|
||||
import net.citizensnpcs.api.event.DespawnReason;
|
||||
import net.citizensnpcs.api.event.NPCCreateEvent;
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
@ -54,7 +55,7 @@ public class CitizensNPCRegistry implements NPCRegistry {
|
||||
|
||||
@Override
|
||||
public void deregister(NPC npc) {
|
||||
npcs.remove(npc.getId());
|
||||
npcs.remove(npc);
|
||||
if (saves != null) {
|
||||
saves.clearData(npc);
|
||||
}
|
||||
@ -92,6 +93,22 @@ public class CitizensNPCRegistry implements NPCRegistry {
|
||||
return new CitizensNPC(uuid, id, name, EntityControllers.createForType(type), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NPC getByUniqueId(UUID uuid) {
|
||||
NPC npc = npcs.get(uuid);
|
||||
if (npc != null)
|
||||
return npc;
|
||||
for (NPCRegistry registry : CitizensAPI.getNPCRegistries()) {
|
||||
if (registry != this) {
|
||||
NPC other = registry.getByUniqueId(uuid);
|
||||
if (other != null) {
|
||||
return other;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NPC getNPC(Entity entity) {
|
||||
if (entity == null)
|
||||
@ -121,12 +138,18 @@ public class CitizensNPCRegistry implements NPCRegistry {
|
||||
|
||||
public static class MapNPCCollection implements NPCCollection {
|
||||
private final Map<Integer, NPC> npcs = Maps.newHashMap();
|
||||
private final Map<UUID, NPC> uniqueNPCs = Maps.newHashMap();
|
||||
|
||||
@Override
|
||||
public NPC get(int id) {
|
||||
return npcs.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NPC get(UUID uuid) {
|
||||
return uniqueNPCs.get(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<NPC> iterator() {
|
||||
return npcs.values().iterator();
|
||||
@ -135,11 +158,13 @@ public class CitizensNPCRegistry implements NPCRegistry {
|
||||
@Override
|
||||
public void put(int id, NPC npc) {
|
||||
npcs.put(id, npc);
|
||||
uniqueNPCs.put(npc.getUniqueId(), npc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(int id) {
|
||||
npcs.remove(id);
|
||||
public void remove(NPC npc) {
|
||||
npcs.remove(npc.getId());
|
||||
uniqueNPCs.remove(npc.getUniqueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -153,21 +178,29 @@ public class CitizensNPCRegistry implements NPCRegistry {
|
||||
public static interface NPCCollection extends Iterable<NPC> {
|
||||
public NPC get(int id);
|
||||
|
||||
public NPC get(UUID uuid);
|
||||
|
||||
public void put(int id, NPC npc);
|
||||
|
||||
public void remove(int id);
|
||||
public void remove(NPC npc);
|
||||
|
||||
public Iterable<NPC> sorted();
|
||||
}
|
||||
|
||||
public static class TroveNPCCollection implements NPCCollection {
|
||||
private final TIntObjectHashMap<NPC> npcs = new TIntObjectHashMap<NPC>();
|
||||
private final Map<UUID, NPC> uniqueNPCs = Maps.newHashMap();
|
||||
|
||||
@Override
|
||||
public NPC get(int id) {
|
||||
return npcs.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NPC get(UUID uuid) {
|
||||
return uniqueNPCs.get(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<NPC> iterator() {
|
||||
return npcs.valueCollection().iterator();
|
||||
@ -176,11 +209,13 @@ public class CitizensNPCRegistry implements NPCRegistry {
|
||||
@Override
|
||||
public void put(int id, NPC npc) {
|
||||
npcs.put(id, npc);
|
||||
uniqueNPCs.put(npc.getUniqueId(), npc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(int id) {
|
||||
npcs.remove(id);
|
||||
public void remove(NPC npc) {
|
||||
npcs.remove(npc.getId());
|
||||
uniqueNPCs.remove(npc.getUniqueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,6 +1,7 @@
|
||||
package net.citizensnpcs.npc;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import net.citizensnpcs.Settings.Setting;
|
||||
import net.citizensnpcs.api.CitizensAPI;
|
||||
@ -30,7 +31,7 @@ import org.bukkit.plugin.Plugin;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
public class NPCSelector implements Listener, net.citizensnpcs.api.npc.NPCSelector {
|
||||
private int consoleSelectedNPC = -1;
|
||||
private UUID consoleSelectedNPC;
|
||||
private final Plugin plugin;
|
||||
|
||||
public NPCSelector(Plugin plugin) {
|
||||
@ -45,9 +46,9 @@ public class NPCSelector implements Listener, net.citizensnpcs.api.npc.NPCSelect
|
||||
} else if (sender instanceof BlockCommandSender) {
|
||||
return getSelectedFromMetadatable(((BlockCommandSender) sender).getBlock());
|
||||
} else if (sender instanceof ConsoleCommandSender) {
|
||||
if (consoleSelectedNPC == -1)
|
||||
if (consoleSelectedNPC == null)
|
||||
return null;
|
||||
return CitizensAPI.getNPCRegistry().getById(consoleSelectedNPC);
|
||||
return CitizensAPI.getNPCRegistry().getByUniqueId(consoleSelectedNPC);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -56,7 +57,7 @@ public class NPCSelector implements Listener, net.citizensnpcs.api.npc.NPCSelect
|
||||
List<MetadataValue> metadata = sender.getMetadata("selected");
|
||||
if (metadata.size() == 0)
|
||||
return null;
|
||||
return CitizensAPI.getNPCRegistry().getById(metadata.get(0).asInt());
|
||||
return CitizensAPI.getNPCRegistry().getByUniqueId((UUID) metadata.get(0).value());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@ -67,7 +68,7 @@ public class NPCSelector implements Listener, net.citizensnpcs.api.npc.NPCSelect
|
||||
return;
|
||||
for (String value : selectors) {
|
||||
if (value.equals("console")) {
|
||||
consoleSelectedNPC = -1;
|
||||
consoleSelectedNPC = null;
|
||||
} else if (value.startsWith("@")) {
|
||||
String[] parts = value.substring(1, value.length()).split(":");
|
||||
World world = Bukkit.getWorld(parts[0]);
|
||||
@ -102,8 +103,9 @@ public class NPCSelector implements Listener, net.citizensnpcs.api.npc.NPCSelect
|
||||
}
|
||||
|
||||
private void removeMetadata(Metadatable metadatable) {
|
||||
if (metadatable != null)
|
||||
if (metadatable != null) {
|
||||
metadatable.removeMetadata("selected", plugin);
|
||||
}
|
||||
}
|
||||
|
||||
public void select(CommandSender sender, NPC npc) {
|
||||
@ -125,7 +127,7 @@ public class NPCSelector implements Listener, net.citizensnpcs.api.npc.NPCSelect
|
||||
setMetadata(npc, block);
|
||||
selectors.add(toName(block));
|
||||
} else if (sender instanceof ConsoleCommandSender) {
|
||||
consoleSelectedNPC = npc.getId();
|
||||
consoleSelectedNPC = npc.getUniqueId();
|
||||
selectors.add("console");
|
||||
}
|
||||
|
||||
@ -133,10 +135,10 @@ public class NPCSelector implements Listener, net.citizensnpcs.api.npc.NPCSelect
|
||||
}
|
||||
|
||||
private void setMetadata(NPC npc, Metadatable metadatable) {
|
||||
if (metadatable.hasMetadata("selected"))
|
||||
if (metadatable.hasMetadata("selected")) {
|
||||
metadatable.removeMetadata("selected", plugin);
|
||||
|
||||
metadatable.setMetadata("selected", new FixedMetadataValue(plugin, npc.getId()));
|
||||
}
|
||||
metadatable.setMetadata("selected", new FixedMetadataValue(plugin, npc.getUniqueId()));
|
||||
}
|
||||
|
||||
private String toName(Block block) {
|
||||
|
@ -30,7 +30,7 @@ public class TalkableEntity implements Talkable {
|
||||
|
||||
/**
|
||||
* Used to compare a LivingEntity to this TalkableEntity
|
||||
*
|
||||
*
|
||||
* @return 0 if the Entities are the same, 1 if they are not, -1 if the
|
||||
* object compared is not a valid LivingEntity
|
||||
*/
|
||||
@ -42,8 +42,8 @@ public class TalkableEntity implements Talkable {
|
||||
// If NPC and matches, return 0
|
||||
} else if (CitizensAPI.getNPCRegistry().isNPC((Entity) o)
|
||||
&& CitizensAPI.getNPCRegistry().isNPC(entity)
|
||||
&& CitizensAPI.getNPCRegistry().getNPC((Entity) o).getId() == CitizensAPI.getNPCRegistry()
|
||||
.getNPC(entity).getId()) {
|
||||
&& CitizensAPI.getNPCRegistry().getNPC((Entity) o).getUniqueId()
|
||||
.equals(CitizensAPI.getNPCRegistry().getNPC(entity).getUniqueId())) {
|
||||
return 0;
|
||||
} else if (entity.equals(o)) {
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user