From 36f1454cfa39c74f12188a9e4e89cf70fd601bae Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Tue, 9 Jan 2024 00:25:51 +0100 Subject: [PATCH] Implements the tab-completers for the payout commands Implements the tab-completer for the set group payout command Implements the tab-completer for the set player payout command Allows specifying the name of an offline player when using the set player payout command Implements proper un-setting of set payouts Adds the pom file Filters out Citizens UUIDs when getting offline players --- pom.xml | 104 ++++++++++++++++++ .../knarcraft/timeismoney/TimeIsMoney.java | 8 +- .../command/SetGroupPaymentCommand.java | 24 +++- .../command/SetGroupPaymentTabCompleter.java | 35 ++++++ .../command/SetPlayerPaymentCommand.java | 51 +++++++-- .../command/SetPlayerPaymentTabCompleter.java | 59 ++++++++++ .../timeismoney/config/Configuration.java | 12 +- .../manager/PermissionManager.java | 9 ++ .../timeismoney/util/StringHelper.java | 25 +++++ .../timeismoney/util/TabCompletionHelper.java | 50 +++++++++ 10 files changed, 361 insertions(+), 16 deletions(-) create mode 100644 pom.xml create mode 100644 src/main/java/net/knarcraft/timeismoney/command/SetGroupPaymentTabCompleter.java create mode 100644 src/main/java/net/knarcraft/timeismoney/command/SetPlayerPaymentTabCompleter.java create mode 100644 src/main/java/net/knarcraft/timeismoney/util/StringHelper.java create mode 100644 src/main/java/net/knarcraft/timeismoney/util/TabCompletionHelper.java diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b78e032 --- /dev/null +++ b/pom.xml @@ -0,0 +1,104 @@ + + + 4.0.0 + + net.knarcraft + TimeIsMoney + 1.0-SNAPSHOT + jar + + TimeIsMoney + + + 16 + UTF-8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + false + + + + + + + + src/main/resources + true + + + + + + + spigotmc-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + sonatype + https://oss.sonatype.org/content/groups/public/ + + + jitpack.io + https://jitpack.io + + + essentials-releases + https://repo.essentialsx.net/releases/ + + + paper-repo + https://papermc.io/repo/repository/maven-public/ + + + + + + org.spigotmc + spigot-api + 1.20.4-R0.1-SNAPSHOT + provided + + + org.jetbrains + annotations + 24.0.1 + provided + + + com.github.MilkBowl + VaultAPI + 1.7 + provided + + + net.essentialsx + EssentialsX + 2.20.1 + provided + + + diff --git a/src/main/java/net/knarcraft/timeismoney/TimeIsMoney.java b/src/main/java/net/knarcraft/timeismoney/TimeIsMoney.java index 1a5b720..50ad52f 100644 --- a/src/main/java/net/knarcraft/timeismoney/TimeIsMoney.java +++ b/src/main/java/net/knarcraft/timeismoney/TimeIsMoney.java @@ -3,7 +3,9 @@ package net.knarcraft.timeismoney; import com.earth2me.essentials.IEssentials; import net.knarcraft.timeismoney.command.ReloadCommand; import net.knarcraft.timeismoney.command.SetGroupPaymentCommand; +import net.knarcraft.timeismoney.command.SetGroupPaymentTabCompleter; import net.knarcraft.timeismoney.command.SetPlayerPaymentCommand; +import net.knarcraft.timeismoney.command.SetPlayerPaymentTabCompleter; import net.knarcraft.timeismoney.config.Configuration; import net.knarcraft.timeismoney.listener.PlayerJoinListener; import net.knarcraft.timeismoney.manager.EconomyManager; @@ -65,8 +67,10 @@ public final class TimeIsMoney extends JavaPlugin { ReloadCommand reloadCommand = new ReloadCommand(); registerCommand(getCommand("reload"), reloadCommand, reloadCommand); - registerCommand(getCommand("setGroupPayout"), new SetGroupPaymentCommand(configuration), null); - registerCommand(getCommand("setPlayerPayout"), new SetPlayerPaymentCommand(configuration), null); + registerCommand(getCommand("setGroupPayout"), new SetGroupPaymentCommand(configuration), + new SetGroupPaymentTabCompleter()); + registerCommand(getCommand("setPlayerPayout"), new SetPlayerPaymentCommand(configuration), + new SetPlayerPaymentTabCompleter()); Bukkit.getPluginManager().registerEvents(new PlayerJoinListener(), this); diff --git a/src/main/java/net/knarcraft/timeismoney/command/SetGroupPaymentCommand.java b/src/main/java/net/knarcraft/timeismoney/command/SetGroupPaymentCommand.java index 32994ec..be95e89 100644 --- a/src/main/java/net/knarcraft/timeismoney/command/SetGroupPaymentCommand.java +++ b/src/main/java/net/knarcraft/timeismoney/command/SetGroupPaymentCommand.java @@ -1,10 +1,12 @@ package net.knarcraft.timeismoney.command; import net.knarcraft.timeismoney.config.Configuration; +import net.knarcraft.timeismoney.util.StringHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * A command for overriding payments for specific groups @@ -31,9 +33,14 @@ public class SetGroupPaymentCommand implements CommandExecutor { try { String group = arguments[0]; - Double payout = Double.parseDouble(arguments[1]); - configuration.setGroupPayout(group, payout); - configuration.save(); + if (StringHelper.isNonValue(arguments[1])) { + setPayout(group, null); + commandSender.sendMessage(String.format("Group payout for group %s has been cleared", group)); + } else { + Double payout = Double.parseDouble(arguments[1]); + setPayout(group, payout); + commandSender.sendMessage(String.format("Group payout for group %s has been set to %s", group, payout)); + } return true; } catch (NumberFormatException exception) { commandSender.sendMessage("Payout must be a number"); @@ -41,4 +48,15 @@ public class SetGroupPaymentCommand implements CommandExecutor { } } + /** + * Sets the payout for the given group + * + * @param group

The group to set payout for

+ * @param payout

The payout to set

+ */ + private void setPayout(@NotNull String group, @Nullable Double payout) { + configuration.setGroupPayout(group, payout); + configuration.save(); + } + } diff --git a/src/main/java/net/knarcraft/timeismoney/command/SetGroupPaymentTabCompleter.java b/src/main/java/net/knarcraft/timeismoney/command/SetGroupPaymentTabCompleter.java new file mode 100644 index 0000000..610c32c --- /dev/null +++ b/src/main/java/net/knarcraft/timeismoney/command/SetGroupPaymentTabCompleter.java @@ -0,0 +1,35 @@ +package net.knarcraft.timeismoney.command; + +import net.knarcraft.timeismoney.manager.PermissionManager; +import net.knarcraft.timeismoney.util.TabCompletionHelper; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; + +/** + * The tab-completer for the set group payment command + */ +public class SetGroupPaymentTabCompleter implements TabCompleter { + + @Nullable + @Override + public List onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, + @NotNull String[] arguments) { + if (arguments.length == 1) { + // List permission groups + return TabCompletionHelper.filterMatchingContains(List.of(PermissionManager.getPermissionGroups()), + arguments[0]); + } else if (arguments.length == 2) { + // List possible payout values + return TabCompletionHelper.filterMatchingStartsWith(List.of("clear", "null", "none", "5", "10", "100"), + arguments[1]); + } + return new ArrayList<>(); + } + +} diff --git a/src/main/java/net/knarcraft/timeismoney/command/SetPlayerPaymentCommand.java b/src/main/java/net/knarcraft/timeismoney/command/SetPlayerPaymentCommand.java index 74dbc87..c1d468c 100644 --- a/src/main/java/net/knarcraft/timeismoney/command/SetPlayerPaymentCommand.java +++ b/src/main/java/net/knarcraft/timeismoney/command/SetPlayerPaymentCommand.java @@ -1,12 +1,15 @@ package net.knarcraft.timeismoney.command; import net.knarcraft.timeismoney.config.Configuration; +import net.knarcraft.timeismoney.util.StringHelper; import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.UUID; @@ -22,7 +25,7 @@ public class SetPlayerPaymentCommand implements CommandExecutor { * * @param configuration

The configuration to use

*/ - public SetPlayerPaymentCommand(Configuration configuration) { + public SetPlayerPaymentCommand(@NotNull Configuration configuration) { this.configuration = configuration; } @@ -32,31 +35,61 @@ public class SetPlayerPaymentCommand implements CommandExecutor { if (arguments.length < 2) { return false; } + String playerName = arguments[0]; + // Parse the player id UUID playerId; try { - playerId = UUID.fromString(arguments[0]); + // Find player from UUID + playerId = UUID.fromString(playerName); } catch (IllegalArgumentException exception) { - Player player = Bukkit.getPlayer(arguments[0]); + // Get player from player name + Player player = Bukkit.getPlayer(playerName); if (player != null) { playerId = player.getUniqueId(); } else { - commandSender.sendMessage("You must supply a valid name of an online player, or a UUID"); - return false; + // Try to match the player name against the offline player list + playerId = null; + for (OfflinePlayer offlinePlayer : Bukkit.getOfflinePlayers()) { + if (offlinePlayer.getName() != null && offlinePlayer.getName().equals(playerName)) { + playerId = offlinePlayer.getUniqueId(); + } + } + + if (playerId == null) { + commandSender.sendMessage("You must supply a valid name of an online player, or a UUID"); + return false; + } } } - double payout; + // Parse the payout value try { - payout = Double.parseDouble(arguments[1]); + if (StringHelper.isNonValue(arguments[1])) { + setPayout(playerId, null); + commandSender.sendMessage(String.format("Player payout for player %s has been cleared", playerName)); + } else { + Double payout = Double.parseDouble(arguments[1]); + setPayout(playerId, payout); + commandSender.sendMessage(String.format("Player payout for player %s has been set to %s", playerName, + payout)); + } + return true; } catch (NumberFormatException exception) { commandSender.sendMessage("Payout must be a number"); return false; } - + } + + /** + * Sets the payout for the given player + * + * @param playerId

The player to set payout for

+ * @param payout

The payout to set

+ */ + private void setPayout(@NotNull UUID playerId, @Nullable Double payout) { configuration.setPlayerPayout(playerId, payout); configuration.save(); - return true; } } diff --git a/src/main/java/net/knarcraft/timeismoney/command/SetPlayerPaymentTabCompleter.java b/src/main/java/net/knarcraft/timeismoney/command/SetPlayerPaymentTabCompleter.java new file mode 100644 index 0000000..e6719fe --- /dev/null +++ b/src/main/java/net/knarcraft/timeismoney/command/SetPlayerPaymentTabCompleter.java @@ -0,0 +1,59 @@ +package net.knarcraft.timeismoney.command; + +import net.knarcraft.timeismoney.util.TabCompletionHelper; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; + +/** + * The tab-completer for the set player payment command + */ +public class SetPlayerPaymentTabCompleter implements TabCompleter { + + @Nullable + @Override + public List onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, + @NotNull String[] arguments) { + if (arguments.length == 1) { + // List online players + return TabCompletionHelper.filterMatchingContains(getPlayerNames(), arguments[0]); + } else if (arguments.length == 2) { + // List possible payout values + return TabCompletionHelper.filterMatchingStartsWith(List.of("clear", "null", "none", "5", "10", "100"), + arguments[1]); + } + return new ArrayList<>(); + } + + /** + * Gets names of all online players, and ids of offline players + * + * @return

The names and ids of all known players

+ */ + private @NotNull List getPlayerNames() { + List playerNames = new ArrayList<>(); + for (Player player : Bukkit.getOnlinePlayers()) { + playerNames.add(player.getName()); + } + for (OfflinePlayer offlinePlayer : Bukkit.getOfflinePlayers()) { + String uuid = offlinePlayer.getUniqueId().toString(); + if (!uuid.startsWith("00000000-0000-0000")) { + if (offlinePlayer.getName() != null) { + playerNames.add(offlinePlayer.getName()); + } else { + playerNames.add(offlinePlayer.getUniqueId().toString()); + } + } + } + return playerNames; + } + +} diff --git a/src/main/java/net/knarcraft/timeismoney/config/Configuration.java b/src/main/java/net/knarcraft/timeismoney/config/Configuration.java index 97e4080..70f8ad6 100644 --- a/src/main/java/net/knarcraft/timeismoney/config/Configuration.java +++ b/src/main/java/net/knarcraft/timeismoney/config/Configuration.java @@ -142,7 +142,11 @@ public class Configuration { * @param payout

The payout to set for the player

*/ public void setPlayerPayout(@NotNull UUID playerId, @Nullable Double payout) { - this.playerPayouts.put(playerId, payout); + if (payout == null || payout < 0) { + this.playerPayouts.put(playerId, null); + } else { + this.playerPayouts.put(playerId, payout); + } } /** @@ -152,7 +156,11 @@ public class Configuration { * @param payout

The payout to set for the group

*/ public void setGroupPayout(@NotNull String groupName, @Nullable Double payout) { - this.groupPayouts.put(groupName, payout); + if (payout == null || payout < 0) { + this.groupPayouts.put(groupName, null); + } else { + this.groupPayouts.put(groupName, payout); + } } /** diff --git a/src/main/java/net/knarcraft/timeismoney/manager/PermissionManager.java b/src/main/java/net/knarcraft/timeismoney/manager/PermissionManager.java index a29eeea..8743671 100644 --- a/src/main/java/net/knarcraft/timeismoney/manager/PermissionManager.java +++ b/src/main/java/net/knarcraft/timeismoney/manager/PermissionManager.java @@ -43,4 +43,13 @@ public final class PermissionManager { return permission.getPlayerGroups(player); } + /** + * Gets all available permission groups + * + * @return

All available permission groups

+ */ + public static @NotNull String[] getPermissionGroups() { + return permission.getGroups(); + } + } diff --git a/src/main/java/net/knarcraft/timeismoney/util/StringHelper.java b/src/main/java/net/knarcraft/timeismoney/util/StringHelper.java new file mode 100644 index 0000000..ce40830 --- /dev/null +++ b/src/main/java/net/knarcraft/timeismoney/util/StringHelper.java @@ -0,0 +1,25 @@ +package net.knarcraft.timeismoney.util; + +import org.jetbrains.annotations.NotNull; + +/** + * A helper-class for dealing with strings + */ +public final class StringHelper { + + private StringHelper() { + + } + + /** + * Checks whether the given string value denotes a non-value + * + * @param value

The string value to check

+ * @return

True if the given value is a non-value

+ */ + public static boolean isNonValue(@NotNull String value) { + return value.equalsIgnoreCase("null") || value.equalsIgnoreCase("none") || + value.equalsIgnoreCase("clear"); + } + +} diff --git a/src/main/java/net/knarcraft/timeismoney/util/TabCompletionHelper.java b/src/main/java/net/knarcraft/timeismoney/util/TabCompletionHelper.java new file mode 100644 index 0000000..f3ff301 --- /dev/null +++ b/src/main/java/net/knarcraft/timeismoney/util/TabCompletionHelper.java @@ -0,0 +1,50 @@ +package net.knarcraft.timeismoney.util; + +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; + +/** + * Helper class for getting string lists required for auto-completion + */ +public final class TabCompletionHelper { + + private TabCompletionHelper() { + } + + /** + * Finds tab complete values that contain the typed text + * + * @param values

The values to filter

+ * @param typedText

The text the player has started typing

+ * @return

The given string values that contain the player's typed text

+ */ + public static @NotNull List filterMatchingContains(@NotNull List values, @NotNull String typedText) { + List configValues = new ArrayList<>(); + for (String value : values) { + if (value.toLowerCase().contains(typedText.toLowerCase())) { + configValues.add(value); + } + } + return configValues; + } + + /** + * Finds tab complete values that match the start of the typed text + * + * @param values

The values to filter

+ * @param typedText

The text the player has started typing

+ * @return

The given string values that start with the player's typed text

+ */ + public static @NotNull List filterMatchingStartsWith(@NotNull List values, @NotNull String typedText) { + List configValues = new ArrayList<>(); + for (String value : values) { + if (value.toLowerCase().startsWith(typedText.toLowerCase())) { + configValues.add(value); + } + } + return configValues; + } + +}