Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Operators reloading info" #7102

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 66 additions & 61 deletions src/main/java/ch/njol/skript/SkriptCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
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;
Expand Down Expand Up @@ -55,12 +54,10 @@
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");

Expand Down Expand Up @@ -95,39 +92,30 @@ public class SkriptCommand implements CommandExecutor {
if (TestMode.DEV_MODE)
SKRIPT_COMMAND_HELP.add("test");
}

private static void reloading(CommandSender sender, String what, RedirectingLogHandler logHandler, Object... args) {
private static void reloading(CommandSender sender, String what, Object... args) {
what = args.length == 0 ? Language.get(CONFIG_NODE + ".reload." + what) : Language.format(CONFIG_NODE + ".reload." + what, args);
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)));
Skript.info(sender, StringUtils.fixCapitalization(m_reloading.toString(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 logHandler, TimingLogHandler timingLogHandler, String what, Object... args) {
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());

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));
}
}
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 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));
Expand All @@ -138,24 +126,15 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
if (!SKRIPT_COMMAND_HELP.test(sender, args))
return true;

Set<CommandSender> 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(recipients, "").start();
RedirectingLogHandler logHandler = new RedirectingLogHandler(sender, "").start();
TimingLogHandler timingLogHandler = new TimingLogHandler().start()
) {

if (args[0].equalsIgnoreCase("reload")) {

if (args[1].equalsIgnoreCase("all")) {
reloading(sender, "config, aliases and scripts", logHandler);
reloading(sender, "config, aliases and scripts");
SkriptConfig.load();
Aliases.clear();
Aliases.load();
Expand All @@ -167,8 +146,10 @@ 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", logHandler);
}

else if (args[1].equalsIgnoreCase("scripts")) {
reloading(sender, "scripts");

ScriptLoader.unloadScripts(ScriptLoader.getLoadedScripts());
ScriptLoader.loadScripts(Skript.getInstance().getScriptsFolder(), OpenCloseable.combine(logHandler, timingLogHandler))
Expand All @@ -177,16 +158,22 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
Skript.warning(Skript.m_no_scripts.toString());
reloaded(sender, logHandler, timingLogHandler, "scripts");
});
} else if (args[1].equalsIgnoreCase("config")) {
reloading(sender, "main config", logHandler);
}

else if (args[1].equalsIgnoreCase("config")) {
reloading(sender, "main config");
SkriptConfig.load();
reloaded(sender, logHandler, timingLogHandler, "main config");
} else if (args[1].equalsIgnoreCase("aliases")) {
reloading(sender, "aliases", logHandler);
}

else if (args[1].equalsIgnoreCase("aliases")) {
reloading(sender, "aliases");
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;
Expand All @@ -197,7 +184,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
return true;
}

reloading(sender, "script", logHandler, scriptFile.getName());
reloading(sender, "script", scriptFile.getName());

Script script = ScriptLoader.getScript(scriptFile);
if (script != null)
Expand All @@ -208,7 +195,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
);
} else {
final String fileName = scriptFile.getName();
reloading(sender, "scripts in folder", logHandler, fileName);
reloading(sender, "scripts in folder", fileName);
ScriptLoader.unloadScripts(ScriptLoader.getScripts(scriptFile));
ScriptLoader.loadScripts(scriptFile, OpenCloseable.combine(logHandler, timingLogHandler))
.thenAccept(scriptInfo -> {
Expand All @@ -221,7 +208,9 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}
}

} else if (args[0].equalsIgnoreCase("enable")) {
}

else if (args[0].equalsIgnoreCase("enable")) {

if (args[1].equalsIgnoreCase("all")) {
try {
Expand All @@ -237,7 +226,9 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
} catch (IOException e) {
error(sender, "enable.all.io error", ExceptionUtils.toString(e));
}
} else {
}

else {
File scriptFile = getScriptFromArgs(sender, args);
if (scriptFile == null)
return true;
Expand Down Expand Up @@ -292,7 +283,9 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}
}

} else if (args[0].equalsIgnoreCase("disable")) {
}

else if (args[0].equalsIgnoreCase("disable")) {

if (args[1].equalsIgnoreCase("all")) {
ScriptLoader.unloadScripts(ScriptLoader.getLoadedScripts());
Expand All @@ -302,7 +295,9 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
} 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;
Expand Down Expand Up @@ -346,7 +341,9 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}
}

} 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);
Expand All @@ -359,7 +356,9 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
} 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");
Expand Down Expand Up @@ -396,7 +395,9 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
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() + "'");
Expand All @@ -411,7 +412,9 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
htmlGenerator.generate(); // Try to generate docs... hopefully
jsonGenerator.generate();
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;
Expand Down Expand Up @@ -445,7 +448,9 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}
})
);
} else if (args[0].equalsIgnoreCase("help")) {
}

else if (args[0].equalsIgnoreCase("help")) {
SKRIPT_COMMAND_HELP.showHelp(sender);
}

Expand All @@ -456,10 +461,10 @@ public boolean onCommand(CommandSender sender, Command command, String label, St

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);
Expand All @@ -472,7 +477,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
Expand Down Expand Up @@ -514,7 +519,7 @@ private static File toggleFile(File file, boolean enable) throws IOException {
false
);
}

private static Set<File> toggleFiles(File folder, boolean enable) throws IOException {
FileFilter filter = enable ? ScriptLoader.getDisabledScriptsFilter() : ScriptLoader.getLoadedScriptsFilter();

Expand All @@ -536,5 +541,5 @@ private static Set<File> toggleFiles(File folder, boolean enable) throws IOExcep

return changed;
}

}
36 changes: 14 additions & 22 deletions src/main/java/ch/njol/skript/log/RedirectingLogHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,43 @@
*/
package ch.njol.skript.log;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.logging.Level;

/**
* Redirects the log to one or more {@link CommandSender}s.
* Redirects the log to a {@link CommandSender}.
*/
public class RedirectingLogHandler extends LogHandler {

private final Collection<CommandSender> recipients;
private int numErrors = 0;
private final CommandSender recipient;

private final String prefix;


private int numErrors = 0;

public RedirectingLogHandler(CommandSender recipient, @Nullable String prefix) {
this(Collections.singletonList(recipient), prefix);
}

public RedirectingLogHandler(Collection<CommandSender> recipients, @Nullable String prefix) {
this.recipients = new ArrayList<>(recipients);
this.recipient = recipient;
this.prefix = prefix == null ? "" : prefix;
}

@Override
public LogResult log(LogEntry entry) {
String formattedMessage = prefix + entry.toFormattedString();
for (CommandSender recipient : recipients) {
SkriptLogger.sendFormatted(recipient, formattedMessage);
}
if (entry.level == Level.SEVERE) {
SkriptLogger.sendFormatted(recipient, prefix + entry.toFormattedString());
if (entry.level == Level.SEVERE)
numErrors++;
}
return LogResult.DO_NOT_LOG;
}

@Override
public RedirectingLogHandler start() {
return SkriptLogger.startLogHandler(this);
}

public int numErrors() {
return numErrors;
}
}

}
1 change: 0 additions & 1 deletion src/main/resources/lang/english.lang
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ skript command:
error line info: <light red><bold>Line %s:<gray> (%s)<reset>\n
reloading: Reloading <gold>%s<reset>...
reloaded: <lime>Successfully reloaded <gold>%s<lime>. <gray>(<gold>%2$sms<gray>)
player reload: <lime>%s <reset>is reloading <gold>%s<reset>...
error: <light red>Encountered <gold>%2$s <light red>error¦¦s¦ while reloading <gold>%1$s<light red>! <gray>(<gold>%3$sms<gray>)
script disabled: <gold>%s<reset> is currently disabled. Use <gray>/<gold>skript <cyan>enable <red>%s<reset> to enable it.
warning details: <yellow> %s<reset>\n
Expand Down