mirror of
https://github.com/dmulloy2/ProtocolLib.git
synced 2025-01-08 09:27:34 +01:00
Improve getEntityFromID (#1740)
This commit is contained in:
parent
23dac3287b
commit
96155b1065
@ -28,6 +28,7 @@ import com.comphenix.protocol.utility.MinecraftFields;
|
||||
import com.comphenix.protocol.utility.MinecraftReflection;
|
||||
import com.comphenix.protocol.utility.MinecraftVersion;
|
||||
import com.comphenix.protocol.wrappers.WrappedIntHashMap;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@ -56,6 +57,7 @@ class EntityUtilities {
|
||||
private FieldAccessor trackedPlayersField;
|
||||
private FieldAccessor trackedEntitiesField;
|
||||
|
||||
private MethodAccessor getEntity;
|
||||
private MethodAccessor getChunkProvider;
|
||||
|
||||
private EntityUtilities() {
|
||||
@ -83,6 +85,20 @@ class EntityUtilities {
|
||||
.invoke(trackerEntry, nmsPlayers);
|
||||
}
|
||||
|
||||
public Entity getEntity(World world, int id) {
|
||||
Object level = BukkitUnwrapper.getInstance().unwrapItem(world);
|
||||
if (getEntity == null) {
|
||||
Method entityGetter = FuzzyReflection.fromObject(level).getMethodByParameters(
|
||||
"getEntity",
|
||||
MinecraftReflection.getEntityClass(),
|
||||
new Class[]{int.class});
|
||||
getEntity = Accessors.getMethodAccessor(entityGetter);
|
||||
}
|
||||
|
||||
Object entity = getEntity.invoke(level, id);
|
||||
return (Entity) MinecraftReflection.getBukkitEntity(entity);
|
||||
}
|
||||
|
||||
private MethodAccessor findScanPlayers(Class<?> trackerClass) {
|
||||
MethodAccessor candidate = Accessors.getMethodAcccessorOrNull(trackerClass, "scanPlayers");
|
||||
if (candidate != null) {
|
||||
|
@ -401,12 +401,7 @@ public class PacketFilterManager implements ListenerInvoker, InternalManager {
|
||||
|
||||
@Override
|
||||
public Entity getEntityFromID(World container, int id) {
|
||||
for (Entity entity : container.getEntities()) {
|
||||
if (entity.getEntityId() == id) {
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return EntityUtilities.getInstance().getEntity(container, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user