Implement #22 and fix pom.xml.

Signed-off-by: Lemon <minecraftoxlemonxo@gmail.com>
This commit is contained in:
Lemon 2018-02-22 17:55:04 +05:00
parent 2bffcef9a9
commit 0551337e8b
12 changed files with 376 additions and 15 deletions

View File

@ -144,6 +144,7 @@
<groupId>me.libraryaddict</groupId>
<artifactId>LibsDisguises</artifactId>
<scope>system</scope>
<version>9.4.0-SNAPSHOT</version>
<systemPath>${project.basedir}/lib/LibsDisguises.jar</systemPath>
</dependency>

View File

@ -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<TotalFreedomMod>
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<TotalFreedomMod>
ldb = bridges.registerService(LibsDisguisesBridge.class);
web = bridges.registerService(WorldEditBridge.class);
wgb = bridges.registerService(WorldGuardBridge.class);
amp = bridges.registerService(AMP.class);
bridges.start();
timer.update();

View File

@ -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() {
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}
}

View File

@ -0,0 +1,6 @@
package me.totalfreedom.totalfreedommod.amp;
public interface LoginCallback
{
void loginDone(boolean success);
}

View File

@ -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<String> 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;
}

View File

@ -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();
}
}
}
}

View File

@ -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 = "/<command>")
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;
}
}

View File

@ -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
{

View File

@ -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;

View File

@ -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