mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-03 06:16:10 +00:00
Corrected issues with worldGenContext not correctly being set (Ref: https://stackoverflow.com/questions/56039341/get-declared-fields-of-java-lang-reflect-fields-in-jdk12/56043252#56043252)
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
package com.dfsek.terra.api.util.reflection;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import sun.misc.Unsafe;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
@@ -26,6 +27,18 @@ import java.util.stream.Stream;
|
||||
|
||||
|
||||
public final class ReflectionUtil {
|
||||
private static final Unsafe UNSAFE;
|
||||
|
||||
static {
|
||||
try{
|
||||
final Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
|
||||
unsafeField.setAccessible(true);
|
||||
UNSAFE = (Unsafe) unsafeField.get(null);
|
||||
} catch(NoSuchFieldException | IllegalAccessException e){
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Field[] getFields(@NotNull Class<?> type) {
|
||||
Field[] result = type.getDeclaredFields();
|
||||
Class<?> parentClass = type.getSuperclass();
|
||||
@@ -35,6 +48,14 @@ public final class ReflectionUtil {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void setFinalField(Object obj, String fieldName, Object value) throws NoSuchFieldException {
|
||||
Field field = obj.getClass().getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
long fieldOffset = UNSAFE.objectFieldOffset(field);
|
||||
|
||||
UNSAFE.putObject(obj, fieldOffset, value);
|
||||
}
|
||||
|
||||
public static Method[] getMethods(@NotNull Class<?> type) {
|
||||
Method[] result = type.getDeclaredMethods();
|
||||
Class<?> parentClass = type.getSuperclass();
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package com.dfsek.terra.bukkit.nms.v1_21;
|
||||
|
||||
import com.dfsek.terra.api.util.reflection.ReflectionUtil;
|
||||
|
||||
import net.minecraft.server.level.ChunkMap;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.level.chunk.ChunkGenerator;
|
||||
import net.minecraft.world.level.chunk.status.WorldGenContext;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.craftbukkit.CraftWorld;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@@ -12,7 +15,6 @@ import org.bukkit.event.world.WorldInitEvent;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
@@ -41,21 +43,17 @@ public class NMSInjectListener implements Listener {
|
||||
ChunkGenerator vanilla = serverWorld.getChunkSource().getGenerator();
|
||||
NMSBiomeProvider provider = new NMSBiomeProvider(pack.getBiomeProvider(), craftWorld.getSeed());
|
||||
ChunkMap chunkMap = serverWorld.getChunkSource().chunkMap;
|
||||
WorldGenContext worldGenContext = chunkMap.worldGenContext;
|
||||
|
||||
try {
|
||||
Field worldGenContextField = chunkMap.getClass().getDeclaredField("worldGenContext");
|
||||
worldGenContextField.setAccessible(true);
|
||||
|
||||
WorldGenContext worldGenContext = (WorldGenContext) worldGenContextField.get(chunkMap);
|
||||
worldGenContextField.set(chunkMap,
|
||||
new WorldGenContext(
|
||||
worldGenContext.level(),
|
||||
new NMSChunkGeneratorDelegate(vanilla, pack, provider, craftWorld.getSeed()),
|
||||
worldGenContext.structureManager(),
|
||||
worldGenContext.lightEngine(),
|
||||
worldGenContext.mainThreadMailBox()
|
||||
));
|
||||
} catch(NoSuchFieldException | IllegalAccessException e) {
|
||||
ReflectionUtil.setFinalField(chunkMap, "worldGenContext", new WorldGenContext(
|
||||
worldGenContext.level(),
|
||||
new NMSChunkGeneratorDelegate(vanilla, pack, provider, craftWorld.getSeed()),
|
||||
worldGenContext.structureManager(),
|
||||
worldGenContext.lightEngine(),
|
||||
worldGenContext.mainThreadMailBox()
|
||||
));
|
||||
} catch(NoSuchFieldException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user