Skip to content

Commit 05c3e57

Browse files
committed
Heads DB cleanup
1 parent c3169f8 commit 05c3e57

File tree

3 files changed

+55
-109
lines changed

3 files changed

+55
-109
lines changed

textures/src/main/java/com/dumbdogdiner/stickyapi/util/textures/TextureHelper.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.dumbdogdiner.stickyapi.util.StringUtil;
88
import com.dumbdogdiner.stickyapi.util.http.HttpUtil;
99
import com.google.gson.Gson;
10+
import com.google.gson.JsonElement;
1011
import com.google.gson.JsonObject;
1112
import com.google.gson.JsonParser;
1213
import com.google.gson.reflect.TypeToken;
@@ -20,6 +21,7 @@
2021
import java.io.IOException;
2122
import java.io.InputStream;
2223
import java.io.InputStreamReader;
24+
import java.net.MalformedURLException;
2325
import java.net.URL;
2426
import java.util.ArrayList;
2527
import java.util.Collections;
@@ -215,7 +217,6 @@ public static String getTexture(@NotNull String cat, @NotNull String name) throw
215217
* @throws InvalidTextureException if the JSON is invalid
216218
*/
217219
public static @NotNull String encodeJson(@NotNull JsonObject texture) throws InvalidTextureException {
218-
219220
TextureValidator.validateTextureJson(texture);
220221
return StringUtil.encodeBase64(HttpUtil.getDefaultGsonInstance().toJson(texture));
221222
}
@@ -230,6 +231,12 @@ public static JsonObject decodeTextureStringToJson(@NotNull String texture) {
230231
return JsonParser.parseString(texture).getAsJsonObject();
231232
}
232233

234+
//FIXME javadoc
235+
public static @NotNull URL decodeTextureStringToUrl(@NotNull String texture) throws MalformedURLException {
236+
return new URL(decodeTextureJsonToUrl(decodeTextureStringToJson(texture)));
237+
}
238+
239+
233240
/**
234241
* Converts a category and head to a qualified name (in the form of <code>{category}.{name}</code>
235242
*
@@ -240,4 +247,13 @@ public static JsonObject decodeTextureStringToJson(@NotNull String texture) {
240247
public static @NotNull String toQualifiedName(String category, String name) {
241248
return String.join(".", category, name).toUpperCase();
242249
}
250+
251+
// FIXME javadoc
252+
public static String decodeTextureJsonToUrl(JsonElement json) {
253+
return json
254+
.getAsJsonObject().get("textures")
255+
.getAsJsonObject().get("SKIN")
256+
.getAsJsonObject().get("url")
257+
.getAsString();
258+
}
243259
}

textures/src/main/java/com/dumbdogdiner/stickyapi/util/textures/TextureValidator.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,9 @@ public static void validateTextureString(String texture) throws InvalidTextureEx
133133
* @throws InvalidTextureException if the texture is invalid
134134
*/
135135
public static void validateTextureJson(@NotNull JsonElement json) throws InvalidTextureException{
136-
String textureURL = json
137-
.getAsJsonObject().get("textures")
138-
.getAsJsonObject().get("SKIN")
139-
.getAsJsonObject().get("url")
140-
.getAsString();
141136

142-
validateTextureUrl(textureURL);
137+
138+
validateTextureUrl(TextureHelper.decodeTextureJsonToUrl(json));
143139
}
144140

145141
/**

textures/src/main/java/com/dumbdogdiner/stickyapi/webapi/minecraftheads/MinecraftHeadsAPI.java

+36-102
Original file line numberDiff line numberDiff line change
@@ -7,138 +7,72 @@
77
import com.dumbdogdiner.stickyapi.util.http.HttpConnectionException;
88
import com.dumbdogdiner.stickyapi.util.http.HttpException;
99
import com.dumbdogdiner.stickyapi.util.http.HttpUtil;
10+
import com.dumbdogdiner.stickyapi.util.reflection.ReflectionUtil;
11+
import com.dumbdogdiner.stickyapi.util.textures.TextureHelper;
1012
import com.google.gson.JsonElement;
1113
import com.google.gson.JsonParser;
1214
import lombok.experimental.UtilityClass;
15+
import me.arcaniax.hdb.api.HeadDatabaseAPI;
1316
import okhttp3.HttpUrl;
1417
import okhttp3.Request;
1518
import okhttp3.Response;
1619
import org.jetbrains.annotations.Nullable;
1720

21+
import org.bukkit.Bukkit;
1822
import org.bukkit.entity.Cat;
23+
import org.bukkit.inventory.ItemStack;
24+
import org.bukkit.inventory.meta.ItemMeta;
25+
import org.bukkit.inventory.meta.SkullMeta;
1926

2027
import java.io.IOException;
28+
import java.net.MalformedURLException;
29+
import java.net.URL;
2130
import java.util.HashMap;
2231
import java.util.HashSet;
2332
import java.util.Map;
2433
import java.util.Set;
2534
import java.util.stream.Collectors;
2635

36+
/**
37+
* A simple basic interface to the minecraft-heads API plugin
38+
*/
2739
@UtilityClass
2840
public class MinecraftHeadsAPI {
29-
private static final HttpUrl APIURL = HttpUrl.parse("https://minecraft-heads.com/scripts/api.php");/* new HttpUrl.Builder()
30-
.scheme("https")
31-
.host("minecraft-heads.com")
32-
.addPathSegments("scripts/api.php")
33-
.build();*/
34-
35-
36-
37-
private final static Map<Category, Map<String /* name */, HeadInfo>> heads = new HashMap<>();
41+
private static final HeadDatabaseAPI mcheads;
3842
static {
39-
for (Category c : Category.values()){
40-
try {
41-
heads.put(c, update(c));
42-
} catch (HttpException e) {
43-
e.printStackTrace();
44-
}
45-
}
46-
}
47-
48-
public enum Category {
49-
ALPHABET, ANIMALS, BLOCKS, DECORATION, FOOD_DRINKS, HUMANS, HUMANOID, MISCELLANEOUS, MONSTERS, PLANTS;
50-
@Override
51-
public String toString(){
52-
return super.toString().toLowerCase().replace("_", "-");
53-
}
54-
}
55-
56-
57-
private static Request buildRequest(Category cat) {
58-
assert APIURL != null;
59-
return new Request.Builder().url(APIURL.newBuilder()
60-
.addQueryParameter("cat", cat.toString())
61-
.addQueryParameter("tags", Boolean.TRUE.toString())
62-
.build())
63-
.build();
64-
}
65-
66-
public static Set<String> getTags() throws HttpException {
67-
Set<String> tags = new HashSet<>();
68-
69-
for(Category c : Category.values()){
70-
tags.addAll(getTags(c));
43+
if(Bukkit.getPluginManager().getPlugin("HeadDatabase") != null){
44+
Bukkit.getLogger().severe("HeadDatabase Plugin was not found!");
45+
mcheads = null;
46+
} else {
47+
mcheads = new HeadDatabaseAPI();
7148
}
72-
return tags;
73-
}
74-
75-
public static Set<String> getTags(Category category) throws HttpException {
76-
Set<String> tagsSet = new HashSet<>();
77-
heads.get(category).values().stream().map(HeadInfo::getTags).collect(Collectors.toSet()).forEach(tagsSet::addAll);
78-
return tagsSet;
7949
}
8050

8151
/**
82-
*
83-
* @param cat Which category to return the heads for; If category is Null, it gives all heads, of all categories
84-
* @return
52+
* Takes in a HeadDatabase ID (see their head database command, ex: <pre>/hdb search id:43058</pre> and gets the associated texture
8553
*/
86-
public static Set<HeadInfo> getHeads(@Nullable Category cat) {
87-
88-
if(cat == null) {
89-
Set<HeadInfo> head = new HashSet<>();
90-
for(Category c : Category.values()){
91-
head.addAll(getHeads(c));
92-
}
93-
return head;
94-
}
95-
return new HashSet<>(heads.get(cat).values());
96-
}
97-
98-
public static HeadInfo getHead(Category cat, String name){
99-
return heads.get(cat).get(name);
100-
}
101-
102-
public static Set<HeadInfo> getHeadByTags(@Nullable Category c, String name){
103-
Set<HeadInfo> head = new HashSet<>();
104-
if(c == null) {
105-
for(Category cat : Category.values())
106-
head.addAll(getHeadByTags(cat, name));
107-
} else {
108-
heads.get(c).values().forEach(headInfo -> headInfo.getTags().forEach(s -> {
109-
if (s.equalsIgnoreCase(name))
110-
head.add(headInfo);
111-
}));
112-
}
113-
return head;
54+
public String getTextureString(int headId) {
55+
return mcheads.getBase64(Integer.toString(headId));
11456
}
11557

116-
public static void updateHeads() throws HttpException {
117-
for(Category c : Category.values()){
118-
updateHeads(c);
119-
}
120-
}
121-
122-
public static void updateHeads(Category c) throws HttpException {
123-
58+
public String getName(int headId){
59+
ItemStack head = mcheads.getItemHead(Integer.toString(headId));
60+
ItemMeta headMeta = head.getItemMeta();
61+
if(headMeta.hasLocalizedName())
62+
return headMeta.getLocalizedName();
63+
else if(headMeta.hasDisplayName())
64+
return headMeta.getDisplayName();
65+
else
66+
return head.getI18NDisplayName();
12467
}
12568

126-
private static Map<String, HeadInfo> update(Category c) throws HttpException {
127-
HashMap<String, HeadInfo> headInfoCategory = new HashMap<>();
128-
Request request = buildRequest(c);
69+
public URL getTextureUrl(int headId){
12970
try {
130-
Response r = HttpUtil.getDefaultClientInstance().newCall(request).execute();
131-
JsonElement resp = JsonParser.parseReader(r.body().charStream());
132-
assert resp.isJsonArray();
133-
for(JsonElement el : resp.getAsJsonArray()){
134-
HeadInfo info = HttpUtil.getDefaultGsonInstance().fromJson(el, HeadInfo.class);
135-
headInfoCategory.put(info.getName(), info);
136-
}
137-
return headInfoCategory;
138-
} catch (IOException e) {
139-
throw new HttpConnectionException(request, e);
140-
} catch (NullPointerException e){
141-
throw new HttpException("Received null body", e);
71+
return TextureHelper.decodeTextureStringToUrl(getTextureString(headId));
72+
} catch (MalformedURLException e) {
73+
e.printStackTrace();
74+
// Should not happen tbh
75+
return null;
14276
}
14377
}
14478
}

0 commit comments

Comments
 (0)