improve CraftWorldInfo creation

This commit is contained in:
Julian Krings
2024-10-09 12:24:33 +02:00
parent 13369bbf32
commit 03582751c5
9 changed files with 75 additions and 121 deletions

View File

@@ -0,0 +1,42 @@
package com.volmit.iris.util.reflect;
import com.volmit.iris.core.nms.container.Pair;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Reflect {
public static <T> T newInstance(Class<T> type, Object... initArgs) throws NoSuchMethodException, InvocationTargetException {
var list = Arrays.stream(initArgs)
.map(arg -> new Pair<Class<?>, Object>(arg.getClass(), arg))
.toList();
return newInstance(type, list);
}
public static <T> T newInstance(Class<T> type, List<Pair<Class<?>, Object>> initArgs) throws NoSuchMethodException, InvocationTargetException{
constructors:
for (var c : type.getDeclaredConstructors()) {
var types = c.getParameterTypes();
for (int i = 0; i < types.length; i++) {
if (!types[i].isAssignableFrom(initArgs.get(i).getA()))
continue constructors;
}
c.setAccessible(true);
try {
return (T) c.newInstance(initArgs);
} catch (InstantiationException | IllegalAccessException e) {
throw new InvocationTargetException(e);
}
}
var constructors = Arrays.stream(type.getDeclaredConstructors())
.map(Constructor::toGenericString)
.collect(Collectors.joining("\n"));
throw new NoSuchMethodException("No matching constructor found in:\n" + constructors);
}
}

View File

@@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList;
import com.mojang.serialization.Lifecycle;
import com.volmit.iris.Iris;
import com.volmit.iris.core.nms.IMemoryWorld;
import com.volmit.iris.util.reflect.Reflect;
import com.volmit.iris.util.scheduling.J;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
@@ -104,21 +105,9 @@ public class MemoryWorld implements IMemoryWorld {
worldInfo = new CraftWorldInfo(worldData, access, creator.environment(), levelStem.typeHolder().value());
} catch (Throwable e) {
try {
var c = CraftWorldInfo.class.getDeclaredConstructor(
ServerLevelData.class,
LevelStorageSource.LevelStorageAccess.class,
World.Environment.class,
DimensionType.class,
net.minecraft.world.level.chunk.ChunkGenerator.class,
RegistryAccess.Frozen.class);
worldInfo = c.newInstance(worldData, access, creator.environment(), levelStem.typeHolder().value(), levelStem.generator(), server.registryAccess());
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException |
IllegalAccessException ex) {
var constructors = Arrays.stream(CraftWorldInfo.class.getDeclaredConstructors())
.map(Constructor::toGenericString)
.collect(Collectors.joining("\n"));
throw new IOException("Failed to find CraftWorldInfo constructor found: " + constructors, ex);
worldInfo = Reflect.newInstance(CraftWorldInfo.class, worldData, access, creator.environment(), levelStem.typeHolder().value(), levelStem.generator(), server.registryAccess());
} catch (NoSuchMethodException | InvocationTargetException ex) {
throw new IOException("Failed to create CraftWorldInfo", ex);
}
}
if (biomeProvider == null && generator != null) {

View File

@@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList;
import com.mojang.serialization.Lifecycle;
import com.volmit.iris.Iris;
import com.volmit.iris.core.nms.IMemoryWorld;
import com.volmit.iris.util.reflect.Reflect;
import net.minecraft.core.BlockPos;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.Registries;
@@ -110,21 +111,9 @@ public class MemoryWorld implements IMemoryWorld {
worldInfo = new CraftWorldInfo(worldData, access, creator.environment(), levelStem.type().value());
} catch (Throwable e) {
try {
var c = CraftWorldInfo.class.getDeclaredConstructor(
ServerLevelData.class,
LevelStorageSource.LevelStorageAccess.class,
World.Environment.class,
DimensionType.class,
net.minecraft.world.level.chunk.ChunkGenerator.class,
RegistryAccess.Frozen.class);
worldInfo = c.newInstance(worldData, access, creator.environment(), levelStem.type().value(), levelStem.generator(), server.registryAccess());
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException |
IllegalAccessException ex) {
var constructors = Arrays.stream(CraftWorldInfo.class.getDeclaredConstructors())
.map(Constructor::toGenericString)
.collect(Collectors.joining("\n"));
throw new IOException("Failed to find CraftWorldInfo constructor found: " + constructors, ex);
worldInfo = Reflect.newInstance(CraftWorldInfo.class, worldData, access, creator.environment(), levelStem.type().value(), levelStem.generator(), server.registryAccess());
} catch (NoSuchMethodException | InvocationTargetException ex) {
throw new IOException("Failed to create CraftWorldInfo", ex);
}
}
if (biomeProvider == null && generator != null) {

View File

@@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList;
import com.mojang.serialization.Lifecycle;
import com.volmit.iris.Iris;
import com.volmit.iris.core.nms.IMemoryWorld;
import com.volmit.iris.util.reflect.Reflect;
import com.volmit.iris.util.scheduling.J;
import net.minecraft.core.BlockPos;
import net.minecraft.core.RegistryAccess;
@@ -112,21 +113,9 @@ public class MemoryWorld implements IMemoryWorld {
worldInfo = new CraftWorldInfo(worldData, access, creator.environment(), levelStem.type().value());
} catch (Throwable e) {
try {
var c = CraftWorldInfo.class.getDeclaredConstructor(
ServerLevelData.class,
LevelStorageSource.LevelStorageAccess.class,
World.Environment.class,
DimensionType.class,
net.minecraft.world.level.chunk.ChunkGenerator.class,
RegistryAccess.Frozen.class);
worldInfo = c.newInstance(worldData, access, creator.environment(), levelStem.type().value(), levelStem.generator(), server.registryAccess());
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException |
IllegalAccessException ex) {
var constructors = Arrays.stream(CraftWorldInfo.class.getDeclaredConstructors())
.map(Constructor::toGenericString)
.collect(Collectors.joining("\n"));
throw new IOException("Failed to find CraftWorldInfo constructor found: " + constructors, ex);
worldInfo = Reflect.newInstance(CraftWorldInfo.class, worldData, access, creator.environment(), levelStem.type().value(), levelStem.generator(), server.registryAccess());
} catch (NoSuchMethodException | InvocationTargetException ex) {
throw new IOException("Failed to create CraftWorldInfo", ex);
}
}
if (biomeProvider == null && generator != null) {

View File

@@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList;
import com.mojang.serialization.Lifecycle;
import com.volmit.iris.Iris;
import com.volmit.iris.core.nms.IMemoryWorld;
import com.volmit.iris.util.reflect.Reflect;
import com.volmit.iris.util.scheduling.J;
import net.minecraft.core.BlockPos;
import net.minecraft.core.RegistryAccess;
@@ -113,22 +114,9 @@ public class MemoryWorld implements IMemoryWorld {
worldInfo = new CraftWorldInfo(worldData, access, creator.environment(), levelStem.type().value());
} catch (Throwable e) {
try {
var c = CraftWorldInfo.class.getDeclaredConstructor(
ServerLevelData.class,
LevelStorageSource.LevelStorageAccess.class,
World.Environment.class,
DimensionType.class,
net.minecraft.world.level.chunk.ChunkGenerator.class,
RegistryAccess.Frozen.class);
worldInfo = c.newInstance(worldData, access, creator.environment(), levelStem.type().value(), levelStem.generator(), server.registryAccess());
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException |
IllegalAccessException ex) {
var constructors = Arrays.stream(CraftWorldInfo.class.getDeclaredConstructors())
.map(Constructor::toGenericString)
.collect(Collectors.joining("\n"));
CraftAttributeInstance
throw new IOException("Failed to find CraftWorldInfo constructor found: " + constructors, ex);
worldInfo = Reflect.newInstance(CraftWorldInfo.class, worldData, access, creator.environment(), levelStem.type().value(), levelStem.generator(), server.registryAccess());
} catch (NoSuchMethodException | InvocationTargetException ex) {
throw new IOException("Failed to create CraftWorldInfo", ex);
}
}
if (biomeProvider == null && generator != null) {

View File

@@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList;
import com.mojang.serialization.Lifecycle;
import com.volmit.iris.Iris;
import com.volmit.iris.core.nms.IMemoryWorld;
import com.volmit.iris.util.reflect.Reflect;
import com.volmit.iris.util.scheduling.J;
import net.minecraft.core.BlockPos;
import net.minecraft.core.RegistryAccess;
@@ -112,21 +113,9 @@ public class MemoryWorld implements IMemoryWorld {
worldInfo = new CraftWorldInfo(worldData, access, creator.environment(), levelStem.type().value());
} catch (Throwable e) {
try {
var c = CraftWorldInfo.class.getDeclaredConstructor(
ServerLevelData.class,
LevelStorageSource.LevelStorageAccess.class,
World.Environment.class,
DimensionType.class,
net.minecraft.world.level.chunk.ChunkGenerator.class,
RegistryAccess.Frozen.class);
worldInfo = c.newInstance(worldData, access, creator.environment(), levelStem.type().value(), levelStem.generator(), server.registryAccess());
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException |
IllegalAccessException ex) {
var constructors = Arrays.stream(CraftWorldInfo.class.getDeclaredConstructors())
.map(Constructor::toGenericString)
.collect(Collectors.joining("\n"));
throw new IOException("Failed to find CraftWorldInfo constructor found: " + constructors, ex);
worldInfo = Reflect.newInstance(CraftWorldInfo.class, worldData, access, creator.environment(), levelStem.type().value(), levelStem.generator(), server.registryAccess());
} catch (NoSuchMethodException | InvocationTargetException ex) {
throw new IOException("Failed to create CraftWorldInfo", ex);
}
}
if (biomeProvider == null && generator != null) {

View File

@@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList;
import com.mojang.serialization.Lifecycle;
import com.volmit.iris.Iris;
import com.volmit.iris.core.nms.IMemoryWorld;
import com.volmit.iris.util.reflect.Reflect;
import com.volmit.iris.util.scheduling.J;
import net.minecraft.core.BlockPos;
import net.minecraft.core.RegistryAccess;
@@ -65,6 +66,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
@@ -116,21 +118,9 @@ public class MemoryWorld implements IMemoryWorld {
worldInfo = new CraftWorldInfo(worldData, access, creator.environment(), levelStem.type().value());
} catch (Throwable e) {
try {
var c = CraftWorldInfo.class.getDeclaredConstructor(
ServerLevelData.class,
LevelStorageSource.LevelStorageAccess.class,
World.Environment.class,
DimensionType.class,
net.minecraft.world.level.chunk.ChunkGenerator.class,
RegistryAccess.Frozen.class);
worldInfo = c.newInstance(worldData, access, creator.environment(), levelStem.type().value(), levelStem.generator(), server.registryAccess());
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException |
IllegalAccessException ex) {
var constructors = Arrays.stream(CraftWorldInfo.class.getDeclaredConstructors())
.map(Constructor::toGenericString)
.collect(Collectors.joining("\n"));
throw new IOException("Failed to find CraftWorldInfo constructor found: " + constructors, ex);
worldInfo = Reflect.newInstance(CraftWorldInfo.class, worldData, access, creator.environment(), levelStem.type().value(), levelStem.generator(), server.registryAccess());
} catch (NoSuchMethodException | InvocationTargetException ex) {
throw new IOException("Failed to create CraftWorldInfo", ex);
}
}
if (biomeProvider == null && generator != null) {

View File

@@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList;
import com.mojang.serialization.Lifecycle;
import com.volmit.iris.Iris;
import com.volmit.iris.core.nms.IMemoryWorld;
import com.volmit.iris.util.reflect.Reflect;
import com.volmit.iris.util.scheduling.J;
import net.minecraft.core.BlockPos;
import net.minecraft.core.RegistryAccess;
@@ -114,21 +115,9 @@ public class MemoryWorld implements IMemoryWorld {
worldInfo = new CraftWorldInfo(worldData, access, creator.environment(), levelStem.type().value());
} catch (Throwable e) {
try {
var c = CraftWorldInfo.class.getDeclaredConstructor(
ServerLevelData.class,
LevelStorageSource.LevelStorageAccess.class,
World.Environment.class,
DimensionType.class,
net.minecraft.world.level.chunk.ChunkGenerator.class,
RegistryAccess.Frozen.class);
worldInfo = c.newInstance(worldData, access, creator.environment(), levelStem.type().value(), levelStem.generator(), server.registryAccess());
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException |
IllegalAccessException ex) {
var constructors = Arrays.stream(CraftWorldInfo.class.getDeclaredConstructors())
.map(Constructor::toGenericString)
.collect(Collectors.joining("\n"));
throw new IOException("Failed to find CraftWorldInfo constructor found: " + constructors, ex);
worldInfo = Reflect.newInstance(CraftWorldInfo.class, worldData, access, creator.environment(), levelStem.type().value(), levelStem.generator(), server.registryAccess());
} catch (NoSuchMethodException | InvocationTargetException ex) {
throw new IOException("Failed to create CraftWorldInfo", ex);
}
}
if (biomeProvider == null && generator != null) {

View File

@@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList;
import com.mojang.serialization.Lifecycle;
import com.volmit.iris.Iris;
import com.volmit.iris.core.nms.IMemoryWorld;
import com.volmit.iris.util.reflect.Reflect;
import com.volmit.iris.util.scheduling.J;
import net.minecraft.core.BlockPos;
import net.minecraft.core.RegistryAccess;
@@ -115,21 +116,9 @@ public class MemoryWorld implements IMemoryWorld {
worldInfo = new CraftWorldInfo(worldData, access, creator.environment(), levelStem.type().value());
} catch (Throwable e) {
try {
var c = CraftWorldInfo.class.getDeclaredConstructor(
ServerLevelData.class,
LevelStorageSource.LevelStorageAccess.class,
World.Environment.class,
DimensionType.class,
net.minecraft.world.level.chunk.ChunkGenerator.class,
RegistryAccess.Frozen.class);
worldInfo = c.newInstance(worldData, access, creator.environment(), levelStem.type().value(), levelStem.generator(), server.registryAccess());
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException |
IllegalAccessException ex) {
var constructors = Arrays.stream(CraftWorldInfo.class.getDeclaredConstructors())
.map(Constructor::toGenericString)
.collect(Collectors.joining("\n"));
throw new IOException("Failed to find CraftWorldInfo constructor found: " + constructors, ex);
worldInfo = Reflect.newInstance(CraftWorldInfo.class, worldData, access, creator.environment(), levelStem.type().value(), levelStem.generator(), server.registryAccess());
} catch (NoSuchMethodException | InvocationTargetException ex) {
throw new IOException("Failed to create CraftWorldInfo", ex);
}
}
if (biomeProvider == null && generator != null) {