9
0
mirror of https://github.com/Samsuik/Sakura.git synced 2026-01-04 15:31:43 +00:00
Files
SakuraMC/patches/server/0009-Optimise-New-Liquid-Level.patch
2025-03-08 11:26:50 +00:00

121 lines
6.4 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Samsuik <kfian294ma4@gmail.com>
Date: Wed, 6 Oct 2021 17:25:27 +0100
Subject: [PATCH] Optimise New Liquid Level
diff --git a/src/main/java/net/minecraft/world/level/material/FlowingFluid.java b/src/main/java/net/minecraft/world/level/material/FlowingFluid.java
index 1c0712295695727ee9c4d430d4157b8e17cbd71f..42a54b08a2f44d431189977ee4bdc0683989163c 100644
--- a/src/main/java/net/minecraft/world/level/material/FlowingFluid.java
+++ b/src/main/java/net/minecraft/world/level/material/FlowingFluid.java
@@ -137,7 +137,7 @@ public abstract class FlowingFluid extends Fluid {
BlockState iblockdata = world.getBlockState(fluidPos);
BlockPos blockposition1 = fluidPos.below();
BlockState iblockdata1 = world.getBlockState(blockposition1);
- FluidState fluid1 = this.getNewLiquid(world, blockposition1, iblockdata1);
+ FluidState fluid1 = this.getLiquidFromSurroundings(world, blockposition1, iblockdata1, Direction.DOWN); // Sakura - optimise new liquid level
if (this.canSpreadTo(world, fluidPos, iblockdata, Direction.DOWN, blockposition1, iblockdata1, world.getFluidState(blockposition1), fluid1.getType())) {
// CraftBukkit start
@@ -196,7 +196,58 @@ public abstract class FlowingFluid extends Fluid {
}
}
+ // Sakura start - optimise new liquid level
+ private static final int FORM_LIQUID_SOURCE = 0xff;
+
+ private boolean canLiquidFlowDown(final Level level, final BlockPos pos, final BlockState state) {
+ final BlockPos abovePos = pos.above();
+ final BlockState stateAbove = level.getBlockState(abovePos);
+ final FluidState fluidStateAbove = stateAbove.getFluidState();
+
+ return !fluidStateAbove.isEmpty() && fluidStateAbove.getType().isSame(this)
+ && canPassThroughWall(Direction.UP, level, pos, state, abovePos, stateAbove);
+ }
+
+ private boolean canFormLiquidSource(final Level level, final BlockPos pos) {
+ if (!this.canConvertToSource(level)) {
+ return false;
+ }
+ final BlockState stateBelow = level.getBlockState(pos.below());
+ final FluidState fluidStateBelow = stateBelow.getFluidState();
+ return stateBelow.isSolid() || this.isSourceBlockOfThisType(fluidStateBelow);
+ }
+
protected FluidState getNewLiquid(Level world, BlockPos pos, BlockState state) {
+ return this.getLiquidFromSurroundings(world, pos, state, null);
+ }
+
+ @org.jspecify.annotations.NullUnmarked
+ private FluidState getLiquidFromSurroundings(final Level level, final BlockPos pos, final BlockState state,
+ final Direction flowing) {
+ final boolean flowingDown = flowing == Direction.DOWN || this.canLiquidFlowDown(level, pos, state);
+
+ // There's no point checking the surroundings if the liquid is unable to turn into a source.
+ if (flowingDown && !this.canFormLiquidSource(level, pos)) {
+ return this.getFlowing(8, true);
+ }
+
+ final Direction incoming = flowing != null ? flowing.getOpposite() : null;
+ final int liquidLevel = this.getLiquidLevelFromSurroundings(level, pos, state, incoming);
+ if (liquidLevel == FORM_LIQUID_SOURCE) {
+ return this.getSource(false);
+ }
+
+ if (flowingDown) {
+ return this.getFlowing(8, true);
+ } else {
+ final int newLiquidLevel = liquidLevel - this.getDropOff(level);
+ return newLiquidLevel <= 0 ? Fluids.EMPTY.defaultFluidState() : this.getFlowing(newLiquidLevel, false);
+ }
+ }
+
+ @org.jspecify.annotations.NullUnmarked
+ private int getLiquidLevelFromSurroundings(final Level world, final BlockPos pos, final BlockState state, final Direction incoming) {
+ // Sakura end - optimise new liquid level
int i = 0;
int j = 0;
Iterator iterator = Direction.Plane.HORIZONTAL.iterator();
@@ -208,7 +259,7 @@ public abstract class FlowingFluid extends Fluid {
if (iblockdata1 == null) continue; // Paper - Prevent chunk loading from fluid flowing
FluidState fluid = iblockdata1.getFluidState();
- if (fluid.getType().isSame(this) && this.canPassThroughWall(enumdirection, world, pos, state, blockposition1, iblockdata1)) {
+ if (fluid.getType().isSame(this) && (incoming == enumdirection || this.canPassThroughWall(enumdirection, world, pos, state, blockposition1, iblockdata1))) { // Sakura - optimise new liquid level
if (fluid.isSource()) {
++j;
}
@@ -222,21 +273,11 @@ public abstract class FlowingFluid extends Fluid {
FluidState fluid1 = iblockdata2.getFluidState();
if (iblockdata2.isSolid() || this.isSourceBlockOfThisType(fluid1)) {
- return this.getSource(false);
+ return FORM_LIQUID_SOURCE; // Sakura - optimise new liquid level
}
}
- BlockPos blockposition2 = pos.above();
- BlockState iblockdata3 = world.getBlockState(blockposition2);
- FluidState fluid2 = iblockdata3.getFluidState();
-
- if (!fluid2.isEmpty() && fluid2.getType().isSame(this) && this.canPassThroughWall(Direction.UP, world, pos, state, blockposition2, iblockdata3)) {
- return this.getFlowing(8, true);
- } else {
- int k = i - this.getDropOff(world);
-
- return k <= 0 ? Fluids.EMPTY.defaultFluidState() : this.getFlowing(k, false);
- }
+ return i; // Sakura - optimise new liquid level
}
private boolean canPassThroughWall(Direction face, BlockGetter world, BlockPos pos, BlockState state, BlockPos fromPos, BlockState fromState) {
@@ -418,7 +459,7 @@ public abstract class FlowingFluid extends Fluid {
// Paper end - Prevent chunk loading from fluid flowing
BlockState iblockdata1 = (BlockState) pair.getFirst();
FluidState fluid = (FluidState) pair.getSecond();
- FluidState fluid1 = this.getNewLiquid(world, blockposition1, iblockdata1);
+ FluidState fluid1 = this.getLiquidFromSurroundings(world, blockposition1, iblockdata1, enumdirection); // Sakura - optimise new liquid level
if (this.canPassThrough(world, fluid1.getType(), pos, state, enumdirection, blockposition1, iblockdata1, fluid)) {
BlockPos blockposition2 = blockposition1.below();