From 96445c46135c7c90d2849b2393507bdd7d6002c3 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Thu, 29 May 2014 18:45:49 +0200 Subject: [PATCH] Fixed item in hand in creative mode, player files are now readable by Minecraft PC --- src/pocketmine/Player.php | 25 +++++++++++----------- src/pocketmine/Server.php | 1 + src/pocketmine/entity/DroppedItem.php | 6 +----- src/pocketmine/entity/Entity.php | 8 +------ src/pocketmine/nbt/NBT.php | 10 ++++----- src/pocketmine/utils/Binary.php | 30 ++++++++++++++------------- 6 files changed, 36 insertions(+), 44 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index cee4d3d8d..32cf836da 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -1066,6 +1066,15 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ return $this->lagStat * 1000; } + protected function getCreativeBlock(Item $item){ + foreach(Block::$creative as $i => $d){ + if($d[0] === $item->getID() and $d[1] === $item->getDamage()){ + return $i; + } + } + return -1; + } + /** * WARNING: Experimental method * @@ -1243,8 +1252,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } } } - - $this->updateMovement(); } /** @@ -1399,6 +1406,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $nbt["lastPlayed"] = floor(microtime(true) * 1000); $this->server->saveOfflinePlayerData($this->username, $nbt); parent::__construct($this->getLevel(), $nbt); + $this->inventory->setHeldItemSlot($this->getCreativeBlock(Item::get(Item::STONE, 0, 1))); $this->loggedIn = true; if(($this->gamemode & 0x01) === 0x01){ @@ -1411,7 +1419,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $this->server->getPluginManager()->callEvent($ev = new PlayerLoginEvent($this, "Plugin reason")); if($ev->isCancelled()){ $this->close($ev->getKickMessage(), "Plugin reason"); - return; } @@ -1537,19 +1544,13 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } if(($this->gamemode & 0x01) === 1){ //Creative mode match - $packet->slot = false; - foreach(Block::$creative as $i => $d){ - if($d[0] === $packet->item and $d[1] === $packet->meta){ - $packet->slot = $i; - $item = Item::get($d[0], $d[1], 1); - break; - } - } + $item = Item::get($packet->item, $packet->meta, 1); + $packet->slot = $this->getCreativeBlock($item); }else{ $item = $this->inventory->getItem($packet->slot); } - if(!isset($item) or $packet->slot === false){ + if(!isset($item) or $packet->slot === -1){ $this->inventory->sendSlot($packet->slot, $this); }else{ $this->inventory->setHeldItemSlot($packet->slot); diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index 8b7c9ead2..2be1c7606 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -674,6 +674,7 @@ class Server{ $nbt = new NBT(NBT::BIG_ENDIAN); $nbt->setData($nbtTag); file_put_contents($this->getDataPath() . "players/" . strtolower($name) . ".dat", $nbt->writeCompressed()); + file_put_contents($this->getDataPath() . "players/" . strtolower($name) . ".raw.dat", $nbt->write()); } /** diff --git a/src/pocketmine/entity/DroppedItem.php b/src/pocketmine/entity/DroppedItem.php index 101346f26..ff513d106 100644 --- a/src/pocketmine/entity/DroppedItem.php +++ b/src/pocketmine/entity/DroppedItem.php @@ -135,10 +135,7 @@ class DroppedItem extends Entity{ $flags |= $this->fireTicks > 0 ? 1 : 0; return [ - 0 => array("type" => 0, "value" => $flags), - 1 => array("type" => 1, "value" => 0), - 16 => array("type" => 0, "value" => 0), - 17 => array("type" => 6, "value" => array(0, 0, 0)), + 0 => array("type" => 0, "value" => $flags) ]; } @@ -205,7 +202,6 @@ class DroppedItem extends Entity{ $pk->pitch = $this->pitch; $pk->roll = 0; $pk->item = $this->getItem(); - $pk->metadata = $this->getData(); $player->dataPacket($pk); $pk = new SetEntityMotionPacket; diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 6c8d4799c..e40abd5f0 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -376,12 +376,7 @@ abstract class Entity extends Position implements Metadatable{ } $hasUpdate = false; - $this->lastX = $this->x; - $this->lastY = $this->y; - $this->lastZ = $this->z; - $this->lastMotionX = $this->motionX; - $this->lastPitch = $this->pitch; - $this->lastYaw = $this->yaw; + $this->updateMovement(); if($this->handleWaterMovement()){ $this->fallDistance = 0; @@ -469,7 +464,6 @@ abstract class Entity extends Position implements Metadatable{ } $hasUpdate = $this->entityBaseTick(); - $this->updateMovement(); //if($this->isStatic()) return true; diff --git a/src/pocketmine/nbt/NBT.php b/src/pocketmine/nbt/NBT.php index 612a98f32..042a6e2fd 100644 --- a/src/pocketmine/nbt/NBT.php +++ b/src/pocketmine/nbt/NBT.php @@ -44,8 +44,6 @@ use pocketmine\utils\Binary; * Named Binary Tag encoder/decoder */ class NBT{ - const COMPRESSION_GZIP = 1; - const COMPRESSION_ZLIB = 2; const LITTLE_ENDIAN = 0; const BIG_ENDIAN = 1; @@ -106,8 +104,8 @@ class NBT{ $this->buffer = ""; } - public function readCompressed($buffer, $compression = self::COMPRESSION_ZLIB){ - $this->read($compression === self::COMPRESSION_ZLIB ? zlib_decode($buffer) : zlib_decode($buffer)); + public function readCompressed($buffer, $compression = ZLIB_ENCODING_GZIP){ + $this->read(zlib_decode($buffer)); } public function write(){ @@ -121,9 +119,9 @@ class NBT{ } } - public function writeCompressed($compression = self::COMPRESSION_ZLIB, $level = 7){ + public function writeCompressed($compression = ZLIB_ENCODING_GZIP, $level = 7){ if(($write = $this->write()) !== false){ - return $compression === self::COMPRESSION_ZLIB ? zlib_encode($write, 15, $level) : zlib_encode($write, 31, $level); + return zlib_encode($write, ZLIB_ENCODING_GZIP, $level); } return false; diff --git a/src/pocketmine/utils/Binary.php b/src/pocketmine/utils/Binary.php index 38a15bbf6..944945d1a 100644 --- a/src/pocketmine/utils/Binary.php +++ b/src/pocketmine/utils/Binary.php @@ -68,32 +68,32 @@ class Binary{ public static function writeMetadata(array $data){ $m = ""; foreach($data as $bottom => $d){ - $m .= chr(($d[0] << 5) | ($bottom & 0x1F)); - switch($d[0]){ + $m .= chr(($d["type"] << 5) | ($bottom & 0x1F)); + switch($d["type"]){ case 0: - $m .= self::writeByte($d[1]); + $m .= self::writeByte($d["value"]); break; case 1: - $m .= self::writeLShort($d[1]); + $m .= self::writeLShort($d["value"]); break; case 2: - $m .= self::writeLInt($d[1]); + $m .= self::writeLInt($d["value"]); break; case 3: - $m .= self::writeLFloat($d[1]); + $m .= self::writeLFloat($d["value"]); break; case 4: - $m .= self::writeLShort(strlen($d[1])) . $d[1]; + $m .= self::writeLShort(strlen($d["value"])) . $d["value"]; break; case 5: - $m .= self::writeLShort($d[1][0]); - $m .= self::writeByte($d[1][1]); - $m .= self::writeLShort($d[1][2]); + $m .= self::writeLShort($d["value"][0]); + $m .= self::writeByte($d["value"][1]); + $m .= self::writeLShort($d["value"][2]); break; case 6: - $m .= self::writeLInt($d[1][0]); - $m .= self::writeLInt($d[1][1]); - $m .= self::writeLInt($d[1][2]); + $m .= self::writeLInt($d["value"][0]); + $m .= self::writeLInt($d["value"][1]); + $m .= self::writeLInt($d["value"][2]); break; } } @@ -158,10 +158,12 @@ class Binary{ $offset += 4; } break; + default: + return []; } if($types === true){ - $m[$bottom] = array($r, $type); + $m[$bottom] = array("value" => $r, "type" => $type); }else{ $m[$bottom] = $r; }