Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pasture seeds work more like random grass spread #4635

Open
wants to merge 1 commit into
base: 1.20.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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.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.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 @@ -231,6 +231,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> SEED_REPLACEABLE = tag("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 @@ -209,6 +209,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.SEED_REPLACEABLE).add(Blocks.DIRT, Blocks.GRASS_BLOCK, Blocks.MYCELIUM);

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