Some more work on variables (Still very unfinished)

This commit is contained in:
Govindas 2020-11-26 17:05:07 +02:00
parent 22e9cfdd40
commit cb7c2c1de8
4 changed files with 93 additions and 26 deletions

View File

@ -1,6 +1,6 @@
<component name="ArtifactManager"> <component name="ArtifactManager">
<artifact type="jar" build-on-make="true" name="RediSkript:jar"> <artifact type="jar" build-on-make="true" name="RediSkript:jar">
<output-path>$PROJECT_DIR$/out/artifacts/RediSkript_jar</output-path> <output-path>$PROJECT_DIR$/../../Govindo/testserver/plugins</output-path>
<root id="archive" name="RediSkript.jar"> <root id="archive" name="RediSkript.jar">
<element id="module-output" name="RediSkript" /> <element id="module-output" name="RediSkript" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/cryptomator/siv-mode/1.4.0/siv-mode-1.4.0.jar" path-in-jar="/" /> <element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/cryptomator/siv-mode/1.4.0/siv-mode-1.4.0.jar" path-in-jar="/" />

View File

@ -3,8 +3,8 @@ package net.limework.rediskript.managers;
import ch.njol.skript.registrations.Classes; import ch.njol.skript.registrations.Classes;
import ch.njol.skript.variables.Variables; import ch.njol.skript.variables.Variables;
import net.limework.rediskript.RediSkript; import net.limework.rediskript.RediSkript;
import net.limework.rediskript.events.RedisMessageEvent;
import net.limework.rediskript.data.Encryption; import net.limework.rediskript.data.Encryption;
import net.limework.rediskript.events.RedisMessageEvent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.configuration.Configuration; import org.bukkit.configuration.Configuration;
@ -149,22 +149,65 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
} }
} }
} else if (j.get("Type").equals("SkriptVariables")) { } else if (j.get("Type").equals("SkriptVariables")) {
//Transfer variables between servers
JSONArray variableNames = j.getJSONArray("Names"); JSONArray variableNames = j.getJSONArray("Names");
boolean delete = false; Object inputValue;
Object inputValue = null; String changeValue = null;
if (j.isNull("Value")) { JSONArray variableValues = null;
delete = true; if (!j.isNull("Values")) {
} else { variableValues = j.getJSONArray("Values");
String input = j.getString("Value");
String [] inputs = input.split("\\^", 2);
inputValue = Classes.deserialize(inputs[0], Base64.getDecoder().decode(inputs[1]));
} }
for (int i = 0; i < variableNames.length(); i++) { 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 { } 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); 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 json.put("Date", System.currentTimeMillis()); //for unique string every time & PING calculations
finishSendMessage(json, channel); 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(); JSONObject json = new JSONObject();
json.put("Names", new JSONArray(variableNames)); 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("Type", "SkriptVariables");
json.put("Date", System.currentTimeMillis()); //for unique string every time & PING calculations json.put("Date", System.currentTimeMillis()); //for unique string every time & PING calculations
json.put("Operation", operation);
finishSendMessage(json, channel); finishSendMessage(json, channel);
} }

View File

@ -19,7 +19,7 @@ public class SkriptHook {
try { try {
addon.loadClasses("net.limework.rediskript.skript", "elements"); addon.loadClasses("net.limework.rediskript.skript", "elements");
Skript.registerEvent("redis message", EvtRedis.class, RedisMessageEvent.class, "redis message"); 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"); Skript.registerExpression(ExprChannel.class, String.class, ExpressionType.SIMPLE, "redis channel");
EventValues.registerEventValue(RedisMessageEvent.class, String.class, new Getter<String, RedisMessageEvent>() { EventValues.registerEventValue(RedisMessageEvent.class, String.class, new Getter<String, RedisMessageEvent>() {

View File

@ -1,11 +1,12 @@
package net.limework.rediskript.skript.elements; package net.limework.rediskript.skript.elements;
import ch.njol.skript.classes.Changer; 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.lang.util.SimpleExpression;
import ch.njol.skript.registrations.Classes; import ch.njol.skript.registrations.Classes;
import ch.njol.skript.variables.SerializedVariable; import ch.njol.skript.variables.SerializedVariable;
import ch.njol.skript.variables.Variables;
import ch.njol.util.Kleenean; import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils; import ch.njol.util.coll.CollectionUtils;
import net.limework.rediskript.RediSkript; import net.limework.rediskript.RediSkript;
@ -19,6 +20,13 @@ public class ExprVariableInChannel extends SimpleExpression<Object> {
private Expression<String> channel; private Expression<String> channel;
@Override @Override
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parser) { 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<String>) expressions[0]; name = (Expression<String>) expressions[0];
channel = (Expression<String>) expressions[1]; channel = (Expression<String>) expressions[1];
return true; return true;
@ -32,7 +40,7 @@ public class ExprVariableInChannel extends SimpleExpression<Object> {
@Override @Override
public boolean isSingle() { public boolean isSingle() {
return false; return true;
} }
@Override @Override
@ -42,20 +50,32 @@ public class ExprVariableInChannel extends SimpleExpression<Object> {
@Override @Override
public String toString(Event event, boolean b) { public String toString(Event event, boolean b) {
return null; return "variable in redis channel";
} }
@Override @Override
public void change(Event e, Object[] changer, Changer.ChangeMode mode) { public void change(Event e, Object[] changer, Changer.ChangeMode mode) {
RediSkript plugin = (RediSkript) Bukkit.getPluginManager().getPlugin("RediSkript"); RediSkript plugin = (RediSkript) Bukkit.getPluginManager().getPlugin("RediSkript");
switch (mode) { switch (mode) {
case ADD:
case SET: case SET:
SerializedVariable.Value serialized = Classes.serialize(changer[0]); case REMOVE:
String encoded = Base64.getEncoder().encodeToString(serialized.data); SerializedVariable.Value serialized;
encoded = serialized.type + "^" + encoded; String encoded;
plugin.getRm().sendVariables(name.getAll(e), encoded, channel.getSingle(e)); 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; break;
case DELETE: case DELETE:
plugin.getRm().sendVariables(name.getAll(e), null, channel.getSingle(e)); plugin.getRm().sendVariables(name.getAll(e), null, channel.getSingle(e), "SET");
break; break;
} }
} }