summaryrefslogtreecommitdiffstats
path: root/nms-patches/ChunkRegionLoader.patch
blob: bff72d1822622f93ccd0067b53e0052f3a89dbc5 (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
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
--- a/net/minecraft/server/ChunkRegionLoader.java
+++ b/net/minecraft/server/ChunkRegionLoader.java
@@ -31,7 +31,7 @@
     private final File c;
     private final DataFixer d;
     private PersistentStructureLegacy e;
-    private boolean f;
+    // private boolean f; // CraftBukkit
 
     public ChunkRegionLoader(File file, DataFixer datafixer) {
         this.c = file;
@@ -40,25 +40,64 @@
 
     @Nullable
     private NBTTagCompound a(GeneratorAccess generatoraccess, int i, int j) throws IOException {
-        return this.a(generatoraccess.o().getDimensionManager(), generatoraccess.h(), i, j);
+        return this.a(generatoraccess.o().getDimensionManager(), generatoraccess.h(), i, j, generatoraccess); // CraftBukkit
+    }
+
+    // CraftBukkit start
+    private boolean check(ChunkProviderServer cps, int x, int z) throws IOException {
+        if (cps != null) {
+            com.google.common.base.Preconditions.checkState(org.bukkit.Bukkit.isPrimaryThread(), "primary thread");
+            if (cps.isLoaded(x, z)) {
+                return true;
+            }
+        }
+
+        if (this.chunkExists(x, z)) {
+            NBTTagCompound nbt = RegionFileCache.read(this.c, x, z);
+            if (nbt != null) {
+                NBTTagCompound level = nbt.getCompound("Level");
+                if (level.getBoolean("TerrainPopulated")) {
+                    return true;
+                }
+
+                ChunkStatus status = ChunkStatus.a(level.getString("Status"));
+                if (status != null && status.a(ChunkStatus.DECORATED)) {
+                    return true;
+                }
+            }
+        }
+
+        return false;
+    }
+
+    public boolean chunkExists(int x, int z) {
+        return RegionFileCache.chunkExists(this.c, x, z);
     }
 
     @Nullable
-    private NBTTagCompound a(DimensionManager dimensionmanager, @Nullable PersistentCollection persistentcollection, int i, int j) throws IOException {
+    private NBTTagCompound a(DimensionManager dimensionmanager, @Nullable PersistentCollection persistentcollection, int i, int j, @Nullable GeneratorAccess generatoraccess) throws IOException {
         NBTTagCompound nbttagcompound = (NBTTagCompound) this.b.get(new ChunkCoordIntPair(i, j));
 
         if (nbttagcompound != null) {
             return nbttagcompound;
         } else {
-            DataInputStream datainputstream = RegionFileCache.read(this.c, i, j);
+            NBTTagCompound nbttagcompound1 = RegionFileCache.read(this.c, i, j);
 
-            if (datainputstream == null) {
+            if (nbttagcompound1 == null) {
                 return null;
             } else {
-                NBTTagCompound nbttagcompound1 = NBTCompressedStreamTools.a(datainputstream);
-
-                datainputstream.close();
                 int k = nbttagcompound1.hasKeyOfType("DataVersion", 99) ? nbttagcompound1.getInt("DataVersion") : -1;
+                // CraftBukkit start
+                if (k < 1466) {
+                    NBTTagCompound level = nbttagcompound1.getCompound("Level");
+                    if (level.getBoolean("TerrainPopulated") && !level.getBoolean("LightPopulated")) {
+                        ChunkProviderServer cps = (generatoraccess == null) ? null : ((WorldServer) generatoraccess).getChunkProviderServer();
+                        if (check(cps, i - 1, j) && check(cps, i - 1, j - 1) && check(cps, i, j - 1)) {
+                            level.setBoolean("LightPopulated", true);
+                        }
+                    }
+                }
+                // CraftBukkit end
 
                 if (k < 1493) {
                     nbttagcompound1 = GameProfileSerializer.a(this.d, DataFixTypes.CHUNK, nbttagcompound1, k, 1493);
@@ -86,13 +125,29 @@
 
     }
 
+    // CraftBukkit start - Add async variant, provide compatibility
     @Nullable
     public Chunk a(GeneratorAccess generatoraccess, int i, int j, Consumer<Chunk> consumer) throws IOException {
+        Object[] data = loadChunk(generatoraccess, i, j, consumer);
+        if (data != null) {
+            Chunk chunk = (Chunk) data[0];
+            NBTTagCompound nbttagcompound = (NBTTagCompound) data[1];
+            consumer.accept(chunk);
+            this.loadEntities(nbttagcompound.getCompound("Level"), chunk);
+            return chunk;
+        }
+
+        return null;
+    }
+
+    public Object[] loadChunk(GeneratorAccess generatoraccess, int i, int j, Consumer<Chunk> consumer) throws IOException {
+        // CraftBukkit end
         NBTTagCompound nbttagcompound = this.a(generatoraccess, i, j);
 
         if (nbttagcompound == null) {
             return null;
         } else {
+            /*
             Chunk chunk = this.a(generatoraccess, i, j, nbttagcompound);
 
             if (chunk != null) {
@@ -101,6 +156,9 @@
             }
 
             return chunk;
+            */
+
+            return this.a(generatoraccess, i, j, nbttagcompound);
         }
     }
 
@@ -132,7 +190,7 @@
     }
 
     @Nullable
-    protected Chunk a(GeneratorAccess generatoraccess, int i, int j, NBTTagCompound nbttagcompound) {
+    protected Object[] a(GeneratorAccess generatoraccess, int i, int j, NBTTagCompound nbttagcompound) { // CraftBukkit - return Chunk -> Object[]
         if (nbttagcompound.hasKeyOfType("Level", 10) && nbttagcompound.getCompound("Level").hasKeyOfType("Status", 8)) {
             ChunkStatus.Type chunkstatus_type = this.a(nbttagcompound);
 
@@ -151,10 +209,28 @@
                         ChunkRegionLoader.a.error("Chunk file at {},{} is in the wrong location; relocating. (Expected {}, {}, got {}, {})", Integer.valueOf(i), Integer.valueOf(j), Integer.valueOf(i), Integer.valueOf(j), Integer.valueOf(chunk.locX), Integer.valueOf(chunk.locZ));
                         nbttagcompound1.setInt("xPos", i);
                         nbttagcompound1.setInt("zPos", j);
+
+                        // CraftBukkit start - Have to move tile entities since we don't load them at this stage
+                        NBTTagList tileEntities = nbttagcompound.getCompound("Level").getList("TileEntities", 10);
+                        if (tileEntities != null) {
+                            for (int te = 0; te < tileEntities.size(); te++) {
+                                NBTTagCompound tileEntity = (NBTTagCompound) tileEntities.get(te);
+                                int x = tileEntity.getInt("x") - chunk.locX * 16;
+                                int z = tileEntity.getInt("z") - chunk.locZ * 16;
+                                tileEntity.setInt("x", i * 16 + x);
+                                tileEntity.setInt("z", j * 16 + z);
+                            }
+                        }
+                        // CraftBukkit end
                         chunk = this.a(generatoraccess, nbttagcompound1);
                     }
 
-                    return chunk;
+                    // CraftBukkit start
+                    Object[] data = new Object[2];
+                    data[0] = chunk;
+                    data[1] = nbttagcompound;
+                    return data;
+                    // CraftBukkit end
                 }
             }
         } else {
@@ -169,7 +245,7 @@
             ChunkStatus.Type chunkstatus_type = this.a(nbttagcompound);
 
             if (chunkstatus_type == ChunkStatus.Type.LEVELCHUNK) {
-                return new ProtoChunkExtension(this.a(generatoraccess, i, j, nbttagcompound));
+                return new ProtoChunkExtension((IChunkAccess) this.a(generatoraccess, i, j, nbttagcompound)[0]); // CraftBukkit - fix up access
             } else {
                 NBTTagCompound nbttagcompound1 = nbttagcompound.getCompound("Level");
 
@@ -217,10 +293,14 @@
     }
 
     public boolean a() {
-        Iterator iterator = this.b.entrySet().iterator();
+        // CraftBukkit start
+        return this.processSaveQueueEntry(false);
+    }
 
+    private boolean processSaveQueueEntry(boolean logCompletion) {
+        Iterator iterator = this.b.entrySet().iterator();
         if (!iterator.hasNext()) {
-            if (this.f) {
+            if (logCompletion) { // CraftBukkit
                 ChunkRegionLoader.a.info("ThreadedAnvilChunkStorage ({}): All chunks are saved", this.c.getName());
             }
 
@@ -236,10 +316,14 @@
                 return true;
             } else {
                 try {
-                    DataOutputStream dataoutputstream = RegionFileCache.write(this.c, chunkcoordintpair.x, chunkcoordintpair.z);
+                    // CraftBukkit start
+                    RegionFileCache.write(this.c, chunkcoordintpair.x, chunkcoordintpair.z, nbttagcompound);
 
+                    /*
                     NBTCompressedStreamTools.a(nbttagcompound, (DataOutput) dataoutputstream);
                     dataoutputstream.close();
+                    */
+                    // CraftBukkit end
                     if (this.e != null) {
                         this.e.a(chunkcoordintpair.a());
                     }
@@ -266,15 +350,16 @@
 
     public void b() {
         try {
-            this.f = true;
+            // this.f = true; // CraftBukkit
 
             while (true) {
-                if (this.a()) {
+                if (this.processSaveQueueEntry(true)) { // CraftBukkit
                     continue;
                 }
+                break; // CraftBukkit - Fix infinite loop when saving chunks
             }
         } finally {
-            this.f = false;
+            // this.f = false; // CraftBukkit
         }
 
     }
@@ -303,7 +388,7 @@
 
         if (abiomebase != null) {
             for (int k = 0; k < abiomebase.length; ++k) {
-                aint[k] = IRegistry.BIOME.a((Object) abiomebase[k]);
+                aint[k] = IRegistry.BIOME.a(abiomebase[k]); // CraftBukkit - decompile error
             }
         }
 
@@ -385,7 +470,7 @@
         int[] aint = new int[abiomebase.length];
 
         for (int i = 0; i < abiomebase.length; ++i) {
-            aint[i] = IRegistry.BIOME.a((Object) abiomebase[i]);
+            aint[i] = IRegistry.BIOME.a(abiomebase[i]); // CraftBukkit - decompile error
         }
 
         nbttagcompound.setIntArray("Biomes", aint);
@@ -487,27 +572,27 @@
         }
 
         ChunkConverter chunkconverter = nbttagcompound.hasKeyOfType("UpgradeData", 10) ? new ChunkConverter(nbttagcompound.getCompound("UpgradeData")) : ChunkConverter.a;
-        Predicate predicate = (block) -> {
+        Predicate<Block> predicate = (block) -> { // CraftBukkit - decompile error
             return block.getBlockData().isAir();
         };
         IRegistry iregistry = IRegistry.BLOCK;
 
         IRegistry.BLOCK.getClass();
         Function function = iregistry::getKey;
-        IRegistry iregistry1 = IRegistry.BLOCK;
+        IRegistry<Block> iregistry1 = IRegistry.BLOCK; // CraftBukkit - decompile error
 
         IRegistry.BLOCK.getClass();
-        ProtoChunkTickList protochunkticklist = new ProtoChunkTickList(predicate, function, iregistry1::getOrDefault, new ChunkCoordIntPair(i, j));
+        ProtoChunkTickList protochunkticklist = new ProtoChunkTickList<>(predicate, function, iregistry1::getOrDefault, new ChunkCoordIntPair(i, j)); // CraftBukkit - decompile error
 
-        predicate = (fluidtype) -> {
+        Predicate<FluidType> predicate1 = (fluidtype) -> { // CraftBukkit - decompile error
             return fluidtype == FluidTypes.a;
         };
         iregistry = IRegistry.FLUID;
         IRegistry.FLUID.getClass();
         function = iregistry::getKey;
-        iregistry1 = IRegistry.FLUID;
+        IRegistry<FluidType> iregistry2 = IRegistry.FLUID; // CraftBukkit - decompile error
         IRegistry.FLUID.getClass();
-        ProtoChunkTickList protochunkticklist1 = new ProtoChunkTickList(predicate, function, iregistry1::getOrDefault, new ChunkCoordIntPair(i, j));
+        ProtoChunkTickList protochunkticklist1 = new ProtoChunkTickList<>(predicate1, function, iregistry2::getOrDefault, new ChunkCoordIntPair(i, j)); // CraftBukkit - decompile error
         long i1 = nbttagcompound.getLong("InhabitedTime");
         Chunk chunk = new Chunk(generatoraccess.getMinecraftWorld(), i, j, abiomebase, chunkconverter, protochunkticklist, protochunkticklist1, i1);
 
@@ -850,17 +935,29 @@
     }
 
     @Nullable
+    // CraftBukkit start
     public static Entity a(NBTTagCompound nbttagcompound, World world, double d0, double d1, double d2, boolean flag) {
+        return spawnEntity(nbttagcompound, world, d0, d1, d2, flag, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.DEFAULT);
+    }
+
+    public static Entity spawnEntity(NBTTagCompound nbttagcompound, World world, double d0, double d1, double d2, boolean flag, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason spawnReason) {
+        // CraftBukkit end
         return a(nbttagcompound, world, (entity) -> {
             entity.setPositionRotation(d0, d1, d2, entity.yaw, entity.pitch);
-            return flag && !world.addEntity(entity) ? null : entity;
+            return flag && !world.addEntity(entity, spawnReason) ? null : entity;
         });
     }
 
     @Nullable
+    // CraftBukkit start
     public static Entity a(NBTTagCompound nbttagcompound, World world, boolean flag) {
+        return spawnEntity(nbttagcompound, world, flag, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.DEFAULT);
+    }
+
+    public static Entity spawnEntity(NBTTagCompound nbttagcompound, World world, boolean flag, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason spawnReason) {
+        // CraftBukkit end
         return a(nbttagcompound, world, (entity) -> {
-            return flag && !world.addEntity(entity) ? null : entity;
+            return flag && !world.addEntity(entity, spawnReason) ? null : entity; // CraftBukkit
         });
     }
 
@@ -874,8 +971,14 @@
         }
     }
 
+    // CraftBukkit start
     public static void a(Entity entity, GeneratorAccess generatoraccess) {
-        if (generatoraccess.addEntity(entity) && entity.isVehicle()) {
+        a(entity, generatoraccess, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.DEFAULT);
+    }
+
+    public static void a(Entity entity, GeneratorAccess generatoraccess, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason reason) {
+        if (generatoraccess.addEntity(entity, reason) && entity.isVehicle()) {
+            // CraftBukkit end
             Iterator iterator = entity.bP().iterator();
 
             while (iterator.hasNext()) {
@@ -891,7 +994,7 @@
         boolean flag = false;
 
         try {
-            this.a(dimensionmanager, persistentcollection, chunkcoordintpair.x, chunkcoordintpair.z);
+            this.a(dimensionmanager, persistentcollection, chunkcoordintpair.x, chunkcoordintpair.z, null); // CraftBukkit
 
             while (this.a()) {
                 flag = true;