2013-12-09 06:22:24 +00:00
|
|
|
package cpw.mods.fml.common.registry;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.BitSet;
|
2014-03-20 11:41:59 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
2013-12-10 02:36:49 +00:00
|
|
|
import java.util.List;
|
2013-12-09 06:22:24 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
2014-03-20 11:41:59 +00:00
|
|
|
|
2013-12-10 02:36:49 +00:00
|
|
|
import net.minecraft.util.ObjectIntIdentityMap;
|
|
|
|
import net.minecraft.util.RegistryNamespaced;
|
2014-03-20 11:41:59 +00:00
|
|
|
|
2013-12-09 06:22:24 +00:00
|
|
|
import com.google.common.collect.BiMap;
|
|
|
|
import com.google.common.collect.HashBiMap;
|
2014-03-24 23:36:37 +00:00
|
|
|
import com.google.common.collect.ImmutableMap;
|
2013-12-09 06:22:24 +00:00
|
|
|
import com.google.common.collect.Maps;
|
2014-03-20 11:41:59 +00:00
|
|
|
|
2013-12-26 17:41:49 +00:00
|
|
|
import cpw.mods.fml.common.FMLLog;
|
2013-12-09 06:22:24 +00:00
|
|
|
import cpw.mods.fml.common.Loader;
|
|
|
|
import cpw.mods.fml.common.ModContainer;
|
|
|
|
|
|
|
|
public class FMLControlledNamespacedRegistry<I> extends RegistryNamespaced {
|
|
|
|
private final Class<I> superType;
|
|
|
|
private String optionalDefaultName;
|
|
|
|
private I optionalDefaultObject;
|
2014-03-24 23:36:37 +00:00
|
|
|
private int maxId;
|
2013-12-09 06:22:24 +00:00
|
|
|
private int minId;
|
2013-12-10 02:36:49 +00:00
|
|
|
private char discriminator;
|
2014-03-24 23:36:37 +00:00
|
|
|
// aliases redirecting legacy names to the actual name, may need recursive application to find the final name
|
|
|
|
private final Map<String, String> aliases = new HashMap<String, String>();
|
2013-12-09 06:22:24 +00:00
|
|
|
|
2014-03-20 11:41:59 +00:00
|
|
|
FMLControlledNamespacedRegistry(String optionalDefault, int maxIdValue, int minIdValue, Class<I> type, char discriminator)
|
2013-12-09 06:22:24 +00:00
|
|
|
{
|
|
|
|
this.superType = type;
|
2013-12-10 02:36:49 +00:00
|
|
|
this.discriminator = discriminator;
|
2013-12-09 06:22:24 +00:00
|
|
|
this.optionalDefaultName = optionalDefault;
|
|
|
|
this.maxId = maxIdValue;
|
|
|
|
this.minId = minIdValue;
|
|
|
|
}
|
|
|
|
|
2014-03-20 11:41:59 +00:00
|
|
|
void set(FMLControlledNamespacedRegistry<I> registry)
|
2013-12-09 06:22:24 +00:00
|
|
|
{
|
2014-03-20 11:41:59 +00:00
|
|
|
if (this.superType != registry.superType) throw new IllegalArgumentException("incompatible registry");
|
2013-12-09 06:22:24 +00:00
|
|
|
|
2014-03-20 11:41:59 +00:00
|
|
|
this.discriminator = registry.discriminator;
|
|
|
|
this.optionalDefaultName = registry.optionalDefaultName;
|
|
|
|
this.maxId = registry.maxId;
|
|
|
|
this.minId = registry.minId;
|
2014-03-24 23:36:37 +00:00
|
|
|
this.aliases.putAll(registry.aliases);
|
2014-03-20 11:41:59 +00:00
|
|
|
field_148759_a = new ObjectIntIdentityMap();
|
|
|
|
field_82596_a.clear();
|
2013-12-26 17:41:49 +00:00
|
|
|
|
2014-03-20 11:41:59 +00:00
|
|
|
for (Iterator<Object> it = registry.iterator(); it.hasNext(); )
|
2013-12-26 17:41:49 +00:00
|
|
|
{
|
2014-03-20 11:41:59 +00:00
|
|
|
I obj = (I) it.next();
|
|
|
|
|
|
|
|
super.func_148756_a(registry.getId(obj), registry.func_148750_c(obj), obj);
|
2013-12-26 17:41:49 +00:00
|
|
|
}
|
2014-03-20 11:41:59 +00:00
|
|
|
}
|
2013-12-26 17:41:49 +00:00
|
|
|
|
2014-03-24 23:36:37 +00:00
|
|
|
// public api
|
|
|
|
|
2014-03-20 11:41:59 +00:00
|
|
|
/**
|
|
|
|
* Add an object to the registry, trying to use the specified id.
|
|
|
|
*
|
|
|
|
* @deprecated register through {@link GameRegistry} instead.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Deprecated
|
|
|
|
public void func_148756_a(int id, String name, Object thing)
|
|
|
|
{
|
|
|
|
GameData.getMain().register(thing, name, id);
|
2013-12-24 05:47:10 +00:00
|
|
|
}
|
2014-03-20 11:41:59 +00:00
|
|
|
|
2013-12-09 06:22:24 +00:00
|
|
|
@Override
|
|
|
|
public I func_82594_a(String name)
|
|
|
|
{
|
2014-03-24 23:36:37 +00:00
|
|
|
I object = getRaw(name);
|
2013-12-09 06:22:24 +00:00
|
|
|
return object == null ? this.optionalDefaultObject : object;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public I func_148754_a(int id)
|
|
|
|
{
|
2014-03-24 23:36:37 +00:00
|
|
|
I object = getRaw(id);
|
2013-12-09 06:22:24 +00:00
|
|
|
return object == null ? this.optionalDefaultObject : object;
|
|
|
|
}
|
|
|
|
|
2014-03-24 23:36:37 +00:00
|
|
|
/**
|
|
|
|
* Get the object identified by the specified id.
|
|
|
|
*
|
|
|
|
* The default object is the air block for the block registry or null for the item registry.
|
|
|
|
*
|
|
|
|
* @param id Block/Item id.
|
|
|
|
* @return Block/Item object or the default object if it wasn't found.
|
|
|
|
*/
|
|
|
|
public I get(int id)
|
|
|
|
{
|
|
|
|
return func_148754_a(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the object identified by the specified name.
|
|
|
|
*
|
|
|
|
* The default object is the air block for the block registry or null for the item registry.
|
|
|
|
*
|
|
|
|
* @param name Block/Item name.
|
|
|
|
* @return Block/Item object or the default object if it wasn't found.
|
|
|
|
*/
|
|
|
|
public I get(String name)
|
|
|
|
{
|
|
|
|
return func_82594_a(name);
|
|
|
|
}
|
2013-12-09 06:22:24 +00:00
|
|
|
|
2014-03-24 23:36:37 +00:00
|
|
|
/**
|
|
|
|
* Get the id for the specified object.
|
|
|
|
*
|
|
|
|
* Don't hold onto the id across the world, it's being dynamically re-mapped as needed.
|
|
|
|
*
|
|
|
|
* Usually the name should be used instead of the id, if using the Block/Item object itself is
|
|
|
|
* not suitable for the task.
|
|
|
|
*
|
|
|
|
* @param think Block/Item object.
|
|
|
|
* @return Block/Item id or -1 if it wasn't found.
|
|
|
|
*/
|
|
|
|
public int getId(I thing)
|
2013-12-09 06:22:24 +00:00
|
|
|
{
|
2014-03-24 23:36:37 +00:00
|
|
|
return func_148757_b(thing);
|
2013-12-10 02:36:49 +00:00
|
|
|
}
|
|
|
|
|
2014-03-24 23:36:37 +00:00
|
|
|
/**
|
|
|
|
* Get the object identified by the specified id.
|
|
|
|
*
|
|
|
|
* @param id Block/Item id.
|
|
|
|
* @return Block/Item object or null if it wasn't found.
|
|
|
|
*/
|
|
|
|
public I getRaw(int id)
|
2013-12-10 02:36:49 +00:00
|
|
|
{
|
2014-03-24 23:36:37 +00:00
|
|
|
return superType.cast(super.func_148754_a(id));
|
2013-12-10 02:36:49 +00:00
|
|
|
}
|
2013-12-09 06:22:24 +00:00
|
|
|
|
2014-03-24 23:36:37 +00:00
|
|
|
/**
|
|
|
|
* Get the object identified by the specified name.
|
|
|
|
*
|
|
|
|
* @param name Block/Item name.
|
|
|
|
* @return Block/Item object or null if it wasn't found.
|
|
|
|
*/
|
|
|
|
public I getRaw(String name)
|
2013-12-10 02:36:49 +00:00
|
|
|
{
|
2014-03-24 23:36:37 +00:00
|
|
|
I ret = superType.cast(super.func_82594_a(name));
|
2013-12-10 02:36:49 +00:00
|
|
|
|
2014-03-24 23:36:37 +00:00
|
|
|
if (ret == null) // no match, try aliases recursively
|
2013-12-24 05:47:10 +00:00
|
|
|
{
|
2014-03-24 23:36:37 +00:00
|
|
|
name = aliases.get(name);
|
|
|
|
|
|
|
|
if (name != null) return getRaw(name);
|
2013-12-24 05:47:10 +00:00
|
|
|
}
|
2013-12-09 06:22:24 +00:00
|
|
|
|
2014-03-20 11:41:59 +00:00
|
|
|
return ret;
|
2013-12-18 16:15:03 +00:00
|
|
|
}
|
|
|
|
|
2014-03-24 23:36:37 +00:00
|
|
|
@Override
|
|
|
|
public boolean func_148741_d(String name)
|
2013-12-09 06:22:24 +00:00
|
|
|
{
|
2014-03-24 23:36:37 +00:00
|
|
|
boolean ret = super.func_148741_d(name);
|
2013-12-09 06:22:24 +00:00
|
|
|
|
2014-03-24 23:36:37 +00:00
|
|
|
if (!ret) // no match, try aliases recursively
|
|
|
|
{
|
|
|
|
name = aliases.get(name);
|
2013-12-09 06:22:24 +00:00
|
|
|
|
2014-03-24 23:36:37 +00:00
|
|
|
if (name != null) return func_148741_d(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2013-12-09 06:22:24 +00:00
|
|
|
}
|
2013-12-10 02:36:49 +00:00
|
|
|
|
2014-03-24 23:36:37 +00:00
|
|
|
public int getId(String itemName)
|
2013-12-09 06:22:24 +00:00
|
|
|
{
|
2014-03-24 23:36:37 +00:00
|
|
|
I obj = getRaw(itemName);
|
|
|
|
if (obj == null) return -1;
|
|
|
|
|
|
|
|
return getId(obj);
|
2013-12-09 06:22:24 +00:00
|
|
|
}
|
2013-12-10 02:36:49 +00:00
|
|
|
|
2014-03-24 23:36:37 +00:00
|
|
|
public boolean contains(String itemName)
|
2013-12-10 02:36:49 +00:00
|
|
|
{
|
2014-03-24 23:36:37 +00:00
|
|
|
return func_148741_d(itemName);
|
2013-12-10 02:36:49 +00:00
|
|
|
}
|
|
|
|
|
2014-03-24 23:36:37 +00:00
|
|
|
// internal
|
|
|
|
|
2014-03-20 11:41:59 +00:00
|
|
|
public void serializeInto(Map<String, Integer> idMapping)
|
2013-12-10 02:36:49 +00:00
|
|
|
{
|
2014-03-20 11:41:59 +00:00
|
|
|
for (Iterator<Object> it = iterator(); it.hasNext(); )
|
|
|
|
{
|
|
|
|
I thing = (I) it.next();
|
|
|
|
idMapping.put(discriminator+func_148750_c(thing), getId(thing));
|
|
|
|
}
|
2013-12-10 02:36:49 +00:00
|
|
|
}
|
|
|
|
|
2014-03-24 23:36:37 +00:00
|
|
|
public Map<String, String> getAliases()
|
2013-12-10 02:36:49 +00:00
|
|
|
{
|
2014-03-24 23:36:37 +00:00
|
|
|
return ImmutableMap.copyOf(aliases);
|
|
|
|
}
|
2014-03-20 11:41:59 +00:00
|
|
|
|
2014-03-24 23:36:37 +00:00
|
|
|
int add(int id, String name, I thing, BitSet availabilityMap)
|
|
|
|
{
|
|
|
|
if (name.equals(optionalDefaultName))
|
|
|
|
{
|
|
|
|
this.optionalDefaultObject = thing;
|
|
|
|
}
|
|
|
|
|
|
|
|
int idToUse = id;
|
|
|
|
if (id == 0 || availabilityMap.get(id))
|
|
|
|
{
|
|
|
|
idToUse = availabilityMap.nextClearBit(minId);
|
|
|
|
}
|
|
|
|
if (idToUse >= maxId)
|
|
|
|
{
|
|
|
|
throw new RuntimeException(String.format("Invalid id %s - not accepted",id));
|
|
|
|
}
|
|
|
|
|
|
|
|
ModContainer mc = Loader.instance().activeModContainer();
|
|
|
|
if (mc != null)
|
|
|
|
{
|
|
|
|
String prefix = mc.getModId();
|
|
|
|
name = prefix + ":"+ name;
|
|
|
|
}
|
|
|
|
super.func_148756_a(idToUse, name, thing);
|
|
|
|
FMLLog.finer("Add : %s %d %s", name, idToUse, thing);
|
|
|
|
return idToUse;
|
2013-12-10 02:36:49 +00:00
|
|
|
}
|
|
|
|
|
2014-03-24 23:36:37 +00:00
|
|
|
void addAlias(String from, String to)
|
2013-12-10 02:36:49 +00:00
|
|
|
{
|
2014-03-24 23:36:37 +00:00
|
|
|
aliases.put(from, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String,Integer> getEntriesNotIn(FMLControlledNamespacedRegistry<I> registry)
|
|
|
|
{
|
|
|
|
Map<String,Integer> ret = new HashMap<String, Integer>();
|
|
|
|
|
|
|
|
for (Iterator<Object> it = iterator(); it.hasNext(); )
|
|
|
|
{
|
|
|
|
I thing = (I) it.next();
|
|
|
|
if (!registry.field_148758_b.containsKey(thing)) ret.put(func_148750_c(thing), getId(thing));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2013-12-10 02:36:49 +00:00
|
|
|
}
|
2013-12-26 17:41:49 +00:00
|
|
|
|
|
|
|
void dump()
|
|
|
|
{
|
2014-03-20 11:41:59 +00:00
|
|
|
List<Integer> ids = new ArrayList<Integer>();
|
|
|
|
|
|
|
|
for (Iterator<Object> it = iterator(); it.hasNext(); )
|
2013-12-26 17:41:49 +00:00
|
|
|
{
|
2014-03-20 11:41:59 +00:00
|
|
|
ids.add(getId((I) it.next()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort by id
|
|
|
|
Collections.sort(ids);
|
|
|
|
|
|
|
|
for (int id : ids)
|
|
|
|
{
|
|
|
|
I thing = getRaw(id);
|
|
|
|
FMLLog.finer("Registry : %s %d %s", func_148750_c(thing), id, thing);
|
2013-12-26 17:41:49 +00:00
|
|
|
}
|
2014-02-01 11:05:33 +00:00
|
|
|
}
|
2013-12-09 06:22:24 +00:00
|
|
|
}
|