base mod created
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package net.minecraft.util.registry;
|
||||
|
||||
import java.util.Set;
|
||||
import javax.annotation.Nullable;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
|
||||
public interface IRegistry<K, V> extends Iterable<V>
|
||||
{
|
||||
@Nullable
|
||||
@SideOnly(Side.CLIENT)
|
||||
V getObject(K name);
|
||||
|
||||
/**
|
||||
* Register an object on this registry.
|
||||
*/
|
||||
void putObject(K key, V value);
|
||||
|
||||
/**
|
||||
* Gets all the keys recognized by this registry.
|
||||
*/
|
||||
@SideOnly(Side.CLIENT)
|
||||
Set<K> getKeys();
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package net.minecraft.util.registry;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public V getObject(@Nullable K name)
|
||||
{
|
||||
V v = (V)super.getObject(name);
|
||||
return (V)(v == null ? this.defaultObject : v);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package net.minecraft.util.registry;
|
||||
|
||||
import com.google.common.collect.BiMap;
|
||||
import com.google.common.collect.HashBiMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import javax.annotation.Nullable;
|
||||
import net.minecraft.util.IObjectIntIterable;
|
||||
import net.minecraft.util.IntIdentityHashBiMap;
|
||||
|
||||
public class RegistryNamespaced<K, V> extends RegistrySimple<K, V> implements IObjectIntIterable<V>
|
||||
{
|
||||
/** The backing store that maps Integers to objects. */
|
||||
protected final IntIdentityHashBiMap<V> underlyingIntegerMap = new IntIdentityHashBiMap<V>(256);
|
||||
/** A BiMap of objects (key) to their names (value). */
|
||||
protected final Map<V, K> inverseObjectRegistry;
|
||||
|
||||
public RegistryNamespaced()
|
||||
{
|
||||
this.inverseObjectRegistry = ((BiMap)this.registryObjects).inverse();
|
||||
}
|
||||
|
||||
public void register(int id, K key, V value)
|
||||
{
|
||||
this.underlyingIntegerMap.put(value, id);
|
||||
this.putObject(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the Map we will use to map keys to their registered values.
|
||||
*/
|
||||
protected Map<K, V> createUnderlyingMap()
|
||||
{
|
||||
return HashBiMap.<K, V>create();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public V getObject(@Nullable K name)
|
||||
{
|
||||
return (V)super.getObject(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name we use to identify the given object.
|
||||
*/
|
||||
@Nullable
|
||||
public K getNameForObject(V value)
|
||||
{
|
||||
return 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(@Nullable V value)
|
||||
{
|
||||
return this.underlyingIntegerMap.getId(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the object identified by the given ID.
|
||||
*/
|
||||
@Nullable
|
||||
public V getObjectById(int id)
|
||||
{
|
||||
return this.underlyingIntegerMap.get(id);
|
||||
}
|
||||
|
||||
public Iterator<V> iterator()
|
||||
{
|
||||
return this.underlyingIntegerMap.iterator();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package net.minecraft.util.registry;
|
||||
|
||||
import java.util.Random;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
super.register(id, key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* validates that this registry's key is non-null
|
||||
*/
|
||||
public void validateKey()
|
||||
{
|
||||
Validate.notNull(this.defaultValue, "Missing default of DefaultedMappedRegistry: " + this.defaultValueKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the integer ID we use to identify the given object.
|
||||
*/
|
||||
public int getIDForObject(V value)
|
||||
{
|
||||
int i = super.getIDForObject(value);
|
||||
return i == -1 ? super.getIDForObject(this.defaultValue) : i;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name we use to identify the given object.
|
||||
*/
|
||||
@Nonnull
|
||||
public K getNameForObject(V value)
|
||||
{
|
||||
K k = (K)super.getNameForObject(value);
|
||||
return (K)(k == null ? this.defaultValueKey : k);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public V getObject(@Nullable K name)
|
||||
{
|
||||
V v = (V)super.getObject(name);
|
||||
return (V)(v == null ? this.defaultValue : v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the object identified by the given ID.
|
||||
*/
|
||||
@Nonnull
|
||||
public V getObjectById(int id)
|
||||
{
|
||||
V v = (V)super.getObjectById(id);
|
||||
return (V)(v == null ? this.defaultValue : v);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public V getRandomObject(Random random)
|
||||
{
|
||||
V v = (V)super.getRandomObject(random);
|
||||
return (V)(v == null ? this.defaultValue : v);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package net.minecraft.util.registry;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
import javax.annotation.Nullable;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class RegistrySimple<K, V> implements IRegistry<K, V>
|
||||
{
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
/** Objects registered on this registry. */
|
||||
protected final Map<K, V> registryObjects = this.createUnderlyingMap();
|
||||
private Object[] values;
|
||||
|
||||
/**
|
||||
* Creates the Map we will use to map keys to their registered values.
|
||||
*/
|
||||
protected Map<K, V> createUnderlyingMap()
|
||||
{
|
||||
return Maps.<K, V>newHashMap();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public V getObject(@Nullable K name)
|
||||
{
|
||||
return this.registryObjects.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an object on this registry.
|
||||
*/
|
||||
public void putObject(K key, V value)
|
||||
{
|
||||
Validate.notNull(key);
|
||||
Validate.notNull(value);
|
||||
this.values = null;
|
||||
|
||||
if (this.registryObjects.containsKey(key))
|
||||
{
|
||||
LOGGER.debug("Adding duplicate key '{}' to registry", key);
|
||||
}
|
||||
|
||||
this.registryObjects.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all the keys recognized by this registry.
|
||||
*/
|
||||
public Set<K> getKeys()
|
||||
{
|
||||
return Collections.<K>unmodifiableSet(this.registryObjects.keySet());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public V getRandomObject(Random random)
|
||||
{
|
||||
if (this.values == null)
|
||||
{
|
||||
Collection<?> collection = this.registryObjects.values();
|
||||
|
||||
if (collection.isEmpty())
|
||||
{
|
||||
return (V)null;
|
||||
}
|
||||
|
||||
this.values = collection.toArray(new Object[collection.size()]);
|
||||
}
|
||||
|
||||
return (V)this.values[random.nextInt(this.values.length)];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// Auto generated package-info by MCP
|
||||
@ParametersAreNonnullByDefault
|
||||
@MethodsReturnNonnullByDefault
|
||||
package net.minecraft.util.registry;
|
||||
|
||||
import mcp.MethodsReturnNonnullByDefault;
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
Reference in New Issue
Block a user