From a551f7a45b71764f32c984bc0227fd4b822ea6d3 Mon Sep 17 00:00:00 2001 From: modmuss Date: Wed, 7 Aug 2024 18:39:15 +0100 Subject: [PATCH] Add an API to add additional supported blocks to block entity types. (#4009) (cherry picked from commit 40875a9333fd6ee06495f5cb275405c84ca8eace) --- .../block/entity/FabricBlockEntityType.java | 12 ++++ .../object/builder/BlockEntityTypeMixin.java | 55 +++++++++++++++++ .../fabric-object-builder-v1.mixins.json | 1 + .../src/main/resources/fabric.mod.json | 1 + .../test/object/builder/TealSignTest.java | 61 ++----------------- .../src/testmod/resources/fabric.mod.json | 9 --- .../builder/client/TealSignClientTest.java | 32 ---------- .../BlockEntityRendererFactoriesAccessor.java | 33 ---------- ...-builder-api-v1-testmod.client.mixins.json | 11 ---- 9 files changed, 75 insertions(+), 140 deletions(-) create mode 100644 fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/mixin/object/builder/BlockEntityTypeMixin.java delete mode 100644 fabric-object-builder-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/object/builder/client/TealSignClientTest.java delete mode 100644 fabric-object-builder-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/object/builder/client/mixin/BlockEntityRendererFactoriesAccessor.java delete mode 100644 fabric-object-builder-api-v1/src/testmodClient/resources/fabric-object-builder-api-v1-testmod.client.mixins.json diff --git a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/block/entity/FabricBlockEntityType.java b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/block/entity/FabricBlockEntityType.java index 7aeba7536..8871bdb9d 100644 --- a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/block/entity/FabricBlockEntityType.java +++ b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/block/entity/FabricBlockEntityType.java @@ -18,13 +18,25 @@ package net.fabricmc.fabric.api.object.builder.v1.block.entity; import com.mojang.datafixers.types.Type; +import net.minecraft.block.Block; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntityType; /** * General-purpose Fabric-provided extensions for {@link BlockEntityType}. + * + *

Note: This interface is automatically implemented on {@link BlockEntityType} via Mixin and interface injection. */ public interface FabricBlockEntityType { + /** + * Adds a block to the list of blocks that this block entity type can be used with. + * + * @param block the {@link Block} to add + */ + default void addSupportedBlock(Block block) { + throw new AssertionError("Implemented in Mixin"); + } + /** * General-purpose Fabric-provided extensions for {@link BlockEntityType.Builder}. * diff --git a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/mixin/object/builder/BlockEntityTypeMixin.java b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/mixin/object/builder/BlockEntityTypeMixin.java new file mode 100644 index 000000000..8eb9a50f4 --- /dev/null +++ b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/mixin/object/builder/BlockEntityTypeMixin.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.mixin.object.builder; + +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +import com.mojang.datafixers.types.Type; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Mutable; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.block.Block; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.BlockEntityType; + +import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityType; + +@Mixin(BlockEntityType.class) +public class BlockEntityTypeMixin implements FabricBlockEntityType { + @Mutable + @Shadow + @Final + private Set blocks; + + @Inject(method = "", at = @At("RETURN")) + private void mutableBlocks(BlockEntityType.BlockEntityFactory factory, Set blocks, Type type, CallbackInfo ci) { + this.blocks = new HashSet<>(this.blocks); + } + + @Override + public void addSupportedBlock(Block block) { + Objects.requireNonNull(block, "block"); + blocks.add(block); + } +} diff --git a/fabric-object-builder-api-v1/src/main/resources/fabric-object-builder-v1.mixins.json b/fabric-object-builder-api-v1/src/main/resources/fabric-object-builder-v1.mixins.json index 22d799f1e..7be4494b8 100644 --- a/fabric-object-builder-api-v1/src/main/resources/fabric-object-builder-v1.mixins.json +++ b/fabric-object-builder-api-v1/src/main/resources/fabric-object-builder-v1.mixins.json @@ -6,6 +6,7 @@ "AbstractBlockAccessor", "AbstractBlockSettingsAccessor", "BlockEntityTypeBuilderMixin", + "BlockEntityTypeMixin", "DefaultAttributeRegistryAccessor", "DefaultAttributeRegistryMixin", "DetectorRailBlockMixin", diff --git a/fabric-object-builder-api-v1/src/main/resources/fabric.mod.json b/fabric-object-builder-api-v1/src/main/resources/fabric.mod.json index 3bee3c5cd..659f12a2c 100644 --- a/fabric-object-builder-api-v1/src/main/resources/fabric.mod.json +++ b/fabric-object-builder-api-v1/src/main/resources/fabric.mod.json @@ -32,6 +32,7 @@ "fabric-api:module-lifecycle": "stable", "loom:injected_interfaces": { "net/minecraft/class_1299\u0024class_1300": ["net/fabricmc/fabric/api/object/builder/v1/entity/FabricEntityType\u0024Builder"], + "net/minecraft/class_2591": ["net/fabricmc/fabric/api/object/builder/v1/block/entity/FabricBlockEntityType"], "net/minecraft/class_2591\u0024class_2592": ["net/fabricmc/fabric/api/object/builder/v1/block/entity/FabricBlockEntityType\u0024Builder"] } } diff --git a/fabric-object-builder-api-v1/src/testmod/java/net/fabricmc/fabric/test/object/builder/TealSignTest.java b/fabric-object-builder-api-v1/src/testmod/java/net/fabricmc/fabric/test/object/builder/TealSignTest.java index 283e6bc18..9fd58c691 100644 --- a/fabric-object-builder-api-v1/src/testmod/java/net/fabricmc/fabric/test/object/builder/TealSignTest.java +++ b/fabric-object-builder-api-v1/src/testmod/java/net/fabricmc/fabric/test/object/builder/TealSignTest.java @@ -18,7 +18,6 @@ package net.fabricmc.fabric.test.object.builder; import net.minecraft.block.AbstractBlock; import net.minecraft.block.BlockSetType; -import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.block.HangingSignBlock; import net.minecraft.block.SignBlock; @@ -26,18 +25,14 @@ import net.minecraft.block.WallHangingSignBlock; import net.minecraft.block.WallSignBlock; import net.minecraft.block.WoodType; import net.minecraft.block.entity.BlockEntityType; -import net.minecraft.block.entity.HangingSignBlockEntity; -import net.minecraft.block.entity.SignBlockEntity; import net.minecraft.item.HangingSignItem; import net.minecraft.item.Item; import net.minecraft.item.SignItem; import net.minecraft.registry.Registries; import net.minecraft.registry.Registry; import net.minecraft.util.Identifier; -import net.minecraft.util.math.BlockPos; import net.fabricmc.api.ModInitializer; -import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder; import net.fabricmc.fabric.api.object.builder.v1.block.type.BlockSetTypeBuilder; import net.fabricmc.fabric.api.object.builder.v1.block.type.WoodTypeBuilder; @@ -45,34 +40,12 @@ public class TealSignTest implements ModInitializer { public static final Identifier TEAL_TYPE_ID = ObjectBuilderTestConstants.id("teal"); public static final BlockSetType TEAL_BLOCK_SET_TYPE = BlockSetTypeBuilder.copyOf(BlockSetType.OAK).build(TEAL_TYPE_ID); public static final WoodType TEAL_WOOD_TYPE = WoodTypeBuilder.copyOf(WoodType.OAK).build(TEAL_TYPE_ID, TEAL_BLOCK_SET_TYPE); - public static final SignBlock TEAL_SIGN = new SignBlock(TEAL_WOOD_TYPE, AbstractBlock.Settings.copy(Blocks.OAK_SIGN)) { - @Override - public TealSign createBlockEntity(BlockPos pos, BlockState state) { - return new TealSign(pos, state); - } - }; - public static final WallSignBlock TEAL_WALL_SIGN = new WallSignBlock(TEAL_WOOD_TYPE, AbstractBlock.Settings.copy(Blocks.OAK_SIGN)) { - @Override - public TealSign createBlockEntity(BlockPos pos, BlockState state) { - return new TealSign(pos, state); - } - }; - public static final HangingSignBlock TEAL_HANGING_SIGN = new HangingSignBlock(TEAL_WOOD_TYPE, AbstractBlock.Settings.copy(Blocks.OAK_HANGING_SIGN)) { - @Override - public TealHangingSign createBlockEntity(BlockPos pos, BlockState state) { - return new TealHangingSign(pos, state); - } - }; - public static final WallHangingSignBlock TEAL_WALL_HANGING_SIGN = new WallHangingSignBlock(TEAL_WOOD_TYPE, AbstractBlock.Settings.copy(Blocks.OAK_HANGING_SIGN)) { - @Override - public TealHangingSign createBlockEntity(BlockPos pos, BlockState state) { - return new TealHangingSign(pos, state); - } - }; + public static final SignBlock TEAL_SIGN = new SignBlock(TEAL_WOOD_TYPE, AbstractBlock.Settings.copy(Blocks.OAK_SIGN)); + public static final WallSignBlock TEAL_WALL_SIGN = new WallSignBlock(TEAL_WOOD_TYPE, AbstractBlock.Settings.copy(Blocks.OAK_SIGN)); + public static final HangingSignBlock TEAL_HANGING_SIGN = new HangingSignBlock(TEAL_WOOD_TYPE, AbstractBlock.Settings.copy(Blocks.OAK_HANGING_SIGN)); + public static final WallHangingSignBlock TEAL_WALL_HANGING_SIGN = new WallHangingSignBlock(TEAL_WOOD_TYPE, AbstractBlock.Settings.copy(Blocks.OAK_HANGING_SIGN)); public static final SignItem TEAL_SIGN_ITEM = new SignItem(new Item.Settings(), TEAL_SIGN, TEAL_WALL_SIGN); public static final HangingSignItem TEAL_HANGING_SIGN_ITEM = new HangingSignItem(TEAL_HANGING_SIGN, TEAL_WALL_HANGING_SIGN, new Item.Settings()); - public static final BlockEntityType TEST_SIGN_BLOCK_ENTITY = FabricBlockEntityTypeBuilder.create(TealSign::new, TEAL_SIGN, TEAL_WALL_SIGN).build(); - public static final BlockEntityType TEST_HANGING_SIGN_BLOCK_ENTITY = FabricBlockEntityTypeBuilder.create(TealHangingSign::new, TEAL_HANGING_SIGN, TEAL_WALL_HANGING_SIGN).build(); @Override public void onInitialize() { @@ -86,29 +59,7 @@ public class TealSignTest implements ModInitializer { Registry.register(Registries.ITEM, ObjectBuilderTestConstants.id("teal_sign"), TEAL_SIGN_ITEM); Registry.register(Registries.ITEM, ObjectBuilderTestConstants.id("teal_hanging_sign"), TEAL_HANGING_SIGN_ITEM); - Registry.register(Registries.BLOCK_ENTITY_TYPE, ObjectBuilderTestConstants.id("teal_sign"), TEST_SIGN_BLOCK_ENTITY); - Registry.register(Registries.BLOCK_ENTITY_TYPE, ObjectBuilderTestConstants.id("teal_hanging_sign"), TEST_HANGING_SIGN_BLOCK_ENTITY); - } - - public static class TealSign extends SignBlockEntity { - public TealSign(BlockPos pos, BlockState state) { - super(pos, state); - } - - @Override - public BlockEntityType getType() { - return TEST_SIGN_BLOCK_ENTITY; - } - } - - public static class TealHangingSign extends HangingSignBlockEntity { - public TealHangingSign(BlockPos pos, BlockState state) { - super(pos, state); - } - - @Override - public BlockEntityType getType() { - return TEST_HANGING_SIGN_BLOCK_ENTITY; - } + BlockEntityType.SIGN.addSupportedBlock(TEAL_SIGN); + BlockEntityType.HANGING_SIGN.addSupportedBlock(TEAL_HANGING_SIGN); } } diff --git a/fabric-object-builder-api-v1/src/testmod/resources/fabric.mod.json b/fabric-object-builder-api-v1/src/testmod/resources/fabric.mod.json index ba9b1e122..83f2d8d7d 100644 --- a/fabric-object-builder-api-v1/src/testmod/resources/fabric.mod.json +++ b/fabric-object-builder-api-v1/src/testmod/resources/fabric.mod.json @@ -20,12 +20,6 @@ "fabric-object-builder-api-v1": "*" }, "description": "Test mod for fabric object builder API v1.", - "mixins": [ - { - "config": "fabric-object-builder-api-v1-testmod.client.mixins.json", - "environment": "client" - } - ], "entrypoints": { "main": [ "net.fabricmc.fabric.test.object.builder.BlockEntityTypeBuilderTest", @@ -35,9 +29,6 @@ "net.fabricmc.fabric.test.object.builder.TealSignTest", "net.fabricmc.fabric.test.object.builder.PersistentStateManagerTest" ], - "client": [ - "net.fabricmc.fabric.test.object.builder.client.TealSignClientTest" - ], "fabric-gametest": [ "net.fabricmc.fabric.test.object.builder.ObjectBuilderGameTest" ] diff --git a/fabric-object-builder-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/object/builder/client/TealSignClientTest.java b/fabric-object-builder-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/object/builder/client/TealSignClientTest.java deleted file mode 100644 index 570126cfb..000000000 --- a/fabric-object-builder-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/object/builder/client/TealSignClientTest.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.test.object.builder.client; - -import net.minecraft.client.render.block.entity.HangingSignBlockEntityRenderer; -import net.minecraft.client.render.block.entity.SignBlockEntityRenderer; - -import net.fabricmc.api.ClientModInitializer; -import net.fabricmc.fabric.test.object.builder.TealSignTest; -import net.fabricmc.fabric.test.object.builder.client.mixin.BlockEntityRendererFactoriesAccessor; - -public class TealSignClientTest implements ClientModInitializer { - @Override - public void onInitializeClient() { - BlockEntityRendererFactoriesAccessor.callRegister(TealSignTest.TEST_SIGN_BLOCK_ENTITY, SignBlockEntityRenderer::new); - BlockEntityRendererFactoriesAccessor.callRegister(TealSignTest.TEST_HANGING_SIGN_BLOCK_ENTITY, HangingSignBlockEntityRenderer::new); - } -} diff --git a/fabric-object-builder-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/object/builder/client/mixin/BlockEntityRendererFactoriesAccessor.java b/fabric-object-builder-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/object/builder/client/mixin/BlockEntityRendererFactoriesAccessor.java deleted file mode 100644 index 0a30dc3e0..000000000 --- a/fabric-object-builder-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/object/builder/client/mixin/BlockEntityRendererFactoriesAccessor.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.test.object.builder.client.mixin; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Invoker; - -import net.minecraft.block.entity.BlockEntity; -import net.minecraft.block.entity.BlockEntityType; -import net.minecraft.client.render.block.entity.BlockEntityRendererFactories; -import net.minecraft.client.render.block.entity.BlockEntityRendererFactory; - -@Mixin(BlockEntityRendererFactories.class) -public interface BlockEntityRendererFactoriesAccessor { - @Invoker - static void callRegister(BlockEntityType type, BlockEntityRendererFactory factory) { - throw new UnsupportedOperationException(); - } -} diff --git a/fabric-object-builder-api-v1/src/testmodClient/resources/fabric-object-builder-api-v1-testmod.client.mixins.json b/fabric-object-builder-api-v1/src/testmodClient/resources/fabric-object-builder-api-v1-testmod.client.mixins.json deleted file mode 100644 index c39b40776..000000000 --- a/fabric-object-builder-api-v1/src/testmodClient/resources/fabric-object-builder-api-v1-testmod.client.mixins.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "required": true, - "package": "net.fabricmc.fabric.test.object.builder.client.mixin", - "compatibilityLevel": "JAVA_17", - "client": [ - "BlockEntityRendererFactoriesAccessor" - ], - "injectors": { - "defaultRequire": 1 - } -}