2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2026-04-08 16:10:26 +00:00

api changes in events, move Listener serialization into Util class

This commit is contained in:
2022-07-30 22:36:29 +04:00
parent fdd537b276
commit 80a4791d12
29 changed files with 204 additions and 98 deletions

View File

@@ -31,13 +31,18 @@ public abstract class AbstractRedisBungeeAPI {
protected final List<String> reservedChannels;
AbstractRedisBungeeAPI(RedisBungeePlugin<?> plugin) {
abstractRedisBungeeAPI = this;
this.plugin = plugin;
// this does make sure that no one can place first initiated API class.
if (abstractRedisBungeeAPI == null) {
abstractRedisBungeeAPI = this;
}
this.reservedChannels = ImmutableList.of(
"redisbungee-allservers",
"redisbungee-" + plugin.getConfiguration().getProxyId(),
"redisbungee-data"
);
this.plugin = plugin;
}
/**
@@ -59,6 +64,7 @@ public abstract class AbstractRedisBungeeAPI {
public final long getLastOnline(@NonNull UUID player) {
return plugin.getDataManager().getLastOnline(player);
}
/**
* Get the server where the specified player is playing. This function also deals with the case of local players
* as well, and will return local information on them.
@@ -338,7 +344,7 @@ public abstract class AbstractRedisBungeeAPI {
* Kicks a player from the network
*
* @param playerName player name
* @param message kick message that player will see on kick
* @param message kick message that player will see on kick
* @since 0.8.0
*/
@@ -350,7 +356,7 @@ public abstract class AbstractRedisBungeeAPI {
* Kicks a player from the network
*
* @param playerUUID player name
* @param message kick message that player will see on kick
* @param message kick message that player will see on kick
* @since 0.8.0
*/
public void kickPlayer(UUID playerUUID, String message) {
@@ -361,9 +367,9 @@ public abstract class AbstractRedisBungeeAPI {
* This gives you instance of Jedis
*
* @return {@link Jedis}
* @since 0.7.0
* @throws IllegalStateException if the {@link #getMode()} is not equal to {@link RedisBungeeMode#SINGLE}
* @see #getJedisPool()
* @since 0.7.0
*/
public Jedis requestJedis() {
if (getMode() == RedisBungeeMode.SINGLE) {
@@ -377,9 +383,9 @@ public abstract class AbstractRedisBungeeAPI {
* This gets Redis Bungee {@link JedisPool}
*
* @return {@link JedisPool}
* @since 0.6.5
* @throws IllegalStateException if the {@link #getMode()} is not equal to {@link RedisBungeeMode#SINGLE}
* @throws IllegalStateException if JedisPool compatibility mode is disabled in the config
* @since 0.6.5
*/
public JedisPool getJedisPool() {
if (getMode() == RedisBungeeMode.SINGLE) {
@@ -398,8 +404,8 @@ public abstract class AbstractRedisBungeeAPI {
* WARNING DO NOT USE {@link JedisCluster#close()} it will break the functionally
*
* @return {@link redis.clients.jedis.JedisCluster}
* @since 0.8.0
* @throws IllegalStateException if the {@link #getMode()} is not equal to {@link RedisBungeeMode#CLUSTER}
* @since 0.8.0
*/
public JedisCluster requestClusterJedis() {
if (getMode() == RedisBungeeMode.CLUSTER) {
@@ -414,8 +420,8 @@ public abstract class AbstractRedisBungeeAPI {
* WARNING: DO NOT USE {@link redis.clients.jedis.JedisPooled#close()} it will break the functionally
*
* @return {@link redis.clients.jedis.JedisPooled}
* @since 0.8.0
* @throws IllegalStateException if the {@link #getMode()} is not equal to {@link RedisBungeeMode#SINGLE}
* @since 0.8.0
*/
public JedisCluster requestJedisPooled() {
if (getMode() == RedisBungeeMode.SINGLE) {

View File

@@ -171,7 +171,7 @@ public abstract class AbstractDataManager<P, PL, PD, PS> {
ipCache.put(message1.getTarget(), message1.getPayload().getAddress());
plugin.executeAsync(() -> {
Object event = plugin.createPlayerJoinedNetworkEvent(message1.getTarget());
plugin.callEvent(event);
plugin.fireEvent(event);
});
break;
case LEAVE:
@@ -181,7 +181,7 @@ public abstract class AbstractDataManager<P, PL, PD, PS> {
lastOnlineCache.put(message2.getTarget(), message2.getPayload().getTimestamp());
plugin.executeAsync(() -> {
Object event = plugin.createPlayerLeftNetworkEvent(message2.getTarget());
plugin.callEvent(event);
plugin.fireEvent(event);
});
break;
case SERVER_CHANGE:
@@ -189,8 +189,8 @@ public abstract class AbstractDataManager<P, PL, PD, PS> {
}.getType());
serverCache.put(message3.getTarget(), message3.getPayload().getServer());
plugin.executeAsync(() -> {
Object event = plugin.createPlayerChangedNetworkEvent(message3.getTarget(), message3.getPayload().getOldServer(), message3.getPayload().getServer());
plugin.callEvent(event);
Object event = plugin.createPlayerChangedServerNetworkEvent(message3.getTarget(), message3.getPayload().getOldServer(), message3.getPayload().getServer());
plugin.fireEvent(event);
});
break;
case KICK:

View File

@@ -38,34 +38,6 @@ public abstract class AbstractRedisBungeeListener<LE, PLE, PD, SC, PP, PM, PS> {
public abstract void onPluginMessage(PM event);
protected void serializeMultiset(Multiset<String> collection, ByteArrayDataOutput output) {
output.writeInt(collection.elementSet().size());
for (Multiset.Entry<String> entry : collection.entrySet()) {
output.writeUTF(entry.getElement());
output.writeInt(entry.getCount());
}
}
@SuppressWarnings("SameParameterValue")
protected void serializeMultimap(Multimap<String, String> collection, boolean includeNames, ByteArrayDataOutput output) {
output.writeInt(collection.keySet().size());
for (Map.Entry<String, Collection<String>> entry : collection.asMap().entrySet()) {
output.writeUTF(entry.getKey());
if (includeNames) {
serializeCollection(entry.getValue(), output);
} else {
output.writeInt(entry.getValue().size());
}
}
}
private void serializeCollection(Collection<?> collection, ByteArrayDataOutput output) {
output.writeInt(collection.size());
for (Object o : collection) {
output.writeUTF(o.toString());
}
}
public abstract void onPubSubMessage(PS event);

View File

@@ -1,23 +0,0 @@
package com.imaginarycode.minecraft.redisbungee.api;
import java.util.UUID;
/**
* Since each platform have their own events' implementation for example Bungeecord events extends Event while velocity don't
*
* @author Ham1255
* @since 0.7.0
*
*/
public interface EventsPlatform {
Object createPlayerChangedNetworkEvent(UUID uuid, String previousServer, String server);
Object createPlayerJoinedNetworkEvent(UUID uuid);
Object createPlayerLeftNetworkEvent(UUID uuid);
Object createPubSubEvent(String channel, String message);
}

View File

@@ -3,8 +3,6 @@ package com.imaginarycode.minecraft.redisbungee.api;
import redis.clients.jedis.JedisPubSub;
import java.lang.reflect.InvocationTargetException;
public class JedisPubSubHandler extends JedisPubSub {
@@ -21,7 +19,7 @@ public class JedisPubSubHandler extends JedisPubSub {
@Override
public void run() {
Object event = plugin.createPubSubEvent(s, s2);
plugin.callEvent(event);
plugin.fireEvent(event);
}
});
}

View File

@@ -5,8 +5,8 @@ import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import com.imaginarycode.minecraft.redisbungee.AbstractRedisBungeeAPI;
import com.imaginarycode.minecraft.redisbungee.api.config.ConfigLoader;
import com.imaginarycode.minecraft.redisbungee.api.config.RedisBungeeConfiguration;
import com.imaginarycode.minecraft.redisbungee.api.events.EventsPlatform;
import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner;
import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask;
import com.imaginarycode.minecraft.redisbungee.api.util.RedisUtil;
@@ -24,12 +24,12 @@ import static com.google.common.base.Preconditions.checkArgument;
/**
* This Class has all internal methods needed by every redis bungee plugin, and it can be used to implement another platforms than bungeecord
* This Class has all internal methods needed by every redis bungee plugin, and it can be used to implement another platforms than bungeecord or another forks of RedisBungee
*
* @author Ham1255
* @since 0.7.0
*/
public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
public interface RedisBungeePlugin<P> extends EventsPlatform {
default void initialize() {
@@ -201,8 +201,6 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
void executeAsyncAfter(Runnable runnable, TimeUnit timeUnit, int time);
void callEvent(Object event);
boolean isOnlineMode();
void logInfo(String msg);

View File

@@ -0,0 +1,24 @@
package com.imaginarycode.minecraft.redisbungee.api.events;
import java.util.UUID;
/**
* Since each platform have their own events' implementation for example Bungeecord events extends Event while velocity don't
*
* @author Ham1255
* @since 0.7.0
*
*/
public interface EventsPlatform {
IPlayerChangedServerNetworkEvent createPlayerChangedServerNetworkEvent(UUID uuid, String previousServer, String server);
IPlayerJoinedNetworkEvent createPlayerJoinedNetworkEvent(UUID uuid);
IPlayerLeftNetworkEvent createPlayerLeftNetworkEvent(UUID uuid);
IPubSubMessageEvent createPubSubEvent(String channel, String message);
void fireEvent(Object event);
}

View File

@@ -0,0 +1,13 @@
package com.imaginarycode.minecraft.redisbungee.api.events;
import java.util.UUID;
public interface IPlayerChangedServerNetworkEvent extends RedisBungeeEvent {
UUID getUuid();
String getServer();
String getPreviousServer();
}

View File

@@ -0,0 +1,9 @@
package com.imaginarycode.minecraft.redisbungee.api.events;
import java.util.UUID;
public interface IPlayerJoinedNetworkEvent extends RedisBungeeEvent {
UUID getUuid();
}

View File

@@ -0,0 +1,9 @@
package com.imaginarycode.minecraft.redisbungee.api.events;
import java.util.UUID;
public interface IPlayerLeftNetworkEvent extends RedisBungeeEvent {
UUID getUuid();
}

View File

@@ -0,0 +1,10 @@
package com.imaginarycode.minecraft.redisbungee.api.events;
public interface IPubSubMessageEvent extends RedisBungeeEvent {
String getChannel();
String getMessage();
}

View File

@@ -0,0 +1,4 @@
package com.imaginarycode.minecraft.redisbungee.api.events;
interface RedisBungeeEvent {
}

View File

@@ -0,0 +1,39 @@
package com.imaginarycode.minecraft.redisbungee.api.util.serialize;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;
import com.google.common.io.ByteArrayDataOutput;
import java.util.Collection;
import java.util.Map;
public class Serializations {
public static void serializeMultiset(Multiset<String> collection, ByteArrayDataOutput output) {
output.writeInt(collection.elementSet().size());
for (Multiset.Entry<String> entry : collection.entrySet()) {
output.writeUTF(entry.getElement());
output.writeInt(entry.getCount());
}
}
@SuppressWarnings("SameParameterValue")
public static void serializeMultimap(Multimap<String, String> collection, boolean includeNames, ByteArrayDataOutput output) {
output.writeInt(collection.keySet().size());
for (Map.Entry<String, Collection<String>> entry : collection.asMap().entrySet()) {
output.writeUTF(entry.getKey());
if (includeNames) {
serializeCollection(entry.getValue(), output);
} else {
output.writeInt(entry.getValue().size());
}
}
}
public static void serializeCollection(Collection<?> collection, ByteArrayDataOutput output) {
output.writeInt(collection.size());
for (Object o : collection) {
output.writeUTF(o.toString());
}
}
}