From 630447a2f05f684cbba15f2391762711e8be20af Mon Sep 17 00:00:00 2001 From: Lukas Planz Date: Sat, 2 Dec 2023 22:54:39 +0100 Subject: [PATCH] Made hologram pointer smoother --- CHANGELOG.md | 3 + pointers/build.gradle.kts | 2 +- .../waypoints/pointers/util/Spigot.kt | 15 ++++- .../pointers/variants/HologramPointer.kt | 57 +++++++------------ .../pointers/variants/PointerVariant.kt | 4 +- 5 files changed, 40 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d861b8..d892330 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## [Unreleased] +### Changed +- The movement of the hologram pointer has been smoothed, but are disabled on Minecraft versions < 1.20.2 + ## 4.3.2 ### Fixed diff --git a/pointers/build.gradle.kts b/pointers/build.gradle.kts index 4ebfbec..03a8495 100644 --- a/pointers/build.gradle.kts +++ b/pointers/build.gradle.kts @@ -12,7 +12,7 @@ plugins { repositories {} dependencies { - api("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT") + api(libs.paper) api(libs.stdlib) implementation(libs.schedulers) diff --git a/pointers/src/main/kotlin/de/md5lukas/waypoints/pointers/util/Spigot.kt b/pointers/src/main/kotlin/de/md5lukas/waypoints/pointers/util/Spigot.kt index dfd5074..095d22c 100644 --- a/pointers/src/main/kotlin/de/md5lukas/waypoints/pointers/util/Spigot.kt +++ b/pointers/src/main/kotlin/de/md5lukas/waypoints/pointers/util/Spigot.kt @@ -1,5 +1,6 @@ package de.md5lukas.waypoints.pointers.util +import org.bukkit.Bukkit import org.bukkit.Location import org.bukkit.block.Block import org.bukkit.entity.Player @@ -12,7 +13,7 @@ internal operator fun Vector.div(d: Int) = this.z / d, ) -operator fun Vector.div(d: Double) = +internal operator fun Vector.div(d: Double) = Vector( this.x / d, this.y / d, @@ -32,3 +33,15 @@ internal val Location.highestBlock: Block internal fun Player.sendActualBlock(location: Location) = this.sendBlockChange(location, location.block.blockData) + +private lateinit var parsedVersion: IntArray + +internal fun minecraftVersionAtLeast(minor: Int, patch: Int = 0): Boolean { + if (!::parsedVersion.isInitialized) { + parsedVersion = IntArray(3) + Bukkit.getMinecraftVersion().split('.').forEachIndexed { index, s -> + parsedVersion[index] = s.toInt() + } + } + return parsedVersion[1] >= minor && parsedVersion[2] >= patch +} diff --git a/pointers/src/main/kotlin/de/md5lukas/waypoints/pointers/variants/HologramPointer.kt b/pointers/src/main/kotlin/de/md5lukas/waypoints/pointers/variants/HologramPointer.kt index dbb3761..d1e89cc 100644 --- a/pointers/src/main/kotlin/de/md5lukas/waypoints/pointers/variants/HologramPointer.kt +++ b/pointers/src/main/kotlin/de/md5lukas/waypoints/pointers/variants/HologramPointer.kt @@ -5,14 +5,13 @@ import de.md5lukas.waypoints.pointers.Pointer import de.md5lukas.waypoints.pointers.PointerManager import de.md5lukas.waypoints.pointers.Trackable import de.md5lukas.waypoints.pointers.util.minus -import io.papermc.paper.entity.TeleportFlag import java.util.concurrent.ConcurrentHashMap import net.kyori.adventure.text.Component import org.bukkit.Location import org.bukkit.entity.* import org.bukkit.entity.ItemDisplay.* -import org.bukkit.event.player.PlayerTeleportEvent import org.bukkit.util.Transformation +import org.bukkit.util.Vector import org.joml.AxisAngle4f import org.joml.Vector3f @@ -36,9 +35,11 @@ internal class HologramPointer( private val activeHolograms: MutableMap = ConcurrentHashMap() private lateinit var playerEyes: Location + private lateinit var velocityOffset: Vector override fun preUpdates() { playerEyes = player.eyeLocation.clone() + velocityOffset = player.velocity.setY(0).multiply(interval) } override fun update(trackable: Trackable, translatedTarget: Location?) { @@ -62,7 +63,10 @@ internal class HologramPointer( val direction = (tVec - pVec).normalize() - pVec.add(direction.multiply(config.distanceFromPlayer)).toLocation(playerEyes.world!!) + pVec + .add(direction.multiply(config.distanceFromPlayer)) + .add(velocityOffset) + .toLocation(playerEyes.world!!) } if (trackable in activeHolograms) { @@ -93,61 +97,42 @@ internal class HologramPointer( var text: Component ) { - private lateinit var smoothingArmorStand: ArmorStand private lateinit var textDisplay: TextDisplay private var itemDisplay: ItemDisplay? = null fun update() { - if (::smoothingArmorStand.isInitialized) { - if (player.world == smoothingArmorStand.world) { - smoothingArmorStand.teleport( - location, - PlayerTeleportEvent.TeleportCause.PLUGIN, - TeleportFlag.EntityState.RETAIN_PASSENGERS) - } else { - // Cannot teleport entities with passengers, so eject, teleport separately, and remount - // them - smoothingArmorStand.eject() - arrayOf(smoothingArmorStand, textDisplay, itemDisplay).forEach { it?.teleport(location) } - smoothingArmorStand.addPassenger(textDisplay) - itemDisplay?.let { smoothingArmorStand.addPassenger(it) } - } + if (::textDisplay.isInitialized) { + textDisplay.teleportAsync(location) + itemDisplay?.teleportAsync(location) textDisplay.text(text) } else { val world = player.world - smoothingArmorStand = - world.spawn(location, ArmorStand::class.java) { - it.isPersistent = false - it.isVisibleByDefault = false - - it.setGravity(false) - it.isVisible = false - it.isMarker = true - } - player.showEntity(pointerManager.plugin, smoothingArmorStand) textDisplay = world.spawn(location, TextDisplay::class.java) { it.isPersistent = false it.isVisibleByDefault = false + it.teleportDuration = interval + it.billboard = Display.Billboard.CENTER it.text(text) it.isDefaultBackground = true it.isSeeThrough = true } - smoothingArmorStand.addPassenger(textDisplay) player.showEntity(pointerManager.plugin, textDisplay) if (config.iconEnabled) { itemDisplay = - trackable.hologramItem - ?.let { itemStack -> - world.spawn(location, ItemDisplay::class.java) { + trackable.hologramItem?.let { itemStack -> + world + .spawn(location, ItemDisplay::class.java) { it.isPersistent = false it.isVisibleByDefault = false + it.teleportDuration = interval + it.itemStack = itemStack val isBlock = itemStack.type.isBlock @@ -166,17 +151,13 @@ internal class HologramPointer( ) it.itemDisplayTransform = ItemDisplayTransform.FIXED } - } - ?.also { - smoothingArmorStand.addPassenger(it) - player.showEntity(pointerManager.plugin, it) - } + .also { player.showEntity(pointerManager.plugin, it) } + } } } } fun remove() { - smoothingArmorStand.remove() textDisplay.remove() itemDisplay?.remove() } diff --git a/pointers/src/main/kotlin/de/md5lukas/waypoints/pointers/variants/PointerVariant.kt b/pointers/src/main/kotlin/de/md5lukas/waypoints/pointers/variants/PointerVariant.kt index 4007127..e4265ad 100644 --- a/pointers/src/main/kotlin/de/md5lukas/waypoints/pointers/variants/PointerVariant.kt +++ b/pointers/src/main/kotlin/de/md5lukas/waypoints/pointers/variants/PointerVariant.kt @@ -4,6 +4,7 @@ import de.md5lukas.schedulers.AbstractScheduler import de.md5lukas.waypoints.pointers.Pointer import de.md5lukas.waypoints.pointers.PointerManager import de.md5lukas.waypoints.pointers.config.PointerConfiguration +import de.md5lukas.waypoints.pointers.util.minecraftVersionAtLeast import org.bukkit.entity.Player enum class PointerVariant( @@ -16,7 +17,8 @@ enum class PointerVariant( BLINKING_BLOCK("blinkingBlock", { it.blinkingBlock.enabled }, ::BlinkingBlockPointer), BOSS_BAR("bossBar", { it.bossBar.enabled }, ::BossBarPointer), COMPASS("compass", { it.compass.enabled }, ::CompassPointer), - HOLOGRAM("hologram", { it.hologram.enabled }, ::HologramPointer), + HOLOGRAM( + "hologram", { it.hologram.enabled && minecraftVersionAtLeast(20, 2) }, ::HologramPointer), PARTICLE("particle", { it.particle.enabled }, ::ParticlePointer), TRAIL("trail", { it.trail.enabled }, ::TrailPointer) }