fixed conflict and removed hard coded channels

This commit is contained in:
ham1255 2020-06-26 17:02:39 +04:00
parent 68788af789
commit e3edfd84fc
2 changed files with 29 additions and 14 deletions

View File

@ -62,7 +62,7 @@ public class AddonPlugin extends JavaPlugin {
addon = Skript.registerAddon(this); addon = Skript.registerAddon(this);
try { addon.loadClasses("net.limework.skLimework", "elements"); try { addon.loadClasses("net.limework.skLimework", "elements");
Skript.registerEvent("redis message", EvtRedis.class, onRedisMessage.class, "redis message"); Skript.registerEvent("redis message", EvtRedis.class, onRedisMessage.class, "redis message");
Skript.registerExpression(ExprChannel.class, String.class, ExpressionType.SIMPLE, "channel"); Skript.registerExpression(ExprChannel.class, String.class, ExpressionType.SIMPLE, "redis channel");
EventValues.registerEventValue(onRedisMessage.class, String.class, new Getter<String, onRedisMessage>() { EventValues.registerEventValue(onRedisMessage.class, String.class, new Getter<String, onRedisMessage>() {
@Override @Override
public String get(onRedisMessage e) { public String get(onRedisMessage e) {

View File

@ -13,7 +13,7 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
public class RedisSub extends BinaryJedisPubSub implements Runnable{ public class RedisSub extends BinaryJedisPubSub implements Runnable {
private AddonPlugin plugin; private AddonPlugin plugin;
private BinaryJedis j; private BinaryJedis j;
@ -29,20 +29,35 @@ public class RedisSub extends BinaryJedisPubSub implements Runnable{
} }
@Override @Override
public void run(){ public void run() {
while (!isShuttingDown.get()){ while (!isShuttingDown.get()) {
try { try {
message("&e[Jedis] &cConnecting to redis..........."); message("&e[Jedis] &cConnecting to redis...........");
if (!this.j.isConnected()) this.j = plugin.getJedisPool().getResource(); if (!this.j.isConnected()) this.j = plugin.getJedisPool().getResource();
isRedisOnline.set(true); isRedisOnline.set(true);
message("&e[Jedis] &aRedis Connected"); message("&e[Jedis] &aRedis Connected");
this.j.subscribe(this, int byteArr2dSize = 1;
channels.get(0).getBytes(), byte[][] channelsInByte = new byte[channels.size()][byteArr2dSize];
channels.get(1).getBytes(), boolean reInitializeByteArray;
channels.get(2).getBytes(),
channels.get(3).getBytes(), // Loop that reInitialize array IF array size is not enough
channels.get(4).getBytes()); do {
} catch (Exception e){ reInitializeByteArray = false;
try {
/* Data Initialization for channelsInByte array from List<String> channels */
for (int x = 0; x < channels.size(); x++) {
channelsInByte[x] = channels.get(x).getBytes();
}
} catch (ArrayIndexOutOfBoundsException ex) {
reInitializeByteArray = true;
/* Increase the current 2d array size to increase 1 and reinitialize the array*/
byteArr2dSize += 1;
channelsInByte = new byte[channels.size()][byteArr2dSize];
}
} while (reInitializeByteArray);
this.j.subscribe(this, channelsInByte);
} catch (Exception e) {
message("&e[Jedis] &cConnection to redis has failed! &ereconnecting..."); message("&e[Jedis] &cConnection to redis has failed! &ereconnecting...");
this.j.close(); this.j.close();
isRedisOnline.set(false); isRedisOnline.set(false);
@ -55,7 +70,7 @@ public class RedisSub extends BinaryJedisPubSub implements Runnable{
} }
} }
private void message(String message){ private void message(String message) {
plugin.getLogger().info(ChatColor.translateAlternateColorCodes('&', message)); plugin.getLogger().info(ChatColor.translateAlternateColorCodes('&', message));
} }
@ -77,12 +92,12 @@ public class RedisSub extends BinaryJedisPubSub implements Runnable{
plugin.getServer().getPluginManager().callEvent(new onRedisMessage(channelString, j.getString("Message"))); plugin.getServer().getPluginManager().callEvent(new onRedisMessage(channelString, j.getString("Message")));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); 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.")); 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."));
} }
} }
public void shutdown(){ public void shutdown() {
this.isShuttingDown.set(true); this.isShuttingDown.set(true);
this.unsubscribe(); this.unsubscribe();
j.close(); j.close();