From ac6fe6cb99e606b9eedebd339593dd02b1d1060f Mon Sep 17 00:00:00 2001 From: mohammed jasem alaajel Date: Mon, 25 Apr 2022 07:03:33 +0400 Subject: [PATCH] 1.3.6-SNAPSHOT && updated deps && moved jedis to the parent && due removal of JedisBinary, Jedis has method of that class now so its easy drop replacement && fixed jedis wont connect if password is not empty? && adding default values if config somehow is empty or field not there --- RediSkript-bukkit/pom.xml | 7 +- .../rediskript/managers/RedisController.java | 336 +++++++++--------- RediSkript-core/pom.xml | 13 +- pom.xml | 11 +- 4 files changed, 184 insertions(+), 183 deletions(-) diff --git a/RediSkript-bukkit/pom.xml b/RediSkript-bukkit/pom.xml index 545b26c..493d115 100644 --- a/RediSkript-bukkit/pom.xml +++ b/RediSkript-bukkit/pom.xml @@ -5,7 +5,7 @@ RediSkript net.limework - 1.3.5 + 1.3.6-SNAPSHOT 4.0.0 @@ -73,11 +73,6 @@ 1.16.5-R0.1-SNAPSHOT provided - - redis.clients - jedis - 3.8.0 - net.limework RediSkript-core 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 e0e768d..f5e1294 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 @@ -47,18 +47,30 @@ public class RedisController extends BinaryJedisPubSub implements Runnable { int maxConnections = config.getInt("Redis.MaxConnections"); //do not allow less than 2 max connections as that causes issues - if (maxConnections < 2) { maxConnections = 2; } + if (maxConnections < 2) { + maxConnections = 2; + } JConfig.setMaxTotal(maxConnections); JConfig.setMaxIdle(maxConnections); JConfig.setMinIdle(1); JConfig.setBlockWhenExhausted(true); - this.jedisPool = new JedisPool(JConfig, - config.getString("Redis.Host"), - config.getInt("Redis.Port"), - config.getInt("Redis.TimeOut"), - config.getString("Redis.Password"), - config.getBoolean("Redis.useTLS")); + final String password = config.getString("Redis.Password", ""); + if (password.isEmpty()) { + this.jedisPool = new JedisPool(JConfig, + config.getString("Redis.Host", "127.0.0.1"), + config.getInt("Redis.Port", 6379), + config.getInt("Redis.TimeOut", 9000), + config.getBoolean("Redis.useTLS", false)); + } else { + this.jedisPool = new JedisPool(JConfig, + config.getString("Redis.Host", "127.0.0.1"), + config.getInt("Redis.Port", 6379), + config.getInt("Redis.TimeOut", 9000), + password, + config.getBoolean("Redis.useTLS", false)); + } + encryption = new Encryption(config.getBoolean("Redis.EncryptMessages"), config.getString("Redis.EncryptionKey"), config.getString("Redis.MacKey")); @@ -102,162 +114,162 @@ public class RedisController extends BinaryJedisPubSub implements Runnable { } @Override - public void onMessage(byte[] channel, byte[] message){ - String channelString = new String(channel, StandardCharsets.UTF_8); - String receivedMessage = null; - try { - //if encryption is enabled, decrypt the message, else just convert binary to string - if (this.encryption.isEncryptionEnabled()) { - try { - receivedMessage = encryption.decrypt(message); - } catch (UnauthenticCiphertextException | IllegalBlockSizeException e) { - e.printStackTrace(); - } - - } else { - //encryption is disabled, so let's just get the string - receivedMessage = new String(message, StandardCharsets.UTF_8); + public void onMessage(byte[] channel, byte[] message) { + String channelString = new String(channel, StandardCharsets.UTF_8); + String receivedMessage = null; + try { + //if encryption is enabled, decrypt the message, else just convert binary to string + if (this.encryption.isEncryptionEnabled()) { + try { + receivedMessage = encryption.decrypt(message); + } catch (UnauthenticCiphertextException | IllegalBlockSizeException e) { + e.printStackTrace(); } - if (receivedMessage != null) { - JSONObject j = new JSONObject(receivedMessage); - if (j.get("Type").equals("Skript")) { - JSONArray messages = j.getJSONArray("Messages"); - RedisMessageEvent event; - for (int i = 0; i < messages.length(); i++) { - event = new RedisMessageEvent(channelString, messages.get(i).toString(), j.getLong("Date")); - //if plugin is disabling, don't call events anymore - if (plugin.isEnabled()) { - RedisMessageEvent finalEvent = event; - Bukkit.getScheduler().runTask(plugin, () -> plugin.getServer().getPluginManager().callEvent(finalEvent)); - } - } - } else if (j.get("Type").equals("SkriptVariables")) { - //Transfer variables between servers - - JSONArray varNames = j.getJSONArray("Names"); - Object inputValue; - String changeValue = null; - JSONArray varValues = null; - if (!j.isNull("Values")) { - varValues = j.getJSONArray("Values"); - } - 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 - if (j.getString("Operation").equals("SET")) { - Variables.setVariable(varName, null, null, false); - } - - } else { - 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": - 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": - 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": - - //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 (varName.charAt(varName.length() - 1) == '*') { - Variables.setVariable(varName, null, null, false); - } - Variables.setVariable(varNames.get(i).toString(), inputValue, null, false); - break; - - } - } - } - } - } - } catch (Exception e) { - plugin.sendErrorLogs("&cI got a message that was empty from channel " + channelString + " please check your code that you used to send the message. Message content:"); - plugin.sendErrorLogs(receivedMessage); - e.printStackTrace(); + } else { + //encryption is disabled, so let's just get the string + receivedMessage = new String(message, StandardCharsets.UTF_8); } + if (receivedMessage != null) { + JSONObject j = new JSONObject(receivedMessage); + if (j.get("Type").equals("Skript")) { + JSONArray messages = j.getJSONArray("Messages"); + RedisMessageEvent event; + for (int i = 0; i < messages.length(); i++) { + event = new RedisMessageEvent(channelString, messages.get(i).toString(), j.getLong("Date")); + //if plugin is disabling, don't call events anymore + if (plugin.isEnabled()) { + RedisMessageEvent finalEvent = event; + Bukkit.getScheduler().runTask(plugin, () -> plugin.getServer().getPluginManager().callEvent(finalEvent)); + } + } + } else if (j.get("Type").equals("SkriptVariables")) { + + //Transfer variables between servers + + JSONArray varNames = j.getJSONArray("Names"); + Object inputValue; + String changeValue = null; + JSONArray varValues = null; + if (!j.isNull("Values")) { + varValues = j.getJSONArray("Values"); + } + 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 + if (j.getString("Operation").equals("SET")) { + Variables.setVariable(varName, null, null, false); + } + + } else { + 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": + 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": + 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": + + //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 (varName.charAt(varName.length() - 1) == '*') { + Variables.setVariable(varName, null, null, false); + } + Variables.setVariable(varNames.get(i).toString(), inputValue, null, false); + break; + + } + } + } + } + } + } catch (Exception e) { + plugin.sendErrorLogs("&cI got a message that was empty from channel " + channelString + " please check your code that you used to send the message. Message content:"); + plugin.sendErrorLogs(receivedMessage); + e.printStackTrace(); } + } public void sendMessage(String[] message, String channel) { JSONObject json = new JSONObject(); @@ -293,17 +305,17 @@ public class RedisController extends BinaryJedisPubSub implements Runnable { //so to avoid issues, it's best to do it always on separate thread if (plugin.isEnabled()) { plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> { - try (BinaryJedis j = jedisPool.getResource()) { + try (Jedis j = jedisPool.getResource()) { j.publish(channel.getBytes(StandardCharsets.UTF_8), message); } catch (Exception e) { plugin.sendErrorLogs("Error sending redis message!"); e.printStackTrace(); - } + } }); } else { //execute sending of redis message on the main thread if plugin is disabling //so it can still process the sending - try (BinaryJedis j = jedisPool.getResource()) { + try (Jedis j = jedisPool.getResource()) { j.publish(channel.getBytes(StandardCharsets.UTF_8), message); } catch (Exception e) { e.printStackTrace(); diff --git a/RediSkript-core/pom.xml b/RediSkript-core/pom.xml index 8ff388c..e2e151b 100644 --- a/RediSkript-core/pom.xml +++ b/RediSkript-core/pom.xml @@ -5,21 +5,10 @@ RediSkript net.limework - 1.3.5 + 1.3.6-SNAPSHOT 4.0.0 RediSkript-core - - - - redis.clients - jedis - 3.8.0 - provided - - - - diff --git a/pom.xml b/pom.xml index fa0d3fb..e838ff6 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ net.limework RediSkript - 1.3.5 + 1.3.6-SNAPSHOT RediSkript-core RediSkript-bukkit @@ -43,7 +43,7 @@ org.json json - 20210307 + 20220320 org.cryptomator @@ -54,7 +54,12 @@ org.apache.commons commons-pool2 - 2.6.2 + 2.11.1 + + + redis.clients + jedis + 4.2.2