Some registry tweaking to provde a non-complicated API modders can use.

This commit is contained in:
LexManos committed 2016-03-22 01:00:25 -07:00
1 parent 08663587d4
commit 46d57dc467
16 files changed
+272 -220

No files matched your search

@@ -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());
}
}
}
@@ -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;
}
}
}
@@ -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);
}
@@ -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<I> extends RegistryNamespacedDefaultedByKey<ResourceLocation, I>
public class FMLControlledNamespacedRegistry<I extends IForgeRegistryEntry> extends RegistryNamespacedDefaultedByKey<ResourceLocation, I> implements IForgeRegistry<I>
{
public static final boolean DEBUG = Boolean.parseBoolean(System.getProperty("fml.debugRegistryEntries", "false"));
private final Class<I> superType;
@@ -673,7 +675,7 @@ public class FMLControlledNamespacedRegistry<I> extends RegistryNamespacedDefaul
}
@SuppressWarnings("unchecked")
public <T> FMLControlledNamespacedRegistry<T> asType(Class<? extends T> type)
public <T extends IForgeRegistryEntry> FMLControlledNamespacedRegistry<T> asType(Class<? extends T> type)
{
return (FMLControlledNamespacedRegistry<T>)this;
}
@@ -766,11 +768,102 @@ public class FMLControlledNamespacedRegistry<I> 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<I> getValues()
{
return Lists.newArrayList(this.iterator());
}
@Override
public Set<Entry<ResourceLocation, I>> getEntries()
{
return Sets.newHashSet(new Iterator<Entry<ResourceLocation, I>>()
{
Iterator<I> itr = FMLControlledNamespacedRegistry.this.iterator();
@Override
public boolean hasNext()
{
return itr.hasNext();
}
@Override
public Entry<ResourceLocation, I> next()
{
final I value = itr.next();
final ResourceLocation key = FMLControlledNamespacedRegistry.this.get(value);
return new Entry<ResourceLocation, I>()
{
@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.");
}
});
}
}
@@ -221,7 +221,7 @@ public class GameData
}
}
public <T> RegistryDelegate<T> makeDelegate(T obj, Class<T> rootClass)
public <T extends IForgeRegistryEntry<T>> RegistryDelegate<T> makeDelegate(T obj, Class<T> rootClass)
{
return PersistentRegistryManager.makeDelegate(obj, rootClass);
}
@@ -273,4 +273,4 @@ public class GameData
// no op for the minute?
}
}
}
}
@@ -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());
}
/**
@@ -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<V extends IForgeRegistryEntry> extends Iterable<V>
{
void register(V value);
boolean contains(ResourceLocation key);
boolean contains(V value);
V get(ResourceLocation key);
ResourceLocation get(V value);
Set<ResourceLocation> getKeys();
List<V> getValues();
Set<Entry<ResourceLocation, V>> getEntries();
}
@@ -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<V>
{
/**
* 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<T extends IForgeRegistryEntry<T>> implements IForgeRegistryEntry<T>
{
private TypeToken<T> token = new TypeToken<T>(getClass()){};
public final RegistryDelegate<T> delegate = PersistentRegistryManager.makeDelegate((T)this, (Class<T>)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;
}
}
}
@@ -48,30 +48,25 @@ public class PersistentRegistryManager
ACTIVE, FROZEN, STAGING;
private final BiMap<ResourceLocation, FMLControlledNamespacedRegistry<?>> registries = HashBiMap.create();
private final BiMap<Class<?>, ResourceLocation> registrySuperTypes = HashBiMap.create();
private final BiMap<Class<IForgeRegistryEntry>, ResourceLocation> registrySuperTypes = HashBiMap.create();
@SuppressWarnings("unchecked")
<T> FMLControlledNamespacedRegistry<T> getRegistry(ResourceLocation key, Class<T> regType)
<T extends IForgeRegistryEntry<T>> FMLControlledNamespacedRegistry<T> getRegistry(ResourceLocation key, Class<T> regType)
{
return (FMLControlledNamespacedRegistry<T>)registries.get(key);
}
<T> FMLControlledNamespacedRegistry<T> getOrShallowCopyRegistry(ResourceLocation key, Class<T> regType, FMLControlledNamespacedRegistry<T> other)
<T extends IForgeRegistryEntry<T>> FMLControlledNamespacedRegistry<T> getOrShallowCopyRegistry(ResourceLocation key, Class<T> regType, FMLControlledNamespacedRegistry<T> 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 <T> FMLControlledNamespacedRegistry<T> createRegistry(ResourceLocation registryName, Class<T> registryType, ResourceLocation optionalDefaultKey, int minId, int maxId, boolean hasDelegates)
{
return this.createRegistry(registryName, registryType, optionalDefaultKey, minId, maxId, hasDelegates, null);
}
private <T> FMLControlledNamespacedRegistry<T> createRegistry(ResourceLocation registryName, Class<T> type, ResourceLocation defaultObjectKey, int minId, int maxId, boolean isDelegated, FMLControlledNamespacedRegistry.AddCallback<T> addCallback)
private <T extends IForgeRegistryEntry<T>> FMLControlledNamespacedRegistry<T> createRegistry(ResourceLocation registryName, Class<T> type, ResourceLocation defaultObjectKey, int minId, int maxId, boolean isDelegated, FMLControlledNamespacedRegistry.AddCallback<T> addCallback)
{
Set<Class<?>> parents = Sets.newHashSet();
findSuperTypes(type, parents);
@@ -84,7 +79,7 @@ public class PersistentRegistryManager
}
FMLControlledNamespacedRegistry<T> fmlControlledNamespacedRegistry = new FMLControlledNamespacedRegistry<T>(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 <T> FMLControlledNamespacedRegistry<T> getRegistry(Class<T> rootClass)
public <T extends IForgeRegistryEntry<T>> FMLControlledNamespacedRegistry<T> getRegistry(Class<T> 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 <T> FMLControlledNamespacedRegistry<T> createRegistry(ResourceLocation registryName, Class<T> registryType, ResourceLocation optionalDefaultKey, int maxId, int minId, boolean hasDelegates, FMLControlledNamespacedRegistry.AddCallback<T> addCallback)
public static <T extends IForgeRegistryEntry<T>> FMLControlledNamespacedRegistry<T> createRegistry(ResourceLocation registryName, Class<T> registryType, ResourceLocation optionalDefaultKey, int maxId, int minId, boolean hasDelegates, FMLControlledNamespacedRegistry.AddCallback<T> addCallback)
{
return PersistentRegistry.ACTIVE.createRegistry(registryName, registryType, optionalDefaultKey, minId, maxId, hasDelegates, addCallback);
}
public static <T> FMLControlledNamespacedRegistry<T> createRegistry(ResourceLocation registryName, Class<T> registryType, ResourceLocation optionalDefaultKey, int maxId, int minId, boolean hasDelegates)
public static <T extends IForgeRegistryEntry<T>> FMLControlledNamespacedRegistry<T> createRegistry(ResourceLocation registryName, Class<T> 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<String> injectSnapshot(GameDataSnapshot snapshot, boolean injectFrozenData, boolean isLocalWorld)
@@ -256,7 +251,7 @@ public class PersistentRegistryManager
}
}
private static <T> void loadRegistry(final ResourceLocation registryName, final PersistentRegistry from, final PersistentRegistry to, Class<T> regType)
private static <T extends IForgeRegistryEntry<T>> void loadRegistry(final ResourceLocation registryName, final PersistentRegistry from, final PersistentRegistry to, Class<T> regType)
{
FMLControlledNamespacedRegistry<T> fromRegistry = from.getRegistry(registryName, regType);
if (fromRegistry == null)
@@ -292,14 +287,14 @@ public class PersistentRegistryManager
}
}
private static <T> void loadFrozenDataToStagingRegistry(Map<ResourceLocation, Map<ResourceLocation, Integer[]>> remaps, ResourceLocation registryName, Class<T> regType)
private static <T extends IForgeRegistryEntry<T>> void loadFrozenDataToStagingRegistry(Map<ResourceLocation, Map<ResourceLocation, Integer[]>> remaps, ResourceLocation registryName, Class<T> regType)
{
FMLControlledNamespacedRegistry<T> frozenRegistry = PersistentRegistry.FROZEN.getRegistry(registryName, regType);
FMLControlledNamespacedRegistry<T> newRegistry = PersistentRegistry.STAGING.getOrShallowCopyRegistry(registryName, regType, frozenRegistry);
newRegistry.loadIds(frozenRegistry.getEntriesNotIn(newRegistry), Maps.<ResourceLocation, Integer>newLinkedHashMap(), remaps.get(registryName), frozenRegistry, registryName);
}
private static <T> void loadPersistentDataToStagingRegistry(boolean injectFrozenData, Map<ResourceLocation, Map<ResourceLocation, Integer[]>> remaps, LinkedHashMap<ResourceLocation, Map<ResourceLocation, Integer>> missing, Map.Entry<ResourceLocation, GameDataSnapshot.Entry> snapEntry, Class<T> regType)
private static <T extends IForgeRegistryEntry<T>> void loadPersistentDataToStagingRegistry(boolean injectFrozenData, Map<ResourceLocation, Map<ResourceLocation, Integer[]>> remaps, LinkedHashMap<ResourceLocation, Map<ResourceLocation, Integer>> missing, Map.Entry<ResourceLocation, GameDataSnapshot.Entry> snapEntry, Class<T> regType)
{
ResourceLocation registryName = snapEntry.getKey();
@@ -576,7 +571,7 @@ public class PersistentRegistryManager
public final Map<ResourceLocation, Entry> entries = Maps.newHashMap();
}
public static <T> RegistryDelegate<T> makeDelegate(T obj, Class<T> rootClass)
public static <T extends IForgeRegistryEntry<T>> RegistryDelegate<T> makeDelegate(T obj, Class<T> rootClass)
{
return PersistentRegistry.ACTIVE.getRegistry(rootClass).getDelegate(obj, rootClass);
}
@@ -23,23 +23,12 @@ public interface RegistryDelegate<T> {
*/
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<T> {
}
@Override
public String name() {
return name.toString();
}
@Override
public ResourceLocation getResourceName() { return name; }
public ResourceLocation name() { return name; }
@Override
public Class<T> type()
@@ -250,7 +250,7 @@ public class VillagerRegistry
}
}
public static class VillagerProfession
public static class VillagerProfession extends IForgeRegistryEntry.Impl
{
private ResourceLocation name;
private ResourceLocation texture;
@@ -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)
{