diff --git a/eternalcore-api/src/main/java/com/eternalcode/core/feature/spawn/event/PreSpawnTeleportEvent.java b/eternalcore-api/src/main/java/com/eternalcode/core/feature/spawn/event/PreSpawnTeleportEvent.java new file mode 100644 index 000000000..20d070dbe --- /dev/null +++ b/eternalcore-api/src/main/java/com/eternalcode/core/feature/spawn/event/PreSpawnTeleportEvent.java @@ -0,0 +1,55 @@ +package com.eternalcode.core.feature.spawn.event; + +import com.google.common.base.Preconditions; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; + +/** + * Called before a spawn command or SpawnService teleport starts its delay or teleportation. + * Cancelling this event prevents both the delay and the teleport. + */ +public class PreSpawnTeleportEvent extends PlayerEvent implements Cancellable { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + + private Location destination; + private boolean cancelled; + + public PreSpawnTeleportEvent(Player player, Location destination) { + super(player); + + this.player = player; + this.destination = destination; + } + + public Location getDestination() { + return this.destination; + } + + public void setDestination(Location destination) { + Preconditions.checkNotNull(destination, "Destination cannot be null"); + this.destination = destination; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } + + public static HandlerList getHandlerList() { + return HANDLER_LIST; + } +} diff --git a/eternalcore-api/src/main/java/com/eternalcode/core/feature/teleportrequest/TeleportRequestAcceptEvent.java b/eternalcore-api/src/main/java/com/eternalcode/core/feature/teleportrequest/TeleportRequestAcceptEvent.java new file mode 100644 index 000000000..ef6ca8dfb --- /dev/null +++ b/eternalcore-api/src/main/java/com/eternalcode/core/feature/teleportrequest/TeleportRequestAcceptEvent.java @@ -0,0 +1,92 @@ +package com.eternalcode.core.feature.teleportrequest; + +import com.google.common.base.Preconditions; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; + +/** + * Called before accepting a teleport request, before the teleport delay starts. + * Cancelling this event preserves the request unless {@link #removeRequest()} is explicitly called. + */ +public class TeleportRequestAcceptEvent extends PlayerEvent implements Cancellable { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + + private final Player requester; + private final Player recipient; + private final TeleportRequestType requestType; + private final Runnable requestRemoval; + private Location destination; + private boolean cancelled; + + public TeleportRequestAcceptEvent( + Player requester, + Player recipient, + TeleportRequestType requestType, + Location destination, + Runnable requestRemoval + ) { + super(requester); + + this.requester = requester; + this.recipient = recipient; + this.requestType = requestType; + this.destination = destination; + this.requestRemoval = requestRemoval; + } + + public Player getRequester() { + return this.requester; + } + + public Player getRecipient() { + return this.recipient; + } + + public Player getTeleportingPlayer() { + return this.requestType == TeleportRequestType.TELEPORT_TO_PLAYER ? this.requester : this.recipient; + } + + public TeleportRequestType getRequestType() { + return this.requestType; + } + + /** + * Explicitly removes this request, even when the event is canceled. + * Call this method synchronously from the event handler when replacing the native teleport. + */ + public void removeRequest() { + this.requestRemoval.run(); + } + + public Location getDestination() { + return this.destination; + } + + public void setDestination(Location destination) { + Preconditions.checkNotNull(destination, "Destination cannot be null"); + this.destination = destination; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } + + public static HandlerList getHandlerList() { + return HANDLER_LIST; + } +} diff --git a/eternalcore-api/src/main/java/com/eternalcode/core/feature/teleportrequest/TeleportRequestType.java b/eternalcore-api/src/main/java/com/eternalcode/core/feature/teleportrequest/TeleportRequestType.java new file mode 100644 index 000000000..5393f46dc --- /dev/null +++ b/eternalcore-api/src/main/java/com/eternalcode/core/feature/teleportrequest/TeleportRequestType.java @@ -0,0 +1,53 @@ +package com.eternalcode.core.feature.teleportrequest; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import org.jetbrains.annotations.ApiStatus.Experimental; + +// In next versions, this class can change their possible values, so it is recommended to use the constants provided by this class instead of creating new instances. +@Experimental +public record TeleportRequestType(String name) { + + private static final Map REGISTRY = new LinkedHashMap<>(); + + public static final TeleportRequestType TELEPORT_TO_PLAYER = register("tpa"); + public static final TeleportRequestType TELEPORT_HERE_PLAYER = register("tpahere"); + + private static TeleportRequestType register(String name) { + TeleportRequestType type = new TeleportRequestType(name); + + if (REGISTRY.putIfAbsent(normalize(name), type) != null) { + throw new IllegalStateException("Teleport request type '" + name + "' is already registered"); + } + + return type; + } + + public static List values() { + return List.copyOf(REGISTRY.values()); + } + + public static TeleportRequestType find(String name) { + if (name == null) { + return null; + } + + TeleportRequestType type = REGISTRY.get(normalize(name)); + + if (type == null) { + throw new IllegalArgumentException("Teleport request type '" + name + "' is not registered"); + } + + return type; + } + + public static TeleportRequestType valueOf(String name) { + return find(name); + } + + private static String normalize(String name) { + return name.toLowerCase(Locale.ROOT); + } +} diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/spawn/SpawnCommand.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/spawn/SpawnCommand.java index d09adce33..59180e71d 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/spawn/SpawnCommand.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/spawn/SpawnCommand.java @@ -3,6 +3,8 @@ import com.eternalcode.annotations.scan.command.DescriptionDocs; import com.eternalcode.annotations.scan.permission.PermissionDocs; import com.eternalcode.commons.bukkit.position.PositionAdapter; +import com.eternalcode.core.event.EventCaller; +import com.eternalcode.core.feature.spawn.event.PreSpawnTeleportEvent; import com.eternalcode.core.feature.teleport.TeleportService; import com.eternalcode.core.teleport.TeleportTaskService; import com.eternalcode.core.injector.annotations.Inject; @@ -27,6 +29,7 @@ class SpawnCommand { private final TeleportService teleportService; private final NoticeService noticeService; private final SpawnService spawnService; + private final EventCaller eventCaller; @Inject SpawnCommand( @@ -34,13 +37,15 @@ class SpawnCommand { SpawnJoinSettings spawnJoinSettings, TeleportService teleportService, NoticeService noticeService, - SpawnService spawnService + SpawnService spawnService, + EventCaller eventCaller ) { this.teleportTaskService = teleportTaskService; this.spawnJoinSettings = spawnJoinSettings; this.teleportService = teleportService; this.noticeService = noticeService; this.spawnService = spawnService; + this.eventCaller = eventCaller; } @Execute @@ -63,7 +68,12 @@ void executeSelf(@Sender Player sender) { } if (sender.hasPermission(SPAWN_TELEPORT_BYPASS)) { - this.teleportService.teleport(sender, spawnLocation); + PreSpawnTeleportEvent event = this.eventCaller.callEvent(new PreSpawnTeleportEvent(sender, spawnLocation)); + if (event.isCancelled()) { + return; + } + + this.teleportService.teleport(sender, event.getDestination()); this.noticeService.create() .notice(translation -> translation.teleport().teleported()) @@ -81,12 +91,17 @@ void executeSelf(@Sender Player sender) { return; } + PreSpawnTeleportEvent event = this.eventCaller.callEvent(new PreSpawnTeleportEvent(sender, spawnLocation)); + if (event.isCancelled()) { + return; + } + Duration teleportDelay = this.spawnJoinSettings.spawnTeleportTime(); this.teleportTaskService.createTeleport( sender.getUniqueId(), PositionAdapter.convert(sender.getLocation()), - PositionAdapter.convert(spawnLocation), + PositionAdapter.convert(event.getDestination()), teleportDelay ); @@ -110,7 +125,12 @@ void execute(@Sender Viewer sender, @Arg Player player) { return; } - this.teleportService.teleport(player, spawnLocation); + PreSpawnTeleportEvent event = this.eventCaller.callEvent(new PreSpawnTeleportEvent(player, spawnLocation)); + if (event.isCancelled()) { + return; + } + + this.teleportService.teleport(player, event.getDestination()); this.noticeService.create() .notice(translation -> translation.spawn().spawnTeleportedBy()) diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/spawn/SpawnServiceImpl.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/spawn/SpawnServiceImpl.java index e6b48b95d..49276921f 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/spawn/SpawnServiceImpl.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/spawn/SpawnServiceImpl.java @@ -2,6 +2,8 @@ import com.eternalcode.core.configuration.ConfigurationManager; import com.eternalcode.core.configuration.implementation.LocationsConfiguration; +import com.eternalcode.core.event.EventCaller; +import com.eternalcode.core.feature.spawn.event.PreSpawnTeleportEvent; import com.eternalcode.core.injector.annotations.Inject; import com.eternalcode.core.injector.annotations.component.Service; import com.eternalcode.commons.bukkit.position.Position; @@ -18,12 +20,14 @@ public class SpawnServiceImpl implements SpawnService { private final LocationsConfiguration locationsConfiguration; private final ConfigurationManager configurationManager; private final Logger logger; + private final EventCaller eventCaller; @Inject - public SpawnServiceImpl(LocationsConfiguration locationsConfiguration, ConfigurationManager configurationManager, Logger logger) { + public SpawnServiceImpl(LocationsConfiguration locationsConfiguration, ConfigurationManager configurationManager, Logger logger, EventCaller eventCaller) { this.locationsConfiguration = locationsConfiguration; this.configurationManager = configurationManager; this.logger = logger; + this.eventCaller = eventCaller; } @Override @@ -34,7 +38,12 @@ public void teleportToSpawn(Player player) { } Location spawnLocation = PositionAdapter.convert(this.locationsConfiguration.spawn); - player.teleportAsync(spawnLocation); + PreSpawnTeleportEvent event = this.eventCaller.callEvent(new PreSpawnTeleportEvent(player, spawnLocation)); + if (event.isCancelled()) { + return; + } + + player.teleportAsync(event.getDestination()); } @Override diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrequest/TeleportRequestService.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrequest/TeleportRequestService.java index e20ab4cc4..b2de04cf1 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrequest/TeleportRequestService.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrequest/TeleportRequestService.java @@ -30,6 +30,10 @@ void removeRequest(UUID requester) { this.requests.asMap().remove(requester); } + void removeRequest(UUID requester, UUID target) { + this.requests.asMap().remove(requester, target); + } + boolean hasRequest(UUID requester, UUID target) { UUID foundTarget = this.requests.getIfPresent(requester); return foundTarget != null && foundTarget.equals(target); diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrequest/TpaAcceptCommand.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrequest/TpaAcceptCommand.java index 51d172168..a67891dfa 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrequest/TpaAcceptCommand.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrequest/TpaAcceptCommand.java @@ -3,6 +3,7 @@ import com.eternalcode.annotations.scan.command.DescriptionDocs; import com.eternalcode.commons.bukkit.position.PositionAdapter; import com.eternalcode.core.teleport.TeleportTaskService; +import com.eternalcode.core.event.EventCaller; import com.eternalcode.core.injector.annotations.Inject; import com.eternalcode.core.notice.NoticeService; import dev.rollczi.litecommands.annotations.argument.Arg; @@ -24,14 +25,16 @@ class TpaAcceptCommand { private final NoticeService noticeService; private final TeleportRequestSettings settings; private final Server server; + private final EventCaller eventCaller; @Inject - TpaAcceptCommand(TeleportRequestService requestService, TeleportTaskService teleportTaskService, NoticeService noticeService, TeleportRequestSettings settings, Server server) { + TpaAcceptCommand(TeleportRequestService requestService, TeleportTaskService teleportTaskService, NoticeService noticeService, TeleportRequestSettings settings, Server server, EventCaller eventCaller) { this.requestService = requestService; this.teleportTaskService = teleportTaskService; this.noticeService = noticeService; this.settings = settings; this.server = server; + this.eventCaller = eventCaller; } @Execute @@ -43,10 +46,15 @@ void executeTarget(@Sender Player player, @Arg(RequesterArgument.KEY) Player tar return; } + TeleportRequestAcceptEvent event = this.callAcceptEvent(target, player); + if (event.isCancelled()) { + return; + } + this.teleportTaskService.createTeleport( target.getUniqueId(), PositionAdapter.convert(target.getLocation()), - PositionAdapter.convert(player.getLocation()), + PositionAdapter.convert(event.getDestination()), this.settings.tpaTimer() ); @@ -84,17 +92,22 @@ void executeAll(@Sender Player player) { return; } + boolean acceptedAny = false; for (UUID uniqueId : requests) { Player requester = this.server.getPlayer(uniqueId); - this.requestService.removeRequest(uniqueId); - if (requester != null) { + TeleportRequestAcceptEvent event = this.callAcceptEvent(requester, player); + if (event.isCancelled()) { + continue; + } + + this.requestService.removeRequest(uniqueId); this.teleportTaskService.createTeleport( requester.getUniqueId(), PositionAdapter.convert(requester.getLocation()), - PositionAdapter.convert(player.getLocation()), + PositionAdapter.convert(event.getDestination()), this.settings.tpaTimer() ); @@ -105,9 +118,25 @@ void executeAll(@Sender Player player) { .placeholder("{PLAYER}", player.getName()) .send(); } + else { + this.requestService.removeRequest(uniqueId); + } + acceptedAny = true; + } + + if (acceptedAny) { + this.noticeService.player(player.getUniqueId(), translation -> translation.tpa().tpaAcceptAllAccepted()); } + } - this.noticeService.player(player.getUniqueId(), translation -> translation.tpa().tpaAcceptAllAccepted()); + private TeleportRequestAcceptEvent callAcceptEvent(Player requester, Player recipient) { + return this.eventCaller.callEvent(new TeleportRequestAcceptEvent( + requester, + recipient, + TeleportRequestType.TELEPORT_TO_PLAYER, + recipient.getLocation(), + () -> this.requestService.removeRequest(requester.getUniqueId(), recipient.getUniqueId()) + )); } private boolean canAcceptTeleportRequest(Player player) { diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrequest/self/TeleportHereRequestService.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrequest/self/TeleportHereRequestService.java index ffcdcbfbc..8d9ed00e7 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrequest/self/TeleportHereRequestService.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrequest/self/TeleportHereRequestService.java @@ -31,6 +31,10 @@ void removeRequest(UUID requester) { this.requests.asMap().remove(requester); } + void removeRequest(UUID requester, UUID target) { + this.requests.asMap().remove(requester, target); + } + boolean hasRequest(UUID requester, UUID target) { Map map = this.requests.asMap(); diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrequest/self/TpaHereActionCommand.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrequest/self/TpaHereActionCommand.java index 86056a0bb..ad0e9c0ba 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrequest/self/TpaHereActionCommand.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/teleportrequest/self/TpaHereActionCommand.java @@ -3,7 +3,10 @@ import com.eternalcode.annotations.scan.command.DescriptionDocs; import com.eternalcode.commons.bukkit.position.PositionAdapter; import com.eternalcode.core.teleport.TeleportTaskService; +import com.eternalcode.core.event.EventCaller; +import com.eternalcode.core.feature.teleportrequest.TeleportRequestAcceptEvent; import com.eternalcode.core.feature.teleportrequest.TeleportRequestSettings; +import com.eternalcode.core.feature.teleportrequest.TeleportRequestType; import com.eternalcode.core.injector.annotations.Inject; import com.eternalcode.core.notice.NoticeService; import dev.rollczi.litecommands.annotations.argument.Arg; @@ -24,23 +27,36 @@ class TpaHereActionCommand { private final TeleportRequestSettings settings; private final NoticeService noticeService; private final Server server; + private final EventCaller eventCaller; @Inject - TpaHereActionCommand(TeleportHereRequestService requestService, TeleportTaskService teleportTaskService, TeleportRequestSettings settings, NoticeService noticeService, Server server) { + TpaHereActionCommand(TeleportHereRequestService requestService, TeleportTaskService teleportTaskService, TeleportRequestSettings settings, NoticeService noticeService, Server server, EventCaller eventCaller) { this.requestService = requestService; this.teleportTaskService = teleportTaskService; this.settings = settings; this.noticeService = noticeService; this.server = server; + this.eventCaller = eventCaller; } @Execute(name = "tpahereaccept") @Permission("eternalcore.tpaccept") void accept(@Sender Player player, @Arg(SelfRequesterArgument.KEY) Player target) { + TeleportRequestAcceptEvent event = this.eventCaller.callEvent(new TeleportRequestAcceptEvent( + target, + player, + TeleportRequestType.TELEPORT_HERE_PLAYER, + target.getLocation(), + () -> this.requestService.removeRequest(target.getUniqueId(), player.getUniqueId()) + )); + if (event.isCancelled()) { + return; + } + this.teleportTaskService.createTeleport( player.getUniqueId(), PositionAdapter.convert(player.getLocation()), - PositionAdapter.convert(target.getLocation()), + PositionAdapter.convert(event.getDestination()), this.settings.tpaTimer() );