Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block break progress improvement #6500

Open
wants to merge 15 commits into
base: minor-next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
23 changes: 23 additions & 0 deletions src/player/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,29 @@ static function() : void{
Timings::$playerChunkSend->stopTiming();
}

/**
* Checks if the player is currently on the ground. This is more accurate than {@link Player::isOnGround()} but slower.
*/
public function isActuallyOnGround() : bool{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like calculate or smth in the name can be better than this function combined with docs ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm not a fan of this naming either. Also, why do we actually need this?

Copy link
Contributor Author

@GameParrot GameParrot Nov 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm not a fan of this naming either. Also, why do we actually need this?

We need it because when using the normal is on ground method the break progress indicator is still too fast when jumping. What should it be renamed to?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like that warrants investigation into regular onGround behaviour.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Legend has it that PlayerAuthInputPacket should allow making onGround checks more accurate, so this hack wouldn't be needed anymore.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the PR should use the built-in onGround for now. It doesn't make sense to add an extra function specifically for this.

Once the proper onGround is fixed per #6547 we won't need this workaround. I don't like adding stuff to the API that we know we're gonna delete anyway.

$bb = $this->boundingBox->expandedCopy(0, 0.001, 0);
$maxY = (int) floor($this->location->y);
$minY = $maxY - 1;
$floorMinX = (int) floor($bb->minX);
$floorMinZ = (int) floor($bb->minZ);
$floorMaxX = (int) floor($bb->maxX);
$floorMaxZ = (int) floor($bb->maxZ);
for ($x = $floorMinX ; $x <= $floorMaxX ; $x++){
for ($y = $minY ; $y <= $maxY ; $y++){
for ($z = $floorMinZ ; $z <= $floorMaxZ ; $z++){
if ($this->getWorld()->getBlockAt($x, $y, $z)->collidesWithBB($bb)){
return true;
}
}
}
}
return false;
}

private function recheckBroadcastPermissions() : void{
foreach([
DefaultPermissionNames::BROADCAST_ADMIN => Server::BROADCAST_CHANNEL_ADMINISTRATIVE,
Expand Down
31 changes: 27 additions & 4 deletions src/player/SurvivalBlockBreakHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

use pocketmine\block\Block;
use pocketmine\entity\animation\ArmSwingAnimation;
use pocketmine\entity\effect\VanillaEffects;
use pocketmine\item\enchantment\VanillaEnchantments;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\LevelEventPacket;
Expand Down Expand Up @@ -65,11 +67,29 @@ private function calculateBreakProgressPerTick() : float{
if(!$this->block->getBreakInfo()->isBreakable()){
return 0.0;
}
//TODO: improve this to take stuff like swimming, ladders, enchanted tools into account, fix wrong tool break time calculations for bad tools (pmmp/PocketMine-MP#211)
$breakTimePerTick = $this->block->getBreakInfo()->getBreakTime($this->player->getInventory()->getItemInHand()) * 20;

if (!$this->player->isActuallyOnGround() && !$this->player->isFlying()){
ShockedPlot7560 marked this conversation as resolved.
Show resolved Hide resolved
$breakTimePerTick *= 5;
}
if ($this->player->isUnderwater() && !$this->player->getArmorInventory()->getHelmet()->hasEnchantment(VanillaEnchantments::AQUA_AFFINITY())){
$breakTimePerTick *= 5;
}
if($breakTimePerTick > 0){
return 1 / $breakTimePerTick;
$progressPerTick = 1 / $breakTimePerTick;

$haste = $this->player->getEffects()->get(VanillaEffects::HASTE());
if ($haste !== null){
$amplifier = $haste->getAmplifier() + 1;
GameParrot marked this conversation as resolved.
Show resolved Hide resolved
$progressPerTick *= (1 + 0.2 * $amplifier) * (1.2 ** $amplifier);
}

$miningFatigue = $this->player->getEffects()->get(VanillaEffects::MINING_FATIGUE());
if ($miningFatigue !== null){
$amplifier = $miningFatigue->getAmplifier() + 1;
GameParrot marked this conversation as resolved.
Show resolved Hide resolved
$progressPerTick *= 0.21 ** $amplifier;
}

return $progressPerTick;
}
return 1;
}
Expand All @@ -82,7 +102,10 @@ public function update() : bool{
$newBreakSpeed = $this->calculateBreakProgressPerTick();
if(abs($newBreakSpeed - $this->breakSpeed) > 0.0001){
$this->breakSpeed = $newBreakSpeed;
//TODO: sync with client
$this->player->getWorld()->broadcastPacketToViewers(
$this->blockPos,
LevelEventPacket::create(LevelEvent::BLOCK_BREAK_SPEED, (int) (65535 * $this->breakSpeed), $this->blockPos)
);
}

$this->breakProgress += $this->breakSpeed;
Expand Down