Skip to content

Commit

Permalink
Add ability to clear history of other accounts that one has access to
Browse files Browse the repository at this point in the history
Also remove some leftover references to the "read" sub command.
  • Loading branch information
Phoenix616 committed Apr 26, 2021
1 parent 6bc0b08 commit f705683
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
10 changes: 4 additions & 6 deletions src/main/java/com/wfector/command/Clear.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
44 changes: 41 additions & 3 deletions src/main/java/com/wfector/command/CommandRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -219,9 +257,9 @@ public boolean onCommand(final CommandSender sender, Command cmd, String label,
@Override
public List<String> 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);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/wfector/command/Help.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ messages:
header: "&dChestShop Notifier // &7Commands"
help: "&7 /csn &dhelp &f- Plugin usage & commands"
history: "&7 /csn &dhistory [<page>]&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 <username> [<page>]&f- View sales of another player"
history-others: "&7 /csn &dhistory <username> [<page>]&f- View sales of another account"
clear-others: "&7 /csn &dclear <username>&f- Remove read sales of another account"
cleandatabase: "&7 /csn &ccleandatabase &f- Remove database entries. Parameters:"
cleandatabase-older-than: "&c --older-than, -o <days> &f- Removes entries older than <days>"
cleandatabase-user: "&c --user, -user <username/uuid> &f- Removes entries from a single user only"
Expand Down

0 comments on commit f705683

Please sign in to comment.