This commit is contained in:
BenceX100 2024-12-11 19:14:15 +01:00
parent f053cde0e8
commit be34f66a48
20 changed files with 355 additions and 124 deletions

View File

@ -6,7 +6,7 @@
<groupId>com.artillexstudios</groupId>
<artifactId>AxTrade</artifactId>
<version>1.11.0</version>
<version>1.12.0</version>
<packaging>jar</packaging>
<name>AxTrade</name>

View File

@ -0,0 +1,64 @@
package com.artillexstudios.axtrade.currency;
import com.artillexstudios.axtrade.hooks.currency.CurrencyHook;
import org.bukkit.entity.Player;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
public class CurrencyProcessor {
private final Player player;
private final Set<Map.Entry<CurrencyHook, Double>> currencies;
private final Map<Map.Entry<CurrencyHook, Double>, Boolean> states = new HashMap<>();
public CurrencyProcessor(Player player, Set<Map.Entry<CurrencyHook, Double>> currencies) {
this.player = player;
this.currencies = currencies;
}
public CompletableFuture<Boolean> run() {
CompletableFuture<Boolean>[] futures = new CompletableFuture[currencies.size()];
int i = 0;
for (Map.Entry<CurrencyHook, Double> entry : currencies) {
futures[i] = entry.getKey().takeBalance(player.getUniqueId(), entry.getValue()); // try taking money, generate futures
i++;
}
// run all futures
CompletableFuture<Boolean> success = CompletableFuture.allOf(futures).thenApply(unused -> {
boolean[] actions = new boolean[futures.length];
boolean anyErrors = false;
for (int j = 0; j < futures.length; j++) {
actions[j] = futures[j].join(); // store currency take results
if (!actions[j]) anyErrors = true; // if any currency take failed, store
}
// used if reversing is needed to give back the currency
int j = 0;
for (Map.Entry<CurrencyHook, Double> entry : currencies) {
states.put(entry, actions[j]); // map of currency, amount and did they succeed?
j++;
}
if (anyErrors) { // if there any fails, reverse process and return fail
reverse(); // todo: use return value maybe
}
return !anyErrors; // if failed, return false, otherwise true
});
return success;
}
public CompletableFuture<Boolean> reverse() {
for (Map.Entry<Map.Entry<CurrencyHook, Double>, Boolean> entry : states.entrySet()) {
if (!entry.getValue()) continue; // if failed to send, don't give back
entry.getKey().getKey().giveBalance(player.getUniqueId(), entry.getKey().getValue());
}
return null; // todo: return a future (maybe there is no point tho)
}
}

View File

@ -4,6 +4,7 @@ import com.artillexstudios.axquestboard.api.AxQuestBoardAPI;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import static com.artillexstudios.axtrade.AxTrade.HOOKS;
@ -44,12 +45,18 @@ public class AxQuestBoardHook implements CurrencyHook {
}
@Override
public void giveBalance(@NotNull UUID player, double amount) {
public CompletableFuture<Boolean> giveBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
AxQuestBoardAPI.modifyPoints(player, (int) amount);
cf.complete(true);
return cf;
}
@Override
public void takeBalance(@NotNull UUID player, double amount) {
public CompletableFuture<Boolean> takeBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
AxQuestBoardAPI.modifyPoints(player, (int) amount * -1);
cf.complete(true);
return cf;
}
}

View File

@ -5,6 +5,7 @@ import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import static com.artillexstudios.axtrade.AxTrade.HOOKS;
@ -45,12 +46,18 @@ public class BeastTokensHook implements CurrencyHook {
}
@Override
public void giveBalance(@NotNull UUID player, double amount) {
public CompletableFuture<Boolean> giveBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
BeastTokensAPI.getTokensManager().addTokens(Bukkit.getOfflinePlayer(player), amount);
cf.complete(true);
return cf;
}
@Override
public void takeBalance(@NotNull UUID player, double amount) {
public CompletableFuture<Boolean> takeBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
BeastTokensAPI.getTokensManager().removeTokens(Bukkit.getOfflinePlayer(player), amount);
cf.complete(true);
return cf;
}
}

View File

@ -7,6 +7,7 @@ import su.nightexpress.coinsengine.api.CoinsEngineAPI;
import su.nightexpress.coinsengine.api.currency.Currency;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class CoinsEngineHook implements CurrencyHook {
private Currency currency = null;
@ -58,14 +59,24 @@ public class CoinsEngineHook implements CurrencyHook {
}
@Override
public void giveBalance(@NotNull UUID player, double amount) {
if (currency == null) return;
public CompletableFuture<Boolean> giveBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
if (currency == null) {
cf.complete(false);
return cf;
}
CoinsEngineAPI.addBalance(Bukkit.getPlayer(player), currency, amount);
return cf;
}
@Override
public void takeBalance(@NotNull UUID player, double amount) {
if (currency == null) return;
public CompletableFuture<Boolean> takeBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
if (currency == null) {
cf.complete(false);
return cf;
}
CoinsEngineAPI.removeBalance(Bukkit.getPlayer(player), currency, amount);
return cf;
}
}

View File

@ -3,6 +3,7 @@ package com.artillexstudios.axtrade.hooks.currency;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public interface CurrencyHook {
void setup();
@ -19,7 +20,7 @@ public interface CurrencyHook {
double getBalance(@NotNull UUID player);
void giveBalance(@NotNull UUID player, double amount);
CompletableFuture<Boolean> giveBalance(@NotNull UUID player, double amount);
void takeBalance(@NotNull UUID player, double amount);
CompletableFuture<Boolean> takeBalance(@NotNull UUID player, double amount);
}

View File

@ -9,6 +9,7 @@ import org.jetbrains.annotations.NotNull;
import java.math.BigDecimal;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class EcoBitsHook implements CurrencyHook {
private Currency currency = null;
@ -60,14 +61,26 @@ public class EcoBitsHook implements CurrencyHook {
}
@Override
public void giveBalance(@NotNull UUID player, double amount) {
if (currency == null) return;
public CompletableFuture<Boolean> giveBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
if (currency == null) {
cf.complete(false);
return cf;
}
CurrencyUtils.adjustBalance(Bukkit.getOfflinePlayer(player), currency, BigDecimal.valueOf(amount));
cf.complete(true);
return cf;
}
@Override
public void takeBalance(@NotNull UUID player, double amount) {
if (currency == null) return;
public CompletableFuture<Boolean> takeBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
if (currency == null) {
cf.complete(false);
return cf;
}
CurrencyUtils.adjustBalance(Bukkit.getOfflinePlayer(player), currency, BigDecimal.valueOf(amount * -1));
cf.complete(true);
return cf;
}
}

View File

@ -5,6 +5,7 @@ import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import static com.artillexstudios.axtrade.AxTrade.HOOKS;
@ -46,15 +47,21 @@ public class ExperienceHook implements CurrencyHook {
}
@Override
public void giveBalance(@NotNull UUID player, double amount) {
public CompletableFuture<Boolean> giveBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
final Player pl = Bukkit.getPlayer(player);
changeExp(pl, (int) amount);
cf.complete(true);
return cf;
}
@Override
public void takeBalance(@NotNull UUID player, double amount) {
public CompletableFuture<Boolean> takeBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
final Player pl = Bukkit.getPlayer(player);
changeExp(pl, (int) (amount * -1));
cf.complete(true);
return cf;
}
// credit: https://gist.githubusercontent.com/Jikoo/30ec040443a4701b8980/raw/0745ca25a8aaaf749ba2f2164a809e998f6a37c4/Experience.java

View File

@ -4,6 +4,7 @@ import org.jetbrains.annotations.NotNull;
import org.kingdoms.constants.player.KingdomPlayer;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import static com.artillexstudios.axtrade.AxTrade.HOOKS;
@ -46,14 +47,20 @@ public class KingdomsXHook implements CurrencyHook {
}
@Override
public void giveBalance(@NotNull UUID player, double amount) {
public CompletableFuture<Boolean> giveBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
final KingdomPlayer kingdomPlayer = KingdomPlayer.getKingdomPlayer(player);
kingdomPlayer.getKingdom().addResourcePoints((long) amount);
cf.complete(true);
return cf;
}
@Override
public void takeBalance(@NotNull UUID player, double amount) {
public CompletableFuture<Boolean> takeBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
final KingdomPlayer kingdomPlayer = KingdomPlayer.getKingdomPlayer(player);
kingdomPlayer.getKingdom().addResourcePoints((long) (amount * -1));
cf.complete(true);
return cf;
}
}

View File

@ -8,6 +8,7 @@ import org.jetbrains.annotations.NotNull;
import java.text.DecimalFormat;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class PlaceholderCurrencyHook implements CurrencyHook {
private final String name;
@ -57,23 +58,33 @@ public class PlaceholderCurrencyHook implements CurrencyHook {
}
@Override
public void giveBalance(@NotNull UUID player, double amount) {
public CompletableFuture<Boolean> giveBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
final OfflinePlayer pl = Bukkit.getOfflinePlayer(player);
if (pl.getName() == null) return;
if (pl.getName() == null) {
cf.complete(false);
return cf;
}
final String placeholder = section.getString("settings.give-command")
.replace("%amount%", parseNumber(amount))
.replace("%player%", pl.getName());
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), placeholder);
cf.complete(Bukkit.dispatchCommand(Bukkit.getConsoleSender(), placeholder));
return cf;
}
@Override
public void takeBalance(@NotNull UUID player, double amount) {
public CompletableFuture<Boolean> takeBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
final OfflinePlayer pl = Bukkit.getOfflinePlayer(player);
if (pl.getName() == null) return;
if (pl.getName() == null) {
cf.complete(false);
return cf;
}
final String placeholder = section.getString("settings.take-command")
.replace("%amount%", parseNumber(amount))
.replace("%player%", pl.getName());
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), placeholder);
cf.complete(Bukkit.dispatchCommand(Bukkit.getConsoleSender(), placeholder));
return cf;
}
private String parseNumber(double amount) {

View File

@ -5,6 +5,7 @@ import org.black_ixx.playerpoints.PlayerPointsAPI;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import static com.artillexstudios.axtrade.AxTrade.HOOKS;
@ -47,12 +48,16 @@ public class PlayerPointsHook implements CurrencyHook {
}
@Override
public void giveBalance(@NotNull UUID player, double amount) {
econ.give(player, (int) amount);
public CompletableFuture<Boolean> giveBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
cf.complete(econ.give(player, (int) amount));
return cf;
}
@Override
public void takeBalance(@NotNull UUID player, double amount) {
econ.take(player, (int) Math.round(amount));
public CompletableFuture<Boolean> takeBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
cf.complete(econ.take(player, (int) Math.round(amount)));
return cf;
}
}

View File

@ -3,10 +3,12 @@ package com.artillexstudios.axtrade.hooks.currency;
import com.artillexstudios.axapi.utils.StringUtils;
import dev.unnm3d.rediseconomy.api.RedisEconomyAPI;
import dev.unnm3d.rediseconomy.currency.Currency;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class RedisEconomyHook implements CurrencyHook {
private Currency currency = null;
@ -59,14 +61,26 @@ public class RedisEconomyHook implements CurrencyHook {
}
@Override
public void giveBalance(@NotNull UUID player, double amount) {
if (currency == null) return;
currency.depositPlayer(player, null, amount, null);
public CompletableFuture<Boolean> giveBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
if (currency == null) {
cf.complete(false);
return cf;
}
EconomyResponse economyResponse = currency.depositPlayer(player, null, amount, null);
cf.complete(economyResponse.transactionSuccess());
return cf;
}
@Override
public void takeBalance(@NotNull UUID player, double amount) {
if (currency == null) return;
currency.withdrawPlayer(player, null, amount, null);
public CompletableFuture<Boolean> takeBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
if (currency == null) {
cf.complete(false);
return cf;
}
EconomyResponse economyResponse = currency.withdrawPlayer(player, null, amount, null);
cf.complete(economyResponse.transactionSuccess());
return cf;
}
}

View File

@ -5,6 +5,7 @@ import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import static com.artillexstudios.axtrade.AxTrade.HOOKS;
@ -45,12 +46,18 @@ public class RivalHarvesterHoesHook implements CurrencyHook {
}
@Override
public void giveBalance(@NotNull UUID player, double amount) {
public CompletableFuture<Boolean> giveBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
Main.instance.getEconomy().giveEconomyAmount(Bukkit.getOfflinePlayer(player), amount);
cf.complete(true);
return cf;
}
@Override
public void takeBalance(@NotNull UUID player, double amount) {
public CompletableFuture<Boolean> takeBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
Main.instance.getEconomy().removeEconomyAmount(Bukkit.getOfflinePlayer(player), amount);
cf.complete(true);
return cf;
}
}

View File

@ -4,6 +4,7 @@ import me.qKing12.RoyaleEconomy.RoyaleEconomy;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import static com.artillexstudios.axtrade.AxTrade.HOOKS;
@ -44,12 +45,18 @@ public class RoyaleEconomyHook implements CurrencyHook {
}
@Override
public void giveBalance(@NotNull UUID player, double amount) {
public CompletableFuture<Boolean> giveBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
RoyaleEconomy.apiHandler.balance.addBalance(player.toString(), amount);
cf.complete(true);
return cf;
}
@Override
public void takeBalance(@NotNull UUID player, double amount) {
public CompletableFuture<Boolean> takeBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
RoyaleEconomy.apiHandler.balance.removeBalance(player.toString(), amount);
cf.complete(true);
return cf;
}
}

View File

@ -5,6 +5,7 @@ import me.swanis.mobcoins.profile.Profile;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import static com.artillexstudios.axtrade.AxTrade.HOOKS;
@ -45,14 +46,20 @@ public class SuperMobCoinsHook implements CurrencyHook {
}
@Override
public void giveBalance(@NotNull UUID player, double amount) {
public CompletableFuture<Boolean> giveBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
final Profile profile = MobCoinsAPI.getProfileManager().getProfile(player);
profile.setMobCoins((long) (profile.getMobCoins() + amount));
cf.complete(true);
return cf;
}
@Override
public void takeBalance(@NotNull UUID player, double amount) {
public CompletableFuture<Boolean> takeBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
final Profile profile = MobCoinsAPI.getProfileManager().getProfile(player);
profile.setMobCoins((long) (profile.getMobCoins() - amount));
cf.complete(true);
return cf;
}
}

View File

@ -5,6 +5,7 @@ import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import static com.artillexstudios.axtrade.AxTrade.HOOKS;
@ -46,14 +47,26 @@ public class TheOnlyMobCoins implements CurrencyHook {
}
@Override
public void giveBalance(@NotNull UUID player, double amount) {
if (MobCoinsAPI.getPlayerData(Bukkit.getPlayer(player)) == null) return;
public CompletableFuture<Boolean> giveBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
if (MobCoinsAPI.getPlayerData(Bukkit.getPlayer(player)) == null) {
cf.complete(false);
return cf;
}
MobCoinsAPI.getPlayerData(Bukkit.getPlayer(player)).addCoins(amount);
cf.complete(true);
return cf;
}
@Override
public void takeBalance(@NotNull UUID player, double amount) {
if (MobCoinsAPI.getPlayerData(Bukkit.getPlayer(player)) == null) return;
public CompletableFuture<Boolean> takeBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
if (MobCoinsAPI.getPlayerData(Bukkit.getPlayer(player)) == null) {
cf.complete(false);
return cf;
}
MobCoinsAPI.getPlayerData(Bukkit.getPlayer(player)).reduceCoins(amount);
cf.complete(true);
return cf;
}
}

View File

@ -5,6 +5,7 @@ import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import static com.artillexstudios.axtrade.AxTrade.HOOKS;
@ -47,12 +48,16 @@ public class TokenManagerHook implements CurrencyHook {
}
@Override
public void giveBalance(@NotNull UUID player, double amount) {
eco.addTokens(Bukkit.getPlayer(player), (long) amount);
public CompletableFuture<Boolean> giveBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
cf.complete(eco.addTokens(Bukkit.getPlayer(player), (long) amount));
return cf;
}
@Override
public void takeBalance(@NotNull UUID player, double amount) {
eco.removeTokens(Bukkit.getPlayer(player), (long) amount);
public CompletableFuture<Boolean> takeBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
cf.complete(eco.removeTokens(Bukkit.getPlayer(player), (long) amount));
return cf;
}
}

View File

@ -9,6 +9,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class UltraEconomyHook implements CurrencyHook {
private Currency currency = null;
@ -63,18 +64,34 @@ public class UltraEconomyHook implements CurrencyHook {
}
@Override
public void giveBalance(@NotNull UUID player, double amount) {
if (currency == null) return;
public CompletableFuture<Boolean> giveBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
if (currency == null) {
cf.complete(false);
return cf;
}
final Optional<Account> account = UltraEconomy.getAPI().getAccounts().uuid(player);
if (account.isEmpty()) return;
account.get().addBalance(currency, amount);
if (account.isEmpty()) {
cf.complete(false);
return cf;
}
cf.complete(account.get().addBalance(currency, amount));
return cf;
}
@Override
public void takeBalance(@NotNull UUID player, double amount) {
if (currency == null) return;
public CompletableFuture<Boolean> takeBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
if (currency == null) {
cf.complete(false);
return cf;
}
final Optional<Account> account = UltraEconomy.getAPI().getAccounts().uuid(player);
if (account.isEmpty()) return;
account.get().removeBalance(currency, amount);
if (account.isEmpty()) {
cf.complete(false);
return cf;
}
cf.complete(account.get().removeBalance(currency, amount));
return cf;
}
}

View File

@ -1,11 +1,13 @@
package com.artillexstudios.axtrade.hooks.currency;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import static com.artillexstudios.axtrade.AxTrade.HOOKS;
@ -51,12 +53,18 @@ public class VaultHook implements CurrencyHook {
}
@Override
public void giveBalance(@NotNull UUID player, double amount) {
econ.depositPlayer(Bukkit.getOfflinePlayer(player), amount);
public CompletableFuture<Boolean> giveBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
EconomyResponse response = econ.depositPlayer(Bukkit.getOfflinePlayer(player), amount);
cf.complete(response.transactionSuccess());
return cf;
}
@Override
public void takeBalance(@NotNull UUID player, double amount) {
econ.withdrawPlayer(Bukkit.getOfflinePlayer(player), amount);
public CompletableFuture<Boolean> takeBalance(@NotNull UUID player, double amount) {
CompletableFuture<Boolean> cf = new CompletableFuture<>();
EconomyResponse response = econ.withdrawPlayer(Bukkit.getOfflinePlayer(player), amount);
cf.complete(response.transactionSuccess());
return cf;
}
}

View File

@ -2,6 +2,7 @@ package com.artillexstudios.axtrade.trade;
import com.artillexstudios.axapi.scheduler.Scheduler;
import com.artillexstudios.axapi.utils.ContainerUtils;
import com.artillexstudios.axtrade.currency.CurrencyProcessor;
import com.artillexstudios.axtrade.hooks.currency.CurrencyHook;
import com.artillexstudios.axtrade.utils.HistoryUtils;
import com.artillexstudios.axtrade.utils.NumberUtils;
@ -53,12 +54,15 @@ public class Trade {
SoundUtils.playSound(player1.getPlayer(), "aborted");
SoundUtils.playSound(player2.getPlayer(), "aborted");
end();
close();
}
public void end() {
if (ended) return;
ended = true;
Scheduler.get().run(scheduledTask -> Trades.removeTrade(Trade.this));
}
public void close() {
Scheduler.get().run(scheduledTask -> Trades.removeTrade(this));
player1.getPlayer().closeInventory();
player1.getPlayer().updateInventory();
player2.getPlayer().closeInventory();
@ -80,72 +84,88 @@ public class Trade {
}
}
MESSAGEUTILS.sendLang(player1.getPlayer(), "trade.completed", Map.of("%player%", player2.getPlayer().getName()));
MESSAGEUTILS.sendLang(player2.getPlayer(), "trade.completed", Map.of("%player%", player1.getPlayer().getName()));
SoundUtils.playSound(player1.getPlayer(), "completed");
SoundUtils.playSound(player2.getPlayer(), "completed");
List<String> player1Currencies = new ArrayList<>();
for (Map.Entry<CurrencyHook, Double> entry : player1.getCurrencies().entrySet()) {
entry.getKey().takeBalance(player1.getPlayer().getUniqueId(), entry.getValue());
entry.getKey().giveBalance(player2.getPlayer().getUniqueId(), entry.getValue());
String currencyName = Utils.getFormattedCurrency(entry.getKey());
String currencyAm = NumberUtils.formatNumber(entry.getValue());
player1Currencies.add(currencyName + ": " + currencyAm);
if (CONFIG.getBoolean("enable-trade-summaries")) {
MESSAGEUTILS.sendFormatted(player2.getPlayer(), LANG.getString("summary.get.currency"), Map.of("%amount%", currencyAm, "%currency%", currencyName));
MESSAGEUTILS.sendFormatted(player1.getPlayer(), LANG.getString("summary.give.currency"), Map.of("%amount%", currencyAm, "%currency%", currencyName));
close();
CurrencyProcessor currencyProcessor1 = new CurrencyProcessor(player1.getPlayer(), player1.getCurrencies().entrySet());
currencyProcessor1.run().thenAccept(success1 -> {
if (!success1) {
abort();
return;
}
}
List<String> player2Currencies = new ArrayList<>();
for (Map.Entry<CurrencyHook, Double> entry : player2.getCurrencies().entrySet()) {
entry.getKey().takeBalance(player2.getPlayer().getUniqueId(), entry.getValue());
entry.getKey().giveBalance(player1.getPlayer().getUniqueId(), entry.getValue());
String currencyName = Utils.getFormattedCurrency(entry.getKey());
String currencyAm = NumberUtils.formatNumber(entry.getValue());
player2Currencies.add(currencyName + ": " + currencyAm);
if (CONFIG.getBoolean("enable-trade-summaries")) {
MESSAGEUTILS.sendFormatted(player2.getPlayer(), LANG.getString("summary.give.currency"), Map.of("%amount%", currencyAm, "%currency%", currencyName));
MESSAGEUTILS.sendFormatted(player1.getPlayer(), LANG.getString("summary.get.currency"), Map.of("%amount%", currencyAm, "%currency%", currencyName));
}
}
CurrencyProcessor currencyProcessor2 = new CurrencyProcessor(player2.getPlayer(), player2.getCurrencies().entrySet());
currencyProcessor2.run().thenAccept(success2 -> {
if (!success2) {
abort();
currencyProcessor1.reverse();
return;
}
List<String> player1Items = new ArrayList<>();
player1.getTradeGui().getItems().forEach(itemStack -> {
if (itemStack == null) return;
Scheduler.get().runAt(player2.getPlayer().getLocation(), task -> {
ContainerUtils.INSTANCE.addOrDrop(player2.getPlayer().getInventory(), List.of(itemStack), player2.getPlayer().getLocation());
end();
MESSAGEUTILS.sendLang(player1.getPlayer(), "trade.completed", Map.of("%player%", player2.getPlayer().getName()));
MESSAGEUTILS.sendLang(player2.getPlayer(), "trade.completed", Map.of("%player%", player1.getPlayer().getName()));
SoundUtils.playSound(player1.getPlayer(), "completed");
SoundUtils.playSound(player2.getPlayer(), "completed");
List<String> player1Currencies = new ArrayList<>();
for (Map.Entry<CurrencyHook, Double> entry : player1.getCurrencies().entrySet()) {
entry.getKey().giveBalance(player2.getPlayer().getUniqueId(), entry.getValue());
String currencyName = Utils.getFormattedCurrency(entry.getKey());
String currencyAm = NumberUtils.formatNumber(entry.getValue());
player1Currencies.add(currencyName + ": " + currencyAm);
if (CONFIG.getBoolean("enable-trade-summaries")) {
MESSAGEUTILS.sendFormatted(player2.getPlayer(), LANG.getString("summary.get.currency"), Map.of("%amount%", currencyAm, "%currency%", currencyName));
MESSAGEUTILS.sendFormatted(player1.getPlayer(), LANG.getString("summary.give.currency"), Map.of("%amount%", currencyAm, "%currency%", currencyName));
}
}
List<String> player2Currencies = new ArrayList<>();
for (Map.Entry<CurrencyHook, Double> entry : player2.getCurrencies().entrySet()) {
entry.getKey().giveBalance(player1.getPlayer().getUniqueId(), entry.getValue());
String currencyName = Utils.getFormattedCurrency(entry.getKey());
String currencyAm = NumberUtils.formatNumber(entry.getValue());
player2Currencies.add(currencyName + ": " + currencyAm);
if (CONFIG.getBoolean("enable-trade-summaries")) {
MESSAGEUTILS.sendFormatted(player2.getPlayer(), LANG.getString("summary.give.currency"), Map.of("%amount%", currencyAm, "%currency%", currencyName));
MESSAGEUTILS.sendFormatted(player1.getPlayer(), LANG.getString("summary.get.currency"), Map.of("%amount%", currencyAm, "%currency%", currencyName));
}
}
List<String> player1Items = new ArrayList<>();
player1.getTradeGui().getItems().forEach(itemStack -> {
if (itemStack == null) return;
Scheduler.get().runAt(player2.getPlayer().getLocation(), task -> {
ContainerUtils.INSTANCE.addOrDrop(player2.getPlayer().getInventory(), List.of(itemStack), player2.getPlayer().getLocation());
});
final String itemName = Utils.getFormattedItemName(itemStack);
int itemAm = itemStack.getAmount();
player1Items.add(itemAm + "x " + itemName);
if (CONFIG.getBoolean("enable-trade-summaries")) {
MESSAGEUTILS.sendFormatted(player1.getPlayer(), LANG.getString("summary.give.item"), Map.of("%amount%", "" + itemAm, "%item%", itemName));
MESSAGEUTILS.sendFormatted(player2.getPlayer(), LANG.getString("summary.get.item"), Map.of("%amount%", "" + itemAm, "%item%", itemName));
}
});
List<String> player2Items = new ArrayList<>();
player2.getTradeGui().getItems().forEach(itemStack -> {
if (itemStack == null) return;
Scheduler.get().runAt(player1.getPlayer().getLocation(), task -> {
ContainerUtils.INSTANCE.addOrDrop(player1.getPlayer().getInventory(), List.of(itemStack), player1.getPlayer().getLocation());
});
final String itemName = Utils.getFormattedItemName(itemStack);
int itemAm = itemStack.getAmount();
player2Items.add(itemAm + "x " + itemName);
if (CONFIG.getBoolean("enable-trade-summaries")) {
MESSAGEUTILS.sendFormatted(player2.getPlayer(), LANG.getString("summary.give.item"), Map.of("%amount%", "" + itemAm, "%item%", itemName));
MESSAGEUTILS.sendFormatted(player1.getPlayer(), LANG.getString("summary.get.item"), Map.of("%amount%", "" + itemAm, "%item%", itemName));
}
});
HistoryUtils.writeToHistory(
String.format("%s: [Currencies: %s] [Items: %s] | %s: [Currencies: %s] [Items: %s]",
player1.getPlayer().getName(), player1Currencies.isEmpty() ? "---" : String.join(", ", player1Currencies), player1Items.isEmpty() ? "---" : String.join(", ", player1Items), player2.getPlayer().getName(), player2Currencies.isEmpty() ? "---" : String.join(", ", player2Currencies), player2Items.isEmpty() ? "---" : String.join(", ", player2Items)));
});
final String itemName = Utils.getFormattedItemName(itemStack);
int itemAm = itemStack.getAmount();
player1Items.add(itemAm + "x " + itemName);
if (CONFIG.getBoolean("enable-trade-summaries")) {
MESSAGEUTILS.sendFormatted(player1.getPlayer(), LANG.getString("summary.give.item"), Map.of("%amount%", "" + itemAm, "%item%", itemName));
MESSAGEUTILS.sendFormatted(player2.getPlayer(), LANG.getString("summary.get.item"), Map.of("%amount%", "" + itemAm, "%item%", itemName));
}
});
List<String> player2Items = new ArrayList<>();
player2.getTradeGui().getItems().forEach(itemStack -> {
if (itemStack == null) return;
Scheduler.get().runAt(player1.getPlayer().getLocation(), task -> {
ContainerUtils.INSTANCE.addOrDrop(player1.getPlayer().getInventory(), List.of(itemStack), player1.getPlayer().getLocation());
});
final String itemName = Utils.getFormattedItemName(itemStack);
int itemAm = itemStack.getAmount();
player2Items.add(itemAm + "x " + itemName);
if (CONFIG.getBoolean("enable-trade-summaries")) {
MESSAGEUTILS.sendFormatted(player2.getPlayer(), LANG.getString("summary.give.item"), Map.of("%amount%", "" + itemAm, "%item%", itemName));
MESSAGEUTILS.sendFormatted(player1.getPlayer(), LANG.getString("summary.get.item"), Map.of("%amount%", "" + itemAm, "%item%", itemName));
}
});
HistoryUtils.writeToHistory(
String.format("%s: [Currencies: %s] [Items: %s] | %s: [Currencies: %s] [Items: %s]",
player1.getPlayer().getName(), player1Currencies.isEmpty() ? "---" : String.join(", ", player1Currencies), player1Items.isEmpty() ? "---" : String.join(", ", player1Items), player2.getPlayer().getName(), player2Currencies.isEmpty() ? "---" : String.join(", ", player2Currencies), player2Items.isEmpty() ? "---" : String.join(", ", player2Items)));
end();
}
public long getPrepTime() {