Add player name auto-completion, Close #105

This commit is contained in:
William committed 2021-11-23 00:26:23 +00:00
1 parent 830a7443b9
commit 8a1cc15d1d
7 files changed
+58 -4

No files matched your search

@@ -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 {
@@ -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");
}
@@ -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());
}
@@ -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());
}
@@ -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;
}
}
@@ -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;
}
}
+1
View File
@@ -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