From d94e258d01f3b9eb9fedab0ef492021756e296a2 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Tue, 17 Dec 2024 21:40:20 +0100 Subject: Clean up add/removePluginChunkTicket No need to be that invasive to Vanilla code for simple, non-hot and small collection checks --- .../server/level/DistanceManager.java.patch | 81 ++++++++-------------- .../java/org/bukkit/craftbukkit/CraftWorld.java | 11 ++- 2 files changed, 35 insertions(+), 57 deletions(-) diff --git a/paper-server/patches/sources/net/minecraft/server/level/DistanceManager.java.patch b/paper-server/patches/sources/net/minecraft/server/level/DistanceManager.java.patch index f2b1b6a10a..eadab866cb 100644 --- a/paper-server/patches/sources/net/minecraft/server/level/DistanceManager.java.patch +++ b/paper-server/patches/sources/net/minecraft/server/level/DistanceManager.java.patch @@ -13,71 +13,50 @@ for (ChunkHolder chunkHolder : this.chunksToUpdateFutures) { chunkHolder.updateHighestAllowedStatus(chunkMap); } -@@ -143,7 +_,7 @@ - } - } - -- void addTicket(long chunkPos, Ticket ticket) { -+ boolean addTicket(long chunkPos, Ticket ticket) { // CraftBukkit - void -> boolean - SortedArraySet> tickets = this.getTickets(chunkPos); - int ticketLevelAt = getTicketLevelAt(tickets); - Ticket ticket1 = tickets.addOrGet(ticket); -@@ -151,11 +_,14 @@ - if (ticket.getTicketLevel() < ticketLevelAt) { - this.ticketTracker.update(chunkPos, ticket.getTicketLevel(), true); - } -+ return ticket == ticket1; // CraftBukkit - } - -- void removeTicket(long chunkPos, Ticket ticket) { -+ boolean removeTicket(long chunkPos, Ticket ticket) { // CraftBukkit - void -> boolean - SortedArraySet> tickets = this.getTickets(chunkPos); -+ boolean removed = false; // CraftBukkit - if (tickets.remove(ticket)) { -+ removed = true; // CraftBukkit - } - - if (tickets.isEmpty()) { -@@ -163,6 +_,7 @@ - } - - this.ticketTracker.update(chunkPos, getTicketLevelAt(tickets), false); -+ return removed; // CraftBukkit - } - - public void addTicket(TicketType type, ChunkPos pos, int level, T value) { -@@ -175,17 +_,29 @@ - } - +@@ -177,16 +_,42 @@ public void addRegionTicket(TicketType type, ChunkPos pos, int distance, T value) { -+ // CraftBukkit start -+ this.addRegionTicketAtDistance(type, pos, distance, value); -+ } -+ public boolean addRegionTicketAtDistance(TicketType type, ChunkPos pos, int distance, T value) { -+ // CraftBukkit end Ticket ticket = new Ticket<>(type, ChunkLevel.byStatus(FullChunkStatus.FULL) - distance, value); long packedChunkPos = pos.toLong(); - this.addTicket(packedChunkPos, ticket); -+ final boolean addded = this.addTicket(packedChunkPos, ticket); // CraftBukkit ++ this.addTicket(packedChunkPos, ticket); // Paper - diff on change above this.tickingTicketsTracker.addTicket(packedChunkPos, ticket); -+ return addded; // CraftBukkit } public void removeRegionTicket(TicketType type, ChunkPos pos, int distance, T value) { -+ // CraftBukkit start -+ removeRegionTicketAtDistance(type, pos, distance, value); -+ } -+ public boolean removeRegionTicketAtDistance(TicketType type, ChunkPos pos, int distance, T value) { -+ // CraftBukkit end Ticket ticket = new Ticket<>(type, ChunkLevel.byStatus(FullChunkStatus.FULL) - distance, value); long packedChunkPos = pos.toLong(); -- this.removeTicket(packedChunkPos, ticket); -+ final boolean removed = this.removeTicket(packedChunkPos, ticket); // CraftBukkit ++ this.removeTicket(packedChunkPos, ticket); // Paper - diff on change above ++ this.tickingTicketsTracker.removeTicket(packedChunkPos, ticket); ++ } ++ ++ // Paper start ++ public boolean addPluginRegionTicket(final ChunkPos pos, final org.bukkit.plugin.Plugin value) { ++ Ticket ticket = new Ticket<>(TicketType.PLUGIN_TICKET, ChunkLevel.byStatus(FullChunkStatus.FULL) - 2, value); // Copied from below and keep in-line with force loading, add at level 31 ++ final long packedChunkPos = pos.toLong(); ++ final Set> tickets = this.getTickets(packedChunkPos); ++ if (tickets.contains(ticket)) { ++ return false; ++ } ++ this.addTicket(packedChunkPos, ticket); ++ this.tickingTicketsTracker.addTicket(packedChunkPos, ticket); ++ return true; ++ } ++ ++ public boolean removePluginRegionTicket(final ChunkPos pos, final org.bukkit.plugin.Plugin value) { ++ Ticket ticket = new Ticket<>(TicketType.PLUGIN_TICKET, ChunkLevel.byStatus(FullChunkStatus.FULL) - 2, value); // Copied from below and keep in-line with force loading, add at level 31 ++ final long packedChunkPos = pos.toLong(); ++ final Set> tickets = this.tickets.get(packedChunkPos); // Don't use getTickets, we don't want to create a new set ++ if (tickets == null || !tickets.contains(ticket)) { ++ return false; ++ } + this.removeTicket(packedChunkPos, ticket); this.tickingTicketsTracker.removeTicket(packedChunkPos, ticket); -+ return removed; // CraftBukkit ++ return true; } ++ // Paper end private SortedArraySet> getTickets(long chunkPos) { + return this.tickets.computeIfAbsent(chunkPos, l -> SortedArraySet.create(4)); @@ -217,8 +_,12 @@ ChunkPos chunkPos = sectionPos.chunk(); long packedChunkPos = chunkPos.toLong(); diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java index 619b71eac6..61eac5fbbe 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java @@ -584,10 +584,9 @@ public class CraftWorld extends CraftRegionAccessor implements World { Preconditions.checkArgument(plugin != null, "null plugin"); Preconditions.checkArgument(plugin.isEnabled(), "plugin is not enabled"); - DistanceManager chunkDistanceManager = this.world.getChunkSource().chunkMap.distanceManager; - - if (chunkDistanceManager.addRegionTicketAtDistance(TicketType.PLUGIN_TICKET, new ChunkPos(x, z), 2, plugin)) { // keep in-line with force loading, add at level 31 - this.getChunkAt(x, z); // ensure loaded + final DistanceManager distanceManager = this.world.getChunkSource().chunkMap.distanceManager; + if (distanceManager.addPluginRegionTicket(new ChunkPos(x, z), plugin)) { + this.getChunkAt(x, z); // ensure it's loaded return true; } @@ -598,8 +597,8 @@ public class CraftWorld extends CraftRegionAccessor implements World { public boolean removePluginChunkTicket(int x, int z, Plugin plugin) { Preconditions.checkNotNull(plugin, "null plugin"); - DistanceManager chunkDistanceManager = this.world.getChunkSource().chunkMap.distanceManager; - return chunkDistanceManager.removeRegionTicketAtDistance(TicketType.PLUGIN_TICKET, new ChunkPos(x, z), 2, plugin); // keep in-line with force loading, remove at level 31 + final DistanceManager distanceManager = this.world.getChunkSource().chunkMap.distanceManager; + return distanceManager.removePluginRegionTicket(new ChunkPos(x, z), plugin); } @Override -- cgit v1.2.3