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

Operators reloading info #6780

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
60d3682
Commit
Asleeepp May 29, 2024
ca7df36
Commit x2
Asleeepp Jun 11, 2024
e522450
whoops
Asleeepp Jun 11, 2024
d91cb9a
Merge branch 'dev/feature' into addition-operators-reloading-info
Asleeepp Jun 11, 2024
299355d
Suggested
Asleeepp Jun 11, 2024
b14b938
Merge remote-tracking branch 'origin/addition-operators-reloading-inf…
Asleeepp Jun 11, 2024
aa33f20
One last change
Asleeepp Jun 28, 2024
52a957f
Commit
Asleeepp May 29, 2024
705235c
Commit x2
Asleeepp Jun 11, 2024
4c0384d
whoops
Asleeepp Jun 11, 2024
b4d9ffa
Suggested
Asleeepp Jun 11, 2024
96edf65
One last change
Asleeepp Jun 28, 2024
186f43d
change
Asleeepp Jul 1, 2024
c1ecc9e
Merge remote-tracking branch 'origin/addition-operators-reloading-inf…
Asleeepp Jul 1, 2024
f64cfd0
yes?!
Asleeepp Jul 1, 2024
573660a
moved config
Asleeepp Jul 1, 2024
1eb07ff
e
Asleeepp Jul 1, 2024
a981ed1
simplification
Asleeepp Jul 1, 2024
52e29b3
e
Asleeepp Jul 1, 2024
f12b61e
pickle please :pray:
Asleeepp Jul 2, 2024
2b58032
stupid IDE
Asleeepp Jul 2, 2024
ffee17d
aaaaaaaaaaaaaaaaaa
Asleeepp Jul 2, 2024
91226fb
Changes, once again
Asleeepp Jul 3, 2024
4115dd0
chagne
Asleeepp Jul 12, 2024
3061e2e
deleted left other thing from a pr that i didnt make
Asleeepp Jul 12, 2024
3f35534
e
Asleeepp Jul 12, 2024
b15f09d
Merge remote-tracking branch 'origin/addition-operators-reloading-inf…
Asleeepp Jul 12, 2024
f501eef
done
Asleeepp Jul 14, 2024
3673f27
Merge branch 'dev/feature' into addition-operators-reloading-info
sovdeeth Jul 18, 2024
bffc5ad
Merge branch 'dev/feature' into addition-operators-reloading-info
sovdeeth Aug 15, 2024
57cc472
Cleanup + Remove config
Asleeepp Aug 15, 2024
405577d
Merge branch 'dev/feature' into addition-operators-reloading-info
Moderocky Aug 30, 2024
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
156 changes: 92 additions & 64 deletions src/main/java/ch/njol/skript/SkriptCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import ch.njol.skript.localization.Language;
import ch.njol.skript.localization.PluralizingArgsMessage;
import ch.njol.skript.log.RedirectingLogHandler;
import ch.njol.skript.log.SkriptLogger;
import ch.njol.skript.log.TimingLogHandler;
import ch.njol.skript.test.runner.SkriptTestEvent;
import ch.njol.skript.test.runner.TestMode;
Expand Down Expand Up @@ -54,8 +55,9 @@
import java.util.Set;
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 @@ -90,30 +92,45 @@ 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) {
String prefix = args.length == 0 ? Language.get(CONFIG_NODE + ".reload." + "player reload") : Language.format(CONFIG_NODE + ".reload." + "player reload", sender.getName());
Asleeepp marked this conversation as resolved.
Show resolved Hide resolved
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);
if (SkriptConfig.sendReloadingInfoToOps.value()) {
String lowerCaseMessage = lowerCaseFirstChar(message);
notifyOperators(prefix + " " + lowerCaseMessage, sender, logHandler);
Asleeepp marked this conversation as resolved.
Show resolved Hide resolved
}
}



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) {

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 timeTaken = String.valueOf(timingLogHandler.getTimeTaken());

String message;
if (logHandler.numErrors() == 0) {
message = StringUtils.fixCapitalization(PluralizingArgsMessage.format(m_reloaded.toString(what, timeTaken)));
Skript.info(sender, message);
} else {
message = StringUtils.fixCapitalization(PluralizingArgsMessage.format(m_reload_error.toString(what, logHandler.numErrors(), timeTaken)));
Skript.error(sender, message);
}

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))));
if (SkriptConfig.sendReloadingInfoToOps.value())
notifyOperators(message, sender, logHandler);
}



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 @@ -132,7 +149,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
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();
Expand All @@ -144,10 +161,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))
Expand All @@ -156,22 +171,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;
Expand All @@ -182,7 +191,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)
Expand All @@ -193,7 +202,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 -> {
Expand All @@ -206,9 +215,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 {
Expand All @@ -224,9 +231,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;
Expand Down Expand Up @@ -281,9 +286,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());
Expand All @@ -293,9 +296,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;
Expand Down Expand Up @@ -339,9 +340,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);
Expand All @@ -354,9 +353,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");
Expand Down Expand Up @@ -393,9 +390,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() + "'");
Expand All @@ -408,9 +403,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;
Expand Down Expand Up @@ -444,9 +437,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);
}

Expand All @@ -457,10 +448,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);
Expand All @@ -473,7 +464,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 @@ -515,7 +506,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 @@ -537,5 +528,42 @@ private static Set<File> toggleFiles(File folder, boolean enable) throws IOExcep

return changed;
}


/**
* Sends the Skript reloading message to operators and console, requires a {@link RedirectingLogHandler} and {@link CommandSender}.
Asleeepp marked this conversation as resolved.
Show resolved Hide resolved
*/

Asleeepp marked this conversation as resolved.
Show resolved Hide resolved
private static void notifyOperators(String message, CommandSender sender, RedirectingLogHandler logHandler) {
Asleeepp marked this conversation as resolved.
Show resolved Hide resolved
if (SkriptConfig.sendReloadingInfoToOps.value()) {
Asleeepp marked this conversation as resolved.
Show resolved Hide resolved
Bukkit.getOnlinePlayers().stream()
.filter(player -> player.hasPermission("skript.reloadnotify") && !player.equals(sender))
.forEach(player -> {
if (logHandler.numErrors() == 0) {
Skript.info(player, message);
} else {
for (String errorMsg : logHandler.getErrors()) {
SkriptLogger.sendFormatted(player, errorMsg);
}
Skript.error(player, message);
}
});

// send to console
if (logHandler.numErrors() == 0) {
Skript.info(Bukkit.getConsoleSender(), message);
} else {
for (String errorMsg : logHandler.getErrors()) {
SkriptLogger.sendFormatted(Bukkit.getConsoleSender(), errorMsg);
}
Skript.error(Bukkit.getConsoleSender(), message);
}
}
}

private static String lowerCaseFirstChar(String str) {
if (str == null || str.isEmpty()) {
return str;
}
return Character.toLowerCase(str.charAt(0)) + str.substring(1);
}
}
2 changes: 2 additions & 0 deletions src/main/java/ch/njol/skript/SkriptConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public class SkriptConfig {
public static final Option<String> effectCommandToken = new Option<>("effect command token", "!");
public static final Option<Boolean> allowOpsToUseEffectCommands = new Option<>("allow ops to use effect commands", false);

public static final Option<Boolean> sendReloadingInfoToOps = new Option<>("send reloading info to ops", false);

/*
* @deprecated Will be removed in 2.8.0. Use {@link #logEffectCommands} instead.
*/
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/ch/njol/skript/log/RedirectingLogHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
*/
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.List;
import java.util.logging.Level;

/**
Expand All @@ -30,31 +31,35 @@
public class RedirectingLogHandler extends LogHandler {

private final CommandSender recipient;

private final String prefix;

private int numErrors = 0;

private final List<String> errorMessages = new ArrayList<>();
Asleeepp marked this conversation as resolved.
Show resolved Hide resolved

public RedirectingLogHandler(CommandSender recipient, @Nullable String prefix) {
Asleeepp marked this conversation as resolved.
Show resolved Hide resolved
this.recipient = recipient;
this.prefix = prefix == null ? "" : prefix;
}

@Override
public LogResult log(LogEntry entry) {
SkriptLogger.sendFormatted(recipient, prefix + entry.toFormattedString());
if (entry.level == Level.SEVERE)
if (entry.level == Level.SEVERE) {
numErrors++;
errorMessages.add(entry.toFormattedString());
}
return LogResult.DO_NOT_LOG;
}

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

public int numErrors() {
return numErrors;
}

public List<String> getErrors() {
return new ArrayList<>(errorMessages);
}
Asleeepp marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 4 additions & 0 deletions src/main/resources/config.sk
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ log effect commands: false
# Whether Skript should log the usage of effect commands.
# They will be logged as [INFORMATION] in this format: '<player> issued effect command: <command>'

send reloading info to ops: false
# Tells Skript whether to alert operators about when someone reloads a script file.
Asleeepp marked this conversation as resolved.
Show resolved Hide resolved
# This will look like the normal message you recieve when reloading a script, containing reload time, errors and/or warnings.

player variable fix: true
# Whether to enable the player variable fix if a player has rejoined and was reciding inside a variable.
# Player objects inside a variable(list or normal) are not updated to the new player object
Expand Down
Loading