Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from json serializaton to custom adapters #89

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@


<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>skript</id>
<url>https://repo.skriptlang.org/releases</url>
Expand Down Expand Up @@ -71,7 +75,7 @@
<dependency>
<groupId>com.github.SkriptLang</groupId>
<artifactId>Skript</artifactId>
<version>2.8.0</version>
<version>2.7.3</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand All @@ -80,6 +84,16 @@
<version>2.10.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.34</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
Expand Down Expand Up @@ -192,7 +206,7 @@
<configuration>
<target>
<mkdir dir="${project.basedir}/server/plugins"/>
<copy file="${project.build.directory}/${project.artifactId}-${project.version} (shaded).jar" tofile="${project.basedir}/server/plugins/SkJson.jar"/>
<copy file="${project.build.directory}/${project.artifactId}-${project.version} (shaded).jar" tofile="E:/Git/mc_server/plugins/SkJson.jar"/>
<echo message="Copied successfully" />
</target>
</configuration>
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/cz/coffee/skjson/SkJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@

public final class SkJson extends JavaPlugin {

static {
//ConfigurationSerialization.registerClass(WorldAdapter.class, "World");
}

public static double CONFIG_PRIMARY_VERSION = 1.4;
static SkJson plugin;
Config config = new Config(this);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cz/coffee/skjson/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public void init() throws IOException {


try {
if (!versionError(Skript.getVersion(), new Version("2.8.0-pre1"), true, manager, plugin))
if (!versionError(Skript.getVersion(), new Version("2.7.3"), true, manager, plugin))
return;

ready = classesRegistration(plugin);
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/cz/coffee/skjson/api/SkJsonCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,23 @@ public boolean onCommand(@NotNull CommandSender sender, Command command, @NotNul
info("%s 🟠 &econfig reloading...", PLUGIN_PREFIX);
try {
final HashMap<String, ?> before = new HashMap<>(Map.ofEntries(
Map.entry("CONFIG_VERSION", CONFIG_VERSION),
Map.entry("PROJECT_DEBUG", PROJECT_DEBUG),
Map.entry("LOGGING_LEVEL", LOGGING_LEVEL),
Map.entry("DEFAULT_WATCHER_INTERVAL", DEFAULT_WATCHER_INTERVAL),
Map.entry("PLUGIN_PREFIX", PLUGIN_PREFIX),
Map.entry("ERROR_PREFIX", ERROR_PREFIX),
Map.entry("WATCHER_PREFIX", WATCHER_PREFIX),
Map.entry("REQUESTS_PREFIX", REQUESTS_PREFIX),
Map.entry("WEBHOOK_PREFIX", WEBHOOK_PREFIX),
Map.entry("PATH_VARIABLE_DELIMITER", PATH_VARIABLE_DELIMITER),
Map.entry("ALLOWED_LINE_LITERAL", ALLOWED_LINE_LITERAL)
Map.entry("CONFIG_VERSION", ConfigRecords.CONFIG_VERSION),
Map.entry("PROJECT_DEBUG", ConfigRecords.PROJECT_DEBUG),
Map.entry("LOGGING_LEVEL", ConfigRecords.LOGGING_LEVEL),
Map.entry("DEFAULT_WATCHER_INTERVAL", ConfigRecords.DEFAULT_WATCHER_INTERVAL),
Map.entry("PLUGIN_PREFIX", ConfigRecords.PLUGIN_PREFIX),
Map.entry("ERROR_PREFIX", ConfigRecords.ERROR_PREFIX),
Map.entry("WATCHER_PREFIX", ConfigRecords.WATCHER_PREFIX),
Map.entry("REQUESTS_PREFIX", ConfigRecords.REQUESTS_PREFIX),
Map.entry("WEBHOOK_PREFIX", ConfigRecords.WEBHOOK_PREFIX),
Map.entry("PATH_VARIABLE_DELIMITER", ConfigRecords.PATH_VARIABLE_DELIMITER),
Map.entry("ALLOWED_LINE_LITERAL", ConfigRecords.ALLOWED_LINE_LITERAL)
));
Config.getConfig().loadConfigFile(false);
AtomicBoolean changed = new AtomicBoolean(false);
before.forEach((key, value) -> {
try {
Field field = Config.class.getDeclaredField(key);
Field field = ConfigRecords.class.getDeclaredField(key);
field.setAccessible(true);
Object fieldValue = field.get(null);
// Porovnejte hodnotu ve fieldu s hodnotou v mapě
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/cz/coffee/skjson/api/Update/UpdateCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cz.coffee.skjson.api.Config;
import cz.coffee.skjson.api.http.RequestClient;
import cz.coffee.skjson.api.http.RequestResponse;
import cz.coffee.skjson.utils.Util;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down Expand Up @@ -78,14 +79,16 @@ public boolean getReady() {
}

private int sanitizeVersion(String version) {
String sanitized = "";
try {
String replaced = version.replaceAll("[.]", "");
sanitized = String.join("", replaced.replaceAll("[^0-9.]+", " ").trim().split("\\s+"));
if (replaced.length() == 2) replaced = replaced + 0;
return Integer.parseInt(replaced);
} catch (NumberFormatException exception) {
if (PROJECT_DEBUG) error(exception);
return Util.parseNumber(sanitized);
//if (PROJECT_DEBUG) error(exception);
}
return 0;
}

private JsonElement getGithubConfig() {
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/cz/coffee/skjson/api/requests/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.google.gson.JsonElement;

import java.util.LinkedList;
import java.util.*;

import static cz.coffee.skjson.utils.Util.fstring;

Expand All @@ -13,6 +13,7 @@ public class Request {
private Pairs[] header;
private LinkedList<Attachment> attachments = new LinkedList<>();
private RequestStatus status = RequestStatus.UNKNOWN;
private HashMap<String, String[]> queryParams = new HashMap<>();
private Response response = Response.empty();

public Request(String uri, RequestMethod method, JsonElement content, Pairs[] headers) {
Expand Down Expand Up @@ -74,6 +75,18 @@ public LinkedList<Attachment> attachments() {
return attachments;
}

public HashMap<String, String[]> getQueryParams() {
return this.queryParams;
}

public void addQueryParam(HashMap<String, String[]> queryParams) {
this.queryParams.putAll(queryParams);
}

public void setQueryParam(HashMap<String, String[]> queryParams) {
this.queryParams = queryParams;
}

public void setAttachments(LinkedList<Attachment> attachments) {
this.attachments = attachments;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/cz/coffee/skjson/json/JsonParserRecords.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,12 @@ public void value(LinkedList<PatternUtil.keyStruct> keys, JsonElement value) {
Deque<JsonElement> currents = new ConcurrentLinkedDeque<>();
currents.offerLast(this.json);
JsonElement current;
if (keys.isEmpty()) return;
PatternUtil.keyStruct lastKey = keys.removeLast();
while ((current = currents.pollLast()) != null) {
for (PatternUtil.keyStruct struct : keys) {
if (struct.key().isEmpty() || struct.key().isBlank()) continue;
try {
int index = Util.isNumber(struct.key()) ? parseNumber(struct.key()) : -1;
if (current instanceof JsonObject jsonobject) {
if (!jsonobject.has(struct.key())) {
if (struct.isList()) {
Expand All @@ -282,6 +282,7 @@ public void value(LinkedList<PatternUtil.keyStruct> keys, JsonElement value) {
}
current = jsonobject.get(struct.key());
} else if (current instanceof JsonArray jsonArray) {
int index = Util.isNumber(struct.key()) ? parseNumber(struct.key()) : -1;
if (index >= jsonArray.size()) {
if (struct.isList()) {
jsonArray.add(new JsonArray());
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/cz/coffee/skjson/skript/base/JsonBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import cz.coffee.skjson.api.FileHandler;
import cz.coffee.skjson.json.JsonParser;
import cz.coffee.skjson.parser.ParserUtil;
import cz.coffee.skjson.utils.Logger;
import cz.coffee.skjson.utils.PatternUtil;
import cz.coffee.skjson.utils.Util;
import org.bukkit.event.Event;
Expand Down Expand Up @@ -210,7 +211,7 @@ public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNul
if (matchingPattern.matches()) {
String[] split = firstField.split("-");
s = split[1];
i = parseNumber(group);
i = (int) parseNumber(group);
}
Class<?> inputClass = Classes.getClassFromUserInput(s);
name = s;
Expand Down Expand Up @@ -302,19 +303,17 @@ public static LinkedList<Object> getNestedElements(JsonElement current) {
@Override
protected @Nullable Object @NotNull [] get(@NotNull Event e) {
try {

boolean emptyPath = pathInput == null;
JsonElement json = null;
try {
json = jsonInput.getSingle(e);
} catch (Exception ignored) {
} catch (Exception ex) {
Logger.error(ex, null, getParser().getNode());
}
if (json == null) return new Object[0];
boolean emptyPath = pathInput == null;
String keys = !emptyPath ? pathInput.getSingle(e) : null;

Deque<PatternUtil.keyStruct> wrappedKeys = convertStringToKeys(keys);
if (wrappedKeys.isEmpty() && (!emptyPath || !isValues)) return new Object[0];

if (isValues) {
if (emptyPath) {
return needConvert ? new Object[]{json} : getNestedElements(json).toArray(new Object[0]);
Expand Down Expand Up @@ -681,14 +680,15 @@ private static void toList(@NotNull String name, JsonElement inputJson, boolean
}

static <T> void parsed(String name, T object, boolean isLocal, Event event) {
if (PROJECT_DEBUG && LOGGING_LEVEL > 2) Logger.info("PARSED -> (Variable) %s => &e%s", name, object);
Variables.setVariable(name, object, event, isLocal);
}

static void primitive(String name, JsonPrimitive input, boolean isLocal, Event event) {
if (name == null || input == null || event == null) return;
Object o = jsonToType(defaultConverter(input));
if (o != null) {
if (PROJECT_DEBUG && LOGGING_LEVEL > 2) info(o.getClass());
if (PROJECT_DEBUG && LOGGING_LEVEL > 2) Logger.info("PRIMITIVE -> (Variable) %s => &e%s", name, input);
}
assert o != null;
Variables.setVariable(name, o, event, isLocal);
Expand Down Expand Up @@ -948,7 +948,7 @@ public boolean check(@NotNull Event e) {
break;
}
} else {
String element = (String) value;
String element = value.toString();
if (directly) {
final Queue<PatternUtil.keyStruct> list = convertStringToKeys(element, PATH_VARIABLE_DELIMITER, true);
final JsonElement result = JsonParser.search(json).key(list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.util.LiteralUtils;
import ch.njol.util.Kleenean;
import com.google.gson.*;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonParser;
import cz.coffee.skjson.SkJsonElements;
import cz.coffee.skjson.api.FileHandler;
import cz.coffee.skjson.api.http.RequestClient;
Expand Down Expand Up @@ -160,7 +162,7 @@ public boolean isSingle() {
case 2 -> isYaml ? "yaml file" : "json file";
case 3 -> "website file";
default -> "object";
} + " " + input.toString(e, debug);
} + " " + (this.input != null ? this.input.toString(e, debug) : this.regexInput.toString(e, debug));
} catch (Exception ex) {
error(ex, null, getParser().getNode());
}
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/cz/coffee/skjson/skript/base/PrettyPrint.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,18 @@
public class PrettyPrint extends SimpleExpression<String> {

static final Gson gson = new GsonBuilder().serializeNulls().disableHtmlEscaping().setPrettyPrinting().create();
static Expression<JsonElement> jsonExpression;
private Expression<JsonElement> jElements;

static {
SkJsonElements.registerExpression(PrettyPrint.class, String.class, ExpressionType.SIMPLE, "%jsons% with [(:uncoloured|:uncolored)] pretty print");
SkJsonElements.registerExpression(PrettyPrint.class, String.class, ExpressionType.SIMPLE,
"%json% with [(:uncoloured|:uncolored)] pretty print");
}

private boolean uncoloured_;

@Override
protected @Nullable String @NotNull [] get(@NotNull Event e) {
JsonElement[] jsons = jsonExpression.getAll(e);
JsonElement[] jsons = jElements.getAll(e);
ArrayList<String> coloredJsons = new ArrayList<>();

if (uncoloured_) {
Expand All @@ -73,7 +74,7 @@ public class PrettyPrint extends SimpleExpression<String> {

@Override
public boolean isSingle() {
return jsonExpression.isSingle();
return jElements.isSingle();
}

@Override
Expand All @@ -83,14 +84,17 @@ public boolean isSingle() {

@Override
public @NotNull String toString(@Nullable Event e, boolean debug) {
assert e != null;
return jsonExpression.toString(e, debug) + " with pretty print";
if(e != null) {
return jElements.toString(e, debug) + " with pretty print";
} else {
return "json with pretty print";
}
}

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parseResult) {
jsonExpression = LiteralUtils.defendExpression(exprs[0]);
jElements = LiteralUtils.defendExpression(exprs[0]);
uncoloured_ = parseResult.hasTag("uncoloured") || parseResult.hasTag("uncolored");
return LiteralUtils.canInitSafely(jsonExpression);
return LiteralUtils.canInitSafely(jElements);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ public boolean init(Expression<?>[] expressions, int i, @NotNull Kleenean kleene
public static class SaveCache extends AsyncEffect {
static {
SkJsonElements.registerEffect(SaveCache.class,
"save json %string%",
"save all jsons"
"save cached json %string%",
"save all cached jsons"
);
}

Expand Down Expand Up @@ -329,7 +329,7 @@ public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNul
@Since("2.8.0 - performance & clean")
public static class UnlinkFile extends Effect {
static {
SkJsonElements.registerEffect(UnlinkFile.class, "unlink json %string%");
SkJsonElements.registerEffect(UnlinkFile.class, "unlink json %string% [(:with) [a] file]");
}

private Expression<String> exprID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public void change(@NotNull Event e, @Nullable Object @Nullable [] inputDelta, C
}
}
} catch (Exception ex) {
error(new RuntimeException("Input or json cannot be null"), null, getParser().getNode());
error(ex, null, getParser().getNode());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class EvtFile extends EventValueExpression<File> {

static {
Skript.registerExpression(EvtFile.class, File.class, ExpressionType.EVENT, "[the] [event-](file|link)");
Skript.registerExpression(EvtFile.class, File.class, ExpressionType.SIMPLE, "[the] [event-](file|link)");
}

public EvtFile() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Since("2.9")
public class EvtUUID extends EventValueExpression<UUID> {
static {
Skript.registerExpression(EvtUUID.class, UUID.class, ExpressionType.EVENT, "[the] [event-](uuid|id)");
Skript.registerExpression(EvtUUID.class, UUID.class, ExpressionType.SIMPLE, "[the] [event-](uuid|id)");
}

public EvtUUID() {
Expand Down
Loading
Loading