From 0551337e8b543a7dfb6a650b4d8bf300ddb074d8 Mon Sep 17 00:00:00 2001 From: Lemon Date: Thu, 22 Feb 2018 17:55:04 +0500 Subject: [PATCH] Implement #22 and fix pom.xml. Signed-off-by: Lemon --- pom.xml | 1 + .../totalfreedommod/TotalFreedomMod.java | 3 + .../totalfreedom/totalfreedommod/amp/AMP.java | 47 +++++ .../totalfreedommod/amp/AMPEndpoints.java | 27 +++ .../totalfreedommod/amp/AMPManager.java | 168 ++++++++++++++++++ .../totalfreedommod/amp/LoginCallback.java | 6 + .../totalfreedommod/amp/LoginResult.java | 50 ++++++ .../bridge/CoreProtectBridge.java | 28 +-- .../command/Command_restart.java | 35 ++++ .../command/Command_wipeflatlands.java | 9 +- .../totalfreedommod/config/ConfigEntry.java | 10 +- src/main/resources/config.yml | 7 + 12 files changed, 376 insertions(+), 15 deletions(-) create mode 100644 src/main/java/me/totalfreedom/totalfreedommod/amp/AMP.java create mode 100644 src/main/java/me/totalfreedom/totalfreedommod/amp/AMPEndpoints.java create mode 100644 src/main/java/me/totalfreedom/totalfreedommod/amp/AMPManager.java create mode 100644 src/main/java/me/totalfreedom/totalfreedommod/amp/LoginCallback.java create mode 100644 src/main/java/me/totalfreedom/totalfreedommod/amp/LoginResult.java create mode 100644 src/main/java/me/totalfreedom/totalfreedommod/command/Command_restart.java diff --git a/pom.xml b/pom.xml index f83f847d..bd9b4a82 100644 --- a/pom.xml +++ b/pom.xml @@ -144,6 +144,7 @@ me.libraryaddict LibsDisguises system + 9.4.0-SNAPSHOT ${project.basedir}/lib/LibsDisguises.jar diff --git a/src/main/java/me/totalfreedom/totalfreedommod/TotalFreedomMod.java b/src/main/java/me/totalfreedom/totalfreedommod/TotalFreedomMod.java index d0a89d9f..d9fdc149 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/TotalFreedomMod.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/TotalFreedomMod.java @@ -1,6 +1,7 @@ package me.totalfreedom.totalfreedommod; import me.totalfreedom.totalfreedommod.admin.AdminList; +import me.totalfreedom.totalfreedommod.amp.AMP; import me.totalfreedom.totalfreedommod.banning.BanManager; import me.totalfreedom.totalfreedommod.banning.PermbanList; import me.totalfreedom.totalfreedommod.blocking.*; @@ -103,6 +104,7 @@ public class TotalFreedomMod extends AeroPlugin public CoreProtectBridge cpb; public WorldEditBridge web; public WorldGuardBridge wgb; + public AMP amp; @Override public void load() @@ -213,6 +215,7 @@ public class TotalFreedomMod extends AeroPlugin ldb = bridges.registerService(LibsDisguisesBridge.class); web = bridges.registerService(WorldEditBridge.class); wgb = bridges.registerService(WorldGuardBridge.class); + amp = bridges.registerService(AMP.class); bridges.start(); timer.update(); diff --git a/src/main/java/me/totalfreedom/totalfreedommod/amp/AMP.java b/src/main/java/me/totalfreedom/totalfreedommod/amp/AMP.java new file mode 100644 index 00000000..e4ad4761 --- /dev/null +++ b/src/main/java/me/totalfreedom/totalfreedommod/amp/AMP.java @@ -0,0 +1,47 @@ +package me.totalfreedom.totalfreedommod.amp; + +import me.totalfreedom.totalfreedommod.FreedomService; +import me.totalfreedom.totalfreedommod.TotalFreedomMod; +import me.totalfreedom.totalfreedommod.config.ConfigEntry; + + +public class AMP extends FreedomService +{ + public AMPManager ampManager; + public Boolean enabled; + + public AMP(TotalFreedomMod plugin) + { + super(plugin); + } + + @Override + protected void onStart() + { + if(!plugin.config.getBoolean(ConfigEntry.AMP_ENABLED)) + { + return; + } + ampManager = new AMPManager(plugin, plugin.config.getString(ConfigEntry.AMP_URL), plugin.config.getString(ConfigEntry.AMP_USERNAME), plugin.config.getString(ConfigEntry.AMP_PASSWORD)); + LoginCallback callback = new LoginCallback() + { + @Override + public void loginDone(boolean success) + { + enabled = success; + } + }; + ampManager.connectAsync(callback); + + } + + public void restartServer() + { + ampManager.restartAsync(); + } + + @Override + protected void onStop() { + + } +} diff --git a/src/main/java/me/totalfreedom/totalfreedommod/amp/AMPEndpoints.java b/src/main/java/me/totalfreedom/totalfreedommod/amp/AMPEndpoints.java new file mode 100644 index 00000000..8e75dd42 --- /dev/null +++ b/src/main/java/me/totalfreedom/totalfreedommod/amp/AMPEndpoints.java @@ -0,0 +1,27 @@ +package me.totalfreedom.totalfreedommod.amp; + + +public enum AMPEndpoints +{ + LOGIN("/API/Core/Login" , "{username:\"%s\", password:\"%s\", token:\"\", rememberMe:false}"), + RESTART("/API/Core/Restart", "{SESSIONID:\"%s\"}"); + + private final String text; + private final String parameters; + + AMPEndpoints(String text, String parameters) + { + this.text = text; + this.parameters = parameters; + } + + @Override + public String toString() + { + return text; + } + public String getParameters() + { + return parameters; + } +} diff --git a/src/main/java/me/totalfreedom/totalfreedommod/amp/AMPManager.java b/src/main/java/me/totalfreedom/totalfreedommod/amp/AMPManager.java new file mode 100644 index 00000000..715bee87 --- /dev/null +++ b/src/main/java/me/totalfreedom/totalfreedommod/amp/AMPManager.java @@ -0,0 +1,168 @@ +package me.totalfreedom.totalfreedommod.amp; + +import com.google.gson.Gson; +import me.totalfreedom.totalfreedommod.TotalFreedomMod; +import me.totalfreedom.totalfreedommod.util.FLog; +import org.bukkit.scheduler.BukkitRunnable; + +import javax.net.ssl.HttpsURLConnection; +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +public class AMPManager +{ + private TotalFreedomMod plugin; + private String url, username, password; + private String sessionID; + + + public AMPManager(TotalFreedomMod plugin, String url, String username, String password) + { + this.plugin = plugin; this.url = url; this.username = username; this.password = password; + } + + public void connectAsync(final LoginCallback callback) + { + + new BukkitRunnable() + { + @Override + public void run() + { + String apiEndpoint = url + AMPEndpoints.LOGIN.toString(); + String body = String.format(AMPEndpoints.LOGIN.getParameters(), username, password); + try + { + LoginResult resp = new Gson().fromJson(postRequestToEndpoint(apiEndpoint, body), LoginResult.class); + if(!resp.getSuccess()) + { + FLog.severe("AMP login unsuccessful. Check if login details are correct."); + sessionID = ""; + callback.loginDone(false); + return; + } + sessionID = resp.getSessionID(); + callback.loginDone(true); + } + catch(IOException ex) + { + FLog.severe("Could not login to AMP. Check if URL is correct. Stacktrace: " + ex.getMessage()); + sessionID = ""; + callback.loginDone(false); + + } + } + }.runTaskAsynchronously(plugin); + } + + public void restartAsync() + { + new BukkitRunnable() + { + @Override + public void run() + { + final String apiEndpoint = url + AMPEndpoints.RESTART.toString(); + final String body = String.format(AMPEndpoints.RESTART.getParameters(), sessionID); + try + { + String resp = postRequestToEndpoint(apiEndpoint, body); + if(resp.contains("Unauthorized Access")) + { + //try connecting one more time + LoginCallback callback = new LoginCallback() + { + @Override + public void loginDone(boolean success) + { + if(!success) + { + FLog.severe("Failed to connect to AMP. Did the panel go down? Were panel user details changed/deleted? Check for more info above. Connection was successful when plugin started, but unsuccessful now." + + " Using server.shutdown() instead."); + plugin.getServer().shutdown(); + return; + } + try + { + String response = postRequestToEndpoint(apiEndpoint, body); + if(response.contains("Unauthorized Access")) + { + FLog.severe("Contact a developer. Panel gives Session ID but trying to use it gives a no perms error. The panel user set in config doesn't" + + " have perms to restart server. Using server.shutdown() instead. "); + plugin.getServer().shutdown(); + + } + } + catch (IOException e) + { + FLog.severe("Could not restart. Using server.shutdown() instead. Stacktrace" + e.getMessage()); + plugin.getServer().shutdown(); + } + } + }; + plugin.amp.ampManager.connectAsync(callback); + } + } + catch(IOException ex) + { + FLog.severe("Could not restart. Using server.shutdown() instead. Stacktrace: " + ex.getMessage()); + plugin.getServer().shutdown(); + } + } + }.runTaskAsynchronously(plugin); + } + + + private String postRequestToEndpoint(String endpoint, String body) throws IOException + { + URL url = new URL(endpoint); + if(endpoint.startsWith("https://")) + { + HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Accept", "application/json"); + connection.setDoOutput(true); + DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); + outputStream.writeBytes(body); + outputStream.flush(); + outputStream.close(); + BufferedReader in = new BufferedReader( + new InputStreamReader(connection.getInputStream())); + String inputLine; + StringBuffer response = new StringBuffer(); + + while ((inputLine = in.readLine()) != null) + { + response.append(inputLine); + } + in.close(); + return response.toString(); + } + else + { + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Accept", "application/json"); + connection.setDoOutput(true); + DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); + outputStream.writeBytes(body); + outputStream.flush(); + outputStream.close(); + BufferedReader in = new BufferedReader( + new InputStreamReader(connection.getInputStream())); + String inputLine; + StringBuffer response = new StringBuffer(); + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + return response.toString(); + } + } + +} \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/totalfreedommod/amp/LoginCallback.java b/src/main/java/me/totalfreedom/totalfreedommod/amp/LoginCallback.java new file mode 100644 index 00000000..29f66611 --- /dev/null +++ b/src/main/java/me/totalfreedom/totalfreedommod/amp/LoginCallback.java @@ -0,0 +1,6 @@ +package me.totalfreedom.totalfreedommod.amp; + +public interface LoginCallback +{ + void loginDone(boolean success); +} diff --git a/src/main/java/me/totalfreedom/totalfreedommod/amp/LoginResult.java b/src/main/java/me/totalfreedom/totalfreedommod/amp/LoginResult.java new file mode 100644 index 00000000..6a8f8a8e --- /dev/null +++ b/src/main/java/me/totalfreedom/totalfreedommod/amp/LoginResult.java @@ -0,0 +1,50 @@ +package me.totalfreedom.totalfreedommod.amp; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +public class LoginResult +{ + + @SerializedName("result") + @Expose + @Getter + @Setter + private Integer result; + @SerializedName("success") + @Expose + @Getter + @Setter + private Boolean success; + @SerializedName("permissions") + @Expose + @Getter + @Setter + private List permissions = null; + @SerializedName("sessionID") + @Expose + @Getter + @Setter + private String sessionID; + @SerializedName("rememberMeToken") + @Expose + @Getter + @Setter + private String rememberMeToken; + @SerializedName("gravatarHash") + @Expose + @Getter + @Setter + private String gravatarHash; + @SerializedName("username") + @Expose + @Getter + @Setter + private String username; + private final static long serialVersionUID = -523050232433919883L; + +} diff --git a/src/main/java/me/totalfreedom/totalfreedommod/bridge/CoreProtectBridge.java b/src/main/java/me/totalfreedom/totalfreedommod/bridge/CoreProtectBridge.java index 83719f5a..53694f74 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/bridge/CoreProtectBridge.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/bridge/CoreProtectBridge.java @@ -2,26 +2,23 @@ package me.totalfreedom.totalfreedommod.bridge; import me.totalfreedom.totalfreedommod.FreedomService; import me.totalfreedom.totalfreedommod.TotalFreedomMod; -import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.config.ConfigEntry; +import me.totalfreedom.totalfreedommod.util.FLog; +import me.totalfreedom.totalfreedommod.util.FUtil; import net.coreprotect.CoreProtect; import net.coreprotect.CoreProtectAPI; import org.bukkit.Bukkit; import org.bukkit.ChatColor; -import org.bukkit.plugin.Plugin; import org.bukkit.World; -import org.bukkit.scheduler.BukkitTask; -import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.sql.Statement; -import java.sql.ResultSet; +import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.scheduler.BukkitTask; + import java.io.File; -import java.util.List; +import java.sql.*; import java.util.Arrays; -import me.totalfreedom.totalfreedommod.util.FUtil; +import java.util.List; public class CoreProtectBridge extends FreedomService { @@ -253,7 +250,14 @@ public class CoreProtectBridge extends FreedomService // This exits for flatlands wipes if (shutdown) { - server.shutdown(); + if(plugin.amp.enabled) + { + plugin.amp.restartServer(); + } + else + { + server.shutdown(); + } } } } \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_restart.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_restart.java new file mode 100644 index 00000000..87b03b2d --- /dev/null +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_restart.java @@ -0,0 +1,35 @@ +package me.totalfreedom.totalfreedommod.command; + +import me.totalfreedom.totalfreedommod.rank.Rank; +import me.totalfreedom.totalfreedommod.util.FUtil; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +@CommandPermissions(level = Rank.SUPER_ADMIN, source = SourceType.BOTH) +@CommandParameters(description = "Kicks everyone and restarts the server.", usage = "/") +public class Command_restart extends FreedomCommand +{ + + @Override + public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) + { + if(!plugin.amp.enabled) + { + msg(ChatColor.RED + "AMP integration is not enabled. Please use /stop instead."); + return true; + } + + FUtil.bcastMsg("Server is going offline!", ChatColor.LIGHT_PURPLE); + + for (Player player : server.getOnlinePlayers()) + { + player.kickPlayer("Server is going offline, come back in about 20 seconds."); + } + + plugin.amp.restartServer(); + + return true; + } +} \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_wipeflatlands.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_wipeflatlands.java index e09be93e..20760b0b 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_wipeflatlands.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_wipeflatlands.java @@ -27,7 +27,14 @@ public class Command_wipeflatlands extends FreedomCommand if (!plugin.cpb.isEnabled()) { - server.shutdown(); + if(!plugin.amp.enabled) + { + server.shutdown(); + } + else + { + plugin.amp.restartServer(); + } } else { diff --git a/src/main/java/me/totalfreedom/totalfreedommod/config/ConfigEntry.java b/src/main/java/me/totalfreedom/totalfreedommod/config/ConfigEntry.java index b7641d5b..edcc15cc 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/config/ConfigEntry.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/config/ConfigEntry.java @@ -1,8 +1,9 @@ package me.totalfreedom.totalfreedommod.config; -import java.util.List; import me.totalfreedom.totalfreedommod.TotalFreedomMod; +import java.util.List; + public enum ConfigEntry { @@ -95,7 +96,12 @@ public enum ConfigEntry OVERLORD_IPS(List.class, "overlord_ips"), NOADMIN_IPS(List.class, "noadmin_ips"), ADMIN_ONLY_MODE(Boolean.class, "admin_only_mode"), - AUTO_ENTITY_WIPE(Boolean.class, "auto_wipe"); + AUTO_ENTITY_WIPE(Boolean.class, "auto_wipe"), + // + AMP_ENABLED(Boolean.class, "amp.enabled"), + AMP_USERNAME(String.class, "amp.username"), + AMP_PASSWORD(String.class, "amp.password"), + AMP_URL(String.class, "amp.url"); // private final Class type; private final String configName; diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 5e3c6289..7ea292b1 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -351,3 +351,10 @@ overlord_ips: # Blocked Chat Codes - Use &code,&code2,&code3 (No spaces) blocked_chatcodes: '&0,&k,&m,&n' + +# AMP Integration, used to restart server (optional, without it, have to start server manually after stop) +amp: + enabled: false + username: username + password: password + url: url \ No newline at end of file