|
| 1 | +package me.wethink.weGuardian.listeners; |
| 2 | + |
| 3 | +import com.tcoded.folialib.FoliaLib; |
| 4 | +import me.wethink.weGuardian.WeGuardian; |
| 5 | +import me.wethink.weGuardian.database.DatabaseManager; |
| 6 | +import me.wethink.weGuardian.models.PlayerData; |
| 7 | +import me.wethink.weGuardian.models.PunishmentTemplate; |
| 8 | +import me.wethink.weGuardian.services.TemplateService; |
| 9 | +import org.bukkit.Bukkit; |
| 10 | +import org.bukkit.command.CommandSender; |
| 11 | +import org.bukkit.entity.Player; |
| 12 | +import org.bukkit.event.EventHandler; |
| 13 | +import org.bukkit.event.EventPriority; |
| 14 | +import org.bukkit.event.Listener; |
| 15 | +import org.bukkit.event.server.TabCompleteEvent; |
| 16 | + |
| 17 | +import java.util.*; |
| 18 | +import java.util.concurrent.ConcurrentHashMap; |
| 19 | +import java.util.stream.Collectors; |
| 20 | + |
| 21 | +public class AsyncSuggestionsListener implements Listener { |
| 22 | + |
| 23 | + private final WeGuardian plugin; |
| 24 | + private final DatabaseManager databaseManager; |
| 25 | + private final TemplateService templateService; |
| 26 | + private final FoliaLib foliaLib; |
| 27 | + |
| 28 | + private final Map<String, List<String>> playerSuggestionsCache = new ConcurrentHashMap<>(); |
| 29 | + private final Map<String, Long> cacheTimestamps = new ConcurrentHashMap<>(); |
| 30 | + private static final long CACHE_DURATION = 30000; |
| 31 | + |
| 32 | + private final Map<String, List<String>> templateSuggestionsCache = new ConcurrentHashMap<>(); |
| 33 | + private long lastTemplateCacheUpdate = 0; |
| 34 | + private static final long TEMPLATE_CACHE_DURATION = 60000; |
| 35 | + |
| 36 | + public AsyncSuggestionsListener(WeGuardian plugin, DatabaseManager databaseManager, TemplateService templateService) { |
| 37 | + this.plugin = plugin; |
| 38 | + this.databaseManager = databaseManager; |
| 39 | + this.templateService = templateService; |
| 40 | + this.foliaLib = plugin.getFoliaLib(); |
| 41 | + } |
| 42 | + |
| 43 | + @EventHandler(priority = EventPriority.HIGHEST) |
| 44 | + public void onTabComplete(TabCompleteEvent event) { |
| 45 | + String command = event.getBuffer().toLowerCase().trim(); |
| 46 | + String[] args = command.split("\\s+"); |
| 47 | + |
| 48 | + if (args.length < 2) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + String commandName = args[0].substring(1); |
| 53 | + String currentArg = args[args.length - 1].toLowerCase(); |
| 54 | + |
| 55 | + if (isWeGuardianCommand(commandName)) { |
| 56 | + event.getCompletions().clear(); |
| 57 | + |
| 58 | + foliaLib.getScheduler().runAsync(task -> { |
| 59 | + try { |
| 60 | + List<String> suggestions = generateSuggestions(event.getSender(), commandName, args, currentArg); |
| 61 | + if (!suggestions.isEmpty()) { |
| 62 | + foliaLib.getScheduler().runNextTick(task2 -> { |
| 63 | + event.getCompletions().addAll(suggestions); |
| 64 | + }); |
| 65 | + } |
| 66 | + } catch (Exception e) { |
| 67 | + plugin.getLogger().warning("Error generating async suggestions: " + e.getMessage()); |
| 68 | + } |
| 69 | + }); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private boolean isWeGuardianCommand(String commandName) { |
| 74 | + return Arrays.asList( |
| 75 | + "ban", "tempban", "mute", "tempmute", "kick", "warn", |
| 76 | + "unban", "unmute", "ipban", "ipmute", "history", "checkban", |
| 77 | + "banlist", "mutelist", "blame", "alts", "warns", "unwarn", |
| 78 | + "banmenu", "tempbanmenu", "mutemenu", "tempmutemenu", |
| 79 | + "kickmenu", "warnmenu", "notesmenu", "unbanmenu", "unmutemenu", |
| 80 | + "punish", "weguardian" |
| 81 | + ).contains(commandName); |
| 82 | + } |
| 83 | + |
| 84 | + private List<String> generateSuggestions(CommandSender sender, String commandName, String[] args, String currentArg) { |
| 85 | + List<String> suggestions = new ArrayList<>(); |
| 86 | + |
| 87 | + switch (commandName) { |
| 88 | + case "ban", "tempban", "mute", "tempmute", "kick", "warn", "unban", "unmute", "history", "checkban", "blame", "alts", "warns", "unwarn", "punish" -> { |
| 89 | + if (args.length == 2) { |
| 90 | + suggestions.addAll(getPlayerSuggestions(currentArg, sender)); |
| 91 | + } else if (args.length > 2) { |
| 92 | + suggestions.addAll(getAdvancedSuggestions(sender, commandName, args, currentArg)); |
| 93 | + } |
| 94 | + } |
| 95 | + case "ipban", "ipmute" -> { |
| 96 | + if (args.length == 2) { |
| 97 | + suggestions.addAll(getIPSuggestions(currentArg)); |
| 98 | + } else if (args.length > 2) { |
| 99 | + suggestions.addAll(getReasonSuggestions(currentArg)); |
| 100 | + } |
| 101 | + } |
| 102 | + case "banlist", "mutelist" -> { |
| 103 | + if (args.length == 2) { |
| 104 | + suggestions.addAll(getPaginationSuggestions(currentArg)); |
| 105 | + } |
| 106 | + } |
| 107 | + case "weguardian" -> { |
| 108 | + if (args.length == 2) { |
| 109 | + suggestions.addAll(getMainCommandSuggestions(currentArg)); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return suggestions.stream() |
| 115 | + .filter(suggestion -> suggestion.toLowerCase().startsWith(currentArg)) |
| 116 | + .limit(20) |
| 117 | + .collect(Collectors.toList()); |
| 118 | + } |
| 119 | + |
| 120 | + private List<String> getPlayerSuggestions(String currentArg, CommandSender sender) { |
| 121 | + String cacheKey = "players_" + currentArg; |
| 122 | + |
| 123 | + if (isCacheValid(cacheKey)) { |
| 124 | + List<String> cached = playerSuggestionsCache.get(cacheKey); |
| 125 | + if (cached != null) { |
| 126 | + return cached; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + List<String> suggestions = new ArrayList<>(); |
| 131 | + |
| 132 | + for (Player onlinePlayer : Bukkit.getOnlinePlayers()) { |
| 133 | + if (onlinePlayer.getName().toLowerCase().startsWith(currentArg)) { |
| 134 | + suggestions.add(onlinePlayer.getName()); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if (suggestions.size() < 10) { |
| 139 | + try { |
| 140 | + List<PlayerData> offlinePlayers = databaseManager.searchPlayersByName(currentArg).join(); |
| 141 | + for (PlayerData playerData : offlinePlayers) { |
| 142 | + if (playerData.getName().toLowerCase().startsWith(currentArg)) { |
| 143 | + suggestions.add(playerData.getName()); |
| 144 | + } |
| 145 | + } |
| 146 | + } catch (Exception e) { |
| 147 | + plugin.debug("Error fetching offline players for suggestions: %s", e.getMessage()); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + playerSuggestionsCache.put(cacheKey, suggestions); |
| 152 | + cacheTimestamps.put(cacheKey, System.currentTimeMillis()); |
| 153 | + |
| 154 | + return suggestions; |
| 155 | + } |
| 156 | + |
| 157 | + private List<String> getAdvancedSuggestions(CommandSender sender, String commandName, String[] args, String currentArg) { |
| 158 | + List<String> suggestions = new ArrayList<>(); |
| 159 | + |
| 160 | + if (args.length > 3 && args[args.length - 2].equals("-t")) { |
| 161 | + return getTemplateSuggestions(currentArg); |
| 162 | + } |
| 163 | + |
| 164 | + if (args.length > 3 && args[args.length - 2].equals("-d")) { |
| 165 | + return getDurationSuggestions(); |
| 166 | + } |
| 167 | + if (!Arrays.asList(args).contains("-s")) { |
| 168 | + suggestions.add("-s"); |
| 169 | + } |
| 170 | + if (!Arrays.asList(args).contains("-t")) { |
| 171 | + suggestions.add("-t"); |
| 172 | + } |
| 173 | + if (!Arrays.asList(args).contains("-d") && (commandName.equals("ban") || commandName.equals("mute"))) { |
| 174 | + suggestions.add("-d"); |
| 175 | + } |
| 176 | + if (!Arrays.asList(args).contains("--ip") && hasIPPermission(sender, commandName)) { |
| 177 | + suggestions.add("--ip"); |
| 178 | + } |
| 179 | + |
| 180 | + suggestions.addAll(getReasonSuggestions(currentArg)); |
| 181 | + |
| 182 | + return suggestions; |
| 183 | + } |
| 184 | + |
| 185 | + private List<String> getTemplateSuggestions(String currentArg) { |
| 186 | + if (System.currentTimeMillis() - lastTemplateCacheUpdate > TEMPLATE_CACHE_DURATION) { |
| 187 | + updateTemplateCache(); |
| 188 | + } |
| 189 | + |
| 190 | + return templateSuggestionsCache.getOrDefault("all", new ArrayList<>()) |
| 191 | + .stream() |
| 192 | + .filter(template -> template.toLowerCase().startsWith(currentArg)) |
| 193 | + .collect(Collectors.toList()); |
| 194 | + } |
| 195 | + |
| 196 | + private void updateTemplateCache() { |
| 197 | + try { |
| 198 | + Collection<PunishmentTemplate> templates = templateService.getAllTemplates(); |
| 199 | + List<String> templateNames = templates.stream() |
| 200 | + .map(PunishmentTemplate::getName) |
| 201 | + .collect(Collectors.toList()); |
| 202 | + |
| 203 | + templateSuggestionsCache.put("all", templateNames); |
| 204 | + lastTemplateCacheUpdate = System.currentTimeMillis(); |
| 205 | + } catch (Exception e) { |
| 206 | + plugin.debug("Error updating template cache: %s", e.getMessage()); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + private List<String> getDurationSuggestions() { |
| 211 | + return Arrays.asList("1h", "2h", "6h", "12h", "1d", "2d", "3d", "7d", "14d", "30d", "1w", "2w", "1m", "3m", "6m", "1y"); |
| 212 | + } |
| 213 | + |
| 214 | + private List<String> getReasonSuggestions(String currentArg) { |
| 215 | + return Arrays.asList( |
| 216 | + "Hacking", "Griefing", "Toxic behavior", "Spam", "Advertising", |
| 217 | + "Inappropriate language", "Cheating", "Rule violation", "X-ray", |
| 218 | + "Duping", "Exploiting", "Alt account", "Disrespect", "Harassment", |
| 219 | + "Chat abuse", "Excessive caps", "Flooding", "Political discussion" |
| 220 | + ); |
| 221 | + } |
| 222 | + |
| 223 | + private List<String> getIPSuggestions(String currentArg) { |
| 224 | + if (currentArg.matches("^\\d{1,3}$")) { |
| 225 | + return Arrays.asList(currentArg + ".", currentArg + "0", currentArg + "1", currentArg + "2"); |
| 226 | + } else if (currentArg.matches("^\\d{1,3}\\.\\d{1,3}$")) { |
| 227 | + return Arrays.asList(currentArg + ".", currentArg + "0", currentArg + "1", currentArg + "2"); |
| 228 | + } else if (currentArg.matches("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$")) { |
| 229 | + return Arrays.asList(currentArg + ".", currentArg + "0", currentArg + "1", currentArg + "2"); |
| 230 | + } |
| 231 | + return new ArrayList<>(); |
| 232 | + } |
| 233 | + |
| 234 | + private List<String> getPaginationSuggestions(String currentArg) { |
| 235 | + return Arrays.asList("1", "2", "3", "4", "5", "10", "20", "50", "100"); |
| 236 | + } |
| 237 | + |
| 238 | + private List<String> getMainCommandSuggestions(String currentArg) { |
| 239 | + return Arrays.asList("reload", "about", "version", "help"); |
| 240 | + } |
| 241 | + |
| 242 | + private boolean hasIPPermission(CommandSender sender, String commandName) { |
| 243 | + return switch (commandName) { |
| 244 | + case "mute", "tempmute" -> sender.hasPermission("weguardian.ipmute"); |
| 245 | + case "kick" -> sender.hasPermission("weguardian.ipkick"); |
| 246 | + default -> false; |
| 247 | + }; |
| 248 | + } |
| 249 | + |
| 250 | + private boolean isCacheValid(String cacheKey) { |
| 251 | + Long timestamp = cacheTimestamps.get(cacheKey); |
| 252 | + return timestamp != null && (System.currentTimeMillis() - timestamp) < CACHE_DURATION; |
| 253 | + } |
| 254 | + |
| 255 | + |
| 256 | + public void clearCache() { |
| 257 | + playerSuggestionsCache.clear(); |
| 258 | + templateSuggestionsCache.clear(); |
| 259 | + cacheTimestamps.clear(); |
| 260 | + lastTemplateCacheUpdate = 0; |
| 261 | + } |
| 262 | +} |
0 commit comments