summaryrefslogtreecommitdiffstats
path: root/nms-patches/ChunkProviderServer.patch
blob: f6fa24c829c60bf077e956f42a357be0c658bdff (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
--- a/net/minecraft/server/ChunkProviderServer.java
+++ b/net/minecraft/server/ChunkProviderServer.java
@@ -14,6 +14,12 @@
 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.event.world.ChunkUnloadEvent;
+// CraftBukkit end
+
 public class ChunkProviderServer implements IChunkProvider {
 
     private static final Logger a = LogManager.getLogger();
@@ -69,6 +75,26 @@
         Chunk chunk = this.getLoadedChunkAt(i, j);
 
         if (chunk == null) {
+            // 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);
+            }
+        }
+
+        return chunk;
+    }
+
+    @Nullable
+    public Chunk originalGetOrLoadChunkAt(int i, int j) {
+        // CraftBukkit end
+        Chunk chunk = this.getLoadedChunkAt(i, j);
+
+        if (chunk == null) {
             chunk = this.loadChunk(i, j);
             if (chunk != null) {
                 this.chunks.put(ChunkCoordIntPair.a(i, j), chunk);
@@ -80,8 +106,52 @@
         return chunk;
     }
 
+    // CraftBukkit start
+    public Chunk getChunkIfLoaded(int x, int z) {
+        return chunks.get(ChunkCoordIntPair.a(x, z));
+    }
+    // CraftBukkit end
+
     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) {
+        return getChunkAt(i, j, runnable, true);
+    }
+
+    public Chunk getChunkAt(int i, int j, Runnable runnable, boolean generate) {
+        Chunk chunk = getChunkIfLoaded(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 && generate) {
+            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) {
+        Chunk chunk = this.originalGetOrLoadChunkAt(i, j);
+        boolean newChunk = false;
+        // CraftBukkit end
 
         if (chunk == null) {
             long k = ChunkCoordIntPair.a(i, j);
@@ -97,9 +167,37 @@
                 crashreportsystemdetails.a("Generator", (Object) this.chunkGenerator);
                 throw new ReportedException(crashreport);
             }
+            newChunk = true; // CraftBukkit
 
             this.chunks.put(k, chunk);
             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);
         }
 
@@ -146,10 +244,12 @@
 
     public boolean a(boolean flag) {
         int i = 0;
-        ArrayList arraylist = Lists.newArrayList(this.chunks.values());
 
-        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);
@@ -182,6 +282,29 @@
                     Chunk chunk = (Chunk) this.chunks.get(olong);
 
                     if (chunk != null && chunk.d) {
+                        // CraftBukkit start
+                        ChunkUnloadEvent event = new ChunkUnloadEvent(chunk.bukkitChunk);
+                        this.world.getServer().getPluginManager().callEvent(event);
+                        if (event.isCancelled()) {
+                            continue;
+                        }
+
+                        // 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
+
                         chunk.removeEntities();
                         this.saveChunk(chunk);
                         this.saveChunkNOP(chunk);