Refactor/rework
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
package com.alttd.playershops.shop;
|
||||
|
||||
import com.alttd.playershops.PlayerShops;
|
||||
import com.alttd.playershops.events.PlayerExchangeShopEvent;
|
||||
import com.alttd.playershops.utils.EconomyUtils;
|
||||
import com.alttd.playershops.utils.InventoryUtils;
|
||||
import com.alttd.playershops.utils.ShopUtil;
|
||||
import com.alttd.playershops.utils.Util;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
@@ -27,7 +31,7 @@ public class PlayerShop {
|
||||
@Getter
|
||||
private UUID ownerUUID;
|
||||
@Getter @Setter
|
||||
private ShopType type = ShopType.DEFAULT;
|
||||
private ShopType type = ShopType.NONE;
|
||||
@Getter
|
||||
private final Location signLocation;
|
||||
@Getter
|
||||
@@ -76,7 +80,7 @@ public class PlayerShop {
|
||||
}
|
||||
|
||||
public int getRemainingStock() {
|
||||
return ShopUtil.countItems(getInventory(), getItemStack());
|
||||
return InventoryUtils.countItems(getInventory(), getItemStack());
|
||||
}
|
||||
|
||||
public int getRemainingSpace() {
|
||||
@@ -121,7 +125,7 @@ public class PlayerShop {
|
||||
}
|
||||
|
||||
public boolean isInitialized() {
|
||||
return type != ShopType.DEFAULT;
|
||||
return type != ShopType.NONE;
|
||||
}
|
||||
|
||||
public void updateSign() {
|
||||
@@ -149,4 +153,108 @@ public class PlayerShop {
|
||||
}
|
||||
}.runTaskLater(PlayerShops.getInstance(), 2L);
|
||||
}
|
||||
|
||||
public double getPricePerItem() {
|
||||
double pricePer = this.getPrice() / this.getAmount();
|
||||
return this.getPrice() / this.getAmount();
|
||||
}
|
||||
|
||||
public void removeBalance(double amount) {
|
||||
this.balance -= amount;
|
||||
}
|
||||
|
||||
public void addBalance(double amount) {
|
||||
this.balance += amount;
|
||||
}
|
||||
|
||||
|
||||
public TransactionError executeTransaction(int orders, Player player) {
|
||||
return switch (getType()) {
|
||||
case SELL -> executeSellTransaction(orders, player);
|
||||
case BUY -> executeBuyTransaction(orders, player);
|
||||
case GAMBLE -> executeGambleTransaction(orders, player);
|
||||
default -> TransactionError.NONE; // This should not happen
|
||||
};
|
||||
}
|
||||
|
||||
private TransactionError executeSellTransaction(int orders, Player player) {
|
||||
ItemStack itemStack = getItemStack().clone();
|
||||
double price = getPrice();
|
||||
if (orders == itemStack.getMaxStackSize()) {
|
||||
itemStack.setAmount(orders);
|
||||
price = getPricePerItem() * orders;
|
||||
}
|
||||
|
||||
int shopItems = InventoryUtils.countItems(getInventory(), itemStack);
|
||||
if (shopItems < itemStack.getAmount())
|
||||
return TransactionError.INSUFFICIENT_FUNDS_SHOP;
|
||||
|
||||
boolean hasFunds = EconomyUtils.hasSufficientFunds(player, price);
|
||||
if (!hasFunds)
|
||||
return TransactionError.INSUFFICIENT_FUNDS_PLAYER;
|
||||
|
||||
boolean hasRoom = EconomyUtils.canAcceptFunds(this, price);
|
||||
if (!hasRoom)
|
||||
return TransactionError.INVENTORY_FULL_SHOP;
|
||||
|
||||
boolean playerHasRoom = InventoryUtils.hasRoom(player.getInventory(), itemStack);
|
||||
if (!playerHasRoom)
|
||||
return TransactionError.INVENTORY_FULL_PLAYER;
|
||||
|
||||
PlayerExchangeShopEvent playerExchangeShopEvent = new PlayerExchangeShopEvent(player, this);
|
||||
if (Util.callCancellableEvent(playerExchangeShopEvent)) {
|
||||
return TransactionError.CANCELLED;
|
||||
}
|
||||
|
||||
InventoryUtils.removeItem(getInventory(), itemStack);
|
||||
EconomyUtils.removeFunds(player, price);
|
||||
addBalance(price);
|
||||
InventoryUtils.addItem(player.getInventory(), itemStack);
|
||||
player.updateInventory();
|
||||
|
||||
return TransactionError.NONE;
|
||||
}
|
||||
|
||||
private TransactionError executeBuyTransaction(int orders, Player player) {
|
||||
ItemStack itemStack = getItemStack().clone();
|
||||
double price = getPrice();
|
||||
if (orders == itemStack.getMaxStackSize()) {
|
||||
itemStack.setAmount(orders);
|
||||
price = getPricePerItem() * orders;
|
||||
}
|
||||
|
||||
int playerItems = InventoryUtils.countItems(player.getInventory(), itemStack);
|
||||
if (playerItems < itemStack.getAmount())
|
||||
return TransactionError.INSUFFICIENT_FUNDS_PLAYER;
|
||||
|
||||
boolean hasFunds = EconomyUtils.hasSufficientFunds(this, price);
|
||||
if (!hasFunds)
|
||||
return TransactionError.INSUFFICIENT_FUNDS_SHOP;
|
||||
|
||||
boolean hasRoom = EconomyUtils.canAcceptFunds(player, price);
|
||||
if (!hasRoom)
|
||||
return TransactionError.INVENTORY_FULL_PLAYER;
|
||||
|
||||
boolean shopHasRoom = InventoryUtils.hasRoom(getInventory(), itemStack);
|
||||
if (!shopHasRoom)
|
||||
return TransactionError.INVENTORY_FULL_SHOP;
|
||||
|
||||
PlayerExchangeShopEvent playerExchangeShopEvent = new PlayerExchangeShopEvent(player, this);
|
||||
if (Util.callCancellableEvent(playerExchangeShopEvent)) {
|
||||
return TransactionError.CANCELLED;
|
||||
}
|
||||
|
||||
InventoryUtils.removeItem(player.getInventory(), itemStack);
|
||||
removeBalance(price);
|
||||
EconomyUtils.addFunds(player, price);
|
||||
InventoryUtils.addItem(getInventory(), itemStack);
|
||||
player.updateInventory();
|
||||
|
||||
return TransactionError.NONE;
|
||||
}
|
||||
|
||||
private TransactionError executeGambleTransaction(int orders, Player player) {
|
||||
return TransactionError.NONE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.alttd.playershops.config.ShopTypeConfig;
|
||||
|
||||
public enum ShopType {
|
||||
|
||||
DEFAULT(),
|
||||
NONE(),
|
||||
SELL(),
|
||||
BUY(),
|
||||
GAMBLE();
|
||||
|
||||
Reference in New Issue
Block a user