From 3d8bb7df05f580b59da550d4ab81533b8563409d Mon Sep 17 00:00:00 2001 From: Administrator Date: Thu, 13 Mar 2025 19:23:05 +0800 Subject: [PATCH 01/18] Support 0.7.6 --- README.md | 2 +- src/Player.php | 1100 ++++++++++++++++- src/network/protocol/ProtocolInfo.php | 140 +++ .../protocol/packet/AddPaintingPacket.php | 3 + .../packet/AdventureSettingsPacket.php | 5 + src/network/protocol/packet/AnimatePacket.php | 5 + src/network/protocol/packet/ChatPacket.php | 5 + .../protocol/packet/ChunkDataPacket.php | 3 + .../protocol/packet/ContainerClosePacket.php | 5 + .../protocol/packet/ContainerOpenPacket.php | 5 + .../packet/ContainerSetContentPacket.php | 5 + .../packet/ContainerSetDataPacket.php | 5 + .../packet/ContainerSetSlotPacket.php | 5 + .../protocol/packet/DropItemPacket.php | 5 + .../protocol/packet/EntityDataPacket.php | 5 + .../protocol/packet/EntityEventPacket.php | 3 + src/network/protocol/packet/ExplodePacket.php | 3 + .../protocol/packet/HurtArmorPacket.php | 3 + .../protocol/packet/InteractPacket.php | 3 + .../protocol/packet/LevelEventPacket.php | 3 + src/network/protocol/packet/LoginPacket.php | 1 + .../protocol/packet/MovePlayerPacket.php | 3 + .../protocol/packet/PlayerActionPacket.php | 3 + .../packet/PlayerArmorEquipmentPacket.php | 3 + .../protocol/packet/PlayerEquipmentPacket.php | 3 + .../protocol/packet/PlayerInputPacket.php | 13 +- .../protocol/packet/RemoveBlockPacket.php | 3 + .../protocol/packet/RequestChunkPacket.php | 3 + src/network/protocol/packet/RespawnPacket.php | 5 + .../protocol/packet/RotateHeadPacket.php | 3 + .../protocol/packet/SendInventoryPacket.php | 5 + .../protocol/packet/SetEntityDataPacket.php | 3 + .../protocol/packet/SetEntityLinkPacket.php | 3 + .../protocol/packet/SetEntityMotionPacket.php | 3 + .../protocol/packet/SetHealthPacket.php | 5 + .../packet/SetSpawnPositionPacket.php | 5 + .../protocol/packet/TileEventPacket.php | 3 + .../protocol/packet/UpdateBlockPacket.php | 3 + src/network/protocol/packet/UseItemPacket.php | 3 + src/network/raknet/RakNetDataPacket.php | 3 + 40 files changed, 1377 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index e58c7783..f2fb0184 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ NostalgiaCore is a fork of Legacy PocketMine-MP 1.3.12, which fixes bugs of Legacy PocketMine-MP, and adds new content -Supported MCPE versions: 0.8.0, 0.8.1 (maybe 0.8.2) +Supported MCPE versions: 0.7.4, 0.7.6, 0.8.0, 0.8.1 (maybe 0.8.2) (Special thx to GullCraft Network and DartMiner43) diff --git a/src/Player.php b/src/Player.php index e6ebdc6c..4b90b8a6 100755 --- a/src/Player.php +++ b/src/Player.php @@ -77,6 +77,8 @@ class Player{ public $blockUpdateQueue; public $blockUpdateQueueLength = 0; + + public $PROTOCOL = ProtocolInfo::CURRENT_PROTOCOL; /** * Sent by a client while it is linked to some entity. @@ -352,6 +354,7 @@ class Player{ return; } + $packet->PROTOCOL = $this->PROTOCOL; $packet->encode(); $len = strlen($packet->buffer) + 1; $MTU = $this->MTU - 24; @@ -1221,6 +1224,7 @@ class Player{ } $this->received[$p->messageIndex] = true; } + $p->PROTOCOL = $this->PROTOCOL; $p->decode(); $this->handleDataPacket($p); } @@ -1284,6 +1288,7 @@ class Player{ $packet->z = $z; $packet->block = $id; $packet->meta = $meta; + $packet->PROTOCOL = $this->PROTOCOL; $packet->encode(); $len = 1 + strlen($packet->buffer); @@ -1316,6 +1321,7 @@ class Player{ $motion->speedX = $e->speedX; $motion->speedY = $e->speedY; $motion->speedZ = $e->speedZ; + $motion->PROTOCOL = $this->PROTOCOL; $motion->encode(); $len += 1 + strlen($motion->buffer); ++$packets; @@ -1337,6 +1343,7 @@ class Player{ $move->yaw = $e->yaw; $move->pitch = $e->pitch; $move->bodyYaw = $e->headYaw; + $move->PROTOCOL = $this->PROTOCOL; $move->encode(); }else{ $move = new MoveEntityPacket_PosRot(); @@ -1346,6 +1353,7 @@ class Player{ $move->z = $e->z; $move->yaw = $e->yaw; $move->pitch = $e->pitch; + $move->PROTOCOL = $this->PROTOCOL; $move->encode(); } @@ -1356,6 +1364,7 @@ class Player{ $headyaw = new RotateHeadPacket(); $headyaw->eid = $e->eid; $headyaw->yaw = $e->headYaw; + $headyaw->PROTOCOL = $this->PROTOCOL; $headyaw->encode(); $len += strlen($headyaw->buffer) + 1; ++$packets; @@ -1481,6 +1490,7 @@ class Player{ return []; } + $packet->PROTOCOL = $this->PROTOCOL; $packet->encode(); $pk = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $pk->data[] = $packet; @@ -1512,7 +1522,1082 @@ class Player{ return; } - + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + switch($packet->pid()){ + case 0x01: + break; + case ProtocolInfo12::PONG_PACKET: + break; + case ProtocolInfo12::PING_PACKET: + $pk = new PongPacket; + $pk->ptime = $packet->time; + $pk->time = abs(microtime(true) * 1000); + $this->directDataPacket($pk); + break; + case ProtocolInfo12::DISCONNECT_PACKET: + $this->close("client disconnect"); + break; + case ProtocolInfo12::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 ProtocolInfo12::CLIENT_HANDSHAKE_PACKET: + if($this->loggedIn === true){ + break; + } + break; + case ProtocolInfo12::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(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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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->sendInventorySlot($packet->slot); + $this->sendInventory(); + } + if($this->entity->inAction === true){ + $this->entity->inAction = false; + $this->entity->inActionCounter = 0; + $this->entity->updateMetadata(); + } + break; + case ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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 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){ + $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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::SET_HEALTH_PACKET: //Not used + break; + case ProtocolInfo12::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 ProtocolInfo12::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; + } + } + + $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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::SEND_INVENTORY_PACKET: + if($this->spawned === false){ + break; + } + break; + case ProtocolInfo12::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 ProtocolInfo12::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; + } + } switch($packet->pid()){ case 0x01: @@ -1550,11 +2635,12 @@ class Player{ $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)){ $this->close("server is full!", false); return; } - if($packet->protocol1 !== ProtocolInfo::CURRENT_PROTOCOL){ + if($packet->protocol1 < ProtocolInfo9::CURRENT_PROTOCOL_9 || $packet->protocol1 > ProtocolInfo::CURRENT_PROTOCOL){ if($packet->protocol1 < ProtocolInfo::CURRENT_PROTOCOL){ $pk = new LoginStatusPacket; $pk->status = 1; @@ -2284,12 +3370,12 @@ class Player{ $this->lastCraft = microtime(true); break; }else{ - inv_desync_on_drop: - ConsoleAPI::debug("Inventory desync on drop({$this->iusername})"); - $sendOnDrop = true; - } + inv_desync_on_drop: + ConsoleAPI::debug("Inventory desync on drop({$this->iusername})"); + $sendOnDrop = true; + } } - + $this->craftingItems = []; $this->toCraft = []; $data["eid"] = $packet->eid; diff --git a/src/network/protocol/ProtocolInfo.php b/src/network/protocol/ProtocolInfo.php index fc6c6b8f..d7fd1e4b 100644 --- a/src/network/protocol/ProtocolInfo.php +++ b/src/network/protocol/ProtocolInfo.php @@ -78,6 +78,146 @@ abstract class ProtocolInfo{ * 0x17 * 0x14 */ +abstract class ProtocolInfo12{ + + const CURRENT_PROTOCOL_12 = 12; + + const PING_PACKET = 0x00; + + const PONG_PACKET = 0x03; + + const CLIENT_CONNECT_PACKET = 0x09; + const SERVER_HANDSHAKE_PACKET = 0x10; + + const CLIENT_HANDSHAKE_PACKET = 0x13; + //const SERVER_FULL_PACKET = 0x14; + const DISCONNECT_PACKET = 0x15; + const LOGIN_PACKET = 0x82; + const LOGIN_STATUS_PACKET = 0x83; + const READY_PACKET = 0x84; + const MESSAGE_PACKET = 0x85; + const SET_TIME_PACKET = 0x86; + const START_GAME_PACKET = 0x87; + const ADD_MOB_PACKET = 0x88; + const ADD_PLAYER_PACKET = 0x89; + const REMOVE_PLAYER_PACKET = 0x8a; + + const ADD_ENTITY_PACKET = 0x8c; + const REMOVE_ENTITY_PACKET = 0x8d; + const ADD_ITEM_ENTITY_PACKET = 0x8e; + const TAKE_ITEM_ENTITY_PACKET = 0x8f; + const MOVE_ENTITY_PACKET = 0x90; + + const MOVE_ENTITY_PACKET_POSROT = 0x93; + const ROTATE_HEAD_PACKET = 0xff; + const MOVE_PLAYER_PACKET = 0x94; + //const PLACE_BLOCK_PACKET = 0x96; + const REMOVE_BLOCK_PACKET = 0x96; + const UPDATE_BLOCK_PACKET = 0x97; + const ADD_PAINTING_PACKET = 0x98; + const EXPLODE_PACKET = 0x99; + const LEVEL_EVENT_PACKET = 0x9a; + const TILE_EVENT_PACKET = 0x9b; + const ENTITY_EVENT_PACKET = 0x9c; + const REQUEST_CHUNK_PACKET = 0x9d; + const CHUNK_DATA_PACKET = 0x9e; + const PLAYER_EQUIPMENT_PACKET = 0x9f; + const PLAYER_ARMOR_EQUIPMENT_PACKET = 0xa0; + const INTERACT_PACKET = 0xa1; + const USE_ITEM_PACKET = 0xa2; + const PLAYER_ACTION_PACKET = 0xa3; + + const HURT_ARMOR_PACKET = 0xa5; + const SET_ENTITY_DATA_PACKET = 0xa6; + const SET_ENTITY_MOTION_PACKET = 0xa7; + const SET_ENTITY_LINK_PACKET = 0xa8; + const SET_HEALTH_PACKET = 0xa9; + const SET_SPAWN_POSITION_PACKET = 0xaa; + const ANIMATE_PACKET = 0xab; + const RESPAWN_PACKET = 0xac; + const SEND_INVENTORY_PACKET = 0xad; + const DROP_ITEM_PACKET = 0xae; + const CONTAINER_OPEN_PACKET = 0xaf; + const CONTAINER_CLOSE_PACKET = 0xb0; + const CONTAINER_SET_SLOT_PACKET = 0xb1; + const CONTAINER_SET_DATA_PACKET = 0xb2; + const CONTAINER_SET_CONTENT_PACKET = 0xb3; + //const CONTAINER_ACK_PACKET = 0xb5; //Bruh + const CHAT_PACKET = 0xb5; + const ADVENTURE_SETTINGS_PACKET = 0xb6; + const ENTITY_DATA_PACKET = 0xb7; + const PLAYER_INPUT_PACKET = 0xb9; +} +abstract class ProtocolInfo9{ + + const CURRENT_PROTOCOL_9 = 9; + + const PING_PACKET = 0x00; + + const PONG_PACKET = 0x03; + + const CLIENT_CONNECT_PACKET = 0x09; + const SERVER_HANDSHAKE_PACKET = 0x10; + + const CLIENT_HANDSHAKE_PACKET = 0x13; + //const SERVER_FULL_PACKET = 0x14; + const DISCONNECT_PACKET = 0x15; + const LOGIN_PACKET = 0x82; + const LOGIN_STATUS_PACKET = 0x83; + const READY_PACKET = 0x84; + const MESSAGE_PACKET = 0x85; + const SET_TIME_PACKET = 0x86; + const START_GAME_PACKET = 0x87; + const ADD_MOB_PACKET = 0x88; + const ADD_PLAYER_PACKET = 0x89; + const REMOVE_PLAYER_PACKET = 0x8a; + + const ADD_ENTITY_PACKET = 0x8c; + const REMOVE_ENTITY_PACKET = 0x8d; + const ADD_ITEM_ENTITY_PACKET = 0x8e; + const TAKE_ITEM_ENTITY_PACKET = 0x8f; + const MOVE_ENTITY_PACKET = 0x90; + + const MOVE_ENTITY_PACKET_POSROT = 0x93; + const MOVE_PLAYER_PACKET = 0x94; + //const PLACE_BLOCK_PACKET = 0x95; + const REMOVE_BLOCK_PACKET = 0x96; + const UPDATE_BLOCK_PACKET = 0x97; + const ADD_PAINTING_PACKET = 0x98; + const EXPLODE_PACKET = 0x99; + const LEVEL_EVENT_PACKET = 0x9a; + const TILE_EVENT_PACKET = 0x9b; + const ENTITY_EVENT_PACKET = 0x9c; + const REQUEST_CHUNK_PACKET = 0x9d; + const CHUNK_DATA_PACKET = 0x9e; + const PLAYER_EQUIPMENT_PACKET = 0x9f; + const PLAYER_ARMOR_EQUIPMENT_PACKET = 0xa0; + const INTERACT_PACKET = 0xa1; + const USE_ITEM_PACKET = 0xa2; + const PLAYER_ACTION_PACKET = 0xa3; + + const HURT_ARMOR_PACKET = 0xa5; + const SET_ENTITY_DATA_PACKET = 0xa6; + const SET_ENTITY_MOTION_PACKET = 0xa7; + //const SET_ENTITY_LINK_PACKET = 0xa?;// Change + const SET_HEALTH_PACKET = 0xa8;// Change + const SET_SPAWN_POSITION_PACKET = 0xa9; + const ANIMATE_PACKET = 0xaa; + const RESPAWN_PACKET = 0xab; + const SEND_INVENTORY_PACKET = 0xac; + const DROP_ITEM_PACKET = 0xad; + const CONTAINER_OPEN_PACKET = 0xae; + const CONTAINER_CLOSE_PACKET = 0xaf; + const CONTAINER_SET_SLOT_PACKET = 0xb0; + const CONTAINER_SET_DATA_PACKET = 0xb1; + const CONTAINER_SET_CONTENT_PACKET = 0xb2; + //const CONTAINER_ACK_PACKET = 0xb3; + const CHAT_PACKET = 0xb4;//12 change + const ADVENTURE_SETTINGS_PACKET = 0xb6; + const ENTITY_DATA_PACKET = 0xb7; + const PLAYER_INPUT_PACKET = 0xb9; + +} /***REM_START***/ require_once(FILE_PATH . "src/network/raknet/RakNetDataPacket.php"); /***REM_END***/ \ No newline at end of file diff --git a/src/network/protocol/packet/AddPaintingPacket.php b/src/network/protocol/packet/AddPaintingPacket.php index bd50ab0a..110bddcf 100644 --- a/src/network/protocol/packet/AddPaintingPacket.php +++ b/src/network/protocol/packet/AddPaintingPacket.php @@ -9,6 +9,9 @@ class AddPaintingPacket extends RakNetDataPacket{ public $title; public function pid(){ + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::ADD_PAINTING_PACKET; + } return ProtocolInfo::ADD_PAINTING_PACKET; } diff --git a/src/network/protocol/packet/AdventureSettingsPacket.php b/src/network/protocol/packet/AdventureSettingsPacket.php index b395975c..e92ef1fc 100644 --- a/src/network/protocol/packet/AdventureSettingsPacket.php +++ b/src/network/protocol/packet/AdventureSettingsPacket.php @@ -4,6 +4,11 @@ class AdventureSettingsPacket extends RakNetDataPacket{ public $flags; public function pid(){ + if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + return ProtocolInfo9::ADVENTURE_SETTINGS_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::ADVENTURE_SETTINGS_PACKET; + } return ProtocolInfo::ADVENTURE_SETTINGS_PACKET; } diff --git a/src/network/protocol/packet/AnimatePacket.php b/src/network/protocol/packet/AnimatePacket.php index a8ea6820..34448421 100644 --- a/src/network/protocol/packet/AnimatePacket.php +++ b/src/network/protocol/packet/AnimatePacket.php @@ -8,6 +8,11 @@ class AnimatePacket extends RakNetDataPacket{ public $eid; public function pid(){ + if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + return ProtocolInfo9::ANIMATE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::ANIMATE_PACKET; + } return ProtocolInfo::ANIMATE_PACKET; } diff --git a/src/network/protocol/packet/ChatPacket.php b/src/network/protocol/packet/ChatPacket.php index cbe5998a..8d7df14f 100644 --- a/src/network/protocol/packet/ChatPacket.php +++ b/src/network/protocol/packet/ChatPacket.php @@ -4,6 +4,11 @@ class ChatPacket extends RakNetDataPacket{ public $message; public function pid(){ + if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + return ProtocolInfo9::CHAT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::CHAT_PACKET; + } return ProtocolInfo::CHAT_PACKET; } diff --git a/src/network/protocol/packet/ChunkDataPacket.php b/src/network/protocol/packet/ChunkDataPacket.php index 024bebbb..b8b70523 100644 --- a/src/network/protocol/packet/ChunkDataPacket.php +++ b/src/network/protocol/packet/ChunkDataPacket.php @@ -6,6 +6,9 @@ class ChunkDataPacket extends RakNetDataPacket{ public $data; public function pid(){ + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::CHUNK_DATA_PACKET; + } return ProtocolInfo::CHUNK_DATA_PACKET; } diff --git a/src/network/protocol/packet/ContainerClosePacket.php b/src/network/protocol/packet/ContainerClosePacket.php index 008d8097..e7f4f564 100644 --- a/src/network/protocol/packet/ContainerClosePacket.php +++ b/src/network/protocol/packet/ContainerClosePacket.php @@ -4,6 +4,11 @@ class ContainerClosePacket extends RakNetDataPacket{ public $windowid; public function pid(){ + if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + return ProtocolInfo9::CONTAINER_CLOSE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::CONTAINER_CLOSE_PACKET; + } return ProtocolInfo::CONTAINER_CLOSE_PACKET; } diff --git a/src/network/protocol/packet/ContainerOpenPacket.php b/src/network/protocol/packet/ContainerOpenPacket.php index 92eecd78..23ad3739 100644 --- a/src/network/protocol/packet/ContainerOpenPacket.php +++ b/src/network/protocol/packet/ContainerOpenPacket.php @@ -9,6 +9,11 @@ class ContainerOpenPacket extends RakNetDataPacket{ public $z; public function pid(){ + if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + return ProtocolInfo9::CONTAINER_OPEN_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::CONTAINER_OPEN_PACKET; + } return ProtocolInfo::CONTAINER_OPEN_PACKET; } diff --git a/src/network/protocol/packet/ContainerSetContentPacket.php b/src/network/protocol/packet/ContainerSetContentPacket.php index a4afe1db..96a53428 100644 --- a/src/network/protocol/packet/ContainerSetContentPacket.php +++ b/src/network/protocol/packet/ContainerSetContentPacket.php @@ -6,6 +6,11 @@ class ContainerSetContentPacket extends RakNetDataPacket{ public $hotbar = array(); public function pid(){ + if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + return ProtocolInfo9::CONTAINER_SET_CONTENT_PACKET; + }else if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::CONTAINER_SET_CONTENT_PACKET; + } return ProtocolInfo::CONTAINER_SET_CONTENT_PACKET; } diff --git a/src/network/protocol/packet/ContainerSetDataPacket.php b/src/network/protocol/packet/ContainerSetDataPacket.php index 0d93c4e1..7e9c7059 100644 --- a/src/network/protocol/packet/ContainerSetDataPacket.php +++ b/src/network/protocol/packet/ContainerSetDataPacket.php @@ -6,6 +6,11 @@ class ContainerSetDataPacket extends RakNetDataPacket{ public $value; public function pid(){ + if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + return ProtocolInfo9::CONTAINER_SET_DATA_PACKET; + }else if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::CONTAINER_SET_DATA_PACKET; + } return ProtocolInfo::CONTAINER_SET_DATA_PACKET; } diff --git a/src/network/protocol/packet/ContainerSetSlotPacket.php b/src/network/protocol/packet/ContainerSetSlotPacket.php index fab25153..0c6bbe41 100644 --- a/src/network/protocol/packet/ContainerSetSlotPacket.php +++ b/src/network/protocol/packet/ContainerSetSlotPacket.php @@ -6,6 +6,11 @@ class ContainerSetSlotPacket extends RakNetDataPacket{ public $item; public function pid(){ + if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + return ProtocolInfo9::CONTAINER_SET_SLOT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::CONTAINER_SET_SLOT_PACKET; + } return ProtocolInfo::CONTAINER_SET_SLOT_PACKET; } diff --git a/src/network/protocol/packet/DropItemPacket.php b/src/network/protocol/packet/DropItemPacket.php index 41531160..3fe52db3 100644 --- a/src/network/protocol/packet/DropItemPacket.php +++ b/src/network/protocol/packet/DropItemPacket.php @@ -6,6 +6,11 @@ class DropItemPacket extends RakNetDataPacket{ public $item; public function pid(){ + if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + return ProtocolInfo9::DROP_ITEM_PACKET; + }else if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::DROP_ITEM_PACKET; + } return ProtocolInfo::DROP_ITEM_PACKET; } diff --git a/src/network/protocol/packet/EntityDataPacket.php b/src/network/protocol/packet/EntityDataPacket.php index 01b2cafb..d7c6f8c1 100644 --- a/src/network/protocol/packet/EntityDataPacket.php +++ b/src/network/protocol/packet/EntityDataPacket.php @@ -7,6 +7,11 @@ class EntityDataPacket extends RakNetDataPacket{ public $namedtag; public function pid(){ + if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + return ProtocolInfo9::ENTITY_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::ENTITY_DATA_PACKET; + } return ProtocolInfo::ENTITY_DATA_PACKET; } diff --git a/src/network/protocol/packet/EntityEventPacket.php b/src/network/protocol/packet/EntityEventPacket.php index 05cf7770..2699ea32 100644 --- a/src/network/protocol/packet/EntityEventPacket.php +++ b/src/network/protocol/packet/EntityEventPacket.php @@ -14,6 +14,9 @@ class EntityEventPacket extends RakNetDataPacket{ } public function pid(){ + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::ENTITY_EVENT_PACKET; + } return ProtocolInfo::ENTITY_EVENT_PACKET; } diff --git a/src/network/protocol/packet/ExplodePacket.php b/src/network/protocol/packet/ExplodePacket.php index 1d60b72a..24765f63 100644 --- a/src/network/protocol/packet/ExplodePacket.php +++ b/src/network/protocol/packet/ExplodePacket.php @@ -8,6 +8,9 @@ class ExplodePacket extends RakNetDataPacket{ public $records; public function pid(){ + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::EXPLODE_PACKET; + } return ProtocolInfo::EXPLODE_PACKET; } diff --git a/src/network/protocol/packet/HurtArmorPacket.php b/src/network/protocol/packet/HurtArmorPacket.php index e23aac3e..ce40d314 100644 --- a/src/network/protocol/packet/HurtArmorPacket.php +++ b/src/network/protocol/packet/HurtArmorPacket.php @@ -4,6 +4,9 @@ class HurtArmorPacket extends RakNetDataPacket{ public $health; public function pid(){ + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::HURT_ARMOR_PACKET; + } return ProtocolInfo::HURT_ARMOR_PACKET; } diff --git a/src/network/protocol/packet/InteractPacket.php b/src/network/protocol/packet/InteractPacket.php index 2d33773d..760650ee 100644 --- a/src/network/protocol/packet/InteractPacket.php +++ b/src/network/protocol/packet/InteractPacket.php @@ -9,6 +9,9 @@ class InteractPacket extends RakNetDataPacket{ public $target; public function pid(){ + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::INTERACT_PACKET; + } return ProtocolInfo::INTERACT_PACKET; } diff --git a/src/network/protocol/packet/LevelEventPacket.php b/src/network/protocol/packet/LevelEventPacket.php index 50727e45..86db88be 100644 --- a/src/network/protocol/packet/LevelEventPacket.php +++ b/src/network/protocol/packet/LevelEventPacket.php @@ -12,6 +12,9 @@ class LevelEventPacket extends RakNetDataPacket{ public $data; public function pid(){ + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::LEVEL_EVENT_PACKET; + } return ProtocolInfo::LEVEL_EVENT_PACKET; } diff --git a/src/network/protocol/packet/LoginPacket.php b/src/network/protocol/packet/LoginPacket.php index 34f04955..e87ea91d 100644 --- a/src/network/protocol/packet/LoginPacket.php +++ b/src/network/protocol/packet/LoginPacket.php @@ -17,6 +17,7 @@ class LoginPacket extends RakNetDataPacket{ $this->protocol2 = $this->getInt(); $this->clientId = $this->getInt(); $this->loginData = $this->getString(); + $this->PROTOCOL = $this->protocol1; } public function encode(){ diff --git a/src/network/protocol/packet/MovePlayerPacket.php b/src/network/protocol/packet/MovePlayerPacket.php index 5325cf76..5d806a15 100644 --- a/src/network/protocol/packet/MovePlayerPacket.php +++ b/src/network/protocol/packet/MovePlayerPacket.php @@ -10,6 +10,9 @@ class MovePlayerPacket extends RakNetDataPacket{ public $bodyYaw; public function pid(){ + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::MOVE_PLAYER_PACKET; + } return ProtocolInfo::MOVE_PLAYER_PACKET; } diff --git a/src/network/protocol/packet/PlayerActionPacket.php b/src/network/protocol/packet/PlayerActionPacket.php index cdcc64ad..e47581e3 100644 --- a/src/network/protocol/packet/PlayerActionPacket.php +++ b/src/network/protocol/packet/PlayerActionPacket.php @@ -9,6 +9,9 @@ class PlayerActionPacket extends RakNetDataPacket{ public $eid; public function pid(){ + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::PLAYER_ACTION_PACKET; + } return ProtocolInfo::PLAYER_ACTION_PACKET; } diff --git a/src/network/protocol/packet/PlayerArmorEquipmentPacket.php b/src/network/protocol/packet/PlayerArmorEquipmentPacket.php index d32be316..75a93a40 100644 --- a/src/network/protocol/packet/PlayerArmorEquipmentPacket.php +++ b/src/network/protocol/packet/PlayerArmorEquipmentPacket.php @@ -5,6 +5,9 @@ class PlayerArmorEquipmentPacket extends RakNetDataPacket{ public $slots = array(); public function pid(){ + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::PLAYER_ARMOR_EQUIPMENT_PACKET; + } return ProtocolInfo::PLAYER_ARMOR_EQUIPMENT_PACKET; } diff --git a/src/network/protocol/packet/PlayerEquipmentPacket.php b/src/network/protocol/packet/PlayerEquipmentPacket.php index 06e65fee..f6f4eecf 100644 --- a/src/network/protocol/packet/PlayerEquipmentPacket.php +++ b/src/network/protocol/packet/PlayerEquipmentPacket.php @@ -7,6 +7,9 @@ class PlayerEquipmentPacket extends RakNetDataPacket{ public $slot; public function pid(){ + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::PLAYER_EQUIPMENT_PACKET; + } return ProtocolInfo::PLAYER_EQUIPMENT_PACKET; } diff --git a/src/network/protocol/packet/PlayerInputPacket.php b/src/network/protocol/packet/PlayerInputPacket.php index f80f18bc..251c17f6 100644 --- a/src/network/protocol/packet/PlayerInputPacket.php +++ b/src/network/protocol/packet/PlayerInputPacket.php @@ -1,13 +1,18 @@ PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + return ProtocolInfo9::PLAYER_INPUT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::PLAYER_INPUT_PACKET; + } return ProtocolInfo::PLAYER_INPUT_PACKET; } diff --git a/src/network/protocol/packet/RemoveBlockPacket.php b/src/network/protocol/packet/RemoveBlockPacket.php index d3e3a969..b528a236 100644 --- a/src/network/protocol/packet/RemoveBlockPacket.php +++ b/src/network/protocol/packet/RemoveBlockPacket.php @@ -7,6 +7,9 @@ class RemoveBlockPacket extends RakNetDataPacket{ public $z; public function pid(){ + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::REMOVE_BLOCK_PACKET; + } return ProtocolInfo::REMOVE_BLOCK_PACKET; } diff --git a/src/network/protocol/packet/RequestChunkPacket.php b/src/network/protocol/packet/RequestChunkPacket.php index d0650748..9ba9b543 100644 --- a/src/network/protocol/packet/RequestChunkPacket.php +++ b/src/network/protocol/packet/RequestChunkPacket.php @@ -5,6 +5,9 @@ class RequestChunkPacket extends RakNetDataPacket{ public $chunkZ; public function pid(){ + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::REQUEST_CHUNK_PACKET; + } return ProtocolInfo::REQUEST_CHUNK_PACKET; } diff --git a/src/network/protocol/packet/RespawnPacket.php b/src/network/protocol/packet/RespawnPacket.php index 1d3c208e..35adcf21 100644 --- a/src/network/protocol/packet/RespawnPacket.php +++ b/src/network/protocol/packet/RespawnPacket.php @@ -7,6 +7,11 @@ class RespawnPacket extends RakNetDataPacket{ public $z; public function pid(){ + if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + return ProtocolInfo9::RESPAWN_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::RESPAWN_PACKET; + } return ProtocolInfo::RESPAWN_PACKET; } diff --git a/src/network/protocol/packet/RotateHeadPacket.php b/src/network/protocol/packet/RotateHeadPacket.php index 21cc7cf0..ca099c85 100644 --- a/src/network/protocol/packet/RotateHeadPacket.php +++ b/src/network/protocol/packet/RotateHeadPacket.php @@ -11,6 +11,9 @@ class RotateHeadPacket extends RakNetDataPacket{ */ public $rawYaw = false; public function pid(){ + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::ROTATE_HEAD_PACKET; + } return ProtocolInfo::ROTATE_HEAD_PACKET; } diff --git a/src/network/protocol/packet/SendInventoryPacket.php b/src/network/protocol/packet/SendInventoryPacket.php index 1bddca97..2d82e0b1 100644 --- a/src/network/protocol/packet/SendInventoryPacket.php +++ b/src/network/protocol/packet/SendInventoryPacket.php @@ -7,6 +7,11 @@ class SendInventoryPacket extends RakNetDataPacket{ public $armor = array(); public function pid(){ + if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + return ProtocolInfo9::SEND_INVENTORY_PACKET; + }else if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::SEND_INVENTORY_PACKET; + } return ProtocolInfo::SEND_INVENTORY_PACKET; } diff --git a/src/network/protocol/packet/SetEntityDataPacket.php b/src/network/protocol/packet/SetEntityDataPacket.php index 8626885d..9e9e4542 100644 --- a/src/network/protocol/packet/SetEntityDataPacket.php +++ b/src/network/protocol/packet/SetEntityDataPacket.php @@ -5,6 +5,9 @@ class SetEntityDataPacket extends RakNetDataPacket{ public $metadata; public function pid(){ + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::SET_ENTITY_DATA_PACKET; + } return ProtocolInfo::SET_ENTITY_DATA_PACKET; } diff --git a/src/network/protocol/packet/SetEntityLinkPacket.php b/src/network/protocol/packet/SetEntityLinkPacket.php index 34de37c1..202c7746 100644 --- a/src/network/protocol/packet/SetEntityLinkPacket.php +++ b/src/network/protocol/packet/SetEntityLinkPacket.php @@ -23,6 +23,9 @@ class SetEntityLinkPacket extends RakNetDataPacket{ public function pid(){ + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::SET_ENTITY_LINK_PACKET; + } return ProtocolInfo::SET_ENTITY_LINK_PACKET; } diff --git a/src/network/protocol/packet/SetEntityMotionPacket.php b/src/network/protocol/packet/SetEntityMotionPacket.php index e4cf70b9..b9ec84bd 100644 --- a/src/network/protocol/packet/SetEntityMotionPacket.php +++ b/src/network/protocol/packet/SetEntityMotionPacket.php @@ -7,6 +7,9 @@ class SetEntityMotionPacket extends RakNetDataPacket{ public $speedZ; public function pid(){ + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::SET_ENTITY_MOTION_PACKET; + } return ProtocolInfo::SET_ENTITY_MOTION_PACKET; } diff --git a/src/network/protocol/packet/SetHealthPacket.php b/src/network/protocol/packet/SetHealthPacket.php index 42e52f81..427f08d8 100644 --- a/src/network/protocol/packet/SetHealthPacket.php +++ b/src/network/protocol/packet/SetHealthPacket.php @@ -4,6 +4,11 @@ class SetHealthPacket extends RakNetDataPacket{ public $health; public function pid(){ + if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + return ProtocolInfo9::SET_HEALTH_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::SET_HEALTH_PACKET; + } return ProtocolInfo::SET_HEALTH_PACKET; } diff --git a/src/network/protocol/packet/SetSpawnPositionPacket.php b/src/network/protocol/packet/SetSpawnPositionPacket.php index 3da187ea..dc8d757e 100644 --- a/src/network/protocol/packet/SetSpawnPositionPacket.php +++ b/src/network/protocol/packet/SetSpawnPositionPacket.php @@ -6,6 +6,11 @@ class SetSpawnPositionPacket extends RakNetDataPacket{ public $y; public function pid(){ + if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + return ProtocolInfo9::SET_SPAWN_POSITION_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::SET_SPAWN_POSITION_PACKET; + } return ProtocolInfo::SET_SPAWN_POSITION_PACKET; } diff --git a/src/network/protocol/packet/TileEventPacket.php b/src/network/protocol/packet/TileEventPacket.php index e2266896..1bb97bdf 100644 --- a/src/network/protocol/packet/TileEventPacket.php +++ b/src/network/protocol/packet/TileEventPacket.php @@ -8,6 +8,9 @@ class TileEventPacket extends RakNetDataPacket{ public $case2; public function pid(){ + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::TILE_EVENT_PACKET; + } return ProtocolInfo::TILE_EVENT_PACKET; } diff --git a/src/network/protocol/packet/UpdateBlockPacket.php b/src/network/protocol/packet/UpdateBlockPacket.php index 020fc63b..d75eb806 100644 --- a/src/network/protocol/packet/UpdateBlockPacket.php +++ b/src/network/protocol/packet/UpdateBlockPacket.php @@ -8,6 +8,9 @@ class UpdateBlockPacket extends RakNetDataPacket{ public $meta; public function pid(){ + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::UPDATE_BLOCK_PACKET; + } return ProtocolInfo::UPDATE_BLOCK_PACKET; } diff --git a/src/network/protocol/packet/UseItemPacket.php b/src/network/protocol/packet/UseItemPacket.php index 0136ed5b..57169c38 100644 --- a/src/network/protocol/packet/UseItemPacket.php +++ b/src/network/protocol/packet/UseItemPacket.php @@ -16,6 +16,9 @@ class UseItemPacket extends RakNetDataPacket{ public $posZ; public function pid(){ + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::USE_ITEM_PACKET; + } return ProtocolInfo::USE_ITEM_PACKET; } diff --git a/src/network/raknet/RakNetDataPacket.php b/src/network/raknet/RakNetDataPacket.php index 5f64ac47..bfcf982b 100755 --- a/src/network/raknet/RakNetDataPacket.php +++ b/src/network/raknet/RakNetDataPacket.php @@ -11,7 +11,10 @@ abstract class RakNetDataPacket extends stdClass{ public $splitCount; public $splitID; public $splitIndex; + private $offset = 0; + + public $PROTOCOL = 14; abstract public function encode(); From 08d04e403d65894450463123104fbf2fd4a2f00b Mon Sep 17 00:00:00 2001 From: Administrator Date: Sat, 15 Mar 2025 14:57:24 +0800 Subject: [PATCH 02/18] Support MCPE && MCPI 0.6.1 test && Fix MessagePacket in MCPE 0.7.4 Todo : Fix RemoveBlockPacket & EntityMovePacket(Maybe) in the next step --- src/Player.php | 3364 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 2301 insertions(+), 1063 deletions(-) diff --git a/src/Player.php b/src/Player.php index 4b90b8a6..c4b79b39 100755 --- a/src/Player.php +++ b/src/Player.php @@ -354,7 +354,6 @@ class Player{ return; } - $packet->PROTOCOL = $this->PROTOCOL; $packet->encode(); $len = strlen($packet->buffer) + 1; $MTU = $this->MTU - 24; @@ -1224,7 +1223,6 @@ class Player{ } $this->received[$p->messageIndex] = true; } - $p->PROTOCOL = $this->PROTOCOL; $p->decode(); $this->handleDataPacket($p); } @@ -1288,7 +1286,6 @@ class Player{ $packet->z = $z; $packet->block = $id; $packet->meta = $meta; - $packet->PROTOCOL = $this->PROTOCOL; $packet->encode(); $len = 1 + strlen($packet->buffer); @@ -1321,7 +1318,6 @@ class Player{ $motion->speedX = $e->speedX; $motion->speedY = $e->speedY; $motion->speedZ = $e->speedZ; - $motion->PROTOCOL = $this->PROTOCOL; $motion->encode(); $len += 1 + strlen($motion->buffer); ++$packets; @@ -1343,7 +1339,6 @@ class Player{ $move->yaw = $e->yaw; $move->pitch = $e->pitch; $move->bodyYaw = $e->headYaw; - $move->PROTOCOL = $this->PROTOCOL; $move->encode(); }else{ $move = new MoveEntityPacket_PosRot(); @@ -1353,7 +1348,6 @@ class Player{ $move->z = $e->z; $move->yaw = $e->yaw; $move->pitch = $e->pitch; - $move->PROTOCOL = $this->PROTOCOL; $move->encode(); } @@ -1364,7 +1358,6 @@ class Player{ $headyaw = new RotateHeadPacket(); $headyaw->eid = $e->eid; $headyaw->yaw = $e->headYaw; - $headyaw->PROTOCOL = $this->PROTOCOL; $headyaw->encode(); $len += strlen($headyaw->buffer) + 1; ++$packets; @@ -1490,7 +1483,6 @@ class Player{ return []; } - $packet->PROTOCOL = $this->PROTOCOL; $packet->encode(); $pk = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $pk->data[] = $packet; @@ -1513,16 +1505,1258 @@ class Player{ } } - public function handleDataPacket(RakNetDataPacket $packet){ - if($this->connected === false){ - return; - } + public function handleDataPacket(RakNetDataPacket $packet) + { + if ($this->connected === false) { + return; + } - if(EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY){ - return; - } + if (EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY) { + return; + } - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + if ($packet->pid == ProtocolInfo::LOGIN_PACKET) { + if ($this->loggedIn === true) { + return; + } + $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)) { + $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) . ")"); + return; + } + if ($this->PROTOCOL >= ProtocolInfo9::CURRENT_PROTOCOL_9 && $this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + switch($packet->pid()){ + case 0x01: + break; + case ProtocolInfo9::PONG_PACKET: + break; + case ProtocolInfo9::PING_PACKET: + $pk = new PongPacket; + $pk->ptime = $packet->time; + $pk->time = abs(microtime(true) * 1000); + $this->directDataPacket($pk); + break; + case ProtocolInfo9::DISCONNECT_PACKET: + $this->close("client disconnect"); + break; + case ProtocolInfo9::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 ProtocolInfo9::CLIENT_HANDSHAKE_PACKET: + if($this->loggedIn === true){ + break; + } + break; + case ProtocolInfo9::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(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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::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->sendInventorySlot($packet->slot); + $this->sendInventory(); + } + if($this->entity->inAction === true){ + $this->entity->inAction = false; + $this->entity->inActionCounter = 0; + $this->entity->updateMetadata(); + } + break; + case ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::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 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){ + $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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::SET_HEALTH_PACKET: //Not used + break; + case ProtocolInfo9::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 ProtocolInfo9::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_drop9; + } + + $this->toCraft[] = $prevItem; //vanilla drops only result? + $this->lastCraft = microtime(true); + break; + } + }else { + inv_desync_on_drop9: + 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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::SEND_INVENTORY_PACKET: + if($this->spawned === false){ + break; + } + break; + case ProtocolInfo9::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 ProtocolInfo9::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; + } + }elseif($this->PROTOCOL >= ProtocolInfo12::CURRENT_PROTOCOL_12 && $this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ switch($packet->pid()){ case 0x01: break; @@ -2271,13 +3505,17 @@ class Player{ 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($item->getID() == 0) goto inv_desync_on_drop12; } $this->toCraft[] = $prevItem; //vanilla drops only result? $this->lastCraft = microtime(true); break; } + }else { + inv_desync_on_drop12: + ConsoleAPI::debug("Inventory desync on drop({$this->iusername})"); + $sendOnDrop = true; } $this->craftingItems = []; @@ -2597,1102 +3835,1102 @@ class Player{ console("[DEBUG] Unhandled 0x" . dechex($packet->pid()) . " data packet for " . $this->username . " (" . $this->clientID . "): " . print_r($packet, true), true, true, 2); break; } - } - - switch($packet->pid()){ - case 0x01: - break; - case ProtocolInfo::PONG_PACKET: - break; - case ProtocolInfo::PING_PACKET: - $pk = new PongPacket; - $pk->ptime = $packet->time; - $pk->time = abs(microtime(true) * 1000); - $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]; - $this->PROTOCOL = $packet->PROTOCOL; - 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 < ProtocolInfo9::CURRENT_PROTOCOL_9 || $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; - } + }else{ + switch($packet->pid()){ + case 0x01: + break; + case ProtocolInfo::PONG_PACKET: + break; + case ProtocolInfo::PING_PACKET: + $pk = new PongPacket; + $pk->ptime = $packet->time; + $pk->time = abs(microtime(true) * 1000); + $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]; + $this->PROTOCOL = $packet->PROTOCOL; + 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($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"); - } + 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; - } + $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; - } + 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->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->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->data->set("lastIP", $this->ip); + $this->data->set("lastID", $this->clientID); - $this->server->api->player->saveOffline($this->data); + $this->server->api->player->saveOffline($this->data); - $pk = new LoginStatusPacket; - $pk->status = 0; - $this->dataPacket($pk); + $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); + $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); + 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); + } - $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->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); - //$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"); - } + $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); - $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; - } + $pk = new SetTimePacket; + $pk->time = $this->level->getTime(); + $pk->started = !$this->level->isTimeStopped(); + $this->dataPacket($pk); - 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;//????? - } + 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; + } - $data["slot"] = $packet->slot; + $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); - 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; - } + //$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"); + } - $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); + $this->sendInventory(); + $this->sendSettings(); + $this->server->schedule(50, [$this, "orderChunks"], []); + $this->blocked = false; - $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); + $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; - $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; + $data = []; + $data["eid"] = $packet->eid; + $data["player"] = $this; - 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($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->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); + 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;//????? + } - $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); + $data["slot"] = $packet->slot; - $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 = []; + 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; + } + } - 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); + 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; + } - $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 = []; + $blockVector = new Vector3($packet->x, $packet->y, $packet->z); - $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; - } + 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); - } - $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); - } - } + $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; - 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); + $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; - $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{ + 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]); + $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; + } - $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($message == "pf"){ + // Living::$pathfind = !Living::$pathfind; + //} - if($this->lastCraft <= (microtime(true) - 1)){ - if(isset($this->toCraft[-1])){ - $this->toCraft = [-1 => $this->toCraft[-1]]; - }else{ - $this->toCraft = []; - } - $this->craftingItems = []; - } + 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]); - 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; - } + $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($craft === true){ - $this->lastCraft = microtime(true); - } + if($this->lastCraft <= (microtime(true) - 1)){ + if(isset($this->toCraft[-1])){ + $this->toCraft = [-1 => $this->toCraft[-1]]; + }else{ + $this->toCraft = []; + } + $this->craftingItems = []; + } - 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($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(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; - } + if($craft === true){ + $this->lastCraft = microtime(true); + } - $item = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count); + 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; + } - $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); + 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; + } - $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; - } + $item = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count); - if($tile->class === TILE_FURNACE and $packet->slot == 2){ - switch($slot->getID()){ - case IRON_INGOT: - AchievementAPI::grantAchievement($this, "acquireIron"); - break; - } - } + $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); - 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); - } + $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; + } - $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; - } + 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; + } + } } /** From ad42a6a4a11a91eee6e1178ffc7d8f8a30a13352 Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Sat, 15 Mar 2025 15:45:15 +0800 Subject: [PATCH 03/18] MCPE 0.5.0 Protocol List --- src/network/protocol/ProtocolInfo.php | 69 +++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/src/network/protocol/ProtocolInfo.php b/src/network/protocol/ProtocolInfo.php index d7fd1e4b..8ece5d7c 100644 --- a/src/network/protocol/ProtocolInfo.php +++ b/src/network/protocol/ProtocolInfo.php @@ -218,6 +218,75 @@ abstract class ProtocolInfo9{ const PLAYER_INPUT_PACKET = 0xb9; } +abstract class ProtocolInfo7{ + + const CURRENT_PROTOCOL_7 = 7; + + const PING_PACKET = 0x00; + + const PONG_PACKET = 0x03; + + const CLIENT_CONNECT_PACKET = 0x09; + const SERVER_HANDSHAKE_PACKET = 0x10; + + const CLIENT_HANDSHAKE_PACKET = 0x13; + //const SERVER_FULL_PACKET = 0x14; + const DISCONNECT_PACKET = 0x15; + const LOGIN_PACKET = 0x82; + const LOGIN_STATUS_PACKET = 0x83; + const READY_PACKET = 0x84; + const MESSAGE_PACKET = 0x85; + const SET_TIME_PACKET = 0x86; + const START_GAME_PACKET = 0x87; + const ADD_MOB_PACKET = 0x88; + const ADD_PLAYER_PACKET = 0x89; + const REMOVE_PLAYER_PACKET = 0x8a;//Maybe Exist + + const ADD_ENTITY_PACKET = 0x8c; + const REMOVE_ENTITY_PACKET = 0x8d; + const ADD_ITEM_ENTITY_PACKET = 0x8e; + const TAKE_ITEM_ENTITY_PACKET = 0x8f; + const MOVE_ENTITY_PACKET = 0x90; + + const MOVE_ENTITY_PACKET_POSROT = 0x93; + const MOVE_PLAYER_PACKET = 0x94; + //const PLACE_BLOCK_PACKET = 0x95; + const REMOVE_BLOCK_PACKET = 0x96; + const UPDATE_BLOCK_PACKET = 0x97; + const ADD_PAINTING_PACKET = 0x98;// Maybe exist + const EXPLODE_PACKET = 0x99; + const LEVEL_EVENT_PACKET = 0x9a; + const TILE_EVENT_PACKET = 0x9b;//Maybe exist + const ENTITY_EVENT_PACKET = 0x9c; + const REQUEST_CHUNK_PACKET = 0x9d; + const CHUNK_DATA_PACKET = 0x9e; + const PLAYER_EQUIPMENT_PACKET = 0x9f; + //const PLAYER_ARMOR_EQUIPMENT_PACKET = 0xa0; + const INTERACT_PACKET = 0xa0; + const USE_ITEM_PACKET = 0xa1; + const PLAYER_ACTION_PACKET = 0xa2; + + //const HURT_ARMOR_PACKET = 0xa3; + const SET_ENTITY_DATA_PACKET = 0xa3; + const SET_ENTITY_MOTION_PACKET = 0xa4; + //const SET_ENTITY_LINK_PACKET = 0xa?;// Change + const SET_HEALTH_PACKET = 0xa5;// Change + const SET_SPAWN_POSITION_PACKET = 0xa6; + const ANIMATE_PACKET = 0xa7; + const RESPAWN_PACKET = 0xa8; + const SEND_INVENTORY_PACKET = 0xa9;//Maybe exist + const DROP_ITEM_PACKET = 0xaa; + const CONTAINER_OPEN_PACKET = 0xab; + const CONTAINER_CLOSE_PACKET = 0xac; + const CONTAINER_SET_SLOT_PACKET = 0xad; + //const CONTAINER_SET_DATA_PACKET = 0xb1; + //const CONTAINER_SET_CONTENT_PACKET = 0xb2; + //const CONTAINER_ACK_PACKET = 0xb3; + const CHAT_PACKET = 0xb1;//12 change + const ADVENTURE_SETTINGS_PACKET = 0xb3; + const ENTITY_DATA_PACKET = 0xb2; + //const PLAYER_INPUT_PACKET = 0xb9; +} /***REM_START***/ require_once(FILE_PATH . "src/network/raknet/RakNetDataPacket.php"); /***REM_END***/ \ No newline at end of file From 61ffae58fa3d6bff99b12372d7480f805dfc036e Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Sun, 16 Mar 2025 12:47:53 +0800 Subject: [PATCH 04/18] MCPE 0.5.0 Protocol List && Stupid PC --- src/Player.php | 12 ++++- src/network/protocol/ProtocolInfo.php | 69 +++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/src/Player.php b/src/Player.php index c4b79b39..c510b5a9 100755 --- a/src/Player.php +++ b/src/Player.php @@ -354,6 +354,7 @@ class Player{ return; } + $packet->PROTOCOL = $this->PROTOCOL; $packet->encode(); $len = strlen($packet->buffer) + 1; $MTU = $this->MTU - 24; @@ -1223,6 +1224,7 @@ class Player{ } $this->received[$p->messageIndex] = true; } + $p->PROTOCOL = $this->PROTOCOL; $p->decode(); $this->handleDataPacket($p); } @@ -1286,6 +1288,7 @@ class Player{ $packet->z = $z; $packet->block = $id; $packet->meta = $meta; + $packet->PROTOCOL = $this->PROTOCOL; $packet->encode(); $len = 1 + strlen($packet->buffer); @@ -1318,6 +1321,7 @@ class Player{ $motion->speedX = $e->speedX; $motion->speedY = $e->speedY; $motion->speedZ = $e->speedZ; + $motion->PROTOCOL = $this->PROTOCOL; $motion->encode(); $len += 1 + strlen($motion->buffer); ++$packets; @@ -1339,6 +1343,7 @@ class Player{ $move->yaw = $e->yaw; $move->pitch = $e->pitch; $move->bodyYaw = $e->headYaw; + $move->PROTOCOL = $this->PROTOCOL; $move->encode(); }else{ $move = new MoveEntityPacket_PosRot(); @@ -1348,6 +1353,7 @@ class Player{ $move->z = $e->z; $move->yaw = $e->yaw; $move->pitch = $e->pitch; + $move->PROTOCOL = $this->PROTOCOL; $move->encode(); } @@ -1358,6 +1364,7 @@ class Player{ $headyaw = new RotateHeadPacket(); $headyaw->eid = $e->eid; $headyaw->yaw = $e->headYaw; + $headyaw->PROTOCOL = $this->PROTOCOL; $headyaw->encode(); $len += strlen($headyaw->buffer) + 1; ++$packets; @@ -1483,6 +1490,7 @@ class Player{ return []; } + $packet->PROTOCOL = $this->PROTOCOL; $packet->encode(); $pk = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $pk->data[] = $packet; @@ -1515,7 +1523,7 @@ class Player{ return; } - if ($packet->pid == ProtocolInfo::LOGIN_PACKET) { + if ($packet->pid() == ProtocolInfo::LOGIN_PACKET) { if ($this->loggedIn === true) { return; } @@ -1527,7 +1535,7 @@ class Player{ $this->close("server is full!", false); return; } - if ($packet->protocol1 !== ProtocolInfo::CURRENT_PROTOCOL) { + if ($packet->protocol1 < ProtocolInfo5::CURRENT_PROTOCOL_5 && $packet->protocol1 > ProtocolInfo::CURRENT_PROTOCOL) { if ($packet->protocol1 < ProtocolInfo::CURRENT_PROTOCOL) { $pk = new LoginStatusPacket; $pk->status = 1; diff --git a/src/network/protocol/ProtocolInfo.php b/src/network/protocol/ProtocolInfo.php index 8ece5d7c..96e60981 100644 --- a/src/network/protocol/ProtocolInfo.php +++ b/src/network/protocol/ProtocolInfo.php @@ -248,6 +248,75 @@ abstract class ProtocolInfo7{ const TAKE_ITEM_ENTITY_PACKET = 0x8f; const MOVE_ENTITY_PACKET = 0x90; + const MOVE_ENTITY_PACKET_POSROT = 0x93; + const MOVE_PLAYER_PACKET = 0x94; + //const PLACE_BLOCK_PACKET = 0x95; + const REMOVE_BLOCK_PACKET = 0x96; + const UPDATE_BLOCK_PACKET = 0x97; + const ADD_PAINTING_PACKET = 0x98;// Maybe exist + const EXPLODE_PACKET = 0x99; + const LEVEL_EVENT_PACKET = 0x9a; + const TILE_EVENT_PACKET = 0x9b;//Maybe exist + const ENTITY_EVENT_PACKET = 0x9c; + const REQUEST_CHUNK_PACKET = 0x9d; + const CHUNK_DATA_PACKET = 0x9e; + const PLAYER_EQUIPMENT_PACKET = 0x9f; + //const PLAYER_ARMOR_EQUIPMENT_PACKET = 0xa0; + const INTERACT_PACKET = 0xa0;//Change + const USE_ITEM_PACKET = 0xa1; + const PLAYER_ACTION_PACKET = 0xa2; + + //const HURT_ARMOR_PACKET = 0xa3; + const SET_ENTITY_DATA_PACKET = 0xa3; + const SET_ENTITY_MOTION_PACKET = 0xa4; + //const SET_ENTITY_LINK_PACKET = 0xa?;// Change + const SET_HEALTH_PACKET = 0xa5;// Change + const SET_SPAWN_POSITION_PACKET = 0xa6; + const ANIMATE_PACKET = 0xa7; + const RESPAWN_PACKET = 0xa8; + const SEND_INVENTORY_PACKET = 0xa9;//Maybe exist + const DROP_ITEM_PACKET = 0xaa; + const CONTAINER_OPEN_PACKET = 0xab; + const CONTAINER_CLOSE_PACKET = 0xac; + const CONTAINER_SET_SLOT_PACKET = 0xad; + //const CONTAINER_SET_DATA_PACKET = 0xb1; + //const CONTAINER_SET_CONTENT_PACKET = 0xb2; + //const CONTAINER_ACK_PACKET = 0xb3; + const CHAT_PACKET = 0xb1;//12 change + const ADVENTURE_SETTINGS_PACKET = 0xb3; + const ENTITY_DATA_PACKET = 0xb2; + //const PLAYER_INPUT_PACKET = 0xb9; +} +abstract class ProtocolInfo5{ + + const CURRENT_PROTOCOL_5 = 5; + + const PING_PACKET = 0x00; + + const PONG_PACKET = 0x03; + + const CLIENT_CONNECT_PACKET = 0x09; + const SERVER_HANDSHAKE_PACKET = 0x10; + + const CLIENT_HANDSHAKE_PACKET = 0x13; + //const SERVER_FULL_PACKET = 0x14; + const DISCONNECT_PACKET = 0x15; + const LOGIN_PACKET = 0x82; + const LOGIN_STATUS_PACKET = 0x83; + const READY_PACKET = 0x84; + const MESSAGE_PACKET = 0x85; + const SET_TIME_PACKET = 0x86; + const START_GAME_PACKET = 0x87; + const ADD_MOB_PACKET = 0x88; + const ADD_PLAYER_PACKET = 0x89; + const REMOVE_PLAYER_PACKET = 0x8a;//Maybe Exist + + const ADD_ENTITY_PACKET = 0x8c; + const REMOVE_ENTITY_PACKET = 0x8d; + const ADD_ITEM_ENTITY_PACKET = 0x8e; + const TAKE_ITEM_ENTITY_PACKET = 0x8f; + const MOVE_ENTITY_PACKET = 0x90; + const MOVE_ENTITY_PACKET_POSROT = 0x93; const MOVE_PLAYER_PACKET = 0x94; //const PLACE_BLOCK_PACKET = 0x95; From 7f70c87a38bcaa27ae062a5a54bb0306738e2259 Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Sun, 16 Mar 2025 13:06:14 +0800 Subject: [PATCH 05/18] MovePlayerPacket fixed --- src/Player.php | 2 +- src/network/protocol/packet/MovePlayerPacket.php | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Player.php b/src/Player.php index c510b5a9..3385af26 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1360,7 +1360,7 @@ class Player{ $len += strlen($move->buffer) + 1; ++$packets; $moveSent = true; - }else if($e->headYaw != $e->lastHeadYaw){ + }else if($this->PROTOCOL > ProtocolInfo12::CURRENT_PROTOCOL_12 && $e->headYaw != $e->lastHeadYaw){ $headyaw = new RotateHeadPacket(); $headyaw->eid = $e->eid; $headyaw->yaw = $e->headYaw; diff --git a/src/network/protocol/packet/MovePlayerPacket.php b/src/network/protocol/packet/MovePlayerPacket.php index 5d806a15..229f2bdf 100644 --- a/src/network/protocol/packet/MovePlayerPacket.php +++ b/src/network/protocol/packet/MovePlayerPacket.php @@ -23,7 +23,9 @@ class MovePlayerPacket extends RakNetDataPacket{ $this->z = $this->getFloat(); $this->yaw = $this->getFloat(); $this->pitch = $this->getFloat(); - $this->bodyYaw = $this->getFloat(); + if($this->PROTOCOL > 13){ + $this->bodyYaw = $this->getFloat(); + } } public function encode(){ @@ -34,7 +36,9 @@ class MovePlayerPacket extends RakNetDataPacket{ $this->putFloat($this->z); $this->putFloat($this->yaw); $this->putFloat($this->pitch); - $this->putFloat($this->bodyYaw); + if($this->PROTOCOL > 13){ + $this->putFloat($this->bodyYaw); + } } } \ No newline at end of file From 50cc72be760bc42df6e991426e9abbdc08b3c3ea Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Wed, 19 Mar 2025 20:02:56 +0800 Subject: [PATCH 06/18] MovePlayerPacket fixed --- src/API/PlayerAPI.php | 12 ++ src/network/MinecraftInterface.php | 2 +- src/network/raknet/RakNetParser.php | 262 +++++++++++++++++++++++++++- 3 files changed, 270 insertions(+), 6 deletions(-) diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 373f3c70..982456a7 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -327,6 +327,18 @@ class PlayerAPI{ return $o; } + /** + * @return int + */ + public static function decodeProtocol($ip){ + foreach(ServerAPI::request()->clients as $p) { + if($p->ip == $ip){ + console($p->PROTOCOL); + return $p->PROTOCOL; + } + } + } + public function add($CID){ if(isset($this->server->clients[$CID])){ $player = $this->server->clients[$CID]; diff --git a/src/network/MinecraftInterface.php b/src/network/MinecraftInterface.php index ad91322e..f7702f17 100644 --- a/src/network/MinecraftInterface.php +++ b/src/network/MinecraftInterface.php @@ -41,7 +41,7 @@ class MinecraftInterface{ $pid = ord($buffer[0]); if(RakNetInfo::isValid($pid)){ - $parser = new RakNetParser($buffer); + $parser = new RakNetParser($buffer, $source); if($parser->packet !== false){ $parser->packet->ip = $source; $parser->packet->port = $port; diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php index 0d6b8b29..179fc478 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -4,9 +4,11 @@ class RakNetParser{ public $packet; private $buffer; + private $source; private $offset; - public function __construct(&$buffer){ + public function __construct(&$buffer, $source){ + $this->source =& $source; $this->buffer =& $buffer; $this->offset = 0; if(strlen($this->buffer) > 0){ @@ -56,10 +58,31 @@ class RakNetParser{ case RakNetInfo::DATA_PACKET_F: $this->packet->seqNumber = $this->getLTriad(); $this->packet->data = []; - while(!$this->feof() and ($pk = $this->parseDataPacket()) instanceof RakNetDataPacket){ - $this->packet->data[] = $pk; - } - break; + $this->packet->ip = $this->source; + console($this->source); + $PROTOCOL = PlayerAPI::decodeProtocol($this->packet->ip); + console("1111"); + if($PROTOCOL > 13){ + while(!$this->feof() and ($pk = $this->parseDataPacket()) instanceof RakNetDataPacket){ + $this->packet->data[] = $pk; + } + break; + }elseif ($PROTOCOL > 10){ + while(!$this->feof() and ($pk = $this->parseDataPacket12()) instanceof RakNetDataPacket){ + $this->packet->data[] = $pk; + } + break; + }elseif ($PROTOCOL >= 3){ + while(!$this->feof() and ($pk = $this->parseDataPacket()) instanceof RakNetDataPacket){ + $this->packet->data[] = $pk; + } + break; + }else{ + while(!$this->feof() and ($pk = $this->parseDataPacket()) instanceof RakNetDataPacket){ + $this->packet->data[] = $pk; + } + break; + } case RakNetInfo::NACK: case RakNetInfo::ACK: $count = $this->getShort(); @@ -346,6 +369,235 @@ class RakNetParser{ return $data; } + private function parseDataPacket12(){ + $packetFlags = $this->getByte(); + $reliability = ($packetFlags & 0b11100000) >> 5; + $hasSplit = ($packetFlags & 0b00010000) > 0; + $length = (int) ceil($this->getShort() / 8); + if($reliability === 2 + or $reliability === 3 + or $reliability === 4 + or $reliability === 6 + or $reliability === 7){ + $messageIndex = $this->getLTriad(); + }else{ + $messageIndex = false; + } + + if($reliability === 1 + or $reliability === 3 + or $reliability === 4 + or $reliability === 7){ + $orderIndex = $this->getLTriad(); + $orderChannel = $this->getByte(); + }else{ + $orderIndex = false; + $orderChannel = false; + } + + if($hasSplit){ + $splitCount = $this->getInt(); + $splitID = $this->getShort(); + $splitIndex = $this->getInt(); + }else{ + $splitCount = false; + $splitID = false; + $splitIndex = false; + } + + if($length <= 0 + or $orderChannel >= 32 + or ($hasSplit === true and $splitIndex >= $splitCount)){ + return false; + }else{ + $pid = $this->getByte(); + $buffer = $this->get($length - 1); + if(strlen($buffer) < ($length - 1)){ + return false; + } + switch($pid){ + case ProtocolInfo12::PING_PACKET: + $data = new PingPacket; + break; + case ProtocolInfo12::PONG_PACKET: + $data = new PongPacket; + break; + case ProtocolInfo12::CLIENT_CONNECT_PACKET: + $data = new ClientConnectPacket; + break; + case ProtocolInfo12::SERVER_HANDSHAKE_PACKET: + $data = new ServerHandshakePacket; + break; + case ProtocolInfo12::DISCONNECT_PACKET: + $data = new DisconnectPacket; + break; + case ProtocolInfo12::LOGIN_PACKET: + $data = new LoginPacket; + break; + case ProtocolInfo12::LOGIN_STATUS_PACKET: + $data = new LoginStatusPacket; + break; + case ProtocolInfo12::READY_PACKET: + $data = new ReadyPacket; + break; + case ProtocolInfo12::MESSAGE_PACKET: + $data = new MessagePacket; + break; + case ProtocolInfo12::SET_TIME_PACKET: + $data = new SetTimePacket; + break; + case ProtocolInfo12::START_GAME_PACKET: + $data = new StartGamePacket; + break; + case ProtocolInfo12::ADD_MOB_PACKET: + $data = new AddMobPacket; + break; + case ProtocolInfo12::ADD_PLAYER_PACKET: + $data = new AddPlayerPacket; + break; + case ProtocolInfo12::REMOVE_PLAYER_PACKET: + $data = new RemovePlayerPacket; + break; + case ProtocolInfo12::ADD_ENTITY_PACKET: + $data = new AddEntityPacket; + break; + case ProtocolInfo12::REMOVE_ENTITY_PACKET: + $data = new RemoveEntityPacket; + break; + case ProtocolInfo12::ADD_ITEM_ENTITY_PACKET: + $data = new AddItemEntityPacket; + break; + case ProtocolInfo12::TAKE_ITEM_ENTITY_PACKET: + $data = new TakeItemEntityPacket; + break; + case ProtocolInfo12::MOVE_ENTITY_PACKET: + $data = new MoveEntityPacket; + break; + case ProtocolInfo12::MOVE_ENTITY_PACKET_POSROT: + $data = new MoveEntityPacket_PosRot; + break; + case ProtocolInfo12::ROTATE_HEAD_PACKET: + $data = new RotateHeadPacket; + break; + case ProtocolInfo12::MOVE_PLAYER_PACKET: + $data = new MovePlayerPacket; + break; + case ProtocolInfo12::REMOVE_BLOCK_PACKET: + $data = new RemoveBlockPacket; + break; + case ProtocolInfo12::UPDATE_BLOCK_PACKET: + $data = new UpdateBlockPacket; + break; + case ProtocolInfo12::ADD_PAINTING_PACKET: + $data = new AddPaintingPacket; + break; + case ProtocolInfo12::EXPLODE_PACKET: + $data = new ExplodePacket; + break; + case ProtocolInfo12::LEVEL_EVENT_PACKET: + $data = new LevelEventPacket; + break; + case ProtocolInfo12::TILE_EVENT_PACKET: + $data = new TileEventPacket; + break; + case ProtocolInfo12::ENTITY_EVENT_PACKET: + $data = new EntityEventPacket; + break; + case ProtocolInfo12::REQUEST_CHUNK_PACKET: + $data = new RequestChunkPacket; + break; + case ProtocolInfo12::CHUNK_DATA_PACKET: + $data = new ChunkDataPacket; + break; + case ProtocolInfo12::PLAYER_EQUIPMENT_PACKET: + $data = new PlayerEquipmentPacket; + break; + case ProtocolInfo12::PLAYER_ARMOR_EQUIPMENT_PACKET: + $data = new PlayerArmorEquipmentPacket; + break; + case ProtocolInfo12::INTERACT_PACKET: + $data = new InteractPacket; + break; + case ProtocolInfo12::USE_ITEM_PACKET: + $data = new UseItemPacket; + break; + case ProtocolInfo12::PLAYER_ACTION_PACKET: + $data = new PlayerActionPacket; + break; + case ProtocolInfo12::HURT_ARMOR_PACKET: + $data = new HurtArmorPacket; + break; + case ProtocolInfo12::SET_ENTITY_DATA_PACKET: + $data = new SetEntityDataPacket; + break; + case ProtocolInfo12::SET_ENTITY_MOTION_PACKET: + $data = new SetEntityMotionPacket; + break; + case ProtocolInfo12::SET_HEALTH_PACKET: + $data = new SetHealthPacket; + break; + case ProtocolInfo12::SET_SPAWN_POSITION_PACKET: + $data = new SetSpawnPositionPacket; + break; + case ProtocolInfo12::ANIMATE_PACKET: + $data = new AnimatePacket; + break; + case ProtocolInfo12::RESPAWN_PACKET: + $data = new RespawnPacket; + break; + case ProtocolInfo12::SEND_INVENTORY_PACKET: + $data = new SendInventoryPacket; + break; + case ProtocolInfo12::DROP_ITEM_PACKET: + $data = new DropItemPacket; + break; + case ProtocolInfo12::CONTAINER_OPEN_PACKET: + $data = new ContainerOpenPacket; + break; + case ProtocolInfo12::CONTAINER_CLOSE_PACKET: + $data = new ContainerClosePacket; + break; + case ProtocolInfo12::CONTAINER_SET_SLOT_PACKET: + $data = new ContainerSetSlotPacket; + break; + case ProtocolInfo12::CONTAINER_SET_DATA_PACKET: + $data = new ContainerSetDataPacket; + break; + case ProtocolInfo12::CONTAINER_SET_CONTENT_PACKET: + $data = new ContainerSetContentPacket; + break; + case ProtocolInfo12::CHAT_PACKET: + $data = new ChatPacket; + break; + case ProtocolInfo12::ADVENTURE_SETTINGS_PACKET: + $data = new AdventureSettingsPacket; + break; + case ProtocolInfo12::ENTITY_DATA_PACKET: + $data = new EntityDataPacket; + break; + case ProtocolInfo12::SET_ENTITY_LINK_PACKET: + $data = new SetEntityLinkPacket; + case ProtocolInfo12::PLAYER_INPUT_PACKET: + $data = new PlayerInputPacket; + break; + default: + $data = new UnknownPacket(); + $data->packetID = $pid; + break; + } + $data->reliability = $reliability; + $data->hasSplit = $hasSplit; + $data->messageIndex = $messageIndex; + $data->orderIndex = $orderIndex; + $data->orderChannel = $orderChannel; + $data->splitCount = $splitCount; + $data->splitID = $splitID; + $data->splitIndex = $splitIndex; + $data->setBuffer($buffer); + } + return $data; + } + private function getInt($unsigned = false){ return Utils::readInt($this->get(4), $unsigned); } From 8988b227701fda2408c25b8048a9c4b204181f59 Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Wed, 19 Mar 2025 20:23:38 +0800 Subject: [PATCH 07/18] Fixed logs --- src/network/raknet/RakNetParser.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php index 179fc478..b378168f 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -59,9 +59,7 @@ class RakNetParser{ $this->packet->seqNumber = $this->getLTriad(); $this->packet->data = []; $this->packet->ip = $this->source; - console($this->source); $PROTOCOL = PlayerAPI::decodeProtocol($this->packet->ip); - console("1111"); if($PROTOCOL > 13){ while(!$this->feof() and ($pk = $this->parseDataPacket()) instanceof RakNetDataPacket){ $this->packet->data[] = $pk; From 6cfc8f138536095c326544c19145760e1572100c Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Wed, 19 Mar 2025 20:56:27 +0800 Subject: [PATCH 08/18] Blame this changes xdd --- src/network/raknet/RakNetParser.php | 455 +++++++++++++++++++++++++++- 1 file changed, 454 insertions(+), 1 deletion(-) diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php index b378168f..b193a36b 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -70,8 +70,13 @@ class RakNetParser{ $this->packet->data[] = $pk; } break; + }elseif ($PROTOCOL >= 9){ + while(!$this->feof() and ($pk = $this->parseDataPacket9()) instanceof RakNetDataPacket){ + $this->packet->data[] = $pk; + } + break; }elseif ($PROTOCOL >= 3){ - while(!$this->feof() and ($pk = $this->parseDataPacket()) instanceof RakNetDataPacket){ + while(!$this->feof() and ($pk = $this->parseDataPacket3()) instanceof RakNetDataPacket){ $this->packet->data[] = $pk; } break; @@ -596,6 +601,454 @@ class RakNetParser{ return $data; } + private function parseDataPacket9(){ + $packetFlags = $this->getByte(); + $reliability = ($packetFlags & 0b11100000) >> 5; + $hasSplit = ($packetFlags & 0b00010000) > 0; + $length = (int) ceil($this->getShort() / 8); + if($reliability === 2 + or $reliability === 3 + or $reliability === 4 + or $reliability === 6 + or $reliability === 7){ + $messageIndex = $this->getLTriad(); + }else{ + $messageIndex = false; + } + + if($reliability === 1 + or $reliability === 3 + or $reliability === 4 + or $reliability === 7){ + $orderIndex = $this->getLTriad(); + $orderChannel = $this->getByte(); + }else{ + $orderIndex = false; + $orderChannel = false; + } + + if($hasSplit){ + $splitCount = $this->getInt(); + $splitID = $this->getShort(); + $splitIndex = $this->getInt(); + }else{ + $splitCount = false; + $splitID = false; + $splitIndex = false; + } + + if($length <= 0 + or $orderChannel >= 32 + or ($hasSplit === true and $splitIndex >= $splitCount)){ + return false; + }else{ + $pid = $this->getByte(); + $buffer = $this->get($length - 1); + if(strlen($buffer) < ($length - 1)){ + return false; + } + switch($pid){ + case ProtocolInfo9::PING_PACKET: + $data = new PingPacket; + break; + case ProtocolInfo9::PONG_PACKET: + $data = new PongPacket; + break; + case ProtocolInfo9::CLIENT_CONNECT_PACKET: + $data = new ClientConnectPacket; + break; + case ProtocolInfo9::SERVER_HANDSHAKE_PACKET: + $data = new ServerHandshakePacket; + break; + case ProtocolInfo9::DISCONNECT_PACKET: + $data = new DisconnectPacket; + break; + case ProtocolInfo9::LOGIN_PACKET: + $data = new LoginPacket; + break; + case ProtocolInfo9::LOGIN_STATUS_PACKET: + $data = new LoginStatusPacket; + break; + case ProtocolInfo9::READY_PACKET: + $data = new ReadyPacket; + break; + case ProtocolInfo9::MESSAGE_PACKET: + $data = new MessagePacket; + break; + case ProtocolInfo9::SET_TIME_PACKET: + $data = new SetTimePacket; + break; + case ProtocolInfo9::START_GAME_PACKET: + $data = new StartGamePacket; + break; + case ProtocolInfo9::ADD_MOB_PACKET: + $data = new AddMobPacket; + break; + case ProtocolInfo9::ADD_PLAYER_PACKET: + $data = new AddPlayerPacket; + break; + case ProtocolInfo9::REMOVE_PLAYER_PACKET: + $data = new RemovePlayerPacket; + break; + case ProtocolInfo9::ADD_ENTITY_PACKET: + $data = new AddEntityPacket; + break; + case ProtocolInfo9::REMOVE_ENTITY_PACKET: + $data = new RemoveEntityPacket; + break; + case ProtocolInfo9::ADD_ITEM_ENTITY_PACKET: + $data = new AddItemEntityPacket; + break; + case ProtocolInfo9::TAKE_ITEM_ENTITY_PACKET: + $data = new TakeItemEntityPacket; + break; + case ProtocolInfo9::MOVE_ENTITY_PACKET: + $data = new MoveEntityPacket; + break; + case ProtocolInfo9::MOVE_ENTITY_PACKET_POSROT: + $data = new MoveEntityPacket_PosRot; + break; + case ProtocolInfo9::MOVE_PLAYER_PACKET: + $data = new MovePlayerPacket; + break; + case ProtocolInfo9::REMOVE_BLOCK_PACKET: + $data = new RemoveBlockPacket; + break; + case ProtocolInfo9::UPDATE_BLOCK_PACKET: + $data = new UpdateBlockPacket; + break; + case ProtocolInfo9::ADD_PAINTING_PACKET: + $data = new AddPaintingPacket; + break; + case ProtocolInfo9::EXPLODE_PACKET: + $data = new ExplodePacket; + break; + case ProtocolInfo9::LEVEL_EVENT_PACKET: + $data = new LevelEventPacket; + break; + case ProtocolInfo9::TILE_EVENT_PACKET: + $data = new TileEventPacket; + break; + case ProtocolInfo9::ENTITY_EVENT_PACKET: + $data = new EntityEventPacket; + break; + case ProtocolInfo9::REQUEST_CHUNK_PACKET: + $data = new RequestChunkPacket; + break; + case ProtocolInfo9::CHUNK_DATA_PACKET: + $data = new ChunkDataPacket; + break; + case ProtocolInfo9::PLAYER_EQUIPMENT_PACKET: + $data = new PlayerEquipmentPacket; + break; + case ProtocolInfo9::PLAYER_ARMOR_EQUIPMENT_PACKET: + $data = new PlayerArmorEquipmentPacket; + break; + case ProtocolInfo9::INTERACT_PACKET: + $data = new InteractPacket; + break; + case ProtocolInfo9::USE_ITEM_PACKET: + $data = new UseItemPacket; + break; + case ProtocolInfo9::PLAYER_ACTION_PACKET: + $data = new PlayerActionPacket; + break; + case ProtocolInfo9::HURT_ARMOR_PACKET: + $data = new HurtArmorPacket; + break; + case ProtocolInfo9::SET_ENTITY_DATA_PACKET: + $data = new SetEntityDataPacket; + break; + case ProtocolInfo9::SET_ENTITY_MOTION_PACKET: + $data = new SetEntityMotionPacket; + break; + case ProtocolInfo9::SET_HEALTH_PACKET: + $data = new SetHealthPacket; + break; + case ProtocolInfo9::SET_SPAWN_POSITION_PACKET: + $data = new SetSpawnPositionPacket; + break; + case ProtocolInfo9::ANIMATE_PACKET: + $data = new AnimatePacket; + break; + case ProtocolInfo9::RESPAWN_PACKET: + $data = new RespawnPacket; + break; + case ProtocolInfo9::SEND_INVENTORY_PACKET: + $data = new SendInventoryPacket; + break; + case ProtocolInfo9::DROP_ITEM_PACKET: + $data = new DropItemPacket; + break; + case ProtocolInfo9::CONTAINER_OPEN_PACKET: + $data = new ContainerOpenPacket; + break; + case ProtocolInfo9::CONTAINER_CLOSE_PACKET: + $data = new ContainerClosePacket; + break; + case ProtocolInfo9::CONTAINER_SET_SLOT_PACKET: + $data = new ContainerSetSlotPacket; + break; + case ProtocolInfo9::CONTAINER_SET_DATA_PACKET: + $data = new ContainerSetDataPacket; + break; + case ProtocolInfo9::CONTAINER_SET_CONTENT_PACKET: + $data = new ContainerSetContentPacket; + break; + case ProtocolInfo9::CHAT_PACKET: + $data = new ChatPacket; + break; + case ProtocolInfo9::ADVENTURE_SETTINGS_PACKET: + $data = new AdventureSettingsPacket; + break; + case ProtocolInfo9::ENTITY_DATA_PACKET: + $data = new EntityDataPacket; + break; + case ProtocolInfo9::PLAYER_INPUT_PACKET: + $data = new PlayerInputPacket; + break; + default: + $data = new UnknownPacket(); + $data->packetID = $pid; + break; + } + $data->reliability = $reliability; + $data->hasSplit = $hasSplit; + $data->messageIndex = $messageIndex; + $data->orderIndex = $orderIndex; + $data->orderChannel = $orderChannel; + $data->splitCount = $splitCount; + $data->splitID = $splitID; + $data->splitIndex = $splitIndex; + $data->setBuffer($buffer); + } + return $data; + } + + private function parseDataPacket3(){ + $packetFlags = $this->getByte(); + $reliability = ($packetFlags & 0b11100000) >> 5; + $hasSplit = ($packetFlags & 0b00010000) > 0; + $length = (int) ceil($this->getShort() / 8); + if($reliability === 2 + or $reliability === 3 + or $reliability === 4 + or $reliability === 6 + or $reliability === 7){ + $messageIndex = $this->getLTriad(); + }else{ + $messageIndex = false; + } + + if($reliability === 1 + or $reliability === 3 + or $reliability === 4 + or $reliability === 7){ + $orderIndex = $this->getLTriad(); + $orderChannel = $this->getByte(); + }else{ + $orderIndex = false; + $orderChannel = false; + } + + if($hasSplit){ + $splitCount = $this->getInt(); + $splitID = $this->getShort(); + $splitIndex = $this->getInt(); + }else{ + $splitCount = false; + $splitID = false; + $splitIndex = false; + } + + if($length <= 0 + or $orderChannel >= 32 + or ($hasSplit === true and $splitIndex >= $splitCount)){ + return false; + }else{ + $pid = $this->getByte(); + $buffer = $this->get($length - 1); + if(strlen($buffer) < ($length - 1)){ + return false; + } + switch($pid){ + case ProtocolInfo7::PING_PACKET: + $data = new PingPacket; + break; + case ProtocolInfo7::PONG_PACKET: + $data = new PongPacket; + break; + case ProtocolInfo7::CLIENT_CONNECT_PACKET: + $data = new ClientConnectPacket; + break; + case ProtocolInfo7::SERVER_HANDSHAKE_PACKET: + $data = new ServerHandshakePacket; + break; + case ProtocolInfo7::DISCONNECT_PACKET: + $data = new DisconnectPacket; + break; + case ProtocolInfo7::LOGIN_PACKET: + $data = new LoginPacket; + break; + case ProtocolInfo7::LOGIN_STATUS_PACKET: + $data = new LoginStatusPacket; + break; + case ProtocolInfo7::READY_PACKET: + $data = new ReadyPacket; + break; + case ProtocolInfo7::MESSAGE_PACKET: + $data = new MessagePacket; + break; + case ProtocolInfo7::SET_TIME_PACKET: + $data = new SetTimePacket; + break; + case ProtocolInfo7::START_GAME_PACKET: + $data = new StartGamePacket; + break; + case ProtocolInfo7::ADD_MOB_PACKET: + $data = new AddMobPacket; + break; + case ProtocolInfo7::ADD_PLAYER_PACKET: + $data = new AddPlayerPacket; + break; + case ProtocolInfo7::REMOVE_PLAYER_PACKET: + $data = new RemovePlayerPacket; + break; + case ProtocolInfo7::ADD_ENTITY_PACKET: + $data = new AddEntityPacket; + break; + case ProtocolInfo7::REMOVE_ENTITY_PACKET: + $data = new RemoveEntityPacket; + break; + case ProtocolInfo7::ADD_ITEM_ENTITY_PACKET: + $data = new AddItemEntityPacket; + break; + case ProtocolInfo7::TAKE_ITEM_ENTITY_PACKET: + $data = new TakeItemEntityPacket; + break; + case ProtocolInfo7::MOVE_ENTITY_PACKET: + $data = new MoveEntityPacket; + break; + case ProtocolInfo7::MOVE_ENTITY_PACKET_POSROT: + $data = new MoveEntityPacket_PosRot; + break; + case ProtocolInfo7::MOVE_PLAYER_PACKET: + $data = new MovePlayerPacket; + break; + case ProtocolInfo7::REMOVE_BLOCK_PACKET: + $data = new RemoveBlockPacket; + break; + case ProtocolInfo7::UPDATE_BLOCK_PACKET: + $data = new UpdateBlockPacket; + break; + case ProtocolInfo7::ADD_PAINTING_PACKET: + $data = new AddPaintingPacket; + break; + case ProtocolInfo7::EXPLODE_PACKET: + $data = new ExplodePacket; + break; + case ProtocolInfo7::LEVEL_EVENT_PACKET: + $data = new LevelEventPacket; + break; + case ProtocolInfo7::TILE_EVENT_PACKET: + $data = new TileEventPacket; + break; + case ProtocolInfo7::ENTITY_EVENT_PACKET: + $data = new EntityEventPacket; + break; + case ProtocolInfo7::REQUEST_CHUNK_PACKET: + $data = new RequestChunkPacket; + break; + case ProtocolInfo7::CHUNK_DATA_PACKET: + $data = new ChunkDataPacket; + break; + case ProtocolInfo7::PLAYER_EQUIPMENT_PACKET: + $data = new PlayerEquipmentPacket; + break; + /*case ProtocolInfo7::PLAYER_ARMOR_EQUIPMENT_PACKET: + $data = new PlayerArmorEquipmentPacket; + break;*/ + case ProtocolInfo7::INTERACT_PACKET: + $data = new InteractPacket; + break; + case ProtocolInfo7::USE_ITEM_PACKET: + $data = new UseItemPacket; + break; + case ProtocolInfo7::PLAYER_ACTION_PACKET: + $data = new PlayerActionPacket; + break; + /*case ProtocolInfo7::HURT_ARMOR_PACKET: + $data = new HurtArmorPacket; + break;*/ + case ProtocolInfo7::SET_ENTITY_DATA_PACKET: + $data = new SetEntityDataPacket; + break; + case ProtocolInfo7::SET_ENTITY_MOTION_PACKET: + $data = new SetEntityMotionPacket; + break; + case ProtocolInfo7::SET_HEALTH_PACKET: + $data = new SetHealthPacket; + break; + case ProtocolInfo7::SET_SPAWN_POSITION_PACKET: + $data = new SetSpawnPositionPacket; + break; + case ProtocolInfo7::ANIMATE_PACKET: + $data = new AnimatePacket; + break; + case ProtocolInfo7::RESPAWN_PACKET: + $data = new RespawnPacket; + break; + case ProtocolInfo7::SEND_INVENTORY_PACKET: + $data = new SendInventoryPacket; + break; + case ProtocolInfo7::DROP_ITEM_PACKET: + $data = new DropItemPacket; + break; + case ProtocolInfo7::CONTAINER_OPEN_PACKET: + $data = new ContainerOpenPacket; + break; + case ProtocolInfo7::CONTAINER_CLOSE_PACKET: + $data = new ContainerClosePacket; + break; + case ProtocolInfo7::CONTAINER_SET_SLOT_PACKET: + $data = new ContainerSetSlotPacket; + break; + /*case ProtocolInfo7::CONTAINER_SET_DATA_PACKET: + $data = new ContainerSetDataPacket; + break;*/ + /*case ProtocolInfo7::CONTAINER_SET_CONTENT_PACKET: + $data = new ContainerSetContentPacket; + break;*/ + case ProtocolInfo7::CHAT_PACKET: + $data = new ChatPacket; + break; + case ProtocolInfo7::ADVENTURE_SETTINGS_PACKET: + $data = new AdventureSettingsPacket; + break; + case ProtocolInfo7::ENTITY_DATA_PACKET: + $data = new EntityDataPacket; + break; + /*case ProtocolInfo7::PLAYER_INPUT_PACKET: + $data = new PlayerInputPacket; + break;*/ + default: + $data = new UnknownPacket(); + $data->packetID = $pid; + break; + } + $data->reliability = $reliability; + $data->hasSplit = $hasSplit; + $data->messageIndex = $messageIndex; + $data->orderIndex = $orderIndex; + $data->orderChannel = $orderChannel; + $data->splitCount = $splitCount; + $data->splitID = $splitID; + $data->splitIndex = $splitIndex; + $data->setBuffer($buffer); + } + return $data; + } + private function getInt($unsigned = false){ return Utils::readInt($this->get(4), $unsigned); } From e428f397361501a153a6b5ec3e3bc670c11dc1e1 Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Sat, 22 Mar 2025 14:27:02 +0800 Subject: [PATCH 09/18] Support MCPE 0.3.2 - 0.6.0 --- src/API/PlayerAPI.php | 1 - src/Player.php | 1053 ++++++++++++++++- src/network/protocol/ProtocolInfo.php | 77 +- .../packet/AdventureSettingsPacket.php | 4 +- src/network/protocol/packet/AnimatePacket.php | 4 +- src/network/protocol/packet/ChatPacket.php | 4 +- .../protocol/packet/ContainerClosePacket.php | 4 +- .../protocol/packet/ContainerOpenPacket.php | 4 +- .../packet/ContainerSetSlotPacket.php | 4 +- .../protocol/packet/DropItemPacket.php | 6 +- .../protocol/packet/EntityDataPacket.php | 4 +- .../protocol/packet/InteractPacket.php | 4 +- src/network/protocol/packet/MessagePacket.php | 8 +- .../protocol/packet/PlayerActionPacket.php | 4 +- src/network/protocol/packet/RespawnPacket.php | 4 +- .../protocol/packet/SendInventoryPacket.php | 4 +- .../protocol/packet/SetEntityDataPacket.php | 5 +- .../protocol/packet/SetEntityMotionPacket.php | 4 +- .../protocol/packet/SetHealthPacket.php | 4 +- .../packet/SetSpawnPositionPacket.php | 4 +- src/network/protocol/packet/UseItemPacket.php | 12 +- 21 files changed, 1079 insertions(+), 139 deletions(-) diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 982456a7..3be8b53d 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -333,7 +333,6 @@ class PlayerAPI{ public static function decodeProtocol($ip){ foreach(ServerAPI::request()->clients as $p) { if($p->ip == $ip){ - console($p->PROTOCOL); return $p->PROTOCOL; } } diff --git a/src/Player.php b/src/Player.php index 3385af26..7104479a 100755 --- a/src/Player.php +++ b/src/Player.php @@ -353,7 +353,6 @@ class Player{ if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY){ return; } - $packet->PROTOCOL = $this->PROTOCOL; $packet->encode(); $len = strlen($packet->buffer) + 1; @@ -1535,7 +1534,7 @@ class Player{ $this->close("server is full!", false); return; } - if ($packet->protocol1 < ProtocolInfo5::CURRENT_PROTOCOL_5 && $packet->protocol1 > ProtocolInfo::CURRENT_PROTOCOL) { + if ($packet->protocol1 < ProtocolInfo7::CURRENT_PROTOCOL_5 && $packet->protocol1 > ProtocolInfo::CURRENT_PROTOCOL) { if ($packet->protocol1 < ProtocolInfo::CURRENT_PROTOCOL) { $pk = new LoginStatusPacket; $pk->status = 1; @@ -1699,7 +1698,1001 @@ 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) . ")"); return; } - if ($this->PROTOCOL >= ProtocolInfo9::CURRENT_PROTOCOL_9 && $this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + if ($this->PROTOCOL >= ProtocolInfo7::CURRENT_PROTOCOL_5 && $this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9) { + switch ($packet->pid()) { + case 0x01: + break; + case ProtocolInfo7::PONG_PACKET: + break; + case ProtocolInfo7::PING_PACKET: + $pk = new PongPacket; + $pk->ptime = $packet->time; + $pk->time = abs(microtime(true) * 1000); + $this->directDataPacket($pk); + break; + case ProtocolInfo7::DISCONNECT_PACKET: + $this->close("client disconnect"); + break; + case ProtocolInfo7::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 ProtocolInfo7::CLIENT_HANDSHAKE_PACKET: + if ($this->loggedIn === true) { + break; + } + break; + case ProtocolInfo7::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 (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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 && count($this->hotbar) >= $this->slotCount) { + $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->sendInventorySlot($packet->slot); + $this->sendInventory(); + } + if ($this->entity->inAction === true) { + $this->entity->inAction = false; + $this->entity->inActionCounter = 0; + $this->entity->updateMetadata(); + } + break; + case ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::SET_HEALTH_PACKET: //Not used + break; + case ProtocolInfo7::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 ProtocolInfo7::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_drop7; + } + + $this->toCraft[] = $prevItem; //vanilla drops only result? + $this->lastCraft = microtime(true); + break; + } + } else { + inv_desync_on_drop7: + 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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::SEND_INVENTORY_PACKET: + if ($this->spawned === false) { + break; + } + break; + case ProtocolInfo7::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; + default: + 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()){ case 0x01: break; @@ -1980,22 +2973,25 @@ class Player{ if(($this->gamemode & 0x01) === SURVIVAL){ + $packet->slot = false; + foreach($this->inventory as $slot => $item) { + if ($item->getID() === $packet->item and $item->getMetadata() === $packet->meta) { + $packet->slot = $slot; + break; + } + } + if($packet->slot === false){ + break; + } $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; - } + $packet->slot = 0; + $item = BlockAPI::getItem($packet->item, $packet->meta); + $this->setSlot(0, $item); + $data["item"] = $item; }else{ break;//????? } @@ -2004,35 +3000,6 @@ class Player{ 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->sendInventorySlot($packet->slot); $this->sendInventory(); diff --git a/src/network/protocol/ProtocolInfo.php b/src/network/protocol/ProtocolInfo.php index 96e60981..58c4ac82 100644 --- a/src/network/protocol/ProtocolInfo.php +++ b/src/network/protocol/ProtocolInfo.php @@ -218,7 +218,9 @@ abstract class ProtocolInfo9{ const PLAYER_INPUT_PACKET = 0xb9; } -abstract class ProtocolInfo7{ +abstract class ProtocolInfo7{ // MCPE 0.3 - 0.5 seem using same packet id + + const CURRENT_PROTOCOL_5 = 5; const CURRENT_PROTOCOL_7 = 7; @@ -269,77 +271,8 @@ abstract class ProtocolInfo7{ //const HURT_ARMOR_PACKET = 0xa3; const SET_ENTITY_DATA_PACKET = 0xa3; const SET_ENTITY_MOTION_PACKET = 0xa4; - //const SET_ENTITY_LINK_PACKET = 0xa?;// Change - const SET_HEALTH_PACKET = 0xa5;// Change - const SET_SPAWN_POSITION_PACKET = 0xa6; - const ANIMATE_PACKET = 0xa7; - const RESPAWN_PACKET = 0xa8; - const SEND_INVENTORY_PACKET = 0xa9;//Maybe exist - const DROP_ITEM_PACKET = 0xaa; - const CONTAINER_OPEN_PACKET = 0xab; - const CONTAINER_CLOSE_PACKET = 0xac; - const CONTAINER_SET_SLOT_PACKET = 0xad; - //const CONTAINER_SET_DATA_PACKET = 0xb1; - //const CONTAINER_SET_CONTENT_PACKET = 0xb2; - //const CONTAINER_ACK_PACKET = 0xb3; - const CHAT_PACKET = 0xb1;//12 change - const ADVENTURE_SETTINGS_PACKET = 0xb3; - const ENTITY_DATA_PACKET = 0xb2; - //const PLAYER_INPUT_PACKET = 0xb9; -} -abstract class ProtocolInfo5{ - - const CURRENT_PROTOCOL_5 = 5; - - const PING_PACKET = 0x00; - - const PONG_PACKET = 0x03; - - const CLIENT_CONNECT_PACKET = 0x09; - const SERVER_HANDSHAKE_PACKET = 0x10; - - const CLIENT_HANDSHAKE_PACKET = 0x13; - //const SERVER_FULL_PACKET = 0x14; - const DISCONNECT_PACKET = 0x15; - const LOGIN_PACKET = 0x82; - const LOGIN_STATUS_PACKET = 0x83; - const READY_PACKET = 0x84; - const MESSAGE_PACKET = 0x85; - const SET_TIME_PACKET = 0x86; - const START_GAME_PACKET = 0x87; - const ADD_MOB_PACKET = 0x88; - const ADD_PLAYER_PACKET = 0x89; - const REMOVE_PLAYER_PACKET = 0x8a;//Maybe Exist - - const ADD_ENTITY_PACKET = 0x8c; - const REMOVE_ENTITY_PACKET = 0x8d; - const ADD_ITEM_ENTITY_PACKET = 0x8e; - const TAKE_ITEM_ENTITY_PACKET = 0x8f; - const MOVE_ENTITY_PACKET = 0x90; - - const MOVE_ENTITY_PACKET_POSROT = 0x93; - const MOVE_PLAYER_PACKET = 0x94; - //const PLACE_BLOCK_PACKET = 0x95; - const REMOVE_BLOCK_PACKET = 0x96; - const UPDATE_BLOCK_PACKET = 0x97; - const ADD_PAINTING_PACKET = 0x98;// Maybe exist - const EXPLODE_PACKET = 0x99; - const LEVEL_EVENT_PACKET = 0x9a; - const TILE_EVENT_PACKET = 0x9b;//Maybe exist - const ENTITY_EVENT_PACKET = 0x9c; - const REQUEST_CHUNK_PACKET = 0x9d; - const CHUNK_DATA_PACKET = 0x9e; - const PLAYER_EQUIPMENT_PACKET = 0x9f; - //const PLAYER_ARMOR_EQUIPMENT_PACKET = 0xa0; - const INTERACT_PACKET = 0xa0; - const USE_ITEM_PACKET = 0xa1; - const PLAYER_ACTION_PACKET = 0xa2; - - //const HURT_ARMOR_PACKET = 0xa3; - const SET_ENTITY_DATA_PACKET = 0xa3; - const SET_ENTITY_MOTION_PACKET = 0xa4; - //const SET_ENTITY_LINK_PACKET = 0xa?;// Change - const SET_HEALTH_PACKET = 0xa5;// Change + //const SET_ENTITY_LINK_PACKET = 0xa?; + const SET_HEALTH_PACKET = 0xa5; const SET_SPAWN_POSITION_PACKET = 0xa6; const ANIMATE_PACKET = 0xa7; const RESPAWN_PACKET = 0xa8; diff --git a/src/network/protocol/packet/AdventureSettingsPacket.php b/src/network/protocol/packet/AdventureSettingsPacket.php index e92ef1fc..7c2e8160 100644 --- a/src/network/protocol/packet/AdventureSettingsPacket.php +++ b/src/network/protocol/packet/AdventureSettingsPacket.php @@ -4,7 +4,9 @@ class AdventureSettingsPacket extends RakNetDataPacket{ public $flags; public function pid(){ - if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::ADVENTURE_SETTINGS_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::ADVENTURE_SETTINGS_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::ADVENTURE_SETTINGS_PACKET; diff --git a/src/network/protocol/packet/AnimatePacket.php b/src/network/protocol/packet/AnimatePacket.php index 34448421..160d4300 100644 --- a/src/network/protocol/packet/AnimatePacket.php +++ b/src/network/protocol/packet/AnimatePacket.php @@ -8,7 +8,9 @@ class AnimatePacket extends RakNetDataPacket{ public $eid; public function pid(){ - if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::ANIMATE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::ANIMATE_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::ANIMATE_PACKET; diff --git a/src/network/protocol/packet/ChatPacket.php b/src/network/protocol/packet/ChatPacket.php index 8d7df14f..d82158cd 100644 --- a/src/network/protocol/packet/ChatPacket.php +++ b/src/network/protocol/packet/ChatPacket.php @@ -4,7 +4,9 @@ class ChatPacket extends RakNetDataPacket{ public $message; public function pid(){ - if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::CHAT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::CHAT_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::CHAT_PACKET; diff --git a/src/network/protocol/packet/ContainerClosePacket.php b/src/network/protocol/packet/ContainerClosePacket.php index e7f4f564..7060e195 100644 --- a/src/network/protocol/packet/ContainerClosePacket.php +++ b/src/network/protocol/packet/ContainerClosePacket.php @@ -4,7 +4,9 @@ class ContainerClosePacket extends RakNetDataPacket{ public $windowid; public function pid(){ - if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::CONTAINER_CLOSE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::CONTAINER_CLOSE_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::CONTAINER_CLOSE_PACKET; diff --git a/src/network/protocol/packet/ContainerOpenPacket.php b/src/network/protocol/packet/ContainerOpenPacket.php index 23ad3739..694afa7e 100644 --- a/src/network/protocol/packet/ContainerOpenPacket.php +++ b/src/network/protocol/packet/ContainerOpenPacket.php @@ -9,7 +9,9 @@ class ContainerOpenPacket extends RakNetDataPacket{ public $z; public function pid(){ - if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::CONTAINER_OPEN_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::CONTAINER_OPEN_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::CONTAINER_OPEN_PACKET; diff --git a/src/network/protocol/packet/ContainerSetSlotPacket.php b/src/network/protocol/packet/ContainerSetSlotPacket.php index 0c6bbe41..2a012112 100644 --- a/src/network/protocol/packet/ContainerSetSlotPacket.php +++ b/src/network/protocol/packet/ContainerSetSlotPacket.php @@ -6,7 +6,9 @@ class ContainerSetSlotPacket extends RakNetDataPacket{ public $item; public function pid(){ - if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::CONTAINER_SET_SLOT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::CONTAINER_SET_SLOT_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::CONTAINER_SET_SLOT_PACKET; diff --git a/src/network/protocol/packet/DropItemPacket.php b/src/network/protocol/packet/DropItemPacket.php index 3fe52db3..d86110cd 100644 --- a/src/network/protocol/packet/DropItemPacket.php +++ b/src/network/protocol/packet/DropItemPacket.php @@ -6,9 +6,11 @@ class DropItemPacket extends RakNetDataPacket{ public $item; public function pid(){ - if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::DROP_ITEM_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::DROP_ITEM_PACKET; - }else if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::DROP_ITEM_PACKET; } return ProtocolInfo::DROP_ITEM_PACKET; diff --git a/src/network/protocol/packet/EntityDataPacket.php b/src/network/protocol/packet/EntityDataPacket.php index d7c6f8c1..efe76702 100644 --- a/src/network/protocol/packet/EntityDataPacket.php +++ b/src/network/protocol/packet/EntityDataPacket.php @@ -7,7 +7,9 @@ class EntityDataPacket extends RakNetDataPacket{ public $namedtag; public function pid(){ - if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::ENTITY_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::ENTITY_DATA_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::ENTITY_DATA_PACKET; diff --git a/src/network/protocol/packet/InteractPacket.php b/src/network/protocol/packet/InteractPacket.php index 760650ee..9d7e9dc8 100644 --- a/src/network/protocol/packet/InteractPacket.php +++ b/src/network/protocol/packet/InteractPacket.php @@ -9,7 +9,9 @@ class InteractPacket extends RakNetDataPacket{ public $target; public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::INTERACT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::INTERACT_PACKET; } return ProtocolInfo::INTERACT_PACKET; diff --git a/src/network/protocol/packet/MessagePacket.php b/src/network/protocol/packet/MessagePacket.php index dcb1b0bb..c766006c 100644 --- a/src/network/protocol/packet/MessagePacket.php +++ b/src/network/protocol/packet/MessagePacket.php @@ -9,13 +9,17 @@ class MessagePacket extends RakNetDataPacket{ } public function decode(){ - $this->source = $this->getString(); + if($this->PROTOCOL > ProtocolInfo9::CURRENT_PROTOCOL_9){ //0.6.1 and below From NostalgiaCore-BackPort Author:Gameherobrine + $this->source = $this->getString(); + } $this->message = $this->getString(); } public function encode(){ $this->reset(); - $this->putString($this->source); + if($this->PROTOCOL > ProtocolInfo9::CURRENT_PROTOCOL_9){ //0.6.1 and below From NostalgiaCore-BackPort Author:Gameherobrine + $this->putString($this->source); + } $this->putString($this->message); } diff --git a/src/network/protocol/packet/PlayerActionPacket.php b/src/network/protocol/packet/PlayerActionPacket.php index e47581e3..ed52db1b 100644 --- a/src/network/protocol/packet/PlayerActionPacket.php +++ b/src/network/protocol/packet/PlayerActionPacket.php @@ -9,7 +9,9 @@ class PlayerActionPacket extends RakNetDataPacket{ public $eid; public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::PLAYER_ACTION_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::PLAYER_ACTION_PACKET; } return ProtocolInfo::PLAYER_ACTION_PACKET; diff --git a/src/network/protocol/packet/RespawnPacket.php b/src/network/protocol/packet/RespawnPacket.php index 35adcf21..2619fc8c 100644 --- a/src/network/protocol/packet/RespawnPacket.php +++ b/src/network/protocol/packet/RespawnPacket.php @@ -7,7 +7,9 @@ class RespawnPacket extends RakNetDataPacket{ public $z; public function pid(){ - if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::RESPAWN_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::RESPAWN_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::RESPAWN_PACKET; diff --git a/src/network/protocol/packet/SendInventoryPacket.php b/src/network/protocol/packet/SendInventoryPacket.php index 2d82e0b1..8bfaef4a 100644 --- a/src/network/protocol/packet/SendInventoryPacket.php +++ b/src/network/protocol/packet/SendInventoryPacket.php @@ -7,7 +7,9 @@ class SendInventoryPacket extends RakNetDataPacket{ public $armor = array(); public function pid(){ - if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::SEND_INVENTORY_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::SEND_INVENTORY_PACKET; }else if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::SEND_INVENTORY_PACKET; diff --git a/src/network/protocol/packet/SetEntityDataPacket.php b/src/network/protocol/packet/SetEntityDataPacket.php index 9e9e4542..0a320b9b 100644 --- a/src/network/protocol/packet/SetEntityDataPacket.php +++ b/src/network/protocol/packet/SetEntityDataPacket.php @@ -5,7 +5,9 @@ class SetEntityDataPacket extends RakNetDataPacket{ public $metadata; public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::SET_ENTITY_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::SET_ENTITY_DATA_PACKET; } return ProtocolInfo::SET_ENTITY_DATA_PACKET; @@ -17,6 +19,7 @@ class SetEntityDataPacket extends RakNetDataPacket{ public function encode(){ $this->reset(); + if($this->PROTOCOL >= ProtocolInfo7::CURRENT_PROTOCOL_7) $this->putInt($this->eid); $this->put(Utils::writeMetadata($this->metadata)); } diff --git a/src/network/protocol/packet/SetEntityMotionPacket.php b/src/network/protocol/packet/SetEntityMotionPacket.php index b9ec84bd..6894927e 100644 --- a/src/network/protocol/packet/SetEntityMotionPacket.php +++ b/src/network/protocol/packet/SetEntityMotionPacket.php @@ -7,7 +7,9 @@ class SetEntityMotionPacket extends RakNetDataPacket{ public $speedZ; public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::SET_ENTITY_MOTION_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::SET_ENTITY_MOTION_PACKET; } return ProtocolInfo::SET_ENTITY_MOTION_PACKET; diff --git a/src/network/protocol/packet/SetHealthPacket.php b/src/network/protocol/packet/SetHealthPacket.php index 427f08d8..fbdbaaa1 100644 --- a/src/network/protocol/packet/SetHealthPacket.php +++ b/src/network/protocol/packet/SetHealthPacket.php @@ -4,7 +4,9 @@ class SetHealthPacket extends RakNetDataPacket{ public $health; public function pid(){ - if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::SET_HEALTH_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::SET_HEALTH_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::SET_HEALTH_PACKET; diff --git a/src/network/protocol/packet/SetSpawnPositionPacket.php b/src/network/protocol/packet/SetSpawnPositionPacket.php index dc8d757e..8712040e 100644 --- a/src/network/protocol/packet/SetSpawnPositionPacket.php +++ b/src/network/protocol/packet/SetSpawnPositionPacket.php @@ -6,7 +6,9 @@ class SetSpawnPositionPacket extends RakNetDataPacket{ public $y; public function pid(){ - if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::SET_SPAWN_POSITION_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::SET_SPAWN_POSITION_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::SET_SPAWN_POSITION_PACKET; diff --git a/src/network/protocol/packet/UseItemPacket.php b/src/network/protocol/packet/UseItemPacket.php index 57169c38..f3f3dd74 100644 --- a/src/network/protocol/packet/UseItemPacket.php +++ b/src/network/protocol/packet/UseItemPacket.php @@ -16,7 +16,9 @@ class UseItemPacket extends RakNetDataPacket{ public $posZ; public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::USE_ITEM_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::USE_ITEM_PACKET; } return ProtocolInfo::USE_ITEM_PACKET; @@ -33,9 +35,11 @@ class UseItemPacket extends RakNetDataPacket{ $this->fx = $this->getFloat(); $this->fy = $this->getFloat(); $this->fz = $this->getFloat(); - $this->posX = $this->getFloat(); - $this->posY = $this->getFloat(); - $this->posZ = $this->getFloat(); + if ($this->PROTOCOL > ProtocolInfo9::CURRENT_PROTOCOL_9) { + $this->posX = $this->getFloat(); + $this->posY = $this->getFloat(); + $this->posZ = $this->getFloat(); + } } public function encode(){ From a05848c99755bd6ab2757727245d8ea0408f930e Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Sun, 23 Mar 2025 15:31:14 +0800 Subject: [PATCH 10/18] Fuck Merge Conflict --- src/Player.php | 2385 +++++++++++++----------------------------------- 1 file changed, 638 insertions(+), 1747 deletions(-) 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 From e4538e166e2d9c732ac32a6edf11e6d01d098c5f Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Tue, 25 Mar 2025 20:22:20 +0800 Subject: [PATCH 11/18] Make Merge More Easy --- src/Player.php | 8228 ++++++++++++++++++++++++------------------------ 1 file changed, 4134 insertions(+), 4094 deletions(-) diff --git a/src/Player.php b/src/Player.php index 865282e1..983e61d4 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1227,7 +1227,15 @@ class Player{ } $p->PROTOCOL = $this->PROTOCOL; $p->decode(); - $this->handleDataPacket($p); + if ($this->PROTOCOL >= ProtocolInfo7::CURRENT_PROTOCOL_5 && $this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9) { //0.3 - 0.5 + $this->handleDataPacketFrom030($p); + } elseif ($this->PROTOCOL >= ProtocolInfo9::CURRENT_PROTOCOL_9 && $this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12) { // 0.6 + $this->handleDataPacketFrom060($p); + } elseif ($this->PROTOCOL >= ProtocolInfo12::CURRENT_PROTOCOL_12 && $this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL) { //0.7 + $this->handleDataPacketFrom076($p); + } else{ //0.8.1 + $this->handleDataPacket($p); + } } } } @@ -1711,4233 +1719,4265 @@ 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) . ")"); return; } - if ($this->PROTOCOL >= ProtocolInfo7::CURRENT_PROTOCOL_5 && $this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9) { - switch ($packet->pid()) { - case 0x01: + + 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; - case ProtocolInfo7::PONG_PACKET: - $currentTime = intdiv(hrtime(true), 1_000_000); - if ($currentTime > $packet->ptime) { - $this->lastPing = $currentTime - $packet->ptime; - } + } + $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; - case ProtocolInfo7::PING_PACKET: - $pk = new PongPacket; - $pk->ptime = $packet->time; - $pk->time = intdiv(hrtime(true), 1_000_000); - $this->directDataPacket($pk); + } + break; + case ProtocolInfo::LOGIN_PACKET: + if ($this->loggedIn === true) { break; - case ProtocolInfo7::DISCONNECT_PACKET: - $this->close("client disconnect"); - break; - case ProtocolInfo7::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 ProtocolInfo7::CLIENT_HANDSHAKE_PACKET: - if ($this->loggedIn === true) { - break; - } - break; - case ProtocolInfo7::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 (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; + } + $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)) { + $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 ($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"); - } + 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; - } + $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->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) { - $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]; + $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]; + } + } } - 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->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->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); + $this->armor = []; + foreach ($this->data->get("armor") as $slot => $item) { + $this->armor[$slot] = BlockAPI::getItem($item[0], $item[1], $item[0] === 0 ? 0 : 1); + } - $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); + $this->data->set("lastIP", $this->ip); + $this->data->set("lastID", $this->clientID); - $pk = new SetTimePacket; - $pk->time = $this->level->getTime(); - $pk->started = !$this->level->isTimeStopped(); + $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) . ")"); + 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; - case ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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)) { + } + switch ($packet->status) { + case 1: //Spawn!! + if ($this->spawned !== false) { 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; + $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); - if ($this->server->handle("player.equipment.change", $data) !== false) { - if (!Player::$experimentalHotbar) $this->slot = $packet->slot; - if (($this->gamemode & 0x01) === SURVIVAL && count($this->hotbar) >= $this->slotCount) { - $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(); + //$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"); } - } else { - //$this->sendInventorySlot($packet->slot); + $this->sendInventory(); - } - if ($this->entity->inAction === true) { - $this->entity->inAction = false; - $this->entity->inActionCounter = 0; - $this->entity->updateMetadata(); - } + $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; - case ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::SET_HEALTH_PACKET: //Not used - break; - case ProtocolInfo7::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 ProtocolInfo7::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_drop7; - } - - $this->toCraft[] = $prevItem; //vanilla drops only result? - $this->lastCraft = microtime(true); - 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 { - inv_desync_on_drop7: - 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(); + $this->entity->setPosition($this->entity, $packet->yaw, $this->entity->pitch); } + } + break; + case ProtocolInfo::MOVE_PLAYER_PACKET: + if ($this->spawned === false) { break; - case ProtocolInfo7::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); + } + 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 { - $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); - } - } + $this->teleport($this->forceMovement, $this->entity->yaw, $this->entity->pitch, false); + break; } } - break; - case ProtocolInfo7::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 ProtocolInfo7::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 = []; + $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->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); + $this->entity->setPosition($newPos, $packet->yaw, $packet->pitch, $packet->bodyYaw); } + $this->entity->updateAABB(); + } + break; + case ProtocolInfo::PLAYER_EQUIPMENT_PACKET: + if ($this->spawned === false) { break; - case ProtocolInfo7::SEND_INVENTORY_PACKET: - if ($this->spawned === false) { - break; - } - break; - case ProtocolInfo7::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; - default: - 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()) { - case 0x01: - break; - case ProtocolInfo9::PONG_PACKET: - $currentTime = intdiv(hrtime(true), 1_000_000); - if ($currentTime > $packet->ptime) { - $this->lastPing = $currentTime - $packet->ptime; - } - break; - case ProtocolInfo9::PING_PACKET: - $pk = new PongPacket; - $pk->ptime = $packet->time; - $pk->time = intdiv(hrtime(true), 1_000_000); - $this->directDataPacket($pk); - break; - case ProtocolInfo9::DISCONNECT_PACKET: - $this->close("client disconnect"); - break; - case ProtocolInfo9::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 ProtocolInfo9::CLIENT_HANDSHAKE_PACKET: - if ($this->loggedIn === true) { - break; - } - break; - case ProtocolInfo9::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 (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; - } + } + $packet->eid = $this->eid; - 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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::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) { - $packet->slot = false; - foreach ($this->inventory as $slot => $item) { - if ($item->getID() === $packet->item and $item->getMetadata() === $packet->meta) { - $packet->slot = $slot; - break; - } - } - if ($packet->slot === false) { - break; - } - $data["item"] = $this->getSlot($packet->slot); - if (!($data["item"] instanceof Item)) { - break; - } - } elseif (($this->gamemode & 0x01) === CREATIVE) { - $packet->slot = 0; - $item = BlockAPI::getItem($packet->item, $packet->meta); - $this->setSlot(0, $item); - $data["item"] = $item; - } else { - break;//????? - } - - $data["slot"] = $packet->slot; + $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) { - if (!Player::$experimentalHotbar) $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(); + $this->slot = -1; } break; - case ProtocolInfo9::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 ProtocolInfo9::USE_ITEM_PACKET: - if (!($this->entity instanceof Entity)) { + } 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; } - - $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(); - } + } 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; } } - break; - case ProtocolInfo9::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 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) { - $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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::SET_HEALTH_PACKET: //Not used - break; - case ProtocolInfo9::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 ProtocolInfo9::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_drop9; - } - - $this->toCraft[] = $prevItem; //vanilla drops only result? - $this->lastCraft = microtime(true); - break; - } - } else { - inv_desync_on_drop9: - 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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::SEND_INVENTORY_PACKET: - if ($this->spawned === false) { - break; - } - break; - case ProtocolInfo9::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 ProtocolInfo9::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; - } - } 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; - } - break; - case ProtocolInfo12::PING_PACKET: - $pk = new PongPacket; - $pk->ptime = $packet->time; - $pk->time = intdiv(hrtime(true), 1_000_000); - $this->directDataPacket($pk); - break; - case ProtocolInfo12::DISCONNECT_PACKET: - $this->close("client disconnect"); - break; - case ProtocolInfo12::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 ProtocolInfo12::CLIENT_HANDSHAKE_PACKET: - if ($this->loggedIn === true) { - break; - } - break; - case ProtocolInfo12::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 (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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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) { + if ($packet->slot !== false) { $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->sendInventorySlot($packet->slot); - $this->sendInventory(); - } - if ($this->entity->inAction === true) { - $this->entity->inAction = false; - $this->entity->inActionCounter = 0; - $this->entity->updateMetadata(); - } - break; - case ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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 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) { - $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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::SET_HEALTH_PACKET: //Not used - break; - case ProtocolInfo12::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 ProtocolInfo12::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_drop12; - } - - $this->toCraft[] = $prevItem; //vanilla drops only result? - $this->lastCraft = microtime(true); - break; - } - } else { - inv_desync_on_drop12: - 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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::SEND_INVENTORY_PACKET: - if ($this->spawned === false) { - break; - } - break; - case ProtocolInfo12::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 ProtocolInfo12::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; - } - } 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; - } - 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]; - $this->PROTOCOL = $packet->PROTOCOL; - 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; - } + } 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) { - $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; + $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 ($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 { + 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->sendInventorySlot($packet->slot); - $this->sendInventory(); + $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(); } - 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)) { + + 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); - $blockVector = new Vector3($packet->x, $packet->y, $packet->z); + $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); - 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 = $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) { - $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); + $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; - $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; + $d = sqrt($x * $x + $y * $y + $z * $z); - if ($packet->face >= 0 and $packet->face <= 5) { //Use Block, place - if ($this->entity->inAction === true) { - $this->entity->inAction = false; + 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(); } - - 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; - case ProtocolInfo::PLAYER_ACTION_PACKET: - if ($this->spawned === false or $this->blocked === true) { - break; - } - $packet->eid = $this->eid; - $this->craftingItems = []; - $this->toCraft = []; + } + $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, - ]); + 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(); + $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(); } + } 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->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; - 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); - } - } - + } + $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; - 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->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(); + $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 { - break; + $s = BlockAPI::getItem($s + 256, 0, 1); } - $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; - } + $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 { - inv_desync_on_drop: - ConsoleAPI::debug("Inventory desync on drop({$this->iusername})"); - $sendOnDrop = true; + $packet->slots[$i] = 255; } - $this->craftingItems = []; - $this->toCraft = []; - $data["eid"] = $packet->eid; - $data["unknown"] = $packet->unknown; - $data["item"] = $packet->item; + } + $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->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(); + if ($this->server->handle("player.interact", $data) !== false) { + $target->interactWith($this->entity, $packet->action); } + } + + break; + case ProtocolInfo::ANIMATE_PACKET: + if ($this->spawned === false) { break; - case ProtocolInfo::MESSAGE_PACKET: - if ($this->spawned === false) { + } + $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; } - $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 { + 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 { - $data = ["player" => $this, "message" => $message]; - if (Utils::hasEmoji($data["message"])) { - $this->sendChat("Your message contains illegal characters"); - break; - } + 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($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); - } + 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; - 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) { + } + $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 = $this->windows[$packet->windowid]->x; - $pk->y = $this->windows[$packet->windowid]->y; - $pk->z = $this->windows[$packet->windowid]->z; + $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]); + } + unset($this->windows[$packet->windowid]); - $pk = new ContainerClosePacket; - $pk->windowid = $packet->windowid; - $this->dataPacket($pk); + $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; - 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 = []; - } + if ($this->lastCraft <= (microtime(true) - 1)) { + if (isset($this->toCraft[-1])) { + $this->toCraft = [-1 => $this->toCraft[-1]]; } 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); + } + + 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 { - $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"]); + $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; } } - 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) { + 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; } - 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); } - /*$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);*/ } + 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; - } + break; + default: + console("[DEBUG] Unhandled 0x" . dechex($packet->pid()) . " data packet for " . $this->username . " (" . $this->clientID . "): " . print_r($packet, true), true, true, 2); + break; } } - - /** + + public function handleDataPacketFrom030(RakNetDataPacket $packet) + { + if ($this->connected === false) { + return; + } + + if (EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY) { + return; + } + + switch ($packet->pid()) { + case 0x01: + break; + case ProtocolInfo7::PONG_PACKET: + $currentTime = intdiv(hrtime(true), 1_000_000); + if ($currentTime > $packet->ptime) { + $this->lastPing = $currentTime - $packet->ptime; + } + break; + case ProtocolInfo7::PING_PACKET: + $pk = new PongPacket; + $pk->ptime = $packet->time; + $pk->time = intdiv(hrtime(true), 1_000_000); + $this->directDataPacket($pk); + break; + case ProtocolInfo7::DISCONNECT_PACKET: + $this->close("client disconnect"); + break; + case ProtocolInfo7::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 ProtocolInfo7::CLIENT_HANDSHAKE_PACKET: + if ($this->loggedIn === true) { + break; + } + break; + case ProtocolInfo7::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 (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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 && count($this->hotbar) >= $this->slotCount) { + $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->sendInventorySlot($packet->slot); + $this->sendInventory(); + } + if ($this->entity->inAction === true) { + $this->entity->inAction = false; + $this->entity->inActionCounter = 0; + $this->entity->updateMetadata(); + } + break; + case ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::SET_HEALTH_PACKET: //Not used + break; + case ProtocolInfo7::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 ProtocolInfo7::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_drop7; + } + + $this->toCraft[] = $prevItem; //vanilla drops only result? + $this->lastCraft = microtime(true); + break; + } + } else { + inv_desync_on_drop7: + 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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::SEND_INVENTORY_PACKET: + if ($this->spawned === false) { + break; + } + break; + case ProtocolInfo7::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; + default: + console("[DEBUG] Unhandled 0x" . dechex($packet->pid()) . " data packet for " . $this->username . " (" . $this->clientID . "): " . print_r($packet, true), true, true, 2); + break; + } + } + + public function handleDataPacketFrom060(RakNetDataPacket $packet) + { + if ($this->connected === false) { + return; + } + + if (EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY) { + return; + } + + 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; + } + break; + case ProtocolInfo9::PING_PACKET: + $pk = new PongPacket; + $pk->ptime = $packet->time; + $pk->time = intdiv(hrtime(true), 1_000_000); + $this->directDataPacket($pk); + break; + case ProtocolInfo9::DISCONNECT_PACKET: + $this->close("client disconnect"); + break; + case ProtocolInfo9::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 ProtocolInfo9::CLIENT_HANDSHAKE_PACKET: + if ($this->loggedIn === true) { + break; + } + break; + case ProtocolInfo9::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 (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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::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) { + $packet->slot = false; + foreach ($this->inventory as $slot => $item) { + if ($item->getID() === $packet->item and $item->getMetadata() === $packet->meta) { + $packet->slot = $slot; + break; + } + } + if ($packet->slot === false) { + break; + } + $data["item"] = $this->getSlot($packet->slot); + if (!($data["item"] instanceof Item)) { + break; + } + } elseif (($this->gamemode & 0x01) === CREATIVE) { + $packet->slot = 0; + $item = BlockAPI::getItem($packet->item, $packet->meta); + $this->setSlot(0, $item); + $data["item"] = $item; + } else { + break;//????? + } + + $data["slot"] = $packet->slot; + + 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) { + $this->entity->inAction = false; + $this->entity->inActionCounter = 0; + $this->entity->updateMetadata(); + } + break; + case ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::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 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) { + $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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::SET_HEALTH_PACKET: //Not used + break; + case ProtocolInfo9::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 ProtocolInfo9::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_drop9; + } + + $this->toCraft[] = $prevItem; //vanilla drops only result? + $this->lastCraft = microtime(true); + break; + } + } else { + inv_desync_on_drop9: + 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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::SEND_INVENTORY_PACKET: + if ($this->spawned === false) { + break; + } + break; + case ProtocolInfo9::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 ProtocolInfo9::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; + } + } + + public function handleDataPacketFrom076(RakNetDataPacket $packet) + { + if ($this->connected === false) { + return; + } + + if (EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY) { + return; + } + + 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; + } + break; + case ProtocolInfo12::PING_PACKET: + $pk = new PongPacket; + $pk->ptime = $packet->time; + $pk->time = intdiv(hrtime(true), 1_000_000); + $this->directDataPacket($pk); + break; + case ProtocolInfo12::DISCONNECT_PACKET: + $this->close("client disconnect"); + break; + case ProtocolInfo12::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 ProtocolInfo12::CLIENT_HANDSHAKE_PACKET: + if ($this->loggedIn === true) { + break; + } + break; + case ProtocolInfo12::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 (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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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->sendInventorySlot($packet->slot); + $this->sendInventory(); + } + if ($this->entity->inAction === true) { + $this->entity->inAction = false; + $this->entity->inActionCounter = 0; + $this->entity->updateMetadata(); + } + break; + case ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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 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) { + $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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::SET_HEALTH_PACKET: //Not used + break; + case ProtocolInfo12::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 ProtocolInfo12::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_drop12; + } + + $this->toCraft[] = $prevItem; //vanilla drops only result? + $this->lastCraft = microtime(true); + break; + } + } else { + inv_desync_on_drop12: + 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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::SEND_INVENTORY_PACKET: + if ($this->spawned === false) { + break; + } + break; + case ProtocolInfo12::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 ProtocolInfo12::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 * @return Item */ From 490516fa0432355d748390265d1c11119ae88ba5 Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Tue, 8 Apr 2025 18:00:05 +0800 Subject: [PATCH 12/18] Thanks to @ArkQuark --- src/Player.php | 3 +- src/network/protocol/ProtocolInfo.php | 36 +++++++++---------- .../protocol/packet/PlayerEquipmentPacket.php | 4 +-- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/Player.php b/src/Player.php index 983e61d4..1716f0b4 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2829,7 +2829,6 @@ class Player{ if (EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY) { return; } - switch ($packet->pid()) { case 0x01: break; @@ -3235,6 +3234,7 @@ class Player{ $this->entity->inActionCounter = 0; $this->entity->updateMetadata(); } + if ($this->blocked === true or ($this->entity->position instanceof Vector3 and $blockVector->distance($this->entity->position) > 10)) { @@ -3797,6 +3797,7 @@ class Player{ } break; case ProtocolInfo7::SEND_INVENTORY_PACKET: + console("send"); if ($this->spawned === false) { break; } diff --git a/src/network/protocol/ProtocolInfo.php b/src/network/protocol/ProtocolInfo.php index 58c4ac82..e1779167 100644 --- a/src/network/protocol/ProtocolInfo.php +++ b/src/network/protocol/ProtocolInfo.php @@ -34,7 +34,7 @@ abstract class ProtocolInfo{ const MOVE_ENTITY_PACKET_POSROT = 0x93; const ROTATE_HEAD_PACKET = 0x94; const MOVE_PLAYER_PACKET = 0x95; - //const PLACE_BLOCK_PACKET = 0x96; + const PLACE_BLOCK_PACKET = 0x96; const REMOVE_BLOCK_PACKET = 0x97; const UPDATE_BLOCK_PACKET = 0x98; const ADD_PAINTING_PACKET = 0x99; @@ -65,7 +65,7 @@ abstract class ProtocolInfo{ const CONTAINER_SET_SLOT_PACKET = 0xb2; const CONTAINER_SET_DATA_PACKET = 0xb3; const CONTAINER_SET_CONTENT_PACKET = 0xb4; - //const CONTAINER_ACK_PACKET = 0xb5; + const CONTAINER_ACK_PACKET = 0xb5; const CHAT_PACKET = 0xb6; const ADVENTURE_SETTINGS_PACKET = 0xb7; const ENTITY_DATA_PACKET = 0xb8; @@ -111,7 +111,7 @@ abstract class ProtocolInfo12{ const MOVE_ENTITY_PACKET_POSROT = 0x93; const ROTATE_HEAD_PACKET = 0xff; const MOVE_PLAYER_PACKET = 0x94; - //const PLACE_BLOCK_PACKET = 0x96; + const PLACE_BLOCK_PACKET = 0x96; const REMOVE_BLOCK_PACKET = 0x96; const UPDATE_BLOCK_PACKET = 0x97; const ADD_PAINTING_PACKET = 0x98; @@ -142,7 +142,7 @@ abstract class ProtocolInfo12{ const CONTAINER_SET_SLOT_PACKET = 0xb1; const CONTAINER_SET_DATA_PACKET = 0xb2; const CONTAINER_SET_CONTENT_PACKET = 0xb3; - //const CONTAINER_ACK_PACKET = 0xb5; //Bruh + const CONTAINER_ACK_PACKET = 0xb5; //Bruh const CHAT_PACKET = 0xb5; const ADVENTURE_SETTINGS_PACKET = 0xb6; const ENTITY_DATA_PACKET = 0xb7; @@ -180,7 +180,7 @@ abstract class ProtocolInfo9{ const MOVE_ENTITY_PACKET_POSROT = 0x93; const MOVE_PLAYER_PACKET = 0x94; - //const PLACE_BLOCK_PACKET = 0x95; + const PLACE_BLOCK_PACKET = 0x95; const REMOVE_BLOCK_PACKET = 0x96; const UPDATE_BLOCK_PACKET = 0x97; const ADD_PAINTING_PACKET = 0x98; @@ -206,12 +206,12 @@ abstract class ProtocolInfo9{ const RESPAWN_PACKET = 0xab; const SEND_INVENTORY_PACKET = 0xac; const DROP_ITEM_PACKET = 0xad; - const CONTAINER_OPEN_PACKET = 0xae; - const CONTAINER_CLOSE_PACKET = 0xaf; - const CONTAINER_SET_SLOT_PACKET = 0xb0; - const CONTAINER_SET_DATA_PACKET = 0xb1; - const CONTAINER_SET_CONTENT_PACKET = 0xb2; - //const CONTAINER_ACK_PACKET = 0xb3; + const CONTAINER_OPEN_PACKET = 0xab; + const CONTAINER_CLOSE_PACKET = 0xac; + const CONTAINER_SET_SLOT_PACKET = 0xad; + const CONTAINER_SET_DATA_PACKET = 0xae; + const CONTAINER_SET_CONTENT_PACKET = 0xaf; + const CONTAINER_ACK_PACKET = 0xb; const CHAT_PACKET = 0xb4;//12 change const ADVENTURE_SETTINGS_PACKET = 0xb6; const ENTITY_DATA_PACKET = 0xb7; @@ -252,7 +252,7 @@ abstract class ProtocolInfo7{ // MCPE 0.3 - 0.5 seem using same packet id const MOVE_ENTITY_PACKET_POSROT = 0x93; const MOVE_PLAYER_PACKET = 0x94; - //const PLACE_BLOCK_PACKET = 0x95; + const PLACE_BLOCK_PACKET = 0x95; const REMOVE_BLOCK_PACKET = 0x96; const UPDATE_BLOCK_PACKET = 0x97; const ADD_PAINTING_PACKET = 0x98;// Maybe exist @@ -263,12 +263,12 @@ abstract class ProtocolInfo7{ // MCPE 0.3 - 0.5 seem using same packet id const REQUEST_CHUNK_PACKET = 0x9d; const CHUNK_DATA_PACKET = 0x9e; const PLAYER_EQUIPMENT_PACKET = 0x9f; - //const PLAYER_ARMOR_EQUIPMENT_PACKET = 0xa0; + const PLAYER_ARMOR_EQUIPMENT_PACKET = 0xa0; const INTERACT_PACKET = 0xa0;//Change const USE_ITEM_PACKET = 0xa1; const PLAYER_ACTION_PACKET = 0xa2; - //const HURT_ARMOR_PACKET = 0xa3; + const HURT_ARMOR_PACKET = 0xa3; const SET_ENTITY_DATA_PACKET = 0xa3; const SET_ENTITY_MOTION_PACKET = 0xa4; //const SET_ENTITY_LINK_PACKET = 0xa?; @@ -281,13 +281,13 @@ abstract class ProtocolInfo7{ // MCPE 0.3 - 0.5 seem using same packet id const CONTAINER_OPEN_PACKET = 0xab; const CONTAINER_CLOSE_PACKET = 0xac; const CONTAINER_SET_SLOT_PACKET = 0xad; - //const CONTAINER_SET_DATA_PACKET = 0xb1; - //const CONTAINER_SET_CONTENT_PACKET = 0xb2; - //const CONTAINER_ACK_PACKET = 0xb3; + const CONTAINER_SET_DATA_PACKET = 0xae; + const CONTAINER_SET_CONTENT_PACKET = 0xaf; + const CONTAINER_ACK_PACKET = 0xb; const CHAT_PACKET = 0xb1;//12 change const ADVENTURE_SETTINGS_PACKET = 0xb3; const ENTITY_DATA_PACKET = 0xb2; - //const PLAYER_INPUT_PACKET = 0xb9; + const PLAYER_INPUT_PACKET = 0xb9; } /***REM_START***/ require_once(FILE_PATH . "src/network/raknet/RakNetDataPacket.php"); diff --git a/src/network/protocol/packet/PlayerEquipmentPacket.php b/src/network/protocol/packet/PlayerEquipmentPacket.php index f6f4eecf..2bcb648f 100644 --- a/src/network/protocol/packet/PlayerEquipmentPacket.php +++ b/src/network/protocol/packet/PlayerEquipmentPacket.php @@ -17,7 +17,7 @@ class PlayerEquipmentPacket extends RakNetDataPacket{ $this->eid = $this->getInt(); $this->item = $this->getShort(); $this->meta = $this->getShort(); - $this->slot = $this->getSignedByte(); + if($this->PROTOCOL === ProtocolInfo::CURRENT_PROTOCOL){$this->slot = $this->getSignedByte();} } public function encode(){ @@ -25,7 +25,7 @@ class PlayerEquipmentPacket extends RakNetDataPacket{ $this->putInt($this->eid); $this->putShort($this->item); $this->putShort($this->meta); - $this->putByte($this->slot); + if($this->PROTOCOL === ProtocolInfo::CURRENT_PROTOCOL){$this->putByte($this->slot);} } } \ No newline at end of file From 7c8f32d100967b1edbed17a7775a2e182bd7a801 Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Sat, 12 Apr 2025 18:54:16 +0800 Subject: [PATCH 13/18] Thanks to @Sunch233 --- src/Player.php | 3172 +---------------------- src/network/raknet/RakNetDataPacket.php | 10 +- 2 files changed, 11 insertions(+), 3171 deletions(-) diff --git a/src/Player.php b/src/Player.php index d48d7398..bc867672 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1227,15 +1227,7 @@ class Player{ } $p->PROTOCOL = $this->PROTOCOL; $p->decode(); - if ($this->PROTOCOL >= ProtocolInfo7::CURRENT_PROTOCOL_5 && $this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9) { //0.3 - 0.5 - $this->handleDataPacketFrom030($p); - } elseif ($this->PROTOCOL >= ProtocolInfo9::CURRENT_PROTOCOL_9 && $this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12) { // 0.6 - $this->handleDataPacketFrom060($p); - } elseif ($this->PROTOCOL >= ProtocolInfo12::CURRENT_PROTOCOL_12 && $this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL) { //0.7 - $this->handleDataPacketFrom076($p); - } else{ //0.8.1 - $this->handleDataPacket($p); - } + $this->handleDataPacket($p); } } } @@ -1523,7 +1515,6 @@ class Player{ } } - public function sendPing() { $pk = new PingPacket; $pk->time = intdiv(hrtime(true), 1_000_000); @@ -2821,3165 +2812,6 @@ class Player{ } } - public function handleDataPacketFrom030(RakNetDataPacket $packet) - { - if ($this->connected === false) { - return; - } - - if (EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY) { - return; - } - switch ($packet->pid()) { - case 0x01: - break; - case ProtocolInfo7::PONG_PACKET: - $currentTime = intdiv(hrtime(true), 1_000_000); - if ($currentTime > $packet->ptime) { - $this->lastPing = $currentTime - $packet->ptime; - } - break; - case ProtocolInfo7::PING_PACKET: - $pk = new PongPacket; - $pk->ptime = $packet->time; - $pk->time = intdiv(hrtime(true), 1_000_000); - $this->directDataPacket($pk); - break; - case ProtocolInfo7::DISCONNECT_PACKET: - $this->close("client disconnect"); - break; - case ProtocolInfo7::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 ProtocolInfo7::CLIENT_HANDSHAKE_PACKET: - if ($this->loggedIn === true) { - break; - } - break; - case ProtocolInfo7::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 (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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 && count($this->hotbar) >= $this->slotCount) { - $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->sendInventorySlot($packet->slot); - $this->sendInventory(); - } - if ($this->entity->inAction === true) { - $this->entity->inAction = false; - $this->entity->inActionCounter = 0; - $this->entity->updateMetadata(); - } - break; - case ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::SET_HEALTH_PACKET: //Not used - break; - case ProtocolInfo7::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 ProtocolInfo7::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_drop7; - } - - $this->toCraft[] = $prevItem; //vanilla drops only result? - $this->lastCraft = microtime(true); - break; - } - } else { - inv_desync_on_drop7: - 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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::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 ProtocolInfo7::SEND_INVENTORY_PACKET: - console("send"); - if ($this->spawned === false) { - break; - } - break; - case ProtocolInfo7::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; - default: - console("[DEBUG] Unhandled 0x" . dechex($packet->pid()) . " data packet for " . $this->username . " (" . $this->clientID . "): " . print_r($packet, true), true, true, 2); - break; - } - } - - public function handleDataPacketFrom060(RakNetDataPacket $packet) - { - if ($this->connected === false) { - return; - } - - if (EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY) { - return; - } - - 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; - } - break; - case ProtocolInfo9::PING_PACKET: - $pk = new PongPacket; - $pk->ptime = $packet->time; - $pk->time = intdiv(hrtime(true), 1_000_000); - $this->directDataPacket($pk); - break; - case ProtocolInfo9::DISCONNECT_PACKET: - $this->close("client disconnect"); - break; - case ProtocolInfo9::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 ProtocolInfo9::CLIENT_HANDSHAKE_PACKET: - if ($this->loggedIn === true) { - break; - } - break; - case ProtocolInfo9::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 (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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::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) { - $packet->slot = false; - foreach ($this->inventory as $slot => $item) { - if ($item->getID() === $packet->item and $item->getMetadata() === $packet->meta) { - $packet->slot = $slot; - break; - } - } - if ($packet->slot === false) { - break; - } - $data["item"] = $this->getSlot($packet->slot); - if (!($data["item"] instanceof Item)) { - break; - } - } elseif (($this->gamemode & 0x01) === CREATIVE) { - $packet->slot = 0; - $item = BlockAPI::getItem($packet->item, $packet->meta); - $this->setSlot(0, $item); - $data["item"] = $item; - } else { - break;//????? - } - - $data["slot"] = $packet->slot; - - 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) { - $this->entity->inAction = false; - $this->entity->inActionCounter = 0; - $this->entity->updateMetadata(); - } - break; - case ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::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 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) { - $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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::SET_HEALTH_PACKET: //Not used - break; - case ProtocolInfo9::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 ProtocolInfo9::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_drop9; - } - - $this->toCraft[] = $prevItem; //vanilla drops only result? - $this->lastCraft = microtime(true); - break; - } - } else { - inv_desync_on_drop9: - 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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::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 ProtocolInfo9::SEND_INVENTORY_PACKET: - if ($this->spawned === false) { - break; - } - break; - case ProtocolInfo9::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 ProtocolInfo9::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; - } - } - - public function handleDataPacketFrom076(RakNetDataPacket $packet) - { - if ($this->connected === false) { - return; - } - - if (EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY) { - return; - } - - 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; - } - break; - case ProtocolInfo12::PING_PACKET: - $pk = new PongPacket; - $pk->ptime = $packet->time; - $pk->time = intdiv(hrtime(true), 1_000_000); - $this->directDataPacket($pk); - break; - case ProtocolInfo12::DISCONNECT_PACKET: - $this->close("client disconnect"); - break; - case ProtocolInfo12::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 ProtocolInfo12::CLIENT_HANDSHAKE_PACKET: - if ($this->loggedIn === true) { - break; - } - break; - case ProtocolInfo12::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 (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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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->sendInventorySlot($packet->slot); - $this->sendInventory(); - } - if ($this->entity->inAction === true) { - $this->entity->inAction = false; - $this->entity->inActionCounter = 0; - $this->entity->updateMetadata(); - } - break; - case ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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 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) { - $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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::SET_HEALTH_PACKET: //Not used - break; - case ProtocolInfo12::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 ProtocolInfo12::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_drop12; - } - - $this->toCraft[] = $prevItem; //vanilla drops only result? - $this->lastCraft = microtime(true); - break; - } - } else { - inv_desync_on_drop12: - 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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::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 ProtocolInfo12::SEND_INVENTORY_PACKET: - if ($this->spawned === false) { - break; - } - break; - case ProtocolInfo12::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 ProtocolInfo12::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 * @return Item @@ -6091,7 +2923,7 @@ class Player{ $s = $this->getSlot($slot); if($s->count <= 0 or $s->getID() === AIR){ $this->setSlot($slot, BlockAPI::getItem($item->getID(), $item->getMetadata(), $item->count), false); - }else if($s->getID() == $item->getID() && $s->getMetadata() == $item->getMetadata() && ($s->count + $item->count) <= $s->getMaxStackSize()){ + }else if($s->getID() == $item->getID() && $s->getMetadata() == $item->getMetadata() && ($s->count + $item->count) <= $s->maxStackSize){ $this->setSlot($slot, BlockAPI::getItem($item->getID(), $item->getMetadata(), $s->count + $item->count), false); }else{ $f1 = 0.3; diff --git a/src/network/raknet/RakNetDataPacket.php b/src/network/raknet/RakNetDataPacket.php index bfcf982b..9c23db73 100755 --- a/src/network/raknet/RakNetDataPacket.php +++ b/src/network/raknet/RakNetDataPacket.php @@ -14,7 +14,7 @@ abstract class RakNetDataPacket extends stdClass{ private $offset = 0; - public $PROTOCOL = 14; + public $PROTOCOL = ProtocolInfo::CURRENT_PROTOCOL; abstract public function encode(); @@ -39,6 +39,14 @@ abstract class RakNetDataPacket extends stdClass{ abstract public function pid(); + public function getInternalPid() : int{ + $rawProtocol = $this->PROTOCOL; + $this->PROTOCOL = ProtocolInfo::CURRENT_PROTOCOL; + $pid = (int) $this->pid(); + $this->PROTOCOL = $rawProtocol; + return $pid; + } + protected function getLong($unsigned = false){ return Utils::readLong($this->get(8), $unsigned); } From 2712adc29f3c7419b1933743a1aaa60d1308028a Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Sat, 12 Apr 2025 19:13:36 +0800 Subject: [PATCH 14/18] Fix auto respawn bug in MCPE 0.3.2 - 0.5.0 --- src/Player.php | 4 +- src/network/protocol/ProtocolInfo.php | 2 - .../protocol/packet/ContainerOpenPacket.php | 2 +- .../packet/ContainerSetContentPacket.php | 2 +- src/network/protocol/packet/LoginPacket.php | 2 +- .../protocol/packet/PlaceBlockPacket.php | 54 +++++++++++++++++++ src/network/protocol/packet/SetTimePacket.php | 2 +- .../protocol/packet/StartGamePacket.php | 2 +- src/network/raknet/RakNetParser.php | 19 +++---- 9 files changed, 70 insertions(+), 19 deletions(-) create mode 100644 src/network/protocol/packet/PlaceBlockPacket.php diff --git a/src/Player.php b/src/Player.php index bc867672..2aebfbd3 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1712,7 +1712,9 @@ class Player{ return; } - switch ($packet->pid()) { + $internalPid = $packet->getInternalPid(); + + switch ($internalPid) { case 0x01: break; case ProtocolInfo::PONG_PACKET: diff --git a/src/network/protocol/ProtocolInfo.php b/src/network/protocol/ProtocolInfo.php index e1779167..b29ad3ef 100644 --- a/src/network/protocol/ProtocolInfo.php +++ b/src/network/protocol/ProtocolInfo.php @@ -267,8 +267,6 @@ abstract class ProtocolInfo7{ // MCPE 0.3 - 0.5 seem using same packet id const INTERACT_PACKET = 0xa0;//Change const USE_ITEM_PACKET = 0xa1; const PLAYER_ACTION_PACKET = 0xa2; - - const HURT_ARMOR_PACKET = 0xa3; const SET_ENTITY_DATA_PACKET = 0xa3; const SET_ENTITY_MOTION_PACKET = 0xa4; //const SET_ENTITY_LINK_PACKET = 0xa?; diff --git a/src/network/protocol/packet/ContainerOpenPacket.php b/src/network/protocol/packet/ContainerOpenPacket.php index 694afa7e..13704ed3 100644 --- a/src/network/protocol/packet/ContainerOpenPacket.php +++ b/src/network/protocol/packet/ContainerOpenPacket.php @@ -28,7 +28,7 @@ class ContainerOpenPacket extends RakNetDataPacket{ $this->putByte($this->windowid); $this->putByte($this->type); $this->putByte($this->slots); - $this->putInt($this->x); + $this->putInt($this->x);//String $this->putInt($this->y); $this->putInt($this->z); } diff --git a/src/network/protocol/packet/ContainerSetContentPacket.php b/src/network/protocol/packet/ContainerSetContentPacket.php index 96a53428..6378c386 100644 --- a/src/network/protocol/packet/ContainerSetContentPacket.php +++ b/src/network/protocol/packet/ContainerSetContentPacket.php @@ -36,7 +36,7 @@ class ContainerSetContentPacket extends RakNetDataPacket{ $this->putSlot($slot); } if($this->windowid === 0 and count($this->hotbar) > 0){ - $this->putShort(count($this->hotbar)); + $this->putShort(count($this->hotbar));//null foreach($this->hotbar as $slot){ $this->putInt($slot); } diff --git a/src/network/protocol/packet/LoginPacket.php b/src/network/protocol/packet/LoginPacket.php index e87ea91d..54135a21 100644 --- a/src/network/protocol/packet/LoginPacket.php +++ b/src/network/protocol/packet/LoginPacket.php @@ -15,7 +15,7 @@ class LoginPacket extends RakNetDataPacket{ $this->username = $this->getString(); $this->protocol1 = $this->getInt(); $this->protocol2 = $this->getInt(); - $this->clientId = $this->getInt(); + $this->clientId = $this->getInt();//null $this->loginData = $this->getString(); $this->PROTOCOL = $this->protocol1; } diff --git a/src/network/protocol/packet/PlaceBlockPacket.php b/src/network/protocol/packet/PlaceBlockPacket.php new file mode 100644 index 00000000..9171f04a --- /dev/null +++ b/src/network/protocol/packet/PlaceBlockPacket.php @@ -0,0 +1,54 @@ +PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9) { + return ProtocolInfo7::PLACE_BLOCK_PACKET; + } + return ProtocolInfo::PLACE_BLOCK_PACKET; + } + + public function decode(){ + $this->unknown1 = $this->getInt(); + $this->unknown2 = $this->getInt(); + $this->unknown3 = $this->getInt(); + $this->unknown4 = $this->getByte(); + $this->unknown5 = $this->getByte(); + $this->unknown6 = $this->getByte(); + $this->unknown7 = $this->getByte(); + ConsoleAPI::info("Int1:".$this->unknown1); + ConsoleAPI::info("Int2:".$this->unknown2); + ConsoleAPI::info("Int3:".$this->unknown3); + ConsoleAPI::info("Byte4:".$this->unknown4); + ConsoleAPI::info("Byte5:".$this->unknown5); + ConsoleAPI::info("Byte6:".$this->unknown6); + ConsoleAPI::info("Byte7:".$this->unknown7); + } + + public function encode(){ + $this->reset(); + $this->putInt($this->unknown1); + $this->putInt($this->unknown2); + $this->putInt($this->unknown3); + $this->putByte($this->unknown4); + $this->putByte($this->unknown5); + $this->putByte($this->unknown6); + $this->putByte($this->unknown7); + ConsoleAPI::info("Int1:".$this->unknown1); + ConsoleAPI::info("Int2:".$this->unknown2); + ConsoleAPI::info("Int3:".$this->unknown3); + ConsoleAPI::info("Byte4:".$this->unknown4); + ConsoleAPI::info("Byte5:".$this->unknown5); + ConsoleAPI::info("Byte6:".$this->unknown6); + ConsoleAPI::info("Byte7:".$this->unknown7); + } + +} \ No newline at end of file diff --git a/src/network/protocol/packet/SetTimePacket.php b/src/network/protocol/packet/SetTimePacket.php index 302afd9e..f50af807 100644 --- a/src/network/protocol/packet/SetTimePacket.php +++ b/src/network/protocol/packet/SetTimePacket.php @@ -14,7 +14,7 @@ class SetTimePacket extends RakNetDataPacket{ public function encode(){ $this->reset(); - $this->putInt($this->time); + $this->putInt($this->time);//long $this->putByte($this->started ? 0x80:0x00); } diff --git a/src/network/protocol/packet/StartGamePacket.php b/src/network/protocol/packet/StartGamePacket.php index a746ea3a..bcbd12bb 100644 --- a/src/network/protocol/packet/StartGamePacket.php +++ b/src/network/protocol/packet/StartGamePacket.php @@ -19,7 +19,7 @@ class StartGamePacket extends RakNetDataPacket{ public function encode(){ $this->reset(); - $this->putInt($this->seed); + $this->putInt($this->seed);//long $this->putInt($this->generator); $this->putInt($this->gamemode); $this->putInt($this->eid); diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php index b193a36b..be0807a9 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -965,9 +965,9 @@ class RakNetParser{ case ProtocolInfo7::PLAYER_EQUIPMENT_PACKET: $data = new PlayerEquipmentPacket; break; - /*case ProtocolInfo7::PLAYER_ARMOR_EQUIPMENT_PACKET: + case ProtocolInfo7::PLAYER_ARMOR_EQUIPMENT_PACKET: $data = new PlayerArmorEquipmentPacket; - break;*/ + break; case ProtocolInfo7::INTERACT_PACKET: $data = new InteractPacket; break; @@ -977,9 +977,6 @@ class RakNetParser{ case ProtocolInfo7::PLAYER_ACTION_PACKET: $data = new PlayerActionPacket; break; - /*case ProtocolInfo7::HURT_ARMOR_PACKET: - $data = new HurtArmorPacket; - break;*/ case ProtocolInfo7::SET_ENTITY_DATA_PACKET: $data = new SetEntityDataPacket; break; @@ -1013,12 +1010,12 @@ class RakNetParser{ case ProtocolInfo7::CONTAINER_SET_SLOT_PACKET: $data = new ContainerSetSlotPacket; break; - /*case ProtocolInfo7::CONTAINER_SET_DATA_PACKET: + case ProtocolInfo7::CONTAINER_SET_DATA_PACKET: $data = new ContainerSetDataPacket; - break;*/ - /*case ProtocolInfo7::CONTAINER_SET_CONTENT_PACKET: + break; + case ProtocolInfo7::CONTAINER_SET_CONTENT_PACKET: $data = new ContainerSetContentPacket; - break;*/ + break; case ProtocolInfo7::CHAT_PACKET: $data = new ChatPacket; break; @@ -1028,9 +1025,9 @@ class RakNetParser{ case ProtocolInfo7::ENTITY_DATA_PACKET: $data = new EntityDataPacket; break; - /*case ProtocolInfo7::PLAYER_INPUT_PACKET: + case ProtocolInfo7::PLAYER_INPUT_PACKET: $data = new PlayerInputPacket; - break;*/ + break; default: $data = new UnknownPacket(); $data->packetID = $pid; From c15eeefaa4b9d41d0d842fe48b3227a2d184c77a Mon Sep 17 00:00:00 2001 From: Sunch <120295462+Sunch233@users.noreply.github.com> Date: Sat, 12 Apr 2025 20:33:03 +0800 Subject: [PATCH 15/18] feat: PacketPool --- src/PocketMinecraftServer.php | 1 + src/network/PacketPool.php | 117 ++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 src/network/PacketPool.php diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index cd260f11..1938e61d 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -67,6 +67,7 @@ class PocketMinecraftServer{ $this->saveEnabled = true; $this->tickMeasure = array_fill(0, 40, 0); $this->setType("normal"); + PacketPool::init(); $this->interface = new MinecraftInterface("255.255.255.255", $this->port, $this->serverip); $this->stop = false; $this->ticks = 0; diff --git a/src/network/PacketPool.php b/src/network/PacketPool.php new file mode 100644 index 00000000..4b94ed5d --- /dev/null +++ b/src/network/PacketPool.php @@ -0,0 +1,117 @@ + $class + */ + private static function registerPacket(string $class, $protocols = self::ACCEPTED_PROTOCOLS): void{ + /** @var RakNetDataPacket $pk */ + $pk = new $class; + foreach ($protocols as $protocol) { + $pk->PROTOCOL = $protocol; + $pid = $pk->pid(); + if(isset(self::$packetPool[$protocol][$pid])) { + $oldPk = self::$packetPool[$protocol][$pid]; + console("[WARNING] 协议$protocol 中 PID:$pid (0x".dechex($pid).")包".get_class($oldPk)."与包".get_class($pk)."冲突"); + } + self::$packetPool[$protocol][$pid] = clone $pk; + } + } + + public static function getPacket(int $pid, int $protocol) : RakNetDataPacket{ + if(isset(self::$packetPool[$protocol][$pid])) { + return clone self::$packetPool[$protocol][$pid]; + } + $pk = new UnknownPacket(); + $pk->packetID = $pid; + $pk->PROTOCOL = $protocol; + return $pk; + } + + public static function init() : void{ + if(self::$isInit){ + return; + } + + self::registerPacket(AddEntityPacket::class); + self::registerPacket(AddItemEntityPacket::class); + self::registerPacket(AddMobPacket::class); + self::registerPacket(AddPaintingPacket::class); + self::registerPacket(AddPlayerPacket::class); + self::registerPacket(AdventureSettingsPacket::class); + self::registerPacket(AnimatePacket::class); + + self::registerPacket(ChatPacket::class); + self::registerPacket(ChunkDataPacket::class); + self::registerPacket(ClientConnectPacket::class); + self::registerPacket(ClientHandshakePacket::class); + self::registerPacket(ContainerClosePacket::class); + self::registerPacket(ContainerOpenPacket::class); + self::registerPacket(ContainerSetContentPacket::class); + self::registerPacket(ContainerSetDataPacket::class); + self::registerPacket(ContainerSetSlotPacket::class); + + self::registerPacket(DisconnectPacket::class); + self::registerPacket(DropItemPacket::class); + + self::registerPacket(EntityDataPacket::class); + self::registerPacket(EntityEventPacket::class); + self::registerPacket(ExplodePacket::class); + + self::registerPacket(HurtArmorPacket::class, [9, 10, 11, 12, 13, 14]); + + self::registerPacket(InteractPacket::class); + + self::registerPacket(LevelEventPacket::class); + self::registerPacket(LoginPacket::class); + self::registerPacket(LoginStatusPacket::class); + + self::registerPacket(MessagePacket::class); + self::registerPacket(MoveEntityPacket::class); + self::registerPacket(MoveEntityPacket_PosRot::class); + self::registerPacket(MovePlayerPacket::class); + + self::registerPacket(PingPacket::class); + self::registerPacket(PlaceBlockPacket::class); + self::registerPacket(PlayerActionPacket::class); + self::registerPacket(PlayerArmorEquipmentPacket::class); + self::registerPacket(PlayerEquipmentPacket::class); + self::registerPacket(PlayerInputPacket::class); + self::registerPacket(PongPacket::class); + + self::registerPacket(ReadyPacket::class); + self::registerPacket(RemoveBlockPacket::class); + self::registerPacket(RemoveEntityPacket::class); + self::registerPacket(RemovePlayerPacket::class); + self::registerPacket(RequestChunkPacket::class); + self::registerPacket(RespawnPacket::class); + self::registerPacket(RotateHeadPacket::class); + + self::registerPacket(SendInventoryPacket::class); + self::registerPacket(ServerHandshakePacket::class); + self::registerPacket(SetEntityDataPacket::class); + self::registerPacket(SetEntityLinkPacket::class, [11, 12, 13, 14]); + self::registerPacket(SetEntityMotionPacket::class); + self::registerPacket(SetHealthPacket::class); + self::registerPacket(SetSpawnPositionPacket::class); + self::registerPacket(SetTimePacket::class); + self::registerPacket(StartGamePacket::class); + + self::registerPacket(TakeItemEntityPacket::class); + self::registerPacket(TileEventPacket::class); + + self::registerPacket(UpdateBlockPacket::class); + self::registerPacket(UseItemPacket::class); + + //self::registerPacket(UnknownPacket::class); //DO NOT REGISTER THIS + + self::$isInit = true; + } +} \ No newline at end of file From ce61a1024ce6d814d7aa719441b612352da91799 Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Sat, 12 Apr 2025 20:40:19 +0800 Subject: [PATCH 16/18] Fuck yefeng typo && Thanks to @Sunch233 --- src/network/protocol/ProtocolInfo.php | 16 ++++++++-------- src/network/raknet/RakNetDataPacket.php | 6 ++++++ src/network/raknet/RakNetParser.php | 4 +--- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/network/protocol/ProtocolInfo.php b/src/network/protocol/ProtocolInfo.php index b29ad3ef..25f7c77d 100644 --- a/src/network/protocol/ProtocolInfo.php +++ b/src/network/protocol/ProtocolInfo.php @@ -111,7 +111,7 @@ abstract class ProtocolInfo12{ const MOVE_ENTITY_PACKET_POSROT = 0x93; const ROTATE_HEAD_PACKET = 0xff; const MOVE_PLAYER_PACKET = 0x94; - const PLACE_BLOCK_PACKET = 0x96; + const PLACE_BLOCK_PACKET = 0x95; const REMOVE_BLOCK_PACKET = 0x96; const UPDATE_BLOCK_PACKET = 0x97; const ADD_PAINTING_PACKET = 0x98; @@ -206,12 +206,12 @@ abstract class ProtocolInfo9{ const RESPAWN_PACKET = 0xab; const SEND_INVENTORY_PACKET = 0xac; const DROP_ITEM_PACKET = 0xad; - const CONTAINER_OPEN_PACKET = 0xab; - const CONTAINER_CLOSE_PACKET = 0xac; - const CONTAINER_SET_SLOT_PACKET = 0xad; - const CONTAINER_SET_DATA_PACKET = 0xae; - const CONTAINER_SET_CONTENT_PACKET = 0xaf; - const CONTAINER_ACK_PACKET = 0xb; + const CONTAINER_OPEN_PACKET = 0xae; + const CONTAINER_CLOSE_PACKET = 0xaf; + const CONTAINER_SET_SLOT_PACKET = 0xb0; + const CONTAINER_SET_DATA_PACKET = 0xb1; + const CONTAINER_SET_CONTENT_PACKET = 0xb2; + const CONTAINER_ACK_PACKET = 0xb3; const CHAT_PACKET = 0xb4;//12 change const ADVENTURE_SETTINGS_PACKET = 0xb6; const ENTITY_DATA_PACKET = 0xb7; @@ -263,7 +263,7 @@ abstract class ProtocolInfo7{ // MCPE 0.3 - 0.5 seem using same packet id const REQUEST_CHUNK_PACKET = 0x9d; const CHUNK_DATA_PACKET = 0x9e; const PLAYER_EQUIPMENT_PACKET = 0x9f; - const PLAYER_ARMOR_EQUIPMENT_PACKET = 0xa0; + // const PLAYER_ARMOR_EQUIPMENT_PACKET = 0xa0; const INTERACT_PACKET = 0xa0;//Change const USE_ITEM_PACKET = 0xa1; const PLAYER_ACTION_PACKET = 0xa2; diff --git a/src/network/raknet/RakNetDataPacket.php b/src/network/raknet/RakNetDataPacket.php index 9c23db73..ba0ab963 100755 --- a/src/network/raknet/RakNetDataPacket.php +++ b/src/network/raknet/RakNetDataPacket.php @@ -37,6 +37,12 @@ abstract class RakNetDataPacket extends stdClass{ $this->setBuffer(chr($this->pid())); } + public function isPacketExist(int $protocol) : int{ + + $exist = true; + return $exist; + } + abstract public function pid(); public function getInternalPid() : int{ diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php index be0807a9..8e4202ae 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -6,6 +6,7 @@ class RakNetParser{ private $buffer; private $source; private $offset; + private $isparse = false; public function __construct(&$buffer, $source){ $this->source =& $source; @@ -965,9 +966,6 @@ class RakNetParser{ case ProtocolInfo7::PLAYER_EQUIPMENT_PACKET: $data = new PlayerEquipmentPacket; break; - case ProtocolInfo7::PLAYER_ARMOR_EQUIPMENT_PACKET: - $data = new PlayerArmorEquipmentPacket; - break; case ProtocolInfo7::INTERACT_PACKET: $data = new InteractPacket; break; From a5e19bb77435161e234852dbf38ec6881a01fe4b Mon Sep 17 00:00:00 2001 From: Sunch <120295462+Sunch233@users.noreply.github.com> Date: Sat, 12 Apr 2025 21:23:26 +0800 Subject: [PATCH 17/18] fixed packets pid mess --- src/network/PacketPool.php | 4 ++-- src/network/protocol/packet/ContainerSetContentPacket.php | 4 +++- src/network/protocol/packet/ContainerSetDataPacket.php | 4 +++- src/network/protocol/packet/PlaceBlockPacket.php | 4 ++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/network/PacketPool.php b/src/network/PacketPool.php index 4b94ed5d..b0f820ba 100644 --- a/src/network/PacketPool.php +++ b/src/network/PacketPool.php @@ -81,7 +81,7 @@ class PacketPool{ self::registerPacket(PingPacket::class); self::registerPacket(PlaceBlockPacket::class); self::registerPacket(PlayerActionPacket::class); - self::registerPacket(PlayerArmorEquipmentPacket::class); + self::registerPacket(PlayerArmorEquipmentPacket::class, [9, 10, 11, 12, 13, 14]); self::registerPacket(PlayerEquipmentPacket::class); self::registerPacket(PlayerInputPacket::class); self::registerPacket(PongPacket::class); @@ -97,7 +97,7 @@ class PacketPool{ self::registerPacket(SendInventoryPacket::class); self::registerPacket(ServerHandshakePacket::class); self::registerPacket(SetEntityDataPacket::class); - self::registerPacket(SetEntityLinkPacket::class, [11, 12, 13, 14]); + self::registerPacket(SetEntityLinkPacket::class, [12, 13, 14]); self::registerPacket(SetEntityMotionPacket::class); self::registerPacket(SetHealthPacket::class); self::registerPacket(SetSpawnPositionPacket::class); diff --git a/src/network/protocol/packet/ContainerSetContentPacket.php b/src/network/protocol/packet/ContainerSetContentPacket.php index 6378c386..e8ae387c 100644 --- a/src/network/protocol/packet/ContainerSetContentPacket.php +++ b/src/network/protocol/packet/ContainerSetContentPacket.php @@ -6,7 +6,9 @@ class ContainerSetContentPacket extends RakNetDataPacket{ public $hotbar = array(); public function pid(){ - if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::CONTAINER_SET_CONTENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::CONTAINER_SET_CONTENT_PACKET; }else if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::CONTAINER_SET_CONTENT_PACKET; diff --git a/src/network/protocol/packet/ContainerSetDataPacket.php b/src/network/protocol/packet/ContainerSetDataPacket.php index 7e9c7059..959e717f 100644 --- a/src/network/protocol/packet/ContainerSetDataPacket.php +++ b/src/network/protocol/packet/ContainerSetDataPacket.php @@ -6,7 +6,9 @@ class ContainerSetDataPacket extends RakNetDataPacket{ public $value; public function pid(){ - if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::CONTAINER_SET_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::CONTAINER_SET_DATA_PACKET; }else if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::CONTAINER_SET_DATA_PACKET; diff --git a/src/network/protocol/packet/PlaceBlockPacket.php b/src/network/protocol/packet/PlaceBlockPacket.php index 9171f04a..448f83fa 100644 --- a/src/network/protocol/packet/PlaceBlockPacket.php +++ b/src/network/protocol/packet/PlaceBlockPacket.php @@ -10,8 +10,8 @@ class PlaceBlockPacket extends RakNetDataPacket{ public $unknown7 = 0; public function pid(){ - if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9) { - return ProtocolInfo7::PLACE_BLOCK_PACKET; + if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL) { + return ProtocolInfo12::PLACE_BLOCK_PACKET; } return ProtocolInfo::PLACE_BLOCK_PACKET; } From 2803ecf3723b6ac67f1d1cc31dd04ee221452d47 Mon Sep 17 00:00:00 2001 From: Sunch <120295462+Sunch233@users.noreply.github.com> Date: Sat, 12 Apr 2025 21:23:59 +0800 Subject: [PATCH 18/18] MutiVersion: clean RakNetParser --- src/network/raknet/RakNetParser.php | 647 +--------------------------- 1 file changed, 8 insertions(+), 639 deletions(-) diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php index 8e4202ae..4594a896 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -61,32 +61,11 @@ class RakNetParser{ $this->packet->data = []; $this->packet->ip = $this->source; $PROTOCOL = PlayerAPI::decodeProtocol($this->packet->ip); - if($PROTOCOL > 13){ - while(!$this->feof() and ($pk = $this->parseDataPacket()) instanceof RakNetDataPacket){ - $this->packet->data[] = $pk; - } - break; - }elseif ($PROTOCOL > 10){ - while(!$this->feof() and ($pk = $this->parseDataPacket12()) instanceof RakNetDataPacket){ - $this->packet->data[] = $pk; - } - break; - }elseif ($PROTOCOL >= 9){ - while(!$this->feof() and ($pk = $this->parseDataPacket9()) instanceof RakNetDataPacket){ - $this->packet->data[] = $pk; - } - break; - }elseif ($PROTOCOL >= 3){ - while(!$this->feof() and ($pk = $this->parseDataPacket3()) instanceof RakNetDataPacket){ - $this->packet->data[] = $pk; - } - break; - }else{ - while(!$this->feof() and ($pk = $this->parseDataPacket()) instanceof RakNetDataPacket){ - $this->packet->data[] = $pk; - } - break; - } + + while(!$this->feof() and ($pk = $this->parseDataPacket()) instanceof RakNetDataPacket){ + $this->packet->data[] = $pk; + } + break; case RakNetInfo::NACK: case RakNetInfo::ACK: $count = $this->getShort(); @@ -144,7 +123,7 @@ class RakNetParser{ return !isset($this->buffer[$this->offset]); } - private function parseDataPacket(){ + private function parseDataPacket(int $protocol = ProtocolInfo::CURRENT_PROTOCOL){ $packetFlags = $this->getByte(); $reliability = ($packetFlags & 0b11100000) >> 5; $hasSplit = ($packetFlags & 0b00010000) > 0; @@ -190,176 +169,8 @@ class RakNetParser{ if(strlen($buffer) < ($length - 1)){ return false; } - switch($pid){ - case ProtocolInfo::PING_PACKET: - $data = new PingPacket; - break; - case ProtocolInfo::PONG_PACKET: - $data = new PongPacket; - break; - case ProtocolInfo::CLIENT_CONNECT_PACKET: - $data = new ClientConnectPacket; - break; - case ProtocolInfo::SERVER_HANDSHAKE_PACKET: - $data = new ServerHandshakePacket; - break; - case ProtocolInfo::DISCONNECT_PACKET: - $data = new DisconnectPacket; - break; - case ProtocolInfo::LOGIN_PACKET: - $data = new LoginPacket; - break; - case ProtocolInfo::LOGIN_STATUS_PACKET: - $data = new LoginStatusPacket; - break; - case ProtocolInfo::READY_PACKET: - $data = new ReadyPacket; - break; - case ProtocolInfo::MESSAGE_PACKET: - $data = new MessagePacket; - break; - case ProtocolInfo::SET_TIME_PACKET: - $data = new SetTimePacket; - break; - case ProtocolInfo::START_GAME_PACKET: - $data = new StartGamePacket; - break; - case ProtocolInfo::ADD_MOB_PACKET: - $data = new AddMobPacket; - break; - case ProtocolInfo::ADD_PLAYER_PACKET: - $data = new AddPlayerPacket; - break; - case ProtocolInfo::REMOVE_PLAYER_PACKET: - $data = new RemovePlayerPacket; - break; - case ProtocolInfo::ADD_ENTITY_PACKET: - $data = new AddEntityPacket; - break; - case ProtocolInfo::REMOVE_ENTITY_PACKET: - $data = new RemoveEntityPacket; - break; - case ProtocolInfo::ADD_ITEM_ENTITY_PACKET: - $data = new AddItemEntityPacket; - break; - case ProtocolInfo::TAKE_ITEM_ENTITY_PACKET: - $data = new TakeItemEntityPacket; - break; - case ProtocolInfo::MOVE_ENTITY_PACKET: - $data = new MoveEntityPacket; - break; - case ProtocolInfo::MOVE_ENTITY_PACKET_POSROT: - $data = new MoveEntityPacket_PosRot; - break; - case ProtocolInfo::ROTATE_HEAD_PACKET: - $data = new RotateHeadPacket; - break; - case ProtocolInfo::MOVE_PLAYER_PACKET: - $data = new MovePlayerPacket; - break; - case ProtocolInfo::REMOVE_BLOCK_PACKET: - $data = new RemoveBlockPacket; - break; - case ProtocolInfo::UPDATE_BLOCK_PACKET: - $data = new UpdateBlockPacket; - break; - case ProtocolInfo::ADD_PAINTING_PACKET: - $data = new AddPaintingPacket; - break; - case ProtocolInfo::EXPLODE_PACKET: - $data = new ExplodePacket; - break; - case ProtocolInfo::LEVEL_EVENT_PACKET: - $data = new LevelEventPacket; - break; - case ProtocolInfo::TILE_EVENT_PACKET: - $data = new TileEventPacket; - break; - case ProtocolInfo::ENTITY_EVENT_PACKET: - $data = new EntityEventPacket; - break; - case ProtocolInfo::REQUEST_CHUNK_PACKET: - $data = new RequestChunkPacket; - break; - case ProtocolInfo::CHUNK_DATA_PACKET: - $data = new ChunkDataPacket; - break; - case ProtocolInfo::PLAYER_EQUIPMENT_PACKET: - $data = new PlayerEquipmentPacket; - break; - case ProtocolInfo::PLAYER_ARMOR_EQUIPMENT_PACKET: - $data = new PlayerArmorEquipmentPacket; - break; - case ProtocolInfo::INTERACT_PACKET: - $data = new InteractPacket; - break; - case ProtocolInfo::USE_ITEM_PACKET: - $data = new UseItemPacket; - break; - case ProtocolInfo::PLAYER_ACTION_PACKET: - $data = new PlayerActionPacket; - break; - case ProtocolInfo::HURT_ARMOR_PACKET: - $data = new HurtArmorPacket; - break; - case ProtocolInfo::SET_ENTITY_DATA_PACKET: - $data = new SetEntityDataPacket; - break; - case ProtocolInfo::SET_ENTITY_MOTION_PACKET: - $data = new SetEntityMotionPacket; - break; - case ProtocolInfo::SET_HEALTH_PACKET: - $data = new SetHealthPacket; - break; - case ProtocolInfo::SET_SPAWN_POSITION_PACKET: - $data = new SetSpawnPositionPacket; - break; - case ProtocolInfo::ANIMATE_PACKET: - $data = new AnimatePacket; - break; - case ProtocolInfo::RESPAWN_PACKET: - $data = new RespawnPacket; - break; - case ProtocolInfo::SEND_INVENTORY_PACKET: - $data = new SendInventoryPacket; - break; - case ProtocolInfo::DROP_ITEM_PACKET: - $data = new DropItemPacket; - break; - case ProtocolInfo::CONTAINER_OPEN_PACKET: - $data = new ContainerOpenPacket; - break; - case ProtocolInfo::CONTAINER_CLOSE_PACKET: - $data = new ContainerClosePacket; - break; - case ProtocolInfo::CONTAINER_SET_SLOT_PACKET: - $data = new ContainerSetSlotPacket; - break; - case ProtocolInfo::CONTAINER_SET_DATA_PACKET: - $data = new ContainerSetDataPacket; - break; - case ProtocolInfo::CONTAINER_SET_CONTENT_PACKET: - $data = new ContainerSetContentPacket; - break; - case ProtocolInfo::CHAT_PACKET: - $data = new ChatPacket; - break; - case ProtocolInfo::ADVENTURE_SETTINGS_PACKET: - $data = new AdventureSettingsPacket; - break; - case ProtocolInfo::ENTITY_DATA_PACKET: - $data = new EntityDataPacket; - break; - case ProtocolInfo::SET_ENTITY_LINK_PACKET: - $data = new SetEntityLinkPacket; - case ProtocolInfo::PLAYER_INPUT_PACKET: - $data = new PlayerInputPacket; - break; - default: - $data = new UnknownPacket(); - $data->packetID = $pid; - break; - } + + $data = PacketPool::getPacket($pid, $protocol); $data->reliability = $reliability; $data->hasSplit = $hasSplit; $data->messageIndex = $messageIndex; @@ -602,448 +413,6 @@ class RakNetParser{ return $data; } - private function parseDataPacket9(){ - $packetFlags = $this->getByte(); - $reliability = ($packetFlags & 0b11100000) >> 5; - $hasSplit = ($packetFlags & 0b00010000) > 0; - $length = (int) ceil($this->getShort() / 8); - if($reliability === 2 - or $reliability === 3 - or $reliability === 4 - or $reliability === 6 - or $reliability === 7){ - $messageIndex = $this->getLTriad(); - }else{ - $messageIndex = false; - } - - if($reliability === 1 - or $reliability === 3 - or $reliability === 4 - or $reliability === 7){ - $orderIndex = $this->getLTriad(); - $orderChannel = $this->getByte(); - }else{ - $orderIndex = false; - $orderChannel = false; - } - - if($hasSplit){ - $splitCount = $this->getInt(); - $splitID = $this->getShort(); - $splitIndex = $this->getInt(); - }else{ - $splitCount = false; - $splitID = false; - $splitIndex = false; - } - - if($length <= 0 - or $orderChannel >= 32 - or ($hasSplit === true and $splitIndex >= $splitCount)){ - return false; - }else{ - $pid = $this->getByte(); - $buffer = $this->get($length - 1); - if(strlen($buffer) < ($length - 1)){ - return false; - } - switch($pid){ - case ProtocolInfo9::PING_PACKET: - $data = new PingPacket; - break; - case ProtocolInfo9::PONG_PACKET: - $data = new PongPacket; - break; - case ProtocolInfo9::CLIENT_CONNECT_PACKET: - $data = new ClientConnectPacket; - break; - case ProtocolInfo9::SERVER_HANDSHAKE_PACKET: - $data = new ServerHandshakePacket; - break; - case ProtocolInfo9::DISCONNECT_PACKET: - $data = new DisconnectPacket; - break; - case ProtocolInfo9::LOGIN_PACKET: - $data = new LoginPacket; - break; - case ProtocolInfo9::LOGIN_STATUS_PACKET: - $data = new LoginStatusPacket; - break; - case ProtocolInfo9::READY_PACKET: - $data = new ReadyPacket; - break; - case ProtocolInfo9::MESSAGE_PACKET: - $data = new MessagePacket; - break; - case ProtocolInfo9::SET_TIME_PACKET: - $data = new SetTimePacket; - break; - case ProtocolInfo9::START_GAME_PACKET: - $data = new StartGamePacket; - break; - case ProtocolInfo9::ADD_MOB_PACKET: - $data = new AddMobPacket; - break; - case ProtocolInfo9::ADD_PLAYER_PACKET: - $data = new AddPlayerPacket; - break; - case ProtocolInfo9::REMOVE_PLAYER_PACKET: - $data = new RemovePlayerPacket; - break; - case ProtocolInfo9::ADD_ENTITY_PACKET: - $data = new AddEntityPacket; - break; - case ProtocolInfo9::REMOVE_ENTITY_PACKET: - $data = new RemoveEntityPacket; - break; - case ProtocolInfo9::ADD_ITEM_ENTITY_PACKET: - $data = new AddItemEntityPacket; - break; - case ProtocolInfo9::TAKE_ITEM_ENTITY_PACKET: - $data = new TakeItemEntityPacket; - break; - case ProtocolInfo9::MOVE_ENTITY_PACKET: - $data = new MoveEntityPacket; - break; - case ProtocolInfo9::MOVE_ENTITY_PACKET_POSROT: - $data = new MoveEntityPacket_PosRot; - break; - case ProtocolInfo9::MOVE_PLAYER_PACKET: - $data = new MovePlayerPacket; - break; - case ProtocolInfo9::REMOVE_BLOCK_PACKET: - $data = new RemoveBlockPacket; - break; - case ProtocolInfo9::UPDATE_BLOCK_PACKET: - $data = new UpdateBlockPacket; - break; - case ProtocolInfo9::ADD_PAINTING_PACKET: - $data = new AddPaintingPacket; - break; - case ProtocolInfo9::EXPLODE_PACKET: - $data = new ExplodePacket; - break; - case ProtocolInfo9::LEVEL_EVENT_PACKET: - $data = new LevelEventPacket; - break; - case ProtocolInfo9::TILE_EVENT_PACKET: - $data = new TileEventPacket; - break; - case ProtocolInfo9::ENTITY_EVENT_PACKET: - $data = new EntityEventPacket; - break; - case ProtocolInfo9::REQUEST_CHUNK_PACKET: - $data = new RequestChunkPacket; - break; - case ProtocolInfo9::CHUNK_DATA_PACKET: - $data = new ChunkDataPacket; - break; - case ProtocolInfo9::PLAYER_EQUIPMENT_PACKET: - $data = new PlayerEquipmentPacket; - break; - case ProtocolInfo9::PLAYER_ARMOR_EQUIPMENT_PACKET: - $data = new PlayerArmorEquipmentPacket; - break; - case ProtocolInfo9::INTERACT_PACKET: - $data = new InteractPacket; - break; - case ProtocolInfo9::USE_ITEM_PACKET: - $data = new UseItemPacket; - break; - case ProtocolInfo9::PLAYER_ACTION_PACKET: - $data = new PlayerActionPacket; - break; - case ProtocolInfo9::HURT_ARMOR_PACKET: - $data = new HurtArmorPacket; - break; - case ProtocolInfo9::SET_ENTITY_DATA_PACKET: - $data = new SetEntityDataPacket; - break; - case ProtocolInfo9::SET_ENTITY_MOTION_PACKET: - $data = new SetEntityMotionPacket; - break; - case ProtocolInfo9::SET_HEALTH_PACKET: - $data = new SetHealthPacket; - break; - case ProtocolInfo9::SET_SPAWN_POSITION_PACKET: - $data = new SetSpawnPositionPacket; - break; - case ProtocolInfo9::ANIMATE_PACKET: - $data = new AnimatePacket; - break; - case ProtocolInfo9::RESPAWN_PACKET: - $data = new RespawnPacket; - break; - case ProtocolInfo9::SEND_INVENTORY_PACKET: - $data = new SendInventoryPacket; - break; - case ProtocolInfo9::DROP_ITEM_PACKET: - $data = new DropItemPacket; - break; - case ProtocolInfo9::CONTAINER_OPEN_PACKET: - $data = new ContainerOpenPacket; - break; - case ProtocolInfo9::CONTAINER_CLOSE_PACKET: - $data = new ContainerClosePacket; - break; - case ProtocolInfo9::CONTAINER_SET_SLOT_PACKET: - $data = new ContainerSetSlotPacket; - break; - case ProtocolInfo9::CONTAINER_SET_DATA_PACKET: - $data = new ContainerSetDataPacket; - break; - case ProtocolInfo9::CONTAINER_SET_CONTENT_PACKET: - $data = new ContainerSetContentPacket; - break; - case ProtocolInfo9::CHAT_PACKET: - $data = new ChatPacket; - break; - case ProtocolInfo9::ADVENTURE_SETTINGS_PACKET: - $data = new AdventureSettingsPacket; - break; - case ProtocolInfo9::ENTITY_DATA_PACKET: - $data = new EntityDataPacket; - break; - case ProtocolInfo9::PLAYER_INPUT_PACKET: - $data = new PlayerInputPacket; - break; - default: - $data = new UnknownPacket(); - $data->packetID = $pid; - break; - } - $data->reliability = $reliability; - $data->hasSplit = $hasSplit; - $data->messageIndex = $messageIndex; - $data->orderIndex = $orderIndex; - $data->orderChannel = $orderChannel; - $data->splitCount = $splitCount; - $data->splitID = $splitID; - $data->splitIndex = $splitIndex; - $data->setBuffer($buffer); - } - return $data; - } - - private function parseDataPacket3(){ - $packetFlags = $this->getByte(); - $reliability = ($packetFlags & 0b11100000) >> 5; - $hasSplit = ($packetFlags & 0b00010000) > 0; - $length = (int) ceil($this->getShort() / 8); - if($reliability === 2 - or $reliability === 3 - or $reliability === 4 - or $reliability === 6 - or $reliability === 7){ - $messageIndex = $this->getLTriad(); - }else{ - $messageIndex = false; - } - - if($reliability === 1 - or $reliability === 3 - or $reliability === 4 - or $reliability === 7){ - $orderIndex = $this->getLTriad(); - $orderChannel = $this->getByte(); - }else{ - $orderIndex = false; - $orderChannel = false; - } - - if($hasSplit){ - $splitCount = $this->getInt(); - $splitID = $this->getShort(); - $splitIndex = $this->getInt(); - }else{ - $splitCount = false; - $splitID = false; - $splitIndex = false; - } - - if($length <= 0 - or $orderChannel >= 32 - or ($hasSplit === true and $splitIndex >= $splitCount)){ - return false; - }else{ - $pid = $this->getByte(); - $buffer = $this->get($length - 1); - if(strlen($buffer) < ($length - 1)){ - return false; - } - switch($pid){ - case ProtocolInfo7::PING_PACKET: - $data = new PingPacket; - break; - case ProtocolInfo7::PONG_PACKET: - $data = new PongPacket; - break; - case ProtocolInfo7::CLIENT_CONNECT_PACKET: - $data = new ClientConnectPacket; - break; - case ProtocolInfo7::SERVER_HANDSHAKE_PACKET: - $data = new ServerHandshakePacket; - break; - case ProtocolInfo7::DISCONNECT_PACKET: - $data = new DisconnectPacket; - break; - case ProtocolInfo7::LOGIN_PACKET: - $data = new LoginPacket; - break; - case ProtocolInfo7::LOGIN_STATUS_PACKET: - $data = new LoginStatusPacket; - break; - case ProtocolInfo7::READY_PACKET: - $data = new ReadyPacket; - break; - case ProtocolInfo7::MESSAGE_PACKET: - $data = new MessagePacket; - break; - case ProtocolInfo7::SET_TIME_PACKET: - $data = new SetTimePacket; - break; - case ProtocolInfo7::START_GAME_PACKET: - $data = new StartGamePacket; - break; - case ProtocolInfo7::ADD_MOB_PACKET: - $data = new AddMobPacket; - break; - case ProtocolInfo7::ADD_PLAYER_PACKET: - $data = new AddPlayerPacket; - break; - case ProtocolInfo7::REMOVE_PLAYER_PACKET: - $data = new RemovePlayerPacket; - break; - case ProtocolInfo7::ADD_ENTITY_PACKET: - $data = new AddEntityPacket; - break; - case ProtocolInfo7::REMOVE_ENTITY_PACKET: - $data = new RemoveEntityPacket; - break; - case ProtocolInfo7::ADD_ITEM_ENTITY_PACKET: - $data = new AddItemEntityPacket; - break; - case ProtocolInfo7::TAKE_ITEM_ENTITY_PACKET: - $data = new TakeItemEntityPacket; - break; - case ProtocolInfo7::MOVE_ENTITY_PACKET: - $data = new MoveEntityPacket; - break; - case ProtocolInfo7::MOVE_ENTITY_PACKET_POSROT: - $data = new MoveEntityPacket_PosRot; - break; - case ProtocolInfo7::MOVE_PLAYER_PACKET: - $data = new MovePlayerPacket; - break; - case ProtocolInfo7::REMOVE_BLOCK_PACKET: - $data = new RemoveBlockPacket; - break; - case ProtocolInfo7::UPDATE_BLOCK_PACKET: - $data = new UpdateBlockPacket; - break; - case ProtocolInfo7::ADD_PAINTING_PACKET: - $data = new AddPaintingPacket; - break; - case ProtocolInfo7::EXPLODE_PACKET: - $data = new ExplodePacket; - break; - case ProtocolInfo7::LEVEL_EVENT_PACKET: - $data = new LevelEventPacket; - break; - case ProtocolInfo7::TILE_EVENT_PACKET: - $data = new TileEventPacket; - break; - case ProtocolInfo7::ENTITY_EVENT_PACKET: - $data = new EntityEventPacket; - break; - case ProtocolInfo7::REQUEST_CHUNK_PACKET: - $data = new RequestChunkPacket; - break; - case ProtocolInfo7::CHUNK_DATA_PACKET: - $data = new ChunkDataPacket; - break; - case ProtocolInfo7::PLAYER_EQUIPMENT_PACKET: - $data = new PlayerEquipmentPacket; - break; - case ProtocolInfo7::INTERACT_PACKET: - $data = new InteractPacket; - break; - case ProtocolInfo7::USE_ITEM_PACKET: - $data = new UseItemPacket; - break; - case ProtocolInfo7::PLAYER_ACTION_PACKET: - $data = new PlayerActionPacket; - break; - case ProtocolInfo7::SET_ENTITY_DATA_PACKET: - $data = new SetEntityDataPacket; - break; - case ProtocolInfo7::SET_ENTITY_MOTION_PACKET: - $data = new SetEntityMotionPacket; - break; - case ProtocolInfo7::SET_HEALTH_PACKET: - $data = new SetHealthPacket; - break; - case ProtocolInfo7::SET_SPAWN_POSITION_PACKET: - $data = new SetSpawnPositionPacket; - break; - case ProtocolInfo7::ANIMATE_PACKET: - $data = new AnimatePacket; - break; - case ProtocolInfo7::RESPAWN_PACKET: - $data = new RespawnPacket; - break; - case ProtocolInfo7::SEND_INVENTORY_PACKET: - $data = new SendInventoryPacket; - break; - case ProtocolInfo7::DROP_ITEM_PACKET: - $data = new DropItemPacket; - break; - case ProtocolInfo7::CONTAINER_OPEN_PACKET: - $data = new ContainerOpenPacket; - break; - case ProtocolInfo7::CONTAINER_CLOSE_PACKET: - $data = new ContainerClosePacket; - break; - case ProtocolInfo7::CONTAINER_SET_SLOT_PACKET: - $data = new ContainerSetSlotPacket; - break; - case ProtocolInfo7::CONTAINER_SET_DATA_PACKET: - $data = new ContainerSetDataPacket; - break; - case ProtocolInfo7::CONTAINER_SET_CONTENT_PACKET: - $data = new ContainerSetContentPacket; - break; - case ProtocolInfo7::CHAT_PACKET: - $data = new ChatPacket; - break; - case ProtocolInfo7::ADVENTURE_SETTINGS_PACKET: - $data = new AdventureSettingsPacket; - break; - case ProtocolInfo7::ENTITY_DATA_PACKET: - $data = new EntityDataPacket; - break; - case ProtocolInfo7::PLAYER_INPUT_PACKET: - $data = new PlayerInputPacket; - break; - default: - $data = new UnknownPacket(); - $data->packetID = $pid; - break; - } - $data->reliability = $reliability; - $data->hasSplit = $hasSplit; - $data->messageIndex = $messageIndex; - $data->orderIndex = $orderIndex; - $data->orderChannel = $orderChannel; - $data->splitCount = $splitCount; - $data->splitID = $splitID; - $data->splitIndex = $splitIndex; - $data->setBuffer($buffer); - } - return $data; - } - private function getInt($unsigned = false){ return Utils::readInt($this->get(4), $unsigned); }