Compare commits
1 Commits
master
...
island-exp
| Author | SHA1 | Date |
|---|---|---|
|
|
e6cdf545e8 | 1 month ago |
5 changed files with 144 additions and 9 deletions
@ -0,0 +1,107 @@ |
||||
package xyz.soukup.ecoCraftCore.shop; |
||||
|
||||
import com.mojang.brigadier.arguments.*; |
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder; |
||||
import com.mojang.brigadier.context.CommandContext; |
||||
import io.papermc.paper.command.brigadier.CommandSourceStack; |
||||
import io.papermc.paper.command.brigadier.Commands; |
||||
import org.bukkit.block.Chest; |
||||
import org.bukkit.block.Sign; |
||||
import org.bukkit.entity.Player; |
||||
import org.bukkit.inventory.ItemStack; |
||||
import xyz.soukup.ecoCraftCore.positionMarker.MarkerEvent; |
||||
import xyz.soukup.ecoCraftCore.database.objects.Shop; |
||||
import xyz.soukup.ecoCraftCore.inventory.VirtualChest; |
||||
import xyz.soukup.ecoCraftCore.inventory.InventoryUtils; |
||||
import xyz.soukup.ecoCraftCore.messages.Messages; |
||||
import xyz.soukup.ecoCraftCore.utilities.PDC; |
||||
|
||||
public class ShopAdminCommand { |
||||
|
||||
public static LiteralArgumentBuilder<CommandSourceStack> getCommand() { |
||||
// Define the argument types
|
||||
var amountArg = Commands.argument("amount", IntegerArgumentType.integer(1)); |
||||
var buyPriceArg = Commands.argument("buy_price", FloatArgumentType.floatArg(0.0F)); |
||||
var sellPriceArg = Commands.argument("sell_price", FloatArgumentType.floatArg(0.0F)); |
||||
|
||||
// Path: /shop <amount> ...
|
||||
return Commands.literal("shop-admin") |
||||
.then(amountArg |
||||
// Branch 1: ... buy <price> [sell <price>]
|
||||
.then(Commands.literal("buy") |
||||
.then(buyPriceArg |
||||
.executes(ShopAdminCommand::createShop) |
||||
.then(Commands.literal("sell") |
||||
.then(sellPriceArg.executes(ShopAdminCommand::createShop))))) |
||||
|
||||
// Branch 2: ... sell <price> [buy <price>]
|
||||
.then(Commands.literal("sell") |
||||
.then(sellPriceArg |
||||
.executes(ShopAdminCommand::createShop) |
||||
.then(Commands.literal("buy") |
||||
.then(buyPriceArg.executes(ShopAdminCommand::createShop)))))); |
||||
} |
||||
|
||||
private static int createShop(CommandContext<CommandSourceStack> ctx) { |
||||
CommandSourceStack source = ctx.getSource(); |
||||
|
||||
if (!(source.getSender() instanceof Player player)) { |
||||
Messages.send(source.getSender(), "generic.error.not-player"); |
||||
return 0; |
||||
} |
||||
|
||||
// 1. Check if blocks are marked
|
||||
Chest chest = MarkerEvent.chests.get(player); |
||||
Sign sign = MarkerEvent.signs.get(player); |
||||
|
||||
if (sign == null || chest == null) { |
||||
Messages.send(player, "shop.error.not-marked"); |
||||
return 0; |
||||
} |
||||
|
||||
// 2. Validate Item in Hand
|
||||
ItemStack item = player.getInventory().getItemInMainHand(); |
||||
if (item.isEmpty()) { |
||||
Messages.send(player, "shop.error.empty-hand"); |
||||
return 0; |
||||
} |
||||
|
||||
// 3. Check if already a shop
|
||||
if (PDC.getInteger(sign, "shop") != null) { |
||||
Messages.send(player, "shop.error.already-shop"); |
||||
return 0; |
||||
} |
||||
|
||||
// 4. Parse Arguments Safely
|
||||
int amount = ctx.getArgument("amount", Integer.class); |
||||
|
||||
float buyPrice = 0.0F; |
||||
try { |
||||
buyPrice = ctx.getArgument("buy_price", Float.class); |
||||
} catch (IllegalArgumentException ignored) {} |
||||
|
||||
float sellPrice = 0.0F; |
||||
try { |
||||
sellPrice = ctx.getArgument("sell_price", Float.class); |
||||
} catch (IllegalArgumentException ignored) {} |
||||
|
||||
// 5. Database and PDC Logic
|
||||
VirtualChest virtualChest = VirtualChest.getOrCreate(chest); |
||||
virtualChest.databaseSave(); |
||||
PDC.set(chest, "virtual", virtualChest.getId()); |
||||
|
||||
// 6. Inventory Calculations
|
||||
int freeSpace = InventoryUtils.getSpaceLeft(chest.getInventory(), item); |
||||
int stock = InventoryUtils.getCount(chest.getInventory(), item); |
||||
|
||||
// 7. Final Creation
|
||||
Shop shop = new Shop(player, item, stock, freeSpace, amount, buyPrice, sellPrice, virtualChest.getId()); |
||||
shop.setOwnerType("admin"); |
||||
shop.setOwner("admin"); |
||||
shop.save(); |
||||
shop.writeIntoSign(sign); |
||||
|
||||
Messages.send(player, "shop.created"); |
||||
return 1; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue