Added Tile::saveNBT(), moved Player::broadcastPacket() to Server

This commit is contained in:
Shoghi Cervantes 2014-05-24 13:21:55 +02:00
parent 1fd7e0431b
commit ef6ca9d2cd
11 changed files with 57 additions and 25 deletions

View File

@ -2295,18 +2295,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
return $this->username; return $this->username;
} }
/**
* Broadcasts a Minecraft packet to a list of players
*
* @param Player[] $players
* @param DataPacket $packet
*/
public static function broadcastPacket(array $players, DataPacket $packet){
foreach($players as $player){
$player->dataPacket(clone $packet);
}
}
/** /**
* @param Item[] $craft * @param Item[] $craft
* @param Item[] $recipe * @param Item[] $recipe

View File

@ -61,6 +61,7 @@ use pocketmine\nbt\tag\Long;
use pocketmine\nbt\tag\Short; use pocketmine\nbt\tag\Short;
use pocketmine\nbt\tag\String; use pocketmine\nbt\tag\String;
use pocketmine\network\Packet; use pocketmine\network\Packet;
use pocketmine\network\protocol\DataPacket;
use pocketmine\network\query\QueryHandler; use pocketmine\network\query\QueryHandler;
use pocketmine\network\query\QueryPacket; use pocketmine\network\query\QueryPacket;
use pocketmine\network\raknet\Info as RakNetInfo; use pocketmine\network\raknet\Info as RakNetInfo;
@ -1382,6 +1383,18 @@ class Server{
return $count; return $count;
} }
/**
* Broadcasts a Minecraft packet to a list of players
*
* @param Player[] $players
* @param DataPacket $packet
*/
public function broadcastPacket(array $players, DataPacket $packet){
foreach($players as $player){
$player->dataPacket(clone $packet);
}
}
/** /**
* @param int $type * @param int $type

View File

@ -25,6 +25,7 @@ use pocketmine\item\Item;
use pocketmine\level\Level; use pocketmine\level\Level;
use pocketmine\network\protocol\LevelEventPacket; use pocketmine\network\protocol\LevelEventPacket;
use pocketmine\Player; use pocketmine\Player;
use pocketmine\Server;
abstract class Door extends Transparent{ abstract class Door extends Transparent{
@ -111,7 +112,7 @@ abstract class Door extends Transparent{
$pk->z = $this->z; $pk->z = $this->z;
$pk->evid = 1003; $pk->evid = 1003;
$pk->data = 0; $pk->data = 0;
Player::broadcastPacket($players, $pk); Server::getInstance()->broadcastPacket($players, $pk);
return true; return true;
} }
@ -130,7 +131,7 @@ abstract class Door extends Transparent{
$pk->z = $this->z; $pk->z = $this->z;
$pk->evid = 1003; $pk->evid = 1003;
$pk->data = 0; $pk->data = 0;
Player::broadcastPacket($players, $pk); Server::getInstance()->broadcastPacket($players, $pk);
} }
return true; return true;

View File

@ -303,7 +303,7 @@ abstract class Entity extends Position implements Metadatable{
$pk->yaw = $this->yaw; $pk->yaw = $this->yaw;
$pk->pitch = $this->pitch; $pk->pitch = $this->pitch;
} }
Player::broadcastPacket($this->hasSpawned, $pk); $this->server->broadcastPacket($this->hasSpawned, $pk);
} }
if($this->motionChanged === true){ if($this->motionChanged === true){
@ -314,7 +314,7 @@ abstract class Entity extends Position implements Metadatable{
$pk->speedX = $this->motionX; $pk->speedX = $this->motionX;
$pk->speedY = $this->motionY; $pk->speedY = $this->motionY;
$pk->speedZ = $this->motionZ; $pk->speedZ = $this->motionZ;
Player::broadcastPacket($this->hasSpawned, $pk); $this->server->broadcastPacket($this->hasSpawned, $pk);
} }
$this->lastUpdate = $timeNow; $this->lastUpdate = $timeNow;

View File

@ -23,6 +23,7 @@ namespace pocketmine\inventory;
use pocketmine\network\protocol\TileEventPacket; use pocketmine\network\protocol\TileEventPacket;
use pocketmine\Player; use pocketmine\Player;
use pocketmine\Server;
use pocketmine\tile\Chest; use pocketmine\tile\Chest;
class ChestInventory extends ContainerInventory{ class ChestInventory extends ContainerInventory{
@ -47,7 +48,7 @@ class ChestInventory extends ContainerInventory{
$pk->z = $this->getHolder()->getZ(); $pk->z = $this->getHolder()->getZ();
$pk->case1 = 1; $pk->case1 = 1;
$pk->case2 = 2; $pk->case2 = 2;
Player::broadcastPacket($this->getHolder()->getLevel()->getPlayers(), $pk); Server::getInstance()->broadcastPacket($this->getHolder()->getLevel()->getPlayers(), $pk);
} }
} }
@ -61,7 +62,7 @@ class ChestInventory extends ContainerInventory{
$pk->z = $this->getHolder()->getZ(); $pk->z = $this->getHolder()->getZ();
$pk->case1 = 1; $pk->case1 = 1;
$pk->case2 = 0; $pk->case2 = 0;
Player::broadcastPacket($this->getHolder()->getLevel()->getPlayers(), $pk); Server::getInstance()->broadcastPacket($this->getHolder()->getLevel()->getPlayers(), $pk);
} }
} }
} }

View File

@ -148,7 +148,7 @@ class Explosion{
$pk->z = $this->source->z; $pk->z = $this->source->z;
$pk->radius = $this->size; $pk->radius = $this->size;
$pk->records = $send; $pk->records = $send;
Player::broadcastPacket($this->level->getPlayers(), $pk); Server::getInstance()->broadcastPacket($this->level->getPlayers(), $pk);
} }
} }

View File

@ -254,7 +254,7 @@ class Level{
$pk = new SetTimePacket; $pk = new SetTimePacket;
$pk->time = (int) $this->time; $pk->time = (int) $this->time;
$pk->started = $this->stopTime == false; $pk->started = $this->stopTime == false;
Player::broadcastPacket($this->players, $pk); $this->server->broadcastPacket($this->players, $pk);
return; return;
} }
@ -304,7 +304,7 @@ class Level{
$pk->z = $b->z; $pk->z = $b->z;
$pk->block = $b->getID(); $pk->block = $b->getID();
$pk->meta = $b->getDamage(); $pk->meta = $b->getDamage();
Player::broadcastPacket($this->players, $pk); $this->server->broadcastPacket($this->players, $pk);
} }
} }
} }
@ -437,6 +437,7 @@ class Level{
$i = 0; $i = 0;
foreach($this->chunkEntities[$index] as $entity){ foreach($this->chunkEntities[$index] as $entity){
/** @var Entity $entity */
if($entity->closed !== true){ if($entity->closed !== true){
$entity->saveNBT(); $entity->saveNBT();
$nbt->Entities[$i] = $entity->namedtag; $nbt->Entities[$i] = $entity->namedtag;
@ -446,7 +447,9 @@ class Level{
$i = 0; $i = 0;
foreach($this->chunkTiles[$index] as $tile){ foreach($this->chunkTiles[$index] as $tile){
/** @var Tile $tile */
if($tile->closed !== true){ if($tile->closed !== true){
$tile->saveNBT();
$nbt->TileEntities[$i] = $tile->namedtag; $nbt->TileEntities[$i] = $tile->namedtag;
++$i; ++$i;
} }
@ -517,7 +520,7 @@ class Level{
$pk->z = $pos->z; $pk->z = $pos->z;
$pk->block = $block->getID(); $pk->block = $block->getID();
$pk->meta = $block->getDamage(); $pk->meta = $block->getDamage();
Player::broadcastPacket($this->players, $pk); $this->server->broadcastPacket($this->players, $pk);
}elseif($direct === false){ }elseif($direct === false){
if(!($pos instanceof Position)){ if(!($pos instanceof Position)){
$pos = new Position($pos->x, $pos->y, $pos->z, $this); $pos = new Position($pos->x, $pos->y, $pos->z, $this);
@ -571,7 +574,7 @@ class Level{
$pk->z = $pos->z; $pk->z = $pos->z;
$pk->block = $block->getID(); $pk->block = $block->getID();
$pk->meta = $block->getDamage(); $pk->meta = $block->getDamage();
Player::broadcastPacket($this->players, $pk); $this->server->broadcastPacket($this->players, $pk);
}else{ }else{
$index = LevelFormat::getIndex($pos->x >> 4, $pos->z >> 4); $index = LevelFormat::getIndex($pos->x >> 4, $pos->z >> 4);
if(ADVANCED_CACHE == true){ if(ADVANCED_CACHE == true){
@ -1037,7 +1040,7 @@ class Level{
$ordered .= substr($mini, $j << 5, 24); //16 + 8 $ordered .= substr($mini, $j << 5, 24); //16 + 8
} }
} }
if(ADVANCED_CACHE == true and $Yndex == 0xff){ if(ADVANCED_CACHE == true and $Yndex === 0xff){
Cache::add($identifier, $ordered, 60); Cache::add($identifier, $ordered, 60);
} }

View File

@ -156,6 +156,7 @@ class Enum extends NamedTag implements \ArrayAccess{
$nbt->putByte($this->tagType); $nbt->putByte($this->tagType);
/** @var Tag[] $tags */
$tags = []; $tags = [];
foreach($this as $tag){ foreach($this as $tag){
if($tag instanceof Tag){ if($tag instanceof Tag){

View File

@ -30,6 +30,7 @@ use pocketmine\math\Vector3 as Vector3;
use pocketmine\nbt\NBT; use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\Byte; use pocketmine\nbt\tag\Byte;
use pocketmine\nbt\tag\Compound; use pocketmine\nbt\tag\Compound;
use pocketmine\nbt\tag\Enum;
use pocketmine\nbt\tag\Int; use pocketmine\nbt\tag\Int;
use pocketmine\nbt\tag\Short; use pocketmine\nbt\tag\Short;
use pocketmine\nbt\tag\String; use pocketmine\nbt\tag\String;
@ -53,6 +54,14 @@ class Chest extends Spawnable implements InventoryHolder, Container{
$this->checkPairing(); $this->checkPairing();
} }
public function saveNBT(){
$this->namedtag->Inventory = new Enum("Inventory", []);
$this->namedtag->Inventory->setTagType(NBT::TAG_Compound);
for($index = 0; $index < $this->getSize(); ++$index){
$this->setItem($index, $this->inventory->getItem($index));
}
}
/** /**
* @return int * @return int
*/ */
@ -67,7 +76,7 @@ class Chest extends Spawnable implements InventoryHolder, Container{
*/ */
protected function getSlotIndex($index){ protected function getSlotIndex($index){
foreach($this->namedtag->Items as $i => $slot){ foreach($this->namedtag->Items as $i => $slot){
if($slot["Slot"] === $s){ if($slot["Slot"] === $index){
return $i; return $i;
} }
} }

View File

@ -26,8 +26,10 @@ use pocketmine\inventory\FurnaceInventory;
use pocketmine\inventory\InventoryHolder; use pocketmine\inventory\InventoryHolder;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\level\Level; use pocketmine\level\Level;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\Byte; use pocketmine\nbt\tag\Byte;
use pocketmine\nbt\tag\Compound; use pocketmine\nbt\tag\Compound;
use pocketmine\nbt\tag\Enum;
use pocketmine\nbt\tag\Short; use pocketmine\nbt\tag\Short;
class Furnace extends Tile implements InventoryHolder, Container{ class Furnace extends Tile implements InventoryHolder, Container{
@ -57,6 +59,14 @@ class Furnace extends Tile implements InventoryHolder, Container{
} }
} }
public function saveNBT(){
$this->namedtag->Inventory = new Enum("Inventory", []);
$this->namedtag->Inventory->setTagType(NBT::TAG_Compound);
for($index = 0; $index < $this->getSize(); ++$index){
$this->setItem($index, $this->inventory->getItem($index));
}
}
/** /**
* @return int * @return int
*/ */

View File

@ -95,6 +95,12 @@ abstract class Tile extends Position{
$this->getLevel()->chunkTiles[$this->chunkIndex][$this->id] = $this; $this->getLevel()->chunkTiles[$this->chunkIndex][$this->id] = $this;
} }
public function saveNBT(){
$this->namedtag["x"] = $this->x;
$this->namedtag["y"] = $this->y;
$this->namedtag["z"] = $this->z;
}
public function onUpdate(){ public function onUpdate(){
return false; return false;
} }