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 b1ee777 commit 2e27468
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/main/java/cz/coffee/SkJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* <p>
* Created: Saturday (3/4/2023)
*/
@SuppressWarnings("unused")
public final class SkJson extends JavaPlugin {
private static final String[] dependencies = {"Skript"};
private static final Version version = Skript.getMinecraftVersion();
Expand Down Expand Up @@ -89,7 +90,7 @@ public void onEnable() {
try {
addon.loadClasses("cz.coffee.skript");
} catch (Exception exception) {
severe("Unable to register " + descriptionFile.getName() + " syntaxes:\n- " + exception.getMessage());
severe("Unable to register " + descriptionFile.getName() + " syntax's:\n- " + exception.getMessage());
pluginManager.disablePlugin(this);
}
console("&aFinished loading.");
Expand Down
1 change: 1 addition & 0 deletions src/main/java/cz/coffee/core/adapters/Adapters.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
* <p>
* Created: Saturday (3/4/2023)
*/
@SuppressWarnings("deprecation")
public abstract class Adapters {
public final static Adapter<World> WorldAdapter = new Adapter<>() {
@Override
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/cz/coffee/core/adapters/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* <p>
* Created: Saturday (3/4/2023)
*/
@SuppressWarnings("unused")
public class FileUtils {
static public JsonElement get(@NotNull File file) {
if (!file.exists() || !file.isFile()) return null;
Expand All @@ -60,6 +61,7 @@ static public CompletableFuture<Boolean> write(@NotNull File file, JsonElement e
if (async) {
return CompletableFuture.supplyAsync(() -> {
try {
//noinspection ReadWriteStringCanBeUsed
Files.write(file.toPath(), dataToWrite.getBytes(StandardCharsets.UTF_8));
return true;
} catch (IOException e) {
Expand All @@ -68,6 +70,7 @@ static public CompletableFuture<Boolean> write(@NotNull File file, JsonElement e
}
});
} else {
//noinspection ReadWriteStringCanBeUsed
Files.write(file.toPath(), dataToWrite.getBytes(StandardCharsets.UTF_8));
return CompletableFuture.completedFuture(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
*/

@Name("Count of objects/phrases")
@Description("You can get the exact number of identical keys or values from the entire json because `countof` works recursively.")
@Description("You can get the exact number of identical keys or values from the entire json because `count of` works recursively.")
@Examples({
"set {_json} to json from string \"{'A': [{'B': {}}], 'X': {}, 'UN': 'A'}\"",
"add diamond sword to {_json} for given path \"A[1]:B\"",
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/cz/coffee/skript/expressions/JsonElements.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ public class JsonElements extends SimpleExpression<Object> {
Skript.registerExpression(JsonElements.class, Object.class, ExpressionType.SIMPLE,
"%json%'s ((value|element)|(:values|:elements)) [%-string%]",
"((value|element)|(:values|:elements)) [%-string%] of %json%",
"%json% entries [%-string%]", "entries [%-string%] of %json%"
"%json%'s entries [%-string%]",
"entries [%-string%] of %json%"
);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.lang.reflect.Array;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -50,7 +51,7 @@ public class JsonLoopExpressions extends SimpleExpression<Object> {

static {
Skript.registerExpression(JsonLoopExpressions.class, Object.class, ExpressionType.SIMPLE,
"[the] [json-]loop-(value|:element|:key)-<(\\d+)>"
"[the] [json-]loop-(value|:element|:key)[-<(\\d+)>]"
);
}

Expand Down Expand Up @@ -106,22 +107,27 @@ public boolean isSingle() {

@Override
public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parseResult) {
Object pResult = parseResult.regexes.get(0).group(0);
MatchResult mResult = parseResult.regexes.size() >0 ? parseResult.regexes.get(0) : null;
Object group = -1;
if (mResult != null) {
group = mResult.group(0);
}
int i = -1;
isKey = parseResult.hasTag("key");
String firstField = parseResult.expr, s = "";
Pattern p = Pattern.compile("(loop.)(.+)(-.)");
Pattern p = Pattern.compile("loop-(.+)(.)");
Matcher m = p.matcher(firstField);
if (m.matches()) {
s = m.group(2) != null ? m.group(2) : "";
i = parsedNumber(pResult);
String[] split = firstField.split("-");
s = split[1];
i = parsedNumber(group);
}
Class<?> c = Classes.getClassFromUserInput(s);
name = s;
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(s)|| JsonElements.isChangedLoopOf(s)) {
if (j < i) {
j++;
continue;
Expand All @@ -131,8 +137,7 @@ public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNul
return false;
}
loop = l;
if (j == i)
break;
if (j == i) break;
}
}
if (loop == null) {
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/cz/coffee/skript/expressions/JsonSize.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@

@Name("Json size of current element")
@Examples({
"command SizeJson:\n" +
" trigger:\n" +
" set {_json} to json from text \"{'A': 1, 'B': 2, 'C': {'A': 'B', 'X': 'Y'}}\"\n" +
" send size of {_json} # = 3 (A, B, C)\n" +
" send size of (element \"C\" of {_json}) # = 2 (A, X)"
"""
command SizeJson:
trigger:
set {_json} to json from text "{'A': 1, 'B': 2, 'C': {'A': 'B', 'X': 'Y'}}"
send size of {_json} # = 3 (A, B, C)
send size of (element "C" of {_json}) # = 2 (A, X)"""
})
@Since("2.8.0 - performance & clean")

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/cz/coffee/skript/types/JsonElementType.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.classes.Serializer;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.lang.util.SimpleLiteral;
import ch.njol.skript.log.ErrorQuality;
Expand Down Expand Up @@ -35,7 +36,6 @@

import static cz.coffee.core.AdapterUtils.parseItem;
import static cz.coffee.core.JsonUtils.convert;

/**
* This file is part of skJson.
* <p>
Expand All @@ -56,6 +56,8 @@
* <p>
* Created: Saturday (3/4/2023)
*/

@Since("2.8.0")
public class JsonElementType {
public static final Collection<Class<?>> allowedTypes = List.of(ItemStack.class, Location.class, World.class, Block.class, Chunk.class, Inventory.class, ConfigurationSerializable.class);

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#file: noinspection SpellCheckingInspection
name: skJson
version: '${version}'
main: cz.coffee.SkJson
Expand Down

0 comments on commit 2e27468

Please sign in to comment.