First draft of add alias. It is probably not properly persistent atm.

This commit is contained in:
Christian 2014-08-01 21:06:18 -02:30
parent 7ee877a567
commit 07466ab036
5 changed files with 170 additions and 71 deletions

View File

@ -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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> itemAliases = new HashMap<String, String>();
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<String, String> 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);
}

View File

@ -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;
}

View File

@ -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<I> 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<String, String> aliases = new HashMap<String, String>();
private BiMap<String, String> persistentAliases = HashBiMap.create();
FMLControlledNamespacedRegistry(String optionalDefault, int maxIdValue, int minIdValue, Class<I> type, char discriminator)
{
@ -231,6 +235,9 @@ public class FMLControlledNamespacedRegistry<I> 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<I> extends RegistryNamespaced {
return ImmutableMap.copyOf(aliases);
}
public Map<String, String> getPersistentAliases()
{
return ImmutableBiMap.copyOf(persistentAliases);
}
/**
* Add the specified object to the registry.
*
@ -429,4 +440,12 @@ public class FMLControlledNamespacedRegistry<I> extends RegistryNamespaced {
public RegistryDelegate<I> getDelegate(I thing, Class<I> 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);
}
}

View File

@ -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 <T> RegistryDelegate<T> buildDelegate(T referant, Class<T> type)
{
return new RegistryDelegate.Delegate<T>(referant, type);

View File

@ -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