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,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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<String, TeleportRequestType> 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<TeleportRequestType> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,20 +29,23 @@ class SpawnCommand {
private final TeleportService teleportService;
private final NoticeService noticeService;
private final SpawnService spawnService;
private final EventCaller eventCaller;

@Inject
SpawnCommand(
TeleportTaskService teleportTaskService,
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
Expand All @@ -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())
Expand All @@ -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
);

Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading