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

com.skillw.attsystem.api.attribute.Attribute.kt Maven / Gradle / Ivy

The newest version!
package com.skillw.attsystem.api.attribute

import com.skillw.attsystem.AttributeSystem
import com.skillw.attsystem.api.read.ReadPattern
import com.skillw.pouvoir.api.plugin.map.component.Registrable

/**
 * Attribute
 *
 * @constructor Create Attribute
 * @property key 键
 * @property names 名称
 * @property readPattern 读取格式
 * @property priority 优先级
 */
class Attribute private constructor(
    override val key: String,
    val display: String,
    val names: Collection,
    val readPattern: ReadPattern<*>,
    val priority: Int = 0,
) : Registrable, Comparable {
    override fun compareTo(other: Attribute): Int = if (this.priority == other.priority) 0
    else if (this.priority > other.priority) 1
    else -1

    /** Entity */
    var entity = true

    /** Release */
    var config = false

    override fun register() {
        AttributeSystem.attributeManager.register(this)
    }

    var mapping: Mapping? = null
        set(value) {
            value?.attribute = this
            field = value
        }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as Attribute

        return key == other.key
    }

    override fun hashCode(): Int {
        return key.hashCode()
    }

    override fun toString(): String {
        return "Attribute(key='$key')"
    }

    /**
     * Builder
     *
     * @param receiver
     * @constructor
     * @property key 键
     * @property readPattern 读取格式
     */
    class Builder(val key: String, private val readPattern: ReadPattern<*>, receiver: Builder.() -> Unit) {
        var display: String? = null

        /** Entity */
        var entity = true

        /** Release */
        var release = false

        /** Priority */
        var priority: Int = 0

        /** Names */
        val names = ArrayList()

        var mapping: Mapping? = null


        init {
            receiver.invoke(this)
        }

        /**
         * Build
         *
         * @return
         */
        fun build(): Attribute {
            val att = Attribute(key, display ?: names.first(), names, readPattern, priority)
            att.config = release
            att.entity = entity
            att.mapping = mapping
            return att
        }

    }

    companion object {
        @JvmStatic
        fun createAttribute(
            key: String,
            readPattern: ReadPattern<*>,
            init: Builder.() -> Unit,
        ): Attribute {
            return Builder(key, readPattern, init).build()
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy