From 845e48db6611c41af99d9fe727157d387a0bd624 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Tue, 5 Mar 2013 17:16:26 +0100 Subject: [PATCH] Instant Break protection --- src/API/BlockAPI.php | 29 +++++++++++++++-------------- src/Player.php | 2 ++ src/material/Block.php | 11 ++++++++++- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index aa1fc6ac0..e977ca843 100644 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -142,7 +142,7 @@ class BlockAPI{ return $output; } - private function cancelAction(Block $block){ + private function cancelAction(Block $block, Player $player){ $this->server->api->dhandle("block.change", array( "x" => $block->x, "y" => $block->y, @@ -158,16 +158,17 @@ class BlockAPI{ $target = $this->getBlock($vector); $item = $player->equipment; - if(!$target->isBreakable($item, $player) or $this->server->gamemode === ADVENTURE){ - return $this->cancelAction($target); + if(!$target->isBreakable($item, $player) or $player->gamemode === ADVENTURE or ($player->lastBreak + $target->getBreakTime($item, $player)) >= microtime(true)){ + return $this->cancelAction($target, $player); } if($this->server->api->dhandle("player.block.break", array("player" => $player, "target" => $target, "item" => $item)) !== false){ + $player->lastBreak = microtime(true); $drops = $target->getDrops($item, $player); $target->onBreak($this, $item, $player); }else{ - return $this->cancelAction($target); + return $this->cancelAction($target, $player); } @@ -213,8 +214,8 @@ class BlockAPI{ $item = $player->equipment; if($target->getID() === AIR){ //If no block exists - $this->cancelAction($target); - return $this->cancelAction($block); + $this->cancelAction($target, $player); + return $this->cancelAction($block, $player); } if($target->isActivable === true){ @@ -222,8 +223,8 @@ class BlockAPI{ return false; } } - if($this->server->gamemode === ADVENTURE){ //Adventure mode!! - return $this->cancelAction($block); + if($player->gamemode === ADVENTURE){ //Adventure mode!! + return $this->cancelAction($block, $player); } /*if(isset(Material::$activable[$target[0]])){ @@ -264,27 +265,27 @@ class BlockAPI{ } if($item->isActivable === true and $item->onActivate($this, $player, $block, $target, $face, $fx, $fy, $fz)){ - return $this->cancelAction($block); + return $this->cancelAction($block, $player); } if($item->isPlaceable()){ $hand = $item->getBlock(); }else{ - return $this->cancelAction($block); + return $this->cancelAction($block, $player); } if(!($block->isReplaceable === true or ($hand->getID() === SLAB and $block->getID() === SLAB))){ - return $this->cancelAction($block); + return $this->cancelAction($block, $player); } 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, $player); //Entity in block } if($this->server->api->dhandle("player.block.place", array("player" => $player, "block" => $block, "target" => $target, "item" => $item)) === false){ - return $this->cancelAction($block); + return $this->cancelAction($block, $player); }elseif($hand->place($this, $item, $player, $block, $target, $face, $fx, $fy, $fz) === false){ - return $this->cancelAction($block); + return $this->cancelAction($block, $player); } if($hand->getID() === SIGN_POST or $hand->getID() === WALL_POST){ $t = $this->server->api->tileentity->addSign($block->x, $block->y, $block->z); diff --git a/src/Player.php b/src/Player.php index 351b3e119..e5e133326 100644 --- a/src/Player.php +++ b/src/Player.php @@ -50,12 +50,14 @@ class Player{ public $armor; var $loggedIn = false; public $gamemode; + public $lastBreak; private $chunksLoaded = array(); private $chunksOrder = array(); function __construct(PocketMinecraftServer $server, $clientID, $ip, $port, $MTU){ $this->MTU = $MTU; $this->server = $server; + $this->lastBreak = microtime(true); $this->clientID = $clientID; $this->CID = $this->server->clientID($ip, $port); $this->ip = $ip; diff --git a/src/material/Block.php b/src/material/Block.php index 0a12fb423..7ceec768c 100644 --- a/src/material/Block.php +++ b/src/material/Block.php @@ -137,7 +137,8 @@ abstract class Block extends Vector3{ ); protected $id; protected $meta; - protected $name = ""; + protected $name; + protected $breakTime; public $isActivable = false; public $breakable = true; public $isFlowable = false; @@ -155,6 +156,7 @@ abstract class Block extends Vector3{ $this->id = (int) $id; $this->meta = (int) $meta; $this->name = $name; + $this->breakTime = 0.25; } public function getName(){ @@ -186,6 +188,13 @@ abstract class Block extends Vector3{ } } + public function getBreakTime(Item $item, Player $player){ + if($player->gamemode === CREATIVE){ + return 0.25; + } + return $this->breakTime; + } + final public function __toString(){ return "Block ". $this->name ." (".$this->id.":".$this->meta.")"; }