From df9297e324233700f7b6b19d82e549febd56b70b Mon Sep 17 00:00:00 2001 From: Govindas Date: Fri, 23 Oct 2020 16:49:25 +0300 Subject: [PATCH] add redis message date expression & improve quality of existing expressions & make event guaranteedly synchronous --- .../java/net/limework/core/RediSkript.java | 6 +-- .../limework/core/commands/ReloadRedis.java | 6 +-- .../core/events/RedisMessageEvent.java | 6 ++- .../net/limework/core/hooks/SkriptHook.java | 13 +++-- .../limework/core/managers/RedisManager.java | 40 ++++----------- .../core/skript/elements/EvtRedis.java | 6 ++- .../core/skript/elements/ExprChannel.java | 9 +++- .../core/skript/elements/ExprMessage.java | 11 ++++- .../core/skript/elements/ExprMessageDate.java | 49 +++++++++++++++++++ src/main/resources/plugin.yml | 3 +- 10 files changed, 101 insertions(+), 48 deletions(-) create mode 100644 src/main/java/net/limework/core/skript/elements/ExprMessageDate.java diff --git a/src/main/java/net/limework/core/RediSkript.java b/src/main/java/net/limework/core/RediSkript.java index d2a2f6a..cf33670 100644 --- a/src/main/java/net/limework/core/RediSkript.java +++ b/src/main/java/net/limework/core/RediSkript.java @@ -9,10 +9,8 @@ public class RediSkript extends JavaPlugin { //Redis manager private RedisManager rm; - public void startRedis(boolean reloadConfig) { - if (reloadConfig) { - reloadConfig(); - } + public void startRedis(boolean reload) { + if (reload) { reloadConfig(); } rm = new RedisManager(this); rm.start(); } diff --git a/src/main/java/net/limework/core/commands/ReloadRedis.java b/src/main/java/net/limework/core/commands/ReloadRedis.java index 76f28b4..a66ac7f 100644 --- a/src/main/java/net/limework/core/commands/ReloadRedis.java +++ b/src/main/java/net/limework/core/commands/ReloadRedis.java @@ -17,12 +17,12 @@ public class ReloadRedis implements CommandExecutor { 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."))); + , "&2[&aRediSkript&a] &cThis command can only be executed in console."))); return true; } - plugin.getRm().reloadRedis(); + plugin.getRm().reload(); sender.sendMessage(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&' - , "&eRediSkript has been reloaded!"))); + , "&2[&aRediSkript&a] &eReloaded! Please note that this command is not guaranteed to work correctly, if you face any problem caused by this command, please report it on github.com/Limework/RediSkript/issues"))); return false; } } diff --git a/src/main/java/net/limework/core/events/RedisMessageEvent.java b/src/main/java/net/limework/core/events/RedisMessageEvent.java index fe6965b..dfaa32f 100644 --- a/src/main/java/net/limework/core/events/RedisMessageEvent.java +++ b/src/main/java/net/limework/core/events/RedisMessageEvent.java @@ -9,11 +9,13 @@ public class RedisMessageEvent extends Event { private String channelName; private String message; + private long date; - public RedisMessageEvent(String channelName , String message) { + public RedisMessageEvent(String channelName , String message, long date) { super(true); this.channelName = channelName; this.message = message; + this.date = date; } @@ -34,6 +36,8 @@ public class RedisMessageEvent extends Event { return channelName; } + public long getDate() { return date;} + public String getMessage() { return message; } diff --git a/src/main/java/net/limework/core/hooks/SkriptHook.java b/src/main/java/net/limework/core/hooks/SkriptHook.java index ed41627..2c11990 100644 --- a/src/main/java/net/limework/core/hooks/SkriptHook.java +++ b/src/main/java/net/limework/core/hooks/SkriptHook.java @@ -4,12 +4,14 @@ import ch.njol.skript.Skript; import ch.njol.skript.SkriptAddon; import ch.njol.skript.lang.ExpressionType; import ch.njol.skript.registrations.EventValues; +import ch.njol.skript.util.Date; import ch.njol.skript.util.Getter; import net.limework.core.RediSkript; import net.limework.core.events.RedisMessageEvent; import net.limework.core.skript.elements.EvtRedis; import net.limework.core.skript.elements.ExprChannel; import net.limework.core.skript.elements.ExprMessage; +import net.limework.core.skript.elements.ExprMessageDate; import java.io.IOException; @@ -35,12 +37,15 @@ public class SkriptHook { return e.getMessage(); } }, 0); + Skript.registerExpression(ExprMessageDate.class, Date.class, ExpressionType.SIMPLE, "redis message date"); + EventValues.registerEventValue(RedisMessageEvent.class, Date.class, new Getter() { + @Override + public Date get(RedisMessageEvent e) { + return new Date(e.getDate()); + } + }, 0); } catch (IOException e) { e.printStackTrace(); } } - - public SkriptAddon getAddon() { - return addon; - } } \ No newline at end of file diff --git a/src/main/java/net/limework/core/managers/RedisManager.java b/src/main/java/net/limework/core/managers/RedisManager.java index ddc6789..17a5b6f 100644 --- a/src/main/java/net/limework/core/managers/RedisManager.java +++ b/src/main/java/net/limework/core/managers/RedisManager.java @@ -69,11 +69,10 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable { @Override public void run() { while (!isShuttingDown.get()) { - isKilled.set(false); try { - message("&2[&aRediSkript&a] &cConnecting to redis..."); + plugin.getLogger().info(ChatColor.translateAlternateColorCodes('&', "&2[&aRediSkript&a] &cConnecting to redis...")); if (!this.subscribeJedis.isConnected()) this.subscribeJedis = this.jedisPool.getResource(); - message("&2[&aRediSkript&a] &aRedis connected!"); + plugin.getLogger().info(ChatColor.translateAlternateColorCodes('&', "&2[&aRediSkript&a] &aRedis connected!")); int byteArr2dSize = 1; byte[][] channelsInByte = new byte[channels.size()][byteArr2dSize]; boolean reInitializeByteArray; @@ -96,7 +95,7 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable { this.subscribeJedis.subscribe(this, channelsInByte); } catch (Exception e) { - message("&2[&aRediSkript&a] &cConnection to redis has failed! &ereconnecting..."); + plugin.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&2[&aRediSkript&a] &cConnection to redis has failed! &ereconnecting...")); if (this.subscribeJedis != null) { this.subscribeJedis.close(); } @@ -106,14 +105,9 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable { } catch (InterruptedException e) { e.printStackTrace(); } - if (isKilled.get()) break; } } - private void message(String message) { - plugin.getLogger().info(ChatColor.translateAlternateColorCodes('&', message)); - } - @Override public void onMessage(byte[] channel, byte[] message) { String channelString = new String(channel, StandardCharsets.UTF_8); @@ -135,7 +129,9 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable { if (receivedMessage != null) { JSONObject j = new JSONObject(receivedMessage); //System.out.println("Message got from channel: "+channel +" and the Message: " +json.toString()); - plugin.getServer().getPluginManager().callEvent(new RedisMessageEvent(channelString, j.getString("Message"))); + RedisMessageEvent event = new RedisMessageEvent(channelString, j.getString("Message"), j.getLong("Date")); + //Running it synchronously to ensure that the event is always synchronous + Bukkit.getScheduler().runTask(plugin, () -> plugin.getServer().getPluginManager().callEvent(event)); } } catch (Exception e) { e.printStackTrace(); @@ -154,6 +150,10 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable { this.RedisService.shutdown(); } + public void reload() { + this.shutdown(); + plugin.startRedis(true); + } public JedisPool getJedisPool() { return jedisPool; @@ -163,26 +163,6 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable { return RedisService; } - public AtomicBoolean isShuttingDown() { - return isShuttingDown; - } - - public void reloadRedis() { - this.isKilled.set(true); - try { - if (this.subscribeJedis != null) { - this.unsubscribe(); - this.subscribeJedis.close(); - } - } catch (Exception e) { - e.printStackTrace(); - } - this.shutdown(); - plugin.startRedis(true); - } - - - public Encryption getEncryption() { return encryption; } diff --git a/src/main/java/net/limework/core/skript/elements/EvtRedis.java b/src/main/java/net/limework/core/skript/elements/EvtRedis.java index 6194755..004f9ee 100644 --- a/src/main/java/net/limework/core/skript/elements/EvtRedis.java +++ b/src/main/java/net/limework/core/skript/elements/EvtRedis.java @@ -2,6 +2,7 @@ package net.limework.core.skript.elements; import ch.njol.skript.lang.Literal; import ch.njol.skript.lang.SkriptEvent; import ch.njol.skript.lang.SkriptParser; +import net.limework.core.events.RedisMessageEvent; import org.bukkit.event.Event; public class EvtRedis extends SkriptEvent { @@ -13,11 +14,14 @@ public class EvtRedis extends SkriptEvent { @Override public boolean check(Event event) { + if (!(event instanceof RedisMessageEvent)) { + return false; + } return true; } @Override - public String toString(Event event, boolean b) { + public String toString(Event event, boolean debug) { return "redis message"; } } diff --git a/src/main/java/net/limework/core/skript/elements/ExprChannel.java b/src/main/java/net/limework/core/skript/elements/ExprChannel.java index 481b9e2..9422a1c 100644 --- a/src/main/java/net/limework/core/skript/elements/ExprChannel.java +++ b/src/main/java/net/limework/core/skript/elements/ExprChannel.java @@ -1,9 +1,12 @@ package net.limework.core.skript.elements; +import ch.njol.skript.ScriptLoader; +import ch.njol.skript.Skript; import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.SkriptParser; import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.skript.log.ErrorQuality; import ch.njol.util.Kleenean; import net.limework.core.events.RedisMessageEvent; import org.bukkit.event.Event; @@ -27,7 +30,11 @@ public class ExprChannel extends SimpleExpression { } @Override - public boolean init(Expression[] expressions, int i, Kleenean kleenean, SkriptParser.ParseResult parseResult) { + public boolean init(final Expression[] expressions, final int matchedPattern, final Kleenean isDelayed, final SkriptParser.ParseResult parseResult) { + if (!ScriptLoader.isCurrentEvent(RedisMessageEvent.class)) { + Skript.error("Cannot use 'redis channel' outside of a redis message event", ErrorQuality.SEMANTIC_ERROR); + return false; + } return true; } diff --git a/src/main/java/net/limework/core/skript/elements/ExprMessage.java b/src/main/java/net/limework/core/skript/elements/ExprMessage.java index a0ee7b1..e902af4 100644 --- a/src/main/java/net/limework/core/skript/elements/ExprMessage.java +++ b/src/main/java/net/limework/core/skript/elements/ExprMessage.java @@ -1,9 +1,12 @@ package net.limework.core.skript.elements; +import ch.njol.skript.ScriptLoader; +import ch.njol.skript.Skript; import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.SkriptParser; import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.skript.log.ErrorQuality; import ch.njol.util.Kleenean; import net.limework.core.events.RedisMessageEvent; import org.bukkit.event.Event; @@ -22,12 +25,16 @@ public class ExprMessage extends SimpleExpression { } @Override - public String toString(Event event, boolean b) { + public String toString(Event event, boolean debug) { return "redis message"; } @Override - public boolean init(Expression[] expressions, int i, Kleenean kleenean, SkriptParser.ParseResult parseResult) { + public boolean init(final Expression[] expressions, final int matchedPattern, final Kleenean isDelayed, final SkriptParser.ParseResult parseResult) { + if (!ScriptLoader.isCurrentEvent(RedisMessageEvent.class)) { + Skript.error("Cannot use 'redis message' outside of a redis message event", ErrorQuality.SEMANTIC_ERROR); + return false; + } return true; } diff --git a/src/main/java/net/limework/core/skript/elements/ExprMessageDate.java b/src/main/java/net/limework/core/skript/elements/ExprMessageDate.java new file mode 100644 index 0000000..51f4491 --- /dev/null +++ b/src/main/java/net/limework/core/skript/elements/ExprMessageDate.java @@ -0,0 +1,49 @@ +package net.limework.core.skript.elements; + + +import ch.njol.skript.ScriptLoader; +import ch.njol.skript.Skript; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.skript.log.ErrorQuality; +import ch.njol.skript.util.Date; +import ch.njol.util.Kleenean; +import net.limework.core.events.RedisMessageEvent; +import org.bukkit.event.Event; + +public class ExprMessageDate extends SimpleExpression { + + + @Override + public boolean isSingle() { + return true; + } + + @Override + public Class getReturnType() { + return Date.class; + } + + @Override + public String toString(Event event, boolean b) { + return "redis message date"; + } + + @Override + public boolean init(final Expression[] expressions, final int matchedPattern, final Kleenean isDelayed, final SkriptParser.ParseResult parseResult) { + if (!ScriptLoader.isCurrentEvent(RedisMessageEvent.class)) { + Skript.error("Cannot use 'redis message date' outside of a redis message event", ErrorQuality.SEMANTIC_ERROR); + return false; + } + return true; + } + @Override + protected Date[] get(Event e) { + if (e instanceof RedisMessageEvent){ + long date = ((RedisMessageEvent) e).getDate(); + return new Date[]{new Date(date)}; + } + return null; + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index d6ad668..b25b929 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -7,5 +7,4 @@ depend: - Skript commands: reloadredis: - description: "Restart redis connection" - permission: "admin.use" \ No newline at end of file + description: "Reload redis configuration & restart the connection." \ No newline at end of file