mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-13 16:54:22 +00:00
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.
183 lines
4.9 KiB
Java
183 lines
4.9 KiB
Java
/*
|
|
* Minecraft Forge
|
|
* Copyright (c) 2016.
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation version 2.1
|
|
* of the License.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
|
|
package net.minecraftforge.registries;
|
|
|
|
import java.util.List;
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
import net.minecraft.util.ResourceLocation;
|
|
import net.minecraftforge.registries.IForgeRegistry.*;
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
public class RegistryBuilder<T extends IForgeRegistryEntry<T>>
|
|
{
|
|
private ResourceLocation registryName;
|
|
private Class<T> registryType;
|
|
private ResourceLocation optionalDefaultKey;
|
|
private int minId = 0;
|
|
private int maxId = Integer.MAX_VALUE;
|
|
private List<AddCallback<T>> addCallback = Lists.newArrayList();
|
|
private List<ClearCallback<T>> clearCallback = Lists.newArrayList();
|
|
private List<CreateCallback<T>> createCallback = Lists.newArrayList();
|
|
private boolean saveToDisc = true;
|
|
private boolean allowOverrides = true;
|
|
private boolean allowModifications = false;
|
|
private DummyFactory<T> dummyFactory;
|
|
|
|
public RegistryBuilder<T> setName(ResourceLocation name)
|
|
{
|
|
this.registryName = name;
|
|
return this;
|
|
}
|
|
|
|
public RegistryBuilder<T> setType(Class<T> type)
|
|
{
|
|
this.registryType = type;
|
|
return this;
|
|
}
|
|
|
|
public RegistryBuilder<T> setIDRange(int min, int max)
|
|
{
|
|
this.minId = min;
|
|
this.maxId = max;
|
|
return this;
|
|
}
|
|
|
|
public RegistryBuilder<T> setMaxID(int max)
|
|
{
|
|
return this.setIDRange(0, max);
|
|
}
|
|
|
|
public RegistryBuilder<T> setDefaultKey(ResourceLocation key)
|
|
{
|
|
this.optionalDefaultKey = key;
|
|
return this;
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
public RegistryBuilder<T> addCallback(Object inst)
|
|
{
|
|
if (inst instanceof AddCallback)
|
|
this.add((AddCallback<T>)inst);
|
|
if (inst instanceof ClearCallback)
|
|
this.add((ClearCallback<T>)inst);
|
|
if (inst instanceof CreateCallback)
|
|
this.add((CreateCallback<T>)inst);
|
|
if (inst instanceof DummyFactory)
|
|
this.set((DummyFactory<T>)inst);
|
|
return this;
|
|
}
|
|
|
|
public RegistryBuilder<T> add(AddCallback<T> add)
|
|
{
|
|
this.addCallback.add(add);
|
|
return this;
|
|
}
|
|
|
|
public RegistryBuilder<T> add(ClearCallback<T> clear)
|
|
{
|
|
this.clearCallback.add(clear);
|
|
return this;
|
|
}
|
|
|
|
public RegistryBuilder<T> add(CreateCallback<T> create)
|
|
{
|
|
this.createCallback.add(create);
|
|
return this;
|
|
}
|
|
|
|
public RegistryBuilder<T> set(DummyFactory<T> factory)
|
|
{
|
|
this.dummyFactory = factory;
|
|
return this;
|
|
}
|
|
|
|
public RegistryBuilder<T> disableSaving()
|
|
{
|
|
this.saveToDisc = false;
|
|
return this;
|
|
}
|
|
|
|
public RegistryBuilder<T> disableOverrides()
|
|
{
|
|
this.allowOverrides = false;
|
|
return this;
|
|
}
|
|
|
|
public RegistryBuilder<T> allowModification()
|
|
{
|
|
this.allowModifications = true;
|
|
return this;
|
|
}
|
|
|
|
public IForgeRegistry<T> create()
|
|
{
|
|
return RegistryManager.ACTIVE.createRegistry(registryName, registryType, optionalDefaultKey, minId, maxId,
|
|
getAdd(), getClear(), getCreate(), saveToDisc, allowOverrides, allowModifications, dummyFactory);
|
|
}
|
|
|
|
@Nullable
|
|
private AddCallback<T> getAdd()
|
|
{
|
|
if (addCallback.isEmpty())
|
|
return null;
|
|
if (addCallback.size() == 1)
|
|
return addCallback.get(0);
|
|
|
|
return (owner, stage, id, obj) ->
|
|
{
|
|
for (AddCallback<T> cb : this.addCallback)
|
|
cb.onAdd(owner, stage, id, obj);
|
|
};
|
|
}
|
|
|
|
@Nullable
|
|
private ClearCallback<T> getClear()
|
|
{
|
|
if (clearCallback.isEmpty())
|
|
return null;
|
|
if (clearCallback.size() == 1)
|
|
return clearCallback.get(0);
|
|
|
|
return (owner, stage) ->
|
|
{
|
|
for (ClearCallback<T> cb : this.clearCallback)
|
|
cb.onClear(owner, stage);
|
|
};
|
|
}
|
|
|
|
@Nullable
|
|
private CreateCallback<T> getCreate()
|
|
{
|
|
if (createCallback.isEmpty())
|
|
return null;
|
|
if (createCallback.size() == 1)
|
|
return createCallback.get(0);
|
|
|
|
return (owner, stage) ->
|
|
{
|
|
for (CreateCallback<T> cb : this.createCallback)
|
|
cb.onCreate(owner, stage);
|
|
};
|
|
}
|
|
}
|