diff --git a/adapters/bukkit/nms/v26_2_R1/src/main/java/art/arcane/iris/core/nms/v26_2_R1/IrisChunkGenerator.java b/adapters/bukkit/nms/v26_2_R1/src/main/java/art/arcane/iris/core/nms/v26_2_R1/IrisChunkGenerator.java index a93fd7906..5dd091050 100644 --- a/adapters/bukkit/nms/v26_2_R1/src/main/java/art/arcane/iris/core/nms/v26_2_R1/IrisChunkGenerator.java +++ b/adapters/bukkit/nms/v26_2_R1/src/main/java/art/arcane/iris/core/nms/v26_2_R1/IrisChunkGenerator.java @@ -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"); diff --git a/adapters/bukkit/nms/v26_2_R1/src/main/java/art/arcane/iris/core/nms/v26_2_R1/NMSBinding.java b/adapters/bukkit/nms/v26_2_R1/src/main/java/art/arcane/iris/core/nms/v26_2_R1/NMSBinding.java index 63eb0975e..c8873d832 100644 --- a/adapters/bukkit/nms/v26_2_R1/src/main/java/art/arcane/iris/core/nms/v26_2_R1/NMSBinding.java +++ b/adapters/bukkit/nms/v26_2_R1/src/main/java/art/arcane/iris/core/nms/v26_2_R1/NMSBinding.java @@ -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) { diff --git a/adapters/bukkit/nms/v26_2_R1/src/main/java/art/arcane/iris/core/nms/v26_2_R1/WrappedGenerators.java b/adapters/bukkit/nms/v26_2_R1/src/main/java/art/arcane/iris/core/nms/v26_2_R1/WrappedGenerators.java new file mode 100644 index 000000000..69bc96226 --- /dev/null +++ b/adapters/bukkit/nms/v26_2_R1/src/main/java/art/arcane/iris/core/nms/v26_2_R1/WrappedGenerators.java @@ -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> WRAPPED_GENERATOR_FIELDS = new ClassValue<>() { + @Override + protected Optional 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 = 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 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(); + } +} diff --git a/adapters/bukkit/nms/v26_2_R1/src/test/java/art/arcane/iris/core/nms/v26_2_R1/IrisChunkGeneratorFailureContractTest.java b/adapters/bukkit/nms/v26_2_R1/src/test/java/art/arcane/iris/core/nms/v26_2_R1/IrisChunkGeneratorFailureContractTest.java index a38b3448f..64e77ebca 100644 --- a/adapters/bukkit/nms/v26_2_R1/src/test/java/art/arcane/iris/core/nms/v26_2_R1/IrisChunkGeneratorFailureContractTest.java +++ b/adapters/bukkit/nms/v26_2_R1/src/test/java/art/arcane/iris/core/nms/v26_2_R1/IrisChunkGeneratorFailureContractTest.java @@ -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(