Skip to content

Commit 7ce3cdd

Browse files
authored
Add API for using custom blocks/items in TagPrefixes (GregTechCEu#3251)
1 parent a8a0500 commit 7ce3cdd

File tree

8 files changed

+97
-54
lines changed

8 files changed

+97
-54
lines changed

src/main/java/com/gregtechceu/gtceu/api/block/OreBlock.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
public class OreBlock extends MaterialBlock {
2020

21-
public OreBlock(Properties properties, TagPrefix tagPrefix, Material material, boolean registerModel) {
21+
public OreBlock(Properties properties, TagPrefix tagPrefix, Material material) {
2222
super(properties, tagPrefix, material, false);
23-
if (registerModel && GTCEu.isClientSide()) {
23+
if (GTCEu.isClientSide()) {
2424
OreBlockRenderer.create(this);
2525
}
2626
}

src/main/java/com/gregtechceu/gtceu/api/data/tag/TagPrefix.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
import com.gregtechceu.gtceu.api.GTValues;
55
import com.gregtechceu.gtceu.api.addon.AddonFinder;
66
import com.gregtechceu.gtceu.api.addon.IGTAddon;
7+
import com.gregtechceu.gtceu.api.block.MaterialBlock;
8+
import com.gregtechceu.gtceu.api.block.OreBlock;
79
import com.gregtechceu.gtceu.api.data.chemical.material.ItemMaterialData;
810
import com.gregtechceu.gtceu.api.data.chemical.material.Material;
911
import com.gregtechceu.gtceu.api.data.chemical.material.info.MaterialFlags;
1012
import com.gregtechceu.gtceu.api.data.chemical.material.info.MaterialIconType;
1113
import com.gregtechceu.gtceu.api.data.chemical.material.properties.PropertyKey;
1214
import com.gregtechceu.gtceu.api.data.chemical.material.stack.MaterialStack;
15+
import com.gregtechceu.gtceu.api.item.MaterialBlockItem;
16+
import com.gregtechceu.gtceu.api.item.TagPrefixItem;
1317
import com.gregtechceu.gtceu.api.item.tool.GTToolType;
1418
import com.gregtechceu.gtceu.common.data.GTBlocks;
1519
import com.gregtechceu.gtceu.common.data.GTMaterialBlocks;
@@ -30,6 +34,7 @@
3034
import net.minecraft.resources.ResourceLocation;
3135
import net.minecraft.tags.BlockTags;
3236
import net.minecraft.tags.TagKey;
37+
import net.minecraft.world.item.BlockItem;
3338
import net.minecraft.world.item.Item;
3439
import net.minecraft.world.level.ItemLike;
3540
import net.minecraft.world.level.block.Block;
@@ -998,10 +1003,19 @@ public record BlockProperties(Supplier<Supplier<RenderType>> renderType,
9981003
private boolean generateRecycling = false;
9991004
@Setter
10001005
private boolean generateItem;
1006+
@Getter
1007+
@Setter
1008+
private ItemConstructor itemConstructor = TagPrefixItem::new;
10011009
@Setter
10021010
private boolean generateBlock;
10031011
@Getter
10041012
@Setter
1013+
private BlockConstructor blockConstructor = MaterialBlock::new;
1014+
@Getter
1015+
@Setter
1016+
private BlockItemConstructor blockItemConstructor = MaterialBlockItem::new;
1017+
@Getter
1018+
@Setter
10051019
private BlockProperties blockProperties = new BlockProperties(() -> RenderType::translucent,
10061020
UnaryOperator.identity());
10071021

@@ -1055,6 +1069,7 @@ public static TagPrefix oreTagPrefix(String name, TagKey<Block> miningToolTag) {
10551069
.materialIconType(MaterialIconType.ore)
10561070
.miningToolTag(miningToolTag)
10571071
.unificationEnabled(true)
1072+
.blockConstructor(OreBlock::new)
10581073
.generationCondition(hasOreProperty);
10591074
}
10601075

@@ -1320,4 +1335,22 @@ public static Collection<TagPrefix> values() {
13201335
public String toString() {
13211336
return name;
13221337
}
1338+
1339+
@FunctionalInterface
1340+
public interface ItemConstructor {
1341+
1342+
Item create(Item.Properties properties, TagPrefix prefix, Material material);
1343+
}
1344+
1345+
@FunctionalInterface
1346+
public interface BlockConstructor {
1347+
1348+
Block create(Block.Properties properties, TagPrefix prefix, Material material);
1349+
}
1350+
1351+
@FunctionalInterface
1352+
public interface BlockItemConstructor {
1353+
1354+
BlockItem create(Block block, Item.Properties properties, TagPrefix prefix, Material material);
1355+
}
13231356
}

src/main/java/com/gregtechceu/gtceu/api/item/MaterialBlockItem.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import com.gregtechceu.gtceu.api.GTValues;
44
import com.gregtechceu.gtceu.api.block.MaterialBlock;
5+
import com.gregtechceu.gtceu.api.data.chemical.material.Material;
56
import com.gregtechceu.gtceu.api.data.chemical.material.properties.DustProperty;
67
import com.gregtechceu.gtceu.api.data.chemical.material.properties.PropertyKey;
8+
import com.gregtechceu.gtceu.api.data.tag.TagPrefix;
79

810
import com.lowdragmc.lowdraglib.client.renderer.IBlockRendererProvider;
911
import com.lowdragmc.lowdraglib.client.renderer.IItemRendererProvider;
@@ -12,9 +14,9 @@
1214
import net.minecraft.client.color.item.ItemColor;
1315
import net.minecraft.network.chat.Component;
1416
import net.minecraft.world.item.BlockItem;
15-
import net.minecraft.world.item.Item;
1617
import net.minecraft.world.item.ItemStack;
1718
import net.minecraft.world.item.crafting.RecipeType;
19+
import net.minecraft.world.level.block.Block;
1820
import net.minecraftforge.api.distmarker.Dist;
1921
import net.minecraftforge.api.distmarker.OnlyIn;
2022

@@ -23,35 +25,29 @@
2325

2426
public class MaterialBlockItem extends BlockItem implements IItemRendererProvider {
2527

26-
protected MaterialBlockItem(MaterialBlock block, Properties properties) {
27-
super(block, properties);
28-
}
28+
public final TagPrefix tagPrefix;
29+
public final Material material;
2930

30-
public static MaterialBlockItem create(MaterialBlock block, Item.Properties properties) {
31-
return new MaterialBlockItem(block, properties);
31+
public MaterialBlockItem(Block block, Properties properties, TagPrefix tagPrefix, Material material) {
32+
super(block, properties);
33+
this.tagPrefix = tagPrefix;
34+
this.material = material;
3235
}
3336

3437
@Override
3538
public int getBurnTime(ItemStack itemStack, @Nullable RecipeType<?> recipeType) {
3639
return getItemBurnTime();
3740
}
3841

39-
public void onRegister() {}
40-
4142
@Override
4243
@NotNull
4344
public MaterialBlock getBlock() {
4445
return (MaterialBlock) super.getBlock();
4546
}
4647

4748
@OnlyIn(Dist.CLIENT)
48-
public static ItemColor tintColor() {
49-
return (itemStack, index) -> {
50-
if (itemStack.getItem() instanceof MaterialBlockItem materialBlockItem) {
51-
return materialBlockItem.getBlock().material.getLayerARGB(index);
52-
}
53-
return -1;
54-
};
49+
public static ItemColor tintColor(Material material) {
50+
return (itemStack, index) -> material.getLayerARGB(index);
5551
}
5652

5753
@Nullable
@@ -85,10 +81,10 @@ public Component getName(ItemStack stack) {
8581
}
8682

8783
public int getItemBurnTime() {
88-
var material = getBlock().material;
8984
DustProperty property = material.isNull() ? null : material.getProperty(PropertyKey.DUST);
90-
if (property != null)
91-
return (int) (property.getBurnTime() * getBlock().tagPrefix.getMaterialAmount(material) / GTValues.M);
85+
if (property != null) {
86+
return (int) (property.getBurnTime() * tagPrefix.getMaterialAmount(material) / GTValues.M);
87+
}
9288
return -1;
9389
}
9490
}

src/main/java/com/gregtechceu/gtceu/api/item/TagPrefixItem.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,9 @@ public int getBurnTime(ItemStack itemStack, @Nullable RecipeType<?> recipeType)
5151
return getItemBurnTime();
5252
}
5353

54-
public void onRegister() {}
55-
5654
@OnlyIn(Dist.CLIENT)
57-
public static ItemColor tintColor() {
58-
return (itemStack, index) -> {
59-
if (itemStack.getItem() instanceof TagPrefixItem tagPrefixItem) {
60-
return tagPrefixItem.material.getLayerARGB(index);
61-
}
62-
return -1;
63-
};
55+
public static ItemColor tintColor(Material material) {
56+
return (itemStack, index) -> material.getLayerARGB(index);
6457
}
6558

6659
@Override

src/main/java/com/gregtechceu/gtceu/api/registry/registrate/GTRegistrate.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.gregtechceu.gtceu.api.machine.MultiblockMachineDefinition;
1414
import com.gregtechceu.gtceu.api.machine.multiblock.MultiblockControllerMachine;
1515
import com.gregtechceu.gtceu.api.registry.registrate.forge.GTFluidBuilder;
16+
import com.gregtechceu.gtceu.core.mixins.AbstractRegistrateAccessor;
1617
import com.gregtechceu.gtceu.utils.FormattingUtil;
1718

1819
import net.minecraft.MethodsReturnNonnullByDefault;
@@ -28,15 +29,20 @@
2829
import net.minecraft.world.level.block.entity.BlockEntityType;
2930
import net.minecraft.world.level.block.state.BlockBehaviour;
3031
import net.minecraft.world.level.block.state.BlockState;
32+
import net.minecraftforge.data.event.GatherDataEvent;
33+
import net.minecraftforge.eventbus.api.EventPriority;
3134
import net.minecraftforge.eventbus.api.IEventBus;
35+
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
3236
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
37+
import net.minecraftforge.registries.RegisterEvent;
3338
import net.minecraftforge.registries.RegistryObject;
3439

3540
import com.tterrag.registrate.Registrate;
3641
import com.tterrag.registrate.builders.Builder;
3742
import com.tterrag.registrate.builders.ItemBuilder;
3843
import com.tterrag.registrate.builders.NoConfigBuilder;
3944
import com.tterrag.registrate.providers.ProviderType;
45+
import com.tterrag.registrate.util.OneTimeEventReceiver;
4046
import com.tterrag.registrate.util.entry.ItemEntry;
4147
import com.tterrag.registrate.util.entry.RegistryEntry;
4248
import com.tterrag.registrate.util.nullness.NonNullBiConsumer;
@@ -89,7 +95,22 @@ public void registerRegistrate() {
8995
@Override
9096
public Registrate registerEventListeners(IEventBus bus) {
9197
if (!registered.getAndSet(true)) {
92-
return super.registerEventListeners(bus);
98+
// recreate the super method so we can register the event listener with LOW priority.
99+
Consumer<RegisterEvent> onRegister = this::onRegister;
100+
Consumer<RegisterEvent> onRegisterLate = this::onRegisterLate;
101+
bus.addListener(EventPriority.LOW, onRegister);
102+
bus.addListener(EventPriority.LOWEST, onRegisterLate);
103+
104+
// Fired multiple times when ever tabs need contents rebuilt (changing op tab perms for example)
105+
bus.addListener(this::onBuildCreativeModeTabContents);
106+
// Register events fire multiple times, so clean them up on common setup
107+
OneTimeEventReceiver.addModListener(this, FMLCommonSetupEvent.class, $ -> {
108+
OneTimeEventReceiver.unregister(this, onRegister, RegisterEvent.class);
109+
OneTimeEventReceiver.unregister(this, onRegisterLate, RegisterEvent.class);
110+
});
111+
if (((AbstractRegistrateAccessor) this).getDoDatagen().get()) {
112+
OneTimeEventReceiver.addModListener(this, GatherDataEvent.class, this::onData);
113+
}
93114
}
94115
return this;
95116
}

src/main/java/com/gregtechceu/gtceu/common/data/GTMaterialBlocks.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.gregtechceu.gtceu.api.GTCEuAPI;
55
import com.gregtechceu.gtceu.api.block.MaterialBlock;
66
import com.gregtechceu.gtceu.api.block.MaterialPipeBlock;
7-
import com.gregtechceu.gtceu.api.block.OreBlock;
87
import com.gregtechceu.gtceu.api.data.chemical.material.Material;
98
import com.gregtechceu.gtceu.api.data.chemical.material.properties.PropertyKey;
109
import com.gregtechceu.gtceu.api.data.chemical.material.registry.MaterialRegistry;
@@ -20,6 +19,7 @@
2019
import com.gregtechceu.gtceu.utils.FormattingUtil;
2120

2221
import net.minecraft.client.renderer.RenderType;
22+
import net.minecraft.world.level.block.Block;
2323
import net.minecraft.world.level.block.Blocks;
2424

2525
import com.google.common.collect.ImmutableMap;
@@ -31,10 +31,11 @@
3131

3232
import java.util.Map;
3333

34+
@SuppressWarnings("removal")
3435
public class GTMaterialBlocks {
3536

3637
// Reference Table Builders
37-
static ImmutableTable.Builder<TagPrefix, Material, BlockEntry<? extends MaterialBlock>> MATERIAL_BLOCKS_BUILDER = ImmutableTable
38+
static ImmutableTable.Builder<TagPrefix, Material, BlockEntry<? extends Block>> MATERIAL_BLOCKS_BUILDER = ImmutableTable
3839
.builder();
3940
static ImmutableMap.Builder<Material, BlockEntry<SurfaceRockBlock>> SURFACE_ROCK_BLOCKS_BUILDER = ImmutableMap
4041
.builder();
@@ -46,7 +47,7 @@ public class GTMaterialBlocks {
4647
.builder();
4748

4849
// Reference Tables
49-
public static Table<TagPrefix, Material, BlockEntry<? extends MaterialBlock>> MATERIAL_BLOCKS;
50+
public static Table<TagPrefix, Material, BlockEntry<? extends Block>> MATERIAL_BLOCKS;
5051
public static Map<Material, BlockEntry<SurfaceRockBlock>> SURFACE_ROCK_BLOCKS;
5152
public static Table<TagPrefix, Material, BlockEntry<CableBlock>> CABLE_BLOCKS;
5253
public static Table<TagPrefix, Material, BlockEntry<FluidPipeBlock>> FLUID_PIPE_BLOCKS;
@@ -74,7 +75,7 @@ public static void generateMaterialBlocks() {
7475
private static void registerMaterialBlock(TagPrefix tagPrefix, Material material, GTRegistrate registrate) {
7576
MATERIAL_BLOCKS_BUILDER.put(tagPrefix, material, registrate
7677
.block(tagPrefix.idPattern().formatted(material.getName()),
77-
properties -> new MaterialBlock(properties, tagPrefix, material))
78+
properties -> tagPrefix.blockConstructor().create(properties, tagPrefix, material))
7879
.initialProperties(() -> Blocks.IRON_BLOCK)
7980
.properties(p -> tagPrefix.blockProperties().properties().apply(p).noLootTable())
8081
.transform(GTBlocks.unificationBlock(tagPrefix, material))
@@ -83,10 +84,9 @@ private static void registerMaterialBlock(TagPrefix tagPrefix, Material material
8384
.setData(ProviderType.LANG, NonNullBiConsumer.noop())
8485
.setData(ProviderType.LOOT, NonNullBiConsumer.noop())
8586
.color(() -> MaterialBlock::tintedColor)
86-
.item(MaterialBlockItem::create)
87-
.onRegister(MaterialBlockItem::onRegister)
87+
.item((b, p) -> tagPrefix.blockItemConstructor().create(b, p, tagPrefix, material))
8888
.model(NonNullBiConsumer.noop())
89-
.color(() -> MaterialBlockItem::tintColor)
89+
.color(() -> () -> MaterialBlockItem.tintColor(material))
9090
.build()
9191
.register());
9292
}
@@ -119,10 +119,10 @@ private static void registerOreBlock(Material material, GTRegistrate registrate)
119119
typePrefix = FormattingUtil.toLowerCaseUnderscore(oreTag.name) + "_";
120120
}
121121
var entry = registrate.block("%s%s_ore".formatted(typePrefix, material.getName()),
122-
properties -> new OreBlock(properties, oreTag, material, true))
122+
properties -> oreTag.blockConstructor().create(properties, oreTag, material))
123123
.initialProperties(() -> {
124-
if (oreType.stoneType().get().isAir()) { // if the block is not registered (yet), fallback to
125-
// stone
124+
if (oreType.stoneType().get().isAir()) {
125+
// if the block is not registered (yet), fallback to stone
126126
return Blocks.IRON_ORE;
127127
}
128128
return oreType.stoneType().get().getBlock();
@@ -133,10 +133,9 @@ private static void registerOreBlock(Material material, GTRegistrate registrate)
133133
.setData(ProviderType.LANG, NonNullBiConsumer.noop())
134134
.setData(ProviderType.LOOT, NonNullBiConsumer.noop())
135135
.color(() -> MaterialBlock::tintedColor)
136-
.item(MaterialBlockItem::create)
137-
.onRegister(MaterialBlockItem::onRegister)
136+
.item((b, p) -> oreTag.blockItemConstructor().create(b, p, oreTag, material))
138137
.model(NonNullBiConsumer.noop())
139-
.color(() -> MaterialBlockItem::tintColor)
138+
.color(() -> () -> MaterialBlockItem.tintColor(material))
140139
.build()
141140
.register();
142141
MATERIAL_BLOCKS_BUILDER.put(oreTag, material, entry);

src/main/java/com/gregtechceu/gtceu/common/data/GTMaterialItems.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.gregtechceu.gtceu.api.item.tool.GTToolType;
1212
import com.gregtechceu.gtceu.api.registry.registrate.GTRegistrate;
1313

14+
import net.minecraft.world.item.Item;
1415
import net.minecraft.world.item.Items;
1516
import net.minecraft.world.level.ItemLike;
1617

@@ -33,7 +34,7 @@
3334
public class GTMaterialItems {
3435

3536
// Reference Table Builders
36-
static ImmutableTable.Builder<TagPrefix, Material, ItemEntry<TagPrefixItem>> MATERIAL_ITEMS_BUILDER = ImmutableTable
37+
static ImmutableTable.Builder<TagPrefix, Material, ItemEntry<? extends Item>> MATERIAL_ITEMS_BUILDER = ImmutableTable
3738
.builder();
3839

3940
// Reference Maps
@@ -46,8 +47,8 @@ public class GTMaterialItems {
4647
}
4748

4849
// Reference Tables
49-
public static Table<TagPrefix, Material, ItemEntry<TagPrefixItem>> MATERIAL_ITEMS;
50-
public final static Table<Material, GTToolType, ItemProviderEntry<IGTTool>> TOOL_ITEMS = ArrayTable.create(
50+
public static Table<TagPrefix, Material, ItemEntry<? extends Item>> MATERIAL_ITEMS;
51+
public static final Table<Material, GTToolType, ItemProviderEntry<IGTTool>> TOOL_ITEMS = ArrayTable.create(
5152
GTCEuAPI.materialManager.getRegisteredMaterials().stream()
5253
.filter(mat -> mat.hasProperty(PropertyKey.TOOL))
5354
.toList(),
@@ -74,13 +75,12 @@ public static void generateMaterialItems() {
7475
private static void generateMaterialItem(TagPrefix tagPrefix, Material material, GTRegistrate registrate) {
7576
MATERIAL_ITEMS_BUILDER.put(tagPrefix, material, registrate
7677
.item(tagPrefix.idPattern().formatted(material.getName()),
77-
properties -> new TagPrefixItem(properties, tagPrefix, material))
78-
.onRegister(TagPrefixItem::onRegister)
78+
properties -> tagPrefix.itemConstructor().create(properties, tagPrefix, material))
7979
.setData(ProviderType.LANG, NonNullBiConsumer.noop())
8080
.transform(GTItems.unificationItem(tagPrefix, material))
8181
.properties(p -> p.stacksTo(tagPrefix.maxStackSize()))
8282
.model(NonNullBiConsumer.noop())
83-
.color(() -> TagPrefixItem::tintColor)
83+
.color(() -> () -> TagPrefixItem.tintColor(material))
8484
.onRegister(GTItems::cauldronInteraction)
8585
.register());
8686
}
@@ -103,6 +103,7 @@ public static void generateTools() {
103103
}
104104
}
105105

106+
@SuppressWarnings("unchecked")
106107
private static void generateTool(Material material, GTToolType toolType, GTRegistrate registrate) {
107108
var tier = material.getToolTier();
108109
TOOL_ITEMS.put(material, toolType, (ItemProviderEntry<IGTTool>) (ItemProviderEntry<?>) registrate

0 commit comments

Comments
 (0)