2323import org .bukkit .entity .Player ;
2424import org .bukkit .event .EventPriority ;
2525import org .bukkit .event .player .PlayerChangedWorldEvent ;
26+ import org .bukkit .event .player .PlayerJoinEvent ;
2627import org .bukkit .event .player .PlayerPortalEvent ;
2728import org .bukkit .event .player .PlayerRespawnEvent ;
2829import org .bukkit .event .player .PlayerTeleportEvent ;
3233import org .mvplugins .multiverse .core .MultiverseCore ;
3334import org .mvplugins .multiverse .core .command .MVCommandManager ;
3435import org .mvplugins .multiverse .core .config .CoreConfig ;
36+ import org .mvplugins .multiverse .core .destination .DestinationInstance ;
3537import org .mvplugins .multiverse .core .destination .DestinationsProvider ;
36- import org .mvplugins .multiverse .core .dynamiclistener .EventRunnable ;
3738import org .mvplugins .multiverse .core .dynamiclistener .annotations .DefaultEventPriority ;
38- import org .mvplugins .multiverse .core .dynamiclistener .annotations .EventClass ;
3939import org .mvplugins .multiverse .core .dynamiclistener .annotations .EventMethod ;
4040import org .mvplugins .multiverse .core .dynamiclistener .annotations .EventPriorityKey ;
4141import org .mvplugins .multiverse .core .economy .MVEconomist ;
4242import org .mvplugins .multiverse .core .event .MVRespawnEvent ;
4343import org .mvplugins .multiverse .core .locale .PluginLocales ;
4444import org .mvplugins .multiverse .core .permissions .CorePermissionsChecker ;
45+ import org .mvplugins .multiverse .core .teleportation .AsyncSafetyTeleporter ;
4546import org .mvplugins .multiverse .core .teleportation .BlockSafety ;
4647import org .mvplugins .multiverse .core .teleportation .TeleportQueue ;
4748import org .mvplugins .multiverse .core .utils .result .ResultChain ;
5253import org .mvplugins .multiverse .core .world .entrycheck .WorldEntryCheckerProvider ;
5354import org .mvplugins .multiverse .core .world .helpers .DimensionFinder ;
5455import 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