1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
--- a/net/minecraft/server/level/DistanceManager.java
+++ b/net/minecraft/server/level/DistanceManager.java
@@ -122,10 +122,25 @@
}
if (!this.chunksToUpdateFutures.isEmpty()) {
- this.chunksToUpdateFutures.forEach((chunkholder) -> {
- chunkholder.updateFutures(chunkmap, this.mainThreadExecutor);
- });
- this.chunksToUpdateFutures.clear();
+ // CraftBukkit start
+ // Iterate pending chunk updates with protection against concurrent modification exceptions
+ java.util.Iterator<ChunkHolder> iter = this.chunksToUpdateFutures.iterator();
+ int expectedSize = this.chunksToUpdateFutures.size();
+ do {
+ ChunkHolder playerchunk = iter.next();
+ iter.remove();
+ expectedSize--;
+
+ playerchunk.updateFutures(chunkManager, this.mainThreadExecutor);
+
+ // 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()) {
@@ -161,30 +176,33 @@
}
}
- void addTicket(long i, Ticket<?> ticket) {
- SortedArraySet<Ticket<?>> sortedarrayset = this.getTickets(i);
- int j = getTicketLevelAt(sortedarrayset);
- Ticket<?> ticket1 = (Ticket) sortedarrayset.addOrGet(ticket);
+ boolean addTicket(long i, Ticket<?> ticket) { // CraftBukkit - void -> boolean
+ SortedArraySet<Ticket<?>> arraysetsorted = this.getTickets(i);
+ int j = getTicketLevelAt(arraysetsorted);
+ Ticket<?> ticket1 = (Ticket) arraysetsorted.addOrGet(ticket);
ticket1.setCreatedTick(this.ticketTickCounter);
if (ticket.getTicketLevel() < j) {
this.ticketTracker.update(i, ticket.getTicketLevel(), true);
}
+ return ticket == ticket1; // CraftBukkit
}
- void removeTicket(long i, Ticket<?> ticket) {
- SortedArraySet<Ticket<?>> sortedarrayset = this.getTickets(i);
+ boolean removeTicket(long i, Ticket<?> ticket) { // CraftBukkit - void -> boolean
+ SortedArraySet<Ticket<?>> arraysetsorted = this.getTickets(i);
- if (sortedarrayset.remove(ticket)) {
- ;
+ boolean removed = false; // CraftBukkit
+ if (arraysetsorted.remove(ticket)) {
+ removed = true; // CraftBukkit
}
if (sortedarrayset.isEmpty()) {
this.tickets.remove(i);
}
- this.ticketTracker.update(i, getTicketLevelAt(sortedarrayset), false);
+ this.ticketTracker.update(i, getTicketLevelAt(arraysetsorted), false);
+ return removed; // CraftBukkit
}
public <T> void addTicket(TicketType<T> tickettype, ChunkPos chunkpos, int i, T t0) {
@@ -197,20 +215,34 @@
this.removeTicket(chunkpos.toLong(), ticket);
}
- public <T> void addRegionTicket(TicketType<T> tickettype, ChunkPos chunkpos, int i, T t0) {
+ public <T> void addRegionTicket(TicketType<T> type, ChunkPos pos, int distance, T value) {
+ // CraftBukkit start
+ addRegionTicketAtDistance(type, pos, distance, value);
+ }
+
+ public <T> boolean addRegionTicketAtDistance(TicketType<T> tickettype, ChunkPos chunkcoordintpair, int i, T t0) {
+ // CraftBukkit end
Ticket<T> ticket = new Ticket<>(tickettype, ChunkLevel.byStatus(FullChunkStatus.FULL) - i, t0);
long j = chunkpos.toLong();
- this.addTicket(j, ticket);
+ boolean added = this.addTicket(j, ticket); // CraftBukkit
this.tickingTicketsTracker.addTicket(j, ticket);
+ return added; // CraftBukkit
}
- public <T> void removeRegionTicket(TicketType<T> tickettype, ChunkPos chunkpos, int i, T t0) {
+ public <T> void removeRegionTicket(TicketType<T> type, ChunkPos pos, int distance, T value) {
+ // CraftBukkit start
+ removeRegionTicketAtDistance(type, pos, distance, value);
+ }
+
+ public <T> boolean removeRegionTicketAtDistance(TicketType<T> tickettype, ChunkPos chunkcoordintpair, int i, T t0) {
+ // CraftBukkit end
Ticket<T> ticket = new Ticket<>(tickettype, ChunkLevel.byStatus(FullChunkStatus.FULL) - i, t0);
long j = chunkpos.toLong();
- this.removeTicket(j, ticket);
+ boolean removed = this.removeTicket(j, ticket); // CraftBukkit
this.tickingTicketsTracker.removeTicket(j, ticket);
+ return removed; // CraftBukkit
}
private SortedArraySet<Ticket<?>> getTickets(long i) {
@@ -249,6 +281,7 @@
ChunkPos chunkpos = sectionpos.chunk();
long i = chunkpos.toLong();
ObjectSet<ServerPlayer> objectset = (ObjectSet) this.playersPerChunk.get(i);
+ if (objectset == null) return; // CraftBukkit - SPIGOT-6208
objectset.remove(serverplayer);
if (objectset.isEmpty()) {
@@ -378,6 +411,26 @@
return !this.tickets.isEmpty();
}
+ // CraftBukkit start
+ public <T> void removeAllTicketsFor(TicketType<T> ticketType, int ticketLevel, T ticketIdentifier) {
+ Ticket<T> target = new Ticket<>(ticketType, ticketLevel, ticketIdentifier);
+
+ for (java.util.Iterator<Entry<SortedArraySet<Ticket<?>>>> iterator = this.tickets.long2ObjectEntrySet().fastIterator(); iterator.hasNext();) {
+ Entry<SortedArraySet<Ticket<?>>> entry = iterator.next();
+ SortedArraySet<Ticket<?>> 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 ChunkTicketTracker extends ChunkTracker {
private static final int MAX_LEVEL = ChunkLevel.MAX_LEVEL + 1;
|