diff --git a/Plugins/Mineplex.Core.Common/pom.xml b/Plugins/Mineplex.Core.Common/pom.xml index c2512a855..86a9ef499 100644 --- a/Plugins/Mineplex.Core.Common/pom.xml +++ b/Plugins/Mineplex.Core.Common/pom.xml @@ -16,6 +16,16 @@ com.mineplex spigot + + com.google.http-client + google-http-client + 1.20.0 + + + com.google.http-client + google-http-client-gson + 1.20.0 + diff --git a/Plugins/Mineplex.Core.Common/src/mineplex/core/common/api/ApiEndpoint.java b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/api/ApiEndpoint.java new file mode 100644 index 000000000..9c5fb2581 --- /dev/null +++ b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/api/ApiEndpoint.java @@ -0,0 +1,25 @@ +package mineplex.core.common.api; + +import com.google.gson.Gson; + +/** + * @author Shaun Bennett + */ +public class ApiEndpoint +{ + private static final String API_HOST = "localhost"; + private static final int API_PORT = 3000; + + private ApiWebCall _webCall; + + public ApiEndpoint(String path, Gson gson) + { + String url = "http://" + API_HOST + ":" + API_PORT + path; + _webCall = new ApiWebCall(url, gson); + } + + protected ApiWebCall getWebCall() + { + return _webCall; + } +} diff --git a/Plugins/Mineplex.Core.Common/src/mineplex/core/common/api/ApiResponse.java b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/api/ApiResponse.java new file mode 100644 index 000000000..be08bf88f --- /dev/null +++ b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/api/ApiResponse.java @@ -0,0 +1,36 @@ +package mineplex.core.common.api; + +import java.util.Date; + +/** + * @author Shaun Bennett + */ +public class ApiResponse +{ + private boolean success; + private String error; + + public ApiResponse() + { + + } + + public boolean isSuccess() + { + return success; + } + + public String getError() + { + return error; + } + + @Override + public String toString() + { + return "ApiResponse{" + + "success=" + success + + ", error='" + error + '\'' + + '}'; + } +} diff --git a/Plugins/Mineplex.Core.Common/src/mineplex/core/common/api/ApiWebCall.java b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/api/ApiWebCall.java new file mode 100644 index 000000000..bcb9cb2f5 --- /dev/null +++ b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/api/ApiWebCall.java @@ -0,0 +1,95 @@ +package mineplex.core.common.api; + +import com.google.gson.Gson; +import org.apache.http.HttpEntity; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.message.BasicHeader; +import org.apache.http.protocol.HTTP; + +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; + +/** + * @author Shaun Bennett + */ +public class ApiWebCall +{ + private String _url; + private Gson _gson; + + private PoolingHttpClientConnectionManager _cm; + private CloseableHttpClient _httpClient; + + public ApiWebCall(String url, Gson gson) + { + _url = url; + _gson = gson; + + _cm = new PoolingHttpClientConnectionManager(); + _cm.setMaxTotal(200); + _cm.setDefaultMaxPerRoute(20); + _httpClient = HttpClients.custom().setConnectionManager(_cm).build(); + } + + public T get(String resource, Class clazz) + { + T returnData = null; + + HttpGet httpGet = new HttpGet(_url + resource); + try (CloseableHttpResponse response = _httpClient.execute(httpGet)) { + returnData = getFromResponse(response, clazz); + } catch (IOException e) + { + e.printStackTrace(); + } + + return returnData; + } + + public T post(String resource, Class clazz, Object data) + { + T returnData = null; + + HttpPost httpPost = new HttpPost(_url + resource); + try + { + if (data != null) + { + StringEntity params = new StringEntity(_gson.toJson(data)); + params.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); + httpPost.setEntity(params); + } + + try (CloseableHttpResponse response = _httpClient.execute(httpPost)) + { + returnData = getFromResponse(response, clazz); + } catch (ClientProtocolException e) + { + e.printStackTrace(); + } catch (IOException e) + { + e.printStackTrace(); + } + + } catch (UnsupportedEncodingException e) + { + e.printStackTrace(); + } + + return returnData; + } + + private T getFromResponse(CloseableHttpResponse response, Class clazz) throws IOException + { + HttpEntity entity = response.getEntity(); + return _gson.fromJson(new InputStreamReader(entity.getContent()), clazz); + } +} diff --git a/Plugins/Mineplex.Core.Common/src/mineplex/core/common/api/ListResponseType.java b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/api/ListResponseType.java new file mode 100644 index 000000000..18393f026 --- /dev/null +++ b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/api/ListResponseType.java @@ -0,0 +1,36 @@ +package mineplex.core.common.api; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.List; + +/** + * @author Shaun Bennett + */ +public class ListResponseType implements ParameterizedType +{ + private Class _wrapped; + + public ListResponseType(Class wrapped) + { + _wrapped = wrapped; + } + + @Override + public Type[] getActualTypeArguments() + { + return new Type[] { _wrapped }; + } + + @Override + public Type getRawType() + { + return List.class; + } + + @Override + public Type getOwnerType() + { + return null; + } +} diff --git a/Plugins/Mineplex.Core/src/mineplex/core/boosters/Booster.java b/Plugins/Mineplex.Core/src/mineplex/core/boosters/Booster.java new file mode 100644 index 000000000..a3f7102c9 --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/boosters/Booster.java @@ -0,0 +1,71 @@ +package mineplex.core.boosters; + +import java.util.Date; +import java.util.UUID; + +/** + * @author Shaun Bennett + */ +public class Booster +{ + private String playerName; + private UUID uuid; + private int accountId; + private int duration; + private Date startTime; + private Date endTime; + private Date activationTime; + + public Booster() + { + } + + public String getPlayerName() + { + return playerName; + } + + public UUID getUuid() + { + return uuid; + } + + public int getAccountId() + { + return accountId; + } + + public int getDuration() + { + return duration; + } + + public Date getStartTime() + { + return startTime; + } + + public Date getEndTime() + { + return endTime; + } + + public Date getActivationTime() + { + return activationTime; + } + + @Override + public String toString() + { + return "Booster{" + + "playerName='" + playerName + '\'' + + ", uuid=" + uuid + + ", accountId=" + accountId + + ", duration=" + duration + + ", startTime=" + startTime + + ", endTime=" + endTime + + ", activationTime=" + activationTime + + '}'; + } +} diff --git a/Plugins/Mineplex.Core/src/mineplex/core/boosters/BoosterApiResponse.java b/Plugins/Mineplex.Core/src/mineplex/core/boosters/BoosterApiResponse.java new file mode 100644 index 000000000..c36c69730 --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/boosters/BoosterApiResponse.java @@ -0,0 +1,19 @@ +package mineplex.core.boosters; + +import mineplex.core.common.api.ApiResponse; + +/** + * @author Shaun Bennett + */ +public class BoosterApiResponse extends ApiResponse +{ + public String startTime; + + @Override + public String toString() + { + return "BoosterApiResponse{" + + "startTime='" + startTime + '\'' + + "} " + super.toString(); + } +} diff --git a/Plugins/Mineplex.Core/src/mineplex/core/boosters/BoosterManager.java b/Plugins/Mineplex.Core/src/mineplex/core/boosters/BoosterManager.java new file mode 100644 index 000000000..44fc87e7d --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/boosters/BoosterManager.java @@ -0,0 +1,61 @@ +package mineplex.core.boosters; + +import mineplex.core.MiniPlugin; +import mineplex.core.common.util.Callback; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.List; +import java.util.UUID; + +/** + * @author Shaun Bennett + */ +public class BoosterManager extends MiniPlugin +{ + private BoosterRepository _repository; + + public BoosterManager(JavaPlugin plugin) + { + super("Booster Manager", plugin); + + _repository = new BoosterRepository(); + } + + public void getBoosters(Callback> callback) + { + + } + + public void getBoosters(String serverGroup, Callback> callback) + { + runAsync(() -> { + try + { + List boosters = _repository.getBoosters(serverGroup); + if (callback != null) runSync(() -> callback.run(boosters)); + } + catch (Exception e) + { + System.err.println("Failed to grab boosters for servergroup: " + serverGroup); + e.printStackTrace(); + } + }); + } + + public void activateBooster(String serverGroup, Player player) + { + + } + + public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException + { + BoosterRepository repository = new BoosterRepository(); + System.out.println("Adding Booster..."); + System.out.println(repository.addBooster("Testing", "Phinary", UUID.randomUUID(), 100, 3600)); + System.out.println("Getting Boosters"); + System.out.println(repository.getBoosters("Testing")); + } +} diff --git a/Plugins/Mineplex.Core/src/mineplex/core/boosters/BoosterRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/boosters/BoosterRepository.java new file mode 100644 index 000000000..e06e6ed71 --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/boosters/BoosterRepository.java @@ -0,0 +1,38 @@ +package mineplex.core.boosters; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import mineplex.core.common.api.ApiEndpoint; +import mineplex.core.common.api.ApiResponse; + +import java.util.Arrays; +import java.util.List; +import java.util.UUID; + +/** + * @author Shaun Bennett + */ +public class BoosterRepository extends ApiEndpoint +{ + public BoosterRepository() + { + super("/booster", new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX").create()); + } + + public List getBoosters(String serverGroup) + { + return Arrays.asList(getWebCall().get("/" + serverGroup, Booster[].class)); + } + + public ApiResponse addBooster(String serverGroup, String playerName, UUID uuid, int accountId, int duration) + { + JsonObject body = new JsonObject(); + body.addProperty("playerName", playerName); + body.addProperty("uuid", uuid.toString()); + body.addProperty("accountId", accountId); + body.addProperty("duration", duration); + + return getWebCall().post("/" + serverGroup, BoosterApiResponse.class, body); + } +} diff --git a/Plugins/Mineplex.Core/src/mineplex/core/server/remotecall/JsonWebCall.java b/Plugins/Mineplex.Core/src/mineplex/core/server/remotecall/JsonWebCall.java index 166c2eb42..5d9d07389 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/server/remotecall/JsonWebCall.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/server/remotecall/JsonWebCall.java @@ -69,7 +69,7 @@ public class JsonWebCall } catch (Exception ex) { - System.out.println("Error executing JsonWebCall: \n" + ex.getMessage()); + System.out.println("Error executing ApiWebCall: \n" + ex.getMessage()); System.out.println("Result: \n" + result); for (StackTraceElement trace : ex.getStackTrace()) @@ -123,7 +123,7 @@ public class JsonWebCall } catch (Exception ex) { - System.out.println("JsonWebCall.Execute() Error:\n" + ex.getMessage()); + System.out.println("ApiWebCall.Execute() Error:\n" + ex.getMessage()); for (StackTraceElement trace : ex.getStackTrace()) { @@ -186,7 +186,7 @@ public class JsonWebCall } catch (Exception ex) { - System.out.println("Error executing JsonWebCall: \n" + ex.getMessage()); + System.out.println("Error executing ApiWebCall: \n" + ex.getMessage()); System.out.println("Result: \n" + result); for (StackTraceElement trace : ex.getStackTrace()) @@ -247,7 +247,7 @@ public class JsonWebCall } catch (Exception ex) { - System.out.println("Error executing JsonWebCall: \n" + ex.getMessage()); + System.out.println("Error executing ApiWebCall: \n" + ex.getMessage()); System.out.println("Result: \n" + result); for (StackTraceElement trace : ex.getStackTrace()) @@ -312,7 +312,7 @@ public class JsonWebCall } catch (Exception ex) { - System.out.println("Error executing JsonWebCall: \n" + ex.getMessage()); + System.out.println("Error executing ApiWebCall: \n" + ex.getMessage()); UtilSystem.printStackTrace(ex.getStackTrace()); System.out.println("Result: \n" + result); } diff --git a/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/JsonWebCall.java b/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/JsonWebCall.java index 9ea4d4572..d0cf143f5 100644 --- a/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/JsonWebCall.java +++ b/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/JsonWebCall.java @@ -69,7 +69,7 @@ public class JsonWebCall } catch (Exception ex) { - System.out.println("Error executing JsonWebCall: \n" + ex.getMessage()); + System.out.println("Error executing ApiWebCall: \n" + ex.getMessage()); System.out.println("Result: \n" + result); for (StackTraceElement trace : ex.getStackTrace()) @@ -123,7 +123,7 @@ public class JsonWebCall } catch (Exception ex) { - System.out.println("JsonWebCall.Execute() Error:\n" + ex.getMessage()); + System.out.println("ApiWebCall.Execute() Error:\n" + ex.getMessage()); for (StackTraceElement trace : ex.getStackTrace()) { @@ -186,7 +186,7 @@ public class JsonWebCall } catch (Exception ex) { - System.out.println("Error executing JsonWebCall: \n" + ex.getMessage()); + System.out.println("Error executing ApiWebCall: \n" + ex.getMessage()); System.out.println("Result: \n" + result); for (StackTraceElement trace : ex.getStackTrace()) @@ -247,7 +247,7 @@ public class JsonWebCall } catch (Exception ex) { - System.out.println("Error executing JsonWebCall: \n" + ex.getMessage()); + System.out.println("Error executing ApiWebCall: \n" + ex.getMessage()); System.out.println("Result: \n" + result); for (StackTraceElement trace : ex.getStackTrace()) @@ -312,7 +312,7 @@ public class JsonWebCall } catch (Exception ex) { - System.out.println("Error executing JsonWebCall: \n" + ex.getMessage()); + System.out.println("Error executing ApiWebCall: \n" + ex.getMessage()); System.out.println("Result: \n" + result); } finally diff --git a/Plugins/pom.xml b/Plugins/pom.xml index fc85e9849..4614d668a 100644 --- a/Plugins/pom.xml +++ b/Plugins/pom.xml @@ -115,7 +115,7 @@ org.apache.httpcomponents httpclient - 4.2 + 4.5.2 compile