Work on ShopTransactions

This commit is contained in:
Len 2022-08-21 00:19:42 +02:00
parent 0f3ec2c575
commit 4fb4ea4a79
3 changed files with 135 additions and 42 deletions

View File

@ -1,13 +1,124 @@
package com.alttd.playershops.shop;
public class ShopTransaction {
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 enum ShopTransactionError {
CANCELLED,
INSUFFICIENT_FUNDS_SHOP,
INSUFFICIENT_FUNDS_PLAYER,
INVENTORY_FULL_SHOP,
INVENTORY_FULL_PLAYER,
NONE;
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;
}
}

View File

@ -0,0 +1,11 @@
package com.alttd.playershops.shop;
public enum TransactionError {
CANCELLED,
INSUFFICIENT_FUNDS_SHOP,
INSUFFICIENT_FUNDS_PLAYER,
INVENTORY_FULL_SHOP,
INVENTORY_FULL_PLAYER,
PLAYER_OFFLINE,
NONE;
}

View File

@ -2,6 +2,7 @@ package com.alttd.playershops.utils;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.enchantments.Enchantment;
@ -13,28 +14,6 @@ import java.util.Map;
public class ShopUtil {
/**
* Counts the number of items in the given inventory where
* Util.matches(inventory item, item) is true.
*
* @param inv
* The inventory to search
* @param item
* The ItemStack to search for
* @return The number of items that match in this inventory.
*/
public static int countItems(Inventory inv, ItemStack item) {
int items = 0;
for (ItemStack iStack : inv.getContents()) {
if (iStack == null)
continue;
if (ShopUtil.matches(item, iStack)) {
items += iStack.getAmount();
}
}
return items;
}
/**
* Returns true if the given location is loaded or not.
*
@ -43,22 +22,12 @@ public class ShopUtil {
* @return true if the given location is loaded or not.
*/
public static boolean isLoaded(Location loc) {
// System.out.println("Checking isLoaded(Location loc)");
if (loc.getWorld() == null) {
// System.out.println("Is not loaded. (No world)");
if (!loc.isWorldLoaded()) {
return false;
}
// Calculate the chunks coordinates. These are 1,2,3 for each chunk, NOT
// location rounded to the nearest 16.
int x = (int) Math.floor((loc.getBlockX()) / 16.0);
int z = (int) Math.floor((loc.getBlockZ()) / 16.0);
if (loc.getWorld().isChunkLoaded(x, z)) {
// System.out.println("Chunk is loaded " + x + ", " + z);
return true;
} else {
// System.out.println("Chunk is NOT loaded " + x + ", " + z);
return false;
}
return (loc.getWorld().isChunkLoaded(x, z));
}
/**
@ -149,4 +118,6 @@ public class ShopUtil {
ItemStack stack = cfg.getItemStack("item");
return stack;
}
}