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

Fix the legendary compare issue with two same literals #5503

Merged
merged 34 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
541ab17
Implement a fix for the famous literal comparing issue
TheLimeGlass Mar 9, 2023
1d10b81
Clean up and get ready for pull request
TheLimeGlass Mar 10, 2023
d95b00a
Clean up and get ready for pull request
TheLimeGlass Mar 10, 2023
009f5c2
Add more regression tests for more issues
TheLimeGlass Mar 10, 2023
efdcb0f
Add more regression tests for more issues
TheLimeGlass Mar 10, 2023
ebdc88d
Add more regression tests for more issues
TheLimeGlass Mar 10, 2023
ce35143
Merge branch 'master' into fix/convert-compare
TheLimeGlass Mar 10, 2023
a2f535f
override wrong .sk test file
TheLimeGlass Mar 10, 2023
a4c6fad
Merge branch 'master' into fix/convert-compare
Moderocky Apr 14, 2023
f679728
Update src/main/java/ch/njol/skript/conditions/CondCompare.java
TheLimeGlass Apr 14, 2023
67d701d
Merge branch 'master' into fix/convert-compare
TheLimeGlass Apr 17, 2023
748d325
Update src/test/skript/tests/regressions/2711-item-compares.sk
TheLimeGlass Apr 25, 2023
76340ce
Update src/test/skript/tests/regressions/4769-fire-visualeffect.sk
TheLimeGlass Apr 25, 2023
71f3da0
Added tests wern't properly setting back block
TheLimeGlass Apr 26, 2023
5f520c6
Added tests wern't properly setting back block
TheLimeGlass Apr 26, 2023
9c72c72
Apply changes
TheLimeGlass Apr 26, 2023
b9c9c93
Update src/test/skript/tests/regressions/pull-5566-block iterator bei…
TheLimeGlass May 20, 2023
ad841ce
Update src/test/skript/tests/regressions/3303-item durability.sk
TheLimeGlass May 20, 2023
f9537d7
Update src/main/java/ch/njol/skript/test/runner/EvtTestCase.java
TheLimeGlass May 20, 2023
cf77032
Update src/main/java/ch/njol/skript/test/runner/EvtTestCase.java
TheLimeGlass May 20, 2023
9bab3aa
Apply changes
TheLimeGlass May 22, 2023
a390a75
Merge branch 'master' into fix/convert-compare
TheLimeGlass May 22, 2023
e6e0dbc
Update src/main/java/ch/njol/skript/conditions/CondCompare.java
TheLimeGlass May 26, 2023
976ed97
Apply changes
TheLimeGlass May 26, 2023
ea6ce6c
Apply changes
TheLimeGlass May 26, 2023
e8a7674
Merge branch 'master' into fix/convert-compare
TheLimeGlass May 30, 2023
7482db7
Change compare gathering
TheLimeGlass May 30, 2023
2bf7325
Merge branch 'fix/convert-compare' of https://github.com/SkriptLang/S…
TheLimeGlass May 30, 2023
e4fd0fa
Grammar in test
TheLimeGlass May 30, 2023
bc8e7ea
Revert block data version changes
TheLimeGlass May 30, 2023
084da81
Lower block data test to 1.14.4
TheLimeGlass May 30, 2023
81241ce
Merge branch 'master' into fix/convert-compare
TheLimeGlass Jun 2, 2023
c0d4068
Change separator
TheLimeGlass Jun 2, 2023
0464f8b
Remove test line from test
TheLimeGlass Jun 2, 2023
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
142 changes: 87 additions & 55 deletions src/main/java/ch/njol/skript/conditions/CondCompare.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;

import org.skriptlang.skript.lang.comparator.Comparator;
import org.skriptlang.skript.lang.comparator.Relation;
import org.skriptlang.skript.lang.converter.ConverterInfo;
import org.skriptlang.skript.lang.converter.Converters;

import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionList;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.UnparsedLiteral;
Expand All @@ -39,15 +44,13 @@
import ch.njol.skript.log.RetainingLogHandler;
import ch.njol.skript.log.SkriptLogger;
import ch.njol.skript.registrations.Classes;

import org.skriptlang.skript.lang.comparator.Comparators;
import ch.njol.skript.util.Patterns;
import ch.njol.skript.util.Utils;
import ch.njol.util.Checker;
import ch.njol.util.Kleenean;

/**
* @author Peter Güttinger
*/
@Name("Comparison")
@Description({"A very general condition, it simply compares two values. Usually you can only compare for equality (e.g. block is/isn't of <type>), " +
"but some values can also be compared using greater than/less than. In that case you can also test for whether an object is between two others.",
Expand Down Expand Up @@ -93,19 +96,18 @@ public class CondCompare extends Condition {
Skript.registerCondition(CondCompare.class, patterns.getPatterns());
}

@SuppressWarnings("null")
private Expression<?> first;
@SuppressWarnings("null")
private Expression<?> second;

@Nullable
private Expression<?> third;
@SuppressWarnings("null")

private Relation relation;
@SuppressWarnings("rawtypes")

@Nullable
@SuppressWarnings("rawtypes")
private Comparator comp;
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved

@SuppressWarnings("null")
@Override
public boolean init(final Expression<?>[] vars, final int matchedPattern, final Kleenean isDelayed, final ParseResult parser) {
first = vars[0];
Expand Down Expand Up @@ -160,57 +162,56 @@ public static String f(final Expression<?> e) {

@SuppressWarnings({"unchecked"})
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
private boolean init(String expr) {
final RetainingLogHandler log = SkriptLogger.startRetainingLog();
RetainingLogHandler log = SkriptLogger.startRetainingLog();
Expression<?> third = this.third;
try {
if (first.getReturnType() == Object.class) {
final Expression<?> e = first.getConvertedExpression(Object.class);
if (e == null) {
Expression<?> expression = null;
if (first instanceof UnparsedLiteral)
expression = attemptReconstruct((UnparsedLiteral) first, second);
if (expression == null)
expression = first.getConvertedExpression(Object.class);
if (expression == null) {
log.printErrors();
return false;
}
first = e;
first = expression;
}
if (second.getReturnType() == Object.class) {
final Expression<?> e = second.getConvertedExpression(Object.class);
if (e == null) {
Expression<?> expression = null;
if (second instanceof UnparsedLiteral)
expression = attemptReconstruct((UnparsedLiteral) second, first);
if (expression == null)
expression = second.getConvertedExpression(Object.class);
if (expression == null) {
log.printErrors();
return false;
}
second = e;
second = expression;
}
if (third != null && third.getReturnType() == Object.class) {
final Expression<?> e = third.getConvertedExpression(Object.class);
if (e == null) {
Expression<?> expression = null;
if (third instanceof UnparsedLiteral)
expression = attemptReconstruct((UnparsedLiteral) third, first);
if (expression == null)
expression = third.getConvertedExpression(Object.class);
if (expression == null) {
log.printErrors();
return false;
}
this.third = third = e;
this.third = third = expression;
}
log.printLog();
} finally {
log.stop();
}
final Class<?> f = first.getReturnType(), s = third == null ? second.getReturnType() : Utils.getSuperType(second.getReturnType(), third.getReturnType());
if (f == Object.class || s == Object.class)
Class<?> firstReturnType = first.getReturnType();
Class<?> secondReturnType = third == null ? second.getReturnType() : Utils.getSuperType(second.getReturnType(), third.getReturnType());
if (firstReturnType == Object.class || secondReturnType == Object.class)
return true;
/*
* https://github.com/Mirreski/Skript/issues/10
*
if(Entity.class.isAssignableFrom(s)){
String[] split = expr.split(" ");
System.out.println(expr);
if(!split[split.length - 1].equalsIgnoreCase("player") && EntityData.parseWithoutIndefiniteArticle(split[split.length - 1]) != null){
comp = Comparators.getComparator(f, EntityData.class);
second = SkriptParser.parseLiteral(split[split.length - 1], EntityData.class, ParseContext.DEFAULT);
}else
comp = Comparators.getComparator(f, s);

}
*///else

comp = Comparators.getComparator(f, s);


comp = Comparators.getComparator(firstReturnType, secondReturnType);

if (comp == null) { // Try to re-parse with more context
/*
* SkriptParser sees that CondCompare takes two objects. Most of the time,
Expand All @@ -224,23 +225,23 @@ private boolean init(String expr) {
* Some damage types not working (issue #2184) would be a good example
* of issues that SkriptParser's lack of context can cause.
*/
SimpleLiteral<?> reparsedSecond = reparseLiteral(first.getReturnType(), second);
SimpleLiteral<?> reparsedSecond = reparseLiteral(firstReturnType, second);
if (reparsedSecond != null) {
second = reparsedSecond;
comp = Comparators.getComparator(f, second.getReturnType());
comp = Comparators.getComparator(firstReturnType, second.getReturnType());
} else {
SimpleLiteral<?> reparsedFirst = reparseLiteral(second.getReturnType(), first);
if (reparsedFirst != null) {
first = reparsedFirst;
comp = Comparators.getComparator(first.getReturnType(), s);
comp = Comparators.getComparator(first.getReturnType(), secondReturnType);
}
}

}

return comp != null;
}

/**
* Attempts to parse given expression again as a literal of given type.
* This will only work if the expression is a literal and its unparsed
Expand All @@ -251,22 +252,53 @@ private boolean init(String expr) {
* @return A literal value, or null if parsing failed.
*/
@Nullable
private <T> SimpleLiteral<T> reparseLiteral(Class<T> type, Expression<?> expr) {
if (expr instanceof SimpleLiteral) { // Only works for simple literals
Expression<?> source = expr.getSource();

// Try to get access to unparsed content of it
if (source instanceof UnparsedLiteral) {
String unparsed = ((UnparsedLiteral) source).getData();
T data = Classes.parse(unparsed, type, ParseContext.DEFAULT);
if (data != null) { // Success, let's make a literal of it
return new SimpleLiteral<>(data, false, new UnparsedLiteral(unparsed));
}
private <T> SimpleLiteral<T> reparseLiteral(Class<T> type, Expression<?> expression) {
Expression<?> source = expression;
if (expression instanceof SimpleLiteral) // Only works for simple literals
source = expression.getSource();

// Try to get access to unparsed content of it
if (source instanceof UnparsedLiteral) {
String unparsed = ((UnparsedLiteral) source).getData();
T data = Classes.parse(unparsed, type, ParseContext.DEFAULT);
if (data != null) { // Success, let's make a literal of it
return new SimpleLiteral<>(data, false, new UnparsedLiteral(unparsed));
}
}
return null; // Context-sensitive parsing failed; can't really help it
}


/**
* Attempts to transform an UnparsedLiteral into a type that is comparable to another.
* For example 'fire' will be VisualEffect without this, but if the user attempts to compare 'fire'
* with a block. This method will see if 'fire' can be compared to the block, and it will find ItemType.
* Essentially solving something a human sees as comparable, but Skript doesn't understand.
*
* @param one The UnparsedLiteral expression to attempt to reconstruct.
* @param two any expression to grab the return type from.
* @return The newly formed Literal, will be SimpleLiteral in most cases.
*/
@SuppressWarnings("unchecked")
private Literal<?> attemptReconstruct(UnparsedLiteral one, Expression<?> two) {
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
Expression<?> expression = null;
// Must handle numbers first.
expression = one.getConvertedExpression(Number.class);
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
if (expression == null) {
for (ConverterInfo<?, ?> converter : Converters.getConverterInfos()) {
// We're comparing the 'two' expression, so we need the 'from' for comparing.
if (!converter.getFrom().isAssignableFrom(two.getReturnType()))
continue;
// Must be comparable if we're going to attempt to convert this UnparsedLiteral. Otherwise why attempt something that won't work.
if (Comparators.getComparator(two.getReturnType(), converter.getTo()) == null)
continue;
expression = reparseLiteral(converter.getTo(), one);
}
}
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
if (expression == null)
expression = one.getConvertedExpression(two.getReturnType());
return (Literal<?>) expression;
}

/*
* # := condition (e.g. is, is less than, contains, is enchanted with, has permission, etc.)
* !# := not #
Expand Down Expand Up @@ -302,7 +334,7 @@ private <T> SimpleLiteral<T> reparseLiteral(Class<T> type, Expression<?> expr) {
* neither a nor b # x or y === a !# x or y && b !# x or y // nor = and
*/
@Override
@SuppressWarnings({"null", "unchecked"})
@SuppressWarnings("unchecked")
public boolean check(final Event e) {
final Expression<?> third = this.third;
return first.check(e, (Checker<Object>) o1 ->
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ch/njol/skript/registrations/Classes.java
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,9 @@ public static <T> T parse(final String s, final Class<T> c, final ParseContext c
continue;
if (c.isAssignableFrom(conv.getTo())) {
log.clear();
final Object o = parseSimple(s, conv.getFrom(), context);
if (o != null) {
t = (T) ((Converter) conv.getConverter()).convert(o);
Object object = parseSimple(s, conv.getFrom(), context);
if (object != null) {
t = (T) ((Converter) conv.getConverter()).convert(object);
if (t != null) {
log.printLog();
return t;
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/ch/njol/skript/test/runner/EffAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.skriptlang.skript.lang.script.Script;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
Expand All @@ -46,6 +47,7 @@ public class EffAssert extends Effect {

@Nullable
private Condition condition;
private Script script;

private Expression<String> errorMsg;
private boolean shouldFail;
Expand All @@ -56,6 +58,7 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
String conditionString = parseResult.regexes.get(0).group();
errorMsg = (Expression<String>) exprs[0];
shouldFail = parseResult.mark != 0;
script = getParser().getCurrentScript();

ParseLogHandler logHandler = SkriptLogger.startParseLogHandler();
try {
Expand Down Expand Up @@ -91,7 +94,7 @@ public TriggerItem walk(Event event) {
if (SkriptJUnitTest.getCurrentJUnitTest() != null) {
TestTracker.junitTestFailed(SkriptJUnitTest.getCurrentJUnitTest(), message);
} else {
TestTracker.testFailed(message);
TestTracker.testFailed(message, script);
}
return null;
}
Expand Down
56 changes: 36 additions & 20 deletions src/main/java/ch/njol/skript/test/runner/EvtTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package ch.njol.skript.test.runner;

import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

Expand All @@ -27,25 +29,39 @@
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptEvent;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.registrations.EventValues;
import ch.njol.skript.util.Getter;

public class EvtTestCase extends SkriptEvent {

static {
if (TestMode.ENABLED)
if (TestMode.ENABLED) {
Skript.registerEvent("Test Case", EvtTestCase.class, SkriptTestEvent.class, "test %string% [when <.+>]")
.description("Contents represent one test case.")
.examples("")
.since("2.5");
.description("Contents represent one test case.")
.examples("")
.since("2.5");
EventValues.registerEventValue(SkriptTestEvent.class, Block.class, new Getter<Block, SkriptTestEvent>() {
@Override
public @Nullable Block get(SkriptTestEvent ignored) {
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
return SkriptJUnitTest.getBlock();
}
}, EventValues.TIME_NOW);
EventValues.registerEventValue(SkriptTestEvent.class, Location.class, new Getter<Location, SkriptTestEvent>() {
@Override
public @Nullable Location get(SkriptTestEvent ignored) {
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
return SkriptJUnitTest.getTestLocation();
}
}, EventValues.TIME_NOW);
}
}

@SuppressWarnings("null")

private Expression<String> name;

@Nullable
private Condition condition;

@SuppressWarnings({"null", "unchecked"})

@Override
@SuppressWarnings("unchecked")
public boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.ParseResult parseResult) {
name = (Expression<String>) args[0];
if (!parseResult.regexes.isEmpty()) { // Do not parse or run unless condition is met
Expand All @@ -54,27 +70,27 @@ public boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.ParseRes
}
return true;
}

@Override
public boolean check(Event e) {
String n = name.getSingle(e);
if (n == null) {
public boolean check(Event event) {
String n = name.getSingle(event);
if (n == null)
return false;
}
Skript.info("Running test case " + n);
TestTracker.testStarted(n);
return true;
}

@Override
public boolean shouldLoadEvent() {
return condition != null ? condition.check(new SkriptTestEvent()) : true;
}

@Override
public String toString(@Nullable Event e, boolean debug) {
if (e != null)
return "test " + name.getSingle(e);
public String toString(@Nullable Event event, boolean debug) {
if (event != null)
return "test " + name.getSingle(event);
return "test case";
}

}
Loading