Skip to content

Commit

Permalink
feat: allow non-player entities to transition between sections
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Oct 26, 2024
1 parent 65d05d4 commit 97522b2
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ object MovementListener : Listener {
fun VehicleMoveEvent.move() {
val players = vehicle.getPassengersRecursive().filterIsInstance<Player>()

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,15 +22,15 @@ object MovementHandler {
private val sectionCheckers = listOf(ConfigSectionChecker)

val teleportCooldown = mutableSetOf<UUID>()
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.
Expand All @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down
Original file line number Diff line number Diff line change
@@ -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?
Expand Down

0 comments on commit 97522b2

Please sign in to comment.