Search in sources :

Example 1 with GuildEmojiCreateEvent

use of io.discloader.discloader.common.event.guild.emoji.GuildEmojiCreateEvent in project DiscLoader by R3alCl0ud.

the class EmojiUpdate method handle.

@Override
public void handle(SocketPacket packet) {
    String d = this.gson.toJson(packet.d);
    EmojiUpdateJSON data = gson.fromJson(d, EmojiUpdateJSON.class);
    IGuild guild = EntityRegistry.getGuildByID(data.guild_id);
    HashMap<Long, IGuildEmoji> emojis = new HashMap<>();
    for (EmojiJSON e : data.emojis) {
        IGuildEmoji emoji = new GuildEmoji(e, guild);
        emojis.put(emoji.getID(), emoji);
    }
    if (shouldEmit()) {
        if (data.emojis.length == guild.getEmojis().size()) {
            IGuildEmoji oldEmoji;
            for (IGuildEmoji emoji : emojis.values()) {
                oldEmoji = guild.getEmojis().get(emoji.getID());
                if (emoji.getID() == oldEmoji.getID() && !emoji.equals(oldEmoji)) {
                    GuildEmojiUpdateEvent event = new GuildEmojiUpdateEvent(emoji, oldEmoji);
                    loader.emit(Events.GUILD_EMOJI_UPDATE, event);
                    loader.emit(event);
                }
            }
        } else if (data.emojis.length < guild.getEmojis().size()) {
            for (IGuildEmoji emoji : guild.getEmojis().values()) {
                if (!emojis.containsKey(emoji.getID())) {
                    GuildEmojiDeleteEvent event = new GuildEmojiDeleteEvent(emoji);
                    loader.emit("GuildEmojiDelete", event);
                    loader.emit(event);
                }
            }
        } else if (data.emojis.length > guild.getEmojis().size()) {
            for (IGuildEmoji emoji : emojis.values()) {
                if (!guild.getEmojis().containsKey(emoji.getID())) {
                    GuildEmojiCreateEvent event = new GuildEmojiCreateEvent(emoji);
                    loader.emit("GuildEmojiCreate", event);
                    loader.emit(event);
                }
            }
        }
    }
    guild.getEmojis().clear();
    emojis.forEach((key, emoji) -> guild.getEmojis().put(key, emoji));
}
Also used : EmojiUpdateJSON(io.discloader.discloader.network.json.EmojiUpdateJSON) IGuildEmoji(io.discloader.discloader.entity.guild.IGuildEmoji) GuildEmojiUpdateEvent(io.discloader.discloader.common.event.guild.emoji.GuildEmojiUpdateEvent) GuildEmojiDeleteEvent(io.discloader.discloader.common.event.guild.emoji.GuildEmojiDeleteEvent) HashMap(java.util.HashMap) GuildEmoji(io.discloader.discloader.core.entity.guild.GuildEmoji) IGuildEmoji(io.discloader.discloader.entity.guild.IGuildEmoji) GuildEmojiCreateEvent(io.discloader.discloader.common.event.guild.emoji.GuildEmojiCreateEvent) IGuild(io.discloader.discloader.entity.guild.IGuild) EmojiJSON(io.discloader.discloader.network.json.EmojiJSON)

Aggregations

GuildEmojiCreateEvent (io.discloader.discloader.common.event.guild.emoji.GuildEmojiCreateEvent)1 GuildEmojiDeleteEvent (io.discloader.discloader.common.event.guild.emoji.GuildEmojiDeleteEvent)1 GuildEmojiUpdateEvent (io.discloader.discloader.common.event.guild.emoji.GuildEmojiUpdateEvent)1 GuildEmoji (io.discloader.discloader.core.entity.guild.GuildEmoji)1 IGuild (io.discloader.discloader.entity.guild.IGuild)1 IGuildEmoji (io.discloader.discloader.entity.guild.IGuildEmoji)1 EmojiJSON (io.discloader.discloader.network.json.EmojiJSON)1 EmojiUpdateJSON (io.discloader.discloader.network.json.EmojiUpdateJSON)1 HashMap (java.util.HashMap)1