
walkmc.hologram.AbstractHologram.kt Maven / Gradle / Ivy
package walkmc.hologram
import org.bukkit.*
import org.bukkit.entity.*
import org.bukkit.inventory.*
import walkmc.*
import walkmc.block.*
import walkmc.collections.*
import walkmc.extensions.*
import walkmc.hologram.api.*
import walkmc.hologram.entity.*
import walkmc.serializer.tag.impl.*
/**
* An abstract implementation of [Hologram].
*
* This works like a skeletal for any hologram.
*/
abstract class AbstractHologram(world: MinecraftWorld) : EntityHologram(world), Hologram {
constructor() : this(worlds[0].handler)
override val world: World
get() = world.world
override val location: Location
get() = Location(world.world, locX, locY, locZ)
override val handler: EntityHologram
get() = this
override val entity: ArmorStand
get() = super.getEntityBukkit()
override var lines: IndexList = IndexList()
override var visibility = HologramVisibility(this)
override var deleteOnDisable: Boolean = false
override var isDeleted = false
override fun display(location: Location) {
if (isSpawned) {
move(location)
return
}
var height = 0.0
for (line in lines) {
height += line.height
line.display(location.down(height))
}
spawnInWorld(world, location, true)
}
override fun delete() {
if (isDeleted)
return
dead = true
for (line in lines)
line.delete()
isDeleted = true
}
override fun move(location: Location, broadcast: Boolean) {
if (!isValid)
return
var height = 0.0
for (line in lines) {
height += line.height
line.move(location.down(height))
}
setPosition(location)
if (broadcast)
broadcastTeleportPacket()
}
override fun onInteract(player: Player, click: Click, line: Line) {
}
override fun createTextLine(text: String): TextLine {
val line = SimpleTextLine(this)
line.addOption(text)
return line
}
override fun createItemLine(item: ItemStack): ItemLine {
val line = SimpleItemLine(this)
line.addOption(item)
return line
}
override fun saveEntityData(tag: CompoundTag) {
if (lines.isNotEmpty())
tag["Lines"] = lines.map { it.uniqueId.toTag() }.toTagList()
}
override fun loadEntityDataLater(tag: CompoundTag) {
tag.getListOrNull("Lines")
?.forEach {
val entity = worldServer.getHologramLine(it.value)
if (entity != null) {
entity.hologram = this
addLine(entity)
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy