Added recursive container search

This commit is contained in:
obvWolf
2024-01-11 01:58:40 -06:00
parent 488e1e070c
commit 339c747e47
8 changed files with 120 additions and 36 deletions

View File

@@ -32,13 +32,10 @@ public final class Sentinel extends JavaPlugin {
*/
@Override
public void onEnable() {
log.info("\n]======------ Pre-load started! ------======[");
instance = this;
log.info("Loading Config...");
Config.loadConfiguration();
log.info("Loading Dictionary (" + Config.lang + ")...");
dict = JsonSerializable.load(LanguageFile.PATH,LanguageFile.class,new LanguageFile());
loadConfig();
log.info("Initializing Server ID...");
String serverID = Authenticator.getServerID();
identifier = serverID;
@@ -56,6 +53,7 @@ public final class Sentinel extends JavaPlugin {
}
switch (authStatus) {
case "AUTHORIZED" -> {
log.info("\n]======----- Auth Success! -----======[");
startup();
authstatus = authstatus.replaceAll("ur a skid lmao","get out of here kiddo");
}
@@ -88,14 +86,9 @@ public final class Sentinel extends JavaPlugin {
}
}
private void startup() {
log.info("\n]======----- Auth Success! -----======[");
// Init
log.info("Verifying Config...");
getConfig().options().copyDefaults();
saveDefaultConfig();
public void startup() {
log.info("\n]======----- Loading Sentinel! -----======[");
loadConfig();
// Plugin startup logic
log.info("Starting Up! (" + getDescription().getVersion() + ")...");
@@ -137,6 +130,18 @@ public final class Sentinel extends JavaPlugin {
" ]====---- Advanced Anti-Grief & Chat Filter ----====[");
}
public void loadConfig() {
// Init
Config.loadConfiguration();
log.info("Loading Dictionary (" + Config.lang + ")...");
dict = JsonSerializable.load(LanguageFile.PATH,LanguageFile.class,new LanguageFile());
log.info("Verifying Config...");
getConfig().options().copyDefaults();
saveDefaultConfig();
}
/**
* Plugin shutdown logic
*/

View File

@@ -34,7 +34,14 @@ public class SentinelCommand extends CustomCommand {
@Override
public void dispatchCommand(CommandSender sender, Command command, String label, String[] args) {
Player p = (Player) sender;
Sentinel instance = Sentinel.getInstance();
switch (args[0]) {
case "reload" -> {
if (!Sentinel.isTrusted(p)) return;
p.sendMessage(Text.prefix("Reloading Sentinel!"));
Sentinel.log.info("[Sentinel] Re-Initializing Sentinel!");
instance.loadConfig();
}
case "debug" -> {
switch (args[1]) {
case "antiswear" -> {
@@ -82,7 +89,8 @@ public class SentinelCommand extends CustomCommand {
@Override
public void registerCompletions(CompletionBuilder builder) {
builder.addCompletion(1, "debug",
"getHeat");
"getHeat",
"reload");
if (builder.args.length >= 2 && builder.args[1].equals("debug")) {
builder.addCompletion(2, "antiswear",
"antispam",

View File

@@ -6,12 +6,17 @@ import io.github.thetrouper.sentinel.data.Action;
import io.github.thetrouper.sentinel.data.ActionType;
import io.github.thetrouper.sentinel.server.util.ServerUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.block.Container;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCreativeEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.Map;
@@ -52,30 +57,85 @@ public class NBTEvents implements Listener {
}
}
private boolean itemPasses(ItemStack i) {
ServerUtils.sendDebugMessage("NBT: Checking if item passes: " + i.getItemMeta());
if (i.getItemMeta() != null) {
ServerUtils.sendDebugMessage("NBT: Item meta isn't null");
ItemMeta meta = i.getItemMeta();
if (!Config.allowName && meta.hasDisplayName()) {
ServerUtils.sendDebugMessage("NBT: No pass N");
return false;
} else if (!Config.allowLore && meta.hasLore()) {
ServerUtils.sendDebugMessage("NBT: No Pass L ");
return false;
} else if (!Config.allowAttributes && meta.hasAttributeModifiers()) {
ServerUtils.sendDebugMessage("NBT: No pass A");
return false;
} else if (Config.globalMaxEnchant != 0 && hasIllegalEnchants(i)) {
ServerUtils.sendDebugMessage("NBT: No pass E");
private boolean isContainer(ItemStack itemStack) {
return itemStack.getType() == Material.CHEST ||
itemStack.getType() == Material.TRAPPED_CHEST ||
itemStack.getType() == Material.FURNACE ||
itemStack.getType() == Material.BLAST_FURNACE ||
itemStack.getType() == Material.DROPPER ||
itemStack.getType() == Material.DISPENSER ||
itemStack.getType() == Material.HOPPER ||
itemStack.getType() == Material.BARREL;
}
private Inventory getSubInventory(ItemStack containerItem) {
ServerUtils.sendDebugMessage("NBT: GetSubInv checking item: " + containerItem);
if (containerItem.getItemMeta() instanceof BlockStateMeta blockStateMeta) {
ServerUtils.sendDebugMessage("NBT: subInv has (is) blockStateMeta: " + blockStateMeta);
BlockState blockState = blockStateMeta.getBlockState();
if (blockState instanceof Container) {
ServerUtils.sendDebugMessage("NBT: subInv has (is) container: " + (Container) blockState);
return ((Container) blockState).getInventory();
}
}
ServerUtils.sendDebugMessage("NBT: Inv is null: " + containerItem);
return null;
}
private boolean containerPasses(Inventory inventory) {
for (ItemStack itemStack : inventory.getContents()) {
if (itemStack == null || itemStack.getType().isAir()) continue;
if (!itemPasses(itemStack)) {
ServerUtils.sendDebugMessage("NBT: No pass C(I)");
return false;
}
ServerUtils.sendDebugMessage("NBT: All checks passed");
return true;
} else {
if (!isContainer(itemStack)) continue;
Inventory subInventory = getSubInventory(itemStack);
if (!containerPasses(subInventory)) {
ServerUtils.sendDebugMessage("NBT: No pass C(R)");
return false;
}
}
ServerUtils.sendDebugMessage("NBT: Item passes recursion check.");
return true;
}
private boolean itemPasses(ItemStack i) {
ServerUtils.sendDebugMessage("NBT: Checking if item passes: " + i.getItemMeta());
if (i.getItemMeta() == null) {
ServerUtils.sendDebugMessage("NBT: Item passes because of no meta");
return true;
}
ServerUtils.sendDebugMessage("NBT: Item meta isn't null");
ItemMeta meta = i.getItemMeta();
Inventory inv = getSubInventory(i);
if (inv != null) {
ServerUtils.sendDebugMessage("NBT: Item has a SubInv: " + inv);
if (!containerPasses(inv)) {
ServerUtils.sendDebugMessage("NBT: No pass C");
return false;
}
}
if (!Config.allowName && meta.hasDisplayName()) {
ServerUtils.sendDebugMessage("NBT: No pass N");
return false;
}
if (!Config.allowLore && meta.hasLore()) {
ServerUtils.sendDebugMessage("NBT: No Pass L ");
return false;
}
if (!Config.allowAttributes && meta.hasAttributeModifiers()) {
ServerUtils.sendDebugMessage("NBT: No pass A");
return false;
}
if (Config.globalMaxEnchant != 0 && hasIllegalEnchants(i)) {
ServerUtils.sendDebugMessage("NBT: No pass E");
return false;
}
ServerUtils.sendDebugMessage("NBT: All checks passed");
return true;
}
/*
[01:23:03 INFO]: [Sentinel] [DEBUG]: NBT: Detected creative mode action

View File

@@ -8,6 +8,9 @@ public class AntiUnicode {
public static void handleAntiUnicode(AsyncPlayerChatEvent e) {
String message = Text.removeFirstColor(e.getMessage());
String nonAllowed = message.replaceAll("[A-Za-z0-9\\[,./?><|\\]§()*&^%$#@!~`{}:;'\"-_]", "").trim();
if (message.matches("https?://(www\\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_\\+.~#?&//=]*)")) {
}
if (nonAllowed.length() != 0) {
e.getPlayer().sendMessage(Text.prefix(Sentinel.dict.get("unicode-warn")));
e.setCancelled(true);

View File

@@ -33,7 +33,7 @@ public class Authenticator {
String authStatus = "";
try {
URL url = new URL("https://sentinelauth.000webhostapp.com/index.html");
URL url = new URL("https://trouper.me/auth/sentinel");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
List<String> lines = readLines(reader);
@@ -45,7 +45,7 @@ public class Authenticator {
if (key.equals(licenseKey)) {
if (Arrays.asList(allowedArr).contains(serverID)) {
authStatus = "AUTHORIZED";
authStatus = "AUTHOReIZED";
return authStatus;
} else {
if (Arrays.asList(allowedArr).contains("minehut")) {

View File

@@ -42,7 +42,15 @@ public class FileUtils {
public static String createNBTLog(String contents) {
ServerUtils.sendDebugMessage("FileUtils: Creating NBT log");
String fileName = "nbt_log-" + Randomizer.generateID();
File file = new File(Sentinel.getInstance().getDataFolder() + "/LoggedNBT/" + fileName + ".txt");
File dataFolder = Sentinel.getInstance().getDataFolder();
File loggedNBTFolder = new File(dataFolder,"LoggedNBT");
if (!loggedNBTFolder.exists()) {
loggedNBTFolder.mkdirs();
}
File file = new File(loggedNBTFolder, fileName + ".txt");
try {
if (!file.exists()) {
file.createNewFile();