23
23
import java .net .MalformedURLException ;
24
24
import java .net .URL ;
25
25
import java .net .URLConnection ;
26
+ import java .nio .file .Files ;
27
+ import java .nio .file .Path ;
28
+ import java .nio .file .Paths ;
26
29
import java .util .Calendar ;
30
+ import java .util .UUID ;
31
+
32
+ import static java .nio .file .StandardCopyOption .REPLACE_EXISTING ;
27
33
28
34
public final class main extends JavaPlugin implements Listener {
29
35
@@ -33,7 +39,8 @@ public final class main extends JavaPlugin implements Listener {
33
39
long cacheHours = 3 * 24 ; //three days by default
34
40
35
41
FloodgateApi floodgateApi ;
36
- String playerheadsDirectory = "bluemap/web/assets/playerheads/" ;
42
+ String blueMapPlayerheadsDirectory ;
43
+ File ownPlayerheadsDirectory ;
37
44
38
45
@ Override
39
46
public void onEnable () {
@@ -51,7 +58,15 @@ public void onEnable() {
51
58
getLogger ().info ("floodgate API ready!" );
52
59
}
53
60
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
+ }
55
70
56
71
getServer ().getPluginManager ().registerEvents (this , this );
57
72
}
@@ -61,63 +76,71 @@ public void onDisable() {
61
76
// Plugin shutdown logic
62
77
}
63
78
64
- void verboseLog (String message ) {
65
- if (verboseUpdateMessages ) getLogger ().info (message );
79
+ private void verboseLog (String message ) {
80
+ if (verboseUpdateMessages ) getLogger ().info (message );
66
81
}
67
82
68
83
@ EventHandler
69
84
public void onJoin (PlayerJoinEvent e ) {
70
85
Bukkit .getScheduler ().runTaskAsynchronously (this , () -> {
71
86
Player p = e .getPlayer ();
72
87
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
+ }
85
92
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" );
115
123
}
124
+
125
+ } else {
126
+ verboseLog ("Head for " + p .getUniqueId () + " already cached" );
116
127
}
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
+ }
118
141
}
119
142
120
- String getTextureID (String xuid ) {
143
+ private String getTextureID (String xuid ) {
121
144
URL url ;
122
145
try {
123
146
url = new URL ("https://api.geysermc.org/v1/skin/" + xuid );
@@ -140,7 +163,7 @@ String getTextureID(String xuid) {
140
163
return null ;
141
164
}
142
165
143
- BufferedImage getSkinFromID (String textureID ) {
166
+ private BufferedImage getSkinFromID (String textureID ) {
144
167
BufferedImage result ;
145
168
try {
146
169
URL imageUrl = new URL ("http://textures.minecraft.net/texture/" + textureID );
0 commit comments