From 737a6b5a0d88019611deaece9f1ba7a3fd7de7b9 Mon Sep 17 00:00:00 2001 From: Chaos Date: Sun, 16 Jan 2022 13:16:06 +0100 Subject: [PATCH 1/2] Pages --- .../commands/SchematicCommand.java | 25 +++++--------- .../commands/SchematicCommandUtils.java | 34 +++++++++++++++---- pom.xml | 5 +++ 3 files changed, 41 insertions(+), 23 deletions(-) diff --git a/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommand.java b/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommand.java index b50034f..84d729a 100644 --- a/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommand.java +++ b/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommand.java @@ -79,7 +79,7 @@ public class SchematicCommand extends SWCommand { @Register("list") @Register({"list", "/"}) public void schemList(Player player, @OptionalValue("0") int page) { - renderSchemlist(player, SchematicNode.getSchematicsAccessibleByUser(getUser(player).getId(), 0), page, "", false, null); + renderSchemlist(player, SchematicNode.getSchematicsAccessibleByUser(getUser(player).getId(), 0), page, "", false, null, s -> s, integer -> "/schem list " + integer); } @Register({"list", "public"}) @@ -90,7 +90,7 @@ public class SchematicCommand extends SWCommand { @Register({"list", "public"}) @Register({"list", "public", "/"}) public void schemListPublic(Player player, @OptionalValue("0") int page) { - renderSchemlist(player, SchematicNode.getSchematicsAccessibleByUser(0, 0), page, "", true, null); + renderSchemlist(player, SchematicNode.getSchematicsAccessibleByUser(0, 0), page, "", true, null, s -> s, integer -> "/schem list public " + integer); } @@ -105,7 +105,7 @@ public class SchematicCommand extends SWCommand { if (!node.isDir()) { player.sendMessage(SchematicSystem.PREFIX + "Das ist eine Schematic und kein Ordner"); } else { - renderSchemlist(player, SchematicNode.getSchematicNodeInNode(node), page, node.generateBreadcrumbs(user), node.getOwner() == 0, node); + renderSchemlist(player, SchematicNode.getSchematicNodeInNode(node), page, node.generateBreadcrumbs(user), node.getOwner() == 0, node, s -> s, integer -> "/schem list " + node.generateBreadcrumbs(user) + " " + integer); } } @@ -266,7 +266,7 @@ public class SchematicCommand extends SWCommand { } @Register("search") - public void schemSearch(Player player, @Mapper("searchMapper") String... query) { + public void schemSearch(Player player, @OptionalValue("0") int page, @Mapper("searchMapper") String... query) { SteamwarUser user = getUser(player); int userId = user.getId(); List> predicates = new ArrayList<>(); @@ -313,19 +313,12 @@ public class SchematicCommand extends SWCommand { } return true; }); - player.sendMessage("§eSchematics §8(§e" + nodes.size() + "§8)"); - nodes.forEach(node -> { - String br = node.generateBreadcrumbs(user); - String displayName = br; - for (String s : nameList) { - displayName = displayName.replace(s, "§e§l" + s + "§8"); + renderSchemlist(player, nodes, page, "", userId == 0, null, s -> { + for (String ss : nameList) { + s = s.replace(ss, "§e§l" + ss + "§7"); } - TextComponent schematics = new TextComponent("§7" + displayName); - schematics.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eSchematic verwalten").create())); - schematics.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/schem info " + br)); - - player.spigot().sendMessage(schematics); - }); + return "§7" + s; + }, integer -> "/schem search " + integer + " " + Arrays.stream(query).reduce((s, s2) -> s + " " + s2).get()); } @Register("download") diff --git a/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommandUtils.java b/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommandUtils.java index 469f178..334dee6 100644 --- a/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommandUtils.java +++ b/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommandUtils.java @@ -26,6 +26,8 @@ import de.steamwar.schematicsystem.AutoCheckResult; import de.steamwar.schematicsystem.CheckSchemType; import de.steamwar.schematicsystem.SchematicSystem; import de.steamwar.sql.*; +import lombok.Builder; +import lombok.Getter; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.ComponentBuilder; @@ -36,6 +38,8 @@ import org.bukkit.entity.Player; import java.time.format.DateTimeFormatter; import java.util.*; +import java.util.function.IntFunction; +import java.util.function.UnaryOperator; import java.util.logging.Level; import java.util.stream.Collectors; @@ -77,18 +81,23 @@ public class SchematicCommandUtils { return false; } - public static void renderSchemlist(Player player, List nodes, int chunk, String breadcrumbs, boolean isPublic, SchematicNode parent) { + public static void renderSchemlist(Player player, + List nodes, + int chunk, + SchematicNode parent, + SchematicListBehavior behavior) { nodes.sort(Comparator.comparing(SchematicNode::getName)); SteamwarUser user = getUser(player); int pageCount = (int) Math.ceil(nodes.size() / (double) CHUNK_SIZE); player.sendMessage("§eSchematics §8(§e" + nodes.size() + "§8)"); + String breadcrumbs = parent == null ? "" : parent.generateBreadcrumbs(user); player.sendMessage("§7Aktueller Pfad: §e" + (breadcrumbs.isEmpty() ? "/" : breadcrumbs)); if (!breadcrumbs.isEmpty()) { TextComponent back = new TextComponent("§e../"); String str = breadcrumbs.substring(0, Math.max(0, breadcrumbs.substring(0, breadcrumbs.length() - 1).lastIndexOf("/"))); back.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eZurück gehen" + str).create())); - back.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/schem list " + (isPublic ? "public" : "") + " " + str)); + back.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/schem list " + (behavior.isPublics() ? "public" : "") + " " + str)); player.spigot().sendMessage(back); } @@ -109,7 +118,7 @@ public class SchematicCommandUtils { nodeString.append("§e"); } - nodeString.append(node.getName()); + nodeString.append(behavior.getRenderHook().apply(node.getName())); if (node.isDir()) { nodeString.append("/"); @@ -123,10 +132,10 @@ public class SchematicCommandUtils { if (node.isDir()) { schematics.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eOrdner anzeigen").create())); - schematics.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/schem list " + (isPublic ? "public" : "") + " " + breadcrumbs + node.getName() + "/")); + schematics.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/schem list " + (behavior.isPublics() ? "public" : "") + " " + breadcrumbs + node.getName() + "/")); } else { schematics.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eSchematic verwalten").create())); - schematics.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/schem info " + (isPublic ? "public" : "") + " " + breadcrumbs + node.getName())); + schematics.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/schem info " + (behavior.isPublics() ? "public" : "") + " " + breadcrumbs + node.getName())); } player.spigot().sendMessage(schematics); @@ -144,7 +153,7 @@ public class SchematicCommandUtils { if (chunk > 0) { beforePage.setColor(ChatColor.YELLOW); beforePage.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eVorherige Seite").create())); - beforePage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/schem list " + (isPublic ? "public" : "") + " " + breadcrumbs + " " + (chunk - 1))); + beforePage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, behavior.getPageCommandGen().apply(chunk - 1))); } else { beforePage.setColor(ChatColor.RED); } @@ -153,7 +162,7 @@ public class SchematicCommandUtils { if (chunk < pageCount - 1) { nextPage.setColor(ChatColor.YELLOW); nextPage.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eNächste Seite").create())); - nextPage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/schem list " + (isPublic ? "public" : "") + " " + breadcrumbs + " " + (chunk + 1))); + nextPage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, behavior.getPageCommandGen().apply(chunk + 1))); } else { nextPage.setColor(ChatColor.RED); } @@ -162,6 +171,17 @@ public class SchematicCommandUtils { player.spigot().sendMessage(beforePage); } + @Getter + @Builder(setterPrefix = "set") + public static class SchematicListBehavior { + public static final SchematicListBehavior DEFAULT = SchematicListBehavior.builder().build(); + + @Builder.Default private boolean showPath = true; + @Builder.Default private boolean publics = false; + @Builder.Default private UnaryOperator renderHook = s -> s; + @Builder.Default private IntFunction pageCommandGen = value -> "/schem list " + value; + } + public static void printSchemInfo(Player player, SchematicNode node) { SteamwarUser user = getUser(player); player.sendMessage("§7Schematic: §e" + node.getName()); diff --git a/pom.xml b/pom.xml index b1a8c5c..cc39eaa 100644 --- a/pom.xml +++ b/pom.xml @@ -45,5 +45,10 @@ system ${main.basedir}/lib/SpigotCore.jar + + org.projectlombok + lombok + 1.18.22 + \ No newline at end of file -- 2.39.5 From 92bcc4b205dcc19b0a8b6ccfb856f0f7253c68c1 Mon Sep 17 00:00:00 2001 From: Chaos Date: Wed, 19 Jan 2022 16:32:16 +0100 Subject: [PATCH 2/2] More Rewrite --- .../schematicsystem/commands/SchematicCommand.java | 10 +++++----- .../commands/SchematicCommandUtils.java | 1 - 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommand.java b/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommand.java index 84d729a..47bfc12 100644 --- a/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommand.java +++ b/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommand.java @@ -79,7 +79,7 @@ public class SchematicCommand extends SWCommand { @Register("list") @Register({"list", "/"}) public void schemList(Player player, @OptionalValue("0") int page) { - renderSchemlist(player, SchematicNode.getSchematicsAccessibleByUser(getUser(player).getId(), 0), page, "", false, null, s -> s, integer -> "/schem list " + integer); + renderSchemlist(player, SchematicNode.getSchematicsAccessibleByUser(getUser(player).getId(), 0), page, null, SchematicListBehavior.DEFAULT); } @Register({"list", "public"}) @@ -90,7 +90,7 @@ public class SchematicCommand extends SWCommand { @Register({"list", "public"}) @Register({"list", "public", "/"}) public void schemListPublic(Player player, @OptionalValue("0") int page) { - renderSchemlist(player, SchematicNode.getSchematicsAccessibleByUser(0, 0), page, "", true, null, s -> s, integer -> "/schem list public " + integer); + renderSchemlist(player, SchematicNode.getSchematicsAccessibleByUser(0, 0), page, null, SchematicListBehavior.builder().setPublics(true).setPageCommandGen(integer -> "/schem list public " + integer).build()); } @@ -105,7 +105,7 @@ public class SchematicCommand extends SWCommand { if (!node.isDir()) { player.sendMessage(SchematicSystem.PREFIX + "Das ist eine Schematic und kein Ordner"); } else { - renderSchemlist(player, SchematicNode.getSchematicNodeInNode(node), page, node.generateBreadcrumbs(user), node.getOwner() == 0, node, s -> s, integer -> "/schem list " + node.generateBreadcrumbs(user) + " " + integer); + renderSchemlist(player, SchematicNode.getSchematicNodeInNode(node), page, node, SchematicListBehavior.builder().setPublics(node.getOwner() == 0).setPageCommandGen(value -> "/schem list " + (node.getOwner()==0?"public ":"") + node.generateBreadcrumbs(user) + " " + value).build()); } } @@ -313,12 +313,12 @@ public class SchematicCommand extends SWCommand { } return true; }); - renderSchemlist(player, nodes, page, "", userId == 0, null, s -> { + renderSchemlist(player, nodes, page, null, SchematicListBehavior.builder().setPublics(userId == 0).setShowPath(false).setRenderHook(s -> { for (String ss : nameList) { s = s.replace(ss, "§e§l" + ss + "§7"); } return "§7" + s; - }, integer -> "/schem search " + integer + " " + Arrays.stream(query).reduce((s, s2) -> s + " " + s2).get()); + }).setPageCommandGen(integer -> "/schem search " + integer + " " + Arrays.stream(query).reduce((s, s2) -> s + " " + s2).orElse("")).build()); } @Register("download") diff --git a/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommandUtils.java b/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommandUtils.java index 334dee6..aa9940c 100644 --- a/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommandUtils.java +++ b/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommandUtils.java @@ -86,7 +86,6 @@ public class SchematicCommandUtils { int chunk, SchematicNode parent, SchematicListBehavior behavior) { - nodes.sort(Comparator.comparing(SchematicNode::getName)); SteamwarUser user = getUser(player); int pageCount = (int) Math.ceil(nodes.size() / (double) CHUNK_SIZE); -- 2.39.5