Fixed BuyCraft PluginData

This commit is contained in:
Rsl1122 2018-01-29 23:46:34 +02:00
parent 554eb83efd
commit bd96978917
6 changed files with 87 additions and 36 deletions

View File

@ -219,7 +219,7 @@
<artifactId>maven-install-plugin</artifactId> <artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version> <version>2.5.2</version>
</plugin> </plugin>
<plugin> <!--<plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId> <artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version> <version>2.2.1</version>
@ -245,7 +245,7 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<!--<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId> <artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version> <version>1.5</version>

View File

@ -21,6 +21,7 @@ public class BuyCraftHook extends Hook {
super(hookHandler); super(hookHandler);
secret = Settings.PLUGIN_BUYCRAFT_SECRET.toString(); secret = Settings.PLUGIN_BUYCRAFT_SECRET.toString();
enabled = !secret.equals("-") && !secret.isEmpty(); enabled = !secret.equals("-") && !secret.isEmpty();
} }

View File

@ -14,11 +14,7 @@ import com.djrapitops.plan.data.plugin.PluginData;
import com.djrapitops.plan.utilities.FormatUtils; import com.djrapitops.plan.utilities.FormatUtils;
import com.djrapitops.plan.utilities.html.Html; import com.djrapitops.plan.utilities.html.Html;
import java.util.Collection; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
/** /**
* PluginData for BuyCraft plugin. * PluginData for BuyCraft plugin.
@ -30,7 +26,7 @@ public class BuyCraftPluginData extends PluginData {
private final String secret; private final String secret;
public BuyCraftPluginData(String secret) { public BuyCraftPluginData(String secret) {
super(ContainerSize.TWO_THIRDS, "BuyCraft"); super(ContainerSize.TAB, "BuyCraft");
super.setIconColor("blue"); super.setIconColor("blue");
super.setPluginIcon("shopping-bag"); super.setPluginIcon("shopping-bag");
@ -47,29 +43,46 @@ public class BuyCraftPluginData extends PluginData {
try { try {
List<Payment> payments = new ListPaymentRequest(secret).makeRequest(); List<Payment> payments = new ListPaymentRequest(secret).makeRequest();
TableContainer payTable = new TableContainer(true, getWithIcon("Date", "calendar"), getWithIcon("Donation", "money")); Collections.sort(payments);
payTable.setColor("blue");
for (Payment payment : payments) { addPaymentTotals(analysisContainer, payments);
String name = payment.getPlayerName(); addPlayerTable(analysisContainer, payments);
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) { } catch (ForbiddenException e) {
analysisContainer.addValue("Configuration error", e.getMessage()); analysisContainer.addValue("Configuration error", e.getMessage());
} }
return analysisContainer; 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()));
}
}
} }

View File

@ -17,8 +17,8 @@ import java.net.URL;
import java.text.ParsePosition; import java.text.ParsePosition;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID;
/** /**
* Request to Buycraft API for payment listings. * Request to Buycraft API for payment listings.
@ -39,7 +39,6 @@ public class ListPaymentRequest {
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
connection.setRequestProperty("X-BuyCraft-Secret", secret); connection.setRequestProperty("X-BuyCraft-Secret", secret);
connection.getOutputStream().write(0);
JsonElement json; JsonElement json;
try { try {
@ -64,20 +63,28 @@ public class ListPaymentRequest {
private void readAndAddPayments(JsonElement json, List<Payment> payments) { private void readAndAddPayments(JsonElement json, List<Payment> payments) {
JsonArray jsonArray = json.getAsJsonArray(); JsonArray jsonArray = json.getAsJsonArray();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
for (JsonElement element : jsonArray) { for (JsonElement element : jsonArray) {
// System.out.println(element.toString().replace(",", ",\n"));
JsonObject payment = element.getAsJsonObject(); JsonObject payment = element.getAsJsonObject();
double amount = payment.get("amount").getAsDouble(); double amount = payment.get("amount").getAsDouble();
String dateString = payment.get("date").getAsString(); String dateString = payment.get("date").getAsString();
long date = dateFormat.parse(dateString, new ParsePosition(0)).getTime(); Date dateObj = dateFormat.parse(dateString, new ParsePosition(0));
String currency = payment.get("currency").getAsJsonObject().get("symbol").getAsString(); long date = dateObj.getTime();
String currency = payment.get("currency").getAsJsonObject().get("iso_4217").getAsString();
JsonObject player = payment.get("player").getAsJsonObject(); JsonObject player = payment.get("player").getAsJsonObject();
String playerName = player.get("name").getAsString(); 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();
} }
} }

View File

@ -11,20 +11,22 @@ import java.util.UUID;
* *
* @author Rsl1122 * @author Rsl1122
*/ */
public class Payment { public class Payment implements Comparable<Payment> {
private final double amount; private final double amount;
private final String currency; private final String currency;
private final UUID uuid; private final UUID uuid;
private final String playerName; private final String playerName;
private final long date; 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.amount = amount;
this.currency = currency; this.currency = currency;
this.uuid = uuid; this.uuid = uuid;
this.playerName = playerName; this.playerName = playerName;
this.date = date; this.date = date;
this.packages = packages;
} }
public double getAmount() { public double getAmount() {
@ -46,4 +48,13 @@ public class Payment {
public UUID getUuid() { public UUID getUuid() {
return uuid; return uuid;
} }
public String getPackages() {
return packages;
}
@Override
public int compareTo(Payment o) {
return this.playerName.toLowerCase().compareTo(o.playerName.toLowerCase());
}
} }

View File

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