From 9b2964fe634014e8dc67f3a083588b667cfb9817 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Sun, 5 Jun 2022 17:17:27 -0700
Subject: [PATCH] Fix treasure maps discovered settings (#7627)

---
 patches/server/Cap-Entity-Collisions.patch    |  4 +--
 ...figurable-Cartographer-Treasure-Maps.patch | 35 +++++++++++++++----
 ...le-Keep-Spawn-Loaded-range-per-world.patch |  4 +--
 patches/server/Paper-config-files.patch       |  9 +++--
 .../server/add-per-world-spawn-limits.patch   |  2 +-
 .../incremental-chunk-and-player-saving.patch |  4 +--
 6 files changed, 43 insertions(+), 15 deletions(-)

diff --git a/patches/server/Cap-Entity-Collisions.patch b/patches/server/Cap-Entity-Collisions.patch
index 1a093e21f2..47578814fd 100644
--- a/patches/server/Cap-Entity-Collisions.patch
+++ b/patches/server/Cap-Entity-Collisions.patch
@@ -16,8 +16,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 --- a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
 +++ b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
 @@ -0,0 +0,0 @@ public class PaperWorldConfig {
-             log("Treasure Maps will return already discovered locations");
-         }
+         treasureMapsAlreadyDiscoveredVillager = getBoolean("treasure-maps-find-already-discovered.villager-trade", treasureMapsAlreadyDiscoveredVillager);
+         treasureMapsAlreadyDiscoveredLootTable = getBooleanOrNull("treasure-maps-find-already-discovered.loot-tables", treasureMapsAlreadyDiscoveredLootTable);
      }
 +
 +    public int maxCollisionsPerEntity = 8;
diff --git a/patches/server/Configurable-Cartographer-Treasure-Maps.patch b/patches/server/Configurable-Cartographer-Treasure-Maps.patch
index 5b665b7cf0..f593bbdb38 100644
--- a/patches/server/Configurable-Cartographer-Treasure-Maps.patch
+++ b/patches/server/Configurable-Cartographer-Treasure-Maps.patch
@@ -12,19 +12,42 @@ diff --git a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java b/src/m
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
 --- a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
 +++ b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
+@@ -0,0 +0,0 @@ public class PaperWorldConfig {
+             set("despawn-ranges.hard", null);
+         }
+ 
++        if (this.config.isSet("world-settings.default.treasure-maps-return-already-discovered") || this.config.isSet("world-settings." + worldName + ".treasure-maps-return-already-discovered")) {
++            set("treasure-maps-return-already-discovered", null);
++            needsSave = true;
++        }
++
+         if (needsSave) {
+             saveConfig();
+         }
 @@ -0,0 +0,0 @@ public class PaperWorldConfig {
              Bukkit.getLogger().warning("Spawn Egg and Armor Stand NBT filtering disabled, this is a potential security risk");
          }
      }
 +
 +    public boolean enableTreasureMaps = true;
-+    public boolean treasureMapsAlreadyDiscovered = false;
++    public boolean treasureMapsAlreadyDiscoveredVillager = false;
++    public Boolean treasureMapsAlreadyDiscoveredLootTable = null;
++    private Boolean getBooleanOrNull(String path, Boolean defaultValue) {
++        this.config.addDefault("world-settings.default." + path, defaultValue == null ? "default" : defaultValue);
++        final Object value = this.config.get("world-settings." + worldName + "." + path, this.config.get("world-settings.default." + path));
++        if (value instanceof Boolean bool) {
++            return bool;
++        }
++        return null;
++    }
 +    private void treasureMapsAlreadyDiscovered() {
 +        enableTreasureMaps = getBoolean("enable-treasure-maps", true);
-+        treasureMapsAlreadyDiscovered = getBoolean("treasure-maps-return-already-discovered", false);
-+        if (treasureMapsAlreadyDiscovered) {
-+            log("Treasure Maps will return already discovered locations");
++        if (getBoolean("treasure-maps-return-already-discovered", false, false)) {
++            treasureMapsAlreadyDiscoveredLootTable = true;
++            treasureMapsAlreadyDiscoveredVillager = true;
 +        }
++        treasureMapsAlreadyDiscoveredVillager = getBoolean("treasure-maps-find-already-discovered.villager-trade", treasureMapsAlreadyDiscoveredVillager);
++        treasureMapsAlreadyDiscoveredLootTable = getBooleanOrNull("treasure-maps-find-already-discovered.loot-tables", treasureMapsAlreadyDiscoveredLootTable);
 +    }
  }
 diff --git a/src/main/java/net/minecraft/world/entity/npc/VillagerTrades.java b/src/main/java/net/minecraft/world/entity/npc/VillagerTrades.java
@@ -37,7 +60,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
                  ServerLevel serverLevel = (ServerLevel)entity.level;
 -                BlockPos blockPos = serverLevel.findNearestMapFeature(this.destination, entity.blockPosition(), 100, true);
 +                if (!serverLevel.paperConfig.enableTreasureMaps) return null; // Paper
-+                BlockPos blockPos = serverLevel.findNearestMapFeature(this.destination, entity.blockPosition(), 100, !serverLevel.paperConfig.treasureMapsAlreadyDiscovered); // Paper
++                BlockPos blockPos = serverLevel.findNearestMapFeature(this.destination, entity.blockPosition(), 100, !serverLevel.paperConfig.treasureMapsAlreadyDiscoveredVillager); // Paper
                  if (blockPos != null) {
                      ItemStack itemStack = MapItem.create(serverLevel, blockPos.getX(), blockPos.getZ(), (byte)2, true, true);
                      MapItem.renderBiomePreviewMap(serverLevel, itemStack);
@@ -59,7 +82,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +                    return stack;
 +                }
 +                // Paper end
-+                BlockPos blockPos = serverLevel.findNearestMapFeature(this.destination, new BlockPos(vec3), this.searchRadius, !serverLevel.paperConfig.treasureMapsAlreadyDiscovered && this.skipKnownStructures); // Paper
++                BlockPos blockPos = serverLevel.findNearestMapFeature(this.destination, new BlockPos(vec3), this.searchRadius, serverLevel.paperConfig.treasureMapsAlreadyDiscoveredLootTable == null ? this.skipKnownStructures : serverLevel.paperConfig.treasureMapsAlreadyDiscoveredLootTable); // Paper
                  if (blockPos != null) {
                      ItemStack itemStack = MapItem.create(serverLevel, blockPos.getX(), blockPos.getZ(), this.zoom, true, true);
                      MapItem.renderBiomePreviewMap(serverLevel, itemStack);
diff --git a/patches/server/Configurable-Keep-Spawn-Loaded-range-per-world.patch b/patches/server/Configurable-Keep-Spawn-Loaded-range-per-world.patch
index 06d1204097..6a68c250ce 100644
--- a/patches/server/Configurable-Keep-Spawn-Loaded-range-per-world.patch
+++ b/patches/server/Configurable-Keep-Spawn-Loaded-range-per-world.patch
@@ -20,8 +20,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +    }
 +
      private boolean getBoolean(String path, boolean def) {
-         config.addDefault("world-settings.default." + path, def);
-         return config.getBoolean("world-settings." + worldName + "." + path, config.getBoolean("world-settings.default." + path));
+         return this.getBoolean(path, def, true);
+     }
 diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
 --- a/src/main/java/net/minecraft/server/MinecraftServer.java
diff --git a/patches/server/Paper-config-files.patch b/patches/server/Paper-config-files.patch
index 765194ee17..872f785f94 100644
--- a/patches/server/Paper-config-files.patch
+++ b/patches/server/Paper-config-files.patch
@@ -259,8 +259,13 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +    }
 +
 +    private boolean getBoolean(String path, boolean def) {
-+        config.addDefault("world-settings.default." + path, def);
-+        return config.getBoolean("world-settings." + worldName + "." + path, config.getBoolean("world-settings.default." + path));
++        return this.getBoolean(path, def, true);
++    }
++    private boolean getBoolean(String path, boolean def, boolean setDefault) {
++        if (setDefault) {
++            config.addDefault("world-settings.default." + path, def);
++        }
++        return config.getBoolean("world-settings." + worldName + "." + path, config.getBoolean("world-settings.default." + path, def));
 +    }
 +
 +    private double getDouble(String path, double def) {
diff --git a/patches/server/add-per-world-spawn-limits.patch b/patches/server/add-per-world-spawn-limits.patch
index b9a7228e7c..1de924b2f3 100644
--- a/patches/server/add-per-world-spawn-limits.patch
+++ b/patches/server/add-per-world-spawn-limits.patch
@@ -20,7 +20,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +            set("spawn-limits.water-ambient", null);
          }
  
-         if (needsSave) {
+         if (this.config.isSet("world-settings.default.treasure-maps-return-already-discovered") || this.config.isSet("world-settings." + worldName + ".treasure-maps-return-already-discovered")) {
 @@ -0,0 +0,0 @@ public class PaperWorldConfig {
          zombieVillagerInfectionChance = getDouble("zombie-villager-infection-chance", zombieVillagerInfectionChance);
      }
diff --git a/patches/server/incremental-chunk-and-player-saving.patch b/patches/server/incremental-chunk-and-player-saving.patch
index f9f2566f18..f05052c91b 100644
--- a/patches/server/incremental-chunk-and-player-saving.patch
+++ b/patches/server/incremental-chunk-and-player-saving.patch
@@ -47,8 +47,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +    }
 +
      private boolean getBoolean(String path, boolean def) {
-         config.addDefault("world-settings.default." + path, def);
-         return config.getBoolean("world-settings." + worldName + "." + path, config.getBoolean("world-settings.default." + path));
+         return this.getBoolean(path, def, true);
+     }
 diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
 --- a/src/main/java/net/minecraft/server/MinecraftServer.java