Skip to content

Commit 5962e4d

Browse files
author
TheProgramSrc
committed
Changelog:
* SimpleItem Code Improvements * SkinTextureManager Cache Changes * SkinTexture Code Improvement
1 parent 40d8442 commit 5962e4d

File tree

4 files changed

+60
-71
lines changed

4 files changed

+60
-71
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>xyz.theprogramsrc</groupId>
88
<artifactId>SuperCoreAPI</artifactId>
9-
<version>4.1.2-BETA_3</version>
9+
<version>4.1.2</version>
1010
<packaging>jar</packaging>
1111

1212
<name>SuperCoreAPI</name>

src/main/java/xyz/theprogramsrc/supercoreapi/spigot/items/Skulls.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public SkinTexture asSkinTexture(){
4747
*/
4848
public static SkinTexture fromDataBase(String key){
4949
if(cache == null) cache = new LinkedHashMap<>();
50-
return new SkinTexture(cache.getOrDefault(key, "http://textures.minecraft.net/texture/badc048a7ce78f7dad72a07da27d85c0916881e5522eeed1e3daf217a38c1a"), System.currentTimeMillis());
50+
return new SkinTexture(cache.getOrDefault(key, "http://textures.minecraft.net/texture/badc048a7ce78f7dad72a07da27d85c0916881e5522eeed1e3daf217a38c1a"));
5151
}
5252

5353
/**

src/main/java/xyz/theprogramsrc/supercoreapi/spigot/utils/skintexture/SkinTexture.java

Lines changed: 19 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,21 @@
99
import org.bukkit.entity.Player;
1010
import xyz.theprogramsrc.supercoreapi.global.utils.Utils;
1111

12+
import java.io.IOException;
1213
import java.net.URL;
1314
import java.nio.charset.Charset;
1415
import java.util.UUID;
1516

1617
@SuppressWarnings("ALL")
1718
public class SkinTexture {
1819
private String url;
19-
private long createdAt;
2020

2121
/**
2222
* Creates a new SkinTexture
2323
* @param url the url of the texture
24-
* @param createdAt Time created {@link System#currentTimeMillis()}
2524
*/
26-
public SkinTexture(String url, long createdAt) {
25+
public SkinTexture(String url) {
2726
this.url = url;
28-
this.createdAt = createdAt;
29-
}
30-
31-
/**
32-
* Parse a SkinTexture from serialized format
33-
* (URL:split:TIME_CREATED)
34-
* @param serialized
35-
*/
36-
public SkinTexture(String serialized) {
37-
this.url = serialized.split(":split:")[0];
38-
this.createdAt = Long.valueOf(serialized.split(":split:")[1]);
3927
}
4028

4129
/**
@@ -46,21 +34,13 @@ public String getUrl() {
4634
return this.url;
4735
}
4836

49-
/**
50-
* Gets when was created the SkinTexture
51-
* @return creation time in millis
52-
*/
53-
public long getCreatedAt() {
54-
return this.createdAt;
55-
}
56-
5737
/**
5838
* Serialize the SkinTexture
5939
* (URL:split:TIME_CREATED)
6040
* @return the serialized SkinTexture
6141
*/
6242
public String toString() {
63-
return this.url + ":split:" + this.createdAt;
43+
return this.url;
6444
}
6545

6646
/**
@@ -70,15 +50,10 @@ public String toString() {
7050
*/
7151
public static SkinTexture fromPlayer(Player player) {
7252
GameProfile gameProfile = PlayerGameProfile.get(player);
73-
74-
try {
75-
Property texturesProperty = (Property)gameProfile.getProperties().get("textures").iterator().next();
76-
String texturesPropertyValue = texturesProperty.getValue();
77-
String base64ToUrl = base64ToUrl(texturesPropertyValue);
78-
return base64ToUrl != null && !base64ToUrl.equals("null") ? new SkinTexture(base64ToUrl, System.currentTimeMillis()) : null;
79-
} catch (Exception ex) {
80-
return null;
81-
}
53+
Property texturesProperty = (Property)gameProfile.getProperties().get("textures").iterator().next();
54+
String texturesPropertyValue = texturesProperty.getValue();
55+
String base64ToUrl = base64ToUrl(texturesPropertyValue);
56+
return base64ToUrl != null && !base64ToUrl.equals("null") ? new SkinTexture(base64ToUrl) : null;
8257
}
8358

8459
/**
@@ -87,16 +62,9 @@ public static SkinTexture fromPlayer(Player player) {
8762
* @return the skin
8863
*/
8964
public static SkinTexture fromMojang(String playerName) {
90-
String url = "https://api.mojang.com/users/profiles/minecraft/" + playerName;
91-
String response = getResponse(url);
92-
93-
try {
94-
String uuid = (new JsonParser()).parse(response).getAsJsonObject().get("id").getAsString();
95-
String fullUUID = Utils.uuidToFullUUID(uuid);
96-
return fromMojang(UUID.fromString(fullUUID));
97-
} catch (Exception ex) {
98-
return null;
99-
}
65+
String uuid = (new JsonParser()).parse(getResponse("https://api.mojang.com/users/profiles/minecraft/" + playerName)).getAsJsonObject().get("id").getAsString();
66+
String fullUUID = Utils.uuidToFullUUID(uuid);
67+
return fromMojang(UUID.fromString(fullUUID));
10068
}
10169

10270
/**
@@ -105,15 +73,9 @@ public static SkinTexture fromMojang(String playerName) {
10573
* @return the skin
10674
*/
10775
public static SkinTexture fromMojang(UUID uuid) {
108-
String url = "https://sessionserver.mojang.com/session/minecraft/profile/" + uuid.toString().replace("-", "") + "?unsigned=false";
109-
String response = getResponse(url);
110-
try {
111-
JsonObject properties = (new JsonParser()).parse(response).getAsJsonObject().get("properties").getAsJsonArray().get(0).getAsJsonObject();
112-
String value = properties.get("value").getAsString();
113-
return new SkinTexture(base64ToUrl(value), System.currentTimeMillis());
114-
} catch (Exception ex) {
115-
return null;
116-
}
76+
JsonObject properties = (new JsonParser()).parse(getResponse("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid.toString().replace("-", "") + "?unsigned=false")).getAsJsonObject().get("properties").getAsJsonArray().get(0).getAsJsonObject();
77+
String value = properties.get("value").getAsString();
78+
return new SkinTexture(base64ToUrl(value));
11779
}
11880

11981
/**
@@ -122,31 +84,22 @@ public static SkinTexture fromMojang(UUID uuid) {
12284
* @return the skin
12385
*/
12486
public static SkinTexture fromURL(String url){
125-
return new SkinTexture(url, System.currentTimeMillis());
87+
return new SkinTexture(url);
12688
}
12789

12890
private static String getResponse(String stringUrl) {
12991
try {
13092
URL url = new URL(stringUrl);
13193
return IOUtils.toString(url.openStream(), Charset.forName("UTF-8"));
132-
} catch (Exception ex) {
94+
} catch (IOException e) {
95+
e.printStackTrace();
13396
return null;
13497
}
13598
}
13699

137100
private static String base64ToUrl(String base64) {
138-
try {
139-
String var1 = new String(Base64.decodeBase64(base64.getBytes()));
140-
JsonObject var2 = (JsonObject)(new JsonParser()).parse(var1);
141-
JsonObject var3 = var2.getAsJsonObject("textures").getAsJsonObject("SKIN");
142-
if (var3 == null) {
143-
return null;
144-
} else {
145-
String var4 = var3.get("url").getAsString();
146-
return var4;
147-
}
148-
} catch (Exception ex) {
149-
return null;
150-
}
101+
String content = new String(Base64.decodeBase64(base64.getBytes()));
102+
JsonObject json = new JsonParser().parse(content).getAsJsonObject().get("textures").getAsJsonObject().get("SKIN").getAsJsonObject();
103+
return json != null ? json.get("url").getAsString() : null;
151104
}
152105
}

src/main/java/xyz/theprogramsrc/supercoreapi/spigot/utils/skintexture/SkinTextureManager.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ public class SkinTextureManager {
1212

1313
private final HashMap<String, SkinTexture> cache;
1414
private final HashMap<String, SkinTexture> urls;
15+
private final HashMap<String, SkinTexture> db;
1516

1617
public SkinTextureManager(){
1718
this.cache = new HashMap<>();
1819
this.urls = new HashMap<>();
20+
this.db = new HashMap<>();
1921
}
2022

2123
/**
@@ -35,6 +37,11 @@ public SkinTexture getSkin(Player player) {
3537
return this.cache.get(key);
3638
}
3739

40+
/**
41+
* Gets a skin from an url
42+
* @param url the url of the skin
43+
* @return the skin
44+
*/
3845
public SkinTexture getSkin(String url){
3946
String key = Utils.encodeBase64(url);
4047
if(!this.urls.containsKey(key)){
@@ -51,14 +58,43 @@ public SkinTexture getSkin(String url){
5158
public void clearCache(){
5259
this.cache.clear();
5360
this.urls.clear();
61+
this.db.clear();
5462
}
5563

5664
/**
5765
* Gets a SkinTexture from the database
58-
* @param key the key of the SkinTexture
66+
* @param name the key/name of the SkinTexture
5967
* @return null if there is any error, otherwise the skin
6068
*/
61-
public SkinTexture fromDataBase(String key){
62-
return Skulls.fromDataBase(key);
69+
public SkinTexture fromDataBase(String name){
70+
String key = Utils.encodeBase64(name);
71+
if(!this.db.containsKey(key)){
72+
this.db.put(key, Skulls.fromDataBase(name));
73+
}
74+
return this.db.get(key);
75+
}
76+
77+
/**
78+
* Gets the cache of the player skulls
79+
* @return the cache of the players skulls
80+
*/
81+
public HashMap<String, SkinTexture> getPlayersCache() {
82+
return this.cache;
83+
}
84+
85+
/**
86+
* Gets the cache of the database skulls
87+
* @return the cache of the database skulls
88+
*/
89+
public HashMap<String, SkinTexture> getDataBaseCache() {
90+
return this.db;
91+
}
92+
93+
/**
94+
* Gets the cache of the URLs skulls
95+
* @return the cache of the URLs skulls
96+
*/
97+
public HashMap<String, SkinTexture> getURLsCache() {
98+
return this.urls;
6399
}
64100
}

0 commit comments

Comments
 (0)