diff --git a/client/src/main/java/client/renderer/blockmodel/ModelBakery.java b/client/src/main/java/client/renderer/blockmodel/ModelBakery.java index 5e812141..626421c3 100755 --- a/client/src/main/java/client/renderer/blockmodel/ModelBakery.java +++ b/client/src/main/java/client/renderer/blockmodel/ModelBakery.java @@ -20,9 +20,8 @@ import common.init.ItemRegistry; import common.item.Item; import common.model.ModelRotation; import common.util.Facing; -import common.util.IRegistry; import common.util.Pair; -import common.util.RegistrySimple; +import common.util.BaseMapping; import common.world.State; public abstract class ModelBakery @@ -47,14 +46,14 @@ public abstract class ModelBakery } } - public static IRegistry setupModelRegistry(TextureMap textureMap, + public static BaseMapping setupModelRegistry(TextureMap textureMap, Map map) { final Map sprites = Maps.newHashMap(); Map models = Maps.newLinkedHashMap(); List variants = Lists.newArrayList(); FaceBakery faceBakery = new FaceBakery(); - RegistrySimple bakedRegistry = new RegistrySimple(); + BaseMapping bakedRegistry = new BaseMapping(); List itemLocations = Lists.newArrayList(); models.put(MISSING, (ModelBlock)new ModelBlock(null).add().all()); variants.add(MISSING); diff --git a/client/src/main/java/client/renderer/blockmodel/ModelManager.java b/client/src/main/java/client/renderer/blockmodel/ModelManager.java index aaa7cc5f..08b10b43 100755 --- a/client/src/main/java/client/renderer/blockmodel/ModelManager.java +++ b/client/src/main/java/client/renderer/blockmodel/ModelManager.java @@ -11,16 +11,17 @@ import common.block.Block; import common.collect.Maps; import common.init.BlockRegistry; import common.properties.Property; -import common.util.IRegistry; +import common.util.BaseMapping; import common.world.State; public class ModelManager { - private IRegistry modelRegistry; private final TextureMap texMap; private final Map bakedModelStore = Maps.newIdentityHashMap(); private final Map mappers = Maps.newIdentityHashMap(); private final Set builtin = Collections.newSetFromMap(Maps.newIdentityHashMap()); + + private BaseMapping modelRegistry; private IBakedModel defaultModel; public ModelManager(TextureMap textures) diff --git a/common/src/main/java/common/init/BlockRegistry.java b/common/src/main/java/common/init/BlockRegistry.java index 68778463..e608d0f8 100755 --- a/common/src/main/java/common/init/BlockRegistry.java +++ b/common/src/main/java/common/init/BlockRegistry.java @@ -132,13 +132,13 @@ import common.item.CheatTab; import common.log.Log; import common.model.TextureAnimation; import common.util.ObjectIntIdentityMap; -import common.util.RegistryNamespacedDefaultedByKey; +import common.util.DefaultedMapping; import common.util.Util; import common.world.State; public abstract class BlockRegistry { private static final String AIR_ID = "air"; - public static final RegistryNamespacedDefaultedByKey REGISTRY = new RegistryNamespacedDefaultedByKey(AIR_ID); + public static final DefaultedMapping REGISTRY = new DefaultedMapping(AIR_ID); public static final ObjectIntIdentityMap STATEMAP = new ObjectIntIdentityMap(); private static int nextBlockId = 1; diff --git a/common/src/main/java/common/init/ItemRegistry.java b/common/src/main/java/common/init/ItemRegistry.java index d420f372..d16df7d8 100755 --- a/common/src/main/java/common/init/ItemRegistry.java +++ b/common/src/main/java/common/init/ItemRegistry.java @@ -119,12 +119,12 @@ import common.log.Log; import common.potion.Potion; import common.potion.PotionHelper; import common.util.Pair; -import common.util.RegistryNamespaced; +import common.util.Mapping; import common.util.Util; import common.world.Weather; public abstract class ItemRegistry { - public static final RegistryNamespaced REGISTRY = new RegistryNamespaced(); + public static final Mapping REGISTRY = new Mapping(); public static final Map BLOCKMAP = Maps.newHashMap(); public static final Set SPECIALIZED = Sets.newHashSet(); diff --git a/common/src/main/java/common/util/BaseMapping.java b/common/src/main/java/common/util/BaseMapping.java new file mode 100755 index 00000000..4b47f083 --- /dev/null +++ b/common/src/main/java/common/util/BaseMapping.java @@ -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 implements Iterable { + protected final Map mapping = this.createUnderlyingMap(); + + protected Map createUnderlyingMap() { + return Maps.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 getKeys() { + return Collections.unmodifiableSet(this.mapping.keySet()); + } + + public boolean containsKey(K key) { + return this.mapping.containsKey(key); + } + + public Iterator iterator() { + return this.mapping.values().iterator(); + } +} diff --git a/common/src/main/java/common/util/DefaultedMapping.java b/common/src/main/java/common/util/DefaultedMapping.java new file mode 100755 index 00000000..3e8b5c74 --- /dev/null +++ b/common/src/main/java/common/util/DefaultedMapping.java @@ -0,0 +1,40 @@ +package common.util; + +public class DefaultedMapping extends Mapping { + 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); + } +} diff --git a/common/src/main/java/common/util/IRegistry.java b/common/src/main/java/common/util/IRegistry.java deleted file mode 100755 index eaf299b5..00000000 --- a/common/src/main/java/common/util/IRegistry.java +++ /dev/null @@ -1,11 +0,0 @@ -package common.util; - -public interface IRegistry extends Iterable -{ - V getObject(K name); - - /** - * Register an object on this registry. - */ - void putObject(K key, V value); -} diff --git a/common/src/main/java/common/util/Mapping.java b/common/src/main/java/common/util/Mapping.java new file mode 100755 index 00000000..bf396491 --- /dev/null +++ b/common/src/main/java/common/util/Mapping.java @@ -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 extends BaseMapping implements IObjectIntIterable { + protected final ObjectIntIdentityMap idmap = new ObjectIntIdentityMap(); + protected final Map 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 createUnderlyingMap() { + return HashBiMap.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 iterator() { + return this.idmap.iterator(); + } +} diff --git a/common/src/main/java/common/util/RegistryDefaulted.java b/common/src/main/java/common/util/RegistryDefaulted.java deleted file mode 100755 index 8a5d13a6..00000000 --- a/common/src/main/java/common/util/RegistryDefaulted.java +++ /dev/null @@ -1,20 +0,0 @@ -package common.util; - -public class RegistryDefaulted extends RegistrySimple -{ - /** - * 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); - } -} diff --git a/common/src/main/java/common/util/RegistryNamespaced.java b/common/src/main/java/common/util/RegistryNamespaced.java deleted file mode 100755 index e6e40d74..00000000 --- a/common/src/main/java/common/util/RegistryNamespaced.java +++ /dev/null @@ -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 extends RegistrySimple implements IObjectIntIterable -{ - protected final ObjectIntIdentityMap underlyingIntegerMap = new ObjectIntIdentityMap(); - protected final Map 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 createUnderlyingMap() - { - return HashBiMap.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 iterator() - { - return this.underlyingIntegerMap.iterator(); - } -} diff --git a/common/src/main/java/common/util/RegistryNamespacedDefaultedByKey.java b/common/src/main/java/common/util/RegistryNamespacedDefaultedByKey.java deleted file mode 100755 index cd413606..00000000 --- a/common/src/main/java/common/util/RegistryNamespacedDefaultedByKey.java +++ /dev/null @@ -1,67 +0,0 @@ -package common.util; - -public class RegistryNamespacedDefaultedByKey extends RegistryNamespaced -{ - /** 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); - } -} diff --git a/common/src/main/java/common/util/RegistrySimple.java b/common/src/main/java/common/util/RegistrySimple.java deleted file mode 100755 index 8e759279..00000000 --- a/common/src/main/java/common/util/RegistrySimple.java +++ /dev/null @@ -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 implements IRegistry -{ - protected final Map registryObjects = this.createUnderlyingMap(); - - protected Map createUnderlyingMap() - { - return Maps.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 getKeys() - { - return Collections.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 iterator() - { - return this.registryObjects.values().iterator(); - } -}