2019-09-10 23:02:07 +00:00
|
|
|
/*
|
|
|
|
* Minecraft Forge
|
2020-07-02 17:49:11 +00:00
|
|
|
* Copyright (c) 2016-2020.
|
2019-09-10 23:02:07 +00:00
|
|
|
*
|
|
|
|
* 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 net.minecraft.util.ResourceLocation;
|
|
|
|
import net.minecraftforge.event.RegistryEvent;
|
2020-06-12 22:28:43 +00:00
|
|
|
import net.minecraftforge.eventbus.api.EventPriority;
|
2019-09-10 23:02:07 +00:00
|
|
|
import net.minecraftforge.eventbus.api.IEventBus;
|
|
|
|
import net.minecraftforge.fml.RegistryObject;
|
|
|
|
|
2019-10-25 05:09:08 +00:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
2019-09-25 22:01:39 +00:00
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.Objects;
|
2019-10-25 05:09:08 +00:00
|
|
|
import java.util.Set;
|
2019-09-25 22:01:39 +00:00
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
2020-06-12 22:28:43 +00:00
|
|
|
import com.google.common.reflect.TypeToken;
|
|
|
|
|
2019-09-10 23:02:07 +00:00
|
|
|
/**
|
|
|
|
* Utility class to help with managing registry entries.
|
|
|
|
* Maintains a list of all suppliers for entries and registers them during the proper Register event.
|
|
|
|
* Suppliers should return NEW instances every time.
|
|
|
|
*
|
|
|
|
*Example Usage:
|
|
|
|
*<pre>
|
|
|
|
* private static final DeferredRegister<Item> ITEMS = new DeferredRegister<>(ForgeRegistries.ITEMS, MODID);
|
|
|
|
* private static final DeferredRegister<Block> BLOCKS = new DeferredRegister<>(ForgeRegistries.BLOCKS, MODID);
|
|
|
|
*
|
|
|
|
* public static final RegistryObject<Block> ROCK_BLOCK = BLOCKS.register("rock", () -> new Block(Block.Properties.create(Material.ROCK)));
|
|
|
|
* public static final RegistryObject<Item> ROCK_ITEM = ITEMS.register("rock", () -> new BlockItem(ROCK_BLOCK.get(), new Item.Properties().group(ItemGroup.MISC)));
|
|
|
|
*
|
|
|
|
* public ExampleMod() {
|
|
|
|
* ITEMS.register(FMLJavaModLoadingContext.get().getModEventBus());
|
|
|
|
* BLOCKS.register(FMLJavaModLoadingContext.get().getModEventBus());
|
|
|
|
* }
|
|
|
|
*</pre>
|
|
|
|
*
|
|
|
|
* @param <T> The base registry type, must be a concrete base class, do not use subclasses or wild cards.
|
|
|
|
*/
|
|
|
|
public class DeferredRegister<T extends IForgeRegistryEntry<T>>
|
|
|
|
{
|
2020-06-12 22:28:43 +00:00
|
|
|
/**
|
|
|
|
* Use for vanilla/forge registries. See example above.
|
|
|
|
*/
|
|
|
|
public static <B extends IForgeRegistryEntry<B>> DeferredRegister<B> create(IForgeRegistry<B> reg, String modid)
|
|
|
|
{
|
|
|
|
return new DeferredRegister<B>(reg, modid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use for custom registries that are made during the NewRegistry event.
|
|
|
|
*/
|
|
|
|
public static <B extends IForgeRegistryEntry<B>> DeferredRegister<B> create(Class<B> base, String modid)
|
|
|
|
{
|
|
|
|
return new DeferredRegister<B>(base, modid);
|
|
|
|
}
|
|
|
|
|
|
|
|
private final Class<T> superType;
|
2019-09-10 23:02:07 +00:00
|
|
|
private final String modid;
|
2019-10-25 05:09:08 +00:00
|
|
|
private final Map<RegistryObject<T>, Supplier<? extends T>> entries = new LinkedHashMap<>();
|
|
|
|
private final Set<RegistryObject<T>> entriesView = Collections.unmodifiableSet(entries.keySet());
|
2019-09-10 23:02:07 +00:00
|
|
|
|
2020-06-12 22:28:43 +00:00
|
|
|
private IForgeRegistry<T> type;
|
|
|
|
private Supplier<RegistryBuilder<T>> registryFactory;
|
2020-06-22 07:52:30 +00:00
|
|
|
private boolean seenRegisterEvent = false;
|
2020-06-12 22:28:43 +00:00
|
|
|
|
|
|
|
private DeferredRegister(Class<T> base, String modid)
|
|
|
|
{
|
|
|
|
this.superType = base;
|
|
|
|
this.modid = modid;
|
|
|
|
}
|
|
|
|
|
2020-06-25 01:25:55 +00:00
|
|
|
private DeferredRegister(IForgeRegistry<T> reg, String modid)
|
2019-09-10 23:02:07 +00:00
|
|
|
{
|
2020-06-12 22:28:43 +00:00
|
|
|
this(reg.getRegistrySuperType(), modid);
|
2019-09-10 23:02:07 +00:00
|
|
|
this.type = reg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new supplier to the list of entries to be registered, and returns a RegistryObject that will be populated with the created entry automatically.
|
|
|
|
*
|
|
|
|
* @param name The new entry's name, it will automatically have the modid prefixed.
|
|
|
|
* @param sup A factory for the new entry, it should return a new instance every time it is called.
|
|
|
|
* @return A RegistryObject that will be updated with when the entries in the registry change.
|
|
|
|
*/
|
2019-09-25 22:01:39 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
2019-09-27 21:54:52 +00:00
|
|
|
public <I extends T> RegistryObject<I> register(final String name, final Supplier<? extends I> sup)
|
2019-09-10 23:02:07 +00:00
|
|
|
{
|
2020-06-22 07:52:30 +00:00
|
|
|
if (seenRegisterEvent)
|
|
|
|
throw new IllegalStateException("Cannot register new entries to DeferredRegister after RegistryEvent.Register has been fired.");
|
2019-09-25 22:01:39 +00:00
|
|
|
Objects.requireNonNull(name);
|
|
|
|
Objects.requireNonNull(sup);
|
2019-09-10 23:02:07 +00:00
|
|
|
final ResourceLocation key = new ResourceLocation(modid, name);
|
2020-06-12 22:28:43 +00:00
|
|
|
|
|
|
|
RegistryObject<I> ret;
|
|
|
|
if (this.type != null)
|
|
|
|
ret = RegistryObject.of(key, this.type);
|
|
|
|
else if (this.superType != null)
|
|
|
|
ret = RegistryObject.of(key, this.superType, this.modid);
|
|
|
|
else
|
|
|
|
throw new IllegalStateException("Could not create RegistryObject in DeferredRegister");
|
|
|
|
|
2019-09-25 22:01:39 +00:00
|
|
|
if (entries.putIfAbsent((RegistryObject<T>) ret, () -> sup.get().setRegistryName(key)) != null) {
|
|
|
|
throw new IllegalArgumentException("Duplicate registration " + name);
|
|
|
|
}
|
2020-06-12 22:28:43 +00:00
|
|
|
|
2019-09-25 22:01:39 +00:00
|
|
|
return ret;
|
2019-09-10 23:02:07 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 22:28:43 +00:00
|
|
|
/**
|
|
|
|
* For custom registries only, fills the {@link #registryFactory} to be called later see {@link #register(IEventBus)}
|
|
|
|
*
|
|
|
|
* Calls {@link RegistryBuilder#setName} and {@link RegistryBuilder#setType} automatically.
|
|
|
|
*
|
|
|
|
* @param name Path of the registry's {@link ResourceLocation}
|
|
|
|
* @param sup Supplier of the RegistryBuilder that is called to fill {@link #type} during the NewRegistry event
|
|
|
|
* @return A supplier of the {@link IForgeRegistry} created by the builder.
|
|
|
|
*/
|
|
|
|
public Supplier<IForgeRegistry<T>> makeRegistry(final String name, final Supplier<RegistryBuilder<T>> sup) {
|
|
|
|
if (this.superType == null)
|
|
|
|
throw new IllegalStateException("Cannot create a registry without specifying a base type");
|
|
|
|
if (this.type != null || this.registryFactory != null)
|
|
|
|
throw new IllegalStateException("Cannot create a registry for a type that already exists");
|
|
|
|
|
|
|
|
this.registryFactory = () -> sup.get().setName(new ResourceLocation(modid, name)).setType(this.superType);
|
|
|
|
return () -> this.type;
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:02:07 +00:00
|
|
|
/**
|
|
|
|
* Adds our event handler to the specified event bus, this MUST be called in order for this class to function.
|
|
|
|
* See the example usage.
|
|
|
|
*
|
|
|
|
* @param bus The Mod Specific event bus.
|
|
|
|
*/
|
|
|
|
public void register(IEventBus bus)
|
|
|
|
{
|
|
|
|
bus.addListener(this::addEntries);
|
2020-06-12 22:28:43 +00:00
|
|
|
if (this.type == null) {
|
|
|
|
if (this.registryFactory != null)
|
|
|
|
bus.addListener(this::createRegistry);
|
|
|
|
else
|
|
|
|
bus.addListener(EventPriority.LOWEST, this::captureRegistry);
|
|
|
|
}
|
2019-09-10 23:02:07 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 05:09:08 +00:00
|
|
|
/**
|
|
|
|
* @return The unmodifiable view of registered entries. Useful for bulk operations on all values.
|
|
|
|
*/
|
|
|
|
public Collection<RegistryObject<T>> getEntries()
|
|
|
|
{
|
|
|
|
return entriesView;
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:02:07 +00:00
|
|
|
private void addEntries(RegistryEvent.Register<?> event)
|
|
|
|
{
|
2020-06-12 22:28:43 +00:00
|
|
|
if (this.type != null && event.getGenericType() == this.type.getRegistrySuperType())
|
2019-09-10 23:02:07 +00:00
|
|
|
{
|
2020-06-22 07:52:30 +00:00
|
|
|
this.seenRegisterEvent = true;
|
2019-09-10 23:02:07 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
IForgeRegistry<T> reg = (IForgeRegistry<T>)event.getRegistry();
|
2019-09-25 22:01:39 +00:00
|
|
|
for (Entry<RegistryObject<T>, Supplier<? extends T>> e : entries.entrySet())
|
|
|
|
{
|
|
|
|
reg.register(e.getValue().get());
|
|
|
|
e.getKey().updateReference(reg);
|
|
|
|
}
|
2019-09-10 23:02:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-12 22:28:43 +00:00
|
|
|
|
|
|
|
private void createRegistry(RegistryEvent.NewRegistry event)
|
|
|
|
{
|
|
|
|
this.type = this.registryFactory.get().create();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void captureRegistry(RegistryEvent.NewRegistry event)
|
|
|
|
{
|
|
|
|
if (this.superType != null)
|
|
|
|
{
|
|
|
|
this.type = RegistryManager.ACTIVE.getRegistry(this.superType);
|
|
|
|
if (this.type == null)
|
|
|
|
throw new IllegalStateException("Unable to find registry for type " + this.superType.getName() + " for modid \"" + modid + "\" after NewRegistry event");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw new IllegalStateException("Unable to find registry for mod \"" + modid + "\" No lookup criteria specified.");
|
|
|
|
}
|
2019-09-10 23:02:07 +00:00
|
|
|
}
|