Skip to content

Commit

Permalink
Apply lokka30 suggestions and also do some tab completion fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MrIvanPlays committed Nov 30, 2021
1 parent db27b9e commit b556145
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -46,7 +42,7 @@ public class BukkitTreasuryPlugin extends TreasuryPlugin

private List<String> 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");
Expand Down Expand Up @@ -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<String> pluginsList() {
public @NotNull List<String> pluginsListRegisteringProvider() {
if (cachedPluginList != null) {
return cachedPluginList;
}
cachedPluginList = Arrays.stream(
Bukkit.getPluginManager().getPlugins()
).map(Plugin::getName).collect(Collectors.toList());
).filter(pl -> {
List<RegisteredServiceProvider<?>> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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<String> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -74,9 +73,9 @@ private SuggestionProvider<BukkitBrigadierCommandSource> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> pluginsList();
public abstract List<String> pluginsListRegisteringProvider();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,7 +83,7 @@ public void execute(@NotNull CommandSource sender, @NotNull String label, @NotNu
}

@NotNull
private final List<String> subcommandCompletion = Arrays.asList("help", "info", "migrate", "reload");
public static final List<String> SUBCOMMAND_COMPLETIONS = Arrays.asList("help", "info", "migrate", "reload");

/**
* Runs completions for the base /treasury command.
Expand All @@ -96,7 +98,9 @@ public List<String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public List<String> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
@Comment("GitHub Repository: <https://github.com/lokka30/Treasury/>")
@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.")
Expand Down

0 comments on commit b556145

Please sign in to comment.