1.12.0 part 2

This commit is contained in:
BenceX100 2024-12-12 19:16:19 +01:00
parent be34f66a48
commit a8f043aca8
4 changed files with 24 additions and 26 deletions

View File

@ -3,7 +3,6 @@ package com.artillexstudios.axtrade.currency;
import com.artillexstudios.axtrade.hooks.currency.CurrencyHook; import com.artillexstudios.axtrade.hooks.currency.CurrencyHook;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;

View File

@ -38,8 +38,22 @@ public class Trade {
player2.getTradeGui().update(); 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() { public void abort() {
if (ended) return; abort(false);
}
public void abort(boolean force) {
if (!force && ended) return;
end();
player1.getTradeGui().getItems().forEach(itemStack -> { player1.getTradeGui().getItems().forEach(itemStack -> {
if (itemStack == null) return; if (itemStack == null) return;
player1.getPlayer().getInventory().addItem(itemStack); player1.getPlayer().getInventory().addItem(itemStack);
@ -53,55 +67,39 @@ public class Trade {
MESSAGEUTILS.sendLang(player2.getPlayer(), "trade.aborted", Map.of("%player%", player1.getPlayer().getName())); MESSAGEUTILS.sendLang(player2.getPlayer(), "trade.aborted", Map.of("%player%", player1.getPlayer().getName()));
SoundUtils.playSound(player1.getPlayer(), "aborted"); SoundUtils.playSound(player1.getPlayer(), "aborted");
SoundUtils.playSound(player2.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() { public void complete() {
end();
for (Map.Entry<CurrencyHook, Double> entry : player1.getCurrencies().entrySet()) { for (Map.Entry<CurrencyHook, Double> entry : player1.getCurrencies().entrySet()) {
if (entry.getKey().getBalance(player1.getPlayer().getUniqueId()) < entry.getValue()) { if (entry.getKey().getBalance(player1.getPlayer().getUniqueId()) < entry.getValue()) {
abort(); abort(true);
return; return;
} }
} }
for (Map.Entry<CurrencyHook, Double> entry : player2.getCurrencies().entrySet()) { for (Map.Entry<CurrencyHook, Double> entry : player2.getCurrencies().entrySet()) {
if (entry.getKey().getBalance(player2.getPlayer().getUniqueId()) < entry.getValue()) { if (entry.getKey().getBalance(player2.getPlayer().getUniqueId()) < entry.getValue()) {
abort(); abort(true);
return; return;
} }
} }
close();
CurrencyProcessor currencyProcessor1 = new CurrencyProcessor(player1.getPlayer(), player1.getCurrencies().entrySet()); CurrencyProcessor currencyProcessor1 = new CurrencyProcessor(player1.getPlayer(), player1.getCurrencies().entrySet());
currencyProcessor1.run().thenAccept(success1 -> { currencyProcessor1.run().thenAccept(success1 -> {
if (!success1) { if (!success1) {
abort(); abort(true);
return; return;
} }
CurrencyProcessor currencyProcessor2 = new CurrencyProcessor(player2.getPlayer(), player2.getCurrencies().entrySet()); CurrencyProcessor currencyProcessor2 = new CurrencyProcessor(player2.getPlayer(), player2.getCurrencies().entrySet());
currencyProcessor2.run().thenAccept(success2 -> { currencyProcessor2.run().thenAccept(success2 -> {
if (!success2) { if (!success2) {
abort(); abort(true);
currencyProcessor1.reverse(); currencyProcessor1.reverse();
return; return;
} }
end();
MESSAGEUTILS.sendLang(player1.getPlayer(), "trade.completed", Map.of("%player%", player2.getPlayer().getName())); MESSAGEUTILS.sendLang(player1.getPlayer(), "trade.completed", Map.of("%player%", player2.getPlayer().getName()));
MESSAGEUTILS.sendLang(player2.getPlayer(), "trade.completed", Map.of("%player%", player1.getPlayer().getName())); MESSAGEUTILS.sendLang(player2.getPlayer(), "trade.completed", Map.of("%player%", player1.getPlayer().getName()));
SoundUtils.playSound(player1.getPlayer(), "completed"); SoundUtils.playSound(player1.getPlayer(), "completed");

View File

@ -6,8 +6,8 @@ import com.artillexstudios.axtrade.utils.NumberUtils;
import com.artillexstudios.axtrade.utils.SoundUtils; import com.artillexstudios.axtrade.utils.SoundUtils;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import static com.artillexstudios.axtrade.AxTrade.CONFIG; import static com.artillexstudios.axtrade.AxTrade.CONFIG;
@ -16,7 +16,7 @@ public class TradePlayer {
private TradePlayer otherPlayer; private TradePlayer otherPlayer;
private TradeGui tradeGui; private TradeGui tradeGui;
private final Trade trade; private final Trade trade;
private final HashMap<CurrencyHook, Double> currencies = new HashMap<>(); private final ConcurrentHashMap<CurrencyHook, Double> currencies = new ConcurrentHashMap<>();
// confirmed // confirmed
// null > not confirmed // null > not confirmed
@ -90,7 +90,7 @@ public class TradePlayer {
SoundUtils.playSound(player, "countdown"); SoundUtils.playSound(player, "countdown");
} }
public HashMap<CurrencyHook, Double> getCurrencies() { public ConcurrentHashMap<CurrencyHook, Double> getCurrencies() {
return currencies; return currencies;
} }

View File

@ -15,6 +15,7 @@ public class TradeTicker {
public static void start() { public static void start() {
Scheduler.get().runTimer(scheduledTask -> { Scheduler.get().runTimer(scheduledTask -> {
for (Trade trade : Trades.getTrades()) { for (Trade trade : Trades.getTrades()) {
if (trade.isEnded()) continue;
if (!(trade.player1.hasConfirmed() && trade.player2.hasConfirmed())) continue; if (!(trade.player1.hasConfirmed() && trade.player2.hasConfirmed())) continue;
if (trade.player1.getConfirmed() == 1) { if (trade.player1.getConfirmed() == 1) {