Work on ShopTransactions
This commit is contained in:
parent
0f3ec2c575
commit
4fb4ea4a79
|
|
@ -1,13 +1,124 @@
|
||||||
package com.alttd.playershops.shop;
|
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 {
|
public record ShopTransaction(int orders, Player player, PlayerShop shop) {
|
||||||
CANCELLED,
|
|
||||||
INSUFFICIENT_FUNDS_SHOP,
|
public TransactionError executeTransaction(boolean isCheck) {
|
||||||
INSUFFICIENT_FUNDS_PLAYER,
|
return switch (shop.getType()) {
|
||||||
INVENTORY_FULL_SHOP,
|
case SELL -> executeSellTransaction();
|
||||||
INVENTORY_FULL_PLAYER,
|
case BUY -> executeBuyTransaction();
|
||||||
NONE;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
@ -2,6 +2,7 @@ package com.alttd.playershops.utils;
|
||||||
|
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.World;
|
||||||
import org.bukkit.configuration.InvalidConfigurationException;
|
import org.bukkit.configuration.InvalidConfigurationException;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.bukkit.enchantments.Enchantment;
|
import org.bukkit.enchantments.Enchantment;
|
||||||
|
|
@ -13,28 +14,6 @@ import java.util.Map;
|
||||||
|
|
||||||
public class ShopUtil {
|
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.
|
* 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.
|
* @return true if the given location is loaded or not.
|
||||||
*/
|
*/
|
||||||
public static boolean isLoaded(Location loc) {
|
public static boolean isLoaded(Location loc) {
|
||||||
// System.out.println("Checking isLoaded(Location loc)");
|
if (!loc.isWorldLoaded()) {
|
||||||
if (loc.getWorld() == null) {
|
|
||||||
// System.out.println("Is not loaded. (No world)");
|
|
||||||
return false;
|
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 x = (int) Math.floor((loc.getBlockX()) / 16.0);
|
||||||
int z = (int) Math.floor((loc.getBlockZ()) / 16.0);
|
int z = (int) Math.floor((loc.getBlockZ()) / 16.0);
|
||||||
if (loc.getWorld().isChunkLoaded(x, z)) {
|
return (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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -149,4 +118,6 @@ public class ShopUtil {
|
||||||
ItemStack stack = cfg.getItemStack("item");
|
ItemStack stack = cfg.getItemStack("item");
|
||||||
return stack;
|
return stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user