From a6dbf41febfcd37dc6c3e5d351ce7af8e7c789e7 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 12 Apr 2023 02:05:23 +0200 Subject: [PATCH] Finish it up --- .../commands/SchemSearchTestCommand.java | 40 ---------------- .../bungeecore/commands/StatCommand.java | 2 + .../network/BungeeNetworkHandler.java | 1 + .../SchematicSearchRequestHandler.java | 28 +++++++++++ .../bungeecore/util/SchematicSearch.java | 46 ++++++++++++++++--- .../steamwar/messages/BungeeCore.properties | 1 + 6 files changed, 71 insertions(+), 47 deletions(-) delete mode 100644 src/de/steamwar/bungeecore/commands/SchemSearchTestCommand.java create mode 100644 src/de/steamwar/bungeecore/network/handlers/SchematicSearchRequestHandler.java diff --git a/src/de/steamwar/bungeecore/commands/SchemSearchTestCommand.java b/src/de/steamwar/bungeecore/commands/SchemSearchTestCommand.java deleted file mode 100644 index 595f97d..0000000 --- a/src/de/steamwar/bungeecore/commands/SchemSearchTestCommand.java +++ /dev/null @@ -1,40 +0,0 @@ -package de.steamwar.bungeecore.commands; - -import de.steamwar.bungeecore.util.SchematicSearch; -import de.steamwar.command.PreviousArguments; -import de.steamwar.command.SWCommand; -import de.steamwar.command.TypeMapper; -import de.steamwar.sql.SchematicNode; -import de.steamwar.sql.SteamwarUser; -import net.md_5.bungee.api.CommandSender; -import net.md_5.bungee.api.connection.ProxiedPlayer; - -import java.util.Collection; - -public class SchemSearchTestCommand extends SWCommand { - public SchemSearchTestCommand() { - super("schemsearch"); - } - - - @Register - public void genericCommand(ProxiedPlayer player, SchematicNode node) { - SchematicSearch.queueSearch(player, node); - } - - @ClassMapper(SchematicNode.class) - public TypeMapper getSchematicNodeMapper() { - return new TypeMapper() { - - @Override - public SchematicNode map(CommandSender commandSender, String[] previousArguments, String s) { - return SchematicNode.getNodeFromPath(SteamwarUser.get(commandSender.getName()), s); - } - - @Override - public Collection tabCompletes(CommandSender sender, PreviousArguments previousArguments, String s) { - return SchematicNode.getNodeTabcomplete(SteamwarUser.get(sender.getName()), s); - } - }; - } -} diff --git a/src/de/steamwar/bungeecore/commands/StatCommand.java b/src/de/steamwar/bungeecore/commands/StatCommand.java index 33c3653..71a72b2 100644 --- a/src/de/steamwar/bungeecore/commands/StatCommand.java +++ b/src/de/steamwar/bungeecore/commands/StatCommand.java @@ -21,6 +21,7 @@ package de.steamwar.bungeecore.commands; import de.steamwar.bungeecore.Message; import de.steamwar.bungeecore.Node; +import de.steamwar.bungeecore.util.SchematicSearch; import de.steamwar.command.SWCommand; import net.md_5.bungee.api.CommandSender; @@ -37,6 +38,7 @@ public class StatCommand extends SWCommand { @Register public void genericCommand(CommandSender sender) { + Message.send("STAT_SEARCH_QUEUE", sender, SchematicSearch.getQueueSize()); Map serverCount = new HashMap<>(); try { Process process = new ProcessBuilder("ps", "x").start(); diff --git a/src/de/steamwar/bungeecore/network/BungeeNetworkHandler.java b/src/de/steamwar/bungeecore/network/BungeeNetworkHandler.java index 1cc4854..ab7a2b3 100644 --- a/src/de/steamwar/bungeecore/network/BungeeNetworkHandler.java +++ b/src/de/steamwar/bungeecore/network/BungeeNetworkHandler.java @@ -31,5 +31,6 @@ public class BungeeNetworkHandler { new ImALobbyHandler().register(); new InventoryCallbackHandler().register(); new PrepareSchemHandler().register(); + new SchematicSearchRequestHandler().register(); } } diff --git a/src/de/steamwar/bungeecore/network/handlers/SchematicSearchRequestHandler.java b/src/de/steamwar/bungeecore/network/handlers/SchematicSearchRequestHandler.java new file mode 100644 index 0000000..5078245 --- /dev/null +++ b/src/de/steamwar/bungeecore/network/handlers/SchematicSearchRequestHandler.java @@ -0,0 +1,28 @@ +package de.steamwar.bungeecore.network.handlers; + +import de.steamwar.bungeecore.BungeeCore; +import de.steamwar.bungeecore.util.SchematicSearch; +import de.steamwar.network.packets.PacketHandler; +import de.steamwar.network.packets.client.RequestSchematicSearchPacket; +import de.steamwar.sql.SchematicNode; +import de.steamwar.sql.SteamwarUser; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +public class SchematicSearchRequestHandler extends PacketHandler { + + @Handler + public void handle(RequestSchematicSearchPacket packet) { + SteamwarUser user = SteamwarUser.get(packet.getPlayerId()); + SchematicNode node = SchematicNode.getSchematicNode(packet.getSchematicId()); + ProxiedPlayer proxiedPlayer = BungeeCore.get().getProxy().getPlayer(user.getUUID()); + if(proxiedPlayer == null) { + return; + } + SchematicSearch.SchematicSearchBehavior behavior = SchematicSearch.SchematicSearchBehavior.builder() + .airAsAny(packet.isAirAsAny()) + .ignoreAir(packet.isIgnoreAir()) + .ignoreBlockData(packet.isIgnoreBlockData()) + .build(); + SchematicSearch.queueSearch(proxiedPlayer, node, behavior); + } +} diff --git a/src/de/steamwar/bungeecore/util/SchematicSearch.java b/src/de/steamwar/bungeecore/util/SchematicSearch.java index 4b4dcfb..2810984 100644 --- a/src/de/steamwar/bungeecore/util/SchematicSearch.java +++ b/src/de/steamwar/bungeecore/util/SchematicSearch.java @@ -10,6 +10,8 @@ import de.steamwar.sql.NodeData; import de.steamwar.sql.SchematicNode; import de.steamwar.sql.SteamwarUser; import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -50,13 +52,13 @@ public class SchematicSearch { }, 0, 1, TimeUnit.SECONDS); } - public static void queueSearch(ProxiedPlayer player, SchematicNode node ) { + public static void queueSearch(ProxiedPlayer player, SchematicNode node, SchematicSearchBehavior behavior) { SteamwarUser user = SteamwarUser.get(player.getUniqueId()); synchronized (searchQueue) { if(user.getUserGroup().isAdminGroup()) { - searchQueue.add(0, new SchematicSearch(player, node)); + searchQueue.add(0, new SchematicSearch(player, node, behavior)); } else { - searchQueue.add(new SchematicSearch(player, node)); + searchQueue.add(new SchematicSearch(player, node, behavior)); } } if(watchdog == null) { @@ -79,8 +81,25 @@ public class SchematicSearch { } } - private static List constructArguments(SteamwarUser user, File pattern) { - return Arrays.asList(searchBinary, "-T", "2", "-s", "-u", String.valueOf(user.getId()), "-o", "json:std", "-m", "50", pattern.getAbsolutePath()); + public static int getQueueSize() { + synchronized (searchQueue) { + return searchQueue.size(); + } + } + + private static List constructArguments(SteamwarUser user, File pattern, SchematicSearchBehavior behavior) { + List args = new ArrayList<>(Arrays.asList(searchBinary, "-T", "2", "-s", "-u", String.valueOf(user.getId()), "-o", "json:std", "-m", "50")); + if(behavior.isAirAsAny()) { + args.add("-A"); + } + if(behavior.isIgnoreAir()) { + args.add("-a"); + } + if(behavior.isIgnoreBlockData()) { + args.add("-d"); + } + args.add(pattern.getAbsolutePath()); + return args; } public static boolean removeFromQueue(ProxiedPlayer player) { @@ -120,13 +139,15 @@ public class SchematicSearch { private final ProxiedPlayer player; private final SchematicNode node; + private final SchematicSearchBehavior behavior; private File pattern; private ScheduledTask task; private Process process; - private SchematicSearch(ProxiedPlayer player, SchematicNode node) { + private SchematicSearch(ProxiedPlayer player, SchematicNode node, SchematicSearchBehavior behavior) { this.player = player; this.node = node; + this.behavior = behavior; } private static String readInputStream(InputStream is) throws IOException { @@ -157,7 +178,7 @@ public class SchematicSearch { } Message.send("SCHEMATIC_SEARCH_STARTED", player, node.getName()); pattern = schematicNodeToTempFile(data); - ProcessBuilder builder = new ProcessBuilder(constructArguments(SteamwarUser.get(player.getUniqueId()), pattern)); + ProcessBuilder builder = new ProcessBuilder(constructArguments(SteamwarUser.get(player.getUniqueId()), pattern, behavior)); process = builder.start(); InputStream stdout = process.getInputStream(); InputStream stderr = process.getErrorStream(); @@ -268,4 +289,15 @@ public class SchematicSearch { float percent; String name; } + + @Builder + @Getter + public static class SchematicSearchBehavior { + @Builder.Default + boolean airAsAny = false; + @Builder.Default + boolean ignoreAir = false; + @Builder.Default + boolean ignoreBlockData = false; + } } diff --git a/src/de/steamwar/messages/BungeeCore.properties b/src/de/steamwar/messages/BungeeCore.properties index cf71a79..ac5db50 100644 --- a/src/de/steamwar/messages/BungeeCore.properties +++ b/src/de/steamwar/messages/BungeeCore.properties @@ -121,6 +121,7 @@ MOD_USE_MODSENDER=§cPlease use the §c§lFabricModSender§c (https://steamwar.d #Various commands ALERT=§f{0} STAT_SERVER=§7Server §e{0}§8: §7Below limit §e{1} §7Server count §e{2} +STAT_SEARCH_QUEUE=§7Schematic search queue: §e{0} #Ban&Mute-Command PUNISHMENT_USAGE=§8/§7{0} §8[§eplayer§8] [§edd§8.§emm§8.§eyyyy §7or §edd§8.§emm§8.§eyyyy§8_§ehh§8:§emm §7or §eperma§8] [§ereason§8]