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

io.github.parzivalExe.guiApi.components.ComponentManager.kt Maven / Gradle / Ivy

Go to download

With GuiAPI you can create Guis for your Bukkit/Spigot-Plugin in seconds while at the same time saving many lines of code

The newest version!
package io.github.parzivalExe.guiApi.components

import io.github.parzivalExe.guiApi.Gui
import org.bukkit.inventory.ItemStack
import kotlin.random.Random

object ComponentManager {

    private val components = arrayListOf()

    fun getNewComponentId(): Int {
        var id: Int
        synchronized(components) {
            do {
                id = Random.nextInt()
            } while (components.any { component -> component.id == id })
        }
        return id
    }

    fun initializeComponent(component: Component) {
        synchronized(components) {
            components.add(component)
        }
    }

    fun finalizeComponent(component: Component): Boolean {
        synchronized(components) {
            return@synchronized components.remove(component)
        }
        return false
    }

    fun isItemComponent(item: ItemStack, itemSlot: Int, gui: Gui): Boolean {
        return components.any { component -> component.getGuiItem() == item && gui.hasComponent(component)
                && gui.getPositionOfComponent(component) == itemSlot }
    }

    fun getComponentFromItem(item: ItemStack, itemSlot: Int, gui: Gui): Component? {
        return components.firstOrNull { component -> component.getGuiItem() == item && gui.hasComponent(component)
                && gui.getPositionOfComponent(component) == itemSlot }
    }

    fun getAllComponents(): ArrayList {
        return components
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy