From c63495e201c33911d13fec1ac373947fd77d0e5e Mon Sep 17 00:00:00 2001 From: Govindass Date: Thu, 26 Nov 2020 17:05:07 +0200 Subject: [PATCH] Some more work on variables (Still very unfinished) --- .idea/artifacts/RediSkript_jar.xml | 2 +- .../rediskript/managers/RedisManager.java | 77 +++++++++++++++---- .../rediskript/skript/SkriptHook.java | 2 +- .../elements/ExprVariableInChannel.java | 38 ++++++--- 4 files changed, 93 insertions(+), 26 deletions(-) diff --git a/.idea/artifacts/RediSkript_jar.xml b/.idea/artifacts/RediSkript_jar.xml index 23fcc92..45bda00 100644 --- a/.idea/artifacts/RediSkript_jar.xml +++ b/.idea/artifacts/RediSkript_jar.xml @@ -1,6 +1,6 @@ - $PROJECT_DIR$/out/artifacts/RediSkript_jar + $PROJECT_DIR$/../../Govindo/testserver/plugins diff --git a/src/main/java/net/limework/rediskript/managers/RedisManager.java b/src/main/java/net/limework/rediskript/managers/RedisManager.java index e1f700c..b637e84 100644 --- a/src/main/java/net/limework/rediskript/managers/RedisManager.java +++ b/src/main/java/net/limework/rediskript/managers/RedisManager.java @@ -3,8 +3,8 @@ package net.limework.rediskript.managers; import ch.njol.skript.registrations.Classes; import ch.njol.skript.variables.Variables; import net.limework.rediskript.RediSkript; -import net.limework.rediskript.events.RedisMessageEvent; import net.limework.rediskript.data.Encryption; +import net.limework.rediskript.events.RedisMessageEvent; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.configuration.Configuration; @@ -149,22 +149,65 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable { } } } else if (j.get("Type").equals("SkriptVariables")) { + + //Transfer variables between servers + JSONArray variableNames = j.getJSONArray("Names"); - boolean delete = false; - Object inputValue = null; - if (j.isNull("Value")) { - delete = true; - } else { - String input = j.getString("Value"); - String [] inputs = input.split("\\^", 2); - inputValue = Classes.deserialize(inputs[0], Base64.getDecoder().decode(inputs[1])); - - + Object inputValue; + String changeValue = null; + JSONArray variableValues = null; + if (!j.isNull("Values")) { + variableValues = j.getJSONArray("Values"); } for (int i = 0; i < variableNames.length(); i++) { - if (delete) { - Variables.setVariable(variableNames.get(i).toString(), null, null, false); + + if (j.isNull("Values")) { + //only check for SET here, because null has to be ignored in all other cases + + if (j.getString("Operation").equals("SET")) { + Variables.setVariable(variableNames.get(i).toString(), null, null, false); + } + } else { + if (!variableValues.isNull(i)) { + changeValue = variableValues.get(i).toString(); + } + String[] inputs = changeValue.split("\\^", 2); + inputValue = Classes.deserialize(inputs[0], Base64.getDecoder().decode(inputs[1])); + + //operations ARE UNFINISHED. because I do not yet know how to handle all the Long/Double conversions without issues. + + //first check if the variable is set + + if (Variables.getVariable(variableNames.get(i).toString(), null, false) != null) { + + //add to variable + + if (j.getString("Operation").equals("ADD")) { + if (inputValue.getClass().getName().equals("java.lang.Long")) { + inputValue = (Long) inputValue + (Long) Variables.getVariable(variableNames.get(i).toString(), null, false); + } else if (inputValue.getClass().getName().equals("java.lang.Double")) { + inputValue = (Double) inputValue + (Double) Variables.getVariable(variableNames.get(i).toString(), null, false); + } + + //remove from variable + + } else if (j.getString("Operation").equals("REMOVE")) { + + if (inputValue.getClass().getName().equals("java.lang.Long")) { + inputValue = (Long) Variables.getVariable(variableNames.get(i).toString(), null, false) - (Long) inputValue; + } else if (inputValue.getClass().getName().equals("java.lang.Double")) { + inputValue = (Double) Variables.getVariable(variableNames.get(i).toString(), null, false) - (Double) inputValue; + } + } + //if variable isn't set and removing, we ned it to properly convert this + } else if (j.getString("Operation").equals("REMOVE")) { + if (inputValue.getClass().getName().equals("java.lang.Long")) { + inputValue = -(Long) inputValue; + } else if (inputValue.getClass().getName().equals("java.lang.Double")) { + inputValue = -(Double) inputValue; + } + } Variables.setVariable(variableNames.get(i).toString(), inputValue, null, false); } } @@ -184,12 +227,16 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable { json.put("Date", System.currentTimeMillis()); //for unique string every time & PING calculations finishSendMessage(json, channel); } - public void sendVariables(String[] variableNames, String variableValue, String channel) { + public void sendVariables(String[] variableNames, String[] variableValues, String channel, String operation) { JSONObject json = new JSONObject(); json.put("Names", new JSONArray(variableNames)); - json.put("Value", variableValue); + if (variableValues != null) { + json.put("Values", new JSONArray(variableValues)); + } + json.put("Type", "SkriptVariables"); json.put("Date", System.currentTimeMillis()); //for unique string every time & PING calculations + json.put("Operation", operation); finishSendMessage(json, channel); } diff --git a/src/main/java/net/limework/rediskript/skript/SkriptHook.java b/src/main/java/net/limework/rediskript/skript/SkriptHook.java index 7613bf2..abb4bda 100644 --- a/src/main/java/net/limework/rediskript/skript/SkriptHook.java +++ b/src/main/java/net/limework/rediskript/skript/SkriptHook.java @@ -19,7 +19,7 @@ public class SkriptHook { try { addon.loadClasses("net.limework.rediskript.skript", "elements"); Skript.registerEvent("redis message", EvtRedis.class, RedisMessageEvent.class, "redis message"); - Skript.registerExpression(ExprVariableInChannel.class, Object.class, ExpressionType.PROPERTY, "variable %strings% in [redis] channel %string%"); + Skript.registerExpression(ExprVariableInChannel.class, Object.class, ExpressionType.PROPERTY, "variable[s] %strings% in [redis] channel %string%"); Skript.registerExpression(ExprChannel.class, String.class, ExpressionType.SIMPLE, "redis channel"); EventValues.registerEventValue(RedisMessageEvent.class, String.class, new Getter() { diff --git a/src/main/java/net/limework/rediskript/skript/elements/ExprVariableInChannel.java b/src/main/java/net/limework/rediskript/skript/elements/ExprVariableInChannel.java index 0e9ea02..071a70d 100644 --- a/src/main/java/net/limework/rediskript/skript/elements/ExprVariableInChannel.java +++ b/src/main/java/net/limework/rediskript/skript/elements/ExprVariableInChannel.java @@ -1,11 +1,12 @@ package net.limework.rediskript.skript.elements; import ch.njol.skript.classes.Changer; -import ch.njol.skript.lang.*; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.Variable; import ch.njol.skript.lang.util.SimpleExpression; import ch.njol.skript.registrations.Classes; import ch.njol.skript.variables.SerializedVariable; -import ch.njol.skript.variables.Variables; import ch.njol.util.Kleenean; import ch.njol.util.coll.CollectionUtils; import net.limework.rediskript.RediSkript; @@ -19,6 +20,13 @@ public class ExprVariableInChannel extends SimpleExpression { private Expression channel; @Override public boolean init(Expression[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parser) { + if (expressions[0] instanceof Variable) { + Variable variable = (Variable) expressions[0]; + System.out.println(variable.getName().toString()); + String var = variable.getName().toString(); + var = var.substring(1, var.length() - 1); + + } name = (Expression) expressions[0]; channel = (Expression) expressions[1]; return true; @@ -32,7 +40,7 @@ public class ExprVariableInChannel extends SimpleExpression { @Override public boolean isSingle() { - return false; + return true; } @Override @@ -42,20 +50,32 @@ public class ExprVariableInChannel extends SimpleExpression { @Override public String toString(Event event, boolean b) { - return null; + return "variable in redis channel"; } @Override public void change(Event e, Object[] changer, Changer.ChangeMode mode) { RediSkript plugin = (RediSkript) Bukkit.getPluginManager().getPlugin("RediSkript"); switch (mode) { + case ADD: case SET: - SerializedVariable.Value serialized = Classes.serialize(changer[0]); - String encoded = Base64.getEncoder().encodeToString(serialized.data); - encoded = serialized.type + "^" + encoded; - plugin.getRm().sendVariables(name.getAll(e), encoded, channel.getSingle(e)); + case REMOVE: + SerializedVariable.Value serialized; + String encoded; + String[] values = new String[changer.length+1]; + for( int i = 0; i < changer.length; i++) { + if (changer[i] != null) { + serialized = Classes.serialize(changer[i]); + encoded = Base64.getEncoder().encodeToString(serialized.data); + encoded = serialized.type + "^" + encoded; + values[i] = encoded; + } + } + String operation = mode.toString(); + System.out.println(operation); + plugin.getRm().sendVariables(name.getAll(e), values, channel.getSingle(e), operation); break; case DELETE: - plugin.getRm().sendVariables(name.getAll(e), null, channel.getSingle(e)); + plugin.getRm().sendVariables(name.getAll(e), null, channel.getSingle(e), "SET"); break; } }