Conform to google's checkstyle standard

This commit is contained in:
Frank van der Heijden 2020-06-27 01:28:10 +02:00
parent 3a52f9afa1
commit 83401bf622
No known key found for this signature in database
GPG key ID: 26DA56488D314D11
42 changed files with 1181 additions and 380 deletions

View file

@ -1,23 +1,45 @@
package net.frankheijden.serverutils.managers;
import net.frankheijden.serverutils.ServerUtils;
import net.frankheijden.serverutils.reflection.*;
import org.bukkit.Bukkit;
import org.bukkit.command.*;
import org.bukkit.plugin.*;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.frankheijden.serverutils.ServerUtils;
import net.frankheijden.serverutils.reflection.RCommandMap;
import net.frankheijden.serverutils.reflection.RCraftServer;
import net.frankheijden.serverutils.reflection.RCraftingManager;
import net.frankheijden.serverutils.reflection.RJavaPlugin;
import net.frankheijden.serverutils.reflection.RPlugin;
import net.frankheijden.serverutils.reflection.RPluginClassLoader;
import net.frankheijden.serverutils.reflection.RSimplePluginManager;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.InvalidDescriptionException;
import org.bukkit.plugin.InvalidPluginException;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.UnknownDependencyException;
public class PluginManager {
/**
* Loads the specified file as a plugin.
* @param jarFile The name of the file in the plugins/ folder.
* @return The result of the loading procedure.
*/
public static LoadResult loadPlugin(String jarFile) {
return loadPlugin(new File(ServerUtils.getInstance().getDataFolder().getParent(), jarFile));
}
/**
* Loads the specified file as a plugin.
* @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);
@ -47,10 +69,20 @@ public class PluginManager {
return new LoadResult(plugin);
}
/**
* Disables the specified plugin by name and cleans all commands and recipes of the plugin.
* @param pluginName The plugin to disable.
* @return The result of the disable call.
*/
public static Result disablePlugin(String pluginName) {
return disablePlugin(Bukkit.getPluginManager().getPlugin(pluginName));
}
/**
* Disables the specified plugin and cleans all commands and recipes of the plugin.
* @param plugin The plugin to disable.
* @return The result of the disable call.
*/
public static Result disablePlugin(Plugin plugin) {
if (plugin == null) return Result.NOT_ENABLED;
if (!plugin.isEnabled()) return Result.ALREADY_DISABLED;
@ -65,10 +97,20 @@ public class PluginManager {
return Result.SUCCESS;
}
/**
* Unloads the specified plugin by name and cleans all traces within bukkit.
* @param pluginName The plugin to unload.
* @return The result of the unload.
*/
public static Result unloadPlugin(String pluginName) {
return unloadPlugin(Bukkit.getPluginManager().getPlugin(pluginName));
}
/**
* Unloads the specified plugin and cleans all traces within bukkit.
* @param plugin The plugin to unload.
* @return The result of the unload.
*/
public static Result unloadPlugin(Plugin plugin) {
if (plugin == null) return Result.NOT_EXISTS;
try {
@ -82,10 +124,20 @@ public class PluginManager {
return Result.SUCCESS;
}
/**
* Enables the specified plugin by name.
* @param pluginName The plugin to enable.
* @return The result of the enabling.
*/
public static Result enablePlugin(String pluginName) {
return enablePlugin(Bukkit.getPluginManager().getPlugin(pluginName));
}
/**
* Enables the specified plugin.
* @param plugin The plugin to enable.
* @return The result of the enabling.
*/
public static Result enablePlugin(Plugin plugin) {
if (plugin == null) return Result.NOT_EXISTS;
if (Bukkit.getPluginManager().isPluginEnabled(plugin.getName())) return Result.ALREADY_ENABLED;
@ -96,12 +148,22 @@ public class PluginManager {
return Result.ERROR;
}
/**
* Reloads the specified plugin by name.
* @param pluginName The plugin to reload.
* @return The result of the reload.
*/
public static Result reloadPlugin(String pluginName) {
Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginName);
if (plugin == null) return Result.NOT_EXISTS;
return reloadPlugin(plugin);
}
/**
* Reloads the specified plugin.
* @param plugin The plugin to reload.
* @return The result of the reload.
*/
public static Result reloadPlugin(Plugin plugin) {
Result disableResult = disablePlugin(plugin);
if (disableResult != Result.SUCCESS && disableResult != Result.ALREADY_DISABLED) return disableResult;
@ -125,6 +187,10 @@ public class PluginManager {
return enablePlugin(loadResult.getPlugin());
}
/**
* Retrieves all known commands registered to bukkit.
* @return A map with all known commands.
*/
public static Map<String, Command> getKnownCommands() {
try {
return RCommandMap.getKnownCommands(RCraftServer.getCommandMap());
@ -134,6 +200,10 @@ public class PluginManager {
}
}
/**
* Unregisters all commands from the specified plugin.
* @param plugin The plugin.
*/
public static void unregisterCommands(Plugin plugin) {
Map<String, Command> knownCommands = getKnownCommands();
if (knownCommands == null) return;
@ -151,12 +221,21 @@ public class PluginManager {
});
}
/**
* Retrieves a command from the command map.
* @param command The command string.
* @return The command.
*/
public static Command getCommand(String command) {
Map<String, Command> knownCommands = getKnownCommands();
if (knownCommands == null) return null;
return knownCommands.get(command);
}
/**
* Retrieves all file associations, i.e. all plugin loaders.
* @return A map with all pluginloaders.
*/
public static Map<Pattern, PluginLoader> getFileAssociations() {
try {
return RSimplePluginManager.getFileAssociations(Bukkit.getPluginManager());
@ -166,6 +245,11 @@ public class PluginManager {
}
}
/**
* Retrieves the PluginLoader for the input file.
* @param file The file.
* @return The appropiate PluginLoader.
*/
public static PluginLoader getPluginLoader(File file) {
Map<Pattern, PluginLoader> fileAssociations = getFileAssociations();
if (fileAssociations == null) return null;
@ -179,6 +263,11 @@ public class PluginManager {
return null;
}
/**
* Retrieves a loaded plugin associated to a jar file.
* @param file The jar file.
* @return The already loaded plugin, or null if none.
*/
public static Plugin getLoadedPlugin(File file) {
PluginDescriptionFile descriptionFile;
try {
@ -190,6 +279,12 @@ public class PluginManager {
return Bukkit.getPluginManager().getPlugin(descriptionFile.getName());
}
/**
* Retrieves the PluginDescriptionFile of a jar file.
* @param file The jar file.
* @return The PluginDescriptionFile.
* @throws InvalidDescriptionException Iff the PluginDescriptionFile is invalid.
*/
public static PluginDescriptionFile getPluginDescription(File file) throws InvalidDescriptionException {
PluginLoader loader = getPluginLoader(file);
if (loader == null) return null;