Skip to content

Commit e85913a

Browse files
author
david
committed
Fix null world checks in spawn handling logic
Added checks to ensure the spawn location's world is not null in listener methods and the spawn command. This prevents potential errors when accessing invalid or undefined worlds in the spawn configuration.
1 parent a6d144a commit e85913a

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/main/java/net/thenextlvl/tweaks/command/spawn/SpawnCommand.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ public void register(Commands registrar) {
2222
.executes(context -> {
2323
var player = (Player) context.getSource().getSender();
2424
var location = plugin.config().spawn().location();
25-
if (location == null) {
25+
if (location == null || location.getWorld() == null) {
2626
plugin.bundle().sendMessage(player, "command.spawn.undefined");
2727
if (player.hasPermission("tweaks.command.setspawn"))
2828
plugin.bundle().sendMessage(player, "command.spawn.define");
29+
return 0;
2930
} else plugin.teleportController().teleport(player, location, COMMAND).thenAccept(success -> {
3031
var message = success ? "command.spawn" : "command.teleport.cancelled";
3132
plugin.bundle().sendMessage(player, message);
3233
});
33-
return location != null ? Command.SINGLE_SUCCESS : 0;
34+
return Command.SINGLE_SUCCESS;
3435
}).build();
3536
registrar.register(command, "Teleport you to spawn", plugin.commands().spawn().aliases());
3637
}

src/main/java/net/thenextlvl/tweaks/listener/SpawnListener.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class SpawnListener implements Listener {
1818
@EventHandler(priority = EventPriority.LOWEST)
1919
public void onPlayerSpawnLocation(PlayerSpawnLocationEvent event) {
2020
var config = plugin.config().spawn();
21-
if (config.location() == null) return;
21+
if (config.location() == null || config.location().getWorld() == null) return;
2222
if ((!config.teleportOnFirstJoin() || event.getPlayer().hasPlayedBefore())
2323
&& (!config.teleportOnJoin() || !event.getPlayer().hasPlayedBefore())) return;
2424
event.setSpawnLocation(config.location());
@@ -28,15 +28,15 @@ public void onPlayerSpawnLocation(PlayerSpawnLocationEvent event) {
2828
public void onPlayerRespawn(PlayerRespawnEvent event) {
2929
if (!event.getRespawnReason().equals(PlayerRespawnEvent.RespawnReason.DEATH)) return;
3030
var config = plugin.config().spawn();
31-
if (config.location() == null || !config.teleportOnRespawn()) return;
31+
if (config.location() == null || config.location().getWorld() == null || !config.teleportOnRespawn()) return;
3232
if (!config.ignoreRespawnPosition() && (event.isAnchorSpawn() || event.isBedSpawn())) return;
3333
event.setRespawnLocation(config.location());
3434
}
3535

3636
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
3737
public void onPlayerDeath(PlayerDeathEvent event) {
3838
var config = plugin.config().spawn();
39-
if (config.location() == null || !config.teleportOnRespawn()) return;
39+
if (config.location() == null || config.location().getWorld() == null || !config.teleportOnRespawn()) return;
4040
if (config.ignoreRespawnPosition()) event.getPlayer().setRespawnLocation(null);
4141
}
4242
}

0 commit comments

Comments
 (0)