From 1969766b7013847d9e9ddb1f3775d431d33e93a5 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 1 May 2020 11:52:32 +0100 Subject: [PATCH] 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. --- src/entity/Living.php | 20 +++++++ .../mcpe/handler/InGamePacketHandler.php | 8 +++ src/network/mcpe/protocol/AddActorPacket.php | 1 + src/world/sound/EntityLandSound.php | 57 +++++++++++++++++++ src/world/sound/EntityLongFallSound.php | 54 ++++++++++++++++++ src/world/sound/EntityShortFallSound.php | 53 +++++++++++++++++ 6 files changed, 193 insertions(+) create mode 100644 src/world/sound/EntityLandSound.php create mode 100644 src/world/sound/EntityLongFallSound.php create mode 100644 src/world/sound/EntityShortFallSound.php diff --git a/src/entity/Living.php b/src/entity/Living.php index 11993396f..d41567ce5 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -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)); } } diff --git a/src/network/mcpe/handler/InGamePacketHandler.php b/src/network/mcpe/handler/InGamePacketHandler.php index adbea3b1a..312a7fda9 100644 --- a/src/network/mcpe/handler/InGamePacketHandler.php +++ b/src/network/mcpe/handler/InGamePacketHandler.php @@ -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; } diff --git a/src/network/mcpe/protocol/AddActorPacket.php b/src/network/mcpe/protocol/AddActorPacket.php index 9dc200c86..3f375ae93 100644 --- a/src/network/mcpe/protocol/AddActorPacket.php +++ b/src/network/mcpe/protocol/AddActorPacket.php @@ -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", diff --git a/src/world/sound/EntityLandSound.php b/src/world/sound/EntityLandSound.php new file mode 100644 index 000000000..3de49758d --- /dev/null +++ b/src/world/sound/EntityLandSound.php @@ -0,0 +1,57 @@ +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? + ); + } +} \ No newline at end of file diff --git a/src/world/sound/EntityLongFallSound.php b/src/world/sound/EntityLongFallSound.php new file mode 100644 index 000000000..bef0286e7 --- /dev/null +++ b/src/world/sound/EntityLongFallSound.php @@ -0,0 +1,54 @@ +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? + ); + } +} \ No newline at end of file diff --git a/src/world/sound/EntityShortFallSound.php b/src/world/sound/EntityShortFallSound.php new file mode 100644 index 000000000..3049f652b --- /dev/null +++ b/src/world/sound/EntityShortFallSound.php @@ -0,0 +1,53 @@ +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? + ); + } +} \ No newline at end of file