Search in sources :

Example 1 with DataCache

use of de.codecrafter47.data.api.DataCache in project BungeeTabListPlus by CodeCrafter47.

the class RedisPlayerManager method onRedisMessage.

@EventHandler
@SuppressWarnings("unchecked")
public void onRedisMessage(PubSubMessageEvent event) {
    String channel = event.getChannel();
    if (channel.equals(CHANNEL_DATA_REQUEST)) {
        ByteArrayDataInput input = ByteStreams.newDataInput(Base64.getDecoder().decode(event.getMessage()));
        try {
            UUID uuid = DataStreamUtils.readUUID(input);
            ProxiedPlayer proxiedPlayer = ProxyServer.getInstance().getPlayer(uuid);
            if (proxiedPlayer != null) {
                BungeePlayer player = bungeePlayerProvider.getPlayerIfPresent(proxiedPlayer);
                if (player != null) {
                    DataKey<?> key = DataStreamUtils.readDataKey(input, keyRegistry, missingDataKeyLogger);
                    if (key != null) {
                        player.addDataChangeListener((DataKey<Object>) key, new DataChangeListener(player, (DataKey<Object>) key));
                        updateData(uuid, (DataKey<Object>) key, player.get(key));
                    }
                }
            }
        } catch (IOException ex) {
            logger.log(Level.SEVERE, "Unexpected error reading redis message", ex);
        }
    } else if (channel.equals(CHANNEL_DATA_UPDATE)) {
        ByteArrayDataInput input = ByteStreams.newDataInput(Base64.getDecoder().decode(event.getMessage()));
        try {
            UUID uuid = DataStreamUtils.readUUID(input);
            RedisPlayer player = byUUID.get(uuid);
            if (player != null) {
                DataCache cache = player.getData();
                DataKey<?> key = DataStreamUtils.readDataKey(input, keyRegistry, missingDataKeyLogger);
                if (key != null) {
                    boolean removed = input.readBoolean();
                    if (removed) {
                        mainThread.execute(() -> cache.updateValue(key, null));
                    } else {
                        Object value = typeRegistry.getTypeAdapter(key.getType()).read(input);
                        mainThread.execute(() -> cache.updateValue((DataKey<Object>) key, value));
                    }
                }
            }
        } catch (IOException ex) {
            logger.log(Level.SEVERE, "Unexpected error reading redis message", ex);
        }
    } else if (channel.equals(CHANNEL_DATA_OLD) || channel.equals(CHANNEL_REQUEST_DATA_OLD)) {
        logger.warning("BungeeTabListPlus on at least one proxy in your network is outdated.");
    }
}
Also used : ProxiedPlayer(net.md_5.bungee.api.connection.ProxiedPlayer) BungeePlayer(codecrafter47.bungeetablistplus.player.BungeePlayer) DataKey(de.codecrafter47.data.api.DataKey) IOException(java.io.IOException) ByteArrayDataInput(com.google.common.io.ByteArrayDataInput) RedisPlayer(codecrafter47.bungeetablistplus.player.RedisPlayer) DataCache(de.codecrafter47.data.api.DataCache) EventHandler(net.md_5.bungee.event.EventHandler)

Aggregations

BungeePlayer (codecrafter47.bungeetablistplus.player.BungeePlayer)1 RedisPlayer (codecrafter47.bungeetablistplus.player.RedisPlayer)1 ByteArrayDataInput (com.google.common.io.ByteArrayDataInput)1 DataCache (de.codecrafter47.data.api.DataCache)1 DataKey (de.codecrafter47.data.api.DataKey)1 IOException (java.io.IOException)1 ProxiedPlayer (net.md_5.bungee.api.connection.ProxiedPlayer)1 EventHandler (net.md_5.bungee.event.EventHandler)1