From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Shane Freeder <theboyetronic@gmail.com>
Date: Sun, 11 Nov 2018 21:01:09 +0000
Subject: [PATCH] Don't allow digging into unloaded chunks


diff --git a/src/main/java/net/minecraft/server/PlayerConnection.java b/src/main/java/net/minecraft/server/PlayerConnection.java
index 146efc064b6238f902c49402dbd9ebfc5aad4f52..aaa365ef648f2582723c42a06f594adc3c1f5423 100644
--- a/src/main/java/net/minecraft/server/PlayerConnection.java
+++ b/src/main/java/net/minecraft/server/PlayerConnection.java
@@ -1273,6 +1273,11 @@ public class PlayerConnection implements PacketListenerPlayIn {
             case START_DESTROY_BLOCK:
             case ABORT_DESTROY_BLOCK:
             case STOP_DESTROY_BLOCK:
+                // Paper start - Don't allow digging in unloaded chunks
+                if (this.player.world.getChunkIfLoadedImmediately(blockposition.getX() >> 4, blockposition.getZ() >> 4) == null) {
+                    return;
+                }
+                // Paper end - Don't allow digging in unloaded chunks
                 this.player.playerInteractManager.a(blockposition, packetplayinblockdig_enumplayerdigtype, packetplayinblockdig.c(), this.minecraftServer.getMaxBuildHeight());
                 return;
             default:
diff --git a/src/main/java/net/minecraft/server/PlayerInteractManager.java b/src/main/java/net/minecraft/server/PlayerInteractManager.java
index e2e5c17c24c8f5e9807ca879b1025d13cb195226..ed3f3362b640746649455f8dd2255ac2da03df7c 100644
--- a/src/main/java/net/minecraft/server/PlayerInteractManager.java
+++ b/src/main/java/net/minecraft/server/PlayerInteractManager.java
@@ -69,8 +69,8 @@ public class PlayerInteractManager {
         IBlockData iblockdata;
 
         if (this.i) {
-            iblockdata = this.world.getType(this.j);
-            if (iblockdata.isAir()) {
+            iblockdata = this.world.getTypeIfLoaded(this.j); // Paper
+            if (iblockdata == null || iblockdata.isAir()) { // Paper
                 this.i = false;
             } else {
                 float f = this.a(iblockdata, this.j, this.k);
@@ -81,7 +81,13 @@ public class PlayerInteractManager {
                 }
             }
         } else if (this.e) {
-            iblockdata = this.world.getType(this.g);
+            // Paper start - don't want to do same logic as above, return instead
+            iblockdata = this.world.getTypeIfLoaded(this.g);
+            if (iblockdata == null) {
+                this.e = false;
+                return;
+            }
+            // Paper end
             if (iblockdata.isAir()) {
                 this.world.a(this.player.getId(), this.g, -1);
                 this.l = -1;
@@ -253,10 +259,12 @@ public class PlayerInteractManager {
                 this.player.playerConnection.sendPacket(new PacketPlayOutBlockBreak(blockposition, this.world.getType(blockposition), packetplayinblockdig_enumplayerdigtype, true, "stopped destroying"));
             } else if (packetplayinblockdig_enumplayerdigtype == PacketPlayInBlockDig.EnumPlayerDigType.ABORT_DESTROY_BLOCK) {
                 this.e = false;
-                if (!Objects.equals(this.g, blockposition)) {
+                if (!Objects.equals(this.g, blockposition) && !BlockPosition.ZERO.equals(this.g)) { // Paper
                     PlayerInteractManager.LOGGER.debug("Mismatch in destroy block pos: " + this.g + " " + blockposition); // CraftBukkit - SPIGOT-5457 sent by client when interact event cancelled
-                    this.world.a(this.player.getId(), this.g, -1);
-                    this.player.playerConnection.sendPacket(new PacketPlayOutBlockBreak(this.g, this.world.getType(this.g), packetplayinblockdig_enumplayerdigtype, true, "aborted mismatched destroying"));
+                    IBlockData type = this.world.getTypeIfLoaded(this.g); // Paper - don't load unloaded chunks for stale records here
+                    if (type != null) this.world.a(this.player.getId(), this.g, -1); // Paper
+                    if (type != null) this.player.playerConnection.sendPacket(new PacketPlayOutBlockBreak(this.g, type, packetplayinblockdig_enumplayerdigtype, true, "aborted mismatched destroying")); // Paper
+                    this.g = BlockPosition.ZERO; // Paper
                 }
 
                 this.world.a(this.player.getId(), blockposition, -1);