Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id 'java-library'
id 'maven-publish'
id "com.github.johnrengelman.shadow" version "8.1.1"
id "io.github.goooler.shadow" version "8.1.8"
}

repositories {
Expand Down Expand Up @@ -73,7 +73,11 @@ build.dependsOn(shadowJar)

group = 'org.metadevs.buycraftapi'
description = 'BuyCraftAPI'
java.sourceCompatibility = JavaVersion.VERSION_17
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}

publishing {
publications {
Expand Down
61 changes: 30 additions & 31 deletions src/main/java/org/metadevs/buycraftapi/BuyCraftAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import me.clip.placeholderapi.expansion.Taskable;
import me.clip.placeholderapi.metrics.bukkit.Metrics;
import me.clip.placeholderapi.metrics.charts.MultiLineChart;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.jetbrains.annotations.NotNull;
import org.metadevs.buycraftapi.config.ConfigManager;
import org.metadevs.buycraftapi.data.Request;
Expand All @@ -18,18 +17,19 @@
import org.metadevs.buycraftapi.providers.BuyCraftXProvider;
import org.metadevs.buycraftapi.providers.Provider;
import org.metadevs.buycraftapi.providers.TebexProvider;
import org.metadevs.buycraftapi.providers.IVaultProvider;
import org.metadevs.buycraftapi.providers.VaultSupport;
import org.metadevs.buycraftapi.tasks.Tasks;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;


@Getter
public class BuyCraftAPI extends PlaceholderExpansion implements Taskable, Configurable {

private Permission perms = null;
private IVaultProvider vaultSupport;
private Request request;
private Placeholders placeholdersManager;
private Query query;
Expand All @@ -45,15 +45,26 @@ private Provider getProvider() {
getLogger().log(Level.INFO, "Tebex found! Using it...");
return new TebexProvider();
} else {
throw new IllegalStateException("No supported plugin found");
return null;
}
}


@Override
public boolean canRegister() {
logger = Logger.getLogger("BuycraftAPI");
provider = getProvider();

try {
provider = getProvider();
} catch (Throwable e) {
logger.severe("Failed to initialize provider: " + e.getMessage());
return false;
}

if (provider == null) {
logger.severe("Plugin Tebex or BuycraftX not found! This expansion requires one of them to work.");
return false;
}

configManager = new ConfigManager(this);

final String key = provider.getKey();
Expand All @@ -66,17 +77,14 @@ public boolean canRegister() {
return true;
}

public @NotNull
String getAuthor() {
public @NotNull String getAuthor() {
return "AlexDev_";
}


public @NotNull String getIdentifier() {
return "buycraftapi";
}


public @NotNull String getVersion() {
try {
return getClass().getPackage().getImplementationVersion();
Expand All @@ -91,32 +99,18 @@ public String onPlaceholderRequest(Player p, @NotNull String identifier) {
return placeholdersManager.onPlaceholderRequest(p, identifier);
}


private void vaultHook() {
if (Bukkit.getPluginManager().isPluginEnabled("Vault")) {
if (setupPermissions()) {
getLogger().log(Level.INFO, "Successfully hooked into Vault for BuyCraftAPI v" + getVersion());
}
}
}


private boolean setupPermissions() {
try {
RegisteredServiceProvider<Permission> rsp = Bukkit.getServer().getServicesManager().getRegistration(Permission.class);
if (rsp == null) return false;
perms = rsp.getProvider();
return true;
} catch (Exception e) {
return false;
public String getGroup(OfflinePlayer player) {
if (vaultSupport != null && vaultSupport.isHooked()) {
return vaultSupport.getPrimaryGroup(player);
}
return "No Vault";
}

@Override
public void start() {
request = new Request(provider.getKey(), this);
query = new Query(this);

new Tasks(this, getPlaceholderAPI());

int pluginId = 10173;
Expand All @@ -129,8 +123,13 @@ public void start() {
return valueMap;
}));

vaultHook();
if (Bukkit.getPluginManager().isPluginEnabled("Vault")) {
vaultSupport = new VaultSupport(logger);
vaultSupport.setupPermissions();
}

placeholdersManager = new Placeholders(this);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,31 @@ public Placeholders(BuyCraftAPI buyCraftAPI) {
public String onPlaceholderRequest(Player p, @NotNull String identifier) {

if (identifier.equalsIgnoreCase("value_from_name")) {
if (p == null) return "Player is Offline";
if (p == null)
return "Player is Offline";
String value = query.getPlayerTotal(p.getName());
return value != null ? value : "0";
}


if (identifier.contains("vault_recent_name_")) {
int num;
try {
num = Integer.parseInt(identifier.replace("vault_recent_name_", ""));
} catch (NumberFormatException e) {
return "Invalid payment number. Put a number from 0 to " + (maxPayments - 1);
}
if (maxPayments == 0) return "Payments could not be found";
if (maxPayments == 0)
return "Payments could not be found";

if (num > maxPayments - 1 || num < 0)
return "Invalid payment number. Put a number from 0 to " + (maxPayments - 1);


if(Bukkit.isPrimaryThread()){
if (Bukkit.isPrimaryThread()) {
return "This placeholder is not supported on the main thread";
}


OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(query.getRecentPayment(num).getUuid());
return buyCraftAPI.getPerms().getPrimaryGroup(null, offlinePlayer);
return buyCraftAPI.getGroup(offlinePlayer);
}

if (identifier.contains("recent_name_")) {
Expand All @@ -63,40 +62,43 @@ public String onPlaceholderRequest(Player p, @NotNull String identifier) {
return "Invalid payment number. Put a number from 0 to " + (maxPayments - 1);
}

if (maxPayments == 0) return "Payments could not be found";
if (maxPayments == 0)
return "Payments could not be found";

if (num > maxPayments - 1 || num < 0)
return "0";
//return "Invalid payment number. Put a number from 0 to " + (maxPayments - 1);

// return "Invalid payment number. Put a number from 0 to " + (maxPayments - 1);

return query.getRecentPayment(num).getName();
}


if (identifier.contains("recent_currency_")) {
String replace = identifier.replace("recent_currency_", "");
if (query.isNotNumeric(replace)) return "Invalid number";
if (query.isNotNumeric(replace))
return "Invalid number";
int num = Integer.parseInt(replace);

if (maxPayments == 0) return "Payments could not be found";
if (maxPayments == 0)
return "Payments could not be found";
if (num > maxPayments - 1 || num < 0)
return "0";
//return "Error, Invalid number! You can put a number from 0 to " + (maxPayments - 1);

// return "Error, Invalid number! You can put a number from 0 to " +
// (maxPayments - 1);

return query.getRecentPayment(num).getCurrency();
}


if (identifier.contains("recent_amount_")) {
String replace = identifier.replace("recent_amount_", "");
if (query.isNotNumeric(replace)) return "Invalid number";
if (query.isNotNumeric(replace))
return "Invalid number";
int num = Integer.parseInt(replace);
if (maxPayments == 0) return "Payments could not be found";
if (maxPayments == 0)
return "Payments could not be found";
if (num > maxPayments - 1 || num < 0)
return "0";
//return "Error, Invalid number! You can put a number from 0 to " + (maxPayments - 1);
// return "Error, Invalid number! You can put a number from 0 to " +
// (maxPayments - 1);

return String.valueOf(Query.round(query.getRecentPayment(num).getAmount(), 2));
}
Expand All @@ -118,21 +120,25 @@ public String onPlaceholderRequest(Player p, @NotNull String identifier) {

if (identifier.contains("top_donor_global_name_")) {
String replace = identifier.replace("top_donor_global_name_", "");
if (query.isNotNumeric(replace)) return "Error, Invalid number";
if (query.isNotNumeric(replace))
return "Error, Invalid number";
int num = Integer.parseInt(replace);
TopValue payment = query.getTop(GLOBAL, num);
if (payment == null) return "";
if (payment == null)
return "";
else {
return payment.getName();
}
}

if (identifier.contains("top_donor_monthly_name_")) {
String replace = identifier.replace("top_donor_monthly_name_", "");
if (query.isNotNumeric(replace)) return "Error, Invalid number";
if (query.isNotNumeric(replace))
return "Error, Invalid number";
int num = Integer.parseInt(replace);
TopValue payment = query.getTop(MONTHLY, num);
if (payment == null) return "0";
if (payment == null)
return "0";
else {
return payment.getName();

Expand All @@ -141,10 +147,12 @@ public String onPlaceholderRequest(Player p, @NotNull String identifier) {

if (identifier.contains("top_donor_current_month_name_")) {
String replace = identifier.replace("top_donor_current_month_name_", "");
if (query.isNotNumeric(replace)) return "Error, Invalid number";
if (query.isNotNumeric(replace))
return "Error, Invalid number";
int num = Integer.parseInt(replace);
TopValue payment = query.getTop(CURRENT_MONTH, num);
if (payment == null) return "0";
if (payment == null)
return "0";
else {
return payment.getName();
}
Expand All @@ -165,81 +173,85 @@ public String onPlaceholderRequest(Player p, @NotNull String identifier) {
return getPrice(replace, CURRENT_MONTH);
}

if(identifier.equalsIgnoreCase("top_donor_global_name")){
if (identifier.equalsIgnoreCase("top_donor_global_name")) {
return query.getTopDonorName(GLOBAL);
}

if(identifier.equalsIgnoreCase("top_donor_monthly_name")){
if (identifier.equalsIgnoreCase("top_donor_monthly_name")) {
return query.getTopDonorName(MONTHLY);
}

if(identifier.equalsIgnoreCase("top_donor_current_month_name")){
if (identifier.equalsIgnoreCase("top_donor_current_month_name")) {
return query.getTopDonorName(CURRENT_MONTH);
}

if (identifier.equalsIgnoreCase("total_earnings_global")) {
double data = query.getAllMoneySpent(GLOBAL);
if (data == -1) return "Error";
if (data == -1)
return "Error";
else {
return String.format("%.2f", data);
}
}

if (identifier.equalsIgnoreCase("total_earnings_monthly")) {
double data = query.getAllMoneySpent(MONTHLY);
if (data == -1) return "Error";
if (data == -1)
return "Error";
else {
return String.format("%.2f", data);
}
}

if (identifier.equalsIgnoreCase("total_earnings_current_month")) {
double data = query.getAllMoneySpent(CURRENT_MONTH);
if (data == -1) return "Error";
if (data == -1)
return "Error";
else {
return String.format("%.2f", data);
}
}


if (identifier.equalsIgnoreCase("info")) {
return query.getPayments().size() + " payments";
}


if (identifier.equalsIgnoreCase("all")) { //only for test
if (identifier.equalsIgnoreCase("all")) { // only for test
StringBuilder sb = new StringBuilder();
for (Payment s : query.getPayments()) {
sb.append(s.getName()).append(" ").append(s.getAmount()).append("\n");
}
return sb.toString();
}


return null;
}

private String getVaultTop(String replace, Type currentMonth) {
if (query.isNotNumeric(replace)) return "Error, Invalid number";
if (query.isNotNumeric(replace))
return "Error, Invalid number";
int num = Integer.parseInt(replace);

if(Bukkit.isPrimaryThread()){
if (Bukkit.isPrimaryThread()) {
return "This placeholder is not supported on the main thread";
}

TopValue payment = query.getTop(currentMonth, num);
if (payment == null) return "0";
if (payment == null)
return "0";
else {
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(payment.getUuid());
return buyCraftAPI.getPerms().getPrimaryGroup(null, offlinePlayer);
return buyCraftAPI.getGroup(offlinePlayer);
}
}

private String getPrice(String replace, Type monthly) {
if (query.isNotNumeric(replace)) return "Error, Invalid number";
if (query.isNotNumeric(replace))
return "Error, Invalid number";
int num = Integer.parseInt(replace);
TopValue payment = query.getTop(monthly, num);
if (payment == null) return "Error";
if (payment == null)
return "Error";
else {
return String.format("%.2f", payment.getAmount());
}
Expand Down
Loading