9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2026-01-04 15:41:40 +00:00
Files
Leaf/patches/unapplied/server/0044-Carpet-Fixes-Use-optimized-PoweredRailBlock-logic.patch
2023-09-05 04:48:58 -04:00

395 lines
22 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com>
Date: Thu, 6 Apr 2023 20:53:05 -0400
Subject: [PATCH] Carpet-Fixes: Use optimized PoweredRailBlock logic
Original license: MIT
Original project: https://github.com/fxmorin/carpet-fixes
Full Rewrite of the powered rail iteration logic.
This rewrite brings a massive performance boost while keeping the vanilla order. This is achieved by running all the
powered rail logic from a single rail instead of each block iterating separately. Which was not only very
expensive but also completely unnecessary and with a lot of massive overhead
diff --git a/src/main/java/net/minecraft/world/level/block/PoweredRailBlock.java b/src/main/java/net/minecraft/world/level/block/PoweredRailBlock.java
index 0ec427fe0e1f85c07585e1a05269c4c38fc3e9f5..c86278caae67fde06d3dcb6ffcf51f5bd77804a3 100644
--- a/src/main/java/net/minecraft/world/level/block/PoweredRailBlock.java
+++ b/src/main/java/net/minecraft/world/level/block/PoweredRailBlock.java
@@ -1,6 +1,7 @@
package net.minecraft.world.level.block;
import net.minecraft.core.BlockPos;
+import net.minecraft.core.Direction;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
@@ -12,6 +13,8 @@ import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.properties.RailShape;
import org.bukkit.craftbukkit.event.CraftEventFactory; // CraftBukkit
+import java.util.HashMap;
+
public class PoweredRailBlock extends BaseRailBlock {
public static final EnumProperty<RailShape> SHAPE = BlockStateProperties.RAIL_SHAPE_STRAIGHT;
@@ -22,7 +25,13 @@ public class PoweredRailBlock extends BaseRailBlock {
this.registerDefaultState((BlockState) ((BlockState) ((BlockState) ((BlockState) this.stateDefinition.any()).setValue(PoweredRailBlock.SHAPE, RailShape.NORTH_SOUTH)).setValue(PoweredRailBlock.POWERED, false)).setValue(PoweredRailBlock.WATERLOGGED, false));
}
- protected boolean findPoweredRailSignal(Level world, BlockPos pos, BlockState state, boolean flag, int distance) {
+ private static final Direction[] EAST_WEST_DIR = new Direction[]{Direction.WEST, Direction.EAST};
+ private static final Direction[] NORTH_SOUTH_DIR = new Direction[]{Direction.SOUTH, Direction.NORTH};
+
+ private static final int FORCE_PLACE = UPDATE_MOVE_BY_PISTON | UPDATE_KNOWN_SHAPE | UPDATE_CLIENTS;
+ ;
+
+ protected boolean findPoweredRailSignal(Level world, BlockPos pos, BlockState state, boolean flag, int distance, HashMap<BlockPos, Boolean> checkedPos) {
if (distance >= world.purpurConfig.railActivationRange) { // Purpur
return false;
} else {
@@ -33,22 +42,22 @@ public class PoweredRailBlock extends BaseRailBlock {
RailShape blockpropertytrackposition = (RailShape) state.getValue(PoweredRailBlock.SHAPE);
// Leaf start - Optimize PoweredRail swtich
- switch (blockpropertytrackposition) {
- case NORTH_SOUTH -> {
+ switch (blockpropertytrackposition.ordinal()) {
+ case 0 -> {
if (flag) {
++l;
} else {
--l;
}
}
- case EAST_WEST -> {
+ case 1 -> {
if (flag) {
--j;
} else {
++j;
}
}
- case ASCENDING_EAST -> {
+ case 2 -> {
if (flag) {
--j;
} else {
@@ -58,7 +67,7 @@ public class PoweredRailBlock extends BaseRailBlock {
}
blockpropertytrackposition = RailShape.EAST_WEST;
}
- case ASCENDING_WEST -> {
+ case 3 -> {
if (flag) {
--j;
++k;
@@ -68,7 +77,7 @@ public class PoweredRailBlock extends BaseRailBlock {
}
blockpropertytrackposition = RailShape.EAST_WEST;
}
- case ASCENDING_NORTH -> {
+ case 4 -> {
if (flag) {
++l;
} else {
@@ -78,7 +87,7 @@ public class PoweredRailBlock extends BaseRailBlock {
}
blockpropertytrackposition = RailShape.NORTH_SOUTH;
}
- case ASCENDING_SOUTH -> {
+ case 5 -> {
if (flag) {
++l;
++k;
@@ -91,19 +100,38 @@ public class PoweredRailBlock extends BaseRailBlock {
}
// Leaf end
- return this.isSameRailWithPower(world, new BlockPos(j, k, l), flag, distance, blockpropertytrackposition) || flag1 && this.isSameRailWithPower(world, new BlockPos(j, k - 1, l), flag, distance, blockpropertytrackposition); // Leaf - Optimize PoweredRail
+ return this.isSameRailWithPower(world, new BlockPos(j, k, l), flag, distance, blockpropertytrackposition, checkedPos) || flag1 && this.isSameRailWithPower(world, new BlockPos(j, k - 1, l), flag, distance, blockpropertytrackposition, checkedPos); // Leaf - Optimize PoweredRail
}
}
- protected boolean isSameRailWithPower(Level world, BlockPos pos, boolean flag, int distance, RailShape shape) {
+ protected boolean isSameRailWithPower(Level world, BlockPos pos, boolean flag, int distance, RailShape shape, HashMap<BlockPos, Boolean> checkedPos) {
BlockState iblockdata = world.getBlockState(pos);
+ boolean speedCheck = checkedPos.containsKey(pos) && checkedPos.get(pos);
- if (!iblockdata.is((Block) this)) {
- return false;
+ if (speedCheck) {
+ return world.hasNeighborSignal(pos) ||
+ this.findPoweredRailSignal(world, pos, iblockdata, flag, distance + 1, checkedPos);
} else {
- RailShape blockpropertytrackposition1 = (RailShape) iblockdata.getValue(PoweredRailBlock.SHAPE);
-
- return (shape != RailShape.EAST_WEST || (blockpropertytrackposition1 != RailShape.NORTH_SOUTH && blockpropertytrackposition1 != RailShape.ASCENDING_NORTH && blockpropertytrackposition1 != RailShape.ASCENDING_SOUTH)) && ((shape != RailShape.NORTH_SOUTH || (blockpropertytrackposition1 != RailShape.EAST_WEST && blockpropertytrackposition1 != RailShape.ASCENDING_EAST && blockpropertytrackposition1 != RailShape.ASCENDING_WEST)) && ((Boolean) iblockdata.getValue(PoweredRailBlock.POWERED) && (world.hasNeighborSignal(pos) || this.findPoweredRailSignal(world, pos, iblockdata, flag, distance + 1)))); // Leaf - Optimize PoweredRail
+ if (iblockdata.is((Block) this)) {
+ RailShape blockpropertytrackposition1 = (RailShape) iblockdata.getValue(PoweredRailBlock.SHAPE);
+ if (shape == RailShape.EAST_WEST && (
+ blockpropertytrackposition1 == RailShape.NORTH_SOUTH ||
+ blockpropertytrackposition1 == RailShape.ASCENDING_NORTH ||
+ blockpropertytrackposition1 == RailShape.ASCENDING_SOUTH
+ ) || shape == RailShape.NORTH_SOUTH && (
+ blockpropertytrackposition1 == RailShape.EAST_WEST ||
+ blockpropertytrackposition1 == RailShape.ASCENDING_EAST ||
+ blockpropertytrackposition1 == RailShape.ASCENDING_WEST
+ )) {
+ return false;
+ } else if ((Boolean) iblockdata.getValue(PoweredRailBlock.POWERED)) {
+ return world.hasNeighborSignal(pos) ||
+ this.findPoweredRailSignal(world, pos, iblockdata, flag, distance + 1, checkedPos);
+ } else {
+ return false;
+ }
+ }
+ return false;
}
}
@@ -120,13 +148,16 @@ public class PoweredRailBlock extends BaseRailBlock {
return;
}
// CraftBukkit end
- world.setBlock(pos, (BlockState) state.setValue(PoweredRailBlock.POWERED, flag1), 3);
- world.updateNeighborsAt(pos.below(), this);
if (((RailShape) state.getValue(PoweredRailBlock.SHAPE)).isAscending()) {
- world.updateNeighborsAt(pos.above(), this);
+ world.setBlock(pos, (BlockState) state.setValue(PoweredRailBlock.POWERED, flag1), 3);
+ world.updateNeighborsAtExceptFromFacing(pos.below(), this, Direction.UP);
+ world.updateNeighborsAtExceptFromFacing(pos.above(), this, Direction.DOWN); //isAscending
+ } else if (flag1) {
+ powerLane(world, pos, state, ((RailShape) state.getValue(PoweredRailBlock.SHAPE)));
+ } else {
+ dePowerLane(world, pos, state, ((RailShape) state.getValue(PoweredRailBlock.SHAPE)));
}
}
-
}
@Override
@@ -258,4 +289,201 @@ public class PoweredRailBlock extends BaseRailBlock {
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(PoweredRailBlock.SHAPE, PoweredRailBlock.POWERED, PoweredRailBlock.WATERLOGGED);
}
+
+ public static void giveShapeUpdate(Level world, BlockState state, BlockPos pos, BlockPos fromPos, Direction direction) {
+ BlockState oldState = world.getBlockState(pos);
+ Block.updateOrDestroy(
+ oldState,
+ oldState.updateShape(direction.getOpposite(), state, world, pos, fromPos),
+ world,
+ pos,
+ Block.UPDATE_CLIENTS & -34,
+ 0
+ );
+ }
+
+ protected boolean findPoweredRailSignal(Level world, BlockPos pos, BlockState state, boolean flag, int distance) {
+ return false;
+ }
+
+ public void powerLane(Level world, BlockPos pos, BlockState state, RailShape railShape) {
+ world.setBlock(pos, (BlockState) state.setValue(PoweredRailBlock.POWERED, true), FORCE_PLACE);
+ HashMap<BlockPos, Boolean> checkedPos = new HashMap<>();
+ checkedPos.put(pos, true);
+ int[] count = new int[2];
+ if (railShape == RailShape.NORTH_SOUTH) { //Order: +z, -z
+ for (int i = 0; i < NORTH_SOUTH_DIR.length; ++i) {
+ setRailPositionsPower(world, pos, checkedPos, count, i, NORTH_SOUTH_DIR[i]);
+ }
+ updateRails(false, world, pos, state, count);
+ } else if (railShape == RailShape.EAST_WEST) { //Order: -x, +x
+ for (int i = 0; i < EAST_WEST_DIR.length; ++i) {
+ setRailPositionsPower(world, pos, checkedPos, count, i, EAST_WEST_DIR[i]);
+ }
+ updateRails(true, world, pos, state, count);
+ }
+ }
+
+ public void dePowerLane(Level world, BlockPos pos, BlockState state, RailShape railShape) {
+ world.setBlock(pos, (BlockState) state.setValue(PoweredRailBlock.POWERED, false), FORCE_PLACE);
+ int[] count = new int[2];
+ if (railShape == RailShape.NORTH_SOUTH) { //Order: +z, -z
+ for (int i = 0; i < NORTH_SOUTH_DIR.length; ++i) {
+ setRailPositionsDePower(world, pos, count, i, NORTH_SOUTH_DIR[i]);
+ }
+ updateRails(false, world, pos, state, count);
+ } else if (railShape == RailShape.EAST_WEST) { //Order: -x, +x
+ for (int i = 0; i < EAST_WEST_DIR.length; ++i) {
+ setRailPositionsDePower(world, pos, count, i, EAST_WEST_DIR[i]);
+ }
+ updateRails(true, world, pos, state, count);
+ }
+ }
+
+ private void setRailPositionsPower(Level world, BlockPos pos, HashMap<BlockPos, Boolean> checkedPos,
+ int[] count, int i, Direction direction) {
+ for (int z = 1; z < world.purpurConfig.railActivationRange; z++) {
+ BlockPos newPos = pos.relative(direction, z);
+ BlockState state = world.getBlockState(newPos);
+ if (checkedPos.containsKey(newPos)) {
+ if (!checkedPos.get(newPos)) break;
+ count[i]++;
+ } else if (!state.is((Block) this) || state.getValue(PoweredRailBlock.POWERED) || !(
+ world.hasNeighborSignal(newPos) ||
+ this.findPoweredRailSignal(world, newPos, state, true, 0, checkedPos) ||
+ this.findPoweredRailSignal(world, newPos, state, false, 0, checkedPos)
+ )) {
+ checkedPos.put(newPos, false);
+ break;
+ } else {
+ checkedPos.put(newPos, true);
+ world.setBlock(newPos, (BlockState) state.setValue(PoweredRailBlock.POWERED, true), FORCE_PLACE);
+ count[i]++;
+ }
+ }
+ }
+
+ private void setRailPositionsDePower(Level world, BlockPos pos, int[] count, int i, Direction direction) {
+ for (int z = 1; z < world.purpurConfig.railActivationRange; z++) {
+ BlockPos newPos = pos.relative(direction, z);
+ BlockState state = world.getBlockState(newPos);
+ if (!state.is((Block) this) || !state.getValue(PoweredRailBlock.POWERED) || world.hasNeighborSignal(newPos) ||
+ this.findPoweredRailSignal(world, newPos, state, true, 0) ||
+ this.findPoweredRailSignal(world, newPos, state, false, 0)) break;
+ world.setBlock(newPos, (BlockState) state.setValue(PoweredRailBlock.POWERED, false), FORCE_PLACE);
+ count[i]++;
+ }
+ }
+
+ private void shapeUpdateEnd(Level world, BlockPos pos, BlockState state, int endPos,
+ Direction direction, int currentPos, BlockPos blockPos) {
+ if (currentPos == endPos) {
+ BlockPos newPos = pos.relative(direction, currentPos + 1);
+ giveShapeUpdate(world, state, newPos, pos, direction);
+ BlockState iblockdata = world.getBlockState(blockPos);
+ if (iblockdata.is((Block) this) && iblockdata.getValue(PoweredRailBlock.SHAPE).isAscending())
+ giveShapeUpdate(world, state, newPos.above(), pos, direction);
+ }
+ }
+
+ private void neighborUpdateEnd(Level world, BlockPos pos, int endPos, Direction direction,
+ Block block, int currentPos, BlockPos blockPos) {
+ if (currentPos == endPos) {
+ BlockPos newPos = pos.relative(direction, currentPos + 1);
+ world.neighborChanged(newPos, block, pos);
+ BlockState state = world.getBlockState(blockPos);
+ if (state.is((Block) this) && state.getValue(PoweredRailBlock.SHAPE).isAscending())
+ world.neighborChanged(newPos.above(), block, blockPos);
+ }
+ }
+
+ private void updateRailsSectionEastWestShape(Level world, BlockPos pos, int c, BlockState state,
+ Direction dir, int[] count, int countAmt) {
+ BlockPos pos1 = pos.relative(dir, c);
+ if (c == 0 && count[1] == 0)
+ giveShapeUpdate(world, state, pos1.relative(dir.getOpposite()), pos, dir.getOpposite());
+ shapeUpdateEnd(world, pos, state, countAmt, dir, c, pos1);
+ giveShapeUpdate(world, state, pos1.below(), pos, Direction.DOWN);
+ giveShapeUpdate(world, state, pos1.above(), pos, Direction.UP);
+ giveShapeUpdate(world, state, pos1.north(), pos, Direction.NORTH);
+ giveShapeUpdate(world, state, pos1.south(), pos, Direction.SOUTH);
+ }
+
+ private void updateRailsSectionNorthSouthShape(Level world, BlockPos pos, int c, BlockState state,
+ Direction dir, int[] count, int countAmt) {
+ BlockPos pos1 = pos.relative(dir, c);
+ giveShapeUpdate(world, state, pos1.west(), pos, Direction.WEST);
+ giveShapeUpdate(world, state, pos1.east(), pos, Direction.EAST);
+ giveShapeUpdate(world, state, pos1.below(), pos, Direction.DOWN);
+ giveShapeUpdate(world, state, pos1.above(), pos, Direction.UP);
+ shapeUpdateEnd(world, pos, state, countAmt, dir, c, pos1);
+ if (c == 0 && count[1] == 0)
+ giveShapeUpdate(
+ world, state, pos1.relative(dir.getOpposite()), pos, dir.getOpposite()
+ );
+ }
+
+ private void updateRails(boolean eastWest, Level world, BlockPos pos, BlockState state, int[] count) {
+ if (eastWest) {
+ for (int i = 0; i < EAST_WEST_DIR.length; ++i) {
+ int countAmt = count[i];
+ if (i == 1 && countAmt == 0) continue;
+ Direction dir = EAST_WEST_DIR[i];
+ Block block = state.getBlock();
+ for (int c = countAmt; c >= i; c--) {
+ BlockPos p = pos.relative(dir, c);
+ if (c == 0 && count[1] == 0) world.neighborChanged(p.relative(dir.getOpposite()), block, pos);
+ neighborUpdateEnd(world, pos, countAmt, dir, block, c, p);
+ world.neighborChanged(p.below(), block, pos);
+ world.neighborChanged(p.above(), block, pos);
+ world.neighborChanged(p.north(), block, pos);
+ world.neighborChanged(p.south(), block, pos);
+ BlockPos pos2 = pos.relative(dir, c).below();
+ world.neighborChanged(pos2.below(), block, pos);
+ world.neighborChanged(pos2.north(), block, pos);
+ world.neighborChanged(pos2.south(), block, pos);
+ if (c == countAmt) world.neighborChanged(pos.relative(dir, c + 1).below(), block, pos);
+ if (c == 0 && count[1] == 0)
+ world.neighborChanged(p.relative(dir.getOpposite()).below(), block, pos);
+ }
+ if (org.dreeam.leaf.LeafConfig.reIntroduceReverseRailUpdateOrder) {
+ for (int c = i; c <= countAmt; c++)
+ updateRailsSectionEastWestShape(world, pos, c, state, dir, count, countAmt);
+ } else {
+ for (int c = countAmt; c >= i; c--)
+ updateRailsSectionEastWestShape(world, pos, c, state, dir, count, countAmt);
+ }
+ }
+ } else {
+ for (int i = 0; i < NORTH_SOUTH_DIR.length; ++i) {
+ int countAmt = count[i];
+ if (i == 1 && countAmt == 0) continue;
+ Direction dir = NORTH_SOUTH_DIR[i];
+ Block block = state.getBlock();
+ for (int c = countAmt; c >= i; c--) {
+ BlockPos p = pos.relative(dir, c);
+ world.neighborChanged(p.west(), block, pos);
+ world.neighborChanged(p.east(), block, pos);
+ world.neighborChanged(p.below(), block, pos);
+ world.neighborChanged(p.above(), block, pos);
+ neighborUpdateEnd(world, pos, countAmt, dir, block, c, p);
+ if (c == 0 && count[1] == 0) world.neighborChanged(p.relative(dir.getOpposite()), block, pos);
+ BlockPos pos2 = pos.relative(dir, c).below();
+ world.neighborChanged(pos2.west(), block, pos);
+ world.neighborChanged(pos2.east(), block, pos);
+ world.neighborChanged(pos2.below(), block, pos);
+ if (c == countAmt) world.neighborChanged(pos.relative(dir, c + 1).below(), block, pos);
+ if (c == 0 && count[1] == 0)
+ world.neighborChanged(p.relative(dir.getOpposite()).below(), block, pos);
+ }
+ if (org.dreeam.leaf.LeafConfig.reIntroduceReverseRailUpdateOrder) {
+ for (int c = i; c <= countAmt; c++)
+ updateRailsSectionNorthSouthShape(world, pos, c, state, dir, count, countAmt);
+ } else {
+ for (int c = countAmt; c >= i; c--)
+ updateRailsSectionNorthSouthShape(world, pos, c, state, dir, count, countAmt);
+ }
+ }
+ }
+ }
}
diff --git a/src/main/java/org/dreeam/leaf/LeafConfig.java b/src/main/java/org/dreeam/leaf/LeafConfig.java
index 11d3ccad179680c46b45e17b4461c5da3d9d5592..a285d391c35bc24250c85f68ac228f2d41456c78 100644
--- a/src/main/java/org/dreeam/leaf/LeafConfig.java
+++ b/src/main/java/org/dreeam/leaf/LeafConfig.java
@@ -203,6 +203,8 @@ public class LeafConfig {
public static boolean enableAsyncEntityTrackerInitialized;
public static boolean enableSyncEventCallsOnAsyncThreads;
public static boolean enableSyncEventCallsOnAsyncThreadsInitialized;
+ public static boolean optimizedPoweredRails = false;
+ public static boolean reIntroduceReverseRailUpdateOrder = false;
private static void performance() {
String sentryEnvironment = System.getenv("SENTRY_DSN");
String sentryConfig = getString("performance.sentry-dsn", sentryDsn, "Sentry DSN for improved error logging, leave blank to disable", "Obtain from https://sentry.io/");
@@ -280,6 +282,8 @@ public class LeafConfig {
enableSyncEventCallsOnAsyncThreadsInitialized = true;
enableSyncEventCallsOnAsyncThreads = syncEventCalls;
}
+ optimizedPoweredRails = getBoolean("performance.optimizedPoweredRails", optimizedPoweredRails);
+ reIntroduceReverseRailUpdateOrder = getBoolean("performance.reIntroduceReverseRailUpdateOrder", reIntroduceReverseRailUpdateOrder);
}
public static boolean jadeProtocol = false;