summaryrefslogtreecommitdiffstats
path: root/nms-patches/PlayerChunkMap.patch
blob: 37c4c4744ddf9d6eba1c6c684299605a9f7756df (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
--- a/net/minecraft/server/PlayerChunkMap.java
+++ b/net/minecraft/server/PlayerChunkMap.java
@@ -15,6 +15,10 @@
 import java.util.Set;
 import javax.annotation.Nullable;
 
+// CraftBukkit start
+import java.util.LinkedList;
+// CraftBukkit end
+
 public class PlayerChunkMap {
 
     private static final Predicate<EntityPlayer> a = new Predicate() {
@@ -46,6 +50,7 @@
     private long k;
     private boolean l = true;
     private boolean m = true;
+    private boolean wasNotEmpty; // CraftBukkit - add field
 
     public PlayerChunkMap(WorldServer worldserver) {
         this.world = worldserver;
@@ -169,7 +174,11 @@
                             break;
                         }
                     }
+                // CraftBukkit start - SPIGOT-2891: remove once chunk has been provided
+                } else {
+                    iterator1.remove();
                 }
+                // CraftBukkit end
             }
         }
 
@@ -231,6 +240,16 @@
         return playerchunk;
     }
 
+    // CraftBukkit start - add method
+    public final boolean isChunkInUse(int x, int z) {
+        PlayerChunk pi = getChunk(x, z);
+        if (pi != null) {
+            return (pi.c.size() > 0);
+        }
+        return false;
+    }
+    // CraftBukkit end
+
     public void flagDirty(BlockPosition blockposition) {
         int i = blockposition.getX() >> 4;
         int j = blockposition.getZ() >> 4;
@@ -249,12 +268,22 @@
         entityplayer.d = entityplayer.locX;
         entityplayer.e = entityplayer.locZ;
 
+
+        // CraftBukkit start - Load nearby chunks first
+        List<ChunkCoordIntPair> chunkList = new LinkedList<ChunkCoordIntPair>();
+
         for (int k = i - this.j; k <= i + this.j; ++k) {
             for (int l = j - this.j; l <= j + this.j; ++l) {
-                this.c(k, l).a(entityplayer);
+                chunkList.add(new ChunkCoordIntPair(k, l));
             }
         }
 
+        Collections.sort(chunkList, new ChunkCoordComparator(entityplayer));
+        for (ChunkCoordIntPair pair : chunkList) {
+            this.c(pair.x, pair.z).a(entityplayer);
+        }
+        // CraftBukkit end
+
         this.managedPlayers.add(entityplayer);
         this.e();
     }
@@ -298,11 +327,14 @@
             int j1 = i - k;
             int k1 = j - l;
 
+            List<ChunkCoordIntPair> chunksToLoad = new LinkedList<ChunkCoordIntPair>(); // CraftBukkit
+
             if (j1 != 0 || k1 != 0) {
                 for (int l1 = i - i1; l1 <= i + i1; ++l1) {
                     for (int i2 = j - i1; i2 <= j + i1; ++i2) {
                         if (!this.a(l1, i2, k, l, i1)) {
-                            this.c(l1, i2).a(entityplayer);
+                            // this.c(l1, i2).a(entityplayer);
+                            chunksToLoad.add(new ChunkCoordIntPair(l1, i2)); // CraftBukkit
                         }
 
                         if (!this.a(l1 - j1, i2 - k1, i, j, i1)) {
@@ -318,6 +350,13 @@
                 entityplayer.d = entityplayer.locX;
                 entityplayer.e = entityplayer.locZ;
                 this.e();
+
+                // CraftBukkit start - send nearest chunks first
+                Collections.sort(chunksToLoad, new ChunkCoordComparator(entityplayer));
+                for (ChunkCoordIntPair pair : chunksToLoad) {
+                    this.c(pair.x, pair.z).a(entityplayer);
+                }
+                // CraftBukkit end
             }
         }
     }
@@ -402,4 +441,47 @@
         }
 
     }
+
+    // CraftBukkit start - Sorter to load nearby chunks first
+    private static class ChunkCoordComparator implements java.util.Comparator<ChunkCoordIntPair> {
+        private int x;
+        private int z;
+
+        public ChunkCoordComparator (EntityPlayer entityplayer) {
+            x = (int) entityplayer.locX >> 4;
+            z = (int) entityplayer.locZ >> 4;
+        }
+
+        public int compare(ChunkCoordIntPair a, ChunkCoordIntPair b) {
+            if (a.equals(b)) {
+                return 0;
+            }
+
+            // Subtract current position to set center point
+            int ax = a.x - this.x;
+            int az = a.z - this.z;
+            int bx = b.x - this.x;
+            int bz = b.z - this.z;
+
+            int result = ((ax - bx) * (ax + bx)) + ((az - bz) * (az + bz));
+            if (result != 0) {
+                return result;
+            }
+
+            if (ax < 0) {
+                if (bx < 0) {
+                    return bz - az;
+                } else {
+                    return -1;
+                }
+            } else {
+                if (bx < 0) {
+                    return 1;
+                } else {
+                    return az - bz;
+                }
+            }
+        }
+    }
+    // CraftBukkit end
 }