RediSkript/src/main/java/net/limework/skLimework/Events/RedisSub.java

95 lines
3.4 KiB
Java
Raw Normal View History

2020-05-10 06:52:06 +00:00
package net.limework.skLimework.Events;
import net.limework.skLimework.AddonPlugin;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
2020-06-08 13:53:23 +00:00
import org.cryptomator.siv.UnauthenticCiphertextException;
2020-05-10 06:52:06 +00:00
import org.json.JSONObject;
2020-06-08 13:53:23 +00:00
import redis.clients.jedis.BinaryJedis;
import redis.clients.jedis.BinaryJedisPubSub;
2020-05-10 06:52:06 +00:00
2020-06-08 13:53:23 +00:00
import javax.crypto.IllegalBlockSizeException;
2020-05-10 06:52:06 +00:00
import java.util.List;
2020-06-14 13:43:46 +00:00
import java.util.concurrent.atomic.AtomicBoolean;
2020-05-10 06:52:06 +00:00
2020-05-10 19:10:45 +00:00
2020-06-08 13:53:23 +00:00
public class RedisSub extends BinaryJedisPubSub implements Runnable{
2020-05-10 19:10:45 +00:00
2020-05-10 06:52:06 +00:00
private AddonPlugin plugin;
2020-06-08 13:53:23 +00:00
private BinaryJedis j;
private List<String> channels;
2020-06-14 13:43:46 +00:00
private AtomicBoolean isShuttingDown = new AtomicBoolean(false);
private AtomicBoolean isRedisOnline = new AtomicBoolean();
2020-05-10 19:10:45 +00:00
2020-06-08 13:53:23 +00:00
public RedisSub(AddonPlugin plugin, BinaryJedis j, List<String> channels) {
2020-05-10 06:52:06 +00:00
this.plugin = plugin;
this.j = j;
2020-06-08 13:53:23 +00:00
this.channels = channels;
2020-05-10 19:10:45 +00:00
}
2020-05-10 19:10:45 +00:00
@Override
public void run(){
2020-06-14 13:43:46 +00:00
while (!isShuttingDown.get()){
2020-05-10 19:10:45 +00:00
try {
2020-06-14 13:43:46 +00:00
message("&e[Jedis] &cConnecting to redis...........");
if (!this.j.isConnected()) this.j = plugin.getJedisPool().getResource();
isRedisOnline.set(true);
message("&e[Jedis] &aRedis Connected");
this.j.subscribe(this,
channels.get(0).getBytes(),
channels.get(1).getBytes(),
channels.get(2).getBytes(),
channels.get(3).getBytes(),
channels.get(4).getBytes());
} catch (Exception e){
message("&e[Jedis] &cConnection to redis has failed! &ereconnecting...");
this.j.close();
isRedisOnline.set(false);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
2020-05-10 19:10:45 +00:00
}
}
2020-05-10 06:52:06 +00:00
}
2020-06-14 13:43:46 +00:00
private void message(String message){
plugin.getLogger().info(ChatColor.translateAlternateColorCodes('&', message));
}
2020-05-10 19:10:45 +00:00
2020-05-10 06:52:06 +00:00
@Override
2020-06-08 13:53:23 +00:00
public void onMessage(byte[] channel, byte[] message) {
AddonPlugin plugin = (AddonPlugin) Bukkit.getPluginManager().getPlugin("SKLimework");
String channelString = new String(channel);
2020-05-10 06:52:06 +00:00
try {
2020-06-08 13:53:23 +00:00
String decrypted = null;
try {
assert plugin != null;
decrypted = plugin.decrypt(message);
} catch (UnauthenticCiphertextException | IllegalBlockSizeException e) {
e.printStackTrace();
}
assert decrypted != null;
JSONObject j = new JSONObject(decrypted);
2020-05-10 06:52:06 +00:00
//System.out.println("Message got from channel: "+channel +" and the Message: " +json.toString());
2020-06-08 13:53:23 +00:00
plugin.getServer().getPluginManager().callEvent(new onRedisMessage(channelString, j.getString("Message")));
2020-05-10 06:52:06 +00:00
} catch (Exception e) {
e.printStackTrace();
Bukkit.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&2[&aGBot&a] &cI Got a Message that Was empty from channel "+ channel +" Please check your code that you used to send the message. ^ ignore the error."));
}
}
2020-05-10 19:10:45 +00:00
public void shutdown(){
2020-06-14 13:43:46 +00:00
this.isShuttingDown.set(true);
2020-05-10 06:52:06 +00:00
this.unsubscribe();
j.close();
}
2020-05-10 19:10:45 +00:00
2020-06-14 13:43:46 +00:00
public boolean IsRedisOnline() {
return isRedisOnline.get();
}
2020-05-10 19:10:45 +00:00
}