Implement the check part of a transaction

This commit is contained in:
Flowsqy 2022-12-21 17:37:00 +01:00
parent d38436f177
commit ffbfe4928a
2 changed files with 74 additions and 2 deletions

View File

@ -1,4 +1,39 @@
package de.epiceric.shopchest.transaction;
import org.bukkit.inventory.ItemStack;
public interface Actor {
/**
* Check if this {@link Actor} have a specified amount of money
*
* @param moneyAmount The money amount required
* @return Whether the {@link Actor} have the money
*/
boolean hasMoney(double moneyAmount);
/**
* Check if this {@link Actor} have a specified amount of an item
*
* @param itemStack The {@link ItemStack} that represent the product
* @param amount The amount to check
* @return Whether the {@link Actor} have the amount of this item stack or more
*/
boolean hasProductAmount(ItemStack itemStack, int amount);
/**
* Check if this {@link Actor} have enough inventory space to accept this item stack with this amount
*
* @param itemStack The {@link ItemStack} that represent the product
* @param amount The amount to check
* @return Whether the {@link Actor} have the space to accept this amount of this item stack in its inventory
*/
boolean hasEnoughInventorySpace(ItemStack itemStack, int amount);
/**
* Send a message to this {@link Actor}
*
* @param message The message to send
*/
void sendMessage(String message);
}

View File

@ -1,19 +1,26 @@
package de.epiceric.shopchest.transaction;
import de.epiceric.shopchest.language.LanguageUtils;
import de.epiceric.shopchest.language.Message;
import org.bukkit.inventory.ItemStack;
public class Transaction {
// A direct transaction is initiated by the buyer
// A indirect transaction is initiated by the seller
private final boolean directTransaction;
private final Actor buyer, seller;
private final ItemStack itemStack;
private int amount;
private final double buyPrice, sellPrice;
private final double moneyAmountRequired, moneyAmountGiven;
public void apply() {
if(!check()) {
if (!check()) {
return;
}
// Call buy or sell event
transferItems();
transferMoney();
@ -23,12 +30,42 @@ public class Transaction {
// log
}
/**
* Check if the transaction can be performed and inform actors
*
* @return {@code true} if the transaction can be performed
*/
private boolean check() {
// Check buyer money
if (!buyer.hasMoney(moneyAmountRequired)) {
if (directTransaction) {
buyer.sendMessage(LanguageUtils.getMessage(Message.NOT_ENOUGH_MONEY));
} else {
seller.sendMessage(LanguageUtils.getMessage(Message.VENDOR_NOT_ENOUGH_MONEY));
}
return false;
}
// Check seller item quantity
if (!seller.hasProductAmount(itemStack, amount)) {
if (directTransaction) {
buyer.sendMessage(LanguageUtils.getMessage(Message.OUT_OF_STOCK));
} else {
seller.sendMessage(LanguageUtils.getMessage(Message.NOT_ENOUGH_ITEMS));
}
return false;
}
// Check buyer inventory space
if (!buyer.hasEnoughInventorySpace(itemStack, amount)) {
if (directTransaction) {
buyer.sendMessage(LanguageUtils.getMessage(Message.NOT_ENOUGH_INVENTORY_SPACE));
} else {
seller.sendMessage(LanguageUtils.getMessage(Message.CHEST_NOT_ENOUGH_INVENTORY_SPACE));
}
return false;
}
return true;
}
private void transferItems() {