Fixed oats not growing after being collected

This commit is contained in:
Sollace 2023-09-10 20:05:13 +01:00
parent c6d5b75896
commit 6f0a11297a
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
2 changed files with 30 additions and 15 deletions

View file

@ -4,12 +4,15 @@ import java.util.function.Supplier;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import com.minelittlepony.unicopia.seasons.FertilizableUtil;
import net.minecraft.block.*; import net.minecraft.block.*;
import net.minecraft.item.ItemConvertible; import net.minecraft.item.ItemConvertible;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.state.StateManager; import net.minecraft.state.StateManager;
import net.minecraft.state.property.IntProperty; import net.minecraft.state.property.IntProperty;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.random.Random; import net.minecraft.util.math.random.Random;
import net.minecraft.util.shape.VoxelShape; import net.minecraft.util.shape.VoxelShape;
import net.minecraft.world.*; import net.minecraft.world.*;
@ -103,6 +106,14 @@ public class SegmentedCropBlock extends CropBlock implements SegmentedBlock {
propagateGrowth(world, pos, state); propagateGrowth(world, pos, state);
} }
@Override
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) {
if (direction == Direction.UP && !isNext(neighborState)) {
return state.with(getAgeProperty(), Math.min(state.get(getAgeProperty()), getMaxAge() - 1));
}
return super.getStateForNeighborUpdate(state, direction, neighborState, world, pos, neighborPos);
}
@Override @Override
public void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random random) { public void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
BlockPos tip = getTip(world, pos); BlockPos tip = getTip(world, pos);
@ -116,6 +127,8 @@ public class SegmentedCropBlock extends CropBlock implements SegmentedBlock {
int age = getAge(state); int age = getAge(state);
if (age < getMaxAge()) { if (age < getMaxAge()) {
float moisture = CropBlock.getAvailableMoisture(world.getBlockState(root).getBlock(), world, root); float moisture = CropBlock.getAvailableMoisture(world.getBlockState(root).getBlock(), world, root);
int steps = FertilizableUtil.getGrowthSteps(world, pos, state, random);
while (steps-- > 0) {
if (random.nextInt((int)(BASE_GROWTH_CHANCE / moisture) + 1) == 0) { if (random.nextInt((int)(BASE_GROWTH_CHANCE / moisture) + 1) == 0) {
world.setBlockState(pos, withAge(age + 1), Block.NOTIFY_LISTENERS); world.setBlockState(pos, withAge(age + 1), Block.NOTIFY_LISTENERS);
propagateGrowth(world, pos, state); propagateGrowth(world, pos, state);
@ -123,15 +136,12 @@ public class SegmentedCropBlock extends CropBlock implements SegmentedBlock {
} }
} }
} }
}
private void propagateGrowth(World world, BlockPos pos, BlockState state) { private void propagateGrowth(World world, BlockPos pos, BlockState state) {
int oldAge = getAge(state); int oldAge = getAge(state);
state = world.getBlockState(pos); state = world.getBlockState(pos);
int ageChange = getAge(state) - oldAge; int ageChange = Math.max(1, getAge(state) - oldAge);
if (ageChange <= 0) {
return;
}
onGrown(world, pos, state, ageChange); onGrown(world, pos, state, ageChange);
@ -160,13 +170,18 @@ public class SegmentedCropBlock extends CropBlock implements SegmentedBlock {
} }
@Override @Override
public boolean hasRandomTicks(BlockState state) { public boolean isFertilizable(WorldView world, BlockPos pos, BlockState state, boolean isClient) {
return super.hasRandomTicks(state) || nextSegmentSupplier != null; if (super.isFertilizable(world, pos, state, isClient)) {
return true;
} }
@Override if (nextSegmentSupplier == null) {
public boolean isFertilizable(WorldView world, BlockPos pos, BlockState state, boolean isClient) { return false;
return super.isFertilizable(world, pos, state, isClient) || (nextSegmentSupplier != null && isNext(world.getBlockState(pos.up()))); }
pos = pos.up();
state = world.getBlockState(pos);
return state.isAir() || (isNext(state) && state.getBlock() instanceof Fertilizable f && f.isFertilizable(world, pos, state, isClient));
} }
@Override @Override

View file

@ -113,7 +113,7 @@ public interface UBlocks {
() -> UItems.APPLE_PIE_HOOF () -> UItems.APPLE_PIE_HOOF
)); ));
SegmentedCropBlock OATS = register("oats", SegmentedCropBlock.create(11, 5, AbstractBlock.Settings.copy(Blocks.WHEAT), () -> UItems.OAT_SEEDS, null, () -> UBlocks.OATS_STEM)); SegmentedCropBlock OATS = register("oats", SegmentedCropBlock.create(11, 5, AbstractBlock.Settings.copy(Blocks.WHEAT), () -> UItems.OAT_SEEDS, null, null));
SegmentedCropBlock OATS_STEM = register("oats_stem", OATS.createNext(5)); SegmentedCropBlock OATS_STEM = register("oats_stem", OATS.createNext(5));
SegmentedCropBlock OATS_CROWN = register("oats_crown", OATS_STEM.createNext(5)); SegmentedCropBlock OATS_CROWN = register("oats_crown", OATS_STEM.createNext(5));