Skip to content

Commit

Permalink
Registration API Compatibility (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pikachu920 authored Jan 18, 2025
1 parent b62ccaa commit bcf6a4d
Show file tree
Hide file tree
Showing 31 changed files with 322 additions and 196 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11
java-version: 17
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
Expand All @@ -36,7 +36,7 @@ jobs:
path: extra-plugins/
merge-multiple: true
- name: Run tests
uses: SkriptLang/skript-test-action@v1.1
uses: SkriptLang/skript-test-action@v1.2
with:
test_script_directory: src/test/scripts
extra_plugins_directory: extra-plugins/
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ processResources {
}

compileJava {
sourceCompatibility = '11'
targetCompatibility = '11'
sourceCompatibility = '17'
targetCompatibility = '17'
options.encoding = 'UTF-8'
}

dependencies {
implementation 'org.spigotmc:spigot-api:1.13.2-R0.1-SNAPSHOT'
implementation 'com.github.SkriptLang:Skript:2.9.0'
implementation 'com.github.SkriptLang:Skript:2.10.0'
implementation 'org.eclipse.jdt:org.eclipse.jdt.annotation:1.1.0'
}
50 changes: 39 additions & 11 deletions src/main/java/com/btk5h/skriptmirror/ParseOrderWorkarounds.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@

import ch.njol.skript.Skript;
import ch.njol.skript.effects.EffReturn;
import ch.njol.util.Checker;
import org.skriptlang.reflect.syntax.CustomSyntaxStructure;
import org.skriptlang.reflect.syntax.condition.elements.CustomCondition;
import org.skriptlang.reflect.syntax.condition.elements.StructCustomCondition;
import org.skriptlang.reflect.syntax.effect.elements.CustomEffect;
import org.skriptlang.reflect.syntax.effect.elements.StructCustomEffect;
import org.skriptlang.reflect.syntax.expression.elements.CustomExpression;
import com.btk5h.skriptmirror.skript.EffExpressionStatement;
import com.btk5h.skriptmirror.skript.custom.ExprMatchedPattern;
import com.btk5h.skriptmirror.util.SkriptReflection;
import org.skriptlang.reflect.syntax.expression.elements.StructCustomExpression;
import org.skriptlang.skript.bukkit.registration.BukkitRegistryKeys;
import org.skriptlang.skript.registration.SyntaxInfo;
import org.skriptlang.skript.registration.SyntaxRegistry;
import org.skriptlang.skript.util.Priority;

import javax.naming.ServiceUnavailableException;
import java.util.Collection;
import java.util.Optional;
import java.util.function.Predicate;

/**
* Explicitly declares the relative parse orders of different statement types. Classes at the start of the list should
Expand All @@ -20,7 +29,11 @@
* This class should only be used to guarantee that skript-mirror's syntax is parsed before other addons. It cannot
* guarantee that another addon's syntax will be parsed before skript-reflect.
*/
@SuppressWarnings("UnstableApiUsage")
public class ParseOrderWorkarounds {

private static final Priority POSITION = Priority.before(SyntaxInfo.PATTERN_MATCHES_EVERYTHING);

private static final String[] PARSE_ORDER = {
EffExpressionStatement.class.getCanonicalName(),
CustomEffect.class.getCanonicalName(),
Expand All @@ -38,22 +51,37 @@ public class ParseOrderWorkarounds {

public static void reorderSyntax() {
for (String c : PARSE_ORDER) {
ensureLast(Skript.getStatements(), o -> o.getElementClass().getName().equals(c));
ensureLast(Skript.getConditions(), o -> o.getElementClass().getName().equals(c));
ensureLast(Skript.getEffects(), o -> o.getElementClass().toString().equals(c));
ensureLast(SkriptReflection.getExpressions(), o -> o.getElementClass().getName().equals(c));
ensureLast(Skript.getEvents(), o -> o.getElementClass().getName().equals(c));
ensureLast(SyntaxRegistry.CONDITION, o -> o.type().getName().equals(c));
ensureLast(SyntaxRegistry.EFFECT, o -> o.type().getName().equals(c));
ensureLast(SyntaxRegistry.EXPRESSION, o -> o.type().getName().equals(c));
ensureLast(BukkitRegistryKeys.EVENT, o -> o.type().getName().equals(c));
ensureLast(SyntaxRegistry.STRUCTURE, o -> o.type().getName().equals(c));
}
}

private static <E> void ensureLast(Collection<E> elements, Checker<E> checker) {
Optional<E> optionalE = elements.stream()
.filter(checker::check)
private static <T> void ensureLast(SyntaxRegistry.Key<? extends SyntaxInfo<? extends T>> elementKey, Predicate<SyntaxInfo<? extends T>> checker) {
SyntaxRegistry syntaxRegistry = SkriptMirror.getAddonInstance().syntaxRegistry();
Optional<? extends SyntaxInfo<? extends T>> optionalE = syntaxRegistry.syntaxes(elementKey).stream()
.filter(checker)
.findFirst();

optionalE.ifPresent(value -> {
elements.remove(value);
elements.add(value);
syntaxRegistry.unregister((SyntaxRegistry.Key) elementKey, value);
var newInfo = value.toBuilder().priority(POSITION).build();
syntaxRegistry.register((SyntaxRegistry.Key) elementKey, newInfo);

// need to update custom syntax references
CustomSyntaxStructure.DataTracker<?> tracker = null;
if (elementKey == (SyntaxRegistry.Key) SyntaxRegistry.EFFECT) {
tracker = StructCustomEffect.dataTracker;
} else if (elementKey == (SyntaxRegistry.Key) SyntaxRegistry.CONDITION) {
tracker = StructCustomCondition.dataTracker;
} else if (elementKey == (SyntaxRegistry.Key) SyntaxRegistry.EXPRESSION) {
tracker = StructCustomExpression.dataTracker;
}
if (tracker != null && tracker.getInfo() == value) {
tracker.setInfo(newInfo);
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.lang.reflect.Array;
import java.util.Iterator;
import java.util.function.Predicate;

public class ExprArrayAccess<T> implements Expression<T> {

Expand Down Expand Up @@ -106,12 +107,12 @@ public boolean isSingle() {
}

@Override
public boolean check(Event e, Checker<? super T> c, boolean negated) {
public boolean check(Event e, Predicate<? super T> c, boolean negated) {
return SimpleExpression.check(getAll(e), c, negated, getAnd());
}

@Override
public boolean check(Event e, Checker<? super T> c) {
public boolean check(Event e, Predicate<? super T> c) {
return SimpleExpression.check(getAll(e), c, false, getAnd());
}

Expand Down
22 changes: 13 additions & 9 deletions src/main/java/com/btk5h/skriptmirror/skript/ExprPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean;
import com.btk5h.skriptmirror.JavaType;
Expand All @@ -22,15 +23,19 @@ public class ExprPlugin extends SimplePropertyExpression<Object, ObjectWrapper>

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
super.init(exprs, matchedPattern, isDelayed, parseResult);
if (!super.init(exprs, matchedPattern, isDelayed, parseResult)) {
return false;
}

if (exprs[0] instanceof StructImport.ImportHandler) {
JavaType javaType = ((StructImport.ImportHandler) exprs[0]).getJavaType();
Class<?> clazz = javaType.getJavaClass();
if (getExpr() instanceof Literal<?> literal) {
Object literalValue = literal.getSingle();
if (literalValue instanceof JavaType javaType) {
Class<?> clazz = javaType.getJavaClass();

if (!JavaPlugin.class.isAssignableFrom(clazz) || JavaPlugin.class.equals(clazz)) {
Skript.error("The class " + clazz.getSimpleName() + " is not a plugin class");
return false;
if (!JavaPlugin.class.isAssignableFrom(clazz) || JavaPlugin.class.equals(clazz)) {
Skript.error("The class " + clazz.getSimpleName() + " is not a plugin class");
return false;
}
}
}

Expand All @@ -39,8 +44,7 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye

@Override
public ObjectWrapper convert(Object plugin) {
if (plugin instanceof String) {
String pluginName = (String) plugin;
if (plugin instanceof String pluginName) {
for (Plugin pluginInstance : Bukkit.getPluginManager().getPlugins()) {
if (pluginInstance.getName().equalsIgnoreCase(pluginName)) {
return ObjectWrapper.create(pluginInstance);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/btk5h/skriptmirror/skript/ExprSpread.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class ExprSpread<T> implements Expression<T> {
Expand Down Expand Up @@ -97,12 +98,12 @@ public boolean isSingle() {
}

@Override
public boolean check(Event e, Checker<? super T> c, boolean negated) {
public boolean check(Event e, Predicate<? super T> c, boolean negated) {
return SimpleExpression.check(getAll(e), c, negated, getAnd());
}

@Override
public boolean check(Event e, Checker<? super T> c) {
public boolean check(Event e, Predicate<? super T> c) {
return SimpleExpression.check(getAll(e), c, false, getAnd());
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/btk5h/skriptmirror/skript/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public JavaType parse(String s, ParseContext context) {

@Override
public boolean canParse(ParseContext context) {
// default context handled in StructImport$ImportHandler
return context != ParseContext.DEFAULT;
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.skriptlang.skript.lang.converter.Converters;

import java.util.Iterator;
import java.util.function.Predicate;

public class ExprExpression<T> implements Expression<T> {
static {
Expand Down Expand Up @@ -123,12 +124,12 @@ public boolean isSingle() {
}

@Override
public boolean check(Event e, Checker<? super T> c, boolean negated) {
public boolean check(Event e, Predicate<? super T> c, boolean negated) {
return SimpleExpression.check(getAll(e), c, negated, getAnd());
}

@Override
public boolean check(Event e, Checker<? super T> c) {
public boolean check(Event e, Predicate<? super T> c) {
return SimpleExpression.check(getAll(e), c, false, getAnd());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class ExprRawExpression extends SimpleExpression<Expression> {
static {
Skript.registerExpression(ExprRawExpression.class, Expression.class, ExpressionType.COMBINED,
"[the] raw %objects%");
"[the] raw [expression] %objects%");
}

private Expression<?> expr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionList;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.*;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.util.Utils;
Expand Down Expand Up @@ -49,6 +46,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -165,10 +163,11 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
}

if (staticDescriptor.getJavaClass() == null
&& rawTarget instanceof StructImport.ImportHandler) {
staticDescriptor = staticDescriptor.orDefaultClass(
((StructImport.ImportHandler) rawTarget).getJavaType().getJavaClass()
);
&& rawTarget instanceof Literal<?> literal) {
Object rawTargetValue = literal.getSingle();
if (rawTargetValue instanceof JavaType) {
staticDescriptor = staticDescriptor.orDefaultClass(((JavaType) rawTargetValue).getJavaClass());
}
}

if (staticDescriptor.getParameterTypes() != null && type.equals(CallType.FIELD)) {
Expand Down Expand Up @@ -253,12 +252,12 @@ public boolean isSingle() {
}

@Override
public boolean check(Event e, Checker<? super T> c, boolean negated) {
public boolean check(Event e, Predicate<? super T> c, boolean negated) {
return SimpleExpression.check(getAll(e), c, negated, getAnd());
}

@Override
public boolean check(Event e, Checker<? super T> c) {
public boolean check(Event e, Predicate<? super T> c) {
return SimpleExpression.check(getAll(e), c, false, getAnd());
}

Expand Down
Loading

0 comments on commit bcf6a4d

Please sign in to comment.