199 lines
5.0 KiB
Java
199 lines
5.0 KiB
Java
package woobungee;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.File;
|
|
import java.io.InputStreamReader;
|
|
import java.net.URL;
|
|
import java.util.Map;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import com.google.gson.JsonArray;
|
|
import com.google.gson.JsonElement;
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonParser;
|
|
|
|
import net.md_5.bungee.api.ProxyServer;
|
|
import net.md_5.bungee.api.chat.TextComponent;
|
|
import net.md_5.bungee.api.plugin.Plugin;
|
|
import net.md_5.bungee.config.Configuration;
|
|
import net.md_5.bungee.config.ConfigurationProvider;
|
|
import net.md_5.bungee.config.YamlConfiguration;
|
|
|
|
public class main extends Plugin{
|
|
|
|
public static Configuration config;
|
|
|
|
@Override
|
|
public void onEnable() {
|
|
|
|
saveDefaultConfig();
|
|
|
|
try {
|
|
config = ConfigurationProvider.getProvider(YamlConfiguration.class)
|
|
.load(new File("plugins/woobungee/config.yml"));
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
getProxy().getPluginManager().registerCommand(this, new WooCommand());
|
|
|
|
if(!update()) {
|
|
ProxyServer.getInstance().getConsole().sendMessage(new TextComponent("WooBungee update failed, check your key!"));
|
|
}
|
|
|
|
scheduleUpdate();
|
|
|
|
}
|
|
|
|
private static boolean sendUpdate(String jsonString)
|
|
{
|
|
String data = "";
|
|
|
|
try {
|
|
|
|
BufferedReader bReader = new BufferedReader(
|
|
new InputStreamReader(
|
|
new URL(config.getString("url") + "?wmc_key=" + config.getString("key") + "&processedOrders=" + jsonString)
|
|
.openConnection().getInputStream()));
|
|
|
|
String line = null;
|
|
while((line = bReader.readLine()) != null) {
|
|
if(data.equals("")) {
|
|
data = line;
|
|
} else {
|
|
data = data + "\n" + line;
|
|
}
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
JsonObject o = new JsonParser().parse(data).getAsJsonObject();
|
|
|
|
return o.get("success").getAsBoolean();
|
|
|
|
}
|
|
|
|
public static boolean update() {
|
|
|
|
try {
|
|
|
|
BufferedReader bReader = new BufferedReader(
|
|
new InputStreamReader(
|
|
new URL(config.getString("url") + "?wmc_key=" + config.getString("key"))
|
|
.openConnection().getInputStream()));
|
|
|
|
String data = "";
|
|
String line = null;
|
|
while((line = bReader.readLine()) != null) {
|
|
if(data.equals("")) {
|
|
data = line;
|
|
} else {
|
|
data = data + "\n" + line;
|
|
}
|
|
}
|
|
|
|
JsonObject o = new JsonParser().parse(data).getAsJsonObject();
|
|
|
|
if(!o.get("success").getAsBoolean()) {
|
|
return false;
|
|
}
|
|
|
|
JsonArray processedData = new JsonArray();
|
|
|
|
if(!(o.get("data") instanceof JsonObject)) {
|
|
return true;
|
|
}
|
|
|
|
for(Map.Entry<String, JsonElement> entry : o.get("data").getAsJsonObject().entrySet()) {
|
|
|
|
ProxyServer.getInstance().getConsole().sendMessage(new TextComponent("WooBungee new product, executing commands!"));
|
|
|
|
String username = entry.getKey();
|
|
String orderId = null;
|
|
|
|
if(processUpdate(username)) {
|
|
|
|
for(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(username).getAsJsonObject().get(orderId).getAsJsonArray().size(); i++) {
|
|
|
|
String command = o.get("data").getAsJsonObject().get(username).getAsJsonObject().get(orderId).getAsJsonArray().get(i).getAsString();
|
|
command = command.replaceAll(""", "\"");
|
|
|
|
ProxyServer.getInstance().getConsole().sendMessage(new TextComponent("WooBungee executed command: " + command));
|
|
|
|
ProxyServer.getInstance().getPluginManager().dispatchCommand(ProxyServer.getInstance().getConsole(), command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( 1 > processedData.size()) {
|
|
return false;
|
|
}
|
|
|
|
if(!sendUpdate(processedData.toString())) {
|
|
return false;
|
|
}
|
|
|
|
bReader.close();
|
|
|
|
return true;
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private static boolean processUpdate(String name) {
|
|
if(config.getBoolean("progressoffline")) {
|
|
return true;
|
|
}
|
|
if(ProxyServer.getInstance().getPlayer(name) != null && ProxyServer.getInstance().getPlayer(name).isConnected()) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public void scheduleUpdate() {
|
|
ProxyServer.getInstance().getScheduler().schedule(this, new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
update();
|
|
scheduleUpdate();
|
|
}
|
|
}, config.getInt("update_interval"), TimeUnit.SECONDS);
|
|
}
|
|
|
|
private void saveDefaultConfig() {
|
|
new File("plugins/woobungee/").mkdirs();
|
|
File file = new File("plugins/woobungee/config.yml");
|
|
if(!file.exists()) {
|
|
Configuration config = new Configuration();
|
|
config.set("update_interval", 1500);
|
|
config.set("url", "http://playground.dev");
|
|
config.set("key", "");
|
|
config.set("progressoffline", false);
|
|
|
|
try {
|
|
ConfigurationProvider.getProvider(YamlConfiguration.class)
|
|
.save(config, new File("plugins/woobungee/config.yml"));
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|