15 Commits
1.2.3 ... 1.3.1

Author SHA1 Message Date
Govindass
2d5d281fe4 Finally fixed connection leak in /reloadredis !!! 2020-12-03 13:07:27 +02:00
Govindass
5699b9445f Improve code quality 2020-12-03 11:55:45 +02:00
Govindass
053bb04918 Remove unnecessary EventHandler 2020-12-03 11:13:03 +02:00
Govindas
2140cd67ff Update README.md 2020-11-28 10:14:19 +02:00
Govindas
e9ae0bd50b Update README.md 2020-11-28 10:12:11 +02:00
Govindass
6ea120a85a Optimizations 2020-11-28 10:02:14 +02:00
Govindass
01525e4d63 Merge branch 'master' of https://github.com/Limework/RediSkript 2020-11-26 17:05:11 +02:00
Govindass
c63495e201 Some more work on variables (Still very unfinished) 2020-11-26 17:05:07 +02:00
Govindas
265dbfa84d Remove to-do list from readme (refer to issues) 2020-11-25 17:07:30 +02:00
Govindass
6f600cc96e Add cross-server variable editing (I'll make it use {} instead of strings later) 2020-11-25 17:01:11 +02:00
Govindass
1b151dfbbe Allow sending multiple redis messages in one go 2020-11-25 13:53:52 +02:00
Govindass
afdb0c3e59 Fix rare error on disable 2020-11-25 13:18:23 +02:00
Govindass
ca64ec19db Fix wrong prefix coloring in command 2020-11-12 12:36:12 +02:00
Govindass
c0bd63e3cd test 2020-11-03 12:23:00 +02:00
Govindass
705ce16f5d Fix wrong class name & Fully fix sending messages while disabled 2020-11-03 11:29:06 +02:00
13 changed files with 352 additions and 148 deletions

2
.gitignore vendored
View File

@@ -1,4 +1,4 @@
target
out
.idea/workspace.xml
.idea
compile

View File

@@ -1,6 +1,6 @@
<component name="ArtifactManager">
<artifact type="jar" build-on-make="true" name="RediSkript:jar">
<output-path>$PROJECT_DIR$/out/artifacts/RediSkript_jar</output-path>
<output-path>$PROJECT_DIR$/../../Govindo/testserver/plugins</output-path>
<root id="archive" name="RediSkript.jar">
<element id="module-output" name="RediSkript" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/cryptomator/siv-mode/1.4.0/siv-mode-1.4.0.jar" path-in-jar="/" />

View File

@@ -1,6 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="PaperMC" />
<option name="name" value="PaperMC" />
<option name="url" value="https://repo.destroystokyo.com/repository/maven-public/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
@@ -16,6 +21,11 @@
<option name="name" value="commons-pool2" />
<option name="url" value="https://mvnrepository.com/artifact/org.apache.commons/commons-pool2" />
</remote-repository>
<remote-repository>
<option name="id" value="sk89q" />
<option name="name" value="sk89q" />
<option name="url" value="http://maven.sk89q.com/repo" />
</remote-repository>
<remote-repository>
<option name="id" value="spigot-repo" />
<option name="name" value="spigot-repo" />

View File

@@ -1,14 +1,11 @@
**To-do List**
1. Fix memory leak of /reloadredis (Not yet known how to fix it, it can be easily noticed by changing IP addresses and using NetworkInterceptor plugin, it'll show connections still being done to old IP address, aswell as the new one, even if it doesn't use old IP address for anything.)
RediSkript allows you to communicate between your servers with use of Redis, it's very fast and easy to use.
You can transfer any data in the form of text between your servers, you can program it to execute a set of instructions on the server depending on the redis message, etc. This can be used for making scripts that sync data between all servers and much more!
It is originally developed by the team of Govindas Limework developers and is now maintained only by Govindas.
Example:
Redis Message:
```
on redis message:
if redis channel is "world":
@@ -20,8 +17,26 @@ command /sendredis <text> <text>:
send redis message arg 1 to channel arg 2
send redis message "hello world!" to channel "world"
```
Managing variables:
```
set variables "test::1", "test::2", "test::3" in channel "global" to 100
#then use this in any server that listens to "global" redis channel and was online when the above line was executed:
send "%{test::*}%" #outputs 100, 100 and 100
and that's all there is to this addon! You only need to have matching configuration in every server for communication and a Redis server to connect to. I recommend using VPS for hosting redis server, I personally use VPS from humbleservers.com.
delete variables "test::*" in channel "global"
set variable "test::%uuid of player%" in channel "playerdata" to tool of player
#then you can in any server that is listening to "playerdata" channel and was online when the above line was executed:
give {test::%uuid of player%} to player
```
Syntax:
```
variable[s] %strings% in [redis] [channel] %string%
```
There is only one command: /reloadredis it fully reloads the configuration, you can reload IP, password, channels and everything else.
You only need to have matching configuration in every server for communication and a Redis server to connect to. I recommend using VPS for hosting redis server, I personally use VPS from humbleservers.com.
Configuration:
```

View File

@@ -11,10 +11,56 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.github.skriptlang:Skript:2.4.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.destroystokyo.paper:paper-api:1.15-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.findbugs:jsr305:1.3.9" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.googlecode.json-simple:json-simple:1.1.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: junit:junit:4.10" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.ow2.asm:asm:7.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.ow2.asm:asm-commons:7.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.ow2.asm:asm-tree:7.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.ow2.asm:asm-analysis:7.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.eclipse.jdt:org.eclipse.jdt.annotation:1.1.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.findbugs:findbugs:3.0.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.jcip:jcip-annotations:1.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.findbugs:bcel-findbugs:6.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.findbugs:jFormatString:2.0.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: dom4j:dom4j:1.6.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: xml-apis:xml-apis:1.0.b2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.ow2.asm:asm-debug-all:5.0.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.apple:AppleJavaExtensions:1.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: jaxen:jaxen:1.1.6" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.sk89q.worldguard:worldguard-legacy:7.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.bukkit:bukkit:1.13-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.sk89q.worldedit:worldedit-bukkit:7.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.sk89q.worldedit:worldedit-core:7.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.sk89q.worldedit.worldedit-libs:core:7.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: de.schlichtherle:truezip:6.8.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: rhino:js:1.7R2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.sk89q.worldedit.worldedit-libs:bukkit:7.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.logging.log4j:log4j-slf4j-impl:2.8.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.logging.log4j:log4j-api:2.8.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.sk89q.worldguard:worldguard-core:7.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.sk89q.intake:intake:4.2-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.sk89q:squirrelid:0.2.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.flywaydb:flyway-core:3.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.khelekore:prtree:1.5.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.sk89q:commandbook:2.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.bstats:bstats-bukkit:1.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.github.marcelo-mason:PreciousStones:24e3d4bf67b7240ae36b32be10e99d4091938c5c" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: ant-contrib:ant-contrib:1.0b3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.milkbowl.vault:Vault:1.7.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.spigotmc:spigot-api:1.16.2-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-lang:commons-lang:2.6" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.guava:guava:21.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-chat:1.16-R0.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.yaml:snakeyaml:1.26" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.6" level="project" />
<orderEntry type="library" name="Maven: redis.clients:jedis:3.3.0" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.30" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-pool2:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.json:json:20190722" level="project" />
<orderEntry type="library" name="Maven: org.cryptomator:siv-mode:1.4.0" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains:annotations:19.0.0" level="project" />
</component>
</module>

View File

@@ -6,7 +6,7 @@
<groupId>net.limework.rediskript</groupId>
<artifactId>RediSkript</artifactId>
<version>1.2.2</version>
<version>1.3.0</version>
<packaging>jar</packaging>
<build>

View File

@@ -1,12 +1,19 @@
package net.limework.rediskript;
import ch.njol.skript.Skript;
import ch.njol.skript.SkriptAddon;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.registrations.EventValues;
import ch.njol.skript.util.Date;
import ch.njol.skript.util.Getter;
import net.limework.rediskript.commands.CommandReloadRedis;
import net.limework.rediskript.skript.SkriptHook;
import net.limework.rediskript.events.RedisMessageEvent;
import net.limework.rediskript.managers.RedisManager;
import org.bukkit.command.PluginCommand;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import net.limework.rediskript.skript.elements.*;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.IOException;
public class RediSkript extends JavaPlugin {
//Redis manager
@@ -17,29 +24,52 @@ public class RediSkript extends JavaPlugin {
rm = new RedisManager(this);
rm.start();
}
public void registerSyntax() {
SkriptAddon addon = Skript.registerAddon(this);
try {
addon.loadClasses("net.limework.rediskript.skript", "elements");
Skript.registerEvent("redis message", EvtRedis.class, RedisMessageEvent.class, "redis message");
Skript.registerExpression(ExprVariableInChannel.class, Object.class, ExpressionType.PROPERTY, "variable[s] %strings% in [redis] [channel] %string%");
Skript.registerExpression(ExprChannel.class, String.class, ExpressionType.SIMPLE, "redis channel");
EventValues.registerEventValue(RedisMessageEvent.class, String.class, new Getter<String, RedisMessageEvent>() {
@Override
public String get(RedisMessageEvent e) {
return e.getChannelName();
}
}, 0);
Skript.registerExpression(ExprMessage.class, String.class, ExpressionType.SIMPLE, "redis message");
EventValues.registerEventValue(RedisMessageEvent.class, String.class, new Getter<String, RedisMessageEvent>() {
@Override
public String get(RedisMessageEvent e) {
return e.getMessage();
}
}, 0);
Skript.registerExpression(ExprMessageDate.class, Date.class, ExpressionType.SIMPLE, "redis message date");
EventValues.registerEventValue(RedisMessageEvent.class, Date.class, new Getter<Date, RedisMessageEvent>() {
@Override
public Date get(RedisMessageEvent e) {
return new Date(e.getDate());
}
}, 0);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onEnable() {
saveDefaultConfig();
startRedis(false);
PluginCommand command = getServer().getPluginCommand("reloadredis");
assert command != null;
command.setExecutor(new CommandReloadRedis(this));
new SkriptHook(this);
getServer().getPluginCommand("reloadredis").setExecutor(new CommandReloadRedis(this));
registerSyntax();
}
@Override
//using HIGHEST event priority so it shuts down last and code can still execute well in "on script unload" and "on skript unload" events
@EventHandler(priority = EventPriority.HIGHEST)
public void onDisable() {
if (rm != null) {
rm.shutdown();
if (rm != null) rm.shutdown();
}
}
public RedisManager getRm() {
public RedisManager getRedisManager() {
return rm;
}
}

View File

@@ -7,7 +7,6 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class CommandReloadRedis implements CommandExecutor {
private RediSkript plugin;
public CommandReloadRedis(RediSkript plugin) {
@@ -18,12 +17,12 @@ public class CommandReloadRedis implements CommandExecutor {
if (sender instanceof Player) {
//not using bungee TextComponent because it is not present in 1.8.8
sender.sendMessage((ChatColor.translateAlternateColorCodes('&'
, "&2[&aRediSkript&a] &cThis command can only be executed in console.")));
, "&2[&aRediSkript&2] &cThis command can only be executed in console.")));
return true;
}
plugin.getRm().reload();
plugin.getRedisManager().reload();
//not sending to sender, because this command can only be executed via console
Bukkit.getLogger().info(ChatColor.translateAlternateColorCodes('&', "&eReloaded via command! Note this command is not stable, it should only be used in urgent cases where you absolutely need to change config details without restarting the server."));
Bukkit.getLogger().info(ChatColor.translateAlternateColorCodes('&', "&eReloaded channels, encryption and login details!"));
return false;
}

View File

@@ -1,20 +1,25 @@
package net.limework.rediskript.managers;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.variables.Variables;
import net.limework.rediskript.RediSkript;
import net.limework.rediskript.events.RedisMessageEvent;
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.cryptomator.siv.UnauthenticCiphertextException;
import org.json.JSONArray;
import org.json.JSONObject;
import redis.clients.jedis.BinaryJedis;
import redis.clients.jedis.BinaryJedisPubSub;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.exceptions.JedisConnectionException;
import javax.crypto.IllegalBlockSizeException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -22,6 +27,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
public class RedisManager extends BinaryJedisPubSub implements Runnable {
private ExecutorService RedisReconnector;
private RediSkript plugin;
private JedisPool jedisPool;
@@ -52,49 +58,39 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
config.getInt("Redis.TimeOut"),
config.getString("Redis.Password"),
config.getBoolean("Redis.useTLS"));
RedisService = Executors.newFixedThreadPool(3);
RedisReconnector = Executors.newSingleThreadExecutor();
RedisService = Executors.newSingleThreadExecutor();
try {
this.subscribeJedis = this.jedisPool.getResource();
} catch (Exception ignored) {
}
} catch (Exception ignored) {}
this.channels = config.getStringList("Channels");
encryption = new Encryption(config);
}
public void start() {
this.RedisService.execute(this);
this.RedisReconnector.execute(this);
}
@Override
public void run() {
while (!isShuttingDown.get()) {
while (!isShuttingDown.get() && plugin.isEnabled()) {
try {
plugin.getLogger().info(ChatColor.translateAlternateColorCodes('&', "&cConnecting to redis..."));
if (!this.subscribeJedis.isConnected()) this.subscribeJedis = this.jedisPool.getResource();
plugin.getLogger().info(ChatColor.translateAlternateColorCodes('&', "&aRedis connected!"));
int byteArr2dSize = 1;
byte[][] channelsInByte = new byte[channels.size()][byteArr2dSize];
boolean reInitializeByteArray;
// Loop that reInitialize array IF array size is not enough
do {
reInitializeByteArray = false;
try {
/* Data Initialization for channelsInByte array from List<String> channels */
byte[][] channelsInByte = new byte[channels.size()][1];
for (int x = 0; x < channels.size(); x++) {
channelsInByte[x] = channels.get(x).getBytes(StandardCharsets.UTF_8);
}
} catch (ArrayIndexOutOfBoundsException ex) {
reInitializeByteArray = true;
/* Increase the current 2d array size to increase 1 and reinitialize the array*/
byteArr2dSize += 1;
channelsInByte = new byte[channels.size()][byteArr2dSize];
}
} while (reInitializeByteArray);
this.subscribeJedis.subscribe(this, channelsInByte);
} catch (Exception e) {
if (isShuttingDown.get() || !plugin.isEnabled()) {
return;
}
plugin.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&cConnection to redis has failed! &cReconnecting..."));
if (this.subscribeJedis != null) {
this.subscribeJedis.close();
@@ -125,12 +121,63 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
//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);
//System.out.println("Message got from channel: "+channel +" and the Message: " +json.toString());
RedisMessageEvent event = new RedisMessageEvent(channelString, j.getString("Message"), j.getLong("Date"));
Bukkit.getScheduler().runTask(plugin, () -> plugin.getServer().getPluginManager().callEvent(event));
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 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.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);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
@@ -139,16 +186,71 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
}
}
public void sendMessage(String[] message, String channel) {
JSONObject json = new JSONObject();
json.put("Messages", new JSONArray(message));
json.put("Type", "Skript");
json.put("Date", System.currentTimeMillis()); //for unique string every time & PING calculations
finishSendMessage(json, channel);
}
public void sendVariables(String[] variableNames, String[] variableValues, String channel, String operation) {
JSONObject json = new JSONObject();
json.put("Names", new JSONArray(variableNames));
if (variableValues != null) {
json.put("Values", new JSONArray(variableValues));
}
json.put("Type", "SkriptVariables");
json.put("Date", System.currentTimeMillis()); //for unique string every time & PING calculations
json.put("Operation", operation);
finishSendMessage(json, channel);
}
public void finishSendMessage(JSONObject json, String channel) {
try {
byte[] message;
if (this.getEncryption().isEncryptionEnabled()) {
message = this.getEncryption().encrypt(json.toString());
} else {
message = json.toString().getBytes(StandardCharsets.UTF_8);
}
//sending a redis message blocks main thread if there's no more connections available
//so to avoid issues, it's best to do it always on separate thread
if (plugin.isEnabled()) {
this.getRedisService().execute(() -> {
BinaryJedis j = this.getJedisPool().getResource();
j.publish(channel.getBytes(StandardCharsets.UTF_8), message);
j.close();
});
} else {
//execute sending of redis message on the main thread if plugin is disabling
//so it can still process the sending
BinaryJedis j = this.getJedisPool().getResource();
j.publish(channel.getBytes(StandardCharsets.UTF_8), message);
j.close();
}
} catch (JedisConnectionException exception) {
exception.printStackTrace();
}
}
public void shutdown() {
this.isShuttingDown.set(true);
if (this.subscribeJedis != null) {
this.unsubscribe();
this.subscribeJedis.close();
this.subscribeJedis.getClient().close();
this.jedisPool.getResource().close();
}
if (this.jedisPool != null) {
jedisPool.close();
jedisPool = null;
}
this.RedisReconnector.shutdown();
this.RedisService.shutdown();
this.RedisService = null;
this.RedisReconnector = null;
encryption = null;
}
public void reload() {

View File

@@ -1,51 +0,0 @@
package net.limework.rediskript.skript;
import ch.njol.skript.Skript;
import ch.njol.skript.SkriptAddon;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.registrations.EventValues;
import ch.njol.skript.util.Date;
import ch.njol.skript.util.Getter;
import net.limework.rediskript.RediSkript;
import net.limework.rediskript.events.RedisMessageEvent;
import net.limework.rediskript.skript.elements.EvtRedis;
import net.limework.rediskript.skript.elements.ExprChannel;
import net.limework.rediskript.skript.elements.ExprMessage;
import net.limework.rediskript.skript.elements.ExprMessageDate;
import java.io.IOException;
public class SkriptHook {
private SkriptAddon addon;
public SkriptHook(RediSkript plugin) {
addon = Skript.registerAddon(plugin);
try {
addon.loadClasses("net.limework.core.skript", "elements");
Skript.registerEvent("redis message", EvtRedis.class, RedisMessageEvent.class, "redis message");
Skript.registerExpression(ExprChannel.class, String.class, ExpressionType.SIMPLE, "redis channel");
EventValues.registerEventValue(RedisMessageEvent.class, String.class, new Getter<String, RedisMessageEvent>() {
@Override
public String get(RedisMessageEvent e) {
return e.getChannelName();
}
}, 0);
Skript.registerExpression(ExprMessage.class, String.class, ExpressionType.SIMPLE, "redis message");
EventValues.registerEventValue(RedisMessageEvent.class, String.class, new Getter<String, RedisMessageEvent>() {
@Override
public String get(RedisMessageEvent e) {
return e.getMessage();
}
}, 0);
Skript.registerExpression(ExprMessageDate.class, Date.class, ExpressionType.SIMPLE, "redis message date");
EventValues.registerEventValue(RedisMessageEvent.class, Date.class, new Getter<Date, RedisMessageEvent>() {
@Override
public Date get(RedisMessageEvent e) {
return new Date(e.getDate());
}
}, 0);
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@@ -6,19 +6,13 @@ import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean;
import net.limework.rediskript.RediSkript;
import net.limework.rediskript.managers.RedisManager;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.event.Event;
import org.json.JSONObject;
import redis.clients.jedis.BinaryJedis;
import redis.clients.jedis.exceptions.JedisConnectionException;
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 +25,9 @@ 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;
}
@@ -42,28 +35,7 @@ public class EffSendMessage extends Effect {
Bukkit.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&2[&aRediSkript&a] &cChannel was empty. Please check your code."));
return;
}
assert plugin != null;
JSONObject json = new JSONObject();
json.put("Message", message);
json.put("Type", "Skript");
json.put("Date", System.currentTimeMillis()); //for unique string every time & PING calculations
byte[] msg;
RedisManager manager = plugin.getRm();
if (manager.getEncryption().isEncryptionEnabled()) {
msg = manager.getEncryption().encrypt(json.toString());
} else {
msg = json.toString().getBytes(StandardCharsets.UTF_8);
}
try {
manager.getRedisService().execute(() -> {
BinaryJedis j = manager.getJedisPool().getResource();
j.publish(channel.getBytes(StandardCharsets.UTF_8), msg);
j.close();
});
} catch (JedisConnectionException exception) {
exception.printStackTrace();
}
plugin.getRedisManager().sendMessage(message, channel);
}
@Override

View File

@@ -14,10 +14,7 @@ public class EvtRedis extends SkriptEvent {
@Override
public boolean check(Event event) {
if (!(event instanceof RedisMessageEvent)) {
return false;
}
return true;
return (event instanceof RedisMessageEvent);
}
@Override

View File

@@ -0,0 +1,84 @@
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;
import ch.njol.util.coll.CollectionUtils;
import net.limework.rediskript.RediSkript;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import java.util.Base64;
public class ExprVariableInChannel extends SimpleExpression<Object> {
private Expression<String> name;
private Expression<String> channel;
@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parser) {
name = (Expression<String>) expressions[0];
channel = (Expression<String>) expressions[1];
return true;
}
@Override
protected Object[] get(Event event) {
return null;
}
@Override
public boolean isSingle() {
return true;
}
@Override
public Class<? extends Object> getReturnType() {
return Object.class;
}
@Override
public String toString(Event event, boolean debug) {
return "variable " + name.toString(event,debug) + " in redis channel " + channel.toString(event, debug);
}
@Override
public void change(Event e, Object[] changer, Changer.ChangeMode mode) {
RediSkript plugin = (RediSkript) Bukkit.getPluginManager().getPlugin("RediSkript");
switch (mode) {
case ADD:
case SET:
case REMOVE:
SerializedVariable.Value serialized;
String encoded;
String[] values = new String[changer.length+1];
for( int i = 0; i < changer.length; i++) {
if (changer[i] != null) {
serialized = Classes.serialize(changer[i]);
encoded = Base64.getEncoder().encodeToString(serialized.data);
encoded = serialized.type + "^" + encoded;
values[i] = encoded;
}
}
String operation = mode.toString();
plugin.getRedisManager().sendVariables(name.getAll(e), values, channel.getSingle(e), operation);
break;
case DELETE:
plugin.getRedisManager().sendVariables(name.getAll(e), null, channel.getSingle(e), "SET");
break;
}
}
@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)
return CollectionUtils.array(Object.class);
return null;
}
}