From 8c8e7968ab61e60ad5cd8a8d53fa42f7a4d9e961 Mon Sep 17 00:00:00 2001
From: Noah van der Aa <ndvdaa@gmail.com>
Date: Fri, 7 Jan 2022 11:58:26 +0100
Subject: [PATCH] Don't tick markers

Fixes https://github.com/PaperMC/Paper/issues/7276 and https://github.com/PaperMC/Paper/issues/8118
by using a config option that, when set to false, does not add markers to the entity
tick list at all and ignores them in Spigot's activation range checks. The entity tick
list is only used in the tick and tickPassenger methods, so we can safely not add the
markers to it. When the config option is set to true, markers are ticked as normal.
---
 .../server/level/ServerLevel.java.patch       | 25 +++++++++++++------
 .../command/subcommands/EntityCommand.java    |  2 +-
 2 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/paper-server/patches/sources/net/minecraft/server/level/ServerLevel.java.patch b/paper-server/patches/sources/net/minecraft/server/level/ServerLevel.java.patch
index 8ca0b75ca2..129855c389 100644
--- a/paper-server/patches/sources/net/minecraft/server/level/ServerLevel.java.patch
+++ b/paper-server/patches/sources/net/minecraft/server/level/ServerLevel.java.patch
@@ -136,7 +136,7 @@
 +
 +        int minChunkX = minBlockX >> 4;
 +        int minChunkZ = minBlockZ >> 4;
-+
+ 
 +        int maxChunkX = maxBlockX >> 4;
 +        int maxChunkZ = maxBlockZ >> 4;
 +
@@ -227,7 +227,7 @@
 +        boolean flag2 = minecraftserver.forceSynchronousWrites();
 +        DataFixer datafixer = minecraftserver.getFixerUpper();
 +        EntityPersistentStorage<Entity> entitypersistentstorage = new EntityStorage(new SimpleRegionStorage(new RegionStorageInfo(convertable_conversionsession.getLevelId(), resourcekey, "entities"), convertable_conversionsession.getDimensionPath(resourcekey).resolve("entities"), datafixer, flag2, DataFixTypes.ENTITY_CHUNK), this, minecraftserver);
- 
++
          this.entityManager = new PersistentEntitySectionManager<>(Entity.class, new ServerLevel.EntityCallbacks(), entitypersistentstorage);
 -        StructureTemplateManager structuretemplatemanager = server.getStructureManager();
 -        int j = server.getPlayerList().getViewDistance();
@@ -830,14 +830,15 @@
              case NONE:
                  explosion_effect = Explosion.BlockInteraction.KEEP;
                  break;
-@@ -1144,16 +1491,27 @@
+@@ -1143,17 +1490,28 @@
+                 break;
              case TRIGGER:
                  explosion_effect = Explosion.BlockInteraction.TRIGGER_BLOCK;
-                 break;
++                break;
 +            // CraftBukkit start - handle custom explosion type
 +            case STANDARD:
 +                explosion_effect = Explosion.BlockInteraction.DESTROY;
-+                break;
+                 break;
 +            // CraftBukkit end
              default:
                  throw new MatchException((String) null, (Throwable) null);
@@ -1035,7 +1036,15 @@
      @Override
      public CrashReportCategory fillReportDetails(CrashReport report) {
          CrashReportCategory crashreportsystemdetails = super.fillReportDetails(report);
-@@ -1836,7 +2242,8 @@
+@@ -1828,6 +2234,7 @@
+         }
+ 
+         public void onTickingStart(Entity entity) {
++            if (entity instanceof net.minecraft.world.entity.Marker && !paperConfig().entities.markers.tick) return; // Paper - Configurable marker ticking
+             ServerLevel.this.entityTickList.add(entity);
+         }
+ 
+@@ -1836,7 +2243,8 @@
          }
  
          public void onTrackingStart(Entity entity) {
@@ -1045,7 +1054,7 @@
              if (entity instanceof ServerPlayer entityplayer) {
                  ServerLevel.this.players.add(entityplayer);
                  ServerLevel.this.updateSleepingPlayerList();
-@@ -1864,9 +2271,58 @@
+@@ -1864,9 +2272,58 @@
              }
  
              entity.updateDynamicGameEventListener(DynamicGameEventListener::add);
@@ -1104,7 +1113,7 @@
              ServerLevel.this.getChunkSource().removeEntity(entity);
              if (entity instanceof ServerPlayer entityplayer) {
                  ServerLevel.this.players.remove(entityplayer);
-@@ -1895,6 +2351,15 @@
+@@ -1895,6 +2352,15 @@
              }
  
              entity.updateDynamicGameEventListener(DynamicGameEventListener::remove);
diff --git a/paper-server/src/main/java/io/papermc/paper/command/subcommands/EntityCommand.java b/paper-server/src/main/java/io/papermc/paper/command/subcommands/EntityCommand.java
index 9d9d133e0d..f671b74e41 100644
--- a/paper-server/src/main/java/io/papermc/paper/command/subcommands/EntityCommand.java
+++ b/paper-server/src/main/java/io/papermc/paper/command/subcommands/EntityCommand.java
@@ -109,7 +109,7 @@ public final class EntityCommand implements PaperSubcommand {
                 ChunkPos chunk = e.chunkPosition();
                 info.left++;
                 info.right.put(chunk, info.right.getOrDefault(chunk, 0) + 1);
-                if (!world.isPositionEntityTicking(e.blockPosition())) {
+                if (!world.isPositionEntityTicking(e.blockPosition()) || (e instanceof net.minecraft.world.entity.Marker && !world.paperConfig().entities.markers.tick)) { // Paper - Configurable marker ticking
                     nonEntityTicking.merge(key, 1, Integer::sum);
                 }
             });