-
-
Notifications
You must be signed in to change notification settings - Fork 294
/
ProfileRequest.java
128 lines (107 loc) · 3.41 KB
/
ProfileRequest.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package net.citizensnpcs.npc.profile;
import java.util.ArrayDeque;
import java.util.Deque;
import javax.annotation.Nullable;
import org.bukkit.Bukkit;
import com.google.common.base.Preconditions;
import com.mojang.authlib.GameProfile;
import net.citizensnpcs.api.CitizensAPI;
/**
* Stores basic information about a single profile used to request profiles from the Mojang servers.
*
* <p>
* Also stores the result of the request.
* </p>
*/
public class ProfileRequest {
private Deque<ProfileFetchHandler> handlers;
private final String playerName;
private GameProfile profile;
private volatile ProfileFetchResult result = ProfileFetchResult.PENDING;
/**
* Constructor.
*
* @param playerName
* The name of the player whose profile is being requested.
* @param handler
* Optional handler to handle the result for the profile. Handler always invoked from the main thread.
*/
public ProfileRequest(String playerName, ProfileFetchHandler handler) {
Preconditions.checkNotNull(playerName);
this.playerName = playerName;
if (handler != null) {
addHandler(handler);
}
}
/**
* Add one time result handler.
*
* <p>
* Handler is always invoked from the main thread.
* </p>
*
* @param handler
* The result handler.
*/
public void addHandler(ProfileFetchHandler handler) {
Preconditions.checkNotNull(handler);
if (result != ProfileFetchResult.PENDING) {
handler.onResult(this);
return;
}
if (handlers == null)
handlers = new ArrayDeque<ProfileFetchHandler>();
handlers.addLast(handler);
}
/**
* Get the name of the player the requested profile belongs to.
*/
public String getPlayerName() {
return playerName;
}
/**
* Get the game profile that was requested.
*
* @return The game profile or null if the profile has not been retrieved yet or there was an error while retrieving
* the profile.
*/
@Nullable
public GameProfile getProfile() {
return profile;
}
/**
* Get the result of the profile fetch.
*/
public ProfileFetchResult getResult() {
return result;
}
/**
* Invoked to set the profile result.
*
* <p>
* Can be invoked from any thread, always executes on the main thread.
* </p>
*
* @param profile
* The profile. Null if there was an error.
* @param result
* The result of the request.
*/
void setResult(final @Nullable GameProfile profile, final ProfileFetchResult result) {
if (!CitizensAPI.hasImplementation())
return;
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
ProfileRequest.this.profile = profile;
ProfileRequest.this.result = result;
if (handlers == null)
return;
while (!handlers.isEmpty()) {
handlers.removeFirst().onResult(ProfileRequest.this);
}
handlers = null;
}
});
}
}