Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 265
src/main/java/dev/kodari/sneaktogrow/SneakToGrowMod.java
package dev.kodari.sneaktogrow;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.block.AmethystClusterBlock;
import net.minecraft.world.level.block.BonemealableBlock;
import net.minecraft.world.level.block.BonemealSource;
import net.minecraft.world.level.block.BuddingAmethystBlock;
import net.minecraft.world.level.block.GrassBlock;
import net.minecraft.world.level.block.NetherWartBlock;
import net.minecraft.world.level.block.ShortDryGrassBlock;
import net.minecraft.world.level.block.SugarCaneBlock;
import net.minecraft.world.level.block.TallDryGrassBlock;
import net.minecraft.world.level.block.TallGrassBlock;
import net.minecraft.world.level.block.state.BlockState;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.Properties;
import java.util.WeakHashMap;
public class SneakToGrowMod implements ModInitializer {
private static final int DEFAULT_RADIUS = 4;
private static final int MAX_RADIUS = 16;
private static int radius = DEFAULT_RADIUS;
private static final int GROWTH_COOLDOWN_TICKS = 5;
private static final Map<ServerPlayer, SneakState> PLAYER_STATES = new WeakHashMap<>();
@Override
public void onInitialize() {
loadConfig();
ServerTickEvents.END_LEVEL_TICK.register(SneakToGrowMod::growNearbyPlants);
}
private static void loadConfig() {
Path configPath = FabricLoader.getInstance().getConfigDir().resolve("sneaktogrow.properties");
Properties config = new Properties();
try {
Files.createDirectories(configPath.getParent());
if (Files.exists(configPath)) {
try (Reader reader = Files.newBufferedReader(configPath)) {
config.load(reader);
}
}
try {
radius = Math.max(1, Math.min(MAX_RADIUS,
Integer.parseInt(config.getProperty("radius", Integer.toString(DEFAULT_RADIUS)))));
} catch (NumberFormatException exception) {
radius = DEFAULT_RADIUS;
}
config.setProperty("radius", Integer.toString(radius));
try (Writer writer = Files.newBufferedWriter(configPath)) {
config.store(writer, "Sneak to Grow settings. Set radius from 1 to 16 blocks.");
}
} catch (IOException exception) {
throw new UncheckedIOException("Unable to load Sneak to Grow configuration", exception);
}
}
private static void growNearbyPlants(ServerLevel level) {
for (ServerPlayer player : level.players()) {
SneakState previous = PLAYER_STATES.get(player);
if (!player.isShiftKeyDown()) {
if (previous != null && previous.sneaking()) {
PLAYER_STATES.put(player, new SneakState(false, previous.lastGrowthTick()));
}
continue;
}
if (previous != null && previous.sneaking()) {
continue;
}
if (previous != null && player.tickCount - previous.lastGrowthTick() < GROWTH_COOLDOWN_TICKS) {
PLAYER_STATES.put(player, new SneakState(true, previous.lastGrowthTick()));
continue;
}
PLAYER_STATES.put(player, new SneakState(true, player.tickCount));
BlockPos playerPos = player.blockPosition();
int playerX = playerPos.getX();
int playerY = playerPos.getY();
int playerZ = playerPos.getZ();
for (int y = -radius; y <= radius; y++) {
for (int z = -radius; z <= radius; z++) {
for (int x = -radius; x <= radius; x++) {
if (x * x + y * y + z * z > radius * radius) {
continue;
}
BlockPos pos = new BlockPos(playerX + x, playerY + y, playerZ + z);
if (!level.hasChunkAt(pos)) {
continue;
}
BlockState state = level.getBlockState(pos);
if (state.getBlock() instanceof GrassBlock
|| state.getBlock() instanceof TallGrassBlock
|| state.getBlock() instanceof ShortDryGrassBlock
|| state.getBlock() instanceof TallDryGrassBlock) {
continue;
}
boolean grew = false;
if (state.getBlock() instanceof BuddingAmethystBlock) {
Direction[] directions = Direction.values();
BlockState[] surroundingStates = new BlockState[directions.length];
for (int i = 0; i < directions.length; i++) {
surroundingStates[i] = level.getBlockState(pos.relative(directions[i]));
}
for (int growthTick = 0; growthTick < 16; growthTick++) {
level.getBlockState(pos).randomTick(level, pos, level.getRandom());
}
for (int i = 0; i < directions.length; i++) {
BlockPos crystalPos = pos.relative(directions[i]);
BlockState crystalState = level.getBlockState(crystalPos);
if (!crystalState.equals(surroundingStates[i])
&& crystalState.getBlock() instanceof AmethystClusterBlock) {
level.sendParticles(ParticleTypes.HAPPY_VILLAGER,
crystalPos.getX() + 0.5, crystalPos.getY() + 0.5, crystalPos.getZ() + 0.5,
15, 0.35, 0.45, 0.35, 0.02);
grew = true;
}
}
} else if (state.getBlock() instanceof NetherWartBlock) {
int age = state.getValue(NetherWartBlock.AGE);
for (int growthTick = 0; growthTick < 16 && age < 3; growthTick++) {
if (level.getRandom().nextInt(10) == 0) {
age++;
level.setBlock(pos, state.setValue(NetherWartBlock.AGE, age), 2);
grew = true;
}
}
} else if (state.getBlock() instanceof SugarCaneBlock) {
BlockPos abovePos = pos.above();
if (level.getBlockState(abovePos).getBlock() instanceof SugarCaneBlock) {
continue;
}
BlockState caneState = state;
for (int growthTick = 0; growthTick < 16; growthTick++) {
caneState.randomTick(level, pos, level.getRandom());
if (level.getBlockState(abovePos).getBlock() instanceof SugarCaneBlock) {
grew = true;
break;
}
BlockState updatedState = level.getBlockState(pos);
if (updatedState.equals(caneState)) {
break;
}
caneState = updatedState;
}
} else if (state.getBlock() instanceof BonemealableBlock growable
&& growable.isValidBonemealTarget(level, pos, state, BonemealSource.INTERACTION)
&& growable.isBonemealSuccess(level, level.getRandom(), pos, state, BonemealSource.INTERACTION)) {
growable.performBonemeal(level, level.getRandom(), pos, state, BonemealSource.INTERACTION);
grew = true;
}
if (grew && !(state.getBlock() instanceof BuddingAmethystBlock)) {
level.sendParticles(ParticleTypes.HAPPY_VILLAGER,
pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5,
15, 0.35, 0.45, 0.35, 0.02);
}
}
}
}
}
}
private record SneakState(boolean sneaking, int lastGrowthTick) {
}
}