From b82d0148f93236bc7181651473c1c6161f65de36 Mon Sep 17 00:00:00 2001 From: Boy Date: Sat, 26 Oct 2024 16:44:52 +0200 Subject: [PATCH] fix: allow items to load chunk before spawning, refactor: keep loaded chunk by item/entity loaded for 10 seconds --- .../deeperworld/listeners/MovementListener.kt | 5 +---- .../movement/TransitionTeleportHandler.kt | 11 ++++++---- .../synchronization/SectionSyncListener.kt | 20 ++++++++++++++----- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/com/mineinabyss/deeperworld/listeners/MovementListener.kt b/src/main/kotlin/com/mineinabyss/deeperworld/listeners/MovementListener.kt index 5d0683b..9ff3183 100644 --- a/src/main/kotlin/com/mineinabyss/deeperworld/listeners/MovementListener.kt +++ b/src/main/kotlin/com/mineinabyss/deeperworld/listeners/MovementListener.kt @@ -33,9 +33,6 @@ object MovementListener : Listener { @EventHandler fun EntityMoveEvent.entityMove() { if (!hasExplicitlyChangedPosition()) return - if (entity.getPassengersRecursive().isEmpty()) return - entity.getPassengersRecursive().filterIsInstance() - .filter { rider -> rider.hasPermission(Permissions.ADMIN_PERMISSION) && rider.canMoveSections } - .forEach { MovementHandler.handleMovement(it, from, to) } + MovementHandler.handleMovement(entity, from, to) } } diff --git a/src/main/kotlin/com/mineinabyss/deeperworld/movement/TransitionTeleportHandler.kt b/src/main/kotlin/com/mineinabyss/deeperworld/movement/TransitionTeleportHandler.kt index c24b18f..7163d09 100644 --- a/src/main/kotlin/com/mineinabyss/deeperworld/movement/TransitionTeleportHandler.kt +++ b/src/main/kotlin/com/mineinabyss/deeperworld/movement/TransitionTeleportHandler.kt @@ -6,13 +6,13 @@ import com.mineinabyss.deeperworld.movement.transition.SectionTransition import com.mineinabyss.idofront.time.ticks import io.papermc.paper.entity.TeleportFlag import kotlinx.coroutines.delay -import kotlinx.coroutines.future.asDeferred import kotlinx.coroutines.future.await import org.bukkit.Location import org.bukkit.entity.Entity import org.bukkit.entity.LivingEntity import org.bukkit.entity.Player import org.bukkit.event.player.PlayerTeleportEvent +import kotlin.time.Duration.Companion.seconds class TransitionTeleportHandler(val teleportEntity: Entity, val from: Location, val to: Location) : TeleportHandler { @@ -43,7 +43,8 @@ class TransitionTeleportHandler(val teleportEntity: Entity, val from: Location, spectators.values.flatten().forEach { it.spectatorTarget = null } deeperWorld.plugin.launch { - to.world.getChunkAtAsync(to).await().addPluginChunkTicket(deeperWorld.plugin) + val chunk = to.world.getChunkAtAsync(to).await() + val addedTicket = chunk.addPluginChunkTicket(deeperWorld.plugin) teleportEntity.teleportAsync(to, PlayerTeleportEvent.TeleportCause.PLUGIN, *teleportFlags).await() teleportEntity.velocity = oldVelocity oldLeashedEntities.forEach { (leashHolder, leashEntities) -> @@ -59,9 +60,11 @@ class TransitionTeleportHandler(val teleportEntity: Entity, val from: Location, } } - delay(10.ticks) - to.chunk.removePluginChunkTicket(deeperWorld.plugin) MovementHandler.teleportCooldown -= teleportEntity.uniqueId + if (addedTicket) { + delay(10.seconds) + to.chunk.removePluginChunkTicket(deeperWorld.plugin) + } } } diff --git a/src/main/kotlin/com/mineinabyss/deeperworld/synchronization/SectionSyncListener.kt b/src/main/kotlin/com/mineinabyss/deeperworld/synchronization/SectionSyncListener.kt index e2b8b0f..b6f3050 100644 --- a/src/main/kotlin/com/mineinabyss/deeperworld/synchronization/SectionSyncListener.kt +++ b/src/main/kotlin/com/mineinabyss/deeperworld/synchronization/SectionSyncListener.kt @@ -12,6 +12,7 @@ import com.mineinabyss.idofront.plugin.Plugins import com.mineinabyss.idofront.spawning.spawn import com.mineinabyss.idofront.time.ticks import kotlinx.coroutines.delay +import kotlinx.coroutines.future.await import net.kyori.adventure.text.Component import nl.rutgerkok.blocklocker.SearchMode import org.bukkit.Material @@ -39,6 +40,7 @@ import org.bukkit.event.player.PlayerBucketFillEvent import org.bukkit.event.player.PlayerInteractEvent import org.bukkit.event.world.StructureGrowEvent import org.bukkit.inventory.EquipmentSlot +import kotlin.time.Duration.Companion.seconds private fun syncBlockLocker(corr: Block) { blockLocker?.protectionFinder?.findProtection(corr, SearchMode.ALL)?.ifPresent { @@ -253,11 +255,19 @@ object SectionSyncListener : Listener { fun EntityRemoveFromWorldEvent.onVoidRemoval() { val item = (entity as? Item)?.takeIf { it.y < it.world.minHeight } ?: return val corrLoc = item.location.apply { y = -240.0 }.correspondingLocation ?: return - corrLoc.spawn { - itemStack = item.itemStack - thrower = item.thrower - owner = item.owner - velocity = velocity + deeperWorld.plugin.launch { + val chunk = corrLoc.world.getChunkAtAsync(corrLoc).await() + val addedTicket = chunk.addPluginChunkTicket(deeperWorld.plugin) + corrLoc.spawn { + itemStack = item.itemStack + thrower = item.thrower + owner = item.owner + velocity = item.velocity + } + if (addedTicket) { + delay(10.seconds) + chunk.removePluginChunkTicket(deeperWorld.plugin) + } } } }