forked from Limework/RediSkript
Compare commits
No commits in common. "backup" and "master" have entirely different histories.
2
.github/workflows/maven.yml
vendored
2
.github/workflows/maven.yml
vendored
@ -29,4 +29,4 @@ jobs:
|
|||||||
# Artifact name
|
# Artifact name
|
||||||
name: RediSkript_JAR
|
name: RediSkript_JAR
|
||||||
# Destination path
|
# Destination path
|
||||||
path: target/Red*
|
path: target/*.jar
|
||||||
|
68
README.md
68
README.md
@ -1,35 +1,42 @@
|
|||||||
[![RediSkript Build](https://github.com/Limework/RediSkript/actions/workflows/maven.yml/badge.svg?branch=master)](https://github.com/Limework/RediSkript/actions/workflows/maven.yml)
|
|
||||||
|
|
||||||
RediSkript allows you to communicate between your servers with use of Redis, it's very fast and easy to use.
|
|
||||||
|
## RediSkript
|
||||||
|
Allows you to communicate between your Minecraft servers with use of Redis and Skript, it's very fast and easy to use.
|
||||||
|
|
||||||
Skript: https://github.com/SkriptLang/Skript
|
Skript: https://github.com/SkriptLang/Skript
|
||||||
|
|
||||||
RediSkript spigot page: https://www.spigotmc.org/resources/rediskript-communicate-between-servers-with-ease.85067/
|
RediSkript spigot page: https://www.spigotmc.org/resources/rediskript-communicate-between-servers-with-ease.85067/s
|
||||||
|
|
||||||
Jedis: https://github.com/redis/jedis
|
Minecraft server version supported: **1.8.8+** (On 1.8, may need to use a fork of Skript)
|
||||||
|
|
||||||
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!
|
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 developed and maintained by Govindas & the team of Govindas Limework developers.
|
It is developed and maintained by Govindas & the team of Govindas Limework developers.
|
||||||
|
|
||||||
Redis Message:
|
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 a VPS for hosting redis server, but there also are free redis hosting options available.
|
||||||
|
|
||||||
|
### Redis Message
|
||||||
```
|
```
|
||||||
on redis message:
|
on redis message:
|
||||||
if redis channel is "world":
|
if redis channel is "world":
|
||||||
broadcast "%redis message% %redis channel% %redis message date%"
|
broadcast "%redis message% %redis channel% %redis message date%"
|
||||||
|
|
||||||
command /sendredis <text> <text>:
|
command /sendredis <text> <text>:
|
||||||
usage: /sendredis <message> <channel>
|
usage: /sendredis <message> <channel>
|
||||||
trigger:
|
trigger:
|
||||||
send redis message arg 1 to channel arg 2
|
send redis message arg 1 to channel arg 2
|
||||||
send redis message "hello world!" to channel "world"
|
send redis message "hello world!" to channel "world"
|
||||||
```
|
```
|
||||||
Managing variables:
|
### Managing variables
|
||||||
```
|
```
|
||||||
set variables "test::1", "test::2", "test::3" in channel "global" to 100
|
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:
|
#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
|
send "%{test::*}%" #outputs 100, 100 and 100
|
||||||
|
|
||||||
|
add 100 to variables "test::1" and "test::2" in channel "global"
|
||||||
|
remove 10 from variable "test::1" in channel "global"
|
||||||
delete variables "test::*" in channel "global"
|
delete variables "test::*" in channel "global"
|
||||||
|
|
||||||
set variable "test::%uuid of player%" in channel "playerdata" to tool of player
|
set variable "test::%uuid of player%" in channel "playerdata" to tool of player
|
||||||
@ -41,39 +48,56 @@ Syntax:
|
|||||||
variable[s] %strings% in [redis] [channel] %string%
|
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.
|
### Configuration
|
||||||
|
plugins/RediSkript/config.yml
|
||||||
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:
|
|
||||||
```
|
```
|
||||||
Redis:
|
Redis:
|
||||||
#a secure password that cannot be cracked, please change it!
|
#a secure password that cannot be cracked, please change it!
|
||||||
#it is also recommended to firewall your redis server with iptables so it can only be accessed by specific IP addresses
|
#it is also recommended to firewall your redis server with iptables so it can only be accessed by specific IP addresses
|
||||||
Password: "yHy0d2zdBlRmaSPj3CiBwEv5V3XxBTLTrCsGW7ntBnzhfxPxXJS6Q1aTtR6DSfAtCZr2VxWnsungXHTcF94a4bsWEpGAvjL9XMU"
|
Password: "yHy0d2zdBlRmaSPj3CiBwEv5V3XxBTLTrCsGW7ntBnzhfxPxXJS6Q1aTtR6DSfAtCZr2VxWnsungXHTcF94a4bsWEpGAvjL9XMU"
|
||||||
|
#hostname of your redis server, you can use free redis hosting (search for it online) if you do not have the ability to host your own redis server
|
||||||
|
#redis server is very lightweight, takes under 30 MB of RAM usually
|
||||||
Host: "127.0.0.1"
|
Host: "127.0.0.1"
|
||||||
#must be 2 or higher, if you set to lower, the addon will automatically use 2 as a minimum
|
#must be 2 or higher, if you set to lower, the addon will automatically use 2 as a minimum
|
||||||
|
#do not edit MaxConnections if you do not know what you're doing
|
||||||
|
#it is only useful to increase this number to account for PING between distant servers and when you are sending a lot of messages constantly
|
||||||
MaxConnections: 2
|
MaxConnections: 2
|
||||||
#the default Redis port
|
#the default Redis port
|
||||||
Port: 6379
|
Port: 6379
|
||||||
#time out in milliseconds, how long it should take before it decides that it is unable to connect when sending a message
|
#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
|
#9000 = 9 seconds
|
||||||
TimeOut: 90000
|
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
|
#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
|
#it encrypts your traffic and makes data exchange between distant servers secure
|
||||||
useTLS: false
|
useTLS: false
|
||||||
#may be useful if you cannot use TLS due to use of older version of Redis
|
#EncryptMessages may be useful if you cannot use TLS due to use of older version of Redis or if you're paranoid about privacy and want to double encrypt your messages
|
||||||
#however this will not encrypt the initial authentication password, only the messages sent
|
#however this will not encrypt the initial authentication password, only the messages sent (use TLS for initial authentication password encryption)
|
||||||
#it uses AES-128 SIV encryption which is secure enough for this
|
|
||||||
|
#the encryption configuration must be the same across all servers in order to communicate
|
||||||
|
|
||||||
|
#use 16 characters long key for AES-128 encryption
|
||||||
|
#32 characters long key for AES-256 encryption (recommended)
|
||||||
|
#the AES implementation used in RediSkript uses SIV mode, which makes the same key resistant to cracking for a big count of messages without the need of changing the key very often
|
||||||
EncryptMessages: true
|
EncryptMessages: true
|
||||||
EncryptionKey: "16CHARACTERS KEY"
|
#EncryptionKey and MacKey must be different
|
||||||
MacKey: "16CHARACTERS KEY"
|
EncryptionKey: "32CHARACTERS KEY"
|
||||||
|
MacKey: "32CHARACTERS KEY"
|
||||||
|
|
||||||
|
|
||||||
#the channels from which this server can receive messages
|
#the channels from which this server can receive messages
|
||||||
#you can always send messages to all channels!
|
#you can always send messages to all channels!
|
||||||
#you can add as many channels as you wish!
|
#you can add as many channels as you wish!
|
||||||
|
|
||||||
|
#ideal setup is having one global channel and having one channel that represents server name, so you know who to send messages to
|
||||||
|
#then a few other utility channels up to your needs
|
||||||
Channels:
|
Channels:
|
||||||
- "Channel1"
|
- "global"
|
||||||
- "Channel2"
|
- "servername"
|
||||||
- "Channel3"
|
- "Channel3"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## YourKit
|
||||||
|
|
||||||
|
YourKit supports open source projects with innovative and intelligent tools for monitoring and profiling Java and .NET applications. YourKit is the creator of [YourKit Java Profiler](https://www.yourkit.com/java/profiler/), [YourKit .NET Profiler](https://www.yourkit.com/.net/profiler/) and [YourKit YouMonitor](https://www.yourkit.com/youmonitor/).
|
||||||
|
|
||||||
|
![YourKit](https://www.yourkit.com/images/yklogo.png)
|
||||||
|
83
RediSkript-bukkit/pom.xml
Normal file
83
RediSkript-bukkit/pom.xml
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>RediSkript</artifactId>
|
||||||
|
<groupId>net.limework</groupId>
|
||||||
|
<version>1.3.7-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>RediSkript-bukkit</artifactId>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>papermc</id>
|
||||||
|
<url>https://papermc.io/repo/repository/maven-public/</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<version>3.2.3</version>
|
||||||
|
<configuration>
|
||||||
|
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
|
<version>3.2.0</version>
|
||||||
|
<configuration>
|
||||||
|
<outputDirectory>../target</outputDirectory>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.SkriptLang</groupId>
|
||||||
|
<artifactId>Skript</artifactId>
|
||||||
|
<version>2.6.1</version>
|
||||||
|
<type>jar</type>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>*</groupId>
|
||||||
|
<artifactId>*</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.spigotmc</groupId>
|
||||||
|
<artifactId>spigot-api</artifactId>
|
||||||
|
<version>1.16.5-R0.1-SNAPSHOT</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.limework</groupId>
|
||||||
|
<artifactId>RediSkript-core</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -10,6 +10,7 @@ import net.limework.rediskript.commands.CommandReloadRedis;
|
|||||||
import net.limework.rediskript.events.RedisMessageEvent;
|
import net.limework.rediskript.events.RedisMessageEvent;
|
||||||
import net.limework.rediskript.managers.RedisController;
|
import net.limework.rediskript.managers.RedisController;
|
||||||
import net.limework.rediskript.skript.elements.*;
|
import net.limework.rediskript.skript.elements.*;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
@ -89,4 +90,12 @@ public class RediSkript extends JavaPlugin {
|
|||||||
public RedisController getRC() {
|
public RedisController getRC() {
|
||||||
return redisController;
|
return redisController;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Developer note: This is use for skript-reflect! and also for plugin use for static stuff -> skript effects classes
|
||||||
|
//DO NOT USE THIS IN NON STATIC CLASSES
|
||||||
|
public static RediSkript getAPI(){
|
||||||
|
//this safer than making static.
|
||||||
|
return (RediSkript) Bukkit.getServer().getPluginManager().getPlugin("RediSkript");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -0,0 +1,386 @@
|
|||||||
|
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.data.Encryption;
|
||||||
|
import net.limework.rediskript.events.RedisMessageEvent;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.configuration.Configuration;
|
||||||
|
import org.bukkit.scheduler.BukkitTask;
|
||||||
|
import org.cryptomator.siv.UnauthenticCiphertextException;
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import redis.clients.jedis.*;
|
||||||
|
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.atomic.AtomicBoolean;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
|
public class RedisController extends BinaryJedisPubSub implements Runnable {
|
||||||
|
|
||||||
|
|
||||||
|
//Jedis Pool to be used by every another class.
|
||||||
|
private final JedisPool jedisPool;
|
||||||
|
|
||||||
|
//this seems useless unless tls is OFF!
|
||||||
|
|
||||||
|
// class author is govindas :/
|
||||||
|
private final Encryption encryption;
|
||||||
|
|
||||||
|
private byte[][] channelsInByte;
|
||||||
|
|
||||||
|
private final AtomicBoolean isConnectionBroken;
|
||||||
|
private final AtomicBoolean isConnecting;
|
||||||
|
private final RediSkript plugin;
|
||||||
|
private final BukkitTask ConnectionTask;
|
||||||
|
|
||||||
|
|
||||||
|
public RedisController(RediSkript plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
Configuration config = plugin.getConfig();
|
||||||
|
JedisPoolConfig JConfig = new JedisPoolConfig();
|
||||||
|
int maxConnections = config.getInt("Redis.MaxConnections");
|
||||||
|
|
||||||
|
//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);
|
||||||
|
JConfig.setBlockWhenExhausted(true);
|
||||||
|
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"));
|
||||||
|
setupChannels(config);
|
||||||
|
isConnectionBroken = new AtomicBoolean(true);
|
||||||
|
isConnecting = new AtomicBoolean(false);
|
||||||
|
//Start the main task on async thread
|
||||||
|
ConnectionTask = plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, this, 0, 20 * 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (!isConnectionBroken.get() || isConnecting.get()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
plugin.sendLogs("Connecting to Redis server...");
|
||||||
|
isConnecting.set(true);
|
||||||
|
try (Jedis jedis = jedisPool.getResource()) {
|
||||||
|
isConnectionBroken.set(false);
|
||||||
|
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 to Redis server has failed! Please check your details in the configuration.");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void shutdown() {
|
||||||
|
ConnectionTask.cancel();
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
} 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 Integer) {
|
||||||
|
inputValue = Long.valueOf((Integer) inputValue);
|
||||||
|
}
|
||||||
|
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 Integer) {
|
||||||
|
inputValue = Double.valueOf((Integer) inputValue);
|
||||||
|
}
|
||||||
|
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 if (variable instanceof Integer) {
|
||||||
|
if (inputValue instanceof Integer) {
|
||||||
|
Variables.setVariable(varName, (Integer) variable + (Integer) inputValue, null, false);
|
||||||
|
} else if (inputValue instanceof Double) {
|
||||||
|
// convert Integer variable to Double
|
||||||
|
variable = Double.valueOf((Integer) variable);
|
||||||
|
Variables.setVariable(varName, (Double) variable + (Double) inputValue, null, false);
|
||||||
|
} else if (inputValue instanceof Long) {
|
||||||
|
// convert Integer variable to Long
|
||||||
|
variable = Long.valueOf((Integer) variable);
|
||||||
|
Variables.setVariable(varName, (Long) variable + (Long) inputValue, null, false);
|
||||||
|
}
|
||||||
|
} 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 if (inputValue instanceof Integer) {
|
||||||
|
Variables.setVariable(varName, -(Integer) 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 Integer) {
|
||||||
|
inputValue = Long.valueOf((Integer) inputValue);
|
||||||
|
}
|
||||||
|
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 Integer) {
|
||||||
|
inputValue = Double.valueOf((Integer) inputValue);
|
||||||
|
}
|
||||||
|
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 if (variable instanceof Integer) {
|
||||||
|
if (inputValue instanceof Integer) {
|
||||||
|
Variables.setVariable(varName, (Integer) variable - (Integer) inputValue, null, false);
|
||||||
|
} else if (inputValue instanceof Long) {
|
||||||
|
// convert Integer variable to Long
|
||||||
|
variable = Long.valueOf((Integer) variable);
|
||||||
|
Variables.setVariable(varName, (Long) variable - (Long) inputValue, null, false);
|
||||||
|
} else if (inputValue instanceof Double) {
|
||||||
|
// convert Integer variable to Double
|
||||||
|
variable = Double.valueOf((Integer) variable);
|
||||||
|
Variables.setVariable(varName, (Double) variable - (Double) inputValue, 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();
|
||||||
|
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 (encryption.isEncryptionEnabled()) {
|
||||||
|
message = encryption.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()) {
|
||||||
|
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||||
|
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 (Jedis j = jedisPool.getResource()) {
|
||||||
|
j.publish(channel.getBytes(StandardCharsets.UTF_8), message);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (JedisConnectionException exception) {
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupChannels(Configuration config) {
|
||||||
|
List<String> channels = config.getStringList("Channels");
|
||||||
|
channelsInByte = new byte[channels.size()][1];
|
||||||
|
for (int x = 0; x < channels.size(); x++) {
|
||||||
|
channelsInByte[x] = channels.get(x).getBytes(StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean isRedisConnectionOffline() {
|
||||||
|
return isConnectionBroken.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
// for skript reflect :)
|
||||||
|
public JedisPool getJedisPool() {
|
||||||
|
return jedisPool;
|
||||||
|
}
|
||||||
|
//
|
||||||
|
}
|
@ -23,7 +23,7 @@ public class EffSendMessage extends Effect {
|
|||||||
@Override
|
@Override
|
||||||
protected void execute(Event event) {
|
protected void execute(Event event) {
|
||||||
|
|
||||||
RediSkript plugin = (RediSkript) Bukkit.getPluginManager().getPlugin("RediSkript");
|
RediSkript plugin = RediSkript.getAPI();
|
||||||
|
|
||||||
String[] message = this.message.getAll(event);
|
String[] message = this.message.getAll(event);
|
||||||
String channel = this.channel.getSingle(event);
|
String channel = this.channel.getSingle(event);
|
@ -73,7 +73,7 @@ public class ExprVariableInChannel extends SimpleExpression<Object> {
|
|||||||
@Override
|
@Override
|
||||||
public Class<?>[] acceptChange(Changer.ChangeMode mode) {
|
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 || mode == Changer.ChangeMode.ADD || mode == Changer.ChangeMode.REMOVE)
|
||||||
if (mode == Changer.ChangeMode.DELETE || mode == Changer.ChangeMode.SET)
|
if (mode == Changer.ChangeMode.DELETE || mode == Changer.ChangeMode.SET || mode == Changer.ChangeMode.ADD || mode == Changer.ChangeMode.REMOVE)
|
||||||
return CollectionUtils.array(Object.class);
|
return CollectionUtils.array(Object.class);
|
||||||
|
|
||||||
return null;
|
return null;
|
@ -23,13 +23,13 @@ Redis:
|
|||||||
#the encryption configuration must be the same across all servers in order to communicate
|
#the encryption configuration must be the same across all servers in order to communicate
|
||||||
|
|
||||||
#use 16 characters long key for AES-128 encryption
|
#use 16 characters long key for AES-128 encryption
|
||||||
#32 characters long key for AES-256 encryption
|
#32 characters long key for AES-256 encryption (recommended)
|
||||||
#AES-128 is faster, but less secure (but it is not crackable by today's technology as of 2020, may be crackable by quantum computers)
|
#AES-128 is faster, but less secure (but it is not crackable by today's technology as of 2020, may be crackable by quantum computers)
|
||||||
#the AES implementation used in RediSkript uses SIV mode, which makes the same key resistant to cracking for a big count of messages without the need of changing the key very often
|
#the AES implementation used in RediSkript uses SIV mode, which makes the same key resistant to cracking for a big count of messages without the need of changing the key very often
|
||||||
EncryptMessages: true
|
EncryptMessages: true
|
||||||
#EncryptionKey and MacKey must be different
|
#EncryptionKey and MacKey must be different
|
||||||
EncryptionKey: "16CHARACTERS KEY"
|
EncryptionKey: "32CHARACTERS KEY"
|
||||||
MacKey: "16CHARACTERS KEY"
|
MacKey: "32CHARACTERS KEY"
|
||||||
|
|
||||||
|
|
||||||
#the channels from which this server can receive messages
|
#the channels from which this server can receive messages
|
||||||
@ -41,4 +41,4 @@ Redis:
|
|||||||
Channels:
|
Channels:
|
||||||
- "global"
|
- "global"
|
||||||
- "servername"
|
- "servername"
|
||||||
- "Channel3"
|
- "Channel3"
|
@ -1,9 +1,10 @@
|
|||||||
main: net.limework.rediskript.RediSkript
|
main: net.limework.rediskript.RediSkript
|
||||||
name: ${project.name}
|
name: RediSkript
|
||||||
version: ${project.version}
|
version: ${project.version}
|
||||||
authors: [Govindas, ham1255, DaemonicKing, limework.net]
|
authors: [Govindas, ham1255, DaemonicKing, limework.net]
|
||||||
api-version: 1.13
|
api-version: 1.13
|
||||||
depend: [Skript]
|
depend: [Skript]
|
||||||
commands:
|
commands:
|
||||||
reloadredis:
|
reloadredis:
|
||||||
description: "Reload redis configuration & restart the connection."
|
description: "Reload redis configuration & restart the connection."
|
||||||
|
permission: reload.redis
|
14
RediSkript-core/pom.xml
Normal file
14
RediSkript-core/pom.xml
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>RediSkript</artifactId>
|
||||||
|
<groupId>net.limework</groupId>
|
||||||
|
<version>1.3.7-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>RediSkript-core</artifactId>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,11 @@
|
|||||||
|
package net.limework.rediskript;
|
||||||
|
|
||||||
|
|
||||||
|
import redis.clients.jedis.BinaryJedisPubSub;
|
||||||
|
|
||||||
|
public abstract class Subscriber extends BinaryJedisPubSub implements Runnable {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,5 @@
|
|||||||
package net.limework.rediskript.data;
|
package net.limework.rediskript.data;
|
||||||
|
|
||||||
import org.bukkit.configuration.Configuration;
|
|
||||||
import org.cryptomator.siv.SivMode;
|
import org.cryptomator.siv.SivMode;
|
||||||
import org.cryptomator.siv.UnauthenticCiphertextException;
|
import org.cryptomator.siv.UnauthenticCiphertextException;
|
||||||
|
|
||||||
@ -9,17 +8,17 @@ import java.nio.charset.StandardCharsets;
|
|||||||
|
|
||||||
public class Encryption {
|
public class Encryption {
|
||||||
|
|
||||||
private boolean encryptionEnabled;
|
private final boolean encryptionEnabled;
|
||||||
private String encryptionKey;
|
private String encryptionKey;
|
||||||
private String macKey;
|
private String macKey;
|
||||||
private final SivMode AES_SIV = new SivMode();
|
private final SivMode AES_SIV = new SivMode();
|
||||||
|
|
||||||
public Encryption(Configuration config){
|
public Encryption(boolean encryptionEnabled, String encryptionKey, String macKey){
|
||||||
encryptionEnabled = config.getBoolean("Redis.EncryptMessages");
|
this.encryptionEnabled = encryptionEnabled;
|
||||||
if (encryptionEnabled) {
|
if (this.encryptionEnabled) {
|
||||||
// AES encryption
|
// AES encryption
|
||||||
encryptionKey = config.getString("Redis.EncryptionKey");
|
this.encryptionKey = encryptionKey;
|
||||||
macKey = config.getString("Redis.MacKey");
|
this.macKey = macKey;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
103
pom.xml
103
pom.xml
@ -6,51 +6,14 @@
|
|||||||
|
|
||||||
<groupId>net.limework</groupId>
|
<groupId>net.limework</groupId>
|
||||||
<artifactId>RediSkript</artifactId>
|
<artifactId>RediSkript</artifactId>
|
||||||
<version>1.3.3</version>
|
<version>1.3.7-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
<modules>
|
||||||
|
<module>RediSkript-core</module>
|
||||||
<build>
|
<module>RediSkript-bukkit</module>
|
||||||
<resources>
|
</modules>
|
||||||
<resource>
|
<packaging>pom</packaging>
|
||||||
<directory>src/main/resources</directory>
|
|
||||||
<filtering>true</filtering>
|
|
||||||
</resource>
|
|
||||||
</resources>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
|
||||||
<version>3.6.1</version>
|
|
||||||
<configuration>
|
|
||||||
<source>1.8</source>
|
|
||||||
<target>1.8</target>
|
|
||||||
<encoding>UTF-8</encoding>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-shade-plugin</artifactId>
|
|
||||||
<version>3.2.3</version>
|
|
||||||
<configuration>
|
|
||||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
|
||||||
</configuration>
|
|
||||||
<executions>
|
|
||||||
<execution>
|
|
||||||
<phase>package</phase>
|
|
||||||
<goals>
|
|
||||||
<goal>shade</goal>
|
|
||||||
</goals>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
<repository>
|
|
||||||
<id>spigot-repo</id>
|
|
||||||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/org/spigotmc/spigot-api/</url>
|
|
||||||
</repository>
|
|
||||||
<repository>
|
<repository>
|
||||||
<id>jitpack.io</id>
|
<id>jitpack.io</id>
|
||||||
<url>https://jitpack.io</url>
|
<url>https://jitpack.io</url>
|
||||||
@ -59,50 +22,44 @@
|
|||||||
<id>commons-pool2</id>
|
<id>commons-pool2</id>
|
||||||
<url>https://mvnrepository.com/artifact/org.apache.commons/commons-pool2</url>
|
<url>https://mvnrepository.com/artifact/org.apache.commons/commons-pool2</url>
|
||||||
</repository>
|
</repository>
|
||||||
<repository>
|
|
||||||
<id>PaperMC</id>
|
|
||||||
<url>https://repo.destroystokyo.com/repository/maven-public/</url>
|
|
||||||
</repository>
|
|
||||||
<repository>
|
|
||||||
<id>sk89q</id>
|
|
||||||
<url>http://maven.sk89q.com/repo</url>
|
|
||||||
</repository>
|
|
||||||
</repositories>
|
</repositories>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.6.1</version>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
<encoding>UTF-8</encoding>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
|
||||||
<groupId>com.github.SkriptLang</groupId>
|
|
||||||
<artifactId>Skript</artifactId>
|
|
||||||
<version>2.5.3</version>
|
|
||||||
<type>jar</type>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.spigotmc</groupId>
|
|
||||||
<artifactId>spigot-api</artifactId>
|
|
||||||
<version>1.16.5-R0.1-SNAPSHOT</version>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>redis.clients</groupId>
|
|
||||||
<artifactId>jedis</artifactId>
|
|
||||||
<version>3.5.1</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.json</groupId>
|
<groupId>org.json</groupId>
|
||||||
<artifactId>json</artifactId>
|
<artifactId>json</artifactId>
|
||||||
<version>20201115</version>
|
<version>20220320</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.cryptomator</groupId>
|
<groupId>org.cryptomator</groupId>
|
||||||
<artifactId>siv-mode</artifactId>
|
<artifactId>siv-mode</artifactId>
|
||||||
<version>1.4.1</version>
|
<version>1.5.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
|
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>commons-pool2</artifactId>
|
<artifactId>commons-pool2</artifactId>
|
||||||
<version>2.6.2</version>
|
<version>2.11.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>redis.clients</groupId>
|
||||||
|
<artifactId>jedis</artifactId>
|
||||||
|
<version>4.4.6</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
@ -1,258 +0,0 @@
|
|||||||
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.data.Encryption;
|
|
||||||
import net.limework.rediskript.events.RedisMessageEvent;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.configuration.Configuration;
|
|
||||||
import org.bukkit.scheduler.BukkitTask;
|
|
||||||
import org.cryptomator.siv.UnauthenticCiphertextException;
|
|
||||||
import org.json.JSONArray;
|
|
||||||
import org.json.JSONObject;
|
|
||||||
import redis.clients.jedis.*;
|
|
||||||
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.atomic.AtomicBoolean;
|
|
||||||
|
|
||||||
public class RedisController extends BinaryJedisPubSub implements Runnable {
|
|
||||||
|
|
||||||
|
|
||||||
//Jedis Pool to be used by every another class.
|
|
||||||
private final JedisPool jedisPool;
|
|
||||||
|
|
||||||
//this seems useless unless tls is OFF!
|
|
||||||
private final Encryption encryption;
|
|
||||||
|
|
||||||
private byte[][] channelsInByte;
|
|
||||||
|
|
||||||
private final AtomicBoolean isConnectionBroken;
|
|
||||||
private final AtomicBoolean isConnecting;
|
|
||||||
private final RediSkript plugin;
|
|
||||||
private final BukkitTask ConnectionTask;
|
|
||||||
|
|
||||||
|
|
||||||
public RedisController(RediSkript plugin) {
|
|
||||||
this.plugin = plugin;
|
|
||||||
Configuration config = plugin.getConfig();
|
|
||||||
JedisPoolConfig JConfig = new JedisPoolConfig();
|
|
||||||
int maxConnections = config.getInt("Redis.MaxConnections");
|
|
||||||
|
|
||||||
//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);
|
|
||||||
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"));
|
|
||||||
encryption = new Encryption(config);
|
|
||||||
setupChannels(config);
|
|
||||||
isConnectionBroken = new AtomicBoolean(true);
|
|
||||||
isConnecting = new AtomicBoolean(false);
|
|
||||||
//Start the main task on async thread
|
|
||||||
ConnectionTask = plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, this, 0, 20 * 5);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (!isConnectionBroken.get() || isConnecting.get()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
plugin.sendLogs("Connecting to Redis server...");
|
|
||||||
isConnecting.set(true);
|
|
||||||
try (Jedis jedis = jedisPool.getResource()) {
|
|
||||||
isConnectionBroken.set(false);
|
|
||||||
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 to Redis server has failed! Please check your details in the configuration.");
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void shutdown() {
|
|
||||||
ConnectionTask.cancel();
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
} 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 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":
|
|
||||||
String variableName = variableNames.get(i).toString();
|
|
||||||
|
|
||||||
//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 (variableName.charAt(variableName.length()-1) == '*') {
|
|
||||||
Variables.setVariable(variableName, null, null, false);
|
|
||||||
}
|
|
||||||
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:");
|
|
||||||
plugin.sendErrorLogs(receivedMessage);
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 (encryption.isEncryptionEnabled()) {
|
|
||||||
message = encryption.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()) {
|
|
||||||
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
|
|
||||||
try (BinaryJedis 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()) {
|
|
||||||
j.publish(channel.getBytes(StandardCharsets.UTF_8), message);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
} catch (JedisConnectionException exception) {
|
|
||||||
exception.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setupChannels(Configuration config) {
|
|
||||||
List<String> channels = config.getStringList("Channels");
|
|
||||||
channelsInByte = new byte[channels.size()][1];
|
|
||||||
for (int x = 0; x < channels.size(); x++) {
|
|
||||||
channelsInByte[x] = channels.get(x).getBytes(StandardCharsets.UTF_8);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Boolean isRedisConnectionOffline() {
|
|
||||||
return isConnectionBroken.get();
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user