diff --git a/src/Player.php b/src/Player.php index 8876d3ab..865282e1 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1513,8 +1513,17 @@ class Player{ $this->entity->setHealth(min(20, $this->entity->health + 1), "regeneration"); } } - } + + public function sendPing() { + $pk = new PingPacket; + $pk->time = intdiv(hrtime(true), 1_000_000); + $this->directDataPacket($pk); + } + + public function getPing() { + return $this->lastPing; + } public function handleDataPacket(RakNetDataPacket $packet) { @@ -1708,9 +1717,9 @@ class Player{ break; case ProtocolInfo7::PONG_PACKET: $currentTime = intdiv(hrtime(true), 1_000_000); - if($currentTime > $packet->ptime){ - $this->lastPing = $currentTime - $packet->ptime; - } + if ($currentTime > $packet->ptime) { + $this->lastPing = $currentTime - $packet->ptime; + } break; case ProtocolInfo7::PING_PACKET: $pk = new PongPacket; @@ -2700,15 +2709,15 @@ class Player{ console("[DEBUG] Unhandled 0x" . dechex($packet->pid()) . " data packet for " . $this->username . " (" . $this->clientID . "): " . print_r($packet, true), true, true, 2); break; } - }elseif ($this->PROTOCOL >= ProtocolInfo9::CURRENT_PROTOCOL_9 && $this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ - switch($packet->pid()){ + } elseif ($this->PROTOCOL >= ProtocolInfo9::CURRENT_PROTOCOL_9 && $this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12) { + switch ($packet->pid()) { case 0x01: break; case ProtocolInfo9::PONG_PACKET: $currentTime = intdiv(hrtime(true), 1_000_000); - if($currentTime > $packet->ptime){ - $this->lastPing = $currentTime - $packet->ptime; - } + if ($currentTime > $packet->ptime) { + $this->lastPing = $currentTime - $packet->ptime; + } break; case ProtocolInfo9::PING_PACKET: $pk = new PongPacket; @@ -2720,7 +2729,7 @@ class Player{ $this->close("client disconnect"); break; case ProtocolInfo9::CLIENT_CONNECT_PACKET: - if($this->loggedIn === true){ + if ($this->loggedIn === true) { break; } $pk = new ServerHandshakePacket; @@ -2730,70 +2739,70 @@ class Player{ $this->dataPacket($pk); break; case ProtocolInfo9::CLIENT_HANDSHAKE_PACKET: - if($this->loggedIn === true){ + if ($this->loggedIn === true) { break; } break; case ProtocolInfo9::LOGIN_PACKET: - if($this->loggedIn === true){ + if ($this->loggedIn === true) { break; } $this->username = $packet->username; $this->iusername = strtolower($this->username); $this->loginData = ["clientId" => $packet->clientId, "loginData" => $packet->loginData]; - if(count($this->server->clients) > $this->server->maxClients and !$this->server->api->ban->isOp($this->iusername)){ + if (count($this->server->clients) > $this->server->maxClients and !$this->server->api->ban->isOp($this->iusername)) { $this->close("server is full!", false); return; } - if(preg_match('#[^a-zA-Z0-9_]#', $this->username) > 0 || $this->username === "" || $this->iusername === "rcon" || $this->iusername === "console" || $this->iusername === "server" || strlen($this->iusername) > 16){ + if (preg_match('#[^a-zA-Z0-9_]#', $this->username) > 0 || $this->username === "" || $this->iusername === "rcon" || $this->iusername === "console" || $this->iusername === "server" || strlen($this->iusername) > 16) { $this->close("Bad username", false); return; } - if($this->server->api->handle("player.connect", $this) === false){ + if ($this->server->api->handle("player.connect", $this) === false) { $this->close("Unknown reason", false); return; } - if($this->server->whitelist === true and !$this->server->api->ban->inWhitelist($this->iusername)){ + if ($this->server->whitelist === true and !$this->server->api->ban->inWhitelist($this->iusername)) { $this->close("Server is white-listed", false); return; - }elseif($this->server->api->ban->isBanned($this->iusername) or $this->server->api->ban->isIPBanned($this->ip)){ + } elseif ($this->server->api->ban->isBanned($this->iusername) or $this->server->api->ban->isIPBanned($this->ip)) { $this->close("You are banned!", false); return; } $this->loggedIn = true; - if(!isset($this->CID) or $this->CID == null){ + if (!isset($this->CID) or $this->CID == null) { console("[DEBUG] Player " . $this->username . " does not have a CID", true, true, 2); $this->CID = Utils::readLong(Utils::getRandomBytes(8, false)); } $u = $this->server->api->player->get($this->iusername, false); - if($u !== false){ + if ($u !== false) { $u = $this->server->clients[$this->CID]; $u->close("this player already in game"); } $this->server->api->player->add($this->CID); - if($this->server->api->handle("player.join", $this) === false){ + if ($this->server->api->handle("player.join", $this) === false) { $this->close("join cancelled", false); return; } - if(!($this->data instanceof Config)){ + if (!($this->data instanceof Config)) { $this->close("no config created", false); return; } $this->auth = true; - if(!$this->data->exists("inventory") or ($this->gamemode & 0x01) === 0x01){ - if(($this->gamemode & 0x01) === 0x01){ + if (!$this->data->exists("inventory") or ($this->gamemode & 0x01) === 0x01) { + if (($this->gamemode & 0x01) === 0x01) { $inv = []; - if(($this->gamemode & 0x02) === 0x02){ - foreach(BlockAPI::$creative as $item){ + if (($this->gamemode & 0x02) === 0x02) { + foreach (BlockAPI::$creative as $item) { $inv[] = [0, 0, 1]; } - }else{ - foreach(BlockAPI::$creative as $item){ + } else { + foreach (BlockAPI::$creative as $item) { $inv[] = [$item[0], $item[1], 1]; } } @@ -2803,15 +2812,15 @@ class Player{ $this->achievements = $this->data->get("achievements"); $this->data->set("caseusername", $this->username); $this->inventory = []; - foreach($this->data->get("inventory") as $slot => $item){ - if(!is_array($item) or count($item) < 3){ + foreach ($this->data->get("inventory") as $slot => $item) { + if (!is_array($item) or count($item) < 3) { $item = [AIR, 0, 0]; } $this->inventory[$slot] = BlockAPI::getItem($item[0], $item[1], $item[2]); } $this->armor = []; - foreach($this->data->get("armor") as $slot => $item){ + foreach ($this->data->get("armor") as $slot => $item) { $this->armor[$slot] = BlockAPI::getItem($item[0], $item[1], $item[0] === 0 ? 0 : 1); } @@ -2835,23 +2844,23 @@ class Player{ $pk->eid = 0; $this->dataPacket($pk); - if(($this->gamemode & 0x01) === 0x01){ + if (($this->gamemode & 0x01) === 0x01) { $this->slot = 0; $this->hotbar = []; - }elseif($this->data->exists("hotbar")){ + } elseif ($this->data->exists("hotbar")) { $this->hotbar = $this->data->get("hotbar"); $this->slot = $this->hotbar[0]; - }else{ + } else { $this->slot = 0; $this->hotbar = [0, 1, 2, 3, 4, 5, 6, 7, 8]; } - for($i = 0; $i < count($this->hotbar); ++$i){ - if($this->hotbar[$i] > 36) $this->hotbar[$i] = -1; //XXX unsafe? - if($this->hotbar[$i] < -1) $this->hotbar[$i] = -1; + for ($i = 0; $i < count($this->hotbar); ++$i) { + if ($this->hotbar[$i] > 36) $this->hotbar[$i] = -1; //XXX unsafe? + if ($this->hotbar[$i] < -1) $this->hotbar[$i] = -1; } - if($this->data->exists("slot-count")){ + if ($this->data->exists("slot-count")) { $this->slotCount = $this->data->get("slot-count"); - }else{ + } else { $this->data->set("slot-count", $this->slotCount); } @@ -2861,13 +2870,13 @@ class Player{ $this->entity->x = $this->data->get("position")["x"]; $this->entity->y = $this->data->get("position")["y"]; $this->entity->z = $this->data->get("position")["z"]; - if(($level = $this->server->api->level->get($this->data->get("spawn")["level"])) !== false){ + if (($level = $this->server->api->level->get($this->data->get("spawn")["level"])) !== false) { $this->spawnPosition = new Position($this->data->get("spawn")["x"], $this->data->get("spawn")["y"], $this->data->get("spawn")["z"], $level); $pk = new SetSpawnPositionPacket; - $pk->x = (int) $this->spawnPosition->x; - $pk->y = (int) $this->spawnPosition->y; - $pk->z = (int) $this->spawnPosition->z; + $pk->x = (int)$this->spawnPosition->x; + $pk->y = (int)$this->spawnPosition->y; + $pk->z = (int)$this->spawnPosition->z; $this->dataPacket($pk); } $this->entity->check = false; @@ -2896,12 +2905,12 @@ class Player{ console("[INFO] " . FORMAT_AQUA . $this->username . FORMAT_RESET . "[/" . $this->ip . ":" . $this->port . "] logged in with entity id " . $this->eid . " at (" . $this->entity->level->getName() . ", " . round($this->entity->x, 2) . ", " . round($this->entity->y, 2) . ", " . round($this->entity->z, 2) . ")"); break; case ProtocolInfo9::READY_PACKET: - if($this->loggedIn === false){ + if ($this->loggedIn === false) { break; } - switch($packet->status){ + switch ($packet->status) { case 1: //Spawn!! - if($this->spawned !== false){ + if ($this->spawned !== false) { break; } @@ -2919,8 +2928,8 @@ class Player{ //$this->server->schedule(2, [$this->entity, "updateMovement"], [], true); $this->sendArmor(); $array = explode("@n", (string)$this->server->motd); - foreach($array as $msg){ - $this->sendChat($msg."\n"); + foreach ($array as $msg) { + $this->sendChat($msg . "\n"); } $this->sendInventory(); @@ -2936,34 +2945,34 @@ class Player{ } break; case ProtocolInfo9::MOVE_PLAYER_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } - if($this->isSleeping) break; - if(($this->entity instanceof Entity) and $packet->messageIndex > $this->lastMovement){ + if ($this->isSleeping) break; + if (($this->entity instanceof Entity) and $packet->messageIndex > $this->lastMovement) { $this->lastMovement = $packet->messageIndex; $newPos = new Vector3($packet->x, $packet->y, $packet->z); - if($this->forceMovement instanceof Vector3){ - if($this->forceMovement->distance($newPos) <= 0.7){ + if ($this->forceMovement instanceof Vector3) { + if ($this->forceMovement->distance($newPos) <= 0.7) { $this->forceMovement = false; - }else{ + } else { $this->teleport($this->forceMovement, $this->entity->yaw, $this->entity->pitch, false); break; } } $speed = $this->entity->getSpeedMeasure(); - if($this->blocked === true or ($this->server->api->getProperty("allow-flight") !== true and (($speed > 9 and ($this->gamemode & 0x01) === 0x00) or $speed > 20 or $this->entity->distance($newPos) > 7)) or $this->server->api->handle("player.move", $this->entity) === false){ - if($this->lastCorrect instanceof Vector3 && !$this->entity->dead){ + if ($this->blocked === true or ($this->server->api->getProperty("allow-flight") !== true and (($speed > 9 and ($this->gamemode & 0x01) === 0x00) or $speed > 20 or $this->entity->distance($newPos) > 7)) or $this->server->api->handle("player.move", $this->entity) === false) { + if ($this->lastCorrect instanceof Vector3 && !$this->entity->dead) { $this->teleport($this->lastCorrect, $this->entity->yaw, $this->entity->pitch, false); } - }else{ + } else { $this->entity->setPosition($newPos, $packet->yaw, $packet->pitch, $packet->bodyYaw); } $this->entity->updateAABB(); } break; case ProtocolInfo9::PLAYER_EQUIPMENT_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } $packet->eid = $this->eid; @@ -2972,51 +2981,51 @@ class Player{ $data["eid"] = $packet->eid; $data["player"] = $this; - if($packet->slot === 0){ + if ($packet->slot === 0) { $data["slot"] = -1; $data["item"] = BlockAPI::getItem(AIR, 0, 0); - if($this->server->handle("player.equipment.change", $data) !== false){ + if ($this->server->handle("player.equipment.change", $data) !== false) { $this->slot = -1; } break; - }else if($packet->slot > 0){ + } else if ($packet->slot > 0) { $packet->slot -= 9; } - if(($this->gamemode & 0x01) === SURVIVAL){ + if (($this->gamemode & 0x01) === SURVIVAL) { $packet->slot = false; - foreach($this->inventory as $slot => $item) { + foreach ($this->inventory as $slot => $item) { if ($item->getID() === $packet->item and $item->getMetadata() === $packet->meta) { $packet->slot = $slot; break; } } - if($packet->slot === false){ + if ($packet->slot === false) { break; } $data["item"] = $this->getSlot($packet->slot); - if(!($data["item"] instanceof Item)){ + if (!($data["item"] instanceof Item)) { break; } - }elseif(($this->gamemode & 0x01) === CREATIVE){ + } elseif (($this->gamemode & 0x01) === CREATIVE) { $packet->slot = 0; $item = BlockAPI::getItem($packet->item, $packet->meta); $this->setSlot(0, $item); $data["item"] = $item; - }else{ + } else { break;//????? } $data["slot"] = $packet->slot; - if($this->server->handle("player.equipment.change", $data) !== false){ - if(!Player::$experimentalHotbar) $this->slot = $packet->slot; - }else{ + if ($this->server->handle("player.equipment.change", $data) !== false) { + if (!Player::$experimentalHotbar) $this->slot = $packet->slot; + } else { //$this->sendInventorySlot($packet->slot); $this->sendInventory(); } - if($this->entity->inAction === true){ + if ($this->entity->inAction === true) { $this->entity->inAction = false; $this->entity->inActionCounter = 0; $this->entity->updateMetadata(); @@ -3028,13 +3037,13 @@ class Player{ //$this->lastChunk = [$packet->chunkX, $packet->chunkZ]; break; case ProtocolInfo9::USE_ITEM_PACKET: - if(!($this->entity instanceof Entity)){ + if (!($this->entity instanceof Entity)) { break; } $blockVector = new Vector3($packet->x, $packet->y, $packet->z); - if(($this->spawned === false or $this->blocked === true) and $packet->face >= 0 and $packet->face <= 5){ + if (($this->spawned === false or $this->blocked === true) and $packet->face >= 0 and $packet->face <= 5) { $target = $this->level->getBlock($blockVector); $block = $target->getSide($packet->face); @@ -3074,18 +3083,18 @@ class Player{ $data["posY"] = $packet->posY; $data["posZ"] = $packet->posZ; - if($packet->face >= 0 and $packet->face <= 5){ //Use Block, place - if($this->entity->inAction === true){ + if ($packet->face >= 0 and $packet->face <= 5) { //Use Block, place + if ($this->entity->inAction === true) { $this->entity->inAction = false; $this->entity->inActionCounter = 0; $this->entity->updateMetadata(); } - if($this->blocked === true or ($this->entity->position instanceof Vector3 and $blockVector->distance($this->entity->position) > 10)){ + if ($this->blocked === true or ($this->entity->position instanceof Vector3 and $blockVector->distance($this->entity->position) > 10)) { - }elseif($this->getSlot($this->slot)->getID() !== $packet->item or ($this->getSlot($this->slot)->isTool() === false and $this->getSlot($this->slot)->getMetadata() !== $packet->meta)){ + } elseif ($this->getSlot($this->slot)->getID() !== $packet->item or ($this->getSlot($this->slot)->isTool() === false and $this->getSlot($this->slot)->getMetadata() !== $packet->meta)) { $this->sendInventorySlot($this->slot); - }else{ + } else { $this->server->api->block->playerBlockAction($this, $blockVector, $packet->face, $packet->fx, $packet->fy, $packet->fz); break; } @@ -3108,17 +3117,17 @@ class Player{ $pk->meta = $block->getMetadata(); $this->dataPacket($pk); break; - }elseif($packet->face === 0xff){ + } elseif ($packet->face === 0xff) { $slotItem = $this->getHeldItem(); - if($slotItem->getID() == SNOWBALL || $slotItem->getID() == EGG){ //TODO better way + if ($slotItem->getID() == SNOWBALL || $slotItem->getID() == EGG) { //TODO better way $x = $packet->x * 0.000030518; $y = $packet->y * 0.000030518; $z = $packet->z * 0.000030518; - $d = sqrt($x*$x + $y*$y + $z*$z); + $d = sqrt($x * $x + $y * $y + $z * $z); - if($d >= 0.0001){ + if ($d >= 0.0001) { $shootX = $x / $d; $shootY = $y / $d; $shootZ = $z / $d; @@ -3135,15 +3144,15 @@ class Player{ "shootZ" => $shootZ ]; - if($slotItem->getID() == EGG){ + if ($slotItem->getID() == EGG) { $e = $this->server->api->entity->add($this->entity->level, ENTITY_OBJECT, OBJECT_EGG, $data); - }else{ + } else { $e = $this->server->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_SNOWBALL, $data); } - if(($this->gamemode & 0x01) == 0x0) { - if($slotItem !== false){ - if($slotItem->count == 1) $this->inventory[$this->slot] = BlockAPI::getItem(AIR, 0, 0); + if (($this->gamemode & 0x01) == 0x0) { + if ($slotItem !== false) { + if ($slotItem->count == 1) $this->inventory[$this->slot] = BlockAPI::getItem(AIR, 0, 0); else $slotItem->count -= 1; //$this->sendInventory(); } @@ -3152,8 +3161,8 @@ class Player{ $this->server->api->entity->spawnToAll($e); } - }else{ - if($this->server->handle("player.action", $data) !== false){ + } else { + if ($this->server->handle("player.action", $data) !== false) { $this->entity->inAction = true; $this->entity->inActionCounter = 0; $this->startAction = microtime(true); @@ -3163,24 +3172,24 @@ class Player{ } break; case ProtocolInfo9::PLAYER_ACTION_PACKET: - if($this->spawned === false or $this->blocked === true){ + if ($this->spawned === false or $this->blocked === true) { break; } $packet->eid = $this->eid; $this->craftingItems = []; $this->toCraft = []; - switch($packet->action){ + switch ($packet->action) { case 5: //Shot arrow - if($this->entity->inAction){ + if ($this->entity->inAction) { $arrowSlot = $this->hasItem(ARROW); - if($this->getSlot($this->slot)->getID() === BOW && (($this->gamemode & 0x01) == 0x1 || $arrowSlot !== false)){ - if($this->startAction !== false){ + if ($this->getSlot($this->slot)->getID() === BOW && (($this->gamemode & 0x01) == 0x1 || $arrowSlot !== false)) { + if ($this->startAction !== false) { $initalPower = $this->entity->inActionCounter; $power = $initalPower / 20; $power = ($power * $power + $power * 2) / 3; - if($power >= 0.1){ - if($power > 1) $power = 1; + if ($power >= 0.1) { + if ($power > 1) $power = 1; $this->server->dhandle("player.shoot", [ "player" => $this, "power" => &$power, @@ -3209,11 +3218,11 @@ class Player{ * args: xvel, yvel, zvel, (power+power)*1.5, 1.0 */ $e->critical = ($power == 1); - $e->shoot($e->speedX, $e->speedY, $e->speedZ, ($power+$power) * 1.5, 1.0); + $e->shoot($e->speedX, $e->speedY, $e->speedZ, ($power + $power) * 1.5, 1.0); $this->server->api->entity->spawnToAll($e); - if(($this->gamemode & 0x01) == 0x0) { + if (($this->gamemode & 0x01) == 0x0) { $bow = $this->getSlot($this->slot); - if(++$bow->meta >= $bow->getMaxDurability()){ + if (++$bow->meta >= $bow->getMaxDurability()) { $this->inventory[$this->slot] = BlockAPI::getItem(AIR, 0, 0); } $this->removeItem(ARROW, 0, 1, false); @@ -3221,7 +3230,7 @@ class Player{ } } } - }else{ //inv desynced, resend + } else { //inv desynced, resend $this->sendInventory(); } } @@ -3236,7 +3245,7 @@ class Player{ break; case ProtocolInfo9::REMOVE_BLOCK_PACKET: $blockVector = new Vector3($packet->x, $packet->y, $packet->z); - if($this->spawned === false or $this->blocked === true or $this->entity->distance($blockVector) > 8){ + if ($this->spawned === false or $this->blocked === true or $this->entity->distance($blockVector) > 8) { $target = $this->level->getBlock($blockVector); $pk = new UpdateBlockPacket; @@ -3253,46 +3262,46 @@ class Player{ $this->server->api->block->playerBlockBreak($this, $blockVector); break; case ProtocolInfo9::PLAYER_ARMOR_EQUIPMENT_PACKET: - if($this->spawned === false or $this->blocked === true){ + if ($this->spawned === false or $this->blocked === true) { break; } $this->craftingItems = []; $this->toCraft = []; $packet->eid = $this->eid; - for($i = 0; $i < 4; ++$i){ + for ($i = 0; $i < 4; ++$i) { $s = $packet->slots[$i]; - if($s === 0 or $s === 255){ + if ($s === 0 or $s === 255) { $s = BlockAPI::getItem(AIR, 0, 0); - }else{ + } else { $s = BlockAPI::getItem($s + 256, 0, 1); } $slot = $this->armor[$i]; - if($slot->getID() !== AIR and $s->getID() === AIR){ + if ($slot->getID() !== AIR and $s->getID() === AIR) { $this->addItem($slot->getID(), $slot->getMetadata(), 1, false); $this->armor[$i] = BlockAPI::getItem(AIR, 0, 0); $packet->slots[$i] = 255; - }elseif($s->getID() !== AIR and $slot->getID() === AIR and ($sl = $this->hasItem($s->getID())) !== false){ + } elseif ($s->getID() !== AIR and $slot->getID() === AIR and ($sl = $this->hasItem($s->getID())) !== false) { $this->armor[$i] = $this->getSlot($sl); $this->setSlot($sl, BlockAPI::getItem(AIR, 0, 0), false); - }elseif($s->getID() !== AIR and $slot->getID() !== AIR and ($slot->getID() !== $s->getID() or $slot->getMetadata() !== $s->getMetadata()) and ($sl = $this->hasItem($s->getID())) !== false){ + } elseif ($s->getID() !== AIR and $slot->getID() !== AIR and ($slot->getID() !== $s->getID() or $slot->getMetadata() !== $s->getMetadata()) and ($sl = $this->hasItem($s->getID())) !== false) { $item = $this->armor[$i]; $this->armor[$i] = $this->getSlot($sl); $this->setSlot($sl, $item, false); - }else{ + } else { $packet->slots[$i] = 255; } } $this->sendArmor(); - if($this->entity->inAction === true){ + if ($this->entity->inAction === true) { $this->entity->inAction = false; $this->entity->inActionCounter = 0; $this->entity->updateMetadata(); } break; case ProtocolInfo9::INTERACT_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } $packet->eid = $this->eid; @@ -3303,28 +3312,28 @@ class Player{ $this->craftingItems = []; $this->toCraft = []; $target = $this->server->api->entity->get($packet->target); - if($target instanceof Entity and $this->entity instanceof Entity and $this->gamemode !== VIEW and $this->blocked === false and ($target instanceof Entity) and $this->entity->distance($target) <= 8){ + if ($target instanceof Entity and $this->entity instanceof Entity and $this->gamemode !== VIEW and $this->blocked === false and ($target instanceof Entity) and $this->entity->distance($target) <= 8) { $data["targetentity"] = $packet->target; $data["entity"] = $this->entity; $data["player"] = $this; - if($this->server->handle("player.interact", $data) !== false){ + if ($this->server->handle("player.interact", $data) !== false) { $target->interactWith($this->entity, $packet->action); } } break; case ProtocolInfo9::ANIMATE_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } $packet->eid = $this->eid; $this->server->api->dhandle("entity.animate", ["eid" => $packet->eid, "entity" => $this->entity, "action" => $packet->action]); break; case ProtocolInfo9::RESPAWN_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } - if(@$this->entity->dead === false){ + if (@$this->entity->dead === false) { break; } $this->craftingItems = []; @@ -3341,18 +3350,18 @@ class Player{ $pk->yaw = $this->entity->yaw; $pk->pitch = $this->entity->pitch; $pk->bodyYaw = $this->entity->headYaw; - foreach($this->entity->level->players as $player){ - if($player->entity->eid != $this->entity->eid){ + foreach ($this->entity->level->players as $player) { + if ($player->entity->eid != $this->entity->eid) { $player->dataPacket(clone $pk); } } - if($this->entity instanceof Entity){ + if ($this->entity instanceof Entity) { $this->entity->fire = 0; $this->entity->air = $this->entity->maxAir; $this->entity->setHealth(20, "respawn", true); $this->entity->updateMetadata(); - }else{ + } else { break; } $this->sendInventory(); @@ -3362,22 +3371,22 @@ class Player{ case ProtocolInfo9::SET_HEALTH_PACKET: //Not used break; case ProtocolInfo9::ENTITY_EVENT_PACKET: - if($this->spawned === false or $this->blocked === true){ + if ($this->spawned === false or $this->blocked === true) { break; } $this->craftingItems = []; $this->toCraft = []; $packet->eid = $this->eid; - if($this->entity->inAction === true){ + if ($this->entity->inAction === true) { $this->entity->inAction = false; $this->entity->inActionCounter = 0; $this->entity->updateMetadata(); } - switch($packet->event){ + switch ($packet->event) { case 9: //Eating $slot = $this->getSlot($this->slot); $foodHeal = Item::getFoodHeal($slot->getID()); - if($this->entity->getHealth() < 20 && $foodHeal != 0){ + if ($this->entity->getHealth() < 20 && $foodHeal != 0) { $pk = new EntityEventPacket; $pk->eid = 0; $pk->event = 9; @@ -3385,10 +3394,10 @@ class Player{ $this->entity->heal($foodHeal, "eating"); --$slot->count; - if($slot->count <= 0){ + if ($slot->count <= 0) { $this->setSlot($this->slot, BlockAPI::getItem(AIR, 0, 0), false); } - if($slot->getID() === MUSHROOM_STEW or $slot->getID() === BEETROOT_SOUP){ + if ($slot->getID() === MUSHROOM_STEW or $slot->getID() === BEETROOT_SOUP) { $this->addItem(BOWL, 0, 1, false); } } @@ -3396,11 +3405,11 @@ class Player{ } break; case ProtocolInfo9::DROP_ITEM_PACKET: - if($this->spawned === false or $this->blocked === true){ + if ($this->spawned === false or $this->blocked === true) { break; } - if($this->gamemode & 0x01 == 1){ + if ($this->gamemode & 0x01 == 1) { ConsoleAPI::warn("{$this->iusername} tried dropping item while in creative!"); return; } @@ -3410,17 +3419,17 @@ class Player{ $packet->item = $this->getSlot($this->slot); $sendOnDrop = false; - if($prevItem->getID() != $packet->item->getID() || $prevItem->getMetadata() != $packet->item->getMetadata()){ - if(count($this->inventory) >= 36){ - foreach($this->inventory as $slot => $item){ - if($item->getID() == 0) goto inv_desync_on_drop9; + if ($prevItem->getID() != $packet->item->getID() || $prevItem->getMetadata() != $packet->item->getMetadata()) { + if (count($this->inventory) >= 36) { + foreach ($this->inventory as $slot => $item) { + if ($item->getID() == 0) goto inv_desync_on_drop9; } $this->toCraft[] = $prevItem; //vanilla drops only result? $this->lastCraft = microtime(true); break; } - }else { + } else { inv_desync_on_drop9: ConsoleAPI::debug("Inventory desync on drop({$this->iusername})"); $sendOnDrop = true; @@ -3432,7 +3441,7 @@ class Player{ $data["unknown"] = $packet->unknown; $data["item"] = $packet->item; $data["player"] = $this; - if($this->blocked === false and $this->server->handle("player.drop", $data) !== false){ + if ($this->blocked === false and $this->server->handle("player.drop", $data) !== false) { $f1 = 0.3; $sX = -sin(($this->entity->yaw / 180) * M_PI) * cos(($this->entity->pitch / 180) * M_PI) * $f1; $sZ = cos(($this->entity->yaw / 180) * M_PI) * cos(($this->entity->pitch / 180) * M_PI) * $f1; @@ -3445,33 +3454,33 @@ class Player{ $sZ += sin($f3) * $f1; $this->server->api->entity->dropRawPos($this->level, $this->entity->x, $this->entity->y - 0.3 + $this->entity->height - 0.12, $this->entity->z, $packet->item, $sX, $sY, $sZ); $this->setSlot($this->slot, BlockAPI::getItem(AIR, 0, 0), $sendOnDrop); - }else{ + } else { $this->sendInventory(); //send if blocked } - if($this->entity->inAction === true){ + if ($this->entity->inAction === true) { $this->entity->inAction = false; $this->entity->inActionCounter = 0; $this->entity->updateMetadata(); } break; case ProtocolInfo9::MESSAGE_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } $this->craftingItems = []; $this->toCraft = []; - if(trim($packet->message) != "" and strlen($packet->message) <= 255){ + if (trim($packet->message) != "" and strlen($packet->message) <= 255) { $message = $packet->message; - if($message[0] === "/"){ //Command - if($this instanceof Player){ + if ($message[0] === "/") { //Command + if ($this instanceof Player) { console("[DEBUG] " . FORMAT_AQUA . $this->username . FORMAT_RESET . " issued server command: " . $message); - }else{ + } else { console("[DEBUG] " . FORMAT_YELLOW . "*" . $this . FORMAT_RESET . " issued server command: " . $message); } $this->server->api->console->run(substr($message, 1), $this); - }else{ + } else { $data = ["player" => $this, "message" => $message]; - if(Utils::hasEmoji($data["message"])){ + if (Utils::hasEmoji($data["message"])) { $this->sendChat("Your message contains illegal characters"); break; } @@ -3480,11 +3489,11 @@ class Player{ // Living::$pathfind = !Living::$pathfind; //} - if($this->server->api->handle("player.chat", $data) !== false){ + if ($this->server->api->handle("player.chat", $data) !== false) { $this->server->send2Discord("<" . $this->username . "> " . $message); - if(isset($data["message"])){ + if (isset($data["message"])) { $this->server->api->chat->send($this, $data["message"]); - }else{ + } else { $this->server->api->chat->send($this, $message); } } @@ -3492,14 +3501,14 @@ class Player{ } break; case ProtocolInfo9::CONTAINER_CLOSE_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } $this->craftingItems = []; $this->toCraft = []; - if(isset($this->windows[$packet->windowid])){ - if(is_array($this->windows[$packet->windowid])){ - foreach($this->windows[$packet->windowid] as $ob){ + if (isset($this->windows[$packet->windowid])) { + if (is_array($this->windows[$packet->windowid])) { + foreach ($this->windows[$packet->windowid] as $ob) { $pk = new TileEventPacket; $pk->x = $ob->x; $pk->y = $ob->y; @@ -3508,7 +3517,7 @@ class Player{ $pk->case2 = 0; $this->server->api->player->broadcastPacket($this->level->players, $pk); } - }elseif($this->windows[$packet->windowid]->class === TILE_CHEST){ + } elseif ($this->windows[$packet->windowid]->class === TILE_CHEST) { $pk = new TileEventPacket; $pk->x = $this->windows[$packet->windowid]->x; $pk->y = $this->windows[$packet->windowid]->y; @@ -3525,36 +3534,36 @@ class Player{ $this->dataPacket($pk); break; case ProtocolInfo9::CONTAINER_SET_SLOT_PACKET: - if($this->spawned === false or $this->blocked === true){ + if ($this->spawned === false or $this->blocked === true) { break; } - if($this->lastCraft <= (microtime(true) - 1)){ - if(isset($this->toCraft[-1])){ + if ($this->lastCraft <= (microtime(true) - 1)) { + if (isset($this->toCraft[-1])) { $this->toCraft = [-1 => $this->toCraft[-1]]; - }else{ + } else { $this->toCraft = []; } $this->craftingItems = []; } - if($packet->windowid === 0){ + if ($packet->windowid === 0) { $craft = false; $slot = $this->getSlot($packet->slot); - if($slot->count >= $packet->item->count and (($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()) or ($packet->item->getID() === AIR and $packet->item->count === 0)) and !isset($this->craftingItems[$packet->slot])){ //Crafting recipe + if ($slot->count >= $packet->item->count and (($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()) or ($packet->item->getID() === AIR and $packet->item->count === 0)) and !isset($this->craftingItems[$packet->slot])) { //Crafting recipe $use = BlockAPI::getItem($slot->getID(), $slot->getMetadata(), $slot->count - $packet->item->count); $this->craftingItems[$packet->slot] = $use; $craft = true; - }elseif($slot->count <= $packet->item->count and ($slot->getID() === AIR or ($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()))){ //Crafting final + } elseif ($slot->count <= $packet->item->count and ($slot->getID() === AIR or ($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()))) { //Crafting final $craftItem = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count - $slot->count); - if(count($this->toCraft) === 0){ + if (count($this->toCraft) === 0) { $this->toCraft[-1] = 0; } $this->toCraft[$packet->slot] = $craftItem; $craft = true; - }elseif(((count($this->toCraft) === 1 and isset($this->toCraft[-1])) or count($this->toCraft) === 0) and $slot->count > 0 and $slot->getID() > AIR and ($slot->getID() !== $packet->item->getID() or $slot->getMetadata() !== $packet->item->getMetadata())){ //Crafting final + } elseif (((count($this->toCraft) === 1 and isset($this->toCraft[-1])) or count($this->toCraft) === 0) and $slot->count > 0 and $slot->getID() > AIR and ($slot->getID() !== $packet->item->getID() or $slot->getMetadata() !== $packet->item->getMetadata())) { //Crafting final $craftItem = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count); - if(count($this->toCraft) === 0){ + if (count($this->toCraft) === 0) { $this->toCraft[-1] = 0; } $use = BlockAPI::getItem($slot->getID(), $slot->getMetadata(), $slot->count); @@ -3563,52 +3572,52 @@ class Player{ $craft = true; } - if($craft === true){ + if ($craft === true) { $this->lastCraft = microtime(true); } - if($craft === true and count($this->craftingItems) > 0 and count($this->toCraft) > 0 and ($recipe = $this->craftItems($this->toCraft, $this->craftingItems, $this->toCraft[-1])) !== true){ - if($recipe === false){ + if ($craft === true and count($this->craftingItems) > 0 and count($this->toCraft) > 0 and ($recipe = $this->craftItems($this->toCraft, $this->craftingItems, $this->toCraft[-1])) !== true) { + if ($recipe === false) { $this->sendInventory(); $this->toCraft = []; - }else{ + } else { $this->toCraft = [-1 => $this->toCraft[-1]]; } $this->craftingItems = []; } - }else{ + } else { $this->toCraft = []; $this->craftingItems = []; } - if(!isset($this->windows[$packet->windowid])){ + if (!isset($this->windows[$packet->windowid])) { break; } - if(is_array($this->windows[$packet->windowid])){ + if (is_array($this->windows[$packet->windowid])) { $tiles = $this->windows[$packet->windowid]; - if($packet->slot >= 0 and $packet->slot < CHEST_SLOTS){ + if ($packet->slot >= 0 and $packet->slot < CHEST_SLOTS) { $tile = $tiles[0]; $slotn = $packet->slot; $offset = 0; - }elseif($packet->slot >= CHEST_SLOTS and $packet->slot <= (CHEST_SLOTS << 1)){ + } elseif ($packet->slot >= CHEST_SLOTS and $packet->slot <= (CHEST_SLOTS << 1)) { $tile = $tiles[1]; $slotn = $packet->slot - CHEST_SLOTS; $offset = CHEST_SLOTS; - }else{ + } else { break; } $item = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count); $slot = $tile->getSlot($slotn); - if($this->server->api->dhandle("player.container.slot", [ + if ($this->server->api->dhandle("player.container.slot", [ "tile" => $tile, "slot" => $packet->slot, "offset" => $offset, "slotdata" => $slot, "itemdata" => $item, "player" => $this - ]) === false){ + ]) === false) { $pk = new ContainerSetSlotPacket; $pk->windowid = $packet->windowid; $pk->slot = $packet->slot; @@ -3616,36 +3625,36 @@ class Player{ $this->dataPacket($pk); break; } - if($item->getID() !== AIR and $slot->getID() == $item->getID()){ - if($slot->count < $item->count){ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count - $slot->count, false) === false){ + if ($item->getID() !== AIR and $slot->getID() == $item->getID()) { + if ($slot->count < $item->count) { + if ($this->removeItem($item->getID(), $item->getMetadata(), $item->count - $slot->count, false) === false) { break; } - }elseif($slot->count > $item->count){ + } elseif ($slot->count > $item->count) { $this->addItem($item->getID(), $item->getMetadata(), $slot->count - $item->count, false); } - }else{ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count, false) === false){ + } else { + if ($this->removeItem($item->getID(), $item->getMetadata(), $item->count, false) === false) { break; } $this->addItem($slot->getID(), $slot->getMetadata(), $slot->count, false); } $tile->setSlot($slotn, $item, true, $offset); - }else{ + } else { $tile = $this->windows[$packet->windowid]; - if(($tile->class !== TILE_CHEST and $tile->class !== TILE_FURNACE) or $packet->slot < 0 or ($tile->class === TILE_CHEST and $packet->slot >= CHEST_SLOTS) or ($tile->class === TILE_FURNACE and $packet->slot >= FURNACE_SLOTS)){ + if (($tile->class !== TILE_CHEST and $tile->class !== TILE_FURNACE) or $packet->slot < 0 or ($tile->class === TILE_CHEST and $packet->slot >= CHEST_SLOTS) or ($tile->class === TILE_FURNACE and $packet->slot >= FURNACE_SLOTS)) { break; } $item = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count); $slot = $tile->getSlot($packet->slot); - if($this->server->api->dhandle("player.container.slot", [ + if ($this->server->api->dhandle("player.container.slot", [ "tile" => $tile, "slot" => $packet->slot, "slotdata" => $slot, "itemdata" => $item, "player" => $this, - ]) === false){ + ]) === false) { $pk = new ContainerSetSlotPacket; $pk->windowid = $packet->windowid; $pk->slot = $packet->slot; @@ -3654,24 +3663,24 @@ class Player{ break; } - if($tile->class === TILE_FURNACE and $packet->slot == 2){ - switch($slot->getID()){ + if ($tile->class === TILE_FURNACE and $packet->slot == 2) { + switch ($slot->getID()) { case IRON_INGOT: AchievementAPI::grantAchievement($this, "acquireIron"); break; } } - if($item->getID() !== AIR and $slot->getID() == $item->getID()){ - if($slot->count < $item->count){ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count - $slot->count, false) === false){ + if ($item->getID() !== AIR and $slot->getID() == $item->getID()) { + if ($slot->count < $item->count) { + if ($this->removeItem($item->getID(), $item->getMetadata(), $item->count - $slot->count, false) === false) { break; } - }elseif($slot->count > $item->count){ + } elseif ($slot->count > $item->count) { $this->addItem($item->getID(), $item->getMetadata(), $slot->count - $item->count, false); } - }else{ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count, false) === false){ + } else { + if ($this->removeItem($item->getID(), $item->getMetadata(), $item->count, false) === false) { break; } $this->addItem($slot->getID(), $slot->getMetadata(), $slot->count, false); @@ -3681,27 +3690,27 @@ class Player{ } break; case ProtocolInfo9::SEND_INVENTORY_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } break; case ProtocolInfo9::ENTITY_DATA_PACKET: - if($this->spawned === false or $this->blocked === true){ + if ($this->spawned === false or $this->blocked === true) { break; } $this->craftingItems = []; $this->toCraft = []; $t = $this->server->api->tile->get(new Position($packet->x, $packet->y, $packet->z, $this->level)); - if(($t instanceof Tile) and $t->class === TILE_SIGN){ - if($t->data["creator"] !== $this->username){ + if (($t instanceof Tile) and $t->class === TILE_SIGN) { + if ($t->data["creator"] !== $this->username) { $t->spawn($this); - }else{ + } else { $nbt = new NBT(); $nbt->load($packet->namedtag); $d = array_shift($nbt->tree); - if($d["id"] !== TILE_SIGN){ + if ($d["id"] !== TILE_SIGN) { $t->spawn($this); - }else{ + } else { $t->setText($d["Text1"], $d["Text2"], $d["Text3"], $d["Text4"]); } } @@ -3713,13 +3722,13 @@ class Player{ $this->entity->moveForward = $packet->moveForward; $this->entity->moveStrafing = $packet->moveStrafe; - if(strlen(bin2hex($packet->buffer)) === 24 && $this->entity->linkedEntity != 0){ + if (strlen(bin2hex($packet->buffer)) === 24 && $this->entity->linkedEntity != 0) { $this->entity->stopRiding(); break; } - if($this->entity->linkedEntity != 0){ //TODO better riding + if ($this->entity->linkedEntity != 0) { //TODO better riding $e = $this->entity->level->entityList[$this->entity->linkedEntity] ?? false; - if($e === false) { + if ($e === false) { ConsoleAPI::warn("Player is riding on entity that doesnt exist in world! ({$this->iusername}, {$this->entity->linkedEntity})"); $this->entity->stopRiding(); break; @@ -3743,15 +3752,15 @@ class Player{ console("[DEBUG] Unhandled 0x" . dechex($packet->pid()) . " data packet for " . $this->username . " (" . $this->clientID . "): " . print_r($packet, true), true, true, 2); break; } - }elseif($this->PROTOCOL >= ProtocolInfo12::CURRENT_PROTOCOL_12 && $this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - switch($packet->pid()){ + } elseif ($this->PROTOCOL >= ProtocolInfo12::CURRENT_PROTOCOL_12 && $this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL) { + switch ($packet->pid()) { case 0x01: break; case ProtocolInfo12::PONG_PACKET: $currentTime = intdiv(hrtime(true), 1_000_000); - if($currentTime > $packet->ptime){ - $this->lastPing = $currentTime - $packet->ptime; - } + if ($currentTime > $packet->ptime) { + $this->lastPing = $currentTime - $packet->ptime; + } break; case ProtocolInfo12::PING_PACKET: $pk = new PongPacket; @@ -3763,7 +3772,7 @@ class Player{ $this->close("client disconnect"); break; case ProtocolInfo12::CLIENT_CONNECT_PACKET: - if($this->loggedIn === true){ + if ($this->loggedIn === true) { break; } $pk = new ServerHandshakePacket; @@ -3773,70 +3782,70 @@ class Player{ $this->dataPacket($pk); break; case ProtocolInfo12::CLIENT_HANDSHAKE_PACKET: - if($this->loggedIn === true){ + if ($this->loggedIn === true) { break; } break; case ProtocolInfo12::LOGIN_PACKET: - if($this->loggedIn === true){ + if ($this->loggedIn === true) { break; } $this->username = $packet->username; $this->iusername = strtolower($this->username); $this->loginData = ["clientId" => $packet->clientId, "loginData" => $packet->loginData]; - if(count($this->server->clients) > $this->server->maxClients and !$this->server->api->ban->isOp($this->iusername)){ + if (count($this->server->clients) > $this->server->maxClients and !$this->server->api->ban->isOp($this->iusername)) { $this->close("server is full!", false); return; } - if(preg_match('#[^a-zA-Z0-9_]#', $this->username) > 0 || $this->username === "" || $this->iusername === "rcon" || $this->iusername === "console" || $this->iusername === "server" || strlen($this->iusername) > 16){ + if (preg_match('#[^a-zA-Z0-9_]#', $this->username) > 0 || $this->username === "" || $this->iusername === "rcon" || $this->iusername === "console" || $this->iusername === "server" || strlen($this->iusername) > 16) { $this->close("Bad username", false); return; } - if($this->server->api->handle("player.connect", $this) === false){ + if ($this->server->api->handle("player.connect", $this) === false) { $this->close("Unknown reason", false); return; } - if($this->server->whitelist === true and !$this->server->api->ban->inWhitelist($this->iusername)){ + if ($this->server->whitelist === true and !$this->server->api->ban->inWhitelist($this->iusername)) { $this->close("Server is white-listed", false); return; - }elseif($this->server->api->ban->isBanned($this->iusername) or $this->server->api->ban->isIPBanned($this->ip)){ + } elseif ($this->server->api->ban->isBanned($this->iusername) or $this->server->api->ban->isIPBanned($this->ip)) { $this->close("You are banned!", false); return; } $this->loggedIn = true; - if(!isset($this->CID) or $this->CID == null){ + if (!isset($this->CID) or $this->CID == null) { console("[DEBUG] Player " . $this->username . " does not have a CID", true, true, 2); $this->CID = Utils::readLong(Utils::getRandomBytes(8, false)); } $u = $this->server->api->player->get($this->iusername, false); - if($u !== false){ + if ($u !== false) { $u = $this->server->clients[$this->CID]; $u->close("this player already in game"); } $this->server->api->player->add($this->CID); - if($this->server->api->handle("player.join", $this) === false){ + if ($this->server->api->handle("player.join", $this) === false) { $this->close("join cancelled", false); return; } - if(!($this->data instanceof Config)){ + if (!($this->data instanceof Config)) { $this->close("no config created", false); return; } $this->auth = true; - if(!$this->data->exists("inventory") or ($this->gamemode & 0x01) === 0x01){ - if(($this->gamemode & 0x01) === 0x01){ + if (!$this->data->exists("inventory") or ($this->gamemode & 0x01) === 0x01) { + if (($this->gamemode & 0x01) === 0x01) { $inv = []; - if(($this->gamemode & 0x02) === 0x02){ - foreach(BlockAPI::$creative as $item){ + if (($this->gamemode & 0x02) === 0x02) { + foreach (BlockAPI::$creative as $item) { $inv[] = [0, 0, 1]; } - }else{ - foreach(BlockAPI::$creative as $item){ + } else { + foreach (BlockAPI::$creative as $item) { $inv[] = [$item[0], $item[1], 1]; } } @@ -3846,15 +3855,15 @@ class Player{ $this->achievements = $this->data->get("achievements"); $this->data->set("caseusername", $this->username); $this->inventory = []; - foreach($this->data->get("inventory") as $slot => $item){ - if(!is_array($item) or count($item) < 3){ + foreach ($this->data->get("inventory") as $slot => $item) { + if (!is_array($item) or count($item) < 3) { $item = [AIR, 0, 0]; } $this->inventory[$slot] = BlockAPI::getItem($item[0], $item[1], $item[2]); } $this->armor = []; - foreach($this->data->get("armor") as $slot => $item){ + foreach ($this->data->get("armor") as $slot => $item) { $this->armor[$slot] = BlockAPI::getItem($item[0], $item[1], $item[0] === 0 ? 0 : 1); } @@ -3878,23 +3887,23 @@ class Player{ $pk->eid = 0; $this->dataPacket($pk); - if(($this->gamemode & 0x01) === 0x01){ + if (($this->gamemode & 0x01) === 0x01) { $this->slot = 0; $this->hotbar = []; - }elseif($this->data->exists("hotbar")){ + } elseif ($this->data->exists("hotbar")) { $this->hotbar = $this->data->get("hotbar"); $this->slot = $this->hotbar[0]; - }else{ + } else { $this->slot = 0; $this->hotbar = [0, 1, 2, 3, 4, 5, 6, 7, 8]; } - for($i = 0; $i < count($this->hotbar); ++$i){ - if($this->hotbar[$i] > 36) $this->hotbar[$i] = -1; //XXX unsafe? - if($this->hotbar[$i] < -1) $this->hotbar[$i] = -1; + for ($i = 0; $i < count($this->hotbar); ++$i) { + if ($this->hotbar[$i] > 36) $this->hotbar[$i] = -1; //XXX unsafe? + if ($this->hotbar[$i] < -1) $this->hotbar[$i] = -1; } - if($this->data->exists("slot-count")){ + if ($this->data->exists("slot-count")) { $this->slotCount = $this->data->get("slot-count"); - }else{ + } else { $this->data->set("slot-count", $this->slotCount); } @@ -3904,13 +3913,13 @@ class Player{ $this->entity->x = $this->data->get("position")["x"]; $this->entity->y = $this->data->get("position")["y"]; $this->entity->z = $this->data->get("position")["z"]; - if(($level = $this->server->api->level->get($this->data->get("spawn")["level"])) !== false){ + if (($level = $this->server->api->level->get($this->data->get("spawn")["level"])) !== false) { $this->spawnPosition = new Position($this->data->get("spawn")["x"], $this->data->get("spawn")["y"], $this->data->get("spawn")["z"], $level); $pk = new SetSpawnPositionPacket; - $pk->x = (int) $this->spawnPosition->x; - $pk->y = (int) $this->spawnPosition->y; - $pk->z = (int) $this->spawnPosition->z; + $pk->x = (int)$this->spawnPosition->x; + $pk->y = (int)$this->spawnPosition->y; + $pk->z = (int)$this->spawnPosition->z; $this->dataPacket($pk); } $this->entity->check = false; @@ -3939,12 +3948,12 @@ class Player{ console("[INFO] " . FORMAT_AQUA . $this->username . FORMAT_RESET . "[/" . $this->ip . ":" . $this->port . "] logged in with entity id " . $this->eid . " at (" . $this->entity->level->getName() . ", " . round($this->entity->x, 2) . ", " . round($this->entity->y, 2) . ", " . round($this->entity->z, 2) . ")"); break; case ProtocolInfo12::READY_PACKET: - if($this->loggedIn === false){ + if ($this->loggedIn === false) { break; } - switch($packet->status){ + switch ($packet->status) { case 1: //Spawn!! - if($this->spawned !== false){ + if ($this->spawned !== false) { break; } @@ -3962,8 +3971,8 @@ class Player{ //$this->server->schedule(2, [$this->entity, "updateMovement"], [], true); $this->sendArmor(); $array = explode("@n", (string)$this->server->motd); - foreach($array as $msg){ - $this->sendChat($msg."\n"); + foreach ($array as $msg) { + $this->sendChat($msg . "\n"); } $this->sendInventory(); @@ -3979,48 +3988,48 @@ class Player{ } break; case ProtocolInfo12::ROTATE_HEAD_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } - if(($this->entity instanceof Entity)){ - if($this->blocked === true or $this->server->api->handle("player.move", $this->entity) === false){ - if($this->lastCorrect instanceof Vector3 && !$this->entity->dead){ + if (($this->entity instanceof Entity)) { + if ($this->blocked === true or $this->server->api->handle("player.move", $this->entity) === false) { + if ($this->lastCorrect instanceof Vector3 && !$this->entity->dead) { $this->teleport($this->lastCorrect, $this->entity->yaw, $this->entity->pitch, false); } - }else{ + } else { $this->entity->setPosition($this->entity, $packet->yaw, $this->entity->pitch); } } break; case ProtocolInfo12::MOVE_PLAYER_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } - if($this->isSleeping) break; - if(($this->entity instanceof Entity) and $packet->messageIndex > $this->lastMovement){ + if ($this->isSleeping) break; + if (($this->entity instanceof Entity) and $packet->messageIndex > $this->lastMovement) { $this->lastMovement = $packet->messageIndex; $newPos = new Vector3($packet->x, $packet->y, $packet->z); - if($this->forceMovement instanceof Vector3){ - if($this->forceMovement->distance($newPos) <= 0.7){ + if ($this->forceMovement instanceof Vector3) { + if ($this->forceMovement->distance($newPos) <= 0.7) { $this->forceMovement = false; - }else{ + } else { $this->teleport($this->forceMovement, $this->entity->yaw, $this->entity->pitch, false); break; } } $speed = $this->entity->getSpeedMeasure(); - if($this->blocked === true or ($this->server->api->getProperty("allow-flight") !== true and (($speed > 9 and ($this->gamemode & 0x01) === 0x00) or $speed > 20 or $this->entity->distance($newPos) > 7)) or $this->server->api->handle("player.move", $this->entity) === false){ - if($this->lastCorrect instanceof Vector3 && !$this->entity->dead){ + if ($this->blocked === true or ($this->server->api->getProperty("allow-flight") !== true and (($speed > 9 and ($this->gamemode & 0x01) === 0x00) or $speed > 20 or $this->entity->distance($newPos) > 7)) or $this->server->api->handle("player.move", $this->entity) === false) { + if ($this->lastCorrect instanceof Vector3 && !$this->entity->dead) { $this->teleport($this->lastCorrect, $this->entity->yaw, $this->entity->pitch, false); } - }else{ + } else { $this->entity->setPosition($newPos, $packet->yaw, $packet->pitch, $packet->bodyYaw); } $this->entity->updateAABB(); } break; case ProtocolInfo12::PLAYER_EQUIPMENT_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } $packet->eid = $this->eid; @@ -4029,77 +4038,77 @@ class Player{ $data["eid"] = $packet->eid; $data["player"] = $this; - if($packet->slot === 0){ + if ($packet->slot === 0) { $data["slot"] = -1; $data["item"] = BlockAPI::getItem(AIR, 0, 0); - if($this->server->handle("player.equipment.change", $data) !== false){ + if ($this->server->handle("player.equipment.change", $data) !== false) { $this->slot = -1; } break; - }else if($packet->slot > 0){ + } else if ($packet->slot > 0) { $packet->slot -= 9; } - if(($this->gamemode & 0x01) === SURVIVAL){ + if (($this->gamemode & 0x01) === SURVIVAL) { $data["item"] = $this->getSlot($packet->slot); - if(!($data["item"] instanceof Item)){ + if (!($data["item"] instanceof Item)) { break; } - }elseif(($this->gamemode & 0x01) === CREATIVE){ + } elseif (($this->gamemode & 0x01) === CREATIVE) { $packet->slot = false; - foreach(BlockAPI::$creative as $i => $d){ - if($d[0] === $packet->item and $d[1] === $packet->meta){ + foreach (BlockAPI::$creative as $i => $d) { + if ($d[0] === $packet->item and $d[1] === $packet->meta) { $packet->slot = $i; } } - if($packet->slot !== false){ + if ($packet->slot !== false) { $data["item"] = $this->getSlot($packet->slot); - }else{ + } else { break; } - }else{ + } else { break;//????? } $data["slot"] = $packet->slot; - if($this->server->handle("player.equipment.change", $data) !== false){ - if(!Player::$experimentalHotbar) $this->slot = $packet->slot; - if(($this->gamemode & 0x01) === SURVIVAL){ + if ($this->server->handle("player.equipment.change", $data) !== false) { + if (!Player::$experimentalHotbar) $this->slot = $packet->slot; + if (($this->gamemode & 0x01) === SURVIVAL) { $has = false; $slotPos = 0; $packetSlotPos = 0; - for($i = 0; $i < $this->slotCount; ++$i){ - if($this->slot == $this->hotbar[$i]) $slotPos = $i; - if($packet->slot == $this->hotbar[$i]){ + for ($i = 0; $i < $this->slotCount; ++$i) { + if ($this->slot == $this->hotbar[$i]) $slotPos = $i; + if ($packet->slot == $this->hotbar[$i]) { $packetSlotPos = $i; $has = true; break; } } - if(Player::$experimentalHotbar && $has) { + if (Player::$experimentalHotbar && $has) { $this->slot = $packet->slot; $this->curHotbarIndex = $packetSlotPos; } - if(!$has){ - if(Player::$experimentalHotbar) { + if (!$has) { + if (Player::$experimentalHotbar) { $this->slot = $packet->slot; $this->hotbar[$this->curHotbarIndex] = $packet->slot; - }else{ + } else { $this->curHotbarIndex = 0; array_pop($this->hotbar); array_unshift($this->hotbar, $this->slot); } } - if(Player::$experimentalHotbar) $this->sendInventory(); + if (Player::$experimentalHotbar) $this->sendInventory(); } - }else{ + } else { //$this->sendInventorySlot($packet->slot); $this->sendInventory(); } - if($this->entity->inAction === true){ + if ($this->entity->inAction === true) { $this->entity->inAction = false; $this->entity->inActionCounter = 0; $this->entity->updateMetadata(); @@ -4111,13 +4120,13 @@ class Player{ //$this->lastChunk = [$packet->chunkX, $packet->chunkZ]; break; case ProtocolInfo12::USE_ITEM_PACKET: - if(!($this->entity instanceof Entity)){ + if (!($this->entity instanceof Entity)) { break; } $blockVector = new Vector3($packet->x, $packet->y, $packet->z); - if(($this->spawned === false or $this->blocked === true) and $packet->face >= 0 and $packet->face <= 5){ + if (($this->spawned === false or $this->blocked === true) and $packet->face >= 0 and $packet->face <= 5) { $target = $this->level->getBlock($blockVector); $block = $target->getSide($packet->face); @@ -4157,18 +4166,18 @@ class Player{ $data["posY"] = $packet->posY; $data["posZ"] = $packet->posZ; - if($packet->face >= 0 and $packet->face <= 5){ //Use Block, place - if($this->entity->inAction === true){ + if ($packet->face >= 0 and $packet->face <= 5) { //Use Block, place + if ($this->entity->inAction === true) { $this->entity->inAction = false; $this->entity->inActionCounter = 0; $this->entity->updateMetadata(); } - if($this->blocked === true or ($this->entity->position instanceof Vector3 and $blockVector->distance($this->entity->position) > 10)){ + if ($this->blocked === true or ($this->entity->position instanceof Vector3 and $blockVector->distance($this->entity->position) > 10)) { - }elseif($this->getSlot($this->slot)->getID() !== $packet->item or ($this->getSlot($this->slot)->isTool() === false and $this->getSlot($this->slot)->getMetadata() !== $packet->meta)){ + } elseif ($this->getSlot($this->slot)->getID() !== $packet->item or ($this->getSlot($this->slot)->isTool() === false and $this->getSlot($this->slot)->getMetadata() !== $packet->meta)) { $this->sendInventorySlot($this->slot); - }else{ + } else { $this->server->api->block->playerBlockAction($this, $blockVector, $packet->face, $packet->fx, $packet->fy, $packet->fz); break; } @@ -4191,17 +4200,17 @@ class Player{ $pk->meta = $block->getMetadata(); $this->dataPacket($pk); break; - }elseif($packet->face === 0xff){ + } elseif ($packet->face === 0xff) { $slotItem = $this->getHeldItem(); - if($slotItem->getID() == SNOWBALL || $slotItem->getID() == EGG){ //TODO better way + if ($slotItem->getID() == SNOWBALL || $slotItem->getID() == EGG) { //TODO better way $x = $packet->x * 0.000030518; $y = $packet->y * 0.000030518; $z = $packet->z * 0.000030518; - $d = sqrt($x*$x + $y*$y + $z*$z); + $d = sqrt($x * $x + $y * $y + $z * $z); - if($d >= 0.0001){ + if ($d >= 0.0001) { $shootX = $x / $d; $shootY = $y / $d; $shootZ = $z / $d; @@ -4218,15 +4227,15 @@ class Player{ "shootZ" => $shootZ ]; - if($slotItem->getID() == EGG){ + if ($slotItem->getID() == EGG) { $e = $this->server->api->entity->add($this->entity->level, ENTITY_OBJECT, OBJECT_EGG, $data); - }else{ + } else { $e = $this->server->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_SNOWBALL, $data); } - if(($this->gamemode & 0x01) == 0x0) { - if($slotItem !== false){ - if($slotItem->count == 1) $this->inventory[$this->slot] = BlockAPI::getItem(AIR, 0, 0); + if (($this->gamemode & 0x01) == 0x0) { + if ($slotItem !== false) { + if ($slotItem->count == 1) $this->inventory[$this->slot] = BlockAPI::getItem(AIR, 0, 0); else $slotItem->count -= 1; //$this->sendInventory(); } @@ -4235,8 +4244,8 @@ class Player{ $this->server->api->entity->spawnToAll($e); } - }else{ - if($this->server->handle("player.action", $data) !== false){ + } else { + if ($this->server->handle("player.action", $data) !== false) { $this->entity->inAction = true; $this->entity->inActionCounter = 0; $this->startAction = microtime(true); @@ -4246,24 +4255,24 @@ class Player{ } break; case ProtocolInfo12::PLAYER_ACTION_PACKET: - if($this->spawned === false or $this->blocked === true){ + if ($this->spawned === false or $this->blocked === true) { break; } $packet->eid = $this->eid; $this->craftingItems = []; $this->toCraft = []; - switch($packet->action){ + switch ($packet->action) { case 5: //Shot arrow - if($this->entity->inAction){ + if ($this->entity->inAction) { $arrowSlot = $this->hasItem(ARROW); - if($this->getSlot($this->slot)->getID() === BOW && (($this->gamemode & 0x01) == 0x1 || $arrowSlot !== false)){ - if($this->startAction !== false){ + if ($this->getSlot($this->slot)->getID() === BOW && (($this->gamemode & 0x01) == 0x1 || $arrowSlot !== false)) { + if ($this->startAction !== false) { $initalPower = $this->entity->inActionCounter; $power = $initalPower / 20; $power = ($power * $power + $power * 2) / 3; - if($power >= 0.1){ - if($power > 1) $power = 1; + if ($power >= 0.1) { + if ($power > 1) $power = 1; $this->server->dhandle("player.shoot", [ "player" => $this, "power" => &$power, @@ -4292,11 +4301,11 @@ class Player{ * args: xvel, yvel, zvel, (power+power)*1.5, 1.0 */ $e->critical = ($power == 1); - $e->shoot($e->speedX, $e->speedY, $e->speedZ, ($power+$power) * 1.5, 1.0); + $e->shoot($e->speedX, $e->speedY, $e->speedZ, ($power + $power) * 1.5, 1.0); $this->server->api->entity->spawnToAll($e); - if(($this->gamemode & 0x01) == 0x0) { + if (($this->gamemode & 0x01) == 0x0) { $bow = $this->getSlot($this->slot); - if(++$bow->meta >= $bow->getMaxDurability()){ + if (++$bow->meta >= $bow->getMaxDurability()) { $this->inventory[$this->slot] = BlockAPI::getItem(AIR, 0, 0); } $this->removeItem(ARROW, 0, 1, false); @@ -4304,7 +4313,7 @@ class Player{ } } } - }else{ //inv desynced, resend + } else { //inv desynced, resend $this->sendInventory(); } } @@ -4319,7 +4328,7 @@ class Player{ break; case ProtocolInfo12::REMOVE_BLOCK_PACKET: $blockVector = new Vector3($packet->x, $packet->y, $packet->z); - if($this->spawned === false or $this->blocked === true or $this->entity->distance($blockVector) > 8){ + if ($this->spawned === false or $this->blocked === true or $this->entity->distance($blockVector) > 8) { $target = $this->level->getBlock($blockVector); $pk = new UpdateBlockPacket; @@ -4336,46 +4345,46 @@ class Player{ $this->server->api->block->playerBlockBreak($this, $blockVector); break; case ProtocolInfo12::PLAYER_ARMOR_EQUIPMENT_PACKET: - if($this->spawned === false or $this->blocked === true){ + if ($this->spawned === false or $this->blocked === true) { break; } $this->craftingItems = []; $this->toCraft = []; $packet->eid = $this->eid; - for($i = 0; $i < 4; ++$i){ + for ($i = 0; $i < 4; ++$i) { $s = $packet->slots[$i]; - if($s === 0 or $s === 255){ + if ($s === 0 or $s === 255) { $s = BlockAPI::getItem(AIR, 0, 0); - }else{ + } else { $s = BlockAPI::getItem($s + 256, 0, 1); } $slot = $this->armor[$i]; - if($slot->getID() !== AIR and $s->getID() === AIR){ + if ($slot->getID() !== AIR and $s->getID() === AIR) { $this->addItem($slot->getID(), $slot->getMetadata(), 1, false); $this->armor[$i] = BlockAPI::getItem(AIR, 0, 0); $packet->slots[$i] = 255; - }elseif($s->getID() !== AIR and $slot->getID() === AIR and ($sl = $this->hasItem($s->getID())) !== false){ + } elseif ($s->getID() !== AIR and $slot->getID() === AIR and ($sl = $this->hasItem($s->getID())) !== false) { $this->armor[$i] = $this->getSlot($sl); $this->setSlot($sl, BlockAPI::getItem(AIR, 0, 0), false); - }elseif($s->getID() !== AIR and $slot->getID() !== AIR and ($slot->getID() !== $s->getID() or $slot->getMetadata() !== $s->getMetadata()) and ($sl = $this->hasItem($s->getID())) !== false){ + } elseif ($s->getID() !== AIR and $slot->getID() !== AIR and ($slot->getID() !== $s->getID() or $slot->getMetadata() !== $s->getMetadata()) and ($sl = $this->hasItem($s->getID())) !== false) { $item = $this->armor[$i]; $this->armor[$i] = $this->getSlot($sl); $this->setSlot($sl, $item, false); - }else{ + } else { $packet->slots[$i] = 255; } } $this->sendArmor(); - if($this->entity->inAction === true){ + if ($this->entity->inAction === true) { $this->entity->inAction = false; $this->entity->inActionCounter = 0; $this->entity->updateMetadata(); } break; case ProtocolInfo12::INTERACT_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } $packet->eid = $this->eid; @@ -4386,28 +4395,28 @@ class Player{ $this->craftingItems = []; $this->toCraft = []; $target = $this->server->api->entity->get($packet->target); - if($target instanceof Entity and $this->entity instanceof Entity and $this->gamemode !== VIEW and $this->blocked === false and ($target instanceof Entity) and $this->entity->distance($target) <= 8){ + if ($target instanceof Entity and $this->entity instanceof Entity and $this->gamemode !== VIEW and $this->blocked === false and ($target instanceof Entity) and $this->entity->distance($target) <= 8) { $data["targetentity"] = $packet->target; $data["entity"] = $this->entity; $data["player"] = $this; - if($this->server->handle("player.interact", $data) !== false){ + if ($this->server->handle("player.interact", $data) !== false) { $target->interactWith($this->entity, $packet->action); } } break; case ProtocolInfo12::ANIMATE_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } $packet->eid = $this->eid; $this->server->api->dhandle("entity.animate", ["eid" => $packet->eid, "entity" => $this->entity, "action" => $packet->action]); break; case ProtocolInfo12::RESPAWN_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } - if(@$this->entity->dead === false){ + if (@$this->entity->dead === false) { break; } $this->craftingItems = []; @@ -4424,18 +4433,18 @@ class Player{ $pk->yaw = $this->entity->yaw; $pk->pitch = $this->entity->pitch; $pk->bodyYaw = $this->entity->headYaw; - foreach($this->entity->level->players as $player){ - if($player->entity->eid != $this->entity->eid){ + foreach ($this->entity->level->players as $player) { + if ($player->entity->eid != $this->entity->eid) { $player->dataPacket(clone $pk); } } - if($this->entity instanceof Entity){ + if ($this->entity instanceof Entity) { $this->entity->fire = 0; $this->entity->air = $this->entity->maxAir; $this->entity->setHealth(20, "respawn", true); $this->entity->updateMetadata(); - }else{ + } else { break; } $this->sendInventory(); @@ -4445,22 +4454,22 @@ class Player{ case ProtocolInfo12::SET_HEALTH_PACKET: //Not used break; case ProtocolInfo12::ENTITY_EVENT_PACKET: - if($this->spawned === false or $this->blocked === true){ + if ($this->spawned === false or $this->blocked === true) { break; } $this->craftingItems = []; $this->toCraft = []; $packet->eid = $this->eid; - if($this->entity->inAction === true){ + if ($this->entity->inAction === true) { $this->entity->inAction = false; $this->entity->inActionCounter = 0; $this->entity->updateMetadata(); } - switch($packet->event){ + switch ($packet->event) { case 9: //Eating $slot = $this->getSlot($this->slot); $foodHeal = Item::getFoodHeal($slot->getID()); - if($this->entity->getHealth() < 20 && $foodHeal != 0){ + if ($this->entity->getHealth() < 20 && $foodHeal != 0) { $pk = new EntityEventPacket; $pk->eid = 0; $pk->event = 9; @@ -4468,10 +4477,10 @@ class Player{ $this->entity->heal($foodHeal, "eating"); --$slot->count; - if($slot->count <= 0){ + if ($slot->count <= 0) { $this->setSlot($this->slot, BlockAPI::getItem(AIR, 0, 0), false); } - if($slot->getID() === MUSHROOM_STEW or $slot->getID() === BEETROOT_SOUP){ + if ($slot->getID() === MUSHROOM_STEW or $slot->getID() === BEETROOT_SOUP) { $this->addItem(BOWL, 0, 1, false); } } @@ -4479,11 +4488,11 @@ class Player{ } break; case ProtocolInfo12::DROP_ITEM_PACKET: - if($this->spawned === false or $this->blocked === true){ + if ($this->spawned === false or $this->blocked === true) { break; } - if($this->gamemode & 0x01 == 1){ + if ($this->gamemode & 0x01 == 1) { ConsoleAPI::warn("{$this->iusername} tried dropping item while in creative!"); return; } @@ -4493,17 +4502,17 @@ class Player{ $packet->item = $this->getSlot($this->slot); $sendOnDrop = false; - if($prevItem->getID() != $packet->item->getID() || $prevItem->getMetadata() != $packet->item->getMetadata()){ - if(count($this->inventory) >= 36){ - foreach($this->inventory as $slot => $item){ - if($item->getID() == 0) goto inv_desync_on_drop12; + if ($prevItem->getID() != $packet->item->getID() || $prevItem->getMetadata() != $packet->item->getMetadata()) { + if (count($this->inventory) >= 36) { + foreach ($this->inventory as $slot => $item) { + if ($item->getID() == 0) goto inv_desync_on_drop12; } $this->toCraft[] = $prevItem; //vanilla drops only result? $this->lastCraft = microtime(true); break; } - }else { + } else { inv_desync_on_drop12: ConsoleAPI::debug("Inventory desync on drop({$this->iusername})"); $sendOnDrop = true; @@ -4515,7 +4524,7 @@ class Player{ $data["unknown"] = $packet->unknown; $data["item"] = $packet->item; $data["player"] = $this; - if($this->blocked === false and $this->server->handle("player.drop", $data) !== false){ + if ($this->blocked === false and $this->server->handle("player.drop", $data) !== false) { $f1 = 0.3; $sX = -sin(($this->entity->yaw / 180) * M_PI) * cos(($this->entity->pitch / 180) * M_PI) * $f1; $sZ = cos(($this->entity->yaw / 180) * M_PI) * cos(($this->entity->pitch / 180) * M_PI) * $f1; @@ -4528,33 +4537,33 @@ class Player{ $sZ += sin($f3) * $f1; $this->server->api->entity->dropRawPos($this->level, $this->entity->x, $this->entity->y - 0.3 + $this->entity->height - 0.12, $this->entity->z, $packet->item, $sX, $sY, $sZ); $this->setSlot($this->slot, BlockAPI::getItem(AIR, 0, 0), $sendOnDrop); - }else{ + } else { $this->sendInventory(); //send if blocked } - if($this->entity->inAction === true){ + if ($this->entity->inAction === true) { $this->entity->inAction = false; $this->entity->inActionCounter = 0; $this->entity->updateMetadata(); } break; case ProtocolInfo12::MESSAGE_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } $this->craftingItems = []; $this->toCraft = []; - if(trim($packet->message) != "" and strlen($packet->message) <= 255){ + if (trim($packet->message) != "" and strlen($packet->message) <= 255) { $message = $packet->message; - if($message[0] === "/"){ //Command - if($this instanceof Player){ + if ($message[0] === "/") { //Command + if ($this instanceof Player) { console("[DEBUG] " . FORMAT_AQUA . $this->username . FORMAT_RESET . " issued server command: " . $message); - }else{ + } else { console("[DEBUG] " . FORMAT_YELLOW . "*" . $this . FORMAT_RESET . " issued server command: " . $message); } $this->server->api->console->run(substr($message, 1), $this); - }else{ + } else { $data = ["player" => $this, "message" => $message]; - if(Utils::hasEmoji($data["message"])){ + if (Utils::hasEmoji($data["message"])) { $this->sendChat("Your message contains illegal characters"); break; } @@ -4563,11 +4572,11 @@ class Player{ // Living::$pathfind = !Living::$pathfind; //} - if($this->server->api->handle("player.chat", $data) !== false){ + if ($this->server->api->handle("player.chat", $data) !== false) { $this->server->send2Discord("<" . $this->username . "> " . $message); - if(isset($data["message"])){ + if (isset($data["message"])) { $this->server->api->chat->send($this, $data["message"]); - }else{ + } else { $this->server->api->chat->send($this, $message); } } @@ -4575,14 +4584,14 @@ class Player{ } break; case ProtocolInfo12::CONTAINER_CLOSE_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } $this->craftingItems = []; $this->toCraft = []; - if(isset($this->windows[$packet->windowid])){ - if(is_array($this->windows[$packet->windowid])){ - foreach($this->windows[$packet->windowid] as $ob){ + if (isset($this->windows[$packet->windowid])) { + if (is_array($this->windows[$packet->windowid])) { + foreach ($this->windows[$packet->windowid] as $ob) { $pk = new TileEventPacket; $pk->x = $ob->x; $pk->y = $ob->y; @@ -4591,7 +4600,7 @@ class Player{ $pk->case2 = 0; $this->server->api->player->broadcastPacket($this->level->players, $pk); } - }elseif($this->windows[$packet->windowid]->class === TILE_CHEST){ + } elseif ($this->windows[$packet->windowid]->class === TILE_CHEST) { $pk = new TileEventPacket; $pk->x = $this->windows[$packet->windowid]->x; $pk->y = $this->windows[$packet->windowid]->y; @@ -4608,36 +4617,36 @@ class Player{ $this->dataPacket($pk); break; case ProtocolInfo12::CONTAINER_SET_SLOT_PACKET: - if($this->spawned === false or $this->blocked === true){ + if ($this->spawned === false or $this->blocked === true) { break; } - if($this->lastCraft <= (microtime(true) - 1)){ - if(isset($this->toCraft[-1])){ + if ($this->lastCraft <= (microtime(true) - 1)) { + if (isset($this->toCraft[-1])) { $this->toCraft = [-1 => $this->toCraft[-1]]; - }else{ + } else { $this->toCraft = []; } $this->craftingItems = []; } - if($packet->windowid === 0){ + if ($packet->windowid === 0) { $craft = false; $slot = $this->getSlot($packet->slot); - if($slot->count >= $packet->item->count and (($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()) or ($packet->item->getID() === AIR and $packet->item->count === 0)) and !isset($this->craftingItems[$packet->slot])){ //Crafting recipe + if ($slot->count >= $packet->item->count and (($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()) or ($packet->item->getID() === AIR and $packet->item->count === 0)) and !isset($this->craftingItems[$packet->slot])) { //Crafting recipe $use = BlockAPI::getItem($slot->getID(), $slot->getMetadata(), $slot->count - $packet->item->count); $this->craftingItems[$packet->slot] = $use; $craft = true; - }elseif($slot->count <= $packet->item->count and ($slot->getID() === AIR or ($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()))){ //Crafting final + } elseif ($slot->count <= $packet->item->count and ($slot->getID() === AIR or ($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()))) { //Crafting final $craftItem = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count - $slot->count); - if(count($this->toCraft) === 0){ + if (count($this->toCraft) === 0) { $this->toCraft[-1] = 0; } $this->toCraft[$packet->slot] = $craftItem; $craft = true; - }elseif(((count($this->toCraft) === 1 and isset($this->toCraft[-1])) or count($this->toCraft) === 0) and $slot->count > 0 and $slot->getID() > AIR and ($slot->getID() !== $packet->item->getID() or $slot->getMetadata() !== $packet->item->getMetadata())){ //Crafting final + } elseif (((count($this->toCraft) === 1 and isset($this->toCraft[-1])) or count($this->toCraft) === 0) and $slot->count > 0 and $slot->getID() > AIR and ($slot->getID() !== $packet->item->getID() or $slot->getMetadata() !== $packet->item->getMetadata())) { //Crafting final $craftItem = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count); - if(count($this->toCraft) === 0){ + if (count($this->toCraft) === 0) { $this->toCraft[-1] = 0; } $use = BlockAPI::getItem($slot->getID(), $slot->getMetadata(), $slot->count); @@ -4646,52 +4655,52 @@ class Player{ $craft = true; } - if($craft === true){ + if ($craft === true) { $this->lastCraft = microtime(true); } - if($craft === true and count($this->craftingItems) > 0 and count($this->toCraft) > 0 and ($recipe = $this->craftItems($this->toCraft, $this->craftingItems, $this->toCraft[-1])) !== true){ - if($recipe === false){ + if ($craft === true and count($this->craftingItems) > 0 and count($this->toCraft) > 0 and ($recipe = $this->craftItems($this->toCraft, $this->craftingItems, $this->toCraft[-1])) !== true) { + if ($recipe === false) { $this->sendInventory(); $this->toCraft = []; - }else{ + } else { $this->toCraft = [-1 => $this->toCraft[-1]]; } $this->craftingItems = []; } - }else{ + } else { $this->toCraft = []; $this->craftingItems = []; } - if(!isset($this->windows[$packet->windowid])){ + if (!isset($this->windows[$packet->windowid])) { break; } - if(is_array($this->windows[$packet->windowid])){ + if (is_array($this->windows[$packet->windowid])) { $tiles = $this->windows[$packet->windowid]; - if($packet->slot >= 0 and $packet->slot < CHEST_SLOTS){ + if ($packet->slot >= 0 and $packet->slot < CHEST_SLOTS) { $tile = $tiles[0]; $slotn = $packet->slot; $offset = 0; - }elseif($packet->slot >= CHEST_SLOTS and $packet->slot <= (CHEST_SLOTS << 1)){ + } elseif ($packet->slot >= CHEST_SLOTS and $packet->slot <= (CHEST_SLOTS << 1)) { $tile = $tiles[1]; $slotn = $packet->slot - CHEST_SLOTS; $offset = CHEST_SLOTS; - }else{ + } else { break; } $item = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count); $slot = $tile->getSlot($slotn); - if($this->server->api->dhandle("player.container.slot", [ + if ($this->server->api->dhandle("player.container.slot", [ "tile" => $tile, "slot" => $packet->slot, "offset" => $offset, "slotdata" => $slot, "itemdata" => $item, "player" => $this - ]) === false){ + ]) === false) { $pk = new ContainerSetSlotPacket; $pk->windowid = $packet->windowid; $pk->slot = $packet->slot; @@ -4699,36 +4708,36 @@ class Player{ $this->dataPacket($pk); break; } - if($item->getID() !== AIR and $slot->getID() == $item->getID()){ - if($slot->count < $item->count){ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count - $slot->count, false) === false){ + if ($item->getID() !== AIR and $slot->getID() == $item->getID()) { + if ($slot->count < $item->count) { + if ($this->removeItem($item->getID(), $item->getMetadata(), $item->count - $slot->count, false) === false) { break; } - }elseif($slot->count > $item->count){ + } elseif ($slot->count > $item->count) { $this->addItem($item->getID(), $item->getMetadata(), $slot->count - $item->count, false); } - }else{ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count, false) === false){ + } else { + if ($this->removeItem($item->getID(), $item->getMetadata(), $item->count, false) === false) { break; } $this->addItem($slot->getID(), $slot->getMetadata(), $slot->count, false); } $tile->setSlot($slotn, $item, true, $offset); - }else{ + } else { $tile = $this->windows[$packet->windowid]; - if(($tile->class !== TILE_CHEST and $tile->class !== TILE_FURNACE) or $packet->slot < 0 or ($tile->class === TILE_CHEST and $packet->slot >= CHEST_SLOTS) or ($tile->class === TILE_FURNACE and $packet->slot >= FURNACE_SLOTS)){ + if (($tile->class !== TILE_CHEST and $tile->class !== TILE_FURNACE) or $packet->slot < 0 or ($tile->class === TILE_CHEST and $packet->slot >= CHEST_SLOTS) or ($tile->class === TILE_FURNACE and $packet->slot >= FURNACE_SLOTS)) { break; } $item = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count); $slot = $tile->getSlot($packet->slot); - if($this->server->api->dhandle("player.container.slot", [ + if ($this->server->api->dhandle("player.container.slot", [ "tile" => $tile, "slot" => $packet->slot, "slotdata" => $slot, "itemdata" => $item, "player" => $this, - ]) === false){ + ]) === false) { $pk = new ContainerSetSlotPacket; $pk->windowid = $packet->windowid; $pk->slot = $packet->slot; @@ -4737,24 +4746,24 @@ class Player{ break; } - if($tile->class === TILE_FURNACE and $packet->slot == 2){ - switch($slot->getID()){ + if ($tile->class === TILE_FURNACE and $packet->slot == 2) { + switch ($slot->getID()) { case IRON_INGOT: AchievementAPI::grantAchievement($this, "acquireIron"); break; } } - if($item->getID() !== AIR and $slot->getID() == $item->getID()){ - if($slot->count < $item->count){ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count - $slot->count, false) === false){ + if ($item->getID() !== AIR and $slot->getID() == $item->getID()) { + if ($slot->count < $item->count) { + if ($this->removeItem($item->getID(), $item->getMetadata(), $item->count - $slot->count, false) === false) { break; } - }elseif($slot->count > $item->count){ + } elseif ($slot->count > $item->count) { $this->addItem($item->getID(), $item->getMetadata(), $slot->count - $item->count, false); } - }else{ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count, false) === false){ + } else { + if ($this->removeItem($item->getID(), $item->getMetadata(), $item->count, false) === false) { break; } $this->addItem($slot->getID(), $slot->getMetadata(), $slot->count, false); @@ -4764,27 +4773,27 @@ class Player{ } break; case ProtocolInfo12::SEND_INVENTORY_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } break; case ProtocolInfo12::ENTITY_DATA_PACKET: - if($this->spawned === false or $this->blocked === true){ + if ($this->spawned === false or $this->blocked === true) { break; } $this->craftingItems = []; $this->toCraft = []; $t = $this->server->api->tile->get(new Position($packet->x, $packet->y, $packet->z, $this->level)); - if(($t instanceof Tile) and $t->class === TILE_SIGN){ - if($t->data["creator"] !== $this->username){ + if (($t instanceof Tile) and $t->class === TILE_SIGN) { + if ($t->data["creator"] !== $this->username) { $t->spawn($this); - }else{ + } else { $nbt = new NBT(); $nbt->load($packet->namedtag); $d = array_shift($nbt->tree); - if($d["id"] !== TILE_SIGN){ + if ($d["id"] !== TILE_SIGN) { $t->spawn($this); - }else{ + } else { $t->setText($d["Text1"], $d["Text2"], $d["Text3"], $d["Text4"]); } } @@ -4796,13 +4805,13 @@ class Player{ $this->entity->moveForward = $packet->moveForward; $this->entity->moveStrafing = $packet->moveStrafe; - if(strlen(bin2hex($packet->buffer)) === 24 && $this->entity->linkedEntity != 0){ + if (strlen(bin2hex($packet->buffer)) === 24 && $this->entity->linkedEntity != 0) { $this->entity->stopRiding(); break; } - if($this->entity->linkedEntity != 0){ //TODO better riding + if ($this->entity->linkedEntity != 0) { //TODO better riding $e = $this->entity->level->entityList[$this->entity->linkedEntity] ?? false; - if($e === false) { + if ($e === false) { ConsoleAPI::warn("Player is riding on entity that doesnt exist in world! ({$this->iusername}, {$this->entity->linkedEntity})"); $this->entity->stopRiding(); break; @@ -4826,15 +4835,15 @@ class Player{ console("[DEBUG] Unhandled 0x" . dechex($packet->pid()) . " data packet for " . $this->username . " (" . $this->clientID . "): " . print_r($packet, true), true, true, 2); break; } - }else{ - switch($packet->pid()){ + } else { + switch ($packet->pid()) { case 0x01: break; case ProtocolInfo::PONG_PACKET: $currentTime = intdiv(hrtime(true), 1_000_000); - if($currentTime > $packet->ptime){ - $this->lastPing = $currentTime - $packet->ptime; - } + if ($currentTime > $packet->ptime) { + $this->lastPing = $currentTime - $packet->ptime; + } break; case ProtocolInfo::PING_PACKET: $pk = new PongPacket; @@ -4846,7 +4855,7 @@ class Player{ $this->close("client disconnect"); break; case ProtocolInfo::CLIENT_CONNECT_PACKET: - if($this->loggedIn === true){ + if ($this->loggedIn === true) { break; } $pk = new ServerHandshakePacket; @@ -4856,28 +4865,28 @@ class Player{ $this->dataPacket($pk); break; case ProtocolInfo::CLIENT_HANDSHAKE_PACKET: - if($this->loggedIn === true){ + if ($this->loggedIn === true) { break; } break; case ProtocolInfo::LOGIN_PACKET: - if($this->loggedIn === true){ + if ($this->loggedIn === true) { break; } $this->username = $packet->username; $this->iusername = strtolower($this->username); $this->loginData = ["clientId" => $packet->clientId, "loginData" => $packet->loginData]; $this->PROTOCOL = $packet->PROTOCOL; - if(count($this->server->clients) > $this->server->maxClients and !$this->server->api->ban->isOp($this->iusername)){ + if (count($this->server->clients) > $this->server->maxClients and !$this->server->api->ban->isOp($this->iusername)) { $this->close("server is full!", false); return; } - if($packet->protocol1 !== ProtocolInfo::CURRENT_PROTOCOL){ - if($packet->protocol1 < ProtocolInfo::CURRENT_PROTOCOL){ + if ($packet->protocol1 !== ProtocolInfo::CURRENT_PROTOCOL) { + if ($packet->protocol1 < ProtocolInfo::CURRENT_PROTOCOL) { $pk = new LoginStatusPacket; $pk->status = 1; $this->directDataPacket($pk); - }else{ + } else { $pk = new LoginStatusPacket; $pk->status = 2; $this->directDataPacket($pk); @@ -4885,55 +4894,55 @@ class Player{ $this->close("Incorrect protocol #" . $packet->protocol1, false); return; } - if(preg_match('#[^a-zA-Z0-9_]#', $this->username) > 0 || $this->username === "" || $this->iusername === "rcon" || $this->iusername === "console" || $this->iusername === "server" || strlen($this->iusername) > 16){ + if (preg_match('#[^a-zA-Z0-9_]#', $this->username) > 0 || $this->username === "" || $this->iusername === "rcon" || $this->iusername === "console" || $this->iusername === "server" || strlen($this->iusername) > 16) { $this->close("Bad username", false); return; } - if($this->server->api->handle("player.connect", $this) === false){ + if ($this->server->api->handle("player.connect", $this) === false) { $this->close("Unknown reason", false); return; } - if($this->server->whitelist === true and !$this->server->api->ban->inWhitelist($this->iusername)){ + if ($this->server->whitelist === true and !$this->server->api->ban->inWhitelist($this->iusername)) { $this->close("Server is white-listed", false); return; - }elseif($this->server->api->ban->isBanned($this->iusername) or $this->server->api->ban->isIPBanned($this->ip)){ + } elseif ($this->server->api->ban->isBanned($this->iusername) or $this->server->api->ban->isIPBanned($this->ip)) { $this->close("You are banned!", false); return; } $this->loggedIn = true; - if(!isset($this->CID) or $this->CID == null){ + if (!isset($this->CID) or $this->CID == null) { console("[DEBUG] Player " . $this->username . " does not have a CID", true, true, 2); $this->CID = Utils::readLong(Utils::getRandomBytes(8, false)); } $u = $this->server->api->player->get($this->iusername, false); - if($u !== false){ + if ($u !== false) { $u = $this->server->clients[$this->CID]; $u->close("this player already in game"); } $this->server->api->player->add($this->CID); - if($this->server->api->handle("player.join", $this) === false){ + if ($this->server->api->handle("player.join", $this) === false) { $this->close("join cancelled", false); return; } - if(!($this->data instanceof Config)){ + if (!($this->data instanceof Config)) { $this->close("no config created", false); return; } $this->auth = true; - if(!$this->data->exists("inventory") or ($this->gamemode & 0x01) === 0x01){ - if(($this->gamemode & 0x01) === 0x01){ + if (!$this->data->exists("inventory") or ($this->gamemode & 0x01) === 0x01) { + if (($this->gamemode & 0x01) === 0x01) { $inv = []; - if(($this->gamemode & 0x02) === 0x02){ - foreach(BlockAPI::$creative as $item){ + if (($this->gamemode & 0x02) === 0x02) { + foreach (BlockAPI::$creative as $item) { $inv[] = [0, 0, 1]; } - }else{ - foreach(BlockAPI::$creative as $item){ + } else { + foreach (BlockAPI::$creative as $item) { $inv[] = [$item[0], $item[1], 1]; } } @@ -4943,15 +4952,15 @@ class Player{ $this->achievements = $this->data->get("achievements"); $this->data->set("caseusername", $this->username); $this->inventory = []; - foreach($this->data->get("inventory") as $slot => $item){ - if(!is_array($item) or count($item) < 3){ + foreach ($this->data->get("inventory") as $slot => $item) { + if (!is_array($item) or count($item) < 3) { $item = [AIR, 0, 0]; } $this->inventory[$slot] = BlockAPI::getItem($item[0], $item[1], $item[2]); } $this->armor = []; - foreach($this->data->get("armor") as $slot => $item){ + foreach ($this->data->get("armor") as $slot => $item) { $this->armor[$slot] = BlockAPI::getItem($item[0], $item[1], $item[0] === 0 ? 0 : 1); } @@ -4975,23 +4984,23 @@ class Player{ $pk->eid = 0; $this->dataPacket($pk); - if(($this->gamemode & 0x01) === 0x01){ + if (($this->gamemode & 0x01) === 0x01) { $this->slot = 0; $this->hotbar = []; - }elseif($this->data->exists("hotbar")){ + } elseif ($this->data->exists("hotbar")) { $this->hotbar = $this->data->get("hotbar"); $this->slot = $this->hotbar[0]; - }else{ + } else { $this->slot = 0; $this->hotbar = [0, 1, 2, 3, 4, 5, 6, 7, 8]; } - for($i = 0; $i < count($this->hotbar); ++$i){ - if($this->hotbar[$i] > 36) $this->hotbar[$i] = -1; //XXX unsafe? - if($this->hotbar[$i] < -1) $this->hotbar[$i] = -1; + for ($i = 0; $i < count($this->hotbar); ++$i) { + if ($this->hotbar[$i] > 36) $this->hotbar[$i] = -1; //XXX unsafe? + if ($this->hotbar[$i] < -1) $this->hotbar[$i] = -1; } - if($this->data->exists("slot-count")){ + if ($this->data->exists("slot-count")) { $this->slotCount = $this->data->get("slot-count"); - }else{ + } else { $this->data->set("slot-count", $this->slotCount); } @@ -5001,13 +5010,13 @@ class Player{ $this->entity->x = $this->data->get("position")["x"]; $this->entity->y = $this->data->get("position")["y"]; $this->entity->z = $this->data->get("position")["z"]; - if(($level = $this->server->api->level->get($this->data->get("spawn")["level"])) !== false){ + if (($level = $this->server->api->level->get($this->data->get("spawn")["level"])) !== false) { $this->spawnPosition = new Position($this->data->get("spawn")["x"], $this->data->get("spawn")["y"], $this->data->get("spawn")["z"], $level); $pk = new SetSpawnPositionPacket; - $pk->x = (int) $this->spawnPosition->x; - $pk->y = (int) $this->spawnPosition->y; - $pk->z = (int) $this->spawnPosition->z; + $pk->x = (int)$this->spawnPosition->x; + $pk->y = (int)$this->spawnPosition->y; + $pk->z = (int)$this->spawnPosition->z; $this->dataPacket($pk); } $this->entity->check = false; @@ -5036,12 +5045,12 @@ class Player{ console("[INFO] " . FORMAT_AQUA . $this->username . FORMAT_RESET . "[/" . $this->ip . ":" . $this->port . "] logged in with entity id " . $this->eid . " at (" . $this->entity->level->getName() . ", " . round($this->entity->x, 2) . ", " . round($this->entity->y, 2) . ", " . round($this->entity->z, 2) . ")"); break; case ProtocolInfo::READY_PACKET: - if($this->loggedIn === false){ + if ($this->loggedIn === false) { break; } - switch($packet->status){ + switch ($packet->status) { case 1: //Spawn!! - if($this->spawned !== false){ + if ($this->spawned !== false) { break; } @@ -5059,8 +5068,8 @@ class Player{ //$this->server->schedule(2, [$this->entity, "updateMovement"], [], true); $this->sendArmor(); $array = explode("@n", (string)$this->server->motd); - foreach($array as $msg){ - $this->sendChat($msg."\n"); + foreach ($array as $msg) { + $this->sendChat($msg . "\n"); } $this->sendInventory(); @@ -5076,48 +5085,48 @@ class Player{ } break; case ProtocolInfo::ROTATE_HEAD_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } - if(($this->entity instanceof Entity)){ - if($this->blocked === true or $this->server->api->handle("player.move", $this->entity) === false){ - if($this->lastCorrect instanceof Vector3 && !$this->entity->dead){ + if (($this->entity instanceof Entity)) { + if ($this->blocked === true or $this->server->api->handle("player.move", $this->entity) === false) { + if ($this->lastCorrect instanceof Vector3 && !$this->entity->dead) { $this->teleport($this->lastCorrect, $this->entity->yaw, $this->entity->pitch, false); } - }else{ + } else { $this->entity->setPosition($this->entity, $packet->yaw, $this->entity->pitch); } } break; case ProtocolInfo::MOVE_PLAYER_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } - if($this->isSleeping) break; - if(($this->entity instanceof Entity) and $packet->messageIndex > $this->lastMovement){ + if ($this->isSleeping) break; + if (($this->entity instanceof Entity) and $packet->messageIndex > $this->lastMovement) { $this->lastMovement = $packet->messageIndex; $newPos = new Vector3($packet->x, $packet->y, $packet->z); - if($this->forceMovement instanceof Vector3){ - if($this->forceMovement->distance($newPos) <= 0.7){ + if ($this->forceMovement instanceof Vector3) { + if ($this->forceMovement->distance($newPos) <= 0.7) { $this->forceMovement = false; - }else{ + } else { $this->teleport($this->forceMovement, $this->entity->yaw, $this->entity->pitch, false); break; } } $speed = $this->entity->getSpeedMeasure(); - if($this->blocked === true or ($this->server->api->getProperty("allow-flight") !== true and (($speed > 9 and ($this->gamemode & 0x01) === 0x00) or $speed > 20 or $this->entity->distance($newPos) > 7)) or $this->server->api->handle("player.move", $this->entity) === false){ - if($this->lastCorrect instanceof Vector3 && !$this->entity->dead){ + if ($this->blocked === true or ($this->server->api->getProperty("allow-flight") !== true and (($speed > 9 and ($this->gamemode & 0x01) === 0x00) or $speed > 20 or $this->entity->distance($newPos) > 7)) or $this->server->api->handle("player.move", $this->entity) === false) { + if ($this->lastCorrect instanceof Vector3 && !$this->entity->dead) { $this->teleport($this->lastCorrect, $this->entity->yaw, $this->entity->pitch, false); } - }else{ + } else { $this->entity->setPosition($newPos, $packet->yaw, $packet->pitch, $packet->bodyYaw); } $this->entity->updateAABB(); } break; case ProtocolInfo::PLAYER_EQUIPMENT_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } $packet->eid = $this->eid; @@ -5126,79 +5135,79 @@ class Player{ $data["eid"] = $packet->eid; $data["player"] = $this; - if($packet->slot === 0){ + if ($packet->slot === 0) { $data["slot"] = -1; $data["item"] = BlockAPI::getItem(AIR, 0, 0); - if($this->server->handle("player.equipment.change", $data) !== false){ + if ($this->server->handle("player.equipment.change", $data) !== false) { $this->slot = -1; } break; - }else if($packet->slot > 0){ + } else if ($packet->slot > 0) { $packet->slot -= 9; } - if(($this->gamemode & 0x01) === SURVIVAL){ + if (($this->gamemode & 0x01) === SURVIVAL) { $data["item"] = $this->getSlot($packet->slot); - if(!($data["item"] instanceof Item)){ + if (!($data["item"] instanceof Item)) { break; } - }elseif(($this->gamemode & 0x01) === CREATIVE){ + } elseif (($this->gamemode & 0x01) === CREATIVE) { $packet->slot = false; - foreach(BlockAPI::$creative as $i => $d){ - if($d[0] === $packet->item and $d[1] === $packet->meta){ + foreach (BlockAPI::$creative as $i => $d) { + if ($d[0] === $packet->item and $d[1] === $packet->meta) { $packet->slot = $i; } } - if($packet->slot !== false){ + if ($packet->slot !== false) { $data["item"] = $this->getSlot($packet->slot); - }else{ + } else { break; } - }else{ + } else { break;//????? } $data["slot"] = $packet->slot; - if($this->server->handle("player.equipment.change", $data) !== false){ - if(!Player::$experimentalHotbar) $this->slot = $packet->slot; - if(($this->gamemode & 0x01) === SURVIVAL){ + if ($this->server->handle("player.equipment.change", $data) !== false) { + if (!Player::$experimentalHotbar) $this->slot = $packet->slot; + if (($this->gamemode & 0x01) === SURVIVAL) { $has = false; $slotPos = 0; $packetSlotPos = 0; - for($i = 0; $i < $this->slotCount; ++$i){ - if($this->slot == $this->hotbar[$i]) $slotPos = $i; - if($packet->slot == $this->hotbar[$i]){ + for ($i = 0; $i < $this->slotCount; ++$i) { + if ($this->slot == $this->hotbar[$i]) $slotPos = $i; + if ($packet->slot == $this->hotbar[$i]) { $packetSlotPos = $i; $has = true; break; } } - if(Player::$experimentalHotbar && $has) { + if (Player::$experimentalHotbar && $has) { $this->slot = $packet->slot; $this->curHotbarIndex = $packetSlotPos; } - if(!$has){ - if(Player::$experimentalHotbar) { + if (!$has) { + if (Player::$experimentalHotbar) { $this->slot = $packet->slot; $this->hotbar[$this->curHotbarIndex] = $packet->slot; - }else{ + } else { $this->curHotbarIndex = 0; array_pop($this->hotbar); array_unshift($this->hotbar, $this->slot); } } - if(Player::$experimentalHotbar) $this->sendInventory(); - }else{ + if (Player::$experimentalHotbar) $this->sendInventory(); + } else { $this->slot = $packet->slot; } - }else{ + } else { //$this->sendInventorySlot($packet->slot); $this->sendInventory(); } - if($this->entity->inAction === true){ + if ($this->entity->inAction === true) { $this->entity->inAction = false; $this->entity->inActionCounter = 0; $this->entity->updateMetadata(); @@ -5210,13 +5219,13 @@ class Player{ //$this->lastChunk = [$packet->chunkX, $packet->chunkZ]; break; case ProtocolInfo::USE_ITEM_PACKET: - if(!($this->entity instanceof Entity)){ + if (!($this->entity instanceof Entity)) { break; } $blockVector = new Vector3($packet->x, $packet->y, $packet->z); - if(($this->spawned === false or $this->blocked === true) and $packet->face >= 0 and $packet->face <= 5){ + if (($this->spawned === false or $this->blocked === true) and $packet->face >= 0 and $packet->face <= 5) { $target = $this->level->getBlock($blockVector); $block = $target->getSide($packet->face); @@ -5256,18 +5265,18 @@ class Player{ $data["posY"] = $packet->posY; $data["posZ"] = $packet->posZ; - if($packet->face >= 0 and $packet->face <= 5){ //Use Block, place - if($this->entity->inAction === true){ + if ($packet->face >= 0 and $packet->face <= 5) { //Use Block, place + if ($this->entity->inAction === true) { $this->entity->inAction = false; $this->entity->inActionCounter = 0; $this->entity->updateMetadata(); } - if($this->blocked === true or ($this->entity->position instanceof Vector3 and $blockVector->distance($this->entity->position) > 10)){ + if ($this->blocked === true or ($this->entity->position instanceof Vector3 and $blockVector->distance($this->entity->position) > 10)) { - }elseif($this->getSlot($this->slot)->getID() !== $packet->item or ($this->getSlot($this->slot)->isTool() === false and $this->getSlot($this->slot)->getMetadata() !== $packet->meta)){ + } elseif ($this->getSlot($this->slot)->getID() !== $packet->item or ($this->getSlot($this->slot)->isTool() === false and $this->getSlot($this->slot)->getMetadata() !== $packet->meta)) { $this->sendInventorySlot($this->slot); - }else{ + } else { $this->server->api->block->playerBlockAction($this, $blockVector, $packet->face, $packet->fx, $packet->fy, $packet->fz); break; } @@ -5290,17 +5299,17 @@ class Player{ $pk->meta = $block->getMetadata(); $this->dataPacket($pk); break; - }elseif($packet->face === 0xff){ + } elseif ($packet->face === 0xff) { $slotItem = $this->getHeldItem(); - if($slotItem->getID() == SNOWBALL || $slotItem->getID() == EGG){ //TODO better way + if ($slotItem->getID() == SNOWBALL || $slotItem->getID() == EGG) { //TODO better way $x = $packet->x * 0.000030518; $y = $packet->y * 0.000030518; $z = $packet->z * 0.000030518; - $d = sqrt($x*$x + $y*$y + $z*$z); + $d = sqrt($x * $x + $y * $y + $z * $z); - if($d >= 0.0001){ + if ($d >= 0.0001) { $shootX = $x / $d; $shootY = $y / $d; $shootZ = $z / $d; @@ -5317,15 +5326,15 @@ class Player{ "shootZ" => $shootZ ]; - if($slotItem->getID() == EGG){ + if ($slotItem->getID() == EGG) { $e = $this->server->api->entity->add($this->entity->level, ENTITY_OBJECT, OBJECT_EGG, $data); - }else{ + } else { $e = $this->server->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_SNOWBALL, $data); } - if(($this->gamemode & 0x01) == 0x0) { - if($slotItem !== false){ - if($slotItem->count == 1) $this->inventory[$this->slot] = BlockAPI::getItem(AIR, 0, 0); + if (($this->gamemode & 0x01) == 0x0) { + if ($slotItem !== false) { + if ($slotItem->count == 1) $this->inventory[$this->slot] = BlockAPI::getItem(AIR, 0, 0); else $slotItem->count -= 1; //$this->sendInventory(); } @@ -5334,8 +5343,8 @@ class Player{ $this->server->api->entity->spawnToAll($e); } - }else{ - if($this->server->handle("player.action", $data) !== false){ + } else { + if ($this->server->handle("player.action", $data) !== false) { $this->entity->inAction = true; $this->entity->inActionCounter = 0; $this->startAction = microtime(true); @@ -5345,24 +5354,24 @@ class Player{ } break; case ProtocolInfo::PLAYER_ACTION_PACKET: - if($this->spawned === false or $this->blocked === true){ + if ($this->spawned === false or $this->blocked === true) { break; } $packet->eid = $this->eid; $this->craftingItems = []; $this->toCraft = []; - switch($packet->action){ + switch ($packet->action) { case 5: //Shot arrow - if($this->entity->inAction){ + if ($this->entity->inAction) { $arrowSlot = $this->hasItem(ARROW); - if($this->getSlot($this->slot)->getID() === BOW && (($this->gamemode & 0x01) == 0x1 || $arrowSlot !== false)){ - if($this->startAction !== false){ + if ($this->getSlot($this->slot)->getID() === BOW && (($this->gamemode & 0x01) == 0x1 || $arrowSlot !== false)) { + if ($this->startAction !== false) { $initalPower = $this->entity->inActionCounter; $power = $initalPower / 20; $power = ($power * $power + $power * 2) / 3; - if($power >= 0.1){ - if($power > 1) $power = 1; + if ($power >= 0.1) { + if ($power > 1) $power = 1; $this->server->dhandle("player.shoot", [ "player" => $this, "power" => &$power, @@ -5391,11 +5400,11 @@ class Player{ * args: xvel, yvel, zvel, (power+power)*1.5, 1.0 */ $e->critical = ($power == 1); - $e->shoot($e->speedX, $e->speedY, $e->speedZ, ($power+$power) * 1.5, 1.0); + $e->shoot($e->speedX, $e->speedY, $e->speedZ, ($power + $power) * 1.5, 1.0); $this->server->api->entity->spawnToAll($e); - if(($this->gamemode & 0x01) == 0x0) { + if (($this->gamemode & 0x01) == 0x0) { $bow = $this->getSlot($this->slot); - if(++$bow->meta >= $bow->getMaxDurability()){ + if (++$bow->meta >= $bow->getMaxDurability()) { $this->inventory[$this->slot] = BlockAPI::getItem(AIR, 0, 0); } $this->removeItem(ARROW, 0, 1, false); @@ -5403,7 +5412,7 @@ class Player{ } } } - }else{ //inv desynced, resend + } else { //inv desynced, resend $this->sendInventory(); } } @@ -5418,7 +5427,7 @@ class Player{ break; case ProtocolInfo::REMOVE_BLOCK_PACKET: $blockVector = new Vector3($packet->x, $packet->y, $packet->z); - if($this->spawned === false or $this->blocked === true or $this->entity->distance($blockVector) > 8){ + if ($this->spawned === false or $this->blocked === true or $this->entity->distance($blockVector) > 8) { $target = $this->level->getBlock($blockVector); $pk = new UpdateBlockPacket; @@ -5435,46 +5444,46 @@ class Player{ $this->server->api->block->playerBlockBreak($this, $blockVector); break; case ProtocolInfo::PLAYER_ARMOR_EQUIPMENT_PACKET: - if($this->spawned === false or $this->blocked === true){ + if ($this->spawned === false or $this->blocked === true) { break; } $this->craftingItems = []; $this->toCraft = []; $packet->eid = $this->eid; - for($i = 0; $i < 4; ++$i){ + for ($i = 0; $i < 4; ++$i) { $s = $packet->slots[$i]; - if($s === 0 or $s === 255){ + if ($s === 0 or $s === 255) { $s = BlockAPI::getItem(AIR, 0, 0); - }else{ + } else { $s = BlockAPI::getItem($s + 256, 0, 1); } $slot = $this->armor[$i]; - if($slot->getID() !== AIR and $s->getID() === AIR){ + if ($slot->getID() !== AIR and $s->getID() === AIR) { $this->addItem($slot->getID(), $slot->getMetadata(), 1, false); $this->armor[$i] = BlockAPI::getItem(AIR, 0, 0); $packet->slots[$i] = 255; - }elseif($s->getID() !== AIR and $slot->getID() === AIR and ($sl = $this->hasItem($s->getID())) !== false){ + } elseif ($s->getID() !== AIR and $slot->getID() === AIR and ($sl = $this->hasItem($s->getID())) !== false) { $this->armor[$i] = $this->getSlot($sl); $this->setSlot($sl, BlockAPI::getItem(AIR, 0, 0), false); - }elseif($s->getID() !== AIR and $slot->getID() !== AIR and ($slot->getID() !== $s->getID() or $slot->getMetadata() !== $s->getMetadata()) and ($sl = $this->hasItem($s->getID())) !== false){ + } elseif ($s->getID() !== AIR and $slot->getID() !== AIR and ($slot->getID() !== $s->getID() or $slot->getMetadata() !== $s->getMetadata()) and ($sl = $this->hasItem($s->getID())) !== false) { $item = $this->armor[$i]; $this->armor[$i] = $this->getSlot($sl); $this->setSlot($sl, $item, false); - }else{ + } else { $packet->slots[$i] = 255; } } $this->sendArmor(); - if($this->entity->inAction === true){ + if ($this->entity->inAction === true) { $this->entity->inAction = false; $this->entity->inActionCounter = 0; $this->entity->updateMetadata(); } break; case ProtocolInfo::INTERACT_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } $packet->eid = $this->eid; @@ -5485,28 +5494,28 @@ class Player{ $this->craftingItems = []; $this->toCraft = []; $target = $this->server->api->entity->get($packet->target); - if($target instanceof Entity and $this->entity instanceof Entity and $this->gamemode !== VIEW and $this->blocked === false and ($target instanceof Entity) and $this->entity->distance($target) <= 8){ + if ($target instanceof Entity and $this->entity instanceof Entity and $this->gamemode !== VIEW and $this->blocked === false and ($target instanceof Entity) and $this->entity->distance($target) <= 8) { $data["targetentity"] = $packet->target; $data["entity"] = $this->entity; $data["player"] = $this; - if($this->server->handle("player.interact", $data) !== false){ + if ($this->server->handle("player.interact", $data) !== false) { $target->interactWith($this->entity, $packet->action); } } break; case ProtocolInfo::ANIMATE_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } $packet->eid = $this->eid; $this->server->api->dhandle("entity.animate", ["eid" => $packet->eid, "entity" => $this->entity, "action" => $packet->action]); break; case ProtocolInfo::RESPAWN_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } - if(@$this->entity->dead === false){ + if (@$this->entity->dead === false) { break; } $this->craftingItems = []; @@ -5523,18 +5532,18 @@ class Player{ $pk->yaw = $this->entity->yaw; $pk->pitch = $this->entity->pitch; $pk->bodyYaw = $this->entity->headYaw; - foreach($this->entity->level->players as $player){ - if($player->entity->eid != $this->entity->eid){ + foreach ($this->entity->level->players as $player) { + if ($player->entity->eid != $this->entity->eid) { $player->dataPacket(clone $pk); } } - if($this->entity instanceof Entity){ + if ($this->entity instanceof Entity) { $this->entity->fire = 0; $this->entity->air = $this->entity->maxAir; $this->entity->setHealth(20, "respawn", true); $this->entity->updateMetadata(); - }else{ + } else { break; } $this->sendInventory(); @@ -5544,22 +5553,22 @@ class Player{ case ProtocolInfo::SET_HEALTH_PACKET: //Not used break; case ProtocolInfo::ENTITY_EVENT_PACKET: - if($this->spawned === false or $this->blocked === true){ + if ($this->spawned === false or $this->blocked === true) { break; } $this->craftingItems = []; $this->toCraft = []; $packet->eid = $this->eid; - if($this->entity->inAction === true){ + if ($this->entity->inAction === true) { $this->entity->inAction = false; $this->entity->inActionCounter = 0; $this->entity->updateMetadata(); } - switch($packet->event){ + switch ($packet->event) { case 9: //Eating $slot = $this->getSlot($this->slot); $foodHeal = Item::getFoodHeal($slot->getID()); - if($this->entity->getHealth() < 20 && $foodHeal != 0){ + if ($this->entity->getHealth() < 20 && $foodHeal != 0) { $pk = new EntityEventPacket; $pk->eid = 0; $pk->event = 9; @@ -5567,10 +5576,10 @@ class Player{ $this->entity->heal($foodHeal, "eating"); --$slot->count; - if($slot->count <= 0){ + if ($slot->count <= 0) { $this->setSlot($this->slot, BlockAPI::getItem(AIR, 0, 0), false); } - if($slot->getID() === MUSHROOM_STEW or $slot->getID() === BEETROOT_SOUP){ + if ($slot->getID() === MUSHROOM_STEW or $slot->getID() === BEETROOT_SOUP) { $this->addItem(BOWL, 0, 1, false); } } @@ -5578,11 +5587,11 @@ class Player{ } break; case ProtocolInfo::DROP_ITEM_PACKET: - if($this->spawned === false or $this->blocked === true){ + if ($this->spawned === false or $this->blocked === true) { break; } - if($this->gamemode & 0x01 == 1){ + if ($this->gamemode & 0x01 == 1) { ConsoleAPI::warn("{$this->iusername} tried dropping item while in creative!"); return; } @@ -5592,17 +5601,17 @@ class Player{ $packet->item = $this->getSlot($this->slot); $sendOnDrop = false; - if($prevItem->getID() != $packet->item->getID() || $prevItem->getMetadata() != $packet->item->getMetadata()){ - if(count($this->inventory) >= 36){ - foreach($this->inventory as $slot => $item){ - if($item->getID() == 0) goto inv_desync_on_drop; + if ($prevItem->getID() != $packet->item->getID() || $prevItem->getMetadata() != $packet->item->getMetadata()) { + if (count($this->inventory) >= 36) { + foreach ($this->inventory as $slot => $item) { + if ($item->getID() == 0) goto inv_desync_on_drop; } $this->toCraft[] = $prevItem; //vanilla drops only result? $this->lastCraft = microtime(true); break; } - }else { + } else { inv_desync_on_drop: ConsoleAPI::debug("Inventory desync on drop({$this->iusername})"); $sendOnDrop = true; @@ -5614,7 +5623,7 @@ class Player{ $data["unknown"] = $packet->unknown; $data["item"] = $packet->item; $data["player"] = $this; - if($this->blocked === false and $this->server->handle("player.drop", $data) !== false){ + if ($this->blocked === false and $this->server->handle("player.drop", $data) !== false) { $f1 = 0.3; $sX = -sin(($this->entity->yaw / 180) * M_PI) * cos(($this->entity->pitch / 180) * M_PI) * $f1; $sZ = cos(($this->entity->yaw / 180) * M_PI) * cos(($this->entity->pitch / 180) * M_PI) * $f1; @@ -5627,33 +5636,33 @@ class Player{ $sZ += sin($f3) * $f1; $this->server->api->entity->dropRawPos($this->level, $this->entity->x, $this->entity->y - 0.3 + $this->entity->height - 0.12, $this->entity->z, $packet->item, $sX, $sY, $sZ); $this->setSlot($this->slot, BlockAPI::getItem(AIR, 0, 0), $sendOnDrop); - }else{ + } else { $this->sendInventory(); //send if blocked } - if($this->entity->inAction === true){ + if ($this->entity->inAction === true) { $this->entity->inAction = false; $this->entity->inActionCounter = 0; $this->entity->updateMetadata(); } break; case ProtocolInfo::MESSAGE_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } $this->craftingItems = []; $this->toCraft = []; - if(trim($packet->message) != "" and strlen($packet->message) <= 255){ + if (trim($packet->message) != "" and strlen($packet->message) <= 255) { $message = $packet->message; - if($message[0] === "/"){ //Command - if($this instanceof Player){ + if ($message[0] === "/") { //Command + if ($this instanceof Player) { console("[DEBUG] " . FORMAT_AQUA . $this->username . FORMAT_RESET . " issued server command: " . $message); - }else{ + } else { console("[DEBUG] " . FORMAT_YELLOW . "*" . $this . FORMAT_RESET . " issued server command: " . $message); } $this->server->api->console->run(substr($message, 1), $this); - }else{ + } else { $data = ["player" => $this, "message" => $message]; - if(Utils::hasEmoji($data["message"])){ + if (Utils::hasEmoji($data["message"])) { $this->sendChat("Your message contains illegal characters"); break; } @@ -5662,11 +5671,11 @@ class Player{ // Living::$pathfind = !Living::$pathfind; //} - if($this->server->api->handle("player.chat", $data) !== false){ + if ($this->server->api->handle("player.chat", $data) !== false) { $this->server->send2Discord("<" . $this->username . "> " . $message); - if(isset($data["message"])){ + if (isset($data["message"])) { $this->server->api->chat->send($this, $data["message"]); - }else{ + } else { $this->server->api->chat->send($this, $message); } } @@ -5674,14 +5683,14 @@ class Player{ } break; case ProtocolInfo::CONTAINER_CLOSE_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } $this->craftingItems = []; $this->toCraft = []; - if(isset($this->windows[$packet->windowid])){ - if(is_array($this->windows[$packet->windowid])){ - foreach($this->windows[$packet->windowid] as $ob){ + if (isset($this->windows[$packet->windowid])) { + if (is_array($this->windows[$packet->windowid])) { + foreach ($this->windows[$packet->windowid] as $ob) { $pk = new TileEventPacket; $pk->x = $ob->x; $pk->y = $ob->y; @@ -5690,7 +5699,7 @@ class Player{ $pk->case2 = 0; $this->server->api->player->broadcastPacket($this->level->players, $pk); } - }elseif($this->windows[$packet->windowid]->class === TILE_CHEST){ + } elseif ($this->windows[$packet->windowid]->class === TILE_CHEST) { $pk = new TileEventPacket; $pk->x = $this->windows[$packet->windowid]->x; $pk->y = $this->windows[$packet->windowid]->y; @@ -5707,36 +5716,36 @@ class Player{ $this->dataPacket($pk); break; case ProtocolInfo::CONTAINER_SET_SLOT_PACKET: - if($this->spawned === false or $this->blocked === true){ + if ($this->spawned === false or $this->blocked === true) { break; } - if($this->lastCraft <= (microtime(true) - 1)){ - if(isset($this->toCraft[-1])){ + if ($this->lastCraft <= (microtime(true) - 1)) { + if (isset($this->toCraft[-1])) { $this->toCraft = [-1 => $this->toCraft[-1]]; - }else{ + } else { $this->toCraft = []; } $this->craftingItems = []; } - if($packet->windowid === 0){ + if ($packet->windowid === 0) { $craft = false; $slot = $this->getSlot($packet->slot); - if($slot->count >= $packet->item->count and (($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()) or ($packet->item->getID() === AIR and $packet->item->count === 0)) and !isset($this->craftingItems[$packet->slot])){ //Crafting recipe + if ($slot->count >= $packet->item->count and (($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()) or ($packet->item->getID() === AIR and $packet->item->count === 0)) and !isset($this->craftingItems[$packet->slot])) { //Crafting recipe $use = BlockAPI::getItem($slot->getID(), $slot->getMetadata(), $slot->count - $packet->item->count); $this->craftingItems[$packet->slot] = $use; $craft = true; - }elseif($slot->count <= $packet->item->count and ($slot->getID() === AIR or ($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()))){ //Crafting final + } elseif ($slot->count <= $packet->item->count and ($slot->getID() === AIR or ($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()))) { //Crafting final $craftItem = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count - $slot->count); - if(count($this->toCraft) === 0){ + if (count($this->toCraft) === 0) { $this->toCraft[-1] = 0; } $this->toCraft[$packet->slot] = $craftItem; $craft = true; - }elseif(((count($this->toCraft) === 1 and isset($this->toCraft[-1])) or count($this->toCraft) === 0) and $slot->count > 0 and $slot->getID() > AIR and ($slot->getID() !== $packet->item->getID() or $slot->getMetadata() !== $packet->item->getMetadata())){ //Crafting final + } elseif (((count($this->toCraft) === 1 and isset($this->toCraft[-1])) or count($this->toCraft) === 0) and $slot->count > 0 and $slot->getID() > AIR and ($slot->getID() !== $packet->item->getID() or $slot->getMetadata() !== $packet->item->getMetadata())) { //Crafting final $craftItem = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count); - if(count($this->toCraft) === 0){ + if (count($this->toCraft) === 0) { $this->toCraft[-1] = 0; } $use = BlockAPI::getItem($slot->getID(), $slot->getMetadata(), $slot->count); @@ -5745,52 +5754,52 @@ class Player{ $craft = true; } - if($craft === true){ + if ($craft === true) { $this->lastCraft = microtime(true); } - if($craft === true and count($this->craftingItems) > 0 and count($this->toCraft) > 0 and ($recipe = $this->craftItems($this->toCraft, $this->craftingItems, $this->toCraft[-1])) !== true){ - if($recipe === false){ + if ($craft === true and count($this->craftingItems) > 0 and count($this->toCraft) > 0 and ($recipe = $this->craftItems($this->toCraft, $this->craftingItems, $this->toCraft[-1])) !== true) { + if ($recipe === false) { $this->sendInventory(); $this->toCraft = []; - }else{ + } else { $this->toCraft = [-1 => $this->toCraft[-1]]; } $this->craftingItems = []; } - }else{ + } else { $this->toCraft = []; $this->craftingItems = []; } - if(!isset($this->windows[$packet->windowid])){ + if (!isset($this->windows[$packet->windowid])) { break; } - if(is_array($this->windows[$packet->windowid])){ + if (is_array($this->windows[$packet->windowid])) { $tiles = $this->windows[$packet->windowid]; - if($packet->slot >= 0 and $packet->slot < CHEST_SLOTS){ + if ($packet->slot >= 0 and $packet->slot < CHEST_SLOTS) { $tile = $tiles[0]; $slotn = $packet->slot; $offset = 0; - }elseif($packet->slot >= CHEST_SLOTS and $packet->slot <= (CHEST_SLOTS << 1)){ + } elseif ($packet->slot >= CHEST_SLOTS and $packet->slot <= (CHEST_SLOTS << 1)) { $tile = $tiles[1]; $slotn = $packet->slot - CHEST_SLOTS; $offset = CHEST_SLOTS; - }else{ + } else { break; } $item = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count); $slot = $tile->getSlot($slotn); - if($this->server->api->dhandle("player.container.slot", [ + if ($this->server->api->dhandle("player.container.slot", [ "tile" => $tile, "slot" => $packet->slot, "offset" => $offset, "slotdata" => $slot, "itemdata" => $item, "player" => $this - ]) === false){ + ]) === false) { $pk = new ContainerSetSlotPacket; $pk->windowid = $packet->windowid; $pk->slot = $packet->slot; @@ -5798,36 +5807,36 @@ class Player{ $this->dataPacket($pk); break; } - if($item->getID() !== AIR and $slot->getID() == $item->getID()){ - if($slot->count < $item->count){ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count - $slot->count, false) === false){ + if ($item->getID() !== AIR and $slot->getID() == $item->getID()) { + if ($slot->count < $item->count) { + if ($this->removeItem($item->getID(), $item->getMetadata(), $item->count - $slot->count, false) === false) { break; } - }elseif($slot->count > $item->count){ + } elseif ($slot->count > $item->count) { $this->addItem($item->getID(), $item->getMetadata(), $slot->count - $item->count, false); } - }else{ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count, false) === false){ + } else { + if ($this->removeItem($item->getID(), $item->getMetadata(), $item->count, false) === false) { break; } $this->addItem($slot->getID(), $slot->getMetadata(), $slot->count, false); } $tile->setSlot($slotn, $item, true, $offset); - }else{ + } else { $tile = $this->windows[$packet->windowid]; - if(($tile->class !== TILE_CHEST and $tile->class !== TILE_FURNACE) or $packet->slot < 0 or ($tile->class === TILE_CHEST and $packet->slot >= CHEST_SLOTS) or ($tile->class === TILE_FURNACE and $packet->slot >= FURNACE_SLOTS)){ + if (($tile->class !== TILE_CHEST and $tile->class !== TILE_FURNACE) or $packet->slot < 0 or ($tile->class === TILE_CHEST and $packet->slot >= CHEST_SLOTS) or ($tile->class === TILE_FURNACE and $packet->slot >= FURNACE_SLOTS)) { break; } $item = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count); $slot = $tile->getSlot($packet->slot); - if($this->server->api->dhandle("player.container.slot", [ + if ($this->server->api->dhandle("player.container.slot", [ "tile" => $tile, "slot" => $packet->slot, "slotdata" => $slot, "itemdata" => $item, "player" => $this, - ]) === false){ + ]) === false) { $pk = new ContainerSetSlotPacket; $pk->windowid = $packet->windowid; $pk->slot = $packet->slot; @@ -5836,24 +5845,24 @@ class Player{ break; } - if($tile->class === TILE_FURNACE and $packet->slot == 2){ - switch($slot->getID()){ + if ($tile->class === TILE_FURNACE and $packet->slot == 2) { + switch ($slot->getID()) { case IRON_INGOT: AchievementAPI::grantAchievement($this, "acquireIron"); break; } } - if($item->getID() !== AIR and $slot->getID() == $item->getID()){ - if($slot->count < $item->count){ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count - $slot->count, false) === false){ + if ($item->getID() !== AIR and $slot->getID() == $item->getID()) { + if ($slot->count < $item->count) { + if ($this->removeItem($item->getID(), $item->getMetadata(), $item->count - $slot->count, false) === false) { break; } - }elseif($slot->count > $item->count){ + } elseif ($slot->count > $item->count) { $this->addItem($item->getID(), $item->getMetadata(), $slot->count - $item->count, false); } - }else{ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count, false) === false){ + } else { + if ($this->removeItem($item->getID(), $item->getMetadata(), $item->count, false) === false) { break; } $this->addItem($slot->getID(), $slot->getMetadata(), $slot->count, false); @@ -5863,27 +5872,27 @@ class Player{ } break; case ProtocolInfo::SEND_INVENTORY_PACKET: - if($this->spawned === false){ + if ($this->spawned === false) { break; } break; case ProtocolInfo::ENTITY_DATA_PACKET: - if($this->spawned === false or $this->blocked === true){ + if ($this->spawned === false or $this->blocked === true) { break; } $this->craftingItems = []; $this->toCraft = []; $t = $this->server->api->tile->get(new Position($packet->x, $packet->y, $packet->z, $this->level)); - if(($t instanceof Tile) and $t->class === TILE_SIGN){ - if($t->data["creator"] !== $this->username){ + if (($t instanceof Tile) and $t->class === TILE_SIGN) { + if ($t->data["creator"] !== $this->username) { $t->spawn($this); - }else{ + } else { $nbt = new NBT(); $nbt->load($packet->namedtag); $d = array_shift($nbt->tree); - if($d["id"] !== TILE_SIGN){ + if ($d["id"] !== TILE_SIGN) { $t->spawn($this); - }else{ + } else { $t->setText($d["Text1"], $d["Text2"], $d["Text3"], $d["Text4"]); } } @@ -5895,13 +5904,13 @@ class Player{ $this->entity->moveForward = $packet->moveForward; $this->entity->moveStrafing = $packet->moveStrafe; - if(strlen(bin2hex($packet->buffer)) === 24 && $this->entity->linkedEntity != 0){ + if (strlen(bin2hex($packet->buffer)) === 24 && $this->entity->linkedEntity != 0) { $this->entity->stopRiding(); break; } - if($this->entity->linkedEntity != 0){ //TODO better riding + if ($this->entity->linkedEntity != 0) { //TODO better riding $e = $this->entity->level->entityList[$this->entity->linkedEntity] ?? false; - if($e === false) { + if ($e === false) { ConsoleAPI::warn("Player is riding on entity that doesnt exist in world! ({$this->iusername}, {$this->entity->linkedEntity})"); $this->entity->stopRiding(); break; @@ -5926,1125 +5935,7 @@ class Player{ break; } } -======= - - public function sendPing() { - $pk = new PingPacket; - $pk->time = intdiv(hrtime(true), 1_000_000); - $this->directDataPacket($pk); - } - - public function getPing() { - return $this->lastPing; - } - - public function handleDataPacket(RakNetDataPacket $packet){ - if($this->connected === false){ - return; - } - - if(EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY){ - return; - } - - switch($packet->pid()){ - case 0x01: - break; - case ProtocolInfo::PONG_PACKET: - $currentTime = intdiv(hrtime(true), 1_000_000); - if($currentTime > $packet->ptime){ - $this->lastPing = $currentTime - $packet->ptime; - } - break; - case ProtocolInfo::PING_PACKET: - $pk = new PongPacket; - $pk->ptime = $packet->time; - $pk->time = intdiv(hrtime(true), 1_000_000); - $this->directDataPacket($pk); - break; - case ProtocolInfo::DISCONNECT_PACKET: - $this->close("client disconnect"); - break; - case ProtocolInfo::CLIENT_CONNECT_PACKET: - if($this->loggedIn === true){ - break; - } - $pk = new ServerHandshakePacket; - $pk->port = $this->port; - $pk->session = $packet->session; - $pk->session2 = Utils::readLong("\x00\x00\x00\x00\x04\x44\x0b\xa9"); - $this->dataPacket($pk); - break; - case ProtocolInfo::CLIENT_HANDSHAKE_PACKET: - if($this->loggedIn === true){ - break; - } - break; - case ProtocolInfo::LOGIN_PACKET: - if($this->loggedIn === true){ - break; - } - $this->username = $packet->username; - $this->iusername = strtolower($this->username); - $this->loginData = ["clientId" => $packet->clientId, "loginData" => $packet->loginData]; - if(count($this->server->clients) > $this->server->maxClients and !$this->server->api->ban->isOp($this->iusername)){ - $this->close("server is full!", false); - return; - } - if($packet->protocol1 !== ProtocolInfo::CURRENT_PROTOCOL){ - if($packet->protocol1 < ProtocolInfo::CURRENT_PROTOCOL){ - $pk = new LoginStatusPacket; - $pk->status = 1; - $this->directDataPacket($pk); - }else{ - $pk = new LoginStatusPacket; - $pk->status = 2; - $this->directDataPacket($pk); - } - $this->close("Incorrect protocol #" . $packet->protocol1, false); - return; - } - if(preg_match('#[^a-zA-Z0-9_]#', $this->username) > 0 || $this->username === "" || $this->iusername === "rcon" || $this->iusername === "console" || $this->iusername === "server" || strlen($this->iusername) > 16){ - $this->close("Bad username", false); - return; - } - if($this->server->api->handle("player.connect", $this) === false){ - $this->close("Unknown reason", false); - return; - } - - if($this->server->whitelist === true and !$this->server->api->ban->inWhitelist($this->iusername)){ - $this->close("Server is white-listed", false); - return; - }elseif($this->server->api->ban->isBanned($this->iusername) or $this->server->api->ban->isIPBanned($this->ip)){ - $this->close("You are banned!", false); - return; - } - $this->loggedIn = true; - - if(!isset($this->CID) or $this->CID == null){ - console("[DEBUG] Player " . $this->username . " does not have a CID", true, true, 2); - $this->CID = Utils::readLong(Utils::getRandomBytes(8, false)); - } - $u = $this->server->api->player->get($this->iusername, false); - if($u !== false){ - $u = $this->server->clients[$this->CID]; - $u->close("this player already in game"); - } - - $this->server->api->player->add($this->CID); - if($this->server->api->handle("player.join", $this) === false){ - $this->close("join cancelled", false); - return; - } - - if(!($this->data instanceof Config)){ - $this->close("no config created", false); - return; - } - - $this->auth = true; - if(!$this->data->exists("inventory") or ($this->gamemode & 0x01) === 0x01){ - if(($this->gamemode & 0x01) === 0x01){ - $inv = []; - if(($this->gamemode & 0x02) === 0x02){ - foreach(BlockAPI::$creative as $item){ - $inv[] = [0, 0, 1]; - } - }else{ - foreach(BlockAPI::$creative as $item){ - $inv[] = [$item[0], $item[1], 1]; - } - } - } - $this->data->set("inventory", $inv); - } - $this->achievements = $this->data->get("achievements"); - $this->data->set("caseusername", $this->username); - $this->inventory = []; - foreach($this->data->get("inventory") as $slot => $item){ - if(!is_array($item) or count($item) < 3){ - $item = [AIR, 0, 0]; - } - $this->inventory[$slot] = BlockAPI::getItem($item[0], $item[1], $item[2]); - } - - $this->armor = []; - foreach($this->data->get("armor") as $slot => $item){ - $this->armor[$slot] = BlockAPI::getItem($item[0], $item[1], $item[0] === 0 ? 0 : 1); - } - - $this->data->set("lastIP", $this->ip); - $this->data->set("lastID", $this->clientID); - - $this->server->api->player->saveOffline($this->data); - - - $pk = new LoginStatusPacket; - $pk->status = 0; - $this->dataPacket($pk); - - $pk = new StartGamePacket; - $pk->seed = $this->level->getSeed(); - $pk->x = $this->data->get("position")["x"]; - $pk->y = $this->data->get("position")["y"]; - $pk->z = $this->data->get("position")["z"]; - $pk->generator = 0; - $pk->gamemode = $this->gamemode & 0x01; - $pk->eid = 0; - $this->dataPacket($pk); - - if(($this->gamemode & 0x01) === 0x01){ - $this->slot = 0; - $this->hotbar = []; - }elseif($this->data->exists("hotbar")){ - $this->hotbar = $this->data->get("hotbar"); - $this->slot = $this->hotbar[0]; - }else{ - $this->slot = 0; - $this->hotbar = [0, 1, 2, 3, 4, 5, 6, 7, 8]; - } - for($i = 0; $i < count($this->hotbar); ++$i){ - if($this->hotbar[$i] > 36) $this->hotbar[$i] = -1; //XXX unsafe? - if($this->hotbar[$i] < -1) $this->hotbar[$i] = -1; - } - if($this->data->exists("slot-count")){ - $this->slotCount = $this->data->get("slot-count"); - }else{ - $this->data->set("slot-count", $this->slotCount); - } - - $this->entity = $this->server->api->entity->add($this->level, ENTITY_PLAYER, 0, ["player" => $this]); - $this->eid = $this->entity->eid; - $this->server->query("UPDATE players SET EID = " . $this->eid . " WHERE CID = " . $this->CID . ";"); - $this->entity->x = $this->data->get("position")["x"]; - $this->entity->y = $this->data->get("position")["y"]; - $this->entity->z = $this->data->get("position")["z"]; - if(($level = $this->server->api->level->get($this->data->get("spawn")["level"])) !== false){ - $this->spawnPosition = new Position($this->data->get("spawn")["x"], $this->data->get("spawn")["y"], $this->data->get("spawn")["z"], $level); - - $pk = new SetSpawnPositionPacket; - $pk->x = (int) $this->spawnPosition->x; - $pk->y = (int) $this->spawnPosition->y; - $pk->z = (int) $this->spawnPosition->z; - $this->dataPacket($pk); - } - $this->entity->check = false; - $this->entity->setName($this->username); - $this->entity->data["CID"] = $this->CID; - $this->evid[] = $this->server->event("server.chat", [$this, "eventHandler"]); - $this->evid[] = $this->server->event("entity.motion", [$this, "eventHandler"]); - $this->evid[] = $this->server->event("entity.animate", [$this, "eventHandler"]); - $this->evid[] = $this->server->event("entity.event", [$this, "eventHandler"]); - $this->evid[] = $this->server->event("entity.metadata", [$this, "eventHandler"]); - $this->evid[] = $this->server->event("entity.link", [$this, "eventHandler"]); - $this->evid[] = $this->server->event("player.equipment.change", [$this, "eventHandler"]); - $this->evid[] = $this->server->event("player.armor", [$this, "eventHandler"]); - $this->evid[] = $this->server->event("player.pickup", [$this, "eventHandler"]); - $this->evid[] = $this->server->event("tile.container.slot", [$this, "eventHandler"]); - $this->evid[] = $this->server->event("tile.update", [$this, "eventHandler"]); - $this->lastMeasure = microtime(true); - $this->server->schedule(50, [$this, "measureLag"], [], true); - - $pk = new SetTimePacket; - $pk->time = $this->level->getTime(); - $pk->started = !$this->level->isTimeStopped(); - $this->dataPacket($pk); - - - console("[INFO] " . FORMAT_AQUA . $this->username . FORMAT_RESET . "[/" . $this->ip . ":" . $this->port . "] logged in with entity id " . $this->eid . " at (" . $this->entity->level->getName() . ", " . round($this->entity->x, 2) . ", " . round($this->entity->y, 2) . ", " . round($this->entity->z, 2) . ")"); - break; - case ProtocolInfo::READY_PACKET: - if($this->loggedIn === false){ - break; - } - switch($packet->status){ - case 1: //Spawn!! - if($this->spawned !== false){ - break; - } - - $pos = new Position($this->entity->x, $this->entity->y, $this->entity->z, $this->level); - $pData = $this->data->get("position"); - $this->entity->setHealth($this->data->get("health"), "spawn", true, false); - $this->spawned = true; - $this->teleport($pos, $pData["yaw"] ?? false, $pData["pitch"] ?? false, true, true); - $this->server->api->player->spawnAllPlayers($this); - $this->server->api->player->spawnToAllPlayers($this); - $this->server->api->entity->spawnAll($this); - $this->server->api->entity->spawnToAll($this->entity); - - //$this->server->schedule(5, [$this->entity, "update"], [], true); - //$this->server->schedule(2, [$this->entity, "updateMovement"], [], true); - $this->sendArmor(); - $array = explode("@n", (string)$this->server->motd); - foreach($array as $msg){ - $this->sendChat($msg."\n"); - } - - $this->sendInventory(); - $this->sendSettings(); - $this->server->schedule(50, [$this, "orderChunks"], []); - $this->blocked = false; - - $this->server->send2Discord($this->username . " joined the game"); - $this->server->handle("player.spawn", $this); - break; - case 2://Chunk loaded? - break; - } - break; - case ProtocolInfo::ROTATE_HEAD_PACKET: - if($this->spawned === false){ - break; - } - if(($this->entity instanceof Entity)){ - if($this->blocked === true or $this->server->api->handle("player.move", $this->entity) === false){ - if($this->lastCorrect instanceof Vector3 && !$this->entity->dead){ - $this->teleport($this->lastCorrect, $this->entity->yaw, $this->entity->pitch, false); - } - }else{ - $this->entity->setPosition($this->entity, $packet->yaw, $this->entity->pitch); - } - } - break; - case ProtocolInfo::MOVE_PLAYER_PACKET: - if($this->spawned === false){ - break; - } - if($this->isSleeping) break; - if(($this->entity instanceof Entity) and $packet->messageIndex > $this->lastMovement){ - $this->lastMovement = $packet->messageIndex; - $newPos = new Vector3($packet->x, $packet->y, $packet->z); - if($this->forceMovement instanceof Vector3){ - if($this->forceMovement->distance($newPos) <= 0.7){ - $this->forceMovement = false; - }else{ - $this->teleport($this->forceMovement, $this->entity->yaw, $this->entity->pitch, false); - break; - } - } - $speed = $this->entity->getSpeedMeasure(); - if($this->blocked === true or ($this->server->api->getProperty("allow-flight") !== true and (($speed > 9 and ($this->gamemode & 0x01) === 0x00) or $speed > 20 or $this->entity->distance($newPos) > 7)) or $this->server->api->handle("player.move", $this->entity) === false){ - if($this->lastCorrect instanceof Vector3 && !$this->entity->dead){ - $this->teleport($this->lastCorrect, $this->entity->yaw, $this->entity->pitch, false); - } - }else{ - $this->entity->setPosition($newPos, $packet->yaw, $packet->pitch, $packet->bodyYaw); - } - $this->entity->updateAABB(); - } - break; - case ProtocolInfo::PLAYER_EQUIPMENT_PACKET: - if($this->spawned === false){ - break; - } - $packet->eid = $this->eid; - - $data = []; - $data["eid"] = $packet->eid; - $data["player"] = $this; - - if($packet->slot === 0){ - $data["slot"] = -1; - $data["item"] = BlockAPI::getItem(AIR, 0, 0); - if($this->server->handle("player.equipment.change", $data) !== false){ - $this->slot = -1; - } - break; - }else if($packet->slot > 0){ - $packet->slot -= 9; - } - - - if(($this->gamemode & 0x01) === SURVIVAL){ - $data["item"] = $this->getSlot($packet->slot); - if(!($data["item"] instanceof Item)){ - break; - } - }elseif(($this->gamemode & 0x01) === CREATIVE){ - $packet->slot = false; - foreach(BlockAPI::$creative as $i => $d){ - if($d[0] === $packet->item and $d[1] === $packet->meta){ - $packet->slot = $i; - } - } - if($packet->slot !== false){ - $data["item"] = $this->getSlot($packet->slot); - }else{ - break; - } - }else{ - break;//????? - } - - $data["slot"] = $packet->slot; - - if($this->server->handle("player.equipment.change", $data) !== false){ - if(!Player::$experimentalHotbar) $this->slot = $packet->slot; - if(($this->gamemode & 0x01) === SURVIVAL){ - $has = false; - $slotPos = 0; - $packetSlotPos = 0; - for($i = 0; $i < $this->slotCount; ++$i){ - if($this->slot == $this->hotbar[$i]) $slotPos = $i; - if($packet->slot == $this->hotbar[$i]){ - $packetSlotPos = $i; - $has = true; - break; - } - } - - if(Player::$experimentalHotbar && $has) { - $this->slot = $packet->slot; - $this->curHotbarIndex = $packetSlotPos; - } - if(!$has){ - if(Player::$experimentalHotbar) { - $this->slot = $packet->slot; - $this->hotbar[$this->curHotbarIndex] = $packet->slot; - }else{ - $this->curHotbarIndex = 0; - array_pop($this->hotbar); - array_unshift($this->hotbar, $this->slot); - } - } - if(Player::$experimentalHotbar) $this->sendInventory(); - }else{ - $this->slot = $packet->slot; - } - }else{ - //$this->sendInventorySlot($packet->slot); - $this->sendInventory(); - } - if($this->entity->inAction === true){ - $this->entity->inAction = false; - $this->entity->inActionCounter = 0; - $this->entity->updateMetadata(); - } - break; - case ProtocolInfo::REQUEST_CHUNK_PACKET: - //console("request x:".$packet->chunkX.", z: ".$packet->chunkZ." chunk"); - //$this->useChunk($packet->chunkX, $packet->chunkZ); - //$this->lastChunk = [$packet->chunkX, $packet->chunkZ]; - break; - case ProtocolInfo::USE_ITEM_PACKET: - if(!($this->entity instanceof Entity)){ - break; - } - - $blockVector = new Vector3($packet->x, $packet->y, $packet->z); - - if(($this->spawned === false or $this->blocked === true) and $packet->face >= 0 and $packet->face <= 5){ - $target = $this->level->getBlock($blockVector); - $block = $target->getSide($packet->face); - - $pk = new UpdateBlockPacket; - $pk->x = $target->x; - $pk->y = $target->y; - $pk->z = $target->z; - $pk->block = $target->getID(); - $pk->meta = $target->getMetadata(); - $this->dataPacket($pk); - - $pk = new UpdateBlockPacket; - $pk->x = $block->x; - $pk->y = $block->y; - $pk->z = $block->z; - $pk->block = $block->getID(); - $pk->meta = $block->getMetadata(); - $this->dataPacket($pk); - break; - } - $this->craftingItems = []; - $this->toCraft = []; - $packet->eid = $this->eid; - $data = []; - $data["eid"] = $packet->eid; - $data["player"] = $this; - $data["face"] = $packet->face; - $data["x"] = $packet->x; - $data["y"] = $packet->y; - $data["z"] = $packet->z; - $data["item"] = $packet->item; - $data["meta"] = $packet->meta; - $data["fx"] = $packet->fx; - $data["fy"] = $packet->fy; - $data["fz"] = $packet->fz; - $data["posX"] = $packet->posX; - $data["posY"] = $packet->posY; - $data["posZ"] = $packet->posZ; - - if($packet->face >= 0 and $packet->face <= 5){ //Use Block, place - if($this->entity->inAction === true){ - $this->entity->inAction = false; - $this->entity->inActionCounter = 0; - $this->entity->updateMetadata(); - } - - if($this->blocked === true or ($this->entity->position instanceof Vector3 and $blockVector->distance($this->entity->position) > 10)){ - - }elseif($this->getSlot($this->slot)->getID() !== $packet->item or ($this->getSlot($this->slot)->isTool() === false and $this->getSlot($this->slot)->getMetadata() !== $packet->meta)){ - $this->sendInventorySlot($this->slot); - }else{ - $this->server->api->block->playerBlockAction($this, $blockVector, $packet->face, $packet->fx, $packet->fy, $packet->fz); - break; - } - $target = $this->level->getBlock($blockVector); - $block = $target->getSide($packet->face); - - $pk = new UpdateBlockPacket; - $pk->x = $target->x; - $pk->y = $target->y; - $pk->z = $target->z; - $pk->block = $target->getID(); - $pk->meta = $target->getMetadata(); - $this->dataPacket($pk); - - $pk = new UpdateBlockPacket; - $pk->x = $block->x; - $pk->y = $block->y; - $pk->z = $block->z; - $pk->block = $block->getID(); - $pk->meta = $block->getMetadata(); - $this->dataPacket($pk); - break; - }elseif($packet->face === 0xff){ - - $slotItem = $this->getHeldItem(); - if($slotItem->getID() == SNOWBALL || $slotItem->getID() == EGG){ //TODO better way - $x = $packet->x * 0.000030518; - $y = $packet->y * 0.000030518; - $z = $packet->z * 0.000030518; - - $d = sqrt($x*$x + $y*$y + $z*$z); - - if($d >= 0.0001){ - $shootX = $x / $d; - $shootY = $y / $d; - $shootZ = $z / $d; - - $data = [ - "x" => $this->entity->x, - "y" => $this->entity->y + $this->entity->getEyeHeight(), - "z" => $this->entity->z, - "yaw" => $this->entity->yaw, - "pitch" => $this->entity->pitch, - "shooter" => $this->entity->eid, - "shootX" => $shootX, - "shootY" => $shootY, - "shootZ" => $shootZ - ]; - - if($slotItem->getID() == EGG){ - $e = $this->server->api->entity->add($this->entity->level, ENTITY_OBJECT, OBJECT_EGG, $data); - }else{ - $e = $this->server->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_SNOWBALL, $data); - } - - if(($this->gamemode & 0x01) == 0x0) { - if($slotItem !== false){ - if($slotItem->count == 1) $this->inventory[$this->slot] = BlockAPI::getItem(AIR, 0, 0); - else $slotItem->count -= 1; - //$this->sendInventory(); - } - } - - $this->server->api->entity->spawnToAll($e); - } - - }else{ - if($this->server->handle("player.action", $data) !== false){ - $this->entity->inAction = true; - $this->entity->inActionCounter = 0; - $this->startAction = microtime(true); - $this->entity->updateMetadata(); - } - } - } - break; - case ProtocolInfo::PLAYER_ACTION_PACKET: - if($this->spawned === false or $this->blocked === true){ - break; - } - $packet->eid = $this->eid; - $this->craftingItems = []; - $this->toCraft = []; - - switch($packet->action){ - case 5: //Shot arrow - if($this->entity->inAction){ - $arrowSlot = $this->hasItem(ARROW); - if($this->getSlot($this->slot)->getID() === BOW && (($this->gamemode & 0x01) == 0x1 || $arrowSlot !== false)){ - if($this->startAction !== false){ - $initalPower = $this->entity->inActionCounter; - $power = $initalPower / 20; - $power = ($power * $power + $power * 2) / 3; - if($power >= 0.1){ - if($power > 1) $power = 1; - $this->server->dhandle("player.shoot", [ - "player" => $this, - "power" => &$power, - ]); - - $d = [ - "x" => $this->entity->x, - "y" => $this->entity->y + 1.6, - "z" => $this->entity->z, - "yaw" => $this->entity->yaw, - "pitch" => $this->entity->pitch, - "shooter" => $this->entity->eid, - ]; - $e = $this->server->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_ARROW, $d); - $e->speedX = -sin(($e->yaw / 180) * M_PI) * cos(($e->pitch / 180) * M_PI); - $e->speedZ = cos(($e->yaw / 180) * M_PI) * cos(($e->pitch / 180) * M_PI); - $e->speedY = -sin(($e->pitch / 180) * M_PI); - $e->shooterEID = $this->entity->eid; - $e->shotByEntity = true; - /** - * Max usage: 72000ticks - * initalPower = 72000 - (72000 - usedCtr) - * power = initialPower / 20' - * power = (power*power+power*2)/3 - * powerMax is 1, powerMin is 0.1 - * args: xvel, yvel, zvel, (power+power)*1.5, 1.0 - */ - $e->critical = ($power == 1); - $e->shoot($e->speedX, $e->speedY, $e->speedZ, ($power+$power) * 1.5, 1.0); - $this->server->api->entity->spawnToAll($e); - if(($this->gamemode & 0x01) == 0x0) { - $bow = $this->getSlot($this->slot); - if(++$bow->meta >= $bow->getMaxDurability()){ - $this->inventory[$this->slot] = BlockAPI::getItem(AIR, 0, 0); - } - $this->removeItem(ARROW, 0, 1, false); - $this->sendInventory(); - } - } - } - }else{ //inv desynced, resend - $this->sendInventory(); - } - } - $this->startAction = false; - $this->entity->inAction = false; - $this->entity->inActionCounter = 0; - $this->entity->updateMetadata(); - break; - case 6: //get out of the bed - $this->stopSleep(); - } - break; - case ProtocolInfo::REMOVE_BLOCK_PACKET: - $blockVector = new Vector3($packet->x, $packet->y, $packet->z); - if($this->spawned === false or $this->blocked === true or $this->entity->distance($blockVector) > 8){ - $target = $this->level->getBlock($blockVector); - - $pk = new UpdateBlockPacket; - $pk->x = $target->x; - $pk->y = $target->y; - $pk->z = $target->z; - $pk->block = $target->getID(); - $pk->meta = $target->getMetadata(); - $this->dataPacket($pk); - break; - } - $this->craftingItems = []; - $this->toCraft = []; - $this->server->api->block->playerBlockBreak($this, $blockVector); - break; - case ProtocolInfo::PLAYER_ARMOR_EQUIPMENT_PACKET: - if($this->spawned === false or $this->blocked === true){ - break; - } - $this->craftingItems = []; - $this->toCraft = []; - - $packet->eid = $this->eid; - for($i = 0; $i < 4; ++$i){ - $s = $packet->slots[$i]; - if($s === 0 or $s === 255){ - $s = BlockAPI::getItem(AIR, 0, 0); - }else{ - $s = BlockAPI::getItem($s + 256, 0, 1); - } - $slot = $this->armor[$i]; - if($slot->getID() !== AIR and $s->getID() === AIR){ - $this->addItem($slot->getID(), $slot->getMetadata(), 1, false); - $this->armor[$i] = BlockAPI::getItem(AIR, 0, 0); - $packet->slots[$i] = 255; - }elseif($s->getID() !== AIR and $slot->getID() === AIR and ($sl = $this->hasItem($s->getID())) !== false){ - $this->armor[$i] = $this->getSlot($sl); - $this->setSlot($sl, BlockAPI::getItem(AIR, 0, 0), false); - }elseif($s->getID() !== AIR and $slot->getID() !== AIR and ($slot->getID() !== $s->getID() or $slot->getMetadata() !== $s->getMetadata()) and ($sl = $this->hasItem($s->getID())) !== false){ - $item = $this->armor[$i]; - $this->armor[$i] = $this->getSlot($sl); - $this->setSlot($sl, $item, false); - }else{ - $packet->slots[$i] = 255; - } - - } - $this->sendArmor(); - if($this->entity->inAction === true){ - $this->entity->inAction = false; - $this->entity->inActionCounter = 0; - $this->entity->updateMetadata(); - } - break; - case ProtocolInfo::INTERACT_PACKET: - if($this->spawned === false){ - break; - } - $packet->eid = $this->eid; - $data = []; - $data["target"] = $packet->target; - $data["eid"] = $packet->eid; - $data["action"] = $packet->action; - $this->craftingItems = []; - $this->toCraft = []; - $target = $this->server->api->entity->get($packet->target); - if($target instanceof Entity and $this->entity instanceof Entity and $this->gamemode !== VIEW and $this->blocked === false and ($target instanceof Entity) and $this->entity->distance($target) <= 8){ - $data["targetentity"] = $packet->target; - $data["entity"] = $this->entity; - $data["player"] = $this; - if($this->server->handle("player.interact", $data) !== false){ - $target->interactWith($this->entity, $packet->action); - } - } - - break; - case ProtocolInfo::ANIMATE_PACKET: - if($this->spawned === false){ - break; - } - $packet->eid = $this->eid; - $this->server->api->dhandle("entity.animate", ["eid" => $packet->eid, "entity" => $this->entity, "action" => $packet->action]); - break; - case ProtocolInfo::RESPAWN_PACKET: - if($this->spawned === false){ - break; - } - if(@$this->entity->dead === false){ - break; - } - $this->craftingItems = []; - $this->toCraft = []; - - $this->checkSpawnPosition(); - $this->teleport($this->spawnPosition, false, false, true, false); - - $pk = new MovePlayerPacket(); - $pk->eid = $this->entity->eid; - $pk->x = $this->entity->x; - $pk->y = $this->entity->y; - $pk->z = $this->entity->z; - $pk->yaw = $this->entity->yaw; - $pk->pitch = $this->entity->pitch; - $pk->bodyYaw = $this->entity->headYaw; - foreach($this->entity->level->players as $player){ - if($player->entity->eid != $this->entity->eid){ - $player->dataPacket(clone $pk); - } - } - - if($this->entity instanceof Entity){ - $this->entity->fire = 0; - $this->entity->air = $this->entity->maxAir; - $this->entity->setHealth(20, "respawn", true); - $this->entity->updateMetadata(); - }else{ - break; - } - $this->sendInventory(); - $this->blocked = false; - $this->server->handle("player.respawn", $this); - break; - case ProtocolInfo::SET_HEALTH_PACKET: //Not used - break; - case ProtocolInfo::ENTITY_EVENT_PACKET: - if($this->spawned === false or $this->blocked === true){ - break; - } - $this->craftingItems = []; - $this->toCraft = []; - $packet->eid = $this->eid; - if($this->entity->inAction === true){ - $this->entity->inAction = false; - $this->entity->inActionCounter = 0; - $this->entity->updateMetadata(); - } - switch($packet->event){ - case 9: //Eating - $slot = $this->getSlot($this->slot); - $foodHeal = Item::getFoodHeal($slot->getID()); - if($this->entity->getHealth() < 20 && $foodHeal != 0){ - $pk = new EntityEventPacket; - $pk->eid = 0; - $pk->event = 9; - $this->dataPacket($pk); - - $this->entity->heal($foodHeal, "eating"); - --$slot->count; - if($slot->count <= 0){ - $this->setSlot($this->slot, BlockAPI::getItem(AIR, 0, 0), false); - } - if($slot->getID() === MUSHROOM_STEW or $slot->getID() === BEETROOT_SOUP){ - $this->addItem(BOWL, 0, 1, false); - } - } - break; - } - break; - case ProtocolInfo::DROP_ITEM_PACKET: - if($this->spawned === false or $this->blocked === true){ - break; - } - - if($this->gamemode & 0x01 == 1){ - ConsoleAPI::warn("{$this->iusername} tried dropping item while in creative!"); - return; - } - - $packet->eid = $this->eid; - $prevItem = $packet->item; - $packet->item = $this->getSlot($this->slot); - $sendOnDrop = false; - - if($prevItem->getID() != $packet->item->getID() || $prevItem->getMetadata() != $packet->item->getMetadata()){ - if(count($this->inventory) >= 36){ - foreach($this->inventory as $slot => $item){ - if($item->getID() == 0) goto inv_desync_on_drop; - } - - $this->toCraft[] = $prevItem; //vanilla drops only result? - $this->lastCraft = microtime(true); - break; - }else{ - inv_desync_on_drop: - ConsoleAPI::debug("Inventory desync on drop({$this->iusername})"); - $sendOnDrop = true; - } - } - - $this->craftingItems = []; - $this->toCraft = []; - $data["eid"] = $packet->eid; - $data["unknown"] = $packet->unknown; - $data["item"] = $packet->item; - $data["player"] = $this; - if($this->blocked === false and $this->server->handle("player.drop", $data) !== false){ - $f1 = 0.3; - $sX = -sin(($this->entity->yaw / 180) * M_PI) * cos(($this->entity->pitch / 180) * M_PI) * $f1; - $sZ = cos(($this->entity->yaw / 180) * M_PI) * cos(($this->entity->pitch / 180) * M_PI) * $f1; - $sY = -sin(($this->entity->pitch / 180) * M_PI) * $f1 + 0.1; - $f1 = 0.02; - $f3 = $this->entity->random->nextFloat() * M_PI * 2.0; - $f1 *= $this->entity->random->nextFloat(); - $sX += cos($f3) * $f1; - $sY += ($this->entity->random->nextFloat() - $this->entity->random->nextFloat()) * 0.1; - $sZ += sin($f3) * $f1; - $this->server->api->entity->dropRawPos($this->level, $this->entity->x, $this->entity->y - 0.3 + $this->entity->height - 0.12, $this->entity->z, $packet->item, $sX, $sY, $sZ); - $this->setSlot($this->slot, BlockAPI::getItem(AIR, 0, 0), $sendOnDrop); - }else{ - $this->sendInventory(); //send if blocked - } - if($this->entity->inAction === true){ - $this->entity->inAction = false; - $this->entity->inActionCounter = 0; - $this->entity->updateMetadata(); - } - break; - case ProtocolInfo::MESSAGE_PACKET: - if($this->spawned === false){ - break; - } - $this->craftingItems = []; - $this->toCraft = []; - if(trim($packet->message) != "" and strlen($packet->message) <= 255){ - $message = $packet->message; - if($message[0] === "/"){ //Command - if($this instanceof Player){ - console("[DEBUG] " . FORMAT_AQUA . $this->username . FORMAT_RESET . " issued server command: " . $message); - }else{ - console("[DEBUG] " . FORMAT_YELLOW . "*" . $this . FORMAT_RESET . " issued server command: " . $message); - } - $this->server->api->console->run(substr($message, 1), $this); - }else{ - $data = ["player" => $this, "message" => $message]; - if(Utils::hasEmoji($data["message"])){ - $this->sendChat("Your message contains illegal characters"); - break; - } - - //if($message == "pf"){ - // Living::$pathfind = !Living::$pathfind; - //} - - if($this->server->api->handle("player.chat", $data) !== false){ - $this->server->send2Discord("<" . $this->username . "> " . $message); - if(isset($data["message"])){ - $this->server->api->chat->send($this, $data["message"]); - }else{ - $this->server->api->chat->send($this, $message); - } - } - } - } - break; - case ProtocolInfo::CONTAINER_CLOSE_PACKET: - if($this->spawned === false){ - break; - } - $this->craftingItems = []; - $this->toCraft = []; - if(isset($this->windows[$packet->windowid])){ - if(is_array($this->windows[$packet->windowid])){ - foreach($this->windows[$packet->windowid] as $ob){ - $pk = new TileEventPacket; - $pk->x = $ob->x; - $pk->y = $ob->y; - $pk->z = $ob->z; - $pk->case1 = 1; - $pk->case2 = 0; - $this->server->api->player->broadcastPacket($this->level->players, $pk); - } - }elseif($this->windows[$packet->windowid]->class === TILE_CHEST){ - $pk = new TileEventPacket; - $pk->x = $this->windows[$packet->windowid]->x; - $pk->y = $this->windows[$packet->windowid]->y; - $pk->z = $this->windows[$packet->windowid]->z; - $pk->case1 = 1; - $pk->case2 = 0; - $this->server->api->player->broadcastPacket($this->level->players, $pk); - } - } - unset($this->windows[$packet->windowid]); - - $pk = new ContainerClosePacket; - $pk->windowid = $packet->windowid; - $this->dataPacket($pk); - break; - case ProtocolInfo::CONTAINER_SET_SLOT_PACKET: - if($this->spawned === false or $this->blocked === true){ - break; - } - - if($this->lastCraft <= (microtime(true) - 1)){ - if(isset($this->toCraft[-1])){ - $this->toCraft = [-1 => $this->toCraft[-1]]; - }else{ - $this->toCraft = []; - } - $this->craftingItems = []; - } - - if($packet->windowid === 0){ - $craft = false; - $slot = $this->getSlot($packet->slot); - if($slot->count >= $packet->item->count and (($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()) or ($packet->item->getID() === AIR and $packet->item->count === 0)) and !isset($this->craftingItems[$packet->slot])){ //Crafting recipe - $use = BlockAPI::getItem($slot->getID(), $slot->getMetadata(), $slot->count - $packet->item->count); - $this->craftingItems[$packet->slot] = $use; - $craft = true; - }elseif($slot->count <= $packet->item->count and ($slot->getID() === AIR or ($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()))){ //Crafting final - $craftItem = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count - $slot->count); - if(count($this->toCraft) === 0){ - $this->toCraft[-1] = 0; - } - $this->toCraft[$packet->slot] = $craftItem; - $craft = true; - }elseif(((count($this->toCraft) === 1 and isset($this->toCraft[-1])) or count($this->toCraft) === 0) and $slot->count > 0 and $slot->getID() > AIR and ($slot->getID() !== $packet->item->getID() or $slot->getMetadata() !== $packet->item->getMetadata())){ //Crafting final - $craftItem = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count); - if(count($this->toCraft) === 0){ - $this->toCraft[-1] = 0; - } - $use = BlockAPI::getItem($slot->getID(), $slot->getMetadata(), $slot->count); - $this->craftingItems[$packet->slot] = $use; - $this->toCraft[$packet->slot] = $craftItem; - $craft = true; - } - - if($craft === true){ - $this->lastCraft = microtime(true); - } - - if($craft === true and count($this->craftingItems) > 0 and count($this->toCraft) > 0 and ($recipe = $this->craftItems($this->toCraft, $this->craftingItems, $this->toCraft[-1])) !== true){ - if($recipe === false){ - $this->sendInventory(); - $this->toCraft = []; - }else{ - $this->toCraft = [-1 => $this->toCraft[-1]]; - } - $this->craftingItems = []; - } - }else{ - $this->toCraft = []; - $this->craftingItems = []; - } - if(!isset($this->windows[$packet->windowid])){ - break; - } - - if(is_array($this->windows[$packet->windowid])){ - $tiles = $this->windows[$packet->windowid]; - if($packet->slot >= 0 and $packet->slot < CHEST_SLOTS){ - $tile = $tiles[0]; - $slotn = $packet->slot; - $offset = 0; - }elseif($packet->slot >= CHEST_SLOTS and $packet->slot <= (CHEST_SLOTS << 1)){ - $tile = $tiles[1]; - $slotn = $packet->slot - CHEST_SLOTS; - $offset = CHEST_SLOTS; - }else{ - break; - } - - $item = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count); - - $slot = $tile->getSlot($slotn); - if($this->server->api->dhandle("player.container.slot", [ - "tile" => $tile, - "slot" => $packet->slot, - "offset" => $offset, - "slotdata" => $slot, - "itemdata" => $item, - "player" => $this - ]) === false){ - $pk = new ContainerSetSlotPacket; - $pk->windowid = $packet->windowid; - $pk->slot = $packet->slot; - $pk->item = $slot; - $this->dataPacket($pk); - break; - } - if($item->getID() !== AIR and $slot->getID() == $item->getID()){ - if($slot->count < $item->count){ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count - $slot->count, false) === false){ - break; - } - }elseif($slot->count > $item->count){ - $this->addItem($item->getID(), $item->getMetadata(), $slot->count - $item->count, false); - } - }else{ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count, false) === false){ - break; - } - $this->addItem($slot->getID(), $slot->getMetadata(), $slot->count, false); - } - $tile->setSlot($slotn, $item, true, $offset); - }else{ - $tile = $this->windows[$packet->windowid]; - if(($tile->class !== TILE_CHEST and $tile->class !== TILE_FURNACE) or $packet->slot < 0 or ($tile->class === TILE_CHEST and $packet->slot >= CHEST_SLOTS) or ($tile->class === TILE_FURNACE and $packet->slot >= FURNACE_SLOTS)){ - break; - } - $item = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count); - - $slot = $tile->getSlot($packet->slot); - if($this->server->api->dhandle("player.container.slot", [ - "tile" => $tile, - "slot" => $packet->slot, - "slotdata" => $slot, - "itemdata" => $item, - "player" => $this, - ]) === false){ - $pk = new ContainerSetSlotPacket; - $pk->windowid = $packet->windowid; - $pk->slot = $packet->slot; - $pk->item = $slot; - $this->dataPacket($pk); - break; - } - - if($tile->class === TILE_FURNACE and $packet->slot == 2){ - switch($slot->getID()){ - case IRON_INGOT: - AchievementAPI::grantAchievement($this, "acquireIron"); - break; - } - } - - if($item->getID() !== AIR and $slot->getID() == $item->getID()){ - if($slot->count < $item->count){ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count - $slot->count, false) === false){ - break; - } - }elseif($slot->count > $item->count){ - $this->addItem($item->getID(), $item->getMetadata(), $slot->count - $item->count, false); - } - }else{ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count, false) === false){ - break; - } - $this->addItem($slot->getID(), $slot->getMetadata(), $slot->count, false); - } - - $tile->setSlot($packet->slot, $item); - } - break; - case ProtocolInfo::SEND_INVENTORY_PACKET: - if($this->spawned === false){ - break; - } - break; - case ProtocolInfo::ENTITY_DATA_PACKET: - if($this->spawned === false or $this->blocked === true){ - break; - } - $this->craftingItems = []; - $this->toCraft = []; - $t = $this->server->api->tile->get(new Position($packet->x, $packet->y, $packet->z, $this->level)); - if(($t instanceof Tile) and $t->class === TILE_SIGN){ - if($t->data["creator"] !== $this->username){ - $t->spawn($this); - }else{ - $nbt = new NBT(); - $nbt->load($packet->namedtag); - $d = array_shift($nbt->tree); - if($d["id"] !== TILE_SIGN){ - $t->spawn($this); - }else{ - $t->setText($d["Text1"], $d["Text2"], $d["Text3"], $d["Text4"]); - } - } - } - break; - case ProtocolInfo::PLAYER_INPUT_PACKET: - $this->isJumping = $packet->isJumping; - $this->isSneaking = $packet->isSneaking; - $this->entity->moveForward = $packet->moveForward; - $this->entity->moveStrafing = $packet->moveStrafe; - - if(strlen(bin2hex($packet->buffer)) === 24 && $this->entity->linkedEntity != 0){ - $this->entity->stopRiding(); - break; - } - if($this->entity->linkedEntity != 0){ //TODO better riding - $e = $this->entity->level->entityList[$this->entity->linkedEntity] ?? false; - if($e === false) { - ConsoleAPI::warn("Player is riding on entity that doesnt exist in world! ({$this->iusername}, {$this->entity->linkedEntity})"); - $this->entity->stopRiding(); - break; - } - /*$pk = new SetEntityMotionPacket; - $pk->eid = $e->eid; - $pk->speedX = ($this->entity->x - $e->x)*1.2; - $pk->speedY = ($this->entity->y - $e->y)*1.2; - $pk->speedZ = ($this->entity->z - $e->z)*1.2; - foreach($this->level->players as $p){ - if($p->entity->eid != $this->entity->eid){ - $p->dataPacket(clone $pk); - } - } - console("{$e} {$this->entity}"); - //$this->entity->linkedEntity->moveFlying($packet->moveStrafe, $packet->moveForward, 1);*/ - } - - break; - default: - console("[DEBUG] Unhandled 0x" . dechex($packet->pid()) . " data packet for " . $this->username . " (" . $this->clientID . "): " . print_r($packet, true), true, true, 2); - break; - } - } + } /** * Get an Item which is currently held by player