2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2024-11-22 20:28:00 +00:00

more progress

This commit is contained in:
mohammed jasem alaajel 2022-04-13 20:08:46 +04:00
parent 61dec7f03c
commit 17fdeda147
10 changed files with 237 additions and 39 deletions

View File

@ -0,0 +1,36 @@
package com.imaginarycode.minecraft.redisbungee.events;
import java.util.UUID;
/**
* This event is sent when a player connects to a new server. RedisBungee sends the event only when
* the proxy the player has been connected to is different than the local proxy.
* <p>
* This event corresponds to net.md_5.bungee.api.event.ServerConnectedEvent on bungee and is fired
* asynchronously.
*
* @since 0.3.4
*/
public class PlayerChangedServerNetworkEvent {
private final UUID uuid;
private final String previousServer;
private final String server;
public PlayerChangedServerNetworkEvent(UUID uuid, String previousServer, String server) {
this.uuid = uuid;
this.previousServer = previousServer;
this.server = server;
}
public UUID getUuid() {
return uuid;
}
public String getServer() {
return server;
}
public String getPreviousServer() {
return previousServer;
}
}

View File

@ -0,0 +1,24 @@
package com.imaginarycode.minecraft.redisbungee.events;
import java.util.UUID;
/**
* This event is sent when a player joins the network. RedisBungee sends the event only when
* the proxy the player has been connected to is different than the local proxy.
* <p>
* This event corresponds to net.md_5.bungee.api.event.PostLoginEvent on bungee, and is fired
* asynchronously.
*
* @since 0.3.4
*/
public class PlayerJoinedNetworkEvent {
private final UUID uuid;
public PlayerJoinedNetworkEvent(UUID uuid) {
this.uuid = uuid;
}
public UUID getUuid() {
return uuid;
}
}

View File

@ -0,0 +1,26 @@
package com.imaginarycode.minecraft.redisbungee.events;
import java.util.UUID;
/**
* This event is sent when a player disconnects. RedisBungee sends the event only when
* the proxy the player has been connected to is different than the local proxy.
* <p>
* This event corresponds to net.md_5.bungee.api.event.PlayerDisconnectEvent on bungee, and is fired
* asynchronously.
*
* @since 0.3.4
*/
public class PlayerLeftNetworkEvent {
private final UUID uuid;
public PlayerLeftNetworkEvent(UUID uuid) {
this.uuid = uuid;
}
public UUID getUuid() {
return uuid;
}
}

View File

@ -11,6 +11,7 @@ import com.google.gson.JsonParser;
import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent;
import redis.clients.jedis.Jedis;
import java.lang.reflect.InvocationTargetException;
import java.net.InetAddress;
import java.util.Objects;
import java.util.UUID;
@ -148,13 +149,9 @@ public abstract class DataManager<P, PS, PL, PD> {
}
public void onPostLogin(PL event) {
public abstract void onPostLogin(PL event);
}
public void onPlayerDisconnect(PD event) {
}
public abstract void onPlayerDisconnect(PD event);
public abstract void onPubSubMessage(PS event);
@ -182,7 +179,14 @@ public abstract class DataManager<P, PS, PL, PD> {
plugin.executeAsync(new Runnable() {
@Override
public void run() {
//plugin.getProxy().getPluginManager().callEvent(new PlayerJoinedNetworkEvent(message1.getTarget()));
Object event;
try {
event = plugin.getNetworkJoinEventClass().getDeclaredConstructor(UUID.class).newInstance(message1.getTarget());
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException("unable to dispatch an network join event", e);
}
plugin.callEvent(event);
}
});
break;
@ -194,7 +198,13 @@ public abstract class DataManager<P, PS, PL, PD> {
plugin.executeAsync(new Runnable() {
@Override
public void run() {
// plugin.getProxy().getPluginManager().callEvent(new PlayerLeftNetworkEvent(message2.getTarget()));
Object event;
try {
event = plugin.getNetworkQuitEventClass().getDeclaredConstructor(UUID.class).newInstance(message2.getTarget());
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException("unable to dispatch an network quit event", e);
}
plugin.callEvent(event);
}
});
break;
@ -205,7 +215,13 @@ public abstract class DataManager<P, PS, PL, PD> {
plugin.executeAsync(new Runnable() {
@Override
public void run() {
//plugin.getProxy().getPluginManager().callEvent(new PlayerChangedServerNetworkEvent(message3.getTarget(), message3.getPayload().getOldServer(), message3.getPayload().getServer()));
Object event;
try {
event = plugin.getServerChangeEventClass().getDeclaredConstructor(UUID.class, String.class, String.class).newInstance(message3.getTarget(), ((ServerChangePayload) message3.getPayload()).getOldServer(), ((ServerChangePayload) message3.getPayload()).getServer());
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException("unable to dispatch an server change event", e);
}
plugin.callEvent(event);
}
});
break;
@ -213,7 +229,6 @@ public abstract class DataManager<P, PS, PL, PD> {
}
public static class DataManagerMessage {
private final UUID target;
private final String source;

View File

@ -1,47 +1,34 @@
package com.imaginarycode.minecraft.redisbungee.internal;
import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent;
import redis.clients.jedis.JedisPubSub;
import java.lang.reflect.InvocationTargetException;
public class JedisPubSubHandler extends JedisPubSub {
private final RedisBungeePlugin<?> plugin;
private final Class<?> eventClass;
public JedisPubSubHandler(RedisBungeePlugin<?> plugin) {
this.plugin = plugin;
this.eventClass = plugin.getPubSubEventClass();
}
private Class<?> bungeeEvent;
@Override
public void onMessage(final String s, final String s2) {
if (s2.trim().length() == 0) return;
plugin.executeAsync(new Runnable() {
@Override
public void run() {
if (isBungeeEvent()) {
Object event;
try {
Object object = bungeeEvent.getConstructor(String.class, String.class).newInstance(s, s2);
plugin.callEvent(object);
event = eventClass.getDeclaredConstructor(String.class, String.class).newInstance(s, s2);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
throw new RuntimeException("unable to fire pubsub event.");
throw new RuntimeException("unable to dispatch an pubsub event", e);
}
return;
}
PubSubMessageEvent event = new PubSubMessageEvent(s, s2);
plugin.callEvent(event);
}
});
}
public boolean isBungeeEvent() {
return bungeeEvent != null;
}
public void setBungeeEvent(Class<?> clazz) {
bungeeEvent = clazz;
}
}

View File

@ -11,7 +11,7 @@ import java.util.logging.Level;
public class PubSubListener implements Runnable {
private JedisPubSubHandler jpsh;
private Set<String> addedChannels = new HashSet<String>();
private final Set<String> addedChannels = new HashSet<String>();
private final RedisBungeePlugin<?> plugin;
@ -24,6 +24,7 @@ public class PubSubListener implements Runnable {
boolean broken = false;
try (Jedis rsc = plugin.requestJedis()) {
try {
jpsh = new JedisPubSubHandler(plugin);
addedChannels.add("redisbungee-" + plugin.getConfiguration().getServerId());
addedChannels.add("redisbungee-allservers");

View File

@ -2,6 +2,10 @@ package com.imaginarycode.minecraft.redisbungee.internal;
import com.google.common.collect.Multimap;
import com.imaginarycode.minecraft.redisbungee.RedisBungeeAPI;
import com.imaginarycode.minecraft.redisbungee.events.PlayerChangedServerNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.events.PlayerJoinedNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.events.PlayerLeftNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent;
import com.imaginarycode.minecraft.redisbungee.internal.util.uuid.UUIDTranslator;
import redis.clients.jedis.Jedis;
@ -75,5 +79,20 @@ public interface RedisBungeePlugin<P> {
void executeProxyCommand(String cmd);
default Class<?> getPubSubEventClass() {
return PubSubMessageEvent.class;
}
default Class<?> getNetworkJoinEventClass() {
return PlayerJoinedNetworkEvent.class;
}
default Class<?> getServerChangeEventClass() {
return PlayerChangedServerNetworkEvent.class;
}
default Class<?> getNetworkQuitEventClass() {
return PlayerLeftNetworkEvent.class;
}
}

View File

@ -0,0 +1,38 @@
package com.imaginarycode.minecraft.redisbungee.events.bungee;
import net.md_5.bungee.api.plugin.Event;
import java.util.UUID;
/**
* This event is sent when a player connects to a new server. RedisBungee sends the event only when
* the proxy the player has been connected to is different than the local proxy.
* <p>
* This event corresponds to {@link net.md_5.bungee.api.event.ServerConnectedEvent}, and is fired
* asynchronously.
*
* @since 0.3.4
*/
public class PlayerChangedServerNetworkEvent extends Event {
private final UUID uuid;
private final String previousServer;
private final String server;
public PlayerChangedServerNetworkEvent(UUID uuid, String previousServer, String server) {
this.uuid = uuid;
this.previousServer = previousServer;
this.server = server;
}
public UUID getUuid() {
return uuid;
}
public String getServer() {
return server;
}
public String getPreviousServer() {
return previousServer;
}
}

View File

@ -0,0 +1,26 @@
package com.imaginarycode.minecraft.redisbungee.events.bungee;
import net.md_5.bungee.api.plugin.Event;
import java.util.UUID;
/**
* This event is sent when a player joins the network. RedisBungee sends the event only when
* the proxy the player has been connected to is different than the local proxy.
* <p>
* This event corresponds to {@link net.md_5.bungee.api.event.PostLoginEvent}, and is fired
* asynchronously.
*
* @since 0.3.4
*/
public class PlayerJoinedNetworkEvent extends Event {
private final UUID uuid;
public PlayerJoinedNetworkEvent(UUID uuid) {
this.uuid = uuid;
}
public UUID getUuid() {
return uuid;
}
}

View File

@ -0,0 +1,26 @@
package com.imaginarycode.minecraft.redisbungee.events.bungee;
import net.md_5.bungee.api.plugin.Event;
import java.util.UUID;
/**
* This event is sent when a player disconnects. RedisBungee sends the event only when
* the proxy the player has been connected to is different than the local proxy.
* <p>
* This event corresponds to {@link net.md_5.bungee.api.event.PlayerDisconnectEvent}, and is fired
* asynchronously.
*
* @since 0.3.4
*/
public class PlayerLeftNetworkEvent extends Event {
private final UUID uuid;
public PlayerLeftNetworkEvent(UUID uuid) {
this.uuid = uuid;
}
public UUID getUuid() {
return uuid;
}
}