Skip to content

Commit

Permalink
Fixes for the hologram pointer, connected worlds and move to <enum>.e…
Browse files Browse the repository at this point in the history
…ntries
  • Loading branch information
Sytm committed Sep 29, 2023
1 parent a053632 commit 78961af
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 34 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
### Changed
- ProtocolLib is no longer required for hologram pointers

### Fixed
- Hologram pointers work across dimensions changes
- Hologram pointers not showing the distance to the target when in a connected world
- Connected worlds not working by default. To apply this fix to existing users:
- Either regenerate the config.yml
- Or update `pointers.connectedWorlds.world` from `world_the_nether` to `world_nether`

## 4.1.0

### Added
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.md5lukas.waypoints.pointers

import java.util.Collections
import net.kyori.adventure.text.format.NamedTextColor
import net.kyori.adventure.text.format.TextColor
import org.bukkit.Material
Expand Down Expand Up @@ -66,9 +65,6 @@ enum class BeaconColor(val material: Material, val textColor: TextColor?) {
val blockData by lazy { material.createBlockData() }

companion object {
private val entries: List<BeaconColor> =
Collections.unmodifiableList(BeaconColor.values().toList())

fun byMaterial(material: Material) = this.entries.firstOrNull { it.material === material }
fun byMaterial(material: Material) = entries.firstOrNull { it.material === material }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ internal class ManagedPlayer(
pointerManager.hooks
.loadEnabledPointers(player)
.thenAccept { enabled ->
PointerVariant.values()
PointerVariant.entries
.filter {
it.isEnabled(pointerManager.configuration) &&
enabled.getOrDefault(it.key, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface Trackable {
*
* If this value is null the hologram pointer will not be available.
*/
fun getHologramText(player: Player): Component? = null
fun getHologramText(player: Player, translatedTarget: Location): Component? = null

/**
* The item the hologram pointer should use.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal class HologramPointer(
override fun update(trackable: Trackable, translatedTarget: Location?) {
if (translatedTarget === null) return

val hologramText = trackable.getHologramText(player)
val hologramText = trackable.getHologramText(player, translatedTarget)

if (hologramText === null) return
val hologramTarget = translatedTarget.clone()
Expand Down Expand Up @@ -95,10 +95,19 @@ internal class HologramPointer(

fun update() {
if (::smoothingArmorStand.isInitialized) {
smoothingArmorStand.teleport(
location,
PlayerTeleportEvent.TeleportCause.PLUGIN,
TeleportFlag.EntityState.RETAIN_PASSENGERS)
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) }
}
textDisplay.text(text)
} else {
val world = player.world
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ class WaypointsScriptCommand(private val plugin: WaypointsPlugin) {
StringArgument("beacon-color")
.replaceSuggestions(
ArgumentSuggestions.strings(
BeaconColor.values().map { it.name }.toList()))) {
BeaconColor.entries.map { it.name }.toList()))) {
anyExecutor { sender, args ->
val player = args["player"] as Player
val target = args["target"] as Location
val beaconColorString = args["beacon-color"] as String?

val beaconColor =
BeaconColor.values().firstOrNull { it.name.equals(beaconColorString, true) }
BeaconColor.entries.firstOrNull { it.name.equals(beaconColorString, true) }
if (beaconColorString !== null && beaconColor === null) {
translations.COMMAND_SCRIPT_TEMPORARY_WAYPOINT_BEACON_COLOR_NOT_FOUND.send(
sender)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class BeaconConfigurationImpl : RepeatingPointerConfigurationImpl(), BeaconConfi
private class BeaconColorDefaults : TypeAdapter<Map<Type, BeaconColor>> {
override fun get(section: ConfigurationSection, path: String) =
mutableMapOf<Type, BeaconColor>().also { map ->
Type.values().forEach {
Type.entries.forEach {
map[it] = BeaconColor.valueOf(section.getStringNotNull("$path.${it.name.lowercase()}"))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CycleSortItem(wpGUI: WaypointsGUI, onCycle: suspend (OverviewSort) -> Unit

private companion object {
fun getOverviewSortCycleValues(wpGUI: WaypointsGUI) =
OverviewSort.values()
OverviewSort.entries
.map { current ->
val additionalLines =
wpGUI.translations.OVERVIEW_CYCLE_SORT_OPTIONS.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class SettingsPage(wpGUI: WaypointsGUI) :
)

val enabledPointers =
PointerVariant.values().mapNotNull {
PointerVariant.entries.mapNotNull {
if (it.isEnabled(wpGUI.plugin.waypointsConfig.pointers)) {
it
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Translations(tl: TranslationLoader) {
val TEXT_DISTANCE_OTHER_WORLD = Translation(tl, "text.distance.otherWorld")

val TEXT_BEACON_COLORS =
BeaconColor.values().map {
BeaconColor.entries.map {
it to
Translation(
tl, "text.beaconColors.${it.name.lowercase()}", miniMessage = tl.itemMiniMessage)
Expand Down Expand Up @@ -178,7 +178,7 @@ class Translations(tl: TranslationLoader) {
Translation(
tl, "inventory.overview.cycleSort.inactiveColor", miniMessage = tl.itemMiniMessage)
val OVERVIEW_CYCLE_SORT_OPTIONS =
OverviewSort.values().map { it to Translation(tl, "text.sortOptions.${it.name.lowercase()}") }
OverviewSort.entries.map { it to Translation(tl, "text.sortOptions.${it.name.lowercase()}") }
val OVERVIEW_SETTINGS = ItemTranslation(tl, "inventory.overview.settings")
val OVERVIEW_DESELECT = ItemTranslation(tl, "inventory.overview.deselect")
val OVERVIEW_DESELECT_SELECTED = InventoryTranslation(tl, "inventory.overview.deselect.selected")
Expand Down Expand Up @@ -223,14 +223,14 @@ class Translations(tl: TranslationLoader) {
val SETTINGS_POINTERS_NAMES: Map<PointerVariant, Translation> =
mutableMapOf<PointerVariant, Translation>().also { map ->
map.putAll(
PointerVariant.values().map {
PointerVariant.entries.map {
it to Translation(tl, "inventory.settings.pointers.${it.key}.name")
})
}
val SETTINGS_POINTERS_DESCRIPTIONS: Map<PointerVariant, InventoryTranslation> =
mutableMapOf<PointerVariant, InventoryTranslation>().also { map ->
map.putAll(
PointerVariant.values().map {
PointerVariant.entries.map {
it to InventoryTranslation(tl, "inventory.settings.pointers.${it.key}.description")
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ class PlayerTrackable(private val plugin: WaypointsPlugin, val player: Player) :
override val location: Location
get() = player.location

override fun getHologramText(player: Player) =
override fun getHologramText(player: Player, translatedTarget: Location) =
plugin.translations.POINTERS_HOLOGRAM_PLAYER_TRACKING.withReplacements(
"name" placeholder this.player.displayName(), *location.getResolvers(plugin, player))
"name" placeholder this.player.displayName(),
*location.getResolvers(plugin, player, translatedTarget))

override val hologramItem =
ItemStack(Material.PLAYER_HEAD).also { stack ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class TemporaryWaypointTrackable(
override val beaconColor: BeaconColor? = null,
) : StaticTrackable {

override fun getHologramText(player: Player) =
override fun getHologramText(player: Player, translatedTarget: Location) =
plugin.apiExtensions.run {
plugin.translations.POINTERS_HOLOGRAM_TEMPORARY.withReplacements(
*location.getResolvers(plugin, player))
*location.getResolvers(plugin, player, translatedTarget))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ class WaypointTrackable(private val plugin: WaypointsPlugin, val waypoint: Waypo
override val beaconColor: BeaconColor?
get() = waypoint.beaconColor?.let { BeaconColor.byMaterial(it) }

override fun getHologramText(player: Player) =
override fun getHologramText(player: Player, translatedTarget: Location) =
plugin.apiExtensions.run {
waypoint.getHologramTranslations().withReplacements(*waypoint.getResolvers(player))
waypoint
.getHologramTranslations()
.withReplacements(*waypoint.getResolvers(player, translatedTarget))
}

override val hologramItem =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import de.md5lukas.waypoints.gui.PlayerTrackingDisplayable
import de.md5lukas.waypoints.gui.SharedDisplayable
import de.md5lukas.waypoints.lang.InventoryTranslation
import net.kyori.adventure.text.Component
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
Expand Down Expand Up @@ -55,7 +56,7 @@ class APIExtensions(private val plugin: WaypointsPlugin) {
return stack
}

fun Waypoint.getResolvers(player: Player?) =
fun Waypoint.getResolvers(player: Player?, translatedTarget: Location = this.location) =
arrayOf(
"name" placeholder name,
"description" placeholder (description ?: ""),
Expand All @@ -69,8 +70,8 @@ class APIExtensions(private val plugin: WaypointsPlugin) {
"block_x" placeholder location.blockX,
"block_y" placeholder location.blockY,
"block_z" placeholder location.blockZ,
if (player !== null && player.world === location.world) {
"distance" placeholder player.location.distance(location)
if (player !== null && player.world === translatedTarget.world) {
"distance" placeholder player.location.distance(translatedTarget)
} else {
"distance" placeholderIgnoringArguments translations.TEXT_DISTANCE_OTHER_WORLD.text
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package de.md5lukas.waypoints.util

import de.md5lukas.commons.paper.placeholder
import de.md5lukas.commons.paper.placeholderIgnoringArguments
import de.md5lukas.waypoints.WaypointsPlugin
import kotlin.math.roundToLong
import org.bukkit.Location
import org.bukkit.entity.Player

fun Location.getResolvers(plugin: WaypointsPlugin, player: Player) =
fun Location.getResolvers(plugin: WaypointsPlugin, player: Player, translatedTarget: Location) =
arrayOf(
"world" placeholder plugin.worldTranslations.getWorldName(world!!),
"distance" placeholder player.location.distance(this).roundToLong(),
if (player.world === translatedTarget.world) {
"distance" placeholder player.location.distance(translatedTarget)
} else {
"distance" placeholderIgnoringArguments plugin.translations.TEXT_DISTANCE_OTHER_WORLD.text
},
"x" placeholder x,
"y" placeholder y,
"z" placeholder z,
Expand Down
2 changes: 1 addition & 1 deletion waypoints/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ pointers:

# Connected worlds in this list allow the translation of the coordinates 1:8, so you can for example navigate in the nether to a waypoint in the overworld
connectedWorlds:
world: world_the_nether
world: world_nether

actionBar:
enabled: false
Expand Down

0 comments on commit 78961af

Please sign in to comment.