mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-02-02 21:41:28 +01:00
Fixed BuyCraft PluginData
This commit is contained in:
parent
554eb83efd
commit
bd96978917
@ -219,7 +219,7 @@
|
||||
<artifactId>maven-install-plugin</artifactId>
|
||||
<version>2.5.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<!--<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>2.2.1</version>
|
||||
@ -245,7 +245,7 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!--<plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-gpg-plugin</artifactId>
|
||||
<version>1.5</version>
|
||||
|
@ -21,6 +21,7 @@ public class BuyCraftHook extends Hook {
|
||||
super(hookHandler);
|
||||
|
||||
secret = Settings.PLUGIN_BUYCRAFT_SECRET.toString();
|
||||
|
||||
enabled = !secret.equals("-") && !secret.isEmpty();
|
||||
}
|
||||
|
||||
|
@ -14,11 +14,7 @@ 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;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* PluginData for BuyCraft plugin.
|
||||
@ -30,7 +26,7 @@ public class BuyCraftPluginData extends PluginData {
|
||||
private final String secret;
|
||||
|
||||
public BuyCraftPluginData(String secret) {
|
||||
super(ContainerSize.TWO_THIRDS, "BuyCraft");
|
||||
super(ContainerSize.TAB, "BuyCraft");
|
||||
super.setIconColor("blue");
|
||||
super.setPluginIcon("shopping-bag");
|
||||
|
||||
@ -47,29 +43,46 @@ public class BuyCraftPluginData extends PluginData {
|
||||
try {
|
||||
|
||||
List<Payment> payments = new ListPaymentRequest(secret).makeRequest();
|
||||
TableContainer payTable = new TableContainer(true, getWithIcon("Date", "calendar"), getWithIcon("Donation", "money"));
|
||||
payTable.setColor("blue");
|
||||
Collections.sort(payments);
|
||||
|
||||
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()
|
||||
);
|
||||
}
|
||||
addPaymentTotals(analysisContainer, payments);
|
||||
addPlayerTable(analysisContainer, payments);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private void addPlayerTable(AnalysisContainer analysisContainer, List<Payment> payments) {
|
||||
TableContainer payTable = new TableContainer(
|
||||
true,
|
||||
getWithIcon("Date", "calendar"),
|
||||
getWithIcon("Amount", "money"),
|
||||
getWithIcon("Packages", "cube")
|
||||
);
|
||||
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(),
|
||||
payment.getPackages()
|
||||
);
|
||||
}
|
||||
analysisContainer.addTable("payTable", payTable);
|
||||
}
|
||||
|
||||
private void addPaymentTotals(AnalysisContainer analysisContainer, List<Payment> payments) {
|
||||
Map<String, Double> paymentTotals = new HashMap<>();
|
||||
for (Payment payment : payments) {
|
||||
String currency = payment.getCurrency();
|
||||
double amount = payment.getAmount();
|
||||
paymentTotals.put(currency, paymentTotals.getOrDefault(currency, 0.0) + amount);
|
||||
}
|
||||
for (Map.Entry<String, Double> entry : paymentTotals.entrySet()) {
|
||||
analysisContainer.addValue(getWithIcon("Total " + entry.getKey(), "money", "blue"), FormatUtils.cutDecimals(entry.getValue()));
|
||||
}
|
||||
}
|
||||
}
|
@ -17,8 +17,8 @@ import java.net.URL;
|
||||
import java.text.ParsePosition;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Request to Buycraft API for payment listings.
|
||||
@ -39,7 +39,6 @@ public class ListPaymentRequest {
|
||||
|
||||
connection.setRequestMethod("GET");
|
||||
connection.setRequestProperty("X-BuyCraft-Secret", secret);
|
||||
connection.getOutputStream().write(0);
|
||||
|
||||
JsonElement json;
|
||||
try {
|
||||
@ -64,20 +63,28 @@ public class ListPaymentRequest {
|
||||
|
||||
private void readAndAddPayments(JsonElement json, List<Payment> payments) {
|
||||
JsonArray jsonArray = json.getAsJsonArray();
|
||||
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
|
||||
|
||||
for (JsonElement element : jsonArray) {
|
||||
// System.out.println(element.toString().replace(",", ",\n"));
|
||||
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();
|
||||
Date dateObj = dateFormat.parse(dateString, new ParsePosition(0));
|
||||
long date = dateObj.getTime();
|
||||
String currency = payment.get("currency").getAsJsonObject().get("iso_4217").getAsString();
|
||||
JsonObject player = payment.get("player").getAsJsonObject();
|
||||
String playerName = player.get("name").getAsString();
|
||||
UUID uuid = UUID.fromString(player.get("uuid").getAsString());
|
||||
// UUID uuid = UUID.fromString(player.get("uuid").getAsString().replaceFirst(
|
||||
// "(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)", "$1-$2-$3-$4-$5"
|
||||
// ));
|
||||
StringBuilder packages = new StringBuilder();
|
||||
for (JsonElement pack : payment.get("packages").getAsJsonArray()) {
|
||||
packages.append(pack.getAsJsonObject().get("name")).append("<br>");
|
||||
}
|
||||
|
||||
payments.add(new Payment(amount, currency, uuid, playerName, date));
|
||||
payments.add(new Payment(amount, currency, null, playerName, date, packages.toString()));
|
||||
// System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -11,20 +11,22 @@ import java.util.UUID;
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class Payment {
|
||||
public class Payment implements Comparable<Payment> {
|
||||
|
||||
private final double amount;
|
||||
private final String currency;
|
||||
private final UUID uuid;
|
||||
private final String playerName;
|
||||
private final long date;
|
||||
private final String packages;
|
||||
|
||||
public Payment(double amount, String currency, UUID uuid, String playerName, long date) {
|
||||
public Payment(double amount, String currency, UUID uuid, String playerName, long date, String packages) {
|
||||
this.amount = amount;
|
||||
this.currency = currency;
|
||||
this.uuid = uuid;
|
||||
this.playerName = playerName;
|
||||
this.date = date;
|
||||
this.packages = packages;
|
||||
}
|
||||
|
||||
public double getAmount() {
|
||||
@ -46,4 +48,13 @@ public class Payment {
|
||||
public UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public String getPackages() {
|
||||
return packages;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Payment o) {
|
||||
return this.playerName.toLowerCase().compareTo(o.playerName.toLowerCase());
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.djrapitops.pluginbridge.plan.buycraft;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.connection.ForbiddenException;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Test for ListPaymentRequest.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class ListPaymentRequestTest {
|
||||
|
||||
@Test
|
||||
public void testSuccess() throws IOException, ForbiddenException {
|
||||
new ListPaymentRequest("166473e780b59e84d6a19f1975c9282bfcc7a2a7").makeRequest();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user