Updated to EtzCore 1.3

Updated all tests
master
Etzelia 2018-09-28 23:12:42 -05:00
parent 4388a1af85
commit 9d2f4c979f
6 changed files with 88 additions and 236 deletions

37
pom.xml
View File

@ -3,7 +3,7 @@
<groupId>xyz.etztech</groupId>
<artifactId>MinecraftManager</artifactId>
<!-- Version is used in plugin.yml -->
<version>1.3</version>
<version>1.4</version>
<packaging>jar</packaging>
<!-- Plugin Information -->
@ -12,31 +12,6 @@
<description>A plugin used alongside the MinecraftManager Django project.</description>
<url>http://www.etztech.xyz</url>
<licenses>
<license>
<name>Zlib License</name>
<url>http://opensource.org/licenses/Zlib</url>
<comments>Copyright (c) 2017 EtzTech
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.</comments>
</license>
</licenses>
<developers>
<developer>
@ -57,21 +32,19 @@
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.13.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>xyz.etztech</groupId>
<artifactId>EtzCore</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
<version>1.3</version>
<scope>compile</scope>
</dependency>

View File

@ -1,27 +1,9 @@
package xyz.etztech.minecraftmanager;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.bukkit.Bukkit;
import xyz.etztech.minecraftmanager.objects.Application;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MCMAPI {
@ -38,98 +20,6 @@ public class MCMAPI {
MCMAPI.api = api;
}
public static JsonArray queryModel(String model, Map<String, String> filters) {
// Querying a model
String djangoUrl = getDjangoUrl();
djangoUrl += "model/";
try {
String get = GET(djangoUrl + model + "/", filters);
JsonParser jsonParser = new JsonParser();
return (JsonArray)jsonParser.parse(get);
} catch (Exception ex) {
log("Could not connect to Django. Is the web server running?");
}
return new JsonArray();
}
public static JsonObject postApplication(Application application) {
String djangoUrl = getDjangoUrl() + "plugin/application/";
String post = POST(djangoUrl, application.getForm());
JsonParser jsonParser = new JsonParser();
return (JsonObject) jsonParser.parse(post);
}
public static JsonObject postApplicationAction(String id, boolean accepted, String username) {
String djangoUrl = getDjangoUrl() + "plugin/application_action/";
Map<String, String> data = new HashMap<>();
data.put("application_id", id);
data.put("action", accepted ? "True" : "False");
data.put("username", username);
String post = POST(djangoUrl, data);
JsonParser jsonParser = new JsonParser();
return (JsonObject) jsonParser.parse(post);
}
public static JsonObject postApplicationClear(String id) {
String djangoUrl = getDjangoUrl() + "plugin/application_clear/";
Map<String, String> data = new HashMap<>();
data.put("application_id", id);
String post = POST(djangoUrl, data);
JsonParser jsonParser = new JsonParser();
return (JsonObject) jsonParser.parse(post);
}
public static JsonObject postLogin(String username, String uuid, String ip) {
String djangoUrl = getDjangoUrl() + "plugin/login/";
Map<String, String> data = new HashMap<>();
data.put("username", username);
data.put("uuid", uuid);
data.put("ip", ip);
String post = POST(djangoUrl, data);
JsonParser jsonParser = new JsonParser();
return (JsonObject) jsonParser.parse(post);
}
public static JsonObject postTicket(String uuid, String message, String x, String y, String z, String world) {
String djangoUrl = getDjangoUrl() + "plugin/ticket/";
Map<String, String> data = new HashMap<>();
data.put("uuid", uuid);
data.put("message", message);
data.put("x", x);
data.put("y", y);
data.put("z", z);
data.put("world", world);
String post = POST(djangoUrl, data);
JsonParser jsonParser = new JsonParser();
return (JsonObject) jsonParser.parse(post);
}
public static JsonObject postWarning(String player, String staff, String severity, String message) {
String djangoUrl = getDjangoUrl() + "plugin/warning/";
Map<String, String> data = new HashMap<>();
data.put("player", player);
data.put("staff", staff);
data.put("severity", severity);
data.put("message", message);
String post = POST(djangoUrl, data);
JsonParser jsonParser = new JsonParser();
return (JsonObject) jsonParser.parse(post);
}
public static JsonObject getPassword(String uuid) {
String djangoUrl = getDjangoUrl() + "plugin/register/";
Map<String, String> data = new HashMap<>();
data.put("uuid", uuid);
String post = POST(djangoUrl, data);
JsonParser jsonParser = new JsonParser();
return (JsonObject) jsonParser.parse(post);
}
private static void log(String message) {
try {
Bukkit.getConsoleSender().sendMessage(message);
@ -149,69 +39,7 @@ public class MCMAPI {
}
private static String GET(String url, Map<String, String> data) {
if (StringUtils.isNotEmpty(MCMAPI.api)) {
data.put("api", MCMAPI.api);
} else {
data.put("api", MinecraftManager.config.getString("django.api"));
}
StringBuffer result = new StringBuffer();
ArrayList<String> query = new ArrayList<>();
for (String key : data.keySet()) {
query.add(key + "=" + data.get(key));
}
String dataQuery = StringUtils.join(query, "&");
try {
HttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet(url + "?" + dataQuery);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
InputStream input = entity.getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
String inputLine;
while ((inputLine = in.readLine()) != null) {
result.append(inputLine);
}
input.close();
} catch (Exception ex) {
log("GET request failed. (" + url + ")");
}
return result.toString();
}
private static String POST(String url, Map<String, String> data) {
StringBuffer result = new StringBuffer();
if (StringUtils.isNotEmpty(MCMAPI.api)) {
data.put("api", MCMAPI.api);
} else {
data.put("api", MinecraftManager.config.getString("django.api"));
}
try {
HttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
List<NameValuePair> params = new ArrayList<>();
for (String key : data.keySet()) {
params.add(new BasicNameValuePair(key, data.get(key)));
}
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
InputStream input = entity.getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
String inputLine;
while ((inputLine = in.readLine()) != null) {
result.append(inputLine);
}
input.close();
} catch (Exception ex) {
log("POST request failed. (" + url + ")");
}
return result.toString();
}
/**
* @return The Django API URL ending with a slash

View File

@ -6,6 +6,8 @@ import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import xyz.etztech.core.web.CoreWeb;
import xyz.etztech.minecraftmanager.objects.ModelResponse;
import javax.xml.soap.Text;
import java.io.FileWriter;
@ -95,12 +97,16 @@ public class MCMUtil {
try {
uuid = Bukkit.getPlayer(playerName).getUniqueId().toString();
} catch(Exception ex) {
Map<String, String> filters = new HashMap<>();
filters.put("username__iexact", playerName);
JsonArray players = MCMAPI.queryModel("player", filters);
if (players.size() == 1) {
uuid = players.get(0).getAsJsonObject().get("uuid").getAsString();
}
try {
Map<String, String> filters = MCMAPI.setup();
filters.put("username__iexact", playerName);
ModelResponse response = new ModelResponse(CoreWeb.HTTP(MCMAPI.getModelUrl("application"), CoreWeb.HttpMethod.GET,
filters));
JsonArray players = response.getResults();
if (players.size() == 1) {
uuid = players.get(0).getAsJsonObject().get("uuid").getAsString();
}
} catch (Exception ignored) {}
}
return uuid;
}

View File

@ -5,6 +5,8 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import xyz.etztech.core.api.IMinecraftManager;
import xyz.etztech.core.maven.MavenLibrary;
import xyz.etztech.core.maven.MavenPlugin;
import xyz.etztech.minecraftmanager.command.*;
import xyz.etztech.minecraftmanager.listeners.AsyncPlayerChatListener;
import xyz.etztech.minecraftmanager.listeners.BlockBreakListener;
@ -21,7 +23,8 @@ import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
public class MinecraftManager extends JavaPlugin implements IMinecraftManager {
@MavenLibrary(group = "commons-lang", artifact = "commons-lang", version = "2.6")
public class MinecraftManager extends MavenPlugin implements IMinecraftManager {
public static MinecraftManager instance;
@ -48,7 +51,7 @@ public class MinecraftManager extends JavaPlugin implements IMinecraftManager {
@Override
public void onEnable() {
public void enable() {
instance = this;
saveDefaultConfig();
@ -93,7 +96,7 @@ public class MinecraftManager extends JavaPlugin implements IMinecraftManager {
}
@Override
public void onDisable() {
public void disable() {
}

View File

@ -14,7 +14,7 @@ public class MCMResponse extends CoreResponse {
public MCMResponse(JsonArray httpResponse) {
super(httpResponse);
setExtra(json.get("extra"));
setExtra(getJson().get("extra"));
}
public MCMResponse(JsonObject httpResponse) {
@ -24,7 +24,7 @@ public class MCMResponse extends CoreResponse {
public MCMResponse(String rawReponse) {
super(rawReponse);
setExtra(json.get("extra"));
setExtra(getJson().get("extra"));
}

View File

@ -3,17 +3,21 @@ package xyz.etztech.minecraftmanager.test;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import org.apache.commons.lang.StringUtils;
import xyz.etztech.core.web.CoreWeb;
import xyz.etztech.core.web.ICallback;
import xyz.etztech.minecraftmanager.objects.Application;
import xyz.etztech.minecraftmanager.MCMAPI;
import xyz.etztech.minecraftmanager.objects.MCMResponse;
import xyz.etztech.minecraftmanager.objects.ModelResponse;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
public static void main(String[] args) throws Exception {
String testUrl = "http://127.0.0.1:8000/api/";
String testApi = "Testing1";
try {
@ -74,12 +78,14 @@ public class Test {
break;
}
Map<String, String> data = MCMAPI.setup();
if (option == 1) {
// Query Model
Map<String, String> filters = new HashMap<>();
filters.put("username__icontains", "1");
JsonArray results = MCMAPI.queryModel("application", filters);
data.put("username__icontains", "1");
ModelResponse response = new ModelResponse(CoreWeb.HTTP(MCMAPI.getModelUrl("application"),
CoreWeb.HttpMethod.GET, data));
JsonArray results = response.getResults();
System.out.println(results.size());
for (JsonElement jsonElement : results) {
System.out.println("Raw JSON: " + jsonElement.toString());
@ -96,7 +102,10 @@ public class Test {
app1.setEverBannedExplanation("");
app1.setReference("reddit");
app1.setReadRules("24karrot");
MCMResponse response1 = new MCMResponse(MCMAPI.postApplication(app1));
String djangoUrl = MCMAPI.getDjangoUrl() + "plugin/application/";
data.putAll(app1.getForm());
MCMResponse response1 = new MCMResponse(CoreWeb.HTTP(djangoUrl, CoreWeb.HttpMethod.POST,
data));
System.out.println("===== App 1 =====");
System.out.println("Status: " + response1.getStatus());
System.out.println("Message: " + response1.getMessage());
@ -109,50 +118,82 @@ public class Test {
app2.setEverBannedExplanation("Griefing");
app2.setReference("planet minecraft");
app2.setReadRules("24karrot");
MCMResponse response2 = new MCMResponse(MCMAPI.postApplication(app2));
data = MCMAPI.setup();
data.putAll(app2.getForm());
MCMResponse response2 = new MCMResponse(CoreWeb.HTTP(djangoUrl, CoreWeb.HttpMethod.POST,
data));
System.out.println("===== App 2 =====");
System.out.println("Status: " + response2.getStatus());
System.out.println("Message: " + response2.getMessage());
} else if (option == 3 || option == 4) {
boolean accept = option == 3;
String username = option == 3 ? "Testing1" : "Testing2";
Map<String, String> filters = new HashMap<>();
filters.put("username__exact", username);
JsonArray array = MCMAPI.queryModel("application", filters);
data.put("username__exact", username);
ModelResponse response = new ModelResponse(CoreWeb.HTTP(MCMAPI.getModelUrl("application"),
CoreWeb.HttpMethod.GET, data));
JsonArray array = response.getResults();
if (array.size() != 1) {
System.out.println("Couldn't find the application for " + username + ". Does it exist?");
} else {
String id = array.get(0).getAsJsonObject().get("id").getAsString();
MCMResponse response = new MCMResponse(MCMAPI.postApplicationAction(id, accept, "Plugin Test"));
String djangoUrl = MCMAPI.getDjangoUrl() + "plugin/application_action/";
data.put("application_id", id);
data.put("action", accept ? "True" : "False");
data.put("username", "Plugin Test");
MCMResponse mcmResponse = new MCMResponse(CoreWeb.HTTP(djangoUrl, CoreWeb.HttpMethod.POST,
data));
System.out.println("===== " + username + " =====");
System.out.println("Status: " + response.getStatus());
System.out.println("Message: " + response.getMessage());
System.out.println("Status: " + mcmResponse.getStatus());
System.out.println("Message: " + mcmResponse.getMessage());
}
} else if (option == 5) {
Map<String, String> filters = new HashMap<>();
filters.put("username__exact", "Testing2");
JsonArray array = MCMAPI.queryModel("application", filters);
data.put("username__exact", "Testing2");
ModelResponse response = new ModelResponse(CoreWeb.HTTP(MCMAPI.getModelUrl("application"),
CoreWeb.HttpMethod.GET, data));
JsonArray array = response.getResults();
if (array.size() != 1) {
System.out.println("Couldn't find the application for Testing2. Does it exist?");
} else {
String id = array.get(0).getAsJsonObject().get("id").getAsString();
MCMResponse response = new MCMResponse(MCMAPI.postApplicationClear(id));
String djangoUrl = MCMAPI.getDjangoUrl() + "plugin/application_clear/";
data.put("application_id", id);
MCMResponse mcmResponse = new MCMResponse(CoreWeb.HTTP(djangoUrl, CoreWeb.HttpMethod.POST,
data));
System.out.println("===== Testing2 =====");
System.out.println("Status: " + response.getStatus());
System.out.println("Message: " + response.getMessage());
System.out.println("Status: " + mcmResponse.getStatus());
System.out.println("Message: " + mcmResponse.getMessage());
}
} else if (option == 6) {
MCMResponse response = new MCMResponse(MCMAPI.postLogin("Etzelia", "bf0446a8-9695-4c41-aa4c-7ff45bfd1171", "127.0.0.1"));
String djangoUrl = MCMAPI.getDjangoUrl() + "plugin/login/";
data.put("uuid", "bf0446a8-9695-4c41-aa4c-7ff45bfd1171");
data.put("username", "Etzelia");
data.put("ip", "127.0.0.1");
MCMResponse response = new MCMResponse(CoreWeb.HTTP(djangoUrl, CoreWeb.HttpMethod.POST,
data));
System.out.println("===== Login =====");
System.out.println("Status: " + response.getStatus());
System.out.println("Message: " + response.getMessage());
} else if (option == 7) {
MCMResponse response = new MCMResponse(MCMAPI.postTicket("bf0446a8-9695-4c41-aa4c-7ff45bfd1171", "Test ticket.", "1", "2", "3", "O"));
String djangoUrl = MCMAPI.getDjangoUrl() + "plugin/ticket/";
data.put("uuid", "bf0446a8-9695-4c41-aa4c-7ff45bfd1171");
data.put("message", "Test Ticket");
data.put("x", "1");
data.put("y", "2");
data.put("z", "3");
data.put("world", "O");
MCMResponse response = new MCMResponse(CoreWeb.HTTP(djangoUrl, CoreWeb.HttpMethod.POST,
data));
System.out.println("===== Ticket =====");
System.out.println("Status: " + response.getStatus());
System.out.println("Message: " + response.getMessage());
} else if (option == 8) {
MCMResponse response = new MCMResponse(MCMAPI.postWarning("bf0446a8-9695-4c41-aa4c-7ff45bfd1171", "bf0446a8-9695-4c41-aa4c-7ff45bfd1171", "M", "Test Warning"));
String djangoUrl = MCMAPI.getDjangoUrl() + "plugin/warning/";
data.put("player", "bf0446a8-9695-4c41-aa4c-7ff45bfd1171");
data.put("staff", "bf0446a8-9695-4c41-aa4c-7ff45bfd1171");
data.put("severity", "H");
data.put("message", "Test Warning");
MCMResponse response = new MCMResponse(CoreWeb.HTTP(djangoUrl, CoreWeb.HttpMethod.POST,
data));
System.out.println("===== Warning =====");
System.out.println("Status: " + response.getStatus());
System.out.println("Message: " + response.getMessage());
@ -160,4 +201,5 @@ public class Test {
}
}
}