mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-17 19:15:11 +00:00
fix: restore Schematica registry reflection compatibility
This commit is contained in:
1 parent
8d0ba7c76e
commit
49ed6fd751
7 files changed
+101
-21
No files matched your search
@@ -6,6 +6,7 @@ import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -13,17 +14,22 @@ import java.util.NoSuchElementException;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import codechicken.lib.data.MCDataInput;
|
||||
import codechicken.lib.vec.BlockCoord;
|
||||
import codechicken.multipart.MultiPartRegistry.IPartConverter;
|
||||
import codechicken.multipart.MultiPartRegistry.IPartFactory2;
|
||||
import cpw.mods.fml.relauncher.ReflectionHelper;
|
||||
import scala.Option;
|
||||
|
||||
/**
|
||||
* Part registration needs FML's active mod container, so it cannot run headless; that half of the registry is
|
||||
* characterized in the Forge server suite instead. Converter registration and dispatch touch neither FML nor the
|
||||
* logger, so they are covered here.
|
||||
* characterized in the Forge server suite instead. Converter dispatch and the Schematica reflection contract touch
|
||||
* neither FML nor the logger, so they are covered here.
|
||||
*/
|
||||
class MultiPartRegistryCharacterizationTest {
|
||||
|
||||
@@ -37,6 +43,30 @@ class MultiPartRegistryCharacterizationTest {
|
||||
assertThrows(NoSuchElementException.class, () -> MultiPartRegistry.getModContainer("test:nosuchpart"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void schematicaCanReadTheLiveScalaTypeMap() throws IllegalAccessException {
|
||||
Field moduleField = ReflectionHelper.findField(MultiPartRegistry$.class, "MODULE$");
|
||||
Field typeMapField = ReflectionHelper
|
||||
.findField(MultiPartRegistry$.class, "codechicken$multipart$MultiPartRegistry$$typeMap");
|
||||
Object module = moduleField.get(MultiPartRegistry$.class);
|
||||
scala.collection.mutable.Map<String, Object> reflectedTypeMap = (scala.collection.mutable.Map<String, Object>) typeMapField
|
||||
.get(module);
|
||||
|
||||
String type = "test:schematica_reflection";
|
||||
TestPart part = new TestPart();
|
||||
TestFactory factory = new TestFactory(part);
|
||||
reflectedTypeMap.put(type, factory);
|
||||
try {
|
||||
Option<Object> reflectedFactory = reflectedTypeMap.get(type);
|
||||
assertFalse(reflectedFactory.isEmpty());
|
||||
assertSame(factory, reflectedFactory.get());
|
||||
assertSame(part, MultiPartRegistry.loadPart(type, new NBTTagCompound()));
|
||||
} finally {
|
||||
reflectedTypeMap.remove(type);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void convertersAreDispatchedPerBlockAndFirstNonNullWins() {
|
||||
Block block = new Block(Material.rock) {};
|
||||
@@ -110,4 +140,23 @@ class MultiPartRegistryCharacterizationTest {
|
||||
return "test:converted";
|
||||
}
|
||||
}
|
||||
|
||||
private static final class TestFactory implements IPartFactory2 {
|
||||
|
||||
private final TMultiPart part;
|
||||
|
||||
private TestFactory(TMultiPart part) {
|
||||
this.part = part;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TMultiPart createPart(String name, NBTTagCompound nbt) {
|
||||
return part;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TMultiPart createPart(String name, MCDataInput packet) {
|
||||
return part;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user