diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoshop/EcoShopPlugin.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoshop/EcoShopPlugin.kt index bab6721..d8dd9ef 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoshop/EcoShopPlugin.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoshop/EcoShopPlugin.kt @@ -6,6 +6,7 @@ import com.willfp.eco.core.config.ConfigType import com.willfp.eco.core.config.interfaces.Config import com.willfp.eco.core.config.readConfig import com.willfp.eco.core.integrations.shop.ShopManager +import com.willfp.ecoshop.commands.CommandBuy import com.willfp.ecoshop.commands.CommandEcoShop import com.willfp.ecoshop.commands.CommandSell import com.willfp.ecoshop.config.UsermadeConfig @@ -89,7 +90,8 @@ class EcoShopPlugin : EcoPlugin() { override fun loadPluginCommands(): List { return listOf( CommandEcoShop(this), - CommandSell(this) + CommandSell(this), + CommandBuy(this) ) } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoshop/commands/CommandBuy.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoshop/commands/CommandBuy.kt new file mode 100644 index 0000000..aa16345 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoshop/commands/CommandBuy.kt @@ -0,0 +1,104 @@ +package com.willfp.ecoshop.commands + +import com.willfp.eco.core.EcoPlugin +import com.willfp.eco.core.command.impl.PluginCommand +import com.willfp.ecoshop.EcoShopPlugin +import com.willfp.ecoshop.shop.* +import org.bukkit.command.CommandSender +import org.bukkit.entity.Player +import org.bukkit.util.StringUtil + +class CommandBuy(plugin: EcoPlugin) : PluginCommand( + plugin, + "buy", + "ecoshop.command.buy", + true +) { + private fun getMaxPurchaseAmount() = EcoShopPlugin.instance.configYml.getInt("max-purchase-amount") + + private val maxPurchaseAmount = EcoShopPlugin.instance.configYml.getInt("max-purchase-amount") + + override fun onExecute(player: Player, args: List) { + if (args.isEmpty()) { + player.sendMessage(EcoShopPlugin.instance.langYml.getMessage("must-specify-item")) + return + } + + val itemId = args[0] + val shopItem = ShopItems.getByID(itemId) + + if (shopItem == null) { + player.sendMessage(EcoShopPlugin.instance.langYml.getMessage("invalid-item")) + return + } + + val buyType = BuyType.NORMAL // Or get this from the command arguments + val maxPurchaseAmount = getMaxPurchaseAmount() + val amount: Int = if (args.size > 1) { + try { + args[1].toInt().also { + if (it <= 0) throw NumberFormatException() + } + } catch (e: NumberFormatException) { + player.sendMessage(EcoShopPlugin.instance.langYml.getMessage("invalid-amount")) + return + } + } else { + 1 // Default amount + } + + // Check if the amount exceeds the maximum limit + if (amount > maxPurchaseAmount) { + player.sendMessage(EcoShopPlugin.instance.langYml.getMessage("max-purchase-amount-exceeded").replace("%maxAmount%", maxPurchaseAmount.toString())) + return + } + + val buyStatus = shopItem.getBuyStatus(player, amount, buyType) + if (buyStatus != BuyStatus.ALLOW) { + player.sendMessage( + EcoShopPlugin.instance.langYml.getMessage("buy-status.${buyStatus.configKey}") + .replace("%price%", shopItem.getBuyPrice(buyType).getDisplay(player, amount)) + ) + return + } + + shopItem.buy(player, amount, buyType) + if (amount > 1) { + player.sendMessage( + EcoShopPlugin.instance.langYml.getMessage("bought-item-multiple") + .replace("%amount%", amount.toString()) + .replace("%item%", shopItem.displayName) + .replace("%price%", shopItem.getBuyPrice(buyType).getDisplay(player, amount)) + ) + } else { + player.sendMessage( + EcoShopPlugin.instance.langYml.getMessage("bought-item") + .replace("%item%", shopItem.displayName) + .replace("%price%", shopItem.getBuyPrice(buyType).getDisplay(player, amount)) + ) + } + } + + override fun tabComplete(sender: CommandSender, args: List): List { + val completions = mutableListOf() + + if (args.isEmpty()) { + return ShopItems.values().map { it.id } + } + + if (args.size == 1) { + StringUtil.copyPartialMatches( + args[0], + ShopItems.values().map { it.id }, + completions + ) + } + + if (args.size == 2) { + val maxPurchaseAmount = getMaxPurchaseAmount() + completions.addAll((1..maxPurchaseAmount).map { it.toString() }) + } + + return completions + } +} \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/resources/config.yml b/eco-core/core-plugin/src/main/resources/config.yml index 618b43a..e96ab28 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -286,3 +286,5 @@ sell-gui: lore: - "&fWhen you close this menu, all" - "&fitems inside it will be sold!" + +max-purchase-amount: 64 # The maximum amount of items a player can buy at once via the /buy command \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/resources/lang.yml b/eco-core/core-plugin/src/main/resources/lang.yml index b6dd9ec..a1c284c 100644 --- a/eco-core/core-plugin/src/main/resources/lang.yml +++ b/eco-core/core-plugin/src/main/resources/lang.yml @@ -10,6 +10,8 @@ messages: must-specify-item: "&cYou must specify an item!" invalid-item: "&cInvalid item!" reset-buys: "&fReset how many times &r%player%&r&f bought &r%item%&f!" + invalid-amount: "&cInvalid amount!" + max-purchase-amount-exceeded: "&cYou can't buy more than %maxAmount% of an item at once!" sell-status: cannot-sell: "&cThis item can't be sold!" diff --git a/eco-core/core-plugin/src/main/resources/plugin.yml b/eco-core/core-plugin/src/main/resources/plugin.yml index ab3658e..acba641 100644 --- a/eco-core/core-plugin/src/main/resources/plugin.yml +++ b/eco-core/core-plugin/src/main/resources/plugin.yml @@ -21,6 +21,9 @@ commands: sell: description: Sells items permission: ecoshop.command.sell + buy: + description: Buys items + permission: ecoshop.command.buy permissions: ecoshop.command.sell.*: