Add TransactionInformer

This commit is contained in:
Flowsqy 2022-12-22 20:56:18 +01:00
parent b6c0bc9b5f
commit 397ebf48d0
4 changed files with 65 additions and 18 deletions

View File

@ -0,0 +1,25 @@
package de.epiceric.shopchest.transaction;
import de.epiceric.shopchest.language.LanguageUtils;
import de.epiceric.shopchest.language.Message;
public class BuyInformer implements TransactionInformer {
private Actor buyer, seller;
@Override
public void sendNotEnoughMoney() {
buyer.sendMessage(LanguageUtils.getMessage(Message.NOT_ENOUGH_MONEY));
}
@Override
public void sendNotEnoughItem() {
buyer.sendMessage(LanguageUtils.getMessage(Message.OUT_OF_STOCK));
}
@Override
public void sendNotEnoughSpace() {
buyer.sendMessage(LanguageUtils.getMessage(Message.NOT_ENOUGH_INVENTORY_SPACE));
}
}

View File

@ -0,0 +1,25 @@
package de.epiceric.shopchest.transaction;
import de.epiceric.shopchest.language.LanguageUtils;
import de.epiceric.shopchest.language.Message;
public class SellInformer implements TransactionInformer {
private Actor buyer, seller;
@Override
public void sendNotEnoughMoney() {
seller.sendMessage(LanguageUtils.getMessage(Message.VENDOR_NOT_ENOUGH_MONEY));
}
@Override
public void sendNotEnoughItem() {
seller.sendMessage(LanguageUtils.getMessage(Message.NOT_ENOUGH_ITEMS));
}
@Override
public void sendNotEnoughSpace() {
seller.sendMessage(LanguageUtils.getMessage(Message.CHEST_NOT_ENOUGH_INVENTORY_SPACE));
}
}

View File

@ -7,10 +7,8 @@ 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 TransactionInformer informer;
private final ItemStack itemStack;
private int amount;
private final double moneyAmountRequired, moneyAmountGiven;
@ -42,31 +40,19 @@ public class Transaction {
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));
}
informer.sendNotEnoughMoney();
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));
}
informer.sendNotEnoughItem();
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));
}
informer.sendNotEnoughSpace();
return false;
}
return true;

View File

@ -0,0 +1,11 @@
package de.epiceric.shopchest.transaction;
public interface TransactionInformer {
void sendNotEnoughMoney();
void sendNotEnoughItem();
void sendNotEnoughSpace();
}