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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
package net.minecraft.server;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
// CraftBukkit start
import java.util.Random;
import org.bukkit.Server;
import org.bukkit.craftbukkit.util.LongHash;
import org.bukkit.craftbukkit.util.LongHashSet;
import org.bukkit.craftbukkit.util.LongObjectHashMap;
import org.bukkit.event.world.ChunkUnloadEvent;
// CraftBukkit end
public class ChunkProviderServer implements IChunkProvider {
private static final Logger b = LogManager.getLogger();
// CraftBukkit start - private -> public
public LongHashSet unloadQueue = new LongHashSet(); // LongHashSet
public Chunk emptyChunk;
public IChunkProvider chunkProvider;
private IChunkLoader f;
public boolean forceChunkLoad = false; // true -> false
public LongObjectHashMap<Chunk> chunks = new LongObjectHashMap<Chunk>();
private List chunkList = new ArrayList();
public WorldServer world;
// CraftBukkit end
public ChunkProviderServer(WorldServer worldserver, IChunkLoader ichunkloader, IChunkProvider ichunkprovider) {
this.emptyChunk = new EmptyChunk(worldserver, 0, 0);
this.world = worldserver;
this.f = ichunkloader;
this.chunkProvider = ichunkprovider;
}
public boolean isChunkLoaded(int i, int j) {
return this.chunks.containsKey(LongHash.toLong(i, j)); // CraftBukkit
}
public void queueUnload(int i, int j) {
if (this.world.worldProvider.e()) {
ChunkCoordinates chunkcoordinates = this.world.getSpawn();
int k = i * 16 + 8 - chunkcoordinates.x;
int l = j * 16 + 8 - chunkcoordinates.z;
short short1 = 128;
// CraftBukkit start
if (k < -short1 || k > short1 || l < -short1 || l > short1 || !(this.world.keepSpawnInMemory)) { // Added 'this.world.keepSpawnInMemory'
this.unloadQueue.add(i, j);
Chunk c = this.chunks.get(LongHash.toLong(i, j));
if (c != null) {
c.mustSave = true;
}
}
// CraftBukkit end
} else {
// CraftBukkit start
this.unloadQueue.add(i, j);
Chunk c = this.chunks.get(LongHash.toLong(i, j));
if (c != null) {
c.mustSave = true;
}
// CraftBukkit end
}
}
public void a() {
Iterator iterator = this.chunks.values().iterator(); // CraftBukkit
while (iterator.hasNext()) {
Chunk chunk = (Chunk) iterator.next();
this.queueUnload(chunk.locX, chunk.locZ);
}
}
// CraftBukkit start - Add async variant, provide compatibility
public Chunk getChunkAt(int i, int j) {
return getChunkAt(i, j, null);
}
public Chunk getChunkAt(int i, int j, Runnable runnable) {
this.unloadQueue.remove(i, j);
Chunk chunk = (Chunk) this.chunks.get(LongHash.toLong(i, j));
boolean newChunk = false;
ChunkRegionLoader loader = null;
if (this.f instanceof ChunkRegionLoader) {
loader = (ChunkRegionLoader) this.f;
}
// If the chunk exists but isn't loaded do it async
if (chunk == null && runnable != null && loader != null && loader.chunkExists(this.world, i, j)) {
org.bukkit.craftbukkit.chunkio.ChunkIOExecutor.queueChunkLoad(this.world, loader, this, i, j, runnable);
return null;
}
// CraftBukkit end
if (chunk == null) {
chunk = this.loadChunk(i, j);
if (chunk == null) {
if (this.chunkProvider == null) {
chunk = this.emptyChunk;
} else {
try {
chunk = this.chunkProvider.getOrCreateChunk(i, j);
} catch (Throwable throwable) {
CrashReport crashreport = CrashReport.a(throwable, "Exception generating new chunk");
CrashReportSystemDetails crashreportsystemdetails = crashreport.a("Chunk to be generated");
crashreportsystemdetails.a("Location", String.format("%d,%d", new Object[] { Integer.valueOf(i), Integer.valueOf(j)}));
crashreportsystemdetails.a("Position hash", Long.valueOf(LongHash.toLong(i, j))); // CraftBukkit - Use LongHash
crashreportsystemdetails.a("Generator", this.chunkProvider.getName());
throw new ReportedException(crashreport);
}
}
newChunk = true; // CraftBukkit
}
this.chunks.put(LongHash.toLong(i, j), chunk); // CraftBukkit
this.chunkList.add(chunk);
chunk.addEntities();
// CraftBukkit start
Server server = this.world.getServer();
if (server != null) {
/*
* If it's a new world, the first few chunks are generated inside
* the World constructor. We can't reliably alter that, so we have
* no way of creating a CraftWorld/CraftServer at that point.
*/
server.getPluginManager().callEvent(new org.bukkit.event.world.ChunkLoadEvent(chunk.bukkitChunk, newChunk));
}
// CraftBukkit end
chunk.a(this, this, i, j);
}
// CraftBukkit start - If we didn't need to load the chunk run the callback now
if (runnable != null) {
runnable.run();
}
// CraftBukkit end
return chunk;
}
public Chunk getOrCreateChunk(int i, int j) {
// CraftBukkit start
Chunk chunk = (Chunk) this.chunks.get(LongHash.toLong(i, j));
chunk = chunk == null ? (!this.world.isLoading && !this.forceChunkLoad ? this.emptyChunk : this.getChunkAt(i, j)) : chunk;
if (chunk == this.emptyChunk) return chunk;
if (i != chunk.locX || j != chunk.locZ) {
b.error("Chunk (" + chunk.locX + ", " + chunk.locZ + ") stored at (" + i + ", " + j + ") in world '" + world.getWorld().getName() + "'");
b.error(chunk.getClass().getName());
Throwable ex = new Throwable();
ex.fillInStackTrace();
ex.printStackTrace();
}
return chunk;
// CraftBukkit end
}
public Chunk loadChunk(int i, int j) { // CraftBukkit - private -> public
if (this.f == null) {
return null;
} else {
try {
Chunk chunk = this.f.a(this.world, i, j);
if (chunk != null) {
chunk.p = this.world.getTime();
if (this.chunkProvider != null) {
this.chunkProvider.recreateStructures(i, j);
}
}
return chunk;
} catch (Exception exception) {
b.error("Couldn\'t load chunk", exception);
return null;
}
}
}
public void saveChunkNOP(Chunk chunk) { // CraftBukkit - private -> public
if (this.f != null) {
try {
this.f.b(this.world, chunk);
} catch (Exception exception) {
b.error("Couldn\'t save entities", exception);
}
}
}
public void saveChunk(Chunk chunk) { // CraftBukkit - private -> public
if (this.f != null) {
try {
chunk.p = this.world.getTime();
this.f.a(this.world, chunk);
// CraftBukkit start - IOException to Exception
} catch (Exception ioexception) {
b.error("Couldn\'t save chunk", ioexception);
/* Remove extra exception
} catch (ExceptionWorldConflict exceptionworldconflict) {
b.error("Couldn\'t save chunk; already in use by another instance of Minecraft?", exceptionworldconflict);
// CraftBukkit end */
}
}
}
public void getChunkAt(IChunkProvider ichunkprovider, int i, int j) {
Chunk chunk = this.getOrCreateChunk(i, j);
if (!chunk.done) {
chunk.p();
if (this.chunkProvider != null) {
this.chunkProvider.getChunkAt(ichunkprovider, i, j);
// CraftBukkit start
BlockSand.instaFall = true;
Random random = new Random();
random.setSeed(world.getSeed());
long xRand = random.nextLong() / 2L * 2L + 1L;
long zRand = random.nextLong() / 2L * 2L + 1L;
random.setSeed((long) i * xRand + (long) j * zRand ^ world.getSeed());
org.bukkit.World world = this.world.getWorld();
if (world != null) {
this.world.populating = true;
try {
for (org.bukkit.generator.BlockPopulator populator : world.getPopulators()) {
populator.populate(world, random, chunk.bukkitChunk);
}
} finally {
this.world.populating = false;
}
}
BlockSand.instaFall = false;
this.world.getServer().getPluginManager().callEvent(new org.bukkit.event.world.ChunkPopulateEvent(chunk.bukkitChunk));
// CraftBukkit end
chunk.e();
}
}
}
public boolean saveChunks(boolean flag, IProgressUpdate iprogressupdate) {
int i = 0;
// CraftBukkit start
Iterator iterator = this.chunks.values().iterator();
while (iterator.hasNext()) {
Chunk chunk = (Chunk) iterator.next();
// CraftBukkit end
if (flag) {
this.saveChunkNOP(chunk);
}
if (chunk.a(flag)) {
this.saveChunk(chunk);
chunk.n = false;
++i;
if (i == 24 && !flag) {
return false;
}
}
}
return true;
}
public void b() {
if (this.f != null) {
this.f.b();
}
}
public boolean unloadChunks() {
if (!this.world.savingDisabled) {
// CraftBukkit start
Server server = this.world.getServer();
for (int i = 0; i < 100 && !this.unloadQueue.isEmpty(); i++) {
long chunkcoordinates = this.unloadQueue.popFirst();
Chunk chunk = this.chunks.get(chunkcoordinates);
if (chunk == null) continue;
ChunkUnloadEvent event = new ChunkUnloadEvent(chunk.bukkitChunk);
server.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
chunk.removeEntities();
this.saveChunk(chunk);
this.saveChunkNOP(chunk);
// this.unloadQueue.remove(olong);
// this.chunks.remove(olong.longValue());
this.chunks.remove(chunkcoordinates); // CraftBukkit
this.chunkList.remove(chunk);
}
}
// CraftBukkit end
if (this.f != null) {
this.f.a();
}
}
return this.chunkProvider.unloadChunks();
}
public boolean canSave() {
return !this.world.savingDisabled;
}
public String getName() {
// CraftBukkit - this.chunks.count() -> .values().size()
return "ServerChunkCache: " + this.chunks.values().size() + " Drop: " + this.unloadQueue.size();
}
public List getMobsFor(EnumCreatureType enumcreaturetype, int i, int j, int k) {
return this.chunkProvider.getMobsFor(enumcreaturetype, i, j, k);
}
public ChunkPosition findNearestMapFeature(World world, String s, int i, int j, int k) {
return this.chunkProvider.findNearestMapFeature(world, s, i, j, k);
}
public int getLoadedChunks() {
// CraftBukkit - this.chunks.count() -> .values().size()
return this.chunks.values().size();
}
public void recreateStructures(int i, int j) {}
}
|