Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- BuycraftAPI class:
- package com.faithfulmc.util.buycraft;
- import org.bukkit.craftbukkit.libs.com.google.gson.*;
- import java.io.*;
- public class BuycraftAPI
- {
- public static final String ENDPOINT = "https://plugin.buycraft.net";
- private final String secret;
- public BuycraftAPI(final String secret) {
- this.secret = secret;
- }
- public JsonElement buycraftGET(final String subURL) throws IOException {
- final JsonParser jsonParser;
- final BufferedReader bufferedReader;
- return new HTTPRequest("https://plugin.buycraft.net/" + subURL).open().requestMethod("GET").setRequestProperty("X-Buycraft-Secret", this.secret).disconnectAndReturn(self -> {
- jsonParser = new JsonParser();
- new BufferedReader(new InputStreamReader(self.getHttpURLConnection().getInputStream()));
- return jsonParser.parse((Reader)bufferedReader);
- });
- }
- public JsonElement buycraftPOST(final String subURL, final JsonObject json) throws IOException {
- final String jsonString = json.toString();
- System.out.println(jsonString);
- final HTTPRequest request = new HTTPRequest("https://plugin.buycraft.net/" + subURL);
- try {
- request.open().requestMethod("POST").setRequestProperty("X-Buycraft-Secret", this.secret).setRequestProperty("Content-Length", Integer.toString(jsonString.getBytes().length)).setRequestProperty("Content-Type", "application/json").doOutput(true).outputStream(outputStream -> new DataOutputStream(outputStream).writeBytes(jsonString));
- final JsonParser jsonParser;
- final BufferedReader bufferedReader;
- return request.disconnectAndReturn(self -> {
- jsonParser = new JsonParser();
- new BufferedReader(new InputStreamReader(self.getHttpURLConnection().getInputStream()));
- return jsonParser.parse((Reader)bufferedReader);
- });
- }
- catch (IOException exception) {
- final JsonParser jsonParser2;
- final BufferedReader bufferedReader2;
- return request.disconnectAndReturn(self -> {
- jsonParser2 = new JsonParser();
- new BufferedReader(new InputStreamReader(self.getHttpURLConnection().getErrorStream()));
- return jsonParser2.parse((Reader)bufferedReader2);
- });
- }
- }
- }
- HTTPRequest Class
- package com.faithfulmc.util.buycraft;
- import java.net.*;
- import java.io.*;
- import java.util.function.*;
- public class HTTPRequest
- {
- private final URL url;
- private HttpURLConnection httpURLConnection;
- public HTTPRequest(final String url) throws MalformedURLException {
- this.url = new URL(url);
- }
- public HTTPRequest open() throws IOException {
- this.httpURLConnection = (HttpURLConnection)this.url.openConnection();
- return this;
- }
- public HTTPRequest requestMethod(final String method) throws ProtocolException {
- this.httpURLConnection.setRequestMethod(method);
- return this;
- }
- public HTTPRequest setRequestProperty(final String property, final String value) {
- this.httpURLConnection.setRequestProperty(property, value);
- return this;
- }
- public HTTPRequest useCaches(final boolean useCaches) {
- this.httpURLConnection.setUseCaches(useCaches);
- return this;
- }
- public HTTPRequest doInput(final boolean doInput) {
- this.httpURLConnection.setDoInput(doInput);
- return this;
- }
- public HTTPRequest doOutput(final boolean doOutput) {
- this.httpURLConnection.setDoOutput(doOutput);
- return this;
- }
- public HTTPRequest outputStream(final HTTPRequestConsumer<OutputStream> function) throws IOException {
- function.accept(this.httpURLConnection.getOutputStream());
- return this;
- }
- public HTTPRequest inputStream(final HTTPRequestConsumer<InputStream> function) throws IOException {
- function.accept(this.httpURLConnection.getInputStream());
- return this;
- }
- public HTTPRequest errorStream(final Function<InputStream, ?> function) throws IOException {
- function.apply(this.httpURLConnection.getErrorStream());
- return this;
- }
- public HTTPRequest disconnect() {
- this.httpURLConnection.disconnect();
- return this;
- }
- public <T> T disconnectAndReturn(final HTTPRequestFunction<T> function) throws IOException {
- return function.apply(this);
- }
- public HttpURLConnection getHttpURLConnection() {
- return this.httpURLConnection;
- }
- @FunctionalInterface
- public interface HTTPRequestConsumer<T>
- {
- void accept(final T p0) throws IOException;
- }
- @FunctionalInterface
- public interface HTTPRequestFunction<T>
- {
- T apply(final HTTPRequest p0) throws IOException;
- }
- }
- Buycraft Framework Class
- package com.faithfulmc.framework.buycraft;
- import com.faithfulmc.util.buycraft.*;
- import com.faithfulmc.framework.buycraft.api.to.*;
- import com.faithfulmc.framework.buycraft.api.*;
- import java.io.*;
- import org.bukkit.craftbukkit.libs.com.google.gson.*;
- import java.util.*;
- import com.faithfulmc.framework.buycraft.api.from.*;
- import java.time.format.*;
- import java.time.temporal.*;
- import com.faithfulmc.framework.*;
- public class BuycraftFramework
- {
- public static final DateTimeFormatter DATE_FORMATTER;
- public static final BuycraftAPI INSTANCE;
- public static List<BuycraftCategory> CACHED_CATEGORIES;
- public static UUID getFromCompressed(final String string) {
- return UUID.fromString(string.replaceFirst("(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)", "$1-$2-$3-$4-$5"));
- }
- public static BuycraftPostResponse addManualPayment(final ManualPayment manualPayment) throws BuycraftException {
- final JsonObject jsonObject = manualPayment.toJSON();
- JsonElement returnObject;
- try {
- returnObject = BuycraftFramework.INSTANCE.buycraftPOST("payments", jsonObject);
- }
- catch (IOException exception) {
- throw new BuycraftException(exception);
- }
- return new BuycraftPostResponse((returnObject != null && returnObject.isJsonObject()) ? returnObject.getAsJsonObject() : null);
- }
- public static List<BuycraftCategory> getCachedCategories() throws BuycraftException {
- if (BuycraftFramework.CACHED_CATEGORIES != null) {
- return BuycraftFramework.CACHED_CATEGORIES;
- }
- BuycraftFramework.CACHED_CATEGORIES = new ArrayList<BuycraftCategory>();
- JsonElement returnObject;
- try {
- returnObject = BuycraftFramework.INSTANCE.buycraftGET("listing");
- }
- catch (IOException exception) {
- throw new BuycraftException(exception);
- }
- if (returnObject.isJsonObject()) {
- final JsonArray jsonArray = returnObject.getAsJsonObject().getAsJsonArray("categories");
- for (final JsonElement jsonElement : jsonArray) {
- BuycraftFramework.CACHED_CATEGORIES.add(new BuycraftCategory(jsonElement.getAsJsonObject()));
- }
- return BuycraftFramework.CACHED_CATEGORIES;
- }
- throw new BuycraftException(returnObject.toString());
- }
- public static List<BuycraftPayment> getPayments() throws BuycraftException {
- JsonArray returnObject;
- try {
- returnObject = BuycraftFramework.INSTANCE.buycraftGET("payments").getAsJsonArray();
- }
- catch (IOException exception) {
- throw new BuycraftException(exception);
- }
- final List<BuycraftPayment> payments = new ArrayList<BuycraftPayment>();
- for (final JsonElement jsonElement : returnObject) {
- payments.add(new BuycraftPayment(jsonElement.getAsJsonObject()));
- }
- return payments;
- }
- static {
- DATE_FORMATTER = new DateTimeFormatterBuilder().appendValue(ChronoField.YEAR).appendLiteral('-').appendValue(ChronoField.MONTH_OF_YEAR).appendLiteral('-').appendValue(ChronoField.DAY_OF_MONTH).appendLiteral('T').appendValue(ChronoField.HOUR_OF_DAY).appendLiteral(':').appendValue(ChronoField.MINUTE_OF_HOUR).toFormatter();
- INSTANCE = BasePlugin.getPlugin().getBuycraftAPI();
- BuycraftFramework.CACHED_CATEGORIES = null;
- }
- }
- BuycraftException Class
- package com.faithfulmc.framework.buycraft.api;
- public class BuycraftException extends Exception
- {
- public BuycraftException() {
- }
- public BuycraftException(final String message) {
- super(message);
- }
- public BuycraftException(final String message, final Throwable cause) {
- super(message, cause);
- }
- public BuycraftException(final Throwable cause) {
- super(cause);
- }
- public BuycraftException(final String message, final Throwable cause, final boolean enableSuppression, final boolean writableStackTrace) {
- super(message, cause, enableSuppression, writableStackTrace);
- }
- }
- Buycraft API TO Package -
- Buycraft Category Class:
- package com.faithfulmc.framework.buycraft.api.from;
- import org.bukkit.craftbukkit.libs.com.google.gson.*;
- import java.util.*;
- public class BuycraftCategory
- {
- private int id;
- private int order;
- private String name;
- private boolean only_subcategories;
- private List<BuycraftCategory> subcategories;
- private List<BuycraftPackage> packages;
- public BuycraftCategory(final int id, final int order, final String name, final boolean only_subcategories, final List<BuycraftCategory> subcategories, final List<BuycraftPackage> packages) {
- this.id = id;
- this.order = order;
- this.name = name;
- this.only_subcategories = only_subcategories;
- this.subcategories = subcategories;
- this.packages = packages;
- }
- public BuycraftCategory(final JsonObject jsonObject) {
- this.id = jsonObject.get("id").getAsInt();
- this.order = jsonObject.get("order").getAsInt();
- this.name = jsonObject.get("name").getAsString();
- this.only_subcategories = (jsonObject.has("only_subcategories") && jsonObject.get("only_subcategories").getAsBoolean());
- this.subcategories = new ArrayList<BuycraftCategory>();
- if (jsonObject.has("subcategories")) {
- for (final JsonElement jsonElement : jsonObject.getAsJsonArray("subcategories")) {
- this.subcategories.add(new BuycraftCategory(jsonElement.getAsJsonObject()));
- }
- }
- this.packages = new ArrayList<BuycraftPackage>();
- if (jsonObject.has("packages")) {
- for (final JsonElement jsonElement : jsonObject.getAsJsonArray("packages")) {
- this.packages.add(new BuycraftPackage(jsonElement.getAsJsonObject()));
- }
- }
- }
- public int getId() {
- return this.id;
- }
- public int getOrder() {
- return this.order;
- }
- public String getName() {
- return this.name;
- }
- public boolean isOnly_subcategories() {
- return this.only_subcategories;
- }
- public List<BuycraftCategory> getSubcategories() {
- return this.subcategories;
- }
- public List<BuycraftPackage> getPackages() {
- return this.packages;
- }
- public List<BuycraftPackage> getPackagesRecursively() {
- final List<BuycraftPackage> packages = new ArrayList<BuycraftPackage>();
- packages.addAll(this.packages);
- for (final BuycraftCategory subCategory : this.subcategories) {
- packages.addAll(subCategory.getPackagesRecursively());
- }
- return packages;
- }
- }
- Buycraft Currency Class:
- package com.faithfulmc.framework.buycraft.api.from;
- import org.bukkit.craftbukkit.libs.com.google.gson.*;
- public class BuycraftCurrency
- {
- private String iso_4217;
- private String symbol;
- public BuycraftCurrency(final String iso_4217, final String symbol) {
- this.iso_4217 = iso_4217;
- this.symbol = symbol;
- }
- public BuycraftCurrency(final JsonObject jsonObject) {
- this.iso_4217 = jsonObject.get("iso_4217").getAsString();
- this.symbol = jsonObject.get("symbol").getAsString();
- }
- public String getIso_4217() {
- return this.iso_4217;
- }
- public String getSymbol() {
- return this.symbol;
- }
- }
- Buycraft Package Class:
- package com.faithfulmc.framework.buycraft.api.from;
- import org.bukkit.craftbukkit.libs.com.google.gson.*;
- public class BuycraftPackage
- {
- private int id;
- private int order;
- private String name;
- private double price;
- private BuycraftSale sale;
- public BuycraftPackage(final int id, final int order, final String name, final double price, final BuycraftSale sale) {
- this.id = id;
- this.order = order;
- this.name = name;
- this.price = price;
- this.sale = sale;
- }
- public BuycraftPackage(final JsonObject jsonObject) {
- this.id = jsonObject.get("id").getAsInt();
- this.order = jsonObject.get("order").getAsInt();
- this.name = jsonObject.get("name").getAsString();
- this.price = jsonObject.get("price").getAsDouble();
- this.sale = new BuycraftSale(jsonObject.get("sale").getAsJsonObject());
- }
- public int getId() {
- return this.id;
- }
- public int getOrder() {
- return this.order;
- }
- public String getName() {
- return this.name;
- }
- public double getPrice() {
- return this.price;
- }
- public BuycraftSale getSale() {
- return this.sale;
- }
- }
- Buycraft Payment Class
- package com.faithfulmc.framework.buycraft.api.from;
- import java.util.*;
- import org.bukkit.craftbukkit.libs.com.google.gson.*;
- import com.faithfulmc.framework.buycraft.*;
- import java.time.*;
- public class BuycraftPayment
- {
- private int id;
- private double amount;
- private Date date;
- private BuycraftCurrency currency;
- private BuycraftPlayer player;
- public BuycraftPayment(final int id, final double amount, final Date date, final BuycraftCurrency currency, final BuycraftPlayer player) {
- this.id = id;
- this.amount = amount;
- this.date = date;
- this.currency = currency;
- this.player = player;
- }
- public BuycraftPayment(final JsonObject jsonObject) {
- this.id = jsonObject.get("id").getAsInt();
- this.amount = jsonObject.get("amount").getAsDouble();
- final String dateString = jsonObject.get("date").getAsString();
- final String dateTime = dateString.substring(0, dateString.length() - 9);
- this.date = Date.from(LocalDateTime.from(BuycraftFramework.DATE_FORMATTER.parse(dateTime)).atZone(ZoneId.of("UTC")).toInstant());
- this.currency = new BuycraftCurrency(jsonObject.get("currency").getAsJsonObject());
- this.player = new BuycraftPlayer(jsonObject.get("player").getAsJsonObject());
- }
- public int getId() {
- return this.id;
- }
- public double getAmount() {
- return this.amount;
- }
- public Date getDate() {
- return this.date;
- }
- public BuycraftCurrency getCurrency() {
- return this.currency;
- }
- public BuycraftPlayer getPlayer() {
- return this.player;
- }
- }
- Buycraft Player CLASS
- package com.faithfulmc.framework.buycraft.api.from;
- import java.util.*;
- import org.bukkit.craftbukkit.libs.com.google.gson.*;
- import com.faithfulmc.framework.buycraft.*;
- public class BuycraftPlayer
- {
- private int id;
- private String name;
- private UUID uuid;
- public BuycraftPlayer(final int id, final String name, final UUID uuid) {
- this.id = id;
- this.name = name;
- this.uuid = uuid;
- }
- public BuycraftPlayer(final JsonObject jsonObject) {
- this.id = jsonObject.get("id").getAsInt();
- this.name = jsonObject.get("name").getAsString();
- this.uuid = BuycraftFramework.getFromCompressed(jsonObject.get("uuid").getAsString());
- }
- public int getId() {
- return this.id;
- }
- public String getName() {
- return this.name;
- }
- public UUID getUuid() {
- return this.uuid;
- }
- }
- Buycraft Sale Class:
- package com.faithfulmc.framework.buycraft.api.from;
- import org.bukkit.craftbukkit.libs.com.google.gson.*;
- public class BuycraftSale
- {
- private boolean active;
- private double discount;
- public BuycraftSale(final boolean active, final double discount) {
- this.active = active;
- this.discount = discount;
- }
- public BuycraftSale(final JsonObject jsonObject) {
- this.active = jsonObject.get("active").getAsBoolean();
- this.discount = jsonObject.get("discount").getAsDouble();
- }
- public boolean isActive() {
- return this.active;
- }
- public double getDiscount() {
- return this.discount;
- }
- }
- Buycraft API to package -
- BuycraftPostResponse class:
- package com.faithfulmc.framework.buycraft.api.to;
- import org.bukkit.craftbukkit.libs.com.google.gson.*;
- public class BuycraftPostResponse
- {
- private Integer error_code;
- private String error_message;
- public BuycraftPostResponse(final Integer error_code, final String error_message) {
- this.error_code = error_code;
- this.error_message = error_message;
- }
- public BuycraftPostResponse(final JsonObject jsonObject) {
- if (jsonObject != null) {
- this.error_code = (jsonObject.has("error_code") ? jsonObject.get("error_code").getAsInt() : null);
- this.error_message = (jsonObject.has("error_message") ? jsonObject.get("error_message").getAsString() : null);
- }
- }
- public boolean isSuccess() {
- return this.error_code == null || this.error_code == 203;
- }
- public Integer getError_code() {
- return this.error_code;
- }
- public String getError_message() {
- return this.error_message;
- }
- }
- Manual Payment Class:
- package com.faithfulmc.framework.buycraft.api.to;
- import org.bukkit.craftbukkit.libs.com.google.gson.*;
- import java.util.*;
- public class ManualPayment
- {
- private String ign;
- private double price;
- private List<PaymentPackage> packages;
- public ManualPayment(final String ign, final double price, final List<PaymentPackage> packages) {
- this.ign = ign;
- this.price = price;
- this.packages = packages;
- }
- public String getIgn() {
- return this.ign;
- }
- public double getPrice() {
- return this.price;
- }
- public List<PaymentPackage> getPackages() {
- return this.packages;
- }
- public JsonObject toJSON() {
- final JsonObject jsonObject = new JsonObject();
- jsonObject.addProperty("ign", this.ign);
- jsonObject.addProperty("price", (Number)this.price);
- final JsonArray packageArray = new JsonArray();
- for (final PaymentPackage paymentPackage : this.packages) {
- packageArray.add((JsonElement)paymentPackage.toJSON());
- }
- jsonObject.add("packages", (JsonElement)packageArray);
- return jsonObject;
- }
- }
- Payment Package Class:
- package com.faithfulmc.framework.buycraft.api.to;
- import org.bukkit.craftbukkit.libs.com.google.gson.*;
- import java.util.*;
- public class PaymentPackage
- {
- private int id;
- private Map<String, Object> options;
- public PaymentPackage(final int id, final Map<String, Object> options) {
- this.id = id;
- this.options = options;
- }
- public int getId() {
- return this.id;
- }
- public Map<String, Object> getOptions() {
- return this.options;
- }
- public Double getPrice() {
- return this.options.get("price");
- }
- public Integer getServer() {
- return this.options.get("server");
- }
- public Integer getGlobal() {
- return this.options.get("global");
- }
- public JsonObject toJSON() {
- final JsonObject jsonObject = new JsonObject();
- jsonObject.addProperty("id", (Number)this.id);
- final JsonObject paymentOptionsObject = new JsonObject();
- for (final Map.Entry<String, Object> entry : this.options.entrySet()) {
- final String property = entry.getKey();
- final Object value = entry.getValue();
- if (value instanceof Number) {
- paymentOptionsObject.addProperty(property, (Number)value);
- }
- else if (value instanceof Boolean) {
- paymentOptionsObject.addProperty(property, (Boolean)value);
- }
- else {
- if (!(value instanceof String)) {
- throw new JsonParseException("Failed to serialize '" + property + "':" + "'" + value.toString() + "'");
- }
- paymentOptionsObject.addProperty(property, (String)value);
- }
- }
- jsonObject.add("options", (JsonElement)paymentOptionsObject);
- return jsonObject;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement