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;
public class EventListen implements Listener {
private final NPCRegistry npcRegistry = CitizensAPI.getNPCRegistry();
private final Map<String, NPCRegistry> registries;
private final SkinUpdateTracker skinUpdateTracker;
private final ListMultimap<ChunkCoord, NPC> toRespawn = ArrayListMultimap.create();
EventListen(Map<String, NPCRegistry> registries) {
this.registries = registries;
this.skinUpdateTracker = new SkinUpdateTracker(npcRegistry, registries);
this.skinUpdateTracker = new SkinUpdateTracker(registries);
}
private void checkCreationEvent(CommandSenderCreateNPCEvent event) {
@ -121,7 +120,7 @@ public class EventListen implements Listener {
if (limit < 0)
return;
int owned = 0;
for (NPC npc : npcRegistry) {
for (NPC npc : CitizensAPI.getNPCRegistry()) {
if (!event.getNPC().equals(npc) && npc.hasTrait(Owner.class)
&& npc.getTrait(Owner.class).isOwnedBy(event.getCreator())) {
owned++;
@ -135,7 +134,7 @@ public class EventListen implements Listener {
}
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());
}
@ -202,11 +201,11 @@ public class EventListen implements Listener {
@EventHandler
public void onEntityDamage(EntityDamageEvent event) {
NPC npc = npcRegistry.getNPC(event.getEntity());
NPC npc = CitizensAPI.getNPCRegistry().getNPC(event.getEntity());
if (npc == null) {
if (event instanceof EntityDamageByEntityEvent) {
npc = npcRegistry.getNPC(((EntityDamageByEntityEvent) event).getDamager());
npc = CitizensAPI.getNPCRegistry().getNPC(((EntityDamageByEntityEvent) event).getDamager());
if (npc == null)
return;
event.setCancelled(!npc.data().get(NPC.DAMAGE_OTHERS_METADATA, true));
@ -235,7 +234,7 @@ public class EventListen implements Listener {
@EventHandler(ignoreCancelled = true)
public void onEntityDeath(EntityDeathEvent event) {
final NPC npc = npcRegistry.getNPC(event.getEntity());
final NPC npc = CitizensAPI.getNPCRegistry().getNPC(event.getEntity());
if (npc == null) {
return;
}
@ -273,7 +272,7 @@ public class EventListen implements Listener {
@EventHandler
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)
return;
event.setCancelled(true);
@ -284,14 +283,14 @@ public class EventListen implements Listener {
@EventHandler(priority = EventPriority.HIGHEST)
public void onEntitySpawn(CreatureSpawnEvent event) {
if (event.isCancelled() && npcRegistry.isNPC(event.getEntity())) {
if (event.isCancelled() && CitizensAPI.getNPCRegistry().isNPC(event.getEntity())) {
event.setCancelled(false);
}
}
@EventHandler
public void onEntityTarget(EntityTargetEvent event) {
NPC npc = npcRegistry.getNPC(event.getTarget());
NPC npc = CitizensAPI.getNPCRegistry().getNPC(event.getTarget());
if (npc == null)
return;
event.setCancelled(
@ -396,7 +395,7 @@ public class EventListen implements Listener {
@EventHandler(ignoreCancelled = true)
public void onPlayerChangedWorld(PlayerChangedWorldEvent event) {
if (npcRegistry.getNPC(event.getPlayer()) == null)
if (CitizensAPI.getNPCRegistry().getNPC(event.getPlayer()) == null)
return;
NMS.removeFromServerPlayerList(event.getPlayer());
// 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)
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);
}
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
NPC npc = npcRegistry.getNPC(event.getRightClicked());
NPC npc = CitizensAPI.getNPCRegistry().getNPC(event.getRightClicked());
if (npc == null || Util.isOffHand(event)) {
return;
}
@ -438,7 +437,7 @@ public class EventListen implements Listener {
@EventHandler(ignoreCancelled = true)
public void onPlayerLeashEntity(PlayerLeashEntityEvent event) {
NPC npc = npcRegistry.getNPC(event.getEntity());
NPC npc = CitizensAPI.getNPCRegistry().getNPC(event.getEntity());
if (npc == null) {
return;
}
@ -458,7 +457,7 @@ public class EventListen implements Listener {
public void onPlayerQuit(PlayerQuitEvent event) {
Editor.leave(event.getPlayer());
if (event.getPlayer().isInsideVehicle()) {
NPC npc = npcRegistry.getNPC(event.getPlayer().getVehicle());
NPC npc = CitizensAPI.getNPCRegistry().getNPC(event.getPlayer().getVehicle());
if (npc != null) {
event.getPlayer().leaveVehicle();
}
@ -474,7 +473,7 @@ public class EventListen implements Listener {
@EventHandler(ignoreCancelled = true)
public void onPlayerTeleport(final PlayerTeleportEvent event) {
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);
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
@ -492,7 +491,7 @@ public class EventListen implements Listener {
@EventHandler(ignoreCancelled = true)
public void onPotionSplashEvent(PotionSplashEvent event) {
for (LivingEntity entity : event.getAffectedEntities()) {
NPC npc = npcRegistry.getNPC(entity);
NPC npc = CitizensAPI.getNPCRegistry().getNPC(entity);
if (npc == null)
continue;
if (npc.isProtected()) {
@ -505,7 +504,7 @@ public class EventListen implements Listener {
public void onProjectileHit(final ProjectileHitEvent event) {
if (!(event.getEntity() instanceof FishHook))
return;
NMS.removeHookIfNecessary(npcRegistry, (FishHook) event.getEntity());
NMS.removeHookIfNecessary(CitizensAPI.getNPCRegistry(), (FishHook) event.getEntity());
new BukkitRunnable() {
int n = 0;
@ -514,14 +513,14 @@ public class EventListen implements Listener {
if (n++ > 5) {
cancel();
}
NMS.removeHookIfNecessary(npcRegistry, (FishHook) event.getEntity());
NMS.removeHookIfNecessary(CitizensAPI.getNPCRegistry(), (FishHook) event.getEntity());
}
}.runTaskTimer(CitizensAPI.getPlugin(), 0, 1);
}
@EventHandler
public void onVehicleDestroy(VehicleDestroyEvent event) {
NPC npc = npcRegistry.getNPC(event.getVehicle());
NPC npc = CitizensAPI.getNPCRegistry().getNPC(event.getVehicle());
if (npc == null) {
return;
}
@ -530,9 +529,9 @@ public class EventListen implements Listener {
@EventHandler(ignoreCancelled = true)
public void onVehicleEnter(final VehicleEnterEvent event) {
if (!npcRegistry.isNPC(event.getVehicle()))
if (!CitizensAPI.getNPCRegistry().isNPC(event.getVehicle()))
return;
NPC npc = npcRegistry.getNPC(event.getVehicle());
NPC npc = CitizensAPI.getNPCRegistry().getNPC(event.getVehicle());
if ((Util.isHorse(npc.getEntity()) || npc.getEntity().getType() == EntityType.BOAT
|| npc.getEntity().getType() == EntityType.PIG || npc.getEntity() instanceof Minecart)
&& (!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)
public class NPCCommands {
private final NPCRegistry npcRegistry;
private final NPCSelector selector;
public NPCCommands(Citizens plugin) {
npcRegistry = CitizensAPI.getNPCRegistry();
selector = plugin.getNPCSelector();
}
@ -373,7 +371,7 @@ public class NPCCommands {
&& !sender.hasPermission("citizens.npc.create." + type.name().toLowerCase().replace("_", "")))
throw new NoPermissionsException();
npc = npcRegistry.createNPC(type, name);
npc = CitizensAPI.getNPCRegistry().createNPC(type, name);
String msg = "You created [[" + npc.getName() + "]]";
int age = 0;
@ -486,7 +484,7 @@ public class NPCCommands {
if (args.argsLength() < 2) {
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 {
callback.run(npc);
}
@ -752,7 +750,7 @@ public class NPCCommands {
@Requirements
public void list(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
NPCRegistry source = args.hasValueFlag("registry") ? CitizensAPI.getNamedNPCRegistry(args.getFlag("registry"))
: npcRegistry;
: CitizensAPI.getNPCRegistry();
if (source == null)
throw new CommandException();
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 {
if (args.hasValueFlag("owner")) {
String owner = args.getFlag("owner");
Collection<NPC> npcs = Lists.newArrayList(npcRegistry);
Collection<NPC> npcs = Lists.newArrayList(CitizensAPI.getNPCRegistry());
for (NPC o : npcs) {
if (o.getTrait(Owner.class).isOwnedBy(owner)) {
o.destroy();
@ -1310,7 +1308,7 @@ public class NPCCommands {
if (args.getString(1).equalsIgnoreCase("all")) {
if (!sender.hasPermission("citizens.admin.remove.all") && !sender.hasPermission("citizens.admin"))
throw new NoPermissionsException();
npcRegistry.deregisterAll();
CitizensAPI.getNPCRegistry().deregisterAll();
Messaging.sendTr(sender, Messages.REMOVED_ALL_NPCS);
return;
} else {
@ -1327,7 +1325,7 @@ public class NPCCommands {
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;
}
}
@ -1447,14 +1445,14 @@ public class NPCCommands {
}
});
for (Entity possibleNPC : search) {
NPC test = npcRegistry.getNPC(possibleNPC);
NPC test = CitizensAPI.getNPCRegistry().getNPC(possibleNPC);
if (test == null)
continue;
callback.run(test);
break;
}
} 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) {
NPCCommandSelector.startWithCallback(callback, npcRegistry, sender, args, args.getString(1));
NPCCommandSelector.startWithCallback(callback, CitizensAPI.getNPCRegistry(), sender, args, args.getString(1));
} else {
callback.run(npc);
}

View File

@ -37,7 +37,6 @@ import net.citizensnpcs.util.Util;
*/
public class SkinUpdateTracker {
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>(
Bukkit.getMaxPlayers() / 2);
private final Map<String, NPCRegistry> registries;
@ -46,16 +45,12 @@ public class SkinUpdateTracker {
/**
* Constructor.
*
* @param npcRegistry
* The primary citizens registry.
* @param registries
* Map of other registries.
*/
public SkinUpdateTracker(NPCRegistry npcRegistry, Map<String, NPCRegistry> registries) {
Preconditions.checkNotNull(npcRegistry);
public SkinUpdateTracker(Map<String, NPCRegistry> registries) {
Preconditions.checkNotNull(registries);
this.npcRegistry = npcRegistry;
this.registries = registries;
updater.runTaskTimer(CitizensAPI.getPlugin(), 1, 1);
@ -109,7 +104,7 @@ public class SkinUpdateTracker {
}
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());
}