|
| 1 | +package dev.pgm.community.audit; |
| 2 | + |
| 3 | +import static net.kyori.adventure.text.Component.newline; |
| 4 | +import static net.kyori.adventure.text.Component.space; |
| 5 | +import static net.kyori.adventure.text.Component.text; |
| 6 | +import static tc.oc.pgm.util.player.PlayerComponent.player; |
| 7 | + |
| 8 | +import dev.pgm.community.feature.FeatureBase; |
| 9 | +import dev.pgm.community.utils.BroadcastUtils; |
| 10 | +import java.util.List; |
| 11 | +import java.util.logging.Logger; |
| 12 | +import net.kyori.adventure.text.Component; |
| 13 | +import net.kyori.adventure.text.TextComponent; |
| 14 | +import net.kyori.adventure.text.event.ClickEvent; |
| 15 | +import net.kyori.adventure.text.event.HoverEvent; |
| 16 | +import net.kyori.adventure.text.format.NamedTextColor; |
| 17 | +import org.bukkit.Bukkit; |
| 18 | +import org.bukkit.command.Command; |
| 19 | +import org.bukkit.command.CommandMap; |
| 20 | +import org.bukkit.command.PluginCommand; |
| 21 | +import org.bukkit.configuration.Configuration; |
| 22 | +import org.bukkit.entity.Player; |
| 23 | +import org.bukkit.event.EventHandler; |
| 24 | +import org.bukkit.event.EventPriority; |
| 25 | +import org.bukkit.event.player.PlayerCommandPreprocessEvent; |
| 26 | +import tc.oc.pgm.util.named.NameStyle; |
| 27 | + |
| 28 | +public class CommandAuditFeature extends FeatureBase { |
| 29 | + |
| 30 | + public CommandAuditFeature(Configuration config, Logger logger) { |
| 31 | + super(new CommandAuditConfig(config), logger, "Staff Command Audit"); |
| 32 | + |
| 33 | + if (getConfig().isEnabled()) { |
| 34 | + enable(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) |
| 39 | + public void onPlayerCommand(PlayerCommandPreprocessEvent event) { |
| 40 | + Player player = event.getPlayer(); |
| 41 | + if (!shouldAudit(player)) return; |
| 42 | + |
| 43 | + String command = event.getMessage(); |
| 44 | + if (!shouldLog(command)) return; |
| 45 | + |
| 46 | + Component alert = buildAlert(player, command); |
| 47 | + BroadcastUtils.sendAdminChatMessage(alert, null, null, null); |
| 48 | + } |
| 49 | + |
| 50 | + private boolean shouldAudit(Player player) { |
| 51 | + CommandAuditConfig config = getCommandAuditConfig(); |
| 52 | + if (hasAnyPermission(player, config.getExemptPermissions())) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + List<String> auditPermissions = config.getAuditPermissions(); |
| 56 | + return auditPermissions.isEmpty() || hasAnyPermission(player, auditPermissions); |
| 57 | + } |
| 58 | + |
| 59 | + private boolean shouldLog(String command) { |
| 60 | + if (command == null) return false; |
| 61 | + String normalized = command.trim().toLowerCase(); |
| 62 | + if (normalized.isEmpty()) return false; |
| 63 | + |
| 64 | + CommandAuditConfig config = getCommandAuditConfig(); |
| 65 | + if (matchesAny(normalized, config.getExcludeCommands())) return false; |
| 66 | + if (matchesAny(normalized, config.getIncludePrefixes())) return true; |
| 67 | + if (matchesAny(normalized, config.getIncludeCommands())) return true; |
| 68 | + return matchesCommandPermission(normalized, config.getIncludePermissionContains()); |
| 69 | + } |
| 70 | + |
| 71 | + private boolean matchesCommandPermission(String command, List<String> permissionContains) { |
| 72 | + if (permissionContains.isEmpty()) return false; |
| 73 | + String label = extractCommandLabel(command); |
| 74 | + if (label.isEmpty()) return false; |
| 75 | + Command cmd = getCommand(label); |
| 76 | + if (cmd == null || cmd.getPermission() == null) return false; |
| 77 | + |
| 78 | + String permission = cmd.getPermission().toLowerCase(); |
| 79 | + for (String matcher : permissionContains) { |
| 80 | + if (permission.contains(matcher)) { |
| 81 | + return true; |
| 82 | + } |
| 83 | + } |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + private Command getCommand(String label) { |
| 88 | + PluginCommand pluginCommand = Bukkit.getPluginCommand(label); |
| 89 | + if (pluginCommand != null) { |
| 90 | + return pluginCommand; |
| 91 | + } |
| 92 | + |
| 93 | + try { |
| 94 | + CommandMap commandMap = Bukkit.getServer().getCommandMap(); |
| 95 | + if (commandMap != null) { |
| 96 | + return commandMap.getCommand(label); |
| 97 | + } |
| 98 | + } catch (NoSuchMethodError ignored) { |
| 99 | + |
| 100 | + } |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + private String extractCommandLabel(String command) { |
| 105 | + String[] parts = command.split(" "); |
| 106 | + if (parts.length == 0) return ""; |
| 107 | + String label = parts[0]; |
| 108 | + if (label.startsWith("/")) { |
| 109 | + label = label.substring(1); |
| 110 | + } |
| 111 | + int namespaceIndex = label.indexOf(':'); |
| 112 | + if (namespaceIndex >= 0 && namespaceIndex + 1 < label.length()) { |
| 113 | + label = label.substring(namespaceIndex + 1); |
| 114 | + } |
| 115 | + return label; |
| 116 | + } |
| 117 | + |
| 118 | + private boolean matchesAny(String command, List<String> values) { |
| 119 | + for (String value : values) { |
| 120 | + if (command.startsWith(value)) return true; |
| 121 | + } |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + private boolean hasAnyPermission(Player player, List<String> permissions) { |
| 126 | + for (String permission : permissions) { |
| 127 | + if (player.hasPermission(permission)) { |
| 128 | + return true; |
| 129 | + } |
| 130 | + } |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + private Component buildAlert(Player player, String command) { |
| 135 | + TextComponent.Builder builder = text() |
| 136 | + .append(player(player, NameStyle.FANCY)) |
| 137 | + .append(text(" executed ", NamedTextColor.GRAY)) |
| 138 | + .append(text(command, NamedTextColor.LIGHT_PURPLE)); |
| 139 | + |
| 140 | + if (getCommandAuditConfig().isClickTeleportEnabled()) { |
| 141 | + builder.hoverEvent(HoverEvent.showText(DEFAULT_HOVER)); |
| 142 | + builder.append(space()).append(buildViewComponent(player)); |
| 143 | + } else { |
| 144 | + builder.hoverEvent(HoverEvent.showText(DEFAULT_HOVER)); |
| 145 | + } |
| 146 | + |
| 147 | + return builder.build(); |
| 148 | + } |
| 149 | + |
| 150 | + private static final Component DEFAULT_HOVER = text( |
| 151 | + "You've been alerted to this action", NamedTextColor.GRAY) |
| 152 | + .append(newline()) |
| 153 | + .append(text("in order to promote transparency.", NamedTextColor.GRAY)); |
| 154 | + |
| 155 | + private Component buildViewComponent(Player player) { |
| 156 | + return text() |
| 157 | + .append(text("[", NamedTextColor.GRAY)) |
| 158 | + .append(text("View", NamedTextColor.AQUA)) |
| 159 | + .append(text("]", NamedTextColor.GRAY)) |
| 160 | + .clickEvent(ClickEvent.runCommand(getTeleportCommand(player))) |
| 161 | + .hoverEvent(HoverEvent.showText(text("Click to teleport", NamedTextColor.GRAY))) |
| 162 | + .build(); |
| 163 | + } |
| 164 | + |
| 165 | + private String getTeleportCommand(Player player) { |
| 166 | + return String.format( |
| 167 | + "/tploc %d %d %d", |
| 168 | + player.getLocation().getBlockX(), |
| 169 | + player.getLocation().getBlockY(), |
| 170 | + player.getLocation().getBlockZ()); |
| 171 | + } |
| 172 | + |
| 173 | + public CommandAuditConfig getCommandAuditConfig() { |
| 174 | + return (CommandAuditConfig) getConfig(); |
| 175 | + } |
| 176 | +} |
0 commit comments