From ecfcf49984d4f359e5ea5d06d1077f558074650e Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 25 Jun 2017 15:30:26 +0100 Subject: [PATCH] Added basic encode/decode for gamerules data and added GameRulesChangedPacket needed it for tests Squashed: oops PhpStorm you asshole --- src/pocketmine/Player.php | 5 ++ src/pocketmine/network/Network.php | 2 + .../network/mcpe/NetworkSession.php | 3 +- .../network/mcpe/protocol/DataPacket.php | 56 +++++++++++++++++++ .../mcpe/protocol/GameRulesChangedPacket.php | 48 ++++++++++++++++ .../network/mcpe/protocol/StartGamePacket.php | 7 ++- 6 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 src/pocketmine/network/mcpe/protocol/GameRulesChangedPacket.php diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 8108a8afc..9b56c78e2 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -138,6 +138,7 @@ use pocketmine\network\mcpe\protocol\DropItemPacket; use pocketmine\network\mcpe\protocol\EntityEventPacket; use pocketmine\network\mcpe\protocol\ExplodePacket; use pocketmine\network\mcpe\protocol\FullChunkDataPacket; +use pocketmine\network\mcpe\protocol\GameRulesChangedPacket; use pocketmine\network\mcpe\protocol\HurtArmorPacket; use pocketmine\network\mcpe\protocol\InteractPacket; use pocketmine\network\mcpe\protocol\InventoryActionPacket; @@ -3313,6 +3314,10 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade return false; } + public function handleGameRulesChanged(GameRulesChangedPacket $packet) : bool{ + return false; + } + public function handleAddItem(AddItemPacket $packet) : bool{ return false; } diff --git a/src/pocketmine/network/Network.php b/src/pocketmine/network/Network.php index 7be7710bf..a9a0d1b74 100644 --- a/src/pocketmine/network/Network.php +++ b/src/pocketmine/network/Network.php @@ -59,6 +59,7 @@ use pocketmine\network\mcpe\protocol\DropItemPacket; use pocketmine\network\mcpe\protocol\EntityEventPacket; use pocketmine\network\mcpe\protocol\ExplodePacket; use pocketmine\network\mcpe\protocol\FullChunkDataPacket; +use pocketmine\network\mcpe\protocol\GameRulesChangedPacket; use pocketmine\network\mcpe\protocol\HurtArmorPacket; use pocketmine\network\mcpe\protocol\InteractPacket; use pocketmine\network\mcpe\protocol\InventoryActionPacket; @@ -313,6 +314,7 @@ class Network{ $this->registerPacket(ProtocolInfo::ENTITY_FALL_PACKET, EntityFallPacket::class); $this->registerPacket(ProtocolInfo::EXPLODE_PACKET, ExplodePacket::class); $this->registerPacket(ProtocolInfo::FULL_CHUNK_DATA_PACKET, FullChunkDataPacket::class); + $this->registerPacket(ProtocolInfo::GAME_RULES_CHANGED_PACKET, GameRulesChangedPacket::class); $this->registerPacket(ProtocolInfo::HURT_ARMOR_PACKET, HurtArmorPacket::class); $this->registerPacket(ProtocolInfo::INTERACT_PACKET, InteractPacket::class); $this->registerPacket(ProtocolInfo::INVENTORY_ACTION_PACKET, InventoryActionPacket::class); diff --git a/src/pocketmine/network/mcpe/NetworkSession.php b/src/pocketmine/network/mcpe/NetworkSession.php index 9f4fb992b..20fc6fb5f 100644 --- a/src/pocketmine/network/mcpe/NetworkSession.php +++ b/src/pocketmine/network/mcpe/NetworkSession.php @@ -57,6 +57,7 @@ use pocketmine\network\mcpe\protocol\DropItemPacket; use pocketmine\network\mcpe\protocol\EntityEventPacket; use pocketmine\network\mcpe\protocol\ExplodePacket; use pocketmine\network\mcpe\protocol\FullChunkDataPacket; +use pocketmine\network\mcpe\protocol\GameRulesChangedPacket; use pocketmine\network\mcpe\protocol\HurtArmorPacket; use pocketmine\network\mcpe\protocol\InteractPacket; use pocketmine\network\mcpe\protocol\InventoryActionPacket; @@ -263,7 +264,7 @@ interface NetworkSession{ public function handleReplaceItemInSlot(ReplaceItemInSlotPacket $packet) : bool; - //public function handleGameRulesChanged(GameRulesChangedPacket $packet) : bool; //TODO + public function handleGameRulesChanged(GameRulesChangedPacket $packet) : bool; //public function handleCamera(CameraPacket $packet) : bool; //edu only :( diff --git a/src/pocketmine/network/mcpe/protocol/DataPacket.php b/src/pocketmine/network/mcpe/protocol/DataPacket.php index 091e332c1..c1f7051d0 100644 --- a/src/pocketmine/network/mcpe/protocol/DataPacket.php +++ b/src/pocketmine/network/mcpe/protocol/DataPacket.php @@ -363,4 +363,60 @@ abstract class DataPacket extends BinaryStream{ public function putByteRotation(float $rotation){ $this->putByte((int) ($rotation / (360 / 256))); } + + /** + * Reads gamerules + * TODO: implement this properly + * + * @return array + */ + public function getGameRules() : array{ + $count = $this->getUnsignedVarInt(); + $rules = []; + for($i = 0; $i < $count; ++$i){ + $name = $this->getString(); + $type = $this->getUnsignedVarInt(); + $value = null; + switch($type){ + case 1: + $value = $this->getBool(); + break; + case 2: + $value = $this->getUnsignedVarInt(); + break; + case 3: + $value = $this->getLFloat(); + break; + } + + $rules[$name] = [$type, $value]; + } + + return $rules; + } + + /** + * Writes a gamerule array + * TODO: implement this properly + * + * @param array $rules + */ + public function putGameRules(array $rules){ + $this->putUnsignedVarInt(count($rules)); + foreach($rules as $name => $rule){ + $this->putString($name); + $this->putUnsignedVarInt($rule[0]); + switch($rule[0]){ + case 1: + $this->putBool($rule[1]); + break; + case 2: + $this->putUnsignedVarInt($rule[1]); + break; + case 3: + $this->putLFloat($rule[1]); + break; + } + } + } } diff --git a/src/pocketmine/network/mcpe/protocol/GameRulesChangedPacket.php b/src/pocketmine/network/mcpe/protocol/GameRulesChangedPacket.php new file mode 100644 index 000000000..cb740461a --- /dev/null +++ b/src/pocketmine/network/mcpe/protocol/GameRulesChangedPacket.php @@ -0,0 +1,48 @@ + + +use pocketmine\network\mcpe\NetworkSession; + +class GameRulesChangedPacket extends DataPacket{ + const NETWORK_ID = ProtocolInfo::GAME_RULES_CHANGED_PACKET; + + public $gameRules = []; + + public function decode(){ + $this->gameRules = $this->getGameRules(); + } + + public function encode(){ + $this->reset(); + $this->putGameRules($this->gameRules); + } + + public function handle(NetworkSession $session) : bool{ + return $session->handleGameRulesChanged($this); + } + +} \ No newline at end of file diff --git a/src/pocketmine/network/mcpe/protocol/StartGamePacket.php b/src/pocketmine/network/mcpe/protocol/StartGamePacket.php index cc0713044..b2abc89b7 100644 --- a/src/pocketmine/network/mcpe/protocol/StartGamePacket.php +++ b/src/pocketmine/network/mcpe/protocol/StartGamePacket.php @@ -54,6 +54,7 @@ class StartGamePacket extends DataPacket{ public $lightningLevel; public $commandsEnabled; public $isTexturePacksRequired = true; + public $gameRules = []; //TODO: implement this public $levelId = ""; //base64 string, usually the same as world folder name in vanilla public $worldName; public $premiumWorldTemplateId = ""; @@ -80,7 +81,7 @@ class StartGamePacket extends DataPacket{ $this->lightningLevel = $this->getLFloat(); $this->commandsEnabled = $this->getBool(); $this->isTexturePacksRequired = $this->getBool(); - /*$gameRulesCount = */$this->getUnsignedVarInt(); //TODO + $this->gameRules = $this->getGameRules(); $this->levelId = $this->getString(); $this->worldName = $this->getString(); $this->premiumWorldTemplateId = $this->getString(); @@ -93,7 +94,7 @@ class StartGamePacket extends DataPacket{ $this->reset(); $this->putEntityUniqueId($this->entityUniqueId); $this->putEntityRuntimeId($this->entityRuntimeId); - $this->putVarInt($this->playerGamemode); //client gamemode, other field is world gamemode + $this->putVarInt($this->playerGamemode); $this->putVector3f($this->x, $this->y, $this->z); $this->putLFloat($this->pitch); $this->putLFloat($this->yaw); @@ -110,7 +111,7 @@ class StartGamePacket extends DataPacket{ $this->putLFloat($this->lightningLevel); $this->putBool($this->commandsEnabled); $this->putBool($this->isTexturePacksRequired); - $this->putUnsignedVarInt(0); //TODO: gamerules + $this->putGameRules($this->gameRules); $this->putString($this->levelId); $this->putString($this->worldName); $this->putString($this->premiumWorldTemplateId);