Fix wrong class name & Fully fix sending messages while disabled

This commit is contained in:
Govindas 2020-11-03 11:29:06 +02:00
parent f35ad377d1
commit f53f9dd5a2
5 changed files with 38 additions and 13 deletions

View File

@ -6,7 +6,7 @@
<groupId>net.limework.rediskript</groupId>
<artifactId>RediSkript</artifactId>
<version>1.2.2</version>
<version>1.2.5</version>
<packaging>jar</packaging>
<build>

View File

@ -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;
}

View File

@ -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,8 +133,13 @@ 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"));
//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();
Bukkit.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&cI got a message that was empty from channel " + channelString + " please check your code that you used to send the message. Message content:"));
@ -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();
}

View File

@ -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<String, RedisMessageEvent>() {

View File

@ -55,11 +55,23 @@ public class EffSendMessage extends Effect {
msg = json.toString().getBytes(StandardCharsets.UTF_8);
}
try {
//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();
}