Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/dev/feature' into dev/beacon-e…
Browse files Browse the repository at this point in the history
…vents

# Conflicts:
#	src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java
#	src/main/java/ch/njol/skript/events/SimpleEvents.java
  • Loading branch information
TheAbsolutionism committed Oct 13, 2024
2 parents 4ce0465 + 790c779 commit 5e1c6b3
Show file tree
Hide file tree
Showing 157 changed files with 7,636 additions and 1,770 deletions.
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ dependencies {
shadow group: 'net.kyori', name: 'adventure-text-serializer-bungeecord', version: '4.3.2'

implementation group: 'io.papermc.paper', name: 'paper-api', version: '1.21-R0.1-SNAPSHOT'
implementation group: 'org.eclipse.jdt', name: 'org.eclipse.jdt.annotation', version: '2.2.700'

// Comes from Minecraft including Guava or Gson
implementation group: 'com.google.code.findbugs', name: 'findbugs', version: '3.0.1'

// bundled with Minecraft 1.19.4+ for display entity transforms
implementation group: 'org.joml', name: 'joml', version: '1.10.5'

// Plugin hook libraries
implementation group: 'com.sk89q.worldguard', name: 'worldguard-legacy', version: '7.0.0-SNAPSHOT'
implementation group: 'net.milkbowl.vault', name: 'Vault', version: '1.7.3', {
exclude group: 'org.bstats', module: 'bstats-bukkit'
Expand Down
160 changes: 146 additions & 14 deletions src/main/java/ch/njol/skript/ScriptLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.util.event.EventRegistry;
import org.skriptlang.skript.lang.script.Script;
import org.skriptlang.skript.lang.script.ScriptWarning;
import org.skriptlang.skript.lang.structure.Structure;

import java.io.File;
Expand All @@ -58,6 +60,7 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
Expand Down Expand Up @@ -488,6 +491,9 @@ private static CompletableFuture<ScriptInfo> loadScripts(List<Config> configs, O
if (configs.isEmpty()) // Nothing to load
return CompletableFuture.completedFuture(new ScriptInfo());

eventRegistry().events(ScriptPreInitEvent.class)
.forEach(event -> event.onPreInit(configs));
//noinspection deprecation - we still need to call it
Bukkit.getPluginManager().callEvent(new PreScriptLoadEvent(configs));

ScriptInfo scriptInfo = new ScriptInfo();
Expand Down Expand Up @@ -605,6 +611,20 @@ private static CompletableFuture<ScriptInfo> loadScripts(List<Config> configs, O
});
parser.setInactive();

// trigger events
scripts.forEach(loadingInfo -> {
Script script = loadingInfo.script;

parser.setActive(script);
parser.setNode(script.getConfig().getMainNode());

ScriptLoader.eventRegistry().events(ScriptLoadEvent.class)
.forEach(event -> event.onLoad(parser, script));
script.eventRegistry().events(ScriptLoadEvent.class)
.forEach(event -> event.onLoad(parser, script));
});
parser.setInactive();

return scriptInfo;
} catch (Exception e) {
// Something went wrong, we need to make sure the exception is printed
Expand Down Expand Up @@ -700,10 +720,13 @@ private static LoadingScriptInfo loadScript(Config config) {
assert file != null;
File disabledFile = new File(file.getParentFile(), DISABLED_SCRIPT_PREFIX + file.getName());
disabledScripts.remove(disabledFile);

// Add to loaded files to use for future reloads
loadedScripts.add(script);


ScriptLoader.eventRegistry().events(ScriptInitEvent.class)
.forEach(event -> event.onInit(script));

return null;
};
if (isAsync()) { // Need to delegate to main thread
Expand Down Expand Up @@ -848,6 +871,13 @@ public static ScriptInfo unloadScripts(Set<Script> scripts) {
// initial unload stage
for (Script script : scripts) {
parser.setActive(script);

// trigger unload event before beginning
eventRegistry().events(ScriptUnloadEvent.class)
.forEach(event -> event.onUnload(parser, script));
script.eventRegistry().events(ScriptUnloadEvent.class)
.forEach(event -> event.onUnload(parser, script));

for (Structure structure : script.getStructures())
structure.unload();
}
Expand Down Expand Up @@ -949,9 +979,10 @@ public static ArrayList<TriggerItem> loadItems(SectionNode node) {

if (Skript.debug())
parser.setIndentation(parser.getIndentation() + " ");

ArrayList<TriggerItem> items = new ArrayList<>();

boolean executionStops = false;
for (Node subNode : node) {
parser.setNode(subNode);

Expand All @@ -962,10 +993,11 @@ public static ArrayList<TriggerItem> loadItems(SectionNode node) {
if (!SkriptParser.validateLine(expr))
continue;

TriggerItem item;
if (subNode instanceof SimpleNode) {
long start = System.currentTimeMillis();
Statement stmt = Statement.parse(expr, items, "Can't understand this condition/effect: " + expr);
if (stmt == null)
item = Statement.parse(expr, items, "Can't understand this condition/effect: " + expr);
if (item == null)
continue;
long requiredTime = SkriptConfig.longParseTimeWarningThreshold.value().getMilliSeconds();
if (requiredTime > 0) {
Expand All @@ -978,34 +1010,44 @@ public static ArrayList<TriggerItem> loadItems(SectionNode node) {
}

if (Skript.debug() || subNode.debug())
Skript.debug(SkriptColor.replaceColorChar(parser.getIndentation() + stmt.toString(null, true)));
Skript.debug(SkriptColor.replaceColorChar(parser.getIndentation() + item.toString(null, true)));

items.add(stmt);
items.add(item);
} else if (subNode instanceof SectionNode) {
TypeHints.enterScope(); // Begin conditional type hints

Section section = Section.parse(expr, "Can't understand this section: " + expr, (SectionNode) subNode, items);
if (section == null)
item = Section.parse(expr, "Can't understand this section: " + expr, (SectionNode) subNode, items);
if (item == null)
continue;

if (Skript.debug() || subNode.debug())
Skript.debug(SkriptColor.replaceColorChar(parser.getIndentation() + section.toString(null, true)));
Skript.debug(SkriptColor.replaceColorChar(parser.getIndentation() + item.toString(null, true)));

items.add(section);
items.add(item);

// Destroy these conditional type hints
TypeHints.exitScope();
} else {
continue;
}

if (executionStops
&& !SkriptConfig.disableUnreachableCodeWarnings.value()
&& parser.isActive()
&& !parser.getCurrentScript().suppressesWarning(ScriptWarning.UNREACHABLE_CODE)) {
Skript.warning("Unreachable code. The previous statement stops further execution.");
}
executionStops = item.executionIntent() != null;
}

for (int i = 0; i < items.size() - 1; i++)
items.get(i).setNext(items.get(i + 1));

parser.setNode(node);

if (Skript.debug())
parser.setIndentation(parser.getIndentation().substring(0, parser.getIndentation().length() - 4));

return items;
}

Expand Down Expand Up @@ -1043,6 +1085,96 @@ public static FileFilter getDisabledScriptsFilter() {
return disabledScriptFilter;
}

/*
* Global Script Event API
*/

// ScriptLoader Events

/**
* Used for listening to events involving a ScriptLoader.
* @see #eventRegistry()
*/
public interface LoaderEvent extends org.skriptlang.skript.util.event.Event { }

/**
* Called when {@link ScriptLoader} is preparing to load {@link Config}s into {@link Script}s.
* @see #loadScripts(File, OpenCloseable)
* @see #loadScripts(Set, OpenCloseable)
*/
@FunctionalInterface
public interface ScriptPreInitEvent extends LoaderEvent {

/**
* The method that is called when this event triggers.
* Modifications to the given collection will affect what is loaded.
* @param configs The Configs to be loaded.
*/
void onPreInit(Collection<Config> configs);

}

/**
* Called when a {@link Script} is created and preloaded in the {@link ScriptLoader}.
* The initializing script may contain {@link Structure}s that are not fully loaded.
* @see #loadScripts(File, OpenCloseable)
* @see #loadScripts(Set, OpenCloseable)
*/
@FunctionalInterface
public interface ScriptInitEvent extends LoaderEvent {

/**
* The method that is called when this event triggers.
* @param script The Script being initialized.
*/
void onInit(Script script);

}

/**
* Called when a {@link Script} is loaded in the {@link ScriptLoader}.
* This event will trigger <b>after</b> the script is completely loaded ({@link Structure} initialization finished).
* @see #loadScripts(File, OpenCloseable)
* @see #loadScripts(Set, OpenCloseable)
*/
@FunctionalInterface
public interface ScriptLoadEvent extends LoaderEvent, Script.Event {

/**
* The method that is called when this event triggers.
* @param parser The ParserInstance handling the loading of <code>script</code>.
* @param script The Script being loaded.
*/
void onLoad(ParserInstance parser, Script script);

}

/**
* Called when a {@link Script} is unloaded in the {@link ScriptLoader}.
* This event will trigger <b>before</b> the script is unloaded.
* @see #unloadScript(Script)
*/
@FunctionalInterface
public interface ScriptUnloadEvent extends LoaderEvent, Script.Event {

/**
* The method that is called when this event triggers.
* @param parser The ParserInstance handling the unloading of <code>script</code>.
* @param script The Script being unloaded.
*/
void onUnload(ParserInstance parser, Script script);

}

private static final EventRegistry<LoaderEvent> eventRegistry = new EventRegistry<>();

/**
* @return An EventRegistry for the ScriptLoader's events.
*/
public static EventRegistry<LoaderEvent> eventRegistry() {
return eventRegistry;
}

/*
* Deprecated stuff
*
Expand Down
Loading

0 comments on commit 5e1c6b3

Please sign in to comment.