Skip to content
Merged
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
3 changes: 2 additions & 1 deletion src/main/java/io/github/syst3ms/skriptparser/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.github.syst3ms.skriptparser.structures.functions.StructFunction;
import org.jetbrains.annotations.Nullable;

import java.util.Comparator;
import java.util.List;

/**
Expand All @@ -30,7 +31,7 @@ public void finishedLoading(@Nullable String scriptName) {
} else {
triggers = TriggerMap.getTriggersByScript(scriptName).values().stream().flatMap(List::stream).toList();
}
triggers.forEach(trigger -> {
triggers.stream().sorted(Comparator.comparing(trigger -> trigger.getEvent().getLoadingPriority())).forEach(trigger -> {
if (trigger.getEvent() instanceof StartOnLoadEvent event) {
event.onInitialLoad(trigger);
} else if (trigger.getEvent() instanceof StructFunction func) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
* A map that holds triggers based on a script and {@link TriggerContext}.
*/
public class TriggerMap {

private static final Map<String, Map<Class<? extends TriggerContext>, List<Trigger>>> TRIGGERS = new HashMap<>();
private static final Map<String, Map<Class<? extends TriggerContext>, List<Trigger>>> TRIGGERS = new TreeMap<>();

/**
* Add a trigger to the map.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import io.github.syst3ms.skriptparser.registration.SyntaxManager;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Array;
import java.util.List;
import java.util.Objects;

/**
Expand Down Expand Up @@ -58,7 +60,8 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, ParseContex
@SuppressWarnings("unchecked")
@Override
public T[] getValues(TriggerContext ctx) {
return (T[]) owner.stream(ctx).map(this::getProperty).filter(Objects::nonNull).toArray(Object[]::new);
List<T> list = this.owner.stream(ctx).map( this::getProperty).filter(Objects::nonNull).toList();
return list.toArray((T[]) Array.newInstance(getReturnType(), list.size()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public Float deserialize(Gson gson, JsonElement element) {
return gson.fromJson(element, Float.class);
}
})
.toStringFunction(f -> String.format("%.2f", f))
.register();

registration.newType(Double.class, "double", "double@s")
Expand Down Expand Up @@ -174,6 +175,7 @@ public Double deserialize(Gson gson, JsonElement element) {
return gson.fromJson(element, Double.class);
}
})
.toStringFunction(d -> String.format("%.2f", d))
.register();

registration.newType(Number.class, "number", "number@s")
Expand Down Expand Up @@ -221,6 +223,13 @@ public Number deserialize(Gson gson, JsonElement element) {
return gson.fromJson(element, Double.class);
}
})
.toStringFunction(n -> {
if (n instanceof Integer || n instanceof Long) {
return n.toString();
} else {
return String.format("%.2f", n.doubleValue());
}
})
.register();

registration.newType(String.class, "string", "string@s")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Functions {

private static final Map<String, List<Function<?>>> functionsMap = new HashMap<>();

private static final String GLOBAL_FUNCTIONS_NAME = "global_functions_dont_change";
private static final String JAVA_FUNCTION_NAME = "java_functions_dont_change";
static final String FUNCTION_NAME_REGEX = "^[a-zA-Z0-9_]*";
private static final Pattern FUNCTION_NAME_PATTERN = Pattern.compile(FUNCTION_NAME_REGEX);
static final String FUNCTION_CALL_PATTERN = "<(" + Functions.FUNCTION_NAME_REGEX + ")\\((.*)\\)>";
Expand All @@ -28,8 +28,12 @@ public static List<Function<?>> getFunctions(String scriptName) {
return functionsMap.getOrDefault(scriptName, List.of());
}

public static List<Function<?>> getGlobalFunctions() {
return functionsMap.getOrDefault(GLOBAL_FUNCTIONS_NAME, List.of());
public static List<Function<?>> getAllFunctions() {
return functionsMap.values().stream().flatMap(List::stream).toList();
}

public static List<Function<?>> getJavaFunctions() {
return functionsMap.getOrDefault(JAVA_FUNCTION_NAME, List.of());
}

static void preRegisterFunction(ScriptFunction<?> function) {
Expand All @@ -53,7 +57,7 @@ public static void removeFunctions(String scriptName) {
}

public static void registerFunction(JavaFunction<?> function) {
functionsMap.computeIfAbsent(GLOBAL_FUNCTIONS_NAME, k -> new ArrayList<>()).add(function);
functionsMap.computeIfAbsent(JAVA_FUNCTION_NAME, k -> new ArrayList<>()).add(function);
}

public static boolean isValidFunction(ScriptFunction<?> function, SkriptLogger logger) {
Expand Down Expand Up @@ -90,16 +94,17 @@ public static boolean isValidFunction(ScriptFunction<?> function, SkriptLogger l
}

public static Optional<Function<?>> getFunctionByName(String name, String scriptName) {
// Find a global function
for (Function<?> function : functionsMap.computeIfAbsent(GLOBAL_FUNCTIONS_NAME, k -> new ArrayList<>())) {
// Find a JavaFunction
for (Function<?> function : functionsMap.computeIfAbsent(JAVA_FUNCTION_NAME, k -> new ArrayList<>())) {
if (function.getName().equals(name)) {
return Optional.of(function);
}
}

// Find a function in a script file
if (scriptName.endsWith(".sk")) scriptName = scriptName.substring(0, scriptName.length() - 3);

for (Function<?> registeredFunction : functionsMap.computeIfAbsent(scriptName, k -> new ArrayList<>())) {
for (Function<?> registeredFunction : getAllFunctions()) {
if (!registeredFunction.getName().equals(name))
continue; // we don't care then!!!! goodbye continue to the next one
if (registeredFunction instanceof ScriptFunction<?> registeredScriptFunction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public static <F, T> T[] convertUnsafe(F[] from, Class<?> to, Function<? super F
* @return the converted values
*/
public static <F, T> T[] convert(F[] from, Class<T> to, Function<? super F, Optional<? extends T>> converter) {
@SuppressWarnings("unchecked")
if (from == null) return (T[])Array.newInstance(to, 0);
var ts = (T[]) Array.newInstance(to, from.length);
var j = 0;
for (var f : from) {
Expand All @@ -314,4 +314,4 @@ public static <F, T> T[] convert(F[] from, Class<T> to, Function<? super F, Opti
ts = Arrays.copyOf(ts, j);
return ts;
}
}
}