fixed npe

This commit is contained in:
Esmorall 2019-09-03 10:44:41 -03:00
parent d9d16c45f7
commit fc8c6b8871
3 changed files with 21 additions and 14 deletions

View File

@ -13,7 +13,7 @@ public class EntityTrackerFixer extends JavaPlugin {
public void onEnable() { public void onEnable() {
plugin = this; plugin = this;
ConfigMain.getInstance().setup(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()); new CheckTask().runTaskTimerAsynchronously(this, ConfigMain.getUntrackTicks() + 1, ConfigMain.getCheckFrequency());
getServer().getPluginManager().registerEvents(new ChunkEventListener(), this); getServer().getPluginManager().registerEvents(new ChunkEventListener(), this);
} }

View File

@ -1,8 +1,11 @@
package net.minemora.entitytrackerfixer; package net.minemora.entitytrackerfixer;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; 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.craftbukkit.v1_14_R1.CraftWorld;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
@ -39,21 +42,15 @@ public class UntrackerTask extends BukkitRunnable {
return; return;
} }
//Set<net.minecraft.server.v1_14_R1.Entity> toRemove = new HashSet<>(); //Set<net.minecraft.server.v1_14_R1.Entity> toRemove = new HashSet<>();
Set<Integer> toRemove = new HashSet<>();
int removed = 0; int removed = 0;
WorldServer ws = ((CraftWorld)Bukkit.getWorld(worldName)).getHandle(); WorldServer ws = ((CraftWorld)Bukkit.getWorld(worldName)).getHandle();
ChunkProviderServer cps = ws.getChunkProvider(); ChunkProviderServer cps = ws.getChunkProvider();
ObjectIterator<EntityTracker> objectiterator = cps.playerChunkMap.trackedEntities.values().iterator();
try { try {
while (objectiterator.hasNext()) { Field field = ReflectionUtils.getClassPrivateField(EntityTracker.class, "tracker");
Object iterobj = objectiterator.next(); for(EntityTracker et : cps.playerChunkMap.trackedEntities.values()) {
if(iterobj == null) { net.minecraft.server.v1_14_R1.Entity nmsEnt = (net.minecraft.server.v1_14_R1.Entity) field.get(et);
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");
if(nmsEnt instanceof EntityPlayer) { if(nmsEnt instanceof EntityPlayer) {
continue; continue;
} }
@ -80,8 +77,7 @@ public class UntrackerTask extends BukkitRunnable {
} }
if(remove) { if(remove) {
//System.out.println("untracked: " + nmsEnt.getBukkitEntity().getType().name()); //System.out.println("untracked: " + nmsEnt.getBukkitEntity().getType().name());
//toRemove.add(nmsEnt); toRemove.add(nmsEnt.getId());
objectiterator.remove();
removed++; removed++;
UntrackedEntitiesCache.getInstance().add(nmsEnt); UntrackedEntitiesCache.getInstance().add(nmsEnt);
} }
@ -90,6 +86,10 @@ public class UntrackerTask extends BukkitRunnable {
e.printStackTrace(); e.printStackTrace();
} }
for(int id : toRemove) {
cps.playerChunkMap.trackedEntities.remove(id);
}
/* /*
new BukkitRunnable() { new BukkitRunnable() {
@Override @Override

View File

@ -17,6 +17,13 @@ public final class ReflectionUtils {
return ret; return ret;
} }
public static Field getClassPrivateField(Class<? extends Object> clazz, String fieldName)
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
return field;
}
public static Object invokePrivateMethod(Class<? extends Object> clazz, Object obj, String methodName) public static Object invokePrivateMethod(Class<? extends Object> clazz, Object obj, String methodName)
throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
return invokePrivateMethod(clazz, obj, methodName, new Class[0]); return invokePrivateMethod(clazz, obj, methodName, new Class[0]);