Skip to content

Commit

Permalink
💴💵💶 Multi-Currency Support 💶💵💴
Browse files Browse the repository at this point in the history
Took 3 hours 16 minutes
  • Loading branch information
kiranhart committed Aug 30, 2024
1 parent 09d2f11 commit 9ae1070
Show file tree
Hide file tree
Showing 39 changed files with 1,061 additions and 435 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

![logo](https://i.imgur.com/ENVi7Jn.png)

**The most feature packed, up to date auction plugin on Spigot. 1.8-1.18 support in a single jar.**
### Yes the code is pretty messy, working on cleaning it up. Stuff inside the api folder is the new object structures. Impl package is said interfaces, managers, model, and settings.settingsv3 are also the 'new' format. Pretty much structuring this project like my other updated ones (github/tweetzy)

**The most feature packed, up to date auction plugin on Spigot. 1.8\* -1.21 support in a single jar.**
**Auction House is the ultimate solution when it comes to server auctions. Unlike other auction plugins, auction house provides many features and is GUI based.**

**1.8 - 1.15.0 support is limited, features may or may not work. Use the latest version.**

<!-- Shields -->
[![Discord](https://img.shields.io/discord/821837624272617473?color=7289DA&label=Discord&logo=discord&logoColor=7289DA)](https://discord.tweetzy.ca)
<br>
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/ca/tweetzy/auctionhouse/AuctionHouse.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@

package ca.tweetzy.auctionhouse;

import ca.tweetzy.auctionhouse.api.AuctionHouseAPI;
import ca.tweetzy.auctionhouse.auction.AuctionedItem;
import ca.tweetzy.auctionhouse.commands.*;
import ca.tweetzy.auctionhouse.database.DataManager;
import ca.tweetzy.auctionhouse.database.migrations.*;
import ca.tweetzy.auctionhouse.database.migrations.v2.*;
import ca.tweetzy.auctionhouse.helpers.UpdateChecker;
import ca.tweetzy.auctionhouse.hooks.PlaceholderAPIHook;
import ca.tweetzy.auctionhouse.impl.AuctionAPI;
import ca.tweetzy.auctionhouse.listeners.*;
import ca.tweetzy.auctionhouse.managers.*;
import ca.tweetzy.auctionhouse.model.manager.BanManager;
Expand Down Expand Up @@ -97,6 +99,7 @@ public class AuctionHouse extends TweetyPlugin {
private final MinItemPriceManager minItemPriceManager = new MinItemPriceManager();
private final PaymentsManager paymentsManager = new PaymentsManager();

private AuctionHouseAPI API;

// the default vault economy
private Economy economy = null;
Expand Down Expand Up @@ -129,6 +132,7 @@ public void onPluginEnable() {
return;
}

API = new AuctionAPI();
taskChainFactory = BukkitTaskChainFactory.create(this);
migrationCoreConfig = new TweetzyYamlConfig(this, "migration-config-dont-touch.yml");

Expand Down Expand Up @@ -187,7 +191,8 @@ public void onPluginEnable() {
new _25_BidHistoryMigration(),
new _26_MultiSerAndCurrencyMigration(),
new _27_FixMigration25to26Migration(),
new _28_PriorityListingMigration()
new _28_PriorityListingMigration(),
new _29_PaymentMultiCurrencyMigration()
);

dataMigrationManager.runMigrations();
Expand All @@ -201,11 +206,6 @@ public void onPluginEnable() {
this.currencyManager.load();
this.paymentsManager.load();

// warn users if there is a mismatch between new economy and old
if (Settings.ECONOMY_PLUGIN.getString().toLowerCase().contains("ultraeconomy") && !Settings.CURRENCY_DEFAULT_SELECTED.getString().toLowerCase().contains("ultraeconomy")) {
getLogger().warning("You are seeing this message because there is a miss match between your old economy provider settings and the new one. While the plugin will default to Vault, if you are seeing this message it's likely because you were using UltraEconomy and you haven't set it up back under 'default selection' in the config.yml!");
}

// listeners
Bukkit.getServer().getPluginManager().registerEvents(new PlayerListeners(), this);
Bukkit.getServer().getPluginManager().registerEvents(new MeteorClientListeners(), this);
Expand Down Expand Up @@ -349,6 +349,10 @@ public static AuctionHouse getInstance() {
return (AuctionHouse) TweetyPlugin.getInstance();
}

public static AuctionHouseAPI getAPI() {
return getInstance().API;
}

public static DataManager getDataManager() {
return getInstance().dataManager;
}
Expand Down
107 changes: 38 additions & 69 deletions src/main/java/ca/tweetzy/auctionhouse/api/AuctionAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import ca.tweetzy.auctionhouse.AuctionHouse;
import ca.tweetzy.auctionhouse.auction.AuctionPayment;
import ca.tweetzy.auctionhouse.auction.AuctionedItem;
import ca.tweetzy.auctionhouse.auction.MinItemPrice;
import ca.tweetzy.auctionhouse.auction.enums.PaymentReason;
import ca.tweetzy.auctionhouse.settings.Settings;
Expand Down Expand Up @@ -53,8 +54,6 @@
import org.jetbrains.annotations.Nullable;

import java.io.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -84,32 +83,6 @@ public static AuctionAPI getInstance() {
return instance;
}

/**
* @param value a long number to be converted into a easily readable text
* @return a user friendly number to read
*/
public String getFriendlyNumber(double value) {
if (value <= 0) return "0";

int power;
String suffix = " KMBTQPEZY";
String formattedNumber = "";

try {
NumberFormat formatter = new DecimalFormat("#,###.#");
power = (int) StrictMath.log10(value);
value = value / (Math.pow(10, (power / 3) * 3));
formattedNumber = formatter.format(value);

formattedNumber = formattedNumber + suffix.charAt(power / 3);

} catch (StringIndexOutOfBoundsException e) {
return formatNumber(value);
}

return formattedNumber.length() > 4 ? formattedNumber.replaceAll("\\.[0-9]+", "") : formattedNumber;
}

/**
* Used to convert seconds to days, hours, minutes, and seconds
*
Expand Down Expand Up @@ -383,32 +356,6 @@ public boolean isValidTimeString(String sentence) {
return matcher.matches();
}

/**
* Used to format numbers with decimals and commas
*
* @param number is the number you want to format
* @return the formatted number string
*/
public String formatNumber(double number) {
String formatted = String.format(Settings.CURRENCY_FORMAT.getString(), number);//%,.2f
if (Settings.USE_SPACE_SEPARATOR_FOR_NUMBER.getBoolean())
formatted = formatted.replace(",", " ");

// do the zero drop here
// this is a bit scuffed, I gotta improve this
if (Settings.STRIP_ZEROS_ON_WHOLE_NUMBERS.getBoolean()) {
if (Double.parseDouble(formatted.replace(",", "")) % 1 == 0) {

formatted = formatted.replaceAll("0+$", "");
if (formatted.endsWith("."))
formatted = formatted.substring(0, formatted.length() - 1);
}
}

String preDecimal = Settings.USE_ALTERNATE_CURRENCY_FORMAT.getBoolean() ? replaceLast(formatted.replace(",", "."), ".", ",") : formatted;
return Settings.USE_FLAT_NUMBER_FORMAT.getBoolean() ? preDecimal.replace(".", "").replace(",", "") : preDecimal;
}

/**
* Used to replace the last portion of a string
*
Expand Down Expand Up @@ -756,47 +703,69 @@ else if (sub.startsWith("potato"))
return seconds;
}

public void withdrawBalance(OfflinePlayer player, double amount) {
private void handleWithdraw(OfflinePlayer player, double amount, AuctionedItem auctionedItem) {
if (auctionedItem.hasValidItemCurrency())
AuctionHouse.getCurrencyManager().withdraw(player, auctionedItem.getCurrencyItem(), (int) amount);
else {
final String[] currSplit = auctionedItem.getCurrency().split("/");
AuctionHouse.getCurrencyManager().withdraw(player, currSplit[0], currSplit[1], amount);
}
}

public void withdrawBalance(OfflinePlayer player, double amount, AuctionedItem auctionedItem) {
if (Settings.PAYMENT_HANDLE_USE_CMD.getBoolean()) {
AuctionHouse.newChain().sync(() -> {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), Settings.PAYMENT_HANDLE_WITHDRAW_CMD.getString().replace("%player%", player.getName()).replace("%price%", String.valueOf(amount)));
}).execute();
} else {
if (Settings.FORCE_SYNC_MONEY_ACTIONS.getBoolean())
AuctionHouse.newChain().sync(() -> AuctionHouse.getCurrencyManager().withdraw(player, amount)).execute();
else
AuctionHouse.getCurrencyManager().withdraw(player, amount);
AuctionHouse.newChain().sync(() -> handleWithdraw(player, amount, auctionedItem)).execute();
else {
handleWithdraw(player, amount, auctionedItem);
}
}
}

public void depositBalance(OfflinePlayer player, double amount, ItemStack item, OfflinePlayer paidFrom) {
public void depositBalance(OfflinePlayer player, double amount, ItemStack item, OfflinePlayer paidFrom, AuctionedItem auctionedItem) {
if (Settings.STORE_PAYMENTS_FOR_MANUAL_COLLECTION.getBoolean()) {
if (Settings.MANUAL_PAYMENTS_ONLY_FOR_OFFLINE_USERS.getBoolean()) {
if (!player.isOnline()) {
AuctionHouse.getDataManager().insertAuctionPayment(new AuctionPayment(player.getUniqueId(), amount, item, paidFrom.getName(), PaymentReason.ITEM_SOLD), null);
AuctionHouse.getDataManager().insertAuctionPayment(new AuctionPayment(player.getUniqueId(), amount, item, paidFrom.getName(), PaymentReason.ITEM_SOLD, auctionedItem.getCurrency(), auctionedItem.getCurrencyItem()), null);
} else {
initiatePayment(player, amount);
initiatePayment(player, amount, auctionedItem);
}
return;
}

AuctionHouse.getDataManager().insertAuctionPayment(new AuctionPayment(player.getUniqueId(), amount, item, paidFrom.getName(), PaymentReason.ITEM_SOLD), null);
AuctionHouse.getDataManager().insertAuctionPayment(new AuctionPayment(player.getUniqueId(), amount, item, paidFrom.getName(), PaymentReason.ITEM_SOLD, auctionedItem.getCurrency(), auctionedItem.getCurrencyItem()), null);
return;
}

initiatePayment(player, amount);
initiatePayment(player, amount, auctionedItem);
}

private void initiatePayment(OfflinePlayer player, double amount) {
private void initiatePayment(OfflinePlayer player, double amount, AuctionedItem auctionedItem) {
if (Settings.PAYMENT_HANDLE_USE_CMD.getBoolean()) {
AuctionHouse.newChain().sync(() -> {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), Settings.PAYMENT_HANDLE_DEPOSIT_CMD.getString().replace("%player%", player.getName()).replace("%price%", String.valueOf(amount)));
}).execute();
AuctionHouse.newChain().sync(() -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), Settings.PAYMENT_HANDLE_DEPOSIT_CMD.getString().replace("%player%", player.getName()).replace("%price%", String.valueOf(amount)))).execute();
} else {
if (Settings.FORCE_SYNC_MONEY_ACTIONS.getBoolean())
AuctionHouse.newChain().sync(() -> AuctionHouse.getCurrencyManager().deposit(player, amount)).execute();
AuctionHouse.newChain().sync(() -> handleDeposit(player, amount, auctionedItem)).execute();
else {
handleDeposit(player, amount, auctionedItem);
}
}
}

private void handleDeposit(OfflinePlayer player, double amount, AuctionedItem auctionedItem) {
if (auctionedItem.hasValidItemCurrency())
if (player.isOnline())
AuctionHouse.getCurrencyManager().deposit(player, auctionedItem.getCurrencyItem(), (int) amount);
else
AuctionHouse.getCurrencyManager().deposit(player, amount);
AuctionHouse.getDataManager().insertAuctionPayment(new AuctionPayment(player.getUniqueId(), amount, auctionedItem.getItem(), AuctionHouse.getInstance().getLocale().getMessage("general.prefix").getMessage(), PaymentReason.ITEM_SOLD, auctionedItem.getCurrency(), auctionedItem.getCurrencyItem()), null);

else {
final String[] currSplit = auctionedItem.getCurrency().split("/");
AuctionHouse.getCurrencyManager().deposit(player, currSplit[0], currSplit[1], amount);
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/ca/tweetzy/auctionhouse/api/AuctionHouseAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,22 @@

package ca.tweetzy.auctionhouse.api;

import org.bukkit.inventory.ItemStack;

public interface AuctionHouseAPI {

/*
----------------------------------------------------------------
CURRENCY / NUMBER / DATE RELATED STUFF
----------------------------------------------------------------
*/
String getNumberAsCurrency(final double number, boolean hideSymbol);

String getNumberAsCurrency(final double number);

String getAbbreviatedNumber(final double number, boolean hideSymbol);

String getAbbreviatedNumber(final double number);

String getFinalizedCurrencyNumber(final double number, final String currency, final ItemStack currencyItem);
}
24 changes: 18 additions & 6 deletions src/main/java/ca/tweetzy/auctionhouse/auction/AuctionPayment.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
package ca.tweetzy.auctionhouse.auction;

import ca.tweetzy.auctionhouse.AuctionHouse;
import ca.tweetzy.auctionhouse.api.AuctionAPI;
import ca.tweetzy.auctionhouse.auction.enums.PaymentReason;
import ca.tweetzy.flight.comp.enums.CompMaterial;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.bukkit.entity.Player;
Expand All @@ -38,17 +38,29 @@ public final class AuctionPayment {
private final String fromName;
private final PaymentReason reason;


private final double amount;
private final long time;

public AuctionPayment(UUID to, double amount, ItemStack item, String fromName, PaymentReason reason) {
this(UUID.randomUUID(), to, item, fromName, reason, amount, System.currentTimeMillis());
private final String currency;
private final ItemStack currencyItem;

public AuctionPayment(UUID to, double amount, ItemStack item, String fromName, PaymentReason reason, String currency, ItemStack currencyItem) {
this(UUID.randomUUID(), to, item, fromName, reason, amount, System.currentTimeMillis(), currency, currencyItem);
}


public void pay(Player player) {
AuctionHouse.getCurrencyManager().deposit(player, this.amount);
AuctionHouse.getInstance().getLocale().getMessage("pricing.moneyadd").processPlaceholder("player_balance", AuctionAPI.getInstance().formatNumber(AuctionHouse.getCurrencyManager().getBalance(player))).processPlaceholder("price", AuctionAPI.getInstance().formatNumber(this.amount)).sendPrefixedMessage(player);
final String[] currSplit = currency.split("/");

if (currencyItem != null && currencyItem.getType() != CompMaterial.AIR.parseMaterial()) {
AuctionHouse.getCurrencyManager().deposit(player, currencyItem, (int) this.amount);
} else {
AuctionHouse.getCurrencyManager().deposit(player, currSplit[0], currSplit[1], this.amount);
}

AuctionHouse.getInstance().getLocale().getMessage("pricing.moneyadd")
.processPlaceholder("player_balance", AuctionHouse.getAPI().getNumberAsCurrency(AuctionHouse.getCurrencyManager().getBalance(player)))
.processPlaceholder("price", AuctionHouse.getAPI().getFinalizedCurrencyNumber(this.amount, this.currency, this.currencyItem))
.sendPrefixedMessage(player);
}
}
Loading

0 comments on commit 9ae1070

Please sign in to comment.