pocketmine\Player::setGamemode PHP Method

setGamemode() public method

Sets the gamemode, and if needed, kicks the Player.
public setGamemode ( integer $gm ) : boolean
$gm integer
return boolean
    public function setGamemode(int $gm)
    {
        if ($gm < 0 or $gm > 3 or $this->gamemode === $gm) {
            return false;
        }
        $this->server->getPluginManager()->callEvent($ev = new PlayerGameModeChangeEvent($this, $gm));
        if ($ev->isCancelled()) {
            return false;
        }
        if ($this->server->autoClearInv) {
            $this->inventory->clearAll();
        }
        $this->gamemode = $gm;
        $this->allowFlight = $this->isCreative();
        if ($this->isSpectator()) {
            $this->despawnFromAll();
        } else {
            $this->spawnToAll();
        }
        $this->namedtag->playerGameType = new IntTag("playerGameType", $this->gamemode);
        /*$spawnPosition = $this->getSpawn();
        
        		$pk = new StartGamePacket();
        		$pk->seed = -1;
        		$pk->x = $this->x;
        		$pk->y = $this->y;
        		$pk->z = $this->z;
        		$pk->spawnX = (int)$spawnPosition->x;
        		$pk->spawnY = (int)$spawnPosition->y;
        		$pk->spawnZ = (int)$spawnPosition->z;
        		$pk->generator = 1; //0 old, 1 infinite, 2 flat
        		$pk->gamemode = $this->gamemode & 0x01;
        		$pk->eid = 0;*/
        $pk = new SetPlayerGameTypePacket();
        $pk->gamemode = $this->gamemode & 0x1;
        $this->dataPacket($pk);
        $this->sendSettings();
        if ($this->gamemode === Player::SPECTATOR) {
            $pk = new ContainerSetContentPacket();
            $pk->windowid = ContainerSetContentPacket::SPECIAL_CREATIVE;
            $this->dataPacket($pk);
        } else {
            $pk = new ContainerSetContentPacket();
            $pk->windowid = ContainerSetContentPacket::SPECIAL_CREATIVE;
            $pk->slots = array_merge(Item::getCreativeItems(), $this->personalCreativeItems);
            $this->dataPacket($pk);
        }
        $this->inventory->sendContents($this);
        $this->inventory->sendContents($this->getViewers());
        $this->inventory->sendHeldItem($this->hasSpawned);
        return true;
    }

Usage Example

Exemplo n.º 1
15
 private function checkInVIP(Player $player)
 {
     //if ($player->getLevel ()->getName () === $this->plugin->vipLevelName) {
     if (isset($this->vips[$player->getName()])) {
         $this->plugin->log("[HG] VIPPlayerListener->VIPCheck-IN: " . $player->getName());
         return true;
     } else {
         $vip = $this->plugin->profileManager->isPlayerVIP($player->getName());
         if (!$vip) {
             return false;
         }
         if (!$player->isOp()) {
             if (!$player->isSurvival()) {
                 $player->setGamemode(Player::SURVIVAL);
             }
         }
         $this->vips[$player->getName()] = $player->getName();
         $this->plugin->log("[HG] VIPPlayerListener->VIPCheck-IN: " . $player->getName());
         return true;
     }
     //}
     return false;
 }
All Usage Examples Of pocketmine\Player::setGamemode
Player