Some more work on variables (Still very unfinished)

This commit is contained in:
Govindass 2020-11-26 17:05:07 +02:00
parent 6f600cc96e
commit c63495e201
4 changed files with 93 additions and 26 deletions

View File

@ -1,6 +1,6 @@
<component name="ArtifactManager">
<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">
<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="/" />

View File

@ -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) {
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);
}

View File

@ -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<String, RedisMessageEvent>() {

View File

@ -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<Object> {
private Expression<String> 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<String>) expressions[0];
channel = (Expression<String>) expressions[1];
return true;
@ -32,7 +40,7 @@ public class ExprVariableInChannel extends SimpleExpression<Object> {
@Override
public boolean isSingle() {
return false;
return true;
}
@Override
@ -42,20 +50,32 @@ public class ExprVariableInChannel extends SimpleExpression<Object> {
@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);
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;
plugin.getRm().sendVariables(name.getAll(e), encoded, channel.getSingle(e));
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;
}
}