Find the Iris generator behind other plugins' generator wrappers

CraftEngine wraps the active chunk generator during WorldInitEvent, after
Iris injected its own. Every identity check against the active generator
then failed: studio open threw while scoping structures, and native
structure creation, references and locating silently did nothing.
This commit is contained in:
piexel committed 2026-11-10 16:53:11 +01:00
1 parent 3c6dc52b8e
commit a00e4bbdf0
4 files changed
+84 -12

No files matched your search

@@ -172,7 +172,7 @@ public class IrisChunkGenerator extends CustomChunkGenerator {
if (platformGenerator != null && !platformGenerator.shouldGenerateStructures()) {
return null;
}
if (level != runtimeLevel || level.getChunkSource().getGenerator() != this) {
if (level != runtimeLevel || !isActiveGeneratorOf(level)) {
return null;
}
try (GenerationSessionLease lease = requireGenerationLease("bukkit_nms_structure_locate");
@@ -348,7 +348,7 @@ public class IrisChunkGenerator extends CustomChunkGenerator {
if (platformGenerator != null && !platformGenerator.shouldGenerateStructures()) {
return;
}
if (runtimeLevel.getChunkSource().getGenerator() != this
if (!isActiveGeneratorOf(runtimeLevel)
|| runtimeLevel.getChunkSource().getGeneratorState() != structureState) {
return;
}
@@ -599,11 +599,15 @@ public class IrisChunkGenerator extends CustomChunkGenerator {
private void requireCurrentStructureOwner(ServerLevel level, ChunkMap chunkMap) {
if (runtimeLevel != level
|| level.getChunkSource().chunkMap != chunkMap
|| level.getChunkSource().getGenerator() != this) {
|| !isActiveGeneratorOf(level)) {
throw new IllegalStateException("Iris native structure state no longer belongs to the active world runtime.");
}
}
private boolean isActiveGeneratorOf(ServerLevel level) {
return WrappedGenerators.irisGeneratorOf(level.getChunkSource().getGenerator()) == this;
}
private void publishStructureState(StructureStatePublisher publisher) {
try {
publisher.publish();
@@ -617,7 +621,7 @@ public class IrisChunkGenerator extends CustomChunkGenerator {
if (platformGenerator != null && !platformGenerator.shouldGenerateStructures()) {
return;
}
if (runtimeLevel.getChunkSource().getGenerator() != this) {
if (!isActiveGeneratorOf(runtimeLevel)) {
return;
}
try (BukkitChunkGenerator.GenerationStagePermit stage = requireGenerationStage("bukkit_nms_create_references");
@@ -1328,8 +1328,8 @@ public class NMSBinding implements INMSBinding {
@Override
public void abandonStudioStructureBootstrap(World world) {
ServerLevel level = ((CraftWorld) world).getHandle();
net.minecraft.world.level.chunk.ChunkGenerator generator = level.getChunkSource().getGenerator();
if (generator instanceof IrisChunkGenerator irisGenerator) {
IrisChunkGenerator irisGenerator = WrappedGenerators.irisGeneratorOf(level.getChunkSource().getGenerator());
if (irisGenerator != null) {
irisGenerator.abandonStudioStructureState();
}
}
@@ -1351,7 +1351,8 @@ public class NMSBinding implements INMSBinding {
ChunkGeneratorStructureState structureState,
IrisChunkGenerator.StructureStatePublisher publisher
) throws IllegalAccessException {
if (generator instanceof IrisChunkGenerator irisGenerator) {
IrisChunkGenerator irisGenerator = WrappedGenerators.irisGeneratorOf(generator);
if (irisGenerator != null) {
irisGenerator.initializeAndPublishStructureState(structureState, publisher);
return;
}
@@ -1362,10 +1363,12 @@ public class NMSBinding implements INMSBinding {
private IrisChunkGenerator requireIrisGenerator(
net.minecraft.world.level.chunk.ChunkGenerator generator
) {
if (generator instanceof IrisChunkGenerator irisGenerator) {
IrisChunkGenerator irisGenerator = WrappedGenerators.irisGeneratorOf(generator);
if (irisGenerator != null) {
return irisGenerator;
}
throw new IllegalStateException("Studio native structure state is not owned by the active Iris generator.");
throw new IllegalStateException("Studio native structure state is not owned by the active Iris generator; active="
+ generator.getClass().getName());
}
private void validateDimensionContract(Engine engine, World world, ServerLevel level) {
@@ -0,0 +1,65 @@
package art.arcane.iris.core.nms.v26_2_R1;
import net.minecraft.world.level.chunk.ChunkGenerator;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Optional;
/**
* Finds the Iris generator behind a level's active generator. Other plugins (CraftEngine, for one)
* wrap the active generator in a delegating generator of their own during WorldInitEvent, after
* Iris has injected its generator, so the active generator is not always Iris itself.
*/
final class WrappedGenerators {
private static final int MAX_WRAPPER_DEPTH = 4;
private static final ClassValue<Optional<Field>> WRAPPED_GENERATOR_FIELDS = new ClassValue<>() {
@Override
protected Optional<Field> computeValue(Class<?> wrapperType) {
return findWrappedGeneratorField(wrapperType);
}
};
private WrappedGenerators() {
}
static IrisChunkGenerator irisGeneratorOf(ChunkGenerator activeGenerator) {
ChunkGenerator current = activeGenerator;
for (int depth = 0; current != null && depth <= MAX_WRAPPER_DEPTH; depth++) {
if (current instanceof IrisChunkGenerator irisGenerator) {
return irisGenerator;
}
current = wrappedBy(current);
}
return null;
}
private static ChunkGenerator wrappedBy(ChunkGenerator wrapper) {
Optional<Field> field = WRAPPED_GENERATOR_FIELDS.get(wrapper.getClass());
if (field.isEmpty()) {
return null;
}
try {
return field.get().get(wrapper) instanceof ChunkGenerator wrapped && wrapped != wrapper ? wrapped : null;
} catch (IllegalAccessException unreadable) {
return null;
}
}
private static Optional<Field> findWrappedGeneratorField(Class<?> wrapperType) {
for (Class<?> type = wrapperType; type != null && type != ChunkGenerator.class; type = type.getSuperclass()) {
for (Field field : type.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers()) || !ChunkGenerator.class.isAssignableFrom(field.getType())) {
continue;
}
try {
field.setAccessible(true);
return Optional.of(field);
} catch (RuntimeException inaccessible) {
return Optional.empty();
}
}
}
return Optional.empty();
}
}
@@ -33,7 +33,7 @@ public class IrisChunkGeneratorFailureContractTest {
int reachabilityEnd = source.indexOf("protected MapCodec", reachabilityStart);
String reachability = source.substring(reachabilityStart, reachabilityEnd);
int bootstrapGate = locate.indexOf("!platformGenerator.shouldGenerateStructures()");
int generatorIdentity = locate.indexOf("level.getChunkSource().getGenerator() != this");
int generatorIdentity = locate.indexOf("!isActiveGeneratorOf(level)");
int lease = locate.indexOf("requireGenerationLease(\"bukkit_nms_structure_locate\")");
int nativePrediction = locate.indexOf("NativeStructureVanillaLocator.predict(");
@@ -97,7 +97,7 @@ public class IrisChunkGeneratorFailureContractTest {
String references = source.substring(referencesStart, referencesEnd);
int nativeStructureGate = references.indexOf("!platformGenerator.shouldGenerateStructures()");
int generatorIdentity = references.indexOf("runtimeLevel.getChunkSource().getGenerator() != this");
int generatorIdentity = references.indexOf("!isActiveGeneratorOf(runtimeLevel)");
int stage = references.indexOf("requireGenerationStage(\"bukkit_nms_create_references\")");
int lease = references.indexOf("requireGenerationLease(\"bukkit_nms_create_references\")");
int repair = references.indexOf("NativeStructureReferenceRepair.createReferences(");
@@ -120,7 +120,7 @@ public class IrisChunkGeneratorFailureContractTest {
String generation = source.substring(generationStart, generationEnd);
int gate = generation.indexOf("!platformGenerator.shouldGenerateStructures()");
int currentGenerator = generation.indexOf(
"runtimeLevel.getChunkSource().getGenerator() != this");
"!isActiveGeneratorOf(runtimeLevel)");
int stateIdentity = generation.indexOf(
"runtimeLevel.getChunkSource().getGeneratorState() != structureState");
int stage = generation.indexOf(