|
| 1 | +package com.y54895.matrixshop.core.economy |
| 2 | + |
| 3 | +import org.bukkit.configuration.ConfigurationSection |
| 4 | +import org.bukkit.entity.Player |
| 5 | +import taboolib.common.platform.function.warning |
| 6 | +import taboolib.module.kether.KetherShell |
| 7 | +import taboolib.module.kether.ScriptOptions |
| 8 | +import java.util.Locale |
| 9 | +import java.util.concurrent.TimeUnit |
| 10 | + |
| 11 | +data class ConditionalTaxRule( |
| 12 | + val id: String, |
| 13 | + val enabled: Boolean, |
| 14 | + val priority: Int, |
| 15 | + val mode: String?, |
| 16 | + val value: Double?, |
| 17 | + val condition: List<String> |
| 18 | +) |
| 19 | + |
| 20 | +data class ConditionalTaxConfig( |
| 21 | + val enabled: Boolean, |
| 22 | + val mode: String, |
| 23 | + val value: Double, |
| 24 | + val rules: List<ConditionalTaxRule> = emptyList() |
| 25 | +) |
| 26 | + |
| 27 | +data class ConditionalTaxResult( |
| 28 | + val amount: Double, |
| 29 | + val mode: String, |
| 30 | + val value: Double, |
| 31 | + val ruleId: String? |
| 32 | +) |
| 33 | + |
| 34 | +object ConditionalTaxEngine { |
| 35 | + |
| 36 | + fun parse( |
| 37 | + root: ConfigurationSection, |
| 38 | + path: String, |
| 39 | + defaultEnabled: Boolean, |
| 40 | + defaultMode: String, |
| 41 | + defaultValue: Double, |
| 42 | + legacyPercentPath: String? = null |
| 43 | + ): ConditionalTaxConfig { |
| 44 | + val section = root.getConfigurationSection(path) |
| 45 | + if (section == null) { |
| 46 | + if (legacyPercentPath != null && root.contains(legacyPercentPath)) { |
| 47 | + val value = root.getDouble(legacyPercentPath, defaultValue).coerceAtLeast(0.0) |
| 48 | + return ConditionalTaxConfig( |
| 49 | + enabled = value > 0.0, |
| 50 | + mode = "percent", |
| 51 | + value = value |
| 52 | + ) |
| 53 | + } |
| 54 | + return ConditionalTaxConfig( |
| 55 | + enabled = defaultEnabled, |
| 56 | + mode = defaultMode, |
| 57 | + value = defaultValue.coerceAtLeast(0.0) |
| 58 | + ) |
| 59 | + } |
| 60 | + val rulesSection = section.getConfigurationSection("Rules") |
| 61 | + val rules = rulesSection?.getKeys(false)?.map { id -> |
| 62 | + val ruleSection = rulesSection.getConfigurationSection(id)!! |
| 63 | + ConditionalTaxRule( |
| 64 | + id = id, |
| 65 | + enabled = ruleSection.getBoolean("Enabled", true), |
| 66 | + priority = ruleSection.getInt("Priority", 0), |
| 67 | + mode = ruleSection.getString("Mode") |
| 68 | + ?.trim() |
| 69 | + ?.takeIf(String::isNotBlank), |
| 70 | + value = ruleSection.get("Value")?.let { raw -> |
| 71 | + when (raw) { |
| 72 | + is Number -> raw.toDouble() |
| 73 | + is String -> raw.trim().toDoubleOrNull() |
| 74 | + else -> null |
| 75 | + } |
| 76 | + }?.coerceAtLeast(0.0), |
| 77 | + condition = readStringList(ruleSection, "Condition", "Conditions", "condition", "conditions") |
| 78 | + ) |
| 79 | + }.orEmpty() |
| 80 | + return ConditionalTaxConfig( |
| 81 | + enabled = section.getBoolean("Enabled", defaultEnabled), |
| 82 | + mode = section.getString("Mode", defaultMode).orEmpty().ifBlank { defaultMode }, |
| 83 | + value = section.getDouble("Value", defaultValue).coerceAtLeast(0.0), |
| 84 | + rules = rules |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | + fun compute( |
| 89 | + config: ConditionalTaxConfig, |
| 90 | + amount: Double, |
| 91 | + actor: Player?, |
| 92 | + moduleId: String, |
| 93 | + context: Map<String, Any?> = emptyMap() |
| 94 | + ): ConditionalTaxResult { |
| 95 | + val normalizedAmount = amount.coerceAtLeast(0.0) |
| 96 | + if (normalizedAmount <= 0.0 || !config.enabled) { |
| 97 | + return ConditionalTaxResult(0.0, config.mode, config.value, null) |
| 98 | + } |
| 99 | + val matchedRule = config.rules |
| 100 | + .sortedWith(compareByDescending<ConditionalTaxRule> { it.priority }.thenBy { it.id }) |
| 101 | + .firstOrNull { rule -> |
| 102 | + rule.enabled && matches(actor, moduleId, rule, context) |
| 103 | + } |
| 104 | + val mode = matchedRule?.mode ?: config.mode |
| 105 | + val value = matchedRule?.value ?: config.value |
| 106 | + val taxAmount = when (mode.lowercase(Locale.ROOT)) { |
| 107 | + "percent", "rate" -> normalizedAmount * value / 100.0 |
| 108 | + "fixed", "flat" -> value |
| 109 | + else -> value |
| 110 | + }.coerceAtLeast(0.0).coerceAtMost(normalizedAmount) |
| 111 | + return ConditionalTaxResult( |
| 112 | + amount = taxAmount, |
| 113 | + mode = mode, |
| 114 | + value = value, |
| 115 | + ruleId = matchedRule?.id |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + private fun matches( |
| 120 | + actor: Player?, |
| 121 | + moduleId: String, |
| 122 | + rule: ConditionalTaxRule, |
| 123 | + context: Map<String, Any?> |
| 124 | + ): Boolean { |
| 125 | + if (rule.condition.isEmpty()) { |
| 126 | + return true |
| 127 | + } |
| 128 | + if (actor == null) { |
| 129 | + return false |
| 130 | + } |
| 131 | + return runCatching { |
| 132 | + val builder = ScriptOptions.builder() |
| 133 | + .sender(actor) |
| 134 | + .set("player", actor) |
| 135 | + .set("actor", actor) |
| 136 | + .detailError(true) |
| 137 | + context.forEach { (key, value) -> |
| 138 | + if (value != null) { |
| 139 | + builder.set(key, value) |
| 140 | + } |
| 141 | + } |
| 142 | + val result = KetherShell.eval(rule.condition, builder.build()).get(3, TimeUnit.SECONDS) |
| 143 | + when (result) { |
| 144 | + is Boolean -> result |
| 145 | + is Number -> result.toInt() != 0 |
| 146 | + else -> result?.toString()?.equals("true", true) == true |
| 147 | + } |
| 148 | + }.getOrElse { |
| 149 | + warning("MatrixShop tax rule [$moduleId:${rule.id}] failed to execute: ${it.message ?: it.javaClass.simpleName}") |
| 150 | + false |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private fun readStringList(section: ConfigurationSection, vararg paths: String): List<String> { |
| 155 | + paths.forEach { path -> |
| 156 | + if (section.isList(path)) { |
| 157 | + return section.getStringList(path).mapNotNull { it?.trim()?.takeIf(String::isNotBlank) } |
| 158 | + } |
| 159 | + val single = section.getString(path)?.trim()?.takeIf(String::isNotBlank) |
| 160 | + if (single != null) { |
| 161 | + return listOf(single) |
| 162 | + } |
| 163 | + } |
| 164 | + return emptyList() |
| 165 | + } |
| 166 | +} |
0 commit comments