From ccb9974e5fbd4a980a5bfb8238054beead9da847 Mon Sep 17 00:00:00 2001 From: Edith Boles Date: Mon, 4 Oct 2021 17:15:13 -0700 Subject: [PATCH] Add tab completion --- .../java/top/penowl/quidproquo/Commands.java | 20 +++++++++++++++---- .../top/penowl/quidproquo/QuidProQuo.java | 4 +++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main/java/top/penowl/quidproquo/Commands.java b/src/main/java/top/penowl/quidproquo/Commands.java index 1202372..9d507c5 100644 --- a/src/main/java/top/penowl/quidproquo/Commands.java +++ b/src/main/java/top/penowl/quidproquo/Commands.java @@ -1,10 +1,12 @@ package top.penowl.quidproquo; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import org.apache.commons.lang.WordUtils; import org.bukkit.ChatColor; @@ -12,14 +14,16 @@ import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; +import org.bukkit.util.StringUtil; import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.HoverEvent; -public class Commands implements CommandExecutor { +public class Commands implements CommandExecutor, TabCompleter { HashMap recipes = new HashMap(); @@ -31,6 +35,7 @@ public class Commands implements CommandExecutor { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + String key = String.join(" ", Arrays.asList(args).stream().map(string -> string.replace("_", " ")).collect(Collectors.toSet())).toLowerCase(); if (args.length < 1 || args[0].toLowerCase().equals("list")) { List ritualNames = new ArrayList(recipes.keySet()); Collections.sort(ritualNames); @@ -83,8 +88,7 @@ public class Commands implements CommandExecutor { playerSender.spigot().sendMessage(builder.create()); } return true; - } else if (recipes.containsKey(String.join(" ", args).toLowerCase())) { - String key = String.join(" ", args).toLowerCase(); + } else if (recipes.containsKey(key)) { ComponentBuilder builder = new ComponentBuilder(""); Ritual ritual = recipes.get(key); builder.append("\n\n\n\n\n\n\n\n\n\n\n"); @@ -109,7 +113,7 @@ public class Commands implements CommandExecutor { } } builder.append(" \n" + ChatColor.YELLOW + "" + ChatColor.BOLD + "Blood: " + ChatColor.RESET + "" + ChatColor.AQUA + String.valueOf(ritual.health / 2.0) + ChatColor.RED + " ♥\n"); - builder.append(ChatColor.YELLOW + "" + ChatColor.BOLD + "Backfire: " + ChatColor.RESET + "" + ChatColor.AQUA + String.valueOf(Math.round(ritual.backfire * 100)) + "%\n"); + builder.append(ChatColor.YELLOW + "" + ChatColor.BOLD + "Backfire: " + ChatColor.RESET + "" + ChatColor.AQUA + String.valueOf(Math.round(ritual.backfire * 100)) + "%"); if (sender instanceof Player) { Player playerSender = (Player) sender; playerSender.spigot().sendMessage(builder.create()); @@ -118,5 +122,13 @@ public class Commands implements CommandExecutor { } return false; } + + @Override + public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { + final List completions = new ArrayList<>(); + StringUtil.copyPartialMatches(args[0], recipes.keySet().stream().map(string -> string.replace(" ", "_")).collect(Collectors.toSet()), completions); + Collections.sort(completions); + return completions; + } } diff --git a/src/main/java/top/penowl/quidproquo/QuidProQuo.java b/src/main/java/top/penowl/quidproquo/QuidProQuo.java index e49ce26..1254a52 100644 --- a/src/main/java/top/penowl/quidproquo/QuidProQuo.java +++ b/src/main/java/top/penowl/quidproquo/QuidProQuo.java @@ -5,6 +5,7 @@ import java.util.HashMap; import java.util.Set; import java.util.UUID; +import org.bukkit.command.Command; import org.bukkit.event.HandlerList; import org.bukkit.plugin.java.JavaPlugin; import org.reflections.Reflections; @@ -89,7 +90,8 @@ public class QuidProQuo extends JavaPlugin { } // register command handler - getCommand("ritual").setExecutor(new Commands(rituals)); + Commands commands = new Commands(rituals); + getCommand("ritual").setExecutor(commands); } @Override