/
SkullManager.java
289 lines (249 loc) · 8.75 KB
/
SkullManager.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package ca.tweetzy.skulls.model;
import ca.tweetzy.skulls.Skulls;
import ca.tweetzy.skulls.api.enums.SkullsDefaultCategory;
import ca.tweetzy.skulls.api.event.SkullsReadyEvent;
import ca.tweetzy.skulls.impl.Skull;
import ca.tweetzy.skulls.impl.SkullCategory;
import ca.tweetzy.tweety.Common;
import ca.tweetzy.tweety.FileUtil;
import ca.tweetzy.tweety.RandomUtil;
import ca.tweetzy.tweety.collection.StrictList;
import ca.tweetzy.tweety.remain.Remain;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* The current file has been created by Kiran Hart
* Date Created: September 27 2021
* Time Created: 10:12 p.m.
* Usage of any code found within this class is prohibited unless given explicit permission otherwise
*/
public final class SkullManager {
@Getter
@Setter
private boolean downloading = false;
@Getter
private boolean loading = false;
@Getter
private final StrictList<Skull> skulls = new StrictList<>();
@Getter
private final StrictList<Integer> blocked = new StrictList<>();
public void addSkull(@NonNull final Skull skull) {
this.skulls.addIfNotExist(skull);
}
public void removeSkull(@NonNull final Skull skull) {
this.skulls.remove(skull);
}
public Skull getSkull(final int id) {
return this.skulls.getSource().stream().filter(all -> all.getId() == id).findFirst().orElse(null);
}
public Skull getRandomSkull() {
return RandomUtil.nextItem(this.skulls);
}
public ItemStack getSkullItemStack(final int id) {
return this.getSkull(id).getItemStack();
}
public List<Skull> getSkullsByCategory(@NonNull final String category) {
return this.skulls.getSource().stream().filter(skull -> skull.getCategory().equalsIgnoreCase(category)).collect(Collectors.toList());
}
public List<Skull> getSkullsByCategory(@NonNull final SkullCategory category) {
return this.getSkullsByCategory(category.getId());
}
public List<Skull> getSkullsByTerm(@NonNull final String keywords) {
if (keywords.startsWith("id:")) {
final int id = Integer.parseInt(keywords.split(":")[1]);
return new ArrayList<>(Collections.singleton(this.getSkull(id)));
}
final List<Skull> locatedSkulls = new ArrayList<>();
for (Skull skull : this.skulls) {
if (match(keywords, skull.getName()) || match(keywords, skull.getCategory()) || skull.getTags().getSource().stream().anyMatch(tag -> match(keywords, tag)))
locatedSkulls.add(skull);
}
return locatedSkulls;
}
public void updateSkullPrice(final int skullId, final double price) {
Skulls.getInstance().getDataFile().setField("Prices." + skullId, price);
}
public void toggleSkullBlock(final int skullId) {
if (Skulls.getInstance().getDataFile().contains("Blocked." + skullId)) {
Skulls.getInstance().getDataFile().setField("Blocked." + skullId, null);
this.blocked.removeWeak(skullId);
} else {
Skulls.getInstance().getDataFile().setField("Blocked." + skullId, true);
this.blocked.addIfNotExist(skullId);
}
}
public boolean isBlocked(final int skullId) {
return this.blocked.contains(skullId);
}
public List<Skull> getSkullsByIds(@NonNull final List<Integer> ids) {
final List<Skull> locatedSkulls = new ArrayList<>();
for (final int id : ids) {
locatedSkulls.add(getSkull(id));
}
return locatedSkulls;
}
/**
* For internal use only
*/
public void downloadHeadCategory(@NonNull final SkullsDefaultCategory category) {
try {
final File dir = new File(Skulls.getInstance().getDataFolder() + "/heads");
if (!dir.exists()) {
//noinspection ResultOfMethodCallIgnored
dir.mkdir();
}
final JsonArray json;
switch (category) {
case ALPHABET:
json = getSkullsJson("https://rose.tweetzy.ca/minecraft/skulls?category=alphabet");
break;
case ANIMALS:
json = getSkullsJson("https://rose.tweetzy.ca/minecraft/skulls?category=animals");
break;
case BLOCKS:
json = getSkullsJson("https://rose.tweetzy.ca/minecraft/skulls?category=blocks");
break;
case DECORATION:
json = getSkullsJson("https://rose.tweetzy.ca/minecraft/skulls?category=decoration");
break;
case FOOD_AND_DRINKS:
json = getSkullsJson("https://rose.tweetzy.ca/minecraft/skulls?category=food%20&%20drinks");
break;
case HUMANS:
json = getSkullsJson("https://rose.tweetzy.ca/minecraft/skulls?category=humans");
break;
case HUMANOID:
json = getSkullsJson("https://rose.tweetzy.ca/minecraft/skulls?category=humanoid");
break;
case MISCELLANEOUS:
json = getSkullsJson("https://rose.tweetzy.ca/minecraft/skulls?category=miscellaneous");
break;
case MONSTERS:
json = getSkullsJson("https://rose.tweetzy.ca/minecraft/skulls?category=monsters");
break;
case PLANTS:
json = getSkullsJson("https://rose.tweetzy.ca/minecraft/skulls?category=plants");
break;
default:
throw new IllegalStateException("Unexpected value: " + category);
}
final FileWriter fileWriter = new FileWriter(Skulls.getInstance().getDataFolder() + "/heads/" + category.getId() + ".json");
fileWriter.write(json.toString());
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* For internal use only
*/
public void downloadHeads(final boolean redownload) {
if (redownload) {
final File directory = FileUtil.getFile("/heads");
if (directory.exists()) {
for (File file : FileUtil.getFiles("heads", "json")) {
file.delete();
}
}
}
final File[] knownFiles = FileUtil.getFiles("/heads", "json");
if (knownFiles.length != 10 || redownload) {
for (SkullsDefaultCategory category : SkullsDefaultCategory.values()) {
final File headFile = FileUtil.getFile("/heads/" + category.getId() + ".json");
if (!headFile.exists()) {
Common.tell(Bukkit.getConsoleSender(), "&eDownloading heads for category&f: &6" + category.getId());
downloadHeadCategory(category);
}
}
}
loadHeads();
}
/**
* For internal use only
*/
private void loadHeads() {
final File[] headFiles = FileUtil.getFiles("/heads", "json");
final JsonParser parser = new JsonParser();
this.skulls.clear();
this.loading = true;
try {
for (final File headFile : headFiles) {
final JsonArray jsonArray = (JsonArray) parser.parse(new FileReader(headFile));
jsonArray.forEach(jsonElement -> {
final JsonObject jsonObject = jsonElement.getAsJsonObject();
addSkull(new Skull(
Integer.parseInt(replace(jsonObject.get("id").toString())),
replace(jsonObject.get("name").toString()),
replace(jsonObject.get("category").toString()),
new StrictList<>(replace(jsonObject.get("tags").toString()).split(",")),
replace(jsonObject.get("texture").toString())
));
});
}
this.loading = false;
Common.callEvent(new SkullsReadyEvent());
// load blocked heads
try {
final ConfigurationSection section = Skulls.getInstance().getDataFile().getConfigField("Blocked");
if (section != null) {
for (String id : section.getKeys(false)) {
this.blocked.add(Integer.parseInt(id));
}
}
} catch (Exception e) {
Common.log("&cNo blocked skulls found");
}
Common.tell(Bukkit.getConsoleSender(), "&eLoaded " + this.skulls.size() + " skulls");
Remain.getOnlinePlayers().stream().filter(Player::isOp).sequential().forEach(player -> {
Common.tell(player, "&aSkulls is ready for usage, loaded " + this.skulls.size() + " skulls.");
});
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* For internal use only
*/
private JsonArray getSkullsJson(final String url) throws IOException {
final InputStream inputStream = new URL(url).openStream();
final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
final StringBuilder builder = new StringBuilder();
int character;
while ((character = bufferedReader.read()) != -1) {
builder.append((char) character);
}
final JsonParser parser = new JsonParser();
return (JsonArray) parser.parse(builder.toString());
}
/**
* For internal use only
*/
private String replace(String in) {
return in.replace("\"", "");
}
/**
* For internal use only
*/
private boolean match(String pattern, String sentence) {
Pattern patt = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = patt.matcher(sentence);
return matcher.find();
}
}