mirror of
https://github.com/Elecast2/EntityTrackerFixer.git
synced 2024-11-25 11:25:31 +01:00
Improve performance of entity tick manager
This commit is contained in:
parent
c4125e69e8
commit
8c1e2600d2
@ -1,6 +1,7 @@
|
|||||||
package net.minemora.entitytrackerfixer.v1_14_R1;
|
package net.minemora.entitytrackerfixer.v1_14_R1;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.craftbukkit.v1_14_R1.CraftWorld;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
import net.minemora.entitytrackerfixer.config.ConfigMain;
|
import net.minemora.entitytrackerfixer.config.ConfigMain;
|
||||||
@ -24,7 +25,8 @@ public class NMSHandler implements NMS {
|
|||||||
if(Bukkit.getWorld(worldName) == null) {
|
if(Bukkit.getWorld(worldName) == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
EntityTickManager.getInstance().getCache().put(worldName, new EntityTickWorldCache(worldName));
|
EntityTickManager.getInstance().getCache().put(worldName,
|
||||||
|
new EntityTickWorldCache(((CraftWorld)Bukkit.getWorld(worldName)).getHandle()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,8 +5,6 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.craftbukkit.v1_14_R1.CraftWorld;
|
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
@ -17,14 +15,12 @@ import net.minemora.entitytrackerfixer.v1_14_R1.tasks.UntrackerTask;
|
|||||||
|
|
||||||
public class EntityTickManager extends BukkitRunnable {
|
public class EntityTickManager extends BukkitRunnable {
|
||||||
|
|
||||||
private static Field tickingField;
|
|
||||||
private static Field tickingEntitiesField;
|
private static Field tickingEntitiesField;
|
||||||
private static Field entityCount;
|
private static Field entityCount;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
tickingEntitiesField = ReflectionUtils.getClassPrivateField(WorldServer.class, "tickingEntities");
|
tickingEntitiesField = ReflectionUtils.getClassPrivateField(WorldServer.class, "tickingEntities");
|
||||||
tickingField = ReflectionUtils.getClassPrivateField(WorldServer.class, "ticking");
|
|
||||||
entityCount = ReflectionUtils.getClassPrivateField(net.minecraft.server.v1_14_R1.Entity.class, "entityCount");
|
entityCount = ReflectionUtils.getClassPrivateField(net.minecraft.server.v1_14_R1.Entity.class, "entityCount");
|
||||||
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
|
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -36,7 +32,7 @@ public class EntityTickManager extends BukkitRunnable {
|
|||||||
private Map<String, EntityTickWorldCache> cache = new HashMap<>();
|
private Map<String, EntityTickWorldCache> cache = new HashMap<>();
|
||||||
|
|
||||||
private EntityTickManager(Plugin plugin) {
|
private EntityTickManager(Plugin plugin) {
|
||||||
this.runTaskTimer(plugin, 41, 41);
|
this.runTaskTimer(plugin, 61, 61);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disableTicking(int id, String worldName) {
|
public void disableTicking(int id, String worldName) {
|
||||||
@ -55,13 +51,15 @@ public class EntityTickManager extends BukkitRunnable {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for(String worldName : cache.keySet()) {
|
for(String worldName : cache.keySet()) {
|
||||||
WorldServer ws = ((CraftWorld)Bukkit.getWorld(worldName)).getHandle();
|
EntityTickWorldCache ewc = cache.get(worldName);
|
||||||
|
WorldServer ws = ewc.getWorldServer();
|
||||||
|
if(ws.b()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
if(tickingEntitiesField.getBoolean(ws) || tickingField.getBoolean(ws)) {
|
if(tickingEntitiesField.getBoolean(ws)) {
|
||||||
//System.out.println("ticking");
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
EntityTickWorldCache ewc = cache.get(worldName);
|
|
||||||
//System.out.println("unticking: " + ewc.getToUntick().size() + " entities, ticking again: " + ewc.getToTick().size() + " entities");
|
//System.out.println("unticking: " + ewc.getToUntick().size() + " entities, ticking again: " + ewc.getToTick().size() + " entities");
|
||||||
for(int i : ewc.getToUntick()) {
|
for(int i : ewc.getToUntick()) {
|
||||||
ws.entitiesById.remove(i);
|
ws.entitiesById.remove(i);
|
||||||
|
@ -5,15 +5,19 @@ import java.util.HashSet;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import net.minecraft.server.v1_14_R1.WorldServer;
|
||||||
|
|
||||||
public class EntityTickWorldCache {
|
public class EntityTickWorldCache {
|
||||||
|
|
||||||
private String worldName;
|
private String worldName;
|
||||||
|
private WorldServer worldServer;
|
||||||
|
|
||||||
private Set<Integer> toUntick = new HashSet<>();
|
private Set<Integer> toUntick = new HashSet<>();
|
||||||
private Map<Integer, net.minecraft.server.v1_14_R1.Entity> toTick = new HashMap<>();
|
private Map<Integer, net.minecraft.server.v1_14_R1.Entity> toTick = new HashMap<>();
|
||||||
|
|
||||||
public EntityTickWorldCache(String worldName) {
|
public EntityTickWorldCache(WorldServer worldServer) {
|
||||||
this.worldName = worldName;
|
this.worldName = worldServer.getWorld().getName();
|
||||||
|
this.worldServer = worldServer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Integer> getToUntick() {
|
public Set<Integer> getToUntick() {
|
||||||
@ -28,4 +32,7 @@ public class EntityTickWorldCache {
|
|||||||
return worldName;
|
return worldName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public WorldServer getWorldServer() {
|
||||||
|
return worldServer;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package net.minemora.entitytrackerfixer.v1_15_R1;
|
package net.minemora.entitytrackerfixer.v1_15_R1;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.craftbukkit.v1_15_R1.CraftWorld;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
import net.minemora.entitytrackerfixer.config.ConfigMain;
|
import net.minemora.entitytrackerfixer.config.ConfigMain;
|
||||||
@ -24,7 +25,8 @@ public class NMSHandler implements NMS {
|
|||||||
if(Bukkit.getWorld(worldName) == null) {
|
if(Bukkit.getWorld(worldName) == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
EntityTickManager.getInstance().getCache().put(worldName, new EntityTickWorldCache(worldName));
|
EntityTickManager.getInstance().getCache().put(worldName,
|
||||||
|
new EntityTickWorldCache(((CraftWorld)Bukkit.getWorld(worldName)).getHandle()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,8 +5,6 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.craftbukkit.v1_15_R1.CraftWorld;
|
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
@ -17,14 +15,12 @@ import net.minemora.entitytrackerfixer.v1_15_R1.tasks.UntrackerTask;
|
|||||||
|
|
||||||
public class EntityTickManager extends BukkitRunnable {
|
public class EntityTickManager extends BukkitRunnable {
|
||||||
|
|
||||||
private static Field tickingField;
|
|
||||||
private static Field tickingEntitiesField;
|
private static Field tickingEntitiesField;
|
||||||
private static Field entityCount;
|
private static Field entityCount;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
tickingEntitiesField = ReflectionUtils.getClassPrivateField(WorldServer.class, "tickingEntities");
|
tickingEntitiesField = ReflectionUtils.getClassPrivateField(WorldServer.class, "tickingEntities");
|
||||||
tickingField = ReflectionUtils.getClassPrivateField(WorldServer.class, "ticking");
|
|
||||||
entityCount = ReflectionUtils.getClassPrivateField(net.minecraft.server.v1_15_R1.Entity.class, "entityCount");
|
entityCount = ReflectionUtils.getClassPrivateField(net.minecraft.server.v1_15_R1.Entity.class, "entityCount");
|
||||||
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
|
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -36,7 +32,7 @@ public class EntityTickManager extends BukkitRunnable {
|
|||||||
private Map<String, EntityTickWorldCache> cache = new HashMap<>();
|
private Map<String, EntityTickWorldCache> cache = new HashMap<>();
|
||||||
|
|
||||||
private EntityTickManager(Plugin plugin) {
|
private EntityTickManager(Plugin plugin) {
|
||||||
this.runTaskTimer(plugin, 41, 41);
|
this.runTaskTimer(plugin, 61, 61);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disableTicking(int id, String worldName) {
|
public void disableTicking(int id, String worldName) {
|
||||||
@ -55,13 +51,15 @@ public class EntityTickManager extends BukkitRunnable {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for(String worldName : cache.keySet()) {
|
for(String worldName : cache.keySet()) {
|
||||||
WorldServer ws = ((CraftWorld)Bukkit.getWorld(worldName)).getHandle();
|
EntityTickWorldCache ewc = cache.get(worldName);
|
||||||
|
WorldServer ws = ewc.getWorldServer();
|
||||||
|
if(ws.b()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
if(tickingEntitiesField.getBoolean(ws) || tickingField.getBoolean(ws)) {
|
if(tickingEntitiesField.getBoolean(ws)) {
|
||||||
//System.out.println("ticking");
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
EntityTickWorldCache ewc = cache.get(worldName);
|
|
||||||
//System.out.println("unticking: " + ewc.getToUntick().size() + " entities, ticking again: " + ewc.getToTick().size() + " entities");
|
//System.out.println("unticking: " + ewc.getToUntick().size() + " entities, ticking again: " + ewc.getToTick().size() + " entities");
|
||||||
for(int i : ewc.getToUntick()) {
|
for(int i : ewc.getToUntick()) {
|
||||||
ws.entitiesById.remove(i);
|
ws.entitiesById.remove(i);
|
||||||
|
@ -5,15 +5,19 @@ import java.util.HashSet;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import net.minecraft.server.v1_15_R1.WorldServer;
|
||||||
|
|
||||||
public class EntityTickWorldCache {
|
public class EntityTickWorldCache {
|
||||||
|
|
||||||
private String worldName;
|
private String worldName;
|
||||||
|
private WorldServer worldServer;
|
||||||
|
|
||||||
private Set<Integer> toUntick = new HashSet<>();
|
private Set<Integer> toUntick = new HashSet<>();
|
||||||
private Map<Integer, net.minecraft.server.v1_15_R1.Entity> toTick = new HashMap<>();
|
private Map<Integer, net.minecraft.server.v1_15_R1.Entity> toTick = new HashMap<>();
|
||||||
|
|
||||||
public EntityTickWorldCache(String worldName) {
|
public EntityTickWorldCache(WorldServer worldServer) {
|
||||||
this.worldName = worldName;
|
this.worldName = worldServer.getWorld().getName();
|
||||||
|
this.worldServer = worldServer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Integer> getToUntick() {
|
public Set<Integer> getToUntick() {
|
||||||
@ -28,4 +32,7 @@ public class EntityTickWorldCache {
|
|||||||
return worldName;
|
return worldName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public WorldServer getWorldServer() {
|
||||||
|
return worldServer;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user