Add custom registry type support to DeferredRegister. (#6742)
It's still a hard dependency because it requires a class reference. But there is not a sane way to test registry types without a class reference.
This commit is contained in:
parent
5dbea9ea8d
commit
fd9bf64d53
|
@ -50,6 +50,10 @@ public final class RegistryObject<T extends IForgeRegistryEntry<? super T>> impl
|
||||||
return new RegistryObject<>(name, registry);
|
return new RegistryObject<>(name, registry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <T extends IForgeRegistryEntry<T>, U extends T> RegistryObject<U> of(final ResourceLocation name, final Class<T> baseType, String modid) {
|
||||||
|
return new RegistryObject<>(name, baseType, modid);
|
||||||
|
}
|
||||||
|
|
||||||
private static RegistryObject<?> EMPTY = new RegistryObject<>();
|
private static RegistryObject<?> EMPTY = new RegistryObject<>();
|
||||||
|
|
||||||
private static <T extends IForgeRegistryEntry<? super T>> RegistryObject<T> empty() {
|
private static <T extends IForgeRegistryEntry<? super T>> RegistryObject<T> empty() {
|
||||||
|
@ -80,6 +84,30 @@ public final class RegistryObject<T extends IForgeRegistryEntry<? super T>> impl
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
private <V extends IForgeRegistryEntry<V>> RegistryObject(final ResourceLocation name, final Class<V> baseType, final String modid)
|
||||||
|
{
|
||||||
|
this.name = name;
|
||||||
|
final Throwable callerStack = new Throwable("Calling Site from mod: " + modid);
|
||||||
|
ObjectHolderRegistry.addHandler(new Consumer<Predicate<ResourceLocation>>()
|
||||||
|
{
|
||||||
|
private IForgeRegistry<V> registry;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(Predicate<ResourceLocation> pred)
|
||||||
|
{
|
||||||
|
if (registry == null)
|
||||||
|
{
|
||||||
|
this.registry = RegistryManager.ACTIVE.getRegistry(baseType);
|
||||||
|
if (registry == null)
|
||||||
|
throw new IllegalStateException("Unable to find registry for type " + baseType.getName() + " for mod \"" + modid + "\". Check the 'caused by' to see futher stack.", callerStack);
|
||||||
|
}
|
||||||
|
if (pred.test(registry.getRegistryName()))
|
||||||
|
RegistryObject.this.value = registry.containsKey(RegistryObject.this.name) ? (T)registry.getValue(RegistryObject.this.name) : null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Directly retrieves the wrapped Registry Object. This value will automatically be updated when the backing registry is updated.
|
* Directly retrieves the wrapped Registry Object. This value will automatically be updated when the backing registry is updated.
|
||||||
* Will throw NPE if the value is null, use isPresent to check first. Or use any of the other guarded functions.
|
* Will throw NPE if the value is null, use isPresent to check first. Or use any of the other guarded functions.
|
||||||
|
|
|
@ -21,6 +21,7 @@ package net.minecraftforge.registries;
|
||||||
|
|
||||||
import net.minecraft.util.ResourceLocation;
|
import net.minecraft.util.ResourceLocation;
|
||||||
import net.minecraftforge.event.RegistryEvent;
|
import net.minecraftforge.event.RegistryEvent;
|
||||||
|
import net.minecraftforge.eventbus.api.EventPriority;
|
||||||
import net.minecraftforge.eventbus.api.IEventBus;
|
import net.minecraftforge.eventbus.api.IEventBus;
|
||||||
import net.minecraftforge.fml.RegistryObject;
|
import net.minecraftforge.fml.RegistryObject;
|
||||||
|
|
||||||
|
@ -33,6 +34,8 @@ import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
import com.google.common.reflect.TypeToken;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility class to help with managing registry entries.
|
* Utility class to help with managing registry entries.
|
||||||
* Maintains a list of all suppliers for entries and registers them during the proper Register event.
|
* Maintains a list of all suppliers for entries and registers them during the proper Register event.
|
||||||
|
@ -56,15 +59,41 @@ import java.util.function.Supplier;
|
||||||
*/
|
*/
|
||||||
public class DeferredRegister<T extends IForgeRegistryEntry<T>>
|
public class DeferredRegister<T extends IForgeRegistryEntry<T>>
|
||||||
{
|
{
|
||||||
private final IForgeRegistry<T> type;
|
/**
|
||||||
|
* 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;
|
||||||
private final String modid;
|
private final String modid;
|
||||||
private final Map<RegistryObject<T>, Supplier<? extends T>> entries = new LinkedHashMap<>();
|
private final Map<RegistryObject<T>, Supplier<? extends T>> entries = new LinkedHashMap<>();
|
||||||
private final Set<RegistryObject<T>> entriesView = Collections.unmodifiableSet(entries.keySet());
|
private final Set<RegistryObject<T>> entriesView = Collections.unmodifiableSet(entries.keySet());
|
||||||
|
|
||||||
|
private IForgeRegistry<T> type;
|
||||||
|
private Supplier<RegistryBuilder<T>> registryFactory;
|
||||||
|
|
||||||
|
private DeferredRegister(Class<T> base, String modid)
|
||||||
|
{
|
||||||
|
this.superType = base;
|
||||||
|
this.modid = modid;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Deprecated // Make private in 1.16, Use create. Constructors are an implementation detail.
|
||||||
public DeferredRegister(IForgeRegistry<T> reg, String modid)
|
public DeferredRegister(IForgeRegistry<T> reg, String modid)
|
||||||
{
|
{
|
||||||
|
this(reg.getRegistrySuperType(), modid);
|
||||||
this.type = reg;
|
this.type = reg;
|
||||||
this.modid = modid;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -80,13 +109,41 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
|
||||||
Objects.requireNonNull(name);
|
Objects.requireNonNull(name);
|
||||||
Objects.requireNonNull(sup);
|
Objects.requireNonNull(sup);
|
||||||
final ResourceLocation key = new ResourceLocation(modid, name);
|
final ResourceLocation key = new ResourceLocation(modid, name);
|
||||||
RegistryObject<I> ret = RegistryObject.of(key, this.type);
|
|
||||||
|
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");
|
||||||
|
|
||||||
if (entries.putIfAbsent((RegistryObject<T>) ret, () -> sup.get().setRegistryName(key)) != null) {
|
if (entries.putIfAbsent((RegistryObject<T>) ret, () -> sup.get().setRegistryName(key)) != null) {
|
||||||
throw new IllegalArgumentException("Duplicate registration " + name);
|
throw new IllegalArgumentException("Duplicate registration " + name);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds our event handler to the specified event bus, this MUST be called in order for this class to function.
|
* Adds our event handler to the specified event bus, this MUST be called in order for this class to function.
|
||||||
* See the example usage.
|
* See the example usage.
|
||||||
|
@ -96,6 +153,12 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
|
||||||
public void register(IEventBus bus)
|
public void register(IEventBus bus)
|
||||||
{
|
{
|
||||||
bus.addListener(this::addEntries);
|
bus.addListener(this::addEntries);
|
||||||
|
if (this.type == null) {
|
||||||
|
if (this.registryFactory != null)
|
||||||
|
bus.addListener(this::createRegistry);
|
||||||
|
else
|
||||||
|
bus.addListener(EventPriority.LOWEST, this::captureRegistry);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -108,7 +171,7 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
|
||||||
|
|
||||||
private void addEntries(RegistryEvent.Register<?> event)
|
private void addEntries(RegistryEvent.Register<?> event)
|
||||||
{
|
{
|
||||||
if (event.getGenericType() == this.type.getRegistrySuperType())
|
if (this.type != null && event.getGenericType() == this.type.getRegistrySuperType())
|
||||||
{
|
{
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
IForgeRegistry<T> reg = (IForgeRegistry<T>)event.getRegistry();
|
IForgeRegistry<T> reg = (IForgeRegistry<T>)event.getRegistry();
|
||||||
|
@ -119,4 +182,21 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,12 +26,6 @@ import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import net.minecraft.util.ResourceLocation;
|
import net.minecraft.util.ResourceLocation;
|
||||||
|
|
||||||
|
|
||||||
import net.minecraft.util.ResourceLocation;
|
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main interface for the registry system. Use this to query the registry system.
|
* Main interface for the registry system. Use this to query the registry system.
|
||||||
*
|
*
|
||||||
|
@ -72,6 +66,7 @@ public interface IForgeRegistry<V extends IForgeRegistryEntry<V>> extends Iterab
|
||||||
* Callback fired when objects are added to the registry. This will fire when the registry is rebuilt
|
* Callback fired when objects are added to the registry. This will fire when the registry is rebuilt
|
||||||
* on the client side from a server side synchronization, or when a world is loaded.
|
* on the client side from a server side synchronization, or when a world is loaded.
|
||||||
*/
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
interface AddCallback<V extends IForgeRegistryEntry<V>>
|
interface AddCallback<V extends IForgeRegistryEntry<V>>
|
||||||
{
|
{
|
||||||
void onAdd(IForgeRegistryInternal<V> owner, RegistryManager stage, int id, V obj, @Nullable V oldObj);
|
void onAdd(IForgeRegistryInternal<V> owner, RegistryManager stage, int id, V obj, @Nullable V oldObj);
|
||||||
|
@ -81,6 +76,7 @@ public interface IForgeRegistry<V extends IForgeRegistryEntry<V>> extends Iterab
|
||||||
* Callback fired when the registry is cleared. This is done before a registry is reloaded from client
|
* Callback fired when the registry is cleared. This is done before a registry is reloaded from client
|
||||||
* or server.
|
* or server.
|
||||||
*/
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
interface ClearCallback<V extends IForgeRegistryEntry<V>>
|
interface ClearCallback<V extends IForgeRegistryEntry<V>>
|
||||||
{
|
{
|
||||||
void onClear(IForgeRegistryInternal<V> owner, RegistryManager stage);
|
void onClear(IForgeRegistryInternal<V> owner, RegistryManager stage);
|
||||||
|
@ -89,6 +85,7 @@ public interface IForgeRegistry<V extends IForgeRegistryEntry<V>> extends Iterab
|
||||||
/**
|
/**
|
||||||
* Callback fired when a registry instance is created. Populate slave maps here.
|
* Callback fired when a registry instance is created. Populate slave maps here.
|
||||||
*/
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
interface CreateCallback<V extends IForgeRegistryEntry<V>>
|
interface CreateCallback<V extends IForgeRegistryEntry<V>>
|
||||||
{
|
{
|
||||||
void onCreate(IForgeRegistryInternal<V> owner, RegistryManager stage);
|
void onCreate(IForgeRegistryInternal<V> owner, RegistryManager stage);
|
||||||
|
@ -97,6 +94,7 @@ public interface IForgeRegistry<V extends IForgeRegistryEntry<V>> extends Iterab
|
||||||
/**
|
/**
|
||||||
* Callback fired when the registry contents are validated.
|
* Callback fired when the registry contents are validated.
|
||||||
*/
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
interface ValidateCallback<V extends IForgeRegistryEntry<V>>
|
interface ValidateCallback<V extends IForgeRegistryEntry<V>>
|
||||||
{
|
{
|
||||||
void onValidate(IForgeRegistryInternal<V> owner, RegistryManager stage, int id, ResourceLocation key, V obj);
|
void onValidate(IForgeRegistryInternal<V> owner, RegistryManager stage, int id, ResourceLocation key, V obj);
|
||||||
|
@ -105,6 +103,7 @@ public interface IForgeRegistry<V extends IForgeRegistryEntry<V>> extends Iterab
|
||||||
/**
|
/**
|
||||||
* Callback fired when the registry is done processing. Used to calculate state ID maps.
|
* Callback fired when the registry is done processing. Used to calculate state ID maps.
|
||||||
*/
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
interface BakeCallback<V extends IForgeRegistryEntry<V>>
|
interface BakeCallback<V extends IForgeRegistryEntry<V>>
|
||||||
{
|
{
|
||||||
void onBake(IForgeRegistryInternal<V> owner, RegistryManager stage);
|
void onBake(IForgeRegistryInternal<V> owner, RegistryManager stage);
|
||||||
|
@ -113,6 +112,7 @@ public interface IForgeRegistry<V extends IForgeRegistryEntry<V>> extends Iterab
|
||||||
/**
|
/**
|
||||||
* Factory for creating dummy entries, allowing worlds to be loaded and keep the missing block references.
|
* Factory for creating dummy entries, allowing worlds to be loaded and keep the missing block references.
|
||||||
*/
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
interface DummyFactory<V extends IForgeRegistryEntry<V>>
|
interface DummyFactory<V extends IForgeRegistryEntry<V>>
|
||||||
{
|
{
|
||||||
V createDummy(ResourceLocation key);
|
V createDummy(ResourceLocation key);
|
||||||
|
@ -121,6 +121,7 @@ public interface IForgeRegistry<V extends IForgeRegistryEntry<V>> extends Iterab
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
interface MissingFactory<V extends IForgeRegistryEntry<V>>
|
interface MissingFactory<V extends IForgeRegistryEntry<V>>
|
||||||
{
|
{
|
||||||
V createMissing(ResourceLocation key, boolean isNetwork);
|
V createMissing(ResourceLocation key, boolean isNetwork);
|
||||||
|
|
|
@ -45,7 +45,6 @@ import org.apache.logging.log4j.Logger;
|
||||||
import org.objectweb.asm.Opcodes;
|
import org.objectweb.asm.Opcodes;
|
||||||
import org.objectweb.asm.Type;
|
import org.objectweb.asm.Type;
|
||||||
|
|
||||||
import static net.minecraftforge.fml.Logging.CORE;
|
|
||||||
import static net.minecraftforge.registries.ForgeRegistry.REGISTRIES;
|
import static net.minecraftforge.registries.ForgeRegistry.REGISTRIES;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -108,42 +108,77 @@ public class RegistryBuilder<T extends IForgeRegistryEntry<T>>
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public RegistryBuilder<T> onAdd(AddCallback<T> add)
|
||||||
|
{
|
||||||
|
return this.add(add);
|
||||||
|
}
|
||||||
|
|
||||||
public RegistryBuilder<T> add(ClearCallback<T> clear)
|
public RegistryBuilder<T> add(ClearCallback<T> clear)
|
||||||
{
|
{
|
||||||
this.clearCallback.add(clear);
|
this.clearCallback.add(clear);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public RegistryBuilder<T> onClear(ClearCallback<T> clear)
|
||||||
|
{
|
||||||
|
return this.add(clear);
|
||||||
|
}
|
||||||
|
|
||||||
public RegistryBuilder<T> add(CreateCallback<T> create)
|
public RegistryBuilder<T> add(CreateCallback<T> create)
|
||||||
{
|
{
|
||||||
this.createCallback.add(create);
|
this.createCallback.add(create);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public RegistryBuilder<T> onCreate(CreateCallback<T> create)
|
||||||
|
{
|
||||||
|
return this.add(create);
|
||||||
|
}
|
||||||
|
|
||||||
public RegistryBuilder<T> add(ValidateCallback<T> validate)
|
public RegistryBuilder<T> add(ValidateCallback<T> validate)
|
||||||
{
|
{
|
||||||
this.validateCallback.add(validate);
|
this.validateCallback.add(validate);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public RegistryBuilder<T> onValidate(ValidateCallback<T> validate)
|
||||||
|
{
|
||||||
|
return this.add(validate);
|
||||||
|
}
|
||||||
|
|
||||||
public RegistryBuilder<T> add(BakeCallback<T> bake)
|
public RegistryBuilder<T> add(BakeCallback<T> bake)
|
||||||
{
|
{
|
||||||
this.bakeCallback.add(bake);
|
this.bakeCallback.add(bake);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public RegistryBuilder<T> onBake(BakeCallback<T> bake)
|
||||||
|
{
|
||||||
|
return this.add(bake);
|
||||||
|
}
|
||||||
|
|
||||||
public RegistryBuilder<T> set(DummyFactory<T> factory)
|
public RegistryBuilder<T> set(DummyFactory<T> factory)
|
||||||
{
|
{
|
||||||
this.dummyFactory = factory;
|
this.dummyFactory = factory;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public RegistryBuilder<T> dummy(DummyFactory<T> factory)
|
||||||
|
{
|
||||||
|
return this.set(factory);
|
||||||
|
}
|
||||||
|
|
||||||
public RegistryBuilder<T> set(MissingFactory<T> missing)
|
public RegistryBuilder<T> set(MissingFactory<T> missing)
|
||||||
{
|
{
|
||||||
this.missingFactory = missing;
|
this.missingFactory = missing;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public RegistryBuilder<T> missing(MissingFactory<T> missing)
|
||||||
|
{
|
||||||
|
return this.set(missing);
|
||||||
|
}
|
||||||
|
|
||||||
public RegistryBuilder<T> disableSaving()
|
public RegistryBuilder<T> disableSaving()
|
||||||
{
|
{
|
||||||
this.saveToDisc = false;
|
this.saveToDisc = false;
|
||||||
|
|
|
@ -0,0 +1,75 @@
|
||||||
|
package net.minecraftforge.debug;
|
||||||
|
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
import net.minecraft.block.Block;
|
||||||
|
import net.minecraft.block.material.Material;
|
||||||
|
import net.minecraft.data.DataGenerator;
|
||||||
|
import net.minecraft.item.BlockItem;
|
||||||
|
import net.minecraft.item.Item;
|
||||||
|
import net.minecraft.item.ItemGroup;
|
||||||
|
import net.minecraft.util.ResourceLocation;
|
||||||
|
import net.minecraftforge.client.model.generators.BlockStateProvider;
|
||||||
|
import net.minecraftforge.client.model.generators.ModelFile;
|
||||||
|
import net.minecraftforge.eventbus.api.IEventBus;
|
||||||
|
import net.minecraftforge.fml.RegistryObject;
|
||||||
|
import net.minecraftforge.fml.common.Mod;
|
||||||
|
import net.minecraftforge.fml.event.lifecycle.GatherDataEvent;
|
||||||
|
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||||
|
import net.minecraftforge.registries.DeferredRegister;
|
||||||
|
import net.minecraftforge.registries.ForgeRegistries;
|
||||||
|
import net.minecraftforge.registries.ForgeRegistryEntry;
|
||||||
|
import net.minecraftforge.registries.IForgeRegistry;
|
||||||
|
import net.minecraftforge.registries.RegistryBuilder;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
@Mod(DeferredRegistryTest.MODID)
|
||||||
|
public class DeferredRegistryTest {
|
||||||
|
static final String MODID = "deferred_registry_test";
|
||||||
|
private static final Logger LOGGER = LogManager.getLogger();
|
||||||
|
|
||||||
|
private static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID);
|
||||||
|
private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID);
|
||||||
|
private static final DeferredRegister<Custom> CUSTOMS = DeferredRegister.create(Custom.class, MODID);
|
||||||
|
|
||||||
|
private static final RegistryObject<Block> BLOCK = BLOCKS.register("test", () -> new Block(Block.Properties.create(Material.ROCK)));
|
||||||
|
private static final RegistryObject<Item> ITEM = ITEMS .register("test", () -> new BlockItem(BLOCK.get(), new Item.Properties().group(ItemGroup.BUILDING_BLOCKS)));
|
||||||
|
private static final RegistryObject<Custom> CUSTOM = CUSTOMS.register("test", () -> new Custom(){});
|
||||||
|
|
||||||
|
private static final Supplier<IForgeRegistry<Custom>> CUSTOM_REG = CUSTOMS.makeRegistry("test_registry", () ->
|
||||||
|
new RegistryBuilder<Custom>().disableSaving().setMaxID(Integer.MAX_VALUE - 1)
|
||||||
|
.onAdd((owner, stage, id, obj, old) -> LOGGER.info("Custom Added: " + id + " " + obj.foo()))
|
||||||
|
);
|
||||||
|
|
||||||
|
public DeferredRegistryTest() {
|
||||||
|
IEventBus modBus = FMLJavaModLoadingContext.get().getModEventBus();
|
||||||
|
BLOCKS.register(modBus);
|
||||||
|
ITEMS.register(modBus);
|
||||||
|
CUSTOMS.register(modBus);
|
||||||
|
modBus.addListener(this::gatherData);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void gatherData(GatherDataEvent event) {
|
||||||
|
DataGenerator gen = event.getGenerator();
|
||||||
|
|
||||||
|
if (event.includeClient()) {
|
||||||
|
gen.addProvider(new BlockStateProvider(gen, MODID, event.getExistingFileHelper()) {
|
||||||
|
@Override
|
||||||
|
protected void registerStatesAndModels() {
|
||||||
|
ModelFile model = models().cubeAll(BLOCK.get().getRegistryName().getPath(), mcLoc("block/furnace_top"));
|
||||||
|
simpleBlock(BLOCK.get(), model);
|
||||||
|
simpleBlockItem(BLOCK.get(), model);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Custom extends ForgeRegistryEntry<Custom> {
|
||||||
|
public String foo() {
|
||||||
|
return this.getClass().getName();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -40,7 +40,6 @@ import net.minecraft.world.World;
|
||||||
import net.minecraftforge.api.distmarker.Dist;
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
import net.minecraftforge.client.model.generators.BlockStateProvider;
|
import net.minecraftforge.client.model.generators.BlockStateProvider;
|
||||||
import net.minecraftforge.client.model.generators.ExistingFileHelper;
|
import net.minecraftforge.client.model.generators.ExistingFileHelper;
|
||||||
import net.minecraftforge.client.model.generators.ItemModelProvider;
|
|
||||||
import net.minecraftforge.client.model.generators.ModelFile;
|
import net.minecraftforge.client.model.generators.ModelFile;
|
||||||
import net.minecraftforge.event.world.PistonEvent;
|
import net.minecraftforge.event.world.PistonEvent;
|
||||||
import net.minecraftforge.event.world.PistonEvent.PistonMoveType;
|
import net.minecraftforge.event.world.PistonEvent.PistonMoveType;
|
||||||
|
@ -66,8 +65,8 @@ public class PistonEventTest
|
||||||
{
|
{
|
||||||
public static final String MODID = "piston_event_test";
|
public static final String MODID = "piston_event_test";
|
||||||
public static String blockName = "shiftonmove";
|
public static String blockName = "shiftonmove";
|
||||||
private static DeferredRegister<Block> BLOCKS = new DeferredRegister<Block>(ForgeRegistries.BLOCKS, MODID);
|
private static DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID);
|
||||||
private static DeferredRegister<Item> ITEMS = new DeferredRegister<Item> (ForgeRegistries.ITEMS, MODID);
|
private static DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID);
|
||||||
|
|
||||||
private static RegistryObject<Block> shiftOnMove = BLOCKS.register(blockName, () -> new Block(Block.Properties.create(Material.ROCK)));
|
private static RegistryObject<Block> shiftOnMove = BLOCKS.register(blockName, () -> new Block(Block.Properties.create(Material.ROCK)));
|
||||||
static {
|
static {
|
||||||
|
|
|
@ -47,6 +47,12 @@ loaderVersion="[28,)"
|
||||||
modId="nameplate_render_test"
|
modId="nameplate_render_test"
|
||||||
[[mods]]
|
[[mods]]
|
||||||
modId="global_loot_test"
|
modId="global_loot_test"
|
||||||
|
[[dependencies.global_loot_test]]
|
||||||
|
modId="forge"
|
||||||
|
mandatory=true
|
||||||
|
versionRange="[1,)"
|
||||||
|
ordering="AFTER"
|
||||||
|
side="BOTH"
|
||||||
[[mods]]
|
[[mods]]
|
||||||
modId="custom_plant_type_test"
|
modId="custom_plant_type_test"
|
||||||
[[mods]]
|
[[mods]]
|
||||||
|
@ -57,9 +63,5 @@ loaderVersion="[28,)"
|
||||||
modId="music_disc_test"
|
modId="music_disc_test"
|
||||||
[[mods]]
|
[[mods]]
|
||||||
modId="stencil_enable_test"
|
modId="stencil_enable_test"
|
||||||
[[dependencies.global_loot_test]]
|
[[mods]]
|
||||||
modId="forge"
|
modId="deferred_registry_test"
|
||||||
mandatory=true
|
|
||||||
versionRange="[1,)"
|
|
||||||
ordering="AFTER"
|
|
||||||
side="BOTH"
|
|
||||||
|
|
Loading…
Reference in New Issue