/
GuildMembersChunkHandler.java
98 lines (83 loc) · 3.33 KB
/
GuildMembersChunkHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Copyright 2015-2017 Austin Keener & Michael Ritter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.core.handle;
import gnu.trove.map.TLongIntMap;
import gnu.trove.map.TLongObjectMap;
import gnu.trove.map.hash.TLongIntHashMap;
import gnu.trove.map.hash.TLongObjectHashMap;
import net.dv8tion.jda.core.entities.EntityBuilder;
import net.dv8tion.jda.core.entities.impl.JDAImpl;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.LinkedList;
import java.util.List;
public class GuildMembersChunkHandler extends SocketHandler
{
private final TLongIntMap expectedGuildMembers = new TLongIntHashMap();
private final TLongObjectMap<List<JSONArray>> memberChunksCache = new TLongObjectHashMap<>();
public GuildMembersChunkHandler(JDAImpl api)
{
super(api);
}
@Override
protected Long handleInternally(JSONObject content)
{
final long guildId = content.getLong("guild_id");
List<JSONArray> memberChunks = memberChunksCache.get(guildId);
int expectMemberCount = expectedGuildMembers.get(guildId);
JSONArray members = content.getJSONArray("members");
JDAImpl.LOG.debug("GUILD_MEMBER_CHUNK for: " + guildId + " \tMembers: " + members.length());
memberChunks.add(members);
int currentTotal = 0;
for (JSONArray arr : memberChunks)
{
currentTotal += arr.length();
}
if (currentTotal >= expectMemberCount)
{
JDAImpl.LOG.debug("Finished chunking for: " + guildId);
api.getEntityBuilder().createGuildSecondPass(guildId, memberChunks);
memberChunksCache.remove(guildId);
expectedGuildMembers.remove(guildId);
}
return null;
}
public void setExpectedGuildMembers(long guildId, int count)
{
if (expectedGuildMembers.containsKey(guildId))
JDAImpl.LOG.warn("Set the count of expected users from GuildMembersChunk even though a value already exists! GuildId: " + guildId);
expectedGuildMembers.put(guildId, count);
if (memberChunksCache.containsKey(guildId))
JDAImpl.LOG.warn("Set the memberChunks for MemberChunking for a guild that was already setup for chunking! GuildId: " + guildId);
memberChunksCache.put(guildId, new LinkedList<>());
}
public void modifyExpectedGuildMember(long guildId, int changeAmount)
{
try
{
Integer i = expectedGuildMembers.get(guildId);
i += changeAmount;
expectedGuildMembers.put(guildId, i);
}
//Ignore. If one of the above things doesn't exist, causing an NPE, then we don't need to worry.
catch (NullPointerException ignored) {}
}
public void clearCache()
{
expectedGuildMembers.clear();
memberChunksCache.clear();
}
}