136 lines
3.6 KiB
Java
136 lines
3.6 KiB
Java
package fi.jonttu.woovelocity.util;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.InputStreamReader;
|
|
import java.net.URL;
|
|
import java.util.Map;
|
|
|
|
import com.google.gson.JsonArray;
|
|
import com.google.gson.JsonElement;
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonParser;
|
|
|
|
import fi.jonttu.woovelocity.WooVelocity;
|
|
|
|
public final class Connector {
|
|
|
|
public static boolean update() {
|
|
try {
|
|
final BufferedReader bReader = new BufferedReader(
|
|
new InputStreamReader(
|
|
new URL(
|
|
WooVelocity.getInstance().getConfig().getString("config.url")
|
|
+ "?wmc_key="
|
|
+ WooVelocity.getInstance().getConfig().getString("config.key")
|
|
).openConnection().getInputStream()
|
|
)
|
|
);
|
|
|
|
String data = "";
|
|
String line = null;
|
|
while ((line = bReader.readLine()) != null) {
|
|
if (data.equals("")) {
|
|
data = line;
|
|
} else {
|
|
data = data + "\n" + line;
|
|
}
|
|
}
|
|
|
|
final JsonObject o = JsonParser.parseString(data).getAsJsonObject();
|
|
if (! o.get("success").getAsBoolean()) {
|
|
return false;
|
|
}
|
|
final JsonArray processedData = new JsonArray();
|
|
if (! (o.get("data") instanceof JsonObject)) {
|
|
return true;
|
|
}
|
|
|
|
for (final Map.Entry<String, JsonElement> entry : o.get("data").getAsJsonObject().entrySet()) {
|
|
WooVelocity.getInstance().getLogger().info("Got new unprocessed orders");
|
|
final String username = entry.getKey();
|
|
String orderId = null;
|
|
if (allowUserProcessing(username)) {
|
|
|
|
for (final Map.Entry<String, JsonElement> e : o.get("data").getAsJsonObject().get(username).getAsJsonObject().entrySet()) {
|
|
orderId = e.getKey();
|
|
}
|
|
|
|
processedData.add(Integer.parseInt(orderId));
|
|
|
|
for (int i = 0; i < o.get("data").getAsJsonObject().get(orderId).getAsJsonArray().size(); i++) {
|
|
String command = o.get("data").getAsJsonObject().get(username).getAsJsonObject().get(orderId).getAsJsonArray().get(i).getAsString();
|
|
|
|
// This fixes command formats that come from wordpress
|
|
command = command.replaceAll(""", "\"");
|
|
|
|
WooVelocity.getInstance().getServer().getCommandManager().executeAsync(
|
|
WooVelocity.getInstance().getServer().getConsoleCommandSource(),
|
|
command
|
|
);
|
|
|
|
WooVelocity.getInstance().getLogger().info("Executed command '" + command + "'");
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
if (1 > processedData.size()) {
|
|
return false;
|
|
}
|
|
|
|
if (! sendProcessedOrders(processedData.toString())) {
|
|
return false;
|
|
}
|
|
|
|
bReader.close();
|
|
|
|
return true;
|
|
} catch (final Exception e) {
|
|
e.printStackTrace();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Check if processing server update and sending command is allowed at a moment
|
|
private static boolean allowUserProcessing(final String name) {
|
|
if (WooVelocity.getInstance().getConfig().getBoolean("config.progressoffline")) {
|
|
return true;
|
|
}
|
|
if (WooVelocity.getInstance().getServer().getPlayer(name).isPresent()) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private static boolean sendProcessedOrders(final String json) {
|
|
String data = "";
|
|
|
|
try {
|
|
final BufferedReader bReader = new BufferedReader(
|
|
new InputStreamReader(
|
|
new URL(
|
|
WooVelocity.getInstance().getConfig().getString("config.url")
|
|
+ "&processedOrders="
|
|
+ json
|
|
).openConnection().getInputStream()
|
|
)
|
|
);
|
|
String line = null;
|
|
while ((line = bReader.readLine()) != null) {
|
|
if (data.equals("")) {
|
|
data = line;
|
|
} else {
|
|
data = data + "\n" + line;
|
|
}
|
|
}
|
|
bReader.close();
|
|
} catch (final Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
final JsonObject o = JsonParser.parseString(data).getAsJsonObject();
|
|
return o.get("success").getAsBoolean();
|
|
}
|
|
|
|
}
|