Skip to content

Commit

Permalink
Waterloggable Papyrus
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Nov 7, 2024
1 parent 6c66e72 commit 6c708c0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/lovetropics/extras/ExtraBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ private static BlockEntry<CustomSeagrassBlock> seagrass(String blockName, @Nulla
public static final BlockEntry<PapyrusStemBlock> PAPYRUS_STEM = REGISTRATE.block("papyrus_stem", PapyrusStemBlock::new)
.initialProperties(() -> Blocks.SUGAR_CANE)
.properties(p -> p.sound(SoundType.WOOD))
.blockstate((ctx, prov) -> prov.getVariantBuilder(ctx.getEntry()).forAllStates(state -> {
.blockstate((ctx, prov) -> prov.getVariantBuilder(ctx.getEntry()).forAllStatesExcept(state -> {
final String type = state.getValue(PapyrusStemBlock.TYPE).getSerializedName();
final String modelName = type + "_" + ctx.getName();
final ResourceLocation texture = prov.modLoc("block/papyrus/" + modelName);
Expand All @@ -852,7 +852,7 @@ private static BlockEntry<CustomSeagrassBlock> seagrass(String blockName, @Nulla
.withExistingParent(modelName, prov.modLoc("block/papyrus_stem"))
.texture("all", texture))
.build();
}))
}, PapyrusStemBlock.WATERLOGGED))
.addLayer(() -> RenderType::cutout)
.item()
.model((ctx, prov) -> prov.generated(ctx, prov.modLoc("block/papyrus/plain_papyrus_stem")))
Expand All @@ -862,7 +862,7 @@ private static BlockEntry<CustomSeagrassBlock> seagrass(String blockName, @Nulla
public static final BlockEntry<PapyrusUmbelBlock> PAPYRUS_UMBEL = REGISTRATE.block("papyrus_umbel", PapyrusUmbelBlock::new)
.initialProperties(() -> Blocks.SUGAR_CANE)
.properties(p -> p.sound(SoundType.FLOWERING_AZALEA))
.blockstate((ctx, prov) -> prov.getVariantBuilder(ctx.getEntry()).forAllStates(state -> {
.blockstate((ctx, prov) -> prov.getVariantBuilder(ctx.getEntry()).forAllStatesExcept(state -> {
final String type = state.getValue(PapyrusStemBlock.TYPE).getSerializedName();
final String modelName = type + "_" + ctx.getName();
final ResourceLocation texture = prov.modLoc("block/papyrus/" + modelName);
Expand All @@ -871,7 +871,7 @@ private static BlockEntry<CustomSeagrassBlock> seagrass(String blockName, @Nulla
.withExistingParent(modelName, prov.mcLoc("block/sugar_cane"))
.texture("cross", texture))
.build();
}))
}, PapyrusUmbelBlock.AGEING))
.addLayer(() -> RenderType::cutout)
.item()
.model((ctx, prov) -> prov.generated(ctx, prov.modLoc("block/papyrus/plain_papyrus_umbel")))
Expand Down
34 changes: 21 additions & 13 deletions src/main/java/com/lovetropics/extras/block/PapyrusStemBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,27 @@
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.BonemealableBlock;
import net.minecraft.world.level.block.SimpleWaterloggedBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;

public final class PapyrusStemBlock extends Block implements BonemealableBlock {


public final class PapyrusStemBlock extends Block implements SimpleWaterloggedBlock, BonemealableBlock {
public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
public static final EnumProperty<Type> TYPE = EnumProperty.create("type", Type.class);
private static final VoxelShape SHAPE = Block.box(7.0, 0.0, 7.0, 9.0, 16.0, 9.0);
protected static final int MAX_HEIGHT = 4;
private static final int MIN_HEIGHT_FOR_FLOWERS = 2;

public PapyrusStemBlock(final Properties properties) {
super(properties);
registerDefaultState(getStateDefinition().any().setValue(TYPE, Type.PLAIN));
super(properties.randomTicks());
registerDefaultState(getStateDefinition().any().setValue(TYPE, Type.PLAIN).setValue(WATERLOGGED, false));
}

@Override
Expand All @@ -40,13 +44,8 @@ public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, Co

@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
return defaultBlockState();
}

@Override
protected boolean isRandomlyTicking(final BlockState state) {
//TODO can stop ticking if there is a flower on top?
return true;
FluidState fluid = context.getLevel().getFluidState(context.getClickedPos());
return defaultBlockState().setValue(WATERLOGGED, fluid.getType() == Fluids.WATER);
}

@Override
Expand All @@ -72,6 +71,7 @@ protected void randomTick(final BlockState state, final ServerLevel level, final
}

private boolean canGrow(final ServerLevel level, final BlockPos pos) {
// Don't grow naturally if there is water above us
boolean aboveIsEmpty = level.isEmptyBlock(pos.above());
boolean hasReachedMaxHeight = hasStemHeightInclusive(level, pos, MAX_HEIGHT);

Expand Down Expand Up @@ -99,6 +99,9 @@ protected BlockState updateShape(BlockState state, Direction direction, BlockSta
if (!state.canSurvive(level, pos)) {
level.scheduleTick(pos, this, 1);
}
if (state.getValue(WATERLOGGED)) {
level.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
}
return super.updateShape(state, direction, neighborState, level, pos, neighborPos);
}

Expand All @@ -111,7 +114,7 @@ protected void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSou

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(TYPE);
builder.add(TYPE, WATERLOGGED);
}

@Override
Expand All @@ -132,6 +135,11 @@ public boolean isBonemealSuccess(final Level level, final RandomSource randomSou
return true;
}

@Override
public FluidState getFluidState(BlockState state) {
return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
}

public enum Type implements StringRepresentable {
PLAIN("plain"),
DRY("dry"),
Expand Down

0 comments on commit 6c708c0

Please sign in to comment.