From 287fa7e551aa45cea3ed60df6acf2cecc32f6c7b Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Mon, 4 Mar 2013 16:50:11 +0100 Subject: [PATCH] Updated the BlockAPI::drop method --- src/API/BlockAPI.php | 36 +++++++++++++++--------------------- src/Player.php | 2 +- src/config.php | 2 +- src/material/Block.php | 4 ++-- src/material/Item.php | 2 +- src/world/Entity.php | 9 +++++++-- 6 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index cde14b68c..05eff99ad 100644 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -127,15 +127,13 @@ class BlockAPI{ $item = BlockAPI::fromString($params[1]); if(!isset($params[2])){ - $amount = 64; + $item->count = $item->getMaxStackSize(); }else{ - $amount = (int) $params[2]; - } - if(isset($params[3])){ - $meta = ((int) $params[3]) & 0xFFFF; + $item->count = (int) $params[2]; } + if(($player = $this->server->api->player->get($username)) !== false){ - $this->drop($player->entity->x - 0.5, $player->entity->y, $player->entity->z - 0.5, $item->getID(), $item->getMetadata(), $amount); + $this->drop(new Vector3($player->entity->x - 0.5, $player->entity->y, $player->entity->z - 0.5), $item, true); $output .= "Giving ".$amount." of ".$item->getName()." (".$item->getID().":".$item->getMetadata().") to ".$username."\n"; }else{ $output .= "Unknown player\n"; @@ -177,32 +175,28 @@ class BlockAPI{ if(count($drops) > 0){ foreach($drops as $drop){ - $this->drop($target->x, $target->y, $target->z, $drop[0] & 0xFFFF, $drop[1] & 0xFFFF, $drop[2] & 0xFF); + $this->drop($target, BlockAPI::getItem($drop[0] & 0xFFFF, $drop[1] & 0xFFFF, $drop[2] & 0xFF)); } } return false; } - public function drop($x, $y, $z, $block, $meta, $stack = 1){ - if($block === AIR or $stack <= 0 or $this->server->gamemode === CREATIVE){ + public function drop(Vector3 $pos, Item $item, $force = false){ + if($item->getID() === AIR or $item->count <= 0 or ($this->server->gamemode === CREATIVE and $force !== true)){ return; } $data = array( - "x" => $x, - "y" => $y, - "z" => $z, - "meta" => $meta, - "stack" => $stack, + "x" => $pos->x + mt_rand(2, 8) / 10, + "y" => $pos->y + 0.19, + "z" => $pos->z + mt_rand(2, 8) / 10, + "item" => $item, ); - $data["x"] += mt_rand(2, 8) / 10; - $data["y"] += 0.19; - $data["z"] += mt_rand(2, 8) / 10; if($this->server->api->handle("block.drop", $data) !== false){ - for($count = $stack; $count > 0; ){ - $data["stack"] = min(64, $count); - $count -= $data["stack"]; + for($count = $item->count; $count > 0; ){ + $item->count = min($item->getMaxStackSize(), $count); + $count -= $item->count; $server = ServerAPI::request(); - $e = $server->api->entity->add(ENTITY_ITEM, $block, $data); + $e = $server->api->entity->add(ENTITY_ITEM, $item->getID(), $data); //$e->speedX = mt_rand(-10, 10) / 100; //$e->speedY = mt_rand(0, 5) / 100; //$e->speedZ = mt_rand(-10, 10) / 100; diff --git a/src/Player.php b/src/Player.php index 28f8328d5..351b3e119 100644 --- a/src/Player.php +++ b/src/Player.php @@ -747,7 +747,7 @@ class Player{ break; } if($this->server->handle("player.drop", $data) !== false){ - $this->server->api->block->drop($this->entity->x, $this->entity->y, $this->entity->z, $data["block"], $data["meta"], $data["stack"]); + $this->server->api->block->drop(new Vector3($this->entity->x, $this->entity->y, $this->entity->z), BlockAPI::getItem($data["block"], $data["meta"], $data["stack"])); } break; case MC_SIGN_UPDATE: diff --git a/src/config.php b/src/config.php index 421ad6b25..d61076a1b 100644 --- a/src/config.php +++ b/src/config.php @@ -42,5 +42,5 @@ define("MAJOR_VERSION", "Alpha_1.2dev"); define("CURRENT_STRUCTURE", 5); define("CURRENT_PROTOCOL", 9); define("CURRENT_MINECRAFT_VERSION", "v0.6.1 alpha"); -define("CURRENT_API_VERSION", 2); +define("CURRENT_API_VERSION", 3); define("CURRENT_PHP_VERSION", "5.4.12"); \ No newline at end of file diff --git a/src/material/Block.php b/src/material/Block.php index c51f66980..f6a6c903c 100644 --- a/src/material/Block.php +++ b/src/material/Block.php @@ -25,7 +25,7 @@ the Free Software Foundation, either version 3 of the License, or */ -abstract class Block{ +abstract class Block extends Vector3{ public static $class = array( AIR => "AirBlock", STONE => "StoneBlock", @@ -183,7 +183,7 @@ abstract class Block{ } final public function __toString(){ - return $this->name ." (".$this->id.":".$this->meta.")"; + return "Block ". $this->name ." (".$this->id.":".$this->meta.")"; } abstract function isBreakable(Item $item, Player $player); diff --git a/src/material/Item.php b/src/material/Item.php index 21f66c8be..fe2ec3f2d 100644 --- a/src/material/Item.php +++ b/src/material/Item.php @@ -128,7 +128,7 @@ class Item{ } final public function __toString(){ - return $this->name ." (".$this->id.":".$this->meta.")"; + return "Item ". $this->name ." (".$this->id.":".$this->meta.")"; } public function getDestroySpeed(Block $block, Player $player){ diff --git a/src/world/Entity.php b/src/world/Entity.php index 0ee15b020..eed1d8bbb 100644 --- a/src/world/Entity.php +++ b/src/world/Entity.php @@ -114,8 +114,13 @@ class Entity extends stdClass{ $this->server->schedule(5, array($this, "update"), array(), true); break; case ENTITY_ITEM: - $this->meta = (int) $this->data["meta"]; - $this->stack = (int) $this->data["stack"]; + if($data["item"] instanceof Item){ + $this->meta = $this->data["item"]->getMetadata(); + $this->stack = $this->data["item"]->count; + }else{ + $this->meta = (int) $this->data["meta"]; + $this->stack = (int) $this->data["stack"]; + } $this->setHealth(5, "generic"); $this->server->schedule(5, array($this, "update"), array(), true); break;