-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add count of object/phrase in the json - Add json size of current element
- Loading branch information
1 parent
81f2084
commit 42669bf
Showing
8 changed files
with
268 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,5 +32,4 @@ replay_pid* | |
*.lock | ||
*.bin | ||
build.___gradle | ||
build.___gradle | ||
*.lang |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/main/java/cz/coffee/skript/expressions/JsonCountOf.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package cz.coffee.skript.expressions; | ||
|
||
import ch.njol.skript.Skript; | ||
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.SimpleExpression; | ||
import ch.njol.skript.util.LiteralUtils; | ||
import ch.njol.util.Kleenean; | ||
import com.google.gson.JsonElement; | ||
import org.bukkit.event.Event; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import static cz.coffee.core.AdapterUtils.parseItem; | ||
import static cz.coffee.core.JsonUtils.countKeys; | ||
import static cz.coffee.core.JsonUtils.countValues; | ||
|
||
/** | ||
* This file is part of skJson. | ||
* <p> | ||
* Skript is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* <p> | ||
* Skript is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* <p> | ||
* You should have received a copy of the GNU General Public License | ||
* along with Skript. If not, see <<a href="http://www.gnu.org/licenses/">...</a>>. | ||
* <p> | ||
* Copyright coffeeRequired nd contributors | ||
* <p> | ||
* Created: Friday (3/10/2023) | ||
*/ | ||
public class JsonCountOf extends SimpleExpression<Integer> { | ||
|
||
static { | ||
Skript.registerExpression(JsonCountOf.class, Integer.class, ExpressionType.SIMPLE, "(count|number) of (:key|[value]) %object% in %json%"); | ||
} | ||
|
||
private boolean isValue; | ||
private Expression<?> valueExpression; | ||
private Expression<JsonElement> jsonElementExpression; | ||
|
||
@Override | ||
protected @Nullable Integer @NotNull [] get(@NotNull Event e) { | ||
JsonElement json = jsonElementExpression.getSingle(e); | ||
Object unparsedValue = valueExpression.getSingle(e); | ||
assert json != null; | ||
if (isValue) { | ||
JsonElement parsed = parseItem(unparsedValue, valueExpression, e); | ||
assert parsed != null; | ||
return new Integer[]{countValues(parsed, json)}; | ||
} else { | ||
if (unparsedValue instanceof String) { | ||
return new Integer[]{countKeys((String) unparsedValue, json)}; | ||
} | ||
} | ||
return new Integer[0]; | ||
} | ||
|
||
@Override | ||
public boolean isSingle() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public @NotNull Class<? extends Integer> getReturnType() { | ||
return Integer.class; | ||
} | ||
|
||
@Override | ||
public @NotNull String toString(@Nullable Event e, boolean debug) { | ||
return "count of " + (isValue ? valueExpression.toString(e, debug) : "key " + valueExpression.toString(e, debug)) + " in " + jsonElementExpression.toString(e, debug); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parseResult) { | ||
isValue = !parseResult.hasTag("key"); | ||
jsonElementExpression = (Expression<JsonElement>) exprs[1]; | ||
valueExpression = LiteralUtils.defendExpression(exprs[0]); | ||
return LiteralUtils.canInitSafely(valueExpression); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package cz.coffee.skript.expressions; | ||
|
||
import ch.njol.skript.expressions.base.SimplePropertyExpression; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* This file is part of skJson. | ||
* <p> | ||
* Skript is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* <p> | ||
* Skript is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* <p> | ||
* You should have received a copy of the GNU General Public License | ||
* along with Skript. If not, see <<a href="http://www.gnu.org/licenses/">...</a>>. | ||
* <p> | ||
* Copyright coffeeRequired nd contributors | ||
* <p> | ||
* Created: Friday (3/10/2023) | ||
*/ | ||
public class JsonSize extends SimplePropertyExpression<JsonElement, Integer> { | ||
|
||
static { | ||
register(JsonSize.class, Integer.class, "size", "jsons"); | ||
} | ||
@Override | ||
protected @NotNull String getPropertyName() { | ||
return "json size"; | ||
} | ||
|
||
@Override | ||
public @Nullable Integer convert(JsonElement jsonElement) { | ||
return jsonElement.isJsonArray() ? ((JsonArray) jsonElement).size() : ((JsonObject) jsonElement).size(); | ||
} | ||
|
||
@Override | ||
public @NotNull Class<? extends Integer> getReturnType() { | ||
return Integer.class; | ||
} | ||
} |
Oops, something went wrong.