5 |
import java.util.Date; |
import java.util.Date; |
6 |
import java.util.Set; |
import java.util.Set; |
7 |
import java.util.Map; |
import java.util.Map; |
8 |
|
import org.bukkit.Location; |
9 |
import org.bukkit.Server; |
import org.bukkit.Server; |
10 |
import org.bukkit.OfflinePlayer; |
import org.bukkit.OfflinePlayer; |
11 |
import org.bukkit.entity.Player; |
import org.bukkit.entity.Player; |
13 |
|
|
14 |
import org.bukkit.configuration.Configuration; |
import org.bukkit.configuration.Configuration; |
15 |
|
|
16 |
import org.kokakiwi.apicraft.events.ApiEvent; |
/*import org.kokakiwi.apicraft.events.ApiEvent; |
17 |
import org.kokakiwi.apicraft.events.ApiListener; |
import org.kokakiwi.apicraft.events.ApiListener;*/ |
18 |
|
import de.codeinfection.quickwango.ApiBukkit.ApiBukkit; |
19 |
|
import de.codeinfection.quickwango.ApiBukkit.ApiRequestAction; |
20 |
|
import de.codeinfection.quickwango.ApiBukkit.ApiRequestException; |
21 |
|
import de.codeinfection.quickwango.ApiBukkit.Net.Parameters; |
22 |
|
import de.codeinfection.quickwango.ApiBukkit.ApiRequestController; |
23 |
|
|
24 |
public class StatusListener extends ApiListener { |
public class StatusListener extends ApiRequestController { |
25 |
|
|
26 |
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
27 |
|
|
28 |
Server server; |
Server server; |
29 |
Configuration loginConfig; |
Configuration loginConfig; |
30 |
|
Configuration homeConfig; |
31 |
|
|
32 |
|
long cachedTime = 0; |
33 |
|
String cachedXml = ""; |
34 |
|
|
35 |
|
public StatusListener(Plugin plugin, Server server, Configuration loginConfig, Configuration homeConfig) { |
36 |
|
super(plugin,true); |
37 |
|
|
|
public StatusListener(Server server, Configuration loginConfig) { |
|
38 |
this.server = server; |
this.server = server; |
39 |
this.loginConfig = loginConfig; |
this.loginConfig = loginConfig; |
40 |
|
this.homeConfig = homeConfig; |
41 |
} |
} |
42 |
|
|
43 |
String formatTime(String input) { |
String formatTime(String input) { |
49 |
} |
} |
50 |
|
|
51 |
@Override |
@Override |
52 |
public void onApiEvent(ApiEvent event) { |
public Object defaultAction(String action, Parameters params, Server server) throws ApiRequestException { |
53 |
if(!event.path[0].equalsIgnoreCase("webstatus")) { |
/* if(!event.path[0].equalsIgnoreCase("webstatus")) { |
54 |
return; |
return; |
55 |
|
}*/ |
56 |
|
|
57 |
|
String xml; |
58 |
|
|
59 |
|
synchronized(this) { //is this necessary ? |
60 |
|
long now = System.currentTimeMillis(); |
61 |
|
if ( (now-cachedTime) > 5000) { |
62 |
|
xml = buildXML(); |
63 |
|
cachedXml = xml; |
64 |
|
cachedTime = now; |
65 |
|
} else { |
66 |
|
xml = cachedXml; |
67 |
|
} |
68 |
} |
} |
69 |
|
|
70 |
|
|
71 |
|
/* event.setResponse(xml); |
72 |
|
event.setActionTaken(true);*/ |
73 |
|
|
74 |
|
return xml; |
75 |
|
} |
76 |
|
|
77 |
|
private String buildXML() { |
78 |
|
|
79 |
StringBuilder sb = new StringBuilder(); |
StringBuilder sb = new StringBuilder(); |
80 |
|
|
81 |
sb.append("<?xml version=\"1.0\"?> \n"); |
sb.append("<?xml version=\"1.0\"?> \n"); |
100 |
long first = player.getFirstPlayed(); |
long first = player.getFirstPlayed(); |
101 |
long last = player.getLastPlayed(); |
long last = player.getLastPlayed(); |
102 |
|
|
|
long login = loginConfig.getLong( player.getName() + ".logintime", -1); |
|
|
long elapsed = (login>-1 ? (now-login) / 1000 : -1); |
|
103 |
|
|
104 |
sb.append("<player>\n"); |
sb.append("<player>\n"); |
105 |
sb.append("<name>" + player.getName() + "</name>\n" ); |
sb.append("<name>" + player.getName() + "</name>\n" ); |
|
sb.append("<online>" + (player.isOnline() ? 1 : 0 ) + "</online>\n" ); |
|
|
sb.append("<sessionlength>" + elapsed + "</sessionlength>\n" ); |
|
106 |
|
|
107 |
sb.append("<firstseen>" + formatTime(first) + "</firstseen>\n"); |
sb.append("<firstseen>" + formatTime(first) + "</firstseen>\n"); |
108 |
sb.append("<lastseen>" + formatTime(last) + "</lastseen>\n"); |
sb.append("<lastseen>" + formatTime(last) + "</lastseen>\n"); |
109 |
|
|
110 |
|
sb.append("<online>" + (player.isOnline() ? 1 : 0 ) + "</online>\n" ); |
111 |
|
|
112 |
|
if (player.isOnline() ) { |
113 |
|
long login = loginConfig.getLong( player.getName() + ".logintime", -1); |
114 |
|
long elapsed = (login>-1 ? (now-login) / 1000 : -1); |
115 |
|
|
116 |
|
Player p2 = server.getPlayer( player.getName() ); |
117 |
|
Location loc = p2.getLocation(); |
118 |
|
|
119 |
|
sb.append("<sessionlength>" + elapsed + "</sessionlength>\n" ); |
120 |
|
sb.append("<ip>" + p2.getAddress().getAddress().toString().replace("/","") + "</ip>\n" ); |
121 |
|
sb.append("<location>"); |
122 |
|
sb.append("<x>" + loc.getBlockX() + "</x>"); |
123 |
|
sb.append("<y>" + loc.getBlockY() + "</y>"); |
124 |
|
sb.append("<z>" + loc.getBlockZ() + "</z>"); |
125 |
|
sb.append("<world>" + loc.getWorld().getName() + "</world>"); |
126 |
|
sb.append("</location>"); |
127 |
|
} |
128 |
|
|
129 |
|
String homeWorld = homeConfig.getString( player.getName() + ".world"); |
130 |
|
if (homeWorld != null) { |
131 |
|
sb.append("<home>"); |
132 |
|
sb.append("<world>" + homeWorld + "</world>\n"); |
133 |
|
sb.append("<x>" + (int)homeConfig.getDouble(player.getName() + ".x") + "</x>\n"); |
134 |
|
sb.append("<y>" + (int)homeConfig.getDouble(player.getName() + ".y") + "</y>\n"); |
135 |
|
sb.append("<z>" + (int)homeConfig.getDouble(player.getName() + ".z") + "</z>\n"); |
136 |
|
sb.append("</home>"); |
137 |
|
|
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
sb.append("</player>\n"); |
sb.append("</player>\n"); |
142 |
} |
} |
143 |
sb.append("</players>\n"); |
sb.append("</players>\n"); |
144 |
|
|
145 |
sb.append("<plugins>\n"); |
sb.append("<plugins>\n"); |
146 |
for (Plugin plugin : server.getPluginManager().getPlugins()) { |
for (Plugin plugin : server.getPluginManager().getPlugins()) { |
147 |
|
|
148 |
sb.append("<plugin>\n"); |
sb.append("<plugin>\n"); |
149 |
sb.append("<name>" + plugin.getDescription().getName() + "</name>\n"); |
sb.append("<name>" + plugin.getDescription().getName() + "</name>\n"); |
150 |
sb.append("<version>" + plugin.getDescription().getVersion() + "</version>\n"); |
sb.append("<version>" + plugin.getDescription().getVersion() + "</version>\n"); |
151 |
|
sb.append("<website>" + (plugin.getDescription().getWebsite() != null ? plugin.getDescription().getWebsite() : "") + "</website>\n"); |
152 |
sb.append("</plugin>\n"); |
sb.append("</plugin>\n"); |
153 |
} |
} |
154 |
sb.append("</plugins>\n"); |
sb.append("</plugins>\n"); |
155 |
|
|
156 |
sb.append("</serverinfo>"); |
sb.append("</serverinfo>"); |
157 |
event.setResponse(sb.toString()); |
|
158 |
event.setActionTaken(true); |
return sb.toString(); |
159 |
|
|
160 |
} |
} |
161 |
|
|