feat: filter reason

fix: check for duplicate entries (filter)
refactor: some other refactorings in the command
This commit is contained in:
ChomeNS
2025-01-03 17:48:01 +07:00
parent a90eec985a
commit b7f2023977
4 changed files with 73 additions and 21 deletions

View File

@@ -59,16 +59,40 @@ public class FilterCommand extends Command {
switch (action) {
case "add" -> {
final String player = context.getString(true, true);
final String player = context.getString(false, true);
final String reason = context.getString(false, false);
if (
FilterPlugin.localList.stream()
.map(filteredPlayer -> filteredPlayer.playerName)
.toList()
.contains(player)
) {
throw new CommandException(
Component.translatable(
"The player %s is already in the filters",
Component.text(player)
)
);
}
final boolean finalRegex = regex;
final boolean finalIgnoreCase = ignoreCase;
DatabasePlugin.executorService.submit(() -> bot.filter.add(player, finalRegex, finalIgnoreCase));
return Component.translatable(
"Added %s to the filters",
Component.text(player).color(ColorUtilities.getColorByString(bot.config.colorPalette.username))
).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
DatabasePlugin.executorService.submit(() -> bot.filter.add(player, reason, finalRegex, finalIgnoreCase));
if (reason.isEmpty()) {
return Component.translatable(
"Added %s to the filters",
Component.text(player).color(ColorUtilities.getColorByString(bot.config.colorPalette.username))
).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
} else {
return Component.translatable(
"Added %s to the filters with reason %s",
Component.text(player).color(ColorUtilities.getColorByString(bot.config.colorPalette.username)),
Component.text(reason).color(ColorUtilities.getColorByString(bot.config.colorPalette.string))
).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
}
}
case "remove" -> {
context.checkOverloadArgs(2);
@@ -107,9 +131,30 @@ public class FilterCommand extends Command {
if (player.ignoreCase) args.add(Component.text("ignore case"));
if (player.regex) args.add(Component.text("regex"));
options = options.append(Component.text("("));
options = options.append(Component.join(JoinConfiguration.commas(true), args).color(ColorUtilities.getColorByString(bot.config.colorPalette.string)));
options = options.append(Component.text(")"));
options = options
.append(Component.text("("))
.append(
Component
.join(
JoinConfiguration.commas(true),
args
)
.color(ColorUtilities.getColorByString(bot.config.colorPalette.string))
)
.append(Component.text(")"))
.append(Component.space());
}
if (!player.reason.isEmpty()) {
options = options
.append(Component.text("("))
.append(Component.text("reason: ").color(NamedTextColor.GRAY))
.append(
Component
.text(player.reason)
.color(ColorUtilities.getColorByString(bot.config.colorPalette.string))
)
.append(Component.text(")"));
}
filtersComponents.add(

View File

@@ -5,16 +5,19 @@ import com.fasterxml.jackson.annotation.JsonProperty;
public class FilteredPlayer {
public final String playerName;
public final String reason;
public final boolean regex;
public final boolean ignoreCase;
@JsonCreator
public FilteredPlayer (
@JsonProperty("playerName") String playerName,
@JsonProperty("reason") String reason,
@JsonProperty("regex") boolean regex,
@JsonProperty("ignoreCase") boolean ignoreCase
) {
this.playerName = playerName;
this.reason = reason;
this.regex = regex;
this.ignoreCase = ignoreCase;
}
@@ -23,6 +26,7 @@ public class FilteredPlayer {
public String toString() {
return "FilteredPlayer{" +
"playerName='" + playerName + '\'' +
", reason='" + reason + '\'' +
", regex=" + regex +
", ignoreCase=" + ignoreCase +
'}';

View File

@@ -20,9 +20,9 @@ import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
public class FilterPlugin extends PlayersPlugin.Listener {
private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS filters (name VARCHAR(255) PRIMARY KEY, regex BOOLEAN, ignoreCase BOOLEAN);";
private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS filters (name VARCHAR(255) PRIMARY KEY, reason VARCHAR(255), regex BOOLEAN, ignoreCase BOOLEAN);";
private static final String LIST_FILTERS = "SELECT * FROM filters;";
private static final String INSERT_FILTER = "INSERT INTO filters (name, regex, ignoreCase) VALUES (?, ?, ?);";
private static final String INSERT_FILTER = "INSERT INTO filters (name, reason, regex, ignoreCase) VALUES (?, ?, ?, ?);";
private static final String REMOVE_FILTER = "DELETE FROM filters WHERE name = ?;";
private static final String CLEAR_FILTER = "DELETE FROM filters;";
@@ -79,6 +79,7 @@ public class FilterPlugin extends PlayersPlugin.Listener {
while (result.next()) {
final FilteredPlayer filteredPlayer = new FilteredPlayer(
result.getString("name"),
result.getString("reason"),
result.getBoolean("regex"),
result.getBoolean("ignoreCase")
);
@@ -139,7 +140,7 @@ public class FilterPlugin extends PlayersPlugin.Listener {
if (player == null) return;
doAll(target);
doAll(target, player.reason);
});
}
@@ -170,7 +171,7 @@ public class FilterPlugin extends PlayersPlugin.Listener {
command.startsWith("/essentials:emute") ||
command.startsWith("/essentials:silence") ||
command.startsWith("/essentials:esilence")
) mute(entry);
) mute(entry, player.reason);
deOp(entry);
gameMode(entry);
@@ -184,11 +185,12 @@ public class FilterPlugin extends PlayersPlugin.Listener {
if (player == null || message.sender.profile.getId().equals(new UUID(0L, 0L))) return;
doAll(message.sender);
doAll(message.sender, player.reason);
}
public void doAll (PlayerEntry entry) {
mute(entry);
public void doAll (PlayerEntry entry) { doAll(entry, ""); }
public void doAll (PlayerEntry entry, String reason) {
mute(entry, reason);
deOp(entry);
gameMode(entry);
bot.exploits.kick(entry.profile.getId());
@@ -217,13 +219,14 @@ public class FilterPlugin extends PlayersPlugin.Listener {
}
}
public void add (String playerName, boolean regex, boolean ignoreCase) {
public void add (String playerName, String reason, boolean regex, boolean ignoreCase) {
try {
final PreparedStatement statement = bot.database.connection.prepareStatement(INSERT_FILTER);
statement.setString(1, playerName);
statement.setBoolean(2, regex);
statement.setBoolean(3, ignoreCase);
statement.setString(2, reason);
statement.setBoolean(3, regex);
statement.setBoolean(4, ignoreCase);
statement.executeUpdate();
@@ -236,7 +239,7 @@ public class FilterPlugin extends PlayersPlugin.Listener {
if (target == null) return;
doAll(target);
doAll(target, reason);
}
public void remove (String playerName) {