From fc8c6b88715fdbad8940fbc37f7ee83a2faa9abd Mon Sep 17 00:00:00 2001 From: Esmorall Date: Tue, 3 Sep 2019 10:44:41 -0300 Subject: [PATCH] fixed npe --- .../EntityTrackerFixer.java | 2 +- .../entitytrackerfixer/UntrackerTask.java | 26 +++++++++---------- .../util/ReflectionUtils.java | 7 +++++ 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/EntityTrackerFixer/src/net/minemora/entitytrackerfixer/EntityTrackerFixer.java b/EntityTrackerFixer/src/net/minemora/entitytrackerfixer/EntityTrackerFixer.java index 6354c9f..54be6d7 100644 --- a/EntityTrackerFixer/src/net/minemora/entitytrackerfixer/EntityTrackerFixer.java +++ b/EntityTrackerFixer/src/net/minemora/entitytrackerfixer/EntityTrackerFixer.java @@ -13,7 +13,7 @@ public class EntityTrackerFixer extends JavaPlugin { public void onEnable() { plugin = this; ConfigMain.getInstance().setup(this); - new UntrackerTask().runTaskTimerAsynchronously(this, ConfigMain.getUntrackTicks(), ConfigMain.getUntrackTicks()); + new UntrackerTask().runTaskTimer(this, ConfigMain.getUntrackTicks(), ConfigMain.getUntrackTicks()); new CheckTask().runTaskTimerAsynchronously(this, ConfigMain.getUntrackTicks() + 1, ConfigMain.getCheckFrequency()); getServer().getPluginManager().registerEvents(new ChunkEventListener(), this); } diff --git a/EntityTrackerFixer/src/net/minemora/entitytrackerfixer/UntrackerTask.java b/EntityTrackerFixer/src/net/minemora/entitytrackerfixer/UntrackerTask.java index 3b13c7c..46997b4 100644 --- a/EntityTrackerFixer/src/net/minemora/entitytrackerfixer/UntrackerTask.java +++ b/EntityTrackerFixer/src/net/minemora/entitytrackerfixer/UntrackerTask.java @@ -1,8 +1,11 @@ package net.minemora.entitytrackerfixer; +import java.lang.reflect.Field; +import java.util.HashSet; +import java.util.Set; + import org.bukkit.Bukkit; import org.bukkit.Location; -import org.bukkit.craftbukkit.libs.it.unimi.dsi.fastutil.objects.ObjectIterator; import org.bukkit.craftbukkit.v1_14_R1.CraftWorld; import org.bukkit.scheduler.BukkitRunnable; @@ -39,21 +42,15 @@ public class UntrackerTask extends BukkitRunnable { return; } //Set toRemove = new HashSet<>(); + Set toRemove = new HashSet<>(); int removed = 0; WorldServer ws = ((CraftWorld)Bukkit.getWorld(worldName)).getHandle(); ChunkProviderServer cps = ws.getChunkProvider(); - ObjectIterator objectiterator = cps.playerChunkMap.trackedEntities.values().iterator(); try { - while (objectiterator.hasNext()) { - Object iterobj = objectiterator.next(); - if(iterobj == null) { - objectiterator.remove(); - continue; - } - EntityTracker et = (EntityTracker) iterobj; - net.minecraft.server.v1_14_R1.Entity nmsEnt = (net.minecraft.server.v1_14_R1.Entity) - ReflectionUtils.getPrivateField(et.getClass(), et, "tracker"); + Field field = ReflectionUtils.getClassPrivateField(EntityTracker.class, "tracker"); + for(EntityTracker et : cps.playerChunkMap.trackedEntities.values()) { + net.minecraft.server.v1_14_R1.Entity nmsEnt = (net.minecraft.server.v1_14_R1.Entity) field.get(et); if(nmsEnt instanceof EntityPlayer) { continue; } @@ -80,8 +77,7 @@ public class UntrackerTask extends BukkitRunnable { } if(remove) { //System.out.println("untracked: " + nmsEnt.getBukkitEntity().getType().name()); - //toRemove.add(nmsEnt); - objectiterator.remove(); + toRemove.add(nmsEnt.getId()); removed++; UntrackedEntitiesCache.getInstance().add(nmsEnt); } @@ -90,6 +86,10 @@ public class UntrackerTask extends BukkitRunnable { e.printStackTrace(); } + for(int id : toRemove) { + cps.playerChunkMap.trackedEntities.remove(id); + } + /* new BukkitRunnable() { @Override diff --git a/EntityTrackerFixer/src/net/minemora/entitytrackerfixer/util/ReflectionUtils.java b/EntityTrackerFixer/src/net/minemora/entitytrackerfixer/util/ReflectionUtils.java index 2a5c41c..1897212 100644 --- a/EntityTrackerFixer/src/net/minemora/entitytrackerfixer/util/ReflectionUtils.java +++ b/EntityTrackerFixer/src/net/minemora/entitytrackerfixer/util/ReflectionUtils.java @@ -17,6 +17,13 @@ public final class ReflectionUtils { return ret; } + public static Field getClassPrivateField(Class clazz, String fieldName) + throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { + Field field = clazz.getDeclaredField(fieldName); + field.setAccessible(true); + return field; + } + public static Object invokePrivateMethod(Class clazz, Object obj, String methodName) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { return invokePrivateMethod(clazz, obj, methodName, new Class[0]);