Player creative check refactor

This commit is contained in:
Shoghi Cervantes 2014-08-31 10:49:20 +02:00
parent 5a756d215d
commit eff63a661e

View File

@ -1040,7 +1040,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
0x80000000 ? 0x80000000 ?
*/ */
$flags = 0; $flags = 0;
if(($this->gamemode & 0x02) === 0x02){ if($this->isAdventure()){
$flags |= 0x01; //Do not allow placing/breaking blocks, adventure mode $flags |= 0x01; //Do not allow placing/breaking blocks, adventure mode
} }
@ -1053,6 +1053,10 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->dataPacket($pk); $this->dataPacket($pk);
} }
public function isSurvival(){
return ($this->gamemode & 0x01) === 0;
}
public function isCreative(){ public function isCreative(){
return ($this->gamemode & 0x01) > 0; return ($this->gamemode & 0x01) > 0;
} }
@ -1090,7 +1094,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$item = $entity->getItem(); $item = $entity->getItem();
if($item instanceof Item){ if($item instanceof Item){
if(($this->gamemode & 0x01) === 0 and !$this->inventory->canAddItem($item)){ if($this->isSurvival() and !$this->inventory->canAddItem($item)){
continue; continue;
} }
@ -1263,7 +1267,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
return; return;
} }
if(($this->gamemode & 0x01) === 0x01){ if($this->isCreative()){
$this->inventory->setHeldItemSlot(0); $this->inventory->setHeldItemSlot(0);
$this->inventory->setItemInHand(Item::get(Item::STONE, 0, 1)); $this->inventory->setItemInHand(Item::get(Item::STONE, 0, 1));
}else{ }else{
@ -1380,7 +1384,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$diff = $diffX ** 2 + $diffY ** 2 + $diffZ ** 2; $diff = $diffX ** 2 + $diffY ** 2 + $diffZ ** 2;
if($diff > 0.0625 and !$this->isSleeping() and ($this->gamemode & 0x01) === 0){ if($diff > 0.0625 and !$this->isSleeping() and $this->isSurvival()){
$revert = true; $revert = true;
$this->server->getLogger()->warning($this->getName()." moved wrongly!"); $this->server->getLogger()->warning($this->getName()." moved wrongly!");
} }
@ -1411,7 +1415,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$packet->slot -= 9; //Get real block slot $packet->slot -= 9; //Get real block slot
} }
if(($this->gamemode & 0x01) === 1){ //Creative mode match if($this->isCreative()){ //Creative mode match
$item = Item::get($packet->item, $packet->meta, 1); $item = Item::get($packet->item, $packet->meta, 1);
$slot = $this->getCreativeBlock($item); $slot = $this->getCreativeBlock($item);
}else{ }else{
@ -1420,7 +1424,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
} }
if($packet->slot === -1){ //Air if($packet->slot === -1){ //Air
if(($this->gamemode & 0x01) === Player::CREATIVE){ if($this->isCreative()){
$found = false; $found = false;
for($i = 0; $i < $this->inventory->getHotbarSize(); ++$i){ for($i = 0; $i < $this->inventory->getHotbarSize(); ++$i){
if($this->inventory->getHotbarSlotIndex($i) === -1){ if($this->inventory->getHotbarSlotIndex($i) === -1){
@ -1440,7 +1444,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
}elseif(!isset($item) or $slot === -1 or $item->getID() !== $packet->item or $item->getDamage() !== $packet->meta){ // packet error or not implemented }elseif(!isset($item) or $slot === -1 or $item->getID() !== $packet->item or $item->getDamage() !== $packet->meta){ // packet error or not implemented
$this->inventory->sendContents($this); $this->inventory->sendContents($this);
break; break;
}elseif(($this->gamemode & 0x01) === Player::CREATIVE){ }elseif($this->isCreative()){
$item = Item::get( $item = Item::get(
Block::$creative[$slot][0], Block::$creative[$slot][0],
Block::$creative[$slot][1], Block::$creative[$slot][1],
@ -1495,7 +1499,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
if($blockVector->distance($this) > 10){ if($blockVector->distance($this) > 10){
}elseif(($this->gamemode & 0x01) === 1){ }elseif($this->isCreative()){
$item = $this->inventory->getItemInHand(); $item = $this->inventory->getItemInHand();
if($this->getLevel()->useItemOn($blockVector, $item, $packet->face, $packet->fx, $packet->fy, $packet->fz, $this) === true){ if($this->getLevel()->useItemOn($blockVector, $item, $packet->face, $packet->fx, $packet->fy, $packet->fz, $this) === true){
break; break;
@ -1550,7 +1554,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
//if($this->entity->inAction === true){ //if($this->entity->inAction === true){
if($this->inventory->getItemInHand()->getID() === Item::BOW){ if($this->inventory->getItemInHand()->getID() === Item::BOW){
$bow = $this->inventory->getItemInHand(); $bow = $this->inventory->getItemInHand();
if(($this->gamemode & 0x01) === 0){ if($this->isSurvival()){
if(!$this->inventory->contains(Item::get(Item::ARROW, 0, 1))){ if(!$this->inventory->contains(Item::get(Item::ARROW, 0, 1))){
$this->inventory->sendContents($this); $this->inventory->sendContents($this);
return; return;
@ -1583,7 +1587,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
if($ev->isCancelled()){ if($ev->isCancelled()){
$arrow->kill(); $arrow->kill();
}else{ }else{
if(($this->gamemode & 0x01) === 0){ if($this->isSurvival()){
$this->inventory->removeItem(Item::get(Item::ARROW, 0, 1)); $this->inventory->removeItem(Item::get(Item::ARROW, 0, 1));
$bow->setDamage($bow->getDamage() + 1); $bow->setDamage($bow->getDamage() + 1);
if($bow->getDamage() >= 385){ if($bow->getDamage() >= 385){
@ -1612,14 +1616,14 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$vector = new Vector3($packet->x, $packet->y, $packet->z); $vector = new Vector3($packet->x, $packet->y, $packet->z);
if(($this->gamemode & 0x01) === 1){ if($this->isCreative()){
$item = $this->inventory->getItemInHand(); $item = $this->inventory->getItemInHand();
}else{ }else{
$item = clone $this->inventory->getItemInHand(); $item = clone $this->inventory->getItemInHand();
} }
if($this->getLevel()->useBreakOn($vector, $item, $this) === true){ if($this->getLevel()->useBreakOn($vector, $item, $this) === true){
if(($this->gamemode & 0x01) === 0){ if($this->isSurvival()){
$this->inventory->setItemInHand($item); $this->inventory->setItemInHand($item);
$this->inventory->sendHeldItem($this->hasSpawned); $this->inventory->sendHeldItem($this->hasSpawned);
} }
@ -1781,7 +1785,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
} }
$this->server->getPluginManager()->callEvent($ev); $this->server->getPluginManager()->callEvent($ev);
if($ev->isCancelled()){ if($ev->isCancelled()){
if($item->isTool() and ($this->gamemode & 0x01) === 0){ if($item->isTool() and $this->isSurvival()){
$this->inventory->sendContents($this); $this->inventory->sendContents($this);
} }
break; break;
@ -1789,7 +1793,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$target->attack($ev->getFinalDamage(), $ev); $target->attack($ev->getFinalDamage(), $ev);
if($item->isTool() and ($this->gamemode & 0x01) === 0){ if($item->isTool() and $this->isSurvival()){
if($item->useOn($target) and $item->getDamage() >= $item->getMaxDurability()){ if($item->useOn($target) and $item->getDamage() >= $item->getMaxDurability()){
$this->inventory->setItemInHand(Item::get(Item::AIR, 0, 1)); $this->inventory->setItemInHand(Item::get(Item::AIR, 0, 1));
} }
@ -1971,7 +1975,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
if($packet->slot > $this->inventory->getSize()){ if($packet->slot > $this->inventory->getSize()){
break; break;
} }
if(($this->gamemode & 0x01) === Player::CREATIVE){ if($this->isCreative()){
if($this->getCreativeBlock($packet->item) !== -1){ if($this->getCreativeBlock($packet->item) !== -1){
$this->inventory->setItem($packet->slot, $packet->item); $this->inventory->setItem($packet->slot, $packet->item);
$this->inventory->setHotbarSlotIndex($packet->slot, $packet->slot); //links $hotbar[$packet->slot] to $slots[$packet->slot] $this->inventory->setHotbarSlotIndex($packet->slot, $packet->slot); //links $hotbar[$packet->slot] to $slots[$packet->slot]