*/
- 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
*/
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;
+ }
+
+}