diff --git a/src/main/java/pro/cloudnode/smp/bankaccounts/Permissions.java b/src/main/java/pro/cloudnode/smp/bankaccounts/Permissions.java index bd10c1d..38e7888 100644 --- a/src/main/java/pro/cloudnode/smp/bankaccounts/Permissions.java +++ b/src/main/java/pro/cloudnode/smp/bankaccounts/Permissions.java @@ -27,6 +27,7 @@ public final class Permissions { public static @NotNull String INVOICE_SEND_OTHER = "bank.invoice.send.other"; public static @NotNull String RELOAD = "bank.reload"; public static @NotNull String BALANCE_OTHER = "bank.balance.other"; + public static @NotNull String TRANSFER_FROM_OTHER = "bank.transfer.from-other"; public static @NotNull String HISTORY_OTHER = "bank.history.other"; public static @NotNull String ACCOUNT_CREATE_OTHER = "bank.account.create.other"; public static @NotNull String ACCOUNT_CREATE_BYPASS = "bank.account.create.bypass"; diff --git a/src/main/java/pro/cloudnode/smp/bankaccounts/commands/BankCommand.java b/src/main/java/pro/cloudnode/smp/bankaccounts/commands/BankCommand.java index 8b573b8..136420c 100644 --- a/src/main/java/pro/cloudnode/smp/bankaccounts/commands/BankCommand.java +++ b/src/main/java/pro/cloudnode/smp/bankaccounts/commands/BankCommand.java @@ -57,14 +57,18 @@ public boolean execute(final @NotNull CommandSender sender, final @NotNull Strin if (!sender.hasPermission(Permissions.BALANCE_SELF) && !sender.hasPermission(Permissions.BALANCE_OTHER)) return suggestions; if (args.length == 2) { - if (sender.hasPermission(Permissions.BALANCE_OTHER)) suggestions.add("--player"); - if (sender instanceof OfflinePlayer player) { - final @NotNull Account @NotNull [] accounts = Account.get(player); - for (Account account : accounts) suggestions.add(account.id); + if (sender.hasPermission(Permissions.BALANCE_OTHER) || !(sender instanceof final @NotNull OfflinePlayer player)) { + suggestions.add("--player"); + if (args[1].startsWith("@")) + suggestions.addAll(sender.getServer().getOnlinePlayers().stream().map(p -> "@" + p.getName()).collect(Collectors.toSet())); + else { + final @NotNull Account @NotNull [] accounts = Account.get(); + for (final @NotNull Account account : accounts) suggestions.add(account.id); + } } else { - final @NotNull Account @NotNull [] accounts = Account.get(); - for (final @NotNull Account account : accounts) suggestions.add(account.id); + final @NotNull Account @NotNull [] accounts = Account.get(player); + for (Account account : accounts) suggestions.add(account.id); } } else if (args.length == 3 && args[1].equals("--player") && sender.hasPermission(Permissions.BALANCE_OTHER)) @@ -87,16 +91,27 @@ else if (args.length == 4 && args[2].equals("--player") && sender.hasPermission( case "setbal", "setbalance" -> { if (!sender.hasPermission(Permissions.SET_BALANCE)) return suggestions; if (args.length == 2) { - final @NotNull Account @NotNull [] accounts = Account.get(); - for (final @NotNull Account account : accounts) suggestions.add(account.id); + if (args[1].startsWith("@")) + suggestions.addAll(sender.getServer().getOnlinePlayers().stream().map(p -> "@" + p.getName()).collect(Collectors.toSet())); + else { + final @NotNull Account @NotNull [] accounts = Account.get(); + for (final @NotNull Account account : accounts) suggestions.add(account.id); + } } if (args.length == 3) suggestions.add("Infinity"); } case "setname", "rename" -> { if (!sender.hasPermission(Permissions.SET_NAME)) return suggestions; if (args.length == 2) { - final @NotNull Account @NotNull [] accounts = sender.hasPermission(Permissions.SET_NAME_OTHER) ? Account.get() : Account.get(BankAccounts.getOfflinePlayer(sender)); - for (final @NotNull Account account : accounts) suggestions.add(account.id); + if (args[1].startsWith("@") && sender.hasPermission(Permissions.SET_NAME_VAULT)) { + if (sender.hasPermission(Permissions.SET_NAME_OTHER) || !(sender instanceof final @NotNull OfflinePlayer player)) + suggestions.addAll(sender.getServer().getOnlinePlayers().stream().map(p -> "@" + p.getName()).collect(Collectors.toSet())); + else suggestions.add("@" + player.getName()); + } + else { + final @NotNull Account @NotNull [] accounts = sender.hasPermission(Permissions.SET_NAME_OTHER) ? Account.get() : Account.get(BankAccounts.getOfflinePlayer(sender)); + for (final @NotNull Account account : accounts) suggestions.add(account.id); + } } } case "freeze", "disable", "block", "unfreeze", "enable", "unblock" -> { @@ -107,35 +122,61 @@ else if (args.length == 4 && args[2].equals("--player") && sender.hasPermission( } case "delete" -> { if (!sender.hasPermission(Permissions.DELETE)) return suggestions; - if (args.length == 2) suggestions.addAll(Arrays - .stream(sender.hasPermission(Permissions.DELETE_OTHER) ? Account.get() : Account.get(BankAccounts.getOfflinePlayer(sender))) - .map(account -> account.id).collect(Collectors.toSet())); + if (args.length == 2) { + if (sender.hasPermission(Permissions.DELETE_OTHER) || !(sender instanceof final @NotNull OfflinePlayer player)) { + if (args[1].startsWith("@")) + suggestions.addAll(sender.getServer().getOnlinePlayers().stream().map(p -> "@" + p.getName()).collect(Collectors.toSet())); + else suggestions.addAll(Arrays.stream(Account.get()).map(account -> account.id).collect(Collectors.toSet())); + } + else + suggestions.addAll(Arrays.stream(Account.get(player)).map(account -> account.id).collect(Collectors.toSet())); + } } case "transfer", "send", "pay" -> { if (!sender.hasPermission(Permissions.TRANSFER_SELF) && !sender.hasPermission(Permissions.TRANSFER_OTHER)) return suggestions; if (args.length == 2) { - final @NotNull Account @NotNull [] accounts = Account.get(BankAccounts.getOfflinePlayer(sender)); - for (final @NotNull Account account : accounts) suggestions.add(account.id); + if (sender.hasPermission(Permissions.TRANSFER_FROM_OTHER) || !(sender instanceof final @NotNull OfflinePlayer player)) { + if (args[1].startsWith("@")) + suggestions.addAll(sender.getServer().getOnlinePlayers().stream().map(p -> "@" + p.getName()).collect(Collectors.toSet())); + else suggestions.addAll(Arrays.stream(Account.get()).map(account -> account.id).collect(Collectors.toSet())); + } + else + suggestions.addAll(Arrays.stream(Account.get(player)).map(account -> account.id).collect(Collectors.toSet())); + } + else if (args.length == 3) { + if (sender.hasPermission(Permissions.TRANSFER_OTHER) || !(sender instanceof final @NotNull OfflinePlayer player)) { + if (args[2].startsWith("@")) + suggestions.addAll(sender.getServer().getOnlinePlayers().stream().map(p -> "@" + p.getName()).collect(Collectors.toSet())); + else suggestions.addAll(Arrays.stream(Account.get()).map(account -> account.id).collect(Collectors.toSet())); + } + else + suggestions.addAll(Arrays.stream(Account.get(player)).map(account -> account.id).collect(Collectors.toSet())); } - else if (args.length == 3) suggestions.addAll(Arrays - .stream(sender.hasPermission(Permissions.TRANSFER_OTHER) ? Account.get() : Account.get(BankAccounts.getOfflinePlayer(sender))) - .filter(account -> !account.id.equals(args[1])).map(account -> account.id) - .collect(Collectors.toSet())); } case "transactions", "history" -> { if (!sender.hasPermission(Permissions.HISTORY)) return suggestions; if (args.length == 2) { - final @NotNull Account @NotNull [] accounts = sender.hasPermission(Permissions.HISTORY_OTHER) ? Account.get() : Account.get(BankAccounts.getOfflinePlayer(sender)); - for (final @NotNull Account account : accounts) suggestions.add(account.id); + if (sender.hasPermission(Permissions.HISTORY_OTHER) || !(sender instanceof final @NotNull OfflinePlayer player)) { + if (args[1].startsWith("@")) + suggestions.addAll(sender.getServer().getOnlinePlayers().stream().map(p -> "@" + p.getName()).collect(Collectors.toSet())); + else suggestions.addAll(Arrays.stream(Account.get()).map(account -> account.id).collect(Collectors.toSet())); + } + else + suggestions.addAll(Arrays.stream(Account.get(player)).map(account -> account.id).collect(Collectors.toSet())); } else if (args.length == 3) suggestions.add("--all"); } case "instrument", "card" -> { if (!sender.hasPermission(Permissions.INSTRUMENT_CREATE)) return suggestions; if (args.length == 2) { - final @NotNull Account @NotNull [] accounts = sender.hasPermission(Permissions.INSTRUMENT_CREATE_OTHER) ? Account.get() : Account.get(BankAccounts.getOfflinePlayer(sender)); - for (final @NotNull Account account : accounts) suggestions.add(account.id); + if (sender.hasPermission(Permissions.INSTRUMENT_CREATE_OTHER) || !(sender instanceof final @NotNull OfflinePlayer player)) { + if (args[1].startsWith("@")) + suggestions.addAll(sender.getServer().getOnlinePlayers().stream().map(p -> "@" + p.getName()).collect(Collectors.toSet())); + else suggestions.addAll(Arrays.stream(Account.get()).map(account -> account.id).collect(Collectors.toSet())); + } + else + suggestions.addAll(Arrays.stream(Account.get(player)).map(account -> account.id).collect(Collectors.toSet())); } else if (args.length == 3 && sender.hasPermission(Permissions.INSTRUMENT_CREATE_OTHER)) suggestions.addAll(BankAccounts.getInstance().getServer().getOnlinePlayers().stream() @@ -143,8 +184,11 @@ else if (args.length == 3 && sender.hasPermission(Permissions.INSTRUMENT_CREATE_ } case "whois", "who", "info" -> { if (!sender.hasPermission(Permissions.WHOIS)) return suggestions; - if (args.length == 2) - suggestions.addAll(Arrays.stream(Account.get()).map(account -> account.id).toList()); + if (args.length == 2) { + if (args[1].startsWith("@")) + suggestions.addAll(sender.getServer().getOnlinePlayers().stream().map(p -> "@" + p.getName()).collect(Collectors.toSet())); + else suggestions.addAll(Arrays.stream(Account.get()).map(account -> account.id).toList()); + } } case "baltop" -> { if (!sender.hasPermission(Permissions.BALTOP)) return suggestions; @@ -266,7 +310,9 @@ else if (args[0].equals("--player")) { else { if (!sender.hasPermission(Permissions.BALANCE_SELF)) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsNoPermission()); - final @NotNull Optional<@NotNull Account> account = Account.get(args[0]); + final @NotNull Optional<@NotNull Account> account; + if (!args[0].startsWith("@")) account = Account.get(args[0]); + else account = Account.getVaultAccount(sender.getServer().getOfflinePlayer(args[0].substring(1))); if (account.isEmpty()) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsAccountNotFound()); else if (!sender.hasPermission(Permissions.BALANCE_OTHER) && !account.get().owner.getUniqueId() .equals((BankAccounts.getOfflinePlayer(sender)).getUniqueId())) @@ -349,7 +395,9 @@ public static boolean setBalance(final @NotNull CommandSender sender, final @Not return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsNoPermission()); if (args.length < 2) return sendUsage(sender, label, "setbalance " + (args.length > 0 ? args[0] : "") + " "); - final @NotNull Optional<@NotNull Account> account = Account.get(args[0]); + final @NotNull Optional<@NotNull Account> account; + if (args[0].startsWith("@")) account = Account.getVaultAccount(sender.getServer().getOfflinePlayer(args[0].substring(1))); + else account = Account.get(args[0]); if (account.isEmpty()) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsAccountNotFound()); else { final @Nullable BigDecimal balance; @@ -373,7 +421,11 @@ public static boolean setName(final @NotNull CommandSender sender, final @NotNul return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsNoPermission()); if (args.length < 2) return sendUsage(sender, label, "setname " + (args.length > 0 ? args[0] : "") + " [name]"); - final @NotNull Optional<@NotNull Account> account = Account.get(args[0]); + /*final @NotNull Optional<@NotNull Account> account = Account.get(args[0]);*/ + final @NotNull Optional<@NotNull Account> account; + if (args[0].startsWith("@") && sender.hasPermission(Permissions.SET_NAME_VAULT)) + account = Account.getVaultAccount(sender.getServer().getOfflinePlayer(args[0].substring(1))); + else account = Account.get(args[0]); if (account.isEmpty()) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsAccountNotFound()); else { if (!sender.hasPermission(Permissions.SET_NAME_OTHER) && !account.get().owner.getUniqueId() @@ -398,7 +450,9 @@ public static boolean setName(final @NotNull CommandSender sender, final @NotNul public static boolean freeze(final @NotNull CommandSender sender, final @NotNull String @NotNull [] args, final @NotNull String label) { if (!sender.hasPermission(Permissions.FREEZE)) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsNoPermission()); if (args.length < 1) return sendUsage(sender, label, "freeze "); - final @NotNull Optional<@NotNull Account> account = Account.get(args[0]); + final @NotNull Optional<@NotNull Account> account; + if (args[0].startsWith("@")) account = Account.getVaultAccount(sender.getServer().getOfflinePlayer(args[0].substring(1))); + else account = Account.get(args[0]); if (account.isEmpty()) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsAccountNotFound()); if (!sender.hasPermission(Permissions.FREEZE_OTHER) && !account.get().owner.getUniqueId() .equals(BankAccounts.getOfflinePlayer(sender).getUniqueId())) @@ -413,7 +467,9 @@ public static boolean freeze(final @NotNull CommandSender sender, final @NotNull public static boolean unfreeze(final @NotNull CommandSender sender, final @NotNull String @NotNull [] args, final @NotNull String label) { if (!sender.hasPermission(Permissions.FREEZE)) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsNoPermission()); if (args.length < 1) return sendUsage(sender, label, "unfreeze "); - final @NotNull Optional<@NotNull Account> account = Account.get(args[0]); + final @NotNull Optional<@NotNull Account> account; + if (args[0].startsWith("@")) account = Account.getVaultAccount(sender.getServer().getOfflinePlayer(args[0].substring(1))); + else account = Account.get(args[0]); if (account.isEmpty()) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsAccountNotFound()); if (!sender.hasPermission(Permissions.FREEZE_OTHER) && !account.get().owner.getUniqueId() .equals(BankAccounts.getOfflinePlayer(sender).getUniqueId())) @@ -431,7 +487,9 @@ public static boolean unfreeze(final @NotNull CommandSender sender, final @NotNu public static boolean delete(final @NotNull CommandSender sender, final @NotNull String @NotNull [] args, final @NotNull String label) { if (!sender.hasPermission(Permissions.DELETE)) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsNoPermission()); if (args.length < 1) return sendUsage(sender, label, "delete "); - final @NotNull Optional<@NotNull Account> account = Account.get(args[0]); + final @NotNull Optional<@NotNull Account> account; + if (args[0].startsWith("@") && sender.hasPermission(Permissions.DELETE_VAULT)) account = Account.getVaultAccount(sender.getServer().getOfflinePlayer(args[0].substring(1))); + else account = Account.get(args[0]); if (account.isEmpty()) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsAccountNotFound()); if (!sender.hasPermission(Permissions.DELETE_OTHER) && !account.get().owner.getUniqueId() .equals(BankAccounts.getOfflinePlayer(sender).getUniqueId())) @@ -460,17 +518,21 @@ public static boolean transfer(final @NotNull CommandSender sender, final @NotNu if (confirm) argsCopy = Arrays.copyOfRange(argsCopy, 1, argsCopy.length); if (args.length < 3) return sendUsage(sender, label, "transfer " + (argsCopy.length > 0 ? argsCopy[0] : "") + " " + (argsCopy.length > 1 ? argsCopy[1] : "") + " [description]"); - final @NotNull Optional<@NotNull Account> from = Account.get(argsCopy[0]); + final @NotNull Optional<@NotNull Account> from; + if (argsCopy[0].startsWith("@")) from = Account.getVaultAccount(sender.getServer().getOfflinePlayer(argsCopy[0].substring(1))); + else from = Account.get(argsCopy[0]); // account does not exist if (from.isEmpty()) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsAccountNotFound()); // sender does not own account - if (!from.get().owner.getUniqueId().equals(BankAccounts.getOfflinePlayer(sender).getUniqueId())) + if (!sender.hasPermission(Permissions.TRANSFER_FROM_OTHER) && !from.get().owner.getUniqueId().equals(BankAccounts.getOfflinePlayer(sender).getUniqueId())) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsNotAccountOwner()); // account is frozen if (from.get().frozen) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsFrozen(from.get())); + final @NotNull Optional<@NotNull Account> to; + if (argsCopy[1].startsWith("@")) to = Account.getVaultAccount(sender.getServer().getOfflinePlayer(argsCopy[1].substring(1))); + else to = Account.get(argsCopy[1]); // recipient does not exist - final @NotNull Optional<@NotNull Account> to = Account.get(argsCopy[1]); if (to.isEmpty()) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsAccountNotFound()); // to is same as from if (from.get().id.equals(to.get().id)) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsSameFromTo()); @@ -536,7 +598,9 @@ public static boolean transfer(final @NotNull CommandSender sender, final @NotNu public static boolean transactions(final @NotNull CommandSender sender, final @NotNull String @NotNull [] args, final @NotNull String label) { if (!sender.hasPermission(Permissions.HISTORY)) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsNoPermission()); if (args.length < 1) return sendUsage(sender, label, "transactions [page=1|--all]"); - final @NotNull Optional<@NotNull Account> account = Account.get(args[0]); + final @NotNull Optional<@NotNull Account> account; + if (args[0].startsWith("@")) account = Account.getVaultAccount(sender.getServer().getOfflinePlayer(args[0].substring(1))); + else account = Account.get(args[0]); if (account.isEmpty()) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsAccountNotFound()); if (!sender.hasPermission(Permissions.HISTORY_OTHER) && !account.get().owner.getUniqueId() .equals(BankAccounts.getOfflinePlayer(sender).getUniqueId())) @@ -592,7 +656,9 @@ else if (args.length < 1) .getInstance().getServer().getPlayer(args[1]) : player; if (target == null || !target.isOnline()) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsPlayerNotFound()); - final @NotNull Optional<@NotNull Account> account = Account.get(args[0]); + final @NotNull Optional<@NotNull Account> account; + if (args[0].startsWith("@")) account = Account.getVaultAccount(sender.getServer().getOfflinePlayer(args[0].substring(1))); + else account = Account.get(args[0]); if (account.isEmpty()) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsAccountNotFound()); if (!sender.hasPermission(Permissions.INSTRUMENT_CREATE_OTHER) && !account.get().owner.getUniqueId() .equals(BankAccounts.getOfflinePlayer(sender).getUniqueId())) @@ -627,7 +693,9 @@ else if (args.length < 1) public static boolean whois(final @NotNull CommandSender sender, final @NotNull String @NotNull [] args, final @NotNull String label) { if (!sender.hasPermission(Permissions.WHOIS)) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsNoPermission()); if (args.length < 1) return sendUsage(sender, label, "whois "); - final @NotNull Optional<@NotNull Account> account = Account.get(args[0]); + final @NotNull Optional<@NotNull Account> account; + if (args[0].startsWith("@")) account = Account.getVaultAccount(sender.getServer().getOfflinePlayer(args[0].substring(1))); + else account = Account.get(args[0]); return account .map(value -> sendMessage(sender, BankAccounts.getInstance().config().messagesWhois(value))) .orElseGet(() -> sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsAccountNotFound())); diff --git a/src/main/java/pro/cloudnode/smp/bankaccounts/commands/POSCommand.java b/src/main/java/pro/cloudnode/smp/bankaccounts/commands/POSCommand.java index e313fff..f76a77f 100644 --- a/src/main/java/pro/cloudnode/smp/bankaccounts/commands/POSCommand.java +++ b/src/main/java/pro/cloudnode/smp/bankaccounts/commands/POSCommand.java @@ -40,7 +40,9 @@ public boolean execute(final @NotNull CommandSender sender, final @NotNull Strin if (args.length < 2) return sendUsage(sender, label, (args.length > 0 ? args[0] : "") + " [description]"); - final @NotNull Optional<@NotNull Account> account = Account.get(args[0]); + final @NotNull Optional<@NotNull Account> account; + if (!args[0].startsWith("@")) account = Account.get(args[0]); + else account = Account.getVaultAccount(sender.getServer().getOfflinePlayer(args[0].substring(1))); if (account.isEmpty()) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsAccountNotFound()); if (account.get().type == Account.Type.PERSONAL && !BankAccounts.getInstance().config().posAllowPersonal() && !player.hasPermission(Permissions.POS_CREATE_PERSONAL)) @@ -91,12 +93,18 @@ public boolean execute(final @NotNull CommandSender sender, final @NotNull Strin @Override public @NotNull ArrayList<@NotNull String> tab(final @NotNull CommandSender sender, final @NotNull String @NotNull [] args) { final @NotNull ArrayList<@NotNull String> suggestions = new ArrayList<>(); - if (sender.hasPermission(Permissions.POS_CREATE) && sender instanceof Player && args.length == 1) { - final @NotNull Account[] accounts = sender.hasPermission(Permissions.POS_CREATE_OTHER) ? Account.get() : Account.get(BankAccounts.getOfflinePlayer(sender)); - for (final @NotNull Account account : accounts) { - if (account.frozen || (account.type == Account.Type.PERSONAL && !BankAccounts.getInstance().config().posAllowPersonal() && !sender.hasPermission(Permissions.POS_CREATE_PERSONAL))) - continue; - suggestions.add(account.id); + if (sender.hasPermission(Permissions.POS_CREATE) && sender instanceof final @NotNull Player player && args.length == 1) { + if (args[0].startsWith("@")) { + if (sender.hasPermission(Permissions.POS_CREATE_OTHER)) + suggestions.addAll(sender.getServer().getOnlinePlayers().stream().map(p -> "@" + p.getName()).toList()); + else suggestions.add("@" + sender.getName()); + } + else { + final @NotNull Account @NotNull [] accounts; + if (sender.hasPermission(Permissions.POS_CREATE_OTHER)) + accounts = Account.get(); + else accounts = Account.get(BankAccounts.getOfflinePlayer(sender)); + suggestions.addAll(Arrays.stream(accounts).filter(account -> !account.frozen && (account.type != Account.Type.PERSONAL || BankAccounts.getInstance().config().posAllowPersonal() || sender.hasPermission(Permissions.POS_CREATE_PERSONAL))).map(a -> a.id).toList()); } } return suggestions;