aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches/0032-Add-player-view-distance-API.patch
blob: 1f7eb0a01f2a4f06d9c91baa65105b948af72176 (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
From b17663fbf677eecd6fbf6267217415c338faf4f2 Mon Sep 17 00:00:00 2001
From: Byteflux <byte@byteflux.net>
Date: Wed, 2 Mar 2016 14:35:27 -0600
Subject: [PATCH] Add player view distance API


diff --git a/src/main/java/net/minecraft/server/EntityHuman.java b/src/main/java/net/minecraft/server/EntityHuman.java
index 58e037e13..e97bb2305 100644
--- a/src/main/java/net/minecraft/server/EntityHuman.java
+++ b/src/main/java/net/minecraft/server/EntityHuman.java
@@ -71,6 +71,15 @@ public abstract class EntityHuman extends EntityLiving {
     // Paper start
     public boolean affectsSpawning = true;
     // Paper end
+    // Paper start - Player view distance API
+    private int viewDistance = -1;
+    public int getViewDistance() {
+        return viewDistance == -1 ? ((WorldServer) world).getPlayerChunkMap().getViewDistance() : viewDistance;
+    }
+    public void setViewDistance(int viewDistance) {
+        this.viewDistance = viewDistance;
+    }
+    // Paper end
 
     // CraftBukkit start
     public boolean fauxSleeping;
diff --git a/src/main/java/net/minecraft/server/EntityTracker.java b/src/main/java/net/minecraft/server/EntityTracker.java
index 45ab33d1a..3854ae976 100644
--- a/src/main/java/net/minecraft/server/EntityTracker.java
+++ b/src/main/java/net/minecraft/server/EntityTracker.java
@@ -200,6 +200,7 @@ public class EntityTracker {
 
     }
 
+    public void updatePlayer(EntityPlayer entityplayer) { a(entityplayer); } // Paper - OBFHELPER
     public void a(EntityPlayer entityplayer) {
         Iterator iterator = this.c.iterator();
 
diff --git a/src/main/java/net/minecraft/server/EntityTrackerEntry.java b/src/main/java/net/minecraft/server/EntityTrackerEntry.java
index d00401ce1..dd6c84b4a 100644
--- a/src/main/java/net/minecraft/server/EntityTrackerEntry.java
+++ b/src/main/java/net/minecraft/server/EntityTrackerEntry.java
@@ -435,7 +435,7 @@ public class EntityTrackerEntry {
     public boolean c(EntityPlayer entityplayer) {
         double d0 = entityplayer.locX - (double) this.xLoc / 4096.0D;
         double d1 = entityplayer.locZ - (double) this.zLoc / 4096.0D;
-        int i = Math.min(this.e, this.f);
+        int i = Math.min(this.e, (entityplayer.getViewDistance() - 1) * 16); // Paper - Use player view distance API
 
         return d0 >= (double) (-i) && d0 <= (double) i && d1 >= (double) (-i) && d1 <= (double) i && this.tracker.a(entityplayer);
     }
diff --git a/src/main/java/net/minecraft/server/PlayerChunkMap.java b/src/main/java/net/minecraft/server/PlayerChunkMap.java
index e01222ad2..55161af9c 100644
--- a/src/main/java/net/minecraft/server/PlayerChunkMap.java
+++ b/src/main/java/net/minecraft/server/PlayerChunkMap.java
@@ -33,7 +33,7 @@ public class PlayerChunkMap {
     private final List<PlayerChunk> g = Lists.newLinkedList();
     private final List<PlayerChunk> h = Lists.newLinkedList();
     private final List<PlayerChunk> i = Lists.newArrayList();
-    private int j;
+    private int j;public int getViewDistance() { return j; } // Paper OBFHELPER
     private long k;
     private boolean l = true;
     private boolean m = true;
@@ -257,8 +257,11 @@ public class PlayerChunkMap {
         // 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) {
+        // Paper start - Player view distance API
+        int viewDistance = entityplayer.getViewDistance();
+        for (int k = i - viewDistance; k <= i + viewDistance; ++k) {
+            for (int l = j - viewDistance; l <= j + viewDistance; ++l) {
+                // Paper end
                 chunkList.add(new ChunkCoordIntPair(k, l));
             }
         }
@@ -277,8 +280,11 @@ public class PlayerChunkMap {
         int i = (int) entityplayer.d >> 4;
         int j = (int) entityplayer.e >> 4;
 
-        for (int k = i - this.j; k <= i + this.j; ++k) {
-            for (int l = j - this.j; l <= j + this.j; ++l) {
+        // Paper start - Player view distance API
+        int viewDistance = entityplayer.getViewDistance();
+        for (int k = i - viewDistance; k <= i + viewDistance; ++k) {
+            for (int l = j - viewDistance; l <= j + viewDistance; ++l) {
+                // Paper end
                 PlayerChunk playerchunk = this.getChunk(k, l);
 
                 if (playerchunk != null) {
@@ -308,7 +314,8 @@ public class PlayerChunkMap {
         if (d2 >= 64.0D) {
             int k = (int) entityplayer.d >> 4;
             int l = (int) entityplayer.e >> 4;
-            int i1 = this.j;
+            int i1 = entityplayer.getViewDistance(); // Paper - Player view distance API
+
             int j1 = i - k;
             int k1 = j - l;
 
@@ -352,6 +359,8 @@ public class PlayerChunkMap {
         return playerchunk != null && playerchunk.d(entityplayer) && playerchunk.e();
     }
 
+    public final void setViewDistanceForAll(int viewDistance) { this.a(viewDistance); } // Paper - OBFHELPER
+    // Paper start - Separate into two methods
     public void a(int i) {
         i = MathHelper.clamp(i, 3, 32);
         if (i != this.j) {
@@ -361,36 +370,55 @@ public class PlayerChunkMap {
 
             while (iterator.hasNext()) {
                 EntityPlayer entityplayer = (EntityPlayer) iterator.next();
-                int k = (int) entityplayer.locX >> 4;
-                int l = (int) entityplayer.locZ >> 4;
-                int i1;
-                int j1;
-
-                if (j > 0) {
-                    for (i1 = k - i; i1 <= k + i; ++i1) {
-                        for (j1 = l - i; j1 <= l + i; ++j1) {
-                            PlayerChunk playerchunk = this.c(i1, j1);
-
-                            if (!playerchunk.d(entityplayer)) {
-                                playerchunk.a(entityplayer);
-                            }
+                this.setViewDistance(entityplayer, i, false); // Paper - Split, don't mark sort pending, we'll handle it after
+            }
+
+            this.j = i;
+            this.e();
+        }
+    }
+
+    public void setViewDistance(EntityPlayer entityplayer, int i) {
+        this.setViewDistance(entityplayer, i, true); // Mark sort pending by default so we don't have to remember to do so all the time
+    }
+    
+    // Copied from above with minor changes
+    public void setViewDistance(EntityPlayer entityplayer, int i, boolean markSort) {
+        i = MathHelper.clamp(i, 3, 32);
+        int oldViewDistance = entityplayer.getViewDistance();
+        if (i != oldViewDistance) {
+            int j = i - oldViewDistance;
+            
+            int k = (int) entityplayer.locX >> 4;
+            int l = (int) entityplayer.locZ >> 4;
+            int i1;
+            int j1;
+
+            if (j > 0) {
+                for (i1 = k - i; i1 <= k + i; ++i1) {
+                    for (j1 = l - i; j1 <= l + i; ++j1) {
+                        PlayerChunk playerchunk = this.c(i1, j1);
+
+                        if (!playerchunk.d(entityplayer)) {
+                            playerchunk.a(entityplayer);
                         }
                     }
-                } else {
-                    for (i1 = k - this.j; i1 <= k + this.j; ++i1) {
-                        for (j1 = l - this.j; j1 <= l + this.j; ++j1) {
-                            if (!this.a(i1, j1, k, l, i)) {
-                                this.c(i1, j1).b(entityplayer);
-                            }
+                }
+            } else {
+                for (i1 = k - oldViewDistance; i1 <= k + oldViewDistance; ++i1) {
+                    for (j1 = l - oldViewDistance; j1 <= l + oldViewDistance; ++j1) {
+                        if (!this.a(i1, j1, k, l, i)) {
+                            this.c(i1, j1).b(entityplayer);
                         }
                     }
                 }
+                if (markSort) {
+                    this.e();
+                }
             }
-
-            this.j = i;
-            this.e();
         }
     }
+    // Paper end
 
     private void e() {
         this.l = true;
@@ -469,4 +497,32 @@ public class PlayerChunkMap {
         }
     }
     // CraftBukkit end
+
+    // Paper start - Player view distance API
+    public void updateViewDistance(EntityPlayer player, int distanceIn) {
+        final int oldViewDistance = player.getViewDistance();
+
+        // This represents the view distance that we will set on the player
+        // It can exist as a negative value
+        int playerViewDistance = MathHelper.clamp(distanceIn, 3, 32);
+
+        // This value is the one we actually use to update the chunk map
+        // We don't ever want this to be a negative
+        int toSet = playerViewDistance;
+
+        if (distanceIn < 0) {
+            playerViewDistance = -1;
+            toSet = world.getPlayerChunkMap().getViewDistance();
+        }
+
+        if (toSet != oldViewDistance) {
+            // Order matters
+            this.setViewDistance(player, toSet);
+            player.setViewDistance(playerViewDistance);
+
+            //Force update entity trackers
+            this.getWorld().getTracker().updatePlayer(player);
+        }
+    }
+    // Paper end
 }
diff --git a/src/main/java/net/minecraft/server/SpawnerCreature.java b/src/main/java/net/minecraft/server/SpawnerCreature.java
index 4eaa5d93b..6720a9648 100644
--- a/src/main/java/net/minecraft/server/SpawnerCreature.java
+++ b/src/main/java/net/minecraft/server/SpawnerCreature.java
@@ -44,7 +44,7 @@ public final class SpawnerCreature {
                     boolean flag3 = true;
                     // Spigot Start
                     byte b0 = worldserver.spigotConfig.mobSpawnRange;
-                    b0 = ( b0 > worldserver.spigotConfig.viewDistance ) ? (byte) worldserver.spigotConfig.viewDistance : b0;
+                    b0 = ( b0 > entityhuman.getViewDistance() ) ? (byte) entityhuman.getViewDistance() : b0; // Paper - Use player view distance API
                     b0 = ( b0 > 8 ) ? 8 : b0;
 
                     for (int i1 = -b0; i1 <= b0; ++i1) {
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
index 0ee063bcd..5496fae40 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
@@ -1626,6 +1626,16 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
     public boolean getAffectsSpawning() {
         return this.getHandle().affectsSpawning;
     }
+
+    @Override
+    public int getViewDistance() {
+        return getHandle().getViewDistance();
+    }
+
+    @Override
+    public void setViewDistance(int viewDistance) {
+        ((WorldServer) getHandle().world).getPlayerChunkMap().updateViewDistance(getHandle(), viewDistance);
+    }
     // Paper end
 
     @Override
diff --git a/src/main/java/org/spigotmc/ActivationRange.java b/src/main/java/org/spigotmc/ActivationRange.java
index a95f93eb7..09df00e94 100644
--- a/src/main/java/org/spigotmc/ActivationRange.java
+++ b/src/main/java/org/spigotmc/ActivationRange.java
@@ -108,13 +108,13 @@ public class ActivationRange
 
         int maxRange = Math.max( monsterActivationRange, animalActivationRange );
         maxRange = Math.max( maxRange, miscActivationRange );
-        maxRange = Math.min( ( world.spigotConfig.viewDistance << 4 ) - 8, maxRange );
+        //maxRange = Math.min( ( world.spigotConfig.viewDistance << 4 ) - 8, maxRange ); Paper - Use player view distance API below instead
 
         for ( EntityHuman player : world.players )
         {
-
+            int playerMaxRange = maxRange = Math.min( ( player.getViewDistance() << 4 ) - 8, maxRange ); // Paper - Use player view distance API
             player.activatedTick = MinecraftServer.currentTick;
-            maxBB = player.getBoundingBox().grow( maxRange, 256, maxRange );
+            maxBB = player.getBoundingBox().grow( playerMaxRange, 256, playerMaxRange ); // Paper - Use player view distance API
             miscBB = player.getBoundingBox().grow( miscActivationRange, 256, miscActivationRange );
             animalBB = player.getBoundingBox().grow( animalActivationRange, 256, animalActivationRange );
             monsterBB = player.getBoundingBox().grow( monsterActivationRange, 256, monsterActivationRange );
-- 
2.21.0