diff --git a/eternalcore-api/src/main/java/com/eternalcode/core/feature/randomteleport/event/RandomSafeLocationCandidateEvent.java b/eternalcore-api/src/main/java/com/eternalcode/core/feature/randomteleport/event/RandomSafeLocationCandidateEvent.java new file mode 100644 index 000000000..b9c459062 --- /dev/null +++ b/eternalcore-api/src/main/java/com/eternalcode/core/feature/randomteleport/event/RandomSafeLocationCandidateEvent.java @@ -0,0 +1,44 @@ +package com.eternalcode.core.feature.randomteleport.event; + +import org.bukkit.Location; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + + +public class RandomSafeLocationCandidateEvent extends Event implements Cancellable { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + + private final Location candidateLocation; + private boolean isCancelled; + + public RandomSafeLocationCandidateEvent(Location candidateLocation) { + super(false); + this.candidateLocation = candidateLocation; + } + + public Location getCandidateLocation() { + return this.candidateLocation.clone(); + } + + @Override + public boolean isCancelled() { + return this.isCancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.isCancelled = cancel; + } + + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } + + public static HandlerList getHandlerList() { + return HANDLER_LIST; + } + +} diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/randomteleport/RandomTeleportSafeLocationService.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/randomteleport/RandomTeleportSafeLocationService.java index bbb3be754..4f21e5b1b 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/randomteleport/RandomTeleportSafeLocationService.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/randomteleport/RandomTeleportSafeLocationService.java @@ -2,6 +2,8 @@ import com.eternalcode.commons.bukkit.position.PositionAdapter; import com.eternalcode.core.configuration.implementation.LocationsConfiguration; +import com.eternalcode.core.event.EventCaller; +import com.eternalcode.core.feature.randomteleport.event.RandomSafeLocationCandidateEvent; import com.eternalcode.core.injector.annotations.Inject; import com.eternalcode.core.injector.annotations.component.Service; import io.papermc.lib.PaperLib; @@ -25,15 +27,18 @@ class RandomTeleportSafeLocationService { private final RandomTeleportSettings randomTeleportSettings; private final LocationsConfiguration locationsConfiguration; + private final EventCaller eventCaller; private final Random random = new Random(); @Inject RandomTeleportSafeLocationService( RandomTeleportSettings randomTeleportSettings, - LocationsConfiguration locationsConfiguration + LocationsConfiguration locationsConfiguration, + EventCaller eventCaller ) { this.randomTeleportSettings = randomTeleportSettings; this.locationsConfiguration = locationsConfiguration; + this.eventCaller = eventCaller; } public CompletableFuture getSafeRandomLocation(World world, RandomTeleportRadius radius, int attemptCount) { @@ -103,9 +108,16 @@ private boolean isSafeLocation(Chunk chunk, Location location) { return false; } - return switch (world.getEnvironment()) { + boolean environmentValid = switch (world.getEnvironment()) { case NORMAL, THE_END, CUSTOM -> true; case NETHER -> location.getY() <= NETHER_MAX_HEIGHT; }; + + if (!environmentValid) { + return false; + } + + RandomSafeLocationCandidateEvent event = this.eventCaller.callEvent(new RandomSafeLocationCandidateEvent(location)); + return !event.isCancelled(); } }