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

walkmc.block.ContainerBlock.kt Maven / Gradle / Ivy

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
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy