diff --git a/shiroha-server/minecraft-patches/features/0028-Reduce-ticket-operations-in-nether-portal-searching.patch b/shiroha-server/minecraft-patches/features/0028-Reduce-ticket-operations-in-nether-portal-searching.patch new file mode 100644 index 0000000..41e96bc --- /dev/null +++ b/shiroha-server/minecraft-patches/features/0028-Reduce-ticket-operations-in-nether-portal-searching.patch @@ -0,0 +1,43 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: NanaChiyo0721 +Date: Thu, 30 Jul 2026 21:16:31 +0800 +Subject: [PATCH] Reduce ticket operations in nether portal searching + + +diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java +index d25b1c7d1347b2731cd12cdcea7190276913de49..878a00bb28223b2719b4ce7c5d715e0346364770 100644 +--- a/net/minecraft/server/level/ServerLevel.java ++++ b/net/minecraft/server/level/ServerLevel.java +@@ -221,6 +221,7 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet + private final StructureCheck structureCheck; + public final boolean tickTime; // Folia - region threading + private final LevelDebugSynchronizers debugSynchronizers = new LevelDebugSynchronizers(this); ++ public final io.nanachiyo0721.shiroha.utils.ReferenceCountingChunkLoader portalSearchingChunkLoader = new io.nanachiyo0721.shiroha.utils.ReferenceCountingChunkLoader(this, net.minecraft.world.level.chunk.status.ChunkStatus.EMPTY); // Shiroha - Reduce ticket operations in nether portal searching + + // CraftBukkit start + private final ResourceKey typeKey; +@@ -965,6 +966,7 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet + + this.debugSynchronizers.tick(this.server.debugSubscribers()); + profiler.pop(); ++ this.portalSearchingChunkLoader.tickChunkReferenceTTL(); // Shiroha - Reduce ticket operations in nether portal searching + } + + // Folia start - region threading +diff --git a/net/minecraft/world/entity/Entity.java b/net/minecraft/world/entity/Entity.java +index 63c1d223d9fc16b4211e992bde3c9f08d3ed15ca..292b062f55b7f37346750ed3a59ed4fb64c92df8 100644 +--- a/net/minecraft/world/entity/Entity.java ++++ b/net/minecraft/world/entity/Entity.java +@@ -4742,10 +4742,10 @@ public abstract class Entity + ); + + // kick off search for existing portal or creation +- destination.moonrise$loadChunksAsync( ++ destination.portalSearchingChunkLoader.loadChunksAsync( // Shiroha - Reduce ticket operations in nether portal searching + // add 32 so that the final search for a portal frame doesn't load any chunks + targetPos, portalSearchRadius + 32, +- net.minecraft.world.level.chunk.status.ChunkStatus.EMPTY, ++ //net.minecraft.world.level.chunk.status.ChunkStatus.EMPTY, // Shiroha - Reduce ticket operations in nether portal searching + ca.spottedleaf.concurrentutil.util.Priority.HIGH, + (chunks) -> { + BlockUtil.FoundRectangle portal = diff --git a/shiroha-server/minecraft-patches/features/0028-Replace-packet-processing-queue-with-mt-queue.patch b/shiroha-server/minecraft-patches/features/0029-Replace-packet-processing-queue-with-mt-queue.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0028-Replace-packet-processing-queue-with-mt-queue.patch rename to shiroha-server/minecraft-patches/features/0029-Replace-packet-processing-queue-with-mt-queue.patch diff --git a/shiroha-server/minecraft-patches/features/0029-Async-protocol-switching-optimization.patch b/shiroha-server/minecraft-patches/features/0030-Async-protocol-switching-optimization.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0029-Async-protocol-switching-optimization.patch rename to shiroha-server/minecraft-patches/features/0030-Async-protocol-switching-optimization.patch diff --git a/shiroha-server/minecraft-patches/features/0030-Improved-server-health-command.patch b/shiroha-server/minecraft-patches/features/0031-Improved-server-health-command.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0030-Improved-server-health-command.patch rename to shiroha-server/minecraft-patches/features/0031-Improved-server-health-command.patch diff --git a/shiroha-server/minecraft-patches/features/0031-Kaiiju-Entity-tick-and-removal-limiter.patch b/shiroha-server/minecraft-patches/features/0032-Kaiiju-Entity-tick-and-removal-limiter.patch similarity index 94% rename from shiroha-server/minecraft-patches/features/0031-Kaiiju-Entity-tick-and-removal-limiter.patch rename to shiroha-server/minecraft-patches/features/0032-Kaiiju-Entity-tick-and-removal-limiter.patch index 20b3826..9ca4167 100644 --- a/shiroha-server/minecraft-patches/features/0031-Kaiiju-Entity-tick-and-removal-limiter.patch +++ b/shiroha-server/minecraft-patches/features/0032-Kaiiju-Entity-tick-and-removal-limiter.patch @@ -20,10 +20,10 @@ index 3333fc2a564531ece613ed2ac77db99c59ec7390..df9a8fba86b61d2d2f82e40e0f317cab // block ticking private final ObjectLinkedOpenHashSet blockEvents = new ObjectLinkedOpenHashSet<>(); diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java -index d25b1c7d1347b2731cd12cdcea7190276913de49..5668f80c2ac1fcf2ac0e1e5378c789d7fce655f3 100644 +index 878a00bb28223b2719b4ce7c5d715e0346364770..e89d2af26ffe83e71cfb733cfdbec542f845f4e5 100644 --- a/net/minecraft/server/level/ServerLevel.java +++ b/net/minecraft/server/level/ServerLevel.java -@@ -911,6 +911,7 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet +@@ -912,6 +912,7 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet } foliaProfiler.startTimer(ca.spottedleaf.leafprofiler.LProfilerRegistry.ACTIVATE_ENTITIES); try { // Folia - profiler @@ -31,7 +31,7 @@ index d25b1c7d1347b2731cd12cdcea7190276913de49..5668f80c2ac1fcf2ac0e1e5378c789d7 io.papermc.paper.entity.activation.ActivationRange.activateEntities(this); // Paper - EAR } finally { foliaProfiler.stopTimer(ca.spottedleaf.leafprofiler.LProfilerRegistry.ACTIVATE_ENTITIES); } // Folia - profiler foliaProfiler.startTimer(ca.spottedleaf.leafprofiler.LProfilerRegistry.ENTITY_TICK); try { // Folia - profiler -@@ -932,6 +933,13 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet +@@ -933,6 +934,13 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet entity.stopRiding(); } @@ -45,7 +45,7 @@ index d25b1c7d1347b2731cd12cdcea7190276913de49..5668f80c2ac1fcf2ac0e1e5378c789d7 profiler.push("tick"); this.guardEntityTick(this::tickNonPassenger, entity); -@@ -941,6 +949,7 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet +@@ -942,6 +950,7 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet } } ); diff --git a/shiroha-server/minecraft-patches/features/0032-Kaiiju-Vanilla-end-portal-teleportation.patch b/shiroha-server/minecraft-patches/features/0033-Kaiiju-Vanilla-end-portal-teleportation.patch similarity index 98% rename from shiroha-server/minecraft-patches/features/0032-Kaiiju-Vanilla-end-portal-teleportation.patch rename to shiroha-server/minecraft-patches/features/0033-Kaiiju-Vanilla-end-portal-teleportation.patch index 148b8ee..65433d8 100644 --- a/shiroha-server/minecraft-patches/features/0032-Kaiiju-Vanilla-end-portal-teleportation.patch +++ b/shiroha-server/minecraft-patches/features/0033-Kaiiju-Vanilla-end-portal-teleportation.patch @@ -8,7 +8,7 @@ As part of: Kaiiju (https://github.com/KaiijuMC/Kaiiju/blob/c2b7aec8f7b418a39a2e Licensed under: GPL-3.0 (https://github.com/KaiijuMC/Kaiiju/blob/c2b7aec8f7b418a39a2ec408e6411e6f752379da/LICENSE) diff --git a/net/minecraft/world/entity/Entity.java b/net/minecraft/world/entity/Entity.java -index 63c1d223d9fc16b4211e992bde3c9f08d3ed15ca..dda97d7e3d3a997ae4a2b6ee1656c165d5f349c4 100644 +index 292b062f55b7f37346750ed3a59ed4fb64c92df8..06fa367e6ea5411bbb433153323867969de0156e 100644 --- a/net/minecraft/world/entity/Entity.java +++ b/net/minecraft/world/entity/Entity.java @@ -4640,15 +4640,19 @@ public abstract class Entity diff --git a/shiroha-server/minecraft-patches/features/0033-Add-config-to-support-for-long-command-inputs.patch b/shiroha-server/minecraft-patches/features/0034-Add-config-to-support-for-long-command-inputs.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0033-Add-config-to-support-for-long-command-inputs.patch rename to shiroha-server/minecraft-patches/features/0034-Add-config-to-support-for-long-command-inputs.patch diff --git a/shiroha-server/minecraft-patches/features/0034-Add-config-for-server-mod-name.patch b/shiroha-server/minecraft-patches/features/0035-Add-config-for-server-mod-name.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0034-Add-config-for-server-mod-name.patch rename to shiroha-server/minecraft-patches/features/0035-Add-config-for-server-mod-name.patch diff --git a/shiroha-server/minecraft-patches/features/0035-Add-config-for-cpu-affinity.patch b/shiroha-server/minecraft-patches/features/0036-Add-config-for-cpu-affinity.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0035-Add-config-for-cpu-affinity.patch rename to shiroha-server/minecraft-patches/features/0036-Add-config-for-cpu-affinity.patch diff --git a/shiroha-server/minecraft-patches/features/0036-Add-config-for-unsafe-teleportation.patch b/shiroha-server/minecraft-patches/features/0037-Add-config-for-unsafe-teleportation.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0036-Add-config-for-unsafe-teleportation.patch rename to shiroha-server/minecraft-patches/features/0037-Add-config-for-unsafe-teleportation.patch diff --git a/shiroha-server/minecraft-patches/features/0037-Add-config-for-watchdog-timeout.patch b/shiroha-server/minecraft-patches/features/0038-Add-config-for-watchdog-timeout.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0037-Add-config-for-watchdog-timeout.patch rename to shiroha-server/minecraft-patches/features/0038-Add-config-for-watchdog-timeout.patch diff --git a/shiroha-server/minecraft-patches/features/0038-Add-config-to-disable-entity-tick-catchers.patch b/shiroha-server/minecraft-patches/features/0039-Add-config-to-disable-entity-tick-catchers.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0038-Add-config-to-disable-entity-tick-catchers.patch rename to shiroha-server/minecraft-patches/features/0039-Add-config-to-disable-entity-tick-catchers.patch diff --git a/shiroha-server/minecraft-patches/features/0039-Add-config-for-heightmap-warning.patch b/shiroha-server/minecraft-patches/features/0040-Add-config-for-heightmap-warning.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0039-Add-config-for-heightmap-warning.patch rename to shiroha-server/minecraft-patches/features/0040-Add-config-for-heightmap-warning.patch diff --git a/shiroha-server/minecraft-patches/features/0040-Add-config-gui-support.patch b/shiroha-server/minecraft-patches/features/0041-Add-config-gui-support.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0040-Add-config-gui-support.patch rename to shiroha-server/minecraft-patches/features/0041-Add-config-gui-support.patch diff --git a/shiroha-server/minecraft-patches/features/0041-Add-force-the-data-command-to-be-enabled-config.patch b/shiroha-server/minecraft-patches/features/0042-Add-force-the-data-command-to-be-enabled-config.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0041-Add-force-the-data-command-to-be-enabled-config.patch rename to shiroha-server/minecraft-patches/features/0042-Add-force-the-data-command-to-be-enabled-config.patch diff --git a/shiroha-server/minecraft-patches/features/0042-Add-experimental-config-for-command-block-command-ex.patch b/shiroha-server/minecraft-patches/features/0043-Add-experimental-config-for-command-block-command-ex.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0042-Add-experimental-config-for-command-block-command-ex.patch rename to shiroha-server/minecraft-patches/features/0043-Add-experimental-config-for-command-block-command-ex.patch diff --git a/shiroha-server/minecraft-patches/features/0043-Add-config-to-modify-tripwire-behavior.patch b/shiroha-server/minecraft-patches/features/0044-Add-config-to-modify-tripwire-behavior.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0043-Add-config-to-modify-tripwire-behavior.patch rename to shiroha-server/minecraft-patches/features/0044-Add-config-to-modify-tripwire-behavior.patch diff --git a/shiroha-server/minecraft-patches/features/0044-Add-config-for-item-multitask.patch b/shiroha-server/minecraft-patches/features/0045-Add-config-for-item-multitask.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0044-Add-config-for-item-multitask.patch rename to shiroha-server/minecraft-patches/features/0045-Add-config-for-item-multitask.patch diff --git a/shiroha-server/minecraft-patches/features/0045-Add-config-to-enable-tick-command.patch b/shiroha-server/minecraft-patches/features/0046-Add-config-to-enable-tick-command.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0045-Add-config-to-enable-tick-command.patch rename to shiroha-server/minecraft-patches/features/0046-Add-config-to-enable-tick-command.patch diff --git a/shiroha-server/minecraft-patches/features/0046-Add-config-for-whether-to-save-portal-tickets.patch b/shiroha-server/minecraft-patches/features/0047-Add-config-for-whether-to-save-portal-tickets.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0046-Add-config-for-whether-to-save-portal-tickets.patch rename to shiroha-server/minecraft-patches/features/0047-Add-config-for-whether-to-save-portal-tickets.patch diff --git a/shiroha-server/minecraft-patches/features/0047-Add-portal-rate-limiter.patch b/shiroha-server/minecraft-patches/features/0048-Add-portal-rate-limiter.patch similarity index 96% rename from shiroha-server/minecraft-patches/features/0047-Add-portal-rate-limiter.patch rename to shiroha-server/minecraft-patches/features/0048-Add-portal-rate-limiter.patch index 43d3139..8964d9b 100644 --- a/shiroha-server/minecraft-patches/features/0047-Add-portal-rate-limiter.patch +++ b/shiroha-server/minecraft-patches/features/0048-Add-portal-rate-limiter.patch @@ -93,10 +93,10 @@ index 3ecedf303384c172a5e2a19c9e6ac0c3a43cef74..ee7fce1e4e9b94b1f9c956c376fff0ab //this.isIteratingOverLevels = false; // Paper - Throw exception on world create while being ticked // Folia - region threading diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java -index 5668f80c2ac1fcf2ac0e1e5378c789d7fce655f3..ecb1b98723e78e9d2f3ce182be8f78f8f32a52da 100644 +index e89d2af26ffe83e71cfb733cfdbec542f845f4e5..1505a920694068260d9d81bc4f3c96f3a154f19d 100644 --- a/net/minecraft/server/level/ServerLevel.java +++ b/net/minecraft/server/level/ServerLevel.java -@@ -1536,8 +1536,15 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet +@@ -1538,8 +1538,15 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet // removed from region while ticking return; } @@ -112,7 +112,7 @@ index 5668f80c2ac1fcf2ac0e1e5378c789d7fce655f3..ecb1b98723e78e9d2f3ce182be8f78f8 return; } } -@@ -1581,8 +1588,15 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet +@@ -1583,8 +1590,15 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet // removed from region while ticking return; } diff --git a/shiroha-server/minecraft-patches/features/0048-Add-status-bar.patch b/shiroha-server/minecraft-patches/features/0049-Add-status-bar.patch similarity index 97% rename from shiroha-server/minecraft-patches/features/0048-Add-status-bar.patch rename to shiroha-server/minecraft-patches/features/0049-Add-status-bar.patch index f21d6ac..85f5a54 100644 --- a/shiroha-server/minecraft-patches/features/0048-Add-status-bar.patch +++ b/shiroha-server/minecraft-patches/features/0049-Add-status-bar.patch @@ -68,10 +68,10 @@ index ee7fce1e4e9b94b1f9c956c376fff0abee51b0ca..87099cabe804741e6dc278bccdfa79b0 this.lastServerStatus = nano; this.status = this.buildServerStatus(); diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java -index ecb1b98723e78e9d2f3ce182be8f78f8f32a52da..ce0dca54b551c67797e197f848334f34e1fb3da7 100644 +index 1505a920694068260d9d81bc4f3c96f3a154f19d..52d74e9df3a01163014ae50ce4f49eba6639ec63 100644 --- a/net/minecraft/server/level/ServerLevel.java +++ b/net/minecraft/server/level/ServerLevel.java -@@ -1485,6 +1485,8 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet +@@ -1487,6 +1487,8 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet public void tickNonPassenger(final Entity entity) { // Paper start - log detailed entity tick information ca.spottedleaf.moonrise.common.util.TickThread.ensureTickThread("Cannot tick an entity off-main"); @@ -80,7 +80,7 @@ index ecb1b98723e78e9d2f3ce182be8f78f8f32a52da..ce0dca54b551c67797e197f848334f34 try { // Folia - region threading // Paper end - log detailed entity tick information -@@ -1561,6 +1563,7 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet +@@ -1563,6 +1565,7 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet } finally { // Folia - region threading } @@ -88,7 +88,7 @@ index ecb1b98723e78e9d2f3ce182be8f78f8f32a52da..ce0dca54b551c67797e197f848334f34 // Paper end - log detailed entity tick information } -@@ -1572,6 +1575,8 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet +@@ -1574,6 +1577,8 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet final int timerId = isActive ? entity.getType().tickTimerId : entity.getType().inactiveTickTimerId; final ca.spottedleaf.leafprofiler.RegionizedProfiler.Handle foliaProfiler = io.papermc.paper.threadedregions.TickRegionScheduler.getProfiler(); foliaProfiler.startTimer(timerId); @@ -97,7 +97,7 @@ index ecb1b98723e78e9d2f3ce182be8f78f8f32a52da..ce0dca54b551c67797e197f848334f34 try { // Folia end - profiler entity.setOldPosAndRot(); -@@ -1613,6 +1618,7 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet +@@ -1615,6 +1620,7 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet this.tickPassenger(entity, passenger, isActive); // Paper - EAR 2 } } finally { foliaProfiler.stopTimer(timerId); } // Folia - profiler @@ -171,7 +171,7 @@ index f03fa06c0ba56f7f5e1e45bc1568a490751efde3..eee1c14249addbf4714df733870da974 + // KioCG end } diff --git a/net/minecraft/world/entity/Entity.java b/net/minecraft/world/entity/Entity.java -index dda97d7e3d3a997ae4a2b6ee1656c165d5f349c4..3f3110efa6a917f5ad3892a57229a8ae937cd023 100644 +index 06fa367e6ea5411bbb433153323867969de0156e..fb0fe5631e8fd5d1053e31e4f121601a64647f1b 100644 --- a/net/minecraft/world/entity/Entity.java +++ b/net/minecraft/world/entity/Entity.java @@ -6432,4 +6432,6 @@ public abstract class Entity diff --git a/shiroha-server/minecraft-patches/features/0049-Leaves-Vanilla-Hopper.patch b/shiroha-server/minecraft-patches/features/0050-Leaves-Vanilla-Hopper.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0049-Leaves-Vanilla-Hopper.patch rename to shiroha-server/minecraft-patches/features/0050-Leaves-Vanilla-Hopper.patch diff --git a/shiroha-server/minecraft-patches/features/0050-Leaves-Lithium-Sleeping-Block-Entity.patch b/shiroha-server/minecraft-patches/features/0051-Leaves-Lithium-Sleeping-Block-Entity.patch similarity index 99% rename from shiroha-server/minecraft-patches/features/0050-Leaves-Lithium-Sleeping-Block-Entity.patch rename to shiroha-server/minecraft-patches/features/0051-Leaves-Lithium-Sleeping-Block-Entity.patch index 744e125..60e1b74 100644 --- a/shiroha-server/minecraft-patches/features/0050-Leaves-Lithium-Sleeping-Block-Entity.patch +++ b/shiroha-server/minecraft-patches/features/0051-Leaves-Lithium-Sleeping-Block-Entity.patch @@ -240,7 +240,7 @@ index 6e08d50794c4af4405f3e164a3fd46f376b3f78f..dd2d0940eece3e85078e574f66f71a4c int getContainerSize(); diff --git a/net/minecraft/world/entity/Entity.java b/net/minecraft/world/entity/Entity.java -index 3f3110efa6a917f5ad3892a57229a8ae937cd023..34daca8ff9cbd03458ac864ed3d31f6603301864 100644 +index fb0fe5631e8fd5d1053e31e4f121601a64647f1b..6b736388138ff58daeffae6ada470b9045fd51b6 100644 --- a/net/minecraft/world/entity/Entity.java +++ b/net/minecraft/world/entity/Entity.java @@ -324,7 +324,7 @@ public abstract class Entity diff --git a/shiroha-server/minecraft-patches/features/0051-Petal-Reduce-sensor-work.patch b/shiroha-server/minecraft-patches/features/0052-Petal-Reduce-sensor-work.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0051-Petal-Reduce-sensor-work.patch rename to shiroha-server/minecraft-patches/features/0052-Petal-Reduce-sensor-work.patch diff --git a/shiroha-server/minecraft-patches/features/0052-Purpur-Lobotomize-stuck-villagers.patch b/shiroha-server/minecraft-patches/features/0053-Purpur-Lobotomize-stuck-villagers.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0052-Purpur-Lobotomize-stuck-villagers.patch rename to shiroha-server/minecraft-patches/features/0053-Purpur-Lobotomize-stuck-villagers.patch diff --git a/shiroha-server/minecraft-patches/features/0053-Pufferfish-Reduce-projectile-chunk-loading.patch b/shiroha-server/minecraft-patches/features/0054-Pufferfish-Reduce-projectile-chunk-loading.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0053-Pufferfish-Reduce-projectile-chunk-loading.patch rename to shiroha-server/minecraft-patches/features/0054-Pufferfish-Reduce-projectile-chunk-loading.patch diff --git a/shiroha-server/minecraft-patches/features/0054-Pufferfish-Throttle-goal-selector-during-inactive-ti.patch b/shiroha-server/minecraft-patches/features/0055-Pufferfish-Throttle-goal-selector-during-inactive-ti.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0054-Pufferfish-Throttle-goal-selector-during-inactive-ti.patch rename to shiroha-server/minecraft-patches/features/0055-Pufferfish-Throttle-goal-selector-during-inactive-ti.patch diff --git a/shiroha-server/minecraft-patches/features/0055-Leaf-Secure-seed-and-matter-seed-command.patch b/shiroha-server/minecraft-patches/features/0056-Leaf-Secure-seed-and-matter-seed-command.patch similarity index 99% rename from shiroha-server/minecraft-patches/features/0055-Leaf-Secure-seed-and-matter-seed-command.patch rename to shiroha-server/minecraft-patches/features/0056-Leaf-Secure-seed-and-matter-seed-command.patch index dc76424..5bcb0cc 100644 --- a/shiroha-server/minecraft-patches/features/0055-Leaf-Secure-seed-and-matter-seed-command.patch +++ b/shiroha-server/minecraft-patches/features/0056-Leaf-Secure-seed-and-matter-seed-command.patch @@ -63,10 +63,10 @@ index 7927769cf9bccb892745f4d1390eb83b2861d1bb..414c21345879f589fb61130180d0ca60 } diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java -index ce0dca54b551c67797e197f848334f34e1fb3da7..8634e0b6d280e2c5900197420ad457f778245087 100644 +index 52d74e9df3a01163014ae50ce4f49eba6639ec63..8c2d6a43b52ae6e70506bb779a9dfd33a7a1d440 100644 --- a/net/minecraft/server/level/ServerLevel.java +++ b/net/minecraft/server/level/ServerLevel.java -@@ -633,6 +633,7 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet +@@ -634,6 +634,7 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet generator = new org.bukkit.craftbukkit.generator.CustomChunkGenerator(this, generator, gen); } // CraftBukkit end diff --git a/shiroha-server/minecraft-patches/features/0056-Secure-seed-V2-with-Blake3.patch b/shiroha-server/minecraft-patches/features/0057-Secure-seed-V2-with-Blake3.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0056-Secure-seed-V2-with-Blake3.patch rename to shiroha-server/minecraft-patches/features/0057-Secure-seed-V2-with-Blake3.patch diff --git a/shiroha-server/minecraft-patches/features/0057-Leaf-Replace-brain-maps-with-optimized-collection.patch b/shiroha-server/minecraft-patches/features/0058-Leaf-Replace-brain-maps-with-optimized-collection.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0057-Leaf-Replace-brain-maps-with-optimized-collection.patch rename to shiroha-server/minecraft-patches/features/0058-Leaf-Replace-brain-maps-with-optimized-collection.patch diff --git a/shiroha-server/minecraft-patches/features/0058-Leaf-Remove-useless-creating-stats-json-bases-on-pla.patch b/shiroha-server/minecraft-patches/features/0059-Leaf-Remove-useless-creating-stats-json-bases-on-pla.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0058-Leaf-Remove-useless-creating-stats-json-bases-on-pla.patch rename to shiroha-server/minecraft-patches/features/0059-Leaf-Remove-useless-creating-stats-json-bases-on-pla.patch diff --git a/shiroha-server/minecraft-patches/features/0059-Leaf-Optimize-PatchedDataComponentMap-equals.patch b/shiroha-server/minecraft-patches/features/0060-Leaf-Optimize-PatchedDataComponentMap-equals.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0059-Leaf-Optimize-PatchedDataComponentMap-equals.patch rename to shiroha-server/minecraft-patches/features/0060-Leaf-Optimize-PatchedDataComponentMap-equals.patch diff --git a/shiroha-server/minecraft-patches/features/0060-Leaf-Better-checking-for-useless-move-packets.patch b/shiroha-server/minecraft-patches/features/0061-Leaf-Better-checking-for-useless-move-packets.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0060-Leaf-Better-checking-for-useless-move-packets.patch rename to shiroha-server/minecraft-patches/features/0061-Leaf-Better-checking-for-useless-move-packets.patch diff --git a/shiroha-server/minecraft-patches/features/0061-Leaf-fast-bit-radix-sort.patch b/shiroha-server/minecraft-patches/features/0062-Leaf-fast-bit-radix-sort.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0061-Leaf-fast-bit-radix-sort.patch rename to shiroha-server/minecraft-patches/features/0062-Leaf-fast-bit-radix-sort.patch diff --git a/shiroha-server/minecraft-patches/features/0062-Leaf-Configurable-vanilla-username-check.patch b/shiroha-server/minecraft-patches/features/0063-Leaf-Configurable-vanilla-username-check.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0062-Leaf-Configurable-vanilla-username-check.patch rename to shiroha-server/minecraft-patches/features/0063-Leaf-Configurable-vanilla-username-check.patch diff --git a/shiroha-server/minecraft-patches/features/0063-Leaf-Lithium-faster-hash-palette.patch b/shiroha-server/minecraft-patches/features/0064-Leaf-Lithium-faster-hash-palette.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0063-Leaf-Lithium-faster-hash-palette.patch rename to shiroha-server/minecraft-patches/features/0064-Leaf-Lithium-faster-hash-palette.patch diff --git a/shiroha-server/minecraft-patches/features/0064-Leaves-Disable-packet-limit-Shiroha-Option-to-full-d.patch b/shiroha-server/minecraft-patches/features/0065-Leaves-Disable-packet-limit-Shiroha-Option-to-full-d.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0064-Leaves-Disable-packet-limit-Shiroha-Option-to-full-d.patch rename to shiroha-server/minecraft-patches/features/0065-Leaves-Disable-packet-limit-Shiroha-Option-to-full-d.patch diff --git a/shiroha-server/minecraft-patches/features/0065-Leaves-Configurable-collision-behavior.patch b/shiroha-server/minecraft-patches/features/0066-Leaves-Configurable-collision-behavior.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0065-Leaves-Configurable-collision-behavior.patch rename to shiroha-server/minecraft-patches/features/0066-Leaves-Configurable-collision-behavior.patch diff --git a/shiroha-server/minecraft-patches/features/0066-Leaves-Optimized-dragon-respawn.patch b/shiroha-server/minecraft-patches/features/0067-Leaves-Optimized-dragon-respawn.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0066-Leaves-Optimized-dragon-respawn.patch rename to shiroha-server/minecraft-patches/features/0067-Leaves-Optimized-dragon-respawn.patch diff --git a/shiroha-server/minecraft-patches/features/0067-Gale-Variable-entity-wake-up-duration.patch b/shiroha-server/minecraft-patches/features/0068-Gale-Variable-entity-wake-up-duration.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0067-Gale-Variable-entity-wake-up-duration.patch rename to shiroha-server/minecraft-patches/features/0068-Gale-Variable-entity-wake-up-duration.patch diff --git a/shiroha-server/minecraft-patches/features/0068-Gale-Replace-AI-attributes-with-optimized-collection.patch b/shiroha-server/minecraft-patches/features/0069-Gale-Replace-AI-attributes-with-optimized-collection.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0068-Gale-Replace-AI-attributes-with-optimized-collection.patch rename to shiroha-server/minecraft-patches/features/0069-Gale-Replace-AI-attributes-with-optimized-collection.patch diff --git a/shiroha-server/minecraft-patches/features/0069-Gale-Skip-entity-move-if-movement-is-zero.patch b/shiroha-server/minecraft-patches/features/0070-Gale-Skip-entity-move-if-movement-is-zero.patch similarity index 95% rename from shiroha-server/minecraft-patches/features/0069-Gale-Skip-entity-move-if-movement-is-zero.patch rename to shiroha-server/minecraft-patches/features/0070-Gale-Skip-entity-move-if-movement-is-zero.patch index f996e0d..248013b 100644 --- a/shiroha-server/minecraft-patches/features/0069-Gale-Skip-entity-move-if-movement-is-zero.patch +++ b/shiroha-server/minecraft-patches/features/0070-Gale-Skip-entity-move-if-movement-is-zero.patch @@ -10,7 +10,7 @@ VMP (https://github.com/RelativityMC/VMP-fabric) Licensed under: MIT (https://opensource.org/licenses/MIT) diff --git a/net/minecraft/world/entity/Entity.java b/net/minecraft/world/entity/Entity.java -index 34daca8ff9cbd03458ac864ed3d31f6603301864..e7497f8f488cf9833b428ce1c82451c902465d19 100644 +index 6b736388138ff58daeffae6ada470b9045fd51b6..fcca07953fb91d5b1b7230682b657ea289d779b7 100644 --- a/net/minecraft/world/entity/Entity.java +++ b/net/minecraft/world/entity/Entity.java @@ -1152,8 +1152,14 @@ public abstract class Entity diff --git a/shiroha-server/minecraft-patches/features/0070-Gale-Store-mob-counts-in-an-array.patch b/shiroha-server/minecraft-patches/features/0071-Gale-Store-mob-counts-in-an-array.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0070-Gale-Store-mob-counts-in-an-array.patch rename to shiroha-server/minecraft-patches/features/0071-Gale-Store-mob-counts-in-an-array.patch diff --git a/shiroha-server/minecraft-patches/features/0071-Gale-Optimize-random-calls-in-chunk-ticking.patch b/shiroha-server/minecraft-patches/features/0072-Gale-Optimize-random-calls-in-chunk-ticking.patch similarity index 96% rename from shiroha-server/minecraft-patches/features/0071-Gale-Optimize-random-calls-in-chunk-ticking.patch rename to shiroha-server/minecraft-patches/features/0072-Gale-Optimize-random-calls-in-chunk-ticking.patch index 51b4b5a..8ae1c0b 100644 --- a/shiroha-server/minecraft-patches/features/0071-Gale-Optimize-random-calls-in-chunk-ticking.patch +++ b/shiroha-server/minecraft-patches/features/0072-Gale-Optimize-random-calls-in-chunk-ticking.patch @@ -57,10 +57,10 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java -index 8634e0b6d280e2c5900197420ad457f778245087..05aa5cd8869d4997da137661dcdc9f2f61c6e438 100644 +index 8c2d6a43b52ae6e70506bb779a9dfd33a7a1d440..5506018ee75eaf13d072969699a41566cf054cf5 100644 --- a/net/minecraft/server/level/ServerLevel.java +++ b/net/minecraft/server/level/ServerLevel.java -@@ -1052,7 +1052,7 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet +@@ -1054,7 +1054,7 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet } // Paper start - optimise random ticking @@ -69,7 +69,7 @@ index 8634e0b6d280e2c5900197420ad457f778245087..05aa5cd8869d4997da137661dcdc9f2f private void optimiseRandomTick(final LevelChunk chunk, final int tickSpeed) { final LevelChunkSection[] sections = chunk.getSections(); -@@ -1134,7 +1134,7 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet +@@ -1136,7 +1136,7 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet int minZ = chunkPos.getMinBlockZ(); ProfilerFiller profiler = Profiler.get(); profiler.push("thunder"); diff --git a/shiroha-server/minecraft-patches/features/0072-Gale-Reduce-enderman-teleport-chunk-lookups.patch b/shiroha-server/minecraft-patches/features/0073-Gale-Reduce-enderman-teleport-chunk-lookups.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0072-Gale-Reduce-enderman-teleport-chunk-lookups.patch rename to shiroha-server/minecraft-patches/features/0073-Gale-Reduce-enderman-teleport-chunk-lookups.patch diff --git a/shiroha-server/minecraft-patches/features/0073-Gale-Cache-ShapePairKey-hash.patch b/shiroha-server/minecraft-patches/features/0074-Gale-Cache-ShapePairKey-hash.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0073-Gale-Cache-ShapePairKey-hash.patch rename to shiroha-server/minecraft-patches/features/0074-Gale-Cache-ShapePairKey-hash.patch diff --git a/shiroha-server/minecraft-patches/features/0074-Gale-For-collision-check-has-physics-before-same-veh.patch b/shiroha-server/minecraft-patches/features/0075-Gale-For-collision-check-has-physics-before-same-veh.patch similarity index 94% rename from shiroha-server/minecraft-patches/features/0074-Gale-For-collision-check-has-physics-before-same-veh.patch rename to shiroha-server/minecraft-patches/features/0075-Gale-For-collision-check-has-physics-before-same-veh.patch index 0b7a9f4..3c199fd 100644 --- a/shiroha-server/minecraft-patches/features/0074-Gale-For-collision-check-has-physics-before-same-veh.patch +++ b/shiroha-server/minecraft-patches/features/0075-Gale-For-collision-check-has-physics-before-same-veh.patch @@ -20,7 +20,7 @@ As part of: Akarin (https://github.com/Akarin-project/Akarin) Licensed under: GPL-3.0-only (https://www.gnu.org/licenses/gpl-3.0.html) diff --git a/net/minecraft/world/entity/Entity.java b/net/minecraft/world/entity/Entity.java -index e7497f8f488cf9833b428ce1c82451c902465d19..3c055fa3f6641137dda1532a19af8b67caa52143 100644 +index fcca07953fb91d5b1b7230682b657ea289d779b7..e4dfb9dec23b1ac38e2f2de529d9683615e758a7 100644 --- a/net/minecraft/world/entity/Entity.java +++ b/net/minecraft/world/entity/Entity.java @@ -2438,8 +2438,8 @@ public abstract class Entity diff --git a/shiroha-server/minecraft-patches/features/0075-Gale-Skip-negligible-planar-movement-multiplication.patch b/shiroha-server/minecraft-patches/features/0076-Gale-Skip-negligible-planar-movement-multiplication.patch similarity index 94% rename from shiroha-server/minecraft-patches/features/0075-Gale-Skip-negligible-planar-movement-multiplication.patch rename to shiroha-server/minecraft-patches/features/0076-Gale-Skip-negligible-planar-movement-multiplication.patch index 88bdd37..38c579c 100644 --- a/shiroha-server/minecraft-patches/features/0075-Gale-Skip-negligible-planar-movement-multiplication.patch +++ b/shiroha-server/minecraft-patches/features/0076-Gale-Skip-negligible-planar-movement-multiplication.patch @@ -7,7 +7,7 @@ License: GPL-3.0-only (https://www.gnu.org/licenses/gpl-3.0.html) Gale - https://github.com/GaleMC/Gale diff --git a/net/minecraft/world/entity/Entity.java b/net/minecraft/world/entity/Entity.java -index 3c055fa3f6641137dda1532a19af8b67caa52143..503af0546705e88ec06e064ec1c24f87cb25abeb 100644 +index e4dfb9dec23b1ac38e2f2de529d9683615e758a7..ab3bc9fea3fb5da4f851f4722fae77dd961f2e55 100644 --- a/net/minecraft/world/entity/Entity.java +++ b/net/minecraft/world/entity/Entity.java @@ -1299,8 +1299,17 @@ public abstract class Entity diff --git a/shiroha-server/minecraft-patches/features/0076-Gale-Optimize-matching-item-checks.patch b/shiroha-server/minecraft-patches/features/0077-Gale-Optimize-matching-item-checks.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0076-Gale-Optimize-matching-item-checks.patch rename to shiroha-server/minecraft-patches/features/0077-Gale-Optimize-matching-item-checks.patch diff --git a/shiroha-server/minecraft-patches/features/0077-Gale-Reduce-lambda-and-Optional-allocation-in-Entity.patch b/shiroha-server/minecraft-patches/features/0078-Gale-Reduce-lambda-and-Optional-allocation-in-Entity.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0077-Gale-Reduce-lambda-and-Optional-allocation-in-Entity.patch rename to shiroha-server/minecraft-patches/features/0078-Gale-Reduce-lambda-and-Optional-allocation-in-Entity.patch diff --git a/shiroha-server/minecraft-patches/features/0078-Gale-Reduce-array-allocations.patch b/shiroha-server/minecraft-patches/features/0079-Gale-Reduce-array-allocations.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0078-Gale-Reduce-array-allocations.patch rename to shiroha-server/minecraft-patches/features/0079-Gale-Reduce-array-allocations.patch diff --git a/shiroha-server/minecraft-patches/features/0079-Some-optimizations-from-krypton.patch b/shiroha-server/minecraft-patches/features/0080-Some-optimizations-from-krypton.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0079-Some-optimizations-from-krypton.patch rename to shiroha-server/minecraft-patches/features/0080-Some-optimizations-from-krypton.patch diff --git a/shiroha-server/minecraft-patches/features/0080-Frustum-filtering-tracker.patch b/shiroha-server/minecraft-patches/features/0081-Frustum-filtering-tracker.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0080-Frustum-filtering-tracker.patch rename to shiroha-server/minecraft-patches/features/0081-Frustum-filtering-tracker.patch diff --git a/shiroha-server/minecraft-patches/features/0081-Lithium-optimized-collections-for-WeightedList.patch b/shiroha-server/minecraft-patches/features/0082-Lithium-optimized-collections-for-WeightedList.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0081-Lithium-optimized-collections-for-WeightedList.patch rename to shiroha-server/minecraft-patches/features/0082-Lithium-optimized-collections-for-WeightedList.patch diff --git a/shiroha-server/minecraft-patches/features/0082-Lithium-optimized-non-poi-block-searching.patch b/shiroha-server/minecraft-patches/features/0083-Lithium-optimized-non-poi-block-searching.patch similarity index 100% rename from shiroha-server/minecraft-patches/features/0082-Lithium-optimized-non-poi-block-searching.patch rename to shiroha-server/minecraft-patches/features/0083-Lithium-optimized-non-poi-block-searching.patch diff --git a/shiroha-server/src/main/java/io/nanachiyo0721/shiroha/utils/ReferenceCountingChunkLoader.java b/shiroha-server/src/main/java/io/nanachiyo0721/shiroha/utils/ReferenceCountingChunkLoader.java new file mode 100644 index 0000000..10c8c11 --- /dev/null +++ b/shiroha-server/src/main/java/io/nanachiyo0721/shiroha/utils/ReferenceCountingChunkLoader.java @@ -0,0 +1,312 @@ +package io.nanachiyo0721.shiroha.utils; + +import ca.spottedleaf.concurrentutil.completable.CallbackCompletable; +import ca.spottedleaf.concurrentutil.map.concurrent.longs.ConcurrentChainedLong2ReferenceHashTable; +import ca.spottedleaf.concurrentutil.util.Priority; +import ca.spottedleaf.moonrise.common.util.CoordinateUtils; +import ca.spottedleaf.moonrise.patches.chunk_system.scheduling.ChunkTaskScheduler; +import io.papermc.paper.threadedregions.RegionizedServer; +import io.papermc.paper.threadedregions.ThreadedRegionizer; +import io.papermc.paper.threadedregions.TickRegions; +import it.unimi.dsi.fastutil.Pair; +import net.minecraft.core.BlockPos; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.level.chunk.ChunkAccess; +import net.minecraft.world.level.chunk.status.ChunkStatus; +import org.jspecify.annotations.NonNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Consumer; + +/** + * 一个简单的基于folia的RegionizedTaskQueue引用计数的轻量ticket区块加载器 + * 用于在folia传送门搜索的高频率scheduleChunkLoad的调用下减少ticket操作从而减轻锁负载 + * 大部分内容物均取自folia的RegionizedTaskQueue(引用计数), ttl机制取自我的优化) + * + * @see io.papermc.paper.threadedregions.RegionizedTaskQueue + * @see net.minecraft.world.entity.Entity#findOrCreatePortalAsync(ServerLevel, BlockPos, ServerLevel, Entity.PortalType, CallbackCompletable) + */ +public class ReferenceCountingChunkLoader { + private static final long MAX_CHUNK_TTL_TICKS = 2L; + + private final ServerLevel world; + private final ChunkStatus targetStatus; + private final ConcurrentChainedLong2ReferenceHashTable referenceCounters = new ConcurrentChainedLong2ReferenceHashTable<>(); + + public ReferenceCountingChunkLoader(ServerLevel world, ChunkStatus targetStatus) { + this.world = world; + this.targetStatus = targetStatus; + } + + private void decrementReference(final @NonNull ReferenceCountData referenceCountData, final long coord) { + if (!referenceCountData.decreaseReferenceCount()) { + return; + } + + final ReferenceCountData[] toRemoveTicket = new ReferenceCountData[1]; + + this.referenceCounters.computeIfPresent(coord, (final long keyInMap, final ReferenceCountData valueInMap) -> { + if (valueInMap.referenceCount.get() != 0L) { + return valueInMap; + } + + toRemoveTicket[0] = valueInMap; + + return null; + }); + + if (toRemoveTicket[0] != null) { + this.removeTicket(coord, toRemoveTicket[0].id); + } + } + + private void removeTicket(final long coord, final long id) { + this.world.moonrise$getChunkTaskScheduler().chunkHolderManager.removeTicketAtLevel( + ChunkTaskScheduler.CHUNK_LOAD, coord, ChunkTaskScheduler.getTicketLevel(this.targetStatus), Long.valueOf(id) + ); + } + + private void addTicket(final long coord, final long id) { + this.world.moonrise$getChunkTaskScheduler().chunkHolderManager.addTicketAtLevel( + ChunkTaskScheduler.CHUNK_LOAD, coord, ChunkTaskScheduler.getTicketLevel(this.targetStatus), Long.valueOf(id) + ); + } + + private void processTicketUpdates(final long coord) { + this.world.moonrise$getChunkTaskScheduler().chunkHolderManager.processTicketUpdates(CoordinateUtils.getChunkX(coord), CoordinateUtils.getChunkZ(coord)); + } + + private void ensureTicketAdded(final long coord, final @NonNull ReferenceCountData referenceCountData) { + if (!referenceCountData.addedTicket) { + this.addTicket(coord, referenceCountData.id); + this.processTicketUpdates(coord); + referenceCountData.addedTicket = true; + } + } + + public final void loadChunksAsync(final @NonNull BlockPos pos, final int radiusBlocks, + final Priority priority, + final Consumer> onLoad) { + this.loadChunksAsync( + (pos.getX() - radiusBlocks) >> 4, + (pos.getX() + radiusBlocks) >> 4, + (pos.getZ() - radiusBlocks) >> 4, + (pos.getZ() + radiusBlocks) >> 4, + priority, onLoad + ); + } + + public final void loadChunksAsync(final int minChunkX, final int maxChunkX, final int minChunkZ, final int maxChunkZ, + final Priority priority, + final Consumer> onLoad) { + this.loadChunksAsync(minChunkX, maxChunkX, minChunkZ, maxChunkZ, priority, onLoad, null); + } + + + public final void loadChunksAsync(final int minChunkX, final int maxChunkX, final int minChunkZ, final int maxChunkZ, + final Priority priority, + final Consumer> onLoad, final Consumer onEachLoad) { + final int requiredChunks = (maxChunkX - minChunkX + 1) * (maxChunkZ - minChunkZ + 1); + final AtomicInteger loadedChunks = new AtomicInteger(); + + final List> ret = new ArrayList<>(requiredChunks); + + final Consumer consumer = (final ChunkAccess chunk) -> { + + if (chunk != null) { + final long pos = chunk.getPos().longKey(); + + synchronized (ret) { + ret.add(Pair.of(this.incrementReference(pos), chunk)); + } + } + + if (onEachLoad != null) { + onEachLoad.accept(chunk); + } + + if (loadedChunks.incrementAndGet() == requiredChunks) { + try { + if (onLoad != null) { + final List processed = new ArrayList<>(ret.size()); + + for (Pair result : ret) { + processed.add(result.right()); + } + + onLoad.accept(processed); + } + } finally { + for (Pair extraRefEntry : ret) { + this.decrementReference(extraRefEntry.left(), extraRefEntry.right().getPos().longKey()); + } + } + } + }; + + for (int cx = minChunkX; cx <= maxChunkX; ++cx) { + for (int cz = minChunkZ; cz <= maxChunkZ; ++cz) { + this.loadAsync(cx, cz, consumer, priority); + } + } + } + + public void loadAsync(int chunkX, int chunkZ, Consumer callback, Priority priority) { + final long coord = CoordinateUtils.getChunkKey(chunkX, chunkZ); + final ReferenceCountData increased = this.incrementReference(coord); + + final ChunkAccess cached = increased.cached; + if (cached != null) { + RegionizedServer.getInstance().taskQueue.queueChunkTask(this.world, chunkX, chunkZ, () -> { + try { + callback.accept(cached); + }finally { + this.decrementReference(increased, coord); + } + }, priority); + + return; + } + + this.world.moonrise$getChunkTaskScheduler().scheduleChunkLoad( + chunkX, chunkZ, this.targetStatus, true, priority, + chunk -> { + try { + increased.cached = chunk; + + callback.accept(chunk); + }finally { + this.decrementReference(increased, coord); + } + } + ); + } + + private ReferenceCountData incrementReference(final long coord) { + ReferenceCountData referenceCountData = this.referenceCounters.get(coord); + + if (referenceCountData != null && referenceCountData.addCount()) { + this.ensureTicketAdded(coord, referenceCountData); + return referenceCountData; + } + + referenceCountData = this.referenceCounters.compute(coord, (final long keyInMap, final ReferenceCountData valueInMap) -> { + if (valueInMap == null) { + return new ReferenceCountData(); + } + + valueInMap.referenceCount.getAndIncrement(); + + return valueInMap; + }); + + this.ensureTicketAdded(coord, referenceCountData); + + return referenceCountData; + } + + public void tickChunkReferenceTTL() { + final ThreadedRegionizer.ThreadedRegion currentRegion + = io.papermc.paper.threadedregions.TickRegionScheduler.getCurrentRegion(); + if (currentRegion == null) { + return; + } + + final ReferenceCountData[] toRemoveTicket = new ReferenceCountData[1]; + + for (ConcurrentChainedLong2ReferenceHashTable.TableEntry counterEntry : this.referenceCounters.entrySet()) { + final long coord = counterEntry.getKey(); + final ReferenceCountData counterData = counterEntry.getValue(); + + if (currentRegion == this.world.regioniser.getRegionAtUnsynchronised(CoordinateUtils.getChunkX(coord), CoordinateUtils.getChunkZ(coord))) { + long curr = counterData.referenceTTL.get(); + if (curr == (curr = counterData.referenceTTL.compareAndExchange(curr, curr - 1))) { + if (counterData.referenceCount.get() != 0L) { + counterData.referenceTTL.set(MAX_CHUNK_TTL_TICKS); + continue; + } + + if (curr <= 0) { + this.referenceCounters.computeIfPresent(coord, (final long keyInMap, final ReferenceCountData valueInMap) -> { + if (valueInMap.referenceCount.get() != 0L) { + valueInMap.referenceTTL.set(MAX_CHUNK_TTL_TICKS); + return valueInMap; + } + + toRemoveTicket[0] = valueInMap; + + return null; + }); + + if (toRemoveTicket[0] != null) { + this.removeTicket(coord, toRemoveTicket[0].id); + toRemoveTicket[0] = null; + } + } + } + } + } + } + + private static final class ReferenceCountData { + + private static final AtomicLong ID_GENERATOR = new AtomicLong(); + + private final long id = ID_GENERATOR.getAndIncrement(); + + public final AtomicLong referenceCount = new AtomicLong(1L); + public final AtomicLong referenceTTL = new AtomicLong(MAX_CHUNK_TTL_TICKS); + + public volatile ChunkAccess cached; + public volatile boolean addedTicket; + + public boolean addCount() { + int failures = 0; + for (long curr = this.referenceCount.get();;) { + for (int i = 0; i < failures; ++i) { + Thread.onSpinWait(); + } + + if (curr == 0L) { + return false; + } + + if (curr == (curr = this.referenceCount.compareAndExchange(curr, curr + 1L))) { + int ttlFailures = 0; + for (long currTTL = this.referenceTTL.get();;) { + for (int i = 0; i < ttlFailures; i++) { + Thread.onSpinWait(); + } + + if (currTTL <= 0) { + this.referenceCount.decrementAndGet(); + return false; + } + + if (currTTL == (currTTL = this.referenceTTL.compareAndExchange(currTTL, MAX_CHUNK_TTL_TICKS))) { + break; + } + + ++ttlFailures; + } + return true; + } + + ++failures; + } + } + + public boolean decreaseReferenceCount() { + final long res = this.referenceCount.decrementAndGet(); + if (res >= 0L) { + return res == 0L && this.referenceTTL.get() <= 0L; + } else { + throw new IllegalStateException("Negative reference count"); + } + } + } +}