summaryrefslogtreecommitdiffstats
path: root/nms-patches/PlayerChunk.patch
blob: 049b3d5804fe47aba36eabfe5757cea3cdba9fc4 (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
--- ../work/decompile-8eb82bde//net/minecraft/server/PlayerChunk.java	2014-11-28 11:22:56.138823158 +0000
+++ src/main/java/net/minecraft/server/PlayerChunk.java	2014-11-28 11:01:54.000000000 +0000
@@ -3,6 +3,11 @@
 import com.google.common.collect.Lists;
 import java.util.List;
 
+// CraftBukkit start
+import org.bukkit.craftbukkit.chunkio.ChunkIOExecutor;
+import java.util.HashMap;
+// CraftBukkit end
+
 class PlayerChunk {
 
     private final List b;
@@ -12,16 +17,26 @@
     private int f;
     private long g;
     final PlayerChunkMap playerChunkMap;
+    
+    // CraftBukkit start - add fields
+    private final HashMap<EntityPlayer, Runnable> players = new HashMap<EntityPlayer, Runnable>();
+    private boolean loaded = false;
+    private Runnable loadedRunnable = new Runnable() {
+        public void run() {
+            PlayerChunk.this.loaded = true;
+        }
+    };
+    // CraftBukkit end
 
     public PlayerChunk(PlayerChunkMap playerchunkmap, int i, int j) {
         this.playerChunkMap = playerchunkmap;
         this.b = Lists.newArrayList();
         this.dirtyBlocks = new short[64];
         this.location = new ChunkCoordIntPair(i, j);
-        playerchunkmap.a().chunkProviderServer.getChunkAt(i, j);
+        playerchunkmap.a().chunkProviderServer.getChunkAt(i, j, loadedRunnable); // CraftBukkit
     }
 
-    public void a(EntityPlayer entityplayer) {
+    public void a(final EntityPlayer entityplayer) { // CraftBukkit - added final to argument
         if (this.b.contains(entityplayer)) {
             PlayerChunkMap.c().debug("Failed to add player. {} already is in chunk {}, {}", new Object[] { entityplayer, Integer.valueOf(this.location.x), Integer.valueOf(this.location.z)});
         } else {
@@ -30,18 +45,50 @@
             }
 
             this.b.add(entityplayer);
-            entityplayer.chunkCoordIntPairQueue.add(this.location);
+            // CraftBukkit start - use async chunk io
+            Runnable playerRunnable;
+            if (this.loaded) {
+                playerRunnable = null;
+                entityplayer.chunkCoordIntPairQueue.add(this.location);
+            } else {
+                playerRunnable = new Runnable() {
+                    public void run() {
+                        entityplayer.chunkCoordIntPairQueue.add(PlayerChunk.this.location);
+                    }
+                };
+                this.playerChunkMap.a().chunkProviderServer.getChunkAt(this.location.x, this.location.z, playerRunnable);
+            }
+
+            this.players.put(entityplayer, playerRunnable);
+            // CraftBukkit end
         }
     }
 
     public void b(EntityPlayer entityplayer) {
         if (this.b.contains(entityplayer)) {
+            // CraftBukkit start - If we haven't loaded yet don't load the chunk just so we can clean it up
+            if (!this.loaded) {
+                ChunkIOExecutor.dropQueuedChunkLoad(this.playerChunkMap.a(), this.location.x, this.location.z, this.players.get(entityplayer));
+                this.b.remove(entityplayer);
+                this.players.remove(entityplayer);
+
+                if (this.b.isEmpty()) {
+                    ChunkIOExecutor.dropQueuedChunkLoad(this.playerChunkMap.a(), this.location.x, this.location.z, this.loadedRunnable);
+                    long i = (long) this.location.x + 2147483647L | (long) this.location.z + 2147483647L << 32;
+                    PlayerChunkMap.b(this.playerChunkMap).remove(i);
+                    PlayerChunkMap.c(this.playerChunkMap).remove(this);
+                }
+
+                return;
+            }
+            // CraftBukkit end
             Chunk chunk = PlayerChunkMap.a(this.playerChunkMap).getChunkAt(this.location.x, this.location.z);
 
             if (chunk.isReady()) {
                 entityplayer.playerConnection.sendPacket(new PacketPlayOutMapChunk(chunk, true, 0));
             }
 
+            this.players.remove(entityplayer); // CraftBukkit
             this.b.remove(entityplayer);
             entityplayer.chunkCoordIntPairQueue.remove(this.location);
             if (this.b.isEmpty()) {
@@ -122,7 +169,7 @@
                 if (this.dirtyCount == 64) {
                     i = this.location.x * 16;
                     j = this.location.z * 16;
-                    this.a((Packet) (new PacketPlayOutMapChunk(PlayerChunkMap.a(this.playerChunkMap).getChunkAt(this.location.x, this.location.z), false, this.f)));
+                    this.a((Packet) (new PacketPlayOutMapChunk(PlayerChunkMap.a(this.playerChunkMap).getChunkAt(this.location.x, this.location.z), (this.f == 0xFFFF), this.f))); // CraftBukkit - send everything (including biome) if all sections flagged
 
                     for (k = 0; k < 16; ++k) {
                         if ((this.f & 1 << k) != 0) {