--- a/net/minecraft/server/level/ChunkMapDistance.java +++ b/net/minecraft/server/level/ChunkMapDistance.java @@ -125,10 +125,25 @@ } if (!this.chunksToUpdateFutures.isEmpty()) { - this.chunksToUpdateFutures.forEach((playerchunk) -> { + // CraftBukkit start + // Iterate pending chunk updates with protection against concurrent modification exceptions + java.util.Iterator iter = this.chunksToUpdateFutures.iterator(); + int expectedSize = this.chunksToUpdateFutures.size(); + do { + PlayerChunk playerchunk = iter.next(); + iter.remove(); + expectedSize--; + playerchunk.updateFutures(playerchunkmap, this.mainThreadExecutor); - }); - this.chunksToUpdateFutures.clear(); + + // Reset iterator if set was modified using add() + if (this.chunksToUpdateFutures.size() != expectedSize) { + expectedSize = this.chunksToUpdateFutures.size(); + iter = this.chunksToUpdateFutures.iterator(); + } + } while (iter.hasNext()); + // CraftBukkit end + return true; } else { if (!this.ticketsToRelease.isEmpty()) { @@ -164,7 +179,7 @@ } } - void addTicket(long i, Ticket ticket) { + boolean addTicket(long i, Ticket ticket) { // CraftBukkit - void -> boolean ArraySetSorted> arraysetsorted = this.getTickets(i); int j = getTicketLevelAt(arraysetsorted); Ticket ticket1 = (Ticket) arraysetsorted.addOrGet(ticket); @@ -174,13 +189,15 @@ this.ticketTracker.update(i, ticket.getTicketLevel(), true); } + return ticket == ticket1; // CraftBukkit } - void removeTicket(long i, Ticket ticket) { + boolean removeTicket(long i, Ticket ticket) { // CraftBukkit - void -> boolean ArraySetSorted> arraysetsorted = this.getTickets(i); + boolean removed = false; // CraftBukkit if (arraysetsorted.remove(ticket)) { - ; + removed = true; // CraftBukkit } if (arraysetsorted.isEmpty()) { @@ -188,16 +205,29 @@ } this.ticketTracker.update(i, getTicketLevelAt(arraysetsorted), false); + return removed; // CraftBukkit } public void addTicket(TicketType tickettype, ChunkCoordIntPair chunkcoordintpair, int i, T t0) { - this.addTicket(chunkcoordintpair.toLong(), new Ticket<>(tickettype, i, t0)); + // CraftBukkit start + this.addTicketAtLevel(tickettype, chunkcoordintpair, i, t0); + } + + public boolean addTicketAtLevel(TicketType ticketType, ChunkCoordIntPair chunkcoordintpair, int level, T identifier) { + return this.addTicket(chunkcoordintpair.toLong(), new Ticket<>(ticketType, level, identifier)); + // CraftBukkit end } public void removeTicket(TicketType tickettype, ChunkCoordIntPair chunkcoordintpair, int i, T t0) { - Ticket ticket = new Ticket<>(tickettype, i, t0); + // CraftBukkit start + this.removeTicketAtLevel(tickettype, chunkcoordintpair, i, t0); + } - this.removeTicket(chunkcoordintpair.toLong(), ticket); + public boolean removeTicketAtLevel(TicketType ticketType, ChunkCoordIntPair chunkcoordintpair, int level, T identifier) { + Ticket ticket = new Ticket<>(ticketType, level, identifier); + + return this.removeTicket(chunkcoordintpair.toLong(), ticket); + // CraftBukkit end } public void addRegionTicket(TicketType tickettype, ChunkCoordIntPair chunkcoordintpair, int i, T t0) { @@ -252,6 +282,7 @@ ChunkCoordIntPair chunkcoordintpair = sectionposition.chunk(); long i = chunkcoordintpair.toLong(); ObjectSet objectset = (ObjectSet) this.playersPerChunk.get(i); + if (objectset == null) return; // CraftBukkit - SPIGOT-6208 objectset.remove(entityplayer); if (objectset.isEmpty()) { @@ -347,6 +378,26 @@ return this.tickingTicketsTracker; } + // 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(); + ArraySetSorted> tickets = entry.getValue(); + if (tickets.remove(target)) { + // copied from removeTicket + this.ticketTracker.update(entry.getLongKey(), getTicketLevelAt(tickets), false); + + // can't use entry after it's removed + if (tickets.isEmpty()) { + iterator.remove(); + } + } + } + } + // CraftBukkit end + private class a extends ChunkMap { public a() {