2022-03-04 09:09:43 +01:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: dfsek <dfsek@protonmail.com>
Date: Tue, 15 Sep 2020 21:59:16 -0700
Subject: [PATCH] Add StructuresLocateEvent
Co-authored-by: Jake Potrebic <jake.m.potrebic@gmail.com>
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..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/io/papermc/paper/event/world/StructuresLocateEvent.java
@@ -0,0 +0,0 @@
+package io.papermc.paper.event.world;
+
2023-02-28 17:36:01 +01:00
+import io.papermc.paper.math.Position;
+import java.util.Collections;
+import java.util.List;
2022-03-04 09:09:43 +01:00
+import org.bukkit.Location;
+import org.bukkit.World;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.world.WorldEvent;
2023-02-28 17:36:01 +01:00
+import org.bukkit.generator.structure.Structure;
2023-04-16 03:13:59 +02:00
+import org.bukkit.generator.structure.StructureType;
2024-02-01 10:15:57 +01:00
+import org.jetbrains.annotations.ApiStatus;
2023-02-28 17:36:01 +01:00
+import org.jetbrains.annotations.UnmodifiableView;
2024-09-30 01:48:34 +02:00
+import org.jspecify.annotations.NullMarked;
+import org.jspecify.annotations.Nullable;
2022-03-04 09:09:43 +01:00
+
+/**
+ * Called <b>before</b> a set of configured structures is located.
+ * This happens when:
+ * <ul>
+ * <li>The /locate command is used.<br></li>
+ * <li>An Eye of Ender is used.</li>
+ * <li>An Explorer/Treasure Map is activated.</li>
+ * <li>A dolphin swims to a treasure location.</li>
+ * <li>A trade is done with a villager for a map.</li>
2023-04-16 03:13:59 +02:00
+ * <li>{@link World#locateNearestStructure(Location, StructureType, int, boolean)} is invoked.</li>
+ * <li>{@link World#locateNearestStructure(Location, Structure, int, boolean)} is invoked.</li>
2022-03-04 09:09:43 +01:00
+ * </ul>
+ */
2024-09-30 01:48:34 +02:00
+@NullMarked
2022-03-04 09:09:43 +01:00
+public class StructuresLocateEvent extends WorldEvent implements Cancellable {
+
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+
+ private final Location origin;
2024-09-30 01:48:34 +02:00
+ private @Nullable Result result;
2023-02-28 17:36:01 +01:00
+ private List<Structure> structures;
2022-03-04 09:09:43 +01:00
+ private int radius;
+ private boolean findUnexplored;
2024-02-01 10:15:57 +01:00
+
2022-03-04 09:09:43 +01:00
+ private boolean cancelled;
+
2024-02-01 10:15:57 +01:00
+ @ApiStatus.Internal
2024-09-30 01:48:34 +02:00
+ public StructuresLocateEvent(final World world, final Location origin, final List<Structure> structures, final int radius, final boolean findUnexplored) {
2022-03-04 09:09:43 +01:00
+ super(world);
+ this.origin = origin;
2024-06-14 23:07:44 +02:00
+ this.structures = structures;
2022-03-04 09:09:43 +01:00
+ this.radius = radius;
+ this.findUnexplored = findUnexplored;
+ }
+
+ /**
+ * Gets the {@link Location} from which the search is to be conducted.
+ *
+ * @return {@link Location} where search begins
+ */
2024-09-30 01:48:34 +02:00
+ public Location getOrigin() {
2024-03-20 21:42:29 +01:00
+ return this.origin.clone();
2022-03-04 09:09:43 +01:00
+ }
+
+ /**
2023-02-28 17:36:01 +01:00
+ * Gets the {@link Location} and {@link Structure} set as the result, if it was defined.
2022-03-04 09:09:43 +01:00
+ * <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.
+ *
2024-02-01 10:15:57 +01:00
+ * @return The result location and structure, if it has been set. {@code null} if it has not.
+ * @see World#locateNearestStructure(Location, StructureType, int, boolean)
2022-03-04 09:09:43 +01:00
+ */
+ public @Nullable Result getResult() {
+ return this.result;
+ }
+
+ /**
2023-02-28 17:36:01 +01:00
+ * Sets the result {@link Location} and {@link Structure}. This causes the search to be
2022-03-04 09:09:43 +01:00
+ * skipped, and the result object passed here to be used as the result.
+ *
2023-02-28 17:36:01 +01:00
+ * @param result the {@link Location} and {@link Structure} of the search.
2022-03-04 09:09:43 +01:00
+ */
2024-09-30 01:48:34 +02:00
+ public void setResult(final @Nullable Result result) {
2022-03-04 09:09:43 +01:00
+ this.result = result;
+ }
+
+ /**
2023-02-28 17:36:01 +01:00
+ * Gets an unmodifiable list of Structures that are valid targets for the search.
+ *
+ * @return an unmodifiable list of Structures
+ */
2024-09-30 01:48:34 +02:00
+ public @UnmodifiableView List<Structure> getStructures() {
2023-02-28 17:36:01 +01:00
+ return Collections.unmodifiableList(this.structures);
+ }
+
+ /**
+ * Sets the list of Structures that are valid targets for the search.
+ *
+ * @param structures a list of Structures targets
+ */
2024-09-30 01:48:34 +02:00
+ public void setStructures(final List<Structure> structures) {
2023-02-28 17:36:01 +01:00
+ this.structures = structures;
2022-03-04 09:09:43 +01:00
+ }
+
+ /**
+ * Gets the search radius in which to attempt locating the structure.
+ * <p>
+ * This radius may not always be obeyed during the structure search!
+ *
2023-02-28 17:36:01 +01:00
+ * @return the search radius (in chunks)
2022-03-04 09:09:43 +01:00
+ */
+ public int getRadius() {
+ return this.radius;
+ }
+
+ /**
+ * Sets the search radius in which to attempt locating the structure.
+ * <p>
+ * This radius may not always be obeyed during the structure search!
+ *
2023-02-28 17:36:01 +01:00
+ * @param radius the search radius (in chunks)
2022-03-04 09:09:43 +01:00
+ */
2024-09-30 01:48:34 +02:00
+ public void setRadius(final int radius) {
2022-03-04 09:09:43 +01:00
+ this.radius = radius;
+ }
+
+ /**
+ * Gets whether to search exclusively for unexplored structures.
+ * <p>
+ * As with the search radius, this value is not always obeyed.
+ *
+ * @return Whether to search for only unexplored structures.
+ */
+ public boolean shouldFindUnexplored() {
+ return this.findUnexplored;
+ }
+
+ /**
+ * Sets whether to search exclusively for unexplored structures.
+ * <p>
+ * As with the search radius, this value is not always obeyed.
+ *
+ * @param findUnexplored Whether to search for only unexplored structures.
+ */
2024-09-30 01:48:34 +02:00
+ public void setFindUnexplored(final boolean findUnexplored) {
2022-03-04 09:09:43 +01:00
+ this.findUnexplored = findUnexplored;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return this.cancelled;
+ }
+
+ @Override
2024-09-30 01:48:34 +02:00
+ public void setCancelled(final boolean cancel) {
2022-03-04 09:09:43 +01:00
+ this.cancelled = cancel;
+ }
+
+ @Override
2024-09-30 01:48:34 +02:00
+ public HandlerList getHandlers() {
2022-03-04 09:09:43 +01:00
+ return HANDLER_LIST;
+ }
+
2024-09-30 01:48:34 +02:00
+ public static HandlerList getHandlerList() {
2022-03-04 09:09:43 +01:00
+ return HANDLER_LIST;
+ }
+
+ /**
+ * Result for {@link StructuresLocateEvent}.
+ */
2024-09-30 01:48:34 +02:00
+ public record Result(Position pos, Structure structure) {
2023-02-28 17:36:01 +01:00
+
+ @Deprecated(forRemoval = true)
2024-09-30 01:48:34 +02:00
+ public Location position() {
2023-02-28 17:36:01 +01:00
+ //noinspection DataFlowIssue
+ return this.pos.toLocation(null);
+ }
2022-03-04 09:09:43 +01:00
+ }
+}