add redis message date expression & improve quality of existing expressions & make event guaranteedly synchronous

This commit is contained in:
Govindass 2020-10-23 16:49:25 +03:00
parent 1ef35dfb60
commit 776ebc9780
10 changed files with 101 additions and 48 deletions

View File

@ -9,10 +9,8 @@ public class RediSkript extends JavaPlugin {
//Redis manager //Redis manager
private RedisManager rm; private RedisManager rm;
public void startRedis(boolean reloadConfig) { public void startRedis(boolean reload) {
if (reloadConfig) { if (reload) { reloadConfig(); }
reloadConfig();
}
rm = new RedisManager(this); rm = new RedisManager(this);
rm.start(); rm.start();
} }

View File

@ -17,12 +17,12 @@ public class ReloadRedis implements CommandExecutor {
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) { if (sender instanceof Player) {
sender.sendMessage(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&' sender.sendMessage(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&'
, "&cYou cannot execute this command."))); , "&2[&aRediSkript&a] &cThis command can only be executed in console.")));
return true; return true;
} }
plugin.getRm().reloadRedis(); plugin.getRm().reload();
sender.sendMessage(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&' 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; return false;
} }
} }

View File

@ -9,11 +9,13 @@ public class RedisMessageEvent extends Event {
private String channelName; private String channelName;
private String message; private String message;
private long date;
public RedisMessageEvent(String channelName , String message) { public RedisMessageEvent(String channelName , String message, long date) {
super(true); super(true);
this.channelName = channelName; this.channelName = channelName;
this.message = message; this.message = message;
this.date = date;
} }
@ -34,6 +36,8 @@ public class RedisMessageEvent extends Event {
return channelName; return channelName;
} }
public long getDate() { return date;}
public String getMessage() { public String getMessage() {
return message; return message;
} }

View File

@ -4,12 +4,14 @@ import ch.njol.skript.Skript;
import ch.njol.skript.SkriptAddon; import ch.njol.skript.SkriptAddon;
import ch.njol.skript.lang.ExpressionType; import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.registrations.EventValues; import ch.njol.skript.registrations.EventValues;
import ch.njol.skript.util.Date;
import ch.njol.skript.util.Getter; import ch.njol.skript.util.Getter;
import net.limework.core.RediSkript; import net.limework.core.RediSkript;
import net.limework.core.events.RedisMessageEvent; import net.limework.core.events.RedisMessageEvent;
import net.limework.core.skript.elements.EvtRedis; import net.limework.core.skript.elements.EvtRedis;
import net.limework.core.skript.elements.ExprChannel; import net.limework.core.skript.elements.ExprChannel;
import net.limework.core.skript.elements.ExprMessage; import net.limework.core.skript.elements.ExprMessage;
import net.limework.core.skript.elements.ExprMessageDate;
import java.io.IOException; import java.io.IOException;
@ -35,12 +37,15 @@ public class SkriptHook {
return e.getMessage(); return e.getMessage();
} }
}, 0); }, 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) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public SkriptAddon getAddon() {
return addon;
}
} }

View File

@ -69,11 +69,10 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
@Override @Override
public void run() { public void run() {
while (!isShuttingDown.get()) { while (!isShuttingDown.get()) {
isKilled.set(false);
try { 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(); 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; int byteArr2dSize = 1;
byte[][] channelsInByte = new byte[channels.size()][byteArr2dSize]; byte[][] channelsInByte = new byte[channels.size()][byteArr2dSize];
boolean reInitializeByteArray; boolean reInitializeByteArray;
@ -96,7 +95,7 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
this.subscribeJedis.subscribe(this, channelsInByte); this.subscribeJedis.subscribe(this, channelsInByte);
} catch (Exception e) { } 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) { if (this.subscribeJedis != null) {
this.subscribeJedis.close(); this.subscribeJedis.close();
} }
@ -106,14 +105,9 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
if (isKilled.get()) break;
} }
} }
private void message(String message) {
plugin.getLogger().info(ChatColor.translateAlternateColorCodes('&', message));
}
@Override @Override
public void onMessage(byte[] channel, byte[] message) { public void onMessage(byte[] channel, byte[] message) {
String channelString = new String(channel, StandardCharsets.UTF_8); String channelString = new String(channel, StandardCharsets.UTF_8);
@ -135,7 +129,9 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
if (receivedMessage != null) { if (receivedMessage != null) {
JSONObject j = new JSONObject(receivedMessage); JSONObject j = new JSONObject(receivedMessage);
//System.out.println("Message got from channel: "+channel +" and the Message: " +json.toString()); //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) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -154,6 +150,10 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
this.RedisService.shutdown(); this.RedisService.shutdown();
} }
public void reload() {
this.shutdown();
plugin.startRedis(true);
}
public JedisPool getJedisPool() { public JedisPool getJedisPool() {
return jedisPool; return jedisPool;
@ -163,26 +163,6 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
return RedisService; 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() { public Encryption getEncryption() {
return encryption; return encryption;
} }

View File

@ -2,6 +2,7 @@ package net.limework.core.skript.elements;
import ch.njol.skript.lang.Literal; import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptEvent; import ch.njol.skript.lang.SkriptEvent;
import ch.njol.skript.lang.SkriptParser; import ch.njol.skript.lang.SkriptParser;
import net.limework.core.events.RedisMessageEvent;
import org.bukkit.event.Event; import org.bukkit.event.Event;
public class EvtRedis extends SkriptEvent { public class EvtRedis extends SkriptEvent {
@ -13,11 +14,14 @@ public class EvtRedis extends SkriptEvent {
@Override @Override
public boolean check(Event event) { public boolean check(Event event) {
if (!(event instanceof RedisMessageEvent)) {
return false;
}
return true; return true;
} }
@Override @Override
public String toString(Event event, boolean b) { public String toString(Event event, boolean debug) {
return "redis message"; return "redis message";
} }
} }

View File

@ -1,9 +1,12 @@
package net.limework.core.skript.elements; 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.Expression;
import ch.njol.skript.lang.SkriptParser; import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.util.SimpleExpression; import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.log.ErrorQuality;
import ch.njol.util.Kleenean; import ch.njol.util.Kleenean;
import net.limework.core.events.RedisMessageEvent; import net.limework.core.events.RedisMessageEvent;
import org.bukkit.event.Event; import org.bukkit.event.Event;
@ -27,7 +30,11 @@ public class ExprChannel extends SimpleExpression<String> {
} }
@Override @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; return true;
} }

View File

@ -1,9 +1,12 @@
package net.limework.core.skript.elements; 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.Expression;
import ch.njol.skript.lang.SkriptParser; import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.util.SimpleExpression; import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.log.ErrorQuality;
import ch.njol.util.Kleenean; import ch.njol.util.Kleenean;
import net.limework.core.events.RedisMessageEvent; import net.limework.core.events.RedisMessageEvent;
import org.bukkit.event.Event; import org.bukkit.event.Event;
@ -22,12 +25,16 @@ public class ExprMessage extends SimpleExpression<String> {
} }
@Override @Override
public String toString(Event event, boolean b) { public String toString(Event event, boolean debug) {
return "redis message"; return "redis message";
} }
@Override @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; return true;
} }

View File

@ -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;
}
}

View File

@ -7,5 +7,4 @@ depend:
- Skript - Skript
commands: commands:
reloadredis: reloadredis:
description: "Restart redis connection" description: "Reload redis configuration & restart the connection."
permission: "admin.use"