diff --git a/src/main/java/ch/njol/skript/SkriptCommand.java b/src/main/java/ch/njol/skript/SkriptCommand.java index 0bc25391267..9a6091b2df6 100644 --- a/src/main/java/ch/njol/skript/SkriptCommand.java +++ b/src/main/java/ch/njol/skript/SkriptCommand.java @@ -25,6 +25,7 @@ import ch.njol.skript.localization.ArgsMessage; import ch.njol.skript.localization.Language; import ch.njol.skript.localization.PluralizingArgsMessage; +import ch.njol.skript.log.LogEntry; import ch.njol.skript.log.RedirectingLogHandler; import ch.njol.skript.log.TimingLogHandler; import ch.njol.skript.test.runner.SkriptTestEvent; @@ -52,10 +53,12 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.logging.Level; import java.util.stream.Collectors; + public class SkriptCommand implements CommandExecutor { - + private static final String CONFIG_NODE = "skript command"; private static final ArgsMessage m_reloading = new ArgsMessage(CONFIG_NODE + ".reload.reloading"); @@ -90,30 +93,39 @@ public class SkriptCommand implements CommandExecutor { if (TestMode.DEV_MODE) SKRIPT_COMMAND_HELP.add("test"); } - - private static void reloading(CommandSender sender, String what, Object... args) { + + private static void reloading(CommandSender sender, String what, RedirectingLogHandler logHandler, Object... args) { what = args.length == 0 ? Language.get(CONFIG_NODE + ".reload." + what) : Language.format(CONFIG_NODE + ".reload." + what, args); - Skript.info(sender, StringUtils.fixCapitalization(m_reloading.toString(what))); + String message = StringUtils.fixCapitalization(m_reloading.toString(what)); + Skript.info(sender, message); + + // Log reloading message + logHandler.log(new LogEntry(Level.INFO, Language.format(CONFIG_NODE + ".reload." + "player reload", sender.getName(), what))); } - + + private static final ArgsMessage m_reloaded = new ArgsMessage(CONFIG_NODE + ".reload.reloaded"); private static final ArgsMessage m_reload_error = new ArgsMessage(CONFIG_NODE + ".reload.error"); - - private static void reloaded(CommandSender sender, RedirectingLogHandler r, TimingLogHandler timingLogHandler, String what, Object... args) { - what = args.length == 0 ? Language.get(CONFIG_NODE + ".reload." + what) : PluralizingArgsMessage.format(Language.format(CONFIG_NODE + ".reload." + what, args)); - String timeTaken = String.valueOf(timingLogHandler.getTimeTaken()); - if (r.numErrors() == 0) - Skript.info(sender, StringUtils.fixCapitalization(PluralizingArgsMessage.format(m_reloaded.toString(what, timeTaken)))); - else - Skript.error(sender, StringUtils.fixCapitalization(PluralizingArgsMessage.format(m_reload_error.toString(what, r.numErrors(), timeTaken)))); + private static void reloaded(CommandSender sender, RedirectingLogHandler logHandler, TimingLogHandler timingLogHandler, String what, Object... args) { + what = args.length == 0 ? Language.get(CONFIG_NODE + ".reload." + what) : PluralizingArgsMessage.format(Language.format(CONFIG_NODE + ".reload." + what, args)); + String timeTaken = String.valueOf(timingLogHandler.getTimeTaken()); + + String message; + if (logHandler.numErrors() == 0) { + message = StringUtils.fixCapitalization(PluralizingArgsMessage.format(m_reloaded.toString(what, timeTaken))); + logHandler.log(new LogEntry(Level.INFO, message)); + } else { + message = StringUtils.fixCapitalization(PluralizingArgsMessage.format(m_reload_error.toString(what, logHandler.numErrors(), timeTaken))); + logHandler.log(new LogEntry(Level.SEVERE, message)); + } } - + private static void info(CommandSender sender, String what, Object... args) { what = args.length == 0 ? Language.get(CONFIG_NODE + "." + what) : PluralizingArgsMessage.format(Language.format(CONFIG_NODE + "." + what, args)); Skript.info(sender, StringUtils.fixCapitalization(what)); } - + private static void error(CommandSender sender, String what, Object... args) { what = args.length == 0 ? Language.get(CONFIG_NODE + "." + what) : PluralizingArgsMessage.format(Language.format(CONFIG_NODE + "." + what, args)); Skript.error(sender, StringUtils.fixCapitalization(what)); @@ -124,15 +136,24 @@ public boolean onCommand(CommandSender sender, Command command, String label, St if (!SKRIPT_COMMAND_HELP.test(sender, args)) return true; + Set recipients = new HashSet<>(); + recipients.add(sender); + + if (args[0].equalsIgnoreCase("reload")) { + recipients.addAll(Bukkit.getOnlinePlayers().stream() + .filter(player -> player.hasPermission("skript.reloadnotify")) + .collect(Collectors.toSet())); + } + try ( - RedirectingLogHandler logHandler = new RedirectingLogHandler(sender, "").start(); + RedirectingLogHandler logHandler = new RedirectingLogHandler(recipients, "").start(); TimingLogHandler timingLogHandler = new TimingLogHandler().start() ) { if (args[0].equalsIgnoreCase("reload")) { if (args[1].equalsIgnoreCase("all")) { - reloading(sender, "config, aliases and scripts"); + reloading(sender, "config, aliases and scripts", logHandler); SkriptConfig.load(); Aliases.clear(); Aliases.load(); @@ -144,10 +165,8 @@ public boolean onCommand(CommandSender sender, Command command, String label, St Skript.warning(Skript.m_no_scripts.toString()); reloaded(sender, logHandler, timingLogHandler, "config, aliases and scripts"); }); - } - - else if (args[1].equalsIgnoreCase("scripts")) { - reloading(sender, "scripts"); + } else if (args[1].equalsIgnoreCase("scripts")) { + reloading(sender, "scripts", logHandler); ScriptLoader.unloadScripts(ScriptLoader.getLoadedScripts()); ScriptLoader.loadScripts(Skript.getInstance().getScriptsFolder(), OpenCloseable.combine(logHandler, timingLogHandler)) @@ -156,22 +175,16 @@ else if (args[1].equalsIgnoreCase("scripts")) { Skript.warning(Skript.m_no_scripts.toString()); reloaded(sender, logHandler, timingLogHandler, "scripts"); }); - } - - else if (args[1].equalsIgnoreCase("config")) { - reloading(sender, "main config"); + } else if (args[1].equalsIgnoreCase("config")) { + reloading(sender, "main config", logHandler); SkriptConfig.load(); reloaded(sender, logHandler, timingLogHandler, "main config"); - } - - else if (args[1].equalsIgnoreCase("aliases")) { - reloading(sender, "aliases"); + } else if (args[1].equalsIgnoreCase("aliases")) { + reloading(sender, "aliases", logHandler); Aliases.clear(); Aliases.load(); reloaded(sender, logHandler, timingLogHandler, "aliases"); - } - - else { // Reloading an individual Script or folder + } else { // Reloading an individual Script or folder File scriptFile = getScriptFromArgs(sender, args); if (scriptFile == null) return true; @@ -182,7 +195,7 @@ else if (args[1].equalsIgnoreCase("aliases")) { return true; } - reloading(sender, "script", scriptFile.getName()); + reloading(sender, "script", logHandler, scriptFile.getName()); Script script = ScriptLoader.getScript(scriptFile); if (script != null) @@ -193,7 +206,7 @@ else if (args[1].equalsIgnoreCase("aliases")) { ); } else { final String fileName = scriptFile.getName(); - reloading(sender, "scripts in folder", fileName); + reloading(sender, "scripts in folder", logHandler, fileName); ScriptLoader.unloadScripts(ScriptLoader.getScripts(scriptFile)); ScriptLoader.loadScripts(scriptFile, OpenCloseable.combine(logHandler, timingLogHandler)) .thenAccept(scriptInfo -> { @@ -206,9 +219,7 @@ else if (args[1].equalsIgnoreCase("aliases")) { } } - } - - else if (args[0].equalsIgnoreCase("enable")) { + } else if (args[0].equalsIgnoreCase("enable")) { if (args[1].equalsIgnoreCase("all")) { try { @@ -224,9 +235,7 @@ else if (args[0].equalsIgnoreCase("enable")) { } catch (IOException e) { error(sender, "enable.all.io error", ExceptionUtils.toString(e)); } - } - - else { + } else { File scriptFile = getScriptFromArgs(sender, args); if (scriptFile == null) return true; @@ -281,9 +290,7 @@ else if (args[0].equalsIgnoreCase("enable")) { } } - } - - else if (args[0].equalsIgnoreCase("disable")) { + } else if (args[0].equalsIgnoreCase("disable")) { if (args[1].equalsIgnoreCase("all")) { ScriptLoader.unloadScripts(ScriptLoader.getLoadedScripts()); @@ -293,9 +300,7 @@ else if (args[0].equalsIgnoreCase("disable")) { } catch (IOException e) { error(sender, "disable.all.io error", ExceptionUtils.toString(e)); } - } - - else { + } else { File scriptFile = getScriptFromArgs(sender, args); if (scriptFile == null) // TODO allow disabling deleted/renamed scripts return true; @@ -339,9 +344,7 @@ else if (args[0].equalsIgnoreCase("disable")) { } } - } - - else if (args[0].equalsIgnoreCase("update")) { + } else if (args[0].equalsIgnoreCase("update")) { SkriptUpdater updater = Skript.getInstance().getUpdater(); if (updater == null) { // Oh. That is bad Skript.info(sender, "" + SkriptUpdater.m_internal_error); @@ -354,9 +357,7 @@ else if (args[0].equalsIgnoreCase("update")) { } else if (args[1].equalsIgnoreCase("download")) { updater.updateCheck(sender); } - } - - else if (args[0].equalsIgnoreCase("info")) { + } else if (args[0].equalsIgnoreCase("info")) { info(sender, "info.aliases"); info(sender, "info.documentation"); info(sender, "info.tutorials"); @@ -393,9 +394,7 @@ else if (args[0].equalsIgnoreCase("info")) { if (!dependenciesFound) info(sender, "info.dependencies", "None"); - } - - else if (args[0].equalsIgnoreCase("gen-docs")) { + } else if (args[0].equalsIgnoreCase("gen-docs")) { File templateDir = Documentation.getDocsTemplateDirectory(); if (!templateDir.exists()) { Skript.error(sender, "Cannot generate docs! Documentation templates not found at '" + Documentation.getDocsTemplateDirectory().getPath() + "'"); @@ -408,9 +407,7 @@ else if (args[0].equalsIgnoreCase("gen-docs")) { Skript.info(sender, "Generating docs..."); generator.generate(); // Try to generate docs... hopefully Skript.info(sender, "Documentation generated!"); - } - - else if (args[0].equalsIgnoreCase("test") && TestMode.DEV_MODE) { + } else if (args[0].equalsIgnoreCase("test") && TestMode.DEV_MODE) { File scriptFile; if (args.length == 1) { scriptFile = TestMode.lastTestFile; @@ -444,9 +441,7 @@ else if (args[0].equalsIgnoreCase("test") && TestMode.DEV_MODE) { } }) ); - } - - else if (args[0].equalsIgnoreCase("help")) { + } else if (args[0].equalsIgnoreCase("help")) { SKRIPT_COMMAND_HELP.showHelp(sender); } @@ -457,10 +452,10 @@ else if (args[0].equalsIgnoreCase("help")) { return true; } - + private static final ArgsMessage m_invalid_script = new ArgsMessage(CONFIG_NODE + ".invalid script"); private static final ArgsMessage m_invalid_folder = new ArgsMessage(CONFIG_NODE + ".invalid folder"); - + @Nullable private static File getScriptFromArgs(CommandSender sender, String[] args) { String script = StringUtils.join(args, " ", 1, args.length); @@ -473,7 +468,7 @@ private static File getScriptFromArgs(CommandSender sender, String[] args) { } return f; } - + @Nullable public static File getScriptFromName(String script) { if (script.endsWith("/") || script.endsWith("\\")) { // Always allow '/' and '\' regardless of OS @@ -515,7 +510,7 @@ private static File toggleFile(File file, boolean enable) throws IOException { false ); } - + private static Set toggleFiles(File folder, boolean enable) throws IOException { FileFilter filter = enable ? ScriptLoader.getDisabledScriptsFilter() : ScriptLoader.getLoadedScriptsFilter(); @@ -537,5 +532,5 @@ private static Set toggleFiles(File folder, boolean enable) throws IOExcep return changed; } - + } diff --git a/src/main/java/ch/njol/skript/log/RedirectingLogHandler.java b/src/main/java/ch/njol/skript/log/RedirectingLogHandler.java index f20afa6ffc8..4d5a622082b 100644 --- a/src/main/java/ch/njol/skript/log/RedirectingLogHandler.java +++ b/src/main/java/ch/njol/skript/log/RedirectingLogHandler.java @@ -18,43 +18,51 @@ */ package ch.njol.skript.log; -import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.eclipse.jdt.annotation.Nullable; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.logging.Level; /** - * Redirects the log to a {@link CommandSender}. + * Redirects the log to one or more {@link CommandSender}s. */ public class RedirectingLogHandler extends LogHandler { - private final CommandSender recipient; - - private final String prefix; - + private final Collection recipients; private int numErrors = 0; - + private final String prefix; + public RedirectingLogHandler(CommandSender recipient, @Nullable String prefix) { - this.recipient = recipient; + this(Collections.singletonList(recipient), prefix); + } + + public RedirectingLogHandler(Collection recipients, @Nullable String prefix) { + this.recipients = new ArrayList<>(recipients); this.prefix = prefix == null ? "" : prefix; } - + @Override public LogResult log(LogEntry entry) { - SkriptLogger.sendFormatted(recipient, prefix + entry.toFormattedString()); - if (entry.level == Level.SEVERE) + String formattedMessage = prefix + entry.toFormattedString(); + for (CommandSender recipient : recipients) { + SkriptLogger.sendFormatted(recipient, formattedMessage); + } + if (entry.level == Level.SEVERE) { numErrors++; + } return LogResult.DO_NOT_LOG; } - + @Override public RedirectingLogHandler start() { return SkriptLogger.startLogHandler(this); } - + public int numErrors() { return numErrors; } - } + diff --git a/src/main/resources/lang/english.lang b/src/main/resources/lang/english.lang index 43e417be1a9..97f95fcdd3d 100644 --- a/src/main/resources/lang/english.lang +++ b/src/main/resources/lang/english.lang @@ -55,6 +55,7 @@ skript command: error line info: Line %s: (%s)\n reloading: Reloading %s... reloaded: Successfully reloaded %s. (%2$sms) + player reload: %s is reloading %s... error: Encountered %2$s error¦¦s¦ while reloading %1$s! (%3$sms) script disabled: %s is currently disabled. Use /skript enable %s to enable it. warning details: %s\n