Another pass at substitutions - now capable of fixing up ItemBlock to point at the right block when a block is substituted, should also handle substituting itemblocks. There's some hackery in the way we have to change the value of ItemBlock.block, but it's not too egregious in my opinion.

This commit is contained in:
cpw committed 2016-07-29 17:08:52 -04:00
1 parent 53b43a6e1f
commit 4dcee246d5
12 files changed
+328 -87

No files matched your search

@@ -39,9 +39,9 @@ public class FreezingTests
{
Loader.instance();
System.setProperty("fml.queryResult", "confirm");
registry = PersistentRegistryManager.createRegistry(resloc, RTest.class, null, 0, 255, false, null, null, null);
PersistentRegistryManager.createRegistry(PersistentRegistryManager.BLOCKS, Block.class, null, 0, 255, false, null, null, null);
PersistentRegistryManager.createRegistry(PersistentRegistryManager.ITEMS, Item.class, null, 0, 255, false, null, null, null);
registry = PersistentRegistryManager.createRegistry(resloc, RTest.class, null, 0, 255, false, null, null, null, null);
PersistentRegistryManager.createRegistry(PersistentRegistryManager.BLOCKS, Block.class, null, 0, 255, false, null, null, null, null);
PersistentRegistryManager.createRegistry(PersistentRegistryManager.ITEMS, Item.class, null, 0, 255, false, null, null, null, null);
r1 = new RTest("test1");
r2 = new RTest("test2");
r3 = new RTest("test3");
@@ -58,9 +58,9 @@ public class FreezingTests
ss.entries.put(resloc, new PersistentRegistryManager.GameDataSnapshot.Entry(PersistentRegistryManager.PersistentRegistry.ACTIVE.getRegistry(RTest.class)));
PersistentRegistryManager.PersistentRegistry.ACTIVE.clean();
PersistentRegistryManager.PersistentRegistry.FROZEN.clean();
registry = PersistentRegistryManager.createRegistry(resloc, RTest.class, null, 0, 255, false, null, null, null);
PersistentRegistryManager.createRegistry(PersistentRegistryManager.BLOCKS, Block.class, null, 0, 255, false, null, null, null);
PersistentRegistryManager.createRegistry(PersistentRegistryManager.ITEMS, Item.class, null, 0, 255, false, null, null, null);
registry = PersistentRegistryManager.createRegistry(resloc, RTest.class, null, 0, 255, false, null, null, null, null);
PersistentRegistryManager.createRegistry(PersistentRegistryManager.BLOCKS, Block.class, null, 0, 255, false, null, null, null, null);
PersistentRegistryManager.createRegistry(PersistentRegistryManager.ITEMS, Item.class, null, 0, 255, false, null, null, null, null);
}
@Test
@@ -0,0 +1,88 @@
package net.minecraftforge.fml.common.registry;
import com.google.common.base.Function;
import net.minecraft.block.Block;
import net.minecraft.block.BlockDirt;
import net.minecraft.init.Blocks;
import net.minecraft.init.Bootstrap;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemMultiTexture;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.DummyModContainer;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.ModMetadata;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import javax.annotation.Nullable;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
/**
* Substitution test harness - tests that substitutions behave correctly
*/
@RunWith(ForgeTestRunner.class)
public class ItemBlockSubstitutionRemoveRestoreTest
{
private ResourceLocation myDirt = new ResourceLocation("minecraft:dirt");
private static class ItemMyDirt extends ItemMultiTexture
{
public ItemMyDirt() {
super(Blocks.DIRT, Blocks.DIRT, new Function<ItemStack, String>()
{
@Nullable
public String apply(@Nullable ItemStack p_apply_1_)
{
return BlockDirt.DirtType.byMetadata(p_apply_1_.getMetadata()).getUnlocalizedName();
}
});
}
}
private static ItemMyDirt myDirtInstance;
private static Item originalDirt;
@BeforeClass
public static void setup()
{
Loader.instance();
Bootstrap.register();
myDirtInstance = new ItemMyDirt();
Loader.instance().setupTestHarness(new DummyModContainer(new ModMetadata() {{
modId = "test";
}}));
originalDirt = new ItemStack(Blocks.DIRT).getItem();
}
@Test
public void testSubstitutionRemovalAndRestore() throws Exception
{
GameRegistry.addSubstitutionAlias("minecraft:dirt", GameRegistry.Type.ITEM, myDirtInstance);
PersistentRegistryManager.freezeData();
ObjectHolderRegistry.INSTANCE.applyObjectHolders();
final FMLControlledNamespacedRegistry<Item> itemRegistry = (FMLControlledNamespacedRegistry<Item>)PersistentRegistryManager.findRegistryByType(Item.class);
// TEST 1: Does my substitute take effect? The substitute should be found in the registry
ItemBlock dirtitem = (ItemBlock)itemRegistry.getValue(myDirt);
assertEquals("ItemBlock points at my block", myDirtInstance, dirtitem);
// TEST 2: Does the substitute get removed when told by remote operation? The substitute should NOT be found in the registry
final PersistentRegistryManager.GameDataSnapshot snapshot = PersistentRegistryManager.takeSnapshot();
snapshot.entries.get(PersistentRegistryManager.ITEMS).substitutions.clear();
PersistentRegistryManager.injectSnapshot(snapshot, false, false);
ObjectHolderRegistry.INSTANCE.applyObjectHolders();
dirtitem = (ItemBlock)itemRegistry.getValue(myDirt);
assertEquals("ItemBlock points at vanilla block", originalDirt, dirtitem);
assertNotEquals("ItemBlock points at my block", myDirtInstance, dirtitem);
// TEST 3: Does the substitute get restored when reverting to frozen state? The substitute should be found in the registry again
PersistentRegistryManager.revertToFrozen();
ObjectHolderRegistry.INSTANCE.applyObjectHolders();
dirtitem = (ItemBlock)itemRegistry.getValue(myDirt);
assertEquals("ItemBlock points at my block", myDirtInstance, dirtitem);
}
}
@@ -7,7 +7,7 @@ import org.junit.runners.Suite;
* Run the full suite of tests
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({VanillaRegistryTests.class, FreezingTests.class, SubstitutionRemoveRestoreTest.class, SubstitutionInjectionTest.class, DummyBlockReplacementTest.class})
@Suite.SuiteClasses({VanillaRegistryTests.class, FreezingTests.class, SubstitutionRemoveRestoreTest.class, SubstitutionInjectionTest.class, DummyBlockReplacementTest.class, BlockStateMappingsTest.class})
public class RegistryTestSuite
{
}
@@ -4,6 +4,8 @@ import net.minecraft.block.Block;
import net.minecraft.block.BlockDirt;
import net.minecraft.init.Blocks;
import net.minecraft.init.Bootstrap;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.stats.StatList;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.DummyModContainer;
@@ -15,6 +17,7 @@ import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.fail;
/**
* Substitution test harness - tests that substitutions behave correctly
@@ -40,24 +43,44 @@ public class SubstitutionInjectionTest
@Test
public void testSubstitutionInjection() throws Exception
{
final FMLControlledNamespacedRegistry<Block> blockRegistry = (FMLControlledNamespacedRegistry<Block>)PersistentRegistryManager.findRegistryByType(Block.class);
final FMLControlledNamespacedRegistry<Item> itemRegistry = (FMLControlledNamespacedRegistry<Item>)PersistentRegistryManager.findRegistryByType(Item.class);
// Capture snapshot prior to registering the substitution - this is a world state "pre-substitute"
final PersistentRegistryManager.GameDataSnapshot snapshot = PersistentRegistryManager.takeSnapshot();
Block fnd = blockRegistry.getValue(myDirt);
Block currDirt = Blocks.DIRT;
// TEST 0: Verify that input state is correct
assertEquals("Got vanilla dirt ", currDirt, fnd);
// TEST 0a: Validate that the ItemBlock for Dirt points at vanilla dirt
ItemBlock dirtitem = (ItemBlock)itemRegistry.getValue(myDirt);
assertEquals("ItemBlock points at my block", currDirt, dirtitem.block);
GameRegistry.addSubstitutionAlias("minecraft:dirt", GameRegistry.Type.BLOCK, toSub);
PersistentRegistryManager.freezeData();
ObjectHolderRegistry.INSTANCE.applyObjectHolders();
// This should not throw an exception
StatList.reinit();
final FMLControlledNamespacedRegistry<Block> blockRegistry = (FMLControlledNamespacedRegistry<Block>)PersistentRegistryManager.findRegistryByType(Block.class);
try
{
StatList.reinit();
} catch (Exception e)
{
fail("Caught exception");
}
// TEST 1: Does my substitute take effect? The substitute should be found in the registry
Block fnd = blockRegistry.getValue(myDirt);
Block currDirt = Blocks.DIRT;
fnd = blockRegistry.getValue(myDirt);
currDirt = Blocks.DIRT;
assertEquals("Got my dirt substitute - Blocks", toSub, currDirt);
assertEquals("Got my dirt substitute - Blocks and registry", currDirt, fnd);
assertEquals("Got my dirt substitute - registry", toSub, fnd);
// TEST 1a: Validate that the ItemBlock for Dirt now points at my dirt
dirtitem = (ItemBlock)itemRegistry.getValue(myDirt);
assertEquals("ItemBlock points at my block", toSub, dirtitem.block);
// TEST 2: Does the substitute get injected when told by loading operation? The substitute should be found in the registry
PersistentRegistryManager.injectSnapshot(snapshot, true, true);
ObjectHolderRegistry.INSTANCE.applyObjectHolders();
@@ -67,6 +90,9 @@ public class SubstitutionInjectionTest
assertEquals("Got my dirt substitute - Blocks and registry", currDirt, fnd);
assertEquals("Got my dirt substitute - registry", toSub, fnd);
dirtitem = (ItemBlock)itemRegistry.getValue(myDirt);
assertEquals("ItemBlock points at my block", toSub, dirtitem.block);
// TEST 3: Does the substitute get restored when reverting to frozen state? The substitute should be found in the registry again
PersistentRegistryManager.revertToFrozen();
ObjectHolderRegistry.INSTANCE.applyObjectHolders();
@@ -75,5 +101,7 @@ public class SubstitutionInjectionTest
assertEquals("Got my dirt substitute - Blocks", toSub, currDirt);
assertEquals("Got my dirt substitute - Blocks and registry", currDirt, fnd);
assertEquals("Got my dirt substitute - registry", toSub, fnd);
dirtitem = (ItemBlock)itemRegistry.getValue(myDirt);
assertEquals("ItemBlock points at my block", toSub, dirtitem.block);
}
}
@@ -4,6 +4,8 @@ import net.minecraft.block.Block;
import net.minecraft.block.BlockDirt;
import net.minecraft.init.Blocks;
import net.minecraft.init.Bootstrap;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.DummyModContainer;
import net.minecraftforge.fml.common.Loader;
@@ -44,6 +46,7 @@ public class SubstitutionRemoveRestoreTest
ObjectHolderRegistry.INSTANCE.applyObjectHolders();
final FMLControlledNamespacedRegistry<Block> blockRegistry = (FMLControlledNamespacedRegistry<Block>)PersistentRegistryManager.findRegistryByType(Block.class);
final FMLControlledNamespacedRegistry<Item> itemRegistry = (FMLControlledNamespacedRegistry<Item>)PersistentRegistryManager.findRegistryByType(Item.class);
// TEST 1: Does my substitute take effect? The substitute should be found in the registry
Block fnd = blockRegistry.getValue(myDirt);
@@ -52,6 +55,9 @@ public class SubstitutionRemoveRestoreTest
assertEquals("Got my dirt substitute - Blocks and registry", currDirt, fnd);
assertEquals("Got my dirt substitute - registry", toSub, fnd);
ItemBlock dirtitem = (ItemBlock)itemRegistry.getValue(myDirt);
assertEquals("ItemBlock points at my block", toSub, dirtitem.block);
// TEST 2: Does the substitute get removed when told by remote operation? The substitute should NOT be found in the registry
final PersistentRegistryManager.GameDataSnapshot snapshot = PersistentRegistryManager.takeSnapshot();
snapshot.entries.get(PersistentRegistryManager.BLOCKS).substitutions.clear();
@@ -63,6 +69,10 @@ public class SubstitutionRemoveRestoreTest
assertEquals("Got my dirt substitute - Blocks and registry", currDirt, fnd);
assertNotEquals("Got my dirt substitute - registry", toSub, fnd);
dirtitem = (ItemBlock)itemRegistry.getValue(myDirt);
assertEquals("ItemBlock points at vanilla block", currDirt, dirtitem.block);
assertNotEquals("ItemBlock points at my block", toSub, dirtitem.block);
// TEST 3: Does the substitute get restored when reverting to frozen state? The substitute should be found in the registry again
PersistentRegistryManager.revertToFrozen();
ObjectHolderRegistry.INSTANCE.applyObjectHolders();
@@ -71,5 +81,8 @@ public class SubstitutionRemoveRestoreTest
assertEquals("Got my dirt substitute - Blocks", toSub, currDirt);
assertEquals("Got my dirt substitute - Blocks and registry", currDirt, fnd);
assertEquals("Got my dirt substitute - registry", toSub, fnd);
dirtitem = (ItemBlock)itemRegistry.getValue(myDirt);
assertEquals("ItemBlock points at my block", toSub, dirtitem.block);
}
}