Skip to content

Commit 0a33d35

Browse files
authored
Merge pull request #3288 from Multiverse/fix/bukkit-compatility
Fix PlayerSpawnLocationEvent may not be available on bukkit
2 parents 4d92964 + b4fedea commit 0a33d35

File tree

1 file changed

+57
-50
lines changed

1 file changed

+57
-50
lines changed

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

Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
import org.mvplugins.multiverse.core.command.MVCommandManager;
3434
import org.mvplugins.multiverse.core.config.CoreConfig;
3535
import org.mvplugins.multiverse.core.destination.DestinationsProvider;
36+
import org.mvplugins.multiverse.core.dynamiclistener.EventRunnable;
3637
import org.mvplugins.multiverse.core.dynamiclistener.annotations.DefaultEventPriority;
38+
import org.mvplugins.multiverse.core.dynamiclistener.annotations.EventClass;
3739
import org.mvplugins.multiverse.core.dynamiclistener.annotations.EventMethod;
3840
import org.mvplugins.multiverse.core.dynamiclistener.annotations.EventPriorityKey;
3941
import org.mvplugins.multiverse.core.economy.MVEconomist;
@@ -195,59 +197,64 @@ private Option<Location> getMostAccurateRespawnLocation(LoadedMultiverseWorld mv
195197
return Option.of(mvWorld.getSpawnLocation());
196198
}
197199

198-
@EventMethod
200+
@EventClass("org.spigotmc.event.player.PlayerSpawnLocationEvent")
199201
@EventPriorityKey("mvcore-player-spawn-location")
200-
void playerSpawnLocation(PlayerSpawnLocationEvent event) {
201-
Player player = event.getPlayer();
202-
MultiverseWorld world = getWorldManager().getLoadedWorld(player.getWorld()).getOrNull();
203-
if (world == null) {
204-
Logging.finer("Player joined in a world that is not managed by Multiverse.");
205-
return;
206-
}
207-
if (!player.hasPlayedBefore()) {
208-
handleFirstSpawn(event);
209-
} else {
210-
handleJoinLocation(event);
211-
}
212-
this.handleGameModeAndFlight(player, event.getSpawnLocation().getWorld());
213-
}
202+
EventRunnable playerSpawnLocation() {
203+
return new EventRunnable<PlayerSpawnLocationEvent>() {
204+
@Override
205+
public void onEvent(PlayerSpawnLocationEvent event) {
206+
Player player = event.getPlayer();
207+
MultiverseWorld world = getWorldManager().getLoadedWorld(player.getWorld()).getOrNull();
208+
if (world == null) {
209+
Logging.finer("Player joined in a world that is not managed by Multiverse.");
210+
return;
211+
}
212+
if (!player.hasPlayedBefore()) {
213+
handleFirstSpawn(event);
214+
} else {
215+
handleJoinLocation(event);
216+
}
217+
handleGameModeAndFlight(player, event.getSpawnLocation().getWorld());
218+
}
214219

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

232-
private void handleJoinLocation(PlayerSpawnLocationEvent event) {
233-
if (!config.getEnableJoinDestination()) {
234-
Logging.finer("JoinDestination is disabled");
235-
// User has disabled the feature in config
236-
return;
237-
}
238-
if (config.getJoinDestination().isBlank()) {
239-
Logging.warning("Joindestination is enabled but no destination has been specified in config!");
240-
return;
241-
}
242-
Logging.finer("JoinDestination is " + config.getJoinDestination());
243-
destinationsProvider.parseDestination(config.getJoinDestination())
244-
.map(destination -> destination.getLocation(event.getPlayer())
245-
.peek(event::setSpawnLocation)
246-
.onEmpty(() -> Logging.warning("The destination in JoinDestination in config is invalid")))
247-
.onFailure(failure -> {
248-
Logging.warning("Invalid destination in JoinDestination in config: %s");
249-
Logging.warning(failure.getFailureMessage().formatted(getLocales()));
250-
});
237+
private void handleJoinLocation(PlayerSpawnLocationEvent event) {
238+
if (!config.getEnableJoinDestination()) {
239+
Logging.finer("JoinDestination is disabled");
240+
// User has disabled the feature in config
241+
return;
242+
}
243+
if (config.getJoinDestination().isBlank()) {
244+
Logging.warning("Joindestination is enabled but no destination has been specified in config!");
245+
return;
246+
}
247+
Logging.finer("JoinDestination is " + config.getJoinDestination());
248+
destinationsProvider.parseDestination(config.getJoinDestination())
249+
.map(destination -> destination.getLocation(event.getPlayer())
250+
.peek(event::setSpawnLocation)
251+
.onEmpty(() -> Logging.warning("The destination in JoinDestination in config is invalid")))
252+
.onFailure(failure -> {
253+
Logging.warning("Invalid destination in JoinDestination in config: %s");
254+
Logging.warning(failure.getFailureMessage().formatted(getLocales()));
255+
});
256+
}
257+
};
251258
}
252259

253260
/**

0 commit comments

Comments
 (0)