Skip to content

Commit

Permalink
Merge pull request #71 from TheNextLvl-net/fix-spawn-npe
Browse files Browse the repository at this point in the history
Fix null world checks in spawn handling logic
  • Loading branch information
NonSwag authored Jan 8, 2025
2 parents e72b892 + e85913a commit 263abf2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ public void register(Commands registrar) {
.executes(context -> {
var player = (Player) context.getSource().getSender();
var location = plugin.config().spawn().location();
if (location == null) {
if (location == null || location.getWorld() == null) {
plugin.bundle().sendMessage(player, "command.spawn.undefined");
if (player.hasPermission("tweaks.command.setspawn"))
plugin.bundle().sendMessage(player, "command.spawn.define");
return 0;
} else plugin.teleportController().teleport(player, location, COMMAND).thenAccept(success -> {
var message = success ? "command.spawn" : "command.teleport.cancelled";
plugin.bundle().sendMessage(player, message);
});
return location != null ? Command.SINGLE_SUCCESS : 0;
return Command.SINGLE_SUCCESS;
}).build();
registrar.register(command, "Teleport you to spawn", plugin.commands().spawn().aliases());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class SpawnListener implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerSpawnLocation(PlayerSpawnLocationEvent event) {
var config = plugin.config().spawn();
if (config.location() == null) return;
if (config.location() == null || config.location().getWorld() == null) return;
if ((!config.teleportOnFirstJoin() || event.getPlayer().hasPlayedBefore())
&& (!config.teleportOnJoin() || !event.getPlayer().hasPlayedBefore())) return;
event.setSpawnLocation(config.location());
Expand All @@ -28,15 +28,15 @@ public void onPlayerSpawnLocation(PlayerSpawnLocationEvent event) {
public void onPlayerRespawn(PlayerRespawnEvent event) {
if (!event.getRespawnReason().equals(PlayerRespawnEvent.RespawnReason.DEATH)) return;
var config = plugin.config().spawn();
if (config.location() == null || !config.teleportOnRespawn()) return;
if (config.location() == null || config.location().getWorld() == null || !config.teleportOnRespawn()) return;
if (!config.ignoreRespawnPosition() && (event.isAnchorSpawn() || event.isBedSpawn())) return;
event.setRespawnLocation(config.location());
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerDeath(PlayerDeathEvent event) {
var config = plugin.config().spawn();
if (config.location() == null || !config.teleportOnRespawn()) return;
if (config.location() == null || config.location().getWorld() == null || !config.teleportOnRespawn()) return;
if (config.ignoreRespawnPosition()) event.getPlayer().setRespawnLocation(null);
}
}

0 comments on commit 263abf2

Please sign in to comment.