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.text.SimpleDateFormat; |
import java.text.SimpleDateFormat; |
5 |
import java.util.Date; |
import java.util.Date; |
6 |
import java.util.Set; |
|
|
import java.util.Map; |
|
7 |
import org.bukkit.Location; |
import org.bukkit.Location; |
|
import org.bukkit.Server; |
|
8 |
import org.bukkit.OfflinePlayer; |
import org.bukkit.OfflinePlayer; |
9 |
|
import org.bukkit.Server; |
10 |
|
import org.bukkit.configuration.Configuration; |
11 |
import org.bukkit.entity.Player; |
import org.bukkit.entity.Player; |
12 |
import org.bukkit.plugin.Plugin; |
import org.bukkit.plugin.Plugin; |
13 |
|
|
14 |
import org.bukkit.configuration.Configuration; |
import com.sun.net.httpserver.HttpExchange; |
15 |
|
import com.sun.net.httpserver.HttpHandler; |
16 |
|
|
|
import org.kokakiwi.apicraft.events.ApiEvent; |
|
|
import org.kokakiwi.apicraft.events.ApiListener; |
|
17 |
|
|
18 |
public class StatusListener extends ApiListener { |
public class StatusListener implements HttpHandler{ |
19 |
|
|
20 |
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
21 |
|
|
22 |
Server server; |
Server server; |
23 |
Configuration loginConfig; |
Configuration loginConfig; |
24 |
|
Configuration homeConfig; |
25 |
|
|
26 |
|
long cachedTime = 0; |
27 |
|
String cachedXml = ""; |
28 |
|
|
29 |
|
public StatusListener(Plugin plugin, Server server, Configuration loginConfig, Configuration homeConfig) { |
30 |
|
|
|
public StatusListener(Server server, Configuration loginConfig) { |
|
31 |
this.server = server; |
this.server = server; |
32 |
this.loginConfig = loginConfig; |
this.loginConfig = loginConfig; |
33 |
|
this.homeConfig = homeConfig; |
34 |
} |
} |
35 |
|
|
36 |
String formatTime(String input) { |
String formatTime(String input) { |
42 |
} |
} |
43 |
|
|
44 |
@Override |
@Override |
45 |
public void onApiEvent(ApiEvent event) { |
public void handle(HttpExchange http) throws IOException { |
46 |
if(!event.path[0].equalsIgnoreCase("webstatus")) { |
/* if(!event.path[0].equalsIgnoreCase("webstatus")) { |
47 |
return; |
return; |
48 |
|
}*/ |
49 |
|
|
50 |
|
String xml; |
51 |
|
|
52 |
|
synchronized(this) { //is this necessary ? |
53 |
|
long now = System.currentTimeMillis(); |
54 |
|
if ( (now-cachedTime) > 5000) { |
55 |
|
xml = buildXML(); |
56 |
|
cachedXml = xml; |
57 |
|
cachedTime = now; |
58 |
|
} else { |
59 |
|
xml = cachedXml; |
60 |
|
} |
61 |
} |
} |
62 |
|
|
63 |
|
|
64 |
|
/* event.setResponse(xml); |
65 |
|
event.setActionTaken(true);*/ |
66 |
|
|
67 |
|
http.getResponseBody().write( xml.getBytes() ); |
68 |
|
} |
69 |
|
|
70 |
|
private String buildXML() { |
71 |
|
|
72 |
StringBuilder sb = new StringBuilder(); |
StringBuilder sb = new StringBuilder(); |
73 |
|
|
74 |
sb.append("<?xml version=\"1.0\"?> \n"); |
sb.append("<?xml version=\"1.0\"?> \n"); |
116 |
sb.append("<y>" + loc.getBlockY() + "</y>"); |
sb.append("<y>" + loc.getBlockY() + "</y>"); |
117 |
sb.append("<z>" + loc.getBlockZ() + "</z>"); |
sb.append("<z>" + loc.getBlockZ() + "</z>"); |
118 |
sb.append("<world>" + loc.getWorld().getName() + "</world>"); |
sb.append("<world>" + loc.getWorld().getName() + "</world>"); |
119 |
sb.append("</location>"); |
sb.append("</location>"); |
120 |
|
} |
121 |
|
|
122 |
|
String homeWorld = homeConfig.getString( player.getName() + ".world"); |
123 |
|
if (homeWorld != null) { |
124 |
|
sb.append("<home>"); |
125 |
|
sb.append("<world>" + homeWorld + "</world>\n"); |
126 |
|
sb.append("<x>" + (int)homeConfig.getDouble(player.getName() + ".x") + "</x>\n"); |
127 |
|
sb.append("<y>" + (int)homeConfig.getDouble(player.getName() + ".y") + "</y>\n"); |
128 |
|
sb.append("<z>" + (int)homeConfig.getDouble(player.getName() + ".z") + "</z>\n"); |
129 |
|
sb.append("</home>"); |
130 |
|
|
131 |
} |
} |
132 |
|
|
133 |
|
|
147 |
sb.append("</plugins>\n"); |
sb.append("</plugins>\n"); |
148 |
|
|
149 |
sb.append("</serverinfo>"); |
sb.append("</serverinfo>"); |
150 |
event.setResponse(sb.toString()); |
|
151 |
event.setActionTaken(true); |
return sb.toString(); |
152 |
|
|
153 |
} |
} |
154 |
|
|