From f823b12e0ae874f478609544c272113cb8f76ac4 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Wed, 8 Jun 2022 12:00:19 -0700 Subject: [PATCH] Fix structures issues/api (#7895) --- patches/api/Add-StructuresLocateEvent.patch | 2 +- patches/server/Add-PaperRegistry.patch | 26 ++++++++++++++++--- .../server/Add-StructuresLocateEvent.patch | 14 +++++----- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/patches/api/Add-StructuresLocateEvent.patch b/patches/api/Add-StructuresLocateEvent.patch index c83d699f4f..b2fbf4bb27 100644 --- a/patches/api/Add-StructuresLocateEvent.patch +++ b/patches/api/Add-StructuresLocateEvent.patch @@ -393,7 +393,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + public static final Reference RUINED_PORTAL_MOUNTAIN = create("ruined_portal_mountain"); + public static final Reference RUINED_PORTAL_OCEAN = create("ruined_portal_ocean"); + public static final Reference RUINED_PORTAL_NETHER = create("ruined_portal_nether"); -+ public static final Reference ANCIENT_CITY = create("ancient_city"); ++ // public static final Reference ANCIENT_CITY = create("ancient_city"); // TODO remove when upstream adds "jigsaw" StructureType + + private final NamespacedKey key; + private final StructureType structureType; diff --git a/patches/server/Add-PaperRegistry.patch b/patches/server/Add-PaperRegistry.patch index 248c8c26c4..f91b27d14d 100644 --- a/patches/server/Add-PaperRegistry.patch +++ b/patches/server/Add-PaperRegistry.patch @@ -35,6 +35,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 +import java.util.Objects; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; ++import java.util.function.Consumer; +import java.util.function.Supplier; + +@DefaultQualifier(NonNull.class) @@ -69,13 +70,21 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + }); + } + -+ public abstract API convertToApi(NamespacedKey key, MINECRAFT nms); ++ public abstract @Nullable API convertToApi(NamespacedKey key, MINECRAFT nms); + -+ public API convertToApi(ResourceLocation resourceLocation, MINECRAFT nms) { ++ public API convertToApiOrThrow(ResourceLocation resourceLocation, MINECRAFT nms) { ++ return Objects.requireNonNull(this.convertToApi(resourceLocation, nms), resourceLocation + " has a null api representation"); ++ } ++ ++ public @Nullable API convertToApi(ResourceLocation resourceLocation, MINECRAFT nms) { + return this.convertToApi(CraftNamespacedKey.fromMinecraft(resourceLocation), nms); + } + -+ public API convertToApi(Holder nmsHolder) { ++ public API convertToApiOrThrow(Holder nmsHolder) { ++ return Objects.requireNonNull(this.convertToApi(nmsHolder), nmsHolder + " has a null api representation"); ++ } ++ ++ public @Nullable API convertToApi(Holder nmsHolder) { + final Optional> key = nmsHolder.unwrapKey(); + if (nmsHolder.isBound() && key.isPresent()) { + return this.convertToApi(key.get().location(), nmsHolder.value()); @@ -90,6 +99,17 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + throw new IllegalStateException("Cannot convert " + nmsHolder + " to an API type in: " + this.registryKey); + } + ++ public void convertToApi(Iterable> holders, Consumer apiConsumer, boolean throwOnNull) { ++ for (Holder holder : holders) { ++ final @Nullable API api = this.convertToApi(holder); ++ if (api == null && throwOnNull) { ++ throw new NullPointerException(holder + " has a null api representation"); ++ } else if (api != null) { ++ apiConsumer.accept(api); ++ } ++ } ++ } ++ + public MINECRAFT getMinecraftValue(API apiValue) { + return this.registry.get().getOptional(CraftNamespacedKey.toMinecraft(apiValue.getKey())).orElseThrow(); + } diff --git a/patches/server/Add-StructuresLocateEvent.patch b/patches/server/Add-StructuresLocateEvent.patch index 970a5b04cb..fba8251140 100644 --- a/patches/server/Add-StructuresLocateEvent.patch +++ b/patches/server/Add-StructuresLocateEvent.patch @@ -39,6 +39,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 +import org.bukkit.NamespacedKey; +import org.bukkit.StructureType; +import org.checkerframework.checker.nullness.qual.NonNull; ++import org.checkerframework.checker.nullness.qual.Nullable; +import org.checkerframework.framework.qual.DefaultQualifier; + +import java.util.Objects; @@ -63,10 +64,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + } + + @Override -+ public ConfiguredStructure convertToApi(NamespacedKey key, Structure nms) { ++ public @Nullable ConfiguredStructure convertToApi(NamespacedKey key, Structure nms) { + final ResourceLocation structureTypeLoc = Objects.requireNonNull(Registry.STRUCTURE_TYPES.getKey(nms.type()), "unexpected structure type " + nms.type()); -+ final StructureType structureType = Objects.requireNonNull(StructureType.getStructureTypes().get(structureTypeLoc.getPath()), structureTypeLoc + " could not be converted to an API type"); // TODO this is just not gonna work until upstream fixes their StructureType pseudo-enum -+ return new ConfiguredStructure(key, structureType); ++ final @Nullable StructureType structureType = StructureType.getStructureTypes().get(structureTypeLoc.getPath()); ++ return structureType == null ? null : new ConfiguredStructure(key, structureType); + } + } +} @@ -83,9 +84,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + final org.bukkit.Location origin = net.minecraft.server.MCUtil.toLocation(world, center); + final var paperRegistry = io.papermc.paper.registry.PaperRegistry.getRegistry(io.papermc.paper.registry.RegistryKey.CONFIGURED_STRUCTURE_REGISTRY); + final List configuredStructures = new ArrayList<>(); -+ for (Holder holder : structures) { -+ configuredStructures.add(paperRegistry.convertToApi(holder)); -+ } ++ paperRegistry.convertToApi(structures, configuredStructures::add, false); // gracefully handle missing api, use tests to check (or exclude) + final io.papermc.paper.event.world.StructuresLocateEvent event = new io.papermc.paper.event.world.StructuresLocateEvent(bukkitWorld, origin, configuredStructures, radius, skipReferencedStructures); + if (!event.callEvent()) { + return null; @@ -178,6 +177,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + for (Structure feature : BuiltinRegistries.STRUCTURES) { + final ResourceLocation key = BuiltinRegistries.STRUCTURES.getKey(feature); + assertNotNull("Missing built-in registry key", key); ++ if (key.equals(BuiltinStructures.ANCIENT_CITY.location())) { ++ continue; // TODO remove when upstream adds "jigsaw" StructureType ++ } + if (DEFAULT_CONFIGURED_STRUCTURES.get(CraftNamespacedKey.fromMinecraft(key)) == null) { + missing.put(key, feature); + }