mirror of
https://github.com/Auxilor/EcoEnchants.git
synced 2025-01-28 01:21:24 +01:00
implement autocomplete for /enchantinfo
This commit is contained in:
parent
dfdf6e997f
commit
af271b07a0
@ -6,6 +6,7 @@ import org.bukkit.Bukkit;
|
|||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.command.PluginCommand;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -43,7 +44,9 @@ public abstract class AbstractCommand implements CommandExecutor, Registerable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void register() {
|
public final void register() {
|
||||||
Bukkit.getPluginCommand(name).setExecutor(this);
|
PluginCommand pluginCommand = Bukkit.getPluginCommand(name);
|
||||||
|
pluginCommand.setExecutor(this);
|
||||||
|
pluginCommand.setTabCompleter(new EcoEnchantsTabCompleter());
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void onExecute(CommandSender sender, List<String> args);
|
public abstract void onExecute(CommandSender sender, List<String> args);
|
||||||
|
@ -0,0 +1,60 @@
|
|||||||
|
package com.willfp.ecoenchants.command;
|
||||||
|
|
||||||
|
import com.willfp.ecoenchants.enchantments.EcoEnchant;
|
||||||
|
import com.willfp.ecoenchants.enchantments.EcoEnchants;
|
||||||
|
import org.bukkit.command.Command;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.command.TabCompleter;
|
||||||
|
import org.bukkit.util.StringUtil;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|
||||||
|
public class EcoEnchantsTabCompleter implements TabCompleter {
|
||||||
|
private final List<String> enchantsNames;
|
||||||
|
|
||||||
|
public EcoEnchantsTabCompleter() {
|
||||||
|
// Fetching the list of all enchants takes longer than anything inside onTabComplete, so caching is effective
|
||||||
|
enchantsNames = EcoEnchants.getAll().stream().map(EcoEnchant::getName).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String removePrefix(String s, String prefix) {
|
||||||
|
if (s != null && prefix != null && s.startsWith(prefix)) {
|
||||||
|
return s.substring(prefix.length());
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||||
|
try {
|
||||||
|
if (command.getLabel().equalsIgnoreCase("enchantinfo") && (sender.isOp() || sender.hasPermission("ecoenchants.enchantinfo"))) {
|
||||||
|
List<String> completions = new ArrayList<>();
|
||||||
|
|
||||||
|
if (args.length == 0) {
|
||||||
|
// Currently, this case is not ever reached
|
||||||
|
return enchantsNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringUtil.copyPartialMatches(String.join(" ", args), enchantsNames, completions);
|
||||||
|
|
||||||
|
if (args.length > 1) { // Remove all previous words from the candidate of completions
|
||||||
|
ArrayList<String> finishedArgs = new ArrayList<>(Arrays.asList(args));
|
||||||
|
finishedArgs.remove(args.length - 1);
|
||||||
|
|
||||||
|
String prefix = String.join(" ", finishedArgs);
|
||||||
|
completions = completions.stream().map(enchantName -> removePrefix(enchantName, prefix).trim()).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
Collections.sort(completions);
|
||||||
|
return completions;
|
||||||
|
}
|
||||||
|
} catch (Exception ignored) {}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -126,7 +126,6 @@ public final class CommandEnchantinfo extends AbstractCommand {
|
|||||||
final String finalTargets = allTargets;
|
final String finalTargets = allTargets;
|
||||||
final String finalConflicts = allConflicts;
|
final String finalConflicts = allConflicts;
|
||||||
final String finalMaxLevel = maxLevel;
|
final String finalMaxLevel = maxLevel;
|
||||||
|
|
||||||
Arrays.asList(ConfigManager.getLang().getMessage("enchantinfo").split("\\r?\\n")).forEach((string -> {
|
Arrays.asList(ConfigManager.getLang().getMessage("enchantinfo").split("\\r?\\n")).forEach((string -> {
|
||||||
string = string.replaceAll("%name%", finalName)
|
string = string.replaceAll("%name%", finalName)
|
||||||
.replaceAll("%description%", finalDescription)
|
.replaceAll("%description%", finalDescription)
|
||||||
|
@ -5,6 +5,7 @@ import com.willfp.ecoenchants.EcoEnchantsPlugin;
|
|||||||
import com.willfp.ecoenchants.command.commands.CommandEcodebug;
|
import com.willfp.ecoenchants.command.commands.CommandEcodebug;
|
||||||
import com.willfp.ecoenchants.command.commands.CommandEcoreload;
|
import com.willfp.ecoenchants.command.commands.CommandEcoreload;
|
||||||
import com.willfp.ecoenchants.command.commands.CommandEnchantinfo;
|
import com.willfp.ecoenchants.command.commands.CommandEnchantinfo;
|
||||||
|
import com.willfp.ecoenchants.command.EcoEnchantsTabCompleter;
|
||||||
import com.willfp.ecoenchants.config.ConfigManager;
|
import com.willfp.ecoenchants.config.ConfigManager;
|
||||||
import com.willfp.ecoenchants.display.EnchantDisplay;
|
import com.willfp.ecoenchants.display.EnchantDisplay;
|
||||||
import com.willfp.ecoenchants.display.packets.PacketOpenWindowMerchant;
|
import com.willfp.ecoenchants.display.packets.PacketOpenWindowMerchant;
|
||||||
|
Loading…
Reference in New Issue
Block a user