diff --git a/src/main/java/com/artillexstudios/axtrade/currency/CurrencyProcessor.java b/src/main/java/com/artillexstudios/axtrade/currency/CurrencyProcessor.java index 3339326..ef27bd4 100644 --- a/src/main/java/com/artillexstudios/axtrade/currency/CurrencyProcessor.java +++ b/src/main/java/com/artillexstudios/axtrade/currency/CurrencyProcessor.java @@ -3,7 +3,6 @@ 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; diff --git a/src/main/java/com/artillexstudios/axtrade/trade/Trade.java b/src/main/java/com/artillexstudios/axtrade/trade/Trade.java index 6137dbc..4b53d3f 100644 --- a/src/main/java/com/artillexstudios/axtrade/trade/Trade.java +++ b/src/main/java/com/artillexstudios/axtrade/trade/Trade.java @@ -38,8 +38,22 @@ public class Trade { player2.getTradeGui().update(); } + public void end() { + ended = true; + Scheduler.get().run(scheduledTask -> Trades.removeTrade(this)); + player1.getPlayer().closeInventory(); + player1.getPlayer().updateInventory(); + player2.getPlayer().closeInventory(); + player2.getPlayer().updateInventory(); + } + public void abort() { - if (ended) return; + abort(false); + } + + public void abort(boolean force) { + if (!force && ended) return; + end(); player1.getTradeGui().getItems().forEach(itemStack -> { if (itemStack == null) return; player1.getPlayer().getInventory().addItem(itemStack); @@ -53,55 +67,39 @@ public class Trade { MESSAGEUTILS.sendLang(player2.getPlayer(), "trade.aborted", Map.of("%player%", player1.getPlayer().getName())); SoundUtils.playSound(player1.getPlayer(), "aborted"); SoundUtils.playSound(player2.getPlayer(), "aborted"); - end(); - close(); - } - - public void end() { - ended = true; - } - - public void close() { - Scheduler.get().run(scheduledTask -> Trades.removeTrade(this)); - player1.getPlayer().closeInventory(); - player1.getPlayer().updateInventory(); - player2.getPlayer().closeInventory(); - player2.getPlayer().updateInventory(); } public void complete() { + end(); for (Map.Entry entry : player1.getCurrencies().entrySet()) { if (entry.getKey().getBalance(player1.getPlayer().getUniqueId()) < entry.getValue()) { - abort(); + abort(true); return; } } for (Map.Entry entry : player2.getCurrencies().entrySet()) { if (entry.getKey().getBalance(player2.getPlayer().getUniqueId()) < entry.getValue()) { - abort(); + abort(true); return; } } - close(); CurrencyProcessor currencyProcessor1 = new CurrencyProcessor(player1.getPlayer(), player1.getCurrencies().entrySet()); currencyProcessor1.run().thenAccept(success1 -> { if (!success1) { - abort(); + abort(true); return; } CurrencyProcessor currencyProcessor2 = new CurrencyProcessor(player2.getPlayer(), player2.getCurrencies().entrySet()); currencyProcessor2.run().thenAccept(success2 -> { if (!success2) { - abort(); + abort(true); currencyProcessor1.reverse(); return; } - 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"); diff --git a/src/main/java/com/artillexstudios/axtrade/trade/TradePlayer.java b/src/main/java/com/artillexstudios/axtrade/trade/TradePlayer.java index c7ec833..1d32774 100644 --- a/src/main/java/com/artillexstudios/axtrade/trade/TradePlayer.java +++ b/src/main/java/com/artillexstudios/axtrade/trade/TradePlayer.java @@ -6,8 +6,8 @@ import com.artillexstudios.axtrade.utils.NumberUtils; import com.artillexstudios.axtrade.utils.SoundUtils; import org.bukkit.entity.Player; -import java.util.HashMap; import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; import static com.artillexstudios.axtrade.AxTrade.CONFIG; @@ -16,7 +16,7 @@ public class TradePlayer { private TradePlayer otherPlayer; private TradeGui tradeGui; private final Trade trade; - private final HashMap currencies = new HashMap<>(); + private final ConcurrentHashMap currencies = new ConcurrentHashMap<>(); // confirmed // null > not confirmed @@ -90,7 +90,7 @@ public class TradePlayer { SoundUtils.playSound(player, "countdown"); } - public HashMap getCurrencies() { + public ConcurrentHashMap getCurrencies() { return currencies; } diff --git a/src/main/java/com/artillexstudios/axtrade/trade/TradeTicker.java b/src/main/java/com/artillexstudios/axtrade/trade/TradeTicker.java index 2a899af..ebe4b9f 100644 --- a/src/main/java/com/artillexstudios/axtrade/trade/TradeTicker.java +++ b/src/main/java/com/artillexstudios/axtrade/trade/TradeTicker.java @@ -15,6 +15,7 @@ public class TradeTicker { public static void start() { Scheduler.get().runTimer(scheduledTask -> { for (Trade trade : Trades.getTrades()) { + if (trade.isEnded()) continue; if (!(trade.player1.hasConfirmed() && trade.player2.hasConfirmed())) continue; if (trade.player1.getConfirmed() == 1) {