Attempting to rewrite action builder, very hard

This commit is contained in:
obvWolf
2024-02-26 01:51:21 -06:00
parent 3545360d74
commit 6c4fde2ee4
9 changed files with 81 additions and 166 deletions

View File

@@ -8,15 +8,24 @@ import io.github.itzispyder.pdk.commands.completions.CompletionBuilder;
import io.github.thetrouper.sentinel.Sentinel;
import io.github.thetrouper.sentinel.data.cmdblocks.WhitelistedBlock;
import io.github.thetrouper.sentinel.server.functions.*;
import io.github.thetrouper.sentinel.server.util.ServerUtils;
import io.github.thetrouper.sentinel.server.util.Text;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.HoverEvent;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
import org.bukkit.block.CommandBlock;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
@CommandRegistry(value = "sentinel",permission = @Permission("sentinel.staff"),printStackTrace = true)
public class SentinelCommand implements CustomCommand {
@@ -28,24 +37,34 @@ public class SentinelCommand implements CustomCommand {
switch (args.get(0).toString()) {
case "commandblock", "cb" -> handleCommandBlock(p,args);
case "reload" -> {
if (!Sentinel.isTrusted(p)) return;
if (!Sentinel.isTrusted(p)) {
p.sendMessage(Sentinel.lang.permissions.noTrust);
return;
}
p.sendMessage(Text.prefix("Reloading Sentinel!"));
Sentinel.log.info("[Sentinel] Re-Initializing Sentinel!");
instance.loadConfig();
}
case "full-system-check" -> {
if (!Sentinel.isTrusted(p)) return;
if (!Sentinel.isTrusted(p)) {
p.sendMessage(Sentinel.lang.permissions.noTrust);
return;
}
p.sendMessage(Text.prefix("Initiating a full system check!"));
SystemCheck.fullCheck(p);
}
case "debug" -> handleDebugCommand(p,args);
case "false-positive" -> {
handleFalsePositive(p,args);
}
}
}
private void handleFalsePositive(Player p, Args args) {
String falsePositive = args.getAll(2).toString();
if (!p.hasPermission("sentinel.chat.antiswear.edit")) {
p.sendMessage(Sentinel.lang.permissions.noPermission);
return;
}
switch (args.get(1).toString()) {
case "add" -> {
Sentinel.fpConfig.swearWhitelist.add(falsePositive);
@@ -53,13 +72,16 @@ public class SentinelCommand implements CustomCommand {
}
case "remove" -> {
Sentinel.fpConfig.swearWhitelist.remove(falsePositive);
p.sendMessage(Text.prefix("&7Successfully removed &c%s&7 to the false positive list!".formatted(falsePositive)));
p.sendMessage(Text.prefix("&7Successfully removed &c%s&7 from the false positive list!".formatted(falsePositive)));
}
}
Sentinel.fpConfig.save();
}
private void handleCommandBlock(Player p, Args args) {
if (!Sentinel.isTrusted(p)) return;
if (!Sentinel.isTrusted(p)) {
p.sendMessage(Sentinel.lang.permissions.noTrust);
return;
}
Block target = p.getTargetBlock(Set.of(Material.AIR),10);
switch (args.get(1).toString()) {
case "add" -> {
@@ -84,7 +106,10 @@ public class SentinelCommand implements CustomCommand {
}
private void handleDebugCommand(Player p, Args args) {
if (!p.hasPermission("sentinel.debug")) return;
if (!p.hasPermission("sentinel.debug")) {
p.sendMessage(Sentinel.lang.permissions.noPermission);
return;
}
switch (args.get(1).toString()) {
case "lang" -> {
p.sendMessage(Sentinel.lang.brokenLang);
@@ -105,10 +130,19 @@ public class SentinelCommand implements CustomCommand {
@Override
public void dispatchCompletions(CompletionBuilder b) {
b.then(b.arg("reload","full-system-check"));
b.then(b.arg("false-positive").then(b.arg("add","remove")));
b.then(b.arg("debug").then(
b.arg("lang","toggle","chat")));
b.then(b.arg("commandblock"));
List<String> players = new ArrayList<>();
for (Player player : ServerUtils.getPlayers()) {
players.add(player.getName());
}
b.then(b.arg("reload"))
.then(b.arg("full-system-check"))
.then(b.arg("false-positive")
.then(b.arg("add"))
.then(b.arg("remove")))
.then(b.arg("debug")
.then(b.arg("lang"))
.then(b.arg("toggle"))
.then(b.arg("chat")))
.then(b.arg("commandblock"));
}
}

View File

@@ -0,0 +1,10 @@
package io.github.thetrouper.sentinel.data;
import org.bukkit.event.Cancellable;
import java.util.function.Consumer;
public record ActionResult(String name, Runnable action, boolean isRan) {
}

View File

@@ -1,51 +0,0 @@
package io.github.thetrouper.sentinel.data;
import io.github.thetrouper.sentinel.Sentinel;
public enum FAT {
BLOCK_SWEAR("Sentinel Profanity Filter",null,"swear-block-warn", "swear-block-notification", null,0x000000),
BLOCK_SPAM("Sentinel Anti-Spam", null, "spam-block-warn", "spam-notification",null,0x000000),
SWEAR_PUNISH("Sentinel Anti-Swear Log","Anti-Swear", "profanity-mute-warn", "profanity-mute-notification", Sentinel.mainConfig.chat.antiSwear.swearPunishCommand, 0xFFB000),
SLUR_PUNISH("Sentinel Anti-Slur Log", "Anti-Slur", "slur-mute-warn", "slur-mute-notification", Sentinel.mainConfig.chat.antiSwear.strictPunishCommand, 0xFF0000),
SPAM_PUNISH("Sentinel Anti-Spam Log", "Anti-Spam", "spam-mute-warn", "spam-mute-notification", Sentinel.mainConfig.chat.antiSpam.spamPunishCommand, 0xFF8000),
BLOCK_URL("Sentinel Anti-URL Log", "Anti-URL","url-warn","url-notification", null,0xFF0000),
BLOCK_UNICODE("Sentinel Anti-Unicode Log", "Anti-Unicode","unicode-warn","unicode-notification", null,0xFF0000),
SAFE("Sentinel Chat Log", "You Shouldn't See this!", "spam-mute-warn", "spam-mute-notification", Sentinel.mainConfig.chat.antiSpam.spamPunishCommand, 0x00FF00);
private final String title;
private final String name;
private final String warnTranslationKey;
private final String notifTranslationKey;
private final String executedCommand;
private final int embedColor;
FAT(String title, String name, String warnTranslationKey, String notifTranslationKey, String executedCommand, int embedColor) {
this.title = title;
this.name = name;
this.warnTranslationKey = warnTranslationKey;
this.notifTranslationKey = notifTranslationKey;
this.executedCommand = executedCommand;
this.embedColor = embedColor;
}
public String getTitle() {
return title;
}
public String getName() {
return name;
}
public String getWarnTranslationKey() {
return warnTranslationKey;
}
public String getNotifTranslationKey() {
return notifTranslationKey;
}
public String getExecutedCommand() {
return executedCommand;
}
public int getColor() {
return embedColor;
}
}

View File

@@ -2,7 +2,7 @@ package io.github.thetrouper.sentinel.data;
import io.github.thetrouper.sentinel.Sentinel;
public record FilterActionType(String logTitle, String logName, String chatWarning, String chatWarningHover, String chatNotification, String chatNotificationHover, String punishmentCommand, int embedColor, boolean isLogged) {
public record FilterActionType(String logSuper, String logName, String chatWarning, String chatWarningHover, String chatNotification, String chatNotificationHover, String punishmentCommand, int embedColor, boolean isLogged) {
public static final FilterActionType UNICODE_BLOCK = new FilterActionType("Sentinel Anti-Unicode Log", "Anti-Unicode", Sentinel.lang.unicodeFilter.unicodeWarn, Sentinel.lang.automatedActions.actionAutomaticReportable, Sentinel.lang.unicodeFilter.unicodeNotification, Sentinel.lang.unicodeFilter.unicodeNotificationHover, null, 0xAAAAFF, Sentinel.mainConfig.chat.logUnicode);
public static final FilterActionType URL_BLOCK = new FilterActionType("Sentinel Anti-URL Log", "Anti-URL", Sentinel.lang.urlFilter.urlWarn, Sentinel.lang.automatedActions.actionAutomaticReportable, Sentinel.lang.urlFilter.urlNotification, Sentinel.lang.urlFilter.urlNotificationHover, null, 0xAAAAFF, Sentinel.mainConfig.chat.logURL);

View File

@@ -0,0 +1,15 @@
package io.github.thetrouper.sentinel.data;
public record NewActionType(String superTitle, String title, int embedColor, String chatNotification) {
public static final NewActionType CMD_BLOCK_EXECUTE = new NewActionType("Command Block Whitelist Log", "An unauthorized command block has been detected",0xFF0000,"The Command Block Whitelist has been triggered!");
public static final NewActionType CMD_BLOCK_PLACE = new NewActionType("Anti-Nuke Log", "A player attempted to place a command block",0xFF0000,"§e%s§7 has triggered the Anti-Nuke!");
public static final NewActionType CMD_BLOCK_USE = new NewActionType("Anti-Nuke Log", "A player attempted to use a command block",0xFF0000,"§e%s§7 has triggered the Anti-Nuke!");
public static final NewActionType CMD_BLOCK_CHANGE = new NewActionType("Anti-Nuke Log", "A player attempted to change a command block",0xFF0000,"§e%s§7 has triggered the Anti-Nuke!");
public static final NewActionType CMD_MINECART_USE = new NewActionType("Anti-Nuke Log", "A player attempted to use a command minecart",0xFF0000,"§e%s§7 has triggered the Anti-Nuke!");
public static final NewActionType CMD_MINECART_PLACE = new NewActionType("Anti-Nuke Log", "A player attempted to place a command minecart",0xFF0000,"§e%s§7 has triggered the Anti-Nuke!");
public static final NewActionType CMD_MINECART_BREAK = new NewActionType("Anti-Nuke Log", "A player attempted to break a command minecart",0xFF0000,"§e%s§7 has triggered the Anti-Nuke!");
public static final NewActionType CMD_EXECUTE = new NewActionType("Anti-Nuke Log", "A player attempted to run a dangerous command",0xFF0000,"§e%s§7 has triggered the Anti-Nuke!");
public static final NewActionType CMD_SPECIFIC = new NewActionType("Anti-Specific Log", "A player attempted to run a plugin specific command",0xFF0000,"§e%s§7 has triggered the Anti-Specific.");
public static final NewActionType CMD_LOGGED = new NewActionType("Command Log", "A player has ran a logged command",0xFF0000,"§e%s§7 has ran a logged command.");
public static final NewActionType NBT_PULL = new NewActionType("Anti-NBT Log", "A player attempted to pull out an NBT item",0xFF0000,"§e%s§7 has triggered the Anti-NBT!");
}

View File

@@ -107,7 +107,7 @@ public class FilterAction {
public static void sendDiscordLog(FilterActionType type, Player offender, String message) {
CompletableFuture.runAsync(()->{
String superTitle = type.logTitle();
String superTitle = type.logSuper();
String title = "%s has triggered the %s!".formatted(offender.getName(),type.logName());
boolean isSwear = type.equals(FilterActionType.SWEAR_BLOCK) || type.equals(FilterActionType.SWEAR_PUNISH) || type.equals(FilterActionType.SLUR_PUNISH);
boolean isSpam = type.equals(FilterActionType.SPAM_BLOCK) || type.equals(FilterActionType.SPAM_PUNISH);
@@ -156,7 +156,7 @@ public class FilterAction {
public static void sendConsoleLog(FilterActionType type, Player offender, String message) {
StringBuilder log = new StringBuilder();
String superTitle = "\n]=- " + type.logTitle() + " -=[";
String superTitle = "\n]=- " + type.logSuper() + " -=[";
String title = "\n%s has triggered the %s!\n".formatted(offender.getName(),type.logName());
boolean isSwear = type.equals(FilterActionType.SWEAR_BLOCK) || type.equals(FilterActionType.SWEAR_PUNISH) || type.equals(FilterActionType.SLUR_PUNISH);
boolean isSpam = type.equals(FilterActionType.SPAM_BLOCK) || type.equals(FilterActionType.SPAM_PUNISH);

View File

@@ -1,6 +1,7 @@
package io.github.thetrouper.sentinel.server;
import io.github.itzispyder.pdk.plugin.builders.ItemBuilder;
import io.github.itzispyder.pdk.utils.SchedulerUtils;
import io.github.itzispyder.pdk.utils.discord.DiscordEmbed;
import io.github.itzispyder.pdk.utils.discord.DiscordWebhook;
@@ -13,9 +14,13 @@ import io.github.thetrouper.sentinel.server.util.Text;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import org.bukkit.block.Block;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import java.util.List;
@@ -98,100 +103,7 @@ public class NewAction {
return this;
}
public NewAction execute() {
String actionTop = action.getMessageTop();
String actionTitle = action.getMessageTitle();
String itemLog = (item != null && item.hasItemMeta() && item.getItemMeta().getAsString() != null) ? FileUtils.createNBTLog(item) : "";
String commandLog = (loggedCommand != null) ? FileUtils.createCommandLog(loggedCommand) : "";
final List<String> punishCommands = Sentinel.mainConfig.plugin.punishCommands;
if (denied) {
event.setCancelled(true);
}
if (deoped) {
player.setOp(false);
}
if (punished) {
for (String command : punishCommands) {
ServerUtils.sendCommand(command.replaceAll("%player%",player.getName()));
}
}
if (revertGM) {
player.setGameMode(GameMode.SURVIVAL);
}
if (notifyConsole) {
String conNotif = "]=- Sentinel -=[\n";
conNotif += actionTop + "\n";
conNotif += (player != null) ? "Player: " + player.getName() + "\n" : "";
conNotif += (command != null) ? ((loggedCommand != null && loggedCommand.length() > 128) ? "Command: Too long to show here!\n | Saved to file: " + commandLog + "\n" : "Command: " + command + "\n") : "";
conNotif += (item != null) ? "Item: /Sentinel/LoggedNBT/" + itemLog + "\n" : "";
conNotif += (block != null) ? "Block: " + block.getType().toString().toLowerCase().replace("_", " ") + "\nLocation: " + block.getLocation().getX() + " " + block.getLocation().getY() + " " + block.getLocation().getZ() + "\n" : "";
conNotif += "Denied: " + (denied ? "\u2714" : "\u2718") + "\n";
conNotif += "Deoped: " + (deoped ? "\u2714" : "\u2718") + "\n";
conNotif += "Punished: " + (punished ? "\u2714" : "\u2718") + "\n";
conNotif += (revertGM) ? "RevertGM: \u2714\n" : "";
conNotif += "Logged: " + (notifyDiscord ? "\u2714" : "\u2718");
Sentinel.log.info(conNotif);
}
if (notifyTrusted) {
TextComponent notification = new TextComponent();
notification.setText(Text.prefix(" " + actionTop));
String body = "&b]=- Sentinel -=[&f\n" + actionTitle + "&r\n";
body += (player != null) ? "&fPlayer: &b" + player.getName() + "&r\n" : "";
body += (command != null) ? ((loggedCommand != null && loggedCommand.length() > 64) ? "&fCommand: &cToo long to show here!&r\n &7&l| &fSaved to file: &b" + commandLog + "&r\n" : "&fCommand: &b" + command + "&r\n") : "";
body += (item != null) ? "&fItem: &b/Sentinel/LoggedNBT/&b" + itemLog + "\n" : "";
body += (block != null) ? "&fBlock: &b" + block.getType().toString().toLowerCase().replace("_", " ") + "\n&fLocation: &b" + block.getLocation().getX() + " " + block.getLocation().getY() + " " + block.getLocation().getZ() + "&r\n" : "";
body += "&fDenied: &b" + (denied ? "&a\u2714" : "&c\u2718") + "&r\n";
body += "&fDeoped: " + (deoped ? "&a\u2714" : "&c\u2718") + "&r\n";
body += "&fPunished: " + (punished ? "&a\u2714" : "&c\u2718") + "&r\n";
body += (revertGM) ? "&fRevertGM: &a\u2714\n" : "";
body += "&fLogged: " + (notifyDiscord ? "&a\u2714" : "&c\u2718");
notification.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new net.md_5.bungee.api.chat.hover.content.Text(Text.color(body))));
ServerUtils.forEachPlayer(trusted -> {
if (Sentinel.isTrusted(trusted)) {
trusted.spigot().sendMessage(notification);
}
});
}
if (notifyDiscord) {
String description = (player != null) ? Emojis.rightSort + " **Player:** " + player.getName() + " " + Emojis.member + "\n" : "";
description += (command != null) ? ((loggedCommand != null && loggedCommand.length() > 128) ? Emojis.rightSort + " **Command:** Too long to show here! " + Emojis.nuke + "\n | Saved to file: " + commandLog + "\n" : Emojis.rightSort + " **Command:** " + command + " " + Emojis.nuke + "\n") : "";
description += (item != null) ? Emojis.rightSort + " **Item:** " + item.getType().toString().toLowerCase() + " " + Emojis.nuke + "\n" + Emojis.space + Emojis.rightDoubleArrow + "**NBT:** Uploaded to /Sentinel/LoggedNBT/" + itemLog : "";
description += (block != null) ? Emojis.rightSort + " **Block:** " + block.getType().toString().toLowerCase() + " " + Emojis.nuke + "\n" + Emojis.space + Emojis.rightDoubleArrow + " **Location:** X: " + block.getX() + " Y: " + block.getY() + " Z: " + block.getZ() + "\n" : "";
String actions = Emojis.rightSort + " **Denied:** " + (denied ? Emojis.success : Emojis.failure) + "\n";
actions += Emojis.rightSort + " **De-oped:** " + (deoped ? Emojis.success : Emojis.failure) + "\n";
actions += Emojis.rightSort + " **Punished:** " + (punished ? Emojis.success : Emojis.failure) + "\n";
actions += (revertGM) ? Emojis.rightSort + " **GM Reverted:** " + Emojis.success + "\n" : "";
actions += Emojis.rightSort + " **Logged:** " + Emojis.success;
try {
String finalDescription = description;
String finalActions = actions;
CompletableFuture.runAsync(()->{
ServerUtils.sendDebugMessage("Executing webhook...");
DiscordWebhook.create()
.username("Sentinel Anti-Nuke | Logs")
.avatar("https://r2.e-z.host/d440b58a-ba90-4839-8df6-8bba298cf817/3lwit5nt.png")
.addEmbed(DiscordEmbed.create()
.author(new DiscordEmbed.Author(actionTop,"https://builtbybit.com/resources/sentinel-anti-nuke.30130/",null))
.title(actionTitle)
.desc(finalDescription)
.addField(new DiscordEmbed.Field("Actions:", finalActions,false))
.thumbnail("https://crafatar.com/avatars/" + (player == null ? "049460f7-21cb-42f5-8059-d42752bf406f" : player.getUniqueId()) + "?size=64&&overlay")
.color(action.getEmbedColor())
.build()).send(Sentinel.mainConfig.plugin.webhook);
});
} catch (Exception e) {
ServerUtils.sendDebugMessage(Text.prefix("Epic webhook failure!!!"));
Sentinel.log.info(e.toString());
}
}
return new NewAction(event, action, player, command, loggedCommand, item, block, denied, deoped, punished, revertGM, notifyDiscord, notifyTrusted, notifyConsole);
}

View File

@@ -51,14 +51,6 @@ public class ProfanityFilter {
};
}
private static FAT getFAT(FilterSeverity severity) {
return switch (severity) {
case SAFE -> FAT.SAFE;
case LOW, MEDIUM_LOW, MEDIUM, MEDIUM_HIGH, HIGH -> FAT.BLOCK_SWEAR;
case SLUR -> FAT.SLUR_PUNISH;
};
}
public static String highlightProfanity(String text) {
return highlightProfanity(text, "&e", "&f");
}

View File

@@ -24,6 +24,9 @@ permissions:
sentinel.chat.antiswear.bypass:
description: Bypass the antiSwear
default: op
sentinel.chat.antiswear.edit:
description: Add a false positive to the config
default: op
sentinel.chat.antispam.flags:
description: See antispam flags
default: op