13
0

Merge pull request 'Update SchematicCommand.schemSearch' (#104) from SchemSearch into master
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Reviewed-on: #104
Reviewed-by: Chaoscaot <chaoscaot444@gmail.com>
Dieser Commit ist enthalten in:
Chaoscaot 2022-01-15 14:53:53 +01:00
Commit d8d5d150d2

Datei anzeigen

@ -20,6 +20,7 @@
package de.steamwar.schematicsystem.commands; package de.steamwar.schematicsystem.commands;
import de.steamwar.command.SWCommand; import de.steamwar.command.SWCommand;
import de.steamwar.command.SWCommandUtils;
import de.steamwar.command.TypeMapper; import de.steamwar.command.TypeMapper;
import de.steamwar.inventory.SWAnvilInv; import de.steamwar.inventory.SWAnvilInv;
import de.steamwar.inventory.SchematicSelector; import de.steamwar.inventory.SchematicSelector;
@ -36,6 +37,8 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.*; import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -44,6 +47,16 @@ import static de.steamwar.schematicsystem.commands.SchematicCommandUtils.*;
public class SchematicCommand extends SWCommand { public class SchematicCommand extends SWCommand {
private static final Map<String, TypeMapper<?>> 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<Enum<?>>) clazz));
searchMapper.put("-public", null);
}
public SchematicCommand() { public SchematicCommand() {
super("schematic", Bukkit.getPluginManager().getPlugin("Teamserver") == null ? new String[] {"schem", "/schem", "/schematic"} : new String[]{"schem"}); super("schematic", Bukkit.getPluginManager().getPlugin("Teamserver") == null ? new String[] {"schem", "/schem", "/schematic"} : new String[]{"schem"});
} }
@ -253,13 +266,60 @@ public class SchematicCommand extends SWCommand {
} }
@Register("search") @Register("search")
public void schemSearch(Player player, String query) { public void schemSearch(Player player, @Mapper("searchMapper") String... query) {
SteamwarUser user = getUser(player); SteamwarUser user = getUser(player);
List<SchematicNode> nodes = SchematicNode.filterSchems(user.getId(), node -> node.getName().contains(query)); int userId = user.getId();
List<Predicate<SchematicNode>> predicates = new ArrayList<>();
List<String> 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<SchematicNode> nodes = SchematicNode.filterSchems(userId, node -> {
for (Predicate<SchematicNode> predicate : predicates) {
if (!predicate.test(node)) {
return false;
}
}
return true;
});
player.sendMessage("§eSchematics §8(§e" + nodes.size() + "§8)"); player.sendMessage("§eSchematics §8(§e" + nodes.size() + "§8)");
nodes.forEach(node -> { nodes.forEach(node -> {
String br = node.generateBreadcrumbs(user); String br = node.generateBreadcrumbs(user);
TextComponent schematics = new TextComponent("§7" + br.replace(query, "§e§l" + query + "§7")); for (String s : nameList) {
br = br.replace(s, "§e§l" + s + "§8");
}
TextComponent schematics = new TextComponent("§7" + br);
schematics.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eSchematic verwalten").create())); schematics.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eSchematic verwalten").create()));
schematics.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/schem info " + br)); schematics.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/schem info " + br));
@ -594,6 +654,39 @@ public class SchematicCommand extends SWCommand {
}; };
} }
@Mapper(value = "searchMapper", local = true)
public TypeMapper<String> searchTypeMapper() {
return new TypeMapper<String>() {
@Override
public String map(CommandSender commandSender, String[] previousArguments, String s) {
return s;
}
@Override
public List<String> tabCompletes(CommandSender commandSender, String[] strings, String s) {
if (strings.length == 0) {
List<String> 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<String> list = new ArrayList<>(searchMapper.keySet());
list.add(s);
return list;
}
return mapper.tabCompletes(commandSender, strings, s);
}
List<String> list = new ArrayList<>(searchMapper.keySet());
list.add(s);
return list;
}
};
}
enum Extend { enum Extend {
AUSFAHREN, AUSFAHREN,
NORMAL NORMAL