1 |
package dk.thoerup.bukkit.hoeruputils.web; |
package dk.thoerup.bukkit.hoeruputils.web; |
2 |
|
|
3 |
import java.io.File; |
import java.io.IOException; |
4 |
|
import java.io.OutputStream; |
5 |
|
import java.net.HttpURLConnection; |
6 |
import java.text.SimpleDateFormat; |
import java.text.SimpleDateFormat; |
7 |
import java.util.Date; |
import java.util.Date; |
8 |
import java.util.List; |
|
9 |
import java.util.Map; |
import org.bukkit.Location; |
10 |
|
import org.bukkit.OfflinePlayer; |
11 |
import org.bukkit.Server; |
import org.bukkit.Server; |
12 |
import org.bukkit.util.config.ConfigurationNode; |
import org.bukkit.configuration.Configuration; |
13 |
import org.bukkit.entity.Player; |
import org.bukkit.entity.Player; |
14 |
import org.bukkit.plugin.Plugin; |
import org.bukkit.plugin.Plugin; |
|
import org.bukkit.util.config.Configuration; |
|
|
import org.kokakiwi.apicraft.events.ApiEvent; |
|
|
import org.kokakiwi.apicraft.events.ApiListener; |
|
15 |
|
|
16 |
public class StatusListener extends ApiListener { |
import com.sun.net.httpserver.HttpExchange; |
17 |
|
import com.sun.net.httpserver.HttpHandler; |
18 |
|
|
19 |
|
|
20 |
|
public class StatusListener implements HttpHandler{ |
21 |
|
|
22 |
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
23 |
|
|
24 |
Server server; |
Server server; |
25 |
public StatusListener(Server server) { |
Configuration loginConfig; |
26 |
this.server = server; |
Configuration homeConfig; |
27 |
|
|
28 |
|
long cachedTime = 0; |
29 |
|
String cachedXml = ""; |
30 |
|
|
31 |
|
public StatusListener(Plugin plugin, Server server, Configuration loginConfig, Configuration homeConfig) { |
32 |
|
|
33 |
|
this.server = server; |
34 |
|
this.loginConfig = loginConfig; |
35 |
|
this.homeConfig = homeConfig; |
36 |
} |
} |
37 |
|
|
38 |
String formatTime(String input) { |
String formatTime(String input) { |
39 |
Date d = new Date( Long.parseLong(input) ); |
return formatTime( Long.parseLong(input) ); |
40 |
|
} |
41 |
|
String formatTime(long input) { |
42 |
|
Date d = new Date( input ); |
43 |
return format.format(d ); |
return format.format(d ); |
44 |
} |
} |
45 |
|
|
46 |
@Override |
@Override |
47 |
public void onApiEvent(ApiEvent event) { |
public void handle(HttpExchange http) throws IOException { |
48 |
if(event.path[0].equalsIgnoreCase("webstatus")) { |
/* if(!event.path[0].equalsIgnoreCase("webstatus")) { |
49 |
StringBuilder sb = new StringBuilder(); |
return; |
50 |
|
}*/ |
51 |
|
|
52 |
|
String xml; |
53 |
|
|
54 |
|
synchronized(this) { //is this necessary ? |
55 |
|
long now = System.currentTimeMillis(); |
56 |
|
if ( (now-cachedTime) > 5000) { |
57 |
|
xml = buildXML(); |
58 |
|
cachedXml = xml; |
59 |
|
cachedTime = now; |
60 |
|
} else { |
61 |
|
xml = cachedXml; |
62 |
|
} |
63 |
|
} |
64 |
|
|
|
//out.writeBytes("Server: " + server.getServerName() + "\n"); |
|
|
sb.append("Version: " + server.getVersion() + "\n"); |
|
65 |
|
|
66 |
long mb = 1024*1024; |
/* event.setResponse(xml); |
67 |
Runtime rt = Runtime.getRuntime(); |
event.setActionTaken(true);*/ |
68 |
sb.append("VM Memory, free: " + rt.freeMemory()/mb + ", total:" + rt.totalMemory()/mb + ", max:" + rt.maxMemory()/mb + "\n\n"); |
byte bytes[] = xml.getBytes(); |
69 |
|
http.sendResponseHeaders(HttpURLConnection.HTTP_OK, bytes.length ); |
70 |
sb.append("--- Online players ---\n"); |
final OutputStream os = http.getResponseBody(); |
71 |
for (Player p : server.getOnlinePlayers()) { |
os.write(bytes); |
72 |
sb.append( p.getName() + "\n"); |
os.close(); |
73 |
|
http.close(); |
74 |
|
} |
75 |
|
|
76 |
|
private String buildXML() { |
77 |
|
|
78 |
|
StringBuilder sb = new StringBuilder(); |
79 |
|
|
80 |
|
sb.append("<?xml version=\"1.0\"?> \n"); |
81 |
|
sb.append("<serverinfo>"); |
82 |
|
|
83 |
|
sb.append("<version>" + server.getVersion() + "</version>\n"); |
84 |
|
|
85 |
|
long mb = 1024*1024; |
86 |
|
Runtime rt = Runtime.getRuntime(); |
87 |
|
sb.append("<memory>\n"); |
88 |
|
sb.append("<free>" + rt.freeMemory()/mb + "</free>\n"); |
89 |
|
sb.append("<total>" + rt.totalMemory()/mb + "</total>\n"); |
90 |
|
sb.append("<max>" + rt.maxMemory()/mb + "</max>\n"); |
91 |
|
sb.append("</memory>\n"); |
92 |
|
|
93 |
|
|
94 |
|
long now = System.currentTimeMillis(); |
95 |
|
|
96 |
|
sb.append("<players>\n"); |
97 |
|
OfflinePlayer playerList[] = server.getOfflinePlayers(); |
98 |
|
for (OfflinePlayer player : playerList) { |
99 |
|
long first = player.getFirstPlayed(); |
100 |
|
long last = player.getLastPlayed(); |
101 |
|
|
102 |
|
|
103 |
|
sb.append("<player>\n"); |
104 |
|
sb.append("<name>" + player.getName() + "</name>\n" ); |
105 |
|
|
106 |
|
sb.append("<firstseen>" + formatTime(first) + "</firstseen>\n"); |
107 |
|
sb.append("<lastseen>" + formatTime(last) + "</lastseen>\n"); |
108 |
|
|
109 |
|
sb.append("<online>" + (player.isOnline() ? 1 : 0 ) + "</online>\n" ); |
110 |
|
|
111 |
|
if (player.isOnline() ) { |
112 |
|
long login = loginConfig.getLong( player.getName() + ".logintime", -1); |
113 |
|
long elapsed = (login>-1 ? (now-login) / 1000 : -1); |
114 |
|
|
115 |
|
Player p2 = server.getPlayer( player.getName() ); |
116 |
|
Location loc = p2.getLocation(); |
117 |
|
|
118 |
|
sb.append("<sessionlength>" + elapsed + "</sessionlength>\n" ); |
119 |
|
sb.append("<ip>" + p2.getAddress().getAddress().toString().replace("/","") + "</ip>\n" ); |
120 |
|
sb.append("<location>"); |
121 |
|
sb.append("<x>" + loc.getBlockX() + "</x>"); |
122 |
|
sb.append("<y>" + loc.getBlockY() + "</y>"); |
123 |
|
sb.append("<z>" + loc.getBlockZ() + "</z>"); |
124 |
|
sb.append("<world>" + loc.getWorld().getName() + "</world>"); |
125 |
|
sb.append("</location>"); |
126 |
} |
} |
|
sb.append("\n"); |
|
127 |
|
|
128 |
sb.append("--- Last seen players/offline ---\n"); |
String homeWorld = homeConfig.getString( player.getName() + ".world"); |
129 |
File configFile = new File("./plugins/LastSeen/times.yml"); |
if (homeWorld != null) { |
130 |
if (configFile.exists() ) { |
sb.append("<home>"); |
131 |
Configuration config = new Configuration(configFile); |
sb.append("<world>" + homeWorld + "</world>\n"); |
132 |
config.load(); |
sb.append("<x>" + (int)homeConfig.getDouble(player.getName() + ".x") + "</x>\n"); |
133 |
|
sb.append("<y>" + (int)homeConfig.getDouble(player.getName() + ".y") + "</y>\n"); |
134 |
List<String> keys = config.getKeys(null); |
sb.append("<z>" + (int)homeConfig.getDouble(player.getName() + ".z") + "</z>\n"); |
135 |
if (keys != null) { |
sb.append("</home>"); |
136 |
for (String key : keys) { |
|
|
if (server.getPlayer(key) != null) // player is online |
|
|
continue; |
|
|
|
|
|
String last = config.getString( key + ".logout"); |
|
|
sb.append("" + key + ": " + formatTime(last) + "\n") ; |
|
|
} |
|
|
} else { |
|
|
sb.append("no players found\n"); |
|
|
} |
|
|
} else { |
|
|
sb.append("could not find LastSeen/times.yml\n"); |
|
137 |
} |
} |
|
sb.append("\n"); |
|
138 |
|
|
139 |
|
|
140 |
|
sb.append("</player>\n"); |
141 |
|
} |
142 |
|
sb.append("</players>\n"); |
143 |
|
|
144 |
sb.append("--- Plugins ---\n"); |
sb.append("<plugins>\n"); |
145 |
for (Plugin plugin : server.getPluginManager().getPlugins()) { |
for (Plugin plugin : server.getPluginManager().getPlugins()) { |
146 |
sb.append( plugin.getDescription().getName() + " " + plugin.getDescription().getVersion() + "\n"); |
|
147 |
} |
sb.append("<plugin>\n"); |
148 |
|
sb.append("<name>" + plugin.getDescription().getName() + "</name>\n"); |
149 |
event.setResponse(sb.toString()); |
sb.append("<version>" + plugin.getDescription().getVersion() + "</version>\n"); |
150 |
event.setActionTaken(true); |
sb.append("<website>" + (plugin.getDescription().getWebsite() != null ? plugin.getDescription().getWebsite() : "") + "</website>\n"); |
151 |
|
sb.append("</plugin>\n"); |
152 |
} |
} |
153 |
|
sb.append("</plugins>\n"); |
154 |
|
|
155 |
|
sb.append("</serverinfo>"); |
156 |
|
|
157 |
|
return sb.toString(); |
158 |
|
|
159 |
} |
} |
160 |
|
|
161 |
} |
} |