Merge pull request #11 from Opalinium/main

Added /buy command with dynamic max purchase limit
This commit is contained in:
Opal
2023-06-19 01:46:53 -07:00
committed by GitHub
5 changed files with 112 additions and 1 deletions

View File

@@ -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.interfaces.Config
import com.willfp.eco.core.config.readConfig import com.willfp.eco.core.config.readConfig
import com.willfp.eco.core.integrations.shop.ShopManager 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.CommandEcoShop
import com.willfp.ecoshop.commands.CommandSell import com.willfp.ecoshop.commands.CommandSell
import com.willfp.ecoshop.config.UsermadeConfig import com.willfp.ecoshop.config.UsermadeConfig
@@ -89,7 +90,8 @@ class EcoShopPlugin : EcoPlugin() {
override fun loadPluginCommands(): List<PluginCommand> { override fun loadPluginCommands(): List<PluginCommand> {
return listOf( return listOf(
CommandEcoShop(this), CommandEcoShop(this),
CommandSell(this) CommandSell(this),
CommandBuy(this)
) )
} }

View File

@@ -0,0 +1,102 @@
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")
override fun onExecute(player: Player, args: List<String>) {
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<String>): List<String> {
val completions = mutableListOf<String>()
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
}
}

View File

@@ -286,3 +286,5 @@ sell-gui:
lore: lore:
- "&fWhen you close this menu, all" - "&fWhen you close this menu, all"
- "&fitems inside it will be sold!" - "&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

View File

@@ -10,6 +10,8 @@ messages:
must-specify-item: "&cYou must specify an item!" must-specify-item: "&cYou must specify an item!"
invalid-item: "&cInvalid item!" invalid-item: "&cInvalid item!"
reset-buys: "&fReset how many times &r%player%&r&f bought &r%item%&f!" 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: sell-status:
cannot-sell: "&cThis item can't be sold!" cannot-sell: "&cThis item can't be sold!"

View File

@@ -21,6 +21,9 @@ commands:
sell: sell:
description: Sells items description: Sells items
permission: ecoshop.command.sell permission: ecoshop.command.sell
buy:
description: Buys items
permission: ecoshop.command.buy
permissions: permissions:
ecoshop.command.sell.*: ecoshop.command.sell.*: