mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-17 19:15:32 +00:00
First pass of registry rewrite.
Tests do not run yet. Things of note: Removed the idea of substitutions, just register multiple items with the same name they will override each other like a normal map. Decoupled Forge registries from vanilla classes. They now use bouncer classes. MODDERS SHOULD NEVER USE THESE Introduced more stringent registry locking. As it sits things are only allowed to be registered during the RegistryEvent.Registry phases! This is to force modders to split up their registrations, and pave the way for calling these functions multiple times during a single MC lifecycle. ObjectHolder, Missing Mappings, etc... *should* now work for all registry types, nothing should be special cased to Blocks/Items anymore. Added optional generic dummy factory to registries, to allow registry creators the ability to dummy more then just Blocks. Re-worked the ItemStack transformer and applied it to ItemBlock and ItemBlockSpecial. Allowing us to cleanup reflective hacks in Block callbacks. Registry onAdd callback is now only fired on the ACTIVE registry, fixing any issues of temp registries overriding active objects.
This commit is contained in:
1 parent
423f9fc04b
commit
a26d89c876
110 files changed
+3275
-4110
No files matched your search
@@ -0,0 +1,122 @@
|
||||
package net.minecraftforge.registries;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.apache.commons.lang3.Validate;
|
||||
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.registry.RegistryNamespaced;
|
||||
import net.minecraftforge.fml.common.FMLLog;
|
||||
|
||||
class NamespacedWrapper<V extends IForgeRegistryEntry<V>> extends RegistryNamespaced<ResourceLocation, V> implements ILockableRegistry
|
||||
{
|
||||
private boolean locked = false;
|
||||
private ForgeRegistry<V> delegate;
|
||||
|
||||
public NamespacedWrapper(ForgeRegistry<V> owner)
|
||||
{
|
||||
this.delegate = owner;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(int id, ResourceLocation key, V value)
|
||||
{
|
||||
if (locked)
|
||||
throw new IllegalStateException("Can not register to a locked registry. Modder should use Forge Register methods.");
|
||||
|
||||
Validate.notNull(value);
|
||||
|
||||
if (value.getRegistryName() == null)
|
||||
value.setRegistryName(key);
|
||||
|
||||
int realId = this.delegate.add(id, value);
|
||||
if (realId != id && id != -1)
|
||||
FMLLog.warning("Registered object did not get ID it asked for. Name: {} Type: {} Expected: {} Got: {}", key, value.getRegistryType().getName(), id, realId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putObject(ResourceLocation key, V value)
|
||||
{
|
||||
register(-1, key, value);
|
||||
}
|
||||
|
||||
|
||||
// Reading Functions
|
||||
@Override
|
||||
@Nullable
|
||||
public V getObject(@Nullable ResourceLocation name)
|
||||
{
|
||||
return this.delegate.getValue(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public ResourceLocation getNameForObject(V value)
|
||||
{
|
||||
return this.delegate.getKey(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey(ResourceLocation key)
|
||||
{
|
||||
return this.delegate.containsKey(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIDForObject(@Nullable V value)
|
||||
{
|
||||
return this.delegate.getID(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public V getObjectById(int id)
|
||||
{
|
||||
return this.delegate.getValue(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<V> iterator()
|
||||
{
|
||||
return this.delegate.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<ResourceLocation> getKeys()
|
||||
{
|
||||
return this.delegate.getKeys();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public V getRandomObject(Random random)
|
||||
{
|
||||
List<V> lst = this.delegate.getValues();
|
||||
if (lst.isEmpty())
|
||||
return null;
|
||||
return lst.get(random.nextInt(lst.size()));
|
||||
}
|
||||
|
||||
//internal
|
||||
@Override //We override all public methods, this should be fine as null.
|
||||
protected Map<ResourceLocation, V> createUnderlyingMap(){ return null; }
|
||||
|
||||
@Override
|
||||
public void lock(){ this.locked = true; }
|
||||
|
||||
public static class Factory<V extends IForgeRegistryEntry<V>> implements IForgeRegistry.CreateCallback<V>
|
||||
{
|
||||
public static final ResourceLocation ID = new ResourceLocation("forge", "registry_defaulted_wrapper");
|
||||
@Override
|
||||
public void onCreate(IForgeRegistryInternal<V> owner, RegistryManager stage)
|
||||
{
|
||||
owner.setSlaveMap(ID, new NamespacedWrapper<V>((ForgeRegistry<V>)owner));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user