Nix some client-sided sounds, control them from the server

this is a necessary step to knock out the implicit assumption that every player is using the same protocol.
This commit is contained in:
Dylan K. Taylor 2020-05-01 11:52:32 +01:00
parent 6f38031121
commit 1969766b70
6 changed files with 193 additions and 0 deletions

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\entity;
use pocketmine\block\Block;
use pocketmine\block\BlockLegacyIds;
use pocketmine\entity\effect\EffectInstance;
use pocketmine\entity\effect\EffectManager;
use pocketmine\entity\effect\VanillaEffects;
@ -51,6 +52,9 @@ use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties;
use pocketmine\player\Player;
use pocketmine\timings\Timings;
use pocketmine\utils\Binary;
use pocketmine\world\sound\EntityLandSound;
use pocketmine\world\sound\EntityLongFallSound;
use pocketmine\world\sound\EntityShortFallSound;
use pocketmine\world\sound\ItemBreakSound;
use function array_shift;
use function atan2;
@ -248,6 +252,22 @@ abstract class Living extends Entity{
if($damage > 0){
$ev = new EntityDamageEvent($this, EntityDamageEvent::CAUSE_FALL, $damage);
$this->attack($ev);
$this->getWorld()->addSound($this->location, $damage > 4 ?
new EntityLongFallSound($this) :
new EntityShortFallSound($this)
);
}else{
$fallBlockPos = $this->location->floor();
$fallBlock = $this->getWorld()->getBlock($fallBlockPos);
for(
;
$fallBlock->getId() === BlockLegacyIds::AIR;
$fallBlockPos = $fallBlockPos->subtract(0, 1, 0), $fallBlock = $this->getWorld()->getBlock($fallBlockPos)
){
//this allows the correct sound to be played when landing in snow
}
$this->getWorld()->addSound($this->location, new EntityLandSound($this, $fallBlock));
}
}

View File

@ -754,6 +754,14 @@ class InGamePacketHandler extends PacketHandler{
}
public function handleLevelSoundEvent(LevelSoundEventPacket $packet) : bool{
//TODO: we want to block out this packet completely, but we don't yet know the full scope of sounds that the client sends us from here
switch($packet->sound){
case LevelSoundEventPacket::SOUND_LAND:
case LevelSoundEventPacket::SOUND_FALL:
case LevelSoundEventPacket::SOUND_FALL_SMALL:
case LevelSoundEventPacket::SOUND_FALL_BIG:
return true;
}
$this->player->getWorld()->broadcastPacketToViewers($this->player->getPosition(), $packet);
return true;
}

View File

@ -44,6 +44,7 @@ class AddActorPacket extends DataPacket implements ClientboundPacket{
* TODO: remove this on 4.0
*/
public const LEGACY_ID_MAP_BC = [
-1 => ":",
EntityLegacyIds::NPC => "minecraft:npc",
EntityLegacyIds::PLAYER => "minecraft:player",
EntityLegacyIds::WITHER_SKELETON => "minecraft:wither_skeleton",

View File

@ -0,0 +1,57 @@
<?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\world\sound;
use pocketmine\block\Block;
use pocketmine\entity\Entity;
use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\AddActorPacket;
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
use pocketmine\player\Player;
/**
* Played when an entity hits the ground after falling a distance that doesn't cause damage, e.g. due to jumping.
*/
class EntityLandSound implements Sound{
/** @var Entity */
private $entity;
/** @var Block */
private $blockLandedOn;
public function __construct(Entity $entity, Block $blockLandedOn){
$this->entity = $entity;
$this->blockLandedOn = $blockLandedOn;
}
public function encode(?Vector3 $pos){
return LevelSoundEventPacket::create(
LevelSoundEventPacket::SOUND_LAND,
$pos,
$this->blockLandedOn->getRuntimeId(),
$this->entity instanceof Player ? "minecraft:player" : AddActorPacket::LEGACY_ID_MAP_BC[$this->entity::NETWORK_ID] //TODO: bad hack, stuff depends on players having a -1 network ID :(
//TODO: does isBaby have any relevance here?
);
}
}

View File

@ -0,0 +1,54 @@
<?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\world\sound;
use pocketmine\entity\Entity;
use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\AddActorPacket;
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
use pocketmine\player\Player;
/**
* Played when an entity hits ground after falling a long distance (damage).
* This is the bone-breaker "crunch" sound.
*/
class EntityLongFallSound implements Sound{
/** @var Entity */
private $entity;
public function __construct(Entity $entity){
$this->entity = $entity;
}
public function encode(?Vector3 $pos){
return LevelSoundEventPacket::create(
LevelSoundEventPacket::SOUND_FALL_BIG,
$pos,
-1,
$this->entity instanceof Player ? "minecraft:player" : AddActorPacket::LEGACY_ID_MAP_BC[$this->entity::NETWORK_ID] //TODO: bad hack, stuff depends on players having a -1 network ID :(
//TODO: is isBaby relevant here?
);
}
}

View File

@ -0,0 +1,53 @@
<?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\world\sound;
use pocketmine\entity\Entity;
use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\AddActorPacket;
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
use pocketmine\player\Player;
/**
* Played when an entity hits the ground after falling a short distance.
*/
class EntityShortFallSound implements Sound{
/** @var Entity */
private $entity;
public function __construct(Entity $entity){
$this->entity = $entity;
}
public function encode(?Vector3 $pos){
return LevelSoundEventPacket::create(
LevelSoundEventPacket::SOUND_FALL_SMALL,
$pos,
-1,
$this->entity instanceof Player ? "minecraft:player" : AddActorPacket::LEGACY_ID_MAP_BC[$this->entity::NETWORK_ID] //TODO: bad hack, stuff depends on players having a -1 network ID :(
//TODO: does isBaby have any relevance here?
);
}
}