Skip to content

Commit

Permalink
Added option to specify value only for counters
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Aug 28, 2023
1 parent 1a38103 commit 5d32c3b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class Counter internal constructor(
val filters: FilterList,
override val config: Config,
val arguments: EffectArgumentList,
val multiplierExpression: String
val multiplierExpression: String?,
val valueExpression: String?
) : ConfigurableElement, PotentiallyTriggerable, Cloneable {
override val uuid: UUID = UUID.randomUUID()

Expand Down Expand Up @@ -45,7 +46,8 @@ class Counter internal constructor(
filters,
config.clone(),
arguments,
multiplierExpression
multiplierExpression,
valueExpression
)
}

Expand Down
20 changes: 16 additions & 4 deletions core/src/main/kotlin/com/willfp/libreforge/counters/Counters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,29 @@ object Counters {
context.with("args")
)

val multiplierExpression = if (config.has("multiplier")) {
config.getString("multiplier")
} else "1" // lmao
val multiplierExpression = config.getStringOrNull("multiplier")

val valueExpression = config.getStringOrNull("value")

if (multiplierExpression != null && valueExpression != null) {
context.log(
ConfigViolation(
"value",
"Cannot specify both multiplier and value!"
)
)

return null
}

return Counter(
trigger,
conditions,
filters,
args,
arguments,
multiplierExpression
multiplierExpression,
valueExpression
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,30 @@ internal data class BoundCounter(
return
}

val multiplier = evaluateExpression(
counter.multiplierExpression,
placeholderContext(
player = player,
injectable = config
met.forEach { it.ifMet(counter, trigger) }

if (counter.valueExpression != null) {
val valueFromExpr = evaluateExpression(
counter.valueExpression,
placeholderContext(
player = player,
injectable = config
)
)
)

met.forEach { it.ifMet(counter, trigger) }
accumulator.accept(player, valueFromExpr)
}

accumulator.accept(player, value * multiplier)
if (counter.multiplierExpression != null) {
val multiplier = evaluateExpression(
counter.multiplierExpression,
placeholderContext(
player = player,
injectable = config
)
)

accumulator.accept(player, value * multiplier)
}
}
}

0 comments on commit 5d32c3b

Please sign in to comment.