Allow sending multiple redis messages in one go

This commit is contained in:
Govindass 2020-11-25 13:53:52 +02:00
parent afdb0c3e59
commit 1b151dfbbe
3 changed files with 22 additions and 9 deletions

View File

@ -7,6 +7,7 @@ import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.configuration.Configuration; import org.bukkit.configuration.Configuration;
import org.cryptomator.siv.UnauthenticCiphertextException; import org.cryptomator.siv.UnauthenticCiphertextException;
import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import redis.clients.jedis.BinaryJedis; import redis.clients.jedis.BinaryJedis;
import redis.clients.jedis.BinaryJedisPubSub; import redis.clients.jedis.BinaryJedisPubSub;
@ -131,13 +132,19 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
if (receivedMessage != null) { if (receivedMessage != null) {
JSONObject j = new JSONObject(receivedMessage); JSONObject j = new JSONObject(receivedMessage);
//System.out.println("Message got from channel: "+channel +" and the Message: " +json.toString()); //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 is disabling, don't call events anymore
if (plugin.isEnabled()) {
Bukkit.getScheduler().runTask(plugin, () -> plugin.getServer().getPluginManager().callEvent(event));
}
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -10,6 +10,7 @@ import net.limework.rediskript.managers.RedisManager;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import redis.clients.jedis.BinaryJedis; import redis.clients.jedis.BinaryJedis;
import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisConnectionException;
@ -18,7 +19,7 @@ import java.nio.charset.StandardCharsets;
public class EffSendMessage extends Effect { public class EffSendMessage extends Effect {
static { 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"); 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); 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.")); Bukkit.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&2[&aRediSkript&a] &cRedis message was empty. Please check your code."));
return; return;
} }
@ -44,7 +45,7 @@ public class EffSendMessage extends Effect {
} }
assert plugin != null; assert plugin != null;
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("Message", message); json.put("Messages", new JSONArray(message));
json.put("Type", "Skript"); json.put("Type", "Skript");
json.put("Date", System.currentTimeMillis()); //for unique string every time & PING calculations json.put("Date", System.currentTimeMillis()); //for unique string every time & PING calculations
byte[] msg; byte[] msg;

View File

@ -0,0 +1,5 @@
package net.limework.rediskript.skript.elements;
public class EffSetVariableInChannel {
}