New /reloadredis fully restarts the plugin with reloaded configuration & some code improvements

This commit is contained in:
Govindass 2020-10-23 14:34:30 +03:00
parent 2049616f64
commit 1ef35dfb60
5 changed files with 82 additions and 58 deletions

View File

@ -1,36 +1,38 @@
package net.limework.core; package net.limework.core;
import net.limework.core.commands.ReloadRedis;
import net.limework.core.hooks.SkriptHook; import net.limework.core.hooks.SkriptHook;
import net.limework.core.managers.RedisManager; import net.limework.core.managers.RedisManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.util.Objects;
public class RediSkript extends JavaPlugin { public class RediSkript extends JavaPlugin {
//Redis manager //Redis manager
private RedisManager rm; private RedisManager rm;
public void startRedis(boolean reloadConfig) {
if (reloadConfig) {
reloadConfig();
}
rm = new RedisManager(this);
rm.start();
}
@Override @Override
public void onEnable() { public void onEnable() {
saveDefaultConfig(); saveDefaultConfig();
rm = new RedisManager(this);
Objects.requireNonNull(getServer().getPluginCommand("reloadredis")).setExecutor(rm);
if (getServer().getPluginManager().getPlugin("Skript") != null) { if (getServer().getPluginManager().getPlugin("Skript") != null) {
startRedis(false);
getServer().getPluginCommand("reloadredis").setExecutor(new ReloadRedis(this));
new SkriptHook(this); new SkriptHook(this);
} else { } else {
getLogger().info("Skript wasn't found."); getLogger().info("Skript wasn't found.");
} }
rm.start();
} }
@Override @Override
public void onDisable() { public void onDisable() {
rm.shutdown(); if (rm != null) {
rm.shutdown();
}
} }

View File

@ -0,0 +1,28 @@
package net.limework.core.commands;
import net.limework.core.RediSkript;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class ReloadRedis implements CommandExecutor {
private RediSkript plugin;
public ReloadRedis(RediSkript plugin) {
this.plugin = plugin;
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
sender.sendMessage(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&'
, "&cYou cannot execute this command.")));
return true;
}
plugin.getRm().reloadRedis();
sender.sendMessage(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&'
, "&eRediSkript has been reloaded!")));
return false;
}
}

View File

@ -1,16 +1,11 @@
package net.limework.core.managers; package net.limework.core.managers;
import net.limework.core.RediSkript; import net.limework.core.RediSkript;
import net.limework.data.Encryption;
import net.limework.core.events.RedisMessageEvent; import net.limework.core.events.RedisMessageEvent;
import net.md_5.bungee.api.chat.TextComponent; import net.limework.data.Encryption;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.Configuration; import org.bukkit.configuration.Configuration;
import org.bukkit.entity.Player;
import org.cryptomator.siv.UnauthenticCiphertextException; import org.cryptomator.siv.UnauthenticCiphertextException;
import org.json.JSONObject; import org.json.JSONObject;
import redis.clients.jedis.BinaryJedis; import redis.clients.jedis.BinaryJedis;
@ -25,7 +20,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
public class RedisManager extends BinaryJedisPubSub implements Runnable, CommandExecutor { public class RedisManager extends BinaryJedisPubSub implements Runnable {
private RediSkript plugin; private RediSkript plugin;
@ -39,7 +34,7 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable, Command
private BinaryJedis subscribeJedis; private BinaryJedis subscribeJedis;
private List<String> channels; private List<String> channels;
private AtomicBoolean isShuttingDown = new AtomicBoolean(false); private AtomicBoolean isShuttingDown = new AtomicBoolean(false);
private AtomicBoolean isRedisOnline = new AtomicBoolean(); private AtomicBoolean isOnline = new AtomicBoolean();
private Encryption encryption; private Encryption encryption;
@ -76,10 +71,9 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable, Command
while (!isShuttingDown.get()) { while (!isShuttingDown.get()) {
isKilled.set(false); isKilled.set(false);
try { try {
message("&e[Jedis] &cConnecting to redis..........."); message("&2[&aRediSkript&a] &cConnecting to redis...");
if (!this.subscribeJedis.isConnected()) this.subscribeJedis = this.jedisPool.getResource(); if (!this.subscribeJedis.isConnected()) this.subscribeJedis = this.jedisPool.getResource();
isRedisOnline.set(true); message("&2[&aRediSkript&a] &aRedis connected!");
message("&e[Jedis] &aRedis Connected");
int byteArr2dSize = 1; int byteArr2dSize = 1;
byte[][] channelsInByte = new byte[channels.size()][byteArr2dSize]; byte[][] channelsInByte = new byte[channels.size()][byteArr2dSize];
boolean reInitializeByteArray; boolean reInitializeByteArray;
@ -102,11 +96,10 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable, Command
this.subscribeJedis.subscribe(this, channelsInByte); this.subscribeJedis.subscribe(this, channelsInByte);
} catch (Exception e) { } catch (Exception e) {
message("&e[Jedis] &cConnection to redis has failed! &ereconnecting..."); message("&2[&aRediSkript&a] &cConnection to redis has failed! &ereconnecting...");
if (this.subscribeJedis != null) { if (this.subscribeJedis != null) {
this.subscribeJedis.close(); this.subscribeJedis.close();
} }
isRedisOnline.set(false);
} }
try { try {
Thread.sleep(1000); Thread.sleep(1000);
@ -162,10 +155,6 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable, Command
} }
public boolean IsRedisOnline() {
return isRedisOnline.get();
}
public JedisPool getJedisPool() { public JedisPool getJedisPool() {
return jedisPool; return jedisPool;
} }
@ -174,27 +163,12 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable, Command
return RedisService; return RedisService;
} }
public AtomicBoolean getIsShuttingDown() { public AtomicBoolean isShuttingDown() {
return isShuttingDown; return isShuttingDown;
} }
public AtomicBoolean getIsRedisOnline() { public void reloadRedis() {
return isRedisOnline; this.isKilled.set(true);
}
public Encryption getEncryption() {
return encryption;
}
// the /reloadredis command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String lable, String[] args) {
if (sender instanceof Player) {
sender.sendMessage(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&'
, "&cYou cannot execute this command.")));
return true;
}
isKilled.set(true);
try { try {
if (this.subscribeJedis != null) { if (this.subscribeJedis != null) {
this.unsubscribe(); this.unsubscribe();
@ -203,7 +177,13 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable, Command
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
start(); this.shutdown();
return false; plugin.startRedis(true);
}
public Encryption getEncryption() {
return encryption;
} }
} }

View File

@ -6,6 +6,7 @@ import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser; import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean; import ch.njol.util.Kleenean;
import net.limework.core.RediSkript; import net.limework.core.RediSkript;
import net.limework.core.managers.RedisManager;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.event.Event; import org.bukkit.event.Event;
@ -16,7 +17,7 @@ import java.nio.charset.StandardCharsets;
public class EffSendMessage extends Effect { public class EffSendMessage extends Effect {
static { static {
Skript.registerEffect(EffSendMessage.class, "send redis message to channel %string% with message %string%"); Skript.registerEffect(EffSendMessage.class, "send redis message to channel %string% with [message] %string%", "send redis message %string% to [channel] %string%");
} }
@ -26,23 +27,31 @@ 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) Bukkit.getPluginManager().getPlugin("RediSkript");
String message = this.message.getSingle(event); String message = this.message.getSingle(event);
String channel = this.channel.getSingle(event); String channel = this.channel.getSingle(event);
if (message == null) {//checks if message equals null if true does not execute.
Bukkit.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&2[&aGBot&a] &cMessage Was empty Please check your code.")); if (message == null) {
Bukkit.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&2[&aRediSkript&a] &cRedis message was empty. Please check your code."));
return;
}
if (channel == null) {
Bukkit.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&2[&aRediSkript&a] &cChannel was empty. Please check your code."));
return; return;
} }
assert plugin != null; assert plugin != null;
plugin.getRm().getRedisService().execute(() -> { RedisManager manager = plugin.getRm();
BinaryJedis j = plugin.getRm().getJedisPool().getResource(); manager.getRedisService().execute(() -> {
BinaryJedis j = manager.getJedisPool().getResource();
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("Message", message); json.put("Message", message);
json.put("Type", "Skript"); json.put("Type", "Skript");
json.put("Date", System.currentTimeMillis()); //for unique string every time & PING calculations json.put("Date", System.currentTimeMillis()); //for unique string every time & PING calculations
byte[] msg; byte[] msg;
if (plugin.getRm().getEncryption().isEncryptionEnabled()) { if (manager.getEncryption().isEncryptionEnabled()) {
msg = plugin.getRm().getEncryption().encrypt(json.toString()); msg = manager.getEncryption().encrypt(json.toString());
} else { } else {
msg = json.toString().getBytes(StandardCharsets.UTF_8); msg = json.toString().getBytes(StandardCharsets.UTF_8);
} }
@ -60,8 +69,13 @@ public class EffSendMessage extends Effect {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parser) { public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parser) {
this.channel = (Expression<String>) expressions[0]; if (matchedPattern == 0) {
this.message = (Expression<String>) expressions[1]; this.channel = (Expression<String>) expressions[0];
this.message = (Expression<String>) expressions[1];
} else {
this.channel = (Expression<String>) expressions[1];
this.message = (Expression<String>) expressions[0];
}
return true; return true;
} }

View File

@ -3,7 +3,7 @@ name: RediSkript
version: ${project.version} version: ${project.version}
authors: [Govindas, ham1255, DaemonicKing] authors: [Govindas, ham1255, DaemonicKing]
api-version: 1.13 api-version: 1.13
softdepend: depend:
- Skript - Skript
commands: commands:
reloadredis: reloadredis: