From 8e188d9ec717e289ef95f9f5efc622de86b3b1b0 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Mon, 28 Oct 2024 18:19:35 +0700 Subject: [PATCH] Refactor FilterPlugin's `getPlayer`, hopefully it works the same --- .../chomens_bot/plugins/FilterPlugin.java | 67 +++++++++---------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/FilterPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/FilterPlugin.java index 0c1ce20..ae8d1ce 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/FilterPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/FilterPlugin.java @@ -55,46 +55,43 @@ public class FilterPlugin extends PlayersPlugin.Listener { } private FilteredPlayer getPlayer (String name) { - // mess - // also regex and ignorecase codes from greplog plugin - FilteredPlayer filteredPlayer = null; for (JsonElement filteredPlayerElement : filteredPlayers) { - final FilteredPlayer _filteredPlayer = gson.fromJson(filteredPlayerElement, FilteredPlayer.class); + final FilteredPlayer filteredPlayer = gson.fromJson(filteredPlayerElement, FilteredPlayer.class); - if (_filteredPlayer.regex) { - Pattern pattern = null; - if (_filteredPlayer.ignoreCase) { - pattern = Pattern.compile(_filteredPlayer.playerName, Pattern.CASE_INSENSITIVE); - } else { - try { - pattern = Pattern.compile(_filteredPlayer.playerName); - } catch (Exception e) { - bot.chat.tellraw(Component.text(e.toString()).color(NamedTextColor.RED)); - } - } - - if (pattern == null) break; - - if (pattern.matcher(name).find()) { - filteredPlayer = _filteredPlayer; - break; - } - } else { - if (_filteredPlayer.ignoreCase) { - if (_filteredPlayer.playerName.toLowerCase().equals(name)) { - filteredPlayer = _filteredPlayer; - break; - } - } else { - if (_filteredPlayer.playerName.equals(name)) { - filteredPlayer = _filteredPlayer; - break; - } - } + if (matchesPlayer(name, filteredPlayer)) { + return filteredPlayer; } } - return filteredPlayer; + return null; + } + + private boolean matchesPlayer (String name, FilteredPlayer player) { + if (player.regex) { + final Pattern pattern = compilePattern(player); + + return pattern != null && pattern.matcher(name).find(); + } else { + return compareNames(name, player); + } + } + + private Pattern compilePattern (FilteredPlayer player) { + try { + final int flags = player.ignoreCase ? Pattern.CASE_INSENSITIVE : 0; + + return Pattern.compile(player.playerName, flags); + } catch (Exception e) { + bot.chat.tellraw(Component.text(e.toString()).color(NamedTextColor.RED)); + return null; + } + } + + private boolean compareNames (String name, FilteredPlayer player) { + final String playerName = player.ignoreCase ? player.playerName.toLowerCase() : player.playerName; + final String targetName = player.ignoreCase ? name.toLowerCase() : name; + + return playerName.equals(targetName); } @Override