
walkmc.hologram.AbstractTextLine.kt Maven / Gradle / Ivy
package walkmc.hologram
import net.minecraft.server.*
import org.bukkit.*
import org.bukkit.World
import org.bukkit.entity.*
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 a hologram text line.
*
* This works like a skeletal model for any hologram text line.
*/
abstract class AbstractTextLine(world: MinecraftWorld) : EntityHologram(world), TextLine {
constructor() : this(worlds[0].handler)
constructor(hologram: Hologram) : this(hologram.handler.world) {
this.hologram = hologram
}
/**
* Returns if the hologram of this text line is already initialized
*/
val isHologramInitialized: Boolean
get() = this::hologram.isInitialized
override val world: World
get() = world.world
override val location: Location
get() = Location(world.world, locX, locY, locZ)
override val holder: EntityHologram
get() = this
override val holderEntity: ArmorStand
get() = entityBukkit
override lateinit var hologram: Hologram
override var options: IndexList = IndexList()
override var height: Double = 0.275
override var allowRefresh: Boolean = false
override var refreshInterval: Int = 20
override fun addOption(option: String) {
changeDisplayIfEmpty(option)
super.addOption(option)
}
override fun addOptionFirst(option: String) {
changeDisplayIfEmpty(option)
super.addOptionFirst(option)
}
override fun removeOption(index: Int) {
super.removeOption(index)
disableDisplayIfEmpty()
}
override fun setOption(index: Int, option: String) {
changeDisplayIfEmpty(option)
super.setOption(index, option)
}
override fun display(location: Location) {
spawnInWorld(location, true)
}
override fun move(location: Location, broadcast: Boolean) {
setPosition(location)
if (broadcast)
broadcastTeleportPacket()
}
override fun onVisibilityChange(player: Player, visible: Boolean) {
if (!isValid)
return
val packet = if (visible) PacketPlayOutSpawnEntityLiving(this) else PacketPlayOutEntityDestroy(id)
player.sendPacket(packet)
}
override fun onInteract(player: Player, click: Click, distance: Double): Boolean {
return true
}
override fun canRefresh(): Boolean {
if (!allowRefresh || !isHologramInitialized || !isValid)
return false
if (!hasOptions || ticksLived % refreshInterval != 0)
return false
return true
}
override fun refresh() {
changeDisplay(options.toNextOrFirst())
}
override fun onTick() {
if (!canRefresh())
return
refresh()
}
override fun onEntityInteract(player: Player, click: Click, distance: Double): Boolean {
if (isHologramInitialized)
hologram.onInteract(player, click, this)
return onInteract(player, click, distance)
}
override fun saveEntityData(tag: CompoundTag) {
try {
tag["AllowRefresh"] = allowRefresh
tag["RefreshInterval"] = refreshInterval
tag["LineHeight"] = height
if (options.isNotEmpty())
tag["Lines"] = options.toTagList()
} catch (ex: Exception) {
delete()
}
}
override fun loadEntityData(tag: CompoundTag) {
try {
options.clear()
allowRefresh = tag.getBoolean("AllowRefresh")
refreshInterval = tag.getInt("RefreshInterval")
height = tag.getDouble("LineHeight")
tag.getListOrNull("Lines")
?.forEach { options.add(it.value) }
} catch (ex: Exception) {
delete()
}
}
fun changeDisplayIfEmpty(line: String) {
if (!hasOptions) {
customNameVisible = true
customName = line
}
}
fun disableDisplayIfEmpty() {
if (!hasOptions)
customNameVisible = false
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy