From 8c5dc018ab785d886a30eac2baa004084ccf93c2 Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 9 Aug 2018 20:32:23 +0300 Subject: [PATCH] Fix bug with economy --- build/resources/main/config.yml | 6 +- build/resources/main/lang/english.lang | 3 +- .../service/economy/EconomyProvider.java | 12 ++ .../zendal/service/economy/VaultEconomy.java | 10 ++ .../zendal/session/TradeSessionManager.java | 109 +++++++++--------- src/main/resources/lang/english.lang | 2 +- 6 files changed, 84 insertions(+), 58 deletions(-) diff --git a/build/resources/main/config.yml b/build/resources/main/config.yml index 8094666..b34c5f5 100644 --- a/build/resources/main/config.yml +++ b/build/resources/main/config.yml @@ -1,9 +1,9 @@ storage: #Type storage: # Local - file in plugin folder - # Mongo - MongoDB NoSQL storage - # mysql - MySQL storage - type: mongo + # MongoDB - MongoDB NoSQL storage + # MySQL - MySQL storage + type: MongoDB setting: #host where store storage, if your use Local this parameter ignore diff --git a/build/resources/main/lang/english.lang b/build/resources/main/lang/english.lang index fa510b2..a26a06d 100644 --- a/build/resources/main/lang/english.lang +++ b/build/resources/main/lang/english.lang @@ -19,11 +19,12 @@ command.to.error.executorNotPlayer=Only player must call this command command.to.error.notSetUser=Please pick user like: /trade to UserName command.to.error.undefinedUser=User %1% undefined. command.to.error.onHimselfItself=You can't send Himself itself trade. - +command.to.error.noMoney=%1% missing %2% money. command.confirm.message=§6Confirm trade from %seller% +command.create.error.executorNotPlayer=Only player must call this command trade.player.offile=User %buyer% is offline. trade.confirm.notHaveSession=Sorry you don't have any sessions diff --git a/src/main/java/ru/zendal/service/economy/EconomyProvider.java b/src/main/java/ru/zendal/service/economy/EconomyProvider.java index b379650..c9c46d2 100644 --- a/src/main/java/ru/zendal/service/economy/EconomyProvider.java +++ b/src/main/java/ru/zendal/service/economy/EconomyProvider.java @@ -10,6 +10,9 @@ import org.bukkit.OfflinePlayer; import ru.zendal.service.economy.exception.EconomyProviderException; +/** + * Interface of Economy Provider + */ public interface EconomyProvider { /** @@ -30,6 +33,15 @@ public interface EconomyProvider { */ void withdraw(OfflinePlayer player, double amount) throws EconomyProviderException; + /** + * Can withdraw (-) money + * + * @param player Instance offline Player + * @param amount AMount money + * @return {@code true} if can else {@code false} + */ + boolean canWithdraw(OfflinePlayer player, double amount); + /** * Deposit (+) money diff --git a/src/main/java/ru/zendal/service/economy/VaultEconomy.java b/src/main/java/ru/zendal/service/economy/VaultEconomy.java index b87a92e..c119d02 100644 --- a/src/main/java/ru/zendal/service/economy/VaultEconomy.java +++ b/src/main/java/ru/zendal/service/economy/VaultEconomy.java @@ -46,6 +46,16 @@ public void withdraw(OfflinePlayer player, double amount) throws EconomyProvider } } + @Override + public boolean canWithdraw(OfflinePlayer player, double amount) { + EconomyResponse economyResponse = economy.withdrawPlayer(player, amount); + if (economyResponse.transactionSuccess()) { + economy.depositPlayer(player, amount); + return true; + } + return false; + } + @Override public void deposit(OfflinePlayer player, double amount) throws EconomyProviderException { EconomyResponse economyResponse = economy.depositPlayer(player, amount); diff --git a/src/main/java/ru/zendal/session/TradeSessionManager.java b/src/main/java/ru/zendal/session/TradeSessionManager.java index 7a73066..b35f8c0 100644 --- a/src/main/java/ru/zendal/session/TradeSessionManager.java +++ b/src/main/java/ru/zendal/session/TradeSessionManager.java @@ -209,52 +209,34 @@ private void processOfflineTrade(TradeOfflineSession session) { */ private void processTrade(TradeSession session) { - session.setReadyBuyer( - economyProvider.haveMoney( - session.getBuyer(), - session.getBetBuyer() - ) - ); - - session.setReadyBuyer( - economyProvider.haveMoney( - session.getSeller(), - session.getBetSeller() - ) - ); - - if (!session.isBuyerReady() || !session.isSellerReady()) { - - //TODO Сообщение о нехватке денег - return; - } - - economyProvider.withdraw(session.getBuyer(), session.getBetBuyer()); - economyProvider.withdraw(session.getSeller(), session.getBetSeller()); - - economyProvider.deposit(session.getBuyer(), session.getBetSeller()); - economyProvider.deposit(session.getSeller(), session.getBetBuyer()); - this.addNotFitItemsIntoStorageInventory( - session.getSeller().getInventory().addItem( - session.getBuyerItems().toArray(new ItemStack[0])), - session.getSeller() - ); - this.addNotFitItemsIntoStorageInventory(session.getBuyer().getInventory().addItem( - session.getSellerItems().toArray(new ItemStack[0])), - session.getBuyer() - ); - - if (session.getBuyer().getOpenInventory().getTopInventory().hashCode() == session.getInventory().hashCode()) { - session.getBuyer().closeInventory(); - } - if (session.getSeller().getOpenInventory().getTopInventory().hashCode() == session.getInventory().hashCode()) { - session.getSeller().closeInventory(); - } + if (this.economyProcess(session)) { + economyProvider.withdraw(session.getBuyer(), session.getBetBuyer()); + economyProvider.withdraw(session.getSeller(), session.getBetSeller()); + + economyProvider.deposit(session.getBuyer(), session.getBetSeller()); + economyProvider.deposit(session.getSeller(), session.getBetBuyer()); + this.addNotFitItemsIntoStorageInventory( + session.getSeller().getInventory().addItem( + session.getBuyerItems().toArray(new ItemStack[0])), + session.getSeller() + ); + this.addNotFitItemsIntoStorageInventory(session.getBuyer().getInventory().addItem( + session.getSellerItems().toArray(new ItemStack[0])), + session.getBuyer() + ); + + if (session.getBuyer().getOpenInventory().getTopInventory().hashCode() == session.getInventory().hashCode()) { + session.getBuyer().closeInventory(); + } + if (session.getSeller().getOpenInventory().getTopInventory().hashCode() == session.getInventory().hashCode()) { + session.getSeller().closeInventory(); + } - try { - this.removeSession(session); - } catch (TradeSessionManagerException e) { - e.printStackTrace(); + try { + this.removeSession(session); + } catch (TradeSessionManagerException e) { + e.printStackTrace(); + } } } @@ -264,14 +246,35 @@ private void processTrade(TradeSession session) { * @param session Session * @return {@code true} if correct else {@code false} */ - private boolean isCorrectBetSession(Session session) { - return economyProvider.haveMoney( - session.getBuyer(), - session.getBetBuyer() - ) && economyProvider.haveMoney( - session.getSeller(), - session.getBetSeller() - ); + private boolean economyProcess(Session session) { + if (!economyProvider.canWithdraw(session.getBuyer(), session.getBetBuyer())) { + session.setReadyBuyer(false); + } + + if (!economyProvider.canWithdraw(session.getSeller(), session.getBetSeller())) { + session.setReadySeller(false); + } + + if (!session.isBuyerReady() || !session.isSellerReady()) { + String messageBuyer = languageConfig.getMessage("command.to.error.noMoney") + .setCustomMessage(1, session.getBuyer().getDisplayName()) + .setCustomMessage(2, String.valueOf(session.getBetSeller() - economyProvider.getBalance(session.getBuyer()))).toString(); + + String messageSeller = languageConfig.getMessage("command.to.error.noMoney") + .setCustomMessage(1, session.getSeller().getDisplayName()) + .setCustomMessage(2, String.valueOf(session.getBetBuyer() - economyProvider.getBalance(session.getSeller()))).toString(); + if (!session.isBuyerReady()) { + session.getBuyer().sendMessage(messageBuyer); + session.getSeller().sendMessage(messageBuyer); + } + + if (!session.isSellerReady()) { + session.getBuyer().sendMessage(messageSeller); + session.getSeller().sendMessage(messageSeller); + } + return false; + } + return true; } /** diff --git a/src/main/resources/lang/english.lang b/src/main/resources/lang/english.lang index 0f96e91..a26a06d 100644 --- a/src/main/resources/lang/english.lang +++ b/src/main/resources/lang/english.lang @@ -19,7 +19,7 @@ command.to.error.executorNotPlayer=Only player must call this command command.to.error.notSetUser=Please pick user like: /trade to UserName command.to.error.undefinedUser=User %1% undefined. command.to.error.onHimselfItself=You can't send Himself itself trade. - +command.to.error.noMoney=%1% missing %2% money. command.confirm.message=§6Confirm trade from %seller%