Skip to content

Commit

Permalink
Fix bug with economy
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander committed Aug 9, 2018
1 parent 92f76d5 commit 8c5dc01
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 58 deletions.
6 changes: 3 additions & 3 deletions build/resources/main/config.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion build/resources/main/lang/english.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/ru/zendal/service/economy/EconomyProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import org.bukkit.OfflinePlayer;
import ru.zendal.service.economy.exception.EconomyProviderException;

/**
* Interface of Economy Provider
*/
public interface EconomyProvider {

/**
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/ru/zendal/service/economy/VaultEconomy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
109 changes: 56 additions & 53 deletions src/main/java/ru/zendal/session/TradeSessionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

Expand All @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/lang/english.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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%

Expand Down

0 comments on commit 8c5dc01

Please sign in to comment.