From 1b151dfbbe9b68c600403111c89ace58e3450fe8 Mon Sep 17 00:00:00 2001 From: Govindass Date: Wed, 25 Nov 2020 13:53:52 +0200 Subject: [PATCH] Allow sending multiple redis messages in one go --- .../rediskript/managers/RedisManager.java | 17 ++++++++++++----- .../skript/elements/EffSendMessage.java | 9 +++++---- .../elements/EffSetVariableInChannel.java | 5 +++++ 3 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 src/main/java/net/limework/rediskript/skript/elements/EffSetVariableInChannel.java diff --git a/src/main/java/net/limework/rediskript/managers/RedisManager.java b/src/main/java/net/limework/rediskript/managers/RedisManager.java index ad85352..794481a 100644 --- a/src/main/java/net/limework/rediskript/managers/RedisManager.java +++ b/src/main/java/net/limework/rediskript/managers/RedisManager.java @@ -7,6 +7,7 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.configuration.Configuration; import org.cryptomator.siv.UnauthenticCiphertextException; +import org.json.JSONArray; import org.json.JSONObject; import redis.clients.jedis.BinaryJedis; import redis.clients.jedis.BinaryJedisPubSub; @@ -131,13 +132,19 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable { if (receivedMessage != null) { JSONObject j = new JSONObject(receivedMessage); //System.out.println("Message got from channel: "+channel +" and the Message: " +json.toString()); - RedisMessageEvent event = new RedisMessageEvent(channelString, j.getString("Message"), j.getLong("Date")); + JSONArray messages = j.getJSONArray("Messages"); + RedisMessageEvent event; + for (int i = 0 ; i < messages.length(); i++) { + + System.out.println(messages.get(i).toString()); + event = new RedisMessageEvent(channelString, messages.get(i).toString(), j.getLong("Date")); + if (plugin.isEnabled()) { + RedisMessageEvent finalEvent = event; + Bukkit.getScheduler().runTask(plugin, () -> plugin.getServer().getPluginManager().callEvent(finalEvent)); + } + } //if plugin is disabling, don't call events anymore - - if (plugin.isEnabled()) { - Bukkit.getScheduler().runTask(plugin, () -> plugin.getServer().getPluginManager().callEvent(event)); - } } } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/net/limework/rediskript/skript/elements/EffSendMessage.java b/src/main/java/net/limework/rediskript/skript/elements/EffSendMessage.java index f0ce436..d823052 100644 --- a/src/main/java/net/limework/rediskript/skript/elements/EffSendMessage.java +++ b/src/main/java/net/limework/rediskript/skript/elements/EffSendMessage.java @@ -10,6 +10,7 @@ import net.limework.rediskript.managers.RedisManager; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.event.Event; +import org.json.JSONArray; import org.json.JSONObject; import redis.clients.jedis.BinaryJedis; import redis.clients.jedis.exceptions.JedisConnectionException; @@ -18,7 +19,7 @@ import java.nio.charset.StandardCharsets; public class EffSendMessage extends Effect { static { - Skript.registerEffect(EffSendMessage.class, "send redis message %string% to [channel] %string%"); + Skript.registerEffect(EffSendMessage.class, "send redis message[s] %strings% to [channel] %string%"); } @@ -31,10 +32,10 @@ public class EffSendMessage extends Effect { RediSkript plugin = (RediSkript) Bukkit.getPluginManager().getPlugin("RediSkript"); - String message = this.message.getSingle(event); + String[] message = this.message.getAll(event); String channel = this.channel.getSingle(event); - if (message == null) { + if (message[0] == null) { Bukkit.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&2[&aRediSkript&a] &cRedis message was empty. Please check your code.")); return; } @@ -44,7 +45,7 @@ public class EffSendMessage extends Effect { } assert plugin != null; JSONObject json = new JSONObject(); - json.put("Message", message); + json.put("Messages", new JSONArray(message)); json.put("Type", "Skript"); json.put("Date", System.currentTimeMillis()); //for unique string every time & PING calculations byte[] msg; diff --git a/src/main/java/net/limework/rediskript/skript/elements/EffSetVariableInChannel.java b/src/main/java/net/limework/rediskript/skript/elements/EffSetVariableInChannel.java new file mode 100644 index 0000000..65a70b5 --- /dev/null +++ b/src/main/java/net/limework/rediskript/skript/elements/EffSetVariableInChannel.java @@ -0,0 +1,5 @@ +package net.limework.rediskript.skript.elements; + +public class EffSetVariableInChannel { + +}