From 7effb90baae95c8e177e1937cef07c02b016c026 Mon Sep 17 00:00:00 2001 From: Govindas Date: Tue, 22 Mar 2022 11:58:41 +0200 Subject: [PATCH] Support add/remove actions with numbers on variables & Update dependencies --- RediSkript-bukkit/pom.xml | 4 +- .../rediskript/managers/RedisController.java | 117 ++++++++++++++---- .../elements/ExprVariableInChannel.java | 2 +- RediSkript-core/pom.xml | 2 +- pom.xml | 6 +- 5 files changed, 99 insertions(+), 32 deletions(-) diff --git a/RediSkript-bukkit/pom.xml b/RediSkript-bukkit/pom.xml index dc99ee1..545b26c 100644 --- a/RediSkript-bukkit/pom.xml +++ b/RediSkript-bukkit/pom.xml @@ -5,7 +5,7 @@ RediSkript net.limework - 1.3.4 + 1.3.5 4.0.0 @@ -57,7 +57,7 @@ com.github.SkriptLang Skript - 2.5.3 + 2.6.1 jar diff --git a/RediSkript-bukkit/src/main/java/net/limework/rediskript/managers/RedisController.java b/RediSkript-bukkit/src/main/java/net/limework/rediskript/managers/RedisController.java index 97287cd..e0e768d 100644 --- a/RediSkript-bukkit/src/main/java/net/limework/rediskript/managers/RedisController.java +++ b/RediSkript-bukkit/src/main/java/net/limework/rediskript/managers/RedisController.java @@ -19,6 +19,7 @@ import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.logging.Level; public class RedisController extends BinaryJedisPubSub implements Runnable { @@ -134,51 +135,117 @@ public class RedisController extends BinaryJedisPubSub implements Runnable { //Transfer variables between servers - JSONArray variableNames = j.getJSONArray("Names"); + JSONArray varNames = j.getJSONArray("Names"); Object inputValue; String changeValue = null; - JSONArray variableValues = null; + JSONArray varValues = null; if (!j.isNull("Values")) { - variableValues = j.getJSONArray("Values"); + varValues = j.getJSONArray("Values"); } - for (int i = 0; i < variableNames.length(); i++) { - + for (int i = 0; i < varNames.length(); i++) { + String varName = varNames.get(i).toString(); if (j.isNull("Values")) { - //only check for SET here, because null has to be ignored in all other cases + // 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); + Variables.setVariable(varName, null, null, false); } } else { - if (!variableValues.isNull(i)) { - changeValue = variableValues.get(i).toString(); - } + if (!varValues.isNull(i)) { + changeValue = varValues.get(i).toString(); + } String[] inputs = changeValue.split("\\^", 2); inputValue = Classes.deserialize(inputs[0], Base64.getDecoder().decode(inputs[1])); switch (j.getString("Operation")) { case "ADD": - //I will add this once someone tells me how to remove from Skript variable - //because using SET operation has issues with inconvertible types (Double and Long) - //variable = (Variable) Variables.getVariable(variableNames.get(i).toString(), null, false); - // variable.change(null, (Object[]) inputValue, Changer.ChangeMode.REMOVE); + if (varName.charAt(varName.length() - 1) == '*') { + plugin.getLogger().log(Level.WARNING, "Adding to {::*} variables in RediSkript is not supported. Variable name: " + varName); + continue; + } + Object variable = Variables.getVariable(varName, null, false); + if (variable == null) { + Variables.setVariable(varName, inputValue, null, false); + } else if (variable instanceof Long) { + if (inputValue instanceof Long) { + Variables.setVariable(varName, (Long) variable + (Long) inputValue, null, false); + } else if (inputValue instanceof Double) { + + // convert Long variable to Double + variable = Double.valueOf((Long) variable); + Variables.setVariable(varName, (Double) variable + (Double) inputValue, null, false); + } else { + // Not supported input type + plugin.getLogger().log(Level.WARNING, "Unsupported add action of data type (" + inputValue.getClass().getName() + ") on variable: " + varName); + continue; + } + } else if (variable instanceof Double) { + if (inputValue instanceof Double) { + Variables.setVariable(varName, (Double) variable + (Double) inputValue, null, false); + } else if (inputValue instanceof Long) { + Variables.setVariable(varName, (Double) variable + ((Long) inputValue).doubleValue(), null, false); + } else { + // Not supported input type + plugin.getLogger().log(Level.WARNING, "Unsupported add action of data type (" + inputValue.getClass().getName() + ") on variable: " + varName); + continue; + } + } else { + // Not supported input type + plugin.getLogger().log(Level.WARNING, "Unsupported variable type in add action (" + variable.getClass().getName() + ") on variable: " + varName); + continue; + } + break; case "REMOVE": - //I will add this once someone tells me how to remove from Skript variable - //because using SET operation has issues with inconvertible types (Double and Long) - //variable = (Variable) Variables.getVariable(variableNames.get(i).toString(), null, false); - // variable.change(null, (Object[]) inputValue, Changer.ChangeMode.REMOVE); + if (varName.charAt(varName.length() - 1) == '*') { + plugin.getLogger().log(Level.WARNING, "Removing from {::*} variables in RediSkript is not supported. Variable name: " + varName); + continue; + } + variable = Variables.getVariable(varName, null, false); + if (variable == null) { + if (inputValue instanceof Long) { + Variables.setVariable(varName, -(Long) inputValue, null, false); + } else if (inputValue instanceof Double) { + Variables.setVariable(varName, -(Double) inputValue, null, false); + } else { + // Not supported input type + plugin.getLogger().log(Level.WARNING, "Unsupported remove action of data type (" + inputValue.getClass().getName() + ") on variable: " + varName); + continue; + } + } else if (variable instanceof Long) { + if (inputValue instanceof Long) { + Variables.setVariable(varName, (Long) variable - (Long) inputValue, null, false); + } else if (inputValue instanceof Double) { + + // convert Long variable to Double + variable = Double.valueOf((Long) variable); + Variables.setVariable(varName, (Double) variable - (Double) inputValue, null, false); + } else { + // Not supported input type + plugin.getLogger().log(Level.WARNING, "Unsupported remove action of data type (" + inputValue.getClass().getName() + ") on variable: " + varName); + continue; + } + } else if (variable instanceof Double) { + if (inputValue instanceof Double) { + Variables.setVariable(varName, (Double) variable - (Double) inputValue, null, false); + } else if (inputValue instanceof Long) { + Variables.setVariable(varName, (Double) variable - ((Long) inputValue).doubleValue(), null, false); + } + } else { + // Not supported input type + plugin.getLogger().log(Level.WARNING, "Unsupported variable type in remove action (" + variable.getClass().getName() + ") on variable: " + varName); + continue; + } break; case "SET": - String variableName = variableNames.get(i).toString(); - + //this is needed, because setting a {variable::*} causes weird behavior, like //1st set operation is no data, 2nd has data, etc. //if you set it to null before action, it works correctly - - if (variableName.charAt(variableName.length()-1) == '*') { - Variables.setVariable(variableName, null, null, false); - } - Variables.setVariable(variableNames.get(i).toString(), inputValue, null, false); + if (varName.charAt(varName.length() - 1) == '*') { + Variables.setVariable(varName, null, null, false); + } + Variables.setVariable(varNames.get(i).toString(), inputValue, null, false); + break; } } diff --git a/RediSkript-bukkit/src/main/java/net/limework/rediskript/skript/elements/ExprVariableInChannel.java b/RediSkript-bukkit/src/main/java/net/limework/rediskript/skript/elements/ExprVariableInChannel.java index aefd275..9c138a1 100644 --- a/RediSkript-bukkit/src/main/java/net/limework/rediskript/skript/elements/ExprVariableInChannel.java +++ b/RediSkript-bukkit/src/main/java/net/limework/rediskript/skript/elements/ExprVariableInChannel.java @@ -73,7 +73,7 @@ public class ExprVariableInChannel extends SimpleExpression { @Override public Class[] acceptChange(Changer.ChangeMode mode) { //if (mode == Changer.ChangeMode.DELETE || mode == Changer.ChangeMode.SET || mode == Changer.ChangeMode.ADD || mode == Changer.ChangeMode.REMOVE) - if (mode == Changer.ChangeMode.DELETE || mode == Changer.ChangeMode.SET) + if (mode == Changer.ChangeMode.DELETE || mode == Changer.ChangeMode.SET || mode == Changer.ChangeMode.ADD || mode == Changer.ChangeMode.REMOVE) return CollectionUtils.array(Object.class); return null; diff --git a/RediSkript-core/pom.xml b/RediSkript-core/pom.xml index 0570b08..8ff388c 100644 --- a/RediSkript-core/pom.xml +++ b/RediSkript-core/pom.xml @@ -5,7 +5,7 @@ RediSkript net.limework - 1.3.4 + 1.3.5 4.0.0 diff --git a/pom.xml b/pom.xml index 4853884..fa0d3fb 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ net.limework RediSkript - 1.3.4 + 1.3.5 RediSkript-core RediSkript-bukkit @@ -48,7 +48,7 @@ org.cryptomator siv-mode - 1.4.1 + 1.4.4 @@ -57,4 +57,4 @@ 2.6.2 - \ No newline at end of file +