mirror of
https://github.com/Sollace/Unicopia.git
synced 2025-02-25 14:14:32 +01:00
Fixed fruit tree growth + use bonemeal to grow apples
This commit is contained in:
parent
3a44b9605f
commit
2d2a6e9b32
2 changed files with 106 additions and 39 deletions
|
@ -24,9 +24,9 @@ import net.minecraft.util.math.random.Random;
|
||||||
import net.minecraft.world.*;
|
import net.minecraft.world.*;
|
||||||
import net.minecraft.world.event.GameEvent;
|
import net.minecraft.world.event.GameEvent;
|
||||||
|
|
||||||
public class FruitBearingBlock extends LeavesBlock implements TintedBlock, Buckable {
|
public class FruitBearingBlock extends LeavesBlock implements TintedBlock, Buckable, Fertilizable {
|
||||||
public static final IntProperty AGE = Properties.AGE_25;
|
public static final IntProperty AGE = Properties.AGE_25;
|
||||||
public static final int WITHER_AGE = 15;
|
public static final int MAX_AGE = 25;
|
||||||
public static final EnumProperty<Stage> STAGE = EnumProperty.of("stage", Stage.class);
|
public static final EnumProperty<Stage> STAGE = EnumProperty.of("stage", Stage.class);
|
||||||
|
|
||||||
private final Supplier<Block> fruit;
|
private final Supplier<Block> fruit;
|
||||||
|
@ -41,7 +41,7 @@ public class FruitBearingBlock extends LeavesBlock implements TintedBlock, Bucka
|
||||||
.allowsSpawning(BlockConstructionUtils::canSpawnOnLeaves)
|
.allowsSpawning(BlockConstructionUtils::canSpawnOnLeaves)
|
||||||
.suffocates(BlockConstructionUtils::never)
|
.suffocates(BlockConstructionUtils::never)
|
||||||
.blockVision(BlockConstructionUtils::never));
|
.blockVision(BlockConstructionUtils::never));
|
||||||
setDefaultState(getDefaultState().with(STAGE, Stage.IDLE));
|
setDefaultState(getDefaultState().with(AGE, 0).with(STAGE, Stage.IDLE));
|
||||||
this.overlay = overlay;
|
this.overlay = overlay;
|
||||||
this.fruit = fruit;
|
this.fruit = fruit;
|
||||||
this.rottenFruitSupplier = rottenFruitSupplier;
|
this.rottenFruitSupplier = rottenFruitSupplier;
|
||||||
|
@ -76,35 +76,66 @@ public class FruitBearingBlock extends LeavesBlock implements TintedBlock, Bucka
|
||||||
}
|
}
|
||||||
|
|
||||||
if (world.getBaseLightLevel(pos, 0) > 8) {
|
if (world.getBaseLightLevel(pos, 0) > 8) {
|
||||||
BlockSoundGroup group = getSoundGroup(state);
|
|
||||||
int steps = FertilizableUtil.getGrowthSteps(world, pos, state, random);
|
int steps = FertilizableUtil.getGrowthSteps(world, pos, state, random);
|
||||||
while (steps-- > 0) {
|
while (steps-- > 0) {
|
||||||
if (!shouldAdvance(random)) {
|
if (!shouldAdvance(random)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state.get(STAGE) == Stage.FRUITING) {
|
state = cycleStage(state);
|
||||||
state = state.cycle(AGE);
|
|
||||||
if (state.get(AGE) > 20) {
|
|
||||||
state = state.with(AGE, 0).cycle(STAGE);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
state = state.with(AGE, 0).cycle(STAGE);
|
|
||||||
}
|
|
||||||
world.setBlockState(pos, state, Block.NOTIFY_ALL);
|
|
||||||
BlockPos fruitPosition = pos.down();
|
BlockPos fruitPosition = pos.down();
|
||||||
|
|
||||||
Stage stage = state.get(STAGE);
|
|
||||||
|
|
||||||
if (stage == Stage.FRUITING && isPositionValidForFruit(state, pos)) {
|
|
||||||
if (world.isAir(fruitPosition)) {
|
|
||||||
world.setBlockState(fruitPosition, getPlacedFruitState(random), Block.NOTIFY_ALL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockState fruitState = world.getBlockState(fruitPosition);
|
BlockState fruitState = world.getBlockState(fruitPosition);
|
||||||
|
|
||||||
if (stage == Stage.WITHERING && fruitState.isOf(fruit.get())) {
|
switch (state.get(STAGE)) {
|
||||||
|
case WITHERING:
|
||||||
|
wither(state, world, pos, fruitPosition, world.getBlockState(fruitPosition));
|
||||||
|
case BEARING:
|
||||||
|
if (!fruitState.isOf(fruit.get())) {
|
||||||
|
state = withStage(state, Stage.IDLE);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case FRUITING: {
|
||||||
|
if (!isPositionValidForFruit(state, pos)) {
|
||||||
|
state = withStage(state, Stage.IDLE);
|
||||||
|
} else {
|
||||||
|
state = grow(state, world, pos, fruitPosition, fruitState, random);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
world.setBlockState(pos, state, Block.NOTIFY_ALL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected BlockState withStage(BlockState state, Stage stage) {
|
||||||
|
return state.with(AGE, 0).with(STAGE, stage);
|
||||||
|
}
|
||||||
|
|
||||||
|
private BlockState cycleStage(BlockState state) {
|
||||||
|
state = state.cycle(AGE);
|
||||||
|
if (state.get(AGE) == 0) {
|
||||||
|
state = state.cycle(STAGE);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected BlockState grow(BlockState state, World world, BlockPos pos, BlockPos fruitPosition, BlockState fruitState, Random random) {
|
||||||
|
if (world.isAir(fruitPosition)) {
|
||||||
|
world.setBlockState(fruitPosition, getPlacedFruitState(random), Block.NOTIFY_ALL);
|
||||||
|
return withStage(state, Stage.BEARING);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!fruitState.isOf(fruit.get())) {
|
||||||
|
return withStage(state, Stage.IDLE);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void wither(BlockState state, World world, BlockPos pos, BlockPos fruitPosition, BlockState fruitState) {
|
||||||
|
if (!fruitState.isOf(fruit.get())) {
|
||||||
if (world.random.nextInt(2) == 0) {
|
if (world.random.nextInt(2) == 0) {
|
||||||
Block.dropStack(world, fruitPosition, rottenFruitSupplier.get());
|
Block.dropStack(world, fruitPosition, rottenFruitSupplier.get());
|
||||||
} else {
|
} else {
|
||||||
|
@ -115,11 +146,10 @@ public class FruitBearingBlock extends LeavesBlock implements TintedBlock, Bucka
|
||||||
world.emitGameEvent(GameEvent.BLOCK_DESTROY, pos, GameEvent.Emitter.of(fruitState));
|
world.emitGameEvent(GameEvent.BLOCK_DESTROY, pos, GameEvent.Emitter.of(fruitState));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BlockSoundGroup group = getSoundGroup(state);
|
||||||
world.playSound(null, pos, USounds.ITEM_APPLE_ROT, SoundCategory.BLOCKS, group.getVolume(), group.getPitch());
|
world.playSound(null, pos, USounds.ITEM_APPLE_ROT, SoundCategory.BLOCKS, group.getVolume(), group.getPitch());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ItemStack> onBucked(ServerWorld world, BlockState state, BlockPos pos) {
|
public List<ItemStack> onBucked(ServerWorld world, BlockState state, BlockPos pos) {
|
||||||
|
@ -142,21 +172,46 @@ public class FruitBearingBlock extends LeavesBlock implements TintedBlock, Bucka
|
||||||
return state.getRenderingSeed(pos) % 3 == 1;
|
return state.getRenderingSeed(pos) % 3 == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isFertilizable(WorldView world, BlockPos pos, BlockState state, boolean isClient) {
|
||||||
|
return switch (state.get(STAGE)) {
|
||||||
|
case FLOWERING -> world.isAir(pos.down());
|
||||||
|
default -> !world.getBlockState(pos.down()).isOf(fruit.get());
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canGrow(World world, Random random, BlockPos pos, BlockState state) {
|
||||||
|
return isFertilizable(world, pos, state, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void grow(ServerWorld world, Random random, BlockPos pos, BlockState state) {
|
||||||
|
state = state.cycle(AGE);
|
||||||
|
if (state.get(AGE) == 0) {
|
||||||
|
state = state.with(STAGE, switch (state.get(STAGE)) {
|
||||||
|
case IDLE -> Stage.FLOWERING;
|
||||||
|
case FLOWERING -> Stage.FRUITING;
|
||||||
|
default -> Stage.FLOWERING;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (state.get(STAGE) == Stage.FRUITING && state.get(AGE) == 0) {
|
||||||
|
state = grow(state, world, pos, pos.down(), world.getBlockState(pos.down()), random);
|
||||||
|
}
|
||||||
|
world.setBlockState(pos, state);
|
||||||
|
}
|
||||||
|
|
||||||
public enum Stage implements StringIdentifiable {
|
public enum Stage implements StringIdentifiable {
|
||||||
IDLE,
|
IDLE,
|
||||||
FLOWERING,
|
FLOWERING,
|
||||||
FRUITING,
|
FRUITING,
|
||||||
|
BEARING,
|
||||||
WITHERING;
|
WITHERING;
|
||||||
|
|
||||||
private static final Stage[] VALUES = values();
|
|
||||||
|
|
||||||
public Stage getNext() {
|
|
||||||
return VALUES[(ordinal() + 1) % VALUES.length];
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String asString() {
|
public String asString() {
|
||||||
return name().toLowerCase(Locale.ROOT);
|
return name().toLowerCase(Locale.ROOT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,6 +74,18 @@ public class FruitBlock extends Block implements Buckable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
|
@Override
|
||||||
|
public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) {
|
||||||
|
super.onStateReplaced(state, world, pos, newState, moved);
|
||||||
|
if (!newState.isOf(state.getBlock())) {
|
||||||
|
BlockState leaves = world.getBlockState(pos.up());
|
||||||
|
if (leaves.contains(FruitBearingBlock.STAGE)) {
|
||||||
|
world.setBlockState(pos.up(), leaves.withIfExists(FruitBearingBlock.AGE, 0).with(FruitBearingBlock.STAGE, FruitBearingBlock.Stage.IDLE));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected boolean canAttachTo(BlockState state) {
|
protected boolean canAttachTo(BlockState state) {
|
||||||
return state.isOf(stem);
|
return state.isOf(stem);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue