2022-07-16 12:50:09 +00:00
|
|
|
package com.imaginarycode.minecraft.redisbungee.api;
|
2022-04-13 13:14:08 +00:00
|
|
|
|
2022-04-13 16:08:46 +00:00
|
|
|
|
2022-04-13 13:14:08 +00:00
|
|
|
import redis.clients.jedis.JedisPubSub;
|
|
|
|
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
|
2022-04-13 16:08:46 +00:00
|
|
|
|
2022-04-13 13:14:08 +00:00
|
|
|
public class JedisPubSubHandler extends JedisPubSub {
|
|
|
|
|
|
|
|
private final RedisBungeePlugin<?> plugin;
|
2022-04-13 16:08:46 +00:00
|
|
|
private final Class<?> eventClass;
|
2022-04-13 13:14:08 +00:00
|
|
|
public JedisPubSubHandler(RedisBungeePlugin<?> plugin) {
|
|
|
|
this.plugin = plugin;
|
2022-04-13 16:08:46 +00:00
|
|
|
this.eventClass = plugin.getPubSubEventClass();
|
2022-04-13 13:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
2022-04-13 13:14:08 +00:00
|
|
|
}
|
|
|
|
plugin.callEvent(event);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|