pull/6/head
jakub 10 months ago
parent 319b29c56e
commit ad3b7bdc90
  1. 43
      src/main/java/xyz/soukup/ecoCraftCore/commands/ShopCommand.java
  2. 44
      src/main/java/xyz/soukup/ecoCraftCore/objects/Shop.java
  3. 18
      src/main/java/xyz/soukup/ecoCraftCore/objects/VirtualChest.java

@ -9,7 +9,12 @@ import org.bukkit.Location;
import org.bukkit.block.Chest;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import xyz.soukup.ecoCraftCore.events.RulerMarking;
import xyz.soukup.ecoCraftCore.objects.Shop;
import xyz.soukup.ecoCraftCore.objects.VirtualChest;
import xyz.soukup.ecoCraftCore.utilities.InventoryUtils;
import xyz.soukup.ecoCraftCore.utilities.PDC;
public class ShopCommand {
public static LiteralArgumentBuilder<CommandSourceStack> createCommand() {
@ -41,15 +46,47 @@ public class ShopCommand {
Chest chest = RulerMarking.chests.get(player);
Sign sign = RulerMarking.signs.get(player);
Integer amount = ctx.getArgument("amount", Integer.class);
Float buyPrice = ctx.getArgument("buy_price", Float.class);
Float sellPrice = ctx.getArgument("sell_price", Float.class);
Float buyPrice;
try {
buyPrice = ctx.getArgument("buy_price", Float.class);
} catch (IllegalArgumentException e) {
buyPrice = 0F;
}
Float sellPrice;
try {
sellPrice = ctx.getArgument("sell_price", Float.class);
}catch (IllegalArgumentException e){
sellPrice = 0F;
}
if (sign == null || chest == null){
player.sendPlainMessage("nemáš označeno");
return 0;
}
ctx.getSource().getSender().sendPlainMessage("buy");
ItemStack item = player.getInventory().getItemInMainHand();
if (item.isEmpty()){
player.sendPlainMessage("nemáš item v ruke");
return 0;
}
VirtualChest virtualChest = new VirtualChest(chest);
virtualChest.save();
PDC.set(chest, "virtual", virtualChest.getId());
int freeSpace = InventoryUtils.getSpaceLeft(chest.getInventory(), item);
int stock = InventoryUtils.getCount(chest.getInventory(), item);
Shop shop = new Shop(player, item, stock, freeSpace, amount, buyPrice, sellPrice, virtualChest.getId());
shop.save();
shop.writeIntoSign(sign);
return 1;
}

@ -1,19 +1,27 @@
package xyz.soukup.ecoCraftCore.objects;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import net.kyori.adventure.text.Component;
import org.bukkit.block.Sign;
import org.bukkit.block.TileState;
import org.bukkit.block.sign.Side;
import org.bukkit.block.sign.SignSide;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import xyz.soukup.ecoCraftCore.utilities.DaoRegistry;
import xyz.soukup.ecoCraftCore.utilities.JSONConverter;
import xyz.soukup.ecoCraftCore.utilities.PDC;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
@DatabaseTable(tableName = "shops")
public class Shop {
@DatabaseField(generatedId = true)
private long id;
private int id;
@DatabaseField(canBeNull = false)
private String owner;
@ -40,6 +48,8 @@ public class Shop {
@DatabaseField(canBeNull = false, defaultValue = "0")
private int stock;
@DatabaseField(canBeNull = false, defaultValue = "0", columnName = "free_space")
private int freeSpace;
@DatabaseField(canBeNull = false)
private int virtualChestID;
@ -54,12 +64,13 @@ public class Shop {
}
public Shop(Player player, ItemStack itemStack, int stock, int amount, float priceBuy, float priceSell, int virtualChestID) {
public Shop(Player player, ItemStack itemStack, int stock, int freeSpace, int amount, float priceBuy, float priceSell, int virtualChestID) {
this.ownerType = "player";
this.owner = player.getName();
this.itemStackJson = JSONConverter.itemStackToJson(itemStack);
this.itemName = itemStack.getType().toString();
this.stock = stock;
this.freeSpace = freeSpace;
this.amount = amount;
this.priceBuy = priceBuy;
this.priceSell = priceSell;
@ -110,6 +121,35 @@ public class Shop {
return JSONConverter.itemStackFromJson(this.itemStackJson);
}
public void writeIntoSign(Sign sign){
PDC.set(sign, "shop", this.id);
Component prices;
Component shopType;
if (this.priceBuy != 0 && this.priceSell != 0){
shopType = Component.text("[ buy / sell ]");
prices = Component.text(String.format("%.2fŧ / %.2fŧ", this.priceBuy, this.priceSell));
} else if (this.priceBuy != 0) {
shopType = Component.text("[ buy ]");
prices = Component.text(String.format("%.2fŧ", this.priceBuy));
} else {
shopType = Component.text("[ sell ]");
prices = Component.text(String.format("%.2fŧ", this.priceSell));
}
@NotNull SignSide side = sign.getSide(Side.FRONT);
side.line(0, Component.text(this.owner));
side.line(1, Component.text(this.itemName));
side.line(2, shopType);
side.line(3, prices);
sign.update();
}
public long getId() {
return id;
}

@ -16,7 +16,7 @@ import java.sql.SQLException;
public class VirtualChest {
@DatabaseField(generatedId = true)
private long id;
private int id;
@DatabaseField(columnName = "inventory")
private String inventoryJson;
@ -66,7 +66,23 @@ public class VirtualChest {
return this.inventory;
}
public int getId() {
return id;
}
public Boolean getOpened() {
return isOpened;
}
public String getActiveServer() {
return activeServer;
}
public void setOpened(Boolean opened) {
isOpened = opened;
}
public void setActiveServer(String activeServer) {
this.activeServer = activeServer;
}
}

Loading…
Cancel
Save