Feature - plugin watcher, detect file changes & reload

This commit is contained in:
Frank van der Heijden 2020-07-20 20:19:07 +02:00
parent 719163c579
commit 07719cc1cc
No known key found for this signature in database
GPG key ID: 26DA56488D314D11
24 changed files with 387 additions and 15 deletions

View file

@ -4,8 +4,6 @@ import co.aikar.commands.BukkitCommandCompletionContext;
import co.aikar.commands.CommandCompletions;
import co.aikar.commands.PaperCommandManager;
import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.Map;
import net.frankheijden.serverutils.bukkit.commands.CommandPlugins;
@ -60,6 +58,7 @@ public class ServerUtils extends JavaPlugin implements CommandExecutor {
reload();
Bukkit.getPluginManager().registerEvents(new BukkitListener(), this);
plugin.enable();
ServerUtilsApp.tryCheckForUpdates();
}
@ -76,6 +75,7 @@ public class ServerUtils extends JavaPlugin implements CommandExecutor {
public void onDisable() {
super.onDisable();
restoreBukkitPluginCommand();
plugin.disable();
}
private void removeCommands(String... commands) {

View file

@ -24,6 +24,7 @@ import net.frankheijden.serverutils.bukkit.reflection.RCraftServer;
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.AbstractResult;
import net.frankheijden.serverutils.common.entities.CloseableResult;
import net.frankheijden.serverutils.common.entities.Result;
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
@ -237,6 +238,35 @@ public class CommandServerUtils extends BaseCommand {
result.sendTo(BukkitUtils.wrap(sender), "disabl", pluginName);
}
/**
* Watches the given plugin and reloads it when a change is detected to the file.
* @param sender The sender of the command.
* @param pluginName The plugin name.
*/
@Subcommand("watchplugin")
@CommandCompletion("@plugins")
@CommandPermission("serverutils.watchplugin")
@Description("Watches the specified plugin for changes.")
public void onWatchPlugin(CommandSender sender, String pluginName) {
ServerCommandSender commandSender = BukkitUtils.wrap(sender);
AbstractResult result = BukkitPluginManager.get().watchPlugin(commandSender, pluginName);
result.sendTo(commandSender, "watch", pluginName);
}
/**
* Stops watching the given plugin.
* @param sender The sender of the command.
* @param pluginName The plugin name.
*/
@Subcommand("unwatchplugin")
@CommandCompletion("@plugins")
@CommandPermission("serverutils.unwatchplugin")
@Description("Stops watching the specified plugin for changes.")
public void onUnwatchPlugin(CommandSender sender, String pluginName) {
AbstractResult result = BukkitPluginManager.get().unwatchPlugin(pluginName);
result.sendTo(BukkitUtils.wrap(sender), "unwatch", pluginName);
}
/**
* Shows information about the specified plugin.
* @param commandSender The sender of the command.

View file

@ -2,6 +2,7 @@ package net.frankheijden.serverutils.bukkit.entities;
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
/**
* A wrap for a Bukkit CommandSender.
@ -36,4 +37,13 @@ public class BukkitCommandSender implements ServerCommandSender {
public boolean hasPermission(String permission) {
return sender.hasPermission(permission);
}
/**
* Whether or not the given instance is a player.
* @return Boolean true or false.
*/
@Override
public boolean isPlayer() {
return sender instanceof Player;
}
}

View file

@ -38,6 +38,7 @@ public class BukkitPlugin extends ServerUtilsPlugin {
}
@Override
@SuppressWarnings("unchecked")
public BukkitTaskManager getTaskManager() {
return taskManager;
}

View file

@ -334,11 +334,21 @@ public class BukkitPluginManager extends AbstractPluginManager<Plugin> {
return loader.getPluginDescription(file);
}
@Override
public File getPluginFile(Plugin plugin) {
try {
return RJavaPlugin.getFile(plugin);
} catch (ReflectiveOperationException ex) {
throw new RuntimeException("Error retrieving current plugin file", ex);
}
}
/**
* Attempts to retrieve the plugin file by plugin name.
* @param pluginName The plugin name.
* @return The file, or null if invalid or not found.
*/
@Override
public File getPluginFile(String pluginName) {
for (File file : getPluginJars()) {
PluginDescriptionFile descriptionFile;
@ -354,12 +364,8 @@ public class BukkitPluginManager extends AbstractPluginManager<Plugin> {
}
@Override
public File getPluginFile(Plugin plugin) {
try {
return RJavaPlugin.getFile(plugin);
} catch (ReflectiveOperationException ex) {
throw new RuntimeException("Error retrieving current plugin file", ex);
}
public Plugin getPlugin(String pluginName) {
return Bukkit.getPluginManager().getPlugin(pluginName);
}
@Override

View file

@ -3,16 +3,21 @@ package net.frankheijden.serverutils.bukkit.managers;
import net.frankheijden.serverutils.bukkit.ServerUtils;
import net.frankheijden.serverutils.common.managers.AbstractTaskManager;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitTask;
public class BukkitTaskManager extends AbstractTaskManager {
public class BukkitTaskManager extends AbstractTaskManager<BukkitTask> {
public BukkitTaskManager() {
super(BukkitTask::cancel);
}
@Override
public void runTask(Runnable runnable) {
Bukkit.getScheduler().runTask(ServerUtils.getInstance(), runnable);
addTask(Bukkit.getScheduler().runTask(ServerUtils.getInstance(), runnable));
}
@Override
public void runTaskAsynchronously(Runnable runnable) {
Bukkit.getScheduler().runTaskAsynchronously(ServerUtils.getInstance(), runnable);
addTask(Bukkit.getScheduler().runTaskAsynchronously(ServerUtils.getInstance(), runnable));
}
}

View file

@ -11,6 +11,11 @@ serverutils:
invalid_description: "&cAn error occurred while %action%ing &4%what%&c, plugin doesn't have a valid description!"
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%"
watcher:
start: "&3Started watching &b%what%&3!"
change: "&3Change detected for plugin &b%what%&3, reloading now..."
stopped: "&3Stopped watching &b%what%&3!"
not_watching: "&cWe aren't watching that plugin!"
update:
available: |-
&8&m------------=&r&8[ &b&lServerUtils Update&r &8]&m=--------------