From b32ca71205b4a0672b230883c06fb202cecaef90 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Fri, 25 Dec 2020 21:22:59 +0100 Subject: [PATCH] Separate GUI from CLI Signed-off-by: Lixfel --- .../schematicsystem/commands/GUI.java | 53 +++++----------- .../commands/SchematicCommand.java | 61 ++++++++++++------- 2 files changed, 55 insertions(+), 59 deletions(-) diff --git a/SchematicSystem_Main/src/de/steamwar/schematicsystem/commands/GUI.java b/SchematicSystem_Main/src/de/steamwar/schematicsystem/commands/GUI.java index 81a6076..f7e9771 100644 --- a/SchematicSystem_Main/src/de/steamwar/schematicsystem/commands/GUI.java +++ b/SchematicSystem_Main/src/de/steamwar/schematicsystem/commands/GUI.java @@ -20,6 +20,7 @@ package de.steamwar.schematicsystem.commands; import com.google.common.collect.Lists; +import de.steamwar.inventory.SWAnvilInv; import de.steamwar.inventory.SWInventory; import de.steamwar.inventory.SWItem; import de.steamwar.inventory.SWListInv; @@ -198,12 +199,11 @@ class GUI { } static void list(Player p){ - list(p, SteamwarUser.get(p.getUniqueId()).getId()); + list(p, Schematic.getSchemsAccessibleByUser(SteamwarUser.get(p.getUniqueId()).getId()), false); } - static void list(Player p, int pid){ - List> schemList = new LinkedList<>(); - List schems = Schematic.getSchemsAccessibleByUser(pid); + private static void list(Player p, List schems, boolean publics){ + List> schemList = new ArrayList<>(); for(Schematic schem : schems) { Material m; @@ -218,43 +218,22 @@ class GUI { schemList.add(new SWListInv.SWListEntry<>(item, schem)); } - SWListInv inv = new SWListInv<>(p, "§eSchematicliste", schemList, (clickType, schem) -> { - p.closeInventory(); - info(p, schem); - }); - inv.setCallback(-999, (ClickType click) -> p.closeInventory()); + SWListInv inv = new SWListInv<>(p, "§eSchematicliste", false, schemList, (clickType, schem) -> info(p, schem)); + if(publics) + inv.setItem(48, Material.BUCKET, "§7Eigene Schematics", clickType -> list(p)); + else + inv.setItem(48, Material.GLASS, "§7Public Schematics", clickType -> list(p, Schematic.getSchemsAccessibleByUser(0), true)); + inv.setItem(50, Material.NAME_TAG, "§7Suche", clickType -> search(p, schems)); inv.open(); } - static boolean listSearch(Player player, String search) { - List> schemList = new LinkedList<>(); - List filteredSchems = new ArrayList<>(); - - for(Schematic currentSchem : Schematic.getSchemsAccessibleByUser(player.getUniqueId())) { - if(currentSchem.getSchemName().toLowerCase().contains(search.toLowerCase())) - filteredSchems.add(currentSchem); - } - - if(filteredSchems.isEmpty()) - return false; - - for(Schematic currentSchem : filteredSchems) { - Material m; - if (currentSchem.getItem().isEmpty()) - m = SWItem.getMaterial("CAULDRON_ITEM"); - else - m = SWItem.getMaterial(currentSchem.getItem()); - - SWItem item = new SWItem(m, "§e" + currentSchem.getSchemName(), Collections.singletonList("§7" + currentSchem.getSchemType().name()), !currentSchem.getSchemType().writeable(), click -> {}); - schemList.add(new SWListInv.SWListEntry<>(item, currentSchem)); - } - - SWListInv inv = new SWListInv<>(player, "§eSuchergebnisse", schemList, (clickType, schem) -> { - player.closeInventory(); - info(player, schem); + static void search(Player p, List schems){ + SWAnvilInv inv = new SWAnvilInv(p, "§7Schematicsuche"); + inv.setCallback(term -> { + ArrayList schemCopy = new ArrayList<>(schems); + schemCopy.removeIf(schem -> !schem.getSchemName().toLowerCase().contains(term.toLowerCase())); + list(p, schemCopy, false); }); - inv.setCallback(-999, (ClickType click) -> player.closeInventory()); inv.open(); - return true; } } diff --git a/SchematicSystem_Main/src/de/steamwar/schematicsystem/commands/SchematicCommand.java b/SchematicSystem_Main/src/de/steamwar/schematicsystem/commands/SchematicCommand.java index 32a0957..3e601fb 100644 --- a/SchematicSystem_Main/src/de/steamwar/schematicsystem/commands/SchematicCommand.java +++ b/SchematicSystem_Main/src/de/steamwar/schematicsystem/commands/SchematicCommand.java @@ -97,35 +97,40 @@ public class SchematicCommand implements CommandExecutor { private void list(Player player, String[] args){ if(args.length <= 1){ - sendPlayerSchematicList(0, SteamwarUser.get(player.getUniqueId()).getId(), player); + sendPlayerSchematicList(0, player, "/schem list ", Schematic.getSchemsAccessibleByUser(SteamwarUser.get(player.getUniqueId()).getId())); return; } if("public".equalsIgnoreCase(args[1])){ - GUI.list(player, 0); + if(args.length == 2){ + sendPlayerSchematicList(0, player, "/schem list public ", Schematic.getSchemsAccessibleByUser(0)); + return; + } + + try { + sendPlayerSchematicList(Integer.parseInt(args[2]), player, "/schem list public ", Schematic.getSchemsAccessibleByUser(0)); + } catch (NumberFormatException ex) { + player.sendMessage(SchematicSystem.PREFIX + "§cUngültige Seitenzahl"); + } return; } try { - sendPlayerSchematicList(Integer.parseInt(args[1]), SteamwarUser.get(player.getUniqueId()).getId(), player); + sendPlayerSchematicList(Integer.parseInt(args[1]), player, "/schem list ", Schematic.getSchemsAccessibleByUser(SteamwarUser.get(player.getUniqueId()).getId())); } catch (NumberFormatException ex) { player.sendMessage(SchematicSystem.PREFIX + "§cUngültige Seitenzahl"); } } - private boolean schemnameForbidden(String schematicName) { - return schematicName.contains("/") || - schematicName.contains("\\") || - schematicName.contains("<") || - schematicName.contains(">") || - schematicName.contains("^") || - schematicName.contains("°") || - schematicName.contains("'") || - schematicName.contains("\""); - } - private boolean invalidSchemName(Player player, String name){ - if(schemnameForbidden(name)){ + if(name.contains("/") || + name.contains("\\") || + name.contains("<") || + name.contains(">") || + name.contains("^") || + name.contains("°") || + name.contains("'") || + name.contains("\"")){ player.sendMessage(SchematicSystem.PREFIX + "§cDer angegebene Schematicname enthält verbotene Zeichen"); return true; } @@ -363,8 +368,20 @@ public class SchematicCommand implements CommandExecutor { help(player); return; } - if(!GUI.listSearch(player, args[1])) - player.sendMessage("§cEs wurden keine Schematics gefunden!"); + + List schems = Schematic.getSchemsAccessibleByUser(SteamwarUser.get(player.getUniqueId()).getId()); + schems.removeIf(schem -> !schem.getSchemName().toLowerCase().contains(args[1].toLowerCase())); + + if(args.length == 2){ + sendPlayerSchematicList(0, player, "/schem search " + args[1] + " ", schems); + return; + } + + try { + sendPlayerSchematicList(Integer.parseInt(args[2]), player, "/schem search " + args[1] + " ", schems); + } catch (NumberFormatException ex) { + player.sendMessage(SchematicSystem.PREFIX + "§cUngültige Seitenzahl"); + } } static void download(Player player, Schematic schematic){ @@ -474,8 +491,8 @@ public class SchematicCommand implements CommandExecutor { player.sendMessage("§8/§eschem delmember §8[§7Schematic§8] §8[§7Spieler§8] - §7Entfernt einen Spieler von einer Schematic"); } - private void sendPlayerSchematicList(int currentPage, int schemOwner, Player player) { - List schematicList = Schematic.getSchemsAccessibleByUser(schemOwner); + private void sendPlayerSchematicList(int currentPage, Player player, String precommand, List schematicList) { + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); if(schematicList.isEmpty()) { player.sendMessage(SchematicSystem.PREFIX + "§cDu hast noch keine Schematics"); @@ -504,7 +521,7 @@ public class SchematicCommand implements CommandExecutor { Schematic schematic = schematicList.get(i); String schematicPlayer = ""; - if(schematic.getSchemOwner() != schemOwner) + if(schematic.getSchemOwner() != user.getId()) schematicPlayer = " §8von §7" + SteamwarUser.get(schematic.getSchemOwner()).getUserName(); TextComponent schematics = new TextComponent("§e" + schematic.getSchemType().getKuerzel() + " §7" + schematic.getSchemName() + schematicPlayer); @@ -522,7 +539,7 @@ public class SchematicCommand implements CommandExecutor { if(currentPage > 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 " + (currentPage - 1))); + beforePage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, precommand + (currentPage - 1))); }else beforePage.setColor(ChatColor.RED); @@ -530,7 +547,7 @@ public class SchematicCommand implements CommandExecutor { if(currentPage < pages-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 " + (currentPage + 1))); + nextPage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, precommand + (currentPage + 1))); }else nextPage.setColor(ChatColor.RED); -- 2.39.5