Skip to content

Commit

Permalink
improvements to mangrove roots
Browse files Browse the repository at this point in the history
  • Loading branch information
supermassimo committed Aug 4, 2024
1 parent f7b3cf1 commit ef8c6ad
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,14 @@ public int setRadius(LevelAccessor level, BlockPos pos, int radius, @javax.annot
boolean replacingWater = currentState.getFluidState() == Fluids.WATER.getSource(false);
boolean replacingGround = getFamily().isAcceptableSoilForRootSystem(currentState);
boolean setWaterlogged = replacingWater && !replacingGround;
Layer layer = currentState.is(this) ? currentState.getValue(LAYER) : (replacingGround?Layer.COVERED:Layer.EXPOSED);
boolean isFullBlock = radius >= 8;
Layer layer;
if (currentState.is(this)){
layer = currentState.getValue(LAYER);
if (layer == Layer.COVERED && isFullBlock){
layer = Layer.FILLED;
}
} else layer = replacingGround ? Layer.COVERED : Layer.EXPOSED;
level.setBlock(pos, getStateForRadius(radius)
.setValue(LAYER, layer)
.setValue(WATERLOGGED, setWaterlogged),
Expand Down Expand Up @@ -281,15 +288,23 @@ protected boolean canPlace(Player player, Level level, BlockPos clickedPos, Bloc
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult) {
if (!isFullBlock(state)) {
ItemStack handStack = player.getItemInHand(hand);
Block coverBlock = getFamily().getPrimitiveCoveredRoots().orElse(null);
if (coverBlock != null && handStack.getItem() == coverBlock.asItem()){
BlockState newState = state.setValue(LAYER, Layer.COVERED).setValue(WATERLOGGED, false);
if (canPlace(player, level, pos, newState)){
level.setBlock(pos, newState, 3);
if (!player.isCreative()) handStack.shrink(1);
level.playSound(null, pos, coverBlock.getSoundType(state, level, pos, player).getPlaceSound(), SoundSource.BLOCKS, 1f, 0.8f);
return InteractionResult.SUCCESS;
Layer layer = Layer.COVERED;
if (state.getValue(RADIUS) >= 8){
if (state.getValue(LAYER) == Layer.EXPOSED)
layer = Layer.FILLED;
else layer = null;
}
if (layer != null){
ItemStack handStack = player.getItemInHand(hand);
Block coverBlock = getFamily().getPrimitiveCoveredRoots().orElse(null);
if (coverBlock != null && handStack.getItem() == coverBlock.asItem()){
BlockState newState = state.setValue(LAYER, layer).setValue(WATERLOGGED, false);
if (canPlace(player, level, pos, newState)){
level.setBlock(pos, newState, 3);
if (!player.isCreative()) handStack.shrink(1);
level.playSound(null, pos, coverBlock.getSoundType(state, level, pos, player).getPlaceSound(), SoundSource.BLOCKS, 1f, 0.8f);
return InteractionResult.SUCCESS;
}
}
}
}
Expand All @@ -298,7 +313,6 @@ public InteractionResult use(BlockState state, Level level, BlockPos pos, Player

@Override
public boolean onDestroyedByPlayer(BlockState state, Level level, BlockPos pos, Player player, boolean willHarvest, FluidState fluid) {

if (isFullBlock(state)){
level.setBlock(pos, state.setValue(LAYER, Layer.FILLED), level.isClientSide ? 11 : 3);
this.spawnDestroyParticles(level, player, pos, state);
Expand Down Expand Up @@ -409,6 +423,14 @@ public float getHardness(BlockState state, BlockGetter level, BlockPos pos) {
return (float) Math.min(hardness, DTConfigs.MAX_TREE_HARDNESS.get()); // So many youtube let's plays start with "OMG, this is taking so long to break this tree!"
}

@Override
public BlockState getStateForDecay(BlockState state, LevelAccessor level, BlockPos pos) {
boolean waterlogged = state.hasProperty(BlockStateProperties.WATERLOGGED) && state.getValue(BlockStateProperties.WATERLOGGED);
BasicRootsBlock.Layer layer = state.hasProperty(BasicRootsBlock.LAYER) ? state.getValue(BasicRootsBlock.LAYER) : BasicRootsBlock.Layer.EXPOSED;
Block primitive = (layer == BasicRootsBlock.Layer.COVERED && layer.getPrimitive(getFamily()).isPresent()) ? layer.getPrimitive(getFamily()).get() : Blocks.AIR;
return waterlogged ? Blocks.WATER.defaultBlockState() : primitive.defaultBlockState();
}

//////////////////////////////
// ROT
//////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.material.PushReaction;
Expand Down Expand Up @@ -238,6 +239,11 @@ public boolean isPathfindable(BlockState state, BlockGetter level, BlockPos pos,
return false;
}

public BlockState getStateForDecay (BlockState state, LevelAccessor level, BlockPos pos){
boolean waterlogged = state.hasProperty(BlockStateProperties.WATERLOGGED) && state.getValue(BlockStateProperties.WATERLOGGED);
return waterlogged ? Blocks.WATER.defaultBlockState() : Blocks.AIR.defaultBlockState();
}

///////////////////////////////////////////
// RENDERING
///////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ public boolean run(BlockState state, LevelAccessor level, BlockPos pos, @Nullabl
BranchBlock branch = TreeHelper.getBranch(state);

if (branch != null && species.getFamily() == branch.getFamily()) {
boolean waterlogged = state.hasProperty(BlockStateProperties.WATERLOGGED) && state.getValue(BlockStateProperties.WATERLOGGED);

level.setBlock(pos, waterlogged ? Blocks.WATER.defaultBlockState() : Blocks.AIR.defaultBlockState(), 3);//Destroy the branch and notify the client
level.setBlock(pos, branch.getStateForDecay(state, level, pos), 3);//Destroy the branch and notify the client
}

return super.run(state, level, pos, fromDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ public boolean run(BlockState state, LevelAccessor level, BlockPos pos, @Nullabl
BranchBlock branch = TreeHelper.getBranch(state);

if (branch != null) {
boolean waterlogged = state.hasProperty(BlockStateProperties.WATERLOGGED) && state.getValue(BlockStateProperties.WATERLOGGED);
BasicRootsBlock.Layer layer = state.hasProperty(BasicRootsBlock.LAYER) ? state.getValue(BasicRootsBlock.LAYER) : BasicRootsBlock.Layer.EXPOSED;
Block primitive = (layer == BasicRootsBlock.Layer.COVERED && layer.getPrimitive(family).isPresent()) ? layer.getPrimitive(family).get() : Blocks.AIR;

level.setBlock(pos, waterlogged ? Blocks.WATER.defaultBlockState() : primitive.defaultBlockState(), 3);//Destroy the branch and notify the client
level.setBlock(pos, branch.getStateForDecay(state, level, pos), 3);//Destroy the branch and notify the client
}

return super.run(state, level, pos, fromDir);
Expand Down

0 comments on commit ef8c6ad

Please sign in to comment.