Skip to content

Commit 3b13b78

Browse files
Property Implementation Fixes (SkriptLang#8354)
1 parent ca3fceb commit 3b13b78

3 files changed

Lines changed: 35 additions & 13 deletions

File tree

src/main/java/org/skriptlang/skript/common/properties/conditions/PropCondContains.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean is
150150
var convertedNeedles = needles.getConvertedExpression((Class[]) elementTypeSet);
151151
if (convertedNeedles == null) {
152152
// attempt direct contains
153-
return initDirect("'" + tempHaystack + "' cannot contain " + Classes.toString(Arrays.stream(needles.possibleReturnTypes()).map(Classes::getSuperClassInfo).toArray(), false));
153+
return initDirect("'" + tempHaystack + "' cannot contain " + Classes.toString(Arrays.stream(needles.possibleReturnTypes()).map(Classes::getSuperClassInfo).toArray(), false));
154154
}
155+
needles = convertedNeedles;
155156
return LiteralUtils.canInitSafely(haystack, needles);
156157
} else {
157158
return initDirect(null);

src/main/java/org/skriptlang/skript/lang/properties/PropertyBaseExpression.java

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.jetbrains.annotations.NotNull;
1515
import org.jetbrains.annotations.Nullable;
1616
import org.skriptlang.skript.common.properties.expressions.PropExprName;
17+
import org.skriptlang.skript.lang.converter.Converters;
1718
import org.skriptlang.skript.lang.properties.Property.PropertyInfo;
1819
import org.skriptlang.skript.lang.properties.handlers.base.ExpressionPropertyHandler;
1920

@@ -208,20 +209,38 @@ public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
208209
}
209210

210211
// check if delta matches any of the allowed types
211-
for (Class<?> allowedType : allowedTypes) {
212-
// array type, compare to delta
213-
// single type, compare to delta[0]
214-
if ((allowedType.isArray() && allowedType.isInstance(delta))
215-
|| (delta != null && allowedType.isInstance(delta[0]))) {
216-
// if the propertyHaver is allowed, change
217-
@SuppressWarnings("unchecked")
218-
var handler = (ExpressionPropertyHandler<Object, ?>) propertyInfo.handler();
219-
handler.change(propertyHaver, delta, mode);
220-
return propertyHaver;
212+
assert delta != null;
213+
Object[] verifiedDelta = delta;
214+
Class<?>[] flatAllowedTypes = new Class[allowedTypes.length];
215+
for (int i = 0; i < allowedTypes.length; i++) {
216+
flatAllowedTypes[i] = Utils.getComponentType(allowedTypes[i]);
217+
}
218+
boolean tryConverting = false;
219+
deltaLoop: for (Object object : verifiedDelta) {
220+
for (Class<?> allowedType : flatAllowedTypes) {
221+
if (allowedType.isInstance(object)) {
222+
continue deltaLoop;
223+
}
224+
}
225+
// delta value cannot be mapped to any allowed types
226+
tryConverting = true;
227+
}
228+
if (tryConverting) {
229+
// typing of delta may not be safe, create a new array
230+
Object[] newDelta = new Object[verifiedDelta.length];
231+
Converters.convert(verifiedDelta, newDelta, flatAllowedTypes);
232+
for (Object object : newDelta) {
233+
if (object == null) { // conversion failed
234+
return null;
235+
}
221236
}
237+
verifiedDelta = newDelta;
222238
}
223-
// no matching types, go next
224-
return null;
239+
// all values verified, convert
240+
//noinspection unchecked
241+
var handler = (ExpressionPropertyHandler<Object, ?>) propertyInfo.handler();
242+
handler.change(propertyHaver, verifiedDelta, mode);
243+
return propertyHaver;
225244
};
226245

227246
if (useCIP) {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test "PropCondContains conversion":
2+
assert "%uuid of world "world"%" contains uuid of world "world" with "failed to convert uuid to string for contains"

0 commit comments

Comments
 (0)