[Change] Refactored Plant API

This commit is contained in:
Frank 2022-06-30 17:51:44 +02:00
parent d5cf63427b
commit 1de4db3cde
11 changed files with 116 additions and 127 deletions

View file

@ -14,19 +14,15 @@ import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block; 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.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.IntegerProperty; 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.LootContext;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams; import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape; import net.minecraft.world.phys.shapes.VoxelShape;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import java.util.Collections; import java.util.Collections;
@ -40,18 +36,11 @@ public class BaseCropBlock extends BasePlantBlock {
private final Item drop; private final Item drop;
public BaseCropBlock(Item drop, Block... terrain) { public BaseCropBlock(Item drop, Block... terrain) {
this( this(basePlantSettings().randomTicks(), drop, terrain);
FabricBlockSettings.of(Material.PLANT)
.sound(SoundType.GRASS)
.randomTicks()
.noCollission()
.offsetType(BlockBehaviour.OffsetType.NONE),
drop, terrain
);
} }
public BaseCropBlock(BlockBehaviour.Properties properties, Item drop, Block... terrain) { protected BaseCropBlock(BlockBehaviour.Properties properties, Item drop, Block... terrain) {
super(properties.offsetType(BlockBehaviour.OffsetType.NONE)); super(properties);
this.drop = drop; this.drop = drop;
this.terrain = terrain; this.terrain = terrain;
this.registerDefaultState(defaultBlockState().setValue(AGE, 0)); this.registerDefaultState(defaultBlockState().setValue(AGE, 0));

View file

@ -50,7 +50,7 @@ public abstract class BaseDoublePlantBlock extends BaseBlockNotFull implements R
FabricBlockSettings.of(Material.PLANT) FabricBlockSettings.of(Material.PLANT)
.sound(SoundType.GRASS) .sound(SoundType.GRASS)
.noCollission() .noCollission()
.offsetType(BlockBehaviour.OffsetType.XZ) .offsetType(BlockBehaviour.OffsetType.NONE)
); );
} }
@ -60,7 +60,7 @@ public abstract class BaseDoublePlantBlock extends BaseBlockNotFull implements R
.sound(SoundType.GRASS) .sound(SoundType.GRASS)
.lightLevel((state) -> state.getValue(TOP) ? light : 0) .lightLevel((state) -> state.getValue(TOP) ? light : 0)
.noCollission() .noCollission()
.offsetType(BlockBehaviour.OffsetType.XZ) .offsetType(BlockBehaviour.OffsetType.NONE)
); );
} }

View file

@ -5,6 +5,7 @@ import org.betterx.bclib.client.models.ModelsHelper;
import org.betterx.bclib.client.models.PatternsHelper; import org.betterx.bclib.client.models.PatternsHelper;
import org.betterx.bclib.client.render.BCLRenderLayer; import org.betterx.bclib.client.render.BCLRenderLayer;
import org.betterx.bclib.interfaces.RenderLayerProvider; import org.betterx.bclib.interfaces.RenderLayerProvider;
import org.betterx.bclib.interfaces.SettingsExtender;
import org.betterx.bclib.items.tool.BaseShearsItem; import org.betterx.bclib.items.tool.BaseShearsItem;
import net.minecraft.client.renderer.block.model.BlockModel; 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.List;
import java.util.Optional; import java.util.Optional;
import java.util.function.Function;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
public abstract class BasePlantBlock extends BaseBlockNotFull implements RenderLayerProvider, BonemealableBlock { 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); private static final VoxelShape SHAPE = box(4, 0, 4, 12, 14, 12);
public BasePlantBlock() { public BasePlantBlock() {
this(false, p -> p); this(basePlantSettings());
} }
public BasePlantBlock(int light) { public BasePlantBlock(int light) {
this(light, p -> p); this(basePlantSettings(light));
} }
public BasePlantBlock(int light, Function<Properties, Properties> propMod) { @Deprecated(forRemoval = true)
public BasePlantBlock(int light, SettingsExtender propMod) {
this(false, light, propMod); this(false, light, propMod);
} }
public BasePlantBlock(boolean replaceabled) { public BasePlantBlock(boolean replaceable) {
this(replaceabled, p -> p); this(basePlantSettings(replaceable));
} }
public BasePlantBlock(boolean replaceable, Function<Properties, Properties> propMod) { @Deprecated(forRemoval = true)
this( public BasePlantBlock(boolean replaceable, SettingsExtender propMod) {
propMod.apply(FabricBlockSettings this(replaceable, 0, propMod);
.of(replaceable ? Material.REPLACEABLE_PLANT : Material.PLANT)
.sound(SoundType.GRASS)
.noCollission()
.offsetType(OffsetType.NONE)
)
);
} }
public BasePlantBlock(boolean replaceable, int light) { public BasePlantBlock(boolean replaceable, int light) {
this(replaceable, light, p -> p); this(basePlantSettings(replaceable, light));
} }
public BasePlantBlock(boolean replaceable, int light, Function<Properties, Properties> propMod) { @Deprecated(forRemoval = true)
public BasePlantBlock(boolean replaceable, int light, SettingsExtender propMod) {
this( this(
propMod.apply(FabricBlockSettings propMod.amend(FabricBlockSettings
.of(replaceable ? Material.REPLACEABLE_PLANT : Material.PLANT) .of(replaceable ? Material.REPLACEABLE_PLANT : Material.PLANT)
.luminance(light) .luminance(light)
.sound(SoundType.GRASS) .sound(SoundType.GRASS)
@ -90,12 +113,13 @@ public abstract class BasePlantBlock extends BaseBlockNotFull implements RenderL
); );
} }
public BasePlantBlock(Properties settings) { protected BasePlantBlock(Properties settings) {
super(settings.offsetType(BlockBehaviour.OffsetType.XZ)); super(settings);
} }
protected abstract boolean isTerrain(BlockState state); protected abstract boolean isTerrain(BlockState state);
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) { public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) {
@ -105,8 +129,8 @@ public abstract class BasePlantBlock extends BaseBlockNotFull implements RenderL
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) { public boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) {
BlockState down = world.getBlockState(pos.below()); BlockState down = level.getBlockState(pos.below());
return isTerrain(down); return isTerrain(down);
} }

View file

@ -6,13 +6,9 @@ import net.minecraft.util.RandomSource;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.WorldGenLevel; import net.minecraft.world.level.WorldGenLevel;
import net.minecraft.world.level.block.Block; 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.BlockState;
import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.IntegerProperty; 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; import java.util.function.Function;
@ -23,16 +19,12 @@ public abstract class BasePlantWithAgeBlock extends BasePlantBlock {
this(p -> p); this(p -> p);
} }
@Deprecated(forRemoval = true)
public BasePlantWithAgeBlock(Function<Properties, Properties> propMod) { public BasePlantWithAgeBlock(Function<Properties, Properties> propMod) {
this( this(propMod.apply(basePlantSettings().randomTicks()));
propMod.apply(FabricBlockSettings.of(Material.PLANT)
.sound(SoundType.GRASS)
.randomTicks()
.noCollission())
);
} }
public BasePlantWithAgeBlock(Properties settings) { protected BasePlantWithAgeBlock(Properties settings) {
super(settings); super(settings);
} }

View file

@ -5,32 +5,20 @@ import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.LevelAccessor; import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader; import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.LiquidBlockContainer; 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.block.state.BlockState;
import net.minecraft.world.level.material.Fluid; import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.FluidState; import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids; 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 abstract class BaseUnderwaterWallPlantBlock extends BaseWallPlantBlock implements LiquidBlockContainer {
public BaseUnderwaterWallPlantBlock() { public BaseUnderwaterWallPlantBlock() {
this( this(0);
FabricBlockSettings
.of(Material.WATER_PLANT)
.sound(SoundType.WET_GRASS)
.noCollission()
);
} }
public BaseUnderwaterWallPlantBlock(int light) { public BaseUnderwaterWallPlantBlock(int light) {
this( this(
FabricBlockSettings UnderwaterPlantBlock.baseUnderwaterPlantSettings(light)
.of(Material.WATER_PLANT)
.luminance(light)
.sound(SoundType.WET_GRASS)
.noCollission()
); );
} }
@ -55,7 +43,7 @@ public abstract class BaseUnderwaterWallPlantBlock extends BaseWallPlantBlock im
} }
@Override @Override
public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) { public boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) {
return world.getFluidState(pos).getType() == Fluids.WATER && super.canSurvive(state, world, pos); return level.getFluidState(pos).getType() == Fluids.WATER && super.canSurvive(state, level, pos);
} }
} }

View file

@ -9,16 +9,12 @@ import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.LevelAccessor; import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader; import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.*; 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.BlockState;
import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.DirectionProperty; 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.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape; 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.ImmutableMap;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
@ -34,30 +30,18 @@ public abstract class BaseWallPlantBlock extends BasePlantBlock {
public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING; public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING;
public BaseWallPlantBlock() { public BaseWallPlantBlock() {
this( this(basePlantSettings());
FabricBlockSettings
.of(Material.PLANT)
.sound(SoundType.GRASS)
.noCollission()
.offsetType(BlockBehaviour.OffsetType.NONE)
);
} }
public BaseWallPlantBlock(int light) { public BaseWallPlantBlock(int light) {
this( this(basePlantSettings(light));
FabricBlockSettings
.of(Material.PLANT)
.luminance(light)
.sound(SoundType.GRASS)
.noCollission()
.offsetType(BlockBehaviour.OffsetType.NONE)
);
} }
public BaseWallPlantBlock(Properties settings) { protected BaseWallPlantBlock(Properties settings) {
super(settings.offsetType(BlockBehaviour.OffsetType.NONE)); super(settings.offsetType(OffsetType.NONE));
} }
@Override @Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> stateManager) { protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> stateManager) {
stateManager.add(FACING); stateManager.add(FACING);
@ -69,11 +53,11 @@ public abstract class BaseWallPlantBlock extends BasePlantBlock {
} }
@Override @Override
public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) { public boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) {
Direction direction = state.getValue(FACING); Direction direction = state.getValue(FACING);
BlockPos blockPos = pos.relative(direction.getOpposite()); BlockPos blockPos = pos.relative(direction.getOpposite());
BlockState blockState = world.getBlockState(blockPos); BlockState blockState = level.getBlockState(blockPos);
return isSupport(world, blockPos, blockState, direction); return isSupport(level, blockPos, blockState, direction);
} }
public boolean isSupport(LevelReader world, BlockPos pos, BlockState blockState, Direction direction) { public boolean isSupport(LevelReader world, BlockPos pos, BlockState blockState, Direction direction) {

View file

@ -40,19 +40,45 @@ import java.util.List;
import java.util.function.Function; import java.util.function.Function;
public abstract class UnderwaterPlantBlock extends BaseBlockNotFull implements RenderLayerProvider, BonemealableBlock, LiquidBlockContainer { 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); private static final VoxelShape SHAPE = box(4, 0, 4, 12, 14, 12);
public UnderwaterPlantBlock() { public UnderwaterPlantBlock() {
this(p -> p); this(p -> p);
} }
@Deprecated(forRemoval = true)
public UnderwaterPlantBlock(Function<Properties, Properties> propMod) { public UnderwaterPlantBlock(Function<Properties, Properties> propMod) {
this( this(
propMod.apply(FabricBlockSettings propMod.apply(baseUnderwaterPlantSettings())
.of(Material.WATER_PLANT)
.sound(SoundType.WET_GRASS)
.noCollission()
.offsetType(BlockBehaviour.OffsetType.XZ))
); );
} }
@ -60,14 +86,10 @@ public abstract class UnderwaterPlantBlock extends BaseBlockNotFull implements R
this(light, p -> p); this(light, p -> p);
} }
@Deprecated(forRemoval = true)
public UnderwaterPlantBlock(int light, Function<Properties, Properties> propMod) { public UnderwaterPlantBlock(int light, Function<Properties, Properties> propMod) {
this( this(
propMod.apply(FabricBlockSettings propMod.apply(baseUnderwaterPlantSettings(light))
.of(Material.WATER_PLANT)
.luminance(light)
.sound(SoundType.WET_GRASS)
.noCollission()
.offsetType(BlockBehaviour.OffsetType.XZ))
); );
} }

View file

@ -5,25 +5,15 @@ import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource; import net.minecraft.util.RandomSource;
import net.minecraft.world.level.WorldGenLevel; import net.minecraft.world.level.WorldGenLevel;
import net.minecraft.world.level.block.Block; 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.BlockState;
import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.IntegerProperty; 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 abstract class UnderwaterPlantWithAgeBlock extends UnderwaterPlantBlock {
public static final IntegerProperty AGE = BlockProperties.AGE; public static final IntegerProperty AGE = BlockProperties.AGE;
public UnderwaterPlantWithAgeBlock() { public UnderwaterPlantWithAgeBlock() {
super( super(baseUnderwaterPlantSettings().randomTicks());
FabricBlockSettings
.of(Material.WATER_PLANT)
.sound(SoundType.WET_GRASS)
.randomTicks()
.noCollission()
);
} }
@Override @Override

View file

@ -7,29 +7,18 @@ import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.SoundType; import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState; 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.minecraft.world.level.storage.loot.LootContext;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import java.util.List; import java.util.List;
public abstract class WallMushroomBlock extends BaseWallPlantBlock { public abstract class WallMushroomBlock extends BaseWallPlantBlock {
public WallMushroomBlock(int light) { public WallMushroomBlock(int light) {
this( super(basePlantSettings(light).destroyTime(0.2F).sound(SoundType.WOOD));
FabricBlockSettings
.of(Material.PLANT)
.luminance(light)
.destroyTime(0.2F)
.sound(SoundType.GRASS)
.sound(SoundType.WOOD)
.noCollission()
);
} }
public WallMushroomBlock(BlockBehaviour.Properties properties) { protected WallMushroomBlock(BlockBehaviour.Properties properties) {
super(properties); super(properties);
} }

View file

@ -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);
}

View file

@ -53,11 +53,14 @@ public interface SurvivesOnSpecialGround {
boolean isSurvivable(BlockState state); 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())); 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())); return isSurvivable(world.getBlockState(pos.above()));
} }
default boolean isTerrain(BlockState state) {
return isSurvivable(state);
}
} }