Skip to content

Commit

Permalink
Fixed riding state and offset for multiple players. --release
Browse files Browse the repository at this point in the history
  • Loading branch information
Shynixn committed Nov 10, 2023
1 parent 0dce5ea commit 2e77723
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: /home/runner/work/PetBlocks/PetBlocks/build/libs/petblocks-root-${{ env.RELEASE_VERSION }}-premium.jar
asset_name: BlockBall-Premium.jar
asset_name: PetBlocks-Premium.jar
asset_content_type: application/jar

Documentation:
Expand Down
84 changes: 17 additions & 67 deletions src/main/java/com/github/shynixn/petblocks/impl/PetEntityImpl.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
package com.github.shynixn.petblocks.impl

import com.github.shynixn.mccoroutine.bukkit.*
import com.github.shynixn.mccoroutine.bukkit.CoroutineTimings
import com.github.shynixn.mccoroutine.bukkit.launch
import com.github.shynixn.mccoroutine.bukkit.minecraftDispatcher
import com.github.shynixn.mccoroutine.bukkit.ticks
import com.github.shynixn.mcutils.common.Vector3d
import com.github.shynixn.mcutils.common.physic.PhysicObject
import com.github.shynixn.mcutils.common.physic.PhysicObjectDispatcher
import com.github.shynixn.mcutils.common.toLocation
import com.github.shynixn.mcutils.common.toVector3d
import com.github.shynixn.mcutils.packet.api.*
import com.github.shynixn.mcutils.packet.api.packet.PacketOutEntityEquipment
import com.github.shynixn.mcutils.packet.api.packet.PacketOutEntityMetadata
import com.github.shynixn.mcutils.packet.api.packet.PacketOutEntityMount
import com.github.shynixn.mcutils.packet.api.PacketService
import com.github.shynixn.mcutils.pathfinder.api.PathfinderResult
import com.github.shynixn.mcutils.pathfinder.api.PathfinderResultType
import com.github.shynixn.mcutils.pathfinder.api.PathfinderService
import com.github.shynixn.mcutils.pathfinder.api.WorldSnapshot
import com.github.shynixn.petblocks.contract.Pet
import com.github.shynixn.petblocks.contract.PetActionExecutionService
import com.github.shynixn.petblocks.contract.PlaceHolderService
import com.github.shynixn.petblocks.entity.PetMeta
import com.github.shynixn.petblocks.enumeration.PetRidingState
import com.github.shynixn.petblocks.enumeration.PetVisibility
import com.github.shynixn.petblocks.impl.physic.ArmorstandEntityComponent
import com.github.shynixn.petblocks.impl.physic.MathComponent
Expand All @@ -30,11 +28,9 @@ import org.bukkit.FluidCollisionMode
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import org.bukkit.plugin.Plugin
import org.bukkit.util.Vector
import java.util.Date
import java.util.Random
import java.util.*
import java.util.logging.Level

class PetEntityImpl(
Expand All @@ -45,7 +41,6 @@ class PetEntityImpl(
private val plugin: Plugin,
private val pet: Pet,
private val petMeta: PetMeta,
private val placeHolderService: PlaceHolderService,
private val packetService: PacketService,
private val physicObjectDispatcher: PhysicObjectDispatcher,
private val pathfinderService: PathfinderService,
Expand All @@ -57,13 +52,14 @@ class PetEntityImpl(
private var positionUpdateCounter = 0
private var velocity = Vector3d(0.0, 0.0, 0.0)
private var lastClickTimeStamp = 0L

// Mover
private var lastRandomTimeStamp = 0L
private var randomMoveOne = 0
private var randomMoveTwo = 0
private var randomMoveThree = 0

companion object{
companion object {
var random = Random()
}

Expand Down Expand Up @@ -130,7 +126,7 @@ class PetEntityImpl(
/**
* RightClicks the pet.
*/
fun rightClick(player : Player) {
fun rightClick(player: Player) {
val currentDateTime = Date().time

if (currentDateTime - lastClickTimeStamp < clickCoolDownMs) {
Expand All @@ -150,7 +146,7 @@ class PetEntityImpl(
/**
* LeftClicks the pet.
*/
fun leftClick(player : Player) {
fun leftClick(player: Player) {
val currentDateTime = Date().time

if (currentDateTime - lastClickTimeStamp < clickCoolDownMs) {
Expand Down Expand Up @@ -180,7 +176,7 @@ class PetEntityImpl(

val dateTime = Date().time

if(dateTime - lastRandomTimeStamp > 3000){
if (dateTime - lastRandomTimeStamp > 3000) {
// For multiple pets.
lastRandomTimeStamp = dateTime
randomMoveOne = random.nextInt(3)
Expand Down Expand Up @@ -309,73 +305,27 @@ class PetEntityImpl(
/**
* Updates the displayName in the world.
*/
fun updateDisplayName(name: String) {
fun updateDisplayName() {
for (player in playerComponent.visiblePlayers) {
packetService.sendPacketOutEntityMetadata(player, PacketOutEntityMetadata().also {
it.entityId = entityComponent.entityId
it.customNameVisible = true
it.customname = placeHolderService.replacePlaceHolders(player, name, pet)
})
entityComponent.updateMetaData(player)
}
}

/**
* Updates the head Itemstack.
*/
fun updateHeadItemStack(itemStack: ItemStack) {
fun updateHeadItemStack() {
for (player in playerComponent.visiblePlayers) {
packetService.sendPacketOutEntityEquipment(player, PacketOutEntityEquipment().also {
it.entityId = entityComponent.entityId
it.items = listOf(Pair(ArmorSlotType.HELMET, itemStack))
})
entityComponent.updateEquipment(player)
}
}

/**
* Updates the riding state of the player.
*/
fun updateRidingState(owner: Player) {
fun updateRidingState() {
for (player in playerComponent.visiblePlayers) {
if (petMeta.visibility == PetVisibility.OWNER && player != owner) {
continue
}

val ridingState = petMeta.ridingState

if (ridingState == PetRidingState.NO) {
// Remove ground and fly
packetService.sendPacketOutEntityMount(player, PacketOutEntityMount().also {
it.entityId = entityComponent.entityId
})
// Remove hat
packetService.sendPacketOutEntityMount(player, PacketOutEntityMount().also {
it.entityId = owner.entityId
})
}

if (ridingState == PetRidingState.HAT) {
// Remove ground and fly
packetService.sendPacketOutEntityMount(player, PacketOutEntityMount().also {
it.entityId = entityComponent.entityId
})
// Set pet as passenger of player
packetService.sendPacketOutEntityMount(player, PacketOutEntityMount().also {
it.entityId = owner.entityId
it.passengers = listOf(entityComponent.entityId)
})
}

if (ridingState == PetRidingState.GROUND) {
// Remove hat
packetService.sendPacketOutEntityMount(player, PacketOutEntityMount().also {
it.entityId = owner.entityId
})
// Set pet as passenger of player
packetService.sendPacketOutEntityMount(player, PacketOutEntityMount().also {
it.entityId = entityComponent.entityId
it.passengers = listOf(owner.entityId)
})
}
entityComponent.updateRidingState(player)
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/github/shynixn/petblocks/impl/PetImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class PetImpl(
}

petMeta.displayName = value.translateChatColors()
petEntity?.updateDisplayName(petMeta.displayName)
petEntity?.updateDisplayName()
}

/**
Expand Down Expand Up @@ -188,7 +188,7 @@ class PetImpl(
}
set(value) {
petMeta.headItem = value.toItem()
petEntity?.updateHeadItemStack(petMeta.headItem.toItemStack())
petEntity?.updateHeadItemStack()
}

/**
Expand All @@ -200,7 +200,7 @@ class PetImpl(
}
set(value) {
petMeta.headItem = value
petEntity?.updateHeadItemStack(petMeta.headItem.toItemStack())
petEntity?.updateHeadItemStack()
}

/**
Expand Down Expand Up @@ -341,7 +341,7 @@ class PetImpl(

call()
petMeta.ridingState = PetRidingState.GROUND
petEntity?.updateRidingState(player)
petEntity?.updateRidingState()
}

/**
Expand All @@ -360,7 +360,7 @@ class PetImpl(
currentLocation.pitch = 0.0F
location = currentLocation
petMeta.ridingState = PetRidingState.HAT
petEntity?.updateRidingState(player)
petEntity?.updateRidingState()
}

/**
Expand All @@ -378,7 +378,7 @@ class PetImpl(
petMeta.ridingState = PetRidingState.NO

if (petEntity != null) {
petEntity!!.updateRidingState(player)
petEntity!!.updateRidingState()
call()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.github.shynixn.mcutils.packet.api.packet.*
import com.github.shynixn.petblocks.contract.Pet
import com.github.shynixn.petblocks.contract.PlaceHolderService
import com.github.shynixn.petblocks.entity.PetMeta
import com.github.shynixn.petblocks.enumeration.PetRidingState
import com.github.shynixn.petblocks.enumeration.PetVisibility
import org.bukkit.Location
import org.bukkit.entity.Player
import org.bukkit.util.EulerAngle
Expand All @@ -17,7 +19,7 @@ class ArmorstandEntityComponent(
private val playerComponent: PlayerComponent,
private val petMeta: PetMeta,
private val placeHolderService: PlaceHolderService,
private val pet : Pet,
private val pet: Pet,
val entityId: Int,
) : PhysicComponent {
init {
Expand All @@ -30,16 +32,30 @@ class ArmorstandEntityComponent(
packetService.sendPacketOutEntitySpawn(player, PacketOutEntitySpawn().also {
it.entityId = this.entityId
it.entityType = EntityType.ARMOR_STAND
it.target = location.toVector3d().addRelativeDown(petMeta.physics.groundOffset).toLocation()
it.target = location.toVector3d().addRelativeUp(petMeta.physics.groundOffset).toLocation()
})

val itemStack = petMeta.headItem.toItemStack()
updateEquipment(player)
updateMetaData(player)
updateRidingState(player)
}

private fun onPlayerRemove(player: Player) {
val outer = this
packetService.sendPacketOutEntityDestroy(player, PacketOutEntityDestroy().also {
it.entityIds = listOf(outer.entityId)
})
}

fun updateEquipment(player: Player) {
val itemStack = petMeta.headItem.toItemStack()
packetService.sendPacketOutEntityEquipment(player, PacketOutEntityEquipment().also {
it.entityId = this.entityId
it.items = listOf(Pair(ArmorSlotType.HELMET, itemStack))
})
}

fun updateMetaData(player: Player) {
packetService.sendPacketOutEntityMetadata(player, PacketOutEntityMetadata().also {
it.entityId = this.entityId
it.isArmorstandSmall = true
Expand All @@ -49,11 +65,49 @@ class ArmorstandEntityComponent(
})
}

private fun onPlayerRemove(player: Player) {
val outer = this
packetService.sendPacketOutEntityDestroy(player, PacketOutEntityDestroy().also {
it.entityIds = listOf(outer.entityId)
})
fun updateRidingState(player: Player) {
val owner = pet.player

if (petMeta.visibility == PetVisibility.OWNER && player != owner) {
return
}

val ridingState = petMeta.ridingState

if (ridingState == PetRidingState.NO) {
// Remove ground and fly
packetService.sendPacketOutEntityMount(player, PacketOutEntityMount().also {
it.entityId = entityId
})
// Remove hat
packetService.sendPacketOutEntityMount(player, PacketOutEntityMount().also {
it.entityId = owner.entityId
})
}

if (ridingState == PetRidingState.HAT) {
// Remove ground and fly
packetService.sendPacketOutEntityMount(player, PacketOutEntityMount().also {
it.entityId = entityId
})
// Set pet as passenger of player
packetService.sendPacketOutEntityMount(player, PacketOutEntityMount().also {
it.entityId = owner.entityId
it.passengers = listOf(entityId)
})
}

if (ridingState == PetRidingState.GROUND) {
// Remove hat
packetService.sendPacketOutEntityMount(player, PacketOutEntityMount().also {
it.entityId = owner.entityId
})
// Set pet as passenger of player
packetService.sendPacketOutEntityMount(player, PacketOutEntityMount().also {
it.entityId = entityId
it.passengers = listOf(owner.entityId)
})
}
}

private fun onPositionChange(position: Vector3d, motion: Vector3d) {
Expand All @@ -67,7 +121,7 @@ class ArmorstandEntityComponent(

packetService.sendPacketOutEntityTeleport(player, PacketOutEntityTeleport().also {
it.entityId = this.entityId
it.target = position.clone().addRelativeDown(petMeta.physics.groundOffset).toLocation()
it.target = position.clone().addRelativeUp(petMeta.physics.groundOffset).toLocation()
})

packetService.sendPacketOutEntityMetadata(player, PacketOutEntityMetadata().also {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class PetEntityFactoryImpl @Inject constructor(
plugin,
pet,
meta,
placeHolderService,
packetService,
physicObjectDispatcher,
pathfinderService,
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/pets/pet_classic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pet:
# e.g. 0.005 means that the speed is reduced by 0.005 after each tick
absoluteVelocityReduce: 0.0005
# The distance the pet hovers above the ground. Depending on the entity type this should be set lower or higher.
groundOffset: 0.35
groundOffset: -0.35

#
# Defines the action a pet should perform on certain events.
Expand Down

0 comments on commit 2e77723

Please sign in to comment.