Skip to content

Commit 12d8141

Browse files
authored
Implement getNewEntityTracker for Folia (#2464)
1 parent 2c48b1c commit 12d8141

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/main/java/com/comphenix/protocol/injector/EntityUtilities.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727
import com.comphenix.protocol.utility.MinecraftFields;
2828
import com.comphenix.protocol.utility.MinecraftReflection;
2929
import com.comphenix.protocol.utility.MinecraftVersion;
30+
import com.comphenix.protocol.utility.Util;
3031
import com.comphenix.protocol.wrappers.WrappedIntHashMap;
3132

3233
import java.lang.reflect.Field;
3334
import java.lang.reflect.Method;
3435
import java.lang.reflect.Modifier;
3536
import java.util.ArrayList;
3637
import java.util.Collection;
37-
import java.util.HashMap;
3838
import java.util.List;
3939
import java.util.Map;
4040
import java.util.concurrent.ConcurrentHashMap;
@@ -61,6 +61,7 @@ class EntityUtilities {
6161
private FieldAccessor entityTrackerField;
6262
private FieldAccessor trackedPlayersField;
6363
private FieldAccessor trackedEntitiesField;
64+
private FieldAccessor foliaTrackerField;
6465

6566
private MethodAccessor getEntity;
6667
private MethodAccessor getChunkProvider;
@@ -203,6 +204,25 @@ private Collection<?> getTrackedPlayers(Entity entity) {
203204

204205
@SuppressWarnings("unchecked")
205206
private Object getNewEntityTracker(Object worldServer, int entityId) {
207+
if (Util.isUsingFolia()) {
208+
if (this.getEntity == null) {
209+
Method entityGetter = FuzzyReflection.fromObject(worldServer).getMethodByReturnTypeAndParameters(
210+
"getEntity",
211+
MinecraftReflection.getEntityClass(),
212+
int.class);
213+
this.getEntity = Accessors.getMethodAccessor(entityGetter);
214+
}
215+
216+
Object entity = this.getEntity.invoke(worldServer, entityId);
217+
218+
if (this.foliaTrackerField == null) {
219+
this.foliaTrackerField = Accessors.getFieldAccessor(FuzzyReflection.fromClass(entity.getClass(), false)
220+
.getField(FuzzyFieldContract.newBuilder().typeExact(MinecraftReflection.getEntityTrackerClass()).build()));
221+
}
222+
223+
return this.foliaTrackerField.get(entity);
224+
}
225+
206226
if (this.getChunkProvider == null) {
207227
Class<?> chunkProviderClass = MinecraftReflection.getChunkProviderServer();
208228
this.getChunkProvider = Accessors.getMethodAccessor(FuzzyReflection.fromClass(worldServer.getClass(), false)

0 commit comments

Comments
 (0)