From 5699b9445f60a5b0d60849e0b481b244aede3e40 Mon Sep 17 00:00:00 2001 From: Govindass Date: Thu, 3 Dec 2020 11:55:45 +0200 Subject: [PATCH] Improve code quality --- .../net/limework/rediskript/RediSkript.java | 61 ++++++++++++++----- .../commands/CommandReloadRedis.java | 3 +- .../rediskript/managers/RedisManager.java | 23 ++----- .../rediskript/skript/SkriptHook.java | 49 --------------- .../skript/elements/EffSendMessage.java | 2 +- .../rediskript/skript/elements/EvtRedis.java | 5 +- .../elements/ExprVariableInChannel.java | 8 +-- 7 files changed, 59 insertions(+), 92 deletions(-) delete mode 100644 src/main/java/net/limework/rediskript/skript/SkriptHook.java diff --git a/src/main/java/net/limework/rediskript/RediSkript.java b/src/main/java/net/limework/rediskript/RediSkript.java index 4752049..c2c1fde 100644 --- a/src/main/java/net/limework/rediskript/RediSkript.java +++ b/src/main/java/net/limework/rediskript/RediSkript.java @@ -1,13 +1,19 @@ package net.limework.rediskript; +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.rediskript.commands.CommandReloadRedis; +import net.limework.rediskript.events.RedisMessageEvent; import net.limework.rediskript.managers.RedisManager; -import net.limework.rediskript.skript.SkriptHook; -import org.bukkit.command.PluginCommand; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; +import net.limework.rediskript.skript.elements.*; import org.bukkit.plugin.java.JavaPlugin; +import java.io.IOException; + public class RediSkript extends JavaPlugin { //Redis manager @@ -18,25 +24,52 @@ public class RediSkript extends JavaPlugin { rm = new RedisManager(this); rm.start(); } + public void registerSyntax() { + SkriptAddon addon = Skript.registerAddon(this); + try { + addon.loadClasses("net.limework.rediskript.skript", "elements"); + Skript.registerEvent("redis message", EvtRedis.class, RedisMessageEvent.class, "redis message"); + Skript.registerExpression(ExprVariableInChannel.class, Object.class, ExpressionType.PROPERTY, "variable[s] %strings% in [redis] [channel] %string%"); + + Skript.registerExpression(ExprChannel.class, String.class, ExpressionType.SIMPLE, "redis channel"); + EventValues.registerEventValue(RedisMessageEvent.class, String.class, new Getter() { + @Override + public String get(RedisMessageEvent e) { + return e.getChannelName(); + } + }, 0); + Skript.registerExpression(ExprMessage.class, String.class, ExpressionType.SIMPLE, "redis message"); + EventValues.registerEventValue(RedisMessageEvent.class, String.class, new Getter() { + @Override + public String get(RedisMessageEvent e) { + 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(); + } + } + @Override public void onEnable() { saveDefaultConfig(); startRedis(false); - - PluginCommand command = getServer().getPluginCommand("reloadredis"); - assert command != null; - command.setExecutor(new CommandReloadRedis(this)); - - new SkriptHook(this); + getServer().getPluginCommand("reloadredis").setExecutor(new CommandReloadRedis(this)); + registerSyntax(); } @Override public void onDisable() { - if (rm != null) { - rm.shutdown(); - } + if (rm != null) rm.shutdown(); } - public RedisManager getRm() { + public RedisManager getRedisManager() { return rm; } } \ No newline at end of file diff --git a/src/main/java/net/limework/rediskript/commands/CommandReloadRedis.java b/src/main/java/net/limework/rediskript/commands/CommandReloadRedis.java index f463f43..a67d0ba 100644 --- a/src/main/java/net/limework/rediskript/commands/CommandReloadRedis.java +++ b/src/main/java/net/limework/rediskript/commands/CommandReloadRedis.java @@ -7,7 +7,6 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; - public class CommandReloadRedis implements CommandExecutor { private RediSkript plugin; public CommandReloadRedis(RediSkript plugin) { @@ -21,7 +20,7 @@ public class CommandReloadRedis implements CommandExecutor { , "&2[&aRediSkript&2] &cThis command can only be executed in console."))); return true; } - plugin.getRm().reload(); + plugin.getRedisManager().reload(); //not sending to sender, because this command can only be executed via console Bukkit.getLogger().info(ChatColor.translateAlternateColorCodes('&', "&eReloaded via command! Note this command is not stable, it should only be used in urgent cases where you absolutely need to change config details without restarting the server.")); diff --git a/src/main/java/net/limework/rediskript/managers/RedisManager.java b/src/main/java/net/limework/rediskript/managers/RedisManager.java index acd43d3..029b49d 100644 --- a/src/main/java/net/limework/rediskript/managers/RedisManager.java +++ b/src/main/java/net/limework/rediskript/managers/RedisManager.java @@ -82,22 +82,9 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable { plugin.getLogger().info(ChatColor.translateAlternateColorCodes('&', "&aRedis connected!")); int byteArr2dSize = 1; byte[][] channelsInByte = new byte[channels.size()][byteArr2dSize]; - boolean reInitializeByteArray; - // Loop that reInitialize array IF array size is not enough - do { - reInitializeByteArray = false; - try { - /* Data Initialization for channelsInByte array from List channels */ - for (int x = 0; x < channels.size(); x++) { - channelsInByte[x] = channels.get(x).getBytes(StandardCharsets.UTF_8); - } - } 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); + for (int x = 0; x < channels.size(); x++) { + channelsInByte[x] = channels.get(x).getBytes(StandardCharsets.UTF_8); + } this.subscribeJedis.subscribe(this, channelsInByte); @@ -228,8 +215,6 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable { } else { message = json.toString().getBytes(StandardCharsets.UTF_8); } - //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 @@ -240,6 +225,8 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable { j.close(); }); } else { + //execute sending of redis message on the main thread if plugin is disabling + //so it can still process the sending BinaryJedis j = this.getJedisPool().getResource(); j.publish(channel.getBytes(StandardCharsets.UTF_8), message); j.close(); diff --git a/src/main/java/net/limework/rediskript/skript/SkriptHook.java b/src/main/java/net/limework/rediskript/skript/SkriptHook.java deleted file mode 100644 index a302588..0000000 --- a/src/main/java/net/limework/rediskript/skript/SkriptHook.java +++ /dev/null @@ -1,49 +0,0 @@ -package net.limework.rediskript.skript; - -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 ch.njol.skript.util.Timespan; -import net.limework.rediskript.RediSkript; -import net.limework.rediskript.events.RedisMessageEvent; -import net.limework.rediskript.skript.elements.*; - -import java.io.IOException; - -public class SkriptHook { - public SkriptHook(RediSkript plugin) { - SkriptAddon addon = Skript.registerAddon(plugin); - try { - addon.loadClasses("net.limework.rediskript.skript", "elements"); - Skript.registerEvent("redis message", EvtRedis.class, RedisMessageEvent.class, "redis message"); - Skript.registerExpression(ExprVariableInChannel.class, Object.class, ExpressionType.PROPERTY, "variable[s] %strings% in [redis] [channel] %string%"); - - Skript.registerExpression(ExprChannel.class, String.class, ExpressionType.SIMPLE, "redis channel"); - EventValues.registerEventValue(RedisMessageEvent.class, String.class, new Getter() { - @Override - public String get(RedisMessageEvent e) { - return e.getChannelName(); - } - }, 0); - Skript.registerExpression(ExprMessage.class, String.class, ExpressionType.SIMPLE, "redis message"); - EventValues.registerEventValue(RedisMessageEvent.class, String.class, new Getter() { - @Override - public String get(RedisMessageEvent e) { - 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(); - } - } -} \ No newline at end of file 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 fc3b318..f98d5b1 100644 --- a/src/main/java/net/limework/rediskript/skript/elements/EffSendMessage.java +++ b/src/main/java/net/limework/rediskript/skript/elements/EffSendMessage.java @@ -35,7 +35,7 @@ public class EffSendMessage extends Effect { Bukkit.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&2[&aRediSkript&a] &cChannel was empty. Please check your code.")); return; } - plugin.getRm().sendMessage(message, channel); + plugin.getRedisManager().sendMessage(message, channel); } @Override diff --git a/src/main/java/net/limework/rediskript/skript/elements/EvtRedis.java b/src/main/java/net/limework/rediskript/skript/elements/EvtRedis.java index 0cc1783..d96b2e0 100644 --- a/src/main/java/net/limework/rediskript/skript/elements/EvtRedis.java +++ b/src/main/java/net/limework/rediskript/skript/elements/EvtRedis.java @@ -14,10 +14,7 @@ public class EvtRedis extends SkriptEvent { @Override public boolean check(Event event) { - if (!(event instanceof RedisMessageEvent)) { - return false; - } - return true; + return (event instanceof RedisMessageEvent); } @Override diff --git a/src/main/java/net/limework/rediskript/skript/elements/ExprVariableInChannel.java b/src/main/java/net/limework/rediskript/skript/elements/ExprVariableInChannel.java index a8bc601..80c760f 100644 --- a/src/main/java/net/limework/rediskript/skript/elements/ExprVariableInChannel.java +++ b/src/main/java/net/limework/rediskript/skript/elements/ExprVariableInChannel.java @@ -44,8 +44,8 @@ public class ExprVariableInChannel extends SimpleExpression { } @Override - public String toString(Event event, boolean b) { - return "variable in redis channel"; + public String toString(Event event, boolean debug) { + return "variable " + name.toString(event,debug) + " in redis channel " + channel.toString(event, debug); } @Override public void change(Event e, Object[] changer, Changer.ChangeMode mode) { @@ -66,10 +66,10 @@ public class ExprVariableInChannel extends SimpleExpression { } } String operation = mode.toString(); - plugin.getRm().sendVariables(name.getAll(e), values, channel.getSingle(e), operation); + plugin.getRedisManager().sendVariables(name.getAll(e), values, channel.getSingle(e), operation); break; case DELETE: - plugin.getRm().sendVariables(name.getAll(e), null, channel.getSingle(e), "SET"); + plugin.getRedisManager().sendVariables(name.getAll(e), null, channel.getSingle(e), "SET"); break; } }