Skip to content

Commit

Permalink
Change from Effect to AsyncEffect to process asynchronous operations …
Browse files Browse the repository at this point in the history
…exactly as expected. For example, the operations for linking a file were error, fix (save json "..."), that will handle in the newest Skript version as World.
  • Loading branch information
cooffeeRequired committed Jan 18, 2024
1 parent 0bc7191 commit d3cf306
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 39 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ gen_server/
*.iml
.idea/inspectionProfiles/Project_Default.xml
/target/*
server
server
tools
4 changes: 3 additions & 1 deletion .gitignore_global
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
.DS_Store
.idea
server
/server
/server
/tools
tools
4 changes: 2 additions & 2 deletions 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.0</version>
<version>3.0.2</version>
<packaging>jar</packaging>

<name>SkJson</name>
Expand Down Expand Up @@ -71,7 +71,7 @@
<dependency>
<groupId>com.github.SkriptLang</groupId>
<artifactId>Skript</artifactId>
<version>2.8.0-pre1</version>
<version>2.8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/cz/coffee/skjson/api/WorldAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cz.coffee.skjson.api;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.SerializableAs;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

@SerializableAs("World")
public class WorldAdapter implements ConfigurationSerializable {
private final String worldName;
public WorldAdapter(World world) {
this.worldName = world.getName();
}

public static World deserialize(Map<String, Object> args) {
return Bukkit.getWorld(args.get("name").toString());
}
@Override
public @NotNull Map<String, Object> serialize() {
Map<String, Object> map = new HashMap<>();
map.put("name", worldName);
return map;
}
}
7 changes: 0 additions & 7 deletions src/main/java/cz/coffee/skjson/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import cz.coffee.skjson.api.DynamicObjectSerializer;
import cz.coffee.skjson.skript.base.Converter;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.entity.Player;
Expand All @@ -27,7 +26,6 @@
import static cz.coffee.skjson.skript.base.Converter.*;
import static cz.coffee.skjson.skript.base.SimpleConverter.SERIALIZED_JSON_TYPE_KEY;
import static cz.coffee.skjson.utils.Logger.error;
import static cz.coffee.skjson.utils.Logger.info;
import static org.bukkit.configuration.serialization.ConfigurationSerialization.SERIALIZED_TYPE_KEY;

/**
Expand Down Expand Up @@ -272,9 +270,6 @@ static <T> JsonElement assign(T object) {
if (object == null) return JsonNull.INSTANCE;
boolean isSerializable = (object instanceof YggdrasilSerializable || object instanceof ConfigurationSerializable);
try {
if (object instanceof World) {
return WorldConverter.toJson((World) object);
}
if (object instanceof Chunk) {
ChunkConverter.toJson((Chunk) object);
}
Expand Down Expand Up @@ -339,8 +334,6 @@ else if (json.getAsJsonObject().has(SERIALIZED_TYPE_KEY))

if (clazz != null) {
try {
if (World.class.isAssignableFrom(clazz))
return (T) WorldConverter.fromJson(finalJson.getAsJsonObject());
if (Chunk.class.isAssignableFrom(clazz))
return (T) ChunkConverter.fromJson(finalJson.getAsJsonObject());
else if (ItemStack.class.isAssignableFrom(clazz))
Expand Down
18 changes: 0 additions & 18 deletions src/main/java/cz/coffee/skjson/skript/base/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,6 @@ public NBTContainer fromJson(JsonObject json) {
return new NBTContainer(json.get("nbt").getAsString());
}
};
public final static SimpleConverter<World> WorldConverter = new SimpleConverter<World>() {
@Override
public @NotNull JsonElement toJson(World source) {
final JsonObject jsonObject = new JsonObject();
jsonObject.addProperty(SERIALIZED_JSON_TYPE_KEY, source.getClass().getName());
jsonObject.addProperty("name", source.getName());
return !jsonObject.isEmpty() ? jsonObject : JsonNull.INSTANCE;
}

@Override
public World fromJson(JsonObject json) {
if (json.has(SERIALIZED_JSON_TYPE_KEY)) {
World world;
if ((world = getWorld(json.get("name").getAsString())) != null) return world;
}
return null;
}
};
public final static SimpleConverter<ItemStack> ItemStackConverter = new SimpleConverter<ItemStack>() {

private static ItemStack enchants(ItemStack itemStack, final JsonObject meta) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ protected void execute(@NotNull Event e) {
if (id == null || fileString == null) return;
JsonCache<String, JsonElement, File> cache = Config.getCache();
File file = new File(fileString);
if (cache.containsKey(id)) return;
FileHandler.get(file).whenComplete((json, error) -> {
cache.addValue(id, json, file);
if (asAlive) if (!JsonWatcher.isRegistered(file)) JsonWatcher.register(id, file);
Expand Down Expand Up @@ -186,6 +187,7 @@ private void StreamOf(String pathDirectory, File folder, String finalCacheDirect
jsonFiles.add(potentialFile, json);
});
JsonCache<String, JsonElement, File> cache = Config.getCache();
if (cache.containsKey(finalCacheDirectory)) return;
cache.addValue(finalCacheDirectory, jsonFiles, folder);
}

Expand Down Expand Up @@ -280,7 +282,7 @@ protected void execute(@NotNull Event e) {
return;
}
try {
await(FileHandler.createOrWrite(file.toString(), json));
await(FileHandler.write(file.toString(), json));
} catch (ExecutionException | InterruptedException ex) {
error(ex, null, getParser().getNode());
}
Expand All @@ -290,7 +292,7 @@ protected void execute(@NotNull Event e) {
cache.forEach((key, map) -> map.forEach((json, file) -> {
if (!file.getName().equals("Undefined")) {
try {
await(FileHandler.createOrWrite(file.toString(), json));
await(FileHandler.write(file.toString(), json));
} catch (ExecutionException | InterruptedException ex) {
error(ex, null, getParser().getNode());
}
Expand Down Expand Up @@ -371,7 +373,7 @@ public static class GetCachedJson extends SimpleExpression<JsonElement> {

static {
SkJsonElements.registerExpression(GetCachedJson.class, JsonElement.class, ExpressionType.SIMPLE,
"json %string%",
"json [id] %string%",
"all cached jsons"
);
}
Expand Down Expand Up @@ -415,15 +417,16 @@ public boolean isSingle() {
public @NotNull String toString(@Nullable Event event, boolean b) {
if (line == 0) {
assert event != null;
return "get cached json " + storedKeyExpr.toString(event, b);
return "get json " + storedKeyExpr.toString(event, b);
} else {
return "all cached jsons";
return "all jsons";
}
}

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parseResult) {
public boolean init(Expression<?>[] exprs, int matchedPattern, @NotNull Kleenean isDelayed,
@NotNull ParseResult parseResult) {
line = matchedPattern;
storedKeyExpr = (Expression<String>) exprs[0];
return true;
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/cz/coffee/skjson/skript/types/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import cz.coffee.skjson.api.WorldAdapter;
import cz.coffee.skjson.api.requests.Request;
import cz.coffee.skjson.api.requests.RequestMethod;
import cz.coffee.skjson.api.requests.Webhook;
Expand All @@ -19,8 +20,8 @@
import cz.coffee.skjson.utils.PatternUtil;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.checkerframework.checker.nullness.qual.NonNull;
Expand All @@ -44,10 +45,13 @@
@SuppressWarnings("deprecation")
abstract class Types {
// JsonElement type
static final Collection<Class<?>> allowedTypes = List.of(ItemStack.class, Location.class, World.class, Chunk.class, Inventory.class, ConfigurationSerializable.class);
static final Collection<Class<?>> allowedTypes = List.of(
ItemStack.class, Location.class, Chunk.class, Inventory.class, ConfigurationSerializable.class
);

static {
try {
ConfigurationSerialization.registerClass(WorldAdapter.class, "World");
if (Skript.getVersion().isLargerThan(new Version(2, 6, 4))) {
allowedTypes.forEach(clazz -> Converters.registerConverter(JsonElement.class, clazz, ParserUtil::from));
} else {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/lang/default.lang
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ types:
json: Json
jsonwebhook: Webhook
requestmethod: Metody
request: Request

damage causes:
kill: This is error what i got!, Defined in .lang
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: 'd8304bbb8'
full-revision-sha1: 'd8304bbb81769ba34c42d1c32cc865744bc673ef'
revision-version: '1c4ae32eb'
full-revision-sha1: '1c4ae32eb7f2091e560630a13bcbd1f41b783bd983ecbb02f10e7752d9cf341'
depend:
- Skript
prefix: SkJson
Expand Down

0 comments on commit d3cf306

Please sign in to comment.