Skip to content

Commit

Permalink
Fixed a giant bug due to the caching
Browse files Browse the repository at this point in the history
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
  • Loading branch information
TechnicJelle committed Aug 1, 2021
1 parent 50004ea commit d9b62fe
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 49 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.mctechnic</groupId>
<artifactId>BlueMapFloodgate</artifactId>
<version>1.3-BETA</version>
<version>1.3.2-BETA</version>
<packaging>jar</packaging>

<name>BlueMapFloodgate</name>
Expand Down
119 changes: 71 additions & 48 deletions src/main/java/net/mctechnic/bluemapfloodgate/main.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Calendar;
import java.util.UUID;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

public final class main extends JavaPlugin implements Listener {

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

FloodgateApi floodgateApi;
String playerheadsDirectory = "bluemap/web/assets/playerheads/";
String blueMapPlayerheadsDirectory;
File ownPlayerheadsDirectory;

@Override
public void onEnable() {
Expand All @@ -51,7 +58,15 @@ public void onEnable() {
getLogger().info("floodgate API ready!");
}

BlueMapAPI.onEnable(blueMapAPI -> getLogger().info("BlueMap API ready!"));
BlueMapAPI.onEnable(blueMapAPI -> {
getLogger().info("BlueMap API ready!");
blueMapPlayerheadsDirectory = "bluemap/web/assets/playerheads/"; //TODO: webroot
});

ownPlayerheadsDirectory = new File(getDataFolder() + "/playerheads");
if(ownPlayerheadsDirectory.mkdir()){
verboseLog(ownPlayerheadsDirectory.toString() + " directory made");
}

getServer().getPluginManager().registerEvents(this, this);
}
Expand All @@ -61,63 +76,71 @@ public void onDisable() {
// Plugin shutdown logic
}

void verboseLog(String message) {
if(verboseUpdateMessages) getLogger().info(message);
private void verboseLog(String message) {
if (verboseUpdateMessages) getLogger().info(message);
}

@EventHandler
public void onJoin(PlayerJoinEvent e) {
Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
Player p = e.getPlayer();
if (floodgateApi.isFloodgatePlayer(p.getUniqueId())) {
File cacheFile = new File(playerheadsDirectory + p.getUniqueId() + ".cache");
if (cacheFile.exists()) {
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)
Calendar currentDate = Calendar.getInstance();
long dateNow = currentDate.getTimeInMillis();
if (dateNow < lastModified + 1000 * 60 * 60 * cacheHours) { //three days
verboseLog("Head for " + p.getUniqueId() + " already cached");
return;
} else {
verboseLog("Cache file for " + p.getUniqueId() + " outdated");
}
}
floodgateJoin(p);
}
});
}

getLogger().info("Grabbing head for " + p.getUniqueId());
FloodgatePlayer floodgatePlayer = floodgateApi.getPlayer(p.getUniqueId());
String xuid = floodgatePlayer.getXuid();
String textureID = getTextureID(xuid);
BufferedImage skin = getSkinFromID(textureID);
if (skin != null) {
BufferedImage head = skin.getSubimage(8, 8, 8, 8);
try {
ImageIO.write(head, "png", new File(playerheadsDirectory + p.getUniqueId() + ".png")); //TODO: webroot

try {
if (cacheFile.createNewFile()) {
verboseLog("Cache file created: " + cacheFile.getName());
} else {
verboseLog("Cache file already exists (It's probably outdated)");
Calendar currentDate = Calendar.getInstance();
long dateNow = currentDate.getTimeInMillis();
if (cacheFile.setLastModified(dateNow)) {
verboseLog("Cache file updated");
} else {
getLogger().warning("Cache file wasn't updated. This should never happen");
}
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
private void getHeadToOwnFolder(UUID uuid, File f) {
FloodgatePlayer floodgatePlayer = floodgateApi.getPlayer(uuid);
String xuid = floodgatePlayer.getXuid();
String textureID = getTextureID(xuid);
BufferedImage skin = getSkinFromID(textureID);
if (skin != null) {
BufferedImage head = skin.getSubimage(8, 8, 8, 8);
try {
ImageIO.write(head, "png", f);
verboseLog(f + " saved");
} catch (IOException e) {
e.printStackTrace();
}
}
}

private void floodgateJoin(Player p) {
File ownHeadFile = new File(ownPlayerheadsDirectory + "/" + p.getUniqueId() + ".png");
if (ownHeadFile.exists()) {
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)
Calendar currentDate = Calendar.getInstance();
long dateNow = currentDate.getTimeInMillis();
if (dateNow > lastModified + 1000 * 60 * 60 * cacheHours) {
verboseLog("Cache for " + p.getUniqueId() + " outdated");
getHeadToOwnFolder(p.getUniqueId(), ownHeadFile);

if (ownHeadFile.setLastModified(dateNow)) {
verboseLog(" Cache updated");
} else {
getLogger().warning(" Cache wasn't updated. This should never happen");
}

} else {
verboseLog("Head for " + p.getUniqueId() + " already cached");
}
});
} else {
getHeadToOwnFolder(p.getUniqueId(), ownHeadFile);
}

verboseLog("Overwriting BlueMap's head with floodgate head");

Path destination = Paths.get(blueMapPlayerheadsDirectory, ownHeadFile.getName());
try {
Files.copy(ownHeadFile.toPath(), destination, REPLACE_EXISTING);
// verboseLog("BlueMap file overwritten!");
} catch (IOException e) {
e.printStackTrace();
}
}

String getTextureID(String xuid) {
private String getTextureID(String xuid) {
URL url;
try {
url = new URL("https://api.geysermc.org/v1/skin/" + xuid);
Expand All @@ -140,7 +163,7 @@ String getTextureID(String xuid) {
return null;
}

BufferedImage getSkinFromID(String textureID) {
private BufferedImage getSkinFromID(String textureID) {
BufferedImage result;
try {
URL imageUrl = new URL("http://textures.minecraft.net/texture/" + textureID);
Expand Down

0 comments on commit d9b62fe

Please sign in to comment.