Instant Break protection

This commit is contained in:
Shoghi Cervantes Pueyo 2013-03-05 17:16:26 +01:00
parent 0ab5fc8fcd
commit 845e48db66
3 changed files with 27 additions and 15 deletions

View File

@ -142,7 +142,7 @@ class BlockAPI{
return $output; return $output;
} }
private function cancelAction(Block $block){ private function cancelAction(Block $block, Player $player){
$this->server->api->dhandle("block.change", array( $this->server->api->dhandle("block.change", array(
"x" => $block->x, "x" => $block->x,
"y" => $block->y, "y" => $block->y,
@ -158,16 +158,17 @@ class BlockAPI{
$target = $this->getBlock($vector); $target = $this->getBlock($vector);
$item = $player->equipment; $item = $player->equipment;
if(!$target->isBreakable($item, $player) or $this->server->gamemode === ADVENTURE){ if(!$target->isBreakable($item, $player) or $player->gamemode === ADVENTURE or ($player->lastBreak + $target->getBreakTime($item, $player)) >= microtime(true)){
return $this->cancelAction($target); return $this->cancelAction($target, $player);
} }
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){
$player->lastBreak = microtime(true);
$drops = $target->getDrops($item, $player); $drops = $target->getDrops($item, $player);
$target->onBreak($this, $item, $player); $target->onBreak($this, $item, $player);
}else{ }else{
return $this->cancelAction($target); return $this->cancelAction($target, $player);
} }
@ -213,8 +214,8 @@ class BlockAPI{
$item = $player->equipment; $item = $player->equipment;
if($target->getID() === AIR){ //If no block exists if($target->getID() === AIR){ //If no block exists
$this->cancelAction($target); $this->cancelAction($target, $player);
return $this->cancelAction($block); return $this->cancelAction($block, $player);
} }
if($target->isActivable === true){ if($target->isActivable === true){
@ -222,8 +223,8 @@ class BlockAPI{
return false; return false;
} }
} }
if($this->server->gamemode === ADVENTURE){ //Adventure mode!! if($player->gamemode === ADVENTURE){ //Adventure mode!!
return $this->cancelAction($block); return $this->cancelAction($block, $player);
} }
/*if(isset(Material::$activable[$target[0]])){ /*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)){ 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()){ if($item->isPlaceable()){
$hand = $item->getBlock(); $hand = $item->getBlock();
}else{ }else{
return $this->cancelAction($block); return $this->cancelAction($block, $player);
} }
if(!($block->isReplaceable === true or ($hand->getID() === SLAB and $block->getID() === SLAB))){ 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)){ 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){ 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){ }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){ if($hand->getID() === SIGN_POST or $hand->getID() === WALL_POST){
$t = $this->server->api->tileentity->addSign($block->x, $block->y, $block->z); $t = $this->server->api->tileentity->addSign($block->x, $block->y, $block->z);

View File

@ -50,12 +50,14 @@ class Player{
public $armor; public $armor;
var $loggedIn = false; var $loggedIn = false;
public $gamemode; public $gamemode;
public $lastBreak;
private $chunksLoaded = array(); private $chunksLoaded = array();
private $chunksOrder = array(); private $chunksOrder = array();
function __construct(PocketMinecraftServer $server, $clientID, $ip, $port, $MTU){ function __construct(PocketMinecraftServer $server, $clientID, $ip, $port, $MTU){
$this->MTU = $MTU; $this->MTU = $MTU;
$this->server = $server; $this->server = $server;
$this->lastBreak = microtime(true);
$this->clientID = $clientID; $this->clientID = $clientID;
$this->CID = $this->server->clientID($ip, $port); $this->CID = $this->server->clientID($ip, $port);
$this->ip = $ip; $this->ip = $ip;

View File

@ -137,7 +137,8 @@ abstract class Block extends Vector3{
); );
protected $id; protected $id;
protected $meta; protected $meta;
protected $name = ""; protected $name;
protected $breakTime;
public $isActivable = false; public $isActivable = false;
public $breakable = true; public $breakable = true;
public $isFlowable = false; public $isFlowable = false;
@ -155,6 +156,7 @@ abstract class Block extends Vector3{
$this->id = (int) $id; $this->id = (int) $id;
$this->meta = (int) $meta; $this->meta = (int) $meta;
$this->name = $name; $this->name = $name;
$this->breakTime = 0.25;
} }
public function getName(){ 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(){ final public function __toString(){
return "Block ". $this->name ." (".$this->id.":".$this->meta.")"; return "Block ". $this->name ." (".$this->id.":".$this->meta.")";
} }