Unfreeze block registry ourselves in tests

This commit is contained in:
embeddedt 2024-07-27 12:51:24 -04:00
parent 6a365be734
commit 6f4212ebc8
No known key found for this signature in database
GPG Key ID: A69433EC199B5613

View File

@ -1,12 +1,17 @@
package org.embeddedt.modernfix.testing.util;
import net.minecraft.DetectedVersion;
import net.minecraft.core.MappedRegistry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.server.Bootstrap;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import java.lang.reflect.Field;
import java.util.IdentityHashMap;
/**
* Simple extension to run vanilla bootstrap, inspired by AE2.
*/
@ -15,6 +20,15 @@ public class BootstrapMinecraftExtension implements Extension, BeforeAllCallback
public void beforeAll(ExtensionContext context) throws Exception {
DetectedVersion.tryDetectVersion();
Bootstrap.bootStrap();
// Allow blocks to be created in tests
Field field = MappedRegistry.class.getDeclaredField("unregisteredIntrusiveHolders");
field.setAccessible(true);
if(field.get(BuiltInRegistries.BLOCK) == null) {
field.set(BuiltInRegistries.BLOCK, new IdentityHashMap<>());
field = MappedRegistry.class.getDeclaredField("frozen");
field.setAccessible(true);
field.setBoolean(BuiltInRegistries.BLOCK, false);
}
}
@Override