Skip to content

Commit

Permalink
Fix null world checks in spawn handling logic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
NonSwag committed Jan 8, 2025
1 parent a6d144a commit e85913a
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 e85913a

Please sign in to comment.