walkmc.block.ContainerBlock.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of resources-addon Show documentation
Show all versions of resources-addon Show documentation
A resources API/Addon with compatibility with others plugins
The newest version!
package walkmc.block
import org.bukkit.entity.*
import org.bukkit.inventory.*
import walkmc.extensions.*
import walkmc.serializer.tag.impl.*
/**
* A implementation of [IBlock] that can have a inventory.
*/
open class ContainerBlock : AbstractBlock {
lateinit var inventory: Inventory
var dropInventory = true
val hasInventory get() = this::inventory.isInitialized
constructor()
constructor(inv: Inventory) {
inventory = inv
}
override fun onInteract(player: Player, click: Click) {
if (click.isLeft || !hasInventory)
return
player.openInventory(inventory)
}
override fun onBreak(player: Player) {
if (dropInventory) {
val loc = location.up()
for (item in inventory) world.dropItemNaturally(loc, item)
}
}
override fun load(tag: CompoundTag) {
if ("Container" in tag)
inventory = tag.getInventory("Container")
}
override fun save(tag: CompoundTag) {
if (hasInventory)
tag["Container"] = inventory
}
}