Skip to content

Commit 15efecd

Browse files
vLuckyyyRollczi
andauthored
GH-1292 feat: Add filter registration option for random teleport locations (#1292)
* feat: Add filter registration option for random teleport locations * CR * CR * CR --------- Co-authored-by: Rollczi <ndejlich5@gmail.com>
1 parent 2ff8112 commit 15efecd

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.eternalcode.core.feature.randomteleport.event;
2+
3+
import org.bukkit.Location;
4+
import org.bukkit.event.Cancellable;
5+
import org.bukkit.event.Event;
6+
import org.bukkit.event.HandlerList;
7+
8+
9+
public class RandomSafeLocationCandidateEvent extends Event implements Cancellable {
10+
11+
private static final HandlerList HANDLER_LIST = new HandlerList();
12+
13+
private final Location candidateLocation;
14+
private boolean isCancelled;
15+
16+
public RandomSafeLocationCandidateEvent(Location candidateLocation) {
17+
super(false);
18+
this.candidateLocation = candidateLocation;
19+
}
20+
21+
public Location getCandidateLocation() {
22+
return this.candidateLocation.clone();
23+
}
24+
25+
@Override
26+
public boolean isCancelled() {
27+
return this.isCancelled;
28+
}
29+
30+
@Override
31+
public void setCancelled(boolean cancel) {
32+
this.isCancelled = cancel;
33+
}
34+
35+
@Override
36+
public HandlerList getHandlers() {
37+
return HANDLER_LIST;
38+
}
39+
40+
public static HandlerList getHandlerList() {
41+
return HANDLER_LIST;
42+
}
43+
44+
}

eternalcore-core/src/main/java/com/eternalcode/core/feature/randomteleport/RandomTeleportSafeLocationService.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.eternalcode.commons.bukkit.position.PositionAdapter;
44
import com.eternalcode.core.configuration.implementation.LocationsConfiguration;
5+
import com.eternalcode.core.event.EventCaller;
6+
import com.eternalcode.core.feature.randomteleport.event.RandomSafeLocationCandidateEvent;
57
import com.eternalcode.core.injector.annotations.Inject;
68
import com.eternalcode.core.injector.annotations.component.Service;
79
import io.papermc.lib.PaperLib;
@@ -25,15 +27,18 @@ class RandomTeleportSafeLocationService {
2527

2628
private final RandomTeleportSettings randomTeleportSettings;
2729
private final LocationsConfiguration locationsConfiguration;
30+
private final EventCaller eventCaller;
2831
private final Random random = new Random();
2932

3033
@Inject
3134
RandomTeleportSafeLocationService(
3235
RandomTeleportSettings randomTeleportSettings,
33-
LocationsConfiguration locationsConfiguration
36+
LocationsConfiguration locationsConfiguration,
37+
EventCaller eventCaller
3438
) {
3539
this.randomTeleportSettings = randomTeleportSettings;
3640
this.locationsConfiguration = locationsConfiguration;
41+
this.eventCaller = eventCaller;
3742
}
3843

3944
public CompletableFuture<Location> getSafeRandomLocation(World world, RandomTeleportRadius radius, int attemptCount) {
@@ -103,9 +108,16 @@ private boolean isSafeLocation(Chunk chunk, Location location) {
103108
return false;
104109
}
105110

106-
return switch (world.getEnvironment()) {
111+
boolean environmentValid = switch (world.getEnvironment()) {
107112
case NORMAL, THE_END, CUSTOM -> true;
108113
case NETHER -> location.getY() <= NETHER_MAX_HEIGHT;
109114
};
115+
116+
if (!environmentValid) {
117+
return false;
118+
}
119+
120+
RandomSafeLocationCandidateEvent event = this.eventCaller.callEvent(new RandomSafeLocationCandidateEvent(location));
121+
return !event.isCancelled();
110122
}
111123
}

0 commit comments

Comments
 (0)