From 46d57dc4677fa5ff3923e64eaccfb33d7e5aad8d Mon Sep 17 00:00:00 2001 From: LexManos Date: Tue, 22 Mar 2016 01:00:25 -0700 Subject: [PATCH] Some registry tweaking to provde a non-complicated API modders can use. --- .../net/minecraft/block/Block.java.patch | 92 ++++------------- .../net/minecraft/item/Item.java.patch | 80 +++------------ .../net/minecraft/potion/Potion.java.patch | 7 +- .../world/biome/BiomeGenBase.java.patch | 9 ++ .../client/model/ModelLoader.java | 10 +- .../common/util/BlockSnapshot.java | 6 +- .../minecraftforge/fluids/FluidRegistry.java | 8 +- .../FMLControlledNamespacedRegistry.java | 99 ++++++++++++++++++- .../fml/common/registry/GameData.java | 4 +- .../fml/common/registry/GameRegistry.java | 18 +--- .../fml/common/registry/IForgeRegistry.java | 22 +++++ .../common/registry/IForgeRegistryEntry.java | 76 ++++++++++++++ .../registry/PersistentRegistryManager.java | 33 +++---- .../fml/common/registry/RegistryDelegate.java | 20 +--- .../fml/common/registry/VillagerRegistry.java | 2 +- .../minecraftforge/oredict/OreDictionary.java | 6 +- 16 files changed, 272 insertions(+), 220 deletions(-) create mode 100644 src/main/java/net/minecraftforge/fml/common/registry/IForgeRegistry.java create mode 100644 src/main/java/net/minecraftforge/fml/common/registry/IForgeRegistryEntry.java diff --git a/patches/minecraft/net/minecraft/block/Block.java.patch b/patches/minecraft/net/minecraft/block/Block.java.patch index e180526660..1a1aef513d 100644 --- a/patches/minecraft/net/minecraft/block/Block.java.patch +++ b/patches/minecraft/net/minecraft/block/Block.java.patch @@ -1,7 +1,11 @@ --- ../src-base/minecraft/net/minecraft/block/Block.java +++ ../src-work/minecraft/net/minecraft/block/Block.java -@@ -46,8 +46,9 @@ - public class Block +@@ -43,11 +43,12 @@ + import net.minecraftforge.fml.relauncher.Side; + import net.minecraftforge.fml.relauncher.SideOnly; + +-public class Block ++public class Block extends net.minecraftforge.fml.common.registry.IForgeRegistryEntry.Impl { private static final ResourceLocation field_176230_a = new ResourceLocation("air"); - public static final RegistryNamespacedDefaultedByKey field_149771_c = new RegistryNamespacedDefaultedByKey(field_176230_a); @@ -12,17 +16,7 @@ public static final AxisAlignedBB field_185505_j = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 1.0D, 1.0D); public static final AxisAlignedBB field_185506_k = null; private CreativeTabs field_149772_a; -@@ -70,6 +71,9 @@ - private IBlockState field_176228_M; - private String field_149770_b; - -+ public final net.minecraftforge.fml.common.registry.RegistryDelegate delegate = -+ ((net.minecraftforge.fml.common.registry.FMLControlledNamespacedRegistry)field_149771_c).getDelegate(this, Block.class); -+ - public static int func_149682_b(Block p_149682_0_) - { - return field_149771_c.func_148757_b(p_149682_0_); -@@ -83,7 +87,8 @@ +@@ -83,7 +84,8 @@ public static Block func_149729_e(int p_149729_0_) { @@ -32,7 +26,7 @@ } public static IBlockState func_176220_d(int p_176220_0_) -@@ -268,7 +273,7 @@ +@@ -268,7 +270,7 @@ public boolean func_176200_f(IBlockAccess p_176200_1_, BlockPos p_176200_2_) { @@ -41,7 +35,7 @@ } public Block func_149711_c(float p_149711_1_) -@@ -305,9 +310,10 @@ +@@ -305,9 +307,10 @@ return this.field_149789_z; } @@ -53,7 +47,7 @@ } public AxisAlignedBB func_185496_a(IBlockState p_185496_1_, IBlockAccess p_185496_2_, BlockPos p_185496_3_) -@@ -346,13 +352,13 @@ +@@ -346,13 +349,13 @@ @SideOnly(Side.CLIENT) public int func_185484_c(IBlockState p_185484_1_, IBlockAccess p_185484_2_, BlockPos p_185484_3_) { @@ -69,7 +63,7 @@ } else { -@@ -415,7 +421,7 @@ +@@ -415,7 +418,7 @@ } } @@ -78,7 +72,7 @@ } @SideOnly(Side.CLIENT) -@@ -472,6 +478,10 @@ +@@ -472,6 +475,10 @@ public void func_180663_b(World p_180663_1_, BlockPos p_180663_2_, IBlockState p_180663_3_) { @@ -89,7 +83,7 @@ } public int func_149745_a(Random p_149745_1_) -@@ -486,8 +496,7 @@ +@@ -486,8 +493,7 @@ public float func_180647_a(IBlockState p_180647_1_, EntityPlayer p_180647_2_, World p_180647_3_, BlockPos p_180647_4_) { @@ -99,7 +93,7 @@ } public final void func_176226_b(World p_176226_1_, BlockPos p_176226_2_, IBlockState p_176226_3_, int p_176226_4_) -@@ -497,20 +506,16 @@ +@@ -497,20 +503,16 @@ public void func_180653_a(World p_180653_1_, BlockPos p_180653_2_, IBlockState p_180653_3_, float p_180653_4_, int p_180653_5_) { @@ -125,7 +119,7 @@ } } } -@@ -518,8 +523,13 @@ +@@ -518,8 +520,13 @@ public static void func_180635_a(World p_180635_0_, BlockPos p_180635_1_, ItemStack p_180635_2_) { @@ -140,7 +134,7 @@ float f = 0.5F; double d0 = (double)(p_180635_0_.field_73012_v.nextFloat() * f) + (double)(1.0F - f) * 0.5D; double d1 = (double)(p_180635_0_.field_73012_v.nextFloat() * f) + (double)(1.0F - f) * 0.5D; -@@ -588,7 +598,7 @@ +@@ -588,7 +595,7 @@ public boolean func_176196_c(World p_176196_1_, BlockPos p_176196_2_) { @@ -149,7 +143,7 @@ } public boolean func_180639_a(World p_180639_1_, BlockPos p_180639_2_, IBlockState p_180639_3_, EntityPlayer p_180639_4_, EnumHand p_180639_5_, ItemStack p_180639_6_, EnumFacing p_180639_7_, float p_180639_8_, float p_180639_9_, float p_180639_10_) -@@ -638,25 +648,35 @@ +@@ -638,25 +645,35 @@ p_180657_2_.func_71029_a(StatList.func_188055_a(this)); p_180657_2_.func_71020_j(0.025F); @@ -188,7 +182,7 @@ } protected ItemStack func_180643_i(IBlockState p_180643_1_) -@@ -747,6 +767,7 @@ +@@ -747,6 +764,7 @@ p_176216_2_.field_70181_x = 0.0D; } @@ -196,7 +190,7 @@ public ItemStack func_185473_a(World p_185473_1_, BlockPos p_185473_2_, IBlockState p_185473_3_) { return new ItemStack(Item.func_150898_a(this), 1, this.func_180651_a(p_185473_3_)); -@@ -844,6 +865,1167 @@ +@@ -844,6 +862,1119 @@ return "Block{" + field_149771_c.func_177774_c(this) + "}"; } @@ -1311,54 +1305,6 @@ + } + } + -+ private ResourceLocation registryName = null; -+ /** -+ * Sets a unique name for this Block. This should be used for uniquely identify the instance of the Block. -+ * This is the valid replacement for the atrocious 'getUnlocalizedName().substring(6)' stuff that everyone does. -+ * Unlocalized names have NOTHING to do with unique identifiers. As demonstrated by vanilla blocks and items. -+ * -+ * The supplied name will be prefixed with the currently active mod's modId. -+ * If the supplied name already has a prefix that is different, it will be used and a warning will be logged. -+ * -+ * If a name already exists, or this Block is already registered in a registry, then an IllegalStateException is thrown. -+ * -+ * Returns 'this' to allow for chaining. -+ * -+ * @param name Unique registry name -+ * @return This instance -+ */ -+ public final Block setRegistryName(String name) -+ { -+ if (getRegistryName() != null) -+ throw new IllegalStateException("Attempted to set registry name on block with exisiting registry name! New: " + name + " Old: " + getRegistryName()); -+ int index = name.lastIndexOf(':'); -+ String oldPrefix = index == -1 ? "" : name.substring(0, index); -+ name = index == -1 ? name : name.substring(index + 1); -+ net.minecraftforge.fml.common.ModContainer mc = net.minecraftforge.fml.common.Loader.instance().activeModContainer(); -+ String prefix = mc == null ? "minecraft" : mc.getModId(); -+ if (!oldPrefix.equals(prefix) && oldPrefix.length() > 0) -+ { -+ net.minecraftforge.fml.common.FMLLog.bigWarning("Dangerous alternative prefix %s for name %s, invalid registry invocation/invalid name?", oldPrefix, name); -+ prefix = oldPrefix; -+ } -+ this.registryName = new ResourceLocation(prefix, name); -+ return this; -+ } -+ public final Block setRegistryName(ResourceLocation name){ return setRegistryName(name.toString()); } -+ public final Block setRegistryName(String modID, String name){ return setRegistryName(modID + ":" + name); } -+ -+ /** -+ * A unique identifier for this block, if this block is registered in the game registry it will return that name. -+ * Otherwise it will return the name set in setRegistryName(). -+ * If neither are valid null is returned. -+ * -+ * @return Unique identifier or null. -+ */ -+ public final String getRegistryName() -+ { -+ if (delegate.getResourceName() != null) return delegate.getResourceName().toString(); -+ return registryName != null ? registryName.toString() : null; -+ } + /* ========================================= FORGE END ======================================*/ + public static void func_149671_p() diff --git a/patches/minecraft/net/minecraft/item/Item.java.patch b/patches/minecraft/net/minecraft/item/Item.java.patch index d0a84e64b9..f8b1ff9140 100644 --- a/patches/minecraft/net/minecraft/item/Item.java.patch +++ b/patches/minecraft/net/minecraft/item/Item.java.patch @@ -1,8 +1,11 @@ --- ../src-base/minecraft/net/minecraft/item/Item.java +++ ../src-work/minecraft/net/minecraft/item/Item.java -@@ -58,8 +58,8 @@ +@@ -56,10 +56,10 @@ + import net.minecraftforge.fml.relauncher.Side; + import net.minecraftforge.fml.relauncher.SideOnly; - public class Item +-public class Item ++public class Item extends net.minecraftforge.fml.common.registry.IForgeRegistryEntry.Impl { - public static final RegistryNamespaced field_150901_e = new RegistryNamespaced(); - private static final Map field_179220_a = Maps.newHashMap(); @@ -11,17 +14,7 @@ private static final IItemPropertyGetter field_185046_b = new IItemPropertyGetter() { @SideOnly(Side.CLIENT) -@@ -104,6 +104,9 @@ - private Item field_77700_c; - private String field_77774_bZ; - -+ public final net.minecraftforge.fml.common.registry.RegistryDelegate delegate = -+ ((net.minecraftforge.fml.common.registry.FMLControlledNamespacedRegistry)field_150901_e).getDelegate(this, Item.class); -+ - public static int func_150891_b(Item p_150891_0_) - { - return p_150891_0_ == null ? 0 : field_150901_e.func_148757_b(p_150891_0_); -@@ -192,6 +195,7 @@ +@@ -192,6 +192,7 @@ return p_77654_1_; } @@ -29,7 +22,7 @@ public int func_77639_j() { return this.field_77777_bU; -@@ -312,6 +316,7 @@ +@@ -312,6 +313,7 @@ return this.field_77700_c; } @@ -37,7 +30,7 @@ public boolean func_77634_r() { return this.field_77700_c != null; -@@ -367,7 +372,7 @@ +@@ -367,7 +369,7 @@ public boolean func_77616_k(ItemStack p_77616_1_) { @@ -46,7 +39,7 @@ } protected RayTraceResult func_77621_a(World p_77621_1_, EntityPlayer p_77621_2_, boolean p_77621_3_) -@@ -385,6 +390,10 @@ +@@ -385,6 +387,10 @@ float f6 = f3 * f4; float f7 = f2 * f4; double d3 = 5.0D; @@ -57,7 +50,7 @@ Vec3d vec3d1 = vec3d.func_72441_c((double)f6 * d3, (double)f5 * d3, (double)f7 * d3); return p_77621_1_.func_147447_a(vec3d, vec3d1, p_77621_3_, !p_77621_3_, false); } -@@ -422,11 +431,606 @@ +@@ -422,11 +428,557 @@ return false; } @@ -575,55 +568,6 @@ + return !ItemStack.func_77989_b(oldStack, newStack); + } + -+ private ResourceLocation registryName = null; -+ /** -+ * Sets a unique name for this Item. This should be used for uniquely identify the instance of the Item. -+ * This is the valid replacement for the atrocious 'getUnlocalizedName().substring(6)' stuff that everyone does. -+ * Unlocalized names have NOTHING to do with unique identifiers. As demonstrated by vanilla blocks and items. -+ * -+ * The supplied name will be prefixed with the currently active mod's modId. -+ * If the supplied name already has a prefix that is different, it will be used and a warning will be logged. -+ * -+ * If a name already exists, or this Item is already registered in a registry, then an IllegalStateException is thrown. -+ * -+ * Returns 'this' to allow for chaining. -+ * -+ * @param name Unique registry name -+ * @return This instance -+ */ -+ public final Item setRegistryName(String name) -+ { -+ if (getRegistryName() != null) -+ throw new IllegalStateException("Attempted to set registry name on block with exisiting registry name! New: " + name + " Old: " + getRegistryName()); -+ int index = name.lastIndexOf(':'); -+ String oldPrefix = index == -1 ? "" : name.substring(0, index); -+ name = index == -1 ? name : name.substring(index + 1); -+ net.minecraftforge.fml.common.ModContainer mc = net.minecraftforge.fml.common.Loader.instance().activeModContainer(); -+ String prefix = mc == null ? "minecraft" : mc.getModId(); -+ if (!oldPrefix.equals(prefix) && oldPrefix.length() > 0) -+ { -+ net.minecraftforge.fml.common.FMLLog.bigWarning("Dangerous alternative prefix %s for name %s, invalid registry invocation/invalid name?", oldPrefix, name); -+ prefix = oldPrefix; -+ } -+ this.registryName = new ResourceLocation(prefix, name); -+ return this; -+ } -+ public final Item setRegistryName(ResourceLocation name){ return setRegistryName(name.toString()); } -+ public final Item setRegistryName(String modID, String name){ return setRegistryName(modID + ":" + name); } -+ -+ /** -+ * A unique identifier for this block, if this block is registered in the game registry it will return that name. -+ * Otherwise it will return the name set in setRegistryName(). -+ * If neither are valid null is returned. -+ * -+ * @return Unique identifier or null. -+ */ -+ public final String getRegistryName() -+ { -+ if (delegate.getResourceName() != null) return delegate.getResourceName().toString(); -+ return registryName != null ? registryName.toString() : null; -+ } -+ + /** + * Called from ItemStack.setItem, will hold extra data for the life of this ItemStack. + * Can be retrieved from stack.getCapabilities() @@ -664,7 +608,7 @@ public static void func_150900_l() { func_179214_a(Blocks.field_150348_b, (new ItemMultiTexture(Blocks.field_150348_b, Blocks.field_150348_b, new Function() -@@ -935,6 +1539,10 @@ +@@ -935,6 +1487,10 @@ private final float field_78011_i; private final int field_78008_j; @@ -675,7 +619,7 @@ private ToolMaterial(int p_i1874_3_, int p_i1874_4_, float p_i1874_5_, float p_i1874_6_, int p_i1874_7_) { this.field_78001_f = p_i1874_3_; -@@ -969,9 +1577,36 @@ +@@ -969,9 +1525,36 @@ return this.field_78008_j; } diff --git a/patches/minecraft/net/minecraft/potion/Potion.java.patch b/patches/minecraft/net/minecraft/potion/Potion.java.patch index d8ed60afcf..9385f031ca 100644 --- a/patches/minecraft/net/minecraft/potion/Potion.java.patch +++ b/patches/minecraft/net/minecraft/potion/Potion.java.patch @@ -1,8 +1,11 @@ --- ../src-base/minecraft/net/minecraft/potion/Potion.java +++ ../src-work/minecraft/net/minecraft/potion/Potion.java -@@ -23,7 +23,7 @@ +@@ -21,9 +21,9 @@ + import net.minecraftforge.fml.relauncher.Side; + import net.minecraftforge.fml.relauncher.SideOnly; - public class Potion +-public class Potion ++public class Potion extends net.minecraftforge.fml.common.registry.IForgeRegistryEntry.Impl { - public static final RegistryNamespaced field_188414_b = new RegistryNamespaced(); + public static final RegistryNamespaced field_188414_b = net.minecraftforge.fml.common.registry.GameData.getPotionRegistry(); diff --git a/patches/minecraft/net/minecraft/world/biome/BiomeGenBase.java.patch b/patches/minecraft/net/minecraft/world/biome/BiomeGenBase.java.patch index 62e355ccee..ead3c3131c 100644 --- a/patches/minecraft/net/minecraft/world/biome/BiomeGenBase.java.patch +++ b/patches/minecraft/net/minecraft/world/biome/BiomeGenBase.java.patch @@ -1,5 +1,14 @@ --- ../src-base/minecraft/net/minecraft/world/biome/BiomeGenBase.java +++ ../src-work/minecraft/net/minecraft/world/biome/BiomeGenBase.java +@@ -51,7 +51,7 @@ + import org.apache.logging.log4j.LogManager; + import org.apache.logging.log4j.Logger; + +-public abstract class BiomeGenBase ++public abstract class BiomeGenBase extends net.minecraftforge.fml.common.registry.IForgeRegistryEntry.Impl + { + private static final Logger field_150586_aC = LogManager.getLogger(); + protected static final IBlockState field_185365_a = Blocks.field_150348_b.func_176223_P(); @@ -70,7 +70,7 @@ protected static final WorldGenTrees field_76757_N = new WorldGenTrees(false); protected static final WorldGenBigTree field_76758_O = new WorldGenBigTree(false); diff --git a/src/main/java/net/minecraftforge/client/model/ModelLoader.java b/src/main/java/net/minecraftforge/client/model/ModelLoader.java index 4014840672..a37285e32c 100644 --- a/src/main/java/net/minecraftforge/client/model/ModelLoader.java +++ b/src/main/java/net/minecraftforge/client/model/ModelLoader.java @@ -192,7 +192,7 @@ public final class ModelLoader extends ModelBakery { public int compare(Block b1, Block b2) { - return b1.getRegistryName().compareTo(b2.getRegistryName()); + return b1.getRegistryName().toString().compareTo(b2.getRegistryName().toString()); } }); ProgressBar blockBar = ProgressManager.push("ModelLoader: blocks", blocks.size()); @@ -201,7 +201,7 @@ public final class ModelLoader extends ModelBakery for(Block block : blocks) { - blockBar.step(block.getRegistryName()); + blockBar.step(block.getRegistryName().toString()); for(ResourceLocation location : mapper.getBlockstateLocations(block)) { loadBlock(mapper, block, location); @@ -276,14 +276,14 @@ public final class ModelLoader extends ModelBakery { public int compare(Item i1, Item i2) { - return i1.getRegistryName().compareTo(i2.getRegistryName()); + return i1.getRegistryName().toString().compareTo(i2.getRegistryName().toString()); } }); ProgressBar itemBar = ProgressManager.push("ModelLoader: items", items.size()); for(Item item : items) { - itemBar.step(item.getRegistryName()); + itemBar.step(item.getRegistryName().toString()); for(String s : getVariantNames(item)) { ResourceLocation file = getItemLocation(s); @@ -901,7 +901,7 @@ public final class ModelLoader extends ModelBakery for(String s : getVariantNames(item)) { ModelResourceLocation memory = getInventoryVariant(s); - reverseItemMap.put(memory, item.getRegistryName()); + reverseItemMap.put(memory, item.getRegistryName().toString()); } } } diff --git a/src/main/java/net/minecraftforge/common/util/BlockSnapshot.java b/src/main/java/net/minecraftforge/common/util/BlockSnapshot.java index f2642aa4b6..a6b404b9d6 100644 --- a/src/main/java/net/minecraftforge/common/util/BlockSnapshot.java +++ b/src/main/java/net/minecraftforge/common/util/BlockSnapshot.java @@ -183,7 +183,7 @@ public class BlockSnapshot implements Serializable if (DEBUG) { - System.out.printf("Restored BlockSnapshot with data [World: %s ][Location: %d,%d,%d ][Meta: %d ][Block: %s ][TileEntity: %s ][force: %s ][applyPhysics: %s]", world.getWorldInfo().getWorldName(), pos.getX(), pos.getY(), pos.getZ(), replaced.getBlock().getMetaFromState(replaced), replaced.getBlock().delegate.name(), te, force, applyPhysics); + System.out.printf("Restored BlockSnapshot with data [World: %s ][Location: %d,%d,%d ][Meta: %d ][Block: %s ][TileEntity: %s ][force: %s ][applyPhysics: %s]", world.getWorldInfo().getWorldName(), pos.getX(), pos.getY(), pos.getZ(), replaced.getBlock().getMetaFromState(replaced), replaced.getBlock().delegate.name().toString(), te, force, applyPhysics); } return true; } @@ -218,7 +218,7 @@ public class BlockSnapshot implements Serializable if (DEBUG) { - System.out.printf("Restored BlockSnapshot with data [World: %s ][Location: %d,%d,%d ][Meta: %d ][Block: %s ][TileEntity: %s ][force: %s ][applyPhysics: %s]", world.getWorldInfo().getWorldName(), pos.getX(), pos.getY(), pos.getZ(), replaced.getBlock().getMetaFromState(replaced), replaced.getBlock().delegate.name(), te, force, applyPhysics); + System.out.printf("Restored BlockSnapshot with data [World: %s ][Location: %d,%d,%d ][Meta: %d ][Block: %s ][TileEntity: %s ][force: %s ][applyPhysics: %s]", world.getWorldInfo().getWorldName(), pos.getX(), pos.getY(), pos.getZ(), replaced.getBlock().getMetaFromState(replaced), replaced.getBlock().delegate.name().toString(), te, force, applyPhysics); } return true; } @@ -295,4 +295,4 @@ public class BlockSnapshot implements Serializable hash = 73 * hash + (this.blockIdentifier != null ? this.blockIdentifier.hashCode() : 0); return hash; } -} \ No newline at end of file +} diff --git a/src/main/java/net/minecraftforge/fluids/FluidRegistry.java b/src/main/java/net/minecraftforge/fluids/FluidRegistry.java index ba32b29e34..ef41354bb2 100644 --- a/src/main/java/net/minecraftforge/fluids/FluidRegistry.java +++ b/src/main/java/net/minecraftforge/fluids/FluidRegistry.java @@ -423,13 +423,7 @@ public abstract class FluidRegistry } @Override - public String name() - { - return name; - } - - @Override - public ResourceLocation getResourceName() { + public ResourceLocation name() { return new ResourceLocation(name); } diff --git a/src/main/java/net/minecraftforge/fml/common/registry/FMLControlledNamespacedRegistry.java b/src/main/java/net/minecraftforge/fml/common/registry/FMLControlledNamespacedRegistry.java index 21b942a659..5a383dd193 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/FMLControlledNamespacedRegistry.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/FMLControlledNamespacedRegistry.java @@ -8,6 +8,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import com.google.common.base.Joiner; @@ -15,6 +16,7 @@ import com.google.common.base.Throwables; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; import com.google.common.collect.Iterators; +import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import net.minecraft.util.IntIdentityHashBiMap; @@ -28,7 +30,7 @@ import net.minecraftforge.fml.common.FMLLog; import net.minecraftforge.fml.common.functions.GenericIterableFactory; import net.minecraftforge.fml.common.registry.RegistryDelegate.Delegate; -public class FMLControlledNamespacedRegistry extends RegistryNamespacedDefaultedByKey +public class FMLControlledNamespacedRegistry extends RegistryNamespacedDefaultedByKey implements IForgeRegistry { public static final boolean DEBUG = Boolean.parseBoolean(System.getProperty("fml.debugRegistryEntries", "false")); private final Class superType; @@ -673,7 +675,7 @@ public class FMLControlledNamespacedRegistry extends RegistryNamespacedDefaul } @SuppressWarnings("unchecked") - public FMLControlledNamespacedRegistry asType(Class type) + public FMLControlledNamespacedRegistry asType(Class type) { return (FMLControlledNamespacedRegistry)this; } @@ -766,11 +768,102 @@ public class FMLControlledNamespacedRegistry extends RegistryNamespacedDefaul @Override public ResourceLocation getNameForObject(I p_177774_1_) { - ResourceLocation rl = super.getNameForObject(p_177774_1_); + ResourceLocation rl = super.getNameForObjectBypass(p_177774_1_); if (rl == null) { rl = activeSubstitutions.inverse().get(p_177774_1_); } return rl; } + + // IForgeRegistry: Modders should only interfaces with these methods + @Override + public void register(I value) + { + ResourceLocation key = value.getRegistryName(); + if (key == null) + { + FMLLog.severe("Attempted to register a entry with a null name: %s", value); + throw new NullPointerException(String.format("Attempted to register a entry with a null name: %s", value)); + } + add(-1, key, value); + } + + @Override + public boolean contains(ResourceLocation key) + { + return containsKey(key); + } + + @Override + public boolean contains(I value) + { + return get(value) != null; + } + + @Override + public I get(ResourceLocation key) + { + return getObject(key); + } + + @Override + public ResourceLocation get(I value) + { + return getNameForObject(value); + } + + @Override + public List getValues() + { + return Lists.newArrayList(this.iterator()); + } + + @Override + public Set> getEntries() + { + return Sets.newHashSet(new Iterator>() + { + Iterator itr = FMLControlledNamespacedRegistry.this.iterator(); + + @Override + public boolean hasNext() + { + return itr.hasNext(); + } + + @Override + public Entry next() + { + final I value = itr.next(); + final ResourceLocation key = FMLControlledNamespacedRegistry.this.get(value); + return new Entry() + { + @Override + public ResourceLocation getKey() + { + return key; + } + + @Override + public I getValue() + { + return value; + } + + @Override + public I setValue(I value) + { + throw new UnsupportedOperationException("Setting the value in an itterator is not allowed"); + } + }; + } + + @Override + public void remove() + { + throw new UnsupportedOperationException("This is a READ ONLY view of this registry."); + } + }); + } } diff --git a/src/main/java/net/minecraftforge/fml/common/registry/GameData.java b/src/main/java/net/minecraftforge/fml/common/registry/GameData.java index 54715ed988..eb36b1bbb3 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/GameData.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/GameData.java @@ -221,7 +221,7 @@ public class GameData } } - public RegistryDelegate makeDelegate(T obj, Class rootClass) + public > RegistryDelegate makeDelegate(T obj, Class rootClass) { return PersistentRegistryManager.makeDelegate(obj, rootClass); } @@ -273,4 +273,4 @@ public class GameData // no op for the minute? } } -} \ No newline at end of file +} diff --git a/src/main/java/net/minecraftforge/fml/common/registry/GameRegistry.java b/src/main/java/net/minecraftforge/fml/common/registry/GameRegistry.java index dd9d7feea3..54e699af72 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/GameRegistry.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/GameRegistry.java @@ -135,7 +135,7 @@ public class GameRegistry */ public static void registerItem(Item item) { - registerItem(item, item.getRegistryName()); + registerItem(item, item.getRegistryName().toString()); } /** @@ -153,20 +153,6 @@ public class GameRegistry GameData.getMain().registerItem(item, name); } - /** - * Register the specified Item with a mod specific name : overrides the standard type based name - * - * @param item The item to register - * @param name The mod-unique name to register it as - null will remove a custom name - * @param modId deprecated, unused - */ - @Deprecated // See version without modID remove in 1.9 - public static Item registerItem(Item item, String name, String modId) - { - registerItem(item, name); - return item; - } - /** * Add a forced persistent substitution alias for the block or item to another block or item. This will have * the effect of using the substituted block or item instead of the original, where ever it is @@ -190,7 +176,7 @@ public class GameRegistry */ public static Block registerBlock(Block block) { - return registerBlock(block, block.getRegistryName()); + return registerBlock(block, block.getRegistryName().toString()); } /** diff --git a/src/main/java/net/minecraftforge/fml/common/registry/IForgeRegistry.java b/src/main/java/net/minecraftforge/fml/common/registry/IForgeRegistry.java new file mode 100644 index 0000000000..80279df414 --- /dev/null +++ b/src/main/java/net/minecraftforge/fml/common/registry/IForgeRegistry.java @@ -0,0 +1,22 @@ +package net.minecraftforge.fml.common.registry; + +import java.util.List; +import java.util.Map.Entry; +import java.util.Set; + +import net.minecraft.util.ResourceLocation; + +public interface IForgeRegistry extends Iterable +{ + void register(V value); + + boolean contains(ResourceLocation key); + boolean contains(V value); + + V get(ResourceLocation key); + ResourceLocation get(V value); + + Set getKeys(); + List getValues(); + Set> getEntries(); +} diff --git a/src/main/java/net/minecraftforge/fml/common/registry/IForgeRegistryEntry.java b/src/main/java/net/minecraftforge/fml/common/registry/IForgeRegistryEntry.java new file mode 100644 index 0000000000..9fc19e75d9 --- /dev/null +++ b/src/main/java/net/minecraftforge/fml/common/registry/IForgeRegistryEntry.java @@ -0,0 +1,76 @@ +package net.minecraftforge.fml.common.registry; + +import com.google.common.reflect.TypeToken; + +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.fml.common.FMLLog; +import net.minecraftforge.fml.common.Loader; +import net.minecraftforge.fml.common.ModContainer; + +public interface IForgeRegistryEntry +{ + /** + * Sets a unique name for this Item. This should be used for uniquely identify the instance of the Item. + * This is the valid replacement for the atrocious 'getUnlocalizedName().substring(6)' stuff that everyone does. + * Unlocalized names have NOTHING to do with unique identifiers. As demonstrated by vanilla blocks and items. + * + * The supplied name will be prefixed with the currently active mod's modId. + * If the supplied name already has a prefix that is different, it will be used and a warning will be logged. + * + * If a name already exists, or this Item is already registered in a registry, then an IllegalStateException is thrown. + * + * Returns 'this' to allow for chaining. + * + * @param name Unique registry name + * @return This instance + */ + V setRegistryName(ResourceLocation name); + + /** + * A unique identifier for this entry, if this entry is registered already it will return it's official registry name. + * Otherwise it will return the name set in setRegistryName(). + * If neither are valid null is returned. + * + * @return Unique identifier or null. + */ + ResourceLocation getRegistryName(); + + // Default implementation, modders who make extra items SHOULD extend this instead of Object. + // We have to do this until we get default implementations in Java 8. + @SuppressWarnings({ "serial", "unchecked" }) + public static class Impl> implements IForgeRegistryEntry + { + private TypeToken token = new TypeToken(getClass()){}; + public final RegistryDelegate delegate = PersistentRegistryManager.makeDelegate((T)this, (Class)token.getRawType()); + private ResourceLocation registryName = null; + + public final T setRegistryName(String name) + { + if (getRegistryName() != null) + throw new IllegalStateException("Attempted to set registry name with exisiting registry name! New: " + name + " Old: " + getRegistryName()); + + int index = name.lastIndexOf(':'); + String oldPrefix = index == -1 ? "" : name.substring(0, index); + name = index == -1 ? name : name.substring(index + 1); + ModContainer mc = Loader.instance().activeModContainer(); + String prefix = mc == null ? "minecraft" : mc.getModId(); + if (!oldPrefix.equals(prefix) && oldPrefix.length() > 0) + { + FMLLog.bigWarning("Dangerous alternative prefix %s for name %s, invalid registry invocation/invalid name?", oldPrefix, name); + prefix = oldPrefix; + } + this.registryName = new ResourceLocation(prefix, name); + return (T)this; + } + + //Helper functions + public final T setRegistryName(ResourceLocation name){ return setRegistryName(name.toString()); } + public final T setRegistryName(String modID, String name){ return setRegistryName(modID + ":" + name); } + + public final ResourceLocation getRegistryName() + { + if (delegate.name() != null) return delegate.name(); + return registryName != null ? registryName : null; + } + } +} diff --git a/src/main/java/net/minecraftforge/fml/common/registry/PersistentRegistryManager.java b/src/main/java/net/minecraftforge/fml/common/registry/PersistentRegistryManager.java index c87476d030..849bf9a3f4 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/PersistentRegistryManager.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/PersistentRegistryManager.java @@ -48,30 +48,25 @@ public class PersistentRegistryManager ACTIVE, FROZEN, STAGING; private final BiMap> registries = HashBiMap.create(); - private final BiMap, ResourceLocation> registrySuperTypes = HashBiMap.create(); + private final BiMap, ResourceLocation> registrySuperTypes = HashBiMap.create(); @SuppressWarnings("unchecked") - FMLControlledNamespacedRegistry getRegistry(ResourceLocation key, Class regType) + > FMLControlledNamespacedRegistry getRegistry(ResourceLocation key, Class regType) { return (FMLControlledNamespacedRegistry)registries.get(key); } - FMLControlledNamespacedRegistry getOrShallowCopyRegistry(ResourceLocation key, Class regType, FMLControlledNamespacedRegistry other) + > FMLControlledNamespacedRegistry getOrShallowCopyRegistry(ResourceLocation key, Class regType, FMLControlledNamespacedRegistry other) { if (!registries.containsKey(key)) { registries.put(key, other.makeShallowCopy()); - registrySuperTypes.put(regType, key); + //TODO: Figure out this generics... registrySuperTypes.put(regType, key); } return getRegistry(key, regType); } - private FMLControlledNamespacedRegistry createRegistry(ResourceLocation registryName, Class registryType, ResourceLocation optionalDefaultKey, int minId, int maxId, boolean hasDelegates) - { - return this.createRegistry(registryName, registryType, optionalDefaultKey, minId, maxId, hasDelegates, null); - } - - private FMLControlledNamespacedRegistry createRegistry(ResourceLocation registryName, Class type, ResourceLocation defaultObjectKey, int minId, int maxId, boolean isDelegated, FMLControlledNamespacedRegistry.AddCallback addCallback) + private > FMLControlledNamespacedRegistry createRegistry(ResourceLocation registryName, Class type, ResourceLocation defaultObjectKey, int minId, int maxId, boolean isDelegated, FMLControlledNamespacedRegistry.AddCallback addCallback) { Set> parents = Sets.newHashSet(); findSuperTypes(type, parents); @@ -84,7 +79,7 @@ public class PersistentRegistryManager } FMLControlledNamespacedRegistry fmlControlledNamespacedRegistry = new FMLControlledNamespacedRegistry(defaultObjectKey, maxId, minId, type, isDelegated, addCallback); registries.put(registryName, fmlControlledNamespacedRegistry); - registrySuperTypes.put(type, registryName); + //TODO: Figure out this generics... registrySuperTypes.put(type, registryName); return getRegistry(registryName, type); } @@ -119,7 +114,7 @@ public class PersistentRegistryManager return registries.containsValue(registry); } - public FMLControlledNamespacedRegistry getRegistry(Class rootClass) + public > FMLControlledNamespacedRegistry getRegistry(Class rootClass) { ResourceLocation rl = registrySuperTypes.get(rootClass); return getRegistry(rl, rootClass); @@ -131,14 +126,14 @@ public class PersistentRegistryManager public static final ResourceLocation POTIONS = new ResourceLocation("minecraft:potions"); public static final ResourceLocation BIOMES = new ResourceLocation("minecraft:biomes"); - public static FMLControlledNamespacedRegistry createRegistry(ResourceLocation registryName, Class registryType, ResourceLocation optionalDefaultKey, int maxId, int minId, boolean hasDelegates, FMLControlledNamespacedRegistry.AddCallback addCallback) + public static > FMLControlledNamespacedRegistry createRegistry(ResourceLocation registryName, Class registryType, ResourceLocation optionalDefaultKey, int maxId, int minId, boolean hasDelegates, FMLControlledNamespacedRegistry.AddCallback addCallback) { return PersistentRegistry.ACTIVE.createRegistry(registryName, registryType, optionalDefaultKey, minId, maxId, hasDelegates, addCallback); } - public static FMLControlledNamespacedRegistry createRegistry(ResourceLocation registryName, Class registryType, ResourceLocation optionalDefaultKey, int maxId, int minId, boolean hasDelegates) + public static > FMLControlledNamespacedRegistry createRegistry(ResourceLocation registryName, Class registryType, ResourceLocation optionalDefaultKey, int maxId, int minId, boolean hasDelegates) { - return PersistentRegistry.ACTIVE.createRegistry(registryName, registryType, optionalDefaultKey, minId, maxId, hasDelegates); + return PersistentRegistry.ACTIVE.createRegistry(registryName, registryType, optionalDefaultKey, minId, maxId, hasDelegates, null); } public static List injectSnapshot(GameDataSnapshot snapshot, boolean injectFrozenData, boolean isLocalWorld) @@ -256,7 +251,7 @@ public class PersistentRegistryManager } } - private static void loadRegistry(final ResourceLocation registryName, final PersistentRegistry from, final PersistentRegistry to, Class regType) + private static > void loadRegistry(final ResourceLocation registryName, final PersistentRegistry from, final PersistentRegistry to, Class regType) { FMLControlledNamespacedRegistry fromRegistry = from.getRegistry(registryName, regType); if (fromRegistry == null) @@ -292,14 +287,14 @@ public class PersistentRegistryManager } } - private static void loadFrozenDataToStagingRegistry(Map> remaps, ResourceLocation registryName, Class regType) + private static > void loadFrozenDataToStagingRegistry(Map> remaps, ResourceLocation registryName, Class regType) { FMLControlledNamespacedRegistry frozenRegistry = PersistentRegistry.FROZEN.getRegistry(registryName, regType); FMLControlledNamespacedRegistry newRegistry = PersistentRegistry.STAGING.getOrShallowCopyRegistry(registryName, regType, frozenRegistry); newRegistry.loadIds(frozenRegistry.getEntriesNotIn(newRegistry), Maps.newLinkedHashMap(), remaps.get(registryName), frozenRegistry, registryName); } - private static void loadPersistentDataToStagingRegistry(boolean injectFrozenData, Map> remaps, LinkedHashMap> missing, Map.Entry snapEntry, Class regType) + private static > void loadPersistentDataToStagingRegistry(boolean injectFrozenData, Map> remaps, LinkedHashMap> missing, Map.Entry snapEntry, Class regType) { ResourceLocation registryName = snapEntry.getKey(); @@ -576,7 +571,7 @@ public class PersistentRegistryManager public final Map entries = Maps.newHashMap(); } - public static RegistryDelegate makeDelegate(T obj, Class rootClass) + public static > RegistryDelegate makeDelegate(T obj, Class rootClass) { return PersistentRegistry.ACTIVE.getRegistry(rootClass).getDelegate(obj, rootClass); } diff --git a/src/main/java/net/minecraftforge/fml/common/registry/RegistryDelegate.java b/src/main/java/net/minecraftforge/fml/common/registry/RegistryDelegate.java index 2e255f25e5..900f150f43 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/RegistryDelegate.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/RegistryDelegate.java @@ -23,23 +23,12 @@ public interface RegistryDelegate { */ T get(); - /** - * Get the name of this delegate. This is completely static after registration has completed and will never change. - * - * Deprecated in favour of the resource location. - * - * @see #getResourceName() - * @return The name - */ - @Deprecated - String name(); - /** * Get the unique resource location for this delegate. Completely static after registration has completed, and * will never change. * @return The name */ - ResourceLocation getResourceName(); + ResourceLocation name(); /** * Get the delegate type. It will be dependent on the registry this delegate is sourced from. @@ -67,12 +56,7 @@ public interface RegistryDelegate { } @Override - public String name() { - return name.toString(); - } - - @Override - public ResourceLocation getResourceName() { return name; } + public ResourceLocation name() { return name; } @Override public Class type() diff --git a/src/main/java/net/minecraftforge/fml/common/registry/VillagerRegistry.java b/src/main/java/net/minecraftforge/fml/common/registry/VillagerRegistry.java index 577edc2652..37abb566e3 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/VillagerRegistry.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/VillagerRegistry.java @@ -250,7 +250,7 @@ public class VillagerRegistry } } - public static class VillagerProfession + public static class VillagerProfession extends IForgeRegistryEntry.Impl { private ResourceLocation name; private ResourceLocation texture; diff --git a/src/main/java/net/minecraftforge/oredict/OreDictionary.java b/src/main/java/net/minecraftforge/oredict/OreDictionary.java index 05c1472c1c..99ac177e3b 100644 --- a/src/main/java/net/minecraftforge/oredict/OreDictionary.java +++ b/src/main/java/net/minecraftforge/oredict/OreDictionary.java @@ -326,7 +326,7 @@ public class OreDictionary // HACK: use the registry name's ID. It is unique and it knows about substitutions. Fallback to a -1 value (what Item.getIDForItem would have returned) in the case where the registry is not aware of the item yet // IT should be noted that -1 will fail the gate further down, if an entry already exists with value -1 for this name. This is what is broken and being warned about. // APPARENTLY it's quite common to do this. OreDictionary should be considered alongside Recipes - you can't make them properly until you've registered with the game. - ResourceLocation registryName = stack.getItem().delegate.getResourceName(); + ResourceLocation registryName = stack.getItem().delegate.name(); int id; if (registryName == null) { @@ -488,7 +488,7 @@ public class OreDictionary // HACK: use the registry name's ID. It is unique and it knows about substitutions. Fallback to a -1 value (what Item.getIDForItem would have returned) in the case where the registry is not aware of the item yet // IT should be noted that -1 will fail the gate further down, if an entry already exists with value -1 for this name. This is what is broken and being warned about. // APPARENTLY it's quite common to do this. OreDictionary should be considered alongside Recipes - you can't make them properly until you've registered with the game. - ResourceLocation registryName = ore.getItem().delegate.getResourceName(); + ResourceLocation registryName = ore.getItem().delegate.name(); int hash; if (registryName == null) { @@ -545,7 +545,7 @@ public class OreDictionary for (ItemStack ore : ores) { // HACK: use the registry name's ID. It is unique and it knows about substitutions - ResourceLocation name = ore.getItem().delegate.getResourceName(); + ResourceLocation name = ore.getItem().delegate.name(); int hash; if (name == null) {