From 1de4db3cdeda49c78e93a10aba520456cb7f1db1 Mon Sep 17 00:00:00 2001 From: Frank Date: Thu, 30 Jun 2022 17:51:44 +0200 Subject: [PATCH] [Change] Refactored Plant API --- .../betterx/bclib/blocks/BaseCropBlock.java | 17 +---- .../bclib/blocks/BaseDoublePlantBlock.java | 4 +- .../betterx/bclib/blocks/BasePlantBlock.java | 68 +++++++++++++------ .../bclib/blocks/BasePlantWithAgeBlock.java | 14 +--- .../blocks/BaseUnderwaterWallPlantBlock.java | 22 ++---- .../bclib/blocks/BaseWallPlantBlock.java | 32 +++------ .../bclib/blocks/UnderwaterPlantBlock.java | 44 +++++++++--- .../blocks/UnderwaterPlantWithAgeBlock.java | 12 +--- .../bclib/blocks/WallMushroomBlock.java | 15 +--- .../bclib/interfaces/SettingsExtender.java | 8 +++ .../interfaces/SurvivesOnSpecialGround.java | 7 +- 11 files changed, 116 insertions(+), 127 deletions(-) create mode 100644 src/main/java/org/betterx/bclib/interfaces/SettingsExtender.java diff --git a/src/main/java/org/betterx/bclib/blocks/BaseCropBlock.java b/src/main/java/org/betterx/bclib/blocks/BaseCropBlock.java index 89779604..93803fa0 100644 --- a/src/main/java/org/betterx/bclib/blocks/BaseCropBlock.java +++ b/src/main/java/org/betterx/bclib/blocks/BaseCropBlock.java @@ -14,19 +14,15 @@ import net.minecraft.world.item.enchantment.Enchantments; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.SoundType; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.IntegerProperty; -import net.minecraft.world.level.material.Material; import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.parameters.LootContextParams; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.VoxelShape; -import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; - import com.google.common.collect.Lists; import java.util.Collections; @@ -40,18 +36,11 @@ public class BaseCropBlock extends BasePlantBlock { private final Item drop; public BaseCropBlock(Item drop, Block... terrain) { - this( - FabricBlockSettings.of(Material.PLANT) - .sound(SoundType.GRASS) - .randomTicks() - .noCollission() - .offsetType(BlockBehaviour.OffsetType.NONE), - drop, terrain - ); + this(basePlantSettings().randomTicks(), drop, terrain); } - public BaseCropBlock(BlockBehaviour.Properties properties, Item drop, Block... terrain) { - super(properties.offsetType(BlockBehaviour.OffsetType.NONE)); + protected BaseCropBlock(BlockBehaviour.Properties properties, Item drop, Block... terrain) { + super(properties); this.drop = drop; this.terrain = terrain; this.registerDefaultState(defaultBlockState().setValue(AGE, 0)); diff --git a/src/main/java/org/betterx/bclib/blocks/BaseDoublePlantBlock.java b/src/main/java/org/betterx/bclib/blocks/BaseDoublePlantBlock.java index b83f6bc7..8930ec6c 100644 --- a/src/main/java/org/betterx/bclib/blocks/BaseDoublePlantBlock.java +++ b/src/main/java/org/betterx/bclib/blocks/BaseDoublePlantBlock.java @@ -50,7 +50,7 @@ public abstract class BaseDoublePlantBlock extends BaseBlockNotFull implements R FabricBlockSettings.of(Material.PLANT) .sound(SoundType.GRASS) .noCollission() - .offsetType(BlockBehaviour.OffsetType.XZ) + .offsetType(BlockBehaviour.OffsetType.NONE) ); } @@ -60,7 +60,7 @@ public abstract class BaseDoublePlantBlock extends BaseBlockNotFull implements R .sound(SoundType.GRASS) .lightLevel((state) -> state.getValue(TOP) ? light : 0) .noCollission() - .offsetType(BlockBehaviour.OffsetType.XZ) + .offsetType(BlockBehaviour.OffsetType.NONE) ); } diff --git a/src/main/java/org/betterx/bclib/blocks/BasePlantBlock.java b/src/main/java/org/betterx/bclib/blocks/BasePlantBlock.java index 9c6c19dc..c0173ee1 100644 --- a/src/main/java/org/betterx/bclib/blocks/BasePlantBlock.java +++ b/src/main/java/org/betterx/bclib/blocks/BasePlantBlock.java @@ -5,6 +5,7 @@ import org.betterx.bclib.client.models.ModelsHelper; import org.betterx.bclib.client.models.PatternsHelper; import org.betterx.bclib.client.render.BCLRenderLayer; import org.betterx.bclib.interfaces.RenderLayerProvider; +import org.betterx.bclib.interfaces.SettingsExtender; import org.betterx.bclib.items.tool.BaseShearsItem; import net.minecraft.client.renderer.block.model.BlockModel; @@ -41,46 +42,68 @@ import com.google.common.collect.Lists; import java.util.List; import java.util.Optional; -import java.util.function.Function; import org.jetbrains.annotations.Nullable; public abstract class BasePlantBlock extends BaseBlockNotFull implements RenderLayerProvider, BonemealableBlock { + public static Properties basePlantSettings() { + return basePlantSettings(false, 0); + } + + public static Properties basePlantSettings(int light) { + return basePlantSettings(false, light); + } + + public static Properties basePlantSettings(boolean replaceable) { + return basePlantSettings(replaceable, 0); + } + + public static Properties basePlantSettings(boolean replaceable, int light) { + return basePlantSettings(replaceable ? Material.REPLACEABLE_PLANT : Material.PLANT, light); + } + + public static Properties basePlantSettings(Material mat, int light) { + Properties props = FabricBlockSettings + .of(mat) + .sounds(SoundType.GRASS) + .noCollision() + .offsetType(BlockBehaviour.OffsetType.XZ); + if (light > 0) props.lightLevel(s -> light); + return props; + } + private static final VoxelShape SHAPE = box(4, 0, 4, 12, 14, 12); public BasePlantBlock() { - this(false, p -> p); + this(basePlantSettings()); } public BasePlantBlock(int light) { - this(light, p -> p); + this(basePlantSettings(light)); } - public BasePlantBlock(int light, Function propMod) { + @Deprecated(forRemoval = true) + public BasePlantBlock(int light, SettingsExtender propMod) { this(false, light, propMod); } - public BasePlantBlock(boolean replaceabled) { - this(replaceabled, p -> p); + public BasePlantBlock(boolean replaceable) { + this(basePlantSettings(replaceable)); } - public BasePlantBlock(boolean replaceable, Function propMod) { - this( - propMod.apply(FabricBlockSettings - .of(replaceable ? Material.REPLACEABLE_PLANT : Material.PLANT) - .sound(SoundType.GRASS) - .noCollission() - .offsetType(OffsetType.NONE) - ) - ); + @Deprecated(forRemoval = true) + public BasePlantBlock(boolean replaceable, SettingsExtender propMod) { + this(replaceable, 0, propMod); } + public BasePlantBlock(boolean replaceable, int light) { - this(replaceable, light, p -> p); + this(basePlantSettings(replaceable, light)); } - public BasePlantBlock(boolean replaceable, int light, Function propMod) { + @Deprecated(forRemoval = true) + public BasePlantBlock(boolean replaceable, int light, SettingsExtender propMod) { this( - propMod.apply(FabricBlockSettings + propMod.amend(FabricBlockSettings .of(replaceable ? Material.REPLACEABLE_PLANT : Material.PLANT) .luminance(light) .sound(SoundType.GRASS) @@ -90,12 +113,13 @@ public abstract class BasePlantBlock extends BaseBlockNotFull implements RenderL ); } - public BasePlantBlock(Properties settings) { - super(settings.offsetType(BlockBehaviour.OffsetType.XZ)); + protected BasePlantBlock(Properties settings) { + super(settings); } protected abstract boolean isTerrain(BlockState state); + @Override @SuppressWarnings("deprecation") public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) { @@ -105,8 +129,8 @@ public abstract class BasePlantBlock extends BaseBlockNotFull implements RenderL @Override @SuppressWarnings("deprecation") - public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) { - BlockState down = world.getBlockState(pos.below()); + public boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) { + BlockState down = level.getBlockState(pos.below()); return isTerrain(down); } diff --git a/src/main/java/org/betterx/bclib/blocks/BasePlantWithAgeBlock.java b/src/main/java/org/betterx/bclib/blocks/BasePlantWithAgeBlock.java index 249e436e..d1b56fa2 100644 --- a/src/main/java/org/betterx/bclib/blocks/BasePlantWithAgeBlock.java +++ b/src/main/java/org/betterx/bclib/blocks/BasePlantWithAgeBlock.java @@ -6,13 +6,9 @@ import net.minecraft.util.RandomSource; import net.minecraft.world.level.Level; import net.minecraft.world.level.WorldGenLevel; import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.SoundType; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.IntegerProperty; -import net.minecraft.world.level.material.Material; - -import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import java.util.function.Function; @@ -23,16 +19,12 @@ public abstract class BasePlantWithAgeBlock extends BasePlantBlock { this(p -> p); } + @Deprecated(forRemoval = true) public BasePlantWithAgeBlock(Function propMod) { - this( - propMod.apply(FabricBlockSettings.of(Material.PLANT) - .sound(SoundType.GRASS) - .randomTicks() - .noCollission()) - ); + this(propMod.apply(basePlantSettings().randomTicks())); } - public BasePlantWithAgeBlock(Properties settings) { + protected BasePlantWithAgeBlock(Properties settings) { super(settings); } diff --git a/src/main/java/org/betterx/bclib/blocks/BaseUnderwaterWallPlantBlock.java b/src/main/java/org/betterx/bclib/blocks/BaseUnderwaterWallPlantBlock.java index 88892db7..389e9fdc 100644 --- a/src/main/java/org/betterx/bclib/blocks/BaseUnderwaterWallPlantBlock.java +++ b/src/main/java/org/betterx/bclib/blocks/BaseUnderwaterWallPlantBlock.java @@ -5,32 +5,20 @@ import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.LevelAccessor; import net.minecraft.world.level.LevelReader; import net.minecraft.world.level.block.LiquidBlockContainer; -import net.minecraft.world.level.block.SoundType; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.material.Fluid; import net.minecraft.world.level.material.FluidState; import net.minecraft.world.level.material.Fluids; -import net.minecraft.world.level.material.Material; - -import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; public abstract class BaseUnderwaterWallPlantBlock extends BaseWallPlantBlock implements LiquidBlockContainer { + public BaseUnderwaterWallPlantBlock() { - this( - FabricBlockSettings - .of(Material.WATER_PLANT) - .sound(SoundType.WET_GRASS) - .noCollission() - ); + this(0); } public BaseUnderwaterWallPlantBlock(int light) { this( - FabricBlockSettings - .of(Material.WATER_PLANT) - .luminance(light) - .sound(SoundType.WET_GRASS) - .noCollission() + UnderwaterPlantBlock.baseUnderwaterPlantSettings(light) ); } @@ -55,7 +43,7 @@ public abstract class BaseUnderwaterWallPlantBlock extends BaseWallPlantBlock im } @Override - public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) { - return world.getFluidState(pos).getType() == Fluids.WATER && super.canSurvive(state, world, pos); + public boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) { + return level.getFluidState(pos).getType() == Fluids.WATER && super.canSurvive(state, level, pos); } } diff --git a/src/main/java/org/betterx/bclib/blocks/BaseWallPlantBlock.java b/src/main/java/org/betterx/bclib/blocks/BaseWallPlantBlock.java index 3c283949..06bed55b 100644 --- a/src/main/java/org/betterx/bclib/blocks/BaseWallPlantBlock.java +++ b/src/main/java/org/betterx/bclib/blocks/BaseWallPlantBlock.java @@ -9,16 +9,12 @@ import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.LevelAccessor; import net.minecraft.world.level.LevelReader; import net.minecraft.world.level.block.*; -import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.DirectionProperty; -import net.minecraft.world.level.material.Material; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.VoxelShape; -import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; - import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; @@ -34,30 +30,18 @@ public abstract class BaseWallPlantBlock extends BasePlantBlock { public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING; public BaseWallPlantBlock() { - this( - FabricBlockSettings - .of(Material.PLANT) - .sound(SoundType.GRASS) - .noCollission() - .offsetType(BlockBehaviour.OffsetType.NONE) - ); + this(basePlantSettings()); } public BaseWallPlantBlock(int light) { - this( - FabricBlockSettings - .of(Material.PLANT) - .luminance(light) - .sound(SoundType.GRASS) - .noCollission() - .offsetType(BlockBehaviour.OffsetType.NONE) - ); + this(basePlantSettings(light)); } - public BaseWallPlantBlock(Properties settings) { - super(settings.offsetType(BlockBehaviour.OffsetType.NONE)); + protected BaseWallPlantBlock(Properties settings) { + super(settings.offsetType(OffsetType.NONE)); } + @Override protected void createBlockStateDefinition(StateDefinition.Builder stateManager) { stateManager.add(FACING); @@ -69,11 +53,11 @@ public abstract class BaseWallPlantBlock extends BasePlantBlock { } @Override - public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) { + public boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) { Direction direction = state.getValue(FACING); BlockPos blockPos = pos.relative(direction.getOpposite()); - BlockState blockState = world.getBlockState(blockPos); - return isSupport(world, blockPos, blockState, direction); + BlockState blockState = level.getBlockState(blockPos); + return isSupport(level, blockPos, blockState, direction); } public boolean isSupport(LevelReader world, BlockPos pos, BlockState blockState, Direction direction) { diff --git a/src/main/java/org/betterx/bclib/blocks/UnderwaterPlantBlock.java b/src/main/java/org/betterx/bclib/blocks/UnderwaterPlantBlock.java index 067f5ef8..a02eb4cd 100644 --- a/src/main/java/org/betterx/bclib/blocks/UnderwaterPlantBlock.java +++ b/src/main/java/org/betterx/bclib/blocks/UnderwaterPlantBlock.java @@ -40,19 +40,45 @@ import java.util.List; import java.util.function.Function; public abstract class UnderwaterPlantBlock extends BaseBlockNotFull implements RenderLayerProvider, BonemealableBlock, LiquidBlockContainer { + public static Properties baseUnderwaterPlantSettings() { + return baseUnderwaterPlantSettings(false, 0); + } + + public static Properties baseUnderwaterPlantSettings(int light) { + return baseUnderwaterPlantSettings(false, light); + } + + public static Properties baseUnderwaterPlantSettings(boolean replaceable) { + return baseUnderwaterPlantSettings(replaceable, 0); + } + + public static Properties baseUnderwaterPlantSettings(boolean replaceable, int light) { + return baseUnderwaterPlantSettings( + replaceable ? Material.REPLACEABLE_WATER_PLANT : Material.WATER_PLANT, + light + ); + } + + public static Properties baseUnderwaterPlantSettings(Material mat, int light) { + Properties props = FabricBlockSettings + .of(mat) + .sound(SoundType.WET_GRASS) + .noCollission() + .offsetType(BlockBehaviour.OffsetType.XZ); + if (light > 0) props.lightLevel(s -> light); + return props; + } + private static final VoxelShape SHAPE = box(4, 0, 4, 12, 14, 12); public UnderwaterPlantBlock() { this(p -> p); } + @Deprecated(forRemoval = true) public UnderwaterPlantBlock(Function propMod) { this( - propMod.apply(FabricBlockSettings - .of(Material.WATER_PLANT) - .sound(SoundType.WET_GRASS) - .noCollission() - .offsetType(BlockBehaviour.OffsetType.XZ)) + propMod.apply(baseUnderwaterPlantSettings()) ); } @@ -60,14 +86,10 @@ public abstract class UnderwaterPlantBlock extends BaseBlockNotFull implements R this(light, p -> p); } + @Deprecated(forRemoval = true) public UnderwaterPlantBlock(int light, Function propMod) { this( - propMod.apply(FabricBlockSettings - .of(Material.WATER_PLANT) - .luminance(light) - .sound(SoundType.WET_GRASS) - .noCollission() - .offsetType(BlockBehaviour.OffsetType.XZ)) + propMod.apply(baseUnderwaterPlantSettings(light)) ); } diff --git a/src/main/java/org/betterx/bclib/blocks/UnderwaterPlantWithAgeBlock.java b/src/main/java/org/betterx/bclib/blocks/UnderwaterPlantWithAgeBlock.java index 2f2d38d0..56acdc8e 100644 --- a/src/main/java/org/betterx/bclib/blocks/UnderwaterPlantWithAgeBlock.java +++ b/src/main/java/org/betterx/bclib/blocks/UnderwaterPlantWithAgeBlock.java @@ -5,25 +5,15 @@ import net.minecraft.server.level.ServerLevel; import net.minecraft.util.RandomSource; import net.minecraft.world.level.WorldGenLevel; import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.SoundType; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.IntegerProperty; -import net.minecraft.world.level.material.Material; - -import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; public abstract class UnderwaterPlantWithAgeBlock extends UnderwaterPlantBlock { public static final IntegerProperty AGE = BlockProperties.AGE; public UnderwaterPlantWithAgeBlock() { - super( - FabricBlockSettings - .of(Material.WATER_PLANT) - .sound(SoundType.WET_GRASS) - .randomTicks() - .noCollission() - ); + super(baseUnderwaterPlantSettings().randomTicks()); } @Override diff --git a/src/main/java/org/betterx/bclib/blocks/WallMushroomBlock.java b/src/main/java/org/betterx/bclib/blocks/WallMushroomBlock.java index 4675be55..10e5867e 100644 --- a/src/main/java/org/betterx/bclib/blocks/WallMushroomBlock.java +++ b/src/main/java/org/betterx/bclib/blocks/WallMushroomBlock.java @@ -7,29 +7,18 @@ import net.minecraft.world.level.LevelReader; import net.minecraft.world.level.block.SoundType; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.level.material.Material; import net.minecraft.world.level.storage.loot.LootContext; -import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; - import com.google.common.collect.Lists; import java.util.List; public abstract class WallMushroomBlock extends BaseWallPlantBlock { public WallMushroomBlock(int light) { - this( - FabricBlockSettings - .of(Material.PLANT) - .luminance(light) - .destroyTime(0.2F) - .sound(SoundType.GRASS) - .sound(SoundType.WOOD) - .noCollission() - ); + super(basePlantSettings(light).destroyTime(0.2F).sound(SoundType.WOOD)); } - public WallMushroomBlock(BlockBehaviour.Properties properties) { + protected WallMushroomBlock(BlockBehaviour.Properties properties) { super(properties); } diff --git a/src/main/java/org/betterx/bclib/interfaces/SettingsExtender.java b/src/main/java/org/betterx/bclib/interfaces/SettingsExtender.java new file mode 100644 index 00000000..357d106a --- /dev/null +++ b/src/main/java/org/betterx/bclib/interfaces/SettingsExtender.java @@ -0,0 +1,8 @@ +package org.betterx.bclib.interfaces; + +import net.minecraft.world.level.block.state.BlockBehaviour; + +@FunctionalInterface +public interface SettingsExtender { + BlockBehaviour.Properties amend(BlockBehaviour.Properties props); +} diff --git a/src/main/java/org/betterx/bclib/interfaces/SurvivesOnSpecialGround.java b/src/main/java/org/betterx/bclib/interfaces/SurvivesOnSpecialGround.java index 2a52cdc9..04ec5562 100644 --- a/src/main/java/org/betterx/bclib/interfaces/SurvivesOnSpecialGround.java +++ b/src/main/java/org/betterx/bclib/interfaces/SurvivesOnSpecialGround.java @@ -53,11 +53,14 @@ public interface SurvivesOnSpecialGround { boolean isSurvivable(BlockState state); - default boolean canSurviveOnTop(BlockState state, LevelReader world, BlockPos pos) { + default boolean canSurviveOnTop(LevelReader world, BlockPos pos) { return isSurvivable(world.getBlockState(pos.below())); } - default boolean canSurviveOnBottom(BlockState state, LevelReader world, BlockPos pos) { + default boolean canSurviveOnBottom(LevelReader world, BlockPos pos) { return isSurvivable(world.getBlockState(pos.above())); } + default boolean isTerrain(BlockState state) { + return isSurvivable(state); + } } \ No newline at end of file