Search in sources :

Example 1 with MemberCacheViewImpl

use of net.dv8tion.jda.internal.utils.cache.MemberCacheViewImpl in project JDA by DV8FromTheWorld.

the class EntityBuilder method updateMemberCache.

public boolean updateMemberCache(MemberImpl member, boolean forceRemove) {
    GuildImpl guild = member.getGuild();
    UserImpl user = (UserImpl) member.getUser();
    MemberCacheViewImpl membersView = guild.getMembersView();
    if (forceRemove || !getJDA().cacheMember(member)) {
        if (membersView.remove(member.getIdLong()) == null)
            return false;
        LOG.trace("Unloading member {}", member);
        if (user.getMutualGuilds().isEmpty()) {
            // we no longer share any guilds/channels with this user so remove it from cache
            user.setFake(true);
            getJDA().getUsersView().remove(user.getIdLong());
        }
        GuildVoiceStateImpl voiceState = (GuildVoiceStateImpl) member.getVoiceState();
        if (voiceState != null) {
            VoiceChannelImpl connectedChannel = (VoiceChannelImpl) voiceState.getChannel();
            if (connectedChannel != null)
                connectedChannel.getConnectedMembersMap().remove(member.getIdLong());
            voiceState.setConnectedChannel(null);
        }
        return false;
    } else if (guild.getMemberById(member.getIdLong()) != null) {
        // Member should be added to cache but already is cached -> do nothing
        return true;
    }
    LOG.trace("Loading member {}", member);
    if (getJDA().getUserById(user.getIdLong()) == null) {
        SnowflakeCacheViewImpl<User> usersView = getJDA().getUsersView();
        try (UnlockHook hook1 = usersView.writeLock()) {
            usersView.getMap().put(user.getIdLong(), user);
        }
    }
    try (UnlockHook hook = membersView.writeLock()) {
        membersView.getMap().put(member.getIdLong(), member);
        if (member.isOwner())
            guild.setOwner(member);
    }
    long hashId = guild.getIdLong() ^ user.getIdLong();
    getJDA().getEventCache().playbackCache(EventCache.Type.USER, member.getIdLong());
    getJDA().getEventCache().playbackCache(EventCache.Type.MEMBER, hashId);
    return true;
}
Also used : UnlockHook(net.dv8tion.jda.internal.utils.UnlockHook) MemberCacheViewImpl(net.dv8tion.jda.internal.utils.cache.MemberCacheViewImpl)

Aggregations

UnlockHook (net.dv8tion.jda.internal.utils.UnlockHook)1 MemberCacheViewImpl (net.dv8tion.jda.internal.utils.cache.MemberCacheViewImpl)1