Skip to content

Commit

Permalink
refactor(api): dynamically fetch the current default npc registry (#1672
Browse files Browse the repository at this point in the history
)

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

#1671
  • Loading branch information
Silthus authored and fullwall committed Jan 18, 2019
1 parent 5447561 commit 52b3902
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 40 deletions.
43 changes: 21 additions & 22 deletions main/src/main/java/net/citizensnpcs/EventListen.java
Expand Up @@ -97,14 +97,13 @@
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) {
Expand All @@ -121,7 +120,7 @@ private void checkCreationEvent(CommandSenderCreateNPCEvent event) {
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++;
Expand All @@ -135,7 +134,7 @@ private void checkCreationEvent(CommandSenderCreateNPCEvent event) {
}

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());
}

Expand Down Expand Up @@ -202,11 +201,11 @@ public void onEntityCombust(EntityCombustEvent event) {

@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));
Expand Down Expand Up @@ -235,7 +234,7 @@ public void onEntityDamage(EntityDamageEvent event) {

@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;
}
Expand Down Expand Up @@ -273,7 +272,7 @@ public void run() {

@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);
Expand All @@ -284,14 +283,14 @@ public void onEntityPortal(EntityPortalEvent event) {

@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(
Expand Down Expand Up @@ -396,7 +395,7 @@ public void onNPCSpawn(NPCSpawnEvent event) {

@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
Expand All @@ -415,14 +414,14 @@ public void onPlayerCreateNPC(PlayerCreateNPCEvent event) {

@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;
}
Expand All @@ -438,7 +437,7 @@ public void onPlayerJoin(PlayerJoinEvent event) {

@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;
}
Expand All @@ -458,7 +457,7 @@ public void onPlayerMove(final PlayerMoveEvent event) {
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();
}
Expand All @@ -474,7 +473,7 @@ public void onPlayerRespawn(PlayerRespawnEvent event) {
@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
Expand All @@ -492,7 +491,7 @@ public void run() {
@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()) {
Expand All @@ -505,7 +504,7 @@ public void onPotionSplashEvent(PotionSplashEvent event) {
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;

Expand All @@ -514,14 +513,14 @@ public void run() {
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;
}
Expand All @@ -530,9 +529,9 @@ public void onVehicleDestroy(VehicleDestroyEvent event) {

@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())) {
Expand Down
20 changes: 9 additions & 11 deletions main/src/main/java/net/citizensnpcs/commands/NPCCommands.java
Expand Up @@ -99,11 +99,9 @@

@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();
}

Expand Down Expand Up @@ -373,7 +371,7 @@ public void create(CommandContext args, CommandSender sender, NPC npc) throws Co
&& !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;
Expand Down Expand Up @@ -486,7 +484,7 @@ public void run(NPC npc) throws CommandException {
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);
}
Expand Down Expand Up @@ -752,7 +750,7 @@ public void leashable(CommandContext args, CommandSender sender, NPC npc) {
@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>();
Expand Down Expand Up @@ -1297,7 +1295,7 @@ public void rabbitType(CommandContext args, CommandSender sender, NPC npc) throw
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();
Expand All @@ -1310,7 +1308,7 @@ public void remove(final CommandContext args, final CommandSender sender, NPC np
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 {
Expand All @@ -1327,7 +1325,7 @@ public void run(NPC npc) throws CommandException {
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;
}
}
Expand Down Expand Up @@ -1447,14 +1445,14 @@ public int compare(Entity o1, Entity o2) {
}
});
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));
}
}

Expand Down Expand Up @@ -1698,7 +1696,7 @@ public void run(NPC respawn) throws CommandException {
}
};
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);
}
Expand Down
Expand Up @@ -37,7 +37,6 @@
*/
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;
Expand All @@ -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);
Expand Down Expand Up @@ -109,7 +104,7 @@ private boolean canSee(Player player, SkinnableEntity skinnable, boolean checkFo
}

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());
}

Expand Down

0 comments on commit 52b3902

Please sign in to comment.