From cbb0a52c18e85fb3beb6dbb33d05d8e0c33b301b Mon Sep 17 00:00:00 2001 From: KnightMiner Date: Tue, 28 Jul 2020 13:25:02 -0500 Subject: [PATCH] Allow an ItemObject name to be fetched before registry events fire Also remove a few constructors provided because of "what if they are needed", as I really doubt anyone will be extending this system. They can make a PR if they need them later --- .../object/BuildingBlockObject.java | 18 ++++++++-- .../object/FenceBuildingBlockObject.java | 17 +--------- .../registration/object/ItemObject.java | 33 ++++++++++++++++++- .../object/WallBuildingBlockObject.java | 17 +--------- 4 files changed, 49 insertions(+), 36 deletions(-) diff --git a/src/main/java/slimeknights/mantle/registration/object/BuildingBlockObject.java b/src/main/java/slimeknights/mantle/registration/object/BuildingBlockObject.java index 54b097c..445ed7b 100644 --- a/src/main/java/slimeknights/mantle/registration/object/BuildingBlockObject.java +++ b/src/main/java/slimeknights/mantle/registration/object/BuildingBlockObject.java @@ -19,19 +19,31 @@ public class BuildingBlockObject extends ItemObject { private final Supplier stairs; /** - * Creates a new object from a set of suppliers + * Standard supplier method, expects the supplier to have resolved in order to call registry name. * @param block Base block * @param slab Slab block * @param stairs Stairs block */ - public BuildingBlockObject(Supplier block, Supplier slab, Supplier stairs) { + protected BuildingBlockObject(Supplier block, Supplier slab, Supplier stairs) { super(block); this.slab = slab; this.stairs = stairs; } /** - * Creates a new object from another building block object + * Creates a new object from a ItemObject. Avoids an extra supplier wrapper + * @param block Base block + * @param slab Slab block + * @param stairs Stairs block + */ + public BuildingBlockObject(ItemObject block, Supplier slab, Supplier stairs) { + super(block); + this.slab = slab; + this.stairs = stairs; + } + + /** + * Creates a new object from another building block object, intended to be used in subclasses to copy properties * @param object Object to copy */ protected BuildingBlockObject(BuildingBlockObject object) { diff --git a/src/main/java/slimeknights/mantle/registration/object/FenceBuildingBlockObject.java b/src/main/java/slimeknights/mantle/registration/object/FenceBuildingBlockObject.java index 80e1297..8ab6558 100644 --- a/src/main/java/slimeknights/mantle/registration/object/FenceBuildingBlockObject.java +++ b/src/main/java/slimeknights/mantle/registration/object/FenceBuildingBlockObject.java @@ -2,8 +2,6 @@ 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 java.util.Arrays; import java.util.List; @@ -16,20 +14,7 @@ public class FenceBuildingBlockObject extends BuildingBlockObject { private final Supplier 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 block, Supplier slab, - Supplier stairs, Supplier fence) { - super(block, slab, stairs); - this.fence = fence; - } - - /** - * Creates a new object from a building block object plus a fence + * Creates a new object from a building block object plus a fence. * @param object Previous building block object * @param fence Fence object */ diff --git a/src/main/java/slimeknights/mantle/registration/object/ItemObject.java b/src/main/java/slimeknights/mantle/registration/object/ItemObject.java index 278efbc..e92c84a 100644 --- a/src/main/java/slimeknights/mantle/registration/object/ItemObject.java +++ b/src/main/java/slimeknights/mantle/registration/object/ItemObject.java @@ -4,8 +4,10 @@ import lombok.AllArgsConstructor; import net.minecraft.item.Item; import net.minecraft.util.IItemProvider; import net.minecraft.util.ResourceLocation; +import net.minecraftforge.fml.RegistryObject; import net.minecraftforge.registries.IForgeRegistryEntry; +import java.util.Objects; import java.util.function.Supplier; /** @@ -17,6 +19,35 @@ import java.util.function.Supplier; public class ItemObject & IItemProvider> implements Supplier, IItemProvider { /** Supplier to the registry entry */ private final Supplier entry; + /** Supplier to the registry name for this entry, allows fetching the name before the entry resolves if registry object is used */ + private final Supplier name; + + /** + * Creates a new item object from a supplier instance. Registry name will be fetched from the supplier entry, so the entry must be present to fetch + * @param supplier Supplier instance + */ + public ItemObject(Supplier supplier) { + this.entry = supplier; + this.name = () -> Objects.requireNonNull(supplier.get().getRegistryName()); + } + + /** + * Creates a new item object using the given registry object. This variant can resolve its name before the registry object entry resolves + * @param object Object base + */ + public ItemObject(RegistryObject object) { + this.entry = object; + this.name = object::getId; + } + + /** + * Creates a new item object using another item object. Intended to be used in a subclass to avoid an extra wrapper + * @param object Object base + */ + protected ItemObject(ItemObject object) { + this.entry = object.entry; + this.name = object.name; + } @Override public I get() { @@ -33,6 +64,6 @@ public class ItemObject & IItemProvider * @return Resource location for the given item */ public ResourceLocation getRegistryName() { - return Objects.requireNonNull(item.get().getRegistryName()); + return name.get(); } } diff --git a/src/main/java/slimeknights/mantle/registration/object/WallBuildingBlockObject.java b/src/main/java/slimeknights/mantle/registration/object/WallBuildingBlockObject.java index e0cca30..32c8114 100644 --- a/src/main/java/slimeknights/mantle/registration/object/WallBuildingBlockObject.java +++ b/src/main/java/slimeknights/mantle/registration/object/WallBuildingBlockObject.java @@ -1,8 +1,6 @@ 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 java.util.Arrays; @@ -16,20 +14,7 @@ public class WallBuildingBlockObject extends BuildingBlockObject { private final Supplier 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 block, Supplier slab, - Supplier stairs, Supplier wall) { - super(block, slab, stairs); - this.wall = wall; - } - - /** - * Creates a new object from a building block object plus a wall + * Creates a new object from a building block object plus a wall. * @param object Previous building block object * @param wall Wall object */