|
7 | 7 | import com.dumbdogdiner.stickyapi.util.http.HttpConnectionException;
|
8 | 8 | import com.dumbdogdiner.stickyapi.util.http.HttpException;
|
9 | 9 | import com.dumbdogdiner.stickyapi.util.http.HttpUtil;
|
| 10 | +import com.dumbdogdiner.stickyapi.util.reflection.ReflectionUtil; |
| 11 | +import com.dumbdogdiner.stickyapi.util.textures.TextureHelper; |
10 | 12 | import com.google.gson.JsonElement;
|
11 | 13 | import com.google.gson.JsonParser;
|
12 | 14 | import lombok.experimental.UtilityClass;
|
| 15 | +import me.arcaniax.hdb.api.HeadDatabaseAPI; |
13 | 16 | import okhttp3.HttpUrl;
|
14 | 17 | import okhttp3.Request;
|
15 | 18 | import okhttp3.Response;
|
16 | 19 | import org.jetbrains.annotations.Nullable;
|
17 | 20 |
|
| 21 | +import org.bukkit.Bukkit; |
18 | 22 | 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; |
19 | 26 |
|
20 | 27 | import java.io.IOException;
|
| 28 | +import java.net.MalformedURLException; |
| 29 | +import java.net.URL; |
21 | 30 | import java.util.HashMap;
|
22 | 31 | import java.util.HashSet;
|
23 | 32 | import java.util.Map;
|
24 | 33 | import java.util.Set;
|
25 | 34 | import java.util.stream.Collectors;
|
26 | 35 |
|
| 36 | +/** |
| 37 | + * A simple basic interface to the minecraft-heads API plugin |
| 38 | + */ |
27 | 39 | @UtilityClass
|
28 | 40 | 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; |
38 | 42 | 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(); |
71 | 48 | }
|
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; |
79 | 49 | }
|
80 | 50 |
|
81 | 51 | /**
|
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 |
85 | 53 | */
|
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)); |
114 | 56 | }
|
115 | 57 |
|
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(); |
124 | 67 | }
|
125 | 68 |
|
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){ |
129 | 70 | 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; |
142 | 76 | }
|
143 | 77 | }
|
144 | 78 | }
|
0 commit comments