From 0f3ab74d984ba86cea610d123d130b3788e80c13 Mon Sep 17 00:00:00 2001 From: Govindass Date: Tue, 26 Jan 2021 10:51:31 +0200 Subject: [PATCH] Various fixes and improvements --- RediSkript.iml | 5 +- pom.xml | 4 +- .../net/limework/rediskript/RediSkript.java | 6 +- .../rediskript/managers/RedisController.java | 172 +++++++++--------- .../skript/elements/ExprChannel.java | 4 +- .../elements/ExprVariableInChannel.java | 5 +- src/main/resources/config.yml | 4 +- 7 files changed, 95 insertions(+), 105 deletions(-) diff --git a/RediSkript.iml b/RediSkript.iml index 008b982..ba6e0fc 100644 --- a/RediSkript.iml +++ b/RediSkript.iml @@ -17,6 +17,7 @@ + @@ -64,9 +65,9 @@ - + - + diff --git a/pom.xml b/pom.xml index b19622e..f80365a 100644 --- a/pom.xml +++ b/pom.xml @@ -93,13 +93,13 @@ redis.clients jedis - 3.4.1 + 3.5.1 compile org.json json - 20190722 + 20201115 compile diff --git a/src/main/java/net/limework/rediskript/RediSkript.java b/src/main/java/net/limework/rediskript/RediSkript.java index cb3c86d..fee04d3 100644 --- a/src/main/java/net/limework/rediskript/RediSkript.java +++ b/src/main/java/net/limework/rediskript/RediSkript.java @@ -79,7 +79,11 @@ public class RediSkript extends JavaPlugin { @Override public void onDisable() { - if (redisController != null) redisController.shutdown(); + if (redisController != null) { + redisController.shutdown(); + } + getServer().getPluginCommand("reloadredis").setExecutor(null); + } public RedisController getRC() { diff --git a/src/main/java/net/limework/rediskript/managers/RedisController.java b/src/main/java/net/limework/rediskript/managers/RedisController.java index 5b68658..242d995 100644 --- a/src/main/java/net/limework/rediskript/managers/RedisController.java +++ b/src/main/java/net/limework/rediskript/managers/RedisController.java @@ -6,7 +6,6 @@ import net.limework.rediskript.RediSkript; 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; import org.bukkit.scheduler.BukkitTask; import org.cryptomator.siv.UnauthenticCiphertextException; @@ -35,7 +34,6 @@ public class RedisController extends BinaryJedisPubSub implements Runnable { private final AtomicBoolean isConnectionBroken; private final AtomicBoolean isConnecting; private final RediSkript plugin; - private final boolean debugMode = false; //todo: will be later in the config in future release. private final BukkitTask ConnectionTask; @@ -44,9 +42,10 @@ public class RedisController extends BinaryJedisPubSub implements Runnable { Configuration config = plugin.getConfig(); JedisPoolConfig JConfig = new JedisPoolConfig(); int maxConnections = config.getInt("Redis.MaxConnections"); - if (maxConnections < 2) { - maxConnections = 2; - } + + //do not allow less than 2 max connections as that causes issues + if (maxConnections < 2) { maxConnections = 2; } + JConfig.setMaxTotal(maxConnections); JConfig.setMaxIdle(maxConnections); JConfig.setMinIdle(1); @@ -70,121 +69,115 @@ public class RedisController extends BinaryJedisPubSub implements Runnable { if (!isConnectionBroken.get() || isConnecting.get()) { return; } - plugin.sendLogs("Connecting to redis......"); + plugin.sendLogs("Connecting to Redis server..."); isConnecting.set(true); try (Jedis jedis = jedisPool.getResource()) { isConnectionBroken.set(false); - plugin.sendLogs("&aConnection to redis has established!"); + plugin.sendLogs("&aConnection to Redis server has established! Success!"); jedis.subscribe(this, channelsInByte); } catch (Exception e) { isConnecting.set(false); isConnectionBroken.set(true); - plugin.sendErrorLogs("Connection has &lFAILED &cor Unable to connect to redis retrying to make connection..."); - if (debugMode) { - e.printStackTrace(); - } + plugin.sendErrorLogs("Connection to Redis server has failed! Please check your details in the configuration."); + e.printStackTrace(); } } public void shutdown() { ConnectionTask.cancel(); - try { - this.unsubscribe(); - } catch (Exception e) { - plugin.sendErrorLogs("Something went wrong during unsubscribing..."); - if (debugMode) { + if (this.isSubscribed()) { + try { + this.unsubscribe(); + } catch (Exception e) { + plugin.sendErrorLogs("Something went wrong during unsubscribing..."); e.printStackTrace(); } } - jedisPool.close(); } @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(); + 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); } - - } 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)); + 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")) { + } else if (j.get("Type").equals("SkriptVariables")) { - //Transfer variables between servers + //Transfer variables between servers - JSONArray variableNames = j.getJSONArray("Names"); - Object inputValue; - String changeValue = null; - JSONArray variableValues = null; - if (!j.isNull("Values")) { - variableValues = j.getJSONArray("Values"); - } - for (int i = 0; i < variableNames.length(); i++) { + JSONArray variableNames = j.getJSONArray("Names"); + 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 (j.isNull("Values")) { - //only check for SET here, because null has to be ignored in all other cases + 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); - } + 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])); - 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); - 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); - break; - case "SET": - Variables.setVariable(variableNames.get(i).toString(), inputValue, 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])); + 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); + 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); + break; + case "SET": + Variables.setVariable(variableNames.get(i).toString(), inputValue, null, false); + } } } } } - } - } 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:"); - if (debugMode) { - e.printStackTrace(); + } 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(); json.put("Messages", new JSONArray(message)); @@ -222,12 +215,9 @@ public class RedisController extends BinaryJedisPubSub implements Runnable { try (BinaryJedis j = jedisPool.getResource()) { j.publish(channel.getBytes(StandardCharsets.UTF_8), message); } catch (Exception e) { - System.out.println("Error sending redis message!"); - if (debugMode) { - e.printStackTrace(); + plugin.sendErrorLogs("Error sending redis message!"); + e.printStackTrace(); } - } - }); } else { //execute sending of redis message on the main thread if plugin is disabling diff --git a/src/main/java/net/limework/rediskript/skript/elements/ExprChannel.java b/src/main/java/net/limework/rediskript/skript/elements/ExprChannel.java index 86625b4..489a794 100644 --- a/src/main/java/net/limework/rediskript/skript/elements/ExprChannel.java +++ b/src/main/java/net/limework/rediskript/skript/elements/ExprChannel.java @@ -25,9 +25,7 @@ public class ExprChannel extends SimpleExpression { } @Override - public String toString(Event event, boolean b) { - return "redis channel"; - } + public String toString(Event event, boolean b) { return "redis channel"; } @Override public boolean init(final Expression[] expressions, final int matchedPattern, final Kleenean isDelayed, final SkriptParser.ParseResult parseResult) { 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 757538c..aefd275 100644 --- a/src/main/java/net/limework/rediskript/skript/elements/ExprVariableInChannel.java +++ b/src/main/java/net/limework/rediskript/skript/elements/ExprVariableInChannel.java @@ -1,12 +1,9 @@ package net.limework.rediskript.skript.elements; -import ch.njol.skript.Skript; import ch.njol.skript.classes.Changer; 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.log.ErrorQuality; import ch.njol.skript.registrations.Classes; import ch.njol.skript.variables.SerializedVariable; import ch.njol.util.Kleenean; @@ -49,7 +46,7 @@ public class ExprVariableInChannel extends SimpleExpression { } @Override public void change(Event e, Object[] changer, Changer.ChangeMode mode) { - RediSkript plugin = (RediSkript) Bukkit.getPluginManager().getPlugin("RediSkript"); + RediSkript plugin = RediSkript.getPlugin(RediSkript.class); switch (mode) { case ADD: case SET: diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 50afa64..796dac8 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -8,8 +8,8 @@ Redis: #the default Redis port Port: 6379 #time out in milliseconds, how long it should take before it decides that it is unable to connect when sending a message - #90000 = 90 seconds - TimeOut: 90000 + #9000 = 9 seconds + TimeOut: 9000 #also known as SSL, only use this if you're running Redis 6.0.6 or higher, older versions will not work correctly #it encrypts your traffic and makes data exchange between distant servers completely secure useTLS: false