clean up mapping classes
This commit is contained in:
parent
7c495f3d1c
commit
0ca276b561
7 changed files with 58 additions and 103 deletions
|
@ -21,7 +21,6 @@ import common.item.Item;
|
||||||
import common.model.ModelRotation;
|
import common.model.ModelRotation;
|
||||||
import common.util.Facing;
|
import common.util.Facing;
|
||||||
import common.util.Pair;
|
import common.util.Pair;
|
||||||
import common.util.BaseMapping;
|
|
||||||
import common.world.State;
|
import common.world.State;
|
||||||
|
|
||||||
public abstract class ModelBakery
|
public abstract class ModelBakery
|
||||||
|
@ -46,14 +45,14 @@ public abstract class ModelBakery
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BaseMapping<String, IBakedModel> setupModelRegistry(TextureMap textureMap,
|
public static Map<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();
|
||||||
BaseMapping<String, IBakedModel> bakedRegistry = new BaseMapping();
|
Map<String, IBakedModel> bakedRegistry = Maps.newHashMap();
|
||||||
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);
|
||||||
|
@ -172,7 +171,7 @@ public abstract class ModelBakery
|
||||||
|
|
||||||
if (modelblock != null)
|
if (modelblock != null)
|
||||||
{
|
{
|
||||||
bakedRegistry.putObject(modelresourcelocation, bakeModel(sprites, faceBakery, textureMap.getMissingSprite(),
|
bakedRegistry.put(modelresourcelocation, bakeModel(sprites, faceBakery, textureMap.getMissingSprite(),
|
||||||
modelblock, modelblock.getRotation(), modelblock.isUvLocked()));
|
modelblock, modelblock.getRotation(), modelblock.isUvLocked()));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -189,11 +188,11 @@ public abstract class ModelBakery
|
||||||
{
|
{
|
||||||
if (modelblock1.getParent() == MODEL_ENTITY)
|
if (modelblock1.getParent() == MODEL_ENTITY)
|
||||||
{
|
{
|
||||||
bakedRegistry.putObject(entry, new BuiltInModel(modelblock1.getTransform()));
|
bakedRegistry.put(entry, new BuiltInModel(modelblock1.getTransform()));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
bakedRegistry.putObject(entry, bakeModel(sprites, faceBakery, textureMap.getMissingSprite(),
|
bakedRegistry.put(entry, bakeModel(sprites, faceBakery, textureMap.getMissingSprite(),
|
||||||
modelblock1, ModelRotation.X0_Y0, false));
|
modelblock1, ModelRotation.X0_Y0, false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,6 @@ 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.BaseMapping;
|
|
||||||
import common.world.State;
|
import common.world.State;
|
||||||
|
|
||||||
public class ModelManager
|
public class ModelManager
|
||||||
|
@ -21,7 +20,7 @@ public class ModelManager
|
||||||
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 Map<String, IBakedModel> modelRegistry;
|
||||||
private IBakedModel defaultModel;
|
private IBakedModel defaultModel;
|
||||||
|
|
||||||
public ModelManager(TextureMap textures)
|
public ModelManager(TextureMap textures)
|
||||||
|
@ -43,7 +42,7 @@ public class ModelManager
|
||||||
public void onReload()
|
public void onReload()
|
||||||
{
|
{
|
||||||
this.modelRegistry = ModelBakery.setupModelRegistry(this.texMap, this.getMap());
|
this.modelRegistry = ModelBakery.setupModelRegistry(this.texMap, this.getMap());
|
||||||
this.defaultModel = this.modelRegistry.getObject(ModelBakery.MISSING);
|
this.defaultModel = this.modelRegistry.get(ModelBakery.MISSING);
|
||||||
this.reloadModels();
|
this.reloadModels();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +54,7 @@ public class ModelManager
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
IBakedModel ibakedmodel = this.modelRegistry.getObject(modelLocation);
|
IBakedModel ibakedmodel = this.modelRegistry.get(modelLocation);
|
||||||
return ibakedmodel == null ? this.defaultModel : ibakedmodel;
|
return ibakedmodel == null ? this.defaultModel : ibakedmodel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.DefaultedMapping;
|
import common.util.Mapping;
|
||||||
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 DefaultedMapping<String, Block> REGISTRY = new DefaultedMapping(AIR_ID);
|
public static final Mapping<String, Block> REGISTRY = new Mapping(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;
|
||||||
|
@ -204,7 +204,7 @@ public abstract class BlockRegistry {
|
||||||
static void register() {
|
static void register() {
|
||||||
REGISTRY.register(0, BlockRegistry.AIR_ID, (new BlockAir()).setDisplay("Luft"));
|
REGISTRY.register(0, BlockRegistry.AIR_ID, (new BlockAir()).setDisplay("Luft"));
|
||||||
registerBlocks();
|
registerBlocks();
|
||||||
REGISTRY.validateKey();
|
REGISTRY.finish();
|
||||||
|
|
||||||
for(Block block : REGISTRY) {
|
for(Block block : REGISTRY) {
|
||||||
if(block != Blocks.air
|
if(block != Blocks.air
|
||||||
|
|
|
@ -551,6 +551,8 @@ public abstract class ItemRegistry {
|
||||||
// Log.info("Block " + BlockRegistry.getNameFromBlock(block) + " hat kein Item");
|
// Log.info("Block " + BlockRegistry.getNameFromBlock(block) + " hat kein Item");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
REGISTRY.finish();
|
||||||
|
|
||||||
Log.SYSTEM.debug("%d Gegenstände registriert", nextItemId);
|
Log.SYSTEM.debug("%d Gegenstände registriert", nextItemId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,40 +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 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();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,40 +0,0 @@
|
||||||
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,36 +1,66 @@
|
||||||
package common.util;
|
package common.util;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import common.collect.BiMap;
|
import common.collect.BiMap;
|
||||||
import common.collect.HashBiMap;
|
import common.collect.HashBiMap;
|
||||||
|
|
||||||
public class Mapping<K, V> extends BaseMapping<K, V> implements IObjectIntIterable<V> {
|
public class Mapping<K, V> implements Iterable<V>, IObjectIntIterable<V> {
|
||||||
protected final ObjectIntIdentityMap<V> idmap = new ObjectIntIdentityMap();
|
private final Map<K, V> mapping = HashBiMap.<K, V>create();
|
||||||
protected final Map<V, K> namemap;
|
private final ObjectIntIdentityMap<V> idmap = new ObjectIntIdentityMap();
|
||||||
|
private final Map<V, K> namemap;
|
||||||
|
private final K defaultKey;
|
||||||
|
|
||||||
|
private V defaultValue;
|
||||||
|
private boolean registered;
|
||||||
|
|
||||||
|
public Mapping(K def) {
|
||||||
|
this.namemap = ((BiMap)this.mapping).inverse();
|
||||||
|
this.defaultKey = def;
|
||||||
|
}
|
||||||
|
|
||||||
public Mapping() {
|
public Mapping() {
|
||||||
this.namemap = ((BiMap)this.mapping).inverse();
|
this(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void register(int id, K key, V value) {
|
public void register(int id, K key, V value) {
|
||||||
|
if(this.registered)
|
||||||
|
throw new IllegalStateException("Es können keine neuen Werte registriert werden");
|
||||||
|
if(key == null)
|
||||||
|
throw new NullPointerException("Schlüssel ist null");
|
||||||
|
if(value == null)
|
||||||
|
throw new NullPointerException("Wert ist null");
|
||||||
if(this.mapping.containsKey(key))
|
if(this.mapping.containsKey(key))
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"Schlüssel " + String.valueOf(key) + " ist bereits mit ID " + this.idmap.get(this.mapping.get(key)) + " registriert");
|
"Schlüssel " + String.valueOf(key) + " ist bereits mit ID " + this.idmap.get(this.mapping.get(key)) + " registriert");
|
||||||
if(this.idmap.getByValue(id) != null)
|
if(this.idmap.getByValue(id) != null)
|
||||||
throw new IllegalArgumentException("ID " + id + " ist bereits mit Name " + this.namemap.get(this.idmap.getByValue(id)) + " registriert");
|
throw new IllegalArgumentException("ID " + id + " ist bereits mit Name " + this.namemap.get(this.idmap.getByValue(id)) + " registriert");
|
||||||
|
|
||||||
this.idmap.put(value, id);
|
this.idmap.put(value, id);
|
||||||
this.putObject(key, value);
|
this.mapping.put(key, value);
|
||||||
|
if(key.equals(this.defaultKey))
|
||||||
|
this.defaultValue = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Map<K, V> createUnderlyingMap() {
|
public void finish() {
|
||||||
return HashBiMap.<K, V>create();
|
if(this.defaultKey != null && this.defaultValue == null)
|
||||||
|
throw new NullPointerException("Standard-Wert zu Standard-Schlüssel ist null");
|
||||||
|
this.registered = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<K> getKeys() {
|
||||||
|
return Collections.<K>unmodifiableSet(this.mapping.keySet());
|
||||||
}
|
}
|
||||||
|
|
||||||
public V getObject(K name) {
|
public V getObject(K name) {
|
||||||
return super.getObject(name);
|
V v = this.mapping.get(name);
|
||||||
|
return v == null ? this.defaultValue : v;
|
||||||
|
}
|
||||||
|
|
||||||
|
public V getObjectExact(K name) {
|
||||||
|
return this.mapping.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public K getNameForObject(V value) {
|
public K getNameForObject(V value) {
|
||||||
|
@ -38,7 +68,7 @@ public class Mapping<K, V> extends BaseMapping<K, V> implements IObjectIntIterab
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean containsKey(K key) {
|
public boolean containsKey(K key) {
|
||||||
return super.containsKey(key);
|
return this.mapping.containsKey(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getIDForObject(V value) {
|
public int getIDForObject(V value) {
|
||||||
|
@ -46,6 +76,11 @@ public class Mapping<K, V> extends BaseMapping<K, V> implements IObjectIntIterab
|
||||||
}
|
}
|
||||||
|
|
||||||
public V getObjectById(int id) {
|
public V getObjectById(int id) {
|
||||||
|
V v = this.idmap.getByValue(id);
|
||||||
|
return v == null ? this.defaultValue : v;
|
||||||
|
}
|
||||||
|
|
||||||
|
public V getObjectExact(int id) {
|
||||||
return this.idmap.getByValue(id);
|
return this.idmap.getByValue(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue