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;
}