/
GameBoxAPI.java
210 lines (186 loc) · 6.56 KB
/
GameBoxAPI.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package me.nikl.gamebox;
import me.nikl.gamebox.data.GBPlayer;
import me.nikl.gamebox.data.database.DataBase;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import javax.annotation.Nullable;
import java.util.UUID;
/**
* API for GameBox
* <p>
* Use this class to retrieve and manipulate GameBox data.
*/
public class GameBoxAPI {
private GameBox plugin;
public GameBoxAPI(GameBox plugin) {
this.plugin = plugin;
}
/**
* Give token to the specified player.
* This might be done async and this method returns before the value is changed!
*
* @param player name
* @param count token to give
*/
public void giveToken(OfflinePlayer player, int count) {
Validate.notNull(player, "Player cannot be null!");
Validate.isTrue(count > 0, "token count to give must be greater then 0");
// handle cached players
GBPlayer gbPlayer = plugin.getPluginManager().getPlayer(player.getUniqueId());
if (gbPlayer != null) {
if (gbPlayer.isLoaded()) {
gbPlayer.setTokens(gbPlayer.getTokens() + count);
return;
} else {
// player data is being looked up... Wait a bit and try again
Bukkit.getScheduler().runTaskLater(plugin, () -> giveToken(player, count), 2);
return;
}
}
// handle offline or not cached players
plugin.getDataBase().getToken(player.getUniqueId(), new DataBase.Callback<Integer>() {
@Override
public void onSuccess(Integer done) {
plugin.getDataBase().setToken(player.getUniqueId(), done + count);
}
@Override
public void onFailure(Throwable throwable, Integer value) {
plugin.getLogger().warning(" Failed to handle API call giveToken for player: " + player.getName());
if (throwable != null) throwable.printStackTrace();
}
});
}
/**
* Set Token count for a player to a specific value.
*
* @param player to manipulate
* @param count new token count
*/
public void setToken(OfflinePlayer player, int count) {
Validate.notNull(player, "Player cannot be null!");
Validate.isTrue(count >= 0, "token count must be greater then or equal 0");
// handle cached players
GBPlayer gbPlayer = plugin.getPluginManager().getPlayer(player.getUniqueId());
if (gbPlayer != null) {
if (gbPlayer.isLoaded()) {
gbPlayer.setTokens(count);
return;
} else {
// player data is being looked up... Wait a bit and try again
Bukkit.getScheduler().runTaskLater(plugin, () -> setToken(player, count), 2);
return;
}
}
plugin.getDataBase().setToken(player.getUniqueId(), count);
}
/**
* Take token from a specified player
*
* @param player to take the token from
* @param count token to take
* @param callback to call with outcome
*/
public void takeToken(OfflinePlayer player, int count, DataBase.Callback<Integer> callback) {
Validate.notNull(player, "Player cannot be null!");
Validate.isTrue(count > 0, "token to take must be greater then 0");
// handle cached players
GBPlayer gbPlayer = plugin.getPluginManager().getPlayer(player.getUniqueId());
if (gbPlayer != null) {
if (gbPlayer.isLoaded()) {
if (gbPlayer.getTokens() >= count) {
gbPlayer.setTokens(gbPlayer.getTokens() - count);
callback.onSuccess(gbPlayer.getTokens());
return;
} else {
callback.onFailure(null, gbPlayer.getTokens());
return;
}
} else {
// player data is being looked up... Wait a bit and try again
Bukkit.getScheduler().runTaskLater(plugin, () -> takeToken(player, count, callback), 2);
return;
}
}
plugin.getDataBase().getToken(player.getUniqueId(), new DataBase.Callback<Integer>() {
@Override
public void onSuccess(Integer done) {
if (done >= count) {
plugin.getDataBase().setToken(player.getUniqueId(), done - count);
callback.onSuccess(done - count);
return;
} else {
callback.onFailure(null, done);
return;
}
}
@Override
public void onFailure(@Nullable Throwable throwable, @Nullable Integer value) {
callback.onFailure(throwable, null);
return;
}
});
}
/**
* Get token count for online/offline player
* <p>
* This is done async for not loaded players and will return the value via Callback
*
* @param player to look up
* @param callback to call with loaded token count or error
*/
public void getToken(OfflinePlayer player, DataBase.Callback<Integer> callback) {
Validate.notNull(player, "Player cannot be null!");
// handle cached players
GBPlayer gbPlayer = plugin.getPluginManager().getPlayer(player.getUniqueId());
if (gbPlayer != null) {
if (gbPlayer.isLoaded()) {
callback.onSuccess(gbPlayer.getTokens());
} else {
// player data is being looked up... Wait a bit and try again
GameBox.debug("rescheduling lookup... Player is being loaded while lookup try.");
Bukkit.getScheduler().runTaskLater(plugin, () -> getToken(player, callback), 2);
return;
}
}
plugin.getDataBase().getToken(player.getUniqueId(), callback);
}
/**
* Get token count for a loaded GBPlayer
* <p>
* First check via GameBoxAPI#isGBPlayer(UUID)
*
* @param player to check
* @return token count
*/
public int getCachedToken(Player player) {
Validate.notNull(player, "Player cannot be null!");
Validate.isTrue(isGBPlayer(player), "Player has to be GBPlayer with loaded data! Check via GameBoxAPI#isGBPlayer(UUID)");
// handle cached player
return plugin.getPluginManager().getPlayer(player.getUniqueId()).getTokens();
}
/**
* Check for a player being a cached gamebox player and having his data loaded
* <p>
* If this returns true, you can get the player and manipulate his tokens...
*
* @param uuid to look up
* @return player is GBPlayer and has his data loaded
*/
public boolean isGBPlayer(UUID uuid) {
GBPlayer gbPlayer = plugin.getPluginManager().getPlayer(uuid);
return gbPlayer != null && gbPlayer.isLoaded();
}
/**
* Check for a player being a cached gamebox player and having his data loaded
* <p>
* If this returns true, you can get the player and manipulate his tokens...
*
* @param player to look up
* @return player is GBPlayer and has his data loaded
*/
public boolean isGBPlayer(OfflinePlayer player) {
return isGBPlayer(player.getUniqueId());
}
}