Skip to content

Commit

Permalink
Move EntitiesLoadEvent to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Aug 5, 2022
1 parent 972be82 commit 7bbe87b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
34 changes: 10 additions & 24 deletions main/src/main/java/net/citizensnpcs/EventListen.java
Expand Up @@ -50,7 +50,6 @@
import org.bukkit.event.world.ChunkEvent;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent;
import org.bukkit.event.world.EntitiesLoadEvent;
import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.event.world.WorldUnloadEvent;
import org.bukkit.inventory.meta.SkullMeta;
Expand Down Expand Up @@ -111,13 +110,20 @@
import net.citizensnpcs.util.Util;

public class EventListen implements Listener {
private EventListenChunk chunkEventListener;
private final Map<String, NPCRegistry> registries;
private final SkinUpdateTracker skinUpdateTracker;
private final ListMultimap<ChunkCoord, NPC> toRespawn = ArrayListMultimap.create(64, 4);

EventListen(Map<String, NPCRegistry> registries) {
this.registries = registries;
this.skinUpdateTracker = new SkinUpdateTracker(registries);
try {
this.chunkEventListener = new EventListenChunk(this);
Bukkit.getPluginManager().registerEvents(chunkEventListener, CitizensAPI.getPlugin());
} catch (Throwable ex) {
this.chunkEventListener = null;
}
}

private void checkCreationEvent(CommandSenderCreateNPCEvent event) {
Expand Down Expand Up @@ -153,7 +159,7 @@ Iterables.<NPC> concat(CitizensAPI.getNPCRegistry(), Iterables.concat(registries
Predicates.notNull());
}

private void loadNPCs(ChunkEvent event) {
void loadNPCs(ChunkEvent event) {
ChunkCoord coord = new ChunkCoord(event.getChunk());
Runnable runnable = new Runnable() {
@Override
Expand All @@ -173,9 +179,8 @@ public void run() {

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onChunkLoad(ChunkLoadEvent event) {
if (usingEntitiesLoadEvents())
if (chunkEventListener != null)
return;

loadNPCs(event);
}

Expand Down Expand Up @@ -241,11 +246,6 @@ public void onCommandSenderCreateNPC(CommandSenderCreateNPCEvent event) {
checkCreationEvent(event);
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntitiesLoad(EntitiesLoadEvent event) {
loadNPCs(event);
}

/*
* Entity events
*/
Expand Down Expand Up @@ -746,18 +746,4 @@ private boolean spawn(NPC npc) {
}
return npc.spawn(spawn, SpawnReason.CHUNK_LOAD);
}

private static boolean usingEntitiesLoadEvents() {
if (USING_ENTITIES_LOAD == null) {
try {
Class.forName("org.bukkit.event.world.EntitiesLoadEvent");
USING_ENTITIES_LOAD = true;
} catch (ClassNotFoundException swallow) {
USING_ENTITIES_LOAD = false;
}
}
return USING_ENTITIES_LOAD;
}

private static Boolean USING_ENTITIES_LOAD;
}
}
19 changes: 19 additions & 0 deletions main/src/main/java/net/citizensnpcs/EventListenChunk.java
@@ -0,0 +1,19 @@
package net.citizensnpcs;

import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.world.EntitiesLoadEvent;

public class EventListenChunk implements Listener {
EventListen listen;

EventListenChunk(EventListen listen) {
this.listen = listen;
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntitiesLoad(EntitiesLoadEvent event) {
listen.loadNPCs(event);
}
}

0 comments on commit 7bbe87b

Please sign in to comment.