From 07466ab036ca29d9d7dfd13551b4cafa9ac9a51b Mon Sep 17 00:00:00 2001 From: Christian Date: Fri, 1 Aug 2014 21:06:18 -0230 Subject: [PATCH] First draft of add alias. It is probably not properly persistent atm. --- .../cpw/mods/fml/common/FMLContainer.java | 40 +++++ .../registry/ExistingAliasException.java | 9 + .../FMLControlledNamespacedRegistry.java | 19 ++ .../mods/fml/common/registry/GameData.java | 4 + .../fml/common/registry/GameRegistry.java | 169 ++++++++++-------- 5 files changed, 170 insertions(+), 71 deletions(-) create mode 100644 fml/src/main/java/cpw/mods/fml/common/registry/ExistingAliasException.java diff --git a/fml/src/main/java/cpw/mods/fml/common/FMLContainer.java b/fml/src/main/java/cpw/mods/fml/common/FMLContainer.java index fd20bb7313..03f8e588ff 100644 --- a/fml/src/main/java/cpw/mods/fml/common/FMLContainer.java +++ b/fml/src/main/java/cpw/mods/fml/common/FMLContainer.java @@ -30,6 +30,8 @@ import net.minecraft.world.storage.WorldInfo; import org.apache.logging.log4j.Level; +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; import com.google.common.collect.Maps; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; @@ -124,6 +126,15 @@ public class FMLContainer extends DummyModContainer implements WorldAccessContai blockAliasList.appendTag(tag); } fmlData.setTag("BlockAliases", blockAliasList); + NBTTagList blockPersistentAliasList = new NBTTagList(); + for (Entry entry : GameData.getBlockRegistry().getPersistentAliases().entrySet()) + { + NBTTagCompound tag = new NBTTagCompound(); + tag.setString("K", entry.getKey()); + tag.setString("V", entry.getValue()); + blockPersistentAliasList.appendTag(tag); + } + fmlData.setTag("PersistentBlockAliases", blockPersistentAliasList); // item aliases NBTTagList itemAliasList = new NBTTagList(); for (Entry entry : GameData.getItemRegistry().getAliases().entrySet()) @@ -135,6 +146,15 @@ public class FMLContainer extends DummyModContainer implements WorldAccessContai } fmlData.setTag("ItemAliases", itemAliasList); + NBTTagList itemPersistentAliasList = new NBTTagList(); + for (Entry entry : GameData.getItemRegistry().getPersistentAliases().entrySet()) + { + NBTTagCompound tag = new NBTTagCompound(); + tag.setString("K", entry.getKey()); + tag.setString("V", entry.getValue()); + itemPersistentAliasList.appendTag(tag); + } + fmlData.setTag("ItemPersistentAliases", itemPersistentAliasList); return fmlData; } @@ -225,6 +245,16 @@ public class FMLContainer extends DummyModContainer implements WorldAccessContai NBTTagCompound dataTag = list.getCompoundTagAt(i); blockAliases.put(dataTag.getString("K"), dataTag.getString("V")); } + BiMap blockPersistentAliases = HashBiMap.create(); + if (tag.hasKey("BlockPersistentAliases", 10)) + { + list = tag.getTagList("BlockPersistentAliases", 10); + for (int i = 0; i < list.tagCount(); i++) + { + NBTTagCompound dataTag = list.getCompoundTagAt(i); + blockPersistentAliases.put(dataTag.getString("K"), dataTag.getString("V")); + } + } // item aliases Map itemAliases = new HashMap(); list = tag.getTagList("ItemAliases", 10); @@ -234,6 +264,16 @@ public class FMLContainer extends DummyModContainer implements WorldAccessContai itemAliases.put(dataTag.getString("K"), dataTag.getString("V")); } + BiMap itemPersistentAliases = HashBiMap.create(); + if (tag.hasKey("ItemPersistentAliases", 10)) + { + list = tag.getTagList("ItemPersistentAliases", 10); + for (int i = 0; i < list.tagCount(); i++) + { + NBTTagCompound dataTag = list.getCompoundTagAt(i); + itemPersistentAliases.put(dataTag.getString("K"), dataTag.getString("V")); + } + } failedElements = GameData.injectWorldIDMap(dataList, blockedIds, blockAliases, itemAliases, true, true); } diff --git a/fml/src/main/java/cpw/mods/fml/common/registry/ExistingAliasException.java b/fml/src/main/java/cpw/mods/fml/common/registry/ExistingAliasException.java new file mode 100644 index 0000000000..aedd66bcec --- /dev/null +++ b/fml/src/main/java/cpw/mods/fml/common/registry/ExistingAliasException.java @@ -0,0 +1,9 @@ +package cpw.mods.fml.common.registry; + +public class ExistingAliasException extends Exception { + public ExistingAliasException(String fromName, String toName) { + } + + private static final long serialVersionUID = 1L; + +} diff --git a/fml/src/main/java/cpw/mods/fml/common/registry/FMLControlledNamespacedRegistry.java b/fml/src/main/java/cpw/mods/fml/common/registry/FMLControlledNamespacedRegistry.java index 78adb35f9c..8503c51321 100644 --- a/fml/src/main/java/cpw/mods/fml/common/registry/FMLControlledNamespacedRegistry.java +++ b/fml/src/main/java/cpw/mods/fml/common/registry/FMLControlledNamespacedRegistry.java @@ -13,6 +13,9 @@ import net.minecraft.item.ItemBlock; import net.minecraft.util.ObjectIntIdentityMap; import net.minecraft.util.RegistryNamespaced; +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; +import com.google.common.collect.ImmutableBiMap; import com.google.common.collect.ImmutableMap; import cpw.mods.fml.common.FMLLog; @@ -28,6 +31,7 @@ public class FMLControlledNamespacedRegistry extends RegistryNamespaced { // aliases redirecting legacy names to the actual name, may need recursive application to find the final name. // these need to be registry specific, it's possible to only have a loosely linked item for a block which may get renamed by itself. private final Map aliases = new HashMap(); + private BiMap persistentAliases = HashBiMap.create(); FMLControlledNamespacedRegistry(String optionalDefault, int maxIdValue, int minIdValue, Class type, char discriminator) { @@ -231,6 +235,9 @@ public class FMLControlledNamespacedRegistry extends RegistryNamespaced { */ public I getRaw(String name) { + String aliasName = persistentAliases.get(name); + name = aliasName != null ? aliasName : name; + I ret = superType.cast(super.getObject(name)); if (ret == null) // no match, try aliases recursively @@ -315,6 +322,10 @@ public class FMLControlledNamespacedRegistry extends RegistryNamespaced { return ImmutableMap.copyOf(aliases); } + public Map getPersistentAliases() + { + return ImmutableBiMap.copyOf(persistentAliases); + } /** * Add the specified object to the registry. * @@ -429,4 +440,12 @@ public class FMLControlledNamespacedRegistry extends RegistryNamespaced { public RegistryDelegate getDelegate(I thing, Class clazz) { return GameData.buildDelegate(thing, clazz); } + + public void addPersistentAlias(String fromName, String toName) throws ExistingAliasException { + if (persistentAliases.containsKey(fromName) || persistentAliases.containsKey(toName) || persistentAliases.containsValue(fromName) || persistentAliases.containsValue(toName)) + { + throw new ExistingAliasException(fromName, toName); + } + persistentAliases.put(fromName, toName); + } } diff --git a/fml/src/main/java/cpw/mods/fml/common/registry/GameData.java b/fml/src/main/java/cpw/mods/fml/common/registry/GameData.java index e701bad010..53949893d5 100644 --- a/fml/src/main/java/cpw/mods/fml/common/registry/GameData.java +++ b/fml/src/main/java/cpw/mods/fml/common/registry/GameData.java @@ -949,6 +949,10 @@ public class GameData { FMLLog.fine("Registry consistency check successful"); } + void registerPersistentAlias(String fromName, String toName, GameRegistry.Type type) throws ExistingAliasException + { + type.getRegistry().addPersistentAlias(fromName, toName); + } static RegistryDelegate buildDelegate(T referant, Class type) { return new RegistryDelegate.Delegate(referant, type); diff --git a/fml/src/main/java/cpw/mods/fml/common/registry/GameRegistry.java b/fml/src/main/java/cpw/mods/fml/common/registry/GameRegistry.java index dfd90f6252..6a26bb3c02 100644 --- a/fml/src/main/java/cpw/mods/fml/common/registry/GameRegistry.java +++ b/fml/src/main/java/cpw/mods/fml/common/registry/GameRegistry.java @@ -145,9 +145,19 @@ public class GameRegistry } - public static void addAlias(String alias, String forName, GameRegistry.Type type) + /** + * Add a forced persistent 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, whereever it is + * referenced. + * + * @param toName The name to link to (this is the NEW block or item) + * @param fromName The name to link from (this is the OLD block or item being substituted) + * @param type The type (Block or Item) + * @throws ExistingAliasException if someone else has already registered an alias either from or to one of the names + */ + public static void addAlias(String toName, String fromName, GameRegistry.Type type) throws ExistingAliasException { - + GameData.getMain().registerPersistentAlias(fromName, toName, type); } /** @@ -300,67 +310,67 @@ public class GameRegistry } /** - * Look up a mod block in the global "named item list" - * @param modId The modid owning the block - * @param name The name of the block itself - * @return The block or null if not found - */ - public static Block findBlock(String modId, String name) - { - return GameData.findBlock(modId, name); - } + * Look up a mod block in the global "named item list" + * @param modId The modid owning the block + * @param name The name of the block itself + * @return The block or null if not found + */ + public static Block findBlock(String modId, String name) + { + return GameData.findBlock(modId, name); + } - /** - * Look up a mod item in the global "named item list" - * @param modId The modid owning the item - * @param name The name of the item itself - * @return The item or null if not found - */ - public static Item findItem(String modId, String name) + /** + * Look up a mod item in the global "named item list" + * @param modId The modid owning the item + * @param name The name of the item itself + * @return The item or null if not found + */ + public static Item findItem(String modId, String name) { return GameData.findItem(modId, name); } - /** - * Manually register a custom item stack with FML for later tracking. It is automatically scoped with the active modid - * - * @param name The name to register it under - * @param itemStack The itemstack to register - */ - public static void registerCustomItemStack(String name, ItemStack itemStack) - { - GameData.registerCustomItemStack(name, itemStack); - } - /** - * Lookup an itemstack based on mod and name. It will create "default" itemstacks from blocks and items if no - * explicit itemstack is found. - * - * If it is built from a block, the metadata is by default the "wildcard" value. - * - * Custom itemstacks can be dumped from minecraft by setting the system property fml.dumpRegistry to true - * (-Dfml.dumpRegistry=true on the command line will work) - * - * @param modId The modid of the stack owner - * @param name The name of the stack - * @param stackSize The size of the stack returned - * @return The custom itemstack or null if no such itemstack was found - */ - public static ItemStack findItemStack(String modId, String name, int stackSize) - { - ItemStack foundStack = GameData.findItemStack(modId, name); - if (foundStack != null) - { + /** + * Manually register a custom item stack with FML for later tracking. It is automatically scoped with the active modid + * + * @param name The name to register it under + * @param itemStack The itemstack to register + */ + public static void registerCustomItemStack(String name, ItemStack itemStack) + { + GameData.registerCustomItemStack(name, itemStack); + } + /** + * Lookup an itemstack based on mod and name. It will create "default" itemstacks from blocks and items if no + * explicit itemstack is found. + * + * If it is built from a block, the metadata is by default the "wildcard" value. + * + * Custom itemstacks can be dumped from minecraft by setting the system property fml.dumpRegistry to true + * (-Dfml.dumpRegistry=true on the command line will work) + * + * @param modId The modid of the stack owner + * @param name The name of the stack + * @param stackSize The size of the stack returned + * @return The custom itemstack or null if no such itemstack was found + */ + public static ItemStack findItemStack(String modId, String name, int stackSize) + { + ItemStack foundStack = GameData.findItemStack(modId, name); + if (foundStack != null) + { ItemStack is = foundStack.copy(); - is.stackSize = Math.min(stackSize, is.getMaxStackSize()); - return is; - } - return null; - } + is.stackSize = Math.min(stackSize, is.getMaxStackSize()); + return is; + } + return null; + } - public static final class UniqueIdentifier - { - public final String modId; - public final String name; + public static final class UniqueIdentifier + { + public final String modId; + public final String name; UniqueIdentifier(String modId, String name) { this.modId = modId; @@ -394,24 +404,41 @@ public class GameRegistry { return String.format("%s:%s", modId, name); } - } + } - public static enum Type { BLOCK, ITEM } + public static enum Type { + BLOCK + { + @Override + public FMLControlledNamespacedRegistry getRegistry() { + return GameData.getBlockRegistry(); + } + }, + ITEM + { + @Override + public FMLControlledNamespacedRegistry getRegistry() { + return GameData.getItemRegistry(); + } + }; + + public abstract FMLControlledNamespacedRegistry getRegistry(); + } /** - * Look up the mod identifier data for a block. - * Returns null if there is no mod specified mod identifier data, or it is part of a - * custom itemstack definition {@link #registerCustomItemStack} - * - * Note: uniqueness and persistence is only guaranteed by mods using the game registry - * correctly. - * - * @param block to lookup + * Look up the mod identifier data for a block. + * Returns null if there is no mod specified mod identifier data, or it is part of a + * custom itemstack definition {@link #registerCustomItemStack} + * + * Note: uniqueness and persistence is only guaranteed by mods using the game registry + * correctly. + * + * @param block to lookup * @return a {@link UniqueIdentifier} for the block or null - */ - public static UniqueIdentifier findUniqueIdentifierFor(Block block) - { - return GameData.getUniqueName(block); - } + */ + public static UniqueIdentifier findUniqueIdentifierFor(Block block) + { + return GameData.getUniqueName(block); + } /** * Look up the mod identifier data for an item. * Returns null if there is no mod specified mod identifier data, or it is part of a