Skip to content

Commit

Permalink
- #36
Browse files Browse the repository at this point in the history
- Fix issue with nested looping for expression without a number
  • Loading branch information
cooffeeRequired committed Mar 11, 2023
1 parent 2e27468 commit 985096a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
2 changes: 0 additions & 2 deletions src/main/java/cz/coffee/core/AdapterUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ public static <T> JsonElement parseItem(T item, Expression<?> expression, Event
if (isClassic(item)) {
return JsonUtils.convert(item);
}


Class<?> clazz = item.getClass();
if (isItem) {
if (!isDefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public Object next() {
public boolean isLoopOf(@NotNull String s) {
if (!isEntries)
return super.isLoopOf(s);
return false;
return super.isLoopOf(s);
}

@Override
Expand Down
34 changes: 21 additions & 13 deletions src/main/java/cz/coffee/skript/expressions/JsonLoopExpressions.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.ConvertedExpression;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.log.ErrorQuality;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.sections.SecLoop;
import ch.njol.util.Kleenean;
import com.google.gson.JsonElement;
import cz.coffee.core.AdapterUtils;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.NotNull;
Expand All @@ -23,6 +24,7 @@
import java.util.regex.Pattern;

import static cz.coffee.core.NumberUtils.parsedNumber;
import static cz.coffee.core.Util.jsonToObject;

/**
* This file is part of skJson.
Expand Down Expand Up @@ -60,11 +62,14 @@ public class JsonLoopExpressions extends SimpleExpression<Object> {
private String name;

private SecLoop loop;
private boolean isCanceled = false;


@Override
@SuppressWarnings("unchecked")
protected @Nullable Object @NotNull [] get(@NotNull Event e) {
if (isCanceled) return new Object[0];

WeakHashMap<String, Object> o;
try {
o = (WeakHashMap<String, Object>) loop.getCurrent(e);
Expand All @@ -76,7 +81,15 @@ public class JsonLoopExpressions extends SimpleExpression<Object> {
for (Map.Entry<String, Object> entry : o.entrySet()) {
if (isKey) return new String[] {entry.getKey()};
Object[] one = (Object[]) Array.newInstance(getReturnType(), 1);
one[0] = entry.getValue();
if (entry.getValue() instanceof JsonElement) {
Object assigned = AdapterUtils.assignFrom((JsonElement) entry.getValue());
if (assigned == null) {
assigned = jsonToObject((JsonElement) entry.getValue());
}
one[0] = assigned;
} else {
one[0] = entry.getValue();
}
return one;
}
return new Object[0];
Expand All @@ -89,6 +102,7 @@ public boolean isSingle() {

@Override
public @NotNull Class<?> getReturnType() {
if (loop == null) return Object.class;
return loop.getLoopedExpression().getReturnType();
}

Expand All @@ -97,14 +111,6 @@ public boolean isSingle() {
if (e == null) return name;
return Classes.getDebugMessage(loop.getCurrent(e));
}

@SuppressWarnings("unchecked")
@Override
@Nullable
protected <R> ConvertedExpression<Object, ? extends R> getConvertedExpr(Class<R> @NotNull ... to) {
return super.getConvertedExpr(to);
}

@Override
public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parseResult) {
MatchResult mResult = parseResult.regexes.size() >0 ? parseResult.regexes.get(0) : null;
Expand All @@ -127,14 +133,14 @@ public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNul
int j = 1;
SecLoop loop = null;
for (SecLoop l : getParser().getCurrentSections(SecLoop.class)) {
if ((c != null && c.isAssignableFrom(l.getLoopedExpression().getReturnType())) || "value".equals(s) ||l.getLoopedExpression().isLoopOf(s)|| JsonElements.isChangedLoopOf(s)) {
if ((c != null && c.isAssignableFrom(l.getLoopedExpression().getReturnType())) || "value".equals(s) || l.getLoopedExpression().isLoopOf("value") || JsonElements.isChangedLoopOf(s)) {
if (j < i) {
j++;
continue;
}
if (loop != null) {
Skript.error("There are multiple loops that match loop-" + s + ". Use loop-" + s + "-1/2/3/etc. to specify which loop's value you want.", ErrorQuality.SEMANTIC_ERROR);
return false;
isCanceled = true;
break;
}
loop = l;
if (j == i) break;
Expand All @@ -144,6 +150,8 @@ public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNul
Skript.error("There's no loop that matches 'loop-" + s + "'", ErrorQuality.SEMANTIC_ERROR);
return false;
}
if (isCanceled)
Skript.error("There are multiple loops that match loop-" + s + ". Use loop-" + s + "-1/2/3/etc. to specify which loop's value you want.", ErrorQuality.SEMANTIC_ERROR);
this.loop = loop;
return true;
}
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/cz/coffee/skript/expressions/ModifyJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,18 @@ public void change(@NotNull Event e, @Nullable Object @Nullable [] delta, Change
String path = pathExpression.getSingle(e);
assert delta != null;
for (Object o : delta) {
final JsonObject objectElement = (JsonObject) o;
assert objectElement != null;
LinkedList<String> keys = extractKeys(path, null);
assert keys != null;
if (!(o instanceof JsonElement)) {
assert o != null;
o = parseItem(o, o.getClass());
removeByValue(element, keys, (JsonElement) o);
}
final JsonObject objectElement = (JsonObject) o;
for (Map.Entry<String, JsonElement> map :objectElement.entrySet()) {
if (map.getKey().equals("value")) {
assert keys != null;
removeByValue(element, keys, map.getValue());
} else if (map.getKey().equals("key")) {
assert keys != null;
keys.add(map.getValue().getAsString());
removeByKey(element, keys);
}
Expand Down

0 comments on commit 985096a

Please sign in to comment.