diff --git a/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommand.java b/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommand.java index 2b0e4af..14578b4 100644 --- a/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommand.java +++ b/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommand.java @@ -20,6 +20,7 @@ package de.steamwar.schematicsystem.commands; import de.steamwar.command.SWCommand; +import de.steamwar.command.SWCommandUtils; import de.steamwar.command.TypeMapper; import de.steamwar.inventory.SWAnvilInv; import de.steamwar.inventory.SchematicSelector; @@ -37,6 +38,8 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import java.util.*; +import java.util.function.Function; +import java.util.function.Predicate; import java.util.logging.Level; import java.util.stream.Collectors; @@ -45,6 +48,16 @@ import static de.steamwar.schematicsystem.commands.SchematicCommandUtils.*; public class SchematicCommand extends SWCommand { + private static final Map> searchMapper = new HashMap<>(); + + static { + searchMapper.put("-type", SWCommandUtils.createMapper(SchematicType.values().stream().map(SchematicType::name).toArray(String[]::new))); + searchMapper.put("-owner", SWCommandUtils.createMapper(Function.identity(), (commandSender, s) -> Collections.singletonList(s))); + Class clazz = Material.class; + searchMapper.put("-item", SWCommandUtils.createEnumMapper((Class>) clazz)); + searchMapper.put("-public", null); + } + public SchematicCommand() { super("schematic", Bukkit.getPluginManager().getPlugin("Teamserver") == null ? new String[] {"schem", "/schem", "/schematic"} : new String[]{"schem"}); } @@ -153,9 +166,14 @@ public class SchematicCommand extends SWCommand { SchematicNode currentNode = mkdirs(layers, user, 1); SchematicNode node = SchematicNode.getNodeFromPath(user, String.join("/", layers)); - if (node != null && !node.getSchemtype().writeable() && node.getOwner() != user.getId()) { - player.sendMessage(SchematicSystem.PREFIX + "§cDu darfst diese Schematic nicht überschreiben"); - return; + if (node != null) { + if(node.isDir()) { + player.sendMessage(SchematicSystem.PREFIX + "§cDie Schematic ist ein Ordner"); + return; + } else if (!node.getSchemtype().writeable() || node.getOwner() != user.getId()) { + player.sendMessage(SchematicSystem.PREFIX + "§cDu darfst diese Schematic nicht überschreiben"); + return; + } } boolean newSchem = false; @@ -265,13 +283,64 @@ public class SchematicCommand extends SWCommand { } @Register("search") - public void schemSearch(Player player, String query) { + public void schemSearch(Player player, @Mapper("searchMapper") String... query) { SteamwarUser user = getUser(player); - List nodes = SchematicNode.filterSchems(user.getId(), node -> node.getName().contains(query)); + int userId = user.getId(); + List> predicates = new ArrayList<>(); + List nameList = new ArrayList<>(); + int i = 0; + while (i < query.length) { + String current = query[i]; + if (searchMapper.containsKey(current)) { + if (searchMapper.get(current) == null) { + if (current.equals("-public")) { + userId = 0; + } + } else if (i + 1 < query.length) { + int finalI = i; + switch (current) { + case "-type": + predicates.add(node -> node.getSchemtype().name().equalsIgnoreCase(query[finalI + 1])); + break; + case "-item": + predicates.add(node -> node.getItem().equalsIgnoreCase(query[finalI + 1])); + break; + case "-owner": + SteamwarUser steamwarUser = SteamwarUser.get(query[finalI + 1]); + if (steamwarUser == null) { + player.sendMessage(SchematicSystem.PREFIX + "§cDer Spieler §e" + query[finalI + 1] + " §cexistiert nicht"); + return; + } + predicates.add(node -> node.getOwner() == steamwarUser.getId()); + } + i++; + } + } else { + predicates.add(node -> node.getName().contains(current)); + nameList.add(current); + } + i++; + } + + List nodes = SchematicNode.filterSchems(userId, node -> { + for (Predicate predicate : predicates) { + if (!predicate.test(node)) { + return false; + } + } + return true; + }); player.sendMessage("§eSchematics §8(§e" + nodes.size() + "§8)"); nodes.forEach(node -> { String br = node.generateBreadcrumbs(user); - TextComponent schematics = new TextComponent("§7" + br.replace(query, "§e§l" + query + "§7")); + String displayName = br; + if (!nameList.isEmpty()) { + displayName = "§8" + br; + } + for (String s : nameList) { + displayName = displayName.replace(s, "§e§l" + s + "§8"); + } + 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)); @@ -606,6 +675,39 @@ public class SchematicCommand extends SWCommand { }; } + @Mapper(value = "searchMapper", local = true) + public TypeMapper searchTypeMapper() { + return new TypeMapper() { + @Override + public String map(CommandSender commandSender, String[] previousArguments, String s) { + return s; + } + + @Override + public List tabCompletes(CommandSender commandSender, String[] strings, String s) { + if (strings.length == 0) { + List list = new ArrayList<>(); + list.add(s); + list.addAll(searchMapper.keySet()); + return list; + } + String last = strings[strings.length - 1]; + if (searchMapper.containsKey(last)) { + TypeMapper mapper = searchMapper.get(last); + if (mapper == null) { + List list = new ArrayList<>(searchMapper.keySet()); + list.add(s); + return list; + } + return mapper.tabCompletes(commandSender, strings, s); + } + List list = new ArrayList<>(searchMapper.keySet()); + list.add(s); + return list; + } + }; + } + enum Extend { AUSFAHREN, NORMAL diff --git a/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommandUtils.java b/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommandUtils.java index 0ad1e4e..0e873c3 100644 --- a/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommandUtils.java +++ b/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommandUtils.java @@ -55,6 +55,10 @@ public class SchematicCommandUtils { player.sendMessage(SchematicSystem.PREFIX + "§cDeine Ordner brauchen schon einen Namen!"); return true; } + if(layer.length() > 64) { + player.sendMessage(SchematicSystem.PREFIX + "§cDer Name der Schematic ist zu lang"); + return true; + } if (layer.contains("/") || layer.contains("\\") || layer.contains("<") || @@ -315,7 +319,7 @@ public class SchematicCommandUtils { player.sendMessage(SchematicSystem.PREFIX + "§cEs konnte keine Daten in der Schematic gefunden werden."); } catch (Exception e) { player.sendMessage(SchematicSystem.PREFIX + "§cDie Schematic konnte nicht geladen werden."); - throw new SecurityException(e); + Bukkit.getLogger().log(Level.INFO, e.getMessage(), e); } }