From 8a1cc15d1df61d2b68f3f119e3f1d1a3213fdaab Mon Sep 17 00:00:00 2001 From: William Date: Tue, 23 Nov 2021 00:26:23 +0000 Subject: [PATCH] Add player name auto-completion, Close #105 --- .../huskhomes2/commands/TpCommand.java | 3 +- .../huskhomes2/commands/TpHereCommand.java | 3 +- .../huskhomes2/commands/TpaCommand.java | 3 +- .../huskhomes2/commands/TpaHereCommand.java | 3 +- .../huskhomes2/config/Settings.java | 9 +++++ .../huskhomes2/util/NameAutoCompleter.java | 40 +++++++++++++++++++ src/main/resources/config.yml | 1 + 7 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 src/main/java/me/william278/huskhomes2/util/NameAutoCompleter.java diff --git a/src/main/java/me/william278/huskhomes2/commands/TpCommand.java b/src/main/java/me/william278/huskhomes2/commands/TpCommand.java index 17af312c..f3989fec 100644 --- a/src/main/java/me/william278/huskhomes2/commands/TpCommand.java +++ b/src/main/java/me/william278/huskhomes2/commands/TpCommand.java @@ -4,6 +4,7 @@ import me.william278.huskhomes2.HuskHomes; import me.william278.huskhomes2.MessageManager; import me.william278.huskhomes2.teleport.TeleportManager; import me.william278.huskhomes2.teleport.points.TeleportationPoint; +import me.william278.huskhomes2.util.NameAutoCompleter; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -34,7 +35,7 @@ public class TpCommand extends CommandBase { return; case 1: String targetPlayer = args[0]; - TeleportManager.teleportPlayer(p, targetPlayer, connection); + TeleportManager.teleportPlayer(p, NameAutoCompleter.getAutoCompletedName(targetPlayer), connection); return; case 3: try { diff --git a/src/main/java/me/william278/huskhomes2/commands/TpHereCommand.java b/src/main/java/me/william278/huskhomes2/commands/TpHereCommand.java index 318e10ec..93b4f819 100644 --- a/src/main/java/me/william278/huskhomes2/commands/TpHereCommand.java +++ b/src/main/java/me/william278/huskhomes2/commands/TpHereCommand.java @@ -3,6 +3,7 @@ package me.william278.huskhomes2.commands; import me.william278.huskhomes2.HuskHomes; import me.william278.huskhomes2.MessageManager; import me.william278.huskhomes2.teleport.TeleportManager; +import me.william278.huskhomes2.util.NameAutoCompleter; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.entity.Player; @@ -21,7 +22,7 @@ public class TpHereCommand extends CommandBase { String targetPlayer = args[0]; Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { try (Connection connection = HuskHomes.getConnection()) { - TeleportManager.teleportHere(p, targetPlayer, connection); + TeleportManager.teleportHere(p, NameAutoCompleter.getAutoCompletedName(targetPlayer), connection); } catch (SQLException e) { plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred using /tphere"); } diff --git a/src/main/java/me/william278/huskhomes2/commands/TpaCommand.java b/src/main/java/me/william278/huskhomes2/commands/TpaCommand.java index 02f55e3b..8aae68a1 100644 --- a/src/main/java/me/william278/huskhomes2/commands/TpaCommand.java +++ b/src/main/java/me/william278/huskhomes2/commands/TpaCommand.java @@ -2,6 +2,7 @@ package me.william278.huskhomes2.commands; import me.william278.huskhomes2.MessageManager; import me.william278.huskhomes2.teleport.TeleportRequestHandler; +import me.william278.huskhomes2.util.NameAutoCompleter; import org.bukkit.command.Command; import org.bukkit.entity.Player; @@ -11,7 +12,7 @@ public class TpaCommand extends CommandBase { protected void onCommand(Player p, Command command, String label, String[] args) { if (args.length == 1) { String targetPlayer = args[0]; - TeleportRequestHandler.sendTeleportToRequest(p, targetPlayer); + TeleportRequestHandler.sendTeleportToRequest(p, NameAutoCompleter.getAutoCompletedName(targetPlayer)); } else { MessageManager.sendMessage(p, "error_invalid_syntax", command.getUsage()); } diff --git a/src/main/java/me/william278/huskhomes2/commands/TpaHereCommand.java b/src/main/java/me/william278/huskhomes2/commands/TpaHereCommand.java index 4e9dd4a3..1b3df5dc 100644 --- a/src/main/java/me/william278/huskhomes2/commands/TpaHereCommand.java +++ b/src/main/java/me/william278/huskhomes2/commands/TpaHereCommand.java @@ -2,6 +2,7 @@ package me.william278.huskhomes2.commands; import me.william278.huskhomes2.MessageManager; import me.william278.huskhomes2.teleport.TeleportRequestHandler; +import me.william278.huskhomes2.util.NameAutoCompleter; import org.bukkit.command.Command; import org.bukkit.entity.Player; @@ -11,7 +12,7 @@ public class TpaHereCommand extends CommandBase { protected void onCommand(Player p, Command command, String label, String[] args) { if (args.length == 1) { String targetPlayer = args[0]; - TeleportRequestHandler.sendTeleportHereRequest(p, targetPlayer); + TeleportRequestHandler.sendTeleportHereRequest(p, NameAutoCompleter.getAutoCompletedName(targetPlayer)); } else { MessageManager.sendMessage(p, "error_invalid_syntax", command.getUsage()); } diff --git a/src/main/java/me/william278/huskhomes2/config/Settings.java b/src/main/java/me/william278/huskhomes2/config/Settings.java index 454b8235..21ec909e 100644 --- a/src/main/java/me/william278/huskhomes2/config/Settings.java +++ b/src/main/java/me/william278/huskhomes2/config/Settings.java @@ -93,6 +93,9 @@ public class Settings { private boolean hideCommandsFromHelpMenuWithoutPermission; private boolean hideHuskHomesCommandFromHelpMenu; + // Whether to autocomplete player names for tpa (e.g /tpa William will assume you mean /tpa William278) + private boolean autoCompletePlayerNames; + // RTP command settings private boolean doRtpCommand; private int rtpRange; @@ -239,6 +242,8 @@ public class Settings { this.warpRestrictionPermissionFormat = config.getString("warp_command.permission_restrictions.format", "huskhomes.warp."); this.hideRestrictedWarps = config.getBoolean("warp_command.permission_restrictions.hide_restricted_warps", true); + this.autoCompletePlayerNames = config.getBoolean("general.auto_complete_player_names", false); + this.doRtpCommand = config.getBoolean("random_teleport_command.enabled", true); this.rtpRange = config.getInt("random_teleport_command.range", 5000); this.rtpCoolDown = config.getInt("random_teleport_command.cooldown", 30); @@ -511,4 +516,8 @@ public class Settings { public long getHikariConnectionTimeOut() { return hikariConnectionTimeOut; } + + public boolean doAutoCompletePlayerNames() { + return autoCompletePlayerNames; + } } diff --git a/src/main/java/me/william278/huskhomes2/util/NameAutoCompleter.java b/src/main/java/me/william278/huskhomes2/util/NameAutoCompleter.java new file mode 100644 index 00000000..a35e12a2 --- /dev/null +++ b/src/main/java/me/william278/huskhomes2/util/NameAutoCompleter.java @@ -0,0 +1,40 @@ +package me.william278.huskhomes2.util; + +import me.william278.huskhomes2.HuskHomes; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +public class NameAutoCompleter { + + /* + * Auto complete a player's name + */ + public static String getAutoCompletedName(String inputName) { + if (HuskHomes.getSettings().doAutoCompletePlayerNames()) { + return inputName; + } + String found = inputName; + + // Try for an exact match first. + Player localPlayer = Bukkit.getPlayerExact(inputName); + if (localPlayer != null) { + return localPlayer.getName(); + } + + // Iterate through players in the PlayerList to find a match + String lowerName = inputName.toLowerCase(); + int delta = Integer.MAX_VALUE; + for (String playerName : HuskHomes.getPlayerList().getPlayers()) { + if (playerName.toLowerCase().startsWith(lowerName)) { + int currentDelta = Math.abs(playerName.length() - lowerName.length()); + if (currentDelta < delta) { + found = playerName; + delta = currentDelta; + } + if (currentDelta == 0) break; + } + } + return found; + } + +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index b7bef51e..629999fb 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -28,6 +28,7 @@ data_storage_options: warps_data: 'huskhomes_warp_data' general: max_sethomes: 10 + auto_complete_player_names: false teleport_warmup_time: 5 teleport_request_expiry_time: 60 allow_unicode_descriptions: true