From 97522b21532fa1a4bce60655f2ae8b571be41869 Mon Sep 17 00:00:00 2001 From: Boy Date: Sat, 26 Oct 2024 16:15:26 +0200 Subject: [PATCH] feat: allow non-player entities to transition between sections --- .../deeperworld/listeners/MovementListener.kt | 5 ++-- .../BedrockBlockingInvalidTeleportHandler.kt | 12 ++++----- .../movement/InvalidTeleportHandler.kt | 5 ++-- .../deeperworld/movement/MovementHandler.kt | 25 ++++++++++--------- .../UndoMovementInvalidTeleportHandler.kt | 5 ++-- .../transition/ConfigSectionChecker.kt | 7 +++--- .../movement/transition/SectionChecker.kt | 5 ++-- 7 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/main/kotlin/com/mineinabyss/deeperworld/listeners/MovementListener.kt b/src/main/kotlin/com/mineinabyss/deeperworld/listeners/MovementListener.kt index c61f492..5d0683b 100644 --- a/src/main/kotlin/com/mineinabyss/deeperworld/listeners/MovementListener.kt +++ b/src/main/kotlin/com/mineinabyss/deeperworld/listeners/MovementListener.kt @@ -26,9 +26,8 @@ object MovementListener : Listener { fun VehicleMoveEvent.move() { val players = vehicle.getPassengersRecursive().filterIsInstance() - players.firstOrNull { it.hasPermission(Permissions.ADMIN_PERMISSION) && it.canMoveSections }?.let { - MovementHandler.handleMovement(it, from, to) - } + val teleportEntity = players.firstOrNull { it.hasPermission(Permissions.ADMIN_PERMISSION) && it.canMoveSections } ?: vehicle + MovementHandler.handleMovement(teleportEntity, from, to) } @EventHandler diff --git a/src/main/kotlin/com/mineinabyss/deeperworld/movement/BedrockBlockingInvalidTeleportHandler.kt b/src/main/kotlin/com/mineinabyss/deeperworld/movement/BedrockBlockingInvalidTeleportHandler.kt index 7aea398..69780a0 100644 --- a/src/main/kotlin/com/mineinabyss/deeperworld/movement/BedrockBlockingInvalidTeleportHandler.kt +++ b/src/main/kotlin/com/mineinabyss/deeperworld/movement/BedrockBlockingInvalidTeleportHandler.kt @@ -12,7 +12,7 @@ import org.bukkit.Material import org.bukkit.entity.Entity import org.bukkit.entity.Player -class BedrockBlockingInvalidTeleportHandler(player: Player, from: Location, to: Location) : InvalidTeleportHandler(player, from, to) { +class BedrockBlockingInvalidTeleportHandler(entity: Entity, from: Location, to: Location) : InvalidTeleportHandler(entity, from, to) { constructor(player: Player, transition: SectionTransition) : this(player, transition.from, transition.to) override fun handleInvalidTeleport() { @@ -32,12 +32,12 @@ class BedrockBlockingInvalidTeleportHandler(player: Player, from: Location, to: MovementListener.temporaryBedrock.remove(spawnedBedrock) } - val oldFallDistance = player.fallDistance - val oldVelocity = player.velocity + val oldFallDistance = entity.fallDistance + val oldVelocity = entity.velocity - player.teleport(from.up(1)) + entity.teleport(from.up(1)) - player.fallDistance = oldFallDistance - player.velocity = oldVelocity + entity.fallDistance = oldFallDistance + entity.velocity = oldVelocity } } diff --git a/src/main/kotlin/com/mineinabyss/deeperworld/movement/InvalidTeleportHandler.kt b/src/main/kotlin/com/mineinabyss/deeperworld/movement/InvalidTeleportHandler.kt index 7700640..a1f2c04 100644 --- a/src/main/kotlin/com/mineinabyss/deeperworld/movement/InvalidTeleportHandler.kt +++ b/src/main/kotlin/com/mineinabyss/deeperworld/movement/InvalidTeleportHandler.kt @@ -3,13 +3,14 @@ package com.mineinabyss.deeperworld.movement import com.mineinabyss.deeperworld.movement.transition.SectionTransition import com.mineinabyss.idofront.messaging.error import org.bukkit.Location +import org.bukkit.entity.Entity import org.bukkit.entity.Player -abstract class InvalidTeleportHandler(val player: Player, val from: Location, val to: Location) : TeleportHandler { +abstract class InvalidTeleportHandler(val entity: Entity, val from: Location, val to: Location) : TeleportHandler { constructor(player: Player, transition: SectionTransition) : this(player, transition.from, transition.to) final override fun handleTeleport() { handleInvalidTeleport() - player.error("There is no where for you to teleport") + entity.error("There is no where for you to teleport") } override fun isValidTeleport(): Boolean { diff --git a/src/main/kotlin/com/mineinabyss/deeperworld/movement/MovementHandler.kt b/src/main/kotlin/com/mineinabyss/deeperworld/movement/MovementHandler.kt index 8395179..194fc48 100644 --- a/src/main/kotlin/com/mineinabyss/deeperworld/movement/MovementHandler.kt +++ b/src/main/kotlin/com/mineinabyss/deeperworld/movement/MovementHandler.kt @@ -12,6 +12,7 @@ import net.kyori.adventure.title.Title import org.bukkit.GameMode import org.bukkit.Location import org.bukkit.attribute.Attribute +import org.bukkit.entity.Entity import org.bukkit.entity.Player import java.util.* import kotlin.time.Duration.Companion.seconds @@ -21,15 +22,15 @@ object MovementHandler { private val sectionCheckers = listOf(ConfigSectionChecker) val teleportCooldown = mutableSetOf() - fun handleMovement(player: Player, from: Location, to: Location) { - if (sectionCheckers.any { it.inSection(player) }) { - sectionCheckers.firstNotNullOfOrNull { it.checkForTransition(player, from, to) }?.let { - with(getTeleportHandler(player, it)) { - if (this.isValidTeleport()) it.toEvent(player).call { this@with.handleTeleport() } + fun handleMovement(entity: Entity, from: Location, to: Location) { + if (sectionCheckers.any { it.inSection(entity) }) { + sectionCheckers.firstNotNullOfOrNull { it.checkForTransition(entity, from, to) }?.let { + with(getTeleportHandler(entity, it)) { + if (this.isValidTeleport() && entity is Player) it.toEvent(entity).call { this@with.handleTeleport() } else this.handleTeleport() } } - } else player.applyOutOfBoundsDamage() + } else (entity as? Player)?.applyOutOfBoundsDamage() } //TODO abstract this away. Should instead do out of bounds action if out of bounds. @@ -56,14 +57,14 @@ object MovementHandler { } } - private fun getTeleportHandler(player: Player, sectionTransition: SectionTransition): TeleportHandler { + private fun getTeleportHandler(entity: Entity, sectionTransition: SectionTransition): TeleportHandler { return when { - sectionTransition.teleportUnnecessary || player.uniqueId in teleportCooldown -> EmptyTeleportHandler - player.gameMode != GameMode.SPECTATOR && sectionTransition.to.block.isSolid -> when (sectionTransition.kind) { - TransitionKind.ASCEND -> UndoMovementInvalidTeleportHandler(player, sectionTransition) - else -> BedrockBlockingInvalidTeleportHandler(player, sectionTransition) + sectionTransition.teleportUnnecessary || entity.uniqueId in teleportCooldown -> EmptyTeleportHandler + entity is Player && entity.gameMode != GameMode.SPECTATOR && sectionTransition.to.block.isSolid -> when (sectionTransition.kind) { + TransitionKind.ASCEND -> UndoMovementInvalidTeleportHandler(entity, sectionTransition) + else -> BedrockBlockingInvalidTeleportHandler(entity, sectionTransition) } - else -> TransitionTeleportHandler(player.vehicle ?: player, sectionTransition) + else -> TransitionTeleportHandler(entity.vehicle ?: entity, sectionTransition) } } } diff --git a/src/main/kotlin/com/mineinabyss/deeperworld/movement/UndoMovementInvalidTeleportHandler.kt b/src/main/kotlin/com/mineinabyss/deeperworld/movement/UndoMovementInvalidTeleportHandler.kt index 8902389..db36765 100644 --- a/src/main/kotlin/com/mineinabyss/deeperworld/movement/UndoMovementInvalidTeleportHandler.kt +++ b/src/main/kotlin/com/mineinabyss/deeperworld/movement/UndoMovementInvalidTeleportHandler.kt @@ -2,11 +2,12 @@ package com.mineinabyss.deeperworld.movement import com.mineinabyss.deeperworld.movement.transition.SectionTransition import org.bukkit.Location +import org.bukkit.entity.Entity import org.bukkit.entity.Player -class UndoMovementInvalidTeleportHandler(player: Player, from: Location, to: Location) : InvalidTeleportHandler(player, from, to) { +class UndoMovementInvalidTeleportHandler(entity: Entity, from: Location, to: Location) : InvalidTeleportHandler(entity, from, to) { constructor(player: Player, transition: SectionTransition) : this(player, transition.from, transition.to) override fun handleInvalidTeleport() { - player.teleport(from) + entity.teleport(from) } } diff --git a/src/main/kotlin/com/mineinabyss/deeperworld/movement/transition/ConfigSectionChecker.kt b/src/main/kotlin/com/mineinabyss/deeperworld/movement/transition/ConfigSectionChecker.kt index d311497..495a2ca 100644 --- a/src/main/kotlin/com/mineinabyss/deeperworld/movement/transition/ConfigSectionChecker.kt +++ b/src/main/kotlin/com/mineinabyss/deeperworld/movement/transition/ConfigSectionChecker.kt @@ -4,15 +4,16 @@ import com.mineinabyss.deeperworld.world.section.correspondingLocation import com.mineinabyss.deeperworld.world.section.inSectionTransition import com.mineinabyss.deeperworld.world.section.section import org.bukkit.Location +import org.bukkit.entity.Entity import org.bukkit.entity.Player object ConfigSectionChecker : SectionChecker { - override fun inSection(player: Player): Boolean { - return player.location.section != null + override fun inSection(entity: Entity): Boolean { + return entity.location.section != null } override fun checkForTransition( - player: Player, + entity: Entity, from: Location, to: Location ): SectionTransition? { diff --git a/src/main/kotlin/com/mineinabyss/deeperworld/movement/transition/SectionChecker.kt b/src/main/kotlin/com/mineinabyss/deeperworld/movement/transition/SectionChecker.kt index f02f113..455059a 100644 --- a/src/main/kotlin/com/mineinabyss/deeperworld/movement/transition/SectionChecker.kt +++ b/src/main/kotlin/com/mineinabyss/deeperworld/movement/transition/SectionChecker.kt @@ -1,14 +1,15 @@ package com.mineinabyss.deeperworld.movement.transition import org.bukkit.Location +import org.bukkit.entity.Entity import org.bukkit.entity.Player interface SectionChecker { - fun inSection(player: Player) : Boolean + fun inSection(entity: Entity) : Boolean fun checkForTransition( - player: Player, + entity: Entity, from: Location, to: Location ): SectionTransition?