From f7955e60f0765b75a09f24cff731abb8f1ad919f Mon Sep 17 00:00:00 2001 From: Frank Bauer Date: Sat, 17 Jul 2021 15:12:23 +0200 Subject: [PATCH] Propsal for property augmentation --- src/main/java/ru/bclib/blocks/BaseBlock.java | 52 +++++++++++++++++-- .../java/ru/bclib/blocks/BaseLeavesBlock.java | 34 ++++++++++-- 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/src/main/java/ru/bclib/blocks/BaseBlock.java b/src/main/java/ru/bclib/blocks/BaseBlock.java index bfb52a10..e9e663bd 100644 --- a/src/main/java/ru/bclib/blocks/BaseBlock.java +++ b/src/main/java/ru/bclib/blocks/BaseBlock.java @@ -1,29 +1,71 @@ package ru.bclib.blocks; +import java.util.Collections; +import java.util.List; +import java.util.function.Consumer; + +import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.minecraft.client.renderer.block.model.BlockModel; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.material.MaterialColor; import net.minecraft.world.level.storage.loot.LootContext; import ru.bclib.client.models.BlockModelProvider; -import java.util.Collections; -import java.util.List; - +/** + * Base class for a default Block. + * + * This Block-Type will: + * + */ public class BaseBlock extends Block implements BlockModelProvider { + /** + * Creates a new Block with the passed properties + * @param settings The properties of the Block. + */ public BaseBlock(Properties settings) { super(settings); } - + + /** + * {@inheritDoc} + * + * This implementation will drop the Block itself + */ @Override @SuppressWarnings("deprecation") public List getDrops(BlockState state, LootContext.Builder builder) { return Collections.singletonList(new ItemStack(this)); } - + + /** + * {@inheritDoc} + * + * This implementation will load the Block-Model and return it as the Item-Model + */ @Override public BlockModel getItemModel(ResourceLocation blockId) { return getBlockModel(blockId, defaultBlockState()); } + + /** + * This method is used internally. + * + * It is called from Block-Contructors, to allow the augmentation of the blocks + * preset properties. + * + * For example in {@link BaseLeavesBlock#BaseLeavesBlock(Block, MaterialColor, Consumer)} + * @param customizeProperties A {@link Consumer} to call with the preset properties + * @param settings The properties as created by the Block + * @return The reconfigured {@code settings} + */ + static FabricBlockSettings acceptAndReturn(Consumer customizeProperties, FabricBlockSettings settings){ + customizeProperties.accept(settings); + return settings; + } } \ No newline at end of file diff --git a/src/main/java/ru/bclib/blocks/BaseLeavesBlock.java b/src/main/java/ru/bclib/blocks/BaseLeavesBlock.java index e77d96c5..c6386ef7 100644 --- a/src/main/java/ru/bclib/blocks/BaseLeavesBlock.java +++ b/src/main/java/ru/bclib/blocks/BaseLeavesBlock.java @@ -1,6 +1,11 @@ package ru.bclib.blocks; +import java.util.Collections; +import java.util.List; +import java.util.function.Consumer; + import com.google.common.collect.Lists; + import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags; import net.minecraft.client.renderer.block.model.BlockModel; @@ -20,19 +25,38 @@ import ru.bclib.client.render.BCLRenderLayer; import ru.bclib.interfaces.IRenderTyped; import ru.bclib.util.MHelper; -import java.util.Collections; -import java.util.List; - public class BaseLeavesBlock extends LeavesBlock implements BlockModelProvider, IRenderTyped { private final Block sapling; + + private static FabricBlockSettings makeLeaves(MaterialColor color){ + return FabricBlockSettings + .copyOf(Blocks.OAK_LEAVES) + .mapColor(color) + .breakByTool(FabricToolTags.HOES) + .breakByTool(FabricToolTags.SHEARS) + .breakByHand(true) + .allowsSpawning((state, world, pos, type) -> false) + .suffocates((state, world, pos) -> false) + .blockVision((state, world, pos) -> false); + } + + public BaseLeavesBlock(Block sapling, MaterialColor color, Consumer customizeProperties) { + super(BaseBlock.acceptAndReturn(customizeProperties, makeLeaves(color))); + this.sapling = sapling; + } + + public BaseLeavesBlock(Block sapling, MaterialColor color, int light, Consumer customizeProperties) { + super(BaseBlock.acceptAndReturn(customizeProperties, makeLeaves(color).luminance(light))); + this.sapling = sapling; + } public BaseLeavesBlock(Block sapling, MaterialColor color) { - super(FabricBlockSettings.copyOf(Blocks.OAK_LEAVES).mapColor(color).breakByTool(FabricToolTags.HOES).breakByTool(FabricToolTags.SHEARS).breakByHand(true).isValidSpawn((state, world, pos, type) -> false).isSuffocating((state, world, pos) -> false).isViewBlocking((state, world, pos) -> false)); + super(makeLeaves(color)); this.sapling = sapling; } public BaseLeavesBlock(Block sapling, MaterialColor color, int light) { - super(FabricBlockSettings.copyOf(Blocks.OAK_LEAVES).mapColor(color).luminance(light).breakByTool(FabricToolTags.HOES).breakByTool(FabricToolTags.SHEARS).isValidSpawn((state, world, pos, type) -> false).isSuffocating((state, world, pos) -> false).isViewBlocking((state, world, pos) -> false)); + super(makeLeaves(color).lightLevel(light)); this.sapling = sapling; }