Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "com.github.shynixn"
version = "9.29.2"
version = "9.29.3"

repositories {
mavenLocal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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.
Expand All @@ -49,7 +52,7 @@ class MathComponent(
fun tickMinecraft() {
val sourceLocation = position.toLocation()

if (!sourceLocation.chunk.isLoaded) {
if (!isChunkLoadedAtLocation(sourceLocation)) {
return
}

Expand All @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class PetEntityFactoryImpl (
visualizePath,
rideUpdateMs
)
mathPhysicComponent.pet = pet

entities[armorStandEntityId] = petEntity
return petEntity
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin-folia.yml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin-legacy.yml
Original file line number Diff line number Diff line change
@@ -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]
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down