refactor(api): dynamically fetch the current default npc registry (#1672)

The api now allows setting the default NPC registry therefor commands
and listeners need to fetch the latest version of the NPCRegistry.

#1671
This commit is contained in:
Silthus 2019-01-18 17:18:43 +01:00 committed by fullwall
parent 5447561d3d
commit 52b3902aca
3 changed files with 32 additions and 40 deletions

View File

@ -97,14 +97,13 @@ import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util; import net.citizensnpcs.util.Util;
public class EventListen implements Listener { public class EventListen implements Listener {
private final NPCRegistry npcRegistry = CitizensAPI.getNPCRegistry();
private final Map<String, NPCRegistry> registries; private final Map<String, NPCRegistry> registries;
private final SkinUpdateTracker skinUpdateTracker; private final SkinUpdateTracker skinUpdateTracker;
private final ListMultimap<ChunkCoord, NPC> toRespawn = ArrayListMultimap.create(); private final ListMultimap<ChunkCoord, NPC> toRespawn = ArrayListMultimap.create();
EventListen(Map<String, NPCRegistry> registries) { EventListen(Map<String, NPCRegistry> registries) {
this.registries = registries; this.registries = registries;
this.skinUpdateTracker = new SkinUpdateTracker(npcRegistry, registries); this.skinUpdateTracker = new SkinUpdateTracker(registries);
} }
private void checkCreationEvent(CommandSenderCreateNPCEvent event) { private void checkCreationEvent(CommandSenderCreateNPCEvent event) {
@ -121,7 +120,7 @@ public class EventListen implements Listener {
if (limit < 0) if (limit < 0)
return; return;
int owned = 0; int owned = 0;
for (NPC npc : npcRegistry) { for (NPC npc : CitizensAPI.getNPCRegistry()) {
if (!event.getNPC().equals(npc) && npc.hasTrait(Owner.class) if (!event.getNPC().equals(npc) && npc.hasTrait(Owner.class)
&& npc.getTrait(Owner.class).isOwnedBy(event.getCreator())) { && npc.getTrait(Owner.class).isOwnedBy(event.getCreator())) {
owned++; owned++;
@ -135,7 +134,7 @@ public class EventListen implements Listener {
} }
private Iterable<NPC> getAllNPCs() { private Iterable<NPC> getAllNPCs() {
return Iterables.filter(Iterables.<NPC> concat(npcRegistry, Iterables.concat(registries.values())), return Iterables.filter(Iterables.<NPC> concat(CitizensAPI.getNPCRegistry(), Iterables.concat(registries.values())),
Predicates.notNull()); Predicates.notNull());
} }
@ -202,11 +201,11 @@ public class EventListen implements Listener {
@EventHandler @EventHandler
public void onEntityDamage(EntityDamageEvent event) { public void onEntityDamage(EntityDamageEvent event) {
NPC npc = npcRegistry.getNPC(event.getEntity()); NPC npc = CitizensAPI.getNPCRegistry().getNPC(event.getEntity());
if (npc == null) { if (npc == null) {
if (event instanceof EntityDamageByEntityEvent) { if (event instanceof EntityDamageByEntityEvent) {
npc = npcRegistry.getNPC(((EntityDamageByEntityEvent) event).getDamager()); npc = CitizensAPI.getNPCRegistry().getNPC(((EntityDamageByEntityEvent) event).getDamager());
if (npc == null) if (npc == null)
return; return;
event.setCancelled(!npc.data().get(NPC.DAMAGE_OTHERS_METADATA, true)); event.setCancelled(!npc.data().get(NPC.DAMAGE_OTHERS_METADATA, true));
@ -235,7 +234,7 @@ public class EventListen implements Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onEntityDeath(EntityDeathEvent event) { public void onEntityDeath(EntityDeathEvent event) {
final NPC npc = npcRegistry.getNPC(event.getEntity()); final NPC npc = CitizensAPI.getNPCRegistry().getNPC(event.getEntity());
if (npc == null) { if (npc == null) {
return; return;
} }
@ -273,7 +272,7 @@ public class EventListen implements Listener {
@EventHandler @EventHandler
public void onEntityPortal(EntityPortalEvent event) { public void onEntityPortal(EntityPortalEvent event) {
NPC npc = npcRegistry.getNPC(event.getEntity()); NPC npc = CitizensAPI.getNPCRegistry().getNPC(event.getEntity());
if (npc == null && event.getEntityType() != EntityType.PLAYER) if (npc == null && event.getEntityType() != EntityType.PLAYER)
return; return;
event.setCancelled(true); event.setCancelled(true);
@ -284,14 +283,14 @@ public class EventListen implements Listener {
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST)
public void onEntitySpawn(CreatureSpawnEvent event) { public void onEntitySpawn(CreatureSpawnEvent event) {
if (event.isCancelled() && npcRegistry.isNPC(event.getEntity())) { if (event.isCancelled() && CitizensAPI.getNPCRegistry().isNPC(event.getEntity())) {
event.setCancelled(false); event.setCancelled(false);
} }
} }
@EventHandler @EventHandler
public void onEntityTarget(EntityTargetEvent event) { public void onEntityTarget(EntityTargetEvent event) {
NPC npc = npcRegistry.getNPC(event.getTarget()); NPC npc = CitizensAPI.getNPCRegistry().getNPC(event.getTarget());
if (npc == null) if (npc == null)
return; return;
event.setCancelled( event.setCancelled(
@ -396,7 +395,7 @@ public class EventListen implements Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onPlayerChangedWorld(PlayerChangedWorldEvent event) { public void onPlayerChangedWorld(PlayerChangedWorldEvent event) {
if (npcRegistry.getNPC(event.getPlayer()) == null) if (CitizensAPI.getNPCRegistry().getNPC(event.getPlayer()) == null)
return; return;
NMS.removeFromServerPlayerList(event.getPlayer()); NMS.removeFromServerPlayerList(event.getPlayer());
// on teleport, player NPCs are added to the server player list. this is // on teleport, player NPCs are added to the server player list. this is
@ -415,14 +414,14 @@ public class EventListen implements Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onPlayerFish(PlayerFishEvent event) { public void onPlayerFish(PlayerFishEvent event) {
if (npcRegistry.isNPC(event.getCaught()) && npcRegistry.getNPC(event.getCaught()).isProtected()) { if (CitizensAPI.getNPCRegistry().isNPC(event.getCaught()) && CitizensAPI.getNPCRegistry().getNPC(event.getCaught()).isProtected()) {
event.setCancelled(true); event.setCancelled(true);
} }
} }
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR) @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) { public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
NPC npc = npcRegistry.getNPC(event.getRightClicked()); NPC npc = CitizensAPI.getNPCRegistry().getNPC(event.getRightClicked());
if (npc == null || Util.isOffHand(event)) { if (npc == null || Util.isOffHand(event)) {
return; return;
} }
@ -438,7 +437,7 @@ public class EventListen implements Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onPlayerLeashEntity(PlayerLeashEntityEvent event) { public void onPlayerLeashEntity(PlayerLeashEntityEvent event) {
NPC npc = npcRegistry.getNPC(event.getEntity()); NPC npc = CitizensAPI.getNPCRegistry().getNPC(event.getEntity());
if (npc == null) { if (npc == null) {
return; return;
} }
@ -458,7 +457,7 @@ public class EventListen implements Listener {
public void onPlayerQuit(PlayerQuitEvent event) { public void onPlayerQuit(PlayerQuitEvent event) {
Editor.leave(event.getPlayer()); Editor.leave(event.getPlayer());
if (event.getPlayer().isInsideVehicle()) { if (event.getPlayer().isInsideVehicle()) {
NPC npc = npcRegistry.getNPC(event.getPlayer().getVehicle()); NPC npc = CitizensAPI.getNPCRegistry().getNPC(event.getPlayer().getVehicle());
if (npc != null) { if (npc != null) {
event.getPlayer().leaveVehicle(); event.getPlayer().leaveVehicle();
} }
@ -474,7 +473,7 @@ public class EventListen implements Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onPlayerTeleport(final PlayerTeleportEvent event) { public void onPlayerTeleport(final PlayerTeleportEvent event) {
if (event.getCause() == TeleportCause.PLUGIN && !event.getPlayer().hasMetadata("citizens-force-teleporting") if (event.getCause() == TeleportCause.PLUGIN && !event.getPlayer().hasMetadata("citizens-force-teleporting")
&& npcRegistry.getNPC(event.getPlayer()) != null && Setting.TELEPORT_DELAY.asInt() > 0) { && CitizensAPI.getNPCRegistry().getNPC(event.getPlayer()) != null && Setting.TELEPORT_DELAY.asInt() > 0) {
event.setCancelled(true); event.setCancelled(true);
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() { Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override @Override
@ -492,7 +491,7 @@ public class EventListen implements Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onPotionSplashEvent(PotionSplashEvent event) { public void onPotionSplashEvent(PotionSplashEvent event) {
for (LivingEntity entity : event.getAffectedEntities()) { for (LivingEntity entity : event.getAffectedEntities()) {
NPC npc = npcRegistry.getNPC(entity); NPC npc = CitizensAPI.getNPCRegistry().getNPC(entity);
if (npc == null) if (npc == null)
continue; continue;
if (npc.isProtected()) { if (npc.isProtected()) {
@ -505,7 +504,7 @@ public class EventListen implements Listener {
public void onProjectileHit(final ProjectileHitEvent event) { public void onProjectileHit(final ProjectileHitEvent event) {
if (!(event.getEntity() instanceof FishHook)) if (!(event.getEntity() instanceof FishHook))
return; return;
NMS.removeHookIfNecessary(npcRegistry, (FishHook) event.getEntity()); NMS.removeHookIfNecessary(CitizensAPI.getNPCRegistry(), (FishHook) event.getEntity());
new BukkitRunnable() { new BukkitRunnable() {
int n = 0; int n = 0;
@ -514,14 +513,14 @@ public class EventListen implements Listener {
if (n++ > 5) { if (n++ > 5) {
cancel(); cancel();
} }
NMS.removeHookIfNecessary(npcRegistry, (FishHook) event.getEntity()); NMS.removeHookIfNecessary(CitizensAPI.getNPCRegistry(), (FishHook) event.getEntity());
} }
}.runTaskTimer(CitizensAPI.getPlugin(), 0, 1); }.runTaskTimer(CitizensAPI.getPlugin(), 0, 1);
} }
@EventHandler @EventHandler
public void onVehicleDestroy(VehicleDestroyEvent event) { public void onVehicleDestroy(VehicleDestroyEvent event) {
NPC npc = npcRegistry.getNPC(event.getVehicle()); NPC npc = CitizensAPI.getNPCRegistry().getNPC(event.getVehicle());
if (npc == null) { if (npc == null) {
return; return;
} }
@ -530,9 +529,9 @@ public class EventListen implements Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onVehicleEnter(final VehicleEnterEvent event) { public void onVehicleEnter(final VehicleEnterEvent event) {
if (!npcRegistry.isNPC(event.getVehicle())) if (!CitizensAPI.getNPCRegistry().isNPC(event.getVehicle()))
return; return;
NPC npc = npcRegistry.getNPC(event.getVehicle()); NPC npc = CitizensAPI.getNPCRegistry().getNPC(event.getVehicle());
if ((Util.isHorse(npc.getEntity()) || npc.getEntity().getType() == EntityType.BOAT if ((Util.isHorse(npc.getEntity()) || npc.getEntity().getType() == EntityType.BOAT
|| npc.getEntity().getType() == EntityType.PIG || npc.getEntity() instanceof Minecart) || npc.getEntity().getType() == EntityType.PIG || npc.getEntity() instanceof Minecart)
&& (!npc.hasTrait(Controllable.class) || !npc.getTrait(Controllable.class).isEnabled())) { && (!npc.hasTrait(Controllable.class) || !npc.getTrait(Controllable.class).isEnabled())) {

View File

@ -99,11 +99,9 @@ import net.citizensnpcs.util.Util;
@Requirements(selected = true, ownership = true) @Requirements(selected = true, ownership = true)
public class NPCCommands { public class NPCCommands {
private final NPCRegistry npcRegistry;
private final NPCSelector selector; private final NPCSelector selector;
public NPCCommands(Citizens plugin) { public NPCCommands(Citizens plugin) {
npcRegistry = CitizensAPI.getNPCRegistry();
selector = plugin.getNPCSelector(); selector = plugin.getNPCSelector();
} }
@ -373,7 +371,7 @@ public class NPCCommands {
&& !sender.hasPermission("citizens.npc.create." + type.name().toLowerCase().replace("_", ""))) && !sender.hasPermission("citizens.npc.create." + type.name().toLowerCase().replace("_", "")))
throw new NoPermissionsException(); throw new NoPermissionsException();
npc = npcRegistry.createNPC(type, name); npc = CitizensAPI.getNPCRegistry().createNPC(type, name);
String msg = "You created [[" + npc.getName() + "]]"; String msg = "You created [[" + npc.getName() + "]]";
int age = 0; int age = 0;
@ -486,7 +484,7 @@ public class NPCCommands {
if (args.argsLength() < 2) { if (args.argsLength() < 2) {
throw new CommandException(Messages.COMMAND_MUST_HAVE_SELECTED); throw new CommandException(Messages.COMMAND_MUST_HAVE_SELECTED);
} }
NPCCommandSelector.startWithCallback(callback, npcRegistry, sender, args, args.getString(1)); NPCCommandSelector.startWithCallback(callback, CitizensAPI.getNPCRegistry(), sender, args, args.getString(1));
} else { } else {
callback.run(npc); callback.run(npc);
} }
@ -752,7 +750,7 @@ public class NPCCommands {
@Requirements @Requirements
public void list(CommandContext args, CommandSender sender, NPC npc) throws CommandException { public void list(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
NPCRegistry source = args.hasValueFlag("registry") ? CitizensAPI.getNamedNPCRegistry(args.getFlag("registry")) NPCRegistry source = args.hasValueFlag("registry") ? CitizensAPI.getNamedNPCRegistry(args.getFlag("registry"))
: npcRegistry; : CitizensAPI.getNPCRegistry();
if (source == null) if (source == null)
throw new CommandException(); throw new CommandException();
List<NPC> npcs = new ArrayList<NPC>(); List<NPC> npcs = new ArrayList<NPC>();
@ -1297,7 +1295,7 @@ public class NPCCommands {
public void remove(final CommandContext args, final CommandSender sender, NPC npc) throws CommandException { public void remove(final CommandContext args, final CommandSender sender, NPC npc) throws CommandException {
if (args.hasValueFlag("owner")) { if (args.hasValueFlag("owner")) {
String owner = args.getFlag("owner"); String owner = args.getFlag("owner");
Collection<NPC> npcs = Lists.newArrayList(npcRegistry); Collection<NPC> npcs = Lists.newArrayList(CitizensAPI.getNPCRegistry());
for (NPC o : npcs) { for (NPC o : npcs) {
if (o.getTrait(Owner.class).isOwnedBy(owner)) { if (o.getTrait(Owner.class).isOwnedBy(owner)) {
o.destroy(); o.destroy();
@ -1310,7 +1308,7 @@ public class NPCCommands {
if (args.getString(1).equalsIgnoreCase("all")) { if (args.getString(1).equalsIgnoreCase("all")) {
if (!sender.hasPermission("citizens.admin.remove.all") && !sender.hasPermission("citizens.admin")) if (!sender.hasPermission("citizens.admin.remove.all") && !sender.hasPermission("citizens.admin"))
throw new NoPermissionsException(); throw new NoPermissionsException();
npcRegistry.deregisterAll(); CitizensAPI.getNPCRegistry().deregisterAll();
Messaging.sendTr(sender, Messages.REMOVED_ALL_NPCS); Messaging.sendTr(sender, Messages.REMOVED_ALL_NPCS);
return; return;
} else { } else {
@ -1327,7 +1325,7 @@ public class NPCCommands {
Messaging.sendTr(sender, Messages.NPC_REMOVED, npc.getName()); Messaging.sendTr(sender, Messages.NPC_REMOVED, npc.getName());
} }
}; };
NPCCommandSelector.startWithCallback(callback, npcRegistry, sender, args, args.getString(1)); NPCCommandSelector.startWithCallback(callback, CitizensAPI.getNPCRegistry(), sender, args, args.getString(1));
return; return;
} }
} }
@ -1447,14 +1445,14 @@ public class NPCCommands {
} }
}); });
for (Entity possibleNPC : search) { for (Entity possibleNPC : search) {
NPC test = npcRegistry.getNPC(possibleNPC); NPC test = CitizensAPI.getNPCRegistry().getNPC(possibleNPC);
if (test == null) if (test == null)
continue; continue;
callback.run(test); callback.run(test);
break; break;
} }
} else { } else {
NPCCommandSelector.startWithCallback(callback, npcRegistry, sender, args, args.getString(1)); NPCCommandSelector.startWithCallback(callback, CitizensAPI.getNPCRegistry(), sender, args, args.getString(1));
} }
} }
@ -1698,7 +1696,7 @@ public class NPCCommands {
} }
}; };
if (args.argsLength() > 1) { if (args.argsLength() > 1) {
NPCCommandSelector.startWithCallback(callback, npcRegistry, sender, args, args.getString(1)); NPCCommandSelector.startWithCallback(callback, CitizensAPI.getNPCRegistry(), sender, args, args.getString(1));
} else { } else {
callback.run(npc); callback.run(npc);
} }

View File

@ -37,7 +37,6 @@ import net.citizensnpcs.util.Util;
*/ */
public class SkinUpdateTracker { public class SkinUpdateTracker {
private final Map<SkinnableEntity, Void> navigating = new WeakHashMap<SkinnableEntity, Void>(25); private final Map<SkinnableEntity, Void> navigating = new WeakHashMap<SkinnableEntity, Void>(25);
private final NPCRegistry npcRegistry;
private final Map<UUID, PlayerTracker> playerTrackers = new HashMap<UUID, PlayerTracker>( private final Map<UUID, PlayerTracker> playerTrackers = new HashMap<UUID, PlayerTracker>(
Bukkit.getMaxPlayers() / 2); Bukkit.getMaxPlayers() / 2);
private final Map<String, NPCRegistry> registries; private final Map<String, NPCRegistry> registries;
@ -46,16 +45,12 @@ public class SkinUpdateTracker {
/** /**
* Constructor. * Constructor.
* *
* @param npcRegistry
* The primary citizens registry.
* @param registries * @param registries
* Map of other registries. * Map of other registries.
*/ */
public SkinUpdateTracker(NPCRegistry npcRegistry, Map<String, NPCRegistry> registries) { public SkinUpdateTracker(Map<String, NPCRegistry> registries) {
Preconditions.checkNotNull(npcRegistry);
Preconditions.checkNotNull(registries); Preconditions.checkNotNull(registries);
this.npcRegistry = npcRegistry;
this.registries = registries; this.registries = registries;
updater.runTaskTimer(CitizensAPI.getPlugin(), 1, 1); updater.runTaskTimer(CitizensAPI.getPlugin(), 1, 1);
@ -109,7 +104,7 @@ public class SkinUpdateTracker {
} }
private Iterable<NPC> getAllNPCs() { private Iterable<NPC> getAllNPCs() {
return Iterables.filter(Iterables.concat(npcRegistry, Iterables.concat(registries.values())), return Iterables.filter(Iterables.concat(CitizensAPI.getNPCRegistry(), Iterables.concat(registries.values())),
Predicates.notNull()); Predicates.notNull());
} }