rework some stuff

This commit is contained in:
Len
2022-08-13 15:22:00 +02:00
parent d33a0db545
commit cf4431326e
36 changed files with 1080 additions and 597 deletions
@@ -1,105 +0,0 @@
package com.alttd.playershops.shop;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.block.data.Directional;
import org.bukkit.inventory.ItemStack;
import java.util.UUID;
public abstract class AbstractShop {
@Getter
private int id;
private String ownerName;
@Getter
private UUID ownerUUID;
@Getter @Setter
private ShopType type;
@Getter
private Location signLocation;
@Getter
private Location containerLocation;
@Getter
private String server;
@Getter @Setter
private double price;
@Getter @Setter
private int amount;
@Getter @Setter
private double balance;
@Getter @Setter
private ItemStack itemStack;
@Getter @Setter
private ItemStack secondaryItem;
@Getter @Setter
private long lastTransaction;
protected boolean initialized;
AbstractShop(Location signLocation, UUID uuid, double price, int amount) {
this.signLocation = signLocation;
if (signLocation != null) {
Directional sign = (Directional) signLocation.getBlock().getState().getBlockData();
this.containerLocation = signLocation.getBlock().getRelative(sign.getFacing().getOppositeFace()).getLocation();
}
this.ownerUUID = uuid;
ownerName = getOwnerName();
this.price = price;
this.amount = amount;
this.server = Bukkit.getServerName();
}
AbstractShop(int id, String ownerName, UUID ownerUUID, String server,
Location containerLocation, Location signLocation, double price, int amount,
double balance, ItemStack itemOne, ItemStack itemTwo, long lastTransaction) {
this.id = id;
this.ownerName = ownerName;
this.ownerUUID = ownerUUID;
this.server = server;
this.containerLocation = containerLocation;
this.signLocation = signLocation;
this.price = price;
this.amount = amount;
this.balance = balance;
this.itemStack = itemOne;
this.secondaryItem = itemTwo;
this.lastTransaction = lastTransaction;
}
public static AbstractShop create(Location signLocation, UUID player, double price, int amount, ShopType shopType) {
return switch (shopType) {
case SELL -> new SellShop(signLocation, player, price, amount);
case BUY -> new BuyShop(signLocation, player, price, amount);
case GAMBLE -> new GambleShop(signLocation, player, price, amount);
case BARTER -> new BarterShop(signLocation, player, price, amount);
};
}
public static AbstractShop create(int id, String ownerName, UUID ownerUUID, ShopType shopType, String server,
Location containerLocation, Location signLocation, double price, int amount,
double balance, ItemStack itemOne, ItemStack itemTwo, long lastTransaction) {
return switch (shopType) {
case SELL -> new SellShop(id, ownerName, ownerUUID, server, containerLocation, signLocation, price, amount, balance, itemOne, itemTwo, lastTransaction);
case BUY -> new BuyShop(id, ownerName, ownerUUID, server, containerLocation, signLocation, price, amount, balance, itemOne, itemTwo, lastTransaction);
case GAMBLE -> new GambleShop(id, ownerName, ownerUUID, server, containerLocation, signLocation, price, amount, balance, itemOne, itemTwo, lastTransaction);
case BARTER -> new BarterShop(id, ownerName, ownerUUID, server, containerLocation, signLocation, price, amount, balance, itemOne, itemTwo, lastTransaction);
};
}
public String getOwnerName() {
if(this.ownerName != null) return ownerName;
if (this.getOwnerUUID() != null) {
ownerName = Bukkit.getOfflinePlayer(this.getOwnerUUID()).getName();
return ownerName;
}
return ChatColor.RED + "[CLOSED]";
}
public boolean isInitialized() {
return initialized;
}
}
@@ -1,24 +0,0 @@
package com.alttd.playershops.shop;
import org.bukkit.Location;
import org.bukkit.inventory.ItemStack;
import java.util.UUID;
public class BarterShop extends AbstractShop {
public BarterShop(Location location, UUID player, double price, int amount) {
super(location, player, price, amount);
this.setType(ShopType.BARTER);
}
public BarterShop(int id, String ownerName, UUID ownerUUID, String server,
Location containerLocation, Location signLocation, double price, int amount,
double balance, ItemStack itemOne, ItemStack itemTwo, long lastTransaction) {
super(id, ownerName, ownerUUID, server, containerLocation, signLocation, price, amount,
balance, itemOne, itemTwo, lastTransaction);
this.setType(ShopType.BARTER);
}
}
@@ -1,23 +0,0 @@
package com.alttd.playershops.shop;
import org.bukkit.Location;
import org.bukkit.inventory.ItemStack;
import java.util.UUID;
public class BuyShop extends AbstractShop {
public BuyShop(Location location, UUID player, double price, int amount) {
super(location, player, price, amount);
this.setType(ShopType.BUY);
}
public BuyShop(int id, String ownerName, UUID ownerUUID, String server,
Location containerLocation, Location signLocation, double price, int amount,
double balance, ItemStack itemOne, ItemStack itemTwo, long lastTransaction) {
super(id, ownerName, ownerUUID, server, containerLocation, signLocation, price, amount,
balance, itemOne, itemTwo, lastTransaction);
this.setType(ShopType.BUY);
}
}
@@ -1,27 +0,0 @@
package com.alttd.playershops.shop;
import org.bukkit.Location;
import org.bukkit.inventory.ItemStack;
import java.util.UUID;
public class GambleShop extends AbstractShop {
private ItemStack gambleItem;
public GambleShop(Location location, UUID player, double price, int amount) {
super(location, player, price, amount);
this.setType(ShopType.GAMBLE);
this.gambleItem = this.getItemStack();
}
public GambleShop(int id, String ownerName, UUID ownerUUID, String server,
Location containerLocation, Location signLocation, double price, int amount,
double balance, ItemStack itemOne, ItemStack itemTwo, long lastTransaction) {
super(id, ownerName, ownerUUID, server, containerLocation, signLocation, price, amount,
balance, itemOne, itemTwo, lastTransaction);
this.setType(ShopType.GAMBLE);
}
}
@@ -0,0 +1,151 @@
package com.alttd.playershops.shop;
import com.alttd.playershops.PlayerShops;
import com.alttd.playershops.utils.ShopUtil;
import lombok.Getter;
import lombok.Setter;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.Location;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.List;
import java.util.UUID;
public class PlayerShop {
@Getter
private int id;
@Getter @Setter // TODO a way to check if the player changed name and update if needed
private String ownerName;
@Getter
private UUID ownerUUID;
@Getter @Setter
private ShopType type = ShopType.DEFAULT;
@Getter
private final Location signLocation;
@Getter
private final Location shopLocation;
@Getter
private String server;
@Getter @Setter
private double price;
@Getter @Setter
private int amount;
@Getter @Setter
private double balance;
@Getter @Setter
private ItemStack itemStack;
@Getter @Setter
private long lastTransaction;
public PlayerShop(Location shopLocation, Location signLocation, Player player) {
this(shopLocation, signLocation, player.getUniqueId(), player.getName());
}
public PlayerShop(Location shopLocation, Location signLocation, UUID uuid, String playerName) {
this.shopLocation = shopLocation;
this.signLocation = signLocation;
this.ownerUUID = uuid;
this.ownerName = playerName;
updateSign();
}
public static PlayerShop create(int id, String ownerName, UUID ownerUUID, ShopType shopType, String server,
Location shopLocation, Location signLocation, double price, int amount,
double balance, ItemStack item, long lastTransaction) {
PlayerShop playerShop = new PlayerShop(shopLocation, signLocation, ownerUUID, ownerName);
playerShop.id = id;
playerShop.type = shopType;
playerShop.server = server;
playerShop.price = price;
playerShop.amount = amount;
playerShop.balance = balance;
playerShop.itemStack = item;
playerShop.lastTransaction = lastTransaction;
return playerShop;
}
public int getRemainingStock() {
return ShopUtil.countItems(getInventory(), getItemStack());
}
public int getRemainingSpace() {
return ShopUtil.countSpace(getInventory(), getItemStack());
}
public boolean matches(ItemStack item) {
return ShopUtil.matches(getItemStack(), item);
}
public void remove(ItemStack item, int amount) {
Inventory inv = getInventory();
int remains = amount;
while (remains > 0) {
int stackSize = Math.min(remains, item.getMaxStackSize());
item.setAmount(stackSize);
inv.removeItem(item);
remains = remains - stackSize;
}
}
public void add(ItemStack item, int amount) {
Inventory inv = getInventory();
int remains = amount;
while (remains > 0) {
int stackSize = Math.min(remains, item.getMaxStackSize());
item.setAmount(stackSize);
inv.addItem(item);
remains = remains - stackSize;
}
}
public void setOwner(Player player) {
ownerUUID = player.getUniqueId();
ownerName = player.getName();
}
public Inventory getInventory() {
// Could use a check if the block is still an InventoryHolder.
InventoryHolder container = (InventoryHolder) shopLocation.getBlock().getState();
return container.getInventory();
}
public boolean isInitialized() {
return type != ShopType.DEFAULT;
}
public void updateSign() {
new BukkitRunnable() {
public void run() {
Sign signBlock = (Sign) signLocation.getBlock().getState();
MiniMessage miniMessage = MiniMessage.miniMessage();
List<String> signLines;
TagResolver tagResolver = TagResolver.resolver(
Placeholder.unparsed("ownername", getOwnerName()),
Placeholder.unparsed("price", String.valueOf(getPrice())),
Placeholder.unparsed("amount", String.valueOf(getAmount()))
);
if (!isInitialized()) {
signLines = type.getShopTypeConfig().inActiveSignLines;
} else {
signLines = type.getShopTypeConfig().activeSignLines;
}
for (int i = 0; i < 4; i++) {
signBlock.line(i, miniMessage.deserialize(signLines.get(i), tagResolver));
}
signBlock.update(true);
}
}.runTaskLater(PlayerShops.getInstance(), 2L);
}
}
@@ -1,24 +0,0 @@
package com.alttd.playershops.shop;
import org.bukkit.Location;
import org.bukkit.inventory.ItemStack;
import java.util.UUID;
public class SellShop extends AbstractShop {
public SellShop(Location location, UUID player, double price, int amount) {
super(location, player, price, amount);
this.setType(ShopType.SELL);
}
public SellShop(int id, String ownerName, UUID ownerUUID, String server,
Location containerLocation, Location signLocation, double price, int amount,
double balance, ItemStack itemOne, ItemStack itemTwo, long lastTransaction) {
super(id, ownerName, ownerUUID, server, containerLocation, signLocation, price, amount,
balance, itemOne, itemTwo, lastTransaction);
this.setType(ShopType.SELL);
}
}
@@ -0,0 +1,8 @@
package com.alttd.playershops.shop;
public enum ShopAction {
BUY,
SELL,
CREATE,
CANCELLED;
}
@@ -0,0 +1,4 @@
package com.alttd.playershops.shop;
public class ShopInfo {
}
@@ -1,10 +0,0 @@
package com.alttd.playershops.shop;
public enum ShopTransactionError {
CANCELLED,
INSUFFICIENT_FUNDS_SHOP,
INSUFFICIENT_FUNDS_PLAYER,
INVENTORY_FULL_SHOP,
INVENTORY_FULL_PLAYER,
NONE;
}
@@ -1,10 +1,22 @@
package com.alttd.playershops.shop;
import com.alttd.playershops.config.ShopTypeConfig;
public enum ShopType {
SELL,
BUY,
GAMBLE,
BARTER;
DEFAULT(),
SELL(),
BUY(),
GAMBLE();
private ShopTypeConfig shopTypeConfig;
ShopType() {
this.shopTypeConfig = new ShopTypeConfig(this.toString());
}
public ShopTypeConfig getShopTypeConfig() {
return shopTypeConfig;
}
@Override
public String toString() {