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

com.skillw.attsystem.internal.manager.CompiledAttrDataManagerImpl.kt Maven / Gradle / Ivy

The newest version!
package com.skillw.attsystem.internal.manager

import com.skillw.attsystem.AttributeSystem
import com.skillw.attsystem.AttributeSystem.readManager
import com.skillw.attsystem.api.compiled.CompiledAttrDataCompound
import com.skillw.attsystem.api.compiled.CompiledData
import com.skillw.attsystem.api.manager.CompiledAttrDataManager
import com.skillw.attsystem.util.Utils.validEntity
import org.bukkit.entity.LivingEntity
import java.util.*

object CompiledAttrDataManagerImpl : CompiledAttrDataManager() {
    override val key = "CompiledAttrDataManager"
    override val priority: Int = 2
    override val subPouvoir = AttributeSystem
    override fun hasCompiledData(entity: LivingEntity, source: String): Boolean {
        return hasCompiledData(entity.uniqueId, source)
    }

    override fun hasCompiledData(uuid: UUID, source: String): Boolean {
        return get(uuid)?.containsKey(source) == true
    }

    override fun addCompiledData(
        entity: LivingEntity,
        source: String,
        attributes: Collection,
        slot: String?,
    ): CompiledData? {
        return this.addCompiledData(entity.uniqueId, source, attributes, slot)
    }

    override fun addCompiledData(
        entity: LivingEntity,
        source: String,
        compiledData: CompiledData,
    ): CompiledData {
        val uuid = entity.uniqueId
        this.computeIfAbsent(uuid) { CompiledAttrDataCompound(entity) }.register(source, compiledData)
        return compiledData
    }

    override fun addCompiledData(
        uuid: UUID,
        source: String,
        attributes: Collection,
        slot: String?,
    ): CompiledData? {
        return readManager.read(attributes, uuid.validEntity(), slot)?.let {
            this.addCompiledData(
                uuid,
                source,
                it
            )
        }
    }

    override fun addCompiledData(uuid: UUID, source: String, compiledData: CompiledData): CompiledData? {
        return uuid.validEntity()?.let { addCompiledData(it, source, compiledData) }
    }

    override fun removeCompiledData(entity: LivingEntity, source: String): CompiledData? {
        return removeCompiledData(entity.uniqueId, source)
    }

    override fun removeCompiledData(uuid: UUID, source: String): CompiledData? {
        return this[uuid]?.run {
            remove(source)
        }
    }

    override fun removeIfStartWith(entity: LivingEntity, prefix: String) {
        return removeIfStartWith(entity.uniqueId, prefix)
    }

    override fun get(key: UUID): CompiledAttrDataCompound? {
        val entity = key.validEntity() ?: return null
        return computeIfAbsent(key) { CompiledAttrDataCompound(entity) }
    }

    override fun removeIfStartWith(uuid: UUID, prefix: String) {
        val lower = prefix.lowercase()
        this[uuid]?.run {
            filterKeys { it.startsWith(lower) }.map { it.key }.forEach(this::remove)
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy