Replace ConfiguredStructure api with Structure (#8642)

This commit is contained in:
Jake Potrebic 2023-02-28 08:36:01 -08:00 committed by GitHub
parent f408c253ec
commit e57441254d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
75 changed files with 283 additions and 486 deletions

View file

@ -170,23 +170,27 @@ index 0000000000000000000000000000000000000000..0c83a02059d65672ff191c42932d8509
+}
diff --git a/src/main/java/io/papermc/paper/event/world/StructuresLocateEvent.java b/src/main/java/io/papermc/paper/event/world/StructuresLocateEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..566f9df8f615142e14330965f3491f4e83846783
index 0000000000000000000000000000000000000000..1648ed317145eb73c0cca431823449b2adc43e04
--- /dev/null
+++ b/src/main/java/io/papermc/paper/event/world/StructuresLocateEvent.java
@@ -0,0 +1,164 @@
@@ -0,0 +1,208 @@
+package io.papermc.paper.event.world;
+
+import io.papermc.paper.math.Position;
+import io.papermc.paper.util.TransformingRandomAccessList;
+import io.papermc.paper.world.structure.ConfiguredStructure;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import org.bukkit.Location;
+import org.bukkit.World;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.world.WorldEvent;
+import org.bukkit.generator.structure.Structure;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.jetbrains.annotations.UnmodifiableView;
+
+/**
+ * Called <b>before</b> a set of configured structures is located.
@ -206,15 +210,16 @@ index 0000000000000000000000000000000000000000..566f9df8f615142e14330965f3491f4e
+
+ private final Location origin;
+ private Result result;
+ private List<ConfiguredStructure> configuredStructures;
+ private List<Structure> structures;
+ private List<ConfiguredStructure> legacy$structures;
+ private int radius;
+ private boolean findUnexplored;
+ private boolean cancelled;
+
+ public StructuresLocateEvent(@NotNull World world, @NotNull Location origin, @NotNull List<ConfiguredStructure> configuredStructures, int radius, boolean findUnexplored) {
+ public StructuresLocateEvent(@NotNull World world, @NotNull Location origin, @NotNull List<Structure> structures, int radius, boolean findUnexplored) {
+ super(world);
+ this.origin = origin;
+ this.configuredStructures = configuredStructures;
+ this.setStructures(structures);
+ this.radius = radius;
+ this.findUnexplored = findUnexplored;
+ }
@ -229,23 +234,23 @@ index 0000000000000000000000000000000000000000..566f9df8f615142e14330965f3491f4e
+ }
+
+ /**
+ * Gets the {@link Location} and {@link ConfiguredStructure} set as the result, if it was defined.
+ * Gets the {@link Location} and {@link Structure} set as the result, if it was defined.
+ * <p>
+ * Returns {@code null} if it has not been set by {@link StructuresLocateEvent#setResult(Result)}.
+ * Since this event fires <i>before</i> the search is done, the actual result is unknown at this point.
+ *
+ * @return The result location and structure, if it has been set. null if it has not.
+ * @see World#locateNearestStructure(Location, org.bukkit.StructureType, int, boolean)
+ * @see World#locateNearestStructure(Location, org.bukkit.generator.structure.StructureType, int, boolean)
+ */
+ public @Nullable Result getResult() {
+ return this.result;
+ }
+
+ /**
+ * Sets the result {@link Location} and {@link ConfiguredStructure}. This causes the search to be
+ * Sets the result {@link Location} and {@link Structure}. This causes the search to be
+ * skipped, and the result object passed here to be used as the result.
+ *
+ * @param result the {@link Location} and {@link ConfiguredStructure} of the search.
+ * @param result the {@link Location} and {@link Structure} of the search.
+ */
+ public void setResult(@Nullable Result result) {
+ this.result = result;
@ -255,18 +260,41 @@ index 0000000000000000000000000000000000000000..566f9df8f615142e14330965f3491f4e
+ * Gets a mutable list of ConfiguredStructures that are valid targets for the search.
+ *
+ * @return a mutable list of ConfiguredStructures
+ * @deprecated use {@link #getStructures()}
+ */
+ @Deprecated(forRemoval = true)
+ public @NotNull List<ConfiguredStructure> getConfiguredStructures() {
+ return this.configuredStructures;
+ return this.legacy$structures;
+ }
+
+ /**
+ * Sets the list of ConfiguredStructures that are valid targets for the search.
+ *
+ * @param configuredStructures a list of ConfiguredStructure targets
+ * @deprecated use {@link #setStructures(List)}
+ */
+ @Deprecated(forRemoval = true)
+ public void setConfiguredStructures(@NotNull List<ConfiguredStructure> configuredStructures) {
+ this.configuredStructures = new ArrayList<>(configuredStructures);
+ this.setStructures(configuredStructures.stream().map(ConfiguredStructure::toModern).toList());
+ }
+
+ /**
+ * Gets an unmodifiable list of Structures that are valid targets for the search.
+ *
+ * @return an unmodifiable list of Structures
+ */
+ public @NotNull @UnmodifiableView List<Structure> getStructures() {
+ return Collections.unmodifiableList(this.structures);
+ }
+
+ /**
+ * Sets the list of Structures that are valid targets for the search.
+ *
+ * @param structures a list of Structures targets
+ */
+ public void setStructures(final @NotNull List<Structure> structures) {
+ this.structures = structures;
+ this.legacy$structures = new TransformingRandomAccessList<>(this.structures, ConfiguredStructure::fromModern, ConfiguredStructure::toModern);
+ }
+
+ /**
@ -274,7 +302,7 @@ index 0000000000000000000000000000000000000000..566f9df8f615142e14330965f3491f4e
+ * <p>
+ * This radius may not always be obeyed during the structure search!
+ *
+ * @return the search radius.
+ * @return the search radius (in chunks)
+ */
+ public int getRadius() {
+ return this.radius;
@ -285,7 +313,7 @@ index 0000000000000000000000000000000000000000..566f9df8f615142e14330965f3491f4e
+ * <p>
+ * This radius may not always be obeyed during the structure search!
+ *
+ * @param radius the search radius.
+ * @param radius the search radius (in chunks)
+ */
+ public void setRadius(int radius) {
+ this.radius = radius;
@ -335,31 +363,51 @@ index 0000000000000000000000000000000000000000..566f9df8f615142e14330965f3491f4e
+ /**
+ * Result for {@link StructuresLocateEvent}.
+ */
+ public record Result(@NotNull Location position, @NotNull ConfiguredStructure configuredStructure) {
+ public record Result(@NotNull Position pos, @NotNull Structure structure) {
+
+ @Deprecated(forRemoval = true)
+ public Result(final @NotNull Location position, @NotNull ConfiguredStructure configuredStructure) {
+ this(position, configuredStructure.toModern());
+ }
+
+ @Deprecated(forRemoval = true)
+ public @NotNull ConfiguredStructure configuredStructure() {
+ return Objects.requireNonNull(ConfiguredStructure.fromModern(this.structure), "Please use the newer Structure API");
+ }
+
+ @Deprecated(forRemoval = true)
+ public @NotNull Location position() {
+ //noinspection DataFlowIssue
+ return this.pos.toLocation(null);
+ }
+ }
+}
diff --git a/src/main/java/io/papermc/paper/world/structure/ConfiguredStructure.java b/src/main/java/io/papermc/paper/world/structure/ConfiguredStructure.java
new file mode 100644
index 0000000000000000000000000000000000000000..5a43e40b7311ed2acb51f6ba8b12d1f34569ff2e
index 0000000000000000000000000000000000000000..b9a98f3c89cf847afd510bd1588d3a6d4b7eac0e
--- /dev/null
+++ b/src/main/java/io/papermc/paper/world/structure/ConfiguredStructure.java
@@ -0,0 +1,98 @@
@@ -0,0 +1,112 @@
+package io.papermc.paper.world.structure;
+
+import io.papermc.paper.registry.Reference;
+import java.util.Objects;
+import org.bukkit.Keyed;
+import org.bukkit.NamespacedKey;
+import org.bukkit.Registry;
+import org.bukkit.StructureType;
+import org.bukkit.generator.structure.Structure;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Objects;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Represents a configured structure each with a
+ * {@link StructureType}. Multiple ConfiguredStructures can have
+ * the same {@link StructureType}.
+ * @deprecated use {@link Structure}
+ */
+@Deprecated(forRemoval = true)
+public final class ConfiguredStructure implements Keyed {
+
+ public static final Reference<ConfiguredStructure> PILLAGER_OUTPOST = create("pillager_outpost");
@ -441,12 +489,22 @@ index 0000000000000000000000000000000000000000..5a43e40b7311ed2acb51f6ba8b12d1f3
+ private static @NotNull Reference<ConfiguredStructure> create(@NotNull String name) {
+ return Reference.create(Registry.CONFIGURED_STRUCTURE, NamespacedKey.minecraft(name));
+ }
+
+ @ApiStatus.Internal
+ public @NotNull Structure toModern() {
+ return Objects.requireNonNull(Registry.STRUCTURE.get(this.key));
+ }
+
+ @ApiStatus.Internal
+ public static @Nullable ConfiguredStructure fromModern(@NotNull Structure structure) {
+ return Registry.CONFIGURED_STRUCTURE.get(structure.getKey());
+ }
+}
diff --git a/src/main/java/org/bukkit/Registry.java b/src/main/java/org/bukkit/Registry.java
index ff1fcdaccbca81602278a0b52670f7b895ba22b7..9e39c028b3d7bea4f2998d6ed8d53d88361ccfcd 100644
index ff1fcdaccbca81602278a0b52670f7b895ba22b7..c0655a7c29e113297484a53d72cc5ea0affbe8ce 100644
--- a/src/main/java/org/bukkit/Registry.java
+++ b/src/main/java/org/bukkit/Registry.java
@@ -211,6 +211,13 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
@@ -211,6 +211,15 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
return GameEvent.getByKey(key);
}
};
@ -454,7 +512,9 @@ index ff1fcdaccbca81602278a0b52670f7b895ba22b7..9e39c028b3d7bea4f2998d6ed8d53d88
+ /**
+ * Configured structures.
+ * @see io.papermc.paper.world.structure.ConfiguredStructure
+ * @deprecated use {@link #STRUCTURE}
+ */
+ @Deprecated(forRemoval = true)
+ Registry<io.papermc.paper.world.structure.ConfiguredStructure> CONFIGURED_STRUCTURE = Bukkit.getRegistry(io.papermc.paper.world.structure.ConfiguredStructure.class);
+ // Paper end