Switched gamemode handler to Player object

This commit is contained in:
Shoghi Cervantes Pueyo 2013-02-06 16:18:15 +01:00
parent 4aa0f8f8d1
commit d2dcf4f43a
10 changed files with 33 additions and 18 deletions

View File

@ -154,10 +154,10 @@ class BlockAPI{
public function playerBlockBreak(Player $player, Vector3 $vector){ public function playerBlockBreak(Player $player, Vector3 $vector){
$target = $this->getBlock($vector); $target = $this->getBlock($vector);
if($target->isBreakable === false or $this->server->gamemode === 2){
return $this->cancelAction($target);
}
$item = $player->equipment; $item = $player->equipment;
if(!$target->isBreakable($item, $player) or $this->server->gamemode === 2){
return $this->cancelAction($target);
}
if($this->server->api->dhandle("player.block.break", array("player" => $player, "target" => $target, "item" => $item)) !== false){ if($this->server->api->dhandle("player.block.break", array("player" => $player, "target" => $target, "item" => $item)) !== false){

View File

@ -135,7 +135,7 @@ class ServerAPI{
console("[NOTICE] \x1b[33mA new DEVELOPMENT version of PocketMine-MP has been released"); console("[NOTICE] \x1b[33mA new DEVELOPMENT version of PocketMine-MP has been released");
console("[NOTICE] \x1b[33mVersion \"".$info["development"]["version"]."\" [".substr($info["development"]["commit"], 0, 10)."]"); console("[NOTICE] \x1b[33mVersion \"".$info["development"]["version"]."\" [".substr($info["development"]["commit"], 0, 10)."]");
console("[NOTICE] \x1b[36mIf you want to update, get the latest version at ".$info["development"]["download"]); console("[NOTICE] \x1b[36mIf you want to update, get the latest version at ".$info["development"]["download"]);
console("[NOTICE] This message will dissapear when you issue the command \"/update-done\""); console("[NOTICE] This message will dissapear after issuing the command \"/update-done\"");
sleep(3); sleep(3);
}else{ }else{
$this->setProperty("last-update", time()); $this->setProperty("last-update", time());

View File

@ -49,6 +49,7 @@ class Player{
public $equipment; public $equipment;
var $armor = array(0, 0, 0, 0); var $armor = array(0, 0, 0, 0);
var $loggedIn = false; var $loggedIn = false;
public $gamemode;
private $chunksLoaded = array(); private $chunksLoaded = array();
private $chunksOrder = array(); private $chunksOrder = array();
@ -61,7 +62,8 @@ class Player{
$this->port = $port; $this->port = $port;
$this->timeout = microtime(true) + 20; $this->timeout = microtime(true) + 20;
$this->inventory = array_fill(0, 36, array(AIR, 0, 0)); $this->inventory = array_fill(0, 36, array(AIR, 0, 0));
if($this->server->gamemode === 0 or $this->server->gamemode === 2){ $this->gamemode = $this->server->gamemode;
if($this->gamemode === 0 or $this->gamemode === 2){
$this->equipment = BlockAPI::getItem(AIR); $this->equipment = BlockAPI::getItem(AIR);
}else{ }else{
$this->equipment = BlockAPI::getItem(STONE); $this->equipment = BlockAPI::getItem(STONE);
@ -272,14 +274,14 @@ 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->gamemode === 0 or $this->gamemode === 2)){
$this->removeItem($data["original"]->getID(), $data["original"]->getMetadata(), 1); $this->removeItem($data["original"]->getID(), $data["original"]->getMetadata(), 1);
} }
break; break;
case "player.pickup": case "player.pickup":
if($data["eid"] === $this->eid){ if($data["eid"] === $this->eid){
$data["eid"] = 0; $data["eid"] = 0;
if(($this->server->gamemode === 0 or $this->server->gamemode === 2)){ if(($this->gamemode === 0 or $this->gamemode === 2)){
$this->addItem($data["entity"]->type, $data["entity"]->meta, $data["entity"]->stack); $this->addItem($data["entity"]->type, $data["entity"]->meta, $data["entity"]->stack);
} }
} }
@ -517,7 +519,7 @@ class Player{
} }
$this->server->api->player->add($this->CID); $this->server->api->player->add($this->CID);
$this->auth = true; $this->auth = true;
if(!isset($this->data["inventory"]) or $this->server->gamemode === 1){ if(!isset($this->data["inventory"]) or $this->gamemode === 1){
$this->data["inventory"] = $this->inventory; $this->data["inventory"] = $this->inventory;
} }
$this->inventory = &$this->data["inventory"]; $this->inventory = &$this->data["inventory"];
@ -535,7 +537,7 @@ class Player{
"y" => $this->data["spawn"]["y"], "y" => $this->data["spawn"]["y"],
"z" => $this->data["spawn"]["z"], "z" => $this->data["spawn"]["z"],
"unknown1" => 0, "unknown1" => 0,
"gamemode" => $this->server->gamemode, "gamemode" => $this->gamemode,
"eid" => 0, "eid" => 0,
)); ));
break; break;
@ -600,7 +602,7 @@ class Player{
0x80 ? 0x80 ?
*/ */
$flags = 0; $flags = 0;
if($this->server->gamemode === 2){ if($this->gamemode === 2){
$flags |= 0x01; //Not allow placing/breaking blocks $flags |= 0x01; //Not allow placing/breaking blocks
} }
$flags |= 0x20; //Nametags $flags |= 0x20; //Nametags
@ -632,7 +634,7 @@ class Player{
$data["eid"] = $this->eid; $data["eid"] = $this->eid;
if(Utils::distance($this->entity->position, $data) > 10){ if(Utils::distance($this->entity->position, $data) > 10){
break; break;
}elseif(($this->server->gamemode === 0 or $this->server->gamemode === 2) and !$this->hasItem($data["block"], $data["meta"])){ }elseif(($this->gamemode === 0 or $this->gamemode === 2) and !$this->hasItem($data["block"], $data["meta"])){
console("[DEBUG] Player \"".$this->username."\" tried to place not got block (or crafted block)", true, true, 2); console("[DEBUG] Player \"".$this->username."\" tried to place not got block (or crafted block)", true, true, 2);
//break; //break;
} }
@ -672,7 +674,7 @@ class Player{
$this->entity->updateMetadata(); $this->entity->updateMetadata();
break; break;
case MC_SET_HEALTH: case MC_SET_HEALTH:
if($this->server->gamemode === 1){ if($this->gamemode === 1){
break; break;
} }
//$this->entity->setHealth($data["health"], "client"); //$this->entity->setHealth($data["health"], "client");

View File

@ -138,7 +138,7 @@ abstract class Block{
protected $shortname = ""; protected $shortname = "";
protected $name = ""; protected $name = "";
public $isActivable = false; public $isActivable = false;
public $isBreakable = true; public $breakable = true;
public $isFlowable = false; public $isFlowable = false;
public $isTransparent = false; public $isTransparent = false;
public $isReplaceable = false; public $isReplaceable = false;
@ -186,6 +186,8 @@ abstract class Block{
} }
} }
abstract function isBreakable(Item $item, Player $player);
abstract function onBreak(BlockAPI $level, Item $item, Player $player); abstract function onBreak(BlockAPI $level, Item $item, Player $player);
abstract function place(BlockAPI $level, Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz); abstract function place(BlockAPI $level, Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz);

View File

@ -38,6 +38,10 @@ class GenericBlock extends Block{
return false; return false;
} }
public function isBreakable(Item $item, Player $player){
return $this->breakable;
}
public function onBreak(BlockAPI $level, Item $item, Player $player){ public function onBreak(BlockAPI $level, Item $item, Player $player){
if($this->inWorld === true){ if($this->inWorld === true){
$level->setBlock($this, AIR, 0); $level->setBlock($this, AIR, 0);

View File

@ -29,7 +29,7 @@ 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; $this->breakable = false;
$this->isReplaceable = true; $this->isReplaceable = true;
} }
} }

View File

@ -29,7 +29,7 @@ class TransparentBlock extends GenericBlock{
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->isActivable = false; $this->isActivable = false;
$this->isBreakable = true; $this->breakable = true;
$this->isFlowable = false; $this->isFlowable = false;
$this->isTransparent = true; $this->isTransparent = true;
$this->isReplaceable = false; $this->isReplaceable = false;

View File

@ -29,7 +29,7 @@ class AirBlock extends TransparentBlock{
public function __construct(){ public function __construct(){
parent::__construct(AIR, 0, "Air"); parent::__construct(AIR, 0, "Air");
$this->isActivable = false; $this->isActivable = false;
$this->isBreakable = false; $this->breakable = false;
$this->isFlowable = true; $this->isFlowable = true;
$this->isTransparent = true; $this->isTransparent = true;
$this->isReplaceable = true; $this->isReplaceable = true;

View File

@ -29,7 +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; $this->breakable = false;
} }
} }

View File

@ -28,7 +28,14 @@ the Free Software Foundation, either version 3 of the License, or
class BedrockBlock extends SolidBlock{ class BedrockBlock extends SolidBlock{
public function __construct(){ public function __construct(){
parent::__construct(BEDROCK, 0, "Bedrock"); parent::__construct(BEDROCK, 0, "Bedrock");
$this->isBreakable = true;//$this->isBreakable = false; $this->breakable = false;
}
public function isBreakable(Item $item, Player $player){
if($player->gamemode === 1){
return true;
}
return false;
} }
} }