From b55614575bc1cd4d626d5fa83c832f1757d0a924 Mon Sep 17 00:00:00 2001 From: Ivan Pekov Date: Tue, 30 Nov 2021 08:05:18 +0200 Subject: [PATCH] Apply lokka30 suggestions and also do some tab completion fixes --- .../plugin/bukkit/BukkitTreasuryPlugin.java | 30 +++++++++++++------ .../{Treasury.java => TreasuryBukkit.java} | 2 +- .../bukkit/command/TreasuryCommand.java | 4 +-- .../paper/PaperAsyncTabEnhancement.java | 18 ++++++----- .../paper/PaperBrigadierEnhancement.java | 9 +++--- .../vendor/paper/PaperEnhancements.java | 4 +-- .../treasury/plugin/core/TreasuryPlugin.java | 6 ++-- .../core/command/TreasuryBaseCommand.java | 8 +++-- .../subcommand/migrate/MigrateSubcommand.java | 2 +- .../core/config/messaging/Messages.java | 2 +- 10 files changed, 53 insertions(+), 32 deletions(-) rename bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/{Treasury.java => TreasuryBukkit.java} (98%) diff --git a/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/BukkitTreasuryPlugin.java b/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/BukkitTreasuryPlugin.java index 7fdb6fc0..dfb1928c 100644 --- a/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/BukkitTreasuryPlugin.java +++ b/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/BukkitTreasuryPlugin.java @@ -1,9 +1,6 @@ package me.lokka30.treasury.plugin.bukkit; import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -24,7 +21,6 @@ import me.lokka30.treasury.plugin.core.logging.Logger; import me.lokka30.treasury.plugin.core.schedule.Scheduler; import org.bukkit.Bukkit; -import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.ServicePriority; @@ -37,7 +33,7 @@ public class BukkitTreasuryPlugin extends TreasuryPlugin implements Logger, Scheduler, ConfigAdapter { - private final Treasury plugin; + private final TreasuryBukkit plugin; private Messages messages; private Settings settings; @@ -46,7 +42,7 @@ public class BukkitTreasuryPlugin extends TreasuryPlugin private List cachedPluginList = null; - public BukkitTreasuryPlugin(@NotNull Treasury plugin) { + public BukkitTreasuryPlugin(@NotNull TreasuryBukkit plugin) { this.plugin = Objects.requireNonNull(plugin, "plugin"); messagesFile = new File(plugin.getDataFolder(), "messages.yml"); settingsFile = new File(plugin.getDataFolder(), "settings.yml"); @@ -139,17 +135,33 @@ public void loadSettings() { @Override public @NotNull EconomyAPIVersion getEconomyAPIVersion() { - return Treasury.ECONOMY_API_VERSION; + return TreasuryBukkit.ECONOMY_API_VERSION; } @Override - public @NotNull List pluginsList() { + public @NotNull List pluginsListRegisteringProvider() { if (cachedPluginList != null) { return cachedPluginList; } cachedPluginList = Arrays.stream( Bukkit.getPluginManager().getPlugins() - ).map(Plugin::getName).collect(Collectors.toList()); + ).filter(pl -> { + List> registrations = Bukkit.getServicesManager().getRegistrations(pl); + if (registrations.isEmpty()) { + return false; + } + if (registrations.size() == 1) { + return registrations.get(0).getProvider().getClass().isAssignableFrom(EconomyProvider.class); + } + boolean hasRegistration = false; + for (RegisteredServiceProvider provider : registrations) { + if (provider.getProvider().getClass().isAssignableFrom(EconomyProvider.class)) { + hasRegistration = true; + break; + } + } + return hasRegistration; + }).map(Plugin::getName).collect(Collectors.toList()); return cachedPluginList; } diff --git a/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/Treasury.java b/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/TreasuryBukkit.java similarity index 98% rename from bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/Treasury.java rename to bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/TreasuryBukkit.java index dae8b3b9..1d565e60 100644 --- a/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/Treasury.java +++ b/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/TreasuryBukkit.java @@ -32,7 +32,7 @@ * the plugin. */ @SuppressWarnings("unused") -public class Treasury extends JavaPlugin { +public class TreasuryBukkit extends JavaPlugin { /** * This is Treasury's API version. (Not the same as api-version from plugin.yml!) diff --git a/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/command/TreasuryCommand.java b/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/command/TreasuryCommand.java index f370e0d4..247758ff 100644 --- a/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/command/TreasuryCommand.java +++ b/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/command/TreasuryCommand.java @@ -1,7 +1,7 @@ package me.lokka30.treasury.plugin.bukkit.command; import java.util.List; -import me.lokka30.treasury.plugin.bukkit.Treasury; +import me.lokka30.treasury.plugin.bukkit.TreasuryBukkit; import me.lokka30.treasury.plugin.core.command.TreasuryBaseCommand; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -12,7 +12,7 @@ public class TreasuryCommand implements TabExecutor { - public static void register(Treasury plugin) { + public static void register(TreasuryBukkit plugin) { PluginCommand cmd = plugin.getCommand("treasury"); CommandSources sources = new CommandSources(); plugin.getServer().getPluginManager().registerEvents(sources, plugin); diff --git a/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/vendor/paper/PaperAsyncTabEnhancement.java b/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/vendor/paper/PaperAsyncTabEnhancement.java index 3de8fec0..b8c4cc19 100644 --- a/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/vendor/paper/PaperAsyncTabEnhancement.java +++ b/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/vendor/paper/PaperAsyncTabEnhancement.java @@ -1,15 +1,14 @@ package me.lokka30.treasury.plugin.bukkit.vendor.paper; import com.destroystokyo.paper.event.server.AsyncTabCompleteEvent; -import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.stream.Collectors; -import org.bukkit.Bukkit; +import me.lokka30.treasury.plugin.core.TreasuryPlugin; +import me.lokka30.treasury.plugin.core.command.TreasuryBaseCommand; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; -import org.bukkit.plugin.Plugin; public class PaperAsyncTabEnhancement implements Listener { @@ -36,15 +35,20 @@ public void onAsyncTab(AsyncTabCompleteEvent event) { } if (parts.length > 2) { event.setCompletions(Collections.emptyList()); - event.setHandled(true); + } else { + event.setCompletions( + TreasuryBaseCommand.SUBCOMMAND_COMPLETIONS.stream() + .filter(s -> s.startsWith(subcommand.toLowerCase(Locale.ROOT))) + .collect(Collectors.toList()) + ); } + event.setHandled(true); } } private List getCompletions(String lastArg) { - return Arrays.stream( - Bukkit.getPluginManager().getPlugins() - ).map(Plugin::getName) + return TreasuryPlugin.getInstance().pluginsListRegisteringProvider() + .stream() .filter(name -> name.toLowerCase(Locale.ROOT).startsWith(lastArg)) .collect(Collectors.toList()); } diff --git a/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/vendor/paper/PaperBrigadierEnhancement.java b/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/vendor/paper/PaperBrigadierEnhancement.java index f783098a..68938d3d 100644 --- a/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/vendor/paper/PaperBrigadierEnhancement.java +++ b/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/vendor/paper/PaperBrigadierEnhancement.java @@ -9,10 +9,9 @@ import com.mojang.brigadier.suggestion.SuggestionProvider; import com.mojang.brigadier.tree.LiteralCommandNode; import java.util.Locale; -import org.bukkit.Bukkit; +import me.lokka30.treasury.plugin.core.TreasuryPlugin; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; -import org.bukkit.plugin.Plugin; public class PaperBrigadierEnhancement implements Listener { @@ -74,9 +73,9 @@ private SuggestionProvider plugins() { return (context, builder) -> { String lastArg = builder.getRemainingLowerCase(); - for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) { - if (plugin.getName().toLowerCase(Locale.ROOT).startsWith(lastArg)) { - builder.suggest(plugin.getName()); + for (String pluginRegistering : TreasuryPlugin.getInstance().pluginsListRegisteringProvider()) { + if (pluginRegistering.toLowerCase(Locale.ROOT).startsWith(lastArg)) { + builder.suggest(pluginRegistering); } } diff --git a/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/vendor/paper/PaperEnhancements.java b/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/vendor/paper/PaperEnhancements.java index 23dfb170..09e7eb2e 100644 --- a/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/vendor/paper/PaperEnhancements.java +++ b/bukkit/src/main/java/me/lokka30/treasury/plugin/bukkit/vendor/paper/PaperEnhancements.java @@ -1,11 +1,11 @@ package me.lokka30.treasury.plugin.bukkit.vendor.paper; import java.util.Arrays; -import me.lokka30.treasury.plugin.bukkit.Treasury; +import me.lokka30.treasury.plugin.bukkit.TreasuryBukkit; public class PaperEnhancements { - public static void enhance(Treasury plugin) { + public static void enhance(TreasuryBukkit plugin) { String pckg = plugin.getServer().getClass().getPackage().getName(); int[] version = Arrays.stream( pckg.substring(pckg.lastIndexOf('.') + 1) diff --git a/core/src/main/java/me/lokka30/treasury/plugin/core/TreasuryPlugin.java b/core/src/main/java/me/lokka30/treasury/plugin/core/TreasuryPlugin.java index ceef9549..d81e6425 100644 --- a/core/src/main/java/me/lokka30/treasury/plugin/core/TreasuryPlugin.java +++ b/core/src/main/java/me/lokka30/treasury/plugin/core/TreasuryPlugin.java @@ -143,10 +143,12 @@ public ProviderEconomy economyProviderProvider() { public abstract EconomyAPIVersion getEconomyAPIVersion(); /** - * Returns the plugins' names as a list with {@link String strings}. + * Returns the plugins' names, which are registering an economy provider, + * as a list with {@link String strings}, as this is being used in + * {@link me.lokka30.treasury.plugin.core.command.subcommand.migrate.MigrateSubcommand}. * * @return plugins' names */ @NotNull - public abstract List pluginsList(); + public abstract List pluginsListRegisteringProvider(); } diff --git a/core/src/main/java/me/lokka30/treasury/plugin/core/command/TreasuryBaseCommand.java b/core/src/main/java/me/lokka30/treasury/plugin/core/command/TreasuryBaseCommand.java index bcca2de2..e1223f46 100644 --- a/core/src/main/java/me/lokka30/treasury/plugin/core/command/TreasuryBaseCommand.java +++ b/core/src/main/java/me/lokka30/treasury/plugin/core/command/TreasuryBaseCommand.java @@ -4,8 +4,10 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Objects; +import java.util.stream.Collectors; import me.lokka30.treasury.plugin.core.command.subcommand.HelpSubcommand; import me.lokka30.treasury.plugin.core.command.subcommand.InfoSubcommand; import me.lokka30.treasury.plugin.core.command.subcommand.ReloadSubcommand; @@ -81,7 +83,7 @@ public void execute(@NotNull CommandSource sender, @NotNull String label, @NotNu } @NotNull - private final List subcommandCompletion = Arrays.asList("help", "info", "migrate", "reload"); + public static final List SUBCOMMAND_COMPLETIONS = Arrays.asList("help", "info", "migrate", "reload"); /** * Runs completions for the base /treasury command. @@ -96,7 +98,9 @@ public List complete(@NotNull CommandSource sender, @NotNull String labe if (args.length == 0) { return Collections.emptyList(); } else if (args.length == 1) { - return subcommandCompletion; + return SUBCOMMAND_COMPLETIONS.stream() + .filter(c -> c.startsWith(args[0].toLowerCase(Locale.ROOT))) + .collect(Collectors.toList()); } else { Subcommand subcommand = subcommands.get(args[0]); if (subcommand == null) { diff --git a/core/src/main/java/me/lokka30/treasury/plugin/core/command/subcommand/migrate/MigrateSubcommand.java b/core/src/main/java/me/lokka30/treasury/plugin/core/command/subcommand/migrate/MigrateSubcommand.java index 1969d3d3..5607f8fe 100644 --- a/core/src/main/java/me/lokka30/treasury/plugin/core/command/subcommand/migrate/MigrateSubcommand.java +++ b/core/src/main/java/me/lokka30/treasury/plugin/core/command/subcommand/migrate/MigrateSubcommand.java @@ -185,7 +185,7 @@ public List complete(@NotNull CommandSource source, @NotNull String labe } if ((args.length == 1 || args.length == 2) && source.hasPermission("treasury.command.treasury.migrate")) { String lastArg = args[args.length - 1].toLowerCase(Locale.ROOT); - return TreasuryPlugin.getInstance().pluginsList() + return TreasuryPlugin.getInstance().pluginsListRegisteringProvider() .stream() .filter(name -> name.toLowerCase(Locale.ROOT).startsWith(lastArg)) .collect(Collectors.toList()); diff --git a/core/src/main/java/me/lokka30/treasury/plugin/core/config/messaging/Messages.java b/core/src/main/java/me/lokka30/treasury/plugin/core/config/messaging/Messages.java index 7100ea73..c1b89ebc 100644 --- a/core/src/main/java/me/lokka30/treasury/plugin/core/config/messaging/Messages.java +++ b/core/src/main/java/me/lokka30/treasury/plugin/core/config/messaging/Messages.java @@ -28,7 +28,7 @@ @Comment("GitHub Repository: ") @Comment(" ") @Comment("## About this File") -@Comment("Welcome to the messages.yl file, here you may translate") +@Comment("Welcome to the messages.yml file, here you may translate") @Comment("and customise all of Treasury's messages (except for those") @Comment("logged to the console). Standard color codes are supported") @Comment("(e.g &a, &b, &c) and also hex color codes (e.g.")