Lotus generation

This commit is contained in:
paulevsGitch 2020-10-20 21:26:33 +03:00
parent d3c279bf53
commit cac7be60b1
18 changed files with 469 additions and 11 deletions

View file

@ -0,0 +1,50 @@
package ru.betterend.blocks;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Material;
import net.minecraft.block.ShapeContext;
import net.minecraft.sound.BlockSoundGroup;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.EnumProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.BlockMirror;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.world.BlockView;
import ru.betterend.blocks.BlockProperties.TripleShape;
import ru.betterend.blocks.basis.BlockBaseNotFull;
import ru.betterend.util.BlocksHelper;
public class BlockEndLotusLeaf extends BlockBaseNotFull {
public static final EnumProperty<Direction> HORIZONTAL_FACING = Properties.HORIZONTAL_FACING;
public static final EnumProperty<TripleShape> SHAPE = BlockProperties.TRIPLE_SHAPE;
private static final VoxelShape VSHAPE = Block.createCuboidShape(0, 0, 0, 16, 1, 16);
public BlockEndLotusLeaf() {
super(FabricBlockSettings.of(Material.PLANT).sounds(BlockSoundGroup.WET_GRASS));
}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(SHAPE, HORIZONTAL_FACING);
}
@Override
public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, ShapeContext ePos) {
return VSHAPE;
}
@Override
public BlockState rotate(BlockState state, BlockRotation rotation) {
return BlocksHelper.rotateHorizontal(state, rotation, HORIZONTAL_FACING);
}
@Override
public BlockState mirror(BlockState state, BlockMirror mirror) {
return BlocksHelper.mirrorHorizontal(state, mirror, HORIZONTAL_FACING);
}
}

View file

@ -0,0 +1,109 @@
package ru.betterend.blocks;
import java.util.Random;
import net.minecraft.block.BlockState;
import net.minecraft.fluid.Fluids;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockPos.Mutable;
import net.minecraft.util.math.Direction;
import net.minecraft.world.StructureWorldAccess;
import ru.betterend.blocks.BlockProperties.TripleShape;
import ru.betterend.blocks.basis.BlockUnderwaterPlantWithAge;
import ru.betterend.registry.BlockRegistry;
import ru.betterend.util.BlocksHelper;
public class BlockEndLotusSeed extends BlockUnderwaterPlantWithAge {
@Override
public void grow(StructureWorldAccess world, Random random, BlockPos pos) {
if (canGrow(world, pos)) {
BlockState startLeaf = BlockRegistry.END_LOTUS_STEM.getDefaultState().with(BlockEndLotusStem.LEAF, true);
BlockState roots = BlockRegistry.END_LOTUS_STEM.getDefaultState().with(BlockEndLotusStem.SHAPE, TripleShape.BOTTOM).with(BlockEndLotusStem.WATERLOGGED, true);
BlockState stem = BlockRegistry.END_LOTUS_STEM.getDefaultState();
BlockState flower = BlockRegistry.END_LOTUS_FLOWER.getDefaultState();
BlocksHelper.setWithoutUpdate(world, pos, roots);
Mutable bpos = new Mutable().set(pos);
bpos.setY(bpos.getY() + 1);
while (world.getFluidState(bpos).isStill()) {
BlocksHelper.setWithoutUpdate(world, bpos, stem.with(BlockEndLotusStem.WATERLOGGED, true));
bpos.setY(bpos.getY() + 1);
}
int height = random.nextBoolean() ? 0 : random.nextBoolean() ? 1 : random.nextBoolean() ? 1 : -1;
TripleShape shape = (height == 0) ? TripleShape.TOP : TripleShape.MIDDLE;
Direction dir = BlocksHelper.randomHorizontal(random);
BlockPos leafCenter = bpos.toImmutable().offset(dir);
if (hasLeaf(world, leafCenter)) {
generateLeaf(world, leafCenter);
BlocksHelper.setWithoutUpdate(world, bpos, startLeaf.with(BlockEndLotusStem.SHAPE, shape).with(BlockEndLotusStem.FACING, dir));
}
else {
BlocksHelper.setWithoutUpdate(world, bpos, stem.with(BlockEndLotusStem.SHAPE, shape));
}
bpos.setY(bpos.getY() + 1);
for (int i = 1; i <= height; i++) {
if (!world.isAir(bpos)) {
System.out.println("Set incorrect flower!");
bpos.setY(bpos.getY() - 1);
BlocksHelper.setWithoutUpdate(world, bpos, flower);
bpos.setY(bpos.getY() - 1);
stem = world.getBlockState(bpos);
BlocksHelper.setWithoutUpdate(world, bpos, stem.with(BlockEndLotusStem.SHAPE, TripleShape.TOP));
return;
}
BlocksHelper.setWithoutUpdate(world, bpos, stem);
bpos.setY(bpos.getY() + 1);
}
if (!world.isAir(bpos) || height < 0) {
bpos.setY(bpos.getY() - 1);
}
System.out.println("Set flower!");
BlocksHelper.setWithoutUpdate(world, bpos, flower);
bpos.setY(bpos.getY() - 1);
stem = world.getBlockState(bpos);
BlocksHelper.setWithoutUpdate(world, bpos, stem.with(BlockEndLotusStem.SHAPE, TripleShape.TOP));
}
}
private boolean canGrow(StructureWorldAccess world, BlockPos pos) {
Mutable bpos = new Mutable();
bpos.set(pos);
while (world.getBlockState(bpos).getFluidState().getFluid().equals(Fluids.WATER.getStill())) {
bpos.setY(bpos.getY() + 1);
}
return world.isAir(bpos) && world.isAir(bpos.up());
}
private void generateLeaf(StructureWorldAccess world, BlockPos pos) {
Mutable p = new Mutable();
BlockState leaf = BlockRegistry.END_LOTUS_LEAF.getDefaultState();
BlocksHelper.setWithoutUpdate(world, pos, leaf.with(BlockEndLotusLeaf.SHAPE, TripleShape.BOTTOM));
for (Direction move: BlocksHelper.HORIZONTAL) {
BlocksHelper.setWithoutUpdate(world, p.set(pos).move(move), leaf.with(BlockEndLotusLeaf.HORIZONTAL_FACING, move).with(BlockEndLotusLeaf.SHAPE, TripleShape.MIDDLE));
}
for (int i = 0; i < 4; i ++) {
Direction d1 = BlocksHelper.HORIZONTAL[i];
Direction d2 = BlocksHelper.HORIZONTAL[(i + 1) & 3];
BlocksHelper.setWithoutUpdate(world, p.set(pos).move(d1).move(d2), leaf.with(BlockEndLotusLeaf.HORIZONTAL_FACING, d1).with(BlockEndLotusLeaf.SHAPE, TripleShape.TOP));
}
}
private boolean hasLeaf(StructureWorldAccess world, BlockPos pos) {
Mutable p = new Mutable();
p.setY(pos.getY());
int count = 0;
for (int x = -1; x < 2; x ++) {
p.setX(pos.getX() + x);
for (int z = -1; z < 2; z ++) {
p.setZ(pos.getZ() + z);
if (world.isAir(p))
count ++;
}
}
return count == 9;
}
}

View file

@ -13,6 +13,8 @@ import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.EnumProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.BlockMirror;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
@ -20,15 +22,18 @@ import net.minecraft.world.BlockView;
import net.minecraft.world.WorldAccess;
import ru.betterend.blocks.BlockProperties.TripleShape;
import ru.betterend.blocks.basis.BlockBase;
import ru.betterend.util.BlocksHelper;
public class BlockEndLotusStem extends BlockBase implements Waterloggable {
public static final EnumProperty<Direction> FACING = Properties.FACING;
public static final BooleanProperty WATERLOGGED = Properties.WATERLOGGED;
public static final BooleanProperty LEAF = BooleanProperty.of("leaf");
public static final EnumProperty<TripleShape> SHAPE = BlockProperties.TRIPLE_SHAPE;
private static final VoxelShape VSHAPE = Block.createCuboidShape(6, 0, 6, 10, 16, 10);
public BlockEndLotusStem() {
super(FabricBlockSettings.copyOf(Blocks.OAK_PLANKS));
this.setDefaultState(getDefaultState().with(WATERLOGGED, false).with(SHAPE, TripleShape.MIDDLE));
this.setDefaultState(getDefaultState().with(WATERLOGGED, false).with(SHAPE, TripleShape.MIDDLE).with(LEAF, false).with(FACING, Direction.UP));
}
@Override
@ -38,7 +43,7 @@ public class BlockEndLotusStem extends BlockBase implements Waterloggable {
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(WATERLOGGED, SHAPE);
builder.add(FACING, WATERLOGGED, SHAPE, LEAF);
}
@Override
@ -50,9 +55,20 @@ public class BlockEndLotusStem extends BlockBase implements Waterloggable {
public BlockState getPlacementState(ItemPlacementContext ctx) {
WorldAccess worldAccess = ctx.getWorld();
BlockPos blockPos = ctx.getBlockPos();
return this.getDefaultState().with(WATERLOGGED, worldAccess.getFluidState(blockPos).getFluid() == Fluids.WATER);
return this.getDefaultState().with(WATERLOGGED, worldAccess.getFluidState(blockPos).getFluid() == Fluids.WATER).with(FACING, ctx.getSide());
}
@Override
public BlockState rotate(BlockState state, BlockRotation rotation) {
return BlocksHelper.rotateHorizontal(state, rotation, FACING);
}
@Override
public BlockState mirror(BlockState state, BlockMirror mirror) {
return BlocksHelper.mirrorHorizontal(state, mirror, FACING);
}
@Override
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState newState, WorldAccess world, BlockPos pos, BlockPos posFrom) {
if ((Boolean) state.get(WATERLOGGED)) {
world.getFluidTickScheduler().schedule(pos, Fluids.WATER, Fluids.WATER.getTickRate(world));