From 3b0e4aa5aad96d5fc5af4f6eebd6f7ef3ab315c4 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Mon, 1 May 2023 16:12:45 +0200 Subject: [PATCH] Fix SchematicSearch --- .../bungeecore/util/SchematicSearch.java | 57 +++++++------------ .../steamwar/messages/BungeeCore.properties | 10 ++-- .../messages/BungeeCore_de.properties | 14 ++++- 3 files changed, 39 insertions(+), 42 deletions(-) diff --git a/src/de/steamwar/bungeecore/util/SchematicSearch.java b/src/de/steamwar/bungeecore/util/SchematicSearch.java index a3b8971..6d189ea 100644 --- a/src/de/steamwar/bungeecore/util/SchematicSearch.java +++ b/src/de/steamwar/bungeecore/util/SchematicSearch.java @@ -19,7 +19,6 @@ package de.steamwar.bungeecore.util; -import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -42,49 +41,37 @@ import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; import java.util.stream.Collectors; import java.util.zip.GZIPOutputStream; public class SchematicSearch { - - private static final Gson gson = new Gson(); - private static final String searchBinary = "/home/chaoscaot/schemsearch/target/release/schemsearch-cli"; - private static final List searchQueue = new ArrayList<>(); + private static final String SEARCH_BINARY = "/home/chaoscaot/schemsearch/target/release/schemsearch-cli"; + private static final LinkedBlockingQueue searchQueue = new LinkedBlockingQueue<>(); private static SchematicSearch currentSearch; - private static ScheduledTask watchdog; - private static void startQueueWatchdog() { - watchdog = BungeeCore.get().getProxy().getScheduler().schedule(BungeeCore.get(), () -> { - synchronized (searchQueue) { - if(searchQueue.isEmpty()) { - watchdog.cancel(); - watchdog = null; - return; - } + private static void startNext() { + if(currentSearch != null) { + return; + } - if(currentSearch == null) { - currentSearch = searchQueue.remove(0); - currentSearch.start(); - } + synchronized (searchQueue) { + if(searchQueue.isEmpty()) { + return; } - }, 0, 1, TimeUnit.SECONDS); + currentSearch = searchQueue.poll(); + currentSearch.start(); + } } 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, behavior)); - } else { - searchQueue.add(new SchematicSearch(player, node, behavior)); + searchQueue.add(new SchematicSearch(player, node, behavior)); + startNext(); + if (!searchQueue.isEmpty()) { + Message.send("SCHEMATIC_SEARCH_QUEUED", player); } } - if(watchdog == null) { - startQueueWatchdog(); - } else { - Message.send("SCHEMATIC_SEARCH_QUEUED", player); - } } public static void abortAll() { @@ -107,7 +94,7 @@ public class SchematicSearch { } 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")); + List args = new ArrayList<>(Arrays.asList(SEARCH_BINARY, "-T", "2", "-s", "-u", String.valueOf(user.getId()), "-o", "json:std", "-m", "50")); if(behavior.isAirAsAny()) { args.add("-A"); } @@ -269,13 +256,10 @@ public class SchematicSearch { if (matches.size() >= 49) { Message.send("SCHEMATIC_SEARCH_TOO_MANY_RESULTS", player); } - - end(); - } catch (IOException | InterruptedException e) { - end(); } catch (Exception e) { - end(); throw new SecurityException(e); + } finally { + end(); } }); } @@ -298,6 +282,7 @@ public class SchematicSearch { currentSearch = null; } } + startNext(); } @AllArgsConstructor diff --git a/src/de/steamwar/messages/BungeeCore.properties b/src/de/steamwar/messages/BungeeCore.properties index e43a398..4bc9401 100644 --- a/src/de/steamwar/messages/BungeeCore.properties +++ b/src/de/steamwar/messages/BungeeCore.properties @@ -696,11 +696,11 @@ MOD_ITEM_BACK=§7Back SCHEMATIC_SEARCH_QUEUED=§7Your search has been queued and will be executed shortly. SCHEMATIC_SEARCH_STARTED=§7Your search for "§e{0}§7" has started. SCHEMATIC_SEARCH_NO_RESULTS=§cNo results found. -SCHEMATIC_SEARCH_RESULT_HEADER=§7Searched in §e{0} §7schematics in§e{1}. +SCHEMATIC_SEARCH_RESULT_HEADER=§7Searched §e{0} §7schematics in§e{1}. SCHEMATIC_SEARCH_RESULT=§7{0}: §e{1}§7% §8(§e{2}§7,§e{3},§e{4}§8) -SCHEMATIC_SEARCH_RESULT_HOVER=§7Click to get more info about {0}. -SCHEMATIC_SEARCH_NOT_SUPPORTED=§cThis schematic is not supported by the schematic search. +SCHEMATIC_SEARCH_RESULT_HOVER=§7Click for more info about {0}. +SCHEMATIC_SEARCH_NOT_SUPPORTED=§cThis schematic is not supported because it is on an old format. SCHEMATIC_SEARCH_REMOVED_FROM_QUEUE=§cYour search has been removed from the queue because you switched servers. -SCHEMATIC_SEARCH_REMOVED_FROM_QUEUE_SOFTRELOAD=§cYour search has been removed from the queue because of an update. -SCHEMATIC_SEARCH_CANCELED_SOFTRELOAD=§cYour search has been canceled because of an update. +SCHEMATIC_SEARCH_REMOVED_FROM_QUEUE_SOFTRELOAD=§cYour search has been removed from the queue because of a software update. +SCHEMATIC_SEARCH_CANCELED_SOFTRELOAD=§cYour search has been cancelled because of a software update. SCHEMATIC_SEARCH_TOO_MANY_RESULTS=§cToo many results found. Please be more specific. \ No newline at end of file diff --git a/src/de/steamwar/messages/BungeeCore_de.properties b/src/de/steamwar/messages/BungeeCore_de.properties index 18c043f..8ef29df 100644 --- a/src/de/steamwar/messages/BungeeCore_de.properties +++ b/src/de/steamwar/messages/BungeeCore_de.properties @@ -647,4 +647,16 @@ ADVENT_CALENDAR_TITLE=§eAdventskalender ADVENT_CALENDAR_DAY=§7Tag§8: §e{0} ADVENT_CALENDAR_MESSAGE=§eHast du heute schon dein Geschenk geholt? ADVENT_CALENDAR_MESSAGE_HOVER=§eKlicken zum öffnen! -ADVENT_CALENDAR_OPEN=§7Du hast §e{0}§7 aus dem Adventskalender erhalten! \ No newline at end of file +ADVENT_CALENDAR_OPEN=§7Du hast §e{0}§7 aus dem Adventskalender erhalten! + +#Schematic Search +SCHEMATIC_SEARCH_QUEUED=§7Deine Suche wurde in die Warteschlange eingereiht. +SCHEMATIC_SEARCH_STARTED=§7Deine Suche nach §e{0} §7wurde gestartet. +SCHEMATIC_SEARCH_NO_RESULTS=§cEs wurden keine Ergebnisse gefunden. +SCHEMATIC_SEARCH_RESULT_HEADER=§e{0} §7Schematics in §e{1} §7durchsucht +SCHEMATIC_SEARCH_RESULT_HOVER=§7Klicke für mehr Informationen +SCHEMATIC_SEARCH_NOT_SUPPORTED=§cDiese Schematic ist in einem alten Format und kann nicht genutzt werden. +SCHEMATIC_SEARCH_REMOVED_FROM_QUEUE=§cDeine Suche wurde aus der Warteschlange entfernt, weil du den Server gewechselt hast. +SCHEMATIC_SEARCH_REMOVED_FROM_QUEUE_SOFTRELOAD=§cWegen eines Software Updates wurde deine Suche aus der Warteschlange entfernt. +SCHEMATIC_SEARCH_CANCELED_SOFTRELOAD=§cDeine Suche wurde wegen eines Software Updates abgebrochen. +SCHEMATIC_SEARCH_TOO_MANY_RESULTS=§cEs wurden zu viele Ergebnisse gefunden. Bitte spezifiziere deine Suche. \ No newline at end of file