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

View File

@ -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<net.minecraft.server.v1_14_R1.Entity> toRemove = new HashSet<>();
Set<Integer> toRemove = new HashSet<>();
int removed = 0;
WorldServer ws = ((CraftWorld)Bukkit.getWorld(worldName)).getHandle();
ChunkProviderServer cps = ws.getChunkProvider();
ObjectIterator<EntityTracker> 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

View File

@ -17,6 +17,13 @@ public final class ReflectionUtils {
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)
throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
return invokePrivateMethod(clazz, obj, methodName, new Class[0]);