Skip to content

Commit

Permalink
fix: allow items to load chunk before spawning, refactor: keep loaded…
Browse files Browse the repository at this point in the history
… chunk by item/entity loaded for 10 seconds
  • Loading branch information
Boy0000 committed Oct 26, 2024
1 parent 97522b2 commit b82d014
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ object MovementListener : Listener {
@EventHandler
fun EntityMoveEvent.entityMove() {
if (!hasExplicitlyChangedPosition()) return
if (entity.getPassengersRecursive().isEmpty()) return
entity.getPassengersRecursive().filterIsInstance<Player>()
.filter { rider -> rider.hasPermission(Permissions.ADMIN_PERMISSION) && rider.canMoveSections }
.forEach { MovementHandler.handleMovement(it, from, to) }
MovementHandler.handleMovement(entity, from, to)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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) ->
Expand All @@ -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)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Item> {
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<Item> {
itemStack = item.itemStack
thrower = item.thrower
owner = item.owner
velocity = item.velocity
}
if (addedTicket) {
delay(10.seconds)
chunk.removePluginChunkTicket(deeperWorld.plugin)
}
}
}
}

0 comments on commit b82d014

Please sign in to comment.