forked from Limework/RediSkript
Improve code quality
This commit is contained in:
parent
053bb04918
commit
5699b9445f
@ -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<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
|
||||
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;
|
||||
}
|
||||
}
|
@ -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."));
|
||||
|
||||
|
@ -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<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);
|
||||
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();
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -44,8 +44,8 @@ public class ExprVariableInChannel extends SimpleExpression<Object> {
|
||||
}
|
||||
|
||||
@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<Object> {
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user