All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.skillw.pouvoir.util.CalculationUtil.kt Maven / Gradle / Ivy

There is a newer version: 1.6.7-beta-6
Show newest version
package com.skillw.pouvoir.util

import com.skillw.pouvoir.Pouvoir
import com.skillw.pouvoir.util.calculate.calculateInfix
import org.bukkit.entity.LivingEntity

/**
 * 计算工具类
 *
 * @constructor Create empty Calculation utils
 */


fun String.calculate(entity: LivingEntity? = null): Double {
    return calculate(Pouvoir.placeholderManager.replace(entity, this))
}


fun String.calculateDouble(entity: LivingEntity? = null): Double {
    return calculate(entity)
}


fun String.calculateInline(entity: LivingEntity? = null): String {
    var previousChar = 'a'
    val replacement = HashMap()
    var start = -1
    forEachIndexed { index, char ->
        if (previousChar == '{' && char == '{') {
            start = index - 1
        }
        if (previousChar == '}' && char == '}') {
            if (start == -1) return@forEachIndexed
            replacement += start..index to this.substring(start + 2, index - 1).calculate(entity).toString()
            start = -1
        }
        previousChar = char
    }
    return replacementIntRange(replacement)
}


fun calculate(input: String): Double {
    return input.calculateInfix()
}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy