mirror of
https://github.com/filoghost/HolographicDisplays.git
synced 2024-11-24 03:15:12 +01:00
Workaround to fix adding entity to a world on Paper servers
This commit is contained in:
parent
df5d3f9c26
commit
f74d80ba43
@ -1,6 +1,7 @@
|
||||
package com.gmail.filoghost.holographicdisplays.nms.v1_13_R1;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
@ -20,6 +21,7 @@ import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBa
|
||||
import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem;
|
||||
import com.gmail.filoghost.holographicdisplays.util.DebugHandler;
|
||||
import com.gmail.filoghost.holographicdisplays.util.Validator;
|
||||
import com.gmail.filoghost.holographicdisplays.util.VersionUtils;
|
||||
import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField;
|
||||
|
||||
import net.minecraft.server.v1_13_R1.Entity;
|
||||
@ -34,6 +36,7 @@ public class NmsManagerImpl implements NMSManager {
|
||||
|
||||
private static final ReflectField<RegistryID<EntityTypes<?>>> REGISTRY_ID_FIELD = new ReflectField<RegistryID<EntityTypes<?>>>(RegistryMaterials.class, "a");
|
||||
private static final ReflectField<Object[]> ID_TO_CLASS_MAP_FIELD = new ReflectField<Object[]>(RegistryID.class, "d");
|
||||
private static final ReflectField<List<Entity>> ENTITY_LIST_FIELD = new ReflectField<List<Entity>>(World.class, "entityList");
|
||||
|
||||
private Method validateEntityMethod;
|
||||
|
||||
@ -114,7 +117,17 @@ public class NmsManagerImpl implements NMSManager {
|
||||
}
|
||||
|
||||
nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity);
|
||||
if (VersionUtils.isPaperServer()) {
|
||||
try {
|
||||
// Workaround because nmsWorld.entityList is a different class in Paper, if used without reflection it throws NoSuchFieldError.
|
||||
ENTITY_LIST_FIELD.get(nmsWorld).add(nmsEntity);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
nmsWorld.entityList.add(nmsEntity);
|
||||
}
|
||||
|
||||
try {
|
||||
validateEntityMethod.invoke(nmsWorld, nmsEntity);
|
||||
|
@ -7,6 +7,17 @@ import org.bukkit.Bukkit;
|
||||
|
||||
public class VersionUtils {
|
||||
|
||||
private static final boolean IS_PAPER_SERVER = Bukkit.getName().equals("Paper");
|
||||
|
||||
|
||||
/**
|
||||
* Paper contains some changes that require
|
||||
*/
|
||||
public static boolean isPaperServer() {
|
||||
return IS_PAPER_SERVER;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method uses a regex to get the NMS package part that changes with every update.
|
||||
* Example: v1_8_R1
|
||||
|
Loading…
Reference in New Issue
Block a user