Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Location> getSafeRandomLocation(World world, RandomTeleportRadius radius, int attemptCount) {
Expand Down Expand Up @@ -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();
}
}