clean up mapping classes
This commit is contained in:
parent
0ca276b561
commit
7ec9c174a1
7 changed files with 50 additions and 55 deletions
|
@ -138,21 +138,21 @@ 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 Mapping<String, Block> REGISTRY = new Mapping(AIR_ID);
|
public static final Mapping<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;
|
||||||
|
|
||||||
public static int getIdFromBlock(Block block) {
|
public static int getIdFromBlock(Block block) {
|
||||||
return REGISTRY.getIDForObject(block);
|
return REGISTRY.getId(block);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getNameFromBlock(Block block) {
|
public static String getNameFromBlock(Block block) {
|
||||||
return REGISTRY.getNameForObject(block);
|
return REGISTRY.getName(block);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getNameFromFluid(BlockLiquid block) {
|
public static String getNameFromFluid(BlockLiquid block) {
|
||||||
return REGISTRY.getNameForObject(block instanceof BlockDynamicLiquid dy ? dy.getStaticBlock() : block);
|
return REGISTRY.getName(block instanceof BlockDynamicLiquid dy ? dy.getStaticBlock() : block);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getStateId(State state) {
|
public static int getStateId(State state) {
|
||||||
|
@ -161,7 +161,7 @@ public abstract class BlockRegistry {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Block getBlockById(int id) {
|
public static Block getBlockById(int id) {
|
||||||
return REGISTRY.getObjectById(id);
|
return REGISTRY.byId(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static State getStateById(int id) {
|
public static State getStateById(int id) {
|
||||||
|
@ -176,7 +176,7 @@ public abstract class BlockRegistry {
|
||||||
String[] tok = name.split(":");
|
String[] tok = name.split(":");
|
||||||
if(tok.length < 1 || tok.length > 2)
|
if(tok.length < 1 || tok.length > 2)
|
||||||
return def;
|
return def;
|
||||||
Block block = REGISTRY.getObjectExact(tok[0]);
|
Block block = REGISTRY.byNameExact(tok[0]);
|
||||||
if(block == null)
|
if(block == null)
|
||||||
return def;
|
return def;
|
||||||
byte data;
|
byte data;
|
||||||
|
@ -198,7 +198,7 @@ public abstract class BlockRegistry {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Block getRegisteredBlock(String name) {
|
public static Block getRegisteredBlock(String name) {
|
||||||
return REGISTRY.getObject(name);
|
return REGISTRY.byName(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void register() {
|
static void register() {
|
||||||
|
@ -216,7 +216,7 @@ public abstract class BlockRegistry {
|
||||||
|
|
||||||
for(Block block : REGISTRY) {
|
for(Block block : REGISTRY) {
|
||||||
for(State state : block.getValidStates()) {
|
for(State state : block.getValidStates()) {
|
||||||
STATEMAP.put(state, REGISTRY.getIDForObject(block) << 4 | block.getMetaFromState(state));
|
STATEMAP.put(state, REGISTRY.getId(block) << 4 | block.getMetaFromState(state));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -580,9 +580,9 @@ public abstract class Blocks {
|
||||||
public static final BlockOre zinc_ore = get("zinc_ore");
|
public static final BlockOre zinc_ore = get("zinc_ore");
|
||||||
|
|
||||||
private static <T extends Block> T get(String id) {
|
private static <T extends Block> T get(String id) {
|
||||||
if(!BlockRegistry.REGISTRY.containsKey(id))
|
if(!BlockRegistry.REGISTRY.has(id))
|
||||||
throw new RuntimeException("Block " + id + " existiert nicht");
|
throw new RuntimeException("Block " + id + " existiert nicht");
|
||||||
return (T)BlockRegistry.REGISTRY.getObject(id);
|
return (T)BlockRegistry.REGISTRY.byName(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
static {
|
static {
|
||||||
|
|
|
@ -124,22 +124,22 @@ import common.util.Util;
|
||||||
import common.world.Weather;
|
import common.world.Weather;
|
||||||
|
|
||||||
public abstract class ItemRegistry {
|
public abstract class ItemRegistry {
|
||||||
public static final Mapping<String, Item> REGISTRY = new Mapping();
|
public static final Mapping<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();
|
||||||
|
|
||||||
private static int nextItemId = 4096;
|
private static int nextItemId = 4096;
|
||||||
|
|
||||||
public static int getIdFromItem(Item item) {
|
public static int getIdFromItem(Item item) {
|
||||||
return item == null ? 0 : REGISTRY.getIDForObject(item);
|
return item == null ? 0 : REGISTRY.getId(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getNameFromItem(Item item) {
|
public static String getNameFromItem(Item item) {
|
||||||
return REGISTRY.getNameForObject(item);
|
return REGISTRY.getName(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Item getItemById(int id) {
|
public static Item getItemById(int id) {
|
||||||
return REGISTRY.getObjectById(id);
|
return REGISTRY.byId(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ItemBlock getItemFromBlock(Block block) {
|
public static ItemBlock getItemFromBlock(Block block) {
|
||||||
|
@ -147,13 +147,13 @@ public abstract class ItemRegistry {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Item getRegisteredItem(String name) {
|
public static Item getRegisteredItem(String name) {
|
||||||
return REGISTRY.getObject(name);
|
return REGISTRY.byName(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ItemStack getFromIdName(String name, ItemStack def) {
|
public static ItemStack getFromIdName(String name, ItemStack def) {
|
||||||
if(name == null)
|
if(name == null)
|
||||||
return def;
|
return def;
|
||||||
Item item = REGISTRY.getObject(name);
|
Item item = REGISTRY.byName(name);
|
||||||
if(item == null)
|
if(item == null)
|
||||||
return def;
|
return def;
|
||||||
return new ItemStack(item);
|
return new ItemStack(item);
|
||||||
|
|
|
@ -897,9 +897,9 @@ public abstract class Items {
|
||||||
public static final ItemFishFood pufferfish = get("pufferfish");
|
public static final ItemFishFood pufferfish = get("pufferfish");
|
||||||
|
|
||||||
private static <T extends Item> T get(String id) {
|
private static <T extends Item> T get(String id) {
|
||||||
if(!ItemRegistry.REGISTRY.containsKey(id))
|
if(!ItemRegistry.REGISTRY.has(id))
|
||||||
throw new RuntimeException("Gegenstand " + id + " existiert nicht");
|
throw new RuntimeException("Gegenstand " + id + " existiert nicht");
|
||||||
return (T)ItemRegistry.REGISTRY.getObject(id);
|
return (T)ItemRegistry.REGISTRY.byName(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
static {
|
static {
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
package common.util;
|
|
||||||
|
|
||||||
public interface IObjectIntIterable<T> extends Iterable<T>
|
|
||||||
{
|
|
||||||
}
|
|
|
@ -8,17 +8,17 @@ 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> implements Iterable<V>, IObjectIntIterable<V> {
|
public class Mapping<V> implements Iterable<V> {
|
||||||
private final Map<K, V> mapping = HashBiMap.<K, V>create();
|
private final Map<String, V> mapping = HashBiMap.<String, V>create();
|
||||||
private final ObjectIntIdentityMap<V> idmap = new ObjectIntIdentityMap();
|
private final ObjectIntIdentityMap<V> idMap = new ObjectIntIdentityMap();
|
||||||
private final Map<V, K> namemap;
|
private final Map<V, String> nameMap;
|
||||||
private final K defaultKey;
|
private final String defaultKey;
|
||||||
|
|
||||||
private V defaultValue;
|
private V defaultValue;
|
||||||
private boolean registered;
|
private boolean registered;
|
||||||
|
|
||||||
public Mapping(K def) {
|
public Mapping(String def) {
|
||||||
this.namemap = ((BiMap)this.mapping).inverse();
|
this.nameMap = ((BiMap)this.mapping).inverse();
|
||||||
this.defaultKey = def;
|
this.defaultKey = def;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ public class Mapping<K, V> implements Iterable<V>, IObjectIntIterable<V> {
|
||||||
this(null);
|
this(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void register(int id, K key, V value) {
|
public void register(int id, String key, V value) {
|
||||||
if(this.registered)
|
if(this.registered)
|
||||||
throw new IllegalStateException("Es können keine neuen Werte registriert werden");
|
throw new IllegalStateException("Es können keine neuen Werte registriert werden");
|
||||||
if(key == null)
|
if(key == null)
|
||||||
|
@ -35,10 +35,10 @@ public class Mapping<K, V> implements Iterable<V>, IObjectIntIterable<V> {
|
||||||
throw new NullPointerException("Wert ist 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 " + 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.mapping.put(key, value);
|
this.mapping.put(key, value);
|
||||||
if(key.equals(this.defaultKey))
|
if(key.equals(this.defaultKey))
|
||||||
this.defaultValue = value;
|
this.defaultValue = value;
|
||||||
|
@ -50,41 +50,41 @@ public class Mapping<K, V> implements Iterable<V>, IObjectIntIterable<V> {
|
||||||
this.registered = true;
|
this.registered = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<K> getKeys() {
|
public Set<String> getKeys() {
|
||||||
return Collections.<K>unmodifiableSet(this.mapping.keySet());
|
return Collections.<String>unmodifiableSet(this.mapping.keySet());
|
||||||
}
|
}
|
||||||
|
|
||||||
public V getObject(K name) {
|
public V byName(String name) {
|
||||||
V v = this.mapping.get(name);
|
V v = this.mapping.get(name);
|
||||||
return v == null ? this.defaultValue : v;
|
return v == null ? this.defaultValue : v;
|
||||||
}
|
}
|
||||||
|
|
||||||
public V getObjectExact(K name) {
|
public V byNameExact(String name) {
|
||||||
return this.mapping.get(name);
|
return this.mapping.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public K getNameForObject(V value) {
|
public String getName(V value) {
|
||||||
return this.namemap.get(value);
|
return this.nameMap.get(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean containsKey(K key) {
|
public V byId(int id) {
|
||||||
return this.mapping.containsKey(key);
|
V v = this.idMap.getByValue(id);
|
||||||
}
|
|
||||||
|
|
||||||
public int getIDForObject(V value) {
|
|
||||||
return this.idmap.get(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public V getObjectById(int id) {
|
|
||||||
V v = this.idmap.getByValue(id);
|
|
||||||
return v == null ? this.defaultValue : v;
|
return v == null ? this.defaultValue : v;
|
||||||
}
|
}
|
||||||
|
|
||||||
public V getObjectExact(int id) {
|
public V byIdExact(int id) {
|
||||||
return this.idmap.getByValue(id);
|
return this.idMap.getByValue(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId(V value) {
|
||||||
|
return this.idMap.get(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean has(String key) {
|
||||||
|
return this.mapping.containsKey(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Iterator<V> iterator() {
|
public Iterator<V> iterator() {
|
||||||
return this.idmap.iterator();
|
return this.idMap.iterator();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import java.util.List;
|
||||||
import common.collect.Iterators;
|
import common.collect.Iterators;
|
||||||
import common.collect.Lists;
|
import common.collect.Lists;
|
||||||
|
|
||||||
public class ObjectIntIdentityMap<T> implements IObjectIntIterable<T>
|
public class ObjectIntIdentityMap<T> implements Iterable<T>
|
||||||
{
|
{
|
||||||
private final IdentityHashMap<T, Integer> identityMap = new IdentityHashMap(512);
|
private final IdentityHashMap<T, Integer> identityMap = new IdentityHashMap(512);
|
||||||
private final List<T> objectList = Lists.<T>newArrayList();
|
private final List<T> objectList = Lists.<T>newArrayList();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue