Added basic encode/decode for gamerules data and added GameRulesChangedPacket

needed it for tests

Squashed:

oops

PhpStorm you asshole
This commit is contained in:
Dylan K. Taylor 2017-06-25 15:30:26 +01:00
parent f7aad8e2fe
commit ecfcf49984
6 changed files with 117 additions and 4 deletions

View File

@ -138,6 +138,7 @@ use pocketmine\network\mcpe\protocol\DropItemPacket;
use pocketmine\network\mcpe\protocol\EntityEventPacket; use pocketmine\network\mcpe\protocol\EntityEventPacket;
use pocketmine\network\mcpe\protocol\ExplodePacket; use pocketmine\network\mcpe\protocol\ExplodePacket;
use pocketmine\network\mcpe\protocol\FullChunkDataPacket; use pocketmine\network\mcpe\protocol\FullChunkDataPacket;
use pocketmine\network\mcpe\protocol\GameRulesChangedPacket;
use pocketmine\network\mcpe\protocol\HurtArmorPacket; use pocketmine\network\mcpe\protocol\HurtArmorPacket;
use pocketmine\network\mcpe\protocol\InteractPacket; use pocketmine\network\mcpe\protocol\InteractPacket;
use pocketmine\network\mcpe\protocol\InventoryActionPacket; use pocketmine\network\mcpe\protocol\InventoryActionPacket;
@ -3313,6 +3314,10 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
return false; return false;
} }
public function handleGameRulesChanged(GameRulesChangedPacket $packet) : bool{
return false;
}
public function handleAddItem(AddItemPacket $packet) : bool{ public function handleAddItem(AddItemPacket $packet) : bool{
return false; return false;
} }

View File

@ -59,6 +59,7 @@ use pocketmine\network\mcpe\protocol\DropItemPacket;
use pocketmine\network\mcpe\protocol\EntityEventPacket; use pocketmine\network\mcpe\protocol\EntityEventPacket;
use pocketmine\network\mcpe\protocol\ExplodePacket; use pocketmine\network\mcpe\protocol\ExplodePacket;
use pocketmine\network\mcpe\protocol\FullChunkDataPacket; use pocketmine\network\mcpe\protocol\FullChunkDataPacket;
use pocketmine\network\mcpe\protocol\GameRulesChangedPacket;
use pocketmine\network\mcpe\protocol\HurtArmorPacket; use pocketmine\network\mcpe\protocol\HurtArmorPacket;
use pocketmine\network\mcpe\protocol\InteractPacket; use pocketmine\network\mcpe\protocol\InteractPacket;
use pocketmine\network\mcpe\protocol\InventoryActionPacket; use pocketmine\network\mcpe\protocol\InventoryActionPacket;
@ -313,6 +314,7 @@ class Network{
$this->registerPacket(ProtocolInfo::ENTITY_FALL_PACKET, EntityFallPacket::class); $this->registerPacket(ProtocolInfo::ENTITY_FALL_PACKET, EntityFallPacket::class);
$this->registerPacket(ProtocolInfo::EXPLODE_PACKET, ExplodePacket::class); $this->registerPacket(ProtocolInfo::EXPLODE_PACKET, ExplodePacket::class);
$this->registerPacket(ProtocolInfo::FULL_CHUNK_DATA_PACKET, FullChunkDataPacket::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::HURT_ARMOR_PACKET, HurtArmorPacket::class);
$this->registerPacket(ProtocolInfo::INTERACT_PACKET, InteractPacket::class); $this->registerPacket(ProtocolInfo::INTERACT_PACKET, InteractPacket::class);
$this->registerPacket(ProtocolInfo::INVENTORY_ACTION_PACKET, InventoryActionPacket::class); $this->registerPacket(ProtocolInfo::INVENTORY_ACTION_PACKET, InventoryActionPacket::class);

View File

@ -57,6 +57,7 @@ use pocketmine\network\mcpe\protocol\DropItemPacket;
use pocketmine\network\mcpe\protocol\EntityEventPacket; use pocketmine\network\mcpe\protocol\EntityEventPacket;
use pocketmine\network\mcpe\protocol\ExplodePacket; use pocketmine\network\mcpe\protocol\ExplodePacket;
use pocketmine\network\mcpe\protocol\FullChunkDataPacket; use pocketmine\network\mcpe\protocol\FullChunkDataPacket;
use pocketmine\network\mcpe\protocol\GameRulesChangedPacket;
use pocketmine\network\mcpe\protocol\HurtArmorPacket; use pocketmine\network\mcpe\protocol\HurtArmorPacket;
use pocketmine\network\mcpe\protocol\InteractPacket; use pocketmine\network\mcpe\protocol\InteractPacket;
use pocketmine\network\mcpe\protocol\InventoryActionPacket; use pocketmine\network\mcpe\protocol\InventoryActionPacket;
@ -263,7 +264,7 @@ interface NetworkSession{
public function handleReplaceItemInSlot(ReplaceItemInSlotPacket $packet) : bool; 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 :( //public function handleCamera(CameraPacket $packet) : bool; //edu only :(

View File

@ -363,4 +363,60 @@ abstract class DataPacket extends BinaryStream{
public function putByteRotation(float $rotation){ public function putByteRotation(float $rotation){
$this->putByte((int) ($rotation / (360 / 256))); $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;
}
}
}
} }

View File

@ -0,0 +1,48 @@
<?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\network\mcpe\protocol;
#include <rules/DataPacket.h>
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);
}
}

View File

@ -54,6 +54,7 @@ class StartGamePacket extends DataPacket{
public $lightningLevel; public $lightningLevel;
public $commandsEnabled; public $commandsEnabled;
public $isTexturePacksRequired = true; public $isTexturePacksRequired = true;
public $gameRules = []; //TODO: implement this
public $levelId = ""; //base64 string, usually the same as world folder name in vanilla public $levelId = ""; //base64 string, usually the same as world folder name in vanilla
public $worldName; public $worldName;
public $premiumWorldTemplateId = ""; public $premiumWorldTemplateId = "";
@ -80,7 +81,7 @@ class StartGamePacket extends DataPacket{
$this->lightningLevel = $this->getLFloat(); $this->lightningLevel = $this->getLFloat();
$this->commandsEnabled = $this->getBool(); $this->commandsEnabled = $this->getBool();
$this->isTexturePacksRequired = $this->getBool(); $this->isTexturePacksRequired = $this->getBool();
/*$gameRulesCount = */$this->getUnsignedVarInt(); //TODO $this->gameRules = $this->getGameRules();
$this->levelId = $this->getString(); $this->levelId = $this->getString();
$this->worldName = $this->getString(); $this->worldName = $this->getString();
$this->premiumWorldTemplateId = $this->getString(); $this->premiumWorldTemplateId = $this->getString();
@ -93,7 +94,7 @@ class StartGamePacket extends DataPacket{
$this->reset(); $this->reset();
$this->putEntityUniqueId($this->entityUniqueId); $this->putEntityUniqueId($this->entityUniqueId);
$this->putEntityRuntimeId($this->entityRuntimeId); $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->putVector3f($this->x, $this->y, $this->z);
$this->putLFloat($this->pitch); $this->putLFloat($this->pitch);
$this->putLFloat($this->yaw); $this->putLFloat($this->yaw);
@ -110,7 +111,7 @@ class StartGamePacket extends DataPacket{
$this->putLFloat($this->lightningLevel); $this->putLFloat($this->lightningLevel);
$this->putBool($this->commandsEnabled); $this->putBool($this->commandsEnabled);
$this->putBool($this->isTexturePacksRequired); $this->putBool($this->isTexturePacksRequired);
$this->putUnsignedVarInt(0); //TODO: gamerules $this->putGameRules($this->gameRules);
$this->putString($this->levelId); $this->putString($this->levelId);
$this->putString($this->worldName); $this->putString($this->worldName);
$this->putString($this->premiumWorldTemplateId); $this->putString($this->premiumWorldTemplateId);