Implement MVRespawnEvent as Cancellable and remove respawn method

This commit is contained in:
Ben Woo 2025-01-19 14:43:34 +08:00
parent bb91a74308
commit 36aa144121
2 changed files with 36 additions and 38 deletions

View File

@ -9,25 +9,22 @@ package org.mvplugins.multiverse.core.api.event;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
/**
* Called when a player is respawning.
*
* @since 5.0
*/
// todo: remove or update its usage. The respawnMethod is always "compatibility" for some reason
@Deprecated
public class MVRespawnEvent extends Event {
private final Player player;
private final String respawnMethod;
public class MVRespawnEvent extends PlayerEvent implements Cancellable {
private Location location;
private boolean cancelled = false;
public MVRespawnEvent(Location spawningAt, Player p, String respawnMethod) {
this.player = p;
public MVRespawnEvent(Location spawningAt, Player player) {
super(player);
this.location = spawningAt;
this.respawnMethod = respawnMethod;
}
private static final HandlerList HANDLERS = new HandlerList();
@ -49,43 +46,39 @@ public class MVRespawnEvent extends Event {
return HANDLERS;
}
/**
* Gets the {@link Player} that's respawning.
*
* @return The {@link Player} that's respawning.
* @since 5.0
*/
public Player getPlayer() {
return this.player;
}
/**
* Gets the respawn-method.
*
* @return The respawn-method.
* @since 5.0
*/
public String getRespawnMethod() {
return this.respawnMethod;
}
/**
* Gets the player's respawn-{@link Location}.
*
* @return The player's respawn-{@link Location}.
* @since 5.0
*/
public Location getPlayersRespawnLocation() {
public Location getRespawnLocation() {
return this.location;
}
/**
* Sets the player's respawn-{@link Location}.
*
* @param l The new respawn-{@link Location}.
* @param location The new respawn-{@link Location}.
* @since 5.0
*/
public void setRespawnLocation(Location l) {
this.location = l;
public void setRespawnLocation(Location location) {
this.location = location;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isCancelled() {
return cancelled;
}
/**
* {@inheritDoc}
*/
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -141,9 +141,14 @@ public class MVPlayerListener implements CoreListener {
})
.flatMap(mvWorld -> getMostAccurateRespawnLocation(player, mvWorld, event.getRespawnLocation()))
.peek(newRespawnLocation -> {
MVRespawnEvent respawnEvent = new MVRespawnEvent(newRespawnLocation, event.getPlayer(), "compatability");
MVRespawnEvent respawnEvent = new MVRespawnEvent(newRespawnLocation, event.getPlayer());
this.server.getPluginManager().callEvent(respawnEvent);
event.setRespawnLocation(respawnEvent.getPlayersRespawnLocation());
if (respawnEvent.isCancelled()) {
Logging.fine("Player '%s' cancelled their respawn event.", player.getName());
return;
}
Logging.fine("Overriding respawn location for player '%s' to '%s'.", player.getName(), respawnEvent.getRespawnLocation());
event.setRespawnLocation(respawnEvent.getRespawnLocation());
});
}
@ -153,15 +158,15 @@ public class MVPlayerListener implements CoreListener {
: server.getWorld(mvWorld.getRespawnWorldName()))
.onEmpty(() -> Logging.warning("World '%s' has respawn-world property of '%s' that does not exist!",
player.getWorld().getName(), mvWorld.getRespawnWorldName()))
.map(newRespawnWorld -> {
.flatMap(newRespawnWorld -> {
if (!config.getEnforceRespawnAtWorldSpawn() && newRespawnWorld.equals(defaultRespawnLocation.getWorld())) {
Logging.fine("Respawn location is within same world as respawn-world, not overriding.");
return defaultRespawnLocation;
return Option.none();
}
return getWorldManager()
.getLoadedWorld(newRespawnWorld)
.map(newMVRespawnWorld -> (Location) newMVRespawnWorld.getSpawnLocation())
.getOrElse(newRespawnWorld::getSpawnLocation);
.orElse(() -> Option.of(newRespawnWorld.getSpawnLocation()));
});
}