Added item buy/sell conditions

This commit is contained in:
Auxilor
2023-11-22 15:27:58 +00:00
parent f8ce92e8d3
commit 297e8b5501
2 changed files with 51 additions and 3 deletions

View File

@@ -22,7 +22,10 @@ import com.willfp.ecoshop.event.EcoShopSellEvent
import com.willfp.ecoshop.shop.gui.BuyMenu import com.willfp.ecoshop.shop.gui.BuyMenu
import com.willfp.ecoshop.shop.gui.SellMenu import com.willfp.ecoshop.shop.gui.SellMenu
import com.willfp.ecoshop.shop.gui.ShopItemSlot import com.willfp.ecoshop.shop.gui.ShopItemSlot
import com.willfp.libreforge.BlankHolder.conditions
import com.willfp.libreforge.EmptyProvidedHolder
import com.willfp.libreforge.ViolationContext import com.willfp.libreforge.ViolationContext
import com.willfp.libreforge.conditions.Conditions
import com.willfp.libreforge.effects.Effects import com.willfp.libreforge.effects.Effects
import com.willfp.libreforge.toDispatcher import com.willfp.libreforge.toDispatcher
import com.willfp.libreforge.triggers.TriggerData import com.willfp.libreforge.triggers.TriggerData
@@ -47,13 +50,15 @@ class ShopItem(
) : KRegistrable { ) : KRegistrable {
override val id = config.getString("id") override val id = config.getString("id")
private val context = ViolationContext(plugin, "shop item $id")
val commands = config.getStrings("command") + config.getStrings("commands") val commands = config.getStrings("command") + config.getStrings("commands")
val item = if (config.has("item")) Items.lookup(config.getString("item")) else null val item = if (config.has("item")) Items.lookup(config.getString("item")) else null
val effects = Effects.compileChain( val effects = Effects.compileChain(
config.getSubsections("effects"), config.getSubsections("effects"),
ViolationContext(plugin, "shop item $id") context.with("effects")
) )
val buyAmount = config.getIntOrNull("buy.amount") ?: 1 val buyAmount = config.getIntOrNull("buy.amount") ?: 1
@@ -96,6 +101,21 @@ class ShopItem(
val sellPrice = ConfiguredPrice.create(config.getSubsection("sell")) val sellPrice = ConfiguredPrice.create(config.getSubsection("sell"))
val buyConditions = Conditions.compile(
config.getSubsections("buy.conditions"),
context.with("buy conditions")
)
val altBuyConditions = Conditions.compile(
config.getSubsections("alt-buy.conditions"),
context.with("alt buy conditions")
)
val sellConditions = Conditions.compile(
config.getSubsections("sell.conditions"),
context.with("sell conditions")
)
val slot = ShopItemSlot(this, plugin) val slot = ShopItemSlot(this, plugin)
val isShowingQuickBuySell = config.getBoolOrNull("gui.show-quick-buy-sell") ?: true val isShowingQuickBuySell = config.getBoolOrNull("gui.show-quick-buy-sell") ?: true
@@ -243,6 +263,20 @@ class ShopItem(
} }
} }
val conditions = if (buyType == BuyType.ALT) altBuyConditions else buyConditions
if (!conditions.areMet(player.toDispatcher(), EmptyProvidedHolder)) {
// Only run not met effects if player can afford to buy
if (getBuyPrice(buyType)!!.canAfford(player, amount.toDouble())) {
conditions.areMetAndTrigger(
TriggerData(
player = player
).dispatch(player.toDispatcher()
))
}
return BuyStatus.MISSING_REQUIREMENTS
}
if (!getBuyPrice(buyType)!!.canAfford(player, amount.toDouble())) { if (!getBuyPrice(buyType)!!.canAfford(player, amount.toDouble())) {
return BuyStatus.CANNOT_AFFORD return BuyStatus.CANNOT_AFFORD
} }
@@ -339,6 +373,10 @@ class ShopItem(
} }
} }
if (!sellConditions.areMet(player.toDispatcher(), EmptyProvidedHolder)) {
return SellStatus.MISSING_REQUIREMENTS
}
return SellStatus.ALLOW return SellStatus.ALLOW
} }

View File

@@ -87,6 +87,10 @@ items:
require: "%player_rank% > 1" # (Optional) This expression must hold true to be allowed to sell this item. require: "%player_rank% > 1" # (Optional) This expression must hold true to be allowed to sell this item.
# The conditions that must be met in order to sell this item.
# Read here: https://plugins.auxilor.io/effects/configuring-a-condition
conditions: [ ]
sell-command: # (Optional) Enter commands that should be executed when a player sells items. You can use %player% and %amount% placeholders sell-command: # (Optional) Enter commands that should be executed when a player sells items. You can use %player% and %amount% placeholders
- "give %player% stone %amount%" - "give %player% stone %amount%"
sell-message: # (Optional) Enter messages to send to the player when the player sells this specific items. It will still send the 'sold-item/sold-multiple' from lang.yml sell-message: # (Optional) Enter messages to send to the player when the player sells this specific items. It will still send the 'sold-item/sold-multiple' from lang.yml
@@ -132,6 +136,10 @@ items:
type: crystals type: crystals
display: "&b%value% Crystals ❖" display: "&b%value% Crystals ❖"
# The conditions that must be met in order to buy this item.
# Read here: https://plugins.auxilor.io/effects/configuring-a-condition
conditions: [ ]
gui: gui:
display: display:
item: nether_star item: nether_star
@@ -150,8 +158,6 @@ items:
type: coins type: coins
display: "&a$%value%" display: "&a$%value%"
require: "%actions_allow_bosses_is_met%" # You can use actions to use conditions here! (https://plugins.auxilor.io/actions/placeholderapi)
max-at-once: 1 # If you want to skip the amount selection GUI, set max at once to 1 so clicking will instant-buy. max-at-once: 1 # If you want to skip the amount selection GUI, set max at once to 1 so clicking will instant-buy.
# Instead of having the item be buyable and sellable, you can have # Instead of having the item be buyable and sellable, you can have
@@ -162,6 +168,10 @@ items:
type: crystals type: crystals
display: "&b%value%❖" display: "&b%value%❖"
# The conditions that must be met in order to buy this item.
# Read here: https://plugins.auxilor.io/effects/configuring-a-condition
conditions: [ ]
gui: gui:
display: display:
lore: # You can inherit the item and just add your own extra lore to show additional information. lore: # You can inherit the item and just add your own extra lore to show additional information.