summaryrefslogtreecommitdiffstats
path: root/nms-patches/Chunk.patch
blob: d24540d67536c48a1231ab82e0190be0556eb99a (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
--- a/net/minecraft/server/Chunk.java
+++ b/net/minecraft/server/Chunk.java
@@ -23,6 +23,8 @@
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
+import com.google.common.collect.Lists; // CraftBukkit
+
 public class Chunk implements IChunkAccess {
 
     private static final Logger d = LogManager.getLogger();
@@ -57,6 +59,35 @@
     private int D;
     private final AtomicInteger E;
 
+    // CraftBukkit start - Neighbor loaded cache for chunk lighting and entity ticking
+    private int neighbors = 0x1 << 12;
+    public long chunkKey;
+
+    public boolean areNeighborsLoaded(final int radius) {
+        switch (radius) {
+            case 2:
+                return this.neighbors == Integer.MAX_VALUE >> 6;
+            case 1:
+                final int mask =
+                        //       x        z   offset          x        z   offset          x         z   offset
+                        (0x1 << (1 * 5 +  1 + 12)) | (0x1 << (0 * 5 +  1 + 12)) | (0x1 << (-1 * 5 +  1 + 12)) |
+                        (0x1 << (1 * 5 +  0 + 12)) | (0x1 << (0 * 5 +  0 + 12)) | (0x1 << (-1 * 5 +  0 + 12)) |
+                        (0x1 << (1 * 5 + -1 + 12)) | (0x1 << (0 * 5 + -1 + 12)) | (0x1 << (-1 * 5 + -1 + 12));
+                return (this.neighbors & mask) == mask;
+            default:
+                throw new UnsupportedOperationException(String.valueOf(radius));
+        }
+    }
+
+    public void setNeighborLoaded(final int x, final int z) {
+        this.neighbors |= 0x1 << (x * 5 + 12 + z);
+    }
+
+    public void setNeighborUnloaded(final int x, final int z) {
+        this.neighbors &= ~(0x1 << (x * 5 + 12 + z));
+    }
+    // CraftBukkit end
+
     public Chunk(World world, int i, int j, BiomeBase[] abiomebase, ChunkConverter chunkconverter, TickList<Block> ticklist, TickList<FluidType> ticklist1, long k) {
         this.sections = new ChunkSection[16];
         this.g = new boolean[256];
@@ -94,8 +125,16 @@
         this.s = ticklist;
         this.t = ticklist1;
         this.z = k;
+        // CraftBukkit start
+        this.bukkitChunk = new org.bukkit.craftbukkit.CraftChunk(this);
+        this.chunkKey = ChunkCoordIntPair.a(this.locX, this.locZ);
     }
 
+    public org.bukkit.Chunk bukkitChunk;
+    public boolean mustSave;
+    private boolean needsDecoration;
+    // CraftBukkit end
+
     public Chunk(World world, ProtoChunk protochunk, int i, int j) {
         this(world, i, j, protochunk.getBiomeIndex(), protochunk.v(), protochunk.n(), protochunk.o(), protochunk.m());
 
@@ -135,14 +174,15 @@
             HeightMap.Type heightmap_type = (HeightMap.Type) iterator.next();
 
             if (heightmap_type.c() == HeightMap.Use.LIVE_WORLD) {
-                ((HeightMap) this.heightMap.computeIfAbsent(heightmap_type, (heightmap_type) -> {
-                    return new HeightMap(this, heightmap_type);
+                ((HeightMap) this.heightMap.computeIfAbsent(heightmap_type, (heightmap_type1) -> { // CraftBukkit - decompile error
+                    return new HeightMap(this, heightmap_type1); // CraftBukkit - decompile error
                 })).a(protochunk.b(heightmap_type).b());
             }
         }
 
         this.x = true;
         this.a(ChunkStatus.FULLCHUNK);
+        this.needsDecoration = protochunk.needsDecoration; // CraftBukkit
     }
 
     public Set<BlockPosition> t() {
@@ -412,8 +452,15 @@
         }
     }
 
+    // CraftBukkit start
     @Nullable
     public IBlockData a(BlockPosition blockposition, IBlockData iblockdata, boolean flag) {
+        return this.a(blockposition, iblockdata, flag, true);
+    }
+
+    @Nullable
+    public IBlockData a(BlockPosition blockposition, IBlockData iblockdata, boolean flag, boolean doPlace) {
+        // CraftBukkit end
         int i = blockposition.getX() & 15;
         int j = blockposition.getY();
         int k = blockposition.getZ() & 15;
@@ -473,7 +520,8 @@
                     }
                 }
 
-                if (!this.world.isClientSide) {
+                // CraftBukkit - Don't place while processing the BlockPlaceEvent, unless it's a BlockContainer. Prevents blocks such as TNT from activating when cancelled.
+                if (!this.world.isClientSide && doPlace && (!this.world.captureBlockStates || block instanceof BlockTileEntity)) {
                     iblockdata.onPlace(this.world, blockposition, iblockdata1);
                 }
 
@@ -653,7 +701,19 @@
 
     @Nullable
     public TileEntity a(BlockPosition blockposition, Chunk.EnumTileEntityState chunk_enumtileentitystate) {
-        TileEntity tileentity = (TileEntity) this.tileEntities.get(blockposition);
+        // CraftBukkit start
+        TileEntity tileentity = world.capturedTileEntities.get(blockposition);
+        if (tileentity == null) {
+            tileentity = (TileEntity) this.tileEntities.get(blockposition);
+        }
+        if (tileentity == null) {
+            NBTTagCompound pending = this.h.remove(blockposition);
+            if (pending != null) {
+                this.processQueuedTile(blockposition, pending);
+                return this.a(blockposition, chunk_enumtileentitystate);
+            }
+        }
+        // CraftBukkit end
 
         if (tileentity == null) {
             if (chunk_enumtileentitystate == Chunk.EnumTileEntityState.IMMEDIATE) {
@@ -679,6 +739,11 @@
     }
 
     public void a(BlockPosition blockposition, TileEntity tileentity) {
+        // CraftBukkit start
+        if (blockposition instanceof BlockPosition.MutableBlockPosition) {
+            blockposition = new BlockPosition(blockposition);
+        }
+        // CraftBukkit end
         tileentity.setWorld(this.world);
         tileentity.setPosition(blockposition);
         if (this.getType(blockposition).getBlock() instanceof ITileEntity) {
@@ -688,6 +753,13 @@
 
             tileentity.z();
             this.tileEntities.put(blockposition, tileentity);
+            // CraftBukkit start
+        } else {
+            System.out.println("Attempted to place a tile entity (" + tileentity + ") at " + tileentity.position.getX() + "," + tileentity.position.getY() + "," + tileentity.position.getZ()
+                + " (" + getType(blockposition) + ") where there was no entity tile!");
+            System.out.println("Chunk coordinates: " + (this.locX * 16) + "," + (this.locZ * 16));
+            new Exception().printStackTrace();
+            // CraftBukkit end
         }
     }
 
@@ -720,6 +792,40 @@
             }));
         }
 
+        // CraftBukkit start
+        org.bukkit.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(this.bukkitChunk, this.needsDecoration));
+
+            if (this.needsDecoration) {
+                BlockSand.instaFall = true;
+                java.util.Random random = new java.util.Random();
+                random.setSeed(world.getSeed());
+                long xRand = random.nextLong() / 2L * 2L + 1L;
+                long zRand = random.nextLong() / 2L * 2L + 1L;
+                random.setSeed((long) locX * xRand + (long) locZ * 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, bukkitChunk);
+                        }
+                    } finally {
+                        this.world.populating = false;
+                    }
+                }
+                BlockSand.instaFall = false;
+                server.getPluginManager().callEvent(new org.bukkit.event.world.ChunkPopulateEvent(bukkitChunk));
+            }
+        }
+        // CraftBukkit end
     }
 
     public void removeEntities() {
@@ -736,9 +842,21 @@
         int i = aentityslice.length;
 
         for (int j = 0; j < i; ++j) {
-            EntitySlice entityslice = aentityslice[j];
+            // CraftBukkit start
+            List<Entity> newList = Lists.newArrayList(aentityslice[j]);
+            java.util.Iterator<Entity> iter = newList.iterator();
+            while (iter.hasNext()) {
+                Entity entity = iter.next();
 
-            this.world.b((Collection) entityslice);
+                // Do not pass along players, as doing so can get them stuck outside of time.
+                // (which for example disables inventory icon updates and prevents block breaking)
+                if (entity instanceof EntityPlayer) {
+                    iter.remove();
+                }
+            }
+
+            this.world.b((Collection) newList);
+            // CraftBukkit end
         }
 
     }
@@ -800,8 +918,8 @@
             while (iterator.hasNext()) {
                 Entity entity = (Entity) iterator.next();
 
-                if (entity.getBoundingBox().c(axisalignedbb) && (predicate == null || predicate.test(entity))) {
-                    list.add(entity);
+                if (entity.getBoundingBox().c(axisalignedbb) && (predicate == null || predicate.test((T) entity))) { // CraftBukkit - fix decompile error
+                    list.add((T) entity); // Fix decompile error
                 }
             }
         }
@@ -1007,13 +1125,13 @@
 
     @Nullable
     public LongSet b(String s) {
-        return (LongSet) this.q.computeIfAbsent(s, (s) -> {
+        return (LongSet) this.q.computeIfAbsent(s, (s1) -> { // CraftBukkit - decompile error
             return new LongOpenHashSet();
         });
     }
 
     public void a(String s, long i) {
-        ((LongSet) this.q.computeIfAbsent(s, (s) -> {
+        ((LongSet) this.q.computeIfAbsent(s, (s1) -> { // CraftBukkit - decompile error
             return new LongOpenHashSet();
         })).add(i);
     }
@@ -1061,18 +1179,18 @@
             }
 
             if (this.s instanceof ProtoChunkTickList) {
-                ((ProtoChunkTickList) this.s).a(this.world.J(), (blockposition) -> {
-                    return this.world.getType(blockposition).getBlock();
+                ((ProtoChunkTickList<Block>) this.s).a(this.world.J(), (blockposition1) -> { // CraftBukkit - decompile error
+                    return this.world.getType(blockposition1).getBlock();
                 });
             }
 
             if (this.t instanceof ProtoChunkTickList) {
-                ((ProtoChunkTickList) this.t).a(this.world.I(), (blockposition) -> {
-                    return this.world.b(blockposition).c();
+                ((ProtoChunkTickList<FluidType>) this.t).a(this.world.I(), (blockposition1) -> { // CraftBukkit - decompile error
+                    return this.world.b(blockposition1).c();
                 });
             }
 
-            Iterator iterator = this.h.entrySet().iterator();
+            Iterator iterator = Maps.newHashMap(this.h).entrySet().iterator(); // CraftBukkit
 
             while (iterator.hasNext()) {
                 Entry entry = (Entry) iterator.next();
@@ -1080,6 +1198,8 @@
                 NBTTagCompound nbttagcompound = (NBTTagCompound) entry.getValue();
 
                 if (this.getTileEntity(blockposition1) == null) {
+                    // CraftBukkit start
+                    /*
                     TileEntity tileentity;
 
                     if ("DUMMY".equals(nbttagcompound.getString("id"))) {
@@ -1101,15 +1221,43 @@
                     } else {
                         Chunk.d.warn("Tried to load a block entity for block {} but failed at location {}", this.getType(blockposition1), blockposition1);
                     }
+                     */
+                    // CraftBukkit end
                 }
             }
 
-            this.h.clear();
+            com.google.common.base.Preconditions.checkState(this.h.isEmpty(), "Pending tiles not empty"); // CraftBukkit
             this.a(ChunkStatus.POSTPROCESSED);
             this.m.a(this);
         }
     }
 
+    // CraftBukkit start
+    private void processQueuedTile(BlockPosition blockposition1, NBTTagCompound nbttagcompound) {
+        TileEntity tileentity;
+
+        if ("DUMMY".equals(nbttagcompound.getString("id"))) {
+            Block block = this.getType(blockposition1).getBlock();
+
+            if (block instanceof ITileEntity) {
+                tileentity = ((ITileEntity) block).a(this.world);
+            } else {
+                tileentity = null;
+                Chunk.d.warn("Tried to load a DUMMY block entity @ {} but found not tile entity block {} at location", blockposition1, this.getType(blockposition1));
+            }
+        } else {
+            tileentity = TileEntity.create(nbttagcompound);
+        }
+
+        if (tileentity != null) {
+            tileentity.setPosition(blockposition1);
+            this.a(tileentity);
+        } else {
+            Chunk.d.warn("Tried to load a block entity for block {} but failed at location {}", this.getType(blockposition1), blockposition1);
+        }
+    }
+    // CraftBukkit end
+
     public ChunkConverter F() {
         return this.m;
     }