Add registration helpers

Contains two styles: adapters (for use in registry events) and deferred (for deferred registration). Should all support the same methods
This commit is contained in:
KnightMiner committed 2020-07-22 20:57:31 -05:00
1 parent fabf401233
commit b0e34d4d60
29 files changed
+1749

No files matched your search

@@ -0,0 +1,25 @@
package slimeknights.mantle.registration;
import lombok.Setter;
import javax.annotation.Nullable;
import java.util.function.Supplier;
/**
* Supplier wrapper that allows setting the supplier at a later time. Basically a pretty big hack.
* Used since fluids are wrapped in a ton of self referencing suppliers, so we make the properties with a supplier that we fill later.
* @param <T> Supplier type
*/
public class DelayedSupplier<T> implements Supplier<T> {
@Setter
@Nullable
private Supplier<T> supplier;
@Override
public T get() {
if (supplier == null) {
throw new IllegalStateException("Attempted to call DelayedSupplier::get() before the supplier was set");
}
return supplier.get();
}
}
@@ -0,0 +1,56 @@
package slimeknights.mantle.registration;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import net.minecraft.block.FlowingFluidBlock;
import net.minecraft.fluid.Fluid;
import net.minecraft.item.Item;
import net.minecraftforge.fluids.FluidAttributes;
import net.minecraftforge.fluids.ForgeFlowingFluid;
import java.util.function.Supplier;
/**
* Fluid properties builder class, since the Forge one requires too many suppliers that we do not have access to yet
*/
@Accessors(fluent = true)
@Setter
@RequiredArgsConstructor
public class FluidBuilder {
private final FluidAttributes.Builder attributes;
private boolean canMultiply = false;
private Supplier<? extends Item> bucket;
private Supplier<? extends FlowingFluidBlock> block;
private int slopeFindDistance = 4;
private int levelDecreasePerBlock = 1;
private float explosionResistance = 1;
private int tickRate = 5;
/** Sets {@code canMultiply} to true */
public FluidBuilder canMultiply() {
canMultiply = true;
return this;
}
/**
* Builds Forge fluid properties from this builder
* @param still Still fluid supplier
* @param flowing Flowing supplier
* @return Forge fluid properties
*/
public ForgeFlowingFluid.Properties build(Supplier<? extends Fluid> still, Supplier<? extends Fluid> flowing) {
ForgeFlowingFluid.Properties properties = new ForgeFlowingFluid.Properties(still, flowing, this.attributes)
.slopeFindDistance(this.slopeFindDistance)
.levelDecreasePerBlock(this.levelDecreasePerBlock)
.explosionResistance(this.explosionResistance)
.tickRate(this.tickRate)
.block(this.block)
.bucket(this.bucket);
if (this.canMultiply) {
properties.canMultiply();
}
return properties;
}
}
@@ -0,0 +1,16 @@
package slimeknights.mantle.registration;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.Items;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ItemProperties {
/** Properties for a standard bucket item */
public static final Item.Properties BUCKET_PROPS = new Item.Properties().containerItem(Items.BUCKET).maxStackSize(1).group(ItemGroup.MISC);
/** Item property used for spawn egg items */
public static final Item.Properties EGG_PROPS = new Item.Properties().group(ItemGroup.MISC);
}
@@ -0,0 +1,98 @@
package slimeknights.mantle.registration.adapter;
import net.minecraft.block.Block;
import net.minecraft.block.FenceBlock;
import net.minecraft.block.FlowingFluidBlock;
import net.minecraft.block.SlabBlock;
import net.minecraft.block.StairsBlock;
import net.minecraft.block.WallBlock;
import net.minecraft.block.material.Material;
import net.minecraftforge.fluids.ForgeFlowingFluid;
import net.minecraftforge.registries.IForgeRegistry;
import slimeknights.mantle.registration.object.BuildingBlockObject;
import slimeknights.mantle.registration.object.FenceBuildingBlockObject;
import slimeknights.mantle.registration.object.WallBuildingBlockObject;
import java.util.function.Supplier;
/**
* Provides utility registration methods when registering blocks.
*/
@SuppressWarnings({"unused", "WeakerAccess"})
public class BlockRegistryAdapter extends EnumRegistryAdapter<Block> {
public BlockRegistryAdapter(IForgeRegistry<Block> registry) {
super(registry);
}
public BlockRegistryAdapter(IForgeRegistry<Block> registry, String modid) {
super(registry, modid);
}
/* Building */
/**
* Registers the given block as well as a slab and a stair variant for it.
* Uses the vanilla slab and stair blocks. Uses the passed blocks properties for both.
* Slabs and stairs are registered with a "_slab" and "_stairs" prefix
*
* @param block The main block to register and whose properties to use
* @param name The registry name to use for the block and as base for the slab and stairs
* @return BuildingBlockObject for the given block
*/
public BuildingBlockObject registerBuilding(Block block, String name) {
return BuildingBlockObject.fromBlocks(
this.register(block, name),
this.register(new SlabBlock(Block.Properties.from(block)), name + "_slab"),
this.register(new StairsBlock(block::getDefaultState, Block.Properties.from(block)), name + "_stairs")
);
}
/**
* Same as {@link #registerBuilding(Block, String)}, but also includes a wall variant
*
* @param block The main block to register and whose properties to use
* @param name The registry name to use for the block and as base for the slab and stairs
* @return BuildingBlockObject for the given block
*/
public WallBuildingBlockObject registerWallBuilding(Block block, String name) {
return WallBuildingBlockObject.fromBlocks(
registerBuilding(block, name),
this.register(new WallBlock(Block.Properties.from(block)), name + "_wall")
);
}
/**
* Same as {@link #registerBuilding(Block, String)}, but also includes a fence variant
*
* @param block The main block to register and whose properties to use
* @param name The registry name to use for the block and as base for the slab and stairs
* @return BuildingBlockObject for the given block
*/
public FenceBuildingBlockObject registerFenceBuilding(Block block, String name) {
return FenceBuildingBlockObject.fromBlocks(
registerBuilding(block, name),
this.register(new FenceBlock(Block.Properties.from(block)), name + "_fence")
);
}
/* Fluid */
/**
* Registers a fluid block from a fluid
* @param fluid Fluid supplier
* @param material Fluid material
* @param lightLevel Fluid light level
* @param name Fluid name, unfortunately no way to fetch from the fluid as it does not exist yet
* @return Fluid block instance
*/
public FlowingFluidBlock registerFluidBlock(Supplier<? extends ForgeFlowingFluid> fluid, Material material, int lightLevel, String name) {
return register(
new FlowingFluidBlock(fluid, Block.Properties.create(material)
.doesNotBlockMovement()
.hardnessAndResistance(100.0F)
.noDrops()
.setLightLevel((state) -> lightLevel)),
name + "_fluid");
}
}
@@ -0,0 +1,28 @@
package slimeknights.mantle.registration.adapter;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.ContainerType;
import net.minecraftforge.common.extensions.IForgeContainerType;
import net.minecraftforge.fml.network.IContainerFactory;
import net.minecraftforge.registries.IForgeRegistry;
public class ContainerTypeRegistryAdapter extends RegistryAdapter<ContainerType<?>> {
public ContainerTypeRegistryAdapter(IForgeRegistry<ContainerType<?>> registry, String modId) {
super(registry, modId);
}
public ContainerTypeRegistryAdapter(IForgeRegistry<ContainerType<?>> registry) {
super(registry);
}
/**
* Registers a container type
* @param name Container name
* @param factory Container factory
* @param <C> Container type
* @return Registry object containing the container type
*/
public <C extends Container> ContainerType<C> register(IContainerFactory<C> factory, String name) {
return register(IForgeContainerType.create(factory), name);
}
}
@@ -0,0 +1,29 @@
package slimeknights.mantle.registration.adapter;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraftforge.registries.IForgeRegistry;
/**
* Registry adapter for registering entity types
*/
public class EntityTypeRegistryAdapter extends RegistryAdapter<EntityType<?>> {
public EntityTypeRegistryAdapter(IForgeRegistry<EntityType<?>> registry, String modId) {
super(registry, modId);
}
public EntityTypeRegistryAdapter(IForgeRegistry<EntityType<?>> registry) {
super(registry);
}
/**
* Registers an entity type from a builder
* @param builder Builder instance
* @param name Type name
* @param <T> Entity type
* @return Registered entity type
*/
public <T extends Entity> EntityType<T> register(EntityType.Builder<T> builder, String name) {
return register(builder.build(resourceName(name)), name);
}
}
@@ -0,0 +1,72 @@
package slimeknights.mantle.registration.adapter;
import net.minecraft.util.IStringSerializable;
import net.minecraftforge.registries.ForgeRegistryEntry;
import net.minecraftforge.registries.IForgeRegistry;
import net.minecraftforge.registries.IRegistryDelegate;
import slimeknights.mantle.registration.object.EnumObject;
import java.util.EnumMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Adapter that allows certain registry types to register from a list of values
* @param <T> Registry type
*/
@SuppressWarnings("unused")
public class EnumRegistryAdapter<T extends ForgeRegistryEntry<T>> extends RegistryAdapter<T> {
public EnumRegistryAdapter(IForgeRegistry<T> registry) {
super(registry);
}
public EnumRegistryAdapter(IForgeRegistry<T> registry, String modId) {
super(registry, modId);
}
/**
* Registers an entry with multiple variants, prefixing the name with the value name
* @param mapper Function to get a block for the given enum value
* @param values Enum values to use for this block
* @param name Name of the block
* @return EnumObject mapping between different block types
*/
@SuppressWarnings("unchecked")
public <E extends Enum<E> & IStringSerializable,I extends T> EnumObject<E,I> registerEnum(Function<E,I> mapper, E[] values, String name) {
if (values.length == 0) {
throw new IllegalArgumentException("Must have at least one value");
}
// note this cast only works because you cannot extend an enum
Map<E,Supplier<? extends I>> map = new EnumMap<>((Class<E>)values[0].getClass());
for (E value : values) {
// assuming the type will not sub for a different class
IRegistryDelegate<T> delegate = register(mapper.apply(value), value.getString() + "_" + name).delegate;
map.put(value, () -> (I) delegate.get());
}
return new EnumObject<>(map);
}
/**
* Registers an entry with multiple variants, suffixing the name with the value name
* @param mapper Function to get a block for the given enum value
* @param name Name of the block
* @param values Enum values to use for this block
* @return EnumObject mapping between different block types
*/
@SuppressWarnings("unchecked")
public <E extends Enum<E> & IStringSerializable,I extends T> EnumObject<E,I> registerEnum(Function<E,I> mapper, String name, E[] values) {
if (values.length == 0) {
throw new IllegalArgumentException("Must have at least one value");
}
// note this cast only works because you cannot extend an enum
Map<E,Supplier<? extends I>> map = new EnumMap<>((Class<E>)values[0].getClass());
for (E value : values) {
// assuming the type will not sub for a different class
IRegistryDelegate<T> delegate = register(mapper.apply(value), name + "_" + value.getString()).delegate;
map.put(value, () -> (I) delegate.get());
}
return new EnumObject<>(map);
}
}
@@ -0,0 +1,59 @@
package slimeknights.mantle.registration.adapter;
import net.minecraft.fluid.Fluid;
import net.minecraftforge.fluids.ForgeFlowingFluid;
import net.minecraftforge.fluids.ForgeFlowingFluid.Properties;
import net.minecraftforge.registries.IForgeRegistry;
import slimeknights.mantle.registration.DelayedSupplier;
import slimeknights.mantle.registration.FluidBuilder;
import java.util.function.Function;
/**
* Registry adapter for registering fluids
*/
public class FluidRegistryAdapter extends RegistryAdapter<Fluid> {
public FluidRegistryAdapter(IForgeRegistry<Fluid> registry) {
super(registry);
}
public FluidRegistryAdapter(IForgeRegistry<Fluid> registry, String modId) {
super(registry, modId);
}
/**
* Registers a new fluid with both still and flowing variants
* @param builder Fluid properties builder
* @param still Still constructor
* @param flowing Flowing constructor
* @param name Fluid name
* @param <F> Fluid type
* @return Still fluid instance
*/
public <F extends ForgeFlowingFluid> F register(FluidBuilder builder, Function<Properties, F> still, Function<Properties,F> flowing, String name) {
// have to create still and flowing later, as the props need these suppliers
DelayedSupplier<Fluid> stillDelayed = new DelayedSupplier<>();
DelayedSupplier<Fluid> flowingDelayed = new DelayedSupplier<>();
// create props with the suppliers
Properties props = builder.build(stillDelayed, flowingDelayed);
// create fluids now that we have props
F fluid = register(still.apply(props), name);
stillDelayed.setSupplier(fluid.delegate);
flowingDelayed.setSupplier(register(flowing.apply(props), "flowing_" + name).delegate);
// return the final nice object
return fluid;
}
/**
* Registers a fluid using default constructors
* @param builder Fluid builder
* @param name Fluid name
* @return Still fluid
*/
public ForgeFlowingFluid register(FluidBuilder builder, String name) {
return register(builder, ForgeFlowingFluid.Source::new, ForgeFlowingFluid.Flowing::new, name);
}
}
@@ -0,0 +1,144 @@
package slimeknights.mantle.registration.adapter;
import net.minecraft.block.Block;
import net.minecraft.entity.EntityType;
import net.minecraft.fluid.Fluid;
import net.minecraft.item.BlockItem;
import net.minecraft.item.BucketItem;
import net.minecraft.item.Item;
import net.minecraft.item.SpawnEggItem;
import net.minecraftforge.registries.IForgeRegistry;
import slimeknights.mantle.item.BlockTooltipItem;
import slimeknights.mantle.registration.ItemProperties;
import slimeknights.mantle.registration.object.BuildingBlockObject;
import slimeknights.mantle.registration.object.EnumObject;
import slimeknights.mantle.registration.object.FenceBuildingBlockObject;
import slimeknights.mantle.registration.object.WallBuildingBlockObject;
import javax.annotation.Nullable;
import java.util.Objects;
import java.util.function.Supplier;
/**
* Provides utility registration methods when registering itemblocks.
*/
@SuppressWarnings({"unused", "WeakerAccess", "UnusedReturnValue"})
public class ItemRegistryAdapter extends EnumRegistryAdapter<Item> {
private final Item.Properties defaultProps;
public ItemRegistryAdapter(IForgeRegistry<Item> registry, @Nullable Item.Properties defaultProps) {
super(registry);
if (defaultProps == null) {
this.defaultProps = new Item.Properties();
} else {
this.defaultProps = defaultProps;
}
}
public ItemRegistryAdapter(IForgeRegistry<Item> registry, String modid, @Nullable Item.Properties defaultProps) {
super(registry, modid);
if (defaultProps == null) {
this.defaultProps = new Item.Properties();
} else {
this.defaultProps = defaultProps;
}
}
/* Standard block items */
/**
* Registers a generic item block for a block.
* If your block does not have its own item, just use this method to make it available as an item.
* The item uses the same name as the block for registration.
* The registered BlockItem has tooltip support by default, see {@link BlockTooltipItem}
* It will be added to the creative itemgroup passed in in the constructor. If you want a different one, use the method with a ItemGroup parameter.
*
* @param block The block you want to have an item for
* @return The registered item for the block
*/
public BlockItem registerBlockItem(Block block) {
return registerBlockItem(block, defaultProps);
}
/**
* Same as the variant without ItemGroup, but registers it for the given itemgroup.
* @param block The block you want to have an item for
* @param props Item properties for the block
*/
public BlockItem registerBlockItem(Block block, Item.Properties props) {
return register(new BlockTooltipItem(block, props), Objects.requireNonNull(block.getRegistryName()));
}
/**
* Shortcut method to register your own BlockItem, registering with the same name as the block it represents.
* @param blockItem Item block instance to register
* @return Registered item block, should be the same as teh one passed in.
*/
public <T extends BlockItem> T registerBlockItem(T blockItem) {
return register(blockItem, Objects.requireNonNull(blockItem.getBlock().getRegistryName()));
}
/* Block wrappers */
/**
* Registers block items for all entries in a building block object
* @param object Building block object instance
*/
public void registerBlockItem(BuildingBlockObject object) {
registerBlockItem(object.get());
registerBlockItem(object.getSlab());
registerBlockItem(object.getStairs());
}
/**
* Registers block items for all entries in a wall building block object
* @param object Building block object instance
*/
public void registerBlockItem(WallBuildingBlockObject object) {
registerBlockItem((BuildingBlockObject)object);
registerBlockItem(object.getWall());
}
/**
* Registers block items for all entries in a fence building block object
* @param object Building block object instance
*/
public void registerBlockItem(FenceBuildingBlockObject object) {
registerBlockItem((BuildingBlockObject)object);
registerBlockItem(object.getFence());
}
/**
* Registers block items for an enum object
* @param enumObject Enum object instance
*/
public void registerBlockItem(EnumObject<?, ? extends Block> enumObject) {
enumObject.values().forEach(this::registerBlockItem);
}
/* Misc */
/**
* Registers the bucket for a fluid
* @param fluid Fluid supplier
* @param baseName Fluid name, unfortunately cannot be fetched from the fluid as it does not exist yet
* @return Bucket instance
*/
public BucketItem registerBucket(Supplier<? extends Fluid> fluid, String baseName) {
return register(new BucketItem(fluid, ItemProperties.BUCKET_PROPS), baseName + "_bucket");
}
/**
* Registers a spawn egg for the entity type
* @param entity Entity type. Will be unregistered at this point
* @param primary Primary color
* @param secondary Secondary color
* @param baseName Entity name, as it may or may not be present in the entity type
* @return Spawn egg item instance
*/
public SpawnEggItem registerSpawnEgg(EntityType<?> entity, int primary, int secondary, String baseName) {
return register(new SpawnEggItem(entity, primary, secondary, ItemProperties.EGG_PROPS), baseName + "_spawn_egg");
}
}
@@ -0,0 +1,70 @@
package slimeknights.mantle.registration.adapter;
import lombok.RequiredArgsConstructor;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.registries.IForgeRegistry;
import net.minecraftforge.registries.IForgeRegistryEntry;
/**
* A convenience wrapper for forge registries, to be used in combination with the {@link net.minecraftforge.event.RegistryEvent.Register} event.
* Simply put it allows you to register things by passing (thing, name) instead of having to set the name inline.
* There also is a convenience variant for items and itemblocks, see {@link ItemRegistryAdapter}.
*/
@SuppressWarnings("WeakerAccess")
@RequiredArgsConstructor
public class RegistryAdapter<T extends IForgeRegistryEntry<T>> {
private final IForgeRegistry<T> registry;
private final String modId;
/**
* Automatically creates determines the modid from the currently loading mod.
* If this results in the wrong namespace, use the other constructor where you can provide the modid.
* The modid is used as the namespace for resource locations, so if your mods id is "foo" it will register an item "bar" as "foo:bar".
*/
public RegistryAdapter(IForgeRegistry<T> registry) {
this(registry, ModLoadingContext.get().getActiveContainer().getModId());
}
/**
* Construct a resource location that belongs to the given namespace. Usually your mod.
* @param name Name for location
*/
public ResourceLocation getResource(String name) {
return new ResourceLocation(modId, name);
}
/**
* Construct a resource location string that belongs to the given namespace. Usually your mod.
* @param name Name for location
*/
public String resourceName(String name) {
return modId + ":" + name;
}
/**
* General purpose registration method. Just pass the name you want your thing registered as.
* @param entry Entry to register
* @param name Registry name
* @return Registry entry
*/
@SuppressWarnings("UnusedReturnValue")
public <I extends T> I register(I entry, String name) {
return this.register(entry, this.getResource(name));
}
/**
* General purpose backup registration method. In case you want to set a very specific resource location.
* You should probably use the special purpose methods instead of this.
* <p>
* Note: changes the things registry name. Do not call this with already registered objects!
* @param entry Entry to register
* @param location Registry name
* @return Registry entry
*/
public <I extends T> I register(I entry, ResourceLocation location) {
entry.setRegistryName(location);
registry.register(entry);
return entry;
}
}
@@ -0,0 +1,80 @@
package slimeknights.mantle.registration.adapter;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
import com.mojang.datafixers.types.Type;
import net.minecraft.block.Block;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Util;
import net.minecraft.util.datafix.TypeReferences;
import net.minecraftforge.registries.IForgeRegistry;
import slimeknights.mantle.registration.object.EnumObject;
import javax.annotation.Nullable;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* Registry adapter for tile entity types with helpers for adding blocks
*/
public class TileEntityTypeRegistryAdapter extends RegistryAdapter<TileEntityType<?>> {
public TileEntityTypeRegistryAdapter(IForgeRegistry<TileEntityType<?>> registry, String modId) {
super(registry, modId);
}
public TileEntityTypeRegistryAdapter(IForgeRegistry<TileEntityType<?>> registry) {
super(registry);
}
/**
* Gets the data fixer type for the tile entity instance
* @param name Tile entity name
* @return Data fixer type
*/
@Nullable
private Type<?> getType(String name) {
return Util.attemptDataFix(TypeReferences.BLOCK_ENTITY, resourceName(name));
}
/**
* Registers a tile entity type for a single block
* @param factory Tile entity factory
* @param block Single block to add
* @param name Tile entity name
* @param <T> Tile entity type
* @return Registry object instance
*/
@SuppressWarnings("ConstantConditions")
public <T extends TileEntity> TileEntityType<T> register(Supplier<? extends T> factory, Block block, String name) {
return register(TileEntityType.Builder.<T>create(factory, block).build(getType(name)), name);
}
/**
* Registers a new tile entity type using a tile entity factory and an enum object
* @param name Tile entity name
* @param factory Tile entity factory
* @param blocks Enum object instance
* @param <T> Tile entity type
* @return Tile entity type registry object
*/
@SuppressWarnings("ConstantConditions")
public <T extends TileEntity> TileEntityType<T> register(Supplier<? extends T> factory, EnumObject<?, ? extends Block> blocks, String name) {
return register(new TileEntityType<>(factory, ImmutableSet.copyOf(blocks.values()), getType(name)), name);
}
/**
* Registers a new tile entity type using a tile entity factory and an immutable set builder
* @param factory Tile entity factory
* @param name Tile entity name
* @param blockCollector Function to get blocks for the list
* @param <T> Tile entity type
* @return Tile entity type registry object
*/
@SuppressWarnings("ConstantConditions")
public <T extends TileEntity> TileEntityType<T> register(Supplier<? extends T> factory, String name, Consumer<Builder<Block>> blockCollector) {
ImmutableSet.Builder<Block> blocks = new ImmutableSet.Builder<>();
blockCollector.accept(blocks);
return register(new TileEntityType<>(factory, blocks.build(), getType(name)), name);
}
}
@@ -0,0 +1,7 @@
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
package slimeknights.mantle.registration.adapter;
import mcp.MethodsReturnNonnullByDefault;
import javax.annotation.ParametersAreNonnullByDefault;
@@ -0,0 +1,164 @@
package slimeknights.mantle.registration.deferred;
import net.minecraft.block.Block;
import net.minecraft.block.FenceBlock;
import net.minecraft.block.SlabBlock;
import net.minecraft.block.StairsBlock;
import net.minecraft.block.WallBlock;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.util.IStringSerializable;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import slimeknights.mantle.registration.object.BlockItemObject;
import slimeknights.mantle.registration.object.BuildingBlockObject;
import slimeknights.mantle.registration.object.EnumObject;
import slimeknights.mantle.registration.object.FenceBuildingBlockObject;
import slimeknights.mantle.registration.object.WallBuildingBlockObject;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Deferred register to handle registering blocks with possible item forms
*/
@SuppressWarnings({"WeakerAccess", "unused"})
public class BlockDeferredRegister extends DeferredRegisterWrapper<Block> {
private final DeferredRegister<Item> itemRegister;
public BlockDeferredRegister(String modID) {
super(ForgeRegistries.BLOCKS, modID);
this.itemRegister = DeferredRegister.create(ForgeRegistries.ITEMS, modID);
}
@Override
public void register(IEventBus bus) {
super.register(bus);
itemRegister.register(bus);
}
/* Blocks with no items */
/**
* Registers a block with the block registry
* @param name Block ID
* @param block Block supplier
* @param <B> Block class
* @return Block registry object
*/
public <B extends Block> RegistryObject<B> registerNoItem(String name, Supplier<? extends B> block) {
return register.register(name, block);
}
/**
* Registers a block with the block registry
* @param name Block ID
* @param props Block properties
* @return Block registry object
*/
public RegistryObject<Block> registerNoItem(String name, Block.Properties props) {
return registerNoItem(name, () -> new Block(props));
}
/* Block item pairs */
/**
* Registers a block with the block registry, using the function for the BlockItem
* @param name Block ID
* @param block Block supplier
* @param item Function to create a BlockItem from a Block
* @param <B> Block class
* @return Block item registry object pair
*/
public <B extends Block> BlockItemObject<B> register(String name, Supplier<? extends B> block, final Function<? super B, ? extends BlockItem> item) {
RegistryObject<B> blockObj = registerNoItem(name, block);
itemRegister.register(name, () -> item.apply(blockObj.get()));
return new BlockItemObject<>(blockObj);
}
/**
* Registers a block with the block registry, using the function for the BlockItem
* @param name Block ID
* @param blockProps Block supplier
* @param item Function to create a BlockItem from a Block
* @return Block item registry object pair
*/
public BlockItemObject<Block> register(String name, Block.Properties blockProps, Function<? super Block, ? extends BlockItem> item) {
return register(name, () -> new Block(blockProps), item);
}
/* Specialty */
/**
* Registers a block with slab, and stairs
* @param name Name of the block
* @param props Block properties
* @param item Function to get an item from the block
* @return BuildingBlockObject class that returns different block types
*/
public BuildingBlockObject registerBuilding(String name, Block.Properties props, Function<? super Block, ? extends BlockItem> item) {
BlockItemObject<Block> blockObj = register(name, props, item);
return new BuildingBlockObject(blockObj,
register(name + "_slab", () -> new SlabBlock(props), item),
register(name + "_stairs", () -> new StairsBlock(() -> blockObj.get().getDefaultState(), props), item)
);
}
/**
* Registers a block with slab, stairs, and wall
* @param name Name of the block
* @param props Block properties
* @param item Function to get an item from the block
* @return StoneBuildingBlockObject class that returns different block types
*/
public WallBuildingBlockObject registerWallBuilding(String name, Block.Properties props, Function<? super Block, ? extends BlockItem> item) {
return new WallBuildingBlockObject(
registerBuilding(name, props, item),
register(name + "_wall", () -> new WallBlock(props), item)
);
}
/**
* Registers a block with slab, stairs, and fence
* @param name Name of the block
* @param props Block properties
* @param item Function to get an item from the block
* @return WoodBuildingBlockObject class that returns different block types
*/
public FenceBuildingBlockObject registerFenceBuilding(String name, Block.Properties props, Function<? super Block, ? extends BlockItem> item) {
return new FenceBuildingBlockObject(
registerBuilding(name, props, item),
register(name + "_fence", () -> new FenceBlock(props), item)
);
}
/**
* Registers an item with multiple variants, prefixing the name with the value name
* @param values Enum values to use for this block
* @param name Name of the block
* @param mapper Function to get a block for the given enum value
* @param item Function to get an item from the block
* @return EnumObject mapping between different block types
*/
public <T extends Enum<T> & IStringSerializable, B extends Block> EnumObject<T,B> registerEnum(
T[] values, String name, Function<T,? extends B> mapper, Function<? super B, ? extends BlockItem> item) {
return registerEnum(values, name, (fullName, value) -> register(fullName, () -> mapper.apply(value), item));
}
/**
* Registers a block with multiple variants, suffixing the name with the value name
* @param name Name of the block
* @param values Enum values to use for this block
* @param mapper Function to get a block for the given enum value
* @param item Function to get an item from the block
* @return EnumObject mapping between different block types
*/
public <T extends Enum<T> & IStringSerializable, B extends Block> EnumObject<T,B> registerEnum(
String name, T[] values, Function<T,? extends B> mapper, Function<? super B, ? extends BlockItem> item) {
return registerEnum(name, values, (fullName, value) -> register(fullName, () -> mapper.apply(value), item));
}
}
@@ -0,0 +1,30 @@
package slimeknights.mantle.registration.deferred;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.ContainerType;
import net.minecraftforge.common.extensions.IForgeContainerType;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.fml.network.IContainerFactory;
import net.minecraftforge.registries.ForgeRegistries;
/**
* Deferred register for container types, automatically mapping a factory argument in {@link IForgeContainerType}
*/
@SuppressWarnings("unused")
public class ContainerTypeDeferredRegister extends DeferredRegisterWrapper<ContainerType<?>> {
public ContainerTypeDeferredRegister(String modID) {
super(ForgeRegistries.CONTAINERS, modID);
}
/**
* Registers a container type
* @param name Container name
* @param factory Container factory
* @param <C> Container type
* @return Registry object containing the container type
*/
public <C extends Container> RegistryObject<ContainerType<C>> register(String name, IContainerFactory<C> factory) {
return register.register(name, () -> IForgeContainerType.create(factory));
}
}
@@ -0,0 +1,91 @@
package slimeknights.mantle.registration.deferred;
import net.minecraft.util.IStringSerializable;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.IForgeRegistry;
import net.minecraftforge.registries.IForgeRegistryEntry;
import slimeknights.mantle.registration.object.EnumObject;
import java.util.EnumMap;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Supplier;
/**
* Base logic for a deferred register wrapper
* @param <T> Registry type
*/
@SuppressWarnings("WeakerAccess")
public abstract class DeferredRegisterWrapper<T extends IForgeRegistryEntry<T>> {
/** Registry instance, use this to provide register methods */
protected final DeferredRegister<T> register;
/** Mod ID for registration */
private final String modID;
protected DeferredRegisterWrapper(IForgeRegistry<T> reg, String modID) {
register = DeferredRegister.create(reg, modID);
this.modID = modID;
}
/**
* Initializes this registry wrapper. Needs to be called during mod construction
*/
public void register(IEventBus bus) {
register.register(bus);
}
/* Utilities */
/**
* Gets a resource location string for the given name
* @param name Name
* @return Resource location string
*/
protected String resourceName(String name) {
return modID + ":" + name;
}
/* Enum objects */
/**
* Registers an item with multiple variants, prefixing the name with the value name
* @param values Enum values to use for this block
* @param name Name of the block
* @param register Function to register an entry
* @return EnumObject mapping between different block types
*/
@SuppressWarnings("unchecked")
protected static <E extends Enum<E> & IStringSerializable, V extends T, T extends IForgeRegistryEntry<T>> EnumObject<E,V> registerEnum(E[] values, String name, BiFunction<String,E,Supplier<? extends V>> register) {
if (values.length == 0) {
throw new IllegalArgumentException("Must have at least one value");
}
// note this cast only works because you cannot extend an enum
Map<E,Supplier<? extends V>> map = new EnumMap<>((Class<E>)values[0].getClass());
for (E value : values) {
map.put(value, register.apply(value.getString() + "_" + name, value));
}
return new EnumObject<>(map);
}
/**
* Registers an item with multiple variants, suffixing the name with the value name
* @param name Name of the block
* @param values Enum values to use for this block
* @param register Function to register an entry
* @return EnumObject mapping between different block types
*/
@SuppressWarnings("unchecked")
protected static <E extends Enum<E> & IStringSerializable, V extends T, T extends IForgeRegistryEntry<T>> EnumObject<E,V> registerEnum(String name, E[] values, BiFunction<String,E,Supplier<? extends V>> register) {
if (values.length == 0) {
throw new IllegalArgumentException("Must have at least one value");
}
// note this cast only works because you cannot extend an enum
Map<E,Supplier<? extends V>> map = new EnumMap<>((Class<E>)values[0].getClass());
for (E value : values) {
map.put(value, register.apply(name + "_" + value.getString(), value));
}
return new EnumObject<>(map);
}
}
@@ -0,0 +1,59 @@
package slimeknights.mantle.registration.deferred;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.item.Item;
import net.minecraft.item.SpawnEggItem;
import net.minecraftforge.common.util.Lazy;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import slimeknights.mantle.registration.ItemProperties;
import java.util.function.Supplier;
/**
* Deferred register for an entity, building the type from a builder instance and adding an egg
*/
@SuppressWarnings("unused")
public class EntityTypeDeferredRegister extends DeferredRegisterWrapper<EntityType<?>> {
private final DeferredRegister<Item> itemRegistry;
public EntityTypeDeferredRegister(String modID) {
super(ForgeRegistries.ENTITIES, modID);
itemRegistry = DeferredRegister.create(ForgeRegistries.ITEMS, modID);
}
@Override
public void register(IEventBus bus) {
super.register(bus);
itemRegistry.register(bus);
}
/**
* Registers a entity type for the given entity type builder
* @param name Entity name
* @param sup Entity builder instance
* @param <T> Entity class type
* @return Entity registry object
*/
public <T extends Entity> RegistryObject<EntityType<T>> register(String name, Supplier<EntityType.Builder<T>> sup) {
return register.register(name, () -> sup.get().build(resourceName(name)));
}
/**
* Registers a entity type for the given entity type builder, and registers a spawn egg for it
* @param name Entity name
* @param sup Entity builder instance
* @param primary Primary egg color
* @param secondary Secondary egg color
* @param <T> Entity class type
* @return Entity registry object
*/
public <T extends Entity> RegistryObject<EntityType<T>> registerWithEgg(String name, Supplier<EntityType.Builder<T>> sup, int primary, int secondary) {
Lazy<EntityType<T>> lazy = Lazy.of(() -> sup.get().build(resourceName(name)));
itemRegistry.register(name + "_spawn_egg", () -> new SpawnEggItem(lazy.get(), primary, secondary, ItemProperties.EGG_PROPS));
return register.register(name, lazy);
}
}
@@ -0,0 +1,118 @@
package slimeknights.mantle.registration.deferred;
import net.minecraft.block.Block;
import net.minecraft.block.FlowingFluidBlock;
import net.minecraft.block.material.Material;
import net.minecraft.fluid.FlowingFluid;
import net.minecraft.fluid.Fluid;
import net.minecraft.item.BucketItem;
import net.minecraft.item.Item;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fluids.FluidAttributes;
import net.minecraftforge.fluids.ForgeFlowingFluid;
import net.minecraftforge.fluids.ForgeFlowingFluid.Properties;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import slimeknights.mantle.registration.DelayedSupplier;
import slimeknights.mantle.registration.FluidBuilder;
import slimeknights.mantle.registration.ItemProperties;
import slimeknights.mantle.registration.object.FluidObject;
import java.util.function.Function;
import java.util.function.Supplier;
@SuppressWarnings({"unused", "WeakerAccess"})
public class FluidDeferredRegister extends DeferredRegisterWrapper<Fluid> {
private final DeferredRegister<Block> blockRegister;
private final DeferredRegister<Item> itemRegister;
public FluidDeferredRegister(String modID) {
super(ForgeRegistries.FLUIDS, modID);
this.blockRegister = DeferredRegister.create(ForgeRegistries.BLOCKS, modID);
this.itemRegister = DeferredRegister.create(ForgeRegistries.ITEMS, modID);
}
@Override
public void register(IEventBus bus) {
super.register(bus);
blockRegister.register(bus);
itemRegister.register(bus);
}
/**
* Registers a fluid to the registry
* @param name Name of the fluid to register
* @param sup Fluid supplier
* @param <I> Fluid type
* @return Fluid to supply
*/
public <I extends Fluid> RegistryObject<I> registerFluid(final String name, final Supplier<? extends I> sup) {
return register.register(name, sup);
}
/**
* Registers a fluid with still, flowing, block, and bucket
* @param name Fluid name
* @param builder Properties builder
* @param still Function to create still from the properties
* @param flowing Function to create flowing from the properties
* @param block Function to create block from the fluid supplier
* @param <F> Fluid type
* @return Fluid object
*/
public <F extends ForgeFlowingFluid> FluidObject<F> register(String name, FluidBuilder builder, Function<Properties,? extends F> still,
Function<Properties,? extends F> flowing, Function<Supplier<? extends FlowingFluid>,? extends FlowingFluidBlock> block) {
// have to create still and flowing later, as the props need these suppliers
DelayedSupplier<F> stillDelayed = new DelayedSupplier<>();
DelayedSupplier<F> flowingDelayed = new DelayedSupplier<>();
// create block and bucket, they just need a still supplier
RegistryObject<FlowingFluidBlock> blockObj = blockRegister.register(name + "_fluid", () -> block.apply(stillDelayed));
builder.bucket(itemRegister.register(name + "_bucket", () -> new BucketItem(stillDelayed, ItemProperties.BUCKET_PROPS)));
// create props with the suppliers
Properties props = builder.block(blockObj).build(stillDelayed, flowingDelayed);
// create fluids now that we have props
Supplier<F> stillSup = registerFluid(name, () -> still.apply(props));
stillDelayed.setSupplier(stillSup);
Supplier<F> flowingSup = registerFluid("flowing_" + name, () -> flowing.apply(props));
flowingDelayed.setSupplier(flowingSup);
// return the final nice object
return new FluidObject<>(stillSup, flowingSup, blockObj);
}
/**
* Registers a fluid with still, flowing, block, and bucket using the default fluid block
* @param name Fluid name
* @param builder Properties builder
* @param still Function to create still from the properties
* @param flowing Function to create flowing from the properties
* @param material Block material
* @param lightLevel Block light level
* @param <F> Fluid type
* @return Fluid object
*/
public <F extends ForgeFlowingFluid> FluidObject<F> register(String name, FluidAttributes.Builder builder,
Function<Properties,? extends F> still, Function<Properties,? extends F> flowing, Material material, int lightLevel) {
return register(
name, new FluidBuilder(builder).explosionResistance(100f), still, flowing,
(fluid) -> new FlowingFluidBlock(fluid, Block.Properties.create(material).doesNotBlockMovement().hardnessAndResistance(100.0F).noDrops().setLightLevel((state) -> lightLevel))
);
}
/**
* Registers a fluid with generic still, flowing, block, and bucket using the default Forge fluid
* @param name Fluid name
* @param builder Properties builder
* @param material Block material
* @param lightLevel Block light level
* @return Fluid object
*/
public FluidObject<ForgeFlowingFluid> register(String name, FluidAttributes.Builder builder, Material material, int lightLevel) {
return register(name, builder, ForgeFlowingFluid.Source::new, ForgeFlowingFluid.Flowing::new, material, lightLevel);
}
}
@@ -0,0 +1,66 @@
package slimeknights.mantle.registration.deferred;
import net.minecraft.item.Item;
import net.minecraft.util.IStringSerializable;
import net.minecraftforge.registries.ForgeRegistries;
import slimeknights.mantle.registration.object.EnumObject;
import slimeknights.mantle.registration.object.ItemObject;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Deferred register that registers items with wrappers
*/
@SuppressWarnings("unused")
public class ItemDeferredRegister extends DeferredRegisterWrapper<Item> {
public ItemDeferredRegister(String modID) {
super(ForgeRegistries.ITEMS, modID);
}
/**
* Adds a new item to the list to be registered, using the given supplier
* @param name Item name
* @param sup Supplier returning an item
* @return Item registry object
*/
public <I extends Item> ItemObject<I> register(String name, Supplier<? extends I> sup) {
return new ItemObject<>(register.register(name, sup));
}
/**
* Adds a new item to the list to be registered, based on the given item properties
* @param name Item name
* @param props Item properties
* @return Item registry object
*/
public ItemObject<Item> register(String name, Item.Properties props) {
return register(name, () -> new Item(props));
}
/* Specialty */
/**
* Registers an item with multiple variants, prefixing the name with the value name
* @param values Enum values to use for this item
* @param name Name of the block
* @param mapper Function to get a item for the given enum value
* @return EnumObject mapping between different item types
*/
public <T extends Enum<T> & IStringSerializable, I extends Item> EnumObject<T,I> registerEnum(T[] values, String name, Function<T,? extends I> mapper) {
return registerEnum(values, name, (fullName, type) -> register(fullName, () -> mapper.apply(type)));
}
/**
* Registers an item with multiple variants, suffixing the name with the value name
* @param values Enum values to use for this item
* @param name Name of the block
* @param mapper Function to get a item for the given enum value
* @return EnumObject mapping between different item types
*/
public <T extends Enum<T> & IStringSerializable, I extends Item> EnumObject<T,I> registerEnum(String name, T[] values, Function<T,? extends I> mapper) {
return registerEnum(name, values, (fullName, type) -> register(fullName, () -> mapper.apply(type)));
}
}
@@ -0,0 +1,78 @@
package slimeknights.mantle.registration.deferred;
import com.google.common.collect.ImmutableSet;
import com.mojang.datafixers.types.Type;
import net.minecraft.block.Block;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Util;
import net.minecraft.util.datafix.TypeReferences;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.registries.ForgeRegistries;
import slimeknights.mantle.registration.object.EnumObject;
import javax.annotation.Nullable;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* Deferred register to register tile entity instances
*/
public class TileEntityTypeDeferredRegister extends DeferredRegisterWrapper<TileEntityType<?>> {
public TileEntityTypeDeferredRegister(String modID) {
super(ForgeRegistries.TILE_ENTITIES, modID);
}
/**
* Gets the data fixer type for the tile entity instance
* @param name Tile entity name
* @return Data fixer type
*/
@Nullable
private Type<?> getType(String name) {
return Util.attemptDataFix(TypeReferences.BLOCK_ENTITY, resourceName(name));
}
/**
* Registers a tile entity type for a single block
* @param name Tile entity name
* @param factory Tile entity factory
* @param block Single block to add
* @param <T> Tile entity type
* @return Registry object instance
*/
@SuppressWarnings("ConstantConditions")
public <T extends TileEntity> RegistryObject<TileEntityType<T>> register(String name, Supplier<? extends T> factory, Supplier<? extends Block> block) {
return register.register(name, () -> TileEntityType.Builder.<T>create(factory, block.get()).build(getType(name)));
}
/**
* Registers a new tile entity type using a tile entity factory and a block supplier
* @param name Tile entity name
* @param factory Tile entity factory
* @param blocks Enum object
* @param <T> Tile entity type
* @return Tile entity type registry object
*/
@SuppressWarnings("ConstantConditions")
public <T extends TileEntity> RegistryObject<TileEntityType<T>> register(String name, Supplier<? extends T> factory, EnumObject<?, ? extends Block> blocks) {
return register.register(name, () -> new TileEntityType<>(factory, ImmutableSet.copyOf(blocks.values()), getType(name)));
}
/**
* Registers a new tile entity type using a tile entity factory and a block supplier
* @param name Tile entity name
* @param factory Tile entity factory
* @param blockCollector Function to get block list
* @param <T> Tile entity type
* @return Tile entity type registry object
*/
@SuppressWarnings("ConstantConditions")
public <T extends TileEntity> RegistryObject<TileEntityType<T>> register(String name, Supplier<? extends T> factory, Consumer<ImmutableSet.Builder<Block>> blockCollector) {
return register.register(name, () -> {
ImmutableSet.Builder<Block> blocks = new ImmutableSet.Builder<>();
blockCollector.accept(blocks);
return new TileEntityType<>(factory, blocks.build(), getType(name));
});
}
}
@@ -0,0 +1,7 @@
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
package slimeknights.mantle.registration.deferred;
import mcp.MethodsReturnNonnullByDefault;
import javax.annotation.ParametersAreNonnullByDefault;
@@ -0,0 +1,55 @@
package slimeknights.mantle.registration.object;
import lombok.AllArgsConstructor;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.util.IItemProvider;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.registries.IRegistryDelegate;
import java.util.Objects;
import java.util.function.Supplier;
/**
* Registry object that represents a block with an item form, allows fetching either form, and implements {@link IItemProvider}
* @param <B> Block class
*/
@SuppressWarnings("WeakerAccess")
@AllArgsConstructor
public class BlockItemObject<B extends Block> implements Supplier<B>, IItemProvider {
protected final Supplier<? extends B> block;
/**
* Creates a block item object based on a block instance
* @param block Block instance
* @param <B> Return type
* @return Block Item object instance
*/
@SuppressWarnings("unchecked")
public static <B extends Block> BlockItemObject<B> fromBlock(B block) {
IRegistryDelegate<Block> delegate = block.delegate;
return new BlockItemObject<>(() -> (B)delegate.get());
}
/**
* Gets the block for this object
* @return Block
*/
@Override
public B get() {
return block.get();
}
@Override
public Item asItem() {
return block.get().asItem();
}
/**
* Gets the resource location for the given block
* @return Resource location for the given block
*/
public ResourceLocation getRegistryName() {
return Objects.requireNonNull(block.get().getRegistryName());
}
}
@@ -0,0 +1,76 @@
package slimeknights.mantle.registration.object;
import net.minecraft.block.Block;
import net.minecraft.block.SlabBlock;
import net.minecraft.block.StairsBlock;
import net.minecraftforge.registries.IRegistryDelegate;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
/**
* Object containing a block
*/
@SuppressWarnings("WeakerAccess")
public class BuildingBlockObject extends BlockItemObject<Block> {
private final Supplier<? extends SlabBlock> slab;
private final Supplier<? extends StairsBlock> stairs;
/**
* Creates a new object from a set of suppliers
* @param block Base block
* @param slab Slab block
* @param stairs Stairs block
*/
public BuildingBlockObject(Supplier<? extends Block> block, Supplier<? extends SlabBlock> slab, Supplier<? extends StairsBlock> stairs) {
super(block);
this.slab = slab;
this.stairs = stairs;
}
/**
* Creates a new object from another building block object
* @param object Object to copy
*/
protected BuildingBlockObject(BuildingBlockObject object) {
super(object.block);
this.slab = object.slab;
this.stairs = object.stairs;
}
/**
* Creates a building block object from the set of blocks
* @param block Block
* @param slab Slab block
* @param stairs Stairs block
* @return BuildingBlockObject instance
*/
public static BuildingBlockObject fromBlocks(Block block, Block slab, Block stairs) {
IRegistryDelegate<Block> slabDelegate = slab.delegate;
IRegistryDelegate<Block> stairsDelegate = stairs.delegate;
return new BuildingBlockObject(
block.delegate,
() -> (SlabBlock) slabDelegate.get(),
() -> (StairsBlock) stairsDelegate.get()
);
}
/** Gets the slab for this block */
public SlabBlock getSlab() {
return slab.get();
}
/** Gets the stairs for this block */
public StairsBlock getStairs() {
return stairs.get();
}
/**
* Gets an array of the blocks in this object
* @return Array of the blocks in this object
*/
public List<Block> values() {
return Arrays.asList(get(), getSlab(), getStairs());
}
}
@@ -0,0 +1,60 @@
package slimeknights.mantle.registration.object;
import lombok.AllArgsConstructor;
import net.minecraftforge.registries.IForgeRegistryEntry;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;
/**
* Represents an object which is a map of an enum to entry
* @param <T> Enum type
* @param <I> Entry type
*/
@AllArgsConstructor
@SuppressWarnings("WeakerAccess")
public class EnumObject<T extends Enum<T>, I extends IForgeRegistryEntry<? super I>> {
private final Map<T,Supplier<? extends I>> map;
/**
* Gets a entry supplier for the given value
* @param value Value to get
* @return Entry supplier
*/
public Supplier<? extends I> getSupplier(T value) {
return map.get(value);
}
/**
* Gets the value for the given enum
* @param value Value to get
* @return Value, or null if missing
*/
@Nullable
public I get(T value) {
if (!map.containsKey(value)) {
return null;
}
return getSupplier(value).get();
}
/**
* Checks if this enum object contains the given value
* @param value Value to check for
* @return True if the value is contained, false otherwise
*/
public boolean contains(IForgeRegistryEntry<? super I> value) {
return this.map.values().stream().map(Supplier::get).anyMatch(value::equals);
}
/**
* Gets a list of values in this enum object
* @return List of values in the object
*/
public List<I> values() {
return this.map.values().stream().map(Supplier::get).collect(Collectors.toList());
}
}
@@ -0,0 +1,73 @@
package slimeknights.mantle.registration.object;
import net.minecraft.block.Block;
import net.minecraft.block.FenceBlock;
import net.minecraft.block.SlabBlock;
import net.minecraft.block.StairsBlock;
import net.minecraftforge.registries.IRegistryDelegate;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
@SuppressWarnings({"WeakerAccess", "unused"})
public class FenceBuildingBlockObject extends BuildingBlockObject {
private final Supplier<? extends FenceBlock> fence;
/**
* Creates a new building block object with all parameters
* @param block Block object
* @param slab Slab object
* @param stairs Stairs object
* @param fence Fence object
*/
public FenceBuildingBlockObject(Supplier<? extends Block> block, Supplier<? extends SlabBlock> slab,
Supplier<? extends StairsBlock> stairs, Supplier<? extends FenceBlock> fence) {
super(block, slab, stairs);
this.fence = fence;
}
/**
* Creates a new object from a building block object plus a fence
* @param object Previous building block object
* @param fence Fence object
*/
public FenceBuildingBlockObject(BuildingBlockObject object, Supplier<? extends FenceBlock> fence) {
super(object);
this.fence = fence;
}
/**
* Creates a new building block object from the given blocks
* @param object Building block object instance
* @param fence Fence variant
* @return BuildingBlockObject instance
*/
public static FenceBuildingBlockObject fromBlocks(BuildingBlockObject object, Block fence) {
IRegistryDelegate<Block> fenceDelegate = fence.delegate;
return new FenceBuildingBlockObject(object, () -> (FenceBlock) fenceDelegate.get()
);
}
/**
* Creates a new building block object from the given blocks
* @param block Block
* @param slab Slab variant
* @param stairs Stairs variant
* @param fence Fence variant
* @return BuildingBlockObject instance
*/
public static FenceBuildingBlockObject fromBlocks(Block block, Block slab, Block stairs, Block fence) {
return fromBlocks(BuildingBlockObject.fromBlocks(block, slab, stairs), fence);
}
/** Gets the fence for this block */
public FenceBlock getFence() {
return fence.get();
}
@Override
public List<Block> values() {
return Arrays.asList(get(), getSlab(), getStairs(), getFence());
}
}
@@ -0,0 +1,65 @@
package slimeknights.mantle.registration.object;
import lombok.AllArgsConstructor;
import net.minecraft.block.FlowingFluidBlock;
import net.minecraft.item.Item;
import net.minecraft.util.IItemProvider;
import net.minecraftforge.fluids.ForgeFlowingFluid;
import javax.annotation.Nullable;
import java.util.function.Supplier;
/**
* Object containing registry entries for a fluid
* @param <F> Fluid class
*/
@SuppressWarnings("WeakerAccess")
@AllArgsConstructor
public class FluidObject<F extends ForgeFlowingFluid> implements Supplier<F>, IItemProvider {
private final Supplier<? extends F> still;
private final Supplier<? extends F> flowing;
@Nullable
private final Supplier<? extends FlowingFluidBlock> block;
/**
* Gets the still form of this fluid
* @return Still form
*/
public F getStill() {
return still.get();
}
@Override
public F get() {
return getStill();
}
/**
* Gets the flowing form of this fluid
* @return flowing form
*/
public F getFlowing() {
return flowing.get();
}
/**
* Gets the block form of this fluid
* @return Block form
*/
@Nullable
public FlowingFluidBlock getBlock() {
if (block == null) {
return null;
}
return block.get();
}
/**
* Gets the bucket form of this fluid
* @return Bucket form
*/
@Override
public Item asItem() {
return still.get().getFilledBucket();
}
}
@@ -0,0 +1,37 @@
package slimeknights.mantle.registration.object;
import lombok.AllArgsConstructor;
import net.minecraft.item.Item;
import net.minecraft.util.IItemProvider;
import net.minecraft.util.ResourceLocation;
import java.util.Objects;
import java.util.function.Supplier;
/**
* Registry object wrapper to implement {@link IItemProvider}
* @param <I> Item class
*/
@SuppressWarnings("unused")
@AllArgsConstructor
public class ItemObject<I extends Item> implements Supplier<I>, IItemProvider {
private final Supplier<? extends I> item;
@Override
public I get() {
return item.get();
}
@Override
public I asItem() {
return item.get();
}
/**
* Gets the resource location for the given item
* @return Resource location for the given item
*/
public ResourceLocation getRegistryName() {
return Objects.requireNonNull(item.get().getRegistryName());
}
}
@@ -0,0 +1,72 @@
package slimeknights.mantle.registration.object;
import net.minecraft.block.Block;
import net.minecraft.block.SlabBlock;
import net.minecraft.block.StairsBlock;
import net.minecraft.block.WallBlock;
import net.minecraftforge.registries.IRegistryDelegate;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
@SuppressWarnings({"WeakerAccess", "unused"})
public class WallBuildingBlockObject extends BuildingBlockObject {
private final Supplier<? extends WallBlock> wall;
/**
* Creates a new building block object with all parameters
* @param block Block supplier
* @param slab Slab supplier
* @param stairs Stairs supplier
* @param wall Wall supplier
*/
public WallBuildingBlockObject(Supplier<? extends Block> block, Supplier<? extends SlabBlock> slab,
Supplier<? extends StairsBlock> stairs, Supplier<? extends WallBlock> wall) {
super(block, slab, stairs);
this.wall = wall;
}
/**
* Creates a new object from a building block object plus a wall
* @param object Previous building block object
* @param wall Wall object
*/
public WallBuildingBlockObject(BuildingBlockObject object, Supplier<? extends WallBlock> wall) {
super(object);
this.wall = wall;
}
/**
* Creates a new wall building block object from the given blocks
* @param object Building block object
* @param wall Wall object
* @return BuildingBlockObject instance
*/
public static WallBuildingBlockObject fromBlocks(BuildingBlockObject object, Block wall) {
IRegistryDelegate<Block> wallDelegate = wall.delegate;
return new WallBuildingBlockObject(object, () -> (WallBlock) wallDelegate.get());
}
/**
* Creates a new wall building block object from the given blocks
* @param block Block object
* @param slab Slab object
* @param stairs Stairs object
* @param wall Wall object
* @return BuildingBlockObject instance
*/
public static WallBuildingBlockObject fromBlocks(Block block, Block slab, Block stairs, Block wall) {
return fromBlocks(BuildingBlockObject.fromBlocks(block, slab, stairs), wall);
}
/** Gets the wall for this block */
public WallBlock getWall() {
return wall.get();
}
@Override
public List<Block> values() {
return Arrays.asList(get(), getSlab(), getStairs(), getWall());
}
}
@@ -0,0 +1,7 @@
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
package slimeknights.mantle.registration.object;
import mcp.MethodsReturnNonnullByDefault;
import javax.annotation.ParametersAreNonnullByDefault;
@@ -0,0 +1,7 @@
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
package slimeknights.mantle.registration;
import mcp.MethodsReturnNonnullByDefault;
import javax.annotation.ParametersAreNonnullByDefault;