Implement gamble shops

This commit is contained in:
Len 2024-07-31 18:52:59 +02:00 committed by destro174
parent 4e1f2d57bc
commit 6aa2e294b4
3 changed files with 69 additions and 24 deletions

View File

@ -71,7 +71,7 @@ public class ConversationManager implements ConversationAbandonedListener {
private class ChangeTypePrompt extends FixedSetPrompt {
public ChangeTypePrompt() {
super("buy", "sell", "none");
super("buy", "sell", "none", "gamble");
}
public @NotNull Component getPromptMessage(ConversationContext context) {

View File

@ -17,42 +17,30 @@ import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;
@Getter
public class PlayerShop {
@Getter
private UUID shopID;
@Getter @Setter // TODO a way to check if the player changed name and update if needed
@Setter // TODO a way to check if the player changed name and update if needed
private String ownerName;
@Getter
private UUID ownerUUID;
@Getter
private ShopType type = ShopType.NONE;
@Getter
private final Location signLocation;
@Getter
private final Location shopLocation;
@Getter
private String server;
@Getter
private double price;
@Getter
private int amount;
@Getter
private double balance;
@Getter
private ItemStack itemStack;
@Getter @Setter
@Setter
private long lastTransaction;
@Getter @Setter
@Setter
private boolean notifiedOwner = false;
@Getter @Setter
@Setter
private boolean dirty;
@Getter
private List<ShopTransaction> transactions = new ArrayList<>();
private final List<ShopTransaction> transactions = new ArrayList<>();
public PlayerShop(Location shopLocation, Location signLocation, Player player) {
this(shopLocation, signLocation, player.getUniqueId(), player.getName());
@ -101,8 +89,7 @@ public class PlayerShop {
return (int) (balance / getPrice());
}
case GAMBLE -> {
Logger.warn("Tried to call getRemainingStock on unimplemented GAMBLE type");
return 0; //not implemented since gamble shops don't exist yet
return getInventory().getContents().length;
}
default -> {
return 0;
@ -231,7 +218,7 @@ public class PlayerShop {
return switch (getType()) {
case SELL -> executeSellTransaction(orders, player);
case BUY -> executeBuyTransaction(orders, player);
case GAMBLE -> executeGambleTransaction(orders, player);
case GAMBLE -> executeGambleTransaction(player);
default -> TransactionError.NONE; // This should not happen
};
}
@ -318,7 +305,44 @@ public class PlayerShop {
return TransactionError.NONE;
}
private TransactionError executeGambleTransaction(int orders, Player player) {
private TransactionError executeGambleTransaction(Player player) {
ItemStack itemStack = getRandomItem();
if (itemStack == null)
return null;
itemStack = itemStack.clone();
double price = getPrice();
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;
}
if (!addBalance(price))
return TransactionError.CANCELLED;
InventoryUtils.removeItem(getInventory(), itemStack);
EconomyUtils.removeFunds(player, price);
InventoryUtils.addItem(player.getInventory(), itemStack);
player.updateInventory();
addTransaction(new ShopTransaction(ShopAction.GAMBLE, player.getName(), 1, price));
return TransactionError.NONE;
}
@ -388,6 +412,26 @@ public class PlayerShop {
transactions.add(transaction);
}
private ItemStack getRandomItem() {
if (!isInitialized())
return null;
if (this.getInventory().isEmpty())
return null;
ArrayList<ItemStack> contents = new ArrayList<>();
for (ItemStack it : this.getInventory().getContents()) {
if (it != null) {
contents.add(it);
}
}
if (contents.isEmpty())
return null;
Collections.shuffle(contents);
int index = new Random().nextInt(contents.size());
return contents.get(index);
}
@Override
public String toString() {
return "Shop: " + this.getShopID()

View File

@ -3,6 +3,7 @@ package com.alttd.playershops.shop;
public enum ShopAction {
BUY,
SELL,
GAMBLE,
CREATE,
CANCELLED;
}