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

com.skillw.asahi.internal.namespacing.prefix.lang.util.Util.kt Maven / Gradle / Ivy

There is a newer version: 1.6.7-beta-6
Show newest version
package com.skillw.asahi.internal.namespacing.prefix.lang.util

import com.skillw.asahi.api.annotation.AsahiPrefix
import com.skillw.asahi.api.prefixParser
import com.skillw.asahi.api.quest
import com.skillw.asahi.api.quester
import com.skillw.asahi.internal.util.Clock
import com.skillw.asahi.internal.util.MapTemplate
import taboolib.common5.Coerce
import java.awt.Color
import java.text.SimpleDateFormat
import java.util.*

/**
 * @className Util
 *
 * @author Glom
 * @date 2023/1/14 0:45 Copyright 2024 Glom.
 */
@AsahiPrefix(["arrayOf"], "lang")
private fun arrayOf() = prefixParser {
    val array = quest>()
    result { array.get() }
}

@AsahiPrefix(["listOf"], "lang")
private fun listOf() = prefixParser {
    val list = quest>()
    result { list.get() }
}

@AsahiPrefix(["setOf"], "lang")
private fun setOf() = prefixParser {
    val set = quest>()
    result { set.get() }
}

@AsahiPrefix(["mapOf"], "lang")
private fun mapOf() = prefixParser {
    if (expect("with")) {
        val template = quest()
        val list = quest>()
        result { template.get().build(list.get()) }
    } else {
        val map = quest>()
        result { map.get() }
    }
}

@AsahiPrefix(["mapListOf"], "lang")
private fun mapListOf() = prefixParser>> {
    expect("with")
    val templateGetter = quest()
    val list = quest>()
    result {
        val template = templateGetter.get()
        list.get().mapNotNull { template.build(it as? List? ?: return@mapNotNull null) }
            .toMutableList()
    }
}

@AsahiPrefix(["pair"], "lang")
private fun pair() = prefixParser> {
    val pair = quest>()
    result { pair.get() }
}

@AsahiPrefix(["mapTemplate"], "lang")
private fun mapTemplate() = prefixParser {
    val list = quest>()
    result { MapTemplate(list.get().map { it.toString() }) }
}

/**
 * @className Operator
 *
 * @author Glom
 * @date 2023/1/14 0:42 Copyright 2024 Glom.
 */
@AsahiPrefix(["join"], "lang")
private fun join() = prefixParser {
    val list = quest>()
    val by = if (expect("by")) quest() else quester { "" }
    result {
        list.get().joinToString(by.get().replace("\\n", "\n"))
    }
}

@AsahiPrefix(["replace"], "lang")
private fun replace() = prefixParser {
    val str = quest()
    expect("with")
    val replacement = quest>()
    result {
        var cache = str.get()
        replacement.get().mapValues { it.value.toString() }.forEach { (origin, new) ->
            cache = cache.replace(origin, new)
        }
        cache
    }
}

@AsahiPrefix(["type"], "lang")
fun type() = prefixParser {
    val type = quest()
    val getter = quest()
    result {
        val obj = getter.get()
        when (type.get()) {
            "double" -> Coerce.toDouble(obj)
            "int" -> Coerce.toInteger(obj)
            "long" -> Coerce.toLong(obj)
            "float" -> Coerce.toFloat(obj)
            "short" -> Coerce.toShort(obj)
            "byte" -> Coerce.toByte(obj)
            "bool" -> Coerce.toBoolean(obj)
            "char" -> Coerce.toChar(obj)
            "string" -> Coerce.toString(obj)
            else -> obj
        }
    }
}

@AsahiPrefix(["select", "with"], "lang")
private fun with() = prefixParser {
    val any = quest()
    val exec = parseScript()
    result {
        context().clone().run {
            when (val obj = any.get()) {
                is Collection<*> -> {
                    obj.filterNotNull().forEach {
                        select(it)
                        exec.run()
                    }
                }

                else -> {
                    select(obj)
                    exec.run()
                }
            }
            [email protected](this)
        }
    }
}

@AsahiPrefix(["date"], "lang")
private fun date() = prefixParser {
    val typeGetter = if (expect("in")) questString() else quester { null }
    result {
        val date = Date()
        SimpleDateFormat(
            when (typeGetter.get()?.lowercase()) {
                "year" -> "yyyy"
                "month" -> "MM"
                "day" -> "dd"
                "time" -> "HH:mm:ss"
                "timeDetail" -> "HH:mm:ss.SSS"
                else -> "yyyy年MM月dd日 HH:mm:ss"
            }
        ).format(date)
    }
}


@AsahiPrefix(["time"], "lang")
private fun time() = prefixParser {
    val formatGetter = if (expect("as")) questString() else quester { "yyyy-MM-dd HH:mm:ss" }
    result {
        SimpleDateFormat(formatGetter.get()).format(Date())
    }
}

@AsahiPrefix(["color"])
private fun color() = prefixParser {
    expect("[")
    val r = quest()
    val g = quest()
    val b = quest()
    expect("]")
    result { Color(r.get(), g.get(), b.get()) }
}


@AsahiPrefix(["currentTick"], "lang")
private fun currentTick() = prefixParser {
    result {
        Clock.currentTick
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy