Initial BungeeCord edition
This commit is contained in:
parent
4416d55173
commit
23e8e80191
75 changed files with 1931 additions and 396 deletions
|
|
@ -3,23 +3,25 @@ package net.frankheijden.serverutils.bukkit;
|
|||
import co.aikar.commands.BukkitCommandCompletionContext;
|
||||
import co.aikar.commands.CommandCompletions;
|
||||
import co.aikar.commands.PaperCommandManager;
|
||||
import java.io.File;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.commands.CommandPlugins;
|
||||
import net.frankheijden.serverutils.bukkit.commands.CommandServerUtils;
|
||||
import net.frankheijden.serverutils.bukkit.config.Config;
|
||||
import net.frankheijden.serverutils.bukkit.config.Messenger;
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitPlugin;
|
||||
import net.frankheijden.serverutils.bukkit.listeners.MainListener;
|
||||
import net.frankheijden.serverutils.bukkit.managers.VersionManager;
|
||||
import net.frankheijden.serverutils.bukkit.reflection.BukkitReflection;
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||
import net.frankheijden.serverutils.bukkit.reflection.RCommandMap;
|
||||
import net.frankheijden.serverutils.bukkit.reflection.RCraftServer;
|
||||
import net.frankheijden.serverutils.bukkit.tasks.UpdateCheckerTask;
|
||||
import net.frankheijden.serverutils.bukkit.utils.BukkitUtils;
|
||||
import net.frankheijden.serverutils.common.ServerUtilsApp;
|
||||
import net.frankheijden.serverutils.common.config.Config;
|
||||
import net.frankheijden.serverutils.common.config.Messenger;
|
||||
import net.frankheijden.serverutils.common.providers.PluginProvider;
|
||||
import org.bstats.bukkit.Metrics;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
|
|
@ -30,31 +32,33 @@ import org.bukkit.plugin.java.JavaPlugin;
|
|||
|
||||
public class ServerUtils extends JavaPlugin implements CommandExecutor {
|
||||
|
||||
private static final int BSTATS_METRICS_ID = 7790;
|
||||
|
||||
private static ServerUtils instance;
|
||||
private static final String CONFIG_RESOURCE = "bukkit-config.yml";
|
||||
private static final String MESSAGES_RESOURCE = "bukkit-messages.yml";
|
||||
|
||||
private BukkitPlugin plugin;
|
||||
private PaperCommandManager commandManager;
|
||||
private CommandPlugins commandPlugins;
|
||||
|
||||
public static ServerUtils getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
super.onEnable();
|
||||
instance = this;
|
||||
|
||||
new Metrics(this, BSTATS_METRICS_ID);
|
||||
this.plugin = new BukkitPlugin(this);
|
||||
ServerUtilsApp.init(plugin);
|
||||
|
||||
new Metrics(this, ServerUtilsApp.BSTATS_METRICS_ID);
|
||||
new BukkitReflection();
|
||||
|
||||
this.commandManager = new PaperCommandManager(this);
|
||||
commandManager.registerCommand(new CommandServerUtils());
|
||||
this.commandPlugins = null;
|
||||
|
||||
PluginProvider<Plugin> provider = plugin.getPluginProvider();
|
||||
CommandCompletions<BukkitCommandCompletionContext> completions = commandManager.getCommandCompletions();
|
||||
completions.registerAsyncCompletion("plugins", context -> getPluginNames());
|
||||
completions.registerAsyncCompletion("pluginJars", context -> getPluginFileNames());
|
||||
completions.registerAsyncCompletion("plugins", context -> provider.getPluginNames());
|
||||
completions.registerAsyncCompletion("pluginJars", context -> provider.getPluginFileNames());
|
||||
completions.registerAsyncCompletion("supportedConfigs ", context -> CommandServerUtils.getSupportedConfigs());
|
||||
completions.registerAsyncCompletion("commands", context -> {
|
||||
try {
|
||||
|
|
@ -72,24 +76,20 @@ public class ServerUtils extends JavaPlugin implements CommandExecutor {
|
|||
checkForUpdates();
|
||||
}
|
||||
|
||||
public static ServerUtils getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public BukkitPlugin getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
super.onDisable();
|
||||
restoreBukkitPluginCommand();
|
||||
}
|
||||
|
||||
private List<String> getPluginNames() {
|
||||
return Arrays.stream(Bukkit.getPluginManager().getPlugins())
|
||||
.map(Plugin::getName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private List<String> getPluginFileNames() {
|
||||
return Arrays.stream(getJars())
|
||||
.map(File::getName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private void removeCommands(String... commands) {
|
||||
Map<String, Command> map;
|
||||
try {
|
||||
|
|
@ -118,10 +118,10 @@ public class ServerUtils extends JavaPlugin implements CommandExecutor {
|
|||
restoreBukkitPluginCommand();
|
||||
}
|
||||
|
||||
new Config(copyResourceIfNotExists("config.yml", "bukkit-config.yml"));
|
||||
new Messenger(copyResourceIfNotExists("messages.yml", "bukkit-messages.yml"));
|
||||
new Config("config.yml", CONFIG_RESOURCE);
|
||||
new Messenger("messages.yml", MESSAGES_RESOURCE);
|
||||
|
||||
if (!Config.getInstance().getBoolean("settings.disable-plugins-command")) {
|
||||
if (!Config.getInstance().getConfig().getBoolean("settings.disable-plugins-command")) {
|
||||
this.removeCommands("pl", "plugins");
|
||||
this.commandPlugins = new CommandPlugins();
|
||||
commandManager.registerCommand(commandPlugins);
|
||||
|
|
@ -132,38 +132,9 @@ public class ServerUtils extends JavaPlugin implements CommandExecutor {
|
|||
return commandManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all files with a jar extension in the plugins/ folder.
|
||||
* @return An array of jar files.
|
||||
*/
|
||||
public File[] getJars() {
|
||||
File parent = getDataFolder().getParentFile();
|
||||
if (parent == null) return new File[0];
|
||||
return parent.listFiles(f -> f.getName().endsWith(".jar"));
|
||||
}
|
||||
|
||||
private void createDataFolderIfNotExists() {
|
||||
if (!getDataFolder().exists()) {
|
||||
getDataFolder().mkdirs();
|
||||
}
|
||||
}
|
||||
|
||||
private File copyResourceIfNotExists(String targetName, String resource) {
|
||||
createDataFolderIfNotExists();
|
||||
|
||||
File file = new File(getDataFolder(), targetName);
|
||||
if (!file.exists()) {
|
||||
getLogger().info(String.format("'%s' not found, creating!", targetName));
|
||||
saveResource(resource, false);
|
||||
File copiedFile = new File(getDataFolder(), resource);
|
||||
copiedFile.renameTo(file);
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
private void checkForUpdates() {
|
||||
if (Config.getInstance().getBoolean("settings.check-updates")) {
|
||||
UpdateCheckerTask.start(Bukkit.getConsoleSender(), true);
|
||||
if (Config.getInstance().getConfig().getBoolean("settings.check-updates")) {
|
||||
UpdateCheckerTask.start(BukkitUtils.wrap(Bukkit.getConsoleSender()), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,19 +6,20 @@ import co.aikar.commands.annotation.CommandPermission;
|
|||
import co.aikar.commands.annotation.Default;
|
||||
import co.aikar.commands.annotation.Description;
|
||||
import co.aikar.commands.annotation.Subcommand;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import net.frankheijden.serverutils.bukkit.config.Messenger;
|
||||
import net.frankheijden.serverutils.common.utils.ListBuilder;
|
||||
import net.frankheijden.serverutils.common.utils.ListFormat;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
||||
import net.frankheijden.serverutils.bukkit.utils.BukkitUtils;
|
||||
import net.frankheijden.serverutils.common.commands.Plugins;
|
||||
import net.frankheijden.serverutils.common.config.Messenger;
|
||||
import net.frankheijden.serverutils.common.providers.PluginProvider;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
@CommandAlias("plugins|pl")
|
||||
public class CommandPlugins extends BaseCommand {
|
||||
|
||||
private static final PluginProvider<Plugin> provider = ServerUtils.getInstance().getPlugin().getPluginProvider();
|
||||
|
||||
/**
|
||||
* Sends the plugin list to the sender, without plugin version.
|
||||
* @param sender The sender of the command.
|
||||
|
|
@ -27,7 +28,7 @@ public class CommandPlugins extends BaseCommand {
|
|||
@CommandPermission("serverutils.plugins")
|
||||
@Description("Shows the plugins of this server.")
|
||||
public void onPlugins(CommandSender sender) {
|
||||
sendPlugins(sender, pl -> {
|
||||
Plugins.sendPlugins(BukkitUtils.wrap(sender), provider.getPluginsSorted(), pl -> {
|
||||
String format = "serverutils.plugins.format" + (pl.isEnabled() ? "" : "_disabled");
|
||||
return Messenger.getMessage(format, "%plugin%", pl.getName());
|
||||
});
|
||||
|
|
@ -41,30 +42,11 @@ public class CommandPlugins extends BaseCommand {
|
|||
@CommandPermission("serverutils.plugins.version")
|
||||
@Description("Shows the plugins of this server with version.")
|
||||
public void onPluginsWithVersion(CommandSender sender) {
|
||||
sendPlugins(sender, pl -> {
|
||||
Plugins.sendPlugins(BukkitUtils.wrap(sender), provider.getPluginsSorted(), pl -> {
|
||||
String format = "serverutils.plugins.format" + (pl.isEnabled() ? "" : "_disabled");
|
||||
String version = Messenger.getMessage("serverutils.plugins.version",
|
||||
"%version%", pl.getDescription().getVersion());
|
||||
return Messenger.getMessage(format, "%plugin%", pl.getName()) + version;
|
||||
});
|
||||
}
|
||||
|
||||
private static void sendPlugins(CommandSender sender, ListFormat<Plugin> pluginFormat) {
|
||||
Messenger.sendMessage(sender, "serverutils.plugins.header");
|
||||
List<Plugin> plugins = getPluginsSorted();
|
||||
String prefix = Messenger.getMessage("serverutils.plugins.prefix",
|
||||
"%count%", String.valueOf(plugins.size()));
|
||||
sender.sendMessage(Messenger.color(prefix + ListBuilder.create(plugins)
|
||||
.seperator(Messenger.getMessage("serverutils.plugins.seperator"))
|
||||
.lastSeperator(Messenger.getMessage("serverutils.plugins.last_seperator"))
|
||||
.format(pluginFormat)
|
||||
.toString()));
|
||||
Messenger.sendMessage(sender, "serverutils.plugins.footer");
|
||||
}
|
||||
|
||||
private static List<Plugin> getPluginsSorted() {
|
||||
List<Plugin> plugins = Arrays.asList(Bukkit.getPluginManager().getPlugins());
|
||||
plugins.sort(Comparator.comparing(Plugin::getName));
|
||||
return plugins;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
package net.frankheijden.serverutils.bukkit.commands;
|
||||
|
||||
import static net.frankheijden.serverutils.bukkit.config.Messenger.sendMessage;
|
||||
import static net.frankheijden.serverutils.bukkit.reflection.BukkitReflection.MINOR;
|
||||
import static net.frankheijden.serverutils.bukkit.entities.BukkitReflection.MINOR;
|
||||
import static net.frankheijden.serverutils.common.config.Messenger.sendMessage;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.CommandAlias;
|
||||
|
|
@ -18,15 +18,17 @@ import java.util.Map;
|
|||
import java.util.Set;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
||||
import net.frankheijden.serverutils.bukkit.config.Messenger;
|
||||
import net.frankheijden.serverutils.bukkit.managers.CloseableResult;
|
||||
import net.frankheijden.serverutils.bukkit.managers.LoadResult;
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitLoadResult;
|
||||
import net.frankheijden.serverutils.bukkit.managers.PluginManager;
|
||||
import net.frankheijden.serverutils.bukkit.managers.Result;
|
||||
import net.frankheijden.serverutils.bukkit.reflection.RCraftServer;
|
||||
import net.frankheijden.serverutils.bukkit.utils.FormatBuilder;
|
||||
import net.frankheijden.serverutils.bukkit.utils.ForwardFilter;
|
||||
import net.frankheijden.serverutils.bukkit.utils.BukkitUtils;
|
||||
import net.frankheijden.serverutils.bukkit.utils.ReloadHandler;
|
||||
import net.frankheijden.serverutils.common.config.Messenger;
|
||||
import net.frankheijden.serverutils.common.entities.CloseableResult;
|
||||
import net.frankheijden.serverutils.common.entities.Result;
|
||||
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||
import net.frankheijden.serverutils.common.utils.FormatBuilder;
|
||||
import net.frankheijden.serverutils.common.utils.ForwardFilter;
|
||||
import net.frankheijden.serverutils.common.utils.ListBuilder;
|
||||
import net.frankheijden.serverutils.common.utils.ListFormat;
|
||||
import org.bukkit.Bukkit;
|
||||
|
|
@ -64,13 +66,14 @@ public class CommandServerUtils extends BaseCommand {
|
|||
|
||||
/**
|
||||
* Shows the help page to the sender.
|
||||
* @param sender The sender of the command.
|
||||
* @param commandSender The sender of the command.
|
||||
*/
|
||||
@Default
|
||||
@Subcommand("help")
|
||||
@CommandPermission("serverutils.help")
|
||||
@Description("Shows a help page with a few commands.")
|
||||
public void onHelp(CommandSender sender) {
|
||||
public void onHelp(CommandSender commandSender) {
|
||||
ServerCommandSender sender = BukkitUtils.wrap(commandSender);
|
||||
Messenger.sendMessage(sender, "serverutils.help.header");
|
||||
|
||||
FormatBuilder builder = FormatBuilder.create(Messenger.getMessage("serverutils.help.format"))
|
||||
|
|
@ -97,26 +100,27 @@ public class CommandServerUtils extends BaseCommand {
|
|||
@Description("Reloads the ServerUtils plugin.")
|
||||
public void onReload(CommandSender sender) {
|
||||
plugin.reload();
|
||||
sendMessage(sender, "serverutils.success",
|
||||
sendMessage(BukkitUtils.wrap(sender), "serverutils.success",
|
||||
"%action%", "reload",
|
||||
"%what%", "ServerUtils configurations");
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads a config from a set of configurations of the server.
|
||||
* @param sender The sender of the command.
|
||||
* @param commandSender The sender of the command.
|
||||
* @param config The configuration to reload.
|
||||
*/
|
||||
@Subcommand("reloadconfig")
|
||||
@CommandCompletion("@supportedConfigs")
|
||||
@CommandPermission("serverutils.reloadconfig")
|
||||
@Description("Reloads individual Server configurations.")
|
||||
public void onReloadCommands(CommandSender sender, String config) {
|
||||
public void onReloadCommands(CommandSender commandSender, String config) {
|
||||
ReloadHandler handler = supportedConfigs.get(config);
|
||||
if (handler == null) {
|
||||
this.doHelp(sender);
|
||||
this.doHelp(commandSender);
|
||||
return;
|
||||
}
|
||||
ServerCommandSender sender = BukkitUtils.wrap(commandSender);
|
||||
|
||||
String[] replacements = new String[]{ "%action%", "reload", "%what%", config };
|
||||
|
||||
|
|
@ -138,34 +142,38 @@ public class CommandServerUtils extends BaseCommand {
|
|||
|
||||
/**
|
||||
* Loads the specified plugin on the server.
|
||||
* @param sender The sender of the command.
|
||||
* @param commandSender The sender of the command.
|
||||
* @param jarFile The filename of the plugin in the plugins/ directory.
|
||||
*/
|
||||
@Subcommand("loadplugin")
|
||||
@CommandCompletion("@pluginJars")
|
||||
@CommandPermission("serverutils.loadplugin")
|
||||
@Description("Loads the specified jar file as a plugin.")
|
||||
public void onLoadPlugin(CommandSender sender, String jarFile) {
|
||||
LoadResult loadResult = PluginManager.loadPlugin(jarFile);
|
||||
public void onLoadPlugin(CommandSender commandSender, String jarFile) {
|
||||
ServerCommandSender sender = BukkitUtils.wrap(commandSender);
|
||||
|
||||
BukkitLoadResult loadResult = PluginManager.loadPlugin(jarFile);
|
||||
if (!loadResult.isSuccess()) {
|
||||
loadResult.getResult().sendTo(sender, "load", jarFile);
|
||||
return;
|
||||
}
|
||||
|
||||
Result result = PluginManager.enablePlugin(loadResult.getPlugin());
|
||||
Result result = PluginManager.enablePlugin(loadResult.get());
|
||||
result.sendTo(sender, "load", jarFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unloads the specified plugin from the server.
|
||||
* @param sender The sender of the command.
|
||||
* @param commandSender The sender of the command.
|
||||
* @param pluginName The plugin name.
|
||||
*/
|
||||
@Subcommand("unloadplugin")
|
||||
@CommandCompletion("@plugins")
|
||||
@CommandPermission("serverutils.unloadplugin")
|
||||
@Description("Disables and unloads the specified plugin.")
|
||||
public void onUnloadPlugin(CommandSender sender, String pluginName) {
|
||||
public void onUnloadPlugin(CommandSender commandSender, String pluginName) {
|
||||
ServerCommandSender sender = BukkitUtils.wrap(commandSender);
|
||||
|
||||
Result disableResult = PluginManager.disablePlugin(pluginName);
|
||||
if (disableResult != Result.SUCCESS && disableResult != Result.ALREADY_DISABLED) {
|
||||
disableResult.sendTo(sender, "disabl", pluginName);
|
||||
|
|
@ -188,7 +196,7 @@ public class CommandServerUtils extends BaseCommand {
|
|||
@Description("Reloads a specified plugin.")
|
||||
public void onReloadPlugin(CommandSender sender, String pluginName) {
|
||||
CloseableResult result = PluginManager.reloadPlugin(pluginName);
|
||||
result.getResult().sendTo(sender, "reload", pluginName);
|
||||
result.getResult().sendTo(BukkitUtils.wrap(sender), "reload", pluginName);
|
||||
result.tryClose();
|
||||
}
|
||||
|
||||
|
|
@ -203,7 +211,7 @@ public class CommandServerUtils extends BaseCommand {
|
|||
@Description("Enables the loaded plugin.")
|
||||
public void onEnablePlugin(CommandSender sender, String pluginName) {
|
||||
Result result = PluginManager.enablePlugin(pluginName);
|
||||
result.sendTo(sender, "enabl", pluginName);
|
||||
result.sendTo(BukkitUtils.wrap(sender), "enabl", pluginName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -217,19 +225,21 @@ public class CommandServerUtils extends BaseCommand {
|
|||
@Description("Disables the specified plugin.")
|
||||
public void onDisablePlugin(CommandSender sender, String pluginName) {
|
||||
Result result = PluginManager.disablePlugin(pluginName);
|
||||
result.sendTo(sender, "disabl", pluginName);
|
||||
result.sendTo(BukkitUtils.wrap(sender), "disabl", pluginName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows information about the specified plugin.
|
||||
* @param sender The sender of the command.
|
||||
* @param commandSender The sender of the command.
|
||||
* @param pluginName The plugin name.
|
||||
*/
|
||||
@Subcommand("plugininfo")
|
||||
@CommandCompletion("@plugins")
|
||||
@CommandPermission("serverutils.plugininfo")
|
||||
@Description("Shows information about the specified plugin.")
|
||||
public void onPluginInfo(CommandSender sender, String pluginName) {
|
||||
public void onPluginInfo(CommandSender commandSender, String pluginName) {
|
||||
ServerCommandSender sender = BukkitUtils.wrap(commandSender);
|
||||
|
||||
Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginName);
|
||||
if (plugin == null) {
|
||||
Result.NOT_EXISTS.sendTo(sender, "fetch", pluginName);
|
||||
|
|
@ -289,14 +299,16 @@ public class CommandServerUtils extends BaseCommand {
|
|||
|
||||
/**
|
||||
* Shows information about a provided command.
|
||||
* @param sender The sender of the command.
|
||||
* @param commandSender The sender of the command.
|
||||
* @param command The command to lookup.
|
||||
*/
|
||||
@Subcommand("commandinfo")
|
||||
@CommandCompletion("@commands")
|
||||
@CommandPermission("serverutils.commandinfo")
|
||||
@Description("Shows information about the specified command.")
|
||||
public void onCommandInfo(CommandSender sender, String command) {
|
||||
public void onCommandInfo(CommandSender commandSender, String command) {
|
||||
ServerCommandSender sender = BukkitUtils.wrap(commandSender);
|
||||
|
||||
Command cmd = PluginManager.getCommand(command);
|
||||
if (cmd == null) {
|
||||
Messenger.sendMessage(sender, "serverutils.commandinfo.not_exists");
|
||||
|
|
|
|||
|
|
@ -1,21 +0,0 @@
|
|||
package net.frankheijden.serverutils.bukkit.config;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class Config extends YamlResource {
|
||||
|
||||
private static Config instance;
|
||||
|
||||
public Config(File file) {
|
||||
super(file, "bukkit-config.yml");
|
||||
instance = this;
|
||||
}
|
||||
|
||||
public static Config getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public boolean getBoolean(String path) {
|
||||
return getConfiguration().getBoolean(path);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,80 +0,0 @@
|
|||
package net.frankheijden.serverutils.bukkit.config;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class Messenger extends YamlResource {
|
||||
|
||||
private static final ServerUtils plugin = ServerUtils.getInstance();
|
||||
private static Messenger instance;
|
||||
|
||||
public Messenger(File file) {
|
||||
super(file, "bukkit-messages.yml");
|
||||
instance = this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a message from the config.
|
||||
* @param path The yml path to the message.
|
||||
* @param replacements The replacements to be taken into account.
|
||||
* @return The config message with translated placeholders.
|
||||
*/
|
||||
public static String getMessage(String path, String... replacements) {
|
||||
String message = instance.getConfiguration().getString(path);
|
||||
if (message != null) {
|
||||
return apply(message, replacements);
|
||||
} else {
|
||||
plugin.getLogger().severe("Missing locale in bukkit-messages.yml at path '" + path + "'!");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies placeholders to a message.
|
||||
* @param message The message.
|
||||
* @param replacements The replacements of the message. Expects input to be even and in a key-value like format.
|
||||
* Example: ["%player%", "Player"]
|
||||
* @return The message with translated placeholders.
|
||||
*/
|
||||
public static String apply(String message, String... replacements) {
|
||||
if (message == null || message.isEmpty()) return null;
|
||||
message = message.replace("\\n", "\n");
|
||||
for (int i = 0; i < replacements.length; i++, i++) {
|
||||
message = message.replace(replacements[i], replacements[i + 1]);
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to a player with translated placeholders.
|
||||
* @param sender The receiver.
|
||||
* @param msg The message to be sent.
|
||||
* @param replacements The replacements to be taken into account.
|
||||
*/
|
||||
public static void sendRawMessage(CommandSender sender, String msg, String... replacements) {
|
||||
String message = apply(msg, replacements);
|
||||
if (message != null) {
|
||||
sender.sendMessage(color(message));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message from the specified config path to a player with translated placeholders.
|
||||
* @param sender The receiver.
|
||||
* @param path The yml path to the message.
|
||||
* @param replacements The replacements to be taken into account.
|
||||
*/
|
||||
public static void sendMessage(CommandSender sender, String path, String... replacements) {
|
||||
String message = getMessage(path, replacements);
|
||||
if (message != null) {
|
||||
sender.sendMessage(color(message));
|
||||
}
|
||||
}
|
||||
|
||||
public static String color(String str) {
|
||||
return ChatColor.translateAlternateColorCodes('&', str);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
package net.frankheijden.serverutils.bukkit.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
||||
import net.frankheijden.serverutils.bukkit.utils.YamlUtils;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
public abstract class YamlResource {
|
||||
|
||||
private static final ServerUtils plugin = ServerUtils.getInstance();
|
||||
|
||||
private final YamlConfiguration configuration;
|
||||
|
||||
/**
|
||||
* Creates a new YamlResource instance.
|
||||
* Loads the resource from the jar file.
|
||||
* @param file The destination file.
|
||||
* @param resource The resource from the jar file.
|
||||
*/
|
||||
public YamlResource(File file, String resource) {
|
||||
InputStream is = plugin.getResource(resource);
|
||||
YamlConfiguration def = YamlConfiguration.loadConfiguration(new InputStreamReader(is));
|
||||
configuration = YamlUtils.init(file, def);
|
||||
}
|
||||
|
||||
public YamlConfiguration getConfiguration() {
|
||||
return configuration;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package net.frankheijden.serverutils.bukkit.entities;
|
||||
|
||||
import net.frankheijden.serverutils.common.providers.ColorProvider;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
|
||||
public class BukkitColorProvider implements ColorProvider {
|
||||
|
||||
@Override
|
||||
public String apply(String str) {
|
||||
return ChatColor.translateAlternateColorCodes('&', str);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package net.frankheijden.serverutils.bukkit.entities;
|
||||
|
||||
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class BukkitCommandSender implements ServerCommandSender {
|
||||
|
||||
private final CommandSender sender;
|
||||
|
||||
public BukkitCommandSender(CommandSender sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(String message) {
|
||||
sender.sendMessage(message);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package net.frankheijden.serverutils.bukkit.entities;
|
||||
|
||||
import net.frankheijden.serverutils.common.entities.LoadResult;
|
||||
import net.frankheijden.serverutils.common.entities.Result;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class BukkitLoadResult extends LoadResult<Plugin> {
|
||||
|
||||
private BukkitLoadResult(Plugin obj, Result result) {
|
||||
super(obj, result);
|
||||
}
|
||||
|
||||
public BukkitLoadResult(Plugin obj) {
|
||||
this(obj, Result.SUCCESS);
|
||||
}
|
||||
|
||||
public BukkitLoadResult(Result result) {
|
||||
this(null, result);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
package net.frankheijden.serverutils.bukkit.entities;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
||||
import net.frankheijden.serverutils.common.providers.ColorProvider;
|
||||
import net.frankheijden.serverutils.common.providers.PluginProvider;
|
||||
import net.frankheijden.serverutils.common.providers.ResourceProvider;
|
||||
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class BukkitPlugin extends ServerUtilsPlugin {
|
||||
|
||||
private final ServerUtils plugin;
|
||||
private final PluginProvider<Plugin> pluginProvider;
|
||||
private final ResourceProvider resourceProvider;
|
||||
private final ColorProvider colorProvider;
|
||||
|
||||
/**
|
||||
* Creates a new BukkitPlugin instance of ServerUtils.
|
||||
* @param plugin The ServerUtils plugin.
|
||||
*/
|
||||
public BukkitPlugin(ServerUtils plugin) {
|
||||
this.plugin = plugin;
|
||||
this.pluginProvider = new BukkitPluginProvider(plugin);
|
||||
this.resourceProvider = new BukkitResourceProvider(plugin);
|
||||
this.colorProvider = new BukkitColorProvider();
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public PluginProvider<Plugin> getPluginProvider() {
|
||||
return pluginProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceProvider getResourceProvider() {
|
||||
return resourceProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ColorProvider getColorProvider() {
|
||||
return colorProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Logger getLogger() {
|
||||
return plugin.getLogger();
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getDataFolder() {
|
||||
return plugin.getDataFolder();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
package net.frankheijden.serverutils.bukkit.entities;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
||||
import net.frankheijden.serverutils.common.providers.PluginProvider;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class BukkitPluginProvider extends PluginProvider<Plugin> {
|
||||
|
||||
private final ServerUtils plugin;
|
||||
|
||||
public BukkitPluginProvider(ServerUtils plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getPluginsFolder() {
|
||||
return plugin.getDataFolder().getParentFile();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Plugin> getPlugins() {
|
||||
return Arrays.asList(Bukkit.getPluginManager().getPlugins());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPluginName(Plugin plugin) {
|
||||
return plugin.getName();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package net.frankheijden.serverutils.bukkit.reflection;
|
||||
package net.frankheijden.serverutils.bukkit.entities;
|
||||
|
||||
import net.frankheijden.serverutils.common.reflection.ReflectionUtils;
|
||||
import net.frankheijden.serverutils.common.reflection.VersionParam;
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
package net.frankheijden.serverutils.bukkit.entities;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
||||
import net.frankheijden.serverutils.common.providers.ResourceProvider;
|
||||
import net.frankheijden.serverutils.common.config.YamlConfig;
|
||||
|
||||
public class BukkitResourceProvider implements ResourceProvider {
|
||||
|
||||
private final ServerUtils plugin;
|
||||
|
||||
public BukkitResourceProvider(ServerUtils plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getResource(String resource) {
|
||||
return plugin.getResource(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public YamlConfig load(InputStream is) {
|
||||
return new BukkitYamlConfig(is);
|
||||
}
|
||||
|
||||
@Override
|
||||
public YamlConfig load(File file) {
|
||||
return new BukkitYamlConfig(file);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
package net.frankheijden.serverutils.bukkit.entities;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Collection;
|
||||
|
||||
import net.frankheijden.serverutils.common.config.YamlConfig;
|
||||
import org.bukkit.configuration.MemorySection;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
public class BukkitYamlConfig implements YamlConfig {
|
||||
|
||||
private final MemorySection config;
|
||||
private File file = null;
|
||||
|
||||
public BukkitYamlConfig(File file) {
|
||||
this.config = YamlConfiguration.loadConfiguration(file);
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
public BukkitYamlConfig(InputStream in) {
|
||||
this.config = YamlConfiguration.loadConfiguration(new InputStreamReader(in));
|
||||
}
|
||||
|
||||
public BukkitYamlConfig(MemorySection section) {
|
||||
this.config = section;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get(String path) {
|
||||
Object obj = config.get(path);
|
||||
if (obj instanceof MemorySection) {
|
||||
return new BukkitYamlConfig((MemorySection) obj);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String path, Object value) {
|
||||
config.set(path, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String path) {
|
||||
return config.getString(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String path) {
|
||||
return config.getBoolean(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<? extends String> getKeys() {
|
||||
return config.getKeys(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() throws IOException {
|
||||
if (!(config instanceof YamlConfiguration)) throw new IllegalArgumentException("Not a YamlConfiguration!");
|
||||
YamlConfiguration yml = (YamlConfiguration) config;
|
||||
yml.save(file);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,9 @@
|
|||
package net.frankheijden.serverutils.bukkit.listeners;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.config.Config;
|
||||
import net.frankheijden.serverutils.bukkit.tasks.UpdateCheckerTask;
|
||||
import net.frankheijden.serverutils.bukkit.utils.BukkitUtils;
|
||||
import net.frankheijden.serverutils.common.config.Config;
|
||||
import net.frankheijden.serverutils.common.config.YamlConfig;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
|
|
@ -10,7 +12,7 @@ import org.bukkit.event.player.PlayerJoinEvent;
|
|||
|
||||
public class MainListener implements Listener {
|
||||
|
||||
private static final Config config = Config.getInstance();
|
||||
private static final YamlConfig config = Config.getInstance().getConfig();
|
||||
|
||||
/**
|
||||
* Called when a player joins the server.
|
||||
|
|
@ -23,7 +25,7 @@ public class MainListener implements Listener {
|
|||
|
||||
Player player = event.getPlayer();
|
||||
if (player.hasPermission("serverutils.notification.update")) {
|
||||
UpdateCheckerTask.start(player);
|
||||
UpdateCheckerTask.start(BukkitUtils.wrap(player));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,33 +0,0 @@
|
|||
package net.frankheijden.serverutils.bukkit.managers;
|
||||
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class LoadResult {
|
||||
private final Plugin plugin;
|
||||
private final Result result;
|
||||
|
||||
private LoadResult(Plugin plugin, Result result) {
|
||||
this.plugin = plugin;
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public LoadResult(Plugin plugin) {
|
||||
this(plugin, Result.SUCCESS);
|
||||
}
|
||||
|
||||
public LoadResult(Result result) {
|
||||
this(null, result);
|
||||
}
|
||||
|
||||
public Result getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public Plugin getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
public boolean isSuccess() {
|
||||
return plugin != null && result == Result.SUCCESS;
|
||||
}
|
||||
}
|
||||
|
|
@ -7,12 +7,15 @@ import java.util.regex.Matcher;
|
|||
import java.util.regex.Pattern;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitLoadResult;
|
||||
import net.frankheijden.serverutils.bukkit.reflection.RCommandMap;
|
||||
import net.frankheijden.serverutils.bukkit.reflection.RCraftServer;
|
||||
import net.frankheijden.serverutils.bukkit.reflection.RCraftingManager;
|
||||
import net.frankheijden.serverutils.bukkit.reflection.RJavaPlugin;
|
||||
import net.frankheijden.serverutils.bukkit.reflection.RPluginClassLoader;
|
||||
import net.frankheijden.serverutils.bukkit.reflection.RSimplePluginManager;
|
||||
import net.frankheijden.serverutils.common.entities.CloseableResult;
|
||||
import net.frankheijden.serverutils.common.entities.Result;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
|
|
@ -30,7 +33,7 @@ public class PluginManager {
|
|||
* @param jarFile The name of the file in the plugins/ folder.
|
||||
* @return The result of the loading procedure.
|
||||
*/
|
||||
public static LoadResult loadPlugin(String jarFile) {
|
||||
public static BukkitLoadResult loadPlugin(String jarFile) {
|
||||
return loadPlugin(new File(ServerUtils.getInstance().getDataFolder().getParent(), jarFile));
|
||||
}
|
||||
|
||||
|
|
@ -39,33 +42,33 @@ public class PluginManager {
|
|||
* @param file The file to be loaded.
|
||||
* @return The result of the loading procedure.
|
||||
*/
|
||||
public static LoadResult loadPlugin(File file) {
|
||||
if (!file.exists()) return new LoadResult(Result.NOT_EXISTS);
|
||||
public static BukkitLoadResult loadPlugin(File file) {
|
||||
if (!file.exists()) return new BukkitLoadResult(Result.NOT_EXISTS);
|
||||
|
||||
Plugin loadedPlugin = getLoadedPlugin(file);
|
||||
if (loadedPlugin != null) return new LoadResult(Result.ALREADY_LOADED);
|
||||
if (loadedPlugin != null) return new BukkitLoadResult(Result.ALREADY_LOADED);
|
||||
|
||||
Plugin plugin;
|
||||
try {
|
||||
plugin = Bukkit.getPluginManager().loadPlugin(file);
|
||||
} catch (InvalidDescriptionException ex) {
|
||||
return new LoadResult(Result.INVALID_DESCRIPTION);
|
||||
return new BukkitLoadResult(Result.INVALID_DESCRIPTION);
|
||||
} catch (UnknownDependencyException ex) {
|
||||
return new LoadResult(Result.UNKNOWN_DEPENDENCY.arg(ex.getMessage()));
|
||||
return new BukkitLoadResult(Result.UNKNOWN_DEPENDENCY.arg(ex.getMessage()));
|
||||
} catch (InvalidPluginException ex) {
|
||||
if (ex.getCause() instanceof IllegalArgumentException) {
|
||||
IllegalArgumentException e = (IllegalArgumentException) ex.getCause();
|
||||
if (e.getMessage().equalsIgnoreCase("Plugin already initialized!")) {
|
||||
return new LoadResult(Result.ALREADY_ENABLED);
|
||||
return new BukkitLoadResult(Result.ALREADY_ENABLED);
|
||||
}
|
||||
}
|
||||
ex.printStackTrace();
|
||||
return new LoadResult(Result.ERROR);
|
||||
return new BukkitLoadResult(Result.ERROR);
|
||||
}
|
||||
|
||||
if (plugin == null) return new LoadResult(Result.INVALID_PLUGIN);
|
||||
if (plugin == null) return new BukkitLoadResult(Result.INVALID_PLUGIN);
|
||||
plugin.onLoad();
|
||||
return new LoadResult(plugin);
|
||||
return new BukkitLoadResult(plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -176,9 +179,9 @@ public class PluginManager {
|
|||
File pluginFile = getPluginFile(plugin.getName());
|
||||
if (pluginFile == null) return result.set(Result.FILE_DELETED);
|
||||
|
||||
LoadResult loadResult = loadPlugin(pluginFile);
|
||||
BukkitLoadResult loadResult = loadPlugin(pluginFile);
|
||||
if (!loadResult.isSuccess()) return result.set(loadResult.getResult());
|
||||
return result.set(enablePlugin(loadResult.getPlugin()));
|
||||
return result.set(enablePlugin(loadResult.get()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -291,7 +294,7 @@ public class PluginManager {
|
|||
* @return The file, or null if invalid or not found.
|
||||
*/
|
||||
public static File getPluginFile(String pluginName) {
|
||||
for (File file : ServerUtils.getInstance().getJars()) {
|
||||
for (File file : ServerUtils.getInstance().getPlugin().getPluginProvider().getPluginJars()) {
|
||||
PluginDescriptionFile descriptionFile;
|
||||
try {
|
||||
descriptionFile = getPluginDescription(file);
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import java.lang.reflect.InvocationTargetException;
|
|||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Warning;
|
||||
import org.bukkit.command.Command;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package net.frankheijden.serverutils.bukkit.reflection;
|
||||
|
||||
import static net.frankheijden.serverutils.bukkit.reflection.BukkitReflection.MINOR;
|
||||
import static net.frankheijden.serverutils.bukkit.entities.BukkitReflection.MINOR;
|
||||
import static net.frankheijden.serverutils.common.reflection.FieldParam.fieldOf;
|
||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.get;
|
||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.getAllFields;
|
||||
|
|
@ -14,6 +14,7 @@ import java.util.Map;
|
|||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||
import net.frankheijden.serverutils.common.utils.MapUtils;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
package net.frankheijden.serverutils.bukkit.reflection;
|
||||
|
||||
import static net.frankheijden.serverutils.bukkit.reflection.BukkitReflection.MINOR;
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||
|
||||
import static net.frankheijden.serverutils.bukkit.entities.BukkitReflection.MINOR;
|
||||
import static net.frankheijden.serverutils.common.reflection.FieldParam.fieldOf;
|
||||
import static net.frankheijden.serverutils.common.reflection.MethodParam.methodOf;
|
||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.get;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package net.frankheijden.serverutils.bukkit.reflection;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||
|
||||
import static net.frankheijden.serverutils.common.reflection.FieldParam.fieldOf;
|
||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.getAllFields;
|
||||
import static net.frankheijden.serverutils.common.reflection.VersionParam.ALL_VERSIONS;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package net.frankheijden.serverutils.bukkit.reflection;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||
|
||||
import static net.frankheijden.serverutils.common.reflection.MethodParam.methodOf;
|
||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.getAllMethods;
|
||||
import static net.frankheijden.serverutils.common.reflection.VersionParam.ALL_VERSIONS;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package net.frankheijden.serverutils.bukkit.reflection;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||
|
||||
import static net.frankheijden.serverutils.common.reflection.MethodParam.methodOf;
|
||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.getAllMethods;
|
||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.invoke;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package net.frankheijden.serverutils.bukkit.reflection;
|
||||
|
||||
import static net.frankheijden.serverutils.bukkit.reflection.BukkitReflection.MINOR;
|
||||
import static net.frankheijden.serverutils.bukkit.entities.BukkitReflection.MINOR;
|
||||
import static net.frankheijden.serverutils.common.reflection.FieldParam.fieldOf;
|
||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.get;
|
||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.getAllFields;
|
||||
|
|
@ -13,6 +13,7 @@ import java.util.Map;
|
|||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class RMinecraftKey {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package net.frankheijden.serverutils.bukkit.reflection;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||
|
||||
import static net.frankheijden.serverutils.common.reflection.MethodParam.methodOf;
|
||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.getAllMethods;
|
||||
import static net.frankheijden.serverutils.common.reflection.VersionParam.ALL_VERSIONS;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package net.frankheijden.serverutils.bukkit.reflection;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||
|
||||
import static net.frankheijden.serverutils.common.reflection.MethodParam.methodOf;
|
||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.getAllMethods;
|
||||
import static net.frankheijden.serverutils.common.reflection.VersionParam.ALL_VERSIONS;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package net.frankheijden.serverutils.bukkit.reflection;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||
|
||||
import static net.frankheijden.serverutils.common.reflection.MethodParam.methodOf;
|
||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.getAllMethods;
|
||||
import static net.frankheijden.serverutils.common.reflection.VersionParam.ALL_VERSIONS;
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import java.lang.reflect.Field;
|
|||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||
import net.frankheijden.serverutils.common.utils.MapUtils;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import java.lang.reflect.Field;
|
|||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||
import net.frankheijden.serverutils.common.utils.MapUtils;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
|
|
|
|||
|
|
@ -14,22 +14,24 @@ import java.net.UnknownHostException;
|
|||
import java.util.logging.Level;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
||||
import net.frankheijden.serverutils.bukkit.config.Config;
|
||||
import net.frankheijden.serverutils.bukkit.config.Messenger;
|
||||
import net.frankheijden.serverutils.bukkit.managers.CloseableResult;
|
||||
import net.frankheijden.serverutils.bukkit.managers.PluginManager;
|
||||
import net.frankheijden.serverutils.bukkit.managers.VersionManager;
|
||||
import net.frankheijden.serverutils.common.config.Config;
|
||||
import net.frankheijden.serverutils.common.config.Messenger;
|
||||
import net.frankheijden.serverutils.common.config.YamlConfig;
|
||||
import net.frankheijden.serverutils.common.entities.CloseableResult;
|
||||
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||
import net.frankheijden.serverutils.common.utils.FileUtils;
|
||||
import net.frankheijden.serverutils.common.utils.VersionUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class UpdateCheckerTask implements Runnable {
|
||||
|
||||
private static final ServerUtils plugin = ServerUtils.getInstance();
|
||||
private static final YamlConfig config = Config.getInstance().getConfig();
|
||||
private static final VersionManager versionManager = VersionManager.getInstance();
|
||||
private final CommandSender sender;
|
||||
private final ServerCommandSender sender;
|
||||
private final String currentVersion;
|
||||
private final boolean startup;
|
||||
|
||||
|
|
@ -47,17 +49,17 @@ public class UpdateCheckerTask implements Runnable {
|
|||
private static final String DOWNLOADED_RESTART = "Downloaded ServerUtils version v%s. Restarting plugin now...";
|
||||
private static final String UP_TO_DATE = "We are up-to-date!";
|
||||
|
||||
private UpdateCheckerTask(CommandSender sender, boolean startup) {
|
||||
private UpdateCheckerTask(ServerCommandSender sender, boolean startup) {
|
||||
this.sender = sender;
|
||||
this.currentVersion = plugin.getDescription().getVersion();
|
||||
this.startup = startup;
|
||||
}
|
||||
|
||||
public static void start(CommandSender sender) {
|
||||
public static void start(ServerCommandSender sender) {
|
||||
start(sender, false);
|
||||
}
|
||||
|
||||
public static void start(CommandSender sender, boolean startup) {
|
||||
public static void start(ServerCommandSender sender, boolean startup) {
|
||||
UpdateCheckerTask task = new UpdateCheckerTask(sender, startup);
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, task);
|
||||
}
|
||||
|
|
@ -143,8 +145,8 @@ public class UpdateCheckerTask implements Runnable {
|
|||
}
|
||||
|
||||
private boolean canDownloadPlugin() {
|
||||
if (isStartupCheck()) return Config.getInstance().getBoolean("settings.download-at-startup-and-update");
|
||||
return Config.getInstance().getBoolean("settings.download-updates");
|
||||
if (isStartupCheck()) return config.getBoolean("settings.download-at-startup-and-update");
|
||||
return config.getBoolean("settings.download-updates");
|
||||
}
|
||||
|
||||
private void downloadPlugin(String githubVersion, String downloadLink) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
package net.frankheijden.serverutils.bukkit.utils;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitCommandSender;
|
||||
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class BukkitUtils {
|
||||
|
||||
public static ServerCommandSender wrap(CommandSender sender) {
|
||||
return new BukkitCommandSender(sender);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,50 +0,0 @@
|
|||
package net.frankheijden.serverutils.bukkit.utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.configuration.MemorySection;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
public class YamlUtils {
|
||||
|
||||
public static void addDefaults(MemorySection defaults, YamlConfiguration yml) {
|
||||
addDefaults(defaults, yml, "");
|
||||
}
|
||||
|
||||
private static void addDefaults(MemorySection defaults, YamlConfiguration yml, String root) {
|
||||
if (defaults == null) return;
|
||||
for (String key : defaults.getKeys(false)) {
|
||||
String newKey = (root.isEmpty() ? "" : root + ".") + key;
|
||||
Object value = defaults.get(key);
|
||||
if (value instanceof MemorySection) {
|
||||
addDefaults((MemorySection) value, yml, newKey);
|
||||
} else if (yml.get(newKey) == null) {
|
||||
yml.set(newKey, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates a YamlConfiguration from a file with associated defaults.
|
||||
* @param file The yml file.
|
||||
* @param def The default YamlConfiguration to be applied.
|
||||
* @return The loaded YamlConfiguration of the file with defaults.
|
||||
*/
|
||||
public static YamlConfiguration init(File file, YamlConfiguration def) {
|
||||
YamlConfiguration yml = YamlConfiguration.loadConfiguration(file);
|
||||
YamlUtils.addDefaults(def, yml);
|
||||
|
||||
try {
|
||||
// Idk somehow the order messes up
|
||||
// of the messages if we don't do this
|
||||
file.delete();
|
||||
file.createNewFile();
|
||||
|
||||
yml.save(file);
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return yml;
|
||||
}
|
||||
}
|
||||
28
Bungee/build.gradle
Normal file
28
Bungee/build.gradle
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
group = rootProject.group + '.bungee'
|
||||
String dependencyDir = group + '.dependencies'
|
||||
version = rootProject.version
|
||||
archivesBaseName = rootProject.name + '-Bungee'
|
||||
|
||||
repositories {
|
||||
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'co.aikar:acf-bungee:0.5.0-SNAPSHOT'
|
||||
implementation 'org.bstats:bstats-bungeecord:1.7'
|
||||
implementation project(":Common")
|
||||
compileOnly 'net.md-5:bungeecord-api:1.16-R0.2-SNAPSHOT'
|
||||
}
|
||||
|
||||
processResources {
|
||||
from('src/main/resources') {
|
||||
include 'bungee.yml'
|
||||
expand(version: project.version)
|
||||
}
|
||||
}
|
||||
|
||||
shadowJar {
|
||||
relocate 'org.bstats.bungeecord', dependencyDir + '.bstats'
|
||||
relocate 'co.aikar.commands', dependencyDir + '.acf'
|
||||
relocate 'co.aikar.locales', dependencyDir + '.locales'
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
package net.frankheijden.serverutils.bungee;
|
||||
|
||||
import co.aikar.commands.BungeeCommandCompletionContext;
|
||||
import co.aikar.commands.BungeeCommandManager;
|
||||
import co.aikar.commands.CommandCompletions;
|
||||
import net.frankheijden.serverutils.bungee.commands.CommandPlugins;
|
||||
import net.frankheijden.serverutils.bungee.commands.CommandServerUtils;
|
||||
import net.frankheijden.serverutils.bungee.entities.BungeePlugin;
|
||||
import net.frankheijden.serverutils.bungee.entities.BungeeReflection;
|
||||
import net.frankheijden.serverutils.common.ServerUtilsApp;
|
||||
import net.frankheijden.serverutils.common.config.Config;
|
||||
import net.frankheijden.serverutils.common.config.Messenger;
|
||||
import net.frankheijden.serverutils.common.providers.PluginProvider;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import org.bstats.bungeecord.Metrics;
|
||||
|
||||
public class ServerUtils extends Plugin {
|
||||
|
||||
private static ServerUtils instance;
|
||||
private static final String CONFIG_RESOURCE = "bungee-config.yml";
|
||||
private static final String MESSAGES_RESOURCE = "bungee-messages.yml";
|
||||
|
||||
private BungeePlugin plugin;
|
||||
private BungeeCommandManager commandManager;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
super.onEnable();
|
||||
instance = this;
|
||||
|
||||
this.plugin = new BungeePlugin(this);
|
||||
ServerUtilsApp.init(plugin);
|
||||
|
||||
new Metrics(this, ServerUtilsApp.BSTATS_METRICS_ID);
|
||||
new BungeeReflection();
|
||||
|
||||
this.commandManager = new BungeeCommandManager(this);
|
||||
commandManager.registerCommand(new CommandPlugins());
|
||||
commandManager.registerCommand(new CommandServerUtils());
|
||||
|
||||
PluginProvider<Plugin> provider = plugin.getPluginProvider();
|
||||
CommandCompletions<BungeeCommandCompletionContext> commandCompletions = commandManager.getCommandCompletions();
|
||||
commandCompletions.registerAsyncCompletion("plugins", context -> provider.getPluginNames());
|
||||
commandCompletions.registerAsyncCompletion("pluginJars", context -> provider.getPluginFileNames());
|
||||
|
||||
reload();
|
||||
}
|
||||
|
||||
public static ServerUtils getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public BungeePlugin getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
public BungeeCommandManager getCommandManager() {
|
||||
return commandManager;
|
||||
}
|
||||
|
||||
public void reload() {
|
||||
new Config("config.yml", CONFIG_RESOURCE);
|
||||
new Messenger("messages.yml", MESSAGES_RESOURCE);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
package net.frankheijden.serverutils.bungee.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.CommandAlias;
|
||||
import co.aikar.commands.annotation.CommandCompletion;
|
||||
import co.aikar.commands.annotation.CommandPermission;
|
||||
import co.aikar.commands.annotation.Default;
|
||||
import co.aikar.commands.annotation.Description;
|
||||
import co.aikar.commands.annotation.Subcommand;
|
||||
import net.frankheijden.serverutils.bungee.ServerUtils;
|
||||
import net.frankheijden.serverutils.bungee.entities.BungeePluginProvider;
|
||||
import net.frankheijden.serverutils.bungee.utils.BungeeUtils;
|
||||
import net.frankheijden.serverutils.common.commands.Plugins;
|
||||
import net.frankheijden.serverutils.common.config.Messenger;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
|
||||
@CommandAlias("bpl|bplugins|bungeepl")
|
||||
public class CommandPlugins extends BaseCommand {
|
||||
|
||||
private static final ServerUtils plugin = ServerUtils.getInstance();
|
||||
private static final BungeePluginProvider provider = (BungeePluginProvider) plugin.getPlugin().getPluginProvider();
|
||||
|
||||
/**
|
||||
* Sends the plugin list to the sender.
|
||||
* The `-v` flag will output the plugins with version.
|
||||
* The `-m` flag will also output modules in the plugin list.
|
||||
* @param sender The sender of the command.
|
||||
*/
|
||||
@Default
|
||||
@CommandCompletion("-v|-m -v|-m")
|
||||
@CommandPermission("serverutils.plugins")
|
||||
@Description("Shows the plugins of this proxy.")
|
||||
public void onPlugins(CommandSender sender, String... args) {
|
||||
boolean version = contains(args, "-v");
|
||||
boolean modules = contains(args, "-m");
|
||||
Plugins.sendPlugins(BungeeUtils.wrap(sender), provider.getPluginsSorted(modules), pl -> {
|
||||
String ver = version ? Messenger.getMessage("serverutils.plugins.version",
|
||||
"%version%", pl.getDescription().getVersion()) : "";
|
||||
return Messenger.getMessage("serverutils.plugins.format",
|
||||
"%plugin%", pl.getDescription().getName()) + ver;
|
||||
});
|
||||
}
|
||||
|
||||
private static boolean contains(String[] arr, String val) {
|
||||
for (String s : arr) {
|
||||
if (s.equalsIgnoreCase(val)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,255 @@
|
|||
package net.frankheijden.serverutils.bungee.commands;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.RegisteredCommand;
|
||||
import co.aikar.commands.annotation.CommandAlias;
|
||||
import co.aikar.commands.annotation.CommandCompletion;
|
||||
import co.aikar.commands.annotation.CommandPermission;
|
||||
import co.aikar.commands.annotation.Default;
|
||||
import co.aikar.commands.annotation.Description;
|
||||
import co.aikar.commands.annotation.Subcommand;
|
||||
import com.google.common.collect.SetMultimap;
|
||||
import net.frankheijden.serverutils.bungee.ServerUtils;
|
||||
import net.frankheijden.serverutils.bungee.entities.BungeeLoadResult;
|
||||
import net.frankheijden.serverutils.bungee.managers.PluginManager;
|
||||
import net.frankheijden.serverutils.bungee.reflection.RPluginManager;
|
||||
import net.frankheijden.serverutils.bungee.utils.BungeeUtils;
|
||||
import net.frankheijden.serverutils.common.config.Messenger;
|
||||
import net.frankheijden.serverutils.common.entities.CloseableResult;
|
||||
import net.frankheijden.serverutils.common.entities.Result;
|
||||
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||
import net.frankheijden.serverutils.common.utils.FormatBuilder;
|
||||
import net.frankheijden.serverutils.common.utils.ListBuilder;
|
||||
import net.frankheijden.serverutils.common.utils.ListFormat;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import net.md_5.bungee.api.plugin.Command;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import net.md_5.bungee.api.plugin.PluginDescription;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static net.frankheijden.serverutils.common.config.Messenger.sendMessage;
|
||||
|
||||
@CommandAlias("bsu|bserverutils")
|
||||
public class CommandServerUtils extends BaseCommand {
|
||||
|
||||
private static final ProxyServer proxy = ProxyServer.getInstance();
|
||||
private static final ServerUtils plugin = ServerUtils.getInstance();
|
||||
private static final Set<String> ALIASES;
|
||||
|
||||
static {
|
||||
ALIASES = new HashSet<>();
|
||||
ALIASES.add("bserverutils");
|
||||
ALIASES.add("bplugins");
|
||||
ALIASES.add("bungeepl");
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the help page to the sender.
|
||||
* @param commandSender The sender of the command.
|
||||
*/
|
||||
@Default
|
||||
@Subcommand("help")
|
||||
@CommandPermission("serverutils.help")
|
||||
@Description("Shows a help page with a few commands.")
|
||||
public void onHelp(CommandSender commandSender) {
|
||||
ServerCommandSender sender = BungeeUtils.wrap(commandSender);
|
||||
Messenger.sendMessage(sender, "serverutils.help.header");
|
||||
|
||||
FormatBuilder builder = FormatBuilder.create(Messenger.getMessage("serverutils.help.format"))
|
||||
.orderedKeys("%command%", "%subcommand%", "%help%");
|
||||
plugin.getCommandManager().getRegisteredRootCommands().stream()
|
||||
.filter(c -> !ALIASES.contains(c.getCommandName().toLowerCase()))
|
||||
.forEach(rootCommand -> {
|
||||
builder.add(rootCommand.getCommandName(), "", rootCommand.getDescription());
|
||||
|
||||
rootCommand.getSubCommands().forEach((str, cmd) -> {
|
||||
if (cmd.getPrefSubCommand().isEmpty()) return;
|
||||
builder.add(rootCommand.getCommandName(), " " + cmd.getPrefSubCommand(), cmd.getHelpText());
|
||||
});
|
||||
});
|
||||
builder.sendTo(sender);
|
||||
Messenger.sendMessage(sender, "serverutils.help.footer");
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads the configurations of ServerUtils.
|
||||
* @param sender The sender of the command.
|
||||
*/
|
||||
@Subcommand("reload")
|
||||
@CommandPermission("serverutils.reload")
|
||||
@Description("Reloads the ServerUtils plugin.")
|
||||
public void onReload(CommandSender sender) {
|
||||
plugin.reload();
|
||||
sendMessage(BungeeUtils.wrap(sender), "serverutils.success",
|
||||
"%action%", "reload",
|
||||
"%what%", "ServerUtils Bungee configurations");
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the specified plugin on the proxy.
|
||||
* @param commandSender The sender of the command.
|
||||
* @param jarFile The filename of the plugin in the plugins/ directory.
|
||||
*/
|
||||
@Subcommand("loadplugin")
|
||||
@CommandCompletion("@pluginJars")
|
||||
@CommandPermission("serverutils.loadplugin")
|
||||
@Description("Loads the specified jar file as a plugin.")
|
||||
public void onLoadPlugin(CommandSender commandSender, String jarFile) {
|
||||
ServerCommandSender sender = BungeeUtils.wrap(commandSender);
|
||||
|
||||
BungeeLoadResult loadResult = PluginManager.loadPlugin(jarFile);
|
||||
if (!loadResult.isSuccess()) {
|
||||
loadResult.getResult().sendTo(sender, "load", jarFile);
|
||||
return;
|
||||
}
|
||||
|
||||
Plugin plugin = loadResult.get();
|
||||
Result result = PluginManager.enablePlugin(plugin);
|
||||
result.sendTo(sender, "load", plugin.getDescription().getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Unloads the specified plugin from the proxy.
|
||||
* @param commandSender The sender of the command.
|
||||
* @param pluginName The plugin name.
|
||||
*/
|
||||
@Subcommand("unloadplugin")
|
||||
@CommandCompletion("@plugins")
|
||||
@CommandPermission("serverutils.unloadplugin")
|
||||
@Description("Disables and unloads the specified plugin.")
|
||||
public void onUnloadPlugin(CommandSender commandSender, String pluginName) {
|
||||
CloseableResult result = PluginManager.unloadPlugin(pluginName);
|
||||
result.getResult().sendTo(BungeeUtils.wrap(commandSender), "unload", pluginName);
|
||||
result.tryClose();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads the specified plugin on the proxy.
|
||||
* @param sender The sender of the command.
|
||||
* @param pluginName The plugin name.
|
||||
*/
|
||||
@Subcommand("reloadplugin")
|
||||
@CommandCompletion("@plugins")
|
||||
@CommandPermission("serverutils.reloadplugin")
|
||||
@Description("Reloads a specified plugin.")
|
||||
public void onReloadPlugin(CommandSender sender, String pluginName) {
|
||||
CloseableResult result = PluginManager.reloadPlugin(pluginName);
|
||||
result.getResult().sendTo(BungeeUtils.wrap(sender), "reload", pluginName);
|
||||
result.tryClose();
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows information about the specified plugin.
|
||||
* @param commandSender The sender of the command.
|
||||
* @param pluginName The plugin name.
|
||||
*/
|
||||
@Subcommand("plugininfo")
|
||||
@CommandCompletion("@plugins")
|
||||
@CommandPermission("serverutils.plugininfo")
|
||||
@Description("Shows information about the specified plugin.")
|
||||
public void onPluginInfo(CommandSender commandSender, String pluginName) {
|
||||
ServerCommandSender sender = BungeeUtils.wrap(commandSender);
|
||||
|
||||
Plugin plugin = ProxyServer.getInstance().getPluginManager().getPlugin(pluginName);
|
||||
if (plugin == null) {
|
||||
Result.NOT_EXISTS.sendTo(sender, "fetch", pluginName);
|
||||
return;
|
||||
}
|
||||
|
||||
PluginDescription desc = plugin.getDescription();
|
||||
String format = Messenger.getMessage("serverutils.plugininfo.format");
|
||||
String listFormatString = Messenger.getMessage("serverutils.plugininfo.list_format");
|
||||
String seperator = Messenger.getMessage("serverutils.plugininfo.seperator");
|
||||
String lastSeperator = Messenger.getMessage("serverutils.plugininfo.last_seperator");
|
||||
|
||||
ListFormat<String> listFormat = str -> listFormatString.replace("%value%", str);
|
||||
|
||||
Messenger.sendMessage(sender, "serverutils.plugininfo.header");
|
||||
|
||||
FormatBuilder builder = FormatBuilder.create(format)
|
||||
.orderedKeys("%key%", "%value%")
|
||||
.add("Name", desc.getName())
|
||||
.add("Version", desc.getVersion())
|
||||
.add("Author", desc.getAuthor())
|
||||
.add("Description", desc.getDescription())
|
||||
.add("Main", desc.getMain())
|
||||
.add("File", desc.getFile().getName())
|
||||
.add("Depend", ListBuilder.create(desc.getDepends())
|
||||
.format(listFormat)
|
||||
.seperator(seperator)
|
||||
.lastSeperator(lastSeperator)
|
||||
.toString())
|
||||
.add("Soft Depend", ListBuilder.create(desc.getSoftDepends())
|
||||
.format(listFormat)
|
||||
.seperator(seperator)
|
||||
.lastSeperator(lastSeperator)
|
||||
.toString());
|
||||
|
||||
builder.sendTo(sender);
|
||||
Messenger.sendMessage(sender, "serverutils.plugininfo.footer");
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows information about a provided command.
|
||||
* @param commandSender The sender of the command.
|
||||
* @param command The command to lookup.
|
||||
*/
|
||||
@Subcommand("commandinfo")
|
||||
@CommandCompletion("@commands")
|
||||
@CommandPermission("serverutils.commandinfo")
|
||||
@Description("Shows information about the specified command.")
|
||||
public void onCommandInfo(CommandSender commandSender, String command) {
|
||||
ServerCommandSender sender = BungeeUtils.wrap(commandSender);
|
||||
|
||||
Map<String, Command> commands;
|
||||
try {
|
||||
commands = RPluginManager.getCommands(proxy.getPluginManager());
|
||||
} catch (IllegalAccessException ex) {
|
||||
ex.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
Command cmd = commands.get(command);
|
||||
if (cmd == null) {
|
||||
Messenger.sendMessage(sender, "serverutils.commandinfo.not_exists");
|
||||
return;
|
||||
}
|
||||
|
||||
Plugin plugin;
|
||||
try {
|
||||
plugin = RPluginManager.getPlugin(proxy.getPluginManager(), cmd);
|
||||
} catch (IllegalAccessException ex) {
|
||||
ex.printStackTrace();
|
||||
return;
|
||||
}
|
||||
if (plugin == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String format = Messenger.getMessage("serverutils.commandinfo.format");
|
||||
String listFormatString = Messenger.getMessage("serverutils.commandinfo.list_format");
|
||||
String seperator = Messenger.getMessage("serverutils.commandinfo.seperator");
|
||||
String lastSeperator = Messenger.getMessage("serverutils.commandinfo.last_seperator");
|
||||
|
||||
ListFormat<String> listFormat = str -> listFormatString.replace("%value%", str);
|
||||
|
||||
Messenger.sendMessage(sender, "serverutils.commandinfo.header");
|
||||
FormatBuilder builder = FormatBuilder.create(format)
|
||||
.orderedKeys("%key%", "%value%")
|
||||
.add("Name", cmd.getName())
|
||||
.add("Plugin", plugin.getDescription().getName())
|
||||
.add("Aliases", ListBuilder.create(cmd.getAliases())
|
||||
.format(listFormat)
|
||||
.seperator(seperator)
|
||||
.lastSeperator(lastSeperator)
|
||||
.toString())
|
||||
.add("Permission", cmd.getPermission());
|
||||
|
||||
builder.sendTo(sender);
|
||||
Messenger.sendMessage(sender, "serverutils.commandinfo.footer");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package net.frankheijden.serverutils.bungee.entities;
|
||||
|
||||
import net.frankheijden.serverutils.common.providers.ColorProvider;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
|
||||
public class BungeeColorProvider implements ColorProvider {
|
||||
|
||||
@Override
|
||||
public String apply(String str) {
|
||||
return ChatColor.translateAlternateColorCodes('&', str);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package net.frankheijden.serverutils.bungee.entities;
|
||||
|
||||
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
|
||||
public class BungeeCommandSender implements ServerCommandSender {
|
||||
|
||||
private final CommandSender sender;
|
||||
|
||||
public BungeeCommandSender(CommandSender sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(String message) {
|
||||
sender.sendMessage(message);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package net.frankheijden.serverutils.bungee.entities;
|
||||
|
||||
import net.frankheijden.serverutils.common.entities.LoadResult;
|
||||
import net.frankheijden.serverutils.common.entities.Result;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
|
||||
public class BungeeLoadResult extends LoadResult<Plugin> {
|
||||
|
||||
public BungeeLoadResult(Plugin obj, Result result) {
|
||||
super(obj, result);
|
||||
}
|
||||
|
||||
public BungeeLoadResult(Plugin obj) {
|
||||
super(obj);
|
||||
}
|
||||
|
||||
public BungeeLoadResult(Result result) {
|
||||
super(result);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
package net.frankheijden.serverutils.bungee.entities;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.frankheijden.serverutils.bungee.ServerUtils;
|
||||
import net.frankheijden.serverutils.common.providers.ColorProvider;
|
||||
import net.frankheijden.serverutils.common.providers.PluginProvider;
|
||||
import net.frankheijden.serverutils.common.providers.ResourceProvider;
|
||||
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
|
||||
public class BungeePlugin extends ServerUtilsPlugin {
|
||||
|
||||
private final ServerUtils plugin;
|
||||
private final PluginProvider<Plugin> pluginProvider;
|
||||
private final ResourceProvider resourceProvider;
|
||||
private final ColorProvider colorProvider;
|
||||
|
||||
/**
|
||||
* Creates a new BungeePlugin instance of ServerUtils.
|
||||
* @param plugin The ServerUtils plugin.
|
||||
*/
|
||||
public BungeePlugin(ServerUtils plugin) {
|
||||
this.plugin = plugin;
|
||||
this.pluginProvider = new BungeePluginProvider(plugin);
|
||||
this.resourceProvider = new BungeeResourceProvider(plugin);
|
||||
this.colorProvider = new BungeeColorProvider();
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public PluginProvider<Plugin> getPluginProvider() {
|
||||
return pluginProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceProvider getResourceProvider() {
|
||||
return resourceProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ColorProvider getColorProvider() {
|
||||
return colorProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Logger getLogger() {
|
||||
return plugin.getLogger();
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getDataFolder() {
|
||||
return plugin.getDataFolder();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
package net.frankheijden.serverutils.bungee.entities;
|
||||
|
||||
import net.frankheijden.serverutils.bungee.ServerUtils;
|
||||
import net.frankheijden.serverutils.bungee.managers.PluginManager;
|
||||
import net.frankheijden.serverutils.common.providers.PluginProvider;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class BungeePluginProvider extends PluginProvider<Plugin> {
|
||||
|
||||
private final ServerUtils plugin;
|
||||
|
||||
public BungeePluginProvider(ServerUtils plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getPluginsFolder() {
|
||||
return plugin.getProxy().getPluginsFolder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Plugin> getPlugins() {
|
||||
return getPlugins(false);
|
||||
}
|
||||
|
||||
public List<Plugin> getPlugins(boolean modules) {
|
||||
Collection<Plugin> plugins = plugin.getProxy().getPluginManager().getPlugins();
|
||||
if (modules) return new ArrayList<>(plugins);
|
||||
return plugins.stream()
|
||||
.filter(PluginManager::isPlugin)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPluginName(Plugin plugin) {
|
||||
return plugin.getDataFolder().getName();
|
||||
}
|
||||
|
||||
public List<Plugin> getPluginsSorted(boolean modules) {
|
||||
List<Plugin> plugins = getPlugins(modules);
|
||||
plugins.sort(Comparator.comparing(this::getPluginName));
|
||||
return plugins;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package net.frankheijden.serverutils.bungee.entities;
|
||||
|
||||
import net.frankheijden.serverutils.common.reflection.ReflectionUtils;
|
||||
import net.frankheijden.serverutils.common.reflection.VersionParam;
|
||||
|
||||
public class BungeeReflection extends ReflectionUtils {
|
||||
|
||||
@Override
|
||||
public boolean isCompatible(VersionParam param) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
package net.frankheijden.serverutils.bungee.entities;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import net.frankheijden.serverutils.bungee.ServerUtils;
|
||||
import net.frankheijden.serverutils.common.providers.ResourceProvider;
|
||||
import net.frankheijden.serverutils.common.config.YamlConfig;
|
||||
|
||||
public class BungeeResourceProvider implements ResourceProvider {
|
||||
|
||||
private final ServerUtils plugin;
|
||||
|
||||
public BungeeResourceProvider(ServerUtils plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getResource(String resource) {
|
||||
return plugin.getResourceAsStream(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public YamlConfig load(InputStream is) {
|
||||
return new BungeeYamlConfig(is);
|
||||
}
|
||||
|
||||
@Override
|
||||
public YamlConfig load(File file) {
|
||||
try {
|
||||
return new BungeeYamlConfig(file);
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
package net.frankheijden.serverutils.bungee.entities;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collection;
|
||||
|
||||
import net.frankheijden.serverutils.common.config.YamlConfig;
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
import net.md_5.bungee.config.ConfigurationProvider;
|
||||
import net.md_5.bungee.config.YamlConfiguration;
|
||||
|
||||
public class BungeeYamlConfig implements YamlConfig {
|
||||
|
||||
private static final ConfigurationProvider provider = ConfigurationProvider.getProvider(YamlConfiguration.class);
|
||||
private final Configuration config;
|
||||
private File file = null;
|
||||
|
||||
public BungeeYamlConfig(File file) throws IOException {
|
||||
this.config = provider.load(file);
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
public BungeeYamlConfig(InputStream in) {
|
||||
this.config = provider.load(in);
|
||||
}
|
||||
|
||||
public BungeeYamlConfig(Configuration config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get(String path) {
|
||||
Object obj = config.get(path);
|
||||
if (obj instanceof Configuration) {
|
||||
return new BungeeYamlConfig((Configuration) obj);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String path, Object value) {
|
||||
config.set(path, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String path) {
|
||||
return config.getString(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String path) {
|
||||
return config.getBoolean(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<? extends String> getKeys() {
|
||||
return config.getKeys();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() throws IOException {
|
||||
provider.save(config, file);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,182 @@
|
|||
package net.frankheijden.serverutils.bungee.managers;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import net.frankheijden.serverutils.bungee.ServerUtils;
|
||||
import net.frankheijden.serverutils.bungee.entities.BungeeLoadResult;
|
||||
import net.frankheijden.serverutils.bungee.entities.BungeePluginProvider;
|
||||
import net.frankheijden.serverutils.bungee.reflection.RPluginClassLoader;
|
||||
import net.frankheijden.serverutils.bungee.reflection.RPluginManager;
|
||||
import net.frankheijden.serverutils.common.entities.CloseableResult;
|
||||
import net.frankheijden.serverutils.common.entities.Result;
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import net.md_5.bungee.api.plugin.PluginDescription;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class PluginManager {
|
||||
|
||||
private static final ProxyServer proxy = ProxyServer.getInstance();
|
||||
private static final ServerUtils plugin = ServerUtils.getInstance();
|
||||
private static final BungeePluginProvider provider = (BungeePluginProvider) plugin.getPlugin().getPluginProvider();
|
||||
|
||||
/**
|
||||
* Checks whether a loaded plugin is a module.
|
||||
* @param plugin The plugin to check.
|
||||
* @return Whether or not it's a module.
|
||||
*/
|
||||
public static boolean isModule(Plugin plugin) {
|
||||
return plugin.getFile().getParent().equalsIgnoreCase("modules");
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a loaded plugin is an actual plugin and not a module.
|
||||
* @param plugin The plugin to check.
|
||||
* @return Whether or not it's a plugin.
|
||||
*/
|
||||
public static boolean isPlugin(Plugin plugin) {
|
||||
return !isModule(plugin);
|
||||
}
|
||||
|
||||
public static BungeeLoadResult loadPlugin(String fileName) {
|
||||
File file = getPluginFileExact(fileName);
|
||||
if (!file.exists()) return new BungeeLoadResult(Result.NOT_EXISTS);
|
||||
return loadPlugin(file);
|
||||
}
|
||||
|
||||
public static BungeeLoadResult loadPlugin(File file) {
|
||||
PluginDescription desc;
|
||||
try {
|
||||
desc = getPluginDescription(file);
|
||||
} catch (Exception ex) {
|
||||
proxy.getLogger().log(Level.WARNING, "Could not load plugin from file " + file, ex );
|
||||
return new BungeeLoadResult(Result.INVALID_DESCRIPTION);
|
||||
}
|
||||
|
||||
try {
|
||||
URL url = desc.getFile().toURI().toURL();
|
||||
URLClassLoader loader = (URLClassLoader) RPluginClassLoader.newInstance(proxy, desc, url);
|
||||
|
||||
Class<?> main = loader.loadClass(desc.getMain());
|
||||
Plugin plugin = (Plugin) main.getDeclaredConstructor().newInstance();
|
||||
|
||||
RPluginManager.getPlugins(proxy.getPluginManager()).put(desc.getName(), plugin);
|
||||
plugin.onLoad();
|
||||
proxy.getLogger().log(Level.INFO, "Loaded plugin {0} version {1} by {2}", new Object[] {
|
||||
desc.getName(), desc.getVersion(), desc.getAuthor()
|
||||
});
|
||||
return new BungeeLoadResult(plugin);
|
||||
} catch (Throwable th) {
|
||||
proxy.getLogger().log(Level.WARNING, "Error loading plugin " + desc.getName(), th);
|
||||
return new BungeeLoadResult(Result.ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
public static Result enablePlugin(Plugin plugin) {
|
||||
PluginDescription desc = plugin.getDescription();
|
||||
String name = desc.getName();
|
||||
try {
|
||||
plugin.onEnable();
|
||||
proxy.getLogger().log(Level.INFO, "Enabled plugin {0} version {1} by {2}", new Object[] {
|
||||
name, desc.getVersion(), desc.getAuthor()
|
||||
});
|
||||
return Result.SUCCESS;
|
||||
} catch (Throwable th) {
|
||||
proxy.getLogger().log(Level.WARNING, "Exception encountered when loading plugin: " + name, th);
|
||||
return Result.ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
public static CloseableResult reloadPlugin(String pluginName) {
|
||||
Plugin plugin = proxy.getPluginManager().getPlugin(pluginName);
|
||||
if (plugin == null) return new CloseableResult(Result.NOT_ENABLED);
|
||||
return reloadPlugin(plugin);
|
||||
}
|
||||
|
||||
public static CloseableResult reloadPlugin(Plugin plugin) {
|
||||
CloseableResult result = unloadPlugin(plugin);
|
||||
if (result.getResult() != Result.SUCCESS) return result;
|
||||
|
||||
File file = getPluginFile(plugin.getDescription().getName());
|
||||
if (file == null) return result.set(Result.FILE_DELETED);
|
||||
|
||||
BungeeLoadResult loadResult = loadPlugin(file);
|
||||
if (!loadResult.isSuccess()) return result.set(loadResult.getResult());
|
||||
|
||||
return result.set(enablePlugin(loadResult.get()));
|
||||
}
|
||||
|
||||
public static CloseableResult unloadPlugin(String pluginName) {
|
||||
Plugin plugin = proxy.getPluginManager().getPlugin(pluginName);
|
||||
if (plugin == null) return new CloseableResult(Result.NOT_ENABLED);
|
||||
return unloadPlugin(plugin);
|
||||
}
|
||||
|
||||
public static CloseableResult unloadPlugin(Plugin plugin) {
|
||||
plugin.onDisable();
|
||||
proxy.getPluginManager().unregisterCommands(plugin);
|
||||
proxy.getPluginManager().unregisterListeners(plugin);
|
||||
try {
|
||||
RPluginManager.clearPlugin(proxy.getPluginManager(), plugin);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return new CloseableResult(Result.ERROR);
|
||||
}
|
||||
return new CloseableResult(getCloseable(plugin));
|
||||
}
|
||||
|
||||
public static File getPluginFileExact(String fileName) {
|
||||
return new File(proxy.getPluginsFolder(), fileName);
|
||||
}
|
||||
|
||||
public static File getPluginFile(String pluginName) {
|
||||
for (File file : provider.getPluginJars()) {
|
||||
PluginDescription desc;
|
||||
try {
|
||||
desc = getPluginDescription(file);
|
||||
} catch (Exception ex) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (desc.getName().equals(pluginName)) return file;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static PluginDescription getPluginDescription(File file) throws Exception {
|
||||
try (JarFile jar = new JarFile(file)) {
|
||||
JarEntry entry = getPluginDescriptionEntry(jar);
|
||||
Preconditions.checkNotNull(entry, "Plugin must have a plugin.yml or bungee.yml");
|
||||
|
||||
try (InputStream in = jar.getInputStream(entry)) {
|
||||
Yaml yaml = RPluginManager.getYaml(proxy.getPluginManager());
|
||||
PluginDescription desc = yaml.loadAs(in, PluginDescription.class);
|
||||
Preconditions.checkNotNull(desc.getName(), "Plugin from %s has no name", file);
|
||||
Preconditions.checkNotNull(desc.getMain(), "Plugin from %s has no main", file);
|
||||
|
||||
desc.setFile(file);
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static JarEntry getPluginDescriptionEntry(JarFile jar) {
|
||||
JarEntry entry = jar.getJarEntry("bungee.yml");
|
||||
if (entry == null) return jar.getJarEntry("plugin.yml");
|
||||
return entry;
|
||||
}
|
||||
|
||||
public static Closeable getCloseable(Plugin plugin) {
|
||||
ClassLoader loader = plugin.getClass().getClassLoader();
|
||||
if (loader instanceof Closeable) return (Closeable) loader;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
package net.frankheijden.serverutils.bungee.reflection;
|
||||
|
||||
import static net.frankheijden.serverutils.common.reflection.FieldParam.fieldOf;
|
||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.getAllFields;
|
||||
import static net.frankheijden.serverutils.common.reflection.VersionParam.ALL_VERSIONS;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Map;
|
||||
|
||||
public class RPlugin {
|
||||
|
||||
private static Class<?> pluginClass;
|
||||
private static Map<String, Field> fields;
|
||||
|
||||
static {
|
||||
try {
|
||||
pluginClass = Class.forName("net.md_5.bungee.api.plugin.Plugin");
|
||||
fields = getAllFields(pluginClass,
|
||||
fieldOf("plugins", ALL_VERSIONS),
|
||||
fieldOf("toLoad", ALL_VERSIONS),
|
||||
fieldOf("commandsByPlugin", ALL_VERSIONS),
|
||||
fieldOf("listenersByPlugin", ALL_VERSIONS));
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
package net.frankheijden.serverutils.bungee.reflection;
|
||||
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import net.md_5.bungee.api.plugin.PluginDescription;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.URL;
|
||||
import java.util.Map;
|
||||
|
||||
import static net.frankheijden.serverutils.common.reflection.FieldParam.fieldOf;
|
||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.getAllFields;
|
||||
import static net.frankheijden.serverutils.common.reflection.VersionParam.ALL_VERSIONS;
|
||||
|
||||
public class RPluginClassLoader {
|
||||
|
||||
private static Class<?> loaderClass;
|
||||
private static Map<String, Field> fields;
|
||||
private static Constructor<?> constructor;
|
||||
|
||||
static {
|
||||
try {
|
||||
loaderClass = Class.forName("net.md_5.bungee.api.plugin.PluginClassloader");
|
||||
constructor = loaderClass.getDeclaredConstructor(ProxyServer.class, PluginDescription.class, URL[].class);
|
||||
constructor.setAccessible(true);
|
||||
fields = getAllFields(loaderClass);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static Object newInstance(ProxyServer proxy, PluginDescription desc, URL... urls) throws Exception {
|
||||
return constructor.newInstance(proxy, desc, urls);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,74 @@
|
|||
package net.frankheijden.serverutils.bungee.reflection;
|
||||
|
||||
import static net.frankheijden.serverutils.common.reflection.FieldParam.fieldOf;
|
||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.get;
|
||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.getAllFields;
|
||||
import static net.frankheijden.serverutils.common.reflection.VersionParam.ALL_VERSIONS;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.collect.Multimap;
|
||||
import net.frankheijden.serverutils.common.utils.MapUtils;
|
||||
import net.md_5.bungee.api.plugin.Command;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
public class RPluginManager {
|
||||
|
||||
private static Class<?> pluginManagerClass;
|
||||
private static Map<String, Field> fields;
|
||||
|
||||
static {
|
||||
try {
|
||||
pluginManagerClass = Class.forName("net.md_5.bungee.api.plugin.PluginManager");
|
||||
fields = getAllFields(pluginManagerClass,
|
||||
fieldOf("yaml", ALL_VERSIONS),
|
||||
fieldOf("plugins", ALL_VERSIONS),
|
||||
fieldOf("commandMap", ALL_VERSIONS),
|
||||
fieldOf("toLoad", ALL_VERSIONS),
|
||||
fieldOf("commandsByPlugin", ALL_VERSIONS));
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the plugin from the PluginManager.
|
||||
* @param instance The instance of the PluginManager.
|
||||
* @param plugin The plugin to clear.
|
||||
* @throws ReflectiveOperationException Iff a reflection error happened.
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static void clearPlugin(Object instance, Plugin plugin) throws ReflectiveOperationException {
|
||||
String pluginName = plugin.getDescription().getName();
|
||||
MapUtils.remove((Map) get(fields, instance, "plugins"), pluginName);
|
||||
MapUtils.remove((Map) get(fields, instance, "toLoad"), pluginName);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Map<String, Plugin> getPlugins(Object instance) throws ReflectiveOperationException {
|
||||
return (Map<String, Plugin>) get(fields, instance, "plugins");
|
||||
}
|
||||
|
||||
public static Yaml getYaml(Object instance) throws IllegalAccessException {
|
||||
return (Yaml) get(fields, instance, "yaml");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Map<String, Command> getCommands(Object instance) throws IllegalAccessException {
|
||||
return (Map<String, Command>) get(fields, instance, "commandMap");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Plugin getPlugin(Object instance, Command cmd) throws IllegalAccessException {
|
||||
Object obj = get(fields, instance, "commandsByPlugin");
|
||||
Multimap<Plugin, Command> plugins = (Multimap<Plugin, Command>) obj;
|
||||
if (plugins == null) return null;
|
||||
|
||||
for (Map.Entry<Plugin, Command> entry : plugins.entries()) {
|
||||
if (entry.getValue().equals(cmd)) return entry.getKey();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package net.frankheijden.serverutils.bungee.utils;
|
||||
|
||||
import net.frankheijden.serverutils.bungee.entities.BungeeCommandSender;
|
||||
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
|
||||
public class BungeeUtils {
|
||||
|
||||
public static ServerCommandSender wrap(CommandSender sender) {
|
||||
return new BungeeCommandSender(sender);
|
||||
}
|
||||
}
|
||||
5
Bungee/src/main/resources/bungee-config.yml
Normal file
5
Bungee/src/main/resources/bungee-config.yml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
settings:
|
||||
check-updates: true
|
||||
check-updates-login: false
|
||||
download-updates: false
|
||||
download-at-startup-and-update: false
|
||||
56
Bungee/src/main/resources/bungee-messages.yml
Normal file
56
Bungee/src/main/resources/bungee-messages.yml
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
serverutils:
|
||||
success: "&3Successfully %action%ed &b%what%&3!"
|
||||
warning: "&3Successfully %action%ed &b%what%&3, but with warnings."
|
||||
error: "&cAn error occurred while %action%ing &4%what%&c, please check the console!"
|
||||
not_exists: "&cAn error occurred while %action%ing &4%what%&c, plugin does not exist!"
|
||||
not_enabled: "&cAn error occurred while %action%ing &4%what%&c, plugin is not enabled!"
|
||||
already_loaded: "&cAn error occurred while %action%ing &4%what%&c, plugin is already loaded!"
|
||||
already_enabled: "&cAn error occurred while %action%ing &4%what%&c, plugin is already enabled!"
|
||||
already_disabled: "&cAn error occurred while %action%ing &4%what%&c, plugin is already disabled!"
|
||||
file_deleted: "&cAccessing the jar file while %action%ing &4%what%&c went wrong, plugin has been deleted!"
|
||||
invalid_description: "&cAn error occurred while %action%ing &4%what%&c, plugin doesn't have a valid description, please check the console!"
|
||||
invalid_plugin: "&cAn error occurred while %action%ing &4%what%&c, plugin is invalid!"
|
||||
unknown_dependency: "&cAn error occurred while %action%ing &4%what%&c, plugin has a dependeny which is not loaded: &4%arg%"
|
||||
update:
|
||||
available: |-
|
||||
&8&m---------=&r&8[ &b&lServerUtils Bungee Update&r &8]&m=----------
|
||||
&3Current version: &b%old%
|
||||
&3New version: &b%new%
|
||||
&3Release info: &b%info%
|
||||
&8&m-------------------------------------------------
|
||||
downloading: |-
|
||||
&8&m---------=&r&8[ &b&lServerUtils Bungee Update&r &8]&m=----------
|
||||
&3A new version of ServerUtils will be downloaded and installed after a restart!
|
||||
&3Current version: &b%old%
|
||||
&3New version: &b%new%
|
||||
&3Release info: &b%info%
|
||||
&8&m-------------------------------------------------
|
||||
download_failed: "&cFailed to download version %new% of ServerUtils. Please update manually."
|
||||
download_success: "&3ServerUtils has been downloaded and will be installed on the next restart."
|
||||
help:
|
||||
header: "&8&m----------=&r&8[ &b&lServerUtils Bungee Help&r &8]&m=----------"
|
||||
format: "&8/&3%command%&b%subcommand% &f(&7%help%&f)"
|
||||
footer: "&8&m-------------------------------------------------"
|
||||
plugins:
|
||||
header: "&8&m---------=&r&8[ &b&lServerUtils Bungee Plugins&r &8]&m=--------"
|
||||
prefix: " &3Plugins &8(&a%count%&8)&b: "
|
||||
format: "&3%plugin%"
|
||||
seperator: "&b, "
|
||||
last_seperator: " &band "
|
||||
version: " &8(&a%version%&8)"
|
||||
footer: "&8&m-------------------------------------------------"
|
||||
plugininfo:
|
||||
header: "&8&m-------=&r&8[ &b&lServerUtils Bungee PluginInfo&r &8]&m=-------"
|
||||
format: " &3%key%&8: &b%value%"
|
||||
list_format: "&b%value%"
|
||||
seperator: "&8, "
|
||||
last_seperator: " &8and "
|
||||
footer: "&8&m-------------------------------------------------"
|
||||
commandinfo:
|
||||
header: "&8&m------=&r&8[ &b&lServerUtils Bungee CommandInfo&r &8]&m=-------"
|
||||
format: " &3%key%&8: &b%value%"
|
||||
list_format: "&b%value%"
|
||||
seperator: "&8, "
|
||||
last_seperator: " &8and "
|
||||
footer: "&8&m-------------------------------------------------"
|
||||
not_exists: "&cThat command is not a valid registered command."
|
||||
4
Bungee/src/main/resources/bungee.yml
Normal file
4
Bungee/src/main/resources/bungee.yml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
name: ServerUtils
|
||||
main: net.frankheijden.serverutils.bungee.ServerUtils
|
||||
version: ${version}
|
||||
author: FrankHeijden
|
||||
|
|
@ -7,5 +7,5 @@ repositories {
|
|||
}
|
||||
|
||||
dependencies {
|
||||
compileOnly 'com.google.code.gson:gson:2.8.6'
|
||||
compileOnly 'com.google.code.gson:gson:2.8.0'
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
package net.frankheijden.serverutils.common;
|
||||
|
||||
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||
|
||||
public class ServerUtilsApp {
|
||||
|
||||
public static final int BSTATS_METRICS_ID = 7790;
|
||||
private static ServerUtilsPlugin plugin;
|
||||
|
||||
public static void init(ServerUtilsPlugin plugin) {
|
||||
ServerUtilsApp.plugin = plugin;
|
||||
}
|
||||
|
||||
public static ServerUtilsPlugin getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package net.frankheijden.serverutils.common.commands;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import net.frankheijden.serverutils.common.config.Messenger;
|
||||
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||
import net.frankheijden.serverutils.common.utils.ListBuilder;
|
||||
import net.frankheijden.serverutils.common.utils.ListFormat;
|
||||
|
||||
public class Plugins {
|
||||
|
||||
/**
|
||||
* Sends a plugin list to the receiver.
|
||||
* @param sender The receiver of the plugin list.
|
||||
* @param plugins The plugins to be sent.
|
||||
* @param pluginFormat The format of the plugins to be sent.
|
||||
* @param <T> The plugin type.
|
||||
*/
|
||||
public static <T> void sendPlugins(ServerCommandSender sender, List<T> plugins, ListFormat<T> pluginFormat) {
|
||||
Messenger.sendMessage(sender, "serverutils.plugins.header");
|
||||
String prefix = Messenger.getMessage("serverutils.plugins.prefix",
|
||||
"%count%", String.valueOf(plugins.size()));
|
||||
sender.sendMessage(Messenger.color(prefix + ListBuilder.create(plugins)
|
||||
.seperator(Messenger.getMessage("serverutils.plugins.seperator"))
|
||||
.lastSeperator(Messenger.getMessage("serverutils.plugins.last_seperator"))
|
||||
.format(pluginFormat)
|
||||
.toString()));
|
||||
Messenger.sendMessage(sender, "serverutils.plugins.footer");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package net.frankheijden.serverutils.common.config;
|
||||
|
||||
public class Config extends YamlResource {
|
||||
|
||||
private static Config instance;
|
||||
|
||||
public Config(String fileName, String resource) {
|
||||
super(fileName, resource);
|
||||
instance = this;
|
||||
}
|
||||
|
||||
public static Config getInstance() {
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
package net.frankheijden.serverutils.common.config;
|
||||
|
||||
import net.frankheijden.serverutils.common.ServerUtilsApp;
|
||||
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||
import net.frankheijden.serverutils.common.utils.StringUtils;
|
||||
|
||||
public class Messenger extends YamlResource {
|
||||
|
||||
private static Messenger instance;
|
||||
private static final ServerUtilsPlugin plugin = ServerUtilsApp.getPlugin();
|
||||
|
||||
public Messenger(String fileName, String resource) {
|
||||
super(fileName, resource);
|
||||
instance = this;
|
||||
}
|
||||
|
||||
public static Messenger getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a message from the config.
|
||||
* @param path The yml path to the message.
|
||||
* @param replacements The replacements to be taken into account.
|
||||
* @return The config message with translated placeholders.
|
||||
*/
|
||||
public static String getMessage(String path, String... replacements) {
|
||||
String message = instance.getConfig().getString(path);
|
||||
if (message != null) {
|
||||
return StringUtils.apply(message, replacements);
|
||||
} else {
|
||||
instance.plugin.getLogger().severe("Missing locale in messages.yml at path '" + path + "'!");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to a player with translated placeholders.
|
||||
* @param sender The receiver.
|
||||
* @param msg The message to be sent.
|
||||
* @param replacements The replacements to be taken into account.
|
||||
*/
|
||||
public static void sendRawMessage(ServerCommandSender sender, String msg, String... replacements) {
|
||||
String message = StringUtils.apply(msg, replacements);
|
||||
if (message != null) {
|
||||
sender.sendMessage(instance.plugin.getColorProvider().apply(message));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message from the specified config path to a player with translated placeholders.
|
||||
* @param sender The receiver.
|
||||
* @param path The yml path to the message.
|
||||
* @param replacements The replacements to be taken into account.
|
||||
*/
|
||||
public static void sendMessage(ServerCommandSender sender, String path, String... replacements) {
|
||||
String message = getMessage(path, replacements);
|
||||
if (message != null) {
|
||||
sender.sendMessage(instance.plugin.getColorProvider().apply(message));
|
||||
}
|
||||
}
|
||||
|
||||
public static String color(String str) {
|
||||
return instance.plugin.getColorProvider().apply(str);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
package net.frankheijden.serverutils.common.config;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
|
||||
public interface YamlConfig {
|
||||
|
||||
Object get(String path);
|
||||
|
||||
void set(String path, Object value);
|
||||
|
||||
String getString(String path);
|
||||
|
||||
boolean getBoolean(String path);
|
||||
|
||||
Collection<? extends String> getKeys();
|
||||
|
||||
void save() throws IOException;
|
||||
|
||||
static void addDefaults(YamlConfig def, YamlConfig conf) {
|
||||
addDefaults(def, conf, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds defaults if keys don't exist to the configuration specified.
|
||||
* @param def The defaults to copy values over from.
|
||||
* @param conf The configuration to copy the defaults to.
|
||||
* @param root The current root path of the iteration.
|
||||
*/
|
||||
static void addDefaults(YamlConfig def, YamlConfig conf, String root) {
|
||||
if (def == null) return;
|
||||
for (String key : def.getKeys()) {
|
||||
String newKey = (root.isEmpty() ? "" : root + ".") + key;
|
||||
Object value = def.get(key);
|
||||
if (value instanceof YamlConfig) {
|
||||
addDefaults((YamlConfig) value, conf, newKey);
|
||||
} else if (conf.get(newKey) == null) {
|
||||
conf.set(newKey, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates a Configuration from a file with associated defaults.
|
||||
* @param def The default Configuration to be applied.
|
||||
* @param conf The Configuration where the defaults will be applied to.
|
||||
* @return The loaded Configuration of the file with defaults.
|
||||
*/
|
||||
static YamlConfig init(YamlConfig def, YamlConfig conf) {
|
||||
YamlConfig.addDefaults(def, conf);
|
||||
|
||||
try {
|
||||
conf.save();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return conf;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
package net.frankheijden.serverutils.common.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
|
||||
import net.frankheijden.serverutils.common.ServerUtilsApp;
|
||||
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||
import net.frankheijden.serverutils.common.providers.ResourceProvider;
|
||||
|
||||
public class YamlResource {
|
||||
|
||||
private static final ServerUtilsPlugin plugin = ServerUtilsApp.getPlugin();
|
||||
|
||||
private final YamlConfig config;
|
||||
|
||||
/**
|
||||
* Creates a new YamlResource instance.
|
||||
* Loads the resource from the jar file.
|
||||
* @param fileName The destination file.
|
||||
* @param resource The resource from the jar file.
|
||||
*/
|
||||
public YamlResource(String fileName, String resource) {
|
||||
ResourceProvider provider = plugin.getResourceProvider();
|
||||
InputStream is = provider.getResource(resource);
|
||||
File file = plugin.copyResourceIfNotExists(fileName, resource);
|
||||
config = YamlConfig.init(provider.load(is), provider.load(file));
|
||||
}
|
||||
|
||||
public YamlConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package net.frankheijden.serverutils.bukkit.managers;
|
||||
package net.frankheijden.serverutils.common.entities;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
package net.frankheijden.serverutils.common.entities;
|
||||
|
||||
public class LoadResult<T> {
|
||||
|
||||
private final T obj;
|
||||
private final Result result;
|
||||
|
||||
public LoadResult(T obj, Result result) {
|
||||
this.obj = obj;
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public LoadResult(T obj) {
|
||||
this(obj, Result.SUCCESS);
|
||||
}
|
||||
|
||||
public LoadResult(Result result) {
|
||||
this(null, result);
|
||||
}
|
||||
|
||||
public T get() {
|
||||
return obj;
|
||||
}
|
||||
|
||||
public Result getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean isSuccess() {
|
||||
return obj != null && result == Result.SUCCESS;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
package net.frankheijden.serverutils.bukkit.managers;
|
||||
package net.frankheijden.serverutils.common.entities;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.config.Messenger;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import net.frankheijden.serverutils.common.config.Messenger;
|
||||
|
||||
public enum Result {
|
||||
NOT_EXISTS,
|
||||
|
|
@ -34,7 +33,7 @@ public enum Result {
|
|||
* @param action The action which let to the result.
|
||||
* @param what An associated variable.
|
||||
*/
|
||||
public void sendTo(CommandSender sender, String action, String what) {
|
||||
public void sendTo(ServerCommandSender sender, String action, String what) {
|
||||
Messenger.sendMessage(sender, "serverutils." + this.name().toLowerCase(),
|
||||
"%action%", action,
|
||||
"%what%", what,
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
package net.frankheijden.serverutils.common.entities;
|
||||
|
||||
public interface ServerCommandSender {
|
||||
|
||||
void sendMessage(String message);
|
||||
}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
package net.frankheijden.serverutils.common.entities;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import net.frankheijden.serverutils.common.providers.ColorProvider;
|
||||
import net.frankheijden.serverutils.common.providers.PluginProvider;
|
||||
import net.frankheijden.serverutils.common.providers.ResourceProvider;
|
||||
import net.frankheijden.serverutils.common.utils.FileUtils;
|
||||
|
||||
public abstract class ServerUtilsPlugin {
|
||||
|
||||
public abstract <T> PluginProvider<T> getPluginProvider();
|
||||
|
||||
public abstract ResourceProvider getResourceProvider();
|
||||
|
||||
public abstract ColorProvider getColorProvider();
|
||||
|
||||
public abstract Logger getLogger();
|
||||
|
||||
public abstract File getDataFolder();
|
||||
|
||||
public void createDataFolderIfNotExists() {
|
||||
if (getDataFolder().exists()) return;
|
||||
getDataFolder().mkdirs();
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies a resource from the jar to the specified target file name under the datafolder.
|
||||
* @param targetName The target file under the datafolder.
|
||||
* @param resource The resource from the jar file to copy.
|
||||
* @return The target file.
|
||||
*/
|
||||
public File copyResourceIfNotExists(String targetName, String resource) {
|
||||
createDataFolderIfNotExists();
|
||||
|
||||
File file = new File(getDataFolder(), targetName);
|
||||
if (!file.exists()) {
|
||||
getLogger().info(String.format("'%s' not found, creating!", targetName));
|
||||
try {
|
||||
FileUtils.saveResource(getResourceProvider().getResource(resource), file);
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
package net.frankheijden.serverutils.common.providers;
|
||||
|
||||
public interface ColorProvider {
|
||||
|
||||
String apply(String str);
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
package net.frankheijden.serverutils.common.providers;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class PluginProvider<T> {
|
||||
|
||||
public abstract File getPluginsFolder();
|
||||
|
||||
public abstract List<T> getPlugins();
|
||||
|
||||
public abstract String getPluginName(T plugin);
|
||||
|
||||
public List<T> getPluginsSorted() {
|
||||
List<T> plugins = getPlugins();
|
||||
plugins.sort(Comparator.comparing(this::getPluginName));
|
||||
return plugins;
|
||||
}
|
||||
|
||||
public List<String> getPluginNames() {
|
||||
return getPlugins().stream()
|
||||
.map(this::getPluginName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all files with a jar extension in the plugins/ folder and returns solely their name.
|
||||
* @return An list of jar file names.
|
||||
*/
|
||||
public List<String> getPluginFileNames() {
|
||||
return Arrays.stream(getPluginJars())
|
||||
.map(File::getName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all files with a jar extension in the plugins/ folder.
|
||||
* @return An array of jar files.
|
||||
*/
|
||||
public File[] getPluginJars() {
|
||||
File parent = getPluginsFolder();
|
||||
if (parent == null || !parent.exists()) return new File[0];
|
||||
return parent.listFiles(f -> f.getName().endsWith(".jar"));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package net.frankheijden.serverutils.common.providers;
|
||||
|
||||
import net.frankheijden.serverutils.common.config.YamlConfig;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
|
||||
public interface ResourceProvider {
|
||||
|
||||
InputStream getResource(String resource);
|
||||
|
||||
YamlConfig load(InputStream is);
|
||||
|
||||
YamlConfig load(File file);
|
||||
}
|
||||
|
|
@ -14,6 +14,7 @@ import java.net.URL;
|
|||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
|
||||
public class FileUtils {
|
||||
|
||||
|
|
@ -80,4 +81,17 @@ public class FileUtils {
|
|||
return new JsonParser().parse(jsonText);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves an InputStream to a file.
|
||||
* @param in The InputStream.
|
||||
* @param target The target file.
|
||||
* @return Whether or not file was created / already exists.
|
||||
* @throws IOException If an I/O exception occurs.
|
||||
*/
|
||||
public static boolean saveResource(InputStream in, File target) throws IOException {
|
||||
if (target.exists()) return false;
|
||||
Files.copy(in, target.toPath());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
package net.frankheijden.serverutils.bukkit.utils;
|
||||
package net.frankheijden.serverutils.common.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.config.Messenger;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import net.frankheijden.serverutils.common.config.Messenger;
|
||||
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||
|
||||
public class FormatBuilder {
|
||||
|
||||
|
|
@ -43,13 +44,13 @@ public class FormatBuilder {
|
|||
* Builds the format and sends it to the CommandSender.
|
||||
* @param sender The receiver of the list.
|
||||
*/
|
||||
public void sendTo(CommandSender sender) {
|
||||
public void sendTo(ServerCommandSender sender) {
|
||||
valueList.forEach(values -> {
|
||||
int length = Math.min(values.length, orderedKeys.length);
|
||||
String message = format;
|
||||
for (int i = 0; i < length; i++) {
|
||||
String value = values[i];
|
||||
if ((value == null || value.isEmpty()) && !alwaysSend) return;
|
||||
if (value == null && !alwaysSend) return;
|
||||
message = message.replace(orderedKeys[i], String.valueOf(value));
|
||||
}
|
||||
Messenger.sendRawMessage(sender, message);
|
||||
|
|
@ -1,27 +1,29 @@
|
|||
package net.frankheijden.serverutils.bukkit.utils;
|
||||
package net.frankheijden.serverutils.common.utils;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
import net.frankheijden.serverutils.common.utils.PredicateFilter;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||
|
||||
public class ForwardFilter extends PredicateFilter {
|
||||
|
||||
private static final char INFO_COLOR = 'a';
|
||||
private static final char WARNING_COLOR = '6';
|
||||
private static final char SEVERE_COLOR = 'c';
|
||||
|
||||
private boolean warnings;
|
||||
|
||||
/**
|
||||
* Creates a filter which forwards all output to the sender.
|
||||
* @param sender The sender to forward logs to.
|
||||
*/
|
||||
public ForwardFilter(CommandSender sender) {
|
||||
public ForwardFilter(ServerCommandSender sender) {
|
||||
this.warnings = false;
|
||||
|
||||
setPredicate(rec -> {
|
||||
ChatColor color = getColor(rec.getLevel());
|
||||
if (color != ChatColor.GREEN) warnings = true;
|
||||
sender.sendMessage(color + format(rec));
|
||||
char color = getColor(rec.getLevel());
|
||||
if (color != INFO_COLOR) warnings = true;
|
||||
sender.sendMessage('&' + color + format(rec));
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
|
@ -30,13 +32,13 @@ public class ForwardFilter extends PredicateFilter {
|
|||
return warnings;
|
||||
}
|
||||
|
||||
private static ChatColor getColor(Level level) {
|
||||
private static char getColor(Level level) {
|
||||
if (Level.SEVERE.equals(level)) {
|
||||
return ChatColor.RED;
|
||||
return SEVERE_COLOR;
|
||||
} else if (Level.WARNING.equals(level)) {
|
||||
return ChatColor.GOLD;
|
||||
return WARNING_COLOR;
|
||||
}
|
||||
return ChatColor.GREEN;
|
||||
return INFO_COLOR;
|
||||
}
|
||||
|
||||
private static String format(LogRecord record) {
|
||||
|
|
@ -1,5 +1,8 @@
|
|||
package net.frankheijden.serverutils.common.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class ListBuilder<T> {
|
||||
|
|
@ -17,6 +20,14 @@ public class ListBuilder<T> {
|
|||
return new ListBuilder<>(list);
|
||||
}
|
||||
|
||||
public static <T> ListBuilder<T> create(Collection<T> list) {
|
||||
return new ListBuilder<>(new ArrayList<>(list));
|
||||
}
|
||||
|
||||
public static <T> ListBuilder<T> create(T... elements) {
|
||||
return new ListBuilder<>(Arrays.asList(elements));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a pre-defined ListBuilder with type String.
|
||||
* @param list The collection to be used.
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ public class MapUtils {
|
|||
*/
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public static void removeKeys(Map map, Predicate<Object> predicate) {
|
||||
if (map == null) return;
|
||||
Set<Object> keysToRemove = new HashSet<>();
|
||||
map.forEach((k, v) -> {
|
||||
if (predicate.test(k)) {
|
||||
|
|
@ -30,12 +31,24 @@ public class MapUtils {
|
|||
*/
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public static void removeValues(Map map, Predicate<Object> predicate) {
|
||||
Set<Object> keysToRemove = new HashSet<>();
|
||||
if (map == null) return;
|
||||
Set<Object> valuesToRemove = new HashSet<>();
|
||||
map.forEach((k, v) -> {
|
||||
if (predicate.test(v)) {
|
||||
keysToRemove.add(k);
|
||||
valuesToRemove.add(k);
|
||||
}
|
||||
});
|
||||
keysToRemove.forEach(map::remove);
|
||||
valuesToRemove.forEach(map::remove);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a key from a map.
|
||||
* @param map The map instance.
|
||||
* @param obj The object to remove.
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static void remove(Map map, Object obj) {
|
||||
if (map == null) return;
|
||||
map.remove(obj);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
package net.frankheijden.serverutils.common.utils;
|
||||
|
||||
public class StringUtils {
|
||||
|
||||
/**
|
||||
* Applies placeholders to a message.
|
||||
* @param message The message.
|
||||
* @param replacements The replacements of the message. Expects input to be even and in a key-value like format.
|
||||
* Example: ["%player%", "Player"]
|
||||
* @return The message with translated placeholders.
|
||||
*/
|
||||
public static String apply(String message, String... replacements) {
|
||||
if (message == null || message.isEmpty()) return null;
|
||||
message = message.replace("\\n", "\n");
|
||||
for (int i = 0; i < replacements.length; i++, i++) {
|
||||
message = message.replace(replacements[i], replacements[i + 1]);
|
||||
}
|
||||
return message;
|
||||
}
|
||||
}
|
||||
16
build.gradle
16
build.gradle
|
|
@ -4,6 +4,7 @@ plugins {
|
|||
}
|
||||
|
||||
group = 'net.frankheijden.serverutils'
|
||||
String dependencyDir = group + '.dependencies'
|
||||
version = '1.5.3'
|
||||
|
||||
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
|
||||
|
|
@ -42,17 +43,22 @@ subprojects {
|
|||
toolVersion "8.25"
|
||||
configFile = rootProject.file('config/checkstyle/checkstyle.xml')
|
||||
|
||||
ignoreFailures = false
|
||||
maxErrors = 0
|
||||
maxWarnings = 0
|
||||
// ignoreFailures = false
|
||||
// maxErrors = 0
|
||||
// maxWarnings = 0
|
||||
}
|
||||
|
||||
shadowJar.dependsOn checkstyleMain, checkstyleTest, test
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile project(path: ':Common', configuration: 'shadow')
|
||||
compile project(path: ':Bukkit', configuration: 'shadow')
|
||||
implementation project(path: ':Common', configuration: 'shadow')
|
||||
implementation project(path: ':Bukkit', configuration: 'shadow')
|
||||
implementation project(path: ':Bungee', configuration: 'shadow')
|
||||
}
|
||||
|
||||
build.dependsOn shadowJar
|
||||
|
|
|
|||
|
|
@ -1,3 +1,2 @@
|
|||
rootProject.name = 'ServerUtils'
|
||||
include 'Common', 'Bukkit'
|
||||
|
||||
include 'Common', 'Bukkit', 'Bungee'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue