diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/modules/impl/render/ESP.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/modules/impl/render/ESP.java
index 8bd3706..8d811dc 100644
--- a/OrangeX/src/main/java/cn/xiaozhou233/orangex/modules/impl/render/ESP.java
+++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/modules/impl/render/ESP.java
@@ -20,13 +20,14 @@ import java.util.List;
import java.util.Locale;
/**
- * 2D ESP: projects players to the screen and draws a box, name, distance and
+ * 2D ESP: projects entities to the screen and draws a box, name, distance and
* health bar around each one.
*
*
All drawing happens in GUI pixel space through {@link Render2D}, so it
* works with both the 1.8.9 and the modern font backends. World data comes
* from the cross-version {@link WorldView} wrapper, never from game classes
- * directly.
+ * directly. The wrapper reports every entity; the target-type filters below
+ * decide which kinds are drawn.
*/
public class ESP extends Module {
@@ -45,6 +46,14 @@ public class ESP extends Module {
private static final int FILL_ALPHA = 32;
private static final int HEALTH_BACKGROUND = 0x80000000;
+ private final BooleanValue players = addBoolean("Players", true);
+ private final BooleanValue localPlayer = addBoolean("Local Player", false);
+ private final BooleanValue monsters = addBoolean("Monsters", false);
+ private final BooleanValue animals = addBoolean("Animals", false);
+ private final BooleanValue villagers = addBoolean("Villagers", false);
+ private final BooleanValue others = addBoolean("Others", false);
+ private final BooleanValue invisible = addBoolean("Invisible", false);
+
private final BooleanValue box = addBoolean("Box", true);
private final BooleanValue fill = addBoolean("Fill", true);
private final BooleanValue health = addBoolean("Health", true);
@@ -92,11 +101,47 @@ public class ESP extends Module {
List targets = snapshot.getTargets();
for (int index = 0; index < targets.size(); index++) {
EntityTarget target = targets.get(index);
+ if (!isTargetEnabled(target)) {
+ continue;
+ }
renderTarget(graphics, font, fontHeight, snapshot, target, index,
screenWidth, screenHeight);
}
}
+ /**
+ * Target filters live here rather than in the world wrapper so the same
+ * snapshot can feed future features (tracers, nametags, ...) without each
+ * of them re-implementing the rules.
+ */
+ private boolean isTargetEnabled(EntityTarget target) {
+ // Invisible entities are their own category: they are drawn only when
+ // "Invisible" is enabled, no matter which kind filters are on. Turn
+ // every kind filter off and keep only this one on for an invisible-only
+ // ESP.
+ if (target.isLocalPlayer()) {
+ return localPlayer.isEnabled()
+ && (!target.isInvisible() || invisible.isEnabled());
+ }
+ if (target.isInvisible()) {
+ return invisible.isEnabled();
+ }
+
+ switch (target.getKind()) {
+ case PLAYER:
+ return players.isEnabled();
+ case MONSTER:
+ return monsters.isEnabled();
+ case ANIMAL:
+ return animals.isEnabled();
+ case VILLAGER:
+ return villagers.isEnabled();
+ case OTHER:
+ default:
+ return others.isEnabled();
+ }
+ }
+
private void renderTarget(Render2D graphics,
FontRenderer font,
double fontHeight,
diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/api/WorldView.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/api/WorldView.java
index cb5d79d..78ae2be 100644
--- a/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/api/WorldView.java
+++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/api/WorldView.java
@@ -3,17 +3,18 @@ package cn.xiaozhou233.orangex.wrapper.api;
import cn.xiaozhou233.orangex.wrapper.common.WorldSnapshot;
/**
- * Cross-version view of the rendered world: camera state and nearby players.
+ * Cross-version view of the rendered world: camera state and nearby entities.
*
* Implementations read the game every time {@link #snapshot()} is called,
* so the result must only be used for the frame that produced it. Feature
* code (ESP, tracers, ...) should depend on this interface and on the
- * {@code wrapper.common} value objects only.
+ * {@code wrapper.common} value objects only. The snapshot is intentionally
+ * unfiltered; features decide which entity kinds they want to use.
*/
public interface WorldView {
/**
- * Captures the camera and player list for the current frame.
+ * Captures the camera and entity list for the current frame.
*
* @return {@code null} while no level is loaded (title screen, loading).
*/
diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/common/EntityKind.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/common/EntityKind.java
new file mode 100644
index 0000000..01dd2b7
--- /dev/null
+++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/common/EntityKind.java
@@ -0,0 +1,37 @@
+package cn.xiaozhou233.orangex.wrapper.common;
+
+/**
+ * Version-independent classification of an entity target.
+ *
+ * The wrapper only reports what the game considers the entity to be;
+ * feature code decides which kinds it wants to render.
+ */
+public enum EntityKind {
+
+ /**
+ * A player. Use {@link EntityTarget#isLocalPlayer()} to tell the local
+ * player apart from everyone else.
+ */
+ PLAYER,
+
+ /**
+ * Hostile mobs such as zombies, skeletons, slimes and ghasts.
+ */
+ MONSTER,
+
+ /**
+ * Passive creatures: animals, ambient creatures (bats), water creatures
+ * and golems.
+ */
+ ANIMAL,
+
+ /**
+ * Villagers and wandering traders.
+ */
+ VILLAGER,
+
+ /**
+ * Anything else: items, projectiles, paintings, experience orbs, ...
+ */
+ OTHER
+}
diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/common/EntityTarget.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/common/EntityTarget.java
index 732b9cd..c0362a6 100644
--- a/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/common/EntityTarget.java
+++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/common/EntityTarget.java
@@ -1,8 +1,8 @@
package cn.xiaozhou233.orangex.wrapper.common;
/**
- * Version-independent snapshot of one player entity that feature code can
- * render (for example an ESP target).
+ * Version-independent snapshot of one entity that feature code can render
+ * (for example an ESP target).
*/
public final class EntityTarget {
@@ -13,6 +13,9 @@ public final class EntityTarget {
private final float health;
private final float maxHealth;
private final double distance;
+ private final EntityKind kind;
+ private final boolean localPlayer;
+ private final boolean invisible;
public EntityTarget(String name,
Vec3 position,
@@ -20,7 +23,10 @@ public final class EntityTarget {
double height,
float health,
float maxHealth,
- double distance) {
+ double distance,
+ EntityKind kind,
+ boolean localPlayer,
+ boolean invisible) {
this.name = name;
this.position = position;
this.width = width;
@@ -28,6 +34,9 @@ public final class EntityTarget {
this.health = health;
this.maxHealth = maxHealth;
this.distance = distance;
+ this.kind = kind;
+ this.localPlayer = localPlayer;
+ this.invisible = invisible;
}
public String getName() {
@@ -64,9 +73,31 @@ public final class EntityTarget {
return distance;
}
+ public EntityKind getKind() {
+ return kind;
+ }
+
+ /**
+ * {@code true} when this target is the entity the camera belongs to.
+ */
+ public boolean isLocalPlayer() {
+ return localPlayer;
+ }
+
+ /**
+ * {@code true} when the game reports the entity as invisible (invisibility
+ * potion, spectator, invisible armor stands, ...).
+ */
+ public boolean isInvisible() {
+ return invisible;
+ }
+
@Override
public String toString() {
- return "EntityTarget{name='" + name + "', position=" + position
+ return "EntityTarget{name='" + name + "', kind=" + kind
+ + ", localPlayer=" + localPlayer
+ + ", invisible=" + invisible
+ + ", position=" + position
+ ", health=" + health + "/" + maxHealth
+ ", distance=" + distance + "}";
}
diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/common/WorldSnapshot.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/common/WorldSnapshot.java
index 0f23314..560158d 100644
--- a/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/common/WorldSnapshot.java
+++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/common/WorldSnapshot.java
@@ -5,11 +5,11 @@ import java.util.Collections;
import java.util.List;
/**
- * One frame of world data: camera state plus the visible player list.
+ * One frame of world data: camera state plus the entity list.
*
* Snapshots are immutable and are produced on the render thread, so the
* partial tick is read only once and every position in this object belongs to
- * the same frame.
+ * the same frame. Entries are not filtered by gameplay rules.
*/
public final class WorldSnapshot {
diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/internal/v1_8_9/V1_8_9WorldView.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/internal/v1_8_9/V1_8_9WorldView.java
index e2355b1..16315a3 100644
--- a/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/internal/v1_8_9/V1_8_9WorldView.java
+++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/internal/v1_8_9/V1_8_9WorldView.java
@@ -6,6 +6,7 @@ import cn.xiaozhou233.orangex.mapping.MappingMethod;
import cn.xiaozhou233.orangex.mapping.MappingResolver;
import cn.xiaozhou233.orangex.wrapper.api.Minecraft;
import cn.xiaozhou233.orangex.wrapper.api.WorldView;
+import cn.xiaozhou233.orangex.wrapper.common.EntityKind;
import cn.xiaozhou233.orangex.wrapper.common.EntityTarget;
import cn.xiaozhou233.orangex.wrapper.common.Vec3;
import cn.xiaozhou233.orangex.wrapper.common.WorldSnapshot;
@@ -36,6 +37,12 @@ public final class V1_8_9WorldView implements WorldView {
private static final String ENTITY = "net/minecraft/entity/Entity";
private static final String ENTITY_LIVING = "net/minecraft/entity/EntityLivingBase";
private static final String ENTITY_PLAYER = "net/minecraft/entity/player/EntityPlayer";
+ private static final String ENTITY_MONSTER = "net/minecraft/entity/monster/IMob";
+ private static final String ENTITY_ANIMAL = "net/minecraft/entity/passive/EntityAnimal";
+ private static final String ENTITY_AMBIENT = "net/minecraft/entity/passive/EntityAmbientCreature";
+ private static final String ENTITY_WATER = "net/minecraft/entity/passive/EntityWaterMob";
+ private static final String ENTITY_GOLEM = "net/minecraft/entity/monster/EntityGolem";
+ private static final String ENTITY_VILLAGER = "net/minecraft/entity/passive/EntityVillager";
private static final String ENTITY_RENDERER = "net/minecraft/client/renderer/EntityRenderer";
private static final float DEFAULT_FOV = 70.0F;
@@ -53,15 +60,20 @@ public final class V1_8_9WorldView implements WorldView {
private final Field entityRendererField;
private final String partialTicksName;
- private final String playerEntitiesName;
+ private final String loadedEntitiesName;
private final String thirdPersonViewName;
private final String fovSettingName;
private final String fovModifierMethodName;
private final String thirdPersonDistanceName;
private final String thirdPersonDistanceTempName;
+ private final String[] playerClassNames;
+ private final String[] monsterClassNames;
+ private final String[] animalClassNames;
+ private final String[] villagerClassNames;
+
private Field partialTicksField;
- private Field playerEntitiesField;
+ private Field loadedEntitiesField;
private Field thirdPersonViewField;
private Field fovSettingField;
private Field thirdPersonDistanceField;
@@ -86,7 +98,7 @@ public final class V1_8_9WorldView implements WorldView {
this.entityRendererField = resolveField(minecraftMapping, "entityRenderer", minecraftClass);
this.partialTicksName = fieldName(TIMER, "renderPartialTicks");
- this.playerEntitiesName = fieldName(WORLD, "playerEntities");
+ this.loadedEntitiesName = fieldName(WORLD, "loadedEntityList");
this.thirdPersonViewName = fieldName(GAME_SETTINGS, "thirdPersonView");
this.fovSettingName = fieldName(GAME_SETTINGS, "fovSetting");
this.fovModifierMethodName = methodName(
@@ -95,6 +107,12 @@ public final class V1_8_9WorldView implements WorldView {
ENTITY_RENDERER, "thirdPersonDistance");
this.thirdPersonDistanceTempName = optionalFieldName(
ENTITY_RENDERER, "thirdPersonDistanceTemp");
+
+ this.playerClassNames = classNames(ENTITY_PLAYER);
+ this.monsterClassNames = classNames(ENTITY_MONSTER);
+ this.animalClassNames = classNames(
+ ENTITY_ANIMAL, ENTITY_AMBIENT, ENTITY_WATER, ENTITY_GOLEM);
+ this.villagerClassNames = classNames(ENTITY_VILLAGER);
}
@Override
@@ -156,6 +174,11 @@ public final class V1_8_9WorldView implements WorldView {
cameraYaw, cameraPitch, fov, targets);
}
+ /**
+ * Captures every entity in the loaded entity list. No gameplay filtering
+ * happens here: feature code (for example the ESP module) decides which
+ * kinds it wants to show.
+ */
private List collectTargets(Object world,
Object localPlayer,
double cameraX,
@@ -164,7 +187,7 @@ public final class V1_8_9WorldView implements WorldView {
float partialTicks) {
List targets = new ArrayList();
- Field entitiesField = playerEntitiesField(world.getClass());
+ Field entitiesField = loadedEntitiesField(world.getClass());
if (entitiesField == null) {
return targets;
}
@@ -175,27 +198,12 @@ public final class V1_8_9WorldView implements WorldView {
}
for (Object entity : (List>) value) {
- if (entity == null || entity == localPlayer) {
+ if (entity == null) {
continue;
}
EntityAccess access = accessFor(entity.getClass());
- if (access.isAlive != null && !invokeBoolean(access.isAlive, entity)) {
- continue;
- }
- if (access.isInvisible != null && invokeBoolean(access.isInvisible, entity)) {
- continue;
- }
- if (access.isSpectator != null && invokeBoolean(access.isSpectator, entity)) {
- continue;
- }
-
- float health = invokeFloat(access.getHealth, entity);
- if (health <= 0.0F) {
- continue;
- }
-
double x = interpolate(access.posX, access.lastTickPosX, entity, partialTicks);
double y = interpolate(access.posY, access.lastTickPosY, entity, partialTicks);
double z = interpolate(access.posZ, access.lastTickPosZ, entity, partialTicks);
@@ -206,20 +214,75 @@ public final class V1_8_9WorldView implements WorldView {
: ReflectionSupport.getFloat(access.height, entity);
double distance = distance(cameraX, cameraY, cameraZ, x, y, z);
+ boolean local = entity == localPlayer;
targets.add(new EntityTarget(
String.valueOf(ReflectionSupport.invoke(access.getName, entity)),
new Vec3(x, y, z),
width,
height,
- health,
- invokeFloat(access.getMaxHealth, entity),
- distance));
+ access.getHealth == null ? 0.0F : invokeFloat(access.getHealth, entity),
+ access.getMaxHealth == null ? 0.0F : invokeFloat(access.getMaxHealth, entity),
+ distance,
+ classify(entity.getClass()),
+ local,
+ invokeBoolean(access.isInvisible, entity)));
}
return targets;
}
+ private EntityKind classify(Class> entityClass) {
+ if (matches(entityClass, playerClassNames)) {
+ return EntityKind.PLAYER;
+ }
+ if (matches(entityClass, villagerClassNames)) {
+ return EntityKind.VILLAGER;
+ }
+ if (matches(entityClass, monsterClassNames)) {
+ return EntityKind.MONSTER;
+ }
+ if (matches(entityClass, animalClassNames)) {
+ return EntityKind.ANIMAL;
+ }
+ return EntityKind.OTHER;
+ }
+
+ /**
+ * Checks the runtime class hierarchy (including interfaces) against a set
+ * of mapped class names. This avoids loading optional entity classes that
+ * may not exist on every runtime.
+ */
+ private static boolean matches(Class> type, String[] classNames) {
+ if (type == null) {
+ return false;
+ }
+
+ String name = type.getName();
+ for (String className : classNames) {
+ if (name.equals(className)) {
+ return true;
+ }
+ }
+
+ for (Class> implemented : type.getInterfaces()) {
+ if (matches(implemented, classNames)) {
+ return true;
+ }
+ }
+
+ return matches(type.getSuperclass(), classNames);
+ }
+
+ private String[] classNames(String... sourceNames) {
+ String[] names = new String[sourceNames.length];
+ for (int index = 0; index < sourceNames.length; index++) {
+ names[index] = resolver.mapClass(sourceNames[index])
+ .getTargetName().replace('/', '.');
+ }
+ return names;
+ }
+
private float partialTicks() {
Object timer = ReflectionSupport.get(timerField, minecraft);
if (timer == null) {
@@ -320,12 +383,12 @@ public final class V1_8_9WorldView implements WorldView {
|| thirdPersonDistanceTempField != null;
}
- private Field playerEntitiesField(Class> worldClass) {
- if (playerEntitiesField == null) {
- playerEntitiesField = ReflectionSupport.findField(
- worldClass, playerEntitiesName);
+ private Field loadedEntitiesField(Class> worldClass) {
+ if (loadedEntitiesField == null) {
+ loadedEntitiesField = ReflectionSupport.findField(
+ worldClass, loadedEntitiesName);
}
- return playerEntitiesField;
+ return loadedEntitiesField;
}
private EntityAccess accessFor(Class> entityClass) {
@@ -431,14 +494,11 @@ public final class V1_8_9WorldView implements WorldView {
private final Method getName;
private final Method getHealth;
private final Method getMaxHealth;
- private final Method isAlive;
private final Method isInvisible;
- private final Method isSpectator;
private EntityAccess(Class> entityClass) {
MappingClass entity = resolver.mapClass(ENTITY);
MappingClass living = resolver.mapClass(ENTITY_LIVING);
- MappingClass player = resolver.mapClass(ENTITY_PLAYER);
this.posX = mappedField(entity, "posX", entityClass);
this.posY = mappedField(entity, "posY", entityClass);
@@ -455,17 +515,13 @@ public final class V1_8_9WorldView implements WorldView {
this.eyeHeight = mappedMethod(entity, "getEyeHeight", "()F",
float.class, entityClass);
- this.isAlive = mappedMethod(entity, "isEntityAlive", "()Z",
- boolean.class, entityClass);
- this.isInvisible = mappedMethod(entity, "isInvisible", "()Z",
- boolean.class, entityClass);
- this.getHealth = mappedMethod(living, "getHealth", "()F",
- float.class, entityClass);
- this.getMaxHealth = mappedMethod(living, "getMaxHealth", "()F",
- float.class, entityClass);
- this.getName = mappedMethod(player, "getName", "()Ljava/lang/String;",
+ this.getName = mappedMethod(entity, "getName", "()Ljava/lang/String;",
String.class, entityClass);
- this.isSpectator = optionalMappedMethod(player, "isSpectator", "()Z",
+ this.getHealth = optionalMappedMethod(living, "getHealth", "()F",
+ float.class, entityClass);
+ this.getMaxHealth = optionalMappedMethod(living, "getMaxHealth", "()F",
+ float.class, entityClass);
+ this.isInvisible = mappedMethod(entity, "isInvisible", "()Z",
boolean.class, entityClass);
}
diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/internal/v26_2/V26_2WorldView.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/internal/v26_2/V26_2WorldView.java
index e99dd69..6caeaf4 100644
--- a/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/internal/v26_2/V26_2WorldView.java
+++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/wrapper/internal/v26_2/V26_2WorldView.java
@@ -2,6 +2,7 @@ package cn.xiaozhou233.orangex.wrapper.internal.v26_2;
import cn.xiaozhou233.orangex.wrapper.api.Minecraft;
import cn.xiaozhou233.orangex.wrapper.api.WorldView;
+import cn.xiaozhou233.orangex.wrapper.common.EntityKind;
import cn.xiaozhou233.orangex.wrapper.common.EntityTarget;
import cn.xiaozhou233.orangex.wrapper.common.Vec3;
import cn.xiaozhou233.orangex.wrapper.common.WorldSnapshot;
@@ -25,6 +26,28 @@ public final class V26_2WorldView implements WorldView {
private static final float DEFAULT_FOV = 70.0F;
+ private static final String ENTITY_PLAYER =
+ "net.minecraft.world.entity.player.Player";
+ private static final String ENTITY_MONSTER =
+ "net.minecraft.world.entity.monster.Enemy";
+ private static final String ENTITY_ANIMAL =
+ "net.minecraft.world.entity.animal.Animal";
+ private static final String ENTITY_WATER_ANIMAL =
+ "net.minecraft.world.entity.animal.fish.WaterAnimal";
+ private static final String ENTITY_AMBIENT =
+ "net.minecraft.world.entity.ambient.AmbientCreature";
+ private static final String ENTITY_GOLEM =
+ "net.minecraft.world.entity.animal.golem.AbstractGolem";
+ private static final String ENTITY_VILLAGER =
+ "net.minecraft.world.entity.npc.villager.AbstractVillager";
+
+ private static final String[] PLAYER_CLASSES = {ENTITY_PLAYER};
+ private static final String[] MONSTER_CLASSES = {ENTITY_MONSTER};
+ private static final String[] ANIMAL_CLASSES = {
+ ENTITY_ANIMAL, ENTITY_WATER_ANIMAL, ENTITY_AMBIENT, ENTITY_GOLEM
+ };
+ private static final String[] VILLAGER_CLASSES = {ENTITY_VILLAGER};
+
private final Object minecraft;
private final Field levelField;
@@ -39,6 +62,12 @@ public final class V26_2WorldView implements WorldView {
private Method cameraYRotMethod;
private Method cameraFovMethod;
+ private boolean entitySourceResolved;
+ private Method entitiesForRenderingMethod;
+ private Method getEntitiesMethod;
+ private Method getAllMethod;
+ private Method playersMethod;
+
private final Map, EntityAccess> accessCache =
new HashMap, EntityAccess>();
private final Map, Field[]> vectorFields =
@@ -114,64 +143,141 @@ public final class V26_2WorldView implements WorldView {
return camera;
}
+ /**
+ * Captures every entity the level exposes for rendering. No gameplay
+ * filtering happens here: feature code (for example the ESP module)
+ * decides which kinds it wants to show.
+ */
private List collectTargets(Object level,
Object localPlayer,
Vec3 cameraPosition,
float partialTicks) {
List targets = new ArrayList();
- Method playersMethod = ReflectionSupport.findMethod(
- level.getClass(), "players", null);
- if (playersMethod == null) {
+ Iterable> entities = entities(level);
+ if (entities == null) {
return targets;
}
- Object value = ReflectionSupport.invoke(playersMethod, level);
- if (!(value instanceof Iterable)) {
- return targets;
- }
-
- for (Object entity : (Iterable>) value) {
- if (entity == null || entity == localPlayer) {
+ for (Object entity : entities) {
+ if (entity == null) {
continue;
}
EntityAccess access = accessFor(entity.getClass());
- if (!invokeBoolean(access.isAlive, entity)) {
- continue;
- }
- if (invokeBoolean(access.isInvisible, entity)) {
- continue;
- }
- if (access.isSpectator != null && invokeBoolean(access.isSpectator, entity)) {
- continue;
- }
-
- float health = invokeFloat(access.getHealth, entity);
- if (health <= 0.0F) {
- continue;
- }
-
Vec3 position = readVec3(ReflectionSupport.invoke(
access.getPosition, entity, Float.valueOf(partialTicks)));
if (position == null) {
continue;
}
+ boolean local = entity == localPlayer;
+
targets.add(new EntityTarget(
invokeString(access.getName, entity),
position,
invokeFloat(access.getBbWidth, entity),
invokeFloat(access.getBbHeight, entity),
- health,
- invokeFloat(access.getMaxHealth, entity),
- distance(cameraPosition, position)));
+ access.getHealth == null ? 0.0F : invokeFloat(access.getHealth, entity),
+ access.getMaxHealth == null ? 0.0F : invokeFloat(access.getMaxHealth, entity),
+ distance(cameraPosition, position),
+ classify(entity.getClass()),
+ local,
+ invokeBoolean(access.isInvisible, entity)));
}
return targets;
}
+ /**
+ * Resolves the all-entity source once. Modern clients expose
+ * {@code Level#entitiesForRendering()}; older/newer builds may instead
+ * expose {@code Level#getEntities()#getAll()}. {@code players()} stays as
+ * a last-resort fallback so the overlay keeps working if the level API
+ * changes again.
+ */
+ private Iterable> entities(Object level) {
+ if (!entitySourceResolved) {
+ entitySourceResolved = true;
+ entitiesForRenderingMethod = ReflectionSupport.findMethod(
+ level.getClass(), "entitiesForRendering", null);
+ if (entitiesForRenderingMethod == null) {
+ getEntitiesMethod = ReflectionSupport.findMethod(
+ level.getClass(), "getEntities", null);
+ }
+ if (entitiesForRenderingMethod == null && getEntitiesMethod == null) {
+ playersMethod = ReflectionSupport.findMethod(
+ level.getClass(), "players", null);
+ }
+ }
+
+ if (entitiesForRenderingMethod != null) {
+ Object value = ReflectionSupport.invoke(entitiesForRenderingMethod, level);
+ return value instanceof Iterable ? (Iterable>) value : null;
+ }
+
+ if (getEntitiesMethod != null) {
+ Object getter = ReflectionSupport.invoke(getEntitiesMethod, level);
+ if (getter == null) {
+ return null;
+ }
+ if (getAllMethod == null) {
+ getAllMethod = ReflectionSupport.findMethod(
+ getter.getClass(), "getAll", null);
+ }
+ if (getAllMethod != null) {
+ Object value = ReflectionSupport.invoke(getAllMethod, getter);
+ return value instanceof Iterable ? (Iterable>) value : null;
+ }
+ return null;
+ }
+
+ if (playersMethod != null) {
+ Object value = ReflectionSupport.invoke(playersMethod, level);
+ return value instanceof Iterable ? (Iterable>) value : null;
+ }
+
+ return null;
+ }
+
+ private static EntityKind classify(Class> entityClass) {
+ if (matches(entityClass, PLAYER_CLASSES)) {
+ return EntityKind.PLAYER;
+ }
+ if (matches(entityClass, VILLAGER_CLASSES)) {
+ return EntityKind.VILLAGER;
+ }
+ if (matches(entityClass, MONSTER_CLASSES)) {
+ return EntityKind.MONSTER;
+ }
+ if (matches(entityClass, ANIMAL_CLASSES)) {
+ return EntityKind.ANIMAL;
+ }
+ return EntityKind.OTHER;
+ }
+
+ private static boolean matches(Class> type, String[] classNames) {
+ if (type == null) {
+ return false;
+ }
+
+ String name = type.getName();
+ for (String className : classNames) {
+ if (name.equals(className)) {
+ return true;
+ }
+ }
+
+ for (Class> implemented : type.getInterfaces()) {
+ if (matches(implemented, classNames)) {
+ return true;
+ }
+ }
+
+ return matches(type.getSuperclass(), classNames);
+ }
+
private float partialTicks() {
if (deltaTrackerMethod == null) {
deltaTrackerMethod = ReflectionSupport.findMethod(
@@ -269,9 +375,7 @@ public final class V26_2WorldView implements WorldView {
private final Method getMaxHealth;
private final Method getBbWidth;
private final Method getBbHeight;
- private final Method isAlive;
private final Method isInvisible;
- private final Method isSpectator;
private EntityAccess(Class> entityClass) {
this.getPosition = ReflectionSupport.requireMethod(
@@ -279,20 +383,18 @@ public final class V26_2WorldView implements WorldView {
float.class);
this.getName = ReflectionSupport.requireMethod(
entityClass, "getName", null, entityClass.getName());
- this.getHealth = ReflectionSupport.requireMethod(
- entityClass, "getHealth", float.class, entityClass.getName());
- this.getMaxHealth = ReflectionSupport.requireMethod(
- entityClass, "getMaxHealth", float.class, entityClass.getName());
+ // Health only exists on LivingEntity, so non-living entities
+ // (items, arrows, ...) leave these null.
+ this.getHealth = ReflectionSupport.findMethod(
+ entityClass, "getHealth", float.class);
+ this.getMaxHealth = ReflectionSupport.findMethod(
+ entityClass, "getMaxHealth", float.class);
this.getBbWidth = ReflectionSupport.requireMethod(
entityClass, "getBbWidth", float.class, entityClass.getName());
this.getBbHeight = ReflectionSupport.requireMethod(
entityClass, "getBbHeight", float.class, entityClass.getName());
- this.isAlive = ReflectionSupport.requireMethod(
- entityClass, "isAlive", boolean.class, entityClass.getName());
this.isInvisible = ReflectionSupport.requireMethod(
entityClass, "isInvisible", boolean.class, entityClass.getName());
- this.isSpectator = ReflectionSupport.findMethod(
- entityClass, "isSpectator", boolean.class);
}
}
}