Skip to content
9 changes: 3 additions & 6 deletions src/main/java/ch/njol/skript/ScriptLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
import ch.njol.skript.config.SectionNode;
import ch.njol.skript.config.SimpleNode;
import ch.njol.skript.events.bukkit.PreScriptLoadEvent;
import ch.njol.skript.lang.ExecutionIntent;
import ch.njol.skript.lang.Section;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.Statement;
import ch.njol.skript.lang.TriggerItem;
import ch.njol.skript.lang.*;
import ch.njol.skript.lang.globals.GlobalOptions;
import ch.njol.skript.lang.parser.ParserInstance;
import ch.njol.skript.log.CountingLogHandler;
import ch.njol.skript.log.LogEntry;
Expand Down Expand Up @@ -971,7 +968,7 @@ public static String replaceOptions(String string) {
return string;
OptionsData optionsData = parser.getCurrentScript().getData(OptionsData.class);
if (optionsData == null)
return string;
return GlobalOptions.replaceOptions(string);
return optionsData.replaceOptions(string);
}

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import ch.njol.skript.lang.*;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Condition.ConditionType;
import ch.njol.skript.lang.globals.GlobalOptions;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.localization.Language;
import ch.njol.skript.localization.Message;
Expand Down Expand Up @@ -521,6 +522,13 @@ public void onEnable() {
RuntimeErrorManager.refresh();
getRuntimeErrorManager().addConsumer(new BukkitRuntimeErrorConsumer());

File globalsFolder = new File(Skript.getInstance().getDataFolder(), "/globals/");
if (!globalsFolder.exists()) {
globalsFolder.mkdir();
}

new GlobalOptions().load();

// Now override the verbosity if test mode is enabled
if (TestMode.VERBOSITY != null)
SkriptLogger.setVerbosity(Verbosity.valueOf(TestMode.VERBOSITY));
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/ch/njol/skript/SkriptCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ch.njol.skript.doc.Documentation;
import ch.njol.skript.doc.HTMLGenerator;
import ch.njol.skript.doc.JSONGenerator;
import ch.njol.skript.lang.globals.GlobalOptions;
import ch.njol.skript.localization.ArgsMessage;
import ch.njol.skript.localization.Language;
import ch.njol.skript.localization.PluralizingArgsMessage;
Expand Down Expand Up @@ -53,6 +54,7 @@ public class SkriptCommand implements CommandExecutor {
.add("config")
.add("aliases")
.add("scripts")
.add("global-options")
.add("<script>")
).add(new CommandHelp("enable", SkriptColor.DARK_RED)
.add("all")
Expand Down Expand Up @@ -138,16 +140,17 @@ 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", logHandler);
reloading(sender, "config, aliases, global options and scripts", logHandler);
SkriptConfig.load();
new GlobalOptions().load();
Aliases.clear();
Aliases.loadAsync().thenRun(() -> {
ScriptLoader.unloadScripts(ScriptLoader.getLoadedScripts());
ScriptLoader.loadScripts(Skript.getInstance().getScriptsFolder(), OpenCloseable.combine(logHandler, timingLogHandler))
.thenAccept(info -> {
if (info.files == 0)
Skript.warning(Skript.m_no_scripts.toString());
reloaded(sender, logHandler, timingLogHandler, "config, aliases and scripts");
reloaded(sender, logHandler, timingLogHandler, "config, aliases, global options and scripts");
});
});
} else if (args[1].equalsIgnoreCase("scripts")) {
Expand All @@ -168,6 +171,11 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
reloading(sender, "aliases", logHandler);
Aliases.clear();
Aliases.loadAsync().thenRun(() -> reloaded(sender, logHandler, timingLogHandler, "aliases"));
} else if (args[1].equalsIgnoreCase("global-options")) {
reloading(sender, "global options", logHandler);
new GlobalOptions().load();
reloaded(sender, logHandler, timingLogHandler, "global options");
info(sender, "reload.global options");
} else { // Reloading an individual Script or folder
File scriptFile = getScriptFromArgs(sender, args);
if (scriptFile == null)
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/ch/njol/skript/lang/globals/GlobalFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ch.njol.skript.lang.globals;

import ch.njol.skript.Skript;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

/**
* Represents a file in the globals folder
*/
abstract class GlobalFile {

protected final File file;

public GlobalFile(String name) {
String filePath = "globals/" + name + ".sk";
file = new File(Skript.getInstance().getDataFolder(), filePath);

if (!file.exists()) {
copyFile(filePath, file);
}
}

/**
* Copies a file from the Skript jar into the target file
*/
private static void copyFile(String sourcePath, File targetFile) {
try (InputStream stream = Skript.getInstance().getResource(sourcePath)) {
if (stream == null) {
Skript.error("The " + sourcePath + " file doesn't exist and couldn't be read from the jar file.");
return;
}
Files.copy(stream, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
Skript.exception(e, "Error while loading the " + sourcePath + " file from the jar file.");
}
}

}
68 changes: 68 additions & 0 deletions src/main/java/ch/njol/skript/lang/globals/GlobalOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ch.njol.skript.lang.globals;

import ch.njol.skript.Skript;
import ch.njol.skript.config.Config;
import ch.njol.skript.structures.StructOptions;
import ch.njol.util.StringUtils;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;

/**
* Represents the 'globals/options.sk' file
*/
public class GlobalOptions extends GlobalFile {

private static final Map<String, String> options = new HashMap<>();

public GlobalOptions() {
super("options");
}

/**
* Loads the 'globals/options.sk' file
*/
public void load() {
options.clear();
try {
Config config = new Config(file, true, false, ":");
config.getMainNode().convertToEntries(-1);
StructOptions.loadOptions(config.getMainNode(), "", options);

// for unit tests
if (Skript.testing()) {
options.put("GlobalOptionTest", "works!!!");
options.put("GlobalOptionOverrideTest", "shouldn't work!!!");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

/**
* @return An unmodifiable map of global options
*/
public static Map<String, String> getOptions() {
return Collections.unmodifiableMap(options);
}

/**
* Replaces global options in a string
*/
public static String replaceOptions(String string) {
if (options.isEmpty()) { // don't bother
return string;
}
return StringUtils.replaceAll(string, "\\{@(.+?)\\}", m -> {
String option = GlobalOptions.getOptions().get(m.group(1));
if (option == null) {
return m.group();
}
return Matcher.quoteReplacement(option);
});
}

}
6 changes: 3 additions & 3 deletions src/main/java/ch/njol/skript/structures/StructOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.globals.GlobalOptions;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.StringUtils;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.entry.EntryContainer;
import org.skriptlang.skript.lang.script.Script;
import org.skriptlang.skript.lang.script.ScriptData;
import org.skriptlang.skript.lang.structure.Structure;

Expand Down Expand Up @@ -63,7 +63,7 @@ public boolean init(Literal<?>[] args, int matchedPattern, ParseResult parseResu
return true;
}

private void loadOptions(SectionNode sectionNode, String prefix, Map<String, String> options) {
public static void loadOptions(SectionNode sectionNode, String prefix, Map<String, String> options) {
for (Node node : sectionNode) {
if (node instanceof EntryNode) {
options.put(prefix + node.getKey(), ((EntryNode) node).getValue());
Expand Down Expand Up @@ -107,7 +107,7 @@ public static final class OptionsData implements ScriptData {
@SuppressWarnings("ConstantConditions") // no way to get null as callback does not return null anywhere
public String replaceOptions(String string) {
return StringUtils.replaceAll(string, "\\{@(.+?)\\}", m -> {
String option = options.get(m.group(1));
String option = options.getOrDefault(m.group(1), GlobalOptions.getOptions().get(m.group(1)));
if (option == null) {
Skript.error("undefined option " + m.group());
return m.group();
Expand Down
13 changes: 13 additions & 0 deletions src/main/resources/globals/options.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
# This file lets you set options globally.
# Options here will be available across all scripts.
# Scripts can override a global option by creating an option with the same name as the global one.
# When you edit this file, you must first reload global-options and then reload all the script files that use global options.
#
# Example usage of this file:
#
# server name: SkriptLand # {@server name}
# join messages:
# first join: %player% joined for the first time! # {@join messages.first join}
# welcome back: Welcome back %player%! # {@join messages.welcome back}
#
5 changes: 4 additions & 1 deletion src/main/resources/lang/english.lang
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ skript command:
all: Reloads the config, all aliases configs and all scripts
config: Reloads the main config
aliases: Reloads the aliases configs (aliases-english.zip or plugin jar)
global-options: Reloads the global options
scripts: Reloads all scripts
<script>: Reloads a specific script or a folder of scripts
enable:
Expand Down Expand Up @@ -64,11 +65,13 @@ skript command:
error details: <light red> %s<reset>\n
other details: <white> %s<reset>\n
line details: <gold> Line: <gray>%s<reset>\n <reset>
global options: You must also reload any scripts that use global options for the changes to take effect.

config, aliases and scripts: the config, aliases and all scripts
config, aliases, global options and scripts: the config, aliases, global options and all scripts
scripts: all scripts
main config: the main configuration
aliases: the aliases
global options: the global options
script: <gold>%s<reset>
scripts in folder: all scripts in <gold>%s<reset>
x scripts in folder success: <gold>%2$s <lime>script¦¦s¦ in <gold>%1$s<reset>
Expand Down
6 changes: 6 additions & 0 deletions src/test/skript/tests/misc/global-options.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
options:
GlobalOptionOverrideTest: overridden!

test "global options":
assert "{@GlobalOptionTest}" is "works!!!" with "global option didn't work"
assert "{@GlobalOptionOverrideTest}" is "overridden!" with "overriding global option didn't work"