Improve code quality

This commit is contained in:
Govindass 2020-12-03 11:55:45 +02:00
parent 053bb04918
commit 5699b9445f
7 changed files with 59 additions and 92 deletions

View File

@ -1,13 +1,19 @@
package net.limework.rediskript; 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.commands.CommandReloadRedis;
import net.limework.rediskript.events.RedisMessageEvent;
import net.limework.rediskript.managers.RedisManager; import net.limework.rediskript.managers.RedisManager;
import net.limework.rediskript.skript.SkriptHook; import net.limework.rediskript.skript.elements.*;
import org.bukkit.command.PluginCommand;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.io.IOException;
public class RediSkript extends JavaPlugin { public class RediSkript extends JavaPlugin {
//Redis manager //Redis manager
@ -18,25 +24,52 @@ public class RediSkript extends JavaPlugin {
rm = new RedisManager(this); rm = new RedisManager(this);
rm.start(); 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<String, RedisMessageEvent>() {
@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<String, RedisMessageEvent>() {
@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<Date, RedisMessageEvent>() {
@Override
public Date get(RedisMessageEvent e) {
return new Date(e.getDate());
}
}, 0);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override @Override
public void onEnable() { public void onEnable() {
saveDefaultConfig(); saveDefaultConfig();
startRedis(false); startRedis(false);
getServer().getPluginCommand("reloadredis").setExecutor(new CommandReloadRedis(this));
PluginCommand command = getServer().getPluginCommand("reloadredis"); registerSyntax();
assert command != null;
command.setExecutor(new CommandReloadRedis(this));
new SkriptHook(this);
} }
@Override @Override
public void onDisable() { public void onDisable() {
if (rm != null) { if (rm != null) rm.shutdown();
rm.shutdown();
}
} }
public RedisManager getRm() { public RedisManager getRedisManager() {
return rm; return rm;
} }
} }

View File

@ -7,7 +7,6 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class CommandReloadRedis implements CommandExecutor { public class CommandReloadRedis implements CommandExecutor {
private RediSkript plugin; private RediSkript plugin;
public CommandReloadRedis(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."))); , "&2[&aRediSkript&2] &cThis command can only be executed in console.")));
return true; return true;
} }
plugin.getRm().reload(); plugin.getRedisManager().reload();
//not sending to sender, because this command can only be executed via console //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.")); 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."));

View File

@ -82,22 +82,9 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
plugin.getLogger().info(ChatColor.translateAlternateColorCodes('&', "&aRedis connected!")); plugin.getLogger().info(ChatColor.translateAlternateColorCodes('&', "&aRedis connected!"));
int byteArr2dSize = 1; int byteArr2dSize = 1;
byte[][] channelsInByte = new byte[channels.size()][byteArr2dSize]; byte[][] channelsInByte = new byte[channels.size()][byteArr2dSize];
boolean reInitializeByteArray; for (int x = 0; x < channels.size(); x++) {
// Loop that reInitialize array IF array size is not enough channelsInByte[x] = channels.get(x).getBytes(StandardCharsets.UTF_8);
do { }
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(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);
this.subscribeJedis.subscribe(this, channelsInByte); this.subscribeJedis.subscribe(this, channelsInByte);
@ -228,8 +215,6 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
} else { } else {
message = json.toString().getBytes(StandardCharsets.UTF_8); 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 //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 //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(); j.close();
}); });
} else { } 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(); BinaryJedis j = this.getJedisPool().getResource();
j.publish(channel.getBytes(StandardCharsets.UTF_8), message); j.publish(channel.getBytes(StandardCharsets.UTF_8), message);
j.close(); j.close();

View File

@ -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<String, RedisMessageEvent>() {
@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<String, RedisMessageEvent>() {
@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<Date, RedisMessageEvent>() {
@Override
public Date get(RedisMessageEvent e) {
return new Date(e.getDate());
}
}, 0);
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -35,7 +35,7 @@ public class EffSendMessage extends Effect {
Bukkit.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&2[&aRediSkript&a] &cChannel was empty. Please check your code.")); Bukkit.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&2[&aRediSkript&a] &cChannel was empty. Please check your code."));
return; return;
} }
plugin.getRm().sendMessage(message, channel); plugin.getRedisManager().sendMessage(message, channel);
} }
@Override @Override

View File

@ -14,10 +14,7 @@ public class EvtRedis extends SkriptEvent {
@Override @Override
public boolean check(Event event) { public boolean check(Event event) {
if (!(event instanceof RedisMessageEvent)) { return (event instanceof RedisMessageEvent);
return false;
}
return true;
} }
@Override @Override

View File

@ -44,8 +44,8 @@ public class ExprVariableInChannel extends SimpleExpression<Object> {
} }
@Override @Override
public String toString(Event event, boolean b) { public String toString(Event event, boolean debug) {
return "variable in redis channel"; return "variable " + name.toString(event,debug) + " in redis channel " + channel.toString(event, debug);
} }
@Override @Override
public void change(Event e, Object[] changer, Changer.ChangeMode mode) { public void change(Event e, Object[] changer, Changer.ChangeMode mode) {
@ -66,10 +66,10 @@ public class ExprVariableInChannel extends SimpleExpression<Object> {
} }
} }
String operation = mode.toString(); 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; break;
case DELETE: 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; break;
} }
} }