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;
@ -113,7 +114,7 @@ public abstract class DataManager<P, PS, PL, PD> {
} catch (ExecutionException | UncheckedExecutionException e) {
if (e.getCause() instanceof NullPointerException && e.getCause().getMessage().equals("user not found"))
return null; // HACK
plugin.logFatal( "Unable to get IP");
plugin.logFatal("Unable to get IP");
throw new RuntimeException("Unable to get IP for " + uuid, e);
}
}
@ -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);
@ -178,11 +175,18 @@ public abstract class DataManager<P, PS, PL, PD> {
}.getType());
proxyCache.put(message1.getTarget(), message1.getSource());
lastOnlineCache.put(message1.getTarget(), (long) 0);
ipCache.put(message1.getTarget(), ((LoginPayload)message1.getPayload()).getAddress());
ipCache.put(message1.getTarget(), ((LoginPayload) message1.getPayload()).getAddress());
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;
@ -190,22 +194,34 @@ public abstract class DataManager<P, PS, PL, PD> {
final DataManagerMessage message2 = gson.fromJson(jsonObject, new TypeToken<DataManagerMessage>() {
}.getType());
invalidate(message2.getTarget());
lastOnlineCache.put(message2.getTarget(), ((LogoutPayload)message2.getPayload()).getTimestamp());
lastOnlineCache.put(message2.getTarget(), ((LogoutPayload) message2.getPayload()).getTimestamp());
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;
case SERVER_CHANGE:
final DataManagerMessage message3 = gson.fromJson(jsonObject, new TypeToken<DataManagerMessage>() {
}.getType());
serverCache.put(message3.getTarget(), ((ServerChangePayload)message3.getPayload()).getServer());
serverCache.put(message3.getTarget(), ((ServerChangePayload) message3.getPayload()).getServer());
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;
@ -254,7 +269,7 @@ public abstract class DataManager<P, PS, PL, PD> {
}
public static class LoginPayload extends Payload{
public static class LoginPayload extends Payload {
private final InetAddress address;
public LoginPayload(InetAddress address) {
@ -266,7 +281,7 @@ public abstract class DataManager<P, PS, PL, PD> {
}
}
public static class ServerChangePayload extends Payload{
public static class ServerChangePayload extends Payload {
private final String server;
private final String oldServer;

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()) {
try {
Object object = bungeeEvent.getConstructor(String.class, String.class).newInstance(s, s2);
plugin.callEvent(object);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
throw new RuntimeException("unable to fire pubsub event.");
}
return;
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);
}
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;
}
}