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

com.skillw.pouvoir.api.feature.selector.SelectResult.kt Maven / Gradle / Ivy

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

import com.skillw.pouvoir.api.feature.selector.target.EntityTarget
import com.skillw.pouvoir.api.feature.selector.target.LocTarget
import org.bukkit.Location
import org.bukkit.entity.Entity
import org.bukkit.entity.LivingEntity

/**
 * @className SelectResult
 *
 * 选取结果
 *
 * @author Glom
 * @date 2023/1/9 7:20 Copyright 2024 Glom.
 */
class SelectResult(targets: Collection = LinkedHashSet()) : LinkedHashSet(targets) {
    /**
     * 遍历
     *
     * @param consumer
     * @param T
     * @receiver
     */
    inline fun  forEach(consumer: T.(Int) -> Unit) =
        filterIsInstance().forEachIndexed { index, entity -> entity.run { consumer(index) } }

    /**
     * 遍历 entity
     *
     * @param consumer
     * @receiver
     */
    fun forEachEntity(consumer: Entity.(Int) -> Unit) =
        forEach { index -> if (isPresent) consumer(entity, index) }

    /**
     * 遍历 living entity
     *
     * @param consumer
     * @receiver
     */
    fun forEachLivingEntity(consumer: LivingEntity.(Int) -> Unit) =
        forEach { index -> if (isPresent) (entity as? LivingEntity?)?.also { consumer(it, index) } }

    /**
     * 遍历 location
     *
     * @param consumer
     * @receiver
     */
    fun forEachLocation(consumer: Location.(Int) -> Unit) =
        forEach { index -> if (isPresent) consumer(location, index) }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy