Skip to content

Commit

Permalink
add: Skript scripts path identifier ~
Browse files Browse the repository at this point in the history
fix: parsing nbt file.
  • Loading branch information
cooffeeRequired committed Mar 18, 2024
1 parent 1d97d4a commit 482cd17
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cz.coffee</groupId>
<artifactId>skJson</artifactId>
<version>3.0.6</version>
<version>3.0.7</version>
<packaging>jar</packaging>

<name>SkJson</name>
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/cz/coffee/skjson/api/FileHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cz.coffee.skjson.api;

import com.google.gson.*;
import cz.coffee.skjson.utils.Logger;
import org.bukkit.Bukkit;
import org.yaml.snakeyaml.Yaml;

import java.io.BufferedReader;
Expand Down Expand Up @@ -47,6 +49,10 @@ public static CompletableFuture<JsonElement> get(final String file) {
*/
public static CompletableFuture<JsonElement> get(final File file) {
return CompletableFuture.supplyAsync(() -> {
if (!file.exists()) {
Logger.warn("File " + file + " does not exist");
return JsonNull.INSTANCE;
}
try (var reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
var split = file.getName().split("\\.");
var ext = split[split.length - 1];
Expand Down Expand Up @@ -79,11 +85,19 @@ public static <T> T await(CompletableFuture<T> future) throws ExecutionException
return future.get();
}

public static CompletableFuture<Boolean> write(final String filePath, JsonElement content) {
public static CompletableFuture<Boolean> write(String filePath, JsonElement content) {
if (filePath.startsWith("~")) {
//noinspection DataFlowIssue
filePath = Bukkit.getPluginManager().getPlugin("Skript").getDataFolder() + "/scripts" + "/" + filePath.substring(1);
}
return createOrWrite(filePath, content, true);
}

public static CompletableFuture<Boolean> createOrWrite(final String filePath, JsonElement content) {
public static CompletableFuture<Boolean> createOrWrite(String filePath, JsonElement content) {
if (filePath.startsWith("~")) {
//noinspection DataFlowIssue
filePath = Bukkit.getPluginManager().getPlugin("Skript").getDataFolder() + "/scripts" + "/" + filePath.substring(1);
}
return createOrWrite(filePath, content, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ public class NewJsonExpression extends SimpleExpression<JsonElement> {
private boolean inputIsRegex;
private JsonExpressionString regexInput;

@SuppressWarnings("DataFlowIssue")
private File sanitizedFile(String file) {
if (file.startsWith("~")) {
return new File(Bukkit.getPluginManager().getPlugin("Skript").getDataFolder(), "scripts" + "/" + file.substring(1));
}
return new File(file);
}

@Override
protected JsonElement @NotNull [] get(@NotNull Event e) {
List<JsonElement> output = new ArrayList<>();
Expand All @@ -94,7 +102,7 @@ public class NewJsonExpression extends SimpleExpression<JsonElement> {
if (isFile) {
String stringifyFile = values[0].toString();
if (stringifyFile != null) {
final File file = new File(stringifyFile);
final File file = sanitizedFile(stringifyFile);

// make a sensitization for Failed get from FileWrapper
JsonElement json = FileHandler.get(file).join();
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ name: SkJson
version: '${project.version}'
main: cz.coffee.skjson.SkJson
api-version: '1.16'
revision-version: 'd4eb6e046'
full-revision-sha1: 'd4eb6e046bdf624dcae0b4cb3a3bda785d6b211534aa1d5099ac8c684819d0d5'
revision-version: '2c1b525c1'
full-revision-sha1: '2c1b525c1f3af22143a8ce5d59ce7b9eb30df41eb79d4da34931997f08c818ca'
depend:
- Skript
prefix: SkJson
Expand Down

0 comments on commit 482cd17

Please sign in to comment.