forked from Limework/RediSkript
		
	New /reloadredis fully restarts the plugin with reloaded configuration & some code improvements
This commit is contained in:
		
							parent
							
								
									94f90efcf1
								
							
						
					
					
						commit
						3992ff508e
					
				@ -1,37 +1,39 @@
 | 
			
		||||
package net.limework.core;
 | 
			
		||||
import net.limework.core.commands.ReloadRedis;
 | 
			
		||||
import net.limework.core.hooks.SkriptHook;
 | 
			
		||||
import net.limework.core.managers.RedisManager;
 | 
			
		||||
import org.bukkit.plugin.java.JavaPlugin;
 | 
			
		||||
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
 | 
			
		||||
public class RediSkript extends JavaPlugin {
 | 
			
		||||
 | 
			
		||||
    //Redis manager
 | 
			
		||||
    private RedisManager rm;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public void startRedis(boolean reloadConfig) {
 | 
			
		||||
        if (reloadConfig) {
 | 
			
		||||
            reloadConfig();
 | 
			
		||||
        }
 | 
			
		||||
        rm = new RedisManager(this);
 | 
			
		||||
        rm.start();
 | 
			
		||||
    }
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onEnable() {
 | 
			
		||||
        saveDefaultConfig();
 | 
			
		||||
        rm = new RedisManager(this);
 | 
			
		||||
        Objects.requireNonNull(getServer().getPluginCommand("reloadredis")).setExecutor(rm);
 | 
			
		||||
        if (getServer().getPluginManager().getPlugin("Skript") != null) {
 | 
			
		||||
            startRedis(false);
 | 
			
		||||
            getServer().getPluginCommand("reloadredis").setExecutor(new ReloadRedis(this));
 | 
			
		||||
            new SkriptHook(this);
 | 
			
		||||
        } else {
 | 
			
		||||
            getLogger().info("Skript wasn't found.");
 | 
			
		||||
        }
 | 
			
		||||
        rm.start();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onDisable() {
 | 
			
		||||
        if (rm != null) {
 | 
			
		||||
            rm.shutdown();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public RedisManager getRm() {
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										28
									
								
								src/main/java/net/limework/core/commands/ReloadRedis.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/main/java/net/limework/core/commands/ReloadRedis.java
									
									
									
									
									
										Normal 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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -1,16 +1,11 @@
 | 
			
		||||
package net.limework.core.managers;
 | 
			
		||||
 | 
			
		||||
import net.limework.core.RediSkript;
 | 
			
		||||
import net.limework.data.Encryption;
 | 
			
		||||
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.ChatColor;
 | 
			
		||||
import org.bukkit.command.Command;
 | 
			
		||||
import org.bukkit.command.CommandExecutor;
 | 
			
		||||
import org.bukkit.command.CommandSender;
 | 
			
		||||
import org.bukkit.configuration.Configuration;
 | 
			
		||||
import org.bukkit.entity.Player;
 | 
			
		||||
import org.cryptomator.siv.UnauthenticCiphertextException;
 | 
			
		||||
import org.json.JSONObject;
 | 
			
		||||
import redis.clients.jedis.BinaryJedis;
 | 
			
		||||
@ -25,7 +20,7 @@ import java.util.concurrent.ExecutorService;
 | 
			
		||||
import java.util.concurrent.Executors;
 | 
			
		||||
import java.util.concurrent.atomic.AtomicBoolean;
 | 
			
		||||
 | 
			
		||||
public class RedisManager extends BinaryJedisPubSub implements Runnable, CommandExecutor {
 | 
			
		||||
public class RedisManager extends BinaryJedisPubSub implements Runnable {
 | 
			
		||||
 | 
			
		||||
    private RediSkript plugin;
 | 
			
		||||
 | 
			
		||||
@ -39,7 +34,7 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable, Command
 | 
			
		||||
    private BinaryJedis subscribeJedis;
 | 
			
		||||
    private List<String> channels;
 | 
			
		||||
    private AtomicBoolean isShuttingDown = new AtomicBoolean(false);
 | 
			
		||||
    private AtomicBoolean isRedisOnline = new AtomicBoolean();
 | 
			
		||||
    private AtomicBoolean isOnline = new AtomicBoolean();
 | 
			
		||||
    private Encryption encryption;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -76,10 +71,9 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable, Command
 | 
			
		||||
        while (!isShuttingDown.get()) {
 | 
			
		||||
            isKilled.set(false);
 | 
			
		||||
            try {
 | 
			
		||||
                message("&e[Jedis] &cConnecting to redis...........");
 | 
			
		||||
                message("&2[&aRediSkript&a] &cConnecting to redis...");
 | 
			
		||||
                if (!this.subscribeJedis.isConnected()) this.subscribeJedis = this.jedisPool.getResource();
 | 
			
		||||
                isRedisOnline.set(true);
 | 
			
		||||
                message("&e[Jedis] &aRedis Connected");
 | 
			
		||||
                message("&2[&aRediSkript&a] &aRedis connected!");
 | 
			
		||||
                int byteArr2dSize = 1;
 | 
			
		||||
                byte[][] channelsInByte = new byte[channels.size()][byteArr2dSize];
 | 
			
		||||
                boolean reInitializeByteArray;
 | 
			
		||||
@ -102,11 +96,10 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable, Command
 | 
			
		||||
                this.subscribeJedis.subscribe(this, channelsInByte);
 | 
			
		||||
 | 
			
		||||
            } 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) {
 | 
			
		||||
                    this.subscribeJedis.close();
 | 
			
		||||
                }
 | 
			
		||||
                isRedisOnline.set(false);
 | 
			
		||||
            }
 | 
			
		||||
            try {
 | 
			
		||||
                Thread.sleep(1000);
 | 
			
		||||
@ -162,10 +155,6 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable, Command
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public boolean IsRedisOnline() {
 | 
			
		||||
        return isRedisOnline.get();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public JedisPool getJedisPool() {
 | 
			
		||||
        return jedisPool;
 | 
			
		||||
    }
 | 
			
		||||
@ -174,27 +163,12 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable, Command
 | 
			
		||||
        return RedisService;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public AtomicBoolean getIsShuttingDown() {
 | 
			
		||||
    public AtomicBoolean isShuttingDown() {
 | 
			
		||||
        return isShuttingDown;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public AtomicBoolean getIsRedisOnline() {
 | 
			
		||||
        return isRedisOnline;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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);
 | 
			
		||||
    public void reloadRedis() {
 | 
			
		||||
        this.isKilled.set(true);
 | 
			
		||||
        try {
 | 
			
		||||
            if (this.subscribeJedis != null) {
 | 
			
		||||
                this.unsubscribe();
 | 
			
		||||
@ -203,7 +177,13 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable, Command
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
        start();
 | 
			
		||||
        return false;
 | 
			
		||||
        this.shutdown();
 | 
			
		||||
        plugin.startRedis(true);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public Encryption getEncryption() {
 | 
			
		||||
        return encryption;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -6,6 +6,7 @@ import ch.njol.skript.lang.Expression;
 | 
			
		||||
import ch.njol.skript.lang.SkriptParser;
 | 
			
		||||
import ch.njol.util.Kleenean;
 | 
			
		||||
import net.limework.core.RediSkript;
 | 
			
		||||
import net.limework.core.managers.RedisManager;
 | 
			
		||||
import org.bukkit.Bukkit;
 | 
			
		||||
import org.bukkit.ChatColor;
 | 
			
		||||
import org.bukkit.event.Event;
 | 
			
		||||
@ -16,7 +17,7 @@ import java.nio.charset.StandardCharsets;
 | 
			
		||||
 | 
			
		||||
public class EffSendMessage extends Effect {
 | 
			
		||||
    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
 | 
			
		||||
    protected void execute(Event event) {
 | 
			
		||||
 | 
			
		||||
        RediSkript plugin = (RediSkript) Bukkit.getPluginManager().getPlugin("RediSkript");
 | 
			
		||||
 | 
			
		||||
        String message = this.message.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;
 | 
			
		||||
        }
 | 
			
		||||
        assert plugin != null;
 | 
			
		||||
        plugin.getRm().getRedisService().execute(() -> {
 | 
			
		||||
            BinaryJedis j = plugin.getRm().getJedisPool().getResource();
 | 
			
		||||
        RedisManager manager = plugin.getRm();
 | 
			
		||||
        manager.getRedisService().execute(() -> {
 | 
			
		||||
            BinaryJedis j = manager.getJedisPool().getResource();
 | 
			
		||||
            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;
 | 
			
		||||
            if (plugin.getRm().getEncryption().isEncryptionEnabled()) {
 | 
			
		||||
                msg = plugin.getRm().getEncryption().encrypt(json.toString());
 | 
			
		||||
            if (manager.getEncryption().isEncryptionEnabled()) {
 | 
			
		||||
                msg = manager.getEncryption().encrypt(json.toString());
 | 
			
		||||
            } else {
 | 
			
		||||
                msg = json.toString().getBytes(StandardCharsets.UTF_8);
 | 
			
		||||
            }
 | 
			
		||||
@ -60,8 +69,13 @@ public class EffSendMessage extends Effect {
 | 
			
		||||
    @SuppressWarnings("unchecked")
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parser) {
 | 
			
		||||
        if (matchedPattern == 0) {
 | 
			
		||||
            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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -3,7 +3,7 @@ name: RediSkript
 | 
			
		||||
version: ${project.version}
 | 
			
		||||
authors: [Govindas, ham1255, DaemonicKing]
 | 
			
		||||
api-version: 1.13
 | 
			
		||||
softdepend:
 | 
			
		||||
depend:
 | 
			
		||||
  - Skript
 | 
			
		||||
commands:
 | 
			
		||||
  reloadredis:
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user