clean up mapping classes

This commit is contained in:
Sen 2025-06-22 18:34:43 +02:00
parent e85166666f
commit 7c495f3d1c
Signed by: sen
GPG key ID: 3AC50A6F47D1B722
12 changed files with 145 additions and 247 deletions

View file

@ -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<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();
private static int nextBlockId = 1;

View file

@ -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<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 Set<Block> SPECIALIZED = Sets.<Block>newHashSet();

View 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();
}
}

View 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);
}
}

View file

@ -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);
}

View 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();
}
}

View file

@ -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);
}
}

View file

@ -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();
}
}

View file

@ -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);
}
}

View file

@ -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();
}
}