diff --git a/build.gradle.kts b/build.gradle.kts index 78d537230..b8cbb0522 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,7 +8,7 @@ plugins { } group = "com.github.shynixn" -version = "9.29.2" +version = "9.29.3" repositories { mavenLocal() diff --git a/src/main/java/com/github/shynixn/petblocks/impl/PetEntityImpl.kt b/src/main/java/com/github/shynixn/petblocks/impl/PetEntityImpl.kt index 20f429990..1b25976ac 100644 --- a/src/main/java/com/github/shynixn/petblocks/impl/PetEntityImpl.kt +++ b/src/main/java/com/github/shynixn/petblocks/impl/PetEntityImpl.kt @@ -429,7 +429,13 @@ class PetEntityImpl( // Required so the position of the player stays in sync while packet riding. // Has to be on the main thread. withContext(plugin.entityDispatcher(player)) { - packetService.setServerPlayerPosition(player, physicsComponent.position.toLocation()) + val physicLocation = physicsComponent.position.toLocation() + val playerLocation = player.location + if (playerLocation.world == physicLocation.world) { + packetService.setServerPlayerPosition(player, physicLocation) + } else { + pet.unmount() + } } for (visiblePlayers in playerComponent.visiblePlayers) { packetService.sendPacketOutEntityMount(visiblePlayers, PacketOutEntityMount().also { diff --git a/src/main/java/com/github/shynixn/petblocks/impl/physic/MathComponent.kt b/src/main/java/com/github/shynixn/petblocks/impl/physic/MathComponent.kt index d92b4cd4b..6e85e3203 100644 --- a/src/main/java/com/github/shynixn/petblocks/impl/physic/MathComponent.kt +++ b/src/main/java/com/github/shynixn/petblocks/impl/physic/MathComponent.kt @@ -6,13 +6,15 @@ import com.github.shynixn.mcutils.common.toVector import com.github.shynixn.mcutils.packet.api.RayTraceResult import com.github.shynixn.mcutils.packet.api.RayTracingService import com.github.shynixn.mcutils.packet.api.meta.enumeration.BlockDirection +import com.github.shynixn.petblocks.contract.Pet import com.github.shynixn.petblocks.entity.PhysicSettings +import org.bukkit.Location import kotlin.math.abs class MathComponent( var position: Vector3d, private val settings: PhysicSettings, private val rayTracingService: RayTracingService -) { +) { /** * Function being called when the position and motion are about to change. */ @@ -27,6 +29,7 @@ class MathComponent( private var cachedTeleportTarget: Vector3d? = null private var movementRayTraceResult: RayTraceResult? = null private var gravityRayTraceResult: RayTraceResult? = null + var pet: Pet? = null /** * Sets the velocity which is applied per tick to the object. @@ -49,7 +52,7 @@ class MathComponent( fun tickMinecraft() { val sourceLocation = position.toLocation() - if (!sourceLocation.chunk.isLoaded) { + if (!isChunkLoadedAtLocation(sourceLocation)) { return } @@ -59,12 +62,17 @@ class MathComponent( // Target location of the object. val targetLocation = position.toLocation().add(motion.toVector()) - if (!targetLocation.chunk.isLoaded) { + if (!isChunkLoadedAtLocation(targetLocation)) { return } // Check gravity. - gravityRayTraceResult = rayTracingService.rayTraceMotion(position, Vector3d(0.0, -1.0, 0.0), settings.collideWithWater, settings.collideWithPassableBlocks) + gravityRayTraceResult = rayTracingService.rayTraceMotion( + position, + Vector3d(0.0, -1.0, 0.0), + settings.collideWithWater, + settings.collideWithPassableBlocks + ) if (gravityRayTraceResult!!.hitBlock && motion.y < 0.0) { // Set gravity to zero and correct y axe. @@ -73,13 +81,35 @@ class MathComponent( } if (motion.x != 0.0 || motion.z != 0.0) { - movementRayTraceResult = rayTracingService.rayTraceMotion(position, motion, settings.collideWithWater, settings.collideWithPassableBlocks) + movementRayTraceResult = rayTracingService.rayTraceMotion( + position, + motion, + settings.collideWithWater, + settings.collideWithPassableBlocks + ) + } else { + // Check if stuck + movementRayTraceResult = rayTracingService.rayTraceMotion( + position, + Vector3d(0.00, 0.1, 0.0), + settings.collideWithWater, + settings.collideWithPassableBlocks + ) + if (movementRayTraceResult!!.hitBlock && pet != null && !pet!!.isRiding()) { + pet?.call() + return + } } if (movementRayTraceResult != null) { if (movementRayTraceResult!!.hitBlock && movementRayTraceResult!!.blockDirection != BlockDirection.UP) { val stuckBackMotion = - rayTracingService.rayTraceMotion(position, Vector3d(motion.x * -1, 0.0, motion.z * -1),settings.collideWithWater, settings.collideWithPassableBlocks) + rayTracingService.rayTraceMotion( + position, + Vector3d(motion.x * -1, 0.0, motion.z * -1), + settings.collideWithWater, + settings.collideWithPassableBlocks + ) if (stuckBackMotion.hitBlock) { // If hit block while moving back. Move the entity up. position.add(motion.x * -1, 0.1, motion.z * -1) @@ -172,6 +202,17 @@ class MathComponent( } } + private fun isChunkLoadedAtLocation(location: Location): Boolean { + val chunkX = location.blockX shr 4 + val chunkZ = location.blockZ shr 4 + try { + return location.world!!.isChunkLoaded(chunkX, chunkZ) + } catch (e: Exception) { + // Folia Thread access. + return false + } + } + /** * If the values get too small, set them to zero. */ diff --git a/src/main/java/com/github/shynixn/petblocks/impl/service/PetEntityFactoryImpl.kt b/src/main/java/com/github/shynixn/petblocks/impl/service/PetEntityFactoryImpl.kt index f3ad653c6..67cbce81a 100644 --- a/src/main/java/com/github/shynixn/petblocks/impl/service/PetEntityFactoryImpl.kt +++ b/src/main/java/com/github/shynixn/petblocks/impl/service/PetEntityFactoryImpl.kt @@ -98,6 +98,7 @@ class PetEntityFactoryImpl ( visualizePath, rideUpdateMs ) + mathPhysicComponent.pet = pet entities[armorStandEntityId] = petEntity return petEntity diff --git a/src/main/resources/plugin-folia.yml b/src/main/resources/plugin-folia.yml index b9459b1d0..58eecb794 100644 --- a/src/main/resources/plugin-folia.yml +++ b/src/main/resources/plugin-folia.yml @@ -1,5 +1,5 @@ name: PetBlocks -version: 9.29.2 +version: 9.29.3 author: Shynixn main: com.github.shynixn.petblocks.PetBlocksPlugin softdepend: [ PlaceholderAPI, HeadDatabase] diff --git a/src/main/resources/plugin-legacy.yml b/src/main/resources/plugin-legacy.yml index 567f7b2d8..fe285f089 100644 --- a/src/main/resources/plugin-legacy.yml +++ b/src/main/resources/plugin-legacy.yml @@ -1,5 +1,5 @@ name: PetBlocks -version: 9.29.2 +version: 9.29.3 author: Shynixn main: com.github.shynixn.petblocks.PetBlocksPlugin softdepend: [ PlaceholderAPI, HeadDatabase] diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 8229c0005..8d31cecf6 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: PetBlocks -version: 9.29.2 +version: 9.29.3 author: Shynixn main: com.github.shynixn.petblocks.PetBlocksPlugin softdepend: [ PlaceholderAPI, HeadDatabase]