Implement gamble shops
This commit is contained in:
parent
4e1f2d57bc
commit
6aa2e294b4
|
|
@ -71,7 +71,7 @@ public class ConversationManager implements ConversationAbandonedListener {
|
||||||
private class ChangeTypePrompt extends FixedSetPrompt {
|
private class ChangeTypePrompt extends FixedSetPrompt {
|
||||||
|
|
||||||
public ChangeTypePrompt() {
|
public ChangeTypePrompt() {
|
||||||
super("buy", "sell", "none");
|
super("buy", "sell", "none", "gamble");
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull Component getPromptMessage(ConversationContext context) {
|
public @NotNull Component getPromptMessage(ConversationContext context) {
|
||||||
|
|
|
||||||
|
|
@ -17,42 +17,30 @@ import org.bukkit.inventory.InventoryHolder;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
|
@Getter
|
||||||
public class PlayerShop {
|
public class PlayerShop {
|
||||||
|
|
||||||
@Getter
|
|
||||||
private UUID shopID;
|
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;
|
private String ownerName;
|
||||||
@Getter
|
|
||||||
private UUID ownerUUID;
|
private UUID ownerUUID;
|
||||||
@Getter
|
|
||||||
private ShopType type = ShopType.NONE;
|
private ShopType type = ShopType.NONE;
|
||||||
@Getter
|
|
||||||
private final Location signLocation;
|
private final Location signLocation;
|
||||||
@Getter
|
|
||||||
private final Location shopLocation;
|
private final Location shopLocation;
|
||||||
@Getter
|
|
||||||
private String server;
|
private String server;
|
||||||
@Getter
|
|
||||||
private double price;
|
private double price;
|
||||||
@Getter
|
|
||||||
private int amount;
|
private int amount;
|
||||||
@Getter
|
|
||||||
private double balance;
|
private double balance;
|
||||||
@Getter
|
|
||||||
private ItemStack itemStack;
|
private ItemStack itemStack;
|
||||||
@Getter @Setter
|
@Setter
|
||||||
private long lastTransaction;
|
private long lastTransaction;
|
||||||
@Getter @Setter
|
@Setter
|
||||||
private boolean notifiedOwner = false;
|
private boolean notifiedOwner = false;
|
||||||
@Getter @Setter
|
@Setter
|
||||||
private boolean dirty;
|
private boolean dirty;
|
||||||
@Getter
|
private final List<ShopTransaction> transactions = new ArrayList<>();
|
||||||
private List<ShopTransaction> transactions = new ArrayList<>();
|
|
||||||
|
|
||||||
public PlayerShop(Location shopLocation, Location signLocation, Player player) {
|
public PlayerShop(Location shopLocation, Location signLocation, Player player) {
|
||||||
this(shopLocation, signLocation, player.getUniqueId(), player.getName());
|
this(shopLocation, signLocation, player.getUniqueId(), player.getName());
|
||||||
|
|
@ -101,8 +89,7 @@ public class PlayerShop {
|
||||||
return (int) (balance / getPrice());
|
return (int) (balance / getPrice());
|
||||||
}
|
}
|
||||||
case GAMBLE -> {
|
case GAMBLE -> {
|
||||||
Logger.warn("Tried to call getRemainingStock on unimplemented GAMBLE type");
|
return getInventory().getContents().length;
|
||||||
return 0; //not implemented since gamble shops don't exist yet
|
|
||||||
}
|
}
|
||||||
default -> {
|
default -> {
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -231,7 +218,7 @@ public class PlayerShop {
|
||||||
return switch (getType()) {
|
return switch (getType()) {
|
||||||
case SELL -> executeSellTransaction(orders, player);
|
case SELL -> executeSellTransaction(orders, player);
|
||||||
case BUY -> executeBuyTransaction(orders, player);
|
case BUY -> executeBuyTransaction(orders, player);
|
||||||
case GAMBLE -> executeGambleTransaction(orders, player);
|
case GAMBLE -> executeGambleTransaction(player);
|
||||||
default -> TransactionError.NONE; // This should not happen
|
default -> TransactionError.NONE; // This should not happen
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -318,7 +305,44 @@ public class PlayerShop {
|
||||||
return TransactionError.NONE;
|
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;
|
return TransactionError.NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -388,6 +412,26 @@ public class PlayerShop {
|
||||||
transactions.add(transaction);
|
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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Shop: " + this.getShopID()
|
return "Shop: " + this.getShopID()
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package com.alttd.playershops.shop;
|
||||||
public enum ShopAction {
|
public enum ShopAction {
|
||||||
BUY,
|
BUY,
|
||||||
SELL,
|
SELL,
|
||||||
|
GAMBLE,
|
||||||
CREATE,
|
CREATE,
|
||||||
CANCELLED;
|
CANCELLED;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user