Refactor FilterPlugin's getPlayer
, hopefully it works the same
This commit is contained in:
parent
43d12f2471
commit
8e188d9ec7
1 changed files with 32 additions and 35 deletions
|
@ -55,46 +55,43 @@ public class FilterPlugin extends PlayersPlugin.Listener {
|
||||||
}
|
}
|
||||||
|
|
||||||
private FilteredPlayer getPlayer (String name) {
|
private FilteredPlayer getPlayer (String name) {
|
||||||
// mess
|
|
||||||
// also regex and ignorecase codes from greplog plugin
|
|
||||||
FilteredPlayer filteredPlayer = null;
|
|
||||||
for (JsonElement filteredPlayerElement : filteredPlayers) {
|
for (JsonElement filteredPlayerElement : filteredPlayers) {
|
||||||
final FilteredPlayer _filteredPlayer = gson.fromJson(filteredPlayerElement, FilteredPlayer.class);
|
final FilteredPlayer filteredPlayer = gson.fromJson(filteredPlayerElement, FilteredPlayer.class);
|
||||||
|
|
||||||
if (_filteredPlayer.regex) {
|
if (matchesPlayer(name, filteredPlayer)) {
|
||||||
Pattern pattern = null;
|
return filteredPlayer;
|
||||||
if (_filteredPlayer.ignoreCase) {
|
}
|
||||||
pattern = Pattern.compile(_filteredPlayer.playerName, Pattern.CASE_INSENSITIVE);
|
}
|
||||||
|
|
||||||
|
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 {
|
} else {
|
||||||
|
return compareNames(name, player);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Pattern compilePattern (FilteredPlayer player) {
|
||||||
try {
|
try {
|
||||||
pattern = Pattern.compile(_filteredPlayer.playerName);
|
final int flags = player.ignoreCase ? Pattern.CASE_INSENSITIVE : 0;
|
||||||
|
|
||||||
|
return Pattern.compile(player.playerName, flags);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
bot.chat.tellraw(Component.text(e.toString()).color(NamedTextColor.RED));
|
bot.chat.tellraw(Component.text(e.toString()).color(NamedTextColor.RED));
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pattern == null) break;
|
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;
|
||||||
|
|
||||||
if (pattern.matcher(name).find()) {
|
return playerName.equals(targetName);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return filteredPlayer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue