From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: MrPowerGamerBR Date: Mon, 20 Nov 2023 20:17:56 -0300 Subject: [PATCH] Blazingly Simple Farm Checks diff --git a/src/main/java/net/minecraft/world/level/block/CropBlock.java b/src/main/java/net/minecraft/world/level/block/CropBlock.java index 112d2feba5f75a2a873b595617780515945c10e4..d19d6e0082c3dba2581aab4911cfe9d36dc22453 100644 --- a/src/main/java/net/minecraft/world/level/block/CropBlock.java +++ b/src/main/java/net/minecraft/world/level/block/CropBlock.java @@ -81,6 +81,57 @@ public class CropBlock extends BushBlock implements BonemealableBlock { int i = this.getAge(state); if (i < this.getMaxAge()) { + // SparklyPaper start - Blazingly simple farm checks + if (world.sparklyPaperConfig.getBlazinglySimpleFarmChecks().getEnabled()) { + // These checks are similar to getGrowthSpeed, but we have "inlined" them because we want to access stuff like the farm block data later on + // Is the block below us moisturised? + BlockPos farmlandBelowTheCurrentBlock = pos.below(); + BlockState farmlandBelowTheCurrentBlockData = world.getBlockState(farmlandBelowTheCurrentBlock); + float f = world.sparklyPaperConfig.getBlazinglySimpleFarmChecks().getDefaultGrowthSpeed(); + boolean isCurrentFarmlandStateMoist = false; + if (farmlandBelowTheCurrentBlockData.is(Blocks.FARMLAND)) { + if ((Integer) farmlandBelowTheCurrentBlockData.getValue(FarmBlock.MOISTURE) > 0) { + // If we are currently moist, increase the speed! + f = world.sparklyPaperConfig.getBlazinglySimpleFarmChecks().getMoistGrowthSpeed(); + isCurrentFarmlandStateMoist = true; + } + } + // If we are skipping the middle aging stages, we need to change the growth speed and the next stage accordingly + if (world.sparklyPaperConfig.getBlazinglySimpleFarmChecks().getSkipMiddleAgingStagesForCrops()) { + f = f / getMaxAge(); + i = getMaxAge() - 1; + } + + // Spigot start + int modifier; + if (this == Blocks.BEETROOTS) { + modifier = world.spigotConfig.beetrootModifier; + } else if (this == Blocks.CARROTS) { + modifier = world.spigotConfig.carrotModifier; + } else if (this == Blocks.POTATOES) { + modifier = world.spigotConfig.potatoModifier; + // Paper start + } else if (this == Blocks.TORCHFLOWER_CROP) { + modifier = world.spigotConfig.torchFlowerModifier; + // Paper end + } else { + modifier = world.spigotConfig.wheatModifier; + } + + if (random.nextFloat() < (modifier / (100.0f * (Math.floor((25.0F / f) + 1))))) { // Spigot - SPIGOT-7159: Better modifier resolution + // Spigot end + if (!CraftEventFactory.handleBlockGrowEvent(world, pos, this.getStateForAge(i + 1), 2)) { + return; + } + + // Now that we know that the crop will grow... is the next stage the crop's max age? If yes, we are going to check if the farm land is moist! + if (i + 1 == getMaxAge() && isCurrentFarmlandStateMoist && !FarmBlock.isNearWater(world, farmlandBelowTheCurrentBlock)) { + // Whoops, farm land ain't moist! + // From FarmBlock, set the moisture to 0 + org.bukkit.craftbukkit.event.CraftEventFactory.handleMoistureChangeEvent(world, farmlandBelowTheCurrentBlock, (BlockState) farmlandBelowTheCurrentBlockData.setValue(FarmBlock.MOISTURE, 0), 2); // CraftBukkit + } + } + } else { float f = CropBlock.getGrowthSpeed(this, world, pos); // Spigot start @@ -103,6 +154,8 @@ public class CropBlock extends BushBlock implements BonemealableBlock { // Spigot end CraftEventFactory.handleBlockGrowEvent(world, pos, this.getStateForAge(i + 1), 2); // CraftBukkit } + } + // SparklyPaper end } } diff --git a/src/main/java/net/minecraft/world/level/block/FarmBlock.java b/src/main/java/net/minecraft/world/level/block/FarmBlock.java index d59e33e7326489c6d55d316d0130f22235f4c63c..3a2c875f8131fc4a30807a69f426b6acb5deb557 100644 --- a/src/main/java/net/minecraft/world/level/block/FarmBlock.java +++ b/src/main/java/net/minecraft/world/level/block/FarmBlock.java @@ -93,6 +93,19 @@ public class FarmBlock extends Block { @Override protected void randomTick(BlockState state, ServerLevel world, BlockPos pos, RandomSource random) { int i = (Integer) state.getValue(FarmBlock.MOISTURE); + // SparklyPaper start - Blazingly simple farm checks + if (world.sparklyPaperConfig.getBlazinglySimpleFarmChecks().getEnabled()) { + if (i == 0) { // We only care about non-moisturised farm blocks + if (FarmBlock.isNearWater(world, pos)) { + // Make it MOIST! + org.bukkit.craftbukkit.event.CraftEventFactory.handleMoistureChangeEvent(world, pos, (BlockState) state.setValue(FarmBlock.MOISTURE, 7), 2); // CraftBukkit + } else if (!FarmBlock.shouldMaintainFarmland(world, pos)) { + FarmBlock.turnToDirt((Entity) null, state, world, pos); + } + } + return; + } + // SparklyPaper end if (i > 0 && world.paperConfig().tickRates.wetFarmland != 1 && (world.paperConfig().tickRates.wetFarmland < 1 || (net.minecraft.server.MinecraftServer.currentTick + pos.hashCode()) % world.paperConfig().tickRates.wetFarmland != 0)) { return; } // Paper - Configurable random tick rates for blocks if (i == 0 && world.paperConfig().tickRates.dryFarmland != 1 && (world.paperConfig().tickRates.dryFarmland < 1 || (net.minecraft.server.MinecraftServer.currentTick + pos.hashCode()) % world.paperConfig().tickRates.dryFarmland != 0)) { return; } // Paper - Configurable random tick rates for blocks @@ -151,7 +164,7 @@ public class FarmBlock extends Block { return world.getBlockState(pos.above()).is(BlockTags.MAINTAINS_FARMLAND); } - private static boolean isNearWater(LevelReader world, BlockPos pos) { + public static boolean isNearWater(LevelReader world, BlockPos pos) { // SparklyPaper - make public for the Blazingly simple farm checks // Paper start - Perf: remove abstract block iteration int xOff = pos.getX(); int yOff = pos.getY(); diff --git a/src/main/java/net/minecraft/world/level/block/StemBlock.java b/src/main/java/net/minecraft/world/level/block/StemBlock.java index 924d80eb41d9a71d1e521c40742557251cf51832..e79ab379ac4ed5cde82a144a83c0326c8b3a6349 100644 --- a/src/main/java/net/minecraft/world/level/block/StemBlock.java +++ b/src/main/java/net/minecraft/world/level/block/StemBlock.java @@ -73,6 +73,56 @@ public class StemBlock extends BushBlock implements BonemealableBlock { @Override protected void randomTick(BlockState state, ServerLevel world, BlockPos pos, RandomSource random) { if (world.getRawBrightness(pos, 0) >= 9) { + // SparklyPaper start - Blazingly simple farm checks + if (world.sparklyPaperConfig.getBlazinglySimpleFarmChecks().getEnabled()) { + // These checks are similar to getGrowthSpeed, but we have "inlined" them because we want to access stuff like the farm block data later on + // Is the block below us moisturised? + BlockPos farmlandBelowTheCurrentBlock = pos.below(); + BlockState farmlandBelowTheCurrentBlockData = world.getBlockState(farmlandBelowTheCurrentBlock); + float f = world.sparklyPaperConfig.getBlazinglySimpleFarmChecks().getDefaultGrowthSpeed(); + boolean isCurrentFarmlandStateMoist = false; + if (farmlandBelowTheCurrentBlockData.is(Blocks.FARMLAND)) { + if ((Integer) farmlandBelowTheCurrentBlockData.getValue(FarmBlock.MOISTURE) > 0) { + // If we are currently moist, increase the speed! + f = world.sparklyPaperConfig.getBlazinglySimpleFarmChecks().getMoistGrowthSpeed(); + isCurrentFarmlandStateMoist = true; + } + } + + if (random.nextFloat() < ((this == Blocks.PUMPKIN_STEM ? world.spigotConfig.pumpkinModifier : world.spigotConfig.melonModifier) / (100.0f * (Math.floor((25.0F / f) + 1))))) { // Spigot - SPIGOT-7159: Better modifier resolution + int i = (Integer) state.getValue(StemBlock.AGE); + + if (i < 7) { + state = (BlockState) state.setValue(StemBlock.AGE, i + 1); + CraftEventFactory.handleBlockGrowEvent(world, pos, state, 2); // CraftBukkit + } else { + Direction enumdirection = Direction.Plane.HORIZONTAL.getRandomDirection(random); + BlockPos blockposition1 = pos.relative(enumdirection); + BlockState iblockdata1 = world.getBlockState(blockposition1.below()); + + if (world.getBlockState(blockposition1).isAir() && (iblockdata1.is(Blocks.FARMLAND) || iblockdata1.is(BlockTags.DIRT))) { + Registry iregistry = world.registryAccess().registryOrThrow(Registries.BLOCK); + Optional optional = iregistry.getOptional(this.fruit); + Optional optional1 = iregistry.getOptional(this.attachedStem); + + if (optional.isPresent() && optional1.isPresent()) { + // CraftBukkit start + if (!CraftEventFactory.handleBlockGrowEvent(world, blockposition1, ((Block) optional.get()).defaultBlockState())) { + return; + } + // CraftBukkit end + // Now that we know that the crop will grow... is the next stage the crop's max age? If yes, we are going to check if the farm land is moist! + if (isCurrentFarmlandStateMoist && !FarmBlock.isNearWater(world, farmlandBelowTheCurrentBlock)) { + // Whoops, farm land ain't moist! + // From FarmBlock, set the moisture to 0 + org.bukkit.craftbukkit.event.CraftEventFactory.handleMoistureChangeEvent(world, farmlandBelowTheCurrentBlock, (BlockState) farmlandBelowTheCurrentBlockData.setValue(FarmBlock.MOISTURE, 0), 2); // CraftBukkit + } + world.setBlockAndUpdate(pos, (BlockState) ((Block) optional1.get()).defaultBlockState().setValue(HorizontalDirectionalBlock.FACING, enumdirection)); + } + } + } + } + } else { float f = CropBlock.getGrowthSpeed(this, world, pos); if (random.nextFloat() < ((this == Blocks.PUMPKIN_STEM ? world.spigotConfig.pumpkinModifier : world.spigotConfig.melonModifier) / (100.0f * (Math.floor((25.0F / f) + 1))))) { // Spigot - SPIGOT-7159: Better modifier resolution @@ -102,7 +152,8 @@ public class StemBlock extends BushBlock implements BonemealableBlock { } } } - + } + // SparklyPaper end } }