mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-22 00:33:59 +00:00
Implemented server-side block-break FX handling, closes #3485
this had been planned for a long time already, just never finished. It's not fully done, because there needs to be synchronization of block-break handlers between different players attempting to break the same block, but this should resolve a lot of the bugs that previously existed, while also opening the doors to making the logic more flexible.
This commit is contained in:
parent
24b63d71ab
commit
3a6cdba281
@ -85,7 +85,6 @@ use pocketmine\nbt\tag\IntTag;
|
||||
use pocketmine\nbt\tag\ListTag;
|
||||
use pocketmine\network\mcpe\NetworkSession;
|
||||
use pocketmine\network\mcpe\protocol\AnimatePacket;
|
||||
use pocketmine\network\mcpe\protocol\LevelEventPacket;
|
||||
use pocketmine\network\mcpe\protocol\MovePlayerPacket;
|
||||
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags;
|
||||
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties;
|
||||
@ -101,15 +100,12 @@ use pocketmine\world\ChunkListener;
|
||||
use pocketmine\world\ChunkListenerNoOpTrait;
|
||||
use pocketmine\world\ChunkLoader;
|
||||
use pocketmine\world\format\Chunk;
|
||||
use pocketmine\world\particle\BlockPunchParticle;
|
||||
use pocketmine\world\Position;
|
||||
use pocketmine\world\sound\BlockPunchSound;
|
||||
use pocketmine\world\sound\EntityAttackNoDamageSound;
|
||||
use pocketmine\world\sound\EntityAttackSound;
|
||||
use pocketmine\world\World;
|
||||
use function abs;
|
||||
use function assert;
|
||||
use function ceil;
|
||||
use function count;
|
||||
use function explode;
|
||||
use function floor;
|
||||
@ -251,6 +247,9 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
|
||||
/** @var \Logger */
|
||||
protected $logger;
|
||||
|
||||
/** @var SurvivalBlockBreakHandler|null */
|
||||
protected $blockBreakHandler = null;
|
||||
|
||||
public function __construct(Server $server, NetworkSession $session, PlayerInfo $playerInfo, bool $authenticated){
|
||||
$username = TextFormat::clean($playerInfo->getUsername());
|
||||
$this->logger = new \PrefixedLogger($server->getLogger(), "Player: $username");
|
||||
@ -1336,6 +1335,10 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
|
||||
$this->checkNearEntities();
|
||||
Timings::$playerCheckNearEntitiesTimer->stopTiming();
|
||||
}
|
||||
|
||||
if($this->blockBreakHandler !== null and !$this->blockBreakHandler->update()){
|
||||
$this->blockBreakHandler = null;
|
||||
}
|
||||
}
|
||||
|
||||
$this->timings->stopTiming();
|
||||
@ -1584,27 +1587,23 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
|
||||
}
|
||||
|
||||
if(!$this->isCreative()){
|
||||
//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)
|
||||
$breakTime = ceil($target->getBreakInfo()->getBreakTime($this->inventory->getItemInHand()) * 20);
|
||||
if($breakTime > 0){
|
||||
$this->getWorld()->broadcastPacketToViewers($pos, LevelEventPacket::create(LevelEventPacket::EVENT_BLOCK_START_BREAK, (int) (65535 / $breakTime), $pos));
|
||||
}
|
||||
$this->blockBreakHandler = new SurvivalBlockBreakHandler($this, $pos, $target, $face, 16);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function continueBreakBlock(Vector3 $pos, int $face) : void{
|
||||
$block = $this->getWorld()->getBlock($pos);
|
||||
$this->getWorld()->addParticle($pos, new BlockPunchParticle($block, $face));
|
||||
$this->getWorld()->addSound($pos, new BlockPunchSound($block));
|
||||
$this->broadcastAnimation(new ArmSwingAnimation($this), $this->getViewers());
|
||||
|
||||
//TODO: destroy-progress level event
|
||||
if($this->blockBreakHandler !== null and $this->blockBreakHandler->getBlockPos()->distanceSquared($pos) < 0.0001){
|
||||
//TODO: check the targeted block matches the one we're told to target
|
||||
$this->blockBreakHandler->setTargetedFace($face);
|
||||
}
|
||||
}
|
||||
|
||||
public function stopBreakBlock(Vector3 $pos) : void{
|
||||
$this->getWorld()->broadcastPacketToViewers($pos, LevelEventPacket::create(LevelEventPacket::EVENT_BLOCK_STOP_BREAK, 0, $pos));
|
||||
if($this->blockBreakHandler !== null and $this->blockBreakHandler->getBlockPos()->distanceSquared($pos) < 0.0001){
|
||||
$this->blockBreakHandler = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1617,6 +1616,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
|
||||
|
||||
if($this->canInteract($pos->add(0.5, 0.5, 0.5), $this->isCreative() ? 13 : 7) and !$this->isSpectator()){
|
||||
$this->broadcastAnimation(new ArmSwingAnimation($this), $this->getViewers());
|
||||
$this->stopBreakBlock($pos);
|
||||
$item = $this->inventory->getItemInHand();
|
||||
$oldItem = clone $item;
|
||||
if($this->getWorld()->useBreakOn($pos, $item, $this, true)){
|
||||
@ -1978,6 +1978,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
|
||||
$this->spawned = false;
|
||||
|
||||
$this->stopSleep();
|
||||
$this->blockBreakHandler = null;
|
||||
$this->despawnFromAll();
|
||||
|
||||
$this->server->removeOnlinePlayer($this);
|
||||
@ -2019,6 +2020,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
|
||||
$this->craftingGrid = null;
|
||||
$this->spawnPosition = null;
|
||||
$this->perm = null;
|
||||
$this->blockBreakHandler = null;
|
||||
parent::destroyCycles();
|
||||
}
|
||||
|
||||
@ -2229,6 +2231,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
|
||||
$this->nextChunkOrderRun = 0;
|
||||
$this->newPosition = null;
|
||||
$this->stopSleep();
|
||||
$this->blockBreakHandler = null;
|
||||
|
||||
$this->isTeleporting = true;
|
||||
|
||||
|
142
src/player/SurvivalBlockBreakHandler.php
Normal file
142
src/player/SurvivalBlockBreakHandler.php
Normal file
@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\player;
|
||||
|
||||
use pocketmine\block\Block;
|
||||
use pocketmine\entity\animation\ArmSwingAnimation;
|
||||
use pocketmine\math\Facing;
|
||||
use pocketmine\math\Vector3;
|
||||
use pocketmine\network\mcpe\protocol\LevelEventPacket;
|
||||
use pocketmine\world\particle\BlockPunchParticle;
|
||||
use pocketmine\world\sound\BlockPunchSound;
|
||||
use function abs;
|
||||
|
||||
final class SurvivalBlockBreakHandler{
|
||||
|
||||
public const DEFAULT_FX_INTERVAL_TICKS = 5;
|
||||
|
||||
/** @var Player */
|
||||
private $player;
|
||||
/** @var Vector3 */
|
||||
private $blockPos;
|
||||
/** @var Block */
|
||||
private $block;
|
||||
/** @var int */
|
||||
private $targetedFace;
|
||||
|
||||
/** @var int */
|
||||
private $fxTicker = 0;
|
||||
/** @var int */
|
||||
private $fxTickInterval;
|
||||
/** @var int */
|
||||
private $maxPlayerDistance;
|
||||
|
||||
/** @var float|null */
|
||||
private $breakSpeed;
|
||||
|
||||
/** @var float */
|
||||
private $breakProgress = 0;
|
||||
|
||||
public function __construct(Player $player, Vector3 $blockPos, Block $block, int $targetedFace, int $maxPlayerDistance, int $fxTickInterval = self::DEFAULT_FX_INTERVAL_TICKS){
|
||||
$this->player = $player;
|
||||
$this->blockPos = $blockPos;
|
||||
$this->block = $block;
|
||||
$this->targetedFace = $targetedFace;
|
||||
$this->fxTickInterval = $fxTickInterval;
|
||||
$this->maxPlayerDistance = $maxPlayerDistance;
|
||||
|
||||
$this->breakSpeed = $this->calculateBreakProgressPerTick();
|
||||
if($this->breakSpeed !== null){
|
||||
$this->player->getWorld()->broadcastPacketToViewers(
|
||||
$this->blockPos,
|
||||
LevelEventPacket::create(LevelEventPacket::EVENT_BLOCK_START_BREAK, (int) (65535 * $this->breakSpeed), $this->blockPos)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the calculated break speed as percentage progress per game tick.
|
||||
*/
|
||||
private function calculateBreakProgressPerTick() : ?float{
|
||||
//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)
|
||||
$breakTime = $this->block->getBreakInfo()->getBreakTime($this->player->getInventory()->getItemInHand()) * 20;
|
||||
if($breakTime > 0){
|
||||
return 1 / $breakTime;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function update() : bool{
|
||||
if(
|
||||
$this->player->getPosition()->distanceSquared($this->blockPos->add(0.5, 0.5, 0.5)) > $this->maxPlayerDistance){
|
||||
return false;
|
||||
}
|
||||
if($this->breakSpeed !== null){
|
||||
$newBreakSpeed = $this->calculateBreakProgressPerTick();
|
||||
if(abs($newBreakSpeed - $this->breakSpeed) > 0.0001){
|
||||
$this->breakSpeed = $newBreakSpeed;
|
||||
//TODO: sync with client
|
||||
}
|
||||
|
||||
$this->breakProgress += $this->breakSpeed;
|
||||
|
||||
if(($this->fxTicker++ % $this->fxTickInterval) === 0){
|
||||
$this->player->getWorld()->addParticle($this->blockPos, new BlockPunchParticle($this->block, $this->targetedFace));
|
||||
$this->player->getWorld()->addSound($this->blockPos, new BlockPunchSound($this->block));
|
||||
$this->player->broadcastAnimation(new ArmSwingAnimation($this->player), $this->player->getViewers());
|
||||
}
|
||||
}
|
||||
return $this->breakSpeed !== null and $this->breakProgress < 1;
|
||||
}
|
||||
|
||||
public function getBlockPos() : Vector3{
|
||||
return $this->blockPos;
|
||||
}
|
||||
|
||||
public function getTargetedFace() : int{
|
||||
return $this->targetedFace;
|
||||
}
|
||||
|
||||
public function setTargetedFace(int $face) : void{
|
||||
Facing::validate($face);
|
||||
$this->targetedFace = $face;
|
||||
}
|
||||
|
||||
public function getBreakSpeed() : ?float{
|
||||
return $this->breakSpeed;
|
||||
}
|
||||
|
||||
public function getBreakProgress() : float{
|
||||
return $this->breakProgress;
|
||||
}
|
||||
|
||||
public function __destruct(){
|
||||
if($this->breakSpeed !== null and $this->player->getWorld()->isInLoadedTerrain($this->blockPos)){
|
||||
$this->player->getWorld()->broadcastPacketToViewers(
|
||||
$this->blockPos,
|
||||
LevelEventPacket::create(LevelEventPacket::EVENT_BLOCK_STOP_BREAK, 0, $this->blockPos)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user