From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Jake Potrebic 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 index 9e93f2b4c28550cd3d1996f0fbab430c0d1c158e..eb668dd94b288ea3dcfa1982eca109ba8e751766 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -2022,6 +2022,11 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop> pair = this.getChunkSource().getGenerator().findNearestMapStructure(this, (HolderSet) optional.get(), pos, radius, skipReferencedStructures); + // Paper start + return this.findNearestMapFeature(optional.get(), pos, radius, skipReferencedStructures); + } + } + } + public @Nullable BlockPos findNearestMapFeature(HolderSet holderSet, BlockPos pos, int radius, boolean skipReferencedStructures) { + { + { + Pair> pair = this.getChunkSource().getGenerator().findNearestMapStructure(this, holderSet, pos, radius, skipReferencedStructures); + // 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 index 2e9b782e34ae25e982b45011a36c3b08ab298648..7cdeaeed4fa2d830ef03b26baf6d17ddc3d5e521 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java @@ -2059,10 +2059,22 @@ public class CraftWorld extends CraftRegionAccessor implements World { } + public final Map>> structureCache = new java.util.HashMap<>(); // Paper @Override public Location locateNearestStructure(Location origin, StructureType structureType, int radius, boolean findUnexplored) { BlockPos originPos = new BlockPos(origin.getX(), origin.getY(), origin.getZ()); - BlockPos nearest = this.getHandle().findNearestMapStructure(TagKey.create(Registry.STRUCTURE_REGISTRY, CraftNamespacedKey.toMinecraft(structureType.getKey())), originPos, radius, findUnexplored); + // Paper start - fix because you can't just create random TagKeys + if (!this.getHandle().serverLevelData.worldGenSettings().generateStructures()) { // from ServerLevel#findNearestMapStructure + return null; + } + final List> features = this.structureCache.computeIfAbsent(structureType, (type) -> { + final Registry> 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())))); + }).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()); }