From 46c36f3b345241be8851b6aa0009c11b172d6256 Mon Sep 17 00:00:00 2001 From: William Date: Fri, 26 Aug 2022 16:54:40 +0100 Subject: [PATCH] Fixed blocking calls to #getPosition causing crashes in rare cases --- .../huskhomes/command/SetSpawnCommand.java | 11 +- .../huskhomes/command/SetWarpCommand.java | 4 +- .../huskhomes/command/TpCommand.java | 162 +++++++++--------- .../huskhomes/command/TpHereCommand.java | 41 +++-- 4 files changed, 114 insertions(+), 104 deletions(-) diff --git a/common/src/main/java/net/william278/huskhomes/command/SetSpawnCommand.java b/common/src/main/java/net/william278/huskhomes/command/SetSpawnCommand.java index 2a65cce0..941f28d9 100644 --- a/common/src/main/java/net/william278/huskhomes/command/SetSpawnCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/SetSpawnCommand.java @@ -19,13 +19,12 @@ public class SetSpawnCommand extends CommandBase { @Override public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) { - if (args.length > 0) { - plugin.getLocales().getLocale("error_invalid_syntax", "/setspawn") - .ifPresent(onlineUser::sendMessage); - return; - } - CompletableFuture.runAsync(() -> { + if (args.length > 0) { + plugin.getLocales().getLocale("error_invalid_syntax", "/setspawn") + .ifPresent(onlineUser::sendMessage); + return; + } final Position position = onlineUser.getPosition(); if (plugin.getSettings().crossServer && plugin.getSettings().globalSpawn) { final Optional warp = plugin.getDatabase().getWarp(plugin.getSettings().globalSpawnName).join(); diff --git a/common/src/main/java/net/william278/huskhomes/command/SetWarpCommand.java b/common/src/main/java/net/william278/huskhomes/command/SetWarpCommand.java index 53c809f7..44b00009 100644 --- a/common/src/main/java/net/william278/huskhomes/command/SetWarpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/SetWarpCommand.java @@ -6,6 +6,8 @@ import net.william278.huskhomes.position.PositionMeta; import net.william278.huskhomes.util.Permission; import org.jetbrains.annotations.NotNull; +import java.util.concurrent.CompletableFuture; + public class SetWarpCommand extends CommandBase { protected SetWarpCommand(@NotNull HuskHomes implementor) { @@ -15,7 +17,7 @@ public class SetWarpCommand extends CommandBase { @Override public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) { if (args.length == 1) { - setWarp(onlineUser, args[0]); + CompletableFuture.runAsync(() -> setWarp(onlineUser, args[0])); } else { plugin.getLocales().getLocale("error_invalid_syntax", "/setwarp ") .ifPresent(onlineUser::sendMessage); diff --git a/common/src/main/java/net/william278/huskhomes/command/TpCommand.java b/common/src/main/java/net/william278/huskhomes/command/TpCommand.java index 702479be..e791a18d 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpCommand.java @@ -11,6 +11,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.*; +import java.util.concurrent.CompletableFuture; import java.util.logging.Level; import java.util.stream.Collectors; @@ -22,93 +23,98 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe @Override public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) { - String teleportingUserName = onlineUser.username; - TeleportCommandTarget targetPosition; + CompletableFuture.runAsync(() -> { + String teleportingUserName = onlineUser.username; + TeleportCommandTarget targetPosition; - // Validate argument length - if (args.length > 6 || args.length < 1) { - plugin.getLocales().getLocale("error_invalid_syntax", "/tp [destination]") - .ifPresent(onlineUser::sendMessage); - return; - } - - // If the first argument is a player target - if (args.length == 1) { - targetPosition = new TeleportCommandTarget(args[0]); - } else { - // Determine if a player argument has been passed before the destination argument - int coordinatesIndex = ((args.length > 3 && (isCoordinate(args[1]) && isCoordinate(args[2]) && isCoordinate(args[3]))) - || (args.length == 2)) ? 1 : 0; - if (coordinatesIndex == 1) { - teleportingUserName = args[0]; + // Validate argument length + if (args.length > 6 || args.length < 1) { + plugin.getLocales().getLocale("error_invalid_syntax", "/tp [destination]") + .ifPresent(onlineUser::sendMessage); + return; } - // Determine the target position (player or coordinates) - if (args.length == 2) { - targetPosition = new TeleportCommandTarget(args[1]); + // If the first argument is a player target + if (args.length == 1) { + targetPosition = new TeleportCommandTarget(args[0]); } else { - // Coordinate teleportation requires a permission node - if (!onlineUser.hasPermission(Permission.COMMAND_TP_TO_COORDINATES.node)) { - plugin.getLocales().getLocale("error_no_permission").ifPresent(onlineUser::sendMessage); - return; + // Determine if a player argument has been passed before the destination argument + int coordinatesIndex = ((args.length > 3 && (isCoordinate(args[1]) && isCoordinate(args[2]) && isCoordinate(args[3]))) + || (args.length == 2)) ? 1 : 0; + if (coordinatesIndex == 1) { + teleportingUserName = args[0]; } - // Parse the coordinates and set the target position - final Position userPosition = onlineUser.getPosition(); - final Optional parsedPosition = Position.parse(userPosition, - Arrays.copyOfRange(args, coordinatesIndex, args.length)); - if (parsedPosition.isEmpty()) { - plugin.getLocales().getLocale("error_invalid_syntax", - "/tp [world] [server]") - .ifPresent(onlineUser::sendMessage); - return; + // Determine the target position (player or coordinates) + if (args.length == 2) { + targetPosition = new TeleportCommandTarget(args[1]); + } else { + // Coordinate teleportation requires a permission node + if (!onlineUser.hasPermission(Permission.COMMAND_TP_TO_COORDINATES.node)) { + plugin.getLocales().getLocale("error_no_permission").ifPresent(onlineUser::sendMessage); + return; + } + + // Parse the coordinates and set the target position + final Position userPosition = onlineUser.getPosition(); + final Optional parsedPosition = Position.parse(userPosition, + Arrays.copyOfRange(args, coordinatesIndex, args.length)); + if (parsedPosition.isEmpty()) { + plugin.getLocales().getLocale("error_invalid_syntax", + "/tp [world] [server]") + .ifPresent(onlineUser::sendMessage); + return; + } + targetPosition = new TeleportCommandTarget(parsedPosition.get()); } - targetPosition = new TeleportCommandTarget(parsedPosition.get()); } - } - // Execute the teleport - final String playerToTeleportName = teleportingUserName; - if (targetPosition.targetType == TeleportCommandTarget.TargetType.PLAYER) { - // Teleport players by usernames - assert targetPosition.targetPlayer != null; - plugin.getTeleportManager() - .teleportPlayerToPlayerByName(playerToTeleportName, targetPosition.targetPlayer, onlineUser, false) - .thenAccept(resultIfPlayerExists -> resultIfPlayerExists.ifPresentOrElse( - result -> { - if (result.successful) { - plugin.getLocales().getLocale("teleporting_other_complete", - playerToTeleportName, targetPosition.targetPlayer) - .ifPresent(onlineUser::sendMessage); - return; - } - plugin.getTeleportManager().finishTeleport(onlineUser, result); - }, - () -> plugin.getLocales().getLocale("error_player_not_found", playerToTeleportName) - .ifPresent(onlineUser::sendMessage))); - return; - } else if (targetPosition.targetType == TeleportCommandTarget.TargetType.POSITION) { - // Teleport players by specified position - assert targetPosition.targetPosition != null; - plugin.getTeleportManager().teleportPlayerByName(playerToTeleportName, targetPosition.targetPosition, onlineUser, false) - .thenAccept(resultIfPlayerExists -> resultIfPlayerExists.ifPresentOrElse( - result -> { - if (result.successful && !playerToTeleportName.equalsIgnoreCase(onlineUser.username)) { - plugin.getLocales().getLocale("teleporting_other_complete_position", - playerToTeleportName, - Integer.toString((int) targetPosition.targetPosition.x), - Integer.toString((int) targetPosition.targetPosition.y), - Integer.toString((int) targetPosition.targetPosition.z)) - .ifPresent(onlineUser::sendMessage); - return; - } - plugin.getTeleportManager().finishTeleport(onlineUser, result); - }, - () -> plugin.getLocales().getLocale("error_player_not_found", playerToTeleportName) - .ifPresent(onlineUser::sendMessage))); - return; - } - throw new HuskHomesException("Attempted to execute invalid teleport command operation"); + // Execute the teleport + final String playerToTeleportName = teleportingUserName; + if (targetPosition.targetType == TeleportCommandTarget.TargetType.PLAYER) { + // Teleport players by usernames + assert targetPosition.targetPlayer != null; + plugin.getTeleportManager() + .teleportPlayerToPlayerByName(playerToTeleportName, targetPosition.targetPlayer, onlineUser, false) + .thenAccept(resultIfPlayerExists -> resultIfPlayerExists.ifPresentOrElse( + result -> { + if (result.successful) { + plugin.getLocales().getLocale("teleporting_other_complete", + playerToTeleportName, targetPosition.targetPlayer) + .ifPresent(onlineUser::sendMessage); + return; + } + plugin.getTeleportManager().finishTeleport(onlineUser, result); + }, + () -> plugin.getLocales().getLocale("error_player_not_found", playerToTeleportName) + .ifPresent(onlineUser::sendMessage))); + return; + } else if (targetPosition.targetType == TeleportCommandTarget.TargetType.POSITION) { + // Teleport players by specified position + assert targetPosition.targetPosition != null; + plugin.getTeleportManager().teleportPlayerByName(playerToTeleportName, targetPosition.targetPosition, onlineUser, false) + .thenAccept(resultIfPlayerExists -> resultIfPlayerExists.ifPresentOrElse( + result -> { + if (result.successful && !playerToTeleportName.equalsIgnoreCase(onlineUser.username)) { + plugin.getLocales().getLocale("teleporting_other_complete_position", + playerToTeleportName, + Integer.toString((int) targetPosition.targetPosition.x), + Integer.toString((int) targetPosition.targetPosition.y), + Integer.toString((int) targetPosition.targetPosition.z)) + .ifPresent(onlineUser::sendMessage); + return; + } + plugin.getTeleportManager().finishTeleport(onlineUser, result); + }, + () -> plugin.getLocales().getLocale("error_player_not_found", playerToTeleportName) + .ifPresent(onlineUser::sendMessage))); + return; + } + throw new HuskHomesException("Attempted to execute invalid teleport command operation"); + }).exceptionally(throwable -> { + plugin.getLoggingAdapter().log(Level.SEVERE, "An error occurred whilst executing a teleport command", throwable); + return null; + }); } private boolean isCoordinate(@NotNull String coordinate) { diff --git a/common/src/main/java/net/william278/huskhomes/command/TpHereCommand.java b/common/src/main/java/net/william278/huskhomes/command/TpHereCommand.java index 6bcb1670..587bc531 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpHereCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpHereCommand.java @@ -8,6 +8,7 @@ import org.jetbrains.annotations.Nullable; import java.util.Collections; import java.util.List; +import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; public class TpHereCommand extends CommandBase implements TabCompletable { @@ -18,25 +19,27 @@ public class TpHereCommand extends CommandBase implements TabCompletable { @Override public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) { - if (args.length != 1) { - plugin.getLocales().getLocale("error_invalid_syntax", "/tphere ") - .ifPresent(onlineUser::sendMessage); - return; - } - final String targetPlayerName = args[0]; - plugin.getTeleportManager().teleportPlayerByName(targetPlayerName, onlineUser.getPosition(), onlineUser, false) - .thenAccept(resultIfPlayerExists -> resultIfPlayerExists.ifPresentOrElse( - result -> { - if (result.successful) { - plugin.getLocales().getLocale("teleporting_other_complete", - targetPlayerName, onlineUser.username) - .ifPresent(onlineUser::sendMessage); - return; - } - plugin.getTeleportManager().finishTeleport(onlineUser, result); - }, - () -> plugin.getLocales().getLocale("error_player_not_found", targetPlayerName) - .ifPresent(onlineUser::sendMessage))); + CompletableFuture.runAsync(() -> { + if (args.length != 1) { + plugin.getLocales().getLocale("error_invalid_syntax", "/tphere ") + .ifPresent(onlineUser::sendMessage); + return; + } + final String targetPlayerName = args[0]; + plugin.getTeleportManager().teleportPlayerByName(targetPlayerName, onlineUser.getPosition(), onlineUser, false) + .thenAccept(resultIfPlayerExists -> resultIfPlayerExists.ifPresentOrElse( + result -> { + if (result.successful) { + plugin.getLocales().getLocale("teleporting_other_complete", + targetPlayerName, onlineUser.username) + .ifPresent(onlineUser::sendMessage); + return; + } + plugin.getTeleportManager().finishTeleport(onlineUser, result); + }, + () -> plugin.getLocales().getLocale("error_player_not_found", targetPlayerName) + .ifPresent(onlineUser::sendMessage))); + }); } @Override