clean up mapping classes

This commit is contained in:
Sen 2025-06-22 19:08:24 +02:00
parent 7ec9c174a1
commit de8d2477a3
Signed by: sen
GPG key ID: 3AC50A6F47D1B722
4 changed files with 38 additions and 46 deletions

View file

@ -131,7 +131,7 @@ import common.color.DyeColor;
import common.item.CheatTab;
import common.log.Log;
import common.model.TextureAnimation;
import common.util.ObjectIntIdentityMap;
import common.util.IdMap;
import common.util.Mapping;
import common.util.Util;
import common.world.State;
@ -139,7 +139,7 @@ import common.world.State;
public abstract class BlockRegistry {
private static final String AIR_ID = "air";
public static final Mapping<Block> REGISTRY = new Mapping(AIR_ID);
public static final ObjectIntIdentityMap<State> STATEMAP = new ObjectIntIdentityMap();
public static final IdMap<State> STATEMAP = new IdMap();
private static int nextBlockId = 1;

View file

@ -0,0 +1,34 @@
package common.util;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import common.collect.Iterators;
import common.collect.Lists;
public class IdMap<T> implements Iterable<T> {
private final IdentityHashMap<T, Integer> mapping = new IdentityHashMap(512);
private final List<T> list = Lists.<T>newArrayList();
public void put(T key, int value) {
this.mapping.put(key, value);
while(this.list.size() <= value) {
this.list.add(null);
}
this.list.set(value, key);
}
public int get(T key) {
Integer i = this.mapping.get(key);
return i == null ? -1 : i.intValue();
}
public final T getByValue(int value) {
return value >= 0 && value < this.list.size() ? this.list.get(value) : null;
}
public Iterator<T> iterator() {
return Iterators.filter(this.list.iterator(), Predicates.notNull());
}
}

View file

@ -10,7 +10,7 @@ import common.collect.HashBiMap;
public class Mapping<V> implements Iterable<V> {
private final Map<String, V> mapping = HashBiMap.<String, V>create();
private final ObjectIntIdentityMap<V> idMap = new ObjectIntIdentityMap();
private final IdMap<V> idMap = new IdMap<V>();
private final Map<V, String> nameMap;
private final String defaultKey;
@ -46,7 +46,7 @@ public class Mapping<V> implements Iterable<V> {
public void finish() {
if(this.defaultKey != null && this.defaultValue == null)
throw new NullPointerException("Standard-Wert zu Standard-Schlüssel ist null");
throw new NullPointerException("Wert zu Standard-Schlüssel " + this.defaultKey + " ist nicht vorhanden");
this.registered = true;
}

View file

@ -1,42 +0,0 @@
package common.util;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import common.collect.Iterators;
import common.collect.Lists;
public class ObjectIntIdentityMap<T> implements Iterable<T>
{
private final IdentityHashMap<T, Integer> identityMap = new IdentityHashMap(512);
private final List<T> objectList = Lists.<T>newArrayList();
public void put(T key, int value)
{
this.identityMap.put(key, value);
while (this.objectList.size() <= value)
{
this.objectList.add(null);
}
this.objectList.set(value, key);
}
public int get(T key)
{
Integer integer = (Integer)this.identityMap.get(key);
return integer == null ? -1 : integer.intValue();
}
public final T getByValue(int value)
{
return (T)(value >= 0 && value < this.objectList.size() ? this.objectList.get(value) : null);
}
public Iterator<T> iterator()
{
return Iterators.filter(this.objectList.iterator(), Predicates.notNull());
}
}