Skip to content

Commit

Permalink
Pasture seeds work more like random grass spread
Browse files Browse the repository at this point in the history
Whether a block can be a source or target of spreading are determined the same way as random grass or mycelium spread. Spread is no longer restricted to one Y-level and considers water or covered blocks properly. (fixes #4251)
Additionally, the replaceable blocks are now defined by a block tag (`#botania:pasture_seed_replaceable`), which also includes mycelium by default now.
  • Loading branch information
TheRealWormbo authored and artemisSystem committed Aug 16, 2024
1 parent a6ea571 commit 7824c8c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 24 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 48 additions & 24 deletions Xplat/src/main/java/vazkii/botania/common/item/GrassSeedsItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@
import com.google.common.collect.ImmutableMap;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.SnowLayerBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.lighting.LightEngine;

import org.jetbrains.annotations.NotNull;

import vazkii.botania.api.block.FloatingFlower.IslandType;
import vazkii.botania.client.fx.WispParticleData;
import vazkii.botania.common.block.BotaniaBlocks;
import vazkii.botania.common.lib.BotaniaTags;

import java.util.*;

Expand Down Expand Up @@ -67,7 +72,7 @@ public InteractionResult useOn(UseOnContext ctx) {
public InteractionResult applySeeds(Level world, BlockPos pos, ItemStack stack) {
BlockState state = world.getBlockState(pos);

if (state.is(Blocks.DIRT) || state.is(Blocks.GRASS_BLOCK) && type != IslandType.GRASS) {
if (state.is(BotaniaTags.Blocks.PASTURE_SEED_REPLACEABLE) && state != stateForType(type)) {
if (!world.isClientSide) {
BlockSwapper swapper = addBlockSwapper(world, pos, type);
world.setBlockAndUpdate(pos, swapper.stateToSet);
Expand Down Expand Up @@ -161,9 +166,14 @@ private static class BlockSwapper {
public static final int RANGE = 3;

/**
* The range around which a block can spread in a single tick.
* The horizontal range around which a block can spread in a single tick.
*/
public static final int TICK_RANGE = 1;
public static final int TICK_RANGE_HORIZONTAL = 1;

/**
* The vertical range around which a block can spread in a single tick.
*/
public static final int TICK_RANGE_VERTICAL = 2;

private final Level world;
private final Random rand;
Expand Down Expand Up @@ -197,12 +207,14 @@ public BlockSwapper(Level world, BlockPos coords, BlockState state) {
*/
public boolean tick() {
if (++ticksExisted % 20 == 0) {
for (BlockPos pos : BlockPos.betweenClosed(startCoords.offset(-RANGE, 0, -RANGE),
startCoords.offset(RANGE, 0, RANGE))) {
if (world.getBlockState(pos) == stateToSet) {
tickBlock(pos);
var tickPositions = new ArrayList<BlockPos>();
for (BlockPos pos : BlockPos.betweenClosed(startCoords.offset(-RANGE, -RANGE, -RANGE),
startCoords.offset(RANGE, RANGE, RANGE))) {
if (world.getBlockState(pos) == stateToSet && canPropagate(pos)) {
tickPositions.add(pos.immutable());
}
}
tickPositions.forEach(this::tickBlock);
}

// This swapper should exist for 80 ticks
Expand All @@ -219,16 +231,15 @@ public void tickBlock(BlockPos pos) {
List<BlockPos> validCoords = new ArrayList<>();

// Go around this block and aggregate valid blocks.
for (int xOffset = -TICK_RANGE; xOffset <= TICK_RANGE; xOffset++) {
for (int zOffset = -TICK_RANGE; zOffset <= TICK_RANGE; zOffset++) {
// Skip the current block
if (xOffset == 0 && zOffset == 0) {
continue;
}
for (BlockPos targetPos : BlockPos.betweenClosed(pos.offset(-TICK_RANGE_HORIZONTAL, -TICK_RANGE_VERTICAL, -TICK_RANGE_HORIZONTAL),
pos.offset(TICK_RANGE_HORIZONTAL, TICK_RANGE_VERTICAL, TICK_RANGE_HORIZONTAL))) {
// Skip the current block, and any blocks that are already converted
if (targetPos.equals(pos) || world.getBlockState(targetPos) == stateToSet) {
continue;
}

if (isValidSwapPosition(pos.offset(xOffset, 0, zOffset))) {
validCoords.add(pos.offset(xOffset, 0, zOffset));
}
if (isValidSwapPosition(targetPos)) {
validCoords.add(targetPos.immutable());
}
}

Expand All @@ -251,16 +262,29 @@ public void tickBlock(BlockPos pos) {
*/
public boolean isValidSwapPosition(BlockPos pos) {
BlockState state = world.getBlockState(pos);
return state.is(BotaniaTags.Blocks.PASTURE_SEED_REPLACEABLE) && canBeGrass(pos, state);
}

// Valid blocks to spread to are either dirt or grass, and do not
// have blocks which block grass growth.

// See http://minecraft.gamepedia.com/Grass_Block
// The major rule is that a block which reduces light
// levels by 2 or more blocks grass growth.
// [VanillaCopy] net.minecraft.world.level.block.SpreadingSnowyDirtBlock#canBeGrass
private boolean canBeGrass(BlockPos pos, BlockState state) {
BlockPos abovePos = pos.above();
BlockState aboveState = world.getBlockState(abovePos);
if (aboveState.is(Blocks.SNOW) && aboveState.getValue(SnowLayerBlock.LAYERS) == 1) {
// single snow layer, okay to spread below that
return true;
}
if (aboveState.getFluidState().getAmount() == 8) {
// full-height liquid, don't spread
return false;
}
int lightLevel = LightEngine.getLightBlockInto(world, state, pos, aboveState, abovePos, Direction.UP, aboveState.getLightBlock(world, abovePos));
return lightLevel < world.getMaxLightLevel();
}

return (state.is(Blocks.DIRT) || state.is(Blocks.GRASS_BLOCK))
&& world.getBlockState(pos.above()).getLightBlock(world, pos.above()) <= 1;
// [VanillaCopy] net.minecraft.world.level.block.SpreadingSnowyDirtBlock#canPropagate
private boolean canPropagate(BlockPos pos) {
BlockPos abovePos = pos.above();
return canBeGrass(pos, stateToSet) && !world.getFluidState(abovePos).is(FluidTags.WATER);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ public static class Blocks {
*/
public static final TagKey<Block> UNWANDABLE = tag("unwandable");

/**
* Blocks in this tag can be replaced by the spreading effect of Pasture Seeds and related items.
*/
public static final TagKey<Block> PASTURE_SEED_REPLACEABLE = tag("pasture_seed_replaceable");

private static TagKey<Block> tag(String name) {
return TagKey.create(Registries.BLOCK, prefix(name));
}
Expand Down
2 changes: 2 additions & 0 deletions Xplat/src/main/java/vazkii/botania/data/BlockTagProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ protected void addTags(HolderLookup.Provider provider) {
tag(BotaniaTags.Blocks.UNWANDABLE).addTag(BlockTags.FIRE)
.add(Blocks.CHORUS_PLANT, Blocks.SCULK_VEIN, Blocks.VINE, Blocks.REDSTONE_WIRE, Blocks.NETHER_PORTAL, BotaniaBlocks.solidVines);

tag(BotaniaTags.Blocks.PASTURE_SEED_REPLACEABLE).add(Blocks.DIRT, Blocks.GRASS_BLOCK, Blocks.MYCELIUM);

tag(BlockTags.FLOWER_POTS)
.add(Arrays.stream(DyeColor.values())
.map(BotaniaBlocks::getPottedFlower)
Expand Down

0 comments on commit 7824c8c

Please sign in to comment.