change the ludicrous 1-tick delay for npc distance checks, fix useless cache miss errors

This commit is contained in:
Pyr 2023-04-19 20:37:02 +01:00
parent 8762d640f5
commit 4f6048c058
7 changed files with 17 additions and 13 deletions

@ -140,16 +140,20 @@ public interface TypeCache {
} }
public T load() { public T load() {
return load(false);
}
public T load(boolean missAllowed) {
if (this.loaded) return this.cached; if (this.loaded) return this.cached;
try { try {
if (this.BUILDER_CLASS == null) throw new IllegalStateException("can't find class for: " + this.cacheBuilder.className); if (this.BUILDER_CLASS == null) throw new ClassNotFoundException("No class found: " + this.cacheBuilder.className.toString());
T eval = (this.cached != null) ? this.cached : (this.cached = onLoad()); T eval = (this.cached != null) ? this.cached : (this.cached = onLoad());
if (eval == null) throw new NullPointerException(); if (eval == null) throw new NullPointerException();
} catch (Throwable throwable) { } catch (Throwable throwable) {
throwable.printStackTrace(); if (!missAllowed) {
if (throwable instanceof IllegalStateException) log("No cache found for: " + this.cacheBuilder.className); log("Cache for class failed to load due to the following exception: " + this.cacheBuilder.className);
log("No cache found for: " + this.cacheBuilder.className + " : " + this.cacheBuilder.methods.toString()); throwable.printStackTrace();
log("Skipping cache for " + this.cacheBuilder.className); }
} }
this.loaded = true; this.loaded = true;
return this.cached; return this.cached;

@ -201,8 +201,7 @@ public class NPC {
} }
public synchronized void spawn(ZUser user) { public synchronized void spawn(ZUser user) {
if (this.viewers.contains(user)) if (this.viewers.contains(user)) throw new IllegalStateException(user.getUUID().toString() + " is already a viewer.");
throw new IllegalStateException(user.getUUID().toString() + " is already a viewer.");
try { try {
this.viewers.add(user); this.viewers.add(user);
boolean npcIsPlayer = (this.npcPojo.getNpcType() == NPCType.PLAYER); boolean npcIsPlayer = (this.npcPojo.getNpcType() == NPCType.PLAYER);

@ -11,7 +11,7 @@ public class PacketV19 extends PacketV18 {
public Object getPlayerPacket(Object nmsWorld, GameProfile gameProfile) throws ReflectiveOperationException { public Object getPlayerPacket(Object nmsWorld, GameProfile gameProfile) throws ReflectiveOperationException {
try { try {
return CacheRegistry.PLAYER_CONSTRUCTOR_NEW_1.load().newInstance(CacheRegistry.GET_SERVER_METHOD.load().invoke(Bukkit.getServer()), nmsWorld, gameProfile, null); return CacheRegistry.PLAYER_CONSTRUCTOR_NEW_1.load(true).newInstance(CacheRegistry.GET_SERVER_METHOD.load().invoke(Bukkit.getServer()), nmsWorld, gameProfile, null);
} catch (Throwable e) { } catch (Throwable e) {
return CacheRegistry.PLAYER_CONSTRUCTOR_NEW_2.load().newInstance(CacheRegistry.GET_SERVER_METHOD.load().invoke(Bukkit.getServer()), nmsWorld, gameProfile); return CacheRegistry.PLAYER_CONSTRUCTOR_NEW_2.load().newInstance(CacheRegistry.GET_SERVER_METHOD.load().invoke(Bukkit.getServer()), nmsWorld, gameProfile);
} }

@ -39,7 +39,7 @@ public class PacketV8 implements Packet {
public Object getMetadataPacket(int entityId, Object nmsEntity) throws ReflectiveOperationException { public Object getMetadataPacket(int entityId, Object nmsEntity) throws ReflectiveOperationException {
Object dataWatcher = CacheRegistry.GET_DATA_WATCHER_METHOD.load().invoke(nmsEntity); Object dataWatcher = CacheRegistry.GET_DATA_WATCHER_METHOD.load().invoke(nmsEntity);
try { try {
return CacheRegistry.PACKET_PLAY_OUT_ENTITY_META_DATA_CONSTRUCTOR.load().newInstance(entityId, dataWatcher, true); return CacheRegistry.PACKET_PLAY_OUT_ENTITY_META_DATA_CONSTRUCTOR.load(true).newInstance(entityId, dataWatcher, true);
} catch (Exception e2) { } catch (Exception e2) {
return CacheRegistry.PACKET_PLAY_OUT_ENTITY_META_DATA_CONSTRUCTOR_V1.load().newInstance(entityId, CacheRegistry.GET_DATAWATCHER_B_LIST.load().invoke(dataWatcher)); return CacheRegistry.PACKET_PLAY_OUT_ENTITY_META_DATA_CONSTRUCTOR_V1.load().newInstance(entityId, CacheRegistry.GET_DATAWATCHER_B_LIST.load().invoke(dataWatcher));
} }

@ -22,8 +22,7 @@ public class NPCLoadTask extends BukkitRunnable {
return; return;
} }
World world = Bukkit.getWorld(this.npc.getNpcPojo().getLocation().getWorldName()); World world = Bukkit.getWorld(this.npc.getNpcPojo().getLocation().getWorldName());
if (world == null) if (world == null) return;
return;
cancel(); cancel();
this.npc.onLoad(); this.npc.onLoad();
} }

@ -12,16 +12,17 @@ import org.bukkit.scheduler.BukkitRunnable;
public class NPCManagerTask extends BukkitRunnable { public class NPCManagerTask extends BukkitRunnable {
public NPCManagerTask(Plugin serversNPC) { public NPCManagerTask(Plugin serversNPC) {
runTaskTimerAsynchronously(serversNPC, 60L, 1L); runTaskTimerAsynchronously(serversNPC, 60L, 10L);
} }
public void run() { public void run() {
int distSq = ConfigurationConstants.VIEW_DISTANCE * ConfigurationConstants.VIEW_DISTANCE;
for (NPC npc : NPC.all()) { for (NPC npc : NPC.all()) {
boolean hasPath = (npc.getNpcPath() != null); boolean hasPath = (npc.getNpcPath() != null);
if (hasPath) npc.getNpcPath().handle(); if (hasPath) npc.getNpcPath().handle();
for (Player player : Bukkit.getOnlinePlayers()) { for (Player player : Bukkit.getOnlinePlayers()) {
ZUser zUser = ZUser.find(player); ZUser zUser = ZUser.find(player);
boolean canSeeNPC = (player.getWorld() == npc.getLocation().getWorld() && player.getLocation().distance(npc.getLocation()) <= ConfigurationConstants.VIEW_DISTANCE); boolean canSeeNPC = (player.getWorld() == npc.getLocation().getWorld() && player.getLocation().distanceSquared(npc.getLocation()) <= distSq);
if (npc.getViewers().contains(zUser) && !canSeeNPC) { if (npc.getViewers().contains(zUser) && !canSeeNPC) {
npc.delete(zUser); npc.delete(zUser);
continue; continue;

@ -1,5 +1,6 @@
name: ZNPCsPlus name: ZNPCsPlus
main: lol.pyr.znpcsplus.ZNPCsPlus main: lol.pyr.znpcsplus.ZNPCsPlus
load: POSTWORLD
version: ${version} version: ${version}
api-version: 1.13 api-version: 1.13
softdepend: [ PlaceholderAPI ] softdepend: [ PlaceholderAPI ]