First attempt at BuyCraft support (#475)

This commit is contained in:
Rsl1122 2018-01-29 23:01:20 +02:00
parent 547b191464
commit 554eb83efd
7 changed files with 260 additions and 17 deletions

View File

@ -61,6 +61,7 @@ public enum Settings {
WEBSERVER_CERTIFICATE_STOREPASS("WebServer.Security.SSL-Certificate.StorePass"),
WEBSERVER_CERTIFICATE_ALIAS("WebServer.Security.SSL-Certificate.Alias"),
EXTERNAL_WEBSERVER_LINK("WebServer.ExternalWebServerAddress"),
PLUGIN_BUYCRAFT_SECRET("Plugins.BuyCraft.Secret"),
//
SERVER_NAME("Server.ServerName"),
//

View File

@ -123,3 +123,6 @@ Plugins:
Towny:
HideTowns:
- ExampleTown
BuyCraft:
# http://help.buycraft.net/article/36-where-to-find-the-secret-key
Secret: "-"

View File

@ -8,9 +8,11 @@ import com.djrapitops.pluginbridge.plan.aac.AdvancedAntiCheatHook;
import com.djrapitops.pluginbridge.plan.advancedachievements.AdvancedAchievementsHook;
import com.djrapitops.pluginbridge.plan.askyblock.ASkyBlockHook;
import com.djrapitops.pluginbridge.plan.banmanager.BanManagerHook;
import com.djrapitops.pluginbridge.plan.buycraft.BuyCraftHook;
import com.djrapitops.pluginbridge.plan.essentials.EssentialsHook;
import com.djrapitops.pluginbridge.plan.factions.FactionsHook;
import com.djrapitops.pluginbridge.plan.griefprevention.GriefPreventionHook;
import com.djrapitops.pluginbridge.plan.griefprevention.plus.GriefPreventionPlusHook;
import com.djrapitops.pluginbridge.plan.jobs.JobsHook;
import com.djrapitops.pluginbridge.plan.kingdoms.KingdomsHook;
import com.djrapitops.pluginbridge.plan.litebans.LiteBansHook;
@ -26,23 +28,6 @@ import com.djrapitops.pluginbridge.plan.viaversion.ViaVersionHook;
* Manages connection to other plugins.
*
* @author Rsl1122
* @see AdvancedAntiCheatHook
* @see AdvancedAchievementsHook
* @see ASkyBlockHook
* @see BanManagerHook
* @see EssentialsHook
* @see FactionsHook
* @see GriefPreventionHook
* @see JobsHook
* @see KingdomsHook
* @see LiteBansHook
* @see McmmoHook
* @see SuperbVoteHook
* @see ProtocolSupportHook
* @see RedProtectHook
* @see TownyHook
* @see VaultHook
* @see ViaVersionHook
*/
@SuppressWarnings("WeakerAccess")
public class Bridge {
@ -88,9 +73,11 @@ public class Bridge {
new AdvancedAchievementsHook(h),
new ASkyBlockHook(h),
new BanManagerHook(h),
new BuyCraftHook(h),
new EssentialsHook(h),
new FactionsHook(h),
new GriefPreventionHook(h),
new GriefPreventionPlusHook(h),
new JobsHook(h),
new KingdomsHook(h),
new LiteBansHook(h),

View File

@ -0,0 +1,33 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package com.djrapitops.pluginbridge.plan.buycraft;
import com.djrapitops.plan.data.plugin.HookHandler;
import com.djrapitops.plan.system.settings.Settings;
import com.djrapitops.pluginbridge.plan.Hook;
/**
* Hook for BuyCraft plugin.
*
* @author Rsl1122
*/
public class BuyCraftHook extends Hook {
private final String secret;
public BuyCraftHook(HookHandler hookHandler) {
super(hookHandler);
secret = Settings.PLUGIN_BUYCRAFT_SECRET.toString();
enabled = !secret.equals("-") && !secret.isEmpty();
}
@Override
public void hook() throws NoClassDefFoundError {
if (enabled) {
hookHandler.addPluginDataSource(new BuyCraftPluginData(secret));
}
}
}

View File

@ -0,0 +1,75 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package com.djrapitops.pluginbridge.plan.buycraft;
import com.djrapitops.plan.api.PlanAPI;
import com.djrapitops.plan.api.exceptions.connection.ForbiddenException;
import com.djrapitops.plan.data.element.AnalysisContainer;
import com.djrapitops.plan.data.element.InspectContainer;
import com.djrapitops.plan.data.element.TableContainer;
import com.djrapitops.plan.data.plugin.ContainerSize;
import com.djrapitops.plan.data.plugin.PluginData;
import com.djrapitops.plan.utilities.FormatUtils;
import com.djrapitops.plan.utilities.html.Html;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
/**
* PluginData for BuyCraft plugin.
*
* @author Rsl1122
*/
public class BuyCraftPluginData extends PluginData {
private final String secret;
public BuyCraftPluginData(String secret) {
super(ContainerSize.TWO_THIRDS, "BuyCraft");
super.setIconColor("blue");
super.setPluginIcon("shopping-bag");
this.secret = secret;
}
@Override
public InspectContainer getPlayerData(UUID uuid, InspectContainer inspectContainer) {
return inspectContainer;
}
@Override
public AnalysisContainer getServerData(Collection<UUID> collection, AnalysisContainer analysisContainer) throws Exception {
try {
List<Payment> payments = new ListPaymentRequest(secret).makeRequest();
TableContainer payTable = new TableContainer(true, getWithIcon("Date", "calendar"), getWithIcon("Donation", "money"));
payTable.setColor("blue");
for (Payment payment : payments) {
String name = payment.getPlayerName();
payTable.addRow(
Html.LINK.parse(PlanAPI.getInstance().getPlayerInspectPageLink(name), name),
FormatUtils.formatTimeStampYear(payment.getDate()),
FormatUtils.cutDecimals(payment.getAmount()) + payment.getCurrency()
);
}
analysisContainer.addTable("payTable", payTable);
Map<UUID, String> playerTableValues = payments.stream()
.collect(Collectors.toMap(Payment::getUuid, payment -> payment.getAmount() + payment.getCurrency()));
analysisContainer.addPlayerTableValues(getWithIcon("Donation", "money"), playerTableValues);
} catch (IllegalStateException | NullPointerException e) {
analysisContainer.addValue("JSON error", e.getMessage());
} catch (ForbiddenException e) {
analysisContainer.addValue("Configuration error", e.getMessage());
}
return analysisContainer;
}
}

View File

@ -0,0 +1,95 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package com.djrapitops.pluginbridge.plan.buycraft;
import com.djrapitops.plan.api.exceptions.connection.ForbiddenException;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* Request to Buycraft API for payment listings.
*
* @author Rsl1122
*/
public class ListPaymentRequest {
private final String secret;
public ListPaymentRequest(String secret) {
this.secret = secret;
}
public List<Payment> makeRequest() throws IOException, ForbiddenException {
URL url = new URL("https://plugin.buycraft.net/payments");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("X-BuyCraft-Secret", secret);
connection.getOutputStream().write(0);
JsonElement json;
try {
InputStreamReader reader = new InputStreamReader(connection.getInputStream());
json = new JsonParser().parse(reader);
} finally {
connection.disconnect();
}
if (json == null || json.isJsonNull()) {
throw new NullPointerException("JSON should not be null");
}
List<Payment> payments = new ArrayList<>();
if (json.isJsonObject()) {
return readError(json);
} else if (json.isJsonArray()) {
readAndAddPayments(json, payments);
}
return payments;
}
private void readAndAddPayments(JsonElement json, List<Payment> payments) {
JsonArray jsonArray = json.getAsJsonArray();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
for (JsonElement element : jsonArray) {
JsonObject payment = element.getAsJsonObject();
double amount = payment.get("amount").getAsDouble();
String dateString = payment.get("date").getAsString();
long date = dateFormat.parse(dateString, new ParsePosition(0)).getTime();
String currency = payment.get("currency").getAsJsonObject().get("symbol").getAsString();
JsonObject player = payment.get("player").getAsJsonObject();
String playerName = player.get("name").getAsString();
UUID uuid = UUID.fromString(player.get("uuid").getAsString());
payments.add(new Payment(amount, currency, uuid, playerName, date));
}
}
private List<Payment> readError(JsonElement json) throws ForbiddenException {
JsonObject jsonObject = json.getAsJsonObject();
int errorCode = jsonObject.get("error_code").getAsInt();
String errorMessage = jsonObject.get("error_message").getAsString();
if (errorCode == 403) {
throw new ForbiddenException("Incorrect Server Secret. Check config.");
} else {
throw new IllegalStateException(errorCode + ": " + errorMessage);
}
}
}

View File

@ -0,0 +1,49 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package com.djrapitops.pluginbridge.plan.buycraft;
import java.util.UUID;
/**
* Represents a BuyCraft payment.
*
* @author Rsl1122
*/
public class Payment {
private final double amount;
private final String currency;
private final UUID uuid;
private final String playerName;
private final long date;
public Payment(double amount, String currency, UUID uuid, String playerName, long date) {
this.amount = amount;
this.currency = currency;
this.uuid = uuid;
this.playerName = playerName;
this.date = date;
}
public double getAmount() {
return amount;
}
public String getCurrency() {
return currency;
}
public String getPlayerName() {
return playerName;
}
public long getDate() {
return date;
}
public UUID getUuid() {
return uuid;
}
}