package net.knarcraft.minigames.util; import net.knarcraft.knarlib.util.TabCompletionHelper; import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.arena.Arena; import net.knarcraft.minigames.arena.ArenaHandler; import net.knarcraft.minigames.arena.EditablePropertyType; import net.knarcraft.minigames.arena.dropper.DropperArenaEditableProperty; import net.knarcraft.minigames.arena.parkour.ParkourArenaEditableProperty; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.permissions.Permission; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.StringJoiner; /** * A helper-class for common tab-completions */ public final class TabCompleteHelper { private static Map> tabCompleteSuggestions; private static List plugins; private static Map> permissions; private TabCompleteHelper() { } /** * Gets the names of all current arenas * * @return

All arena names

*/ public static @NotNull List getDropperArenas() { return getArenas(MiniGames.getInstance().getDropperArenaHandler()); } /** * Gets the names of all current arenas * * @return

All arena names

*/ public static @NotNull List getParkourArenas() { return getArenas(MiniGames.getInstance().getParkourArenaHandler()); } /** * Gets the names of all current arenas * * @return

All arena names

*/ private static @NotNull List getArenas(ArenaHandler arenaHandler) { List arenaNames = new ArrayList<>(); for (Arena arena : arenaHandler.getArenas().values()) { arenaNames.add(arena.getArenaName()); } return arenaNames; } /** * Gets the argument strings of all arena properties * * @return

All arena properties

*/ public static @NotNull List getDropperArenaProperties() { List arenaProperties = new ArrayList<>(); for (DropperArenaEditableProperty property : DropperArenaEditableProperty.values()) { arenaProperties.add(property.getArgumentString()); } return arenaProperties; } /** * Gets the argument strings of all arena properties * * @return

All arena properties

*/ public static @NotNull List getParkourArenaProperties() { List arenaProperties = new ArrayList<>(); for (ParkourArenaEditableProperty property : ParkourArenaEditableProperty.values()) { arenaProperties.add(property.getArgumentString()); } return arenaProperties; } /** * Gets tab-complete suggestions for the given property type * * @param propertyType

The property type to get suggestions for

* @return

The suggestions produced

*/ public static List getTabCompleteSuggestions(EditablePropertyType propertyType) { if (tabCompleteSuggestions == null) { tabCompleteSuggestions = new HashMap<>(); tabCompleteSuggestions.put(EditablePropertyType.LOCATION, getLocationSuggestions()); tabCompleteSuggestions.put(EditablePropertyType.ARENA_NAME, getNameSuggestions()); tabCompleteSuggestions.put(EditablePropertyType.HORIZONTAL_VELOCITY, getHorizontalVelocitySuggestions()); tabCompleteSuggestions.put(EditablePropertyType.VERTICAL_VELOCITY, getVerticalVelocitySuggestions()); tabCompleteSuggestions.put(EditablePropertyType.BLOCK_TYPE, getBlockTypeSuggestions()); tabCompleteSuggestions.put(EditablePropertyType.CHECKPOINT_CLEAR, getCheckpointClearSuggestions()); tabCompleteSuggestions.put(EditablePropertyType.MATERIAL_LIST, getMaterialListSuggestions()); tabCompleteSuggestions.put(EditablePropertyType.DOUBLE, getDoubleSuggestions()); } return tabCompleteSuggestions.get(propertyType); } /** * Gets suggestions for double values * * @return

A list of suggestions

*/ private static List getDoubleSuggestions() { List suggestions = new ArrayList<>(); suggestions.add("0"); suggestions.add("0.01"); suggestions.add("0.1"); suggestions.add("0.2"); suggestions.add("0.3"); suggestions.add("0.4"); suggestions.add("0.5"); suggestions.add("1"); return suggestions; } /** * Gets suggestions for a list of materials * * @return

A list of suggestions

*/ private static List getMaterialListSuggestions() { List suggestions = new ArrayList<>(); suggestions.add("LAVA,MAGMA_BLOCK"); suggestions.add("WATER,MAGMA_BLOCK,LAVA,+BUTTONS,+CORALS"); suggestions.add("CHAIN,END_ROD,LIGHTNING_ROD"); return suggestions; } /** * Gets suggestions for checkpointClear * * @return

A list of suggestions

*/ private static List getCheckpointClearSuggestions() { List suggestions = new ArrayList<>(); suggestions.add("true"); return suggestions; } /** * Gets suggestions for a block material * * @return

A list of suggestions

*/ private static List getBlockTypeSuggestions() { List materials = new ArrayList<>(); for (Material material : Material.values()) { if (material.isBlock()) { materials.add(material.name()); } } return materials; } /** * Gets suggestions for a vertical velocity * * @return

A list of suggestions

*/ private static List getVerticalVelocitySuggestions() { List velocities = new ArrayList<>(); velocities.add("0.01"); velocities.add("0.5"); velocities.add("1"); velocities.add("3"); velocities.add("10"); velocities.add("30"); velocities.add("75"); return velocities; } /** * Gets suggestions for a horizontal velocity * * @return

A list of suggestions

*/ private static List getHorizontalVelocitySuggestions() { List velocities = new ArrayList<>(); velocities.add("0.01"); velocities.add("0.1"); velocities.add("0.5"); velocities.add("1"); return velocities; } /** * Gets suggestions for an arena name * * @return

A list of suggestions

*/ private static List getNameSuggestions() { List locations = new ArrayList<>(); locations.add("DropperArena1"); locations.add("DropperArena2"); locations.add("ParkourArena1"); locations.add("ParkourArena2"); return locations; } /** * Gets suggestions for a location * * @return

A list of suggestions

*/ private static List getLocationSuggestions() { List locations = new ArrayList<>(); locations.add("here"); locations.add("x,y,z"); return locations; } /** * Gets the tab complete value for the permission typed * * @param typedNode

The full permission node typed by the player

* @return

All known valid auto-complete options

*/ public static List tabCompletePermission(String typedNode) { if (plugins == null) { loadAvailablePermissions(); } List output; if (typedNode.contains(".")) { List matchingPermissions = permissions.get(typedNode.substring(0, typedNode.lastIndexOf("."))); if (matchingPermissions == null) { output = new ArrayList<>(); } else { //Filter by the typed text output = TabCompletionHelper.filterMatchingStartsWith(matchingPermissions, typedNode); } } else { output = plugins; } //Add previous permissions in the comma-separated lists as a prefix return output; } /** * Loads all permissions available from bukkit plugins */ private static void loadAvailablePermissions() { plugins = new ArrayList<>(); permissions = new HashMap<>(); for (Permission permission : Bukkit.getPluginManager().getPermissions()) { loadPermission(permission.getName()); } } /** * Loads a given permission into the proper lists and maps * * @param permissionName

The permission to load

*/ private static void loadPermission(String permissionName) { String[] permissionParts = permissionName.split("\\."); if (permissionParts.length == 1 && !plugins.contains(permissionParts[0])) { plugins.add(permissionParts[0]); } else if (permissionParts.length > 1) { StringJoiner pathJoiner = new StringJoiner("."); for (int j = 0; j < permissionParts.length - 1; j++) { pathJoiner.add(permissionParts[j]); } String path = pathJoiner.toString(); List permissionList = permissions.computeIfAbsent(path, k -> new ArrayList<>()); permissionList.add(permissionName); loadPermission(path); } } }