From add380c7ed89064e658bae494e1b6d88c393d075 Mon Sep 17 00:00:00 2001
From: Shoghi Cervantes <shoghicp@gmail.com>
Date: Sat, 21 Mar 2015 00:45:20 +0100
Subject: [PATCH] A few fixes!

---
 src/pocketmine/Player.php                     |  7 +++-
 src/pocketmine/Server.php                     | 34 +++++++++++++++----
 src/pocketmine/entity/Entity.php              | 11 ++----
 src/pocketmine/inventory/PlayerInventory.php  |  2 +-
 src/pocketmine/level/Level.php                |  2 ++
 src/pocketmine/level/generator/Flat.php       | 11 ++++--
 .../level/particle/FloatingTextParticle.php   |  4 +--
 .../level/particle/TerrainParticle.php        |  2 +-
 src/pocketmine/math/Vector3.php               |  6 +++-
 src/pocketmine/utils/Config.php               |  1 -
 10 files changed, 55 insertions(+), 25 deletions(-)

diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php
index 5f2ea1e18..8eb32c06c 100644
--- a/src/pocketmine/Player.php
+++ b/src/pocketmine/Player.php
@@ -1802,6 +1802,11 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
 					case 0: //Start break
 						$target = $this->level->getBlock(new Vector3($packet->x, $packet->y, $packet->z));
 						$ev = new PlayerInteractEvent($this, $this->inventory->getItemInHand(), $target, $packet->face, $target->getId() === 0 ? PlayerInteractEvent::LEFT_CLICK_AIR : PlayerInteractEvent::LEFT_CLICK_BLOCK);
+						$this->getServer()->getPluginManager()->callEvent($ev);
+						if($ev->isCancelled()){
+							$this->inventory->sendHeldItem($this);
+							break;
+						}
 						$this->lastBreak = microtime(true);
 						break;
 					case 5: //Shot arrow
@@ -2066,7 +2071,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
 
 				$this->teleport($ev->getRespawnPosition());
 
-				$this->fireTicks = 0;
+				$this->extinguish();
 				$this->setDataProperty(self::DATA_AIR, self::DATA_TYPE_SHORT, 300);
 				$this->deadTicks = 0;
 				$this->noDamageTicks = 60;
diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php
index 69cfdc77d..e2c037ac0 100644
--- a/src/pocketmine/Server.php
+++ b/src/pocketmine/Server.php
@@ -315,7 +315,7 @@ class Server{
 	}
 
 	/**
-	 * @return string
+	 * @deprecated
 	 */
 	public function getServerName(){
 		return $this->getConfigString("motd", "Minecraft: PE Server");
@@ -2211,14 +2211,36 @@ class Server{
 			" | Load " . $this->getTickUsage() . "%\x07";
 	}
 	
-	public function getMemoryUsage(){
+	public function getMemoryUsage($advanced = false){
+		$VmSize = null;
+		$VmHWM = null;
 		if(Utils::getOS() === "linux" or Utils::getOS() === "bsd"){
-			if(preg_match("/VmSize:[ \t]+([0-9]+) kB/", file_get_contents("/proc/self/status"), $matches) > 0){
-				return $matches[1] * 1024;
+			$status = file_get_contents("/proc/self/status");
+			if(preg_match("/VmHWM:[ \t]+([0-9]+) kB/", $status, $matches) > 0){
+				$VmHWM = $matches[1] * 1024;
+			}
+
+			if(preg_match("/VmData:[ \t]+([0-9]+) kB/", $status, $matches) > 0){
+				$VmData = $matches[1] * 1024;
 			}
 		}
-		
-		return memory_get_usage(true);
+
+		if($VmHWM === null){
+			$VmHWM = memory_get_usage();
+		}
+
+		if(!$advanced){
+			return $VmHWM;
+		}
+
+		if($VmSize === null){
+			$VmSize = memory_get_usage(true);
+		}
+
+		return [
+			"hardware" => $VmHWM,
+			"virtual" => $VmSize,
+		];
 	}
 	
 	public function getThreadCount(){
diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php
index 31470468d..f9f5583f7 100644
--- a/src/pocketmine/entity/Entity.php
+++ b/src/pocketmine/entity/Entity.php
@@ -736,6 +736,7 @@ abstract class Entity extends Location implements Metadatable{
 			if($this->fireTicks <= 0){
 				$this->extinguish();
 			}else{
+				$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ONFIRE, true);
 				$hasUpdate = true;
 			}
 		}
@@ -844,11 +845,6 @@ abstract class Entity extends Location implements Metadatable{
 		if($ticks > $this->fireTicks){
 			$this->fireTicks = $ticks;
 		}
-
-		$this->sendMetadata($this->hasSpawned);
-		if($this instanceof Player){
-			$this->sendMetadata($this);
-		}
 	}
 
 	public function getDirection(){
@@ -871,10 +867,7 @@ abstract class Entity extends Location implements Metadatable{
 
 	public function extinguish(){
 		$this->fireTicks = 0;
-		$this->sendMetadata($this->hasSpawned);
-		if($this instanceof Player){
-			$this->sendMetadata($this);
-		}
+		$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ONFIRE, false);
 	}
 
 	public function canTriggerWalking(){
diff --git a/src/pocketmine/inventory/PlayerInventory.php b/src/pocketmine/inventory/PlayerInventory.php
index ee5af4b8d..805c95c74 100644
--- a/src/pocketmine/inventory/PlayerInventory.php
+++ b/src/pocketmine/inventory/PlayerInventory.php
@@ -146,7 +146,7 @@ class PlayerInventory extends BaseInventory{
 
 		foreach($target as $player){
 			if($player === $this->getHolder()){
-				$this->sendSlot($this->getHeldItemSlot());
+				$this->sendSlot($this->getHeldItemSlot(), $player);
 			}else{
 				$player->dataPacket($pk);
 			}
diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php
index 368aae246..677dfba86 100644
--- a/src/pocketmine/level/Level.php
+++ b/src/pocketmine/level/Level.php
@@ -1404,6 +1404,8 @@ class Level implements ChunkManager, Metadatable{
 						return true;
 					}
 				}
+			}else{
+				return false;
 			}
 		}elseif($target->canBeActivated() === true and $target->onActivate($item, $player) === true){
 			return true;
diff --git a/src/pocketmine/level/generator/Flat.php b/src/pocketmine/level/generator/Flat.php
index 8b8eed68b..82ce07fba 100644
--- a/src/pocketmine/level/generator/Flat.php
+++ b/src/pocketmine/level/generator/Flat.php
@@ -88,12 +88,12 @@ class Flat extends Generator{
 		$blocks = isset($preset[1]) ? $preset[1] : "";
 		$biome = isset($preset[2]) ? $preset[2] : 1;
 		$options = isset($preset[3]) ? $preset[3] : "";
-		preg_match_all('#(([0-9]{0,})x?([0-9]{1,3}:?[0-9]{0,2})),?#', $blocks, $matches);
+		preg_match_all('#^(([0-9]*x|)([0-9]{1,3})(|:[0-9]{0,2}))$#m', str_replace(",", "\n", $blocks), $matches);
 		$y = 0;
 		$this->structure = [];
 		$this->chunks = [];
 		foreach($matches[3] as $i => $b){
-			$b = Item::fromString($b);
+			$b = Item::fromString($b . $matches[4][$i]);
 			$cnt = $matches[2][$i] === "" ? 1 : intval($matches[2][$i]);
 			for($cY = $y, $y += $cnt; $cY < $y; ++$cY){
 				$this->structure[$cY] = [$b->getId(), $b->getDamage()];
@@ -107,11 +107,16 @@ class Flat extends Generator{
 		}
 
 
-		$this->chunk = $this->level->getChunk(0, 0);
+		$chunk = $this->level->getChunk(0, 0);
+		if($chunk === null){
+			return;
+		}
+		$this->chunk = clone $chunk;
 		$this->chunk->setGenerated();
 
 		for($Z = 0; $Z < 16; ++$Z){
 			for($X = 0; $X < 16; ++$X){
+				$this->chunk->setBiomeId($X, $Z, $biome);
 				for($y = 0; $y < 128; ++$y){
 					$this->chunk->setBlock($X, $y, $Z, ...$this->structure[$y]);
 				}
diff --git a/src/pocketmine/level/particle/FloatingTextParticle.php b/src/pocketmine/level/particle/FloatingTextParticle.php
index 797d98278..671b15c6d 100644
--- a/src/pocketmine/level/particle/FloatingTextParticle.php
+++ b/src/pocketmine/level/particle/FloatingTextParticle.php
@@ -79,10 +79,10 @@ class FloatingTextParticle extends Particle{
 			
 			$pk = new AddPlayerPacket();
 			$pk->eid = $this->entityId;
-			$pk->username = $this->title . "\n" . $this->text;
+			$pk->username = $this->title . ($this->text !== "" ? "\n" . $this->text : "");
 			$pk->clientID = $this->entityId;
 			$pk->x = $this->x;
-			$pk->y = $this->y - 2;
+			$pk->y = $this->y - 2.5;
 			$pk->z = $this->z;
 			$pk->yaw = 0;
 			$pk->pitch = 0;
diff --git a/src/pocketmine/level/particle/TerrainParticle.php b/src/pocketmine/level/particle/TerrainParticle.php
index a74137dd9..abe8f1105 100644
--- a/src/pocketmine/level/particle/TerrainParticle.php
+++ b/src/pocketmine/level/particle/TerrainParticle.php
@@ -26,6 +26,6 @@ use pocketmine\math\Vector3;
 
 class TerrainParticle extends GenericParticle{
 	public function __construct(Vector3 $pos, Block $b){
-		parent::__construct($pos, 15, ($b->getId() << 8) | $b->getDamage());
+		parent::__construct($pos, 15, ($b->getDamage() << 8) | $b->getId());
 	}
 }
diff --git a/src/pocketmine/math/Vector3.php b/src/pocketmine/math/Vector3.php
index 2d62334ac..4395aa4e3 100644
--- a/src/pocketmine/math/Vector3.php
+++ b/src/pocketmine/math/Vector3.php
@@ -129,7 +129,7 @@ class Vector3{
 	public function floor(){
 		$x = (int) $this->x;
 		$z = (int) $this->z;
-		return new Vector3($this->x >= $x ? $x : $x - 1, round($this->y), $this->z >= $z ? $z : $z - 1);
+		return new Vector3($this->x >= $x ? $x : $x - 1, (int) round($this->y), $this->z >= $z ? $z : $z - 1);
 	}
 
 	public function round(){
@@ -228,6 +228,10 @@ class Vector3{
 		);
 	}
 
+	public function equals(Vector3 $v){
+		return $this->x == $v->x and $this->y == $v->y and $this->z == $v->z;
+	}
+
 	/**
 	 * Returns a new vector with x value equal to the second parameter, along the line between this vector and the
 	 * passed in vector, or null if not possible.
diff --git a/src/pocketmine/utils/Config.php b/src/pocketmine/utils/Config.php
index 56aa56f2a..2541ca1d6 100644
--- a/src/pocketmine/utils/Config.php
+++ b/src/pocketmine/utils/Config.php
@@ -83,7 +83,6 @@ class Config{
 	public function reload(){
 		$this->config = [];
 		$this->correct = false;
-		unset($this->type);
 		$this->load($this->file);
 	}