From 34e15fada48467a28c701e3baabca291869b583d Mon Sep 17 00:00:00 2001 From: daoge_cmd <3523206925@qq.com> Date: Tue, 17 Dec 2024 22:00:40 +0800 Subject: [PATCH] feat: correct random tick speed to match vanilla and improve the performance --- .../org/allaymc/server/datastruct/palette/Palette.java | 10 ++++++++++ .../org/allaymc/server/world/chunk/AllayChunk.java | 8 +++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/org/allaymc/server/datastruct/palette/Palette.java b/server/src/main/java/org/allaymc/server/datastruct/palette/Palette.java index e85127425..0786bd071 100644 --- a/server/src/main/java/org/allaymc/server/datastruct/palette/Palette.java +++ b/server/src/main/java/org/allaymc/server/datastruct/palette/Palette.java @@ -1,6 +1,7 @@ package org.allaymc.server.datastruct.palette; import com.google.common.base.Objects; +import com.google.common.base.Predicate; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufOutputStream; import it.unimi.dsi.fastutil.objects.ReferenceArrayList; @@ -207,6 +208,15 @@ public boolean oneEntryOnly() { return true; } + public boolean allEntriesMatch(Predicate predicate) { + for (var entry : palette) { + if (!predicate.test(entry)) { + return false; + } + } + return true; + } + public void copyTo(Palette palette) { palette.bitArray = this.bitArray.copy(); palette.palette.clear(); diff --git a/server/src/main/java/org/allaymc/server/world/chunk/AllayChunk.java b/server/src/main/java/org/allaymc/server/world/chunk/AllayChunk.java index 5d3c5925d..e3967ea0b 100644 --- a/server/src/main/java/org/allaymc/server/world/chunk/AllayChunk.java +++ b/server/src/main/java/org/allaymc/server/world/chunk/AllayChunk.java @@ -130,9 +130,15 @@ protected void tickRandomUpdates(Dimension dimension) { if (section.isAirSection()) { continue; } + // Check the entry list of this section, and + // if there is no block that support random tick + // in this section, we can just skip this section + if (section.blockLayers()[0].allEntriesMatch(blockState -> !blockState.getBehavior().canRandomUpdate())) { + continue; + } int sectionY = section.sectionY(); - for (int i = 0; i < randomTickSpeed; i++) { + for (int i = 0; i < randomTickSpeed * 3; i++) { int lcg = nextUpdateLCG(); int localX = lcg & 0x0f; int localZ = lcg >>> 8 & 0x0f;