summaryrefslogtreecommitdiffstats
path: root/nms-patches/ChunkProviderServer.patch
blob: cb45aa3b06c2bdc79dbe3c66ff3710faaf4c5f91 (plain)
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
--- a/net/minecraft/server/ChunkProviderServer.java
+++ b/net/minecraft/server/ChunkProviderServer.java
@@ -1,24 +1,31 @@
 package net.minecraft.server;
 
-import com.google.common.collect.Lists;
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
+
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
+// CraftBukkit start
+
+import org.bukkit.Server;
+import org.bukkit.craftbukkit.chunkio.ChunkIOExecutor;
+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 a = LogManager.getLogger();
-    private final Set<Long> unloadQueue = Collections.newSetFromMap(new ConcurrentHashMap());
-    private final ChunkGenerator chunkGenerator;
+    public final LongHashSet unloadQueue = new LongHashSet(); // CraftBukkit - LongHashSet
+    public final ChunkGenerator chunkGenerator; // CraftBukkit - public
     private final IChunkLoader chunkLoader;
-    private final LongHashMap<Chunk> chunks = new LongHashMap();
-    private final List<Chunk> chunkList = Lists.newArrayList();
+    public LongObjectHashMap<Chunk> chunks = new LongObjectHashMap<Chunk>(); // CraftBukkit
+    // private final LongHashMap<Chunk> chunks = new LongHashMap();
+    // private final List<Chunk> chunkList = Lists.newArrayList();
     public final WorldServer world;
 
     public ChunkProviderServer(WorldServer worldserver, IChunkLoader ichunkloader, ChunkGenerator chunkgenerator) {
@@ -27,19 +34,33 @@
         this.chunkGenerator = chunkgenerator;
     }
 
-    public List<Chunk> a() {
-        return this.chunkList;
+    public boolean isChunkLoaded(int i, int j) {
+        return this.chunks.containsKey(LongHash.toLong(i, j)); // CraftBukkit
+    }
+
+    // CraftBukkit start - Change return type to Collection and return the values of our chunk map
+    public java.util.Collection a() {
+        // return this.chunkList;
+        return this.chunks.values();
+        // CraftBukkit end
     }
 
     public void queueUnload(int i, int j) {
         if (this.world.worldProvider.c(i, j)) {
-            this.unloadQueue.add(Long.valueOf(ChunkCoordIntPair.a(i, j)));
+            // CraftBukkit start
+            this.unloadQueue.add(i, j);
+
+            Chunk c = chunks.get(LongHash.toLong(i, j));
+            if (c != null) {
+                c.mustSave = true;
+            }
+            // CraftBukkit end
         }
 
     }
 
     public void b() {
-        Iterator iterator = this.chunkList.iterator();
+        Iterator iterator = this.chunks.values().iterator(); // CraftBukkit
 
         while (iterator.hasNext()) {
             Chunk chunk = (Chunk) iterator.next();
@@ -49,11 +70,15 @@
 
     }
 
+    // CraftBukkit start - Add async variant, provide compatibility
+    public Chunk getChunkIfLoaded(int x, int z) {
+        return chunks.get(LongHash.toLong(x, z));
+    }
+
     public Chunk getLoadedChunkAt(int i, int j) {
-        long k = ChunkCoordIntPair.a(i, j);
-        Chunk chunk = (Chunk) this.chunks.getEntry(k);
+        Chunk chunk = chunks.get(LongHash.toLong(i, j)); // CraftBukkit
 
-        this.unloadQueue.remove(Long.valueOf(k));
+        this.unloadQueue.remove(i, j); // CraftBukkit
         return chunk;
     }
 
@@ -61,20 +86,67 @@
         Chunk chunk = this.getLoadedChunkAt(i, j);
 
         if (chunk == null) {
-            chunk = this.loadChunk(i, j);
+            // CraftBukkit start
+            ChunkRegionLoader loader = null;
+
+            if (this.chunkLoader instanceof ChunkRegionLoader) {
+                loader = (ChunkRegionLoader) this.chunkLoader;
+            }
+            if (loader != null && loader.chunkExists(world, i, j)) {
+                chunk = ChunkIOExecutor.syncChunkLoad(world, loader, this, i, j);
+            }
+            /* chunk = this.loadChunk(i, j);
             if (chunk != null) {
                 this.chunks.put(ChunkCoordIntPair.a(i, j), chunk);
                 this.chunkList.add(chunk);
                 chunk.addEntities();
                 chunk.loadNearby(this, this.chunkGenerator);
             }
+            */
+            // CraftBukkit end
         }
 
         return chunk;
     }
 
     public Chunk getChunkAt(int i, int j) {
-        Chunk chunk = this.getOrLoadChunkAt(i, j);
+        return getChunkAt(i, j, null);
+    }
+
+    public Chunk getChunkAt(int i, int j, Runnable runnable) {
+        unloadQueue.remove(i, j);
+        Chunk chunk = chunks.get(LongHash.toLong(i, j));
+        ChunkRegionLoader loader = null;
+
+        if (this.chunkLoader instanceof ChunkRegionLoader) {
+            loader = (ChunkRegionLoader) this.chunkLoader;
+
+        }
+        // We can only use the queue for already generated chunks
+        if (chunk == null && loader != null && loader.chunkExists(world, i, j)) {
+            if (runnable != null) {
+                ChunkIOExecutor.queueChunkLoad(world, loader, this, i, j, runnable);
+                return null;
+            } else {
+                chunk = ChunkIOExecutor.syncChunkLoad(world, loader, this, i, j);
+            }
+        } else if (chunk == null) {
+            chunk = originalGetChunkAt(i, j);
+        }
+
+        // If we didn't load the chunk async and have a callback run it now
+        if (runnable != null) {
+            runnable.run();
+        }
+
+        return chunk;
+    }
+
+    public Chunk originalGetChunkAt(int i, int j) {
+        this.unloadQueue.remove(i, j);
+        Chunk chunk = this.chunks.get(LongHash.toLong(i, j));
+        boolean newChunk = false;
+        // CraftBukkit end
 
         if (chunk == null) {
             long k = ChunkCoordIntPair.a(i, j);
@@ -92,11 +164,38 @@
                     crashreportsystemdetails.a("Generator", (Object) this.chunkGenerator);
                     throw new ReportedException(crashreport);
                 }
+                newChunk = true; // CraftBukkit
             }
 
-            this.chunks.put(k, chunk);
-            this.chunkList.add(chunk);
+            this.chunks.put(LongHash.toLong(i, j), chunk); // CraftBukkit
             chunk.addEntities();
+
+            // CraftBukkit start
+            Server server = 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));
+            }
+
+            // Update neighbor counts
+            for (int x = -2; x < 3; x++) {
+                for (int z = -2; z < 3; z++) {
+                    if (x == 0 && z == 0) {
+                        continue;
+                    }
+
+                    Chunk neighbor = this.getChunkIfLoaded(chunk.locX + x, chunk.locZ + z);
+                    if (neighbor != null) {
+                        neighbor.setNeighborLoaded(-x, -z);
+                        chunk.setNeighborLoaded(x, z);
+                    }
+                }
+            }
+            // CraftBukkit end
             chunk.loadNearby(this, this.chunkGenerator);
         }
 
@@ -142,10 +241,12 @@
 
     public boolean a(boolean flag) {
         int i = 0;
-        ArrayList arraylist = Lists.newArrayList(this.chunkList);
 
-        for (int j = 0; j < arraylist.size(); ++j) {
-            Chunk chunk = (Chunk) arraylist.get(j);
+        // CraftBukkit start
+        Iterator iterator = this.chunks.values().iterator();
+        while (iterator.hasNext()) {
+            Chunk chunk = (Chunk) iterator.next();
+            // CraftBukkit end
 
             if (flag) {
                 this.saveChunkNOP(chunk);
@@ -170,22 +271,43 @@
 
     public boolean unloadChunks() {
         if (!this.world.savingDisabled) {
-            for (int i = 0; i < 100; ++i) {
-                if (!this.unloadQueue.isEmpty()) {
-                    Long olong = (Long) this.unloadQueue.iterator().next();
-                    Chunk chunk = (Chunk) this.chunks.getEntry(olong.longValue());
+            // 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()) {
 
                     if (chunk != null) {
                         chunk.removeEntities();
                         this.saveChunk(chunk);
                         this.saveChunkNOP(chunk);
-                        this.chunks.remove(olong.longValue());
-                        this.chunkList.remove(chunk);
+                        this.chunks.remove(chunkcoordinates); // CraftBukkit
                     }
 
-                    this.unloadQueue.remove(olong);
+                    // this.unloadQueue.remove(olong);
+
+                    // Update neighbor counts
+                    for (int x = -2; x < 3; x++) {
+                        for (int z = -2; z < 3; z++) {
+                            if (x == 0 && z == 0) {
+                                continue;
+                            }
+
+                            Chunk neighbor = this.getChunkIfLoaded(chunk.locX + x, chunk.locZ + z);
+                            if (neighbor != null) {
+                                neighbor.setNeighborUnloaded(-x, -z);
+                                chunk.setNeighborUnloaded(x, z);
+                            }
+                        }
+                    }
                 }
             }
+            // CraftBukkit end
 
             this.chunkLoader.a();
         }
@@ -198,7 +320,8 @@
     }
 
     public String getName() {
-        return "ServerChunkCache: " + this.chunks.count() + " Drop: " + this.unloadQueue.size();
+        // CraftBukkit - this.chunks.count() -> .size()
+        return "ServerChunkCache: " + this.chunks.size() + " Drop: " + this.unloadQueue.size();
     }
 
     public List<BiomeBase.BiomeMeta> a(EnumCreatureType enumcreaturetype, BlockPosition blockposition) {
@@ -210,10 +333,11 @@
     }
 
     public int g() {
-        return this.chunks.count();
+        // CraftBukkit - this.chunks.count() -> this.chunks.size()
+        return this.chunks.size();
     }
 
     public boolean e(int i, int j) {
-        return this.chunks.contains(ChunkCoordIntPair.a(i, j));
+        return this.chunks.containsKey(LongHash.toLong(i, j)); // CraftBukkit
     }
 }