RedisBungee/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/JedisPubSubHandler.java

34 lines
1.1 KiB
Java
Raw Normal View History

2022-07-16 12:50:09 +00:00
package com.imaginarycode.minecraft.redisbungee.api;
2022-04-13 16:08:46 +00:00
import redis.clients.jedis.JedisPubSub;
import java.lang.reflect.InvocationTargetException;
2022-04-13 16:08:46 +00:00
public class JedisPubSubHandler extends JedisPubSub {
private final RedisBungeePlugin<?> plugin;
2022-04-13 16:08:46 +00:00
private final Class<?> eventClass;
public JedisPubSubHandler(RedisBungeePlugin<?> plugin) {
this.plugin = plugin;
2022-04-13 16:08:46 +00:00
this.eventClass = plugin.getPubSubEventClass();
}
@Override
public void onMessage(final String s, final String s2) {
if (s2.trim().length() == 0) return;
plugin.executeAsync(new Runnable() {
@Override
public void run() {
2022-04-13 16:08:46 +00:00
Object event;
try {
event = eventClass.getDeclaredConstructor(String.class, String.class).newInstance(s, s2);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException("unable to dispatch an pubsub event", e);
}
plugin.callEvent(event);
}
});
}
}