Skip to content

Commit

Permalink
#36
Browse files Browse the repository at this point in the history
2.8.0 initial
  • Loading branch information
cooffeeRequired committed Mar 17, 2023
1 parent 8c95ba9 commit dac38ea
Show file tree
Hide file tree
Showing 34 changed files with 778 additions and 125 deletions.
10 changes: 7 additions & 3 deletions src/main/java/cz/coffee/SkJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import ch.njol.skript.config.Node;
import ch.njol.skript.util.Version;
import com.google.gson.JsonElement;
import cz.coffee.core.CacheMap;
import cz.coffee.core.cache.CacheMap;
import cz.coffee.core.Updater;
import cz.coffee.core.cache.JsonWatcher;
import de.tr7zw.nbtapi.NBTContainer;
Expand All @@ -20,8 +20,8 @@
import java.util.WeakHashMap;
import java.util.logging.Logger;

import static cz.coffee.core.Util.color;
import static cz.coffee.core.Util.hex;
import static cz.coffee.core.utils.Util.color;
import static cz.coffee.core.utils.Util.hex;

/**
* This file is part of skJson.
Expand Down Expand Up @@ -70,6 +70,10 @@ public static void error(@NotNull Object message) {
logger.info(color("&c"+message));
}

public static void warning(@NotNull Object message) {
logger.warning(color(message));
}

public static void error(String message, Node node) {
logger.info(color(node.toString()));
logger.info(color("&c" + message));
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/cz/coffee/core/adapters/Adapters.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

import java.util.*;

import static cz.coffee.core.AdapterUtils.parseItem;
import static cz.coffee.core.Util.GSON_ADAPTER;
import static cz.coffee.core.utils.AdapterUtils.parseItem;
import static cz.coffee.core.utils.Util.GSON_ADAPTER;
import static org.bukkit.Bukkit.createInventory;
import static org.bukkit.Bukkit.getWorld;
import static org.bukkit.configuration.serialization.ConfigurationSerialization.SERIALIZED_TYPE_KEY;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cz.coffee.core;
package cz.coffee.core.cache;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/cz/coffee/core/cache/JsonWatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.google.gson.JsonElement;
import cz.coffee.SkJson;
import cz.coffee.core.FileUtils;
import cz.coffee.core.utils.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -45,7 +45,6 @@ public class JsonWatcher {
public static void init() {
LOGGER = LoggerFactory.getLogger("AsyncJsonWatcher");
service = Executors.newSingleThreadScheduledExecutor(runnable -> new Thread(runnable, "AsyncJsonWatcher"));
service.isShutdown();
}

public static Logger getLogger() {
Expand Down
155 changes: 155 additions & 0 deletions src/main/java/cz/coffee/core/mapping/JsonMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package cz.coffee.core.mapping;

import ch.njol.skript.lang.Variable;
import ch.njol.skript.variables.Variables;
import com.google.gson.*;
import cz.coffee.core.utils.NumberUtils;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.stream.Stream;

import static ch.njol.skript.variables.Variables.getVariable;
import static cz.coffee.core.utils.AdapterUtils.parseItem;
import static cz.coffee.core.utils.Util.jsonToObject;

/**
* 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: pátek (17.03.2023)
*/

public abstract class JsonMap {
private static final Gson GSON = new GsonBuilder().serializeNulls().enableComplexMapKeySerialization().disableHtmlEscaping().create();
private static final JsonObject JSON_OBJECT = new JsonObject();
private static final JsonArray JSON_ARRAY = new JsonArray();
private static final String SEPARATOR = Variable.SEPARATOR;
public static class Skript {
public static void toList(@NotNull String name, JsonElement input, boolean isLocal, Event event) {
JsonElement current;
Queue<JsonElement> elements = new ConcurrentLinkedQueue<>();
if (input != null) elements.add(input);

while ((current = elements.poll()) != null) {
if (current instanceof JsonPrimitive primitive) {
primitive(name, primitive, isLocal, event);
} else if (current instanceof JsonObject object){
nested(name, object, isLocal, event);
} else if (current instanceof JsonArray array) {
nested(name, array, isLocal, event);
}
}
}

private static void primitive(String name, JsonPrimitive input, boolean isLocal, Event event) {
if (input.isBoolean()) Variables.setVariable(name, input.getAsBoolean(), event, isLocal);
else if (input.isNumber()) Variables.setVariable(name, input.getAsNumber(), event, isLocal);
else if (input.isString()) Variables.setVariable(name, input.getAsString(), event, isLocal);
}

private static void nested(@NotNull String name, @NotNull JsonElement input, boolean isLocal, Event event) {
if (input instanceof JsonObject object) {
//main(name + "*", object, isLocal, event);
object.keySet().forEach(key -> {
if (key != null)
toList(name + key, object.get(key), isLocal, event);
});
} else if (input instanceof JsonArray array) {
//main(name + "*", array, isLocal, event);
for (int index = 0; array.size() > index; index++)
toList(name + (index+1), array.get(index), isLocal, event);
}
}

private static void main(@NotNull String name, @NotNull JsonElement input, boolean isLocal, Event e) {
final Object o = GSON.fromJson(input.toString(), Object.class);
Variables.setVariable(name, o, e, isLocal);
}

}
public static class Json {

@SuppressWarnings("unchecked")
public static JsonElement convert(@NotNull String name, boolean isLocal, boolean nullable, Event event) {
final Object varObject = getVariable(name + "*", event, isLocal);
Map<String, Object> variable = (Map<String, Object>) varObject;
if (variable == null) return nullable ? null : JSON_OBJECT;
Stream<String> keys = variable.keySet().stream().filter(Objects::nonNull);

if (variable.keySet().stream().filter(Objects::nonNull).allMatch(NumberUtils::isNumber)) {
final List<String> checkKeys = new ArrayList<>();
variable.keySet().stream().filter(Objects::nonNull).forEach(checkKeys::add);
if (NumberUtils.isIncrement(checkKeys.toArray())) {
final JsonArray jsonStructure = JSON_ARRAY;
keys.forEach(key -> {
Object rawValue = subList(name + key, isLocal, event);
JsonElement valueData = GSON.toJsonTree(rawValue);
if (valueData instanceof JsonPrimitive primitive) {
if (NumberUtils.isNumber(jsonToObject(primitive))) {
JsonElement jsonPrimitive = JsonParser.parseString(jsonToObject(primitive).toString());
jsonStructure.add(jsonPrimitive);
} else {
jsonStructure.add(primitive);
}
} else {
jsonStructure.add(valueData);
}
});
return jsonStructure;
} else {
final JsonObject jsonStructure = JSON_OBJECT;
keys.forEach(key -> {
JsonElement data = GSON.toJsonTree(subList(name + key, isLocal, event));
if (data instanceof JsonPrimitive primitive) {
jsonStructure.add(key, primitive);
} else {
jsonStructure.add(key, data);
}
});
return jsonStructure;
}
} else {
final JsonObject jsonStructure = JSON_OBJECT;
keys.forEach(key -> {
JsonElement data = GSON.toJsonTree(subList(name + key, isLocal, event));
if (data instanceof JsonPrimitive primitive) {
jsonStructure.add(key, primitive);
} else {
jsonStructure.add(key, data);
}
});
return jsonStructure;
}
}

private static Object subList(String name, boolean isLocal, Event event) {
Object variable = getVariable(name, event, isLocal);
if (variable == null) convert(name + SEPARATOR, isLocal, false, event);
else if (variable == Boolean.TRUE) {
Object subVar = convert(name + SEPARATOR, isLocal, true, event);
if (subVar != null) variable = subVar;
}
if (!(variable instanceof String || variable instanceof Number || variable instanceof Boolean || variable instanceof JsonElement || variable instanceof Map || variable instanceof List)) {
if (variable != null) variable = parseItem(variable, variable.getClass());
}
return variable;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cz.coffee.core;
package cz.coffee.core.utils;

import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.lang.Expression;
Expand All @@ -15,9 +15,9 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import static cz.coffee.core.Util.GSON_ADAPTER;
import static cz.coffee.core.adapters.Adapter.SERIALIZED_JSON_TYPE_KEY;
import static cz.coffee.core.adapters.Adapters.*;
import static cz.coffee.core.utils.Util.GSON_ADAPTER;
import static org.bukkit.configuration.serialization.ConfigurationSerialization.SERIALIZED_TYPE_KEY;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cz.coffee.core;
package cz.coffee.core.utils;

import com.google.gson.*;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cz.coffee.core;
package cz.coffee.core.utils;

import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cz.coffee.core;
package cz.coffee.core.utils;

import ch.njol.skript.Skript;
import com.google.gson.*;
Expand All @@ -7,12 +7,12 @@
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;

import static cz.coffee.core.AdapterUtils.assignFrom;
import static cz.coffee.core.AdapterUtils.parseItem;
import static cz.coffee.core.NumberUtils.isNumber;
import static cz.coffee.core.NumberUtils.parsedNumber;
import static cz.coffee.core.Util.arrayIsSafe;
import static cz.coffee.core.Util.jsonToObject;
import static cz.coffee.core.utils.AdapterUtils.assignFrom;
import static cz.coffee.core.utils.AdapterUtils.parseItem;
import static cz.coffee.core.utils.NumberUtils.isNumber;
import static cz.coffee.core.utils.NumberUtils.parsedNumber;
import static cz.coffee.core.utils.Util.arrayIsSafe;
import static cz.coffee.core.utils.Util.jsonToObject;

/**
* This file is part of skJson.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cz.coffee.core;
package cz.coffee.core.utils;

import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cz.coffee.core;
package cz.coffee.core.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand All @@ -15,7 +15,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static cz.coffee.core.NumberUtils.parsedNumber;
import static cz.coffee.core.utils.NumberUtils.parsedNumber;

/**
* This file is part of skJson.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cz/coffee/skript/SupportExpressions.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import java.util.ArrayList;

import static cz.coffee.core.AdapterUtils.parseItem;
import static cz.coffee.core.utils.AdapterUtils.parseItem;

/**
* This file is part of skJson.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cz/coffee/skript/cache/EffLinkJsonFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean;
import com.google.gson.JsonElement;
import cz.coffee.core.FileUtils;
import cz.coffee.core.utils.FileUtils;
import cz.coffee.core.cache.JsonWatcher;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cz/coffee/skript/cache/EffSaveCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean;
import com.google.gson.JsonElement;
import cz.coffee.core.FileUtils;
import cz.coffee.core.utils.FileUtils;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.NotNull;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/cz/coffee/skript/changer/JsonChanger.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import java.util.LinkedList;
import java.util.Map;

import static cz.coffee.core.AdapterUtils.parseItem;
import static cz.coffee.core.JsonUtils.*;
import static cz.coffee.core.Util.extractKeys;
import static cz.coffee.core.utils.AdapterUtils.parseItem;
import static cz.coffee.core.utils.JsonUtils.*;
import static cz.coffee.core.utils.Util.extractKeys;

/**
* This file is part of skJson.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.NotNull;

import static cz.coffee.core.FileUtils.isJsonFile;
import static cz.coffee.core.utils.FileUtils.isJsonFile;

/**
* This file is part of skJson.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/cz/coffee/skript/conditions/CondJsonHas.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import ch.njol.skript.util.LiteralUtils;
import ch.njol.util.Kleenean;
import com.google.gson.JsonElement;
import cz.coffee.core.JsonUtils;
import cz.coffee.core.utils.JsonUtils;
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.utils.AdapterUtils.parseItem;

/**
* This file is part of skJson.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/cz/coffee/skript/debug/RunTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.NotNull;

import static cz.coffee.core.Util.codeRunTime;
import static cz.coffee.core.Util.color;
import static cz.coffee.core.utils.Util.codeRunTime;
import static cz.coffee.core.utils.Util.color;

/**
* This file is part of skJson.
Expand Down
Loading

0 comments on commit dac38ea

Please sign in to comment.