2022-03-01 23:40:34 +01:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Tue, 1 Mar 2022 14:12:17 -0800
Subject: [PATCH] Fix World#locateNearestStructure
1.18.2 switched to TagKeys to reference tags of objects, and this method
impl needs to be changed to reflect that
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
2022-06-16 22:59:53 +02:00
index ecaa4d9ff28551b7039f3489f1884d3b664c8ce6..eee4badba71c7e568224b91893a0647f8d8ab6cc 100644
2022-03-01 23:40:34 +01:00
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
2022-06-08 16:46:01 +02:00
@@ -2022,6 +2022,11 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
2022-03-01 23:40:34 +01:00
this.resources.managers.updateRegistryTags(this.registryAccess());
2022-03-04 09:09:43 +01:00
io.papermc.paper.registry.PaperRegistry.clearCaches(); // Paper
2022-03-01 23:40:34 +01:00
net.minecraft.world.item.alchemy.PotionBrewing.reload(); // Paper
+ // Paper start - clear cache cause datapacks can add more configured structures
+ for (ServerLevel level : this.levels.values()) {
+ level.getWorld().structureCache.clear();
+ }
+ // Paper end
new io.papermc.paper.event.server.ServerResourcesReloadedEvent(cause).callEvent(); // Paper
2022-06-16 22:59:53 +02:00
// Paper start
if (Thread.currentThread() != this.serverThread) {
2022-03-01 23:40:34 +01:00
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
2022-06-16 22:59:53 +02:00
index da1533ade2d51a6a978ba4334d07b07681421693..570711979720d85590c10573e4201e60244df4eb 100644
2022-03-01 23:40:34 +01:00
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
2022-06-10 03:20:47 +02:00
@@ -1887,7 +1887,16 @@ public class ServerLevel extends Level implements WorldGenLevel {
2022-03-01 23:40:34 +01:00
if (optional.isEmpty()) {
return null;
} else {
2022-06-08 16:46:01 +02:00
- Pair<BlockPos, Holder<Structure>> pair = this.getChunkSource().getGenerator().findNearestMapStructure(this, (HolderSet) optional.get(), pos, radius, skipReferencedStructures);
2022-03-01 23:40:34 +01:00
+ // Paper start
2022-06-08 16:46:01 +02:00
+ return this.findNearestMapFeature(optional.get(), pos, radius, skipReferencedStructures);
2022-03-01 23:40:34 +01:00
+ }
+ }
+ }
2022-06-08 16:46:01 +02:00
+ public @Nullable BlockPos findNearestMapFeature(HolderSet<Structure> holderSet, BlockPos pos, int radius, boolean skipReferencedStructures) {
2022-03-01 23:40:34 +01:00
+ {
+ {
2022-06-08 16:46:01 +02:00
+ Pair<BlockPos, Holder<Structure>> pair = this.getChunkSource().getGenerator().findNearestMapStructure(this, holderSet, pos, radius, skipReferencedStructures);
2022-03-01 23:40:34 +01:00
+ // Paper end
return pair != null ? (BlockPos) pair.getFirst() : null;
}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
2022-06-16 22:59:53 +02:00
index 131f40a7defc87a01f6a554f7c7819d34e84fe1d..526f7b170270efe34dc43110e23c013f19665b9e 100644
2022-03-01 23:40:34 +01:00
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
2022-06-08 16:46:01 +02:00
@@ -2059,10 +2059,22 @@ public class CraftWorld extends CraftRegionAccessor implements World {
2022-03-01 23:40:34 +01:00
}
2022-06-08 16:46:01 +02:00
+ public final Map<StructureType, List<Holder.Reference<net.minecraft.world.level.levelgen.structure.Structure>>> structureCache = new java.util.HashMap<>(); // Paper
2022-03-01 23:40:34 +01:00
@Override
public Location locateNearestStructure(Location origin, StructureType structureType, int radius, boolean findUnexplored) {
BlockPos originPos = new BlockPos(origin.getX(), origin.getY(), origin.getZ());
2022-06-08 16:46:01 +02:00
- BlockPos nearest = this.getHandle().findNearestMapStructure(TagKey.create(Registry.STRUCTURE_REGISTRY, CraftNamespacedKey.toMinecraft(structureType.getKey())), originPos, radius, findUnexplored);
2022-03-01 23:40:34 +01:00
+ // Paper start - fix because you can't just create random TagKeys
2022-06-08 16:46:01 +02:00
+ if (!this.getHandle().serverLevelData.worldGenSettings().generateStructures()) { // from ServerLevel#findNearestMapStructure
2022-03-01 23:40:34 +01:00
+ return null;
+ }
2022-06-08 16:46:01 +02:00
+ final List<Holder.Reference<net.minecraft.world.level.levelgen.structure.Structure>> features = this.structureCache.computeIfAbsent(structureType, (type) -> {
+ final Registry<net.minecraft.world.level.levelgen.structure.StructureType<?>> structureFeatureRegistry = this.getHandle().registryAccess().registryOrThrow(Registry.STRUCTURE_TYPE_REGISTRY);
+ return this.getHandle().registryAccess().registryOrThrow(Registry.STRUCTURE_REGISTRY).holders().filter(holder -> {
+ return structureType.getKey().equals(CraftNamespacedKey.fromMinecraft(Objects.requireNonNull(structureFeatureRegistry.getKey(holder.value().type()))));
2022-03-01 23:40:34 +01:00
+ }).toList();
+ });
+ BlockPos nearest = this.getHandle().findNearestMapFeature(net.minecraft.core.HolderSet.direct(features), originPos, radius, findUnexplored);
+ // Paper end
return (nearest == null) ? null : new Location(this, nearest.getX(), nearest.getY(), nearest.getZ());
}