Working on serializing location

This commit is contained in:
obvWolf
2024-02-12 07:26:09 -06:00
parent 634ac2fec9
commit daea256b4c
7 changed files with 101 additions and 14 deletions

View File

@@ -19,7 +19,6 @@ import java.util.logging.Logger;
public final class Sentinel extends JavaPlugin {
private static Sentinel instance;
private static final File cfgfile = new File("plugins/Sentinel/main-config.json");
private static final File nbtcfg = new File("plugins/Sentinel/nbt-config.json");

View File

@@ -6,6 +6,8 @@ import io.github.itzispyder.pdk.commands.CustomCommand;
import io.github.itzispyder.pdk.commands.Permission;
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.CMDBlockWhitelist;
import io.github.thetrouper.sentinel.server.functions.ProfanityFilter;
import io.github.thetrouper.sentinel.server.functions.SystemCheck;
import io.github.thetrouper.sentinel.server.functions.Telemetry;
@@ -15,11 +17,16 @@ import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.Material;
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.util.HashSet;
import java.util.Set;
@CommandRegistry(value = "sentinel",permission = @Permission("sentinel.debug"),printStackTrace = true)
public class SentinelCommand implements CustomCommand {
public static boolean debugMode;
@@ -28,9 +35,7 @@ public class SentinelCommand implements CustomCommand {
Player p = (Player) commandSender;
Sentinel instance = Sentinel.getInstance();
switch (args.get(0).toString()) {
case "commandblock", "cb" -> {
}
case "commandblock", "cb" -> handleCommandBlock(p,args);
case "reload" -> {
if (!Sentinel.isTrusted(p)) return;
p.sendMessage(Text.prefix("Reloading Sentinel!"));
@@ -41,16 +46,32 @@ public class SentinelCommand implements CustomCommand {
p.sendMessage(Text.prefix("Initiating a full system check!"));
SystemCheck.fullCheck(p);
}
case "debug" -> {
handleDebugCommand(p,args);
}
case "debug" -> handleDebugCommand(p,args);
}
}
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 "whitelist" -> {
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());
p.sendMessage(Text.prefix("Successfully whitelisted a &b" + Text.blockName(cb.getType().toString()) + "&7 with the command &a" + cb.getCommand() + "&7."));
return;
}
p.sendMessage(Text.prefix("Could not whitelist the &b" + Text.blockName(target.getType().toString()) + "&7 it is not a command block!"));
}
case "remove" -> {
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."));
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

@@ -0,0 +1,4 @@
package io.github.thetrouper.sentinel.data.cmdblocks;
public record Location(String world, double x, double y,double z) {
}

View File

@@ -1,6 +1,8 @@
package io.github.thetrouper.sentinel.data.cmdblocks;
import io.github.itzispyder.pdk.utils.misc.JsonSerializable;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import java.io.File;
import java.util.HashSet;
@@ -15,4 +17,5 @@ public class WhitelistStorage implements JsonSerializable<WhitelistStorage> {
}
public Set<WhitelistedBlock> whitelistedCMDBlocks = new HashSet<>();
}

View File

@@ -1,11 +1,16 @@
package io.github.thetrouper.sentinel.data.cmdblocks;
import io.papermc.paper.command.CommandBlockHolder;
import org.bukkit.Location;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.block.CommandBlock;
import java.util.UUID;
public record WhitelistedBlock(Location loc, UUID owner, CMDBlockType type, boolean conditional, boolean active ,String command) {
public record WhitelistedBlock(UUID owner, Location loc, CMDBlockType 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());
}
}

View File

@@ -5,14 +5,50 @@ 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.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.block.CommandBlock;
import org.bukkit.persistence.PersistentDataType;
import java.util.UUID;
public class CMDBlockWhitelist {
public static void addWhitelist(CommandBlock cb, UUID owner) {
//WhitelistedBlock commandblock = new WhitelistedBlock(cb.getLocation(),owner,getType(cb),)
//Sentinel.whitelist.whitelistedCMDBlocks.add(new WhitelistedBlock(cb.getLocation(),))
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());
Location wbl = WhitelistedBlock.fromSerialized(wb.loc());
for (WhitelistedBlock wl : Sentinel.whitelist.whitelistedCMDBlocks) {
Location wll = WhitelistedBlock.fromSerialized(wl.loc());
if (wll.distance(wbl) < 0.5) {
Sentinel.whitelist.whitelistedCMDBlocks.remove(wb);
break;
}
}
Sentinel.whitelist.whitelistedCMDBlocks.add(wb);
Sentinel.whitelist.save();
}
public static void remove(Location where) {
for (WhitelistedBlock cb : Sentinel.whitelist.whitelistedCMDBlocks) {
if (cb.loc().distance(where) < 0.5) {
Sentinel.whitelist.whitelistedCMDBlocks.remove(cb);
break;
}
}
Sentinel.whitelist.save();
}
public static WhitelistedBlock get(Location where) {
for (WhitelistedBlock cb : Sentinel.whitelist.whitelistedCMDBlocks) {
if (cb.loc().distance(where) < 0.5) {
return cb;
}
}
return null;
}
public static CMDBlockType getType(CommandBlock cb) {
@@ -29,4 +65,19 @@ public class CMDBlockWhitelist {
}
return null;
}
private static boolean getNBTBoolean(CommandBlock cmdBlock, String key) {
return cmdBlock.getPersistentDataContainer().has(
getKey(key),
PersistentDataType.BYTE
) && cmdBlock.getPersistentDataContainer().get(
getKey(key),
PersistentDataType.BYTE
) == 1;
}
// Helper method to get PersistentDataContainer key
private static NamespacedKey getKey(String key) {
return new NamespacedKey(Sentinel.getInstance(), key);
}
}

View File

@@ -67,4 +67,8 @@ public class Text {
}
return msg;
}
public static String blockName(String type) {
return type.replaceAll("_"," ").toLowerCase();
}
}