From f705683209ce1a111f3ede8f364666f4c278f00e Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Mon, 26 Apr 2021 13:33:08 +0100 Subject: [PATCH] Add ability to clear history of other accounts that one has access to Also remove some leftover references to the "read" sub command. --- src/main/java/com/wfector/command/Clear.java | 10 ++--- .../com/wfector/command/CommandRunner.java | 44 +++++++++++++++++-- src/main/java/com/wfector/command/Help.java | 1 + src/main/resources/config.yml | 4 +- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/wfector/command/Clear.java b/src/main/java/com/wfector/command/Clear.java index 430c10c..a17093f 100644 --- a/src/main/java/com/wfector/command/Clear.java +++ b/src/main/java/com/wfector/command/Clear.java @@ -5,28 +5,26 @@ import java.sql.Statement; import java.util.UUID; -import com.Acrobot.ChestShop.Configuration.Properties; -import com.Acrobot.ChestShop.UUIDs.NameManager; import com.wfector.notifier.ChestShopNotifier; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; public class Clear extends BukkitRunnable { private final ChestShopNotifier plugin; private final CommandSender sender; + private final UUID userId; - public Clear(ChestShopNotifier plugin, CommandSender sender) { + public Clear(ChestShopNotifier plugin, CommandSender sender, UUID userId) { this.plugin = plugin; this.sender = sender; + this.userId = userId; } public void run() { - UUID senderId = (sender instanceof Player) ? ((Player) sender).getUniqueId() : NameManager.getAccount(Properties.ADMIN_SHOP_NAME).getUuid(); try (Connection c = plugin.getConnection()){ Statement statement = c.createStatement(); - statement.executeUpdate("DELETE FROM csnUUID WHERE `Unread`='1' AND `ShopOwnerId`='" + senderId.toString() + "'"); + statement.executeUpdate("DELETE FROM csnUUID WHERE `Unread`='1' AND `ShopOwnerId`='" + userId.toString() + "'"); sender.sendMessage(plugin.getMessage("history-clear")); } catch (SQLException e) { diff --git a/src/main/java/com/wfector/command/CommandRunner.java b/src/main/java/com/wfector/command/CommandRunner.java index 2570f52..9a589e1 100644 --- a/src/main/java/com/wfector/command/CommandRunner.java +++ b/src/main/java/com/wfector/command/CommandRunner.java @@ -206,7 +206,45 @@ public boolean onCommand(final CommandSender sender, Command cmd, String label, return true; } - new Clear(plugin, sender).runTaskAsynchronously(plugin); + UUID userId; + if (sender instanceof Player) { + userId = ((Player) sender).getUniqueId(); + } else { + userId = NameManager.getAccount(Properties.ADMIN_SHOP_NAME).getUuid(); + } + if(args.length > 1 && sender.hasPermission("csn.command.history.others")) { + if (!sender.hasPermission("csn.command.history.others")) { + sender.sendMessage(plugin.getMessage("missing-permission", "permission", "csn.command.history.others")); + return true; + } + StringBuilder userNameBuilder = new StringBuilder(args[1]); + for (int i = 2; i < args.length; i++) { + userNameBuilder.append(" ").append(args[i]); + } + String userName = userNameBuilder.toString(); + AccountQueryEvent queryEvent = new AccountQueryEvent(userName); + plugin.getServer().getPluginManager().callEvent(queryEvent); + Account target = queryEvent.getAccount(); + if (target == null) { + target = NameManager.getAccount(userName); + } + if (target == null) { + sender.sendMessage(plugin.getMessage("user-not-found", "player", userName)); + return true; + } + if (sender instanceof Player + && !sender.hasPermission("csn.command.history.other." + userName)) { + AccountAccessEvent event = new AccountAccessEvent((Player) sender, target); + ChestShop.callEvent(event); + if (!event.canAccess()) { + sender.sendMessage(plugin.getMessage("history-others-not-allowed", "username", userName)); + return true; + } + } + userId = target.getUuid(); + } + + new Clear(plugin, sender, userId).runTaskAsynchronously(plugin); return true; } @@ -219,9 +257,9 @@ public boolean onCommand(final CommandSender sender, Command cmd, String label, @Override public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { if (args.length == 0) { - return Arrays.asList("history", "read", "clear"); + return Arrays.asList("history", "clear"); } else if (args.length == 1) { - for (String s : new String[]{"history", "read", "clear"}) { + for (String s : new String[]{"history", "clear"}) { if (s.toLowerCase().startsWith(args[0].toLowerCase()) && s.length() > args[0].length()) { return Collections.singletonList(s); } diff --git a/src/main/java/com/wfector/command/Help.java b/src/main/java/com/wfector/command/Help.java index 498b434..3cccf84 100644 --- a/src/main/java/com/wfector/command/Help.java +++ b/src/main/java/com/wfector/command/Help.java @@ -28,6 +28,7 @@ public void SendDialog(CommandSender sender) { } if(sender.hasPermission("csn.command.history.others")) { helpItems.add(plugin.getMessage("help.history-others")); + helpItems.add(plugin.getMessage("help.clear-others")); } if(sender.hasPermission("csn.command.cleandatabase")) { helpItems.add(plugin.getMessage("help.cleandatabase")); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 56c1865..0081de4 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -68,9 +68,9 @@ messages: header: "&dChestShop Notifier // &7Commands" help: "&7 /csn &dhelp &f- Plugin usage & commands" history: "&7 /csn &dhistory []&f- View sales" - read: "&7 /csn &dread &f- Mark all sales as read" clear: "&7 /csn &dclear &f- Remove read sales" - history-others: "&7 /csn &dhistory []&f- View sales of another player" + history-others: "&7 /csn &dhistory []&f- View sales of another account" + clear-others: "&7 /csn &dclear &f- Remove read sales of another account" cleandatabase: "&7 /csn &ccleandatabase &f- Remove database entries. Parameters:" cleandatabase-older-than: "&c --older-than, -o &f- Removes entries older than " cleandatabase-user: "&c --user, -user &f- Removes entries from a single user only"