Skip to content

Commit

Permalink
Merge pull request #633 from Shynixn/development
Browse files Browse the repository at this point in the history
Merge changes to master --release
  • Loading branch information
Shynixn authored Nov 22, 2024
2 parents d07f665 + 2cfd25c commit dd0c842
Show file tree
Hide file tree
Showing 18 changed files with 175 additions and 100 deletions.
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "com.github.shynixn"
version = "9.16.0"
version = "9.17.0"

repositories {
mavenCentral()
Expand Down Expand Up @@ -46,8 +46,8 @@ dependencies {

// Custom dependencies
implementation("com.github.shynixn.shygui:shygui:1.0.1")
implementation("com.github.shynixn.mcutils:common:2024.36")
implementation("com.github.shynixn.mcutils:packet:2024.47")
implementation("com.github.shynixn.mcutils:common:2024.37")
implementation("com.github.shynixn.mcutils:packet:2024.49")
implementation("com.github.shynixn.mcutils:database:2024.8")
implementation("com.github.shynixn.mcutils:pathfinder:2024.3")
implementation("com.github.shynixn.mcutils:guice:2024.2")
Expand Down
12 changes: 12 additions & 0 deletions docs/wiki/docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,18 @@ Changes the offset of the body of the entity to the ground. Useful when configur
* Offset: A numeric comma value. e.g. 0.3, -0.3, 1.0
* Player: Optional player_name/player_UUID parameter targeting a player from the console or command block.

### /petblocks ridingspeed

```
/petblocks ridingspeed <name> <speed> [player]
```

Changes the speed while riding a pet.

* Name: Identifier of a pet
* Offset: A numeric comma value. e.g. 0.3, 0.5, 1.5
* Player: Optional player_name/player_UUID parameter targeting a player from the console or command block.

### /petblocks variable

```
Expand Down
1 change: 1 addition & 0 deletions docs/wiki/docs/permission.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ The following permissions are available in PetBlocks.
| petblocks.pet.entityType | Admin/User | Allows to use the /petblocks entitytype command |
| petblocks.pet.entityVisibility | Admin/User | Allows to use the /petblocks entityvisible command
| petblocks.pet.groundOffset | Admin/User | Allows to use the /petblocks groundOffset command
| petblocks.pet.ridingSpeed | Admin/User | Allows to use the /petblocks ridingspeed command
| petblocks.pet.variable | Admin | Allows to use the /petblocks variable command
| petblocks.pet.manipulateOther | Admin | Allows to manipulate the pets of other players. e.g. Changing skins, etc. |
| petblocks.reload | Admin | Permission to use the /petblocks reload command. |
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ class PetBlocksLanguageImpl : Language, LanguageProviderImpl() {
override var variableCommandHint = LanguageItem()
override var variableChangedMessage = LanguageItem()
override var velocityRelCommandHint = LanguageItem()
override var ridingSpeedChangedMessage = LanguageItem().also {
it.text = "[&9PetBlocks&f] The riding-speed of the pet has been changed."
}
override var ridingSpeedCommandHint = LanguageItem().also {
it.text = "Changes the speed while riding a pet."
}
override val names: List<String>
get() = listOf("en_us", "es_es")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.github.shynixn.mcutils.common.language.LanguageItem
import com.github.shynixn.mcutils.common.language.LanguageProvider

interface Language : LanguageProvider {
var playerNotFoundMessage: LanguageItem
var petNameChangeMessage: LanguageItem

var templateNotFoundMessage: LanguageItem

Expand All @@ -16,7 +16,7 @@ interface Language : LanguageProvider {

var petDeletedMessage: LanguageItem

var petNameChangeMessage: LanguageItem
var playerNotFoundMessage: LanguageItem

var petCalledMessage: LanguageItem

Expand Down Expand Up @@ -209,4 +209,8 @@ interface Language : LanguageProvider {
var variableChangedMessage: LanguageItem

var velocityRelCommandHint: LanguageItem

var ridingSpeedChangedMessage: LanguageItem

var ridingSpeedCommandHint: LanguageItem
}
5 changes: 5 additions & 0 deletions src/main/java/com/github/shynixn/petblocks/contract/Pet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ interface Pet {
*/
var groundOffset: Double

/**
* Riding speed.
*/
var ridingSpeed : Double

/**
* Storage of arbitrary data in the pet.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ class PhysicSettings {
/**
* Collide With water.
*/
var collideWithWater : Boolean = false
var collideWithWater: Boolean = false

/**
* Collide With passable Blocks.
*/
var collideWithPassableBlocks : Boolean = false
var collideWithPassableBlocks: Boolean = false

/**
* Riding speed of the pet.
*/
var ridingSpeed: Double = 0.5
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ enum class Permission(val text: String) {
ENTITYTYPE("petblocks.pet.entityType"),
ENTITYVISIBILITY("petblocks.pet.entityVisibility"),
GROUNDOFFSET("petblocks.pet.groundOffset"),
RIDINGSPEED("petblocks.pet.ridingSpeed"),
VARIABLE("petblocks.pet.variable"),

// Dynamic
Expand Down
96 changes: 62 additions & 34 deletions src/main/java/com/github/shynixn/petblocks/impl/PetEntityImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.github.shynixn.mcutils.common.physic.PhysicObject
import com.github.shynixn.mcutils.common.physic.PhysicObjectDispatcher
import com.github.shynixn.mcutils.packet.api.PacketService
import com.github.shynixn.mcutils.packet.api.RayTracingService
import com.github.shynixn.mcutils.packet.api.meta.enumeration.RidingMoveType
import com.github.shynixn.mcutils.packet.api.packet.PacketOutEntityMount
import com.github.shynixn.mcutils.pathfinder.api.PathfinderResult
import com.github.shynixn.mcutils.pathfinder.api.PathfinderResultType
Expand Down Expand Up @@ -59,6 +60,8 @@ class PetEntityImpl(
private var cancellationTokenLongRunning = CancellationToken()
private var lastRideUpdate = 0L
private var lastSneakUpdate = 0L
private var ridingMoveType: RidingMoveType = RidingMoveType.STOP

var isBreakingBlock = false

// Mover
Expand Down Expand Up @@ -231,7 +234,7 @@ class PetEntityImpl(
/**
* Spawn is initiated.
*/
fun onSpawn(player: Player){
fun onSpawn(player: Player) {
cancellationTokenLongRunning.isCancelled = true
val spawnEvent = pet.template.events["spawn"]
if (spawnEvent != null) {
Expand All @@ -244,7 +247,7 @@ class PetEntityImpl(
/**
* Despawn is initiated.
*/
fun onDespawn(player: Player){
fun onDespawn(player: Player) {
cancellationTokenLongRunning.isCancelled = true
val despawnEvent = pet.template.events["despawn"]
if (despawnEvent != null) {
Expand Down Expand Up @@ -356,48 +359,51 @@ class PetEntityImpl(
/**
* Gets called when the player is riding the entity.
*/
fun ride(player: Player, forward: Double, isJumping: Boolean, isSneaking: Boolean) {
fun ride(player: Player, moveType: RidingMoveType, isJumping: Boolean, isSneaking: Boolean) {
cancellationTokenLongRunning.isCancelled = true

val current = Date().time
if (current - lastRideUpdate >= ridePositionUpdateMs) {
// Required so the position of the player stays in sync while packet riding.
packetService.setServerPlayerPosition(player, physicsComponent.position.toLocation())
for (visiblePlayers in playerComponent.visiblePlayers) {
packetService.sendPacketOutEntityMount(visiblePlayers, PacketOutEntityMount().also {
it.entityId = entityComponent.entityId
it.passengers = listOf(player.entityId)
})
if (isJumping) {
if (isOnGround(getLocation().toLocation())) {
plugin.launch(physicObjectDispatcher) {
physicsComponent.motion.y = 1.0
}
}
lastRideUpdate = current
}
synchronizeRidingState(player)

val isOnGround = if (isJumping) {
isOnGround(getLocation().toLocation())
} else {
false
return
}

plugin.launch(physicObjectDispatcher) {
if (forward != 0.0) {
val movementVector = if (forward > 0.0) {
player.location.direction.normalize().multiply(0.5).toVector3d()
} else {
player.location.direction.normalize().multiply(-0.5).toVector3d()
}
if (this.ridingMoveType == RidingMoveType.STOP) {
this.ridingMoveType = moveType
plugin.launch(physicObjectDispatcher) {
while (!isDead && ridingMoveType != RidingMoveType.STOP && player.isOnline && pet.isRiding()) {
synchronizeRidingState(player)
val movementVector = if (ridingMoveType == RidingMoveType.FORWARD) {
player.location.direction.normalize().multiply(petMeta.physics.ridingSpeed).toVector3d()
} else {
player.location.direction.normalize().multiply(petMeta.physics.ridingSpeed * -1).toVector3d()
}

physicsComponent.motion.x = movementVector.x
physicsComponent.motion.z = movementVector.z
}
physicsComponent.motion.x = movementVector.x
physicsComponent.motion.z = movementVector.z

if (isJumping && isOnGround) {
physicsComponent.motion.y = 1.0
physicsComponent.position.pitch = 0.0
physicsComponent.position.yaw = player.location.yaw.toDouble()
delay(5.ticks)
}
}
}

this.ridingMoveType = moveType

physicsComponent.position.pitch = 0.0
physicsComponent.position.yaw = player.location.yaw.toDouble()
if (this.ridingMoveType == RidingMoveType.STOP) {
// Fast Stop.
physicsComponent.motion.x = 0.0
physicsComponent.motion.z = 0.0
}

val current = System.currentTimeMillis()

if (isSneaking && current - lastSneakUpdate >= 200) {
lastSneakUpdate = current
val sneakEvent = pet.template.events["ridingSneak"]
Expand All @@ -409,8 +415,28 @@ class PetEntityImpl(
}
}

private fun synchronizeRidingState(player: Player) {
val current = Date().time
if (current - lastRideUpdate >= ridePositionUpdateMs) {
// Required so the position of the player stays in sync while packet riding.
packetService.setServerPlayerPosition(player, physicsComponent.position.toLocation())
for (visiblePlayers in playerComponent.visiblePlayers) {
packetService.sendPacketOutEntityMount(visiblePlayers, PacketOutEntityMount().also {
it.entityId = entityComponent.entityId
it.passengers = listOf(player.entityId)
})
}
lastRideUpdate = current
}
}

private fun isOnGround(location: Location): Boolean {
val rayTraceResult = rayTracingService.rayTraceMotion(location.toVector3d(), Vector3d(0.0, -1.0, 0.0), petMeta.physics.collideWithWater, petMeta.physics.collideWithPassableBlocks)
val rayTraceResult = rayTracingService.rayTraceMotion(
location.toVector3d(),
Vector3d(0.0, -1.0, 0.0),
petMeta.physics.collideWithWater,
petMeta.physics.collideWithPassableBlocks
)
return rayTraceResult.hitBlock
}

Expand Down Expand Up @@ -496,7 +522,9 @@ class PetEntityImpl(
val worldLocation = getLocation().toLocation()
val rayTraceResult = rayTracingService.rayTraceMotion(
worldLocation.toVector3d(),
worldLocation.direction.normalize().multiply(maxDistance).toVector3d(),petMeta.physics.collideWithWater, petMeta.physics.collideWithPassableBlocks
worldLocation.direction.normalize().multiply(maxDistance).toVector3d(),
petMeta.physics.collideWithWater,
petMeta.physics.collideWithPassableBlocks
)
return rayTraceResult.block
}
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/com/github/shynixn/petblocks/impl/PetImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.shynixn.petblocks.impl
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.*
import com.github.shynixn.mcutils.common.item.Item
import com.github.shynixn.mcutils.common.item.ItemService
Expand Down Expand Up @@ -40,7 +41,7 @@ class PetImpl(
private var petEntity: PetEntityImpl? = null
private var disposed = false
private var templateCache: PetTemplate? = null
private var isWorldTransferActive : Boolean = false
private var isWorldTransferActive: Boolean = false

init {
plugin.launch(plugin.minecraftDispatcher + object : CoroutineTimings() {}) {
Expand Down Expand Up @@ -295,6 +296,17 @@ class PetImpl(
location = location // Triggers teleport.
}

/**
* Riding speed.
*/
override var ridingSpeed: Double
get() {
return petMeta.physics.ridingSpeed
}
set(value) {
petMeta.physics.ridingSpeed = value
}

/**
* Storage of arbitrary data in the pet.
*/
Expand Down Expand Up @@ -419,7 +431,13 @@ class PetImpl(

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

plugin.launch {
val location = player.location.toVector3d()
petEntity?.updateRidingState()
delay(3.ticks)
petEntity?.teleportInWorld(location) // Correct riding position.
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,18 @@ class PetBlocksCommandExecutor @Inject constructor(
setGroundOffset(commandSender, petMustExist(player, name), offset)
}
}
subCommand("ridingspeed") {
permission(Permission.RIDINGSPEED)
toolTip { language.ridingSpeedCommandHint.text }
builder().argument("name").tabs(petNamesTabs).argument("speed").validator(mustBeDouble)
.tabs { listOf("<speed>") }.executePlayer(senderHasToBePlayer) { player, name, speed ->
setRidingSpeed(player, petMustExist(player, name), speed)
}.argument("player").validator(playerMustExist).tabs(onlinePlayerTabs)
.permission(manipulateOtherPermission).permissionMessage(manipulateOtherPermissionMessage)
.execute { commandSender, name, speed, player ->
setRidingSpeed(commandSender, petMustExist(player, name), speed)
}
}
subCommand("snap") {
permission(Permission.SNAP)
toolTip { language.snapCommandHint.text }
Expand Down Expand Up @@ -792,6 +804,11 @@ class PetBlocksCommandExecutor @Inject constructor(
commandBuilder.build()
}

private fun setRidingSpeed(sender: CommandSender, pet: Pet, speed: Double) {
pet.ridingSpeed = speed
language.sendMessage(language.ridingSpeedChangedMessage, sender, speed)
}

private suspend fun createPet(
sender: CommandSender, player: Player, petName: String, petTemplate: PetTemplate
) {
Expand Down
Loading

0 comments on commit dd0c842

Please sign in to comment.