Add StructureLocateEvent

This commit is contained in:
Bud Gidiere 2020-09-27 14:36:48 -05:00
parent 3348208059
commit 6c04ab54c4
No known key found for this signature in database
GPG Key ID: CD18F99E348902F7
3 changed files with 212 additions and 0 deletions

View File

@ -14,6 +14,8 @@ # Patches
| server | Add GameProfileLookupEvent | tr7zw | |
| api | Add NBT API as a first-class lib | tr7zw | |
| server | Add NBT API as a first-class lib | tr7zw | |
| api | Add StructureLocateEvent | dfsek | |
| server | Add StructureLocateEvent | dfsek | |
| api | Add last tick time API | Ivan Pekov | tr7zw |
| server | Add last tick time API | Ivan Pekov | tr7zw |
| server | Add no-tick block list | William Blake Galbreath | |

View File

@ -0,0 +1,149 @@
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 StructureLocateEvent
diff --git a/src/main/java/org/bukkit/event/world/StructureLocateEvent.java b/src/main/java/org/bukkit/event/world/StructureLocateEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..1011b03fcc7be912e617a6fde3f62e68d70f2c1c
--- /dev/null
+++ b/src/main/java/org/bukkit/event/world/StructureLocateEvent.java
@@ -0,0 +1,137 @@
+package org.bukkit.event.world;
+
+import org.bukkit.Location;
+import org.bukkit.StructureType;
+import org.bukkit.World;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Called <b>before</b> a structure/feature is located.
+ * This happens when:<br>
+ * - The /locate command is used.<br>
+ * - An Eye of Ender is used.<br>
+ * - An Explorer/Treasure Map is activated.<br>
+ * - {@link World#locateNearestStructure(Location, StructureType, int, boolean)} is invoked.
+ */
+public class StructureLocateEvent extends WorldEvent implements Cancellable {
+ private static final HandlerList handlers = new HandlerList();
+ private final Location origin;
+ private Location result = null;
+ private StructureType type;
+ private int radius;
+ private boolean findUnexplored;
+ private boolean cancelled = false;
+ public StructureLocateEvent(@NotNull World world, @NotNull Location origin, @NotNull StructureType structureType, int radius, boolean findUnexplored) {
+ super(world);
+ this.origin = origin;
+ this.type = structureType;
+ this.radius = radius;
+ this.findUnexplored = findUnexplored;
+ }
+
+ @NotNull
+ @Override
+ public HandlerList getHandlers() {
+ return handlers;
+ }
+
+ @NotNull
+ public static HandlerList getHandlerList() {
+ return handlers;
+ }
+
+ /**
+ * Gets the location set as the structure location, if it was defined.<br>
+ * Returns null if it has not been set by {@link StructureLocateEvent#setResult(Location)}.<br>
+ * Since this event fires <i>before</i> the search is done, the actual location is unknown at this point.<br>
+ * If you wish to manipulate the location, use {@link World#locateNearestStructure(Location, StructureType, int, boolean)}
+ * @return The result location, if it has been set. null if it has not.
+ */
+ @Nullable
+ public Location getResult() {
+ return result;
+ }
+
+ /**
+ * Sets the result Location. This causes the search to be skipped, and the location passed here to be used as the result.
+ * @param result the Location of the structure.
+ */
+ public void setResult(@Nullable Location result) {
+ this.result = result;
+ }
+
+ /**
+ * Gets the {@link StructureType} that is to be located.
+ * @return the structure type.
+ */
+ @NotNull
+ public StructureType getType() {
+ return type;
+ }
+
+ /**
+ * Sets the {@link StructureType} that is to be located.
+ * @param type the structure type.
+ */
+ public void setType(@NotNull StructureType type) {
+ this.type = type;
+ }
+
+ /**
+ * Gets the location from which the search is to be conducted.
+ * @return Location where search begins
+ */
+ @NotNull
+ public Location getOrigin() {
+ return origin;
+ }
+
+ /**
+ * Gets the search radius in which to attempt locating the structure.<br>
+ * This radius may not always be obeyed during the structure search!
+ * @return the search radius.
+ */
+ public int getRadius() {
+ return radius;
+ }
+
+ /**
+ * Sets the search radius in which to attempt locating the structure.<br>
+ * This radius may not always be obeyed during the structure search!
+ * @param radius the search radius.
+ */
+ public void setRadius(int radius) {
+ this.radius = radius;
+ }
+
+ /**
+ * Gets whether to search exclusively for unexplored structures.<br>
+ * As with the search radius, this value is not always obeyed.
+ * @return Whether to search for only unexplored structures.
+ */
+ public boolean shouldFindUnexplored() {
+ return findUnexplored;
+ }
+
+ /**
+ * Sets whether to search exclusively for unexplored structures.<br>
+ * As with the search radius, this value is not always obeyed.
+ * @param findUnexplored Whether to search for only unexplored structures.
+ */
+ public void setFindUnexplored(boolean findUnexplored) {
+ this.findUnexplored = findUnexplored;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return cancelled;
+ }
+
+ @Override
+ public void setCancelled(boolean cancel) {
+ this.cancelled = cancel;
+ }
+}

View File

@ -0,0 +1,61 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: dfsek <dfsek@protonmail.com>
Date: Wed, 16 Sep 2020 01:12:29 -0700
Subject: [PATCH] Add StructureLocateEvent
diff --git a/src/main/java/net/minecraft/server/ChunkGenerator.java b/src/main/java/net/minecraft/server/ChunkGenerator.java
index 9a6fef215052f9c513b23024968995c97863a453..16a477f2a99a5c8d68f0d1468d4cd79650a5a05d 100644
--- a/src/main/java/net/minecraft/server/ChunkGenerator.java
+++ b/src/main/java/net/minecraft/server/ChunkGenerator.java
@@ -124,9 +124,27 @@ public abstract class ChunkGenerator {
@Nullable
public BlockPosition findNearestMapFeature(WorldServer worldserver, StructureGenerator<?> structuregenerator, BlockPosition blockposition, int i, boolean flag) {
- if (!this.b.a(structuregenerator)) {
+ // Paper start
+ org.bukkit.World world = worldserver.getWorld();
+ org.bukkit.Location originLocation = new org.bukkit.Location(world, blockposition.getX(), blockposition.getY(), blockposition.getZ());
+ org.bukkit.event.world.StructureLocateEvent event = new org.bukkit.event.world.StructureLocateEvent(world, originLocation, org.bukkit.StructureType.getStructureTypes().get(structuregenerator.i()), i, flag);
+ if(!event.callEvent()) return null;
+ // If event call set a final location, skip structure finding and just return set result.
+ org.bukkit.Location finalLocation = event.getResult();
+ if(finalLocation != null) {
+ return new BlockPosition(finalLocation.getBlockX(), finalLocation.getBlockY(), finalLocation.getBlockZ());
+ }
+ // Get origin location (re)defined by event call.
+ org.bukkit.Location newOriginLocation = event.getOrigin();
+ BlockPosition newOriginPosition = new BlockPosition(newOriginLocation.getBlockX(), newOriginLocation.getBlockY(), newOriginLocation.getBlockZ());
+ // Get radius and whether to find unexplored structures (re)defined by event call.
+ int radius = event.getRadius();
+ boolean findUnexplored = event.shouldFindUnexplored();
+ StructureGenerator<?> newGenerator = StructureGenerator.a.get(event.getType().getName());
+ // Paper end
+ if(! this.b.a(newGenerator)) { // Paper
return null;
- } else if (structuregenerator == StructureGenerator.STRONGHOLD) {
+ } else if (newGenerator == StructureGenerator.STRONGHOLD) { // Paper
this.g();
BlockPosition blockposition1 = null;
double d0 = Double.MAX_VALUE;
@@ -137,7 +155,7 @@ public abstract class ChunkGenerator {
ChunkCoordIntPair chunkcoordintpair = (ChunkCoordIntPair) iterator.next();
blockposition_mutableblockposition.d((chunkcoordintpair.x << 4) + 8, 32, (chunkcoordintpair.z << 4) + 8);
- double d1 = blockposition_mutableblockposition.j(blockposition);
+ double d1 = blockposition_mutableblockposition.j(newOriginPosition); // Paper
if (blockposition1 == null) {
blockposition1 = new BlockPosition(blockposition_mutableblockposition);
@@ -151,9 +169,9 @@ public abstract class ChunkGenerator {
return blockposition1;
} else {
updateStructureSettings(worldserver, structureSettings); // Spigot
- StructureSettingsFeature structuresettingsfeature = this.structureSettings.a(structuregenerator);
+ StructureSettingsFeature structuresettingsfeature = this.structureSettings.a(newGenerator); // Paper
- return structuresettingsfeature == null ? null : structuregenerator.getNearestGeneratedFeature(worldserver, worldserver.getStructureManager(), blockposition, i, flag, worldserver.getSeed(), structuresettingsfeature);
+ return structuresettingsfeature == null ? null : newGenerator.getNearestGeneratedFeature(worldserver, worldserver.getStructureManager(), newOriginPosition, radius, findUnexplored, worldserver.getSeed(), structuresettingsfeature); // Paper
}
}