Updated the BlockAPI::drop method

This commit is contained in:
Shoghi Cervantes Pueyo 2013-03-04 16:50:11 +01:00
parent 326864f889
commit 287fa7e551
6 changed files with 27 additions and 28 deletions

View File

@ -127,15 +127,13 @@ class BlockAPI{
$item = BlockAPI::fromString($params[1]); $item = BlockAPI::fromString($params[1]);
if(!isset($params[2])){ if(!isset($params[2])){
$amount = 64; $item->count = $item->getMaxStackSize();
}else{ }else{
$amount = (int) $params[2]; $item->count = (int) $params[2];
}
if(isset($params[3])){
$meta = ((int) $params[3]) & 0xFFFF;
} }
if(($player = $this->server->api->player->get($username)) !== false){ 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"; $output .= "Giving ".$amount." of ".$item->getName()." (".$item->getID().":".$item->getMetadata().") to ".$username."\n";
}else{ }else{
$output .= "Unknown player\n"; $output .= "Unknown player\n";
@ -177,32 +175,28 @@ class BlockAPI{
if(count($drops) > 0){ if(count($drops) > 0){
foreach($drops as $drop){ 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; return false;
} }
public function drop($x, $y, $z, $block, $meta, $stack = 1){ public function drop(Vector3 $pos, Item $item, $force = false){
if($block === AIR or $stack <= 0 or $this->server->gamemode === CREATIVE){ if($item->getID() === AIR or $item->count <= 0 or ($this->server->gamemode === CREATIVE and $force !== true)){
return; return;
} }
$data = array( $data = array(
"x" => $x, "x" => $pos->x + mt_rand(2, 8) / 10,
"y" => $y, "y" => $pos->y + 0.19,
"z" => $z, "z" => $pos->z + mt_rand(2, 8) / 10,
"meta" => $meta, "item" => $item,
"stack" => $stack,
); );
$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){ if($this->server->api->handle("block.drop", $data) !== false){
for($count = $stack; $count > 0; ){ for($count = $item->count; $count > 0; ){
$data["stack"] = min(64, $count); $item->count = min($item->getMaxStackSize(), $count);
$count -= $data["stack"]; $count -= $item->count;
$server = ServerAPI::request(); $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->speedX = mt_rand(-10, 10) / 100;
//$e->speedY = mt_rand(0, 5) / 100; //$e->speedY = mt_rand(0, 5) / 100;
//$e->speedZ = mt_rand(-10, 10) / 100; //$e->speedZ = mt_rand(-10, 10) / 100;

View File

@ -747,7 +747,7 @@ class Player{
break; break;
} }
if($this->server->handle("player.drop", $data) !== false){ 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; break;
case MC_SIGN_UPDATE: case MC_SIGN_UPDATE:

View File

@ -42,5 +42,5 @@ define("MAJOR_VERSION", "Alpha_1.2dev");
define("CURRENT_STRUCTURE", 5); define("CURRENT_STRUCTURE", 5);
define("CURRENT_PROTOCOL", 9); define("CURRENT_PROTOCOL", 9);
define("CURRENT_MINECRAFT_VERSION", "v0.6.1 alpha"); 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"); define("CURRENT_PHP_VERSION", "5.4.12");

View File

@ -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( public static $class = array(
AIR => "AirBlock", AIR => "AirBlock",
STONE => "StoneBlock", STONE => "StoneBlock",
@ -183,7 +183,7 @@ abstract class Block{
} }
final public function __toString(){ 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); abstract function isBreakable(Item $item, Player $player);

View File

@ -128,7 +128,7 @@ class Item{
} }
final public function __toString(){ 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){ public function getDestroySpeed(Block $block, Player $player){

View File

@ -114,8 +114,13 @@ class Entity extends stdClass{
$this->server->schedule(5, array($this, "update"), array(), true); $this->server->schedule(5, array($this, "update"), array(), true);
break; break;
case ENTITY_ITEM: case ENTITY_ITEM:
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->meta = (int) $this->data["meta"];
$this->stack = (int) $this->data["stack"]; $this->stack = (int) $this->data["stack"];
}
$this->setHealth(5, "generic"); $this->setHealth(5, "generic");
$this->server->schedule(5, array($this, "update"), array(), true); $this->server->schedule(5, array($this, "update"), array(), true);
break; break;