Skip to content

Commit 3983af7

Browse files
authored
Merge pull request #3351 from Multiverse/fix/spawn-location
Fix join location on 1.21.9 due to deprecated PlayerSpawnLocationEvent
2 parents 08d361e + 8853f88 commit 3983af7

File tree

2 files changed

+65
-65
lines changed

2 files changed

+65
-65
lines changed

build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import org.apache.tools.ant.filters.ReplaceTokens
22

33
plugins {
4-
id 'org.mvplugins.multiverse-plugin' version '1.2.2'
4+
id 'org.mvplugins.multiverse-plugin' version '1.2.1'
55
}
66

77
group = 'org.mvplugins.multiverse.core'
@@ -25,12 +25,14 @@ repositories {
2525
}
2626

2727
configure(apiDependencies) {
28-
serverApiVersion = '1.21.8-R0.1-SNAPSHOT'
28+
serverApiVersion = '1.21.9-R0.1-SNAPSHOT'
2929
mockBukkitServerApiVersion = '1.21'
3030
mockBukkitVersion = '4.78.0'
3131
}
3232

3333
dependencies {
34+
testImplementation 'io.papermc.paper:paper-api:1.21.8-R0.1-SNAPSHOT'
35+
3436
// Economy
3537
externalPlugin('com.github.MilkBowl:VaultAPI:1.7.1') {
3638
exclude group: 'org.bukkit', module: 'bukkit'

src/main/java/org/mvplugins/multiverse/core/listeners/MVPlayerListener.java

Lines changed: 61 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.bukkit.entity.Player;
2424
import org.bukkit.event.EventPriority;
2525
import org.bukkit.event.player.PlayerChangedWorldEvent;
26+
import org.bukkit.event.player.PlayerJoinEvent;
2627
import org.bukkit.event.player.PlayerPortalEvent;
2728
import org.bukkit.event.player.PlayerRespawnEvent;
2829
import org.bukkit.event.player.PlayerTeleportEvent;
@@ -32,16 +33,16 @@
3233
import org.mvplugins.multiverse.core.MultiverseCore;
3334
import org.mvplugins.multiverse.core.command.MVCommandManager;
3435
import org.mvplugins.multiverse.core.config.CoreConfig;
36+
import org.mvplugins.multiverse.core.destination.DestinationInstance;
3537
import org.mvplugins.multiverse.core.destination.DestinationsProvider;
36-
import org.mvplugins.multiverse.core.dynamiclistener.EventRunnable;
3738
import org.mvplugins.multiverse.core.dynamiclistener.annotations.DefaultEventPriority;
38-
import org.mvplugins.multiverse.core.dynamiclistener.annotations.EventClass;
3939
import org.mvplugins.multiverse.core.dynamiclistener.annotations.EventMethod;
4040
import org.mvplugins.multiverse.core.dynamiclistener.annotations.EventPriorityKey;
4141
import org.mvplugins.multiverse.core.economy.MVEconomist;
4242
import org.mvplugins.multiverse.core.event.MVRespawnEvent;
4343
import org.mvplugins.multiverse.core.locale.PluginLocales;
4444
import org.mvplugins.multiverse.core.permissions.CorePermissionsChecker;
45+
import org.mvplugins.multiverse.core.teleportation.AsyncSafetyTeleporter;
4546
import org.mvplugins.multiverse.core.teleportation.BlockSafety;
4647
import org.mvplugins.multiverse.core.teleportation.TeleportQueue;
4748
import org.mvplugins.multiverse.core.utils.result.ResultChain;
@@ -52,7 +53,6 @@
5253
import org.mvplugins.multiverse.core.world.entrycheck.WorldEntryCheckerProvider;
5354
import org.mvplugins.multiverse.core.world.helpers.DimensionFinder;
5455
import org.mvplugins.multiverse.core.world.helpers.EnforcementHandler;
55-
import org.spigotmc.event.player.PlayerSpawnLocationEvent;
5656

5757
/**
5858
* Multiverse's Listener for players.
@@ -72,6 +72,7 @@ final class MVPlayerListener implements CoreListener {
7272
private final EnforcementHandler enforcementHandler;
7373
private final DimensionFinder dimensionFinder;
7474
private final CorePermissionsChecker corePermissionsChecker;
75+
private final AsyncSafetyTeleporter asyncSafetyTeleporter;
7576

7677
private final Map<String, String> playerWorld = new ConcurrentHashMap<>();
7778

@@ -89,7 +90,8 @@ final class MVPlayerListener implements CoreListener {
8990
DestinationsProvider destinationsProvider,
9091
EnforcementHandler enforcementHandler,
9192
DimensionFinder dimensionFinder,
92-
CorePermissionsChecker corePermissionsChecker) {
93+
CorePermissionsChecker corePermissionsChecker,
94+
AsyncSafetyTeleporter asyncSafetyTeleporter) {
9395
this.plugin = plugin;
9496
this.config = config;
9597
this.worldManagerProvider = worldManagerProvider;
@@ -103,6 +105,7 @@ final class MVPlayerListener implements CoreListener {
103105
this.enforcementHandler = enforcementHandler;
104106
this.dimensionFinder = dimensionFinder;
105107
this.corePermissionsChecker = corePermissionsChecker;
108+
this.asyncSafetyTeleporter = asyncSafetyTeleporter;
106109
}
107110

108111
private WorldManager getWorldManager() {
@@ -201,68 +204,63 @@ private Option<Location> getMostAccurateRespawnLocation(LoadedMultiverseWorld mv
201204
return Option.of(mvWorld.getSpawnLocation());
202205
}
203206

204-
@EventClass("org.spigotmc.event.player.PlayerSpawnLocationEvent")
207+
@EventMethod
208+
// TODO: Consider if this key is needed anymore, need to remove from config.yml as well
205209
@EventPriorityKey("mvcore-player-spawn-location")
206-
EventRunnable playerSpawnLocation() {
207-
return new EventRunnable<PlayerSpawnLocationEvent>() {
208-
@Override
209-
public void onEvent(PlayerSpawnLocationEvent event) {
210-
Player player = event.getPlayer();
211-
MultiverseWorld world = getWorldManager().getLoadedWorld(player.getWorld()).getOrNull();
212-
if (world == null) {
213-
Logging.finer("Player joined in a world that is not managed by Multiverse.");
214-
return;
215-
}
216-
if (!player.hasPlayedBefore()) {
217-
handleFirstSpawn(event);
218-
} else {
219-
handleJoinLocation(event);
220-
}
221-
handleGameModeAndFlight(player, event.getSpawnLocation().getWorld());
222-
}
210+
void onPlayerJoin(PlayerJoinEvent event) {
211+
Player player = event.getPlayer();
212+
if (player.hasPlayedBefore()) {
213+
handleJoinLocation(player);
214+
} else {
215+
handleFirstSpawn(player);
216+
}
217+
handleGameModeAndFlight(player, player.getWorld());
218+
}
223219

224-
private void handleFirstSpawn(PlayerSpawnLocationEvent event) {
225-
if (!config.getFirstSpawnOverride()) {
226-
Logging.finer("FirstSpawnOverride is disabled");
227-
// User has disabled the feature in config
228-
return;
229-
}
230-
Logging.fine("Moving NEW player to(firstspawnoverride): %s", config.getFirstSpawnLocation());
231-
destinationsProvider.parseDestination(config.getFirstSpawnLocation())
232-
.map(destination -> destination.getLocation(event.getPlayer())
233-
.peek(event::setSpawnLocation)
234-
.onEmpty(() -> Logging.warning("The destination in FirstSpawnLocation in config is invalid")))
235-
.onFailure(failure -> {
236-
Logging.warning("Invalid destination in FirstSpawnLocation in config: %s");
237-
Logging.warning(failure.getFailureMessage().formatted(getLocales()));
238-
});
239-
}
220+
private void handleFirstSpawn(Player player) {
221+
if (!config.getFirstSpawnOverride()) {
222+
Logging.finer("FirstSpawnOverride is disabled");
223+
// User has disabled the feature in config
224+
return;
225+
}
226+
Logging.fine("Moving NEW player to(firstspawnoverride): %s", config.getFirstSpawnLocation());
227+
destinationsProvider.parseDestination(config.getFirstSpawnLocation())
228+
.peek(destination -> teleportToDestinationOnJoin(player, destination))
229+
.onFailure(failure -> {
230+
Logging.warning("Invalid destination in FirstSpawnLocation in config: %s");
231+
Logging.warning(failure.getFailureMessage().formatted(getLocales()));
232+
});
233+
}
240234

241-
private void handleJoinLocation(PlayerSpawnLocationEvent event) {
242-
if (!config.getEnableJoinDestination()) {
243-
Logging.finer("JoinDestination is disabled");
244-
// User has disabled the feature in config
245-
return;
246-
}
247-
if (config.getJoinDestination().isBlank()) {
248-
Logging.warning("Joindestination is enabled but no destination has been specified in config!");
249-
return;
250-
}
251-
if (corePermissionsChecker.hasJoinLocationBypassPermission(event.getPlayer())) {
252-
Logging.finer("Player %s has bypass permission for JoinDestination", event.getPlayer().getName());
253-
return;
254-
}
255-
Logging.finer("JoinDestination is " + config.getJoinDestination());
256-
destinationsProvider.parseDestination(config.getJoinDestination())
257-
.map(destination -> destination.getLocation(event.getPlayer())
258-
.peek(event::setSpawnLocation)
259-
.onEmpty(() -> Logging.warning("The destination in JoinDestination in config is invalid")))
260-
.onFailure(failure -> {
261-
Logging.warning("Invalid destination in JoinDestination in config: %s");
262-
Logging.warning(failure.getFailureMessage().formatted(getLocales()));
263-
});
264-
}
265-
};
235+
private void handleJoinLocation(Player player) {
236+
if (!config.getEnableJoinDestination()) {
237+
Logging.finer("JoinDestination is disabled");
238+
// User has disabled the feature in config
239+
return;
240+
}
241+
if (config.getJoinDestination().isBlank()) {
242+
Logging.warning("Joindestination is enabled but no destination has been specified in config!");
243+
return;
244+
}
245+
if (corePermissionsChecker.hasJoinLocationBypassPermission(player)) {
246+
Logging.finer("Player %s has bypass permission for JoinDestination", player.getName());
247+
return;
248+
}
249+
Logging.finer("JoinDestination is " + config.getJoinDestination());
250+
destinationsProvider.parseDestination(config.getJoinDestination())
251+
.peek(destination -> teleportToDestinationOnJoin(player, destination))
252+
.onFailure(failure -> {
253+
Logging.warning("Invalid destination in JoinDestination in config: %s");
254+
Logging.warning(failure.getFailureMessage().formatted(getLocales()));
255+
});
256+
}
257+
258+
private void teleportToDestinationOnJoin(Player player, DestinationInstance<?, ?> destination) {
259+
asyncSafetyTeleporter.to(destination).teleportSingle(player)
260+
.onSuccess(result -> Logging.fine("Player %s has been teleported on join",
261+
player.getName()))
262+
.onFailure(failure -> Logging.warning("Failed to teleport player %s on join: %s",
263+
player.getName(), failure.getFirst()));
266264
}
267265

268266
/**

0 commit comments

Comments
 (0)