From 705ce16f5d48d87d8cd839e2d245ca9813bb32d9 Mon Sep 17 00:00:00 2001 From: Govindass Date: Tue, 3 Nov 2020 11:29:06 +0200 Subject: [PATCH] Fix wrong class name & Fully fix sending messages while disabled --- pom.xml | 2 +- .../net/limework/rediskript/RediSkript.java | 7 ++++--- .../rediskript/managers/RedisManager.java | 20 ++++++++++++++++--- .../rediskript/skript/SkriptHook.java | 6 ++---- .../skript/elements/EffSendMessage.java | 16 +++++++++++++-- 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/pom.xml b/pom.xml index 1bbb1e5..47a2e5c 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ net.limework.rediskript RediSkript - 1.2.2 + 1.2.5 jar diff --git a/src/main/java/net/limework/rediskript/RediSkript.java b/src/main/java/net/limework/rediskript/RediSkript.java index e09a3b7..55c8d9a 100644 --- a/src/main/java/net/limework/rediskript/RediSkript.java +++ b/src/main/java/net/limework/rediskript/RediSkript.java @@ -2,11 +2,14 @@ package net.limework.rediskript; import net.limework.rediskript.commands.CommandReloadRedis; import net.limework.rediskript.skript.SkriptHook; import net.limework.rediskript.managers.RedisManager; +import org.bukkit.Bukkit; import org.bukkit.command.PluginCommand; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.plugin.java.JavaPlugin; +import java.util.concurrent.TimeUnit; + public class RediSkript extends JavaPlugin { //Redis manager @@ -31,14 +34,12 @@ public class RediSkript extends JavaPlugin { @Override //using HIGHEST event priority so it shuts down last and code can still execute well in "on script unload" and "on skript unload" events - @EventHandler(priority = EventPriority.HIGHEST) + @EventHandler(priority = EventPriority.LOWEST) public void onDisable() { if (rm != null) { rm.shutdown(); } } - - public RedisManager getRm() { return rm; } diff --git a/src/main/java/net/limework/rediskript/managers/RedisManager.java b/src/main/java/net/limework/rediskript/managers/RedisManager.java index 090f030..e5cd3f1 100644 --- a/src/main/java/net/limework/rediskript/managers/RedisManager.java +++ b/src/main/java/net/limework/rediskript/managers/RedisManager.java @@ -18,10 +18,12 @@ import java.nio.charset.StandardCharsets; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; public class RedisManager extends BinaryJedisPubSub implements Runnable { + private final ExecutorService RedisReconnector; private RediSkript plugin; private JedisPool jedisPool; @@ -52,7 +54,8 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable { config.getInt("Redis.TimeOut"), config.getString("Redis.Password"), config.getBoolean("Redis.useTLS")); - RedisService = Executors.newFixedThreadPool(3); + RedisReconnector = Executors.newSingleThreadExecutor(); + RedisService = Executors.newSingleThreadExecutor(); try { this.subscribeJedis = this.jedisPool.getResource(); } catch (Exception ignored) { @@ -63,7 +66,7 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable { } public void start() { - this.RedisService.execute(this); + this.RedisReconnector.execute(this); } @Override @@ -130,7 +133,12 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable { JSONObject j = new JSONObject(receivedMessage); //System.out.println("Message got from channel: "+channel +" and the Message: " +json.toString()); RedisMessageEvent event = new RedisMessageEvent(channelString, j.getString("Message"), j.getLong("Date")); - Bukkit.getScheduler().runTask(plugin, () -> plugin.getServer().getPluginManager().callEvent(event)); + + //if plugin is disabling, don't call events anymore + + if (plugin.isEnabled()) { + Bukkit.getScheduler().runTask(plugin, () -> plugin.getServer().getPluginManager().callEvent(event)); + } } } catch (Exception e) { e.printStackTrace(); @@ -141,6 +149,11 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable { } public void shutdown() { + try { + this.RedisService.awaitTermination(1, TimeUnit.SECONDS); + } catch (InterruptedException e) { + e.printStackTrace(); + } this.isShuttingDown.set(true); if (this.subscribeJedis != null) { this.unsubscribe(); @@ -148,6 +161,7 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable { this.subscribeJedis.getClient().close(); this.jedisPool.getResource().close(); } + this.RedisReconnector.shutdown(); this.RedisService.shutdown(); } diff --git a/src/main/java/net/limework/rediskript/skript/SkriptHook.java b/src/main/java/net/limework/rediskript/skript/SkriptHook.java index 27bf0f3..5f54935 100644 --- a/src/main/java/net/limework/rediskript/skript/SkriptHook.java +++ b/src/main/java/net/limework/rediskript/skript/SkriptHook.java @@ -16,12 +16,10 @@ import net.limework.rediskript.skript.elements.ExprMessageDate; import java.io.IOException; public class SkriptHook { - - private SkriptAddon addon; public SkriptHook(RediSkript plugin) { - addon = Skript.registerAddon(plugin); + SkriptAddon addon = Skript.registerAddon(plugin); try { - addon.loadClasses("net.limework.core.skript", "elements"); + addon.loadClasses("net.limework.rediskript.skript", "elements"); Skript.registerEvent("redis message", EvtRedis.class, RedisMessageEvent.class, "redis message"); Skript.registerExpression(ExprChannel.class, String.class, ExpressionType.SIMPLE, "redis channel"); EventValues.registerEventValue(RedisMessageEvent.class, String.class, new Getter() { diff --git a/src/main/java/net/limework/rediskript/skript/elements/EffSendMessage.java b/src/main/java/net/limework/rediskript/skript/elements/EffSendMessage.java index 427d704..f0ce436 100644 --- a/src/main/java/net/limework/rediskript/skript/elements/EffSendMessage.java +++ b/src/main/java/net/limework/rediskript/skript/elements/EffSendMessage.java @@ -55,11 +55,23 @@ public class EffSendMessage extends Effect { msg = json.toString().getBytes(StandardCharsets.UTF_8); } try { - manager.getRedisService().execute(() -> { + + //execute sending of redis message on the main thread if plugin is disabling + //so it can still process the sending + + //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()) { + manager.getRedisService().execute(() -> { + BinaryJedis j = manager.getJedisPool().getResource(); + j.publish(channel.getBytes(StandardCharsets.UTF_8), msg); + j.close(); + }); + } else { BinaryJedis j = manager.getJedisPool().getResource(); j.publish(channel.getBytes(StandardCharsets.UTF_8), msg); j.close(); - }); + } } catch (JedisConnectionException exception) { exception.printStackTrace(); }