Place/Breaking/Activation logic separated from BlockAPI

This commit is contained in:
Shoghi Cervantes Pueyo 2013-02-03 18:55:59 +01:00
parent 6156baab85
commit d9c87161ba
37 changed files with 387 additions and 216 deletions

View File

@ -38,13 +38,42 @@ class BlockAPI{
public static $class = array(); public static $class = array();
private $server; private $server;
public static function get($id, $meta = 0){ public static function get($id, $meta = 0, $v = false){
$id = (int) $id; $id = (int) $id;
if(isset(BlockAPI::$class[$id])){ if(isset(BlockAPI::$class[$id])){
$classname = BlockAPI::$class[$id]; $classname = BlockAPI::$class[$id];
return new $classname($meta); $b = new $classname($meta);
}else{
$b = new GenericBlock($id, $meta, "");
} }
return (new GenericBlock($id, $meta, "")); if($v instanceof Vector3){
$b->position($v);
}
return $b;
}
public function setBlock($block, $id, $meta){
if(($block instanceof Vector3) or (($block instanceof Block) and $block->inWorld === true)){
$this->server->api->level->setBlock($block->x, $block->y, $block->z, (int) $id, (int) $meta);
return true;
}
return false;
}
public function getBlock($x, $y = 0, $z = 0){
if($x instanceof Vector3){
$y = $x->y;
$z = $x->z;
$x = $x->x;
}
$b = $this->server->api->level->getBlock($x, $y, $z);
return BlockAPI::get($b[0], $b[1], new Vector3($b[2][0], $b[2][1], $b[2][2]));
}
public function getBlockFace(Block $block, $face){
$data = array("x" => $block->x, "y" => $block->y, "z" => $block->z);
BlockFace::setPosition($data, $face);
return $this->getBlock($data["x"], $data["y"], $data["z"]);
} }
function __construct(PocketMinecraftServer $server){ function __construct(PocketMinecraftServer $server){
@ -198,13 +227,13 @@ class BlockAPI{
} }
} }
private function cancelAction($block){ private function cancelAction(Block $block){
$this->server->api->dhandle("block.change", array( $this->server->api->dhandle("block.change", array(
"x" => $block[2][0], "x" => $block->x,
"y" => $block[2][1], "y" => $block->y,
"z" => $block[2][2], "z" => $block->z,
"block" => $block[0], "block" => $block->getID(),
"meta" => $block[1], "meta" => $block->getMetadata(),
"fake" => true, "fake" => true,
)); ));
return false; return false;
@ -215,30 +244,19 @@ class BlockAPI{
return; return;
} }
$target = $this->server->api->level->getBlock($data["x"], $data["y"], $data["z"]); $target = $this->getBlock($data["x"], $data["y"], $data["z"]);
$entity = $this->server->api->entity->get($data["eid"]); $player = $this->server->api->player->getByEID($data["eid"]);
if(($entity instanceof Entity) !== true){ if(($player instanceof Player) !== true){
return $this->cancelAction($block); //No Entity WTF? return $this->cancelAction($target); //No Entity WTF?
} }
$data["entity"] = $entity; $data["player"] = $player;
if(isset(Material::$unbreakable[$target[0]]) or $this->server->gamemode === 2){ if($target->isBreakable === false or $this->server->gamemode === 2){
return $this->cancelAction($target); return $this->cancelAction($target);
} }
$drop = array( $item = $player->equipment;
$target[0], //Block $drops = $target->getDrops($item, $player);
$target[1], //Meta
1, //Count /*switch($target->getID()){
);
switch($target[0]){
case 1:
$drop[0] = 4;
break;
case 16:
$drop = array(263, 0, 1);
break;
case 21:
$drop = array(351, 4, mt_rand(4, 8));
break;
case 62: case 62:
$drop[0] = 61; $drop[0] = 61;
case 50: //Drop without metadata case 50: //Drop without metadata
@ -256,103 +274,40 @@ class BlockAPI{
case 156: case 156:
$drop[1] = 0; $drop[1] = 0;
break; break;
case 56:
$drop = array(264, 0, 1);
break;
case 63:
case 68:
$drop = array(323, 0, 1);
break;
case 73:
case 74:
$drop = array(351, 4, mt_rand(4, 5));
break;
case 18:
$drop = false;
if(mt_rand(1,20) === 1){ //Saplings
$drop = array(6, $target[1], 1);
}
if($target[1] === 0 and mt_rand(1,200) === 1){ //Apples
$this->drop($data["x"], $data["y"], $data["z"], 260, 0, 1);
}
break;
case 59:
if($target[1] >= 0x07){ //Seeds
$drop = array(296, 0, 1);
$this->drop($data["x"], $data["y"], $data["z"], 295, 0, mt_rand(0,3));
}else{
$drop = array(295, 0, 1);
}
break;
case 31:
$drop = false;
if(mt_rand(1,10) === 1){ //Seeds
$drop = array(295, 0, 1);
}
break;
case 20:
$drop = false;
break;
case 30:
$drop = false;
break;
case 51:
$drop = false;
break;
case 52:
$drop = false;
break;
case 43:
$drop = array(
44,
$target[1],
2,
);
break;
case 46: //TNT
if(($player = $this->server->api->player->getByEID($data["eid"])) !== false){
$player->dataPacket(MC_EXPLOSION, array(
"x" => $data["x"],
"y" => $data["y"],
"z" => $data["z"],
"radius" => 2,
"records" => array(),
));
}
break;
case 60:
case 2:
$drop = array(3, 0, 1);
break;
case 64: //Wood Door case 64: //Wood Door
case 71: //Iron Door case 71: //Iron Door
$drop = array(($target[0] === 64 ? 324:330), 0, 1); if(($target->getMetadata() & 0x08) === 0x08){
if(($target[1] & 0x08) === 0x08){ $down = $this->getBlock($target->x, $target->y - 1, $target->z);
$down = $this->server->api->level->getBlock($data["x"], $data["y"] - 1, $data["z"]); if($down->getID() === $target->getID()){
if($down[0] === $target[0]){
$data2 = $data; $data2 = $data;
--$data2["y"]; --$data2["y"];
$this->server->trigger("player.block.break", $data2); $this->server->trigger("player.block.break", $data2);
} }
}else{ }else{
$up = $this->server->api->level->getBlock($data["x"], $data["y"] + 1, $data["z"]); $up = $this->getBlock($target->x, $target->y + 1, $target->z);
if($up[0] === $target[0]){ if($up->getID() === $target->getID()){
$data2 = $data; $data2 = $data;
++$data2["y"]; ++$data2["y"];
$this->server->trigger("player.block.break", $data2); $this->server->trigger("player.block.break", $data2);
} }
} }
break; break;
}*/
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);
}
} }
if($drop !== false and $drop[0] !== 0 and $drop[2] > 0){ $this->server->trigger("player.block.break", array(
$this->drop($data["x"], $data["y"], $data["z"], $drop[0], $drop[1] & 0x0F, $drop[2] & 0xFF); "block" => $target,
} "player" => $player,
$this->server->trigger("player.block.break", $data); "item" => $item,
));
return false; return false;
} }
public function drop($x, $y, $z, $block, $meta, $stack = 1){ public function drop($x, $y, $z, $block, $meta, $stack = 1){
if($block === 0 or $stack <= 0 or $this->server->gamemode === 1){ if($block === AIR or $stack <= 0 or $this->server->gamemode === 1){
return; return;
} }
$data = array( $data = array(
@ -385,21 +340,32 @@ class BlockAPI{
if($data["face"] < 0 or $data["face"] > 5){ if($data["face"] < 0 or $data["face"] > 5){
return false; return false;
} }
$data["original"] = array($data["block"], $data["meta"]); $data["original"] = BlockAPI::get($data["block"], $data["meta"]);
$target = $this->server->api->level->getBlock($data["x"], $data["y"], $data["z"]); $target = $this->getBlock(new Vector3($data["x"], $data["y"], $data["z"]));
$entity = $this->server->api->entity->get($data["eid"]); $player = $this->server->api->player->getByEID($data["eid"]);
if(($entity instanceof Entity) !== true){ if(($player instanceof Player) !== true){
return $this->cancelAction($block); //No Entity WTF? return $this->cancelAction($target); //No Entity WTF?
} }
$data["entity"] = $entity; $data["player"] = $player;
if($target[0] === 0){ //If no block exists $block = $this->getBlockFace($target, $data["face"]);
$item = $player->equipment;
if($target->getID() === AIR){ //If no block exists
$this->cancelAction($target); $this->cancelAction($target);
$block = $this->server->api->level->getBlockFace($target, $data["face"]);
return $this->cancelAction($block); return $this->cancelAction($block);
} }
$cancelPlace = false; $cancelPlace = false;
if(isset(Material::$activable[$target[0]])){ if($target->isActivable === true){
if($target->onActivate($this, $item, $player) === true){
return false;
}
}
if($this->server->gamemode === 2){ //Adventure mode!!
return $this->cancelAction($block);
}
/*if(isset(Material::$activable[$target[0]])){
switch($target[0]){ switch($target[0]){
case 54: case 54:
$cancelPlace = true; $cancelPlace = true;
@ -519,16 +485,11 @@ class BlockAPI{
$cancelPlace = true; $cancelPlace = true;
break; break;
} }
} }*/
if($cancelPlace === true){
return false;
}
if($this->server->gamemode === 2){
return $this->cancelAction($block); /*
}
$replace = false; $replace = false;
switch($data["block"]){ switch($data["block"]){
case 44: //Slabs case 44: //Slabs
@ -564,34 +525,29 @@ class BlockAPI{
} }
break; break;
} }
*/
if($replace === false){ if($block->y > 127 or $block->y < 0){
BlockFace::setPosition($data, $data["face"]);
}
if($data["y"] >= 127){
return false; return false;
} }
$block = $this->server->api->level->getBlock($data["x"], $data["y"], $data["z"]); if($block->isReplaceable === true and $item->isPlaceable()){
$hand = $item->getBlock();
if($replace === false and !isset(Material::$replaceable[$block[0]])){
return $this->cancelAction($block);
}
if(isset(Material::$placeable[$data["block"]])){
$data["block"] = Material::$placeable[$data["block"]] === true ? $data["block"]:Material::$placeable[$data["block"]];
}else{ }else{
return $this->cancelAction($block); return $this->cancelAction($block);
} }
if(!isset(Material::$transparent[$data["block"]]) and $entity->inBlock($block[2][0], $block[2][1], $block[2][2])){ if($hand->isTransparent === false and $player->entity->inBlock($block->x, $block->y, $block->z)){
return $this->cancelAction($block); //Entity in block return $this->cancelAction($block); //Entity in block
} }
$direction = $entity->getDirection(); //$direction = $player->entity->getDirection();
switch($data["block"]){ if($hand->place($this, $item, $player, $block, $target, $data["face"]) === false){
return false;
}
/*switch($data["block"]){
case 6: case 6:
if($target[0] === 60){ if($target[0] === 60){
break; break;
@ -800,7 +756,9 @@ class BlockAPI{
} }
break; break;
} }
$this->server->handle("player.block.place", $data); */
//$this->server->handle("player.block.place", $data);
return false; return false;
} }

View File

@ -48,13 +48,12 @@ class LevelAPI{
$this->setBlock($data["x"], $data["y"], $data["z"], $data["block"], $data["meta"]); $this->setBlock($data["x"], $data["y"], $data["z"], $data["block"], $data["meta"]);
break; break;
case "player.block.break": case "player.block.break":
$block = $this->getBlock($data["x"], $data["y"], $data["z"]); if($data["block"]->getID() === 0){
if($block[0] === 0){
break; break;
} }
$b = BlockAPI::get($block[0], $block[1]); $b = BlockAPI::get($block[0], $block[1]);
console("[DEBUG] Player ".$data["entity"]->player->username." broke ".$b->getName()." (".$block[0].":".$block[1].") at (".$data["x"].", ".$data["y"].", ".$data["z"].")", true, true, 2); console("[DEBUG] Player ".$data["player"]->username." broke ".$data["block"]->getName()." (".$data["block"]->getID().":".$data["block"]->getMetadata().") at (".$data["block"]->x.", ".$data["block"]->y.", ".$data["block"]->z.")", true, true, 2);
$this->setBlock($data["x"], $data["y"], $data["z"], 0, 0, true, true); $this->setBlock($data["block"]->x, $data["block"]->y, $data["block"]->z, 0, 0, true, true);
break; break;
} }
} }

View File

@ -46,7 +46,7 @@ class Player{
var $MTU; var $MTU;
var $spawned = false; var $spawned = false;
var $inventory; var $inventory;
var $equipment = array(1, 0); public $equipment;
var $armor = array(0, 0, 0, 0); var $armor = array(0, 0, 0, 0);
var $loggedIn = false; var $loggedIn = false;
function __construct(PocketMinecraftServer $server, $clientID, $ip, $port, $MTU){ function __construct(PocketMinecraftServer $server, $clientID, $ip, $port, $MTU){
@ -57,7 +57,12 @@ class Player{
$this->ip = $ip; $this->ip = $ip;
$this->port = $port; $this->port = $port;
$this->timeout = microtime(true) + 20; $this->timeout = microtime(true) + 20;
$this->inventory = array_fill(0, 36, array(0, 0, 0)); $this->inventory = array_fill(0, 36, array(AIR, 0, 0));
if($this->server->gamemode === 0 or $this->server->gamemode === 2){
$this->equipment = new Item(AIR);
}else{
$this->equipment = new Item(STONE);
}
$this->evid[] = $this->server->event("server.tick", array($this, "onTick")); $this->evid[] = $this->server->event("server.tick", array($this, "onTick"));
$this->evid[] = $this->server->event("server.close", array($this, "close")); $this->evid[] = $this->server->event("server.close", array($this, "close"));
console("[DEBUG] New Session started with ".$ip.":".$port.". MTU ".$this->MTU.", Client ID ".$this->clientID, true, true, 2); console("[DEBUG] New Session started with ".$ip.":".$port.". MTU ".$this->MTU.", Client ID ".$this->clientID, true, true, 2);
@ -199,7 +204,7 @@ class Player{
break; break;
case "player.block.place": case "player.block.place":
if($data["eid"] === $this->eid and ($this->server->gamemode === 0 or $this->server->gamemode === 2)){ if($data["eid"] === $this->eid and ($this->server->gamemode === 0 or $this->server->gamemode === 2)){
$this->removeItem($data["original"][0], $data["original"][1], 1); $this->removeItem($data["original"]->getID(), $data["original"]->getMetadata(), 1);
} }
break; break;
case "player.pickup": case "player.pickup":
@ -547,10 +552,8 @@ class Player{
case MC_PLAYER_EQUIPMENT: case MC_PLAYER_EQUIPMENT:
$data["eid"] = $this->eid; $data["eid"] = $this->eid;
if($this->server->handle("player.equipment.change", $data) !== false){ if($this->server->handle("player.equipment.change", $data) !== false){
$this->equipment[0] = $data["block"]; $this->equipment = new Item($data["block"], $data["meta"]);
$this->equipment[1] = $data["meta"]; console("[DEBUG] Player ".$this->username." has now ".$this->equipment->getName()." (".$this->equipment->getID().":".$this->equipment->getMetadata().") in their hands!", true, true, 2);
$b = BlockAPI::get($data["block"], $data["meta"]);
console("[DEBUG] Player ".$this->username." has now ".$b->getName()." (".$data["block"].":".$data["meta"].") in their hands!", true, true, 2);
} }
break; break;
case MC_REQUEST_CHUNK: case MC_REQUEST_CHUNK:
@ -639,13 +642,13 @@ class Player{
363 => 3, 363 => 3,
364 => 8, 364 => 8,
); );
if(isset($items[$this->equipment[0]])){ if(isset($items[$this->equipment->getID()])){
$this->removeItem($this->equipment[0], 0, 1); $this->removeItem($this->equipment[0], 0, 1);
$this->dataPacket(MC_ENTITY_EVENT, array( $this->dataPacket(MC_ENTITY_EVENT, array(
"eid" => 0, "eid" => 0,
"event" => 9, "event" => 9,
)); ));
$this->entity->heal($items[$this->equipment[0]], "eating"); $this->entity->heal($items[$this->equipment->getID()], "eating");
} }
break; break;
} }

View File

@ -41,7 +41,9 @@ abstract class Block{
public $inWorld = false; public $inWorld = false;
public $hasPhysics = false; public $hasPhysics = false;
public $isLiquid = false; public $isLiquid = false;
public $v = false; public $x;
public $y;
public $z;
public function __construct($id, $meta = 0, $name = "Unknown"){ public function __construct($id, $meta = 0, $name = "Unknown"){
$this->id = (int) $id; $this->id = (int) $id;
@ -55,7 +57,7 @@ abstract class Block{
} }
final public function getID(){ final public function getID(){
return $id; return $this->id;
} }
final public function getMetadata(){ final public function getMetadata(){
@ -64,7 +66,9 @@ abstract class Block{
final public function position(Vector3 $v){ final public function position(Vector3 $v){
$this->inWorld = true; $this->inWorld = true;
$this->v = new Vector3((int) $v->x, (int) $v->y, (int) $v->z); $this->x = (int) $v->x;
$this->y = (int) $v->y;
$this->z = (int) $v->z;
} }
public function getDrops(Item $item, Player $player){ public function getDrops(Item $item, Player $player){
@ -73,9 +77,11 @@ abstract class Block{
); );
} }
abstract function onActivate(LevelAPI $level, Item $item, Player $player); abstract function place(BlockAPI $level, Item $item, Player $player, Block $block, Block $target, $face);
abstract function onUpdate(LevelAPI $level, $type); abstract function onActivate(BlockAPI $level, Item $item, Player $player);
abstract function onUpdate(BlockAPI $level, $type);
} }
require_once("block/GenericBlock.php"); require_once("block/GenericBlock.php");

View File

@ -35,6 +35,7 @@ define("PLANK", 5);
define("PLANKS", 5); define("PLANKS", 5);
define("WOODEN_PLANK", 5); define("WOODEN_PLANK", 5);
define("SAPLING", 6); define("SAPLING", 6);
define("SAPLINGS", 6);
define("BEDROCK", 7); define("BEDROCK", 7);
define("WATER", 8); define("WATER", 8);
define("STILL_WATER", 9); define("STILL_WATER", 9);

View File

@ -26,24 +26,70 @@ the Free Software Foundation, either version 3 of the License, or
*/ */
class Item{ class Item{
protected $block = false;
protected $id; protected $id;
protected $meta; protected $meta;
protected $count;
protected $maxStackSize = 64; protected $maxStackSize = 64;
protected $durability = 0; protected $durability = 0;
protected $name = "Unknown"; protected $name = "Unknown";
public function __construct($id, $meta = 0){ public function __construct($id, $meta = 0, $count = 1){
$this->id = (int) $id; $this->id = (int) $id;
$this->meta = (int) $meta; $this->meta = (int) $meta;
$this->count = (int) $count;
if(isset(BlockAPI::$class[$this->id])){
$this->block = BlockAPI::get($this->id, $this->meta);
$this->name = $this->block->getName();
}
} }
public function getName(){
return $this->name;
}
public function isPlaceable(){
return (($this->block instanceof Block) and $this->block->isPlaceable === true);
}
public function getBlock(){
if($this->block instanceof Block){
return $this->block;
}else{
return BlockAPI::get(AIR);
}
}
public function getID(){
return $this->id;
}
public function getMetadata(){
return $this->meta;
}
public function getMaxStackSize(){ public function getMaxStackSize(){
return $this->maxStackSize; return $this->maxStackSize;
} }
public function getDestroySpeed(Item $item, Player $player){ public function isPickaxe(){ //Returns false or level of the pickaxe
switch($this->id){
case IRON_PICKAXE:
return 3;
case 270: //Wood
return 1;
case 274: //Stone
return 2;
case 278: //Diamond
return 4;
case 285: //Gold
return 3;
default:
return false;
}
}
public function getDestroySpeed(Block $block, Player $player){
return 1; return 1;
} }

View File

@ -29,11 +29,19 @@ the Free Software Foundation, either version 3 of the License, or
class GenericBlock extends Block{ class GenericBlock extends Block{
public function __construct($id, $meta = 0, $name = "Unknown"){ public function __construct($id, $meta = 0, $name = "Unknown"){
parent::__construct($id, $meta, $name); parent::__construct($id, $meta, $name);
} }
public function onUpdate(LevelAPI $level, $type){ public function place(BlockAPI $level, Item $item, Player $player, Block $block, Block $target, $face){
if($block->inWorld === true){
$level->setBlock($block, $this->id, $this->getMetadata());
return true;
}
return false; return false;
} }
public function onActivate(LevelAPI $level, Item $item, Player $player){
public function onUpdate(BlockAPI $level, $type){
return false;
}
public function onActivate(BlockAPI $level, Item $item, Player $player){
return false; return false;
} }
} }

View File

@ -29,5 +29,6 @@ class LiquidBlock extends TransparentBlock{
public function __construct($id, $meta = 0, $name = "Unknown"){ public function __construct($id, $meta = 0, $name = "Unknown"){
parent::__construct($id, $meta, $name); parent::__construct($id, $meta, $name);
$this->isLiquid = true; $this->isLiquid = true;
$this->isBreakable = false;
} }
} }

View File

@ -29,6 +29,7 @@ class FireBlock extends FlowableBlock{
public function __construct($meta = 0){ public function __construct($meta = 0){
parent::__construct(FIRE, $meta, "Fire"); parent::__construct(FIRE, $meta, "Fire");
$this->isReplaceable = true; $this->isReplaceable = true;
$this->isBreakable = false;
} }
} }

View File

@ -29,5 +29,18 @@ class TNTBlock extends SolidBlock{
public function __construct(){ public function __construct(){
parent::__construct(TNT, 0, "TNT"); parent::__construct(TNT, 0, "TNT");
} }
public function getDrops(Item $item, Player $player){
if($this->inWorld === true){
$player->dataPacket(MC_EXPLOSION, array(
"x" => $this->x,
"y" => $this->y,
"z" => $this->z,
"radius" => 2,
"records" => array(),
));
}
return array(
array(TNT, 0, 1),
);
}
} }

View File

@ -31,9 +31,13 @@ class CoalOreBlock extends SolidBlock{
} }
public function getDrops(Item $item, Player $player){ public function getDrops(Item $item, Player $player){
return array( if($item->isPickaxe() >= 1){
array(263, 0, 1), return array(
); array(263, 0, 1), //Coal
);
}else{
return array();
}
} }
} }

View File

@ -30,4 +30,13 @@ class DiamondOreBlock extends SolidBlock{
parent::__construct(DIAMOND_ORE, 0, "Diamond Ore"); parent::__construct(DIAMOND_ORE, 0, "Diamond Ore");
} }
public function getDrops(Item $item, Player $player){
if($item->isPickaxe() >= 3){
return array(
array(264, 0, 1),
);
}else{
return array();
}
}
} }

View File

@ -31,9 +31,13 @@ class LapisOreBlock extends SolidBlock{
} }
public function getDrops(Item $item, Player $player){ public function getDrops(Item $item, Player $player){
return array( if($item->isPickaxe() >= 2){
array(351, 4, 1), return array(
); array(351, 4, mt_rand(4, 8)),
);
}else{
return array();
}
} }
} }

View File

@ -29,5 +29,13 @@ class RedstoneOreBlock extends SolidBlock{
public function __construct(){ public function __construct(){
parent::__construct(REDSTONE_ORE, 0, "Redstone Ore"); parent::__construct(REDSTONE_ORE, 0, "Redstone Ore");
} }
public function getDrops(Item $item, Player $player){
if($item->isPickaxe() >= 2){
return array(
//array(331, 4, mt_rand(4, 5)),
);
}else{
return array();
}
}
} }

View File

@ -43,11 +43,26 @@ class SaplingBlock extends TransparentBlock{
$this->name = $names[$this->meta & 0x03]; $this->name = $names[$this->meta & 0x03];
} }
public function onActivate(LevelAPI $level, Item $item, Player $player){ public function place(BlockAPI $level, Item $item, Player $player, Block $block, Block $target, $face){
TreeObject::growTree($level, $this); if($block->inWorld === true){
$down = $level->getBlockFace($block, 0);
if($down->getID() === 2 or $down->getID() === 3 or $down->getID() === 60){
$level->setBlock($block, $this->id, $this->getMetadata());
return true;
}
}
return false;
} }
public function onUpdate(LevelAPI $level, $type){ public function onActivate(BlockAPI $level, Item $item, Player $player){
if($item->getID() === 351 and $item->getMetadata() === 0x0F){ //Bonemeal
TreeObject::growTree($level, $this);
return true;
}
return false;
}
public function onUpdate(BlockAPI $level, $type){
if($this->inWorld !== true){ if($this->inWorld !== true){
return false; return false;
} }

View File

@ -38,4 +38,12 @@ class TallGrassBlock extends FlowableBlock{
$this->name = $names[$this->meta & 0x03]; $this->name = $names[$this->meta & 0x03];
} }
public function getDrops(Item $item, Player $player){
$drops = array();
if(mt_rand(1,10) === 1){//Seeds
$drops[] = array(295, 0, 1);
}
return $drops;
}
} }

View File

@ -29,5 +29,15 @@ class WheatBlock extends FlowableBlock{
public function __construct($meta = 0){ public function __construct($meta = 0){
parent::__construct(WHEAT, $meta, "Wheat"); parent::__construct(WHEAT, $meta, "Wheat");
} }
public function getDrops(Item $item, Player $player){
$drops = array();
if($this->meta >= 0x07){
$drops[] = array(296, 0, 1);
$drops[] = array(295, 0, mt_rand(0, 3));
}else{
$drops[] = array(295, 0, 1);
}
return $drops;
}
} }

View File

@ -31,4 +31,14 @@ class BurningFurnaceBlock extends SolidBlock{
$this->isActivable = true; $this->isActivable = true;
} }
public function getDrops(Item $item, Player $player){
if($item->isPickaxe() >= 1){
return array(
array(FURNACE, 0, 1),
);
}else{
return array();
}
}
} }

View File

@ -30,5 +30,7 @@ class CobwebBlock extends FlowableBlock{
parent::__construct(COBWEB, 0, "Cobweb"); parent::__construct(COBWEB, 0, "Cobweb");
$this->isFlowable = true; $this->isFlowable = true;
} }
public function getDrops(Item $item, Player $player){
return array();
}
} }

View File

@ -40,5 +40,10 @@ class DoubleSlabBlock extends SolidBlock{
); );
$this->name = "Double " . $names[$this->meta & 0x07] . " Slab"; $this->name = "Double " . $names[$this->meta & 0x07] . " Slab";
} }
public function getDrops(Item $item, Player $player){
return array(
array(SLAB, $this->meta & 0x07, 2),
);
}
} }

View File

@ -29,5 +29,9 @@ class FarmlandBlock extends SolidBlock{
public function __construct($meta = 0){ public function __construct($meta = 0){
parent::__construct(FARMLAND, $meta, "Farmland"); parent::__construct(FARMLAND, $meta, "Farmland");
} }
public function getDrops(Item $item, Player $player){
return array(
array(DIRT, 0, 1),
);
}
} }

View File

@ -31,4 +31,13 @@ class FurnaceBlock extends SolidBlock{
$this->isActivable = true; $this->isActivable = true;
} }
public function getDrops(Item $item, Player $player){
if($item->isPickaxe() >= 1){
return array(
array(FURNACE, 0, 1),
);
}else{
return array();
}
}
} }

View File

@ -30,4 +30,7 @@ class GlassBlock extends TransparentBlock{
parent::__construct(GLASS, 0, "Glass"); parent::__construct(GLASS, 0, "Glass");
} }
public function getDrops(Item $item, Player $player){
return array();
}
} }

View File

@ -29,5 +29,9 @@ class GrassBlock extends SolidBlock{
public function __construct(){ public function __construct(){
parent::__construct(GRASS, 0, "Grass"); parent::__construct(GRASS, 0, "Grass");
} }
public function getDrops(Item $item, Player $player){
return array(
array(DIRT, 0, 1),
);
}
} }

View File

@ -30,5 +30,9 @@ class IronDoorBlock extends TransparentBlock{
parent::__construct(IRON_DOOR, $meta, "Iron Door"); parent::__construct(IRON_DOOR, $meta, "Iron Door");
$this->isActivable = true; $this->isActivable = true;
} }
public function getDrops(Item $item, Player $player){
return array(
array(330, 0, 1),
);
}
} }

View File

@ -26,14 +26,26 @@ the Free Software Foundation, either version 3 of the License, or
*/ */
class LeavesBlock extends TransparentBlock{ class LeavesBlock extends TransparentBlock{
const OAK = 0;
const SPRUCE = 1;
const BIRCH = 2;
public function __construct($meta = 0){ public function __construct($meta = 0){
parent::__construct(LEAVES, $meta, "Leaves"); parent::__construct(LEAVES, $meta, "Leaves");
$names = array( $names = array(
0 => "Oak Leaves", LeavesBlock::OAK => "Oak Leaves",
1 => "Spruce Leaves", LeavesBlock::SPRUCE => "Spruce Leaves",
2 => "Birch Leaves", LeavesBlock::BIRCH => "Birch Leaves",
); );
$this->name = $names[$this->meta & 0x03]; $this->name = $names[$this->meta & 0x03];
} }
public function getDrops(Item $item, Player $player){
$drops = array();
if(mt_rand(1,20) === 1){ //Saplings
$drops[] = array(SAPLING, $this->meta & 0x03, 1);
}
if(($this->meta & 0x03) === LeavesBlock::OAK and mt_rand(1,200) === 1){ //Apples
$drops[] = array(260, 0, 1);
}
return $drops;
}
} }

View File

@ -27,7 +27,7 @@ the Free Software Foundation, either version 3 of the License, or
class NetherrackBlock extends SolidBlock{ class NetherrackBlock extends SolidBlock{
public function __construct(){ public function __construct(){
parent::__construct(NETHERRACK, 0, "Netherrrack"); parent::__construct(NETHERRACK, 0, "Netherrack");
} }
} }

View File

@ -29,5 +29,9 @@ class SignPostBlock extends TransparentBlock{
public function __construct($meta = 0){ public function __construct($meta = 0){
parent::__construct(SIGN_POST, $meta, "Sign Post"); parent::__construct(SIGN_POST, $meta, "Sign Post");
} }
public function getDrops(Item $item, Player $player){
return array(
array(323, 0, 1),
);
}
} }

View File

@ -30,4 +30,14 @@ class StoneBlock extends SolidBlock{
parent::__construct(STONE, 0, "Stone"); parent::__construct(STONE, 0, "Stone");
} }
public function getDrops(Item $item, Player $player){
if($item->isPickaxe() >= 1){
return array(
array(COBBLESTONE, 0, 1),
);
}else{
return array();
}
}
} }

View File

@ -29,5 +29,10 @@ class WallSignBlock extends TransparentBlock{
public function __construct($meta = 0){ public function __construct($meta = 0){
parent::__construct(WALL_SIGN, $meta, "Wall Sign"); parent::__construct(WALL_SIGN, $meta, "Wall Sign");
} }
public function getDrops(Item $item, Player $player){
return array(
array(323, 0, 1),
);
}
} }

View File

@ -32,9 +32,9 @@ class WoodBlock extends SolidBlock{
public function __construct($meta = 0){ public function __construct($meta = 0){
parent::__construct(WOOD, $meta, "Wood"); parent::__construct(WOOD, $meta, "Wood");
$names = array( $names = array(
0 => "Oak Wood", WoodBlock::OAK => "Oak Wood",
1 => "Spruce Wood", WoodBlock::SPRUCE => "Spruce Wood",
2 => "Birch Wood", WoodBlock::BIRCH => "Birch Wood",
); );
$this->name = $names[$this->meta & 0x03]; $this->name = $names[$this->meta & 0x03];
} }

View File

@ -31,4 +31,9 @@ class WoodDoorBlock extends TransparentBlock{
$this->isActivable = true; $this->isActivable = true;
} }
public function getDrops(Item $item, Player $player){
return array(
array(324, 0, 1),
);
}
} }

View File

@ -43,11 +43,11 @@ class BigTreeObject extends TreeObject{
private $addLogVines = false; private $addLogVines = false;
private $addCocoaPlants = false; private $addCocoaPlants = false;
public function canPlaceObject(LevelAPI $level, $x, $y, $z){ public function canPlaceObject(BlockAPI $level, $x, $y, $z){
return false; return false;
} }
public function placeObject(LevelAPI $level, $x, $y, $z, $type){ public function placeObject(BlockAPI $level, $x, $y, $z, $type){
$this->trunkHeight = (int) ($this->totalHeight * $this->trunkHeightMultiplier); $this->trunkHeight = (int) ($this->totalHeight * $this->trunkHeightMultiplier);
$leaves = $this->getLeafGroupPoints($level, $x, $y, $z); $leaves = $this->getLeafGroupPoints($level, $x, $y, $z);

View File

@ -34,7 +34,7 @@ class PineTreeObject extends TreeObject{
private $leavesSizeY = -1; private $leavesSizeY = -1;
private $leavesAbsoluteMaxRadius = -1; private $leavesAbsoluteMaxRadius = -1;
public function canPlaceObject(LevelAPI $level, $x, $y, $z){ public function canPlaceObject(BlockAPI $level, $x, $y, $z){
$this->findRandomLeavesSize(); $this->findRandomLeavesSize();
$checkRadius = 0; $checkRadius = 0;
for($yy = 0; $yy < $this->totalHeight; ++$yy) { for($yy = 0; $yy < $this->totalHeight; ++$yy) {
@ -44,7 +44,7 @@ class PineTreeObject extends TreeObject{
for($xx = -$checkRadius; $xx < ($checkRadius + 1); ++$xx){ for($xx = -$checkRadius; $xx < ($checkRadius + 1); ++$xx){
for($zz = -$checkRadius; $zz < ($checkRadius + 1); ++$zz){ for($zz = -$checkRadius; $zz < ($checkRadius + 1); ++$zz){
$block = $level->getBlock($x + $xx, $y + $yy, $z + $zz); $block = $level->getBlock($x + $xx, $y + $yy, $z + $zz);
if(!isset($this->overridable[$block[0]])){ if(!isset($this->overridable[$block->getID()])){
return false; return false;
} }
} }
@ -59,11 +59,11 @@ class PineTreeObject extends TreeObject{
$this->leavesAbsoluteMaxRadius = 2 + mt_rand(0, 2); $this->leavesAbsoluteMaxRadius = 2 + mt_rand(0, 2);
} }
public function placeObject(LevelAPI $level, $x, $y, $z){ public function placeObject(BlockAPI $level, $x, $y, $z){
if($this->leavesSizeY === -1 or $this->leavesAbsoluteMaxRadius === -1) { if($this->leavesSizeY === -1 or $this->leavesAbsoluteMaxRadius === -1) {
$this->findRandomLeavesSize(); $this->findRandomLeavesSize();
} }
$level->setBlock($x, $y - 1, $z, 3, 0); $level->setBlock(new Vector3($x, $y - 1, $z), 3, 0);
$leavesRadius = mt_rand(0,2); $leavesRadius = mt_rand(0,2);
$leavesMaxRadius = 1; $leavesMaxRadius = 1;
$leavesBottomY = $this->totalHeight - $this->leavesSizeY; $leavesBottomY = $this->totalHeight - $this->leavesSizeY;
@ -73,7 +73,7 @@ class PineTreeObject extends TreeObject{
for ($xx = -$leavesRadius; $xx < ($leavesRadius + 1); ++$xx) { for ($xx = -$leavesRadius; $xx < ($leavesRadius + 1); ++$xx) {
for ($zz = -$leavesRadius; $zz < ($leavesRadius + 1); ++$zz) { for ($zz = -$leavesRadius; $zz < ($leavesRadius + 1); ++$zz) {
if (abs($xx) != $leavesRadius or abs($zz) != $leavesRadius or $leavesRadius <= 0) { if (abs($xx) != $leavesRadius or abs($zz) != $leavesRadius or $leavesRadius <= 0) {
$level->setBlock($x + $xx, $y + $yy, $z + $zz, 18, $this->type); $level->setBlock(New Vector3($x + $xx, $y + $yy, $z + $zz), 18, $this->type);
} }
} }
} }
@ -89,7 +89,7 @@ class PineTreeObject extends TreeObject{
} }
$trunkHeightReducer = mt_rand(0,3); $trunkHeightReducer = mt_rand(0,3);
for($yy = 0; $yy < ($this->totalHeight - $trunkHeightReducer); ++$yy){ for($yy = 0; $yy < ($this->totalHeight - $trunkHeightReducer); ++$yy){
$level->setBlock($x, $y + $yy, $z, 17, $this->type); $level->setBlock(new Vector3($x, $y + $yy, $z), 17, $this->type);
} }
} }

View File

@ -37,7 +37,7 @@ class SmallTreeObject extends TreeObject{
private $addLogVines = false; private $addLogVines = false;
private $addCocoaPlants = false; private $addCocoaPlants = false;
public function canPlaceObject(LevelAPI $level, $x, $y, $z){ public function canPlaceObject(BlockAPI $level, $x, $y, $z){
$radiusToCheck = $this->radiusIncrease; $radiusToCheck = $this->radiusIncrease;
for ($yy = 0; $yy < $this->totalHeight + 2; ++$yy) { for ($yy = 0; $yy < $this->totalHeight + 2; ++$yy) {
if ($yy == 1 or $yy === $this->totalHeight - 1) { if ($yy == 1 or $yy === $this->totalHeight - 1) {
@ -45,8 +45,8 @@ class SmallTreeObject extends TreeObject{
} }
for($xx = -$radiusToCheck; $xx < ($radiusToCheck + 1); ++$xx){ for($xx = -$radiusToCheck; $xx < ($radiusToCheck + 1); ++$xx){
for($zz = -$radiusToCheck; $zz < ($radiusToCheck + 1); ++$zz){ for($zz = -$radiusToCheck; $zz < ($radiusToCheck + 1); ++$zz){
$block = $level->getBlock($x + $xx, $y + $yy, $z + $zz); $block = $level->getBlock(new Vector3($x + $xx, $y + $yy, $z + $zz));
if(!isset($this->overridable[$block[0]])){ if(!isset($this->overridable[$block->getID()])){
return false; return false;
} }
} }
@ -55,8 +55,8 @@ class SmallTreeObject extends TreeObject{
return true; return true;
} }
public function placeObject(LevelAPI $level, $x, $y, $z){ public function placeObject(BlockAPI $level, $x, $y, $z){
$level->setBlock($x, $y - 1, $z, 3, 0); $level->setBlock(new Vector3($x, $y - 1, $z), 3, 0);
$this->totalHeight += mt_rand(-1, 3); $this->totalHeight += mt_rand(-1, 3);
$this->leavesHeight += mt_rand(0, 1); $this->leavesHeight += mt_rand(0, 1);
for($yy = ($this->totalHeight - $this->leavesHeight); $yy < ($this->totalHeight + 1); ++$yy){ for($yy = ($this->totalHeight - $this->leavesHeight); $yy < ($this->totalHeight + 1); ++$yy){
@ -65,13 +65,13 @@ class SmallTreeObject extends TreeObject{
for($xx = -$xzRadius; $xx < ($xzRadius + 1); ++$xx){ for($xx = -$xzRadius; $xx < ($xzRadius + 1); ++$xx){
for($zz = -$xzRadius; $zz < ($xzRadius + 1); ++$zz){ for($zz = -$xzRadius; $zz < ($xzRadius + 1); ++$zz){
if((abs($xx) != $xzRadius or abs($zz) != $xzRadius) and $yRadius != 0){ if((abs($xx) != $xzRadius or abs($zz) != $xzRadius) and $yRadius != 0){
$level->setBlock($x + $xx, $y + $yy, $z + $zz, 18, $this->type); $level->setBlock(new Vector3($x + $xx, $y + $yy, $z + $zz), 18, $this->type);
} }
} }
} }
} }
for($yy = 0; $yy < ($this->totalHeight - 1); ++$yy){ for($yy = 0; $yy < ($this->totalHeight - 1); ++$yy){
$level->setBlock($x, $y + $yy, $z, 17, $this->type); $level->setBlock(new Vector3($x, $y + $yy, $z), 17, $this->type);
} }
} }

View File

@ -34,7 +34,7 @@ class SpruceTreeObject extends TreeObject{
private $leavesBottomY = -1; private $leavesBottomY = -1;
private $leavesMaxRadius = -1; private $leavesMaxRadius = -1;
public function canPlaceObject(LevelAPI $level, $x, $y, $z){ public function canPlaceObject(BlockAPI $level, $x, $y, $z){
$this->findRandomLeavesSize(); $this->findRandomLeavesSize();
$checkRadius = 0; $checkRadius = 0;
for($yy = 0; $yy < $this->totalHeight + 2; ++$yy) { for($yy = 0; $yy < $this->totalHeight + 2; ++$yy) {
@ -43,8 +43,8 @@ class SpruceTreeObject extends TreeObject{
} }
for($xx = -$checkRadius; $xx < ($checkRadius + 1); ++$xx){ for($xx = -$checkRadius; $xx < ($checkRadius + 1); ++$xx){
for($zz = -$checkRadius; $zz < ($checkRadius + 1); ++$zz){ for($zz = -$checkRadius; $zz < ($checkRadius + 1); ++$zz){
$block = $level->getBlock($x + $xx, $y + $yy, $z + $zz); $block = $level->getBlock(new Vector3($x + $xx, $y + $yy, $z + $zz));
if(!isset($this->overridable[$block[0]])){ if(!isset($this->overridable[$block->getID()])){
return false; return false;
} }
} }
@ -59,17 +59,17 @@ class SpruceTreeObject extends TreeObject{
$this->leavesMaxRadius = 1 + mt_rand(0, 1); $this->leavesMaxRadius = 1 + mt_rand(0, 1);
} }
public function placeObject(LevelAPI $level, $x, $y, $z){ public function placeObject(BlockAPI $level, $x, $y, $z){
if($this->leavesBottomY === -1 or $this->leavesMaxRadius === -1) { if($this->leavesBottomY === -1 or $this->leavesMaxRadius === -1) {
$this->findRandomLeavesSize(); $this->findRandomLeavesSize();
} }
$level->setBlock($x, $y - 1, $z, 3, 0); $level->setBlock(new Vector3($x, $y - 1, $z), 3, 0);
$leavesRadius = 0; $leavesRadius = 0;
for($yy = $this->totalHeight; $yy >= $this->leavesBottomY; --$yy){ for($yy = $this->totalHeight; $yy >= $this->leavesBottomY; --$yy){
for ($xx = -$leavesRadius; $xx < ($leavesRadius + 1); ++$xx) { for ($xx = -$leavesRadius; $xx < ($leavesRadius + 1); ++$xx) {
for ($zz = -$leavesRadius; $zz < ($leavesRadius + 1); ++$zz) { for ($zz = -$leavesRadius; $zz < ($leavesRadius + 1); ++$zz) {
if (abs($xx) != $leavesRadius or abs($zz) != $leavesRadius or $leavesRadius <= 0) { if (abs($xx) != $leavesRadius or abs($zz) != $leavesRadius or $leavesRadius <= 0) {
$level->setBlock($x + $xx, $y + $yy, $z + $zz, 18, $this->type); $level->setBlock(new Vector3($x + $xx, $y + $yy, $z + $zz), 18, $this->type);
} }
} }
} }
@ -80,7 +80,7 @@ class SpruceTreeObject extends TreeObject{
} }
} }
for($yy = 0; $yy < ($this->totalHeight - 1); ++$yy){ for($yy = 0; $yy < ($this->totalHeight - 1); ++$yy){
$level->setBlock($x, $y + $yy, $z, 17, $this->type); $level->setBlock(new Vector3($x, $y + $yy, $z), 17, $this->type);
} }
} }

View File

@ -33,7 +33,7 @@ class TreeObject{
17 => true, 17 => true,
18 => true, 18 => true,
); );
public static function growTree(LevelAPI $level, Block $block){ public static function growTree(BlockAPI $level, Block $block){
switch($block->getMetadata() & 0x03){ switch($block->getMetadata() & 0x03){
case SaplingBlock::SPRUCE: case SaplingBlock::SPRUCE:
if(mt_rand(0,1) == 1){ if(mt_rand(0,1) == 1){
@ -55,8 +55,8 @@ class TreeObject{
} }
break; break;
} }
if($tree->canPlaceObject($level, $block->v->x, $block->v->y, $block->v->z)){ if($tree->canPlaceObject($level, $block->x, $block->y, $block->z)){
$tree->placeObject($level, $block->v->x, $block->v->y, $block->v->z); $tree->placeObject($level, $block->x, $block->y, $block->z);
} }
} }
} }