Skip to content

Commit

Permalink
Made hologram pointer smoother
Browse files Browse the repository at this point in the history
  • Loading branch information
Sytm committed Dec 2, 2023
1 parent 4130c7c commit 630447a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 41 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pointers/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -36,9 +35,11 @@ internal class HologramPointer(
private val activeHolograms: MutableMap<Trackable, Hologram> = 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?) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
}

0 comments on commit 630447a

Please sign in to comment.