-
Notifications
You must be signed in to change notification settings - Fork 19
/
HeadAPI.java
189 lines (169 loc) · 5.68 KB
/
HeadAPI.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
package tsp.headdb.core.api;
import org.bukkit.Bukkit;
import tsp.headdb.HeadDB;
import tsp.headdb.core.storage.PlayerData;
import tsp.headdb.core.util.Utils;
import tsp.headdb.implementation.category.Category;
import tsp.headdb.implementation.head.Head;
import tsp.headdb.implementation.head.HeadDatabase;
import tsp.headdb.implementation.head.LocalHead;
import tsp.headdb.implementation.requester.HeadProvider;
import tsp.helperlite.scheduler.promise.Promise;
import javax.annotation.Nonnull;
import java.util.*;
import java.util.stream.Collectors;
/**
* Head API for interacting with the main {@link HeadDatabase}.
*
* @author TheSilentPro (Silent)
* @see HeadDatabase
*/
public final class HeadAPI {
/**
* Utility class. No initialization nor extension.
*/
private HeadAPI() {}
/**
* The main {@link HeadDatabase}.
*/
private static final HeadDatabase database = new HeadDatabase(HeadDB.getInstance(), HeadProvider.HEAD_STORAGE);
/**
* Retrieve a {@link List} of {@link Head} matching the name.
*
* @param name The name to match against
* @param lenient Whether the filter should be lenient when matching
* @return {@link List<Head> Heads}
*/
@Nonnull
public static List<Head> getHeadsByName(String name, boolean lenient) {
return getHeads().stream().filter(head -> (lenient ? Utils.matches(head.getName(), name) : head.getName().equalsIgnoreCase(name))).collect(Collectors.toList());
}
/**
* Retrieve a {@link List} of {@link Head} matching the name.
*
* @param name The name to match against
* @return {@link List<Head> Heads}
*/
@Nonnull
public static List<Head> getHeadsByName(String name) {
return getHeadsByName(name, true);
}
/**
* Retrieve a {@link Head} by its exact name.
*
* @param name The name to look for
* @param lenient Whether the filter to be lenient when matching
* @return The {@link Head}, else empty
*/
public static Optional<Head> getHeadByExactName(String name, boolean lenient) {
return getHeads().stream().filter(head -> (lenient ? Utils.matches(head.getName(), name) : head.getName().equalsIgnoreCase(name))).findAny();
}
/**
* Retrieve a {@link Head} by its exact name.
*
* @param name The name to look for
* @return The {@link Head}, else empty
*/
@Nonnull
public static Optional<Head> getHeadByExactName(String name) {
return getHeadByExactName(name, false);
}
/**
* Retrieve a {@link Head} by its id.
*
* @param id The id to look for
* @return The {@link Head}, else empty
*/
@Nonnull
public static Optional<Head> getHeadById(int id) {
return getHeads().stream().filter(head -> head.getId() == id).findAny();
}
/**
* Retrieve a {@link Head} by its texture value.
*
* @param texture The texture to look for
* @return The {@link Head}, else empty
*/
@Nonnull
public static Optional<Head> getHeadByTexture(String texture) {
return getHeads().stream().filter(head -> head.getTexture().equals(texture)).findAny();
}
/**
* Retrieve a {@link List} of {@link Head} within the main {@link HeadDatabase}.
*
* @return {@link List<Head> Heads}
*/
@Nonnull
public static List<Head> getHeads() {
List<Head> result = new ArrayList<>();
for (Category category : getHeadsMap().keySet()) {
result.addAll(getHeads(category));
}
return result;
}
/**
* Retrieve a {@link List} of {@link Head} within a {@link Category}.
*
* @param category The category to retrieve the heads from
* @return {@link List<Head> Heads}
*/
@Nonnull
public static List<Head> getHeads(Category category) {
return getHeadsMap().get(category);
}
/**
* Retrieve an unmodifiable view of the database head map.
*
* @return The map
*/
@Nonnull
public static Map<Category, List<Head>> getHeadsMap() {
return Collections.unmodifiableMap(database.getHeads());
}
/**
* Retrieve the total amount of {@link Head heads} present in the main {@link HeadDatabase}.
*
* @return Amount of heads
*/
public static int getTotalHeads() {
return getHeads().size();
}
/**
* Retrieve a {@link Set} of local heads.
* Note that this calculates the heads on every call.
*
* @return {@link Set<LocalHead> Local Heads}
*/
@Nonnull
public static Set<LocalHead> getLocalHeads() {
return Arrays.stream(Bukkit.getOfflinePlayers()).map(player -> new LocalHead(player.getUniqueId(), player.getName())).collect(Collectors.toSet());
}
/**
* Retrieve a {@link Set} of favorite heads for the specified {@link UUID player id}.
* Note that this calculates the heads on every call.
*
* @param player The players id
* @return {@link Set<Head> Favorite Heads}
*/
@Nonnull
public static Promise<List<Head>> getFavoriteHeads(UUID player) {
return Promise.supplyingAsync(() -> {
List<Head> result = new ArrayList<>();
Optional<PlayerData> data = HeadDB.getInstance().getStorage().getPlayerStorage().get(player);
data.ifPresent(playerData -> playerData.favorites()
.forEach(texture -> getHeadByTexture(texture)
.ifPresent(result::add))
);
return result;
});
}
/**
* Retrieve the main {@link HeadDatabase} used by the plugin.
*
* @return {@link HeadDatabase Database}
*/
@Nonnull
public static HeadDatabase getDatabase() {
return database;
}
}