Skip to content

Commit d9b62fe

Browse files
committed
Fixed a giant bug due to the caching
Giant refactor! Now saves all gotten playerhead image files in the plugin's own data folder instead of overwriting BlueMap's playerheads directly. On every player join the playerhead from this plugin gets copied to BlueMap's folder and overwrites BlueMap's own file. Also removed the cache file as that's now combined into the own copy of the playerhead. Fixes #2
1 parent 50004ea commit d9b62fe

File tree

2 files changed

+72
-49
lines changed

2 files changed

+72
-49
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>net.mctechnic</groupId>
88
<artifactId>BlueMapFloodgate</artifactId>
9-
<version>1.3-BETA</version>
9+
<version>1.3.2-BETA</version>
1010
<packaging>jar</packaging>
1111

1212
<name>BlueMapFloodgate</name>

src/main/java/net/mctechnic/bluemapfloodgate/main.java

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@
2323
import java.net.MalformedURLException;
2424
import java.net.URL;
2525
import java.net.URLConnection;
26+
import java.nio.file.Files;
27+
import java.nio.file.Path;
28+
import java.nio.file.Paths;
2629
import java.util.Calendar;
30+
import java.util.UUID;
31+
32+
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
2733

2834
public final class main extends JavaPlugin implements Listener {
2935

@@ -33,7 +39,8 @@ public final class main extends JavaPlugin implements Listener {
3339
long cacheHours = 3 * 24; //three days by default
3440

3541
FloodgateApi floodgateApi;
36-
String playerheadsDirectory = "bluemap/web/assets/playerheads/";
42+
String blueMapPlayerheadsDirectory;
43+
File ownPlayerheadsDirectory;
3744

3845
@Override
3946
public void onEnable() {
@@ -51,7 +58,15 @@ public void onEnable() {
5158
getLogger().info("floodgate API ready!");
5259
}
5360

54-
BlueMapAPI.onEnable(blueMapAPI -> getLogger().info("BlueMap API ready!"));
61+
BlueMapAPI.onEnable(blueMapAPI -> {
62+
getLogger().info("BlueMap API ready!");
63+
blueMapPlayerheadsDirectory = "bluemap/web/assets/playerheads/"; //TODO: webroot
64+
});
65+
66+
ownPlayerheadsDirectory = new File(getDataFolder() + "/playerheads");
67+
if(ownPlayerheadsDirectory.mkdir()){
68+
verboseLog(ownPlayerheadsDirectory.toString() + " directory made");
69+
}
5570

5671
getServer().getPluginManager().registerEvents(this, this);
5772
}
@@ -61,63 +76,71 @@ public void onDisable() {
6176
// Plugin shutdown logic
6277
}
6378

64-
void verboseLog(String message) {
65-
if(verboseUpdateMessages) getLogger().info(message);
79+
private void verboseLog(String message) {
80+
if (verboseUpdateMessages) getLogger().info(message);
6681
}
6782

6883
@EventHandler
6984
public void onJoin(PlayerJoinEvent e) {
7085
Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
7186
Player p = e.getPlayer();
7287
if (floodgateApi.isFloodgatePlayer(p.getUniqueId())) {
73-
File cacheFile = new File(playerheadsDirectory + p.getUniqueId() + ".cache");
74-
if (cacheFile.exists()) {
75-
long lastModified = cacheFile.lastModified(); //long value representing the time the file was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)
76-
Calendar currentDate = Calendar.getInstance();
77-
long dateNow = currentDate.getTimeInMillis();
78-
if (dateNow < lastModified + 1000 * 60 * 60 * cacheHours) { //three days
79-
verboseLog("Head for " + p.getUniqueId() + " already cached");
80-
return;
81-
} else {
82-
verboseLog("Cache file for " + p.getUniqueId() + " outdated");
83-
}
84-
}
88+
floodgateJoin(p);
89+
}
90+
});
91+
}
8592

86-
getLogger().info("Grabbing head for " + p.getUniqueId());
87-
FloodgatePlayer floodgatePlayer = floodgateApi.getPlayer(p.getUniqueId());
88-
String xuid = floodgatePlayer.getXuid();
89-
String textureID = getTextureID(xuid);
90-
BufferedImage skin = getSkinFromID(textureID);
91-
if (skin != null) {
92-
BufferedImage head = skin.getSubimage(8, 8, 8, 8);
93-
try {
94-
ImageIO.write(head, "png", new File(playerheadsDirectory + p.getUniqueId() + ".png")); //TODO: webroot
95-
96-
try {
97-
if (cacheFile.createNewFile()) {
98-
verboseLog("Cache file created: " + cacheFile.getName());
99-
} else {
100-
verboseLog("Cache file already exists (It's probably outdated)");
101-
Calendar currentDate = Calendar.getInstance();
102-
long dateNow = currentDate.getTimeInMillis();
103-
if (cacheFile.setLastModified(dateNow)) {
104-
verboseLog("Cache file updated");
105-
} else {
106-
getLogger().warning("Cache file wasn't updated. This should never happen");
107-
}
108-
}
109-
} catch (IOException ioException) {
110-
ioException.printStackTrace();
111-
}
112-
} catch (IOException ioException) {
113-
ioException.printStackTrace();
114-
}
93+
private void getHeadToOwnFolder(UUID uuid, File f) {
94+
FloodgatePlayer floodgatePlayer = floodgateApi.getPlayer(uuid);
95+
String xuid = floodgatePlayer.getXuid();
96+
String textureID = getTextureID(xuid);
97+
BufferedImage skin = getSkinFromID(textureID);
98+
if (skin != null) {
99+
BufferedImage head = skin.getSubimage(8, 8, 8, 8);
100+
try {
101+
ImageIO.write(head, "png", f);
102+
verboseLog(f + " saved");
103+
} catch (IOException e) {
104+
e.printStackTrace();
105+
}
106+
}
107+
}
108+
109+
private void floodgateJoin(Player p) {
110+
File ownHeadFile = new File(ownPlayerheadsDirectory + "/" + p.getUniqueId() + ".png");
111+
if (ownHeadFile.exists()) {
112+
long lastModified = ownHeadFile.lastModified(); //long value representing the time the file was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)
113+
Calendar currentDate = Calendar.getInstance();
114+
long dateNow = currentDate.getTimeInMillis();
115+
if (dateNow > lastModified + 1000 * 60 * 60 * cacheHours) {
116+
verboseLog("Cache for " + p.getUniqueId() + " outdated");
117+
getHeadToOwnFolder(p.getUniqueId(), ownHeadFile);
118+
119+
if (ownHeadFile.setLastModified(dateNow)) {
120+
verboseLog(" Cache updated");
121+
} else {
122+
getLogger().warning(" Cache wasn't updated. This should never happen");
115123
}
124+
125+
} else {
126+
verboseLog("Head for " + p.getUniqueId() + " already cached");
116127
}
117-
});
128+
} else {
129+
getHeadToOwnFolder(p.getUniqueId(), ownHeadFile);
130+
}
131+
132+
verboseLog("Overwriting BlueMap's head with floodgate head");
133+
134+
Path destination = Paths.get(blueMapPlayerheadsDirectory, ownHeadFile.getName());
135+
try {
136+
Files.copy(ownHeadFile.toPath(), destination, REPLACE_EXISTING);
137+
// verboseLog("BlueMap file overwritten!");
138+
} catch (IOException e) {
139+
e.printStackTrace();
140+
}
118141
}
119142

120-
String getTextureID(String xuid) {
143+
private String getTextureID(String xuid) {
121144
URL url;
122145
try {
123146
url = new URL("https://api.geysermc.org/v1/skin/" + xuid);
@@ -140,7 +163,7 @@ String getTextureID(String xuid) {
140163
return null;
141164
}
142165

143-
BufferedImage getSkinFromID(String textureID) {
166+
private BufferedImage getSkinFromID(String textureID) {
144167
BufferedImage result;
145168
try {
146169
URL imageUrl = new URL("http://textures.minecraft.net/texture/" + textureID);

0 commit comments

Comments
 (0)