Skip to content

Commit

Permalink
Added level numerals and increasing prices
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Jul 5, 2024
1 parent 9749f0a commit 5297952
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.willfp.eco.core.gui.slot.MaskItems
import com.willfp.eco.core.gui.slot.Slot
import com.willfp.eco.core.items.Items
import com.willfp.eco.core.items.builder.modify
import com.willfp.eco.core.recipe.parts.EmptyTestableItem
import com.willfp.eco.util.formatEco
import com.willfp.ecomponent.CaptiveItem
import com.willfp.ecomponent.addComponent
Expand Down Expand Up @@ -261,12 +262,12 @@ private abstract class MenuSlot(
}

private fun String.injectPlaceholders(player: Player, menu: Menu): String {
val price = menu.scroll[player]?.getOrNull()?.inscriptionPrice?.getDisplay(player)
val scroll = menu.scroll[player]?.getOrNull()?.name
val scroll = menu.scroll[player]?.getOrNull()
val price = scroll?.getInscriptionPriceDisplay(player, capturedItem[player])

return this
.replaceNullable("%price%", price)
.replaceNullable("%scroll%", scroll)
.replaceNullable("%scroll%", scroll?.name)
.formatEco(player = player, formatPlaceholders = true)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import com.willfp.eco.core.placeholder.templates.DynamicInjectablePlaceholder
import com.willfp.eco.core.price.ConfiguredPrice
import com.willfp.eco.core.recipe.Recipes
import com.willfp.eco.core.registry.KRegistrable
import com.willfp.eco.util.evaluateExpression
import com.willfp.eco.util.evaluateExpressionOrNull
import com.willfp.eco.util.formatEco
import com.willfp.eco.util.toNumeral
import com.willfp.ecoscrolls.EcoScrollsPlugin
import com.willfp.ecoscrolls.plugin
import com.willfp.ecoscrolls.target.Targets
Expand Down Expand Up @@ -88,22 +91,24 @@ class Scroll(
)
}

val removeRequirements = config.getBool("remove-requirements")
private val removeRequirements = config.getBool("remove-requirements")

val inscriptionConditions = Conditions.compile(
private val inscriptionConditions = Conditions.compile(
config.getSubsections("inscription.conditions"),
context.with("inscription conditions")
)

val inscriptionEffects = Effects.compile(
private val inscriptionEffects = Effects.compile(
config.getSubsections("inscription.effects"),
context.with("inscription effects")
)

val inscriptionPrice = ConfiguredPrice.createOrFree(
private val inscriptionPrice = ConfiguredPrice.createOrFree(
config.getSubsection("inscription.price"),
)

private val priceLevelMultiplier = config.getStringOrNull("inscription.price-level-multiplier")

val isDragAndDropEnabled = config.getBool("inscription.drag-and-drop")

val isInscriptionTableEnabled = config.getBool("inscription.inscription-table")
Expand All @@ -117,6 +122,12 @@ class Scroll(
}
}

private val levelNumeralPlaceholder = object : DynamicInjectablePlaceholder(Pattern.compile("level_numeral")) {
override fun getValue(p0: String, p1: PlaceholderContext): String? {
return p1.itemStack?.getScrollLevel(this@Scroll)?.level?.toNumeral()
}
}

private val usesLeftPlaceholder = object : DynamicInjectablePlaceholder(Pattern.compile("uses_left")) {
override fun getValue(p0: String, p1: PlaceholderContext): String? {
val item = p1.itemStack ?: return null
Expand All @@ -143,6 +154,7 @@ class Scroll(
override fun getPlaceholderInjections(): List<InjectablePlaceholder> {
return listOf(
levelPlaceholder,
levelNumeralPlaceholder,
usesLeftPlaceholder,
usesPlaceholder,
maxUsesPlaceholder
Expand Down Expand Up @@ -211,11 +223,11 @@ class Scroll(
return false
}

if (!inscriptionPrice.canAfford(player)) {
if (!inscriptionPrice.canAfford(player, getPriceMultiplier(itemStack))) {
return false
}

inscriptionPrice.pay(player)
inscriptionPrice.pay(player, getPriceMultiplier(itemStack))

inscribe(itemStack)

Expand Down Expand Up @@ -280,6 +292,27 @@ class Scroll(
other.conflicts.contains(this.id)
}

fun getPriceMultiplier(itemStack: ItemStack?): Double {
if (itemStack == null || priceLevelMultiplier == null) {
return 1.0
}

val level = itemStack.getScrollLevel(this)?.level ?: 0

return evaluateExpressionOrNull(
// Less elegant than actually using placeholders, but it avoids a bunch
// of extra code.
priceLevelMultiplier.replace("%level%", level.toString()),
placeholderContext(
item = itemStack
)
) ?: 1.0
}

fun getInscriptionPriceDisplay(player: Player, itemStack: ItemStack?): String {
return inscriptionPrice.getDisplay(player, getPriceMultiplier(itemStack))
}

override fun equals(other: Any?): Boolean {
if (other !is Scroll) {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.bukkit.event.inventory.InventoryClickEvent

class DragAndDropListener(private val plugin: EcoScrollsPlugin) : Listener {
@EventHandler
fun onDrag(event: InventoryClickEvent) {
fun handle(event: InventoryClickEvent) {
val player = event.whoClicked as? Player ?: return

if (player.gameMode == GameMode.CREATIVE) {
Expand Down
4 changes: 4 additions & 0 deletions eco-core/core-plugin/src/main/resources/scrolls/_example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ inscription:
type: coins
display: "&e%value% coins"

# The formula to multiply the price depending on the level.
# The %level% placeholder is the *current* level of the scroll
price-level-multiplier: "1 + %level% * 0.5"

# If the scroll can be applied to items via drag-and-drop
drag-and-drop: true

Expand Down

0 comments on commit 5297952

Please sign in to comment.