forked from Limework/RediSkript
added code
This commit is contained in:
96
src/main/java/net/limework/skLimework/AddonPlugin.java
Normal file
96
src/main/java/net/limework/skLimework/AddonPlugin.java
Normal file
@@ -0,0 +1,96 @@
|
||||
package net.limework.skLimework;
|
||||
|
||||
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.Getter;
|
||||
import net.limework.skLimework.Events.RedisSub;
|
||||
import net.limework.skLimework.Events.onRedisMessage;
|
||||
import net.limework.skLimework.elements.EvtRedis;
|
||||
import net.limework.skLimework.elements.ExprChannel;
|
||||
import net.limework.skLimework.elements.ExprMessage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
import java.beans.Expression;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class AddonPlugin extends JavaPlugin {
|
||||
private AddonPlugin instance;
|
||||
private SkriptAddon addon;
|
||||
private JedisPool jedisPool;
|
||||
private RedisSub redisSub;
|
||||
private ExecutorService service;
|
||||
|
||||
|
||||
@Override
|
||||
public void onEnable(){
|
||||
instance = this;
|
||||
this.saveDefaultConfig();
|
||||
addon = Skript.registerAddon(this);
|
||||
try { addon.loadClasses("net.limework.skLimework", "elements");
|
||||
Skript.registerEvent("redis message", EvtRedis.class, onRedisMessage.class, "redis message");
|
||||
Skript.registerExpression(ExprChannel.class, String.class, ExpressionType.SIMPLE, "channel");
|
||||
EventValues.registerEventValue(onRedisMessage.class, String.class, new Getter<String, onRedisMessage>() {
|
||||
@Override
|
||||
public String get(onRedisMessage e) {
|
||||
return e.getChannelName();
|
||||
}
|
||||
}, 0);
|
||||
Skript.registerExpression(ExprMessage.class, String.class, ExpressionType.SIMPLE, "redis message");
|
||||
EventValues.registerEventValue(onRedisMessage.class, String.class, new Getter<String, onRedisMessage>() {
|
||||
@Override
|
||||
public String get(onRedisMessage e) {
|
||||
return e.getMessage();
|
||||
}
|
||||
}, 0);
|
||||
|
||||
|
||||
|
||||
|
||||
} catch (IOException e) { e.printStackTrace(); }
|
||||
JedisPoolConfig jconfig = new JedisPoolConfig();
|
||||
jconfig.setMaxTotal(this.getConfig().getInt("Redis.MaxConnections"));
|
||||
jconfig.setMaxIdle(this.getConfig().getInt("Redis.MaxConnections"));
|
||||
jconfig.setMinIdle(1);
|
||||
jedisPool = new JedisPool(jconfig,
|
||||
this.getConfig().getString("Redis.Host"),
|
||||
this.getConfig().getInt("Redis.Port") ,
|
||||
this.getConfig().getInt("Redis.TimeOut"),
|
||||
this.getConfig().getString("Redis.Password"),
|
||||
this.getConfig().getBoolean("Redis.useSSL"));
|
||||
redisSub = new RedisSub(this, jedisPool.getResource(), this.getConfig().getStringList("Channels"));
|
||||
service = Executors.newFixedThreadPool(this.getConfig().getInt("Redis.Threads"));
|
||||
|
||||
Bukkit.getLogger().info("[Govindas limework Addon] was enabled!");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onDisable(){
|
||||
redisSub.unSubAndCloseConnection();
|
||||
service.shutdown();
|
||||
try { service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { e.printStackTrace(); }
|
||||
jedisPool.close();
|
||||
}
|
||||
|
||||
public AddonPlugin getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public SkriptAddon getAddonInstance() {
|
||||
return addon;
|
||||
}
|
||||
|
||||
public ExecutorService getJedisExecutionService() {
|
||||
return service;
|
||||
}
|
||||
|
||||
public JedisPool getJedisPool() { return jedisPool; }
|
||||
}
|
||||
29
src/main/java/net/limework/skLimework/DoNotUse/Tester.java
Normal file
29
src/main/java/net/limework/skLimework/DoNotUse/Tester.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package net.limework.skLimework.DoNotUse;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
import redis.clients.jedis.JedisPubSub;
|
||||
|
||||
public class Tester {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
JedisPoolConfig jconfig = new JedisPoolConfig();
|
||||
jconfig.setMaxTotal(100);
|
||||
jconfig.setMaxIdle(100);
|
||||
jconfig.setMinIdle(1);
|
||||
JedisPool d = new JedisPool(jconfig, "192.168.0.112", 6379, 400, "yHy0d2zdBlRmaSPj3CiBwEv5V3XxBTLTrCsGW7ntBnzhfxPxXJS6Q1aTtR6DSfAtCZr2VxWnsungXHTcF94a4bsWEpGAvjL6XMU");
|
||||
Jedis dd = d.getResource();
|
||||
JSONObject J = new JSONObject();
|
||||
J.put("Message", "something::something::something");
|
||||
int x = 0;
|
||||
while (true) {
|
||||
x++;
|
||||
System.out.println(x);
|
||||
dd.publish("fs", J.toString() );
|
||||
if (x == 1000) break;
|
||||
Thread.sleep(1000);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
40
src/main/java/net/limework/skLimework/Events/RedisSub.java
Normal file
40
src/main/java/net/limework/skLimework/Events/RedisSub.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package net.limework.skLimework.Events;
|
||||
|
||||
import net.limework.skLimework.AddonPlugin;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.json.JSONObject;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPubSub;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class RedisSub extends JedisPubSub {
|
||||
private AddonPlugin plugin;
|
||||
private Jedis j;
|
||||
public RedisSub(AddonPlugin plugin, Jedis j, List<String> channels) {
|
||||
this.plugin = plugin;
|
||||
this.j = j;
|
||||
String[] ss = channels.toArray(new String[0]);
|
||||
plugin.getServer().getScheduler().runTaskAsynchronously(plugin,
|
||||
() -> this.j.subscribe(this, ss));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(String channel, String message) {
|
||||
try {
|
||||
JSONObject json = new JSONObject(message);
|
||||
//System.out.println("Message got from channel: "+channel +" and the Message: " +json.toString());
|
||||
plugin.getServer().getPluginManager().callEvent(new onRedisMessage(channel, json.getString("Message")));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Bukkit.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&2[&aGBot&a] &cI Got a Message that Was empty from channel "+ channel +" Please check your code that you used to send the message. ^ ignore the error."));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void unSubAndCloseConnection(){
|
||||
this.unsubscribe();
|
||||
j.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package net.limework.skLimework.Events;
|
||||
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
||||
|
||||
public class onRedisMessage extends Event {
|
||||
private final static HandlerList HANDLERS = new HandlerList();
|
||||
|
||||
private String channelName;
|
||||
private String message;
|
||||
|
||||
onRedisMessage(String channelName , String message) {
|
||||
super(true);
|
||||
this.channelName = channelName;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getEventName() {
|
||||
return super.getEventName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return HANDLERS;
|
||||
}
|
||||
public static HandlerList getHandlerList() {
|
||||
return HANDLERS;
|
||||
}
|
||||
|
||||
public String getChannelName() {
|
||||
return channelName;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package net.limework.skLimework.elements;
|
||||
|
||||
import ch.njol.skript.Skript;
|
||||
import ch.njol.skript.SkriptAPIException;
|
||||
import ch.njol.skript.lang.Effect;
|
||||
import ch.njol.skript.lang.Expression;
|
||||
import ch.njol.skript.lang.SkriptParser;
|
||||
import ch.njol.util.Kleenean;
|
||||
import net.limework.skLimework.AddonPlugin;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.event.Event;
|
||||
import org.json.JSONObject;
|
||||
import redis.clients.jedis.Jedis;
|
||||
|
||||
public class EffSendMessage extends Effect {
|
||||
//"hi"
|
||||
static {
|
||||
Skript.registerEffect(EffSendMessage.class, "send redis message to channel %string% with message %string%");
|
||||
}
|
||||
|
||||
|
||||
private Expression<String> channel;
|
||||
private Expression<String> message;
|
||||
|
||||
|
||||
@Override
|
||||
protected void execute(Event event) {
|
||||
AddonPlugin plugin = (AddonPlugin) Bukkit.getPluginManager().getPlugin("SKLimework");
|
||||
String message = this.message.getSingle(event);
|
||||
String channel = this.channel.getSingle(event);
|
||||
if (message == null){//checks if message equals null if true does not execute.
|
||||
Bukkit.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&2[&aGBot&a] &cMessage Was empty Please check your code."));
|
||||
return;
|
||||
}
|
||||
plugin.getJedisExecutionService().execute(() -> {
|
||||
Jedis j = plugin.getJedisPool().getResource();
|
||||
JSONObject json = new JSONObject();
|
||||
try {
|
||||
json.put("Message", message);
|
||||
json.put("Type", "Skript");
|
||||
j.publish(channel, json.toString());
|
||||
//System.out.println("SkriptSide sent MESSAGE: ["+ message + "] to channel: " + channel + " and json: \n" + json.toString());
|
||||
}catch (Exception e){e.printStackTrace();}
|
||||
j.close();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(Event event, boolean b) {
|
||||
return null;
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parser) {
|
||||
this.channel = (Expression<String>) expressions[0];
|
||||
this.message = (Expression<String>) expressions[1];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
32
src/main/java/net/limework/skLimework/elements/EvtRedis.java
Normal file
32
src/main/java/net/limework/skLimework/elements/EvtRedis.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package net.limework.skLimework.elements;
|
||||
|
||||
import ch.njol.skript.Skript;
|
||||
import ch.njol.skript.lang.ExpressionType;
|
||||
import ch.njol.skript.lang.Literal;
|
||||
import ch.njol.skript.lang.SkriptEvent;
|
||||
import ch.njol.skript.lang.SkriptParser;
|
||||
import ch.njol.skript.registrations.EventValues;
|
||||
import ch.njol.skript.util.Getter;
|
||||
import net.limework.skLimework.Events.onRedisMessage;
|
||||
import org.bukkit.event.Event;
|
||||
|
||||
public class EvtRedis extends SkriptEvent {
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean init(Literal<?>[] literals, int i, SkriptParser.ParseResult parseResult) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean check(Event event) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(Event event, boolean b) {
|
||||
return "redis message";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package net.limework.skLimework.elements;
|
||||
|
||||
|
||||
import ch.njol.skript.lang.Expression;
|
||||
import ch.njol.skript.lang.SkriptParser;
|
||||
import ch.njol.skript.lang.util.SimpleExpression;
|
||||
import ch.njol.util.Kleenean;
|
||||
import net.limework.skLimework.Events.onRedisMessage;
|
||||
import org.bukkit.event.Event;
|
||||
|
||||
public class ExprChannel extends SimpleExpression<String> {
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isSingle() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends String> getReturnType() {
|
||||
return String.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(Event event, boolean b) {
|
||||
return "redis channel";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean init(Expression<?>[] expressions, int i, Kleenean kleenean, SkriptParser.ParseResult parseResult) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected String[] get(Event e) {
|
||||
if (e instanceof onRedisMessage){
|
||||
return new String[]{((onRedisMessage) e).getChannelName()};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package net.limework.skLimework.elements;
|
||||
|
||||
|
||||
import ch.njol.skript.lang.Expression;
|
||||
import ch.njol.skript.lang.SkriptParser;
|
||||
import ch.njol.skript.lang.util.SimpleExpression;
|
||||
import ch.njol.util.Kleenean;
|
||||
import net.limework.skLimework.Events.onRedisMessage;
|
||||
import org.bukkit.event.Event;
|
||||
|
||||
public class ExprMessage extends SimpleExpression<String> {
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isSingle() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends String> getReturnType() {
|
||||
return String.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(Event event, boolean b) {
|
||||
return "redis message";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean init(Expression<?>[] expressions, int i, Kleenean kleenean, SkriptParser.ParseResult parseResult) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected String[] get(Event e) {
|
||||
if (e instanceof onRedisMessage){
|
||||
return new String[]{((onRedisMessage) e).getMessage()};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user