diff --git a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/block/entity/FabricBlockEntityTypeBuilder.java b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/block/entity/FabricBlockEntityTypeBuilder.java index dc62f9512..87e9df904 100644 --- a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/block/entity/FabricBlockEntityTypeBuilder.java +++ b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/block/entity/FabricBlockEntityTypeBuilder.java @@ -16,6 +16,10 @@ package net.fabricmc.fabric.api.object.builder.v1.block.entity; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + import com.mojang.datafixers.types.Type; import net.minecraft.block.Block; @@ -31,15 +35,40 @@ import net.minecraft.util.math.BlockPos; */ public final class FabricBlockEntityTypeBuilder { private final Factory factory; - private final Block[] blocks; + private final List blocks; - private FabricBlockEntityTypeBuilder(Factory factory, Block[] blocks) { + private FabricBlockEntityTypeBuilder(Factory factory, List blocks) { this.factory = factory; this.blocks = blocks; } public static FabricBlockEntityTypeBuilder create(Factory factory, Block... blocks) { - return new FabricBlockEntityTypeBuilder<>(factory, blocks); + List blocksList = new ArrayList<>(blocks.length); + Collections.addAll(blocksList, blocks); + + return new FabricBlockEntityTypeBuilder<>(factory, blocksList); + } + + /** + * Adds a supported block for the block entity type. + * + * @param block the supported block + * @return this builder + */ + public FabricBlockEntityTypeBuilder addBlock(Block block) { + this.blocks.add(block); + return this; + } + + /** + * Adds supported blocks for the block entity type. + * + * @param blocks the supported blocks + * @return this builder + */ + public FabricBlockEntityTypeBuilder addBlocks(Block... blocks) { + Collections.addAll(this.blocks, blocks); + return this; } public BlockEntityType build() { @@ -47,7 +76,7 @@ public final class FabricBlockEntityTypeBuilder { } public BlockEntityType build(Type type) { - return BlockEntityType.Builder.create(factory::create, blocks) + return BlockEntityType.Builder.create(factory::create, blocks.toArray(new Block[0])) .build(type); } diff --git a/fabric-object-builder-api-v1/src/testmod/java/net/fabricmc/fabric/test/object/builder/BlockEntityTypeBuilderTest.java b/fabric-object-builder-api-v1/src/testmod/java/net/fabricmc/fabric/test/object/builder/BlockEntityTypeBuilderTest.java new file mode 100644 index 000000000..352b2a08f --- /dev/null +++ b/fabric-object-builder-api-v1/src/testmod/java/net/fabricmc/fabric/test/object/builder/BlockEntityTypeBuilderTest.java @@ -0,0 +1,117 @@ +/* + * 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; + +import net.minecraft.block.AbstractBlock; +import net.minecraft.block.Block; +import net.minecraft.block.BlockEntityProvider; +import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; +import net.minecraft.block.MapColor; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.BlockEntityType; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.BlockItem; +import net.minecraft.item.Item; +import net.minecraft.item.ItemGroup; +import net.minecraft.text.Text; +import net.minecraft.text.TranslatableText; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.util.Identifier; +import net.minecraft.util.hit.BlockHitResult; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.registry.Registry; +import net.minecraft.world.World; + +import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder; + +public class BlockEntityTypeBuilderTest implements ModInitializer { + private static final Identifier INITIAL_BETRAYAL_BLOCK_ID = ObjectBuilderTestConstants.id("initial_betrayal_block"); + private static final Block INITIAL_BETRAYAL_BLOCK = new BetrayalBlock(MapColor.BLUE); + + private static final Identifier ADDED_BETRAYAL_BLOCK_ID = ObjectBuilderTestConstants.id("added_betrayal_block"); + private static final Block ADDED_BETRAYAL_BLOCK = new BetrayalBlock(MapColor.GREEN); + + private static final Identifier FIRST_MULTI_BETRAYAL_BLOCK_ID = ObjectBuilderTestConstants.id("first_multi_betrayal_block"); + private static final Block FIRST_MULTI_BETRAYAL_BLOCK = new BetrayalBlock(MapColor.RED); + + private static final Identifier SECOND_MULTI_BETRAYAL_BLOCK_ID = ObjectBuilderTestConstants.id("second_multi_betrayal_block"); + private static final Block SECOND_MULTI_BETRAYAL_BLOCK = new BetrayalBlock(MapColor.YELLOW); + + private static final Identifier BLOCK_ENTITY_TYPE_ID = ObjectBuilderTestConstants.id("betrayal_block"); + public static final BlockEntityType BLOCK_ENTITY_TYPE = FabricBlockEntityTypeBuilder.create(BetrayalBlockEntity::new, INITIAL_BETRAYAL_BLOCK) + .addBlock(ADDED_BETRAYAL_BLOCK) + .addBlocks(FIRST_MULTI_BETRAYAL_BLOCK, SECOND_MULTI_BETRAYAL_BLOCK) + .build(); + + @Override + public void onInitialize() { + register(INITIAL_BETRAYAL_BLOCK_ID, INITIAL_BETRAYAL_BLOCK); + register(ADDED_BETRAYAL_BLOCK_ID, ADDED_BETRAYAL_BLOCK); + register(FIRST_MULTI_BETRAYAL_BLOCK_ID, FIRST_MULTI_BETRAYAL_BLOCK); + register(SECOND_MULTI_BETRAYAL_BLOCK_ID, SECOND_MULTI_BETRAYAL_BLOCK); + + Registry.register(Registry.BLOCK_ENTITY_TYPE, BLOCK_ENTITY_TYPE_ID, BLOCK_ENTITY_TYPE); + } + + private static void register(Identifier id, Block block) { + Registry.register(Registry.BLOCK, id, block); + + Item item = new BlockItem(block, new Item.Settings().group(ItemGroup.MISC)); + Registry.register(Registry.ITEM, id, item); + } + + private static class BetrayalBlock extends Block implements BlockEntityProvider { + private BetrayalBlock(MapColor color) { + super(AbstractBlock.Settings.copy(Blocks.STONE).mapColor(color)); + } + + @Override + public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { + if (!world.isClient()) { + BlockEntity blockEntity = world.getBlockEntity(pos); + + if (blockEntity == null) { + throw new AssertionError("Missing block entity for betrayal block at " + pos); + } else if (!BLOCK_ENTITY_TYPE.equals(blockEntity.getType())) { + Identifier id = BlockEntityType.getId(blockEntity.getType()); + throw new AssertionError("Incorrect block entity for betrayal block at " + pos + ": " + id); + } + + Text posText = new TranslatableText("chat.coordinates", pos.getX(), pos.getY(), pos.getZ()); + Text message = new TranslatableText("text.fabric-object-builder-api-v1-testmod.block_entity_type_success", posText, BLOCK_ENTITY_TYPE_ID); + + player.sendMessage(message, false); + } + + return ActionResult.SUCCESS; + } + + @Override + public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { + return new BetrayalBlockEntity(pos, state); + } + } + + private static class BetrayalBlockEntity extends BlockEntity { + private BetrayalBlockEntity(BlockPos pos, BlockState state) { + super(BLOCK_ENTITY_TYPE, pos, state); + } + } +} diff --git a/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/blockstates/added_betrayal_block.json b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/blockstates/added_betrayal_block.json new file mode 100644 index 000000000..24490093a --- /dev/null +++ b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/blockstates/added_betrayal_block.json @@ -0,0 +1,7 @@ +{ + "variants": { + "": { + "model": "fabric-object-builder-api-v1-testmod:block/added_betrayal_block" + } + } +} diff --git a/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/blockstates/first_multi_betrayal_block.json b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/blockstates/first_multi_betrayal_block.json new file mode 100644 index 000000000..10eb5394d --- /dev/null +++ b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/blockstates/first_multi_betrayal_block.json @@ -0,0 +1,7 @@ +{ + "variants": { + "": { + "model": "fabric-object-builder-api-v1-testmod:block/first_multi_betrayal_block" + } + } +} diff --git a/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/blockstates/initial_betrayal_block.json b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/blockstates/initial_betrayal_block.json new file mode 100644 index 000000000..80ef9d66a --- /dev/null +++ b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/blockstates/initial_betrayal_block.json @@ -0,0 +1,7 @@ +{ + "variants": { + "": { + "model": "fabric-object-builder-api-v1-testmod:block/initial_betrayal_block" + } + } +} diff --git a/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/blockstates/second_multi_betrayal_block.json b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/blockstates/second_multi_betrayal_block.json new file mode 100644 index 000000000..dfd3d177a --- /dev/null +++ b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/blockstates/second_multi_betrayal_block.json @@ -0,0 +1,7 @@ +{ + "variants": { + "": { + "model": "fabric-object-builder-api-v1-testmod:block/second_multi_betrayal_block" + } + } +} diff --git a/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/lang/en_us.json b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/lang/en_us.json new file mode 100644 index 000000000..e2cf21775 --- /dev/null +++ b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/lang/en_us.json @@ -0,0 +1,7 @@ +{ + "block.fabric-object-builder-api-v1-testmod.added_betrayal_block": "Added Betrayal Block", + "block.fabric-object-builder-api-v1-testmod.first_multi_betrayal_block": "First Multi Betrayal Block", + "block.fabric-object-builder-api-v1-testmod.initial_betrayal_block": "Initial Betrayal Block", + "block.fabric-object-builder-api-v1-testmod.second_multi_betrayal_block": "Second Multi Betrayal Block", + "text.fabric-object-builder-api-v1-testmod.block_entity_type_success": "Betrayal block at %s has correct block entity type: %s" +} diff --git a/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/block/added_betrayal_block.json b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/block/added_betrayal_block.json new file mode 100644 index 000000000..2d8c77731 --- /dev/null +++ b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/block/added_betrayal_block.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:block/cube_all", + "textures": { + "all": "fabric-object-builder-api-v1-testmod:block/added_betrayal_block" + } +} diff --git a/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/block/first_multi_betrayal_block.json b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/block/first_multi_betrayal_block.json new file mode 100644 index 000000000..c944798e4 --- /dev/null +++ b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/block/first_multi_betrayal_block.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:block/cube_all", + "textures": { + "all": "fabric-object-builder-api-v1-testmod:block/first_multi_betrayal_block" + } +} diff --git a/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/block/initial_betrayal_block.json b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/block/initial_betrayal_block.json new file mode 100644 index 000000000..4dbc4adba --- /dev/null +++ b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/block/initial_betrayal_block.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:block/cube_all", + "textures": { + "all": "fabric-object-builder-api-v1-testmod:block/initial_betrayal_block" + } +} diff --git a/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/block/second_multi_betrayal_block.json b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/block/second_multi_betrayal_block.json new file mode 100644 index 000000000..92a475c44 --- /dev/null +++ b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/block/second_multi_betrayal_block.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:block/cube_all", + "textures": { + "all": "fabric-object-builder-api-v1-testmod:block/second_multi_betrayal_block" + } +} diff --git a/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/item/added_betrayal_block.json b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/item/added_betrayal_block.json new file mode 100644 index 000000000..cea465dbc --- /dev/null +++ b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/item/added_betrayal_block.json @@ -0,0 +1,3 @@ +{ + "parent": "fabric-object-builder-api-v1-testmod:block/added_betrayal_block" +} diff --git a/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/item/first_multi_betrayal_block.json b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/item/first_multi_betrayal_block.json new file mode 100644 index 000000000..7dd11b7da --- /dev/null +++ b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/item/first_multi_betrayal_block.json @@ -0,0 +1,3 @@ +{ + "parent": "fabric-object-builder-api-v1-testmod:block/first_multi_betrayal_block" +} diff --git a/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/item/initial_betrayal_block.json b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/item/initial_betrayal_block.json new file mode 100644 index 000000000..03d9b4929 --- /dev/null +++ b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/item/initial_betrayal_block.json @@ -0,0 +1,3 @@ +{ + "parent": "fabric-object-builder-api-v1-testmod:block/initial_betrayal_block" +} diff --git a/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/item/second_multi_betrayal_block.json b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/item/second_multi_betrayal_block.json new file mode 100644 index 000000000..389368cec --- /dev/null +++ b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/models/item/second_multi_betrayal_block.json @@ -0,0 +1,3 @@ +{ + "parent": "fabric-object-builder-api-v1-testmod:block/second_multi_betrayal_block" +} diff --git a/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/textures/block/added_betrayal_block.png b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/textures/block/added_betrayal_block.png new file mode 100644 index 000000000..27f6ec975 Binary files /dev/null and b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/textures/block/added_betrayal_block.png differ diff --git a/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/textures/block/first_multi_betrayal_block.png b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/textures/block/first_multi_betrayal_block.png new file mode 100644 index 000000000..0f0be0ab1 Binary files /dev/null and b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/textures/block/first_multi_betrayal_block.png differ diff --git a/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/textures/block/initial_betrayal_block.png b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/textures/block/initial_betrayal_block.png new file mode 100644 index 000000000..5cc4eae1b Binary files /dev/null and b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/textures/block/initial_betrayal_block.png differ diff --git a/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/textures/block/second_multi_betrayal_block.png b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/textures/block/second_multi_betrayal_block.png new file mode 100644 index 000000000..8743efa3f Binary files /dev/null and b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/textures/block/second_multi_betrayal_block.png differ 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 cd56198f3..6aa22767c 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 @@ -16,11 +16,13 @@ "FabricMC" ], "depends": { + "fabric-resource-loader-v0": "*", "fabric-object-builder-api-v1": "*" }, "description": "Test mod for fabric object builder API v1.", "entrypoints": { "main": [ + "net.fabricmc.fabric.test.object.builder.BlockEntityTypeBuilderTest", "net.fabricmc.fabric.test.object.builder.CriterionRegistryTest::init", "net.fabricmc.fabric.test.object.builder.VillagerTypeTest1", "net.fabricmc.fabric.test.object.builder.VillagerTypeTest2"