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.Set; |
|
|
import java.util.Map; |
|
9 |
import org.bukkit.Location; |
import org.bukkit.Location; |
|
import org.bukkit.Server; |
|
10 |
import org.bukkit.OfflinePlayer; |
import org.bukkit.OfflinePlayer; |
11 |
|
import org.bukkit.Server; |
12 |
|
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; |
15 |
|
|
16 |
import org.bukkit.configuration.Configuration; |
import com.sun.net.httpserver.HttpExchange; |
17 |
|
import com.sun.net.httpserver.HttpHandler; |
18 |
|
|
|
import org.kokakiwi.apicraft.events.ApiEvent; |
|
|
import org.kokakiwi.apicraft.events.ApiListener; |
|
19 |
|
|
20 |
public class StatusListener extends ApiListener { |
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 |
Configuration loginConfig; |
Configuration loginConfig; |
26 |
|
Configuration homeConfig; |
27 |
|
|
28 |
|
long cachedTime = 0; |
29 |
|
String cachedXml = ""; |
30 |
|
|
31 |
|
public StatusListener(Plugin plugin, Server server, Configuration loginConfig, Configuration homeConfig) { |
32 |
|
|
|
public StatusListener(Server server, Configuration loginConfig) { |
|
33 |
this.server = server; |
this.server = server; |
34 |
this.loginConfig = loginConfig; |
this.loginConfig = loginConfig; |
35 |
|
this.homeConfig = homeConfig; |
36 |
} |
} |
37 |
|
|
38 |
String formatTime(String input) { |
String formatTime(String input) { |
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 |
return; |
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 |
|
|
65 |
|
|
66 |
|
/* event.setResponse(xml); |
67 |
|
event.setActionTaken(true);*/ |
68 |
|
byte bytes[] = xml.getBytes(); |
69 |
|
http.sendResponseHeaders(HttpURLConnection.HTTP_OK, bytes.length ); |
70 |
|
final OutputStream os = http.getResponseBody(); |
71 |
|
os.write(bytes); |
72 |
|
os.close(); |
73 |
|
http.close(); |
74 |
|
} |
75 |
|
|
76 |
|
private String buildXML() { |
77 |
|
|
78 |
StringBuilder sb = new StringBuilder(); |
StringBuilder sb = new StringBuilder(); |
79 |
|
|
80 |
sb.append("<?xml version=\"1.0\"?> \n"); |
sb.append("<?xml version=\"1.0\"?> \n"); |
122 |
sb.append("<y>" + loc.getBlockY() + "</y>"); |
sb.append("<y>" + loc.getBlockY() + "</y>"); |
123 |
sb.append("<z>" + loc.getBlockZ() + "</z>"); |
sb.append("<z>" + loc.getBlockZ() + "</z>"); |
124 |
sb.append("<world>" + loc.getWorld().getName() + "</world>"); |
sb.append("<world>" + loc.getWorld().getName() + "</world>"); |
125 |
sb.append("</location>"); |
sb.append("</location>"); |
126 |
|
} |
127 |
|
|
128 |
|
String homeWorld = homeConfig.getString( player.getName() + ".world"); |
129 |
|
if (homeWorld != null) { |
130 |
|
sb.append("<home>"); |
131 |
|
sb.append("<world>" + homeWorld + "</world>\n"); |
132 |
|
sb.append("<x>" + (int)homeConfig.getDouble(player.getName() + ".x") + "</x>\n"); |
133 |
|
sb.append("<y>" + (int)homeConfig.getDouble(player.getName() + ".y") + "</y>\n"); |
134 |
|
sb.append("<z>" + (int)homeConfig.getDouble(player.getName() + ".z") + "</z>\n"); |
135 |
|
sb.append("</home>"); |
136 |
|
|
137 |
} |
} |
138 |
|
|
139 |
|
|
153 |
sb.append("</plugins>\n"); |
sb.append("</plugins>\n"); |
154 |
|
|
155 |
sb.append("</serverinfo>"); |
sb.append("</serverinfo>"); |
156 |
event.setResponse(sb.toString()); |
|
157 |
event.setActionTaken(true); |
return sb.toString(); |
158 |
|
|
159 |
} |
} |
160 |
|
|