From 94b4dc473c08c9903cc6a49e9bb742ce97dbef9f 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/EntityPlayer.java b/src/main/java/net/minecraft/server/EntityPlayer.java
index 8d04c82..ed307bc 100644
--- a/src/main/java/net/minecraft/server/EntityPlayer.java
+++ b/src/main/java/net/minecraft/server/EntityPlayer.java
@@ -53,6 +53,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
     public boolean f;
     public int ping;
     public boolean viewingCredits;
+    public int viewDistance; // Paper - Player view distance API
 
     // CraftBukkit start
     public String displayName;
@@ -68,6 +69,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
 
     public EntityPlayer(MinecraftServer minecraftserver, WorldServer worldserver, GameProfile gameprofile, PlayerInteractManager playerinteractmanager) {
         super(worldserver, gameprofile);
+        this.viewDistance = world.spigotConfig.viewDistance; // Paper - Player view distance API
         playerinteractmanager.player = this;
         this.playerInteractManager = playerinteractmanager;
         BlockPosition blockposition = worldserver.getSpawn();
diff --git a/src/main/java/net/minecraft/server/PlayerChunkMap.java b/src/main/java/net/minecraft/server/PlayerChunkMap.java
index 8ef7eb2..5421026 100644
--- a/src/main/java/net/minecraft/server/PlayerChunkMap.java
+++ b/src/main/java/net/minecraft/server/PlayerChunkMap.java
@@ -284,8 +284,10 @@ 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
+        for (int k = i - entityplayer.viewDistance; k <= i + entityplayer.viewDistance; ++k) {
+            for (int l = j - entityplayer.viewDistance; l <= j + entityplayer.viewDistance; ++l) {
+                // Paper end
                 PlayerChunk playerchunk = this.b(k, l);
 
                 if (playerchunk != null) {
@@ -315,7 +317,7 @@ 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.viewDistance; // Paper - Player view distance API
             int j1 = i - k;
             int k1 = j - l;
 
@@ -471,4 +473,36 @@ public class PlayerChunkMap {
         }
     }
     // CraftBukkit end
+
+    // Paper start - Player view distance API
+    public void updateViewDistance(EntityPlayer player, int viewDistance) {
+        viewDistance = MathHelper.clamp(viewDistance, 3, 32);
+        if (viewDistance != player.viewDistance) {
+            int cx = (int) player.locX >> 4;
+            int cz = (int) player.locZ >> 4;
+
+            if (viewDistance - player.viewDistance > 0) {
+                for (int x = cx - viewDistance; x <= cx + viewDistance; ++x) {
+                    for (int z = cz - viewDistance; z <= cz + viewDistance; ++z) {
+                        PlayerChunk playerchunkmap_playerchunk = this.c(x, z);
+
+                        if (!playerchunkmap_playerchunk.c.contains(player)) {
+                            playerchunkmap_playerchunk.a(player);
+                        }
+                    }
+                }
+            } else {
+                for (int x = cx - player.viewDistance; x <= cx + player.viewDistance; ++x) {
+                    for (int z = cz - player.viewDistance; z <= cz + player.viewDistance; ++z) {
+                        if (!this.a(x, z, cx, cz, viewDistance)) {
+                            this.c(x, z).b(player);
+                        }
+                    }
+                }
+            }
+
+            player.viewDistance = viewDistance;
+        }
+    }
+    // Paper end
 }
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
index 8370d94..8e94ddd 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
@@ -1416,6 +1416,16 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
     public void setAffectsSpawning(boolean affects) {
         getHandle().affectsSpawning = affects;
     }
+
+    @Override
+    public int getViewDistance() {
+        return getHandle().viewDistance;
+    }
+
+    @Override
+    public void setViewDistance(int viewDistance) {
+        ((WorldServer) getHandle().world).getPlayerChunkMap().updateViewDistance(getHandle(), viewDistance);
+    }
     // Paper end
 
     // Spigot start
-- 
2.7.4