--- a/net/minecraft/server/level/DistanceManager.java +++ b/net/minecraft/server/level/DistanceManager.java @@ -107,6 +_,12 @@ } if (!this.chunksToUpdateFutures.isEmpty()) { + // CraftBukkit start - SPIGOT-7780: Call chunk unload events before updateHighestAllowedStatus + for (final ChunkHolder chunkHolder : this.chunksToUpdateFutures) { + chunkHolder.callEventIfUnloading(chunkMap); + } + // CraftBukkit end - SPIGOT-7780: Call chunk unload events before updateHighestAllowedStatus + for (ChunkHolder chunkHolder : this.chunksToUpdateFutures) { chunkHolder.updateHighestAllowedStatus(chunkMap); } @@ -177,16 +_,42 @@ public void addRegionTicket(TicketType type, ChunkPos pos, int distance, T value) { Ticket ticket = new Ticket<>(type, ChunkLevel.byStatus(FullChunkStatus.FULL) - distance, value); long packedChunkPos = pos.toLong(); - this.addTicket(packedChunkPos, ticket); + this.addTicket(packedChunkPos, ticket); // Paper - diff on change above this.tickingTicketsTracker.addTicket(packedChunkPos, ticket); } public void removeRegionTicket(TicketType type, ChunkPos pos, int distance, T value) { Ticket ticket = new Ticket<>(type, ChunkLevel.byStatus(FullChunkStatus.FULL) - distance, value); long packedChunkPos = pos.toLong(); + 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 true; } + // Paper end private SortedArraySet> getTickets(long chunkPos) { return this.tickets.computeIfAbsent(chunkPos, l -> SortedArraySet.create(4)); @@ -217,8 +_,10 @@ ChunkPos chunkPos = sectionPos.chunk(); long packedChunkPos = chunkPos.toLong(); ObjectSet set = this.playersPerChunk.get(packedChunkPos); - set.remove(player); - if (set.isEmpty()) { + // Paper start - some state corruption happens here, don't crash, clean up gracefully + if (set != null) set.remove(player); + if (set == null || set.isEmpty()) { + // Paper end - some state corruption happens here, don't crash, clean up gracefully this.playersPerChunk.remove(packedChunkPos); this.naturalSpawnChunkCounter.update(packedChunkPos, Integer.MAX_VALUE, false); this.playerTicketManager.update(packedChunkPos, Integer.MAX_VALUE, false); @@ -299,7 +_,7 @@ } public void removeTicketsOnClosing() { - ImmutableSet> set = ImmutableSet.of(TicketType.UNKNOWN); + ImmutableSet> set = ImmutableSet.of(TicketType.UNKNOWN, TicketType.POST_TELEPORT, TicketType.FUTURE_AWAIT); // Paper - add additional tickets to preserve ObjectIterator>>> objectIterator = this.tickets.long2ObjectEntrySet().fastIterator(); while (objectIterator.hasNext()) { @@ -329,6 +_,26 @@ public boolean hasTickets() { return !this.tickets.isEmpty(); } + + // CraftBukkit start + public void removeAllTicketsFor(TicketType ticketType, int ticketLevel, T ticketIdentifier) { + Ticket target = new Ticket<>(ticketType, ticketLevel, ticketIdentifier); + + for (java.util.Iterator>>> iterator = this.tickets.long2ObjectEntrySet().fastIterator(); iterator.hasNext();) { + Entry>> entry = iterator.next(); + SortedArraySet> tickets = entry.getValue(); + if (tickets.remove(target)) { + // copied from removeTicket + this.ticketTracker.update(entry.getLongKey(), DistanceManager.getTicketLevelAt(tickets), false); + + // can't use entry after it's removed + if (tickets.isEmpty()) { + iterator.remove(); + } + } + } + } + // CraftBukkit end class ChunkTicketTracker extends ChunkTracker { private static final int MAX_LEVEL = ChunkLevel.MAX_LEVEL + 1;