Got the event made

This commit is contained in:
TheTrouper
2024-02-12 16:04:30 -06:00
parent daea256b4c
commit 420d5a1866
16 changed files with 76 additions and 32 deletions

View File

@@ -178,6 +178,9 @@ public final class Sentinel extends JavaPlugin {
swearConfig.save();
nbtConfig.save();
whitelist = JsonSerializable.load(cmdWhitelist, WhitelistStorage.class, new WhitelistStorage());
whitelist.save();
log.info("Loading Dictionary (" + Sentinel.mainConfig.plugin.lang + ")...");
language = JsonSerializable.load(LanguageFile.PATH,LanguageFile.class,new LanguageFile());
@@ -206,6 +209,10 @@ public final class Sentinel extends JavaPlugin {
return Sentinel.mainConfig.plugin.trustedPlayers.contains(player.getUniqueId().toString());
}
public static boolean isTrusted(String uuid) {
return Sentinel.mainConfig.plugin.trustedPlayers.contains(uuid);
}
/**
* Checks if a command is a logged command.
* @param command the command to check

View File

@@ -52,10 +52,8 @@ public class SentinelCommand implements CustomCommand {
private void handleCommandBlock(Player p, Args args) {
Block target = p.getTargetBlock(Set.of(Material.AIR),10);
p.sendMessage("1");
switch (args.get(1).toString()) {
case "add" -> {
p.sendMessage("2");
if (target.getType().equals(Material.COMMAND_BLOCK) || target.getType().equals(Material.REPEATING_COMMAND_BLOCK) || target.getType().equals(Material.CHAIN_COMMAND_BLOCK)) {
CommandBlock cb = (CommandBlock) target.getState();
CMDBlockWhitelist.add(cb,p.getUniqueId());
@@ -68,7 +66,7 @@ public class SentinelCommand implements CustomCommand {
WhitelistedBlock wb = CMDBlockWhitelist.get(target.getLocation());
if (wb != null) {
CMDBlockWhitelist.remove(target.getLocation());
p.sendMessage(Text.prefix("Successfully removed 1 &b" + Text.blockName(wb.loc().getBlock().getType().toString()) + "&7 with the command &a" + wb.command() + "&7."));
p.sendMessage(Text.prefix("Successfully removed 1 &b" + Text.blockName(WhitelistedBlock.fromSerialized(wb.loc()).getBlock().getType().toString()) + "&7 with the command &a" + wb.command() + "&7."));
return;
}
p.sendMessage(Text.prefix("Could not un-whitelist the &b" + Text.blockName(target.getType().toString()) + "&7 it wasn't whitelisted in the first place!"));

View File

@@ -10,7 +10,8 @@ public enum ActionType {
UPDATE_COMMAND_BLOCK("HoneyPot log","Caught a command block command!", 0xF8FF00),
PLACE_MINECART_COMMAND("Anti-Nuke has been triggered","The placing of a minecart command has been detected!", 0xFF0000),
USE_MINECART_COMMAND("Anti-Nuke has been triggered", "The use of a command block has been detected!", 0xFF0000),
UPDATE_MINECART_COMMAND("HoneyPot log","Caught a command block command!", 0xFFB000);
UPDATE_MINECART_COMMAND("HoneyPot log","Caught a command minecart command!", 0xFFB000),
COMMAND_BLOCK_EXECUTE("Command Block log","Caught an invalid command block!", 0xFFB000);
private final String messageTop;
private final String messageTitle;
private final int embedColor;

View File

@@ -5,7 +5,9 @@ import org.bukkit.Bukkit;
import org.bukkit.Location;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class WhitelistStorage implements JsonSerializable<WhitelistStorage> {
@@ -16,6 +18,6 @@ public class WhitelistStorage implements JsonSerializable<WhitelistStorage> {
return file;
}
public Set<WhitelistedBlock> whitelistedCMDBlocks = new HashSet<>();
public List<WhitelistedBlock> whitelistedCMDBlocks = new ArrayList<>();
}

View File

@@ -1,16 +1,17 @@
package io.github.thetrouper.sentinel.data.cmdblocks;
import io.papermc.paper.command.CommandBlockHolder;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.block.CommandBlock;
import java.util.UUID;
public record WhitelistedBlock(UUID owner, Location loc, CMDBlockType type, boolean active, String command) {
public record WhitelistedBlock(String owner, Location loc, String type, boolean active, String command) {
public static org.bukkit.Location fromSerialized(Location loc) {
World w = Bukkit.getWorld(loc.world());
return new org.bukkit.Location(w,loc.x(),loc.y(),loc.z());
}
public static Location serialize(org.bukkit.Location loc) {
return new Location(loc.getWorld().getName(),loc.x(),loc.y(),loc.z());
}
}

View File

@@ -30,6 +30,8 @@ public class MainConfig implements JsonSerializable<MainConfig> {
public boolean preventCmdBlockPlace = true;
public boolean preventCmdBlockUse = true;
public boolean preventCmdBlockChange = true;
public boolean cmdBlockWhitelist = false;
public boolean logUnauthorizedCmdBlocks = false;
public boolean preventCmdCartPlace = true;
public boolean preventCmdCartUse = true;
public boolean cmdBlockOpCheck = true;

View File

@@ -1,6 +1,11 @@
package io.github.thetrouper.sentinel.events;
import io.github.itzispyder.pdk.events.CustomListener;
import io.github.thetrouper.sentinel.Sentinel;
import io.github.thetrouper.sentinel.data.ActionType;
import io.github.thetrouper.sentinel.server.Action;
import io.github.thetrouper.sentinel.server.functions.CMDBlockWhitelist;
import org.bukkit.block.Block;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.event.EventHandler;
import org.bukkit.event.server.ServerCommandEvent;
@@ -10,6 +15,18 @@ public class CMDBlockExecute implements CustomListener {
@EventHandler
private void onCommandBlock(ServerCommandEvent e) {
if (!(e.getSender() instanceof BlockCommandSender s)) return;
Block cmdBlock = s.getBlock();
if (CMDBlockWhitelist.canRun(cmdBlock)) return;
Action a = new Action.Builder()
.setEvent(e)
.setAction(ActionType.COMMAND_BLOCK_EXECUTE)
.setBlock(cmdBlock)
.setDenied(true)
.setNotifyDiscord(Sentinel.mainConfig.plugin.logUnauthorizedCmdBlocks)
.setNotifyTrusted(true)
.setNotifyConsole(true)
.setDenied(true)
.setLoggedCommand(e.getCommand())
.execute();
}
}

View File

@@ -34,7 +34,7 @@ public class CMDBlockPlace implements CustomListener {
.setDenied(true)
.setDeoped(Sentinel.mainConfig.plugin.deop)
.setPunished(Sentinel.mainConfig.plugin.cmdBlockPunish)
.setnotifyDiscord(Sentinel.mainConfig.plugin.logCmdBlocks)
.setNotifyDiscord(Sentinel.mainConfig.plugin.logCmdBlocks)
.setNotifyTrusted(true)
.setNotifyConsole(true)
.execute();

View File

@@ -39,7 +39,7 @@ public class CMDBlockUse implements CustomListener {
.setDenied(true)
.setPunished(Sentinel.mainConfig.plugin.cmdBlockPunish)
.setDeoped(Sentinel.mainConfig.plugin.deop)
.setnotifyDiscord(Sentinel.mainConfig.plugin.logCmdBlocks)
.setNotifyDiscord(Sentinel.mainConfig.plugin.logCmdBlocks)
.setNotifyTrusted(true)
.setNotifyConsole(true)
.execute();
@@ -72,7 +72,7 @@ public class CMDBlockUse implements CustomListener {
.setDenied(true)
.setPunished(Sentinel.mainConfig.plugin.cmdBlockPunish)
.setDeoped(Sentinel.mainConfig.plugin.deop)
.setnotifyDiscord(Sentinel.mainConfig.plugin.logCmdBlocks)
.setNotifyDiscord(Sentinel.mainConfig.plugin.logCmdBlocks)
.setNotifyTrusted(true)
.setNotifyConsole(true)
.execute();

View File

@@ -42,7 +42,7 @@ public class CMDMinecartPlace implements CustomListener {
.setDeoped(Sentinel.mainConfig.plugin.deop)
.setNotifyConsole(true)
.setNotifyTrusted(true)
.setnotifyDiscord(Sentinel.mainConfig.plugin.logCmdBlocks)
.setNotifyDiscord(Sentinel.mainConfig.plugin.logCmdBlocks)
.execute();
}
}

View File

@@ -30,7 +30,7 @@ public class CMDMinecartUse implements CustomListener {
.setPlayer(p)
.setDenied(true)
.setPunished(Sentinel.mainConfig.plugin.cmdBlockPunish)
.setnotifyDiscord(Sentinel.mainConfig.plugin.logCmdBlocks)
.setNotifyDiscord(Sentinel.mainConfig.plugin.logCmdBlocks)
.setDeoped(Sentinel.mainConfig.plugin.deop)
.setNotifyTrusted(true)
.setNotifyConsole(true)

View File

@@ -30,7 +30,7 @@ public class CommandEvent implements CustomListener {
.setDenied(true)
.setDeoped(Sentinel.mainConfig.plugin.deop)
.setPunished(Sentinel.mainConfig.plugin.commandPunish)
.setnotifyDiscord(Sentinel.mainConfig.plugin.logDangerous)
.setNotifyDiscord(Sentinel.mainConfig.plugin.logDangerous)
.setNotifyConsole(true)
.setNotifyTrusted(true)
.execute();
@@ -49,7 +49,7 @@ public class CommandEvent implements CustomListener {
.setDenied(true)
.setDeoped(Sentinel.mainConfig.plugin.deop)
.setPunished(Sentinel.mainConfig.plugin.specificPunish)
.setnotifyDiscord(Sentinel.mainConfig.plugin.logSpecific)
.setNotifyDiscord(Sentinel.mainConfig.plugin.logSpecific)
.setNotifyConsole(true)
.setNotifyTrusted(true)
.execute();
@@ -66,7 +66,7 @@ public class CommandEvent implements CustomListener {
.setDenied(false)
.setDeoped(false)
.setPunished(false)
.setnotifyDiscord(true)
.setNotifyDiscord(true)
.setNotifyConsole(true)
.setNotifyTrusted(true)
.execute();

View File

@@ -50,7 +50,7 @@ public class NBTEvents implements CustomListener {
.setRevertGM(Sentinel.mainConfig.plugin.preventNBT)
.setNotifyConsole(true)
.setNotifyTrusted(true)
.setnotifyDiscord(Sentinel.mainConfig.plugin.logNBT)
.setNotifyDiscord(Sentinel.mainConfig.plugin.logNBT)
.execute();
}
}

View File

@@ -83,7 +83,7 @@ public class Action {
this.revertGM = revertGM;
return this;
}
public Builder setnotifyDiscord(boolean notifyDiscord) {
public Builder setNotifyDiscord(boolean notifyDiscord) {
this.notifyDiscord= notifyDiscord;
return this;
}

View File

@@ -1,12 +1,13 @@
package io.github.thetrouper.sentinel.server.functions;
import io.github.itzispyder.pdk.utils.misc.JsonSerializable;
import io.github.thetrouper.sentinel.Sentinel;
import io.github.thetrouper.sentinel.data.cmdblocks.CMDBlockType;
import io.github.thetrouper.sentinel.data.cmdblocks.WhitelistStorage;
import io.github.thetrouper.sentinel.data.cmdblocks.WhitelistedBlock;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.block.CommandBlock;
import org.bukkit.persistence.PersistentDataType;
@@ -15,7 +16,7 @@ import java.util.UUID;
public class CMDBlockWhitelist {
public static void add(CommandBlock cb, UUID owner) {
boolean alwaysActive = getNBTBoolean(cb, "auto");
WhitelistedBlock wb = new WhitelistedBlock(owner,cb.getLocation(),getType(cb),alwaysActive,cb.getCommand());
WhitelistedBlock wb = new WhitelistedBlock(owner.toString(),WhitelistedBlock.serialize(cb.getLocation()),getType(cb),alwaysActive,cb.getCommand());
Location wbl = WhitelistedBlock.fromSerialized(wb.loc());
@@ -33,7 +34,8 @@ public class CMDBlockWhitelist {
public static void remove(Location where) {
for (WhitelistedBlock cb : Sentinel.whitelist.whitelistedCMDBlocks) {
if (cb.loc().distance(where) < 0.5) {
Location cbl = WhitelistedBlock.fromSerialized(cb.loc());
if (cbl.distance(where) < 0.5) {
Sentinel.whitelist.whitelistedCMDBlocks.remove(cb);
break;
}
@@ -42,25 +44,41 @@ public class CMDBlockWhitelist {
Sentinel.whitelist.save();
}
public static boolean canRun(Block b) {
CommandBlock test = (CommandBlock) b.getState();
String command = test.getCommand();
boolean alwaysActive = getNBTBoolean(test, "auto");
for (WhitelistedBlock cb : Sentinel.whitelist.whitelistedCMDBlocks) {
if (!(b.getLocation().distance(WhitelistedBlock.fromSerialized(cb.loc())) < 0.5)) continue;
if (cb.active() != alwaysActive) return false;
if (!cb.command().equals(command)) return false;
if (!cb.type().equals(getType(test))) return false;
if (!Sentinel.isTrusted(cb.owner())) return false;
return true;
}
return false;
}
public static WhitelistedBlock get(Location where) {
for (WhitelistedBlock cb : Sentinel.whitelist.whitelistedCMDBlocks) {
if (cb.loc().distance(where) < 0.5) {
Location cbl = WhitelistedBlock.fromSerialized(cb.loc());
if (cbl.distance(where) < 0.5) {
return cb;
}
}
return null;
}
public static CMDBlockType getType(CommandBlock cb) {
public static String getType(CommandBlock cb) {
switch (cb.getType()) {
case COMMAND_BLOCK -> {
return CMDBlockType.IMPULSE;
return "impulse";
}
case REPEATING_COMMAND_BLOCK -> {
return CMDBlockType.REPEAT;
return "repeat";
}
case CHAIN_COMMAND_BLOCK -> {
return CMDBlockType.CHAIN;
return "chain";
}
}
return null;
@@ -76,7 +94,6 @@ public class CMDBlockWhitelist {
) == 1;
}
// Helper method to get PersistentDataContainer key
private static NamespacedKey getKey(String key) {
return new NamespacedKey(Sentinel.getInstance(), key);
}

View File

@@ -14,7 +14,6 @@ import org.bukkit.attribute.AttributeModifier;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.type.CommandBlock;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockPlaceEvent;
@@ -63,7 +62,7 @@ public class SystemCheck {
.setDenied(true)
.setPunished(Sentinel.mainConfig.plugin.cmdBlockPunish)
.setDeoped(Sentinel.mainConfig.plugin.deop)
.setnotifyDiscord(Sentinel.mainConfig.plugin.logCmdBlocks)
.setNotifyDiscord(Sentinel.mainConfig.plugin.logCmdBlocks)
.setNotifyTrusted(true)
.setNotifyConsole(true)
.execute();
@@ -82,7 +81,7 @@ public class SystemCheck {
.setDenied(true)
.setPunished(Sentinel.mainConfig.plugin.cmdBlockPunish)
.setDeoped(Sentinel.mainConfig.plugin.deop)
.setnotifyDiscord(Sentinel.mainConfig.plugin.logCmdBlocks)
.setNotifyDiscord(Sentinel.mainConfig.plugin.logCmdBlocks)
.setNotifyTrusted(true)
.setNotifyConsole(true)
.execute();
@@ -122,7 +121,7 @@ public class SystemCheck {
.setRevertGM(Sentinel.mainConfig.plugin.preventNBT)
.setNotifyConsole(true)
.setNotifyTrusted(true)
.setnotifyDiscord(Sentinel.mainConfig.plugin.logNBT)
.setNotifyDiscord(Sentinel.mainConfig.plugin.logNBT)
.execute();
p.setOp(true);
}