clean up mapping classes
This commit is contained in:
parent
e85166666f
commit
7c495f3d1c
12 changed files with 145 additions and 247 deletions
|
@ -20,9 +20,8 @@ import common.init.ItemRegistry;
|
||||||
import common.item.Item;
|
import common.item.Item;
|
||||||
import common.model.ModelRotation;
|
import common.model.ModelRotation;
|
||||||
import common.util.Facing;
|
import common.util.Facing;
|
||||||
import common.util.IRegistry;
|
|
||||||
import common.util.Pair;
|
import common.util.Pair;
|
||||||
import common.util.RegistrySimple;
|
import common.util.BaseMapping;
|
||||||
import common.world.State;
|
import common.world.State;
|
||||||
|
|
||||||
public abstract class ModelBakery
|
public abstract class ModelBakery
|
||||||
|
@ -47,14 +46,14 @@ public abstract class ModelBakery
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IRegistry<String, IBakedModel> setupModelRegistry(TextureMap textureMap,
|
public static BaseMapping<String, IBakedModel> setupModelRegistry(TextureMap textureMap,
|
||||||
Map<State, String> map)
|
Map<State, String> map)
|
||||||
{
|
{
|
||||||
final Map<String, TextureAtlasSprite> sprites = Maps.<String, TextureAtlasSprite>newHashMap();
|
final Map<String, TextureAtlasSprite> sprites = Maps.<String, TextureAtlasSprite>newHashMap();
|
||||||
Map<String, ModelBlock> models = Maps.<String, ModelBlock>newLinkedHashMap();
|
Map<String, ModelBlock> models = Maps.<String, ModelBlock>newLinkedHashMap();
|
||||||
List<String> variants = Lists.<String>newArrayList();
|
List<String> variants = Lists.<String>newArrayList();
|
||||||
FaceBakery faceBakery = new FaceBakery();
|
FaceBakery faceBakery = new FaceBakery();
|
||||||
RegistrySimple<String, IBakedModel> bakedRegistry = new RegistrySimple();
|
BaseMapping<String, IBakedModel> bakedRegistry = new BaseMapping();
|
||||||
List<String> itemLocations = Lists.<String>newArrayList();
|
List<String> itemLocations = Lists.<String>newArrayList();
|
||||||
models.put(MISSING, (ModelBlock)new ModelBlock(null).add().all());
|
models.put(MISSING, (ModelBlock)new ModelBlock(null).add().all());
|
||||||
variants.add(MISSING);
|
variants.add(MISSING);
|
||||||
|
|
|
@ -11,16 +11,17 @@ import common.block.Block;
|
||||||
import common.collect.Maps;
|
import common.collect.Maps;
|
||||||
import common.init.BlockRegistry;
|
import common.init.BlockRegistry;
|
||||||
import common.properties.Property;
|
import common.properties.Property;
|
||||||
import common.util.IRegistry;
|
import common.util.BaseMapping;
|
||||||
import common.world.State;
|
import common.world.State;
|
||||||
|
|
||||||
public class ModelManager
|
public class ModelManager
|
||||||
{
|
{
|
||||||
private IRegistry<String, IBakedModel> modelRegistry;
|
|
||||||
private final TextureMap texMap;
|
private final TextureMap texMap;
|
||||||
private final Map<State, IBakedModel> bakedModelStore = Maps.<State, IBakedModel>newIdentityHashMap();
|
private final Map<State, IBakedModel> bakedModelStore = Maps.<State, IBakedModel>newIdentityHashMap();
|
||||||
private final Map<Block, StateMap> mappers = Maps.<Block, StateMap>newIdentityHashMap();
|
private final Map<Block, StateMap> mappers = Maps.<Block, StateMap>newIdentityHashMap();
|
||||||
private final Set<Block> builtin = Collections.newSetFromMap(Maps.<Block, Boolean>newIdentityHashMap());
|
private final Set<Block> builtin = Collections.newSetFromMap(Maps.<Block, Boolean>newIdentityHashMap());
|
||||||
|
|
||||||
|
private BaseMapping<String, IBakedModel> modelRegistry;
|
||||||
private IBakedModel defaultModel;
|
private IBakedModel defaultModel;
|
||||||
|
|
||||||
public ModelManager(TextureMap textures)
|
public ModelManager(TextureMap textures)
|
||||||
|
|
|
@ -132,13 +132,13 @@ import common.item.CheatTab;
|
||||||
import common.log.Log;
|
import common.log.Log;
|
||||||
import common.model.TextureAnimation;
|
import common.model.TextureAnimation;
|
||||||
import common.util.ObjectIntIdentityMap;
|
import common.util.ObjectIntIdentityMap;
|
||||||
import common.util.RegistryNamespacedDefaultedByKey;
|
import common.util.DefaultedMapping;
|
||||||
import common.util.Util;
|
import common.util.Util;
|
||||||
import common.world.State;
|
import common.world.State;
|
||||||
|
|
||||||
public abstract class BlockRegistry {
|
public abstract class BlockRegistry {
|
||||||
private static final String AIR_ID = "air";
|
private static final String AIR_ID = "air";
|
||||||
public static final RegistryNamespacedDefaultedByKey<String, Block> REGISTRY = new RegistryNamespacedDefaultedByKey(AIR_ID);
|
public static final DefaultedMapping<String, Block> REGISTRY = new DefaultedMapping(AIR_ID);
|
||||||
public static final ObjectIntIdentityMap<State> STATEMAP = new ObjectIntIdentityMap();
|
public static final ObjectIntIdentityMap<State> STATEMAP = new ObjectIntIdentityMap();
|
||||||
|
|
||||||
private static int nextBlockId = 1;
|
private static int nextBlockId = 1;
|
||||||
|
|
|
@ -119,12 +119,12 @@ import common.log.Log;
|
||||||
import common.potion.Potion;
|
import common.potion.Potion;
|
||||||
import common.potion.PotionHelper;
|
import common.potion.PotionHelper;
|
||||||
import common.util.Pair;
|
import common.util.Pair;
|
||||||
import common.util.RegistryNamespaced;
|
import common.util.Mapping;
|
||||||
import common.util.Util;
|
import common.util.Util;
|
||||||
import common.world.Weather;
|
import common.world.Weather;
|
||||||
|
|
||||||
public abstract class ItemRegistry {
|
public abstract class ItemRegistry {
|
||||||
public static final RegistryNamespaced<String, Item> REGISTRY = new RegistryNamespaced();
|
public static final Mapping<String, Item> REGISTRY = new Mapping();
|
||||||
public static final Map<Block, ItemBlock> BLOCKMAP = Maps.<Block, ItemBlock>newHashMap();
|
public static final Map<Block, ItemBlock> BLOCKMAP = Maps.<Block, ItemBlock>newHashMap();
|
||||||
public static final Set<Block> SPECIALIZED = Sets.<Block>newHashSet();
|
public static final Set<Block> SPECIALIZED = Sets.<Block>newHashSet();
|
||||||
|
|
||||||
|
|
40
common/src/main/java/common/util/BaseMapping.java
Executable file
40
common/src/main/java/common/util/BaseMapping.java
Executable file
|
@ -0,0 +1,40 @@
|
||||||
|
package common.util;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import common.collect.Maps;
|
||||||
|
|
||||||
|
public class BaseMapping<K, V> implements Iterable<V> {
|
||||||
|
protected final Map<K, V> mapping = this.createUnderlyingMap();
|
||||||
|
|
||||||
|
protected Map<K, V> createUnderlyingMap() {
|
||||||
|
return Maps.<K, V>newHashMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
public V getObject(K name) {
|
||||||
|
return this.mapping.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void putObject(K key, V value) {
|
||||||
|
if(key == null)
|
||||||
|
throw new NullPointerException("Schlüssel ist null");
|
||||||
|
if(value == null)
|
||||||
|
throw new NullPointerException("Wert ist null");
|
||||||
|
this.mapping.put(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<K> getKeys() {
|
||||||
|
return Collections.<K>unmodifiableSet(this.mapping.keySet());
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean containsKey(K key) {
|
||||||
|
return this.mapping.containsKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Iterator<V> iterator() {
|
||||||
|
return this.mapping.values().iterator();
|
||||||
|
}
|
||||||
|
}
|
40
common/src/main/java/common/util/DefaultedMapping.java
Executable file
40
common/src/main/java/common/util/DefaultedMapping.java
Executable file
|
@ -0,0 +1,40 @@
|
||||||
|
package common.util;
|
||||||
|
|
||||||
|
public class DefaultedMapping<K, V> extends Mapping<K, V> {
|
||||||
|
private final K defaultKey;
|
||||||
|
|
||||||
|
private V defaultValue;
|
||||||
|
|
||||||
|
public DefaultedMapping(K def) {
|
||||||
|
this.defaultKey = def;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void register(int id, K key, V value) {
|
||||||
|
if(this.defaultKey.equals(key))
|
||||||
|
this.defaultValue = value;
|
||||||
|
super.register(id, key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validateKey() {
|
||||||
|
if(this.defaultValue == null)
|
||||||
|
throw new NullPointerException("Standard-Wert ist null");
|
||||||
|
}
|
||||||
|
|
||||||
|
public V getObject(K name) {
|
||||||
|
V v = super.getObject(name);
|
||||||
|
return v == null ? this.defaultValue : v;
|
||||||
|
}
|
||||||
|
|
||||||
|
public V getObjectExact(K name) {
|
||||||
|
return super.getObject(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public V getObjectById(int id) {
|
||||||
|
V v = super.getObjectById(id);
|
||||||
|
return v == null ? this.defaultValue : v;
|
||||||
|
}
|
||||||
|
|
||||||
|
public V getObjectExact(int id) {
|
||||||
|
return super.getObjectById(id);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,11 +0,0 @@
|
||||||
package common.util;
|
|
||||||
|
|
||||||
public interface IRegistry<K, V> extends Iterable<V>
|
|
||||||
{
|
|
||||||
V getObject(K name);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Register an object on this registry.
|
|
||||||
*/
|
|
||||||
void putObject(K key, V value);
|
|
||||||
}
|
|
55
common/src/main/java/common/util/Mapping.java
Executable file
55
common/src/main/java/common/util/Mapping.java
Executable file
|
@ -0,0 +1,55 @@
|
||||||
|
package common.util;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import common.collect.BiMap;
|
||||||
|
import common.collect.HashBiMap;
|
||||||
|
|
||||||
|
public class Mapping<K, V> extends BaseMapping<K, V> implements IObjectIntIterable<V> {
|
||||||
|
protected final ObjectIntIdentityMap<V> idmap = new ObjectIntIdentityMap();
|
||||||
|
protected final Map<V, K> namemap;
|
||||||
|
|
||||||
|
public Mapping() {
|
||||||
|
this.namemap = ((BiMap)this.mapping).inverse();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void register(int id, K key, V value) {
|
||||||
|
if(this.mapping.containsKey(key))
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Schlüssel " + String.valueOf(key) + " ist bereits mit ID " + this.idmap.get(this.mapping.get(key)) + " registriert");
|
||||||
|
if(this.idmap.getByValue(id) != null)
|
||||||
|
throw new IllegalArgumentException("ID " + id + " ist bereits mit Name " + this.namemap.get(this.idmap.getByValue(id)) + " registriert");
|
||||||
|
|
||||||
|
this.idmap.put(value, id);
|
||||||
|
this.putObject(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Map<K, V> createUnderlyingMap() {
|
||||||
|
return HashBiMap.<K, V>create();
|
||||||
|
}
|
||||||
|
|
||||||
|
public V getObject(K name) {
|
||||||
|
return super.getObject(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public K getNameForObject(V value) {
|
||||||
|
return this.namemap.get(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean containsKey(K key) {
|
||||||
|
return super.containsKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIDForObject(V value) {
|
||||||
|
return this.idmap.get(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public V getObjectById(int id) {
|
||||||
|
return this.idmap.getByValue(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Iterator<V> iterator() {
|
||||||
|
return this.idmap.iterator();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,20 +0,0 @@
|
||||||
package common.util;
|
|
||||||
|
|
||||||
public class RegistryDefaulted<K, V> extends RegistrySimple<K, V>
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Default object for this registry, returned when an object is not found.
|
|
||||||
*/
|
|
||||||
private final V defaultObject;
|
|
||||||
|
|
||||||
public RegistryDefaulted(V defaultObjectIn)
|
|
||||||
{
|
|
||||||
this.defaultObject = defaultObjectIn;
|
|
||||||
}
|
|
||||||
|
|
||||||
public V getObject(K name)
|
|
||||||
{
|
|
||||||
V v = super.getObject(name);
|
|
||||||
return (V)(v == null ? this.defaultObject : v);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,78 +0,0 @@
|
||||||
package common.util;
|
|
||||||
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import common.collect.BiMap;
|
|
||||||
import common.collect.HashBiMap;
|
|
||||||
|
|
||||||
public class RegistryNamespaced<K, V> extends RegistrySimple<K, V> implements IObjectIntIterable<V>
|
|
||||||
{
|
|
||||||
protected final ObjectIntIdentityMap<V> underlyingIntegerMap = new ObjectIntIdentityMap();
|
|
||||||
protected final Map<V, K> inverseObjectRegistry;
|
|
||||||
|
|
||||||
public RegistryNamespaced()
|
|
||||||
{
|
|
||||||
this.inverseObjectRegistry = ((BiMap)this.registryObjects).inverse();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void register(int id, K key, V value)
|
|
||||||
{
|
|
||||||
if(this.registryObjects.containsKey(key))
|
|
||||||
throw new IllegalArgumentException("Schlüssel " + String.valueOf(key) + " ist bereits mit ID " +
|
|
||||||
this.underlyingIntegerMap.get(this.registryObjects.get(key)) + " registriert");
|
|
||||||
if(this.underlyingIntegerMap.getByValue(id) != null)
|
|
||||||
throw new IllegalArgumentException("ID " + id + " ist bereits mit Name " +
|
|
||||||
this.inverseObjectRegistry.get(this.underlyingIntegerMap.getByValue(id)) + " registriert");
|
|
||||||
|
|
||||||
this.underlyingIntegerMap.put(value, id);
|
|
||||||
this.putObject(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Map<K, V> createUnderlyingMap()
|
|
||||||
{
|
|
||||||
return HashBiMap.<K, V>create();
|
|
||||||
}
|
|
||||||
|
|
||||||
public V getObject(K name)
|
|
||||||
{
|
|
||||||
return super.getObject(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the name we use to identify the given object.
|
|
||||||
*/
|
|
||||||
public K getNameForObject(V value)
|
|
||||||
{
|
|
||||||
return (K)this.inverseObjectRegistry.get(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Does this registry contain an entry for the given key?
|
|
||||||
*/
|
|
||||||
public boolean containsKey(K key)
|
|
||||||
{
|
|
||||||
return super.containsKey(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the integer ID we use to identify the given object.
|
|
||||||
*/
|
|
||||||
public int getIDForObject(V value)
|
|
||||||
{
|
|
||||||
return this.underlyingIntegerMap.get(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the object identified by the given ID.
|
|
||||||
*/
|
|
||||||
public V getObjectById(int id)
|
|
||||||
{
|
|
||||||
return (V)this.underlyingIntegerMap.getByValue(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Iterator<V> iterator()
|
|
||||||
{
|
|
||||||
return this.underlyingIntegerMap.iterator();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,67 +0,0 @@
|
||||||
package common.util;
|
|
||||||
|
|
||||||
public class RegistryNamespacedDefaultedByKey<K, V> extends RegistryNamespaced<K, V>
|
|
||||||
{
|
|
||||||
/** The key of the default value. */
|
|
||||||
private final K defaultValueKey;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The default value for this registry, retrurned in the place of a null value.
|
|
||||||
*/
|
|
||||||
private V defaultValue;
|
|
||||||
|
|
||||||
public RegistryNamespacedDefaultedByKey(K defaultValueKeyIn)
|
|
||||||
{
|
|
||||||
this.defaultValueKey = defaultValueKeyIn;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void register(int id, K key, V value)
|
|
||||||
{
|
|
||||||
if (this.defaultValueKey.equals(key))
|
|
||||||
{
|
|
||||||
this.defaultValue = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if(this.getObjectExact(key) != null)
|
|
||||||
// throw new IllegalArgumentException("Schlüssel " + String.valueOf(key) + " ist bereits registriert");
|
|
||||||
// if(this.getObjectExact(id) != null)
|
|
||||||
// throw new IllegalArgumentException("ID " + id + " ist bereits registriert");
|
|
||||||
|
|
||||||
super.register(id, key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* validates that this registry's key is non-null
|
|
||||||
*/
|
|
||||||
public void validateKey()
|
|
||||||
{
|
|
||||||
if (this.defaultValueKey == null) {
|
|
||||||
throw new NullPointerException("Standard-Schlüssel ist Null");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public V getObject(K name)
|
|
||||||
{
|
|
||||||
V v = super.getObject(name);
|
|
||||||
return (V)(v == null ? this.defaultValue : v);
|
|
||||||
}
|
|
||||||
|
|
||||||
public V getObjectExact(K name)
|
|
||||||
{
|
|
||||||
return super.getObject(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the object identified by the given ID.
|
|
||||||
*/
|
|
||||||
public V getObjectById(int id)
|
|
||||||
{
|
|
||||||
V v = super.getObjectById(id);
|
|
||||||
return (V)(v == null ? this.defaultValue : v);
|
|
||||||
}
|
|
||||||
|
|
||||||
public V getObjectExact(int id)
|
|
||||||
{
|
|
||||||
return super.getObjectById(id);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,61 +0,0 @@
|
||||||
package common.util;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import common.collect.Maps;
|
|
||||||
|
|
||||||
public class RegistrySimple<K, V> implements IRegistry<K, V>
|
|
||||||
{
|
|
||||||
protected final Map<K, V> registryObjects = this.createUnderlyingMap();
|
|
||||||
|
|
||||||
protected Map<K, V> createUnderlyingMap()
|
|
||||||
{
|
|
||||||
return Maps.<K, V>newHashMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
public V getObject(K name)
|
|
||||||
{
|
|
||||||
return this.registryObjects.get(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Register an object on this registry.
|
|
||||||
*/
|
|
||||||
public void putObject(K key, V value)
|
|
||||||
{
|
|
||||||
if (key == null) {
|
|
||||||
throw new NullPointerException("Key is null");
|
|
||||||
}
|
|
||||||
if (value == null) {
|
|
||||||
throw new NullPointerException("Value is null");
|
|
||||||
}
|
|
||||||
|
|
||||||
// if (this.registryObjects.containsKey(key))
|
|
||||||
// {
|
|
||||||
// Log.CONFIG.debug("Füge doppelten Schlüssel \'" + key + "\' der Registry hinzu");
|
|
||||||
// }
|
|
||||||
|
|
||||||
this.registryObjects.put(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<K> getKeys()
|
|
||||||
{
|
|
||||||
return Collections.<K>unmodifiableSet(this.registryObjects.keySet());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Does this registry contain an entry for the given key?
|
|
||||||
*/
|
|
||||||
public boolean containsKey(K key)
|
|
||||||
{
|
|
||||||
return this.registryObjects.containsKey(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Iterator<V> iterator()
|
|
||||||
{
|
|
||||||
return this.registryObjects.values().iterator();
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue