mirror of
https://github.com/FabricMC/fabric.git
synced 2024-11-22 15:47:57 -05:00
Add methods to the FabricBlockEntityTypeBuilder class for adding additional blocks (#1699)
* Add methods to the FabricBlockEntityTypeBuilder class for adding additional blocks * Fix the FabricBlockEntityTypeBuilder class creating a fixed-size initial blocks list * Document the FabricBlockEntityTypeBuilder#addBlock method Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com> * Document the FabricBlockEntityTypeBuilder#addBlocks method Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com> * Add tests for the FabricBlockEntityTypeBuilder class * Fix license violations in the BlockEntityTypeBuilderTest class Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>
This commit is contained in:
parent
91b7aa665b
commit
fe4ddef067
20 changed files with 223 additions and 4 deletions
|
@ -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<T extends BlockEntity> {
|
||||
private final Factory<? extends T> factory;
|
||||
private final Block[] blocks;
|
||||
private final List<Block> blocks;
|
||||
|
||||
private FabricBlockEntityTypeBuilder(Factory<? extends T> factory, Block[] blocks) {
|
||||
private FabricBlockEntityTypeBuilder(Factory<? extends T> factory, List<Block> blocks) {
|
||||
this.factory = factory;
|
||||
this.blocks = blocks;
|
||||
}
|
||||
|
||||
public static <T extends BlockEntity> FabricBlockEntityTypeBuilder<T> create(Factory<? extends T> factory, Block... blocks) {
|
||||
return new FabricBlockEntityTypeBuilder<>(factory, blocks);
|
||||
List<Block> 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<T> 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<T> addBlocks(Block... blocks) {
|
||||
Collections.addAll(this.blocks, blocks);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BlockEntityType<T> build() {
|
||||
|
@ -47,7 +76,7 @@ public final class FabricBlockEntityTypeBuilder<T extends BlockEntity> {
|
|||
}
|
||||
|
||||
public BlockEntityType<T> build(Type<?> type) {
|
||||
return BlockEntityType.Builder.<T>create(factory::create, blocks)
|
||||
return BlockEntityType.Builder.<T>create(factory::create, blocks.toArray(new Block[0]))
|
||||
.build(type);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"variants": {
|
||||
"": {
|
||||
"model": "fabric-object-builder-api-v1-testmod:block/added_betrayal_block"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"variants": {
|
||||
"": {
|
||||
"model": "fabric-object-builder-api-v1-testmod:block/first_multi_betrayal_block"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"variants": {
|
||||
"": {
|
||||
"model": "fabric-object-builder-api-v1-testmod:block/initial_betrayal_block"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"variants": {
|
||||
"": {
|
||||
"model": "fabric-object-builder-api-v1-testmod:block/second_multi_betrayal_block"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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"
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"parent": "minecraft:block/cube_all",
|
||||
"textures": {
|
||||
"all": "fabric-object-builder-api-v1-testmod:block/added_betrayal_block"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"parent": "minecraft:block/cube_all",
|
||||
"textures": {
|
||||
"all": "fabric-object-builder-api-v1-testmod:block/first_multi_betrayal_block"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"parent": "minecraft:block/cube_all",
|
||||
"textures": {
|
||||
"all": "fabric-object-builder-api-v1-testmod:block/initial_betrayal_block"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"parent": "minecraft:block/cube_all",
|
||||
"textures": {
|
||||
"all": "fabric-object-builder-api-v1-testmod:block/second_multi_betrayal_block"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"parent": "fabric-object-builder-api-v1-testmod:block/added_betrayal_block"
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"parent": "fabric-object-builder-api-v1-testmod:block/first_multi_betrayal_block"
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"parent": "fabric-object-builder-api-v1-testmod:block/initial_betrayal_block"
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"parent": "fabric-object-builder-api-v1-testmod:block/second_multi_betrayal_block"
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 107 B |
Binary file not shown.
After Width: | Height: | Size: 107 B |
Binary file not shown.
After Width: | Height: | Size: 104 B |
Binary file not shown.
After Width: | Height: | Size: 104 B |
|
@ -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"
|
||||
|
|
Loading…
Reference in a new issue