forked from Limework/RediSkript
add redis message date expression & improve quality of existing expressions & make event guaranteedly synchronous
This commit is contained in:
parent
3992ff508e
commit
df9297e324
@ -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();
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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<Date, RedisMessageEvent>() {
|
||||
@Override
|
||||
public Date get(RedisMessageEvent e) {
|
||||
return new Date(e.getDate());
|
||||
}
|
||||
}, 0);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public SkriptAddon getAddon() {
|
||||
return addon;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
@ -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";
|
||||
}
|
||||
}
|
||||
|
@ -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<String> {
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
|
@ -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<String> {
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
|
@ -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<Date> {
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isSingle() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends Date> 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;
|
||||
}
|
||||
}
|
@ -7,5 +7,4 @@ depend:
|
||||
- Skript
|
||||
commands:
|
||||
reloadredis:
|
||||
description: "Restart redis connection"
|
||||
permission: "admin.use"
|
||||
description: "Reload redis configuration & restart the connection."
|
Loading…
Reference in New Issue
Block a user