forked from Limework/RediSkript
commit
e5d9d17a23
@ -8,22 +8,33 @@ import ch.njol.skript.util.Date;
|
||||
import ch.njol.skript.util.Getter;
|
||||
import net.limework.rediskript.commands.CommandReloadRedis;
|
||||
import net.limework.rediskript.events.RedisMessageEvent;
|
||||
import net.limework.rediskript.managers.RedisManager;
|
||||
import net.limework.rediskript.managers.RedisController;
|
||||
import net.limework.rediskript.skript.elements.*;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class RediSkript extends JavaPlugin {
|
||||
|
||||
//Redis manager
|
||||
private RedisManager rm;
|
||||
private RedisController redisController;
|
||||
|
||||
public void startRedis(boolean reload) {
|
||||
if (reload) { reloadConfig(); }
|
||||
rm = new RedisManager(this);
|
||||
rm.start();
|
||||
public void reloadRedis() {
|
||||
redisController.shutdown();
|
||||
redisController = new RedisController(this);
|
||||
}
|
||||
|
||||
public void sendLogs(String message) {
|
||||
getLogger().info(
|
||||
ChatColor.translateAlternateColorCodes('&', "&b" + message)
|
||||
);
|
||||
}
|
||||
public void sendErrorLogs(String message) {
|
||||
getLogger().severe(
|
||||
ChatColor.translateAlternateColorCodes('&', "&c" + message)
|
||||
);
|
||||
}
|
||||
|
||||
public void registerSyntax() {
|
||||
SkriptAddon addon = Skript.registerAddon(this);
|
||||
try {
|
||||
@ -60,16 +71,17 @@ public class RediSkript extends JavaPlugin {
|
||||
@Override
|
||||
public void onEnable() {
|
||||
saveDefaultConfig();
|
||||
startRedis(false);
|
||||
redisController = new RedisController(this);
|
||||
getServer().getPluginCommand("reloadredis").setExecutor(new CommandReloadRedis(this));
|
||||
registerSyntax();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
if (rm != null) rm.shutdown();
|
||||
if (redisController != null) redisController.shutdown();
|
||||
}
|
||||
public RedisManager getRedisManager() {
|
||||
return rm;
|
||||
|
||||
public RedisController getRC() {
|
||||
return redisController;
|
||||
}
|
||||
}
|
@ -27,7 +27,7 @@ public class CommandReloadRedis implements CommandExecutor {
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
plugin.getRedisManager().reload();
|
||||
plugin.reloadRedis();
|
||||
}
|
||||
}.runTaskAsynchronously(plugin);
|
||||
|
||||
|
@ -8,46 +8,45 @@ import net.limework.rediskript.events.RedisMessageEvent;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.configuration.Configuration;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
import org.cryptomator.siv.UnauthenticCiphertextException;
|
||||
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.*;
|
||||
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;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class RedisManager extends BinaryJedisPubSub implements Runnable {
|
||||
|
||||
private ExecutorService RedisReconnector;
|
||||
private RediSkript plugin;
|
||||
|
||||
private JedisPool jedisPool;
|
||||
private ExecutorService RedisService;
|
||||
public class RedisController extends BinaryJedisPubSub implements Runnable {
|
||||
|
||||
|
||||
//sub
|
||||
private BinaryJedis subscribeJedis;
|
||||
private List<String> channels;
|
||||
private AtomicBoolean isShuttingDown = new AtomicBoolean(false);
|
||||
private Encryption encryption;
|
||||
//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 boolean debugMode = false; //todo: will be later in the config in future release.
|
||||
private final BukkitTask ConnectionTask;
|
||||
|
||||
|
||||
public RedisManager(RediSkript plugin) {
|
||||
public RedisController(RediSkript plugin) {
|
||||
this.plugin = plugin;
|
||||
Configuration config = this.plugin.getConfig();
|
||||
Configuration config = plugin.getConfig();
|
||||
JedisPoolConfig JConfig = new JedisPoolConfig();
|
||||
int maxConnections = config.getInt("Redis.MaxConnections");
|
||||
if (maxConnections < 2) { maxConnections = 2; }
|
||||
|
||||
if (maxConnections < 2) {
|
||||
maxConnections = 2;
|
||||
}
|
||||
JConfig.setMaxTotal(maxConnections);
|
||||
JConfig.setMaxIdle(maxConnections);
|
||||
JConfig.setMinIdle(1);
|
||||
@ -58,52 +57,49 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
|
||||
config.getInt("Redis.TimeOut"),
|
||||
config.getString("Redis.Password"),
|
||||
config.getBoolean("Redis.useTLS"));
|
||||
RedisReconnector = Executors.newSingleThreadExecutor();
|
||||
RedisService = Executors.newSingleThreadExecutor();
|
||||
try {
|
||||
this.subscribeJedis = this.jedisPool.getResource();
|
||||
} catch (Exception ignored) {}
|
||||
|
||||
this.channels = config.getStringList("Channels");
|
||||
encryption = new Encryption(config);
|
||||
|
||||
}
|
||||
|
||||
public void start() {
|
||||
this.RedisReconnector.execute(this);
|
||||
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() {
|
||||
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!"));
|
||||
byte[][] channelsInByte = new byte[channels.size()][1];
|
||||
for (int x = 0; x < channels.size(); x++) {
|
||||
channelsInByte[x] = channels.get(x).getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
this.subscribeJedis.subscribe(this, channelsInByte);
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
if (isShuttingDown.get() || !plugin.isEnabled()) {
|
||||
if (!isConnectionBroken.get() || isConnecting.get()) {
|
||||
return;
|
||||
}
|
||||
plugin.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&cConnection to redis has failed! &cReconnecting..."));
|
||||
if (this.subscribeJedis != null) {
|
||||
this.subscribeJedis.close();
|
||||
}
|
||||
}
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
plugin.sendLogs("Connecting to redis......");
|
||||
isConnecting.set(true);
|
||||
try (Jedis jedis = jedisPool.getResource()) {
|
||||
isConnectionBroken.set(false);
|
||||
plugin.sendLogs("&aConnection to redis has established!");
|
||||
jedis.subscribe(this, channelsInByte);
|
||||
} catch (Exception e) {
|
||||
isConnecting.set(false);
|
||||
isConnectionBroken.set(true);
|
||||
plugin.sendLogs("Connection has &kFAILED &cor Unable to connect to redis retrying to make connection...");
|
||||
if (debugMode) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
ConnectionTask.cancel();
|
||||
try {
|
||||
this.unsubscribe();
|
||||
} catch (Exception e) {
|
||||
plugin.sendErrorLogs("Something went wrong during unsubscribing...");
|
||||
if (debugMode) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
jedisPool.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(byte[] channel, byte[] message) {
|
||||
String channelString = new String(channel, StandardCharsets.UTF_8);
|
||||
@ -180,12 +176,15 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
plugin.sendErrorLogs("&cI got a message that was empty from channel " + channelString + " please check your code that you used to send the message. Message content:");
|
||||
if (debugMode) {
|
||||
e.printStackTrace();
|
||||
Bukkit.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&cI got a message that was empty from channel " + channelString + " please check your code that you used to send the message. Message content:"));
|
||||
Bukkit.getLogger().warning(receivedMessage);
|
||||
plugin.sendErrorLogs(receivedMessage);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void sendMessage(String[] message, String channel) {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("Messages", new JSONArray(message));
|
||||
@ -193,6 +192,7 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
|
||||
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));
|
||||
@ -209,8 +209,8 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
|
||||
public void finishSendMessage(JSONObject json, String channel) {
|
||||
try {
|
||||
byte[] message;
|
||||
if (this.getEncryption().isEncryptionEnabled()) {
|
||||
message = this.getEncryption().encrypt(json.toString());
|
||||
if (encryption.isEncryptionEnabled()) {
|
||||
message = encryption.encrypt(json.toString());
|
||||
} else {
|
||||
message = json.toString().getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
@ -218,51 +218,39 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
|
||||
//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();
|
||||
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
try (BinaryJedis j = jedisPool.getResource()) {
|
||||
j.publish(channel.getBytes(StandardCharsets.UTF_8), message);
|
||||
j.close();
|
||||
} catch (Exception e) {
|
||||
System.out.println("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
|
||||
BinaryJedis j = this.getJedisPool().getResource();
|
||||
try (BinaryJedis j = jedisPool.getResource()) {
|
||||
j.publish(channel.getBytes(StandardCharsets.UTF_8), message);
|
||||
j.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
} catch (JedisConnectionException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
|
||||
this.isShuttingDown.set(true);
|
||||
|
||||
if (this.subscribeJedis != null) {
|
||||
this.unsubscribe();
|
||||
this.subscribeJedis.close();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.jedisPool != null) {
|
||||
jedisPool.close();
|
||||
public Boolean isRedisConnectionOffline() {
|
||||
return isConnectionBroken.get();
|
||||
}
|
||||
this.RedisReconnector.shutdown();
|
||||
this.RedisService.shutdown();
|
||||
|
||||
}
|
||||
public void reload() {
|
||||
this.shutdown();
|
||||
plugin.startRedis(true);
|
||||
}
|
||||
|
||||
public JedisPool getJedisPool() {
|
||||
return jedisPool;
|
||||
}
|
||||
|
||||
public Encryption getEncryption() {
|
||||
return encryption;
|
||||
}
|
||||
|
||||
public ExecutorService getRedisService() { return RedisService; }
|
||||
}
|
@ -35,7 +35,7 @@ public class EffSendMessage extends Effect {
|
||||
Bukkit.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&2[&aRediSkript&a] &cChannel was empty. Please check your code."));
|
||||
return;
|
||||
}
|
||||
plugin.getRedisManager().sendMessage(message, channel);
|
||||
plugin.getRC().sendMessage(message, channel);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -66,10 +66,10 @@ public class ExprVariableInChannel extends SimpleExpression<Object> {
|
||||
}
|
||||
}
|
||||
String operation = mode.toString();
|
||||
plugin.getRedisManager().sendVariables(name.getAll(e), values, channel.getSingle(e), operation);
|
||||
plugin.getRC().sendVariables(name.getAll(e), values, channel.getSingle(e), operation);
|
||||
break;
|
||||
case DELETE:
|
||||
plugin.getRedisManager().sendVariables(name.getAll(e), null, channel.getSingle(e), "SET");
|
||||
plugin.getRC().sendVariables(name.getAll(e), null, channel.getSingle(e), "SET");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
main: net.limework.rediskript.RediSkript
|
||||
name: ${project.name}
|
||||
version: ${project.version}
|
||||
authors: [Govindas, ham1255, DaemonicKing]
|
||||
authors: [Govindas, ham1255, DaemonicKing, limework.net]
|
||||
api-version: 1.13
|
||||
depend: [Skript]
|
||||
commands:
|
||||
|
Loading…
Reference in New Issue
Block a user