Refactor ShopTransactions
This commit is contained in:
parent
4fb4ea4a79
commit
658837814a
|
|
@ -1,8 +1,10 @@
|
|||
package com.alttd.playershops;
|
||||
|
||||
import com.alttd.playershops.commands.ShopCommand;
|
||||
import com.alttd.playershops.config.Config;
|
||||
import com.alttd.playershops.config.DatabaseConfig;
|
||||
import com.alttd.playershops.config.MessageConfig;
|
||||
import com.alttd.playershops.gui.GuiIcon;
|
||||
import com.alttd.playershops.handler.ShopHandler;
|
||||
import com.alttd.playershops.listener.BlockListener;
|
||||
import com.alttd.playershops.listener.PlayerListener;
|
||||
|
|
@ -84,9 +86,7 @@ public class PlayerShops extends JavaPlugin {
|
|||
}
|
||||
|
||||
private void registerCommands() {
|
||||
// for(String command : this.getDescription().getCommands().keySet()) {
|
||||
// getCommand(command).setExecutor();
|
||||
// }
|
||||
getCommand("playershop").setExecutor(new ShopCommand());
|
||||
}
|
||||
|
||||
public void reloadConfigs() {
|
||||
|
|
@ -97,6 +97,9 @@ public class PlayerShops extends JavaPlugin {
|
|||
for (ShopType shopType : ShopType.values()) {
|
||||
// preload ShopType to get the configs active
|
||||
}
|
||||
for (GuiIcon guiIcon : GuiIcon.values()) {
|
||||
// preload to get config values generated
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,124 +0,0 @@
|
|||
package com.alttd.playershops.shop;
|
||||
|
||||
import com.alttd.playershops.events.PlayerExchangeShopEvent;
|
||||
import com.alttd.playershops.utils.EconomyUtils;
|
||||
import com.alttd.playershops.utils.InventoryUtils;
|
||||
import com.alttd.playershops.utils.Util;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public record ShopTransaction(int orders, Player player, PlayerShop shop) {
|
||||
|
||||
public TransactionError executeTransaction(boolean isCheck) {
|
||||
return switch (shop.getType()) {
|
||||
case SELL -> executeSellTransaction();
|
||||
case BUY -> executeBuyTransaction();
|
||||
case GAMBLE -> executeGambleTransaction();
|
||||
default -> TransactionError.NONE; // This should not happen
|
||||
};
|
||||
}
|
||||
|
||||
public TransactionError executeSellTransaction() {
|
||||
ItemStack itemStack = shop.getItemStack().clone();
|
||||
double price = shop.getPrice();
|
||||
if (orders == itemStack.getMaxStackSize()) {
|
||||
itemStack.setAmount(orders);
|
||||
price = shop.getPricePerItem() * orders;
|
||||
}
|
||||
|
||||
int shopItems = InventoryUtils.countItems(shop.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(shop, 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, shop);
|
||||
if (Util.fireCancellableEvent(playerExchangeShopEvent)) {
|
||||
return TransactionError.CANCELLED;
|
||||
}
|
||||
|
||||
InventoryUtils.removeItem(shop.getInventory(), itemStack);
|
||||
EconomyUtils.removeFunds(player, price);
|
||||
shop.AddBalance(price);
|
||||
InventoryUtils.addItem(player.getInventory(), itemStack);
|
||||
|
||||
player.updateInventory();
|
||||
return TransactionError.NONE;
|
||||
}
|
||||
|
||||
public TransactionError executeBuyTransaction() {
|
||||
// ItemStack is = shop.getItemStack();
|
||||
// double price = shop.getPrice();
|
||||
// if(orders == is.getMaxStackSize()) {
|
||||
// is.setAmount(orders);
|
||||
// price = shop.getPricePerItem() * orders;
|
||||
// }
|
||||
//
|
||||
// //check if player has enough items
|
||||
// if(isCheck) {
|
||||
// int playerItems = InventoryUtils.countItems(player.getInventory(), is);
|
||||
// if (playerItems < is.getAmount())
|
||||
// return TransactionError.INSUFFICIENT_FUNDS_PLAYER;
|
||||
// } else {
|
||||
// //remove items from player
|
||||
// InventoryUtils.removeItem(player.getInventory(), is, player);
|
||||
// }
|
||||
//
|
||||
// //check if shop has enough currency
|
||||
// if(isCheck) {
|
||||
// if (!(shop.getBalance() >= price))
|
||||
// return TransactionError.INSUFFICIENT_FUNDS_SHOP;
|
||||
// } else {
|
||||
// shop.RemoveBalance(price);
|
||||
// }
|
||||
//
|
||||
// if(isCheck) {
|
||||
// if (!player.isOnline())
|
||||
// return TransactionError.PLAYER_OFFLINE;
|
||||
// } else {
|
||||
// //add currency to player
|
||||
// EconomyUtils.addFunds(player, player.getInventory(), price);
|
||||
// }
|
||||
//
|
||||
// //check if shop has enough room to accept items
|
||||
// if(isCheck) {
|
||||
// boolean shopHasRoom = InventoryUtils.hasRoom(this.getInventory(), is, this.getOwner());
|
||||
// if (!shopHasRoom)
|
||||
// return TransactionError.INVENTORY_FULL_SHOP;
|
||||
// } else{
|
||||
// //add items to shop's inventory
|
||||
// InventoryUtils.addItem(this.getInventory(), is, this.getOwner());
|
||||
// }
|
||||
//
|
||||
// player.updateInventory();
|
||||
//
|
||||
// //if there are no issues with the test/check transaction
|
||||
// if(isCheck){
|
||||
//
|
||||
// PlayerExchangeShopEvent e = new PlayerExchangeShopEvent(player, this);
|
||||
// Bukkit.getPluginManager().callEvent(e);
|
||||
//
|
||||
// if(e.isCancelled())
|
||||
// return TransactionError.CANCELLED;
|
||||
//
|
||||
// //run the transaction again without the check clause
|
||||
// return executeTransaction(false);
|
||||
// }
|
||||
return TransactionError.NONE;
|
||||
}
|
||||
|
||||
public TransactionError executeGambleTransaction() {
|
||||
return TransactionError.NONE;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user