From 3d8bb7df05f580b59da550d4ab81533b8563409d Mon Sep 17 00:00:00 2001 From: Administrator Date: Thu, 13 Mar 2025 19:23:05 +0800 Subject: [PATCH 01/39] 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/39] 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/39] 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/39] 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/39] 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/39] 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/39] 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/39] 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/39] 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/39] 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/39] 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/39] 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/39] 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/39] 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/39] 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/39] 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/39] 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/39] 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); } From 755c12ac36a31cb5d0dde454f9f974a76b96a866 Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Tue, 15 Apr 2025 19:05:06 +0800 Subject: [PATCH 19/39] Fix @Sunch233 typo --- src/network/raknet/RakNetParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php index 4594a896..16d44c95 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -62,7 +62,7 @@ class RakNetParser{ $this->packet->ip = $this->source; $PROTOCOL = PlayerAPI::decodeProtocol($this->packet->ip); - while(!$this->feof() and ($pk = $this->parseDataPacket()) instanceof RakNetDataPacket){ + while(!$this->feof() and ($pk = $this->parseDataPacket($PROTOCOL)) instanceof RakNetDataPacket){ $this->packet->data[] = $pk; } break; From eccf3e713157c2e47553137b40dbd5e1bd5aa997 Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <121912649+yefengeeeeeeeeeee@users.noreply.github.com> Date: Thu, 17 Apr 2025 23:44:15 +0800 Subject: [PATCH 20/39] Seems Fix TypeError --- src/network/raknet/RakNetParser.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php index 16d44c95..0cbb5ee8 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -123,7 +123,7 @@ class RakNetParser{ return !isset($this->buffer[$this->offset]); } - private function parseDataPacket(int $protocol = ProtocolInfo::CURRENT_PROTOCOL){ + private function parseDataPacket($protocol = ProtocolInfo::CURRENT_PROTOCOL){ $packetFlags = $this->getByte(); $reliability = ($packetFlags & 0b11100000) >> 5; $hasSplit = ($packetFlags & 0b00010000) > 0; @@ -417,4 +417,4 @@ class RakNetParser{ return Utils::readInt($this->get(4), $unsigned); } -} \ No newline at end of file +} From edefb7fe0587dcdf9d47238a97c20e18a9b65bed Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Tue, 22 Apr 2025 19:41:57 +0800 Subject: [PATCH 21/39] PlaceBlockPacket impelement --- src/Player.php | 72 ++++++++++++++++++- .../protocol/packet/PlaceBlockPacket.php | 56 ++++++--------- 2 files changed, 92 insertions(+), 36 deletions(-) diff --git a/src/Player.php b/src/Player.php index fc8349d8..f3de9b1c 100755 --- a/src/Player.php +++ b/src/Player.php @@ -356,9 +356,12 @@ class Player{ return; } - $packet->PROTOCOL = $this->PROTOCOL; + $packet->PROTOCOL = $this->PROTOCOL; $packet->encode(); + if($packet->pid() == 163 or $packet->pid() == 164 && $this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return; + } $len = strlen($packet->buffer) + 1; $MTU = $this->MTU - 24; if($len > $MTU){ @@ -2228,6 +2231,73 @@ class Player{ } } break; + case ProtocolInfo::PLACE_BLOCK_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 = []; + + 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)) { + + } 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; + } + break; case ProtocolInfo::PLAYER_ACTION_PACKET: if ($this->spawned === false or $this->blocked === true) { break; diff --git a/src/network/protocol/packet/PlaceBlockPacket.php b/src/network/protocol/packet/PlaceBlockPacket.php index 448f83fa..22cbf9dc 100644 --- a/src/network/protocol/packet/PlaceBlockPacket.php +++ b/src/network/protocol/packet/PlaceBlockPacket.php @@ -1,13 +1,13 @@ PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL) { @@ -17,38 +17,24 @@ class PlaceBlockPacket extends RakNetDataPacket{ } 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); + $this->eid = $this->getInt(); + $this->x = $this->getInt(); + $this->z = $this->getInt(); + $this->y = $this->getByte(); + $this->block = $this->getByte(); + $this->meta = $this->getByte(); + $this->face = $this->getByte(); } 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); + $this->putInt($this->eid); + $this->putInt($this->x); + $this->putInt($this->z); + $this->putByte($this->y); + $this->putByte($this->block); + $this->putByte($this->meta); + $this->putByte($this->face); } } \ No newline at end of file From 6b7563d8ea645e431a179d90f92534f958162e02 Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Wed, 23 Apr 2025 20:44:45 +0800 Subject: [PATCH 22/39] Seem Fix Type Error blame yefeng moment --- src/network/PacketPool.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/network/PacketPool.php b/src/network/PacketPool.php index b0f820ba..5dc4fb61 100644 --- a/src/network/PacketPool.php +++ b/src/network/PacketPool.php @@ -25,7 +25,7 @@ class PacketPool{ } } - public static function getPacket(int $pid, int $protocol) : RakNetDataPacket{ + public static function getPacket($pid, $protocol) : RakNetDataPacket{ if(isset(self::$packetPool[$protocol][$pid])) { return clone self::$packetPool[$protocol][$pid]; } From fd23ba04ec234a8c394e160b81c4747870923694 Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Fri, 25 Apr 2025 17:19:22 +0800 Subject: [PATCH 23/39] Conver High ItemID to Old ItemId --- src/API/BlockAPI.php | 26 +++++++++++++++++++ .../protocol/packet/AddItemEntityPacket.php | 2 +- .../packet/ContainerSetContentPacket.php | 2 +- .../packet/ContainerSetSlotPacket.php | 2 +- .../protocol/packet/PlaceBlockPacket.php | 2 +- .../protocol/packet/PlayerEquipmentPacket.php | 2 +- .../protocol/packet/SendInventoryPacket.php | 4 +-- .../protocol/packet/UpdateBlockPacket.php | 2 +- src/network/raknet/RakNetDataPacket.php | 4 +-- 9 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index ddd309d9..5e44794d 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -644,4 +644,30 @@ class BlockAPI{ } } } + + public static function convertHighItemIdsToOldItemIds(int $protocolId, int $itemId){ + if ($protocolId >= ProtocolInfo::CURRENT_PROTOCOL) { + return $itemId; + } + + $idMap = []; + if ($protocolId < ProtocolInfo9::CURRENT_PROTOCOL_9) { + $idMap += [ + 87 => 49, + 405 => 336, + 406 => 336, + ]; + } + if ($protocolId < ProtocolInfo12::CURRENT_PROTOCOL_12) { + $idMap += [ + 91 => 103, + 361 => 362, + 400 => 297, + 457 => 295, + 458 => 297, + ]; + } + + return $idMap[$itemId] ?? $itemId; + } } diff --git a/src/network/protocol/packet/AddItemEntityPacket.php b/src/network/protocol/packet/AddItemEntityPacket.php index fb68f60a..b9d66cf3 100644 --- a/src/network/protocol/packet/AddItemEntityPacket.php +++ b/src/network/protocol/packet/AddItemEntityPacket.php @@ -21,7 +21,7 @@ class AddItemEntityPacket extends RakNetDataPacket{ public function encode(){ $this->reset(); $this->putInt($this->eid); - $this->putSlot($this->item); + $this->putSlot($this->PROTOCOL, $this->item); $this->putFloat($this->x); $this->putFloat($this->y); $this->putFloat($this->z); diff --git a/src/network/protocol/packet/ContainerSetContentPacket.php b/src/network/protocol/packet/ContainerSetContentPacket.php index e8ae387c..9df71cdc 100644 --- a/src/network/protocol/packet/ContainerSetContentPacket.php +++ b/src/network/protocol/packet/ContainerSetContentPacket.php @@ -35,7 +35,7 @@ class ContainerSetContentPacket extends RakNetDataPacket{ $this->putByte($this->windowid); $this->putShort(count($this->slots)); foreach($this->slots as $slot){ - $this->putSlot($slot); + $this->putSlot($this->PROTOCOL, $slot); } if($this->windowid === 0 and count($this->hotbar) > 0){ $this->putShort(count($this->hotbar));//null diff --git a/src/network/protocol/packet/ContainerSetSlotPacket.php b/src/network/protocol/packet/ContainerSetSlotPacket.php index 2a012112..70fb6b39 100644 --- a/src/network/protocol/packet/ContainerSetSlotPacket.php +++ b/src/network/protocol/packet/ContainerSetSlotPacket.php @@ -26,7 +26,7 @@ class ContainerSetSlotPacket extends RakNetDataPacket{ $this->reset(); $this->putByte($this->windowid); $this->putShort($this->slot); - $this->putSlot($this->item); + $this->putSlot($this->PROTOCOL, $this->item); } } \ No newline at end of file diff --git a/src/network/protocol/packet/PlaceBlockPacket.php b/src/network/protocol/packet/PlaceBlockPacket.php index 22cbf9dc..950115ef 100644 --- a/src/network/protocol/packet/PlaceBlockPacket.php +++ b/src/network/protocol/packet/PlaceBlockPacket.php @@ -32,7 +32,7 @@ class PlaceBlockPacket extends RakNetDataPacket{ $this->putInt($this->x); $this->putInt($this->z); $this->putByte($this->y); - $this->putByte($this->block); + $this->putByte(BlockAPI::convertHighItemIdsToOldItemIds($this->PROTOCOL, $this->block)); $this->putByte($this->meta); $this->putByte($this->face); } diff --git a/src/network/protocol/packet/PlayerEquipmentPacket.php b/src/network/protocol/packet/PlayerEquipmentPacket.php index 2bcb648f..d95ef3be 100644 --- a/src/network/protocol/packet/PlayerEquipmentPacket.php +++ b/src/network/protocol/packet/PlayerEquipmentPacket.php @@ -23,7 +23,7 @@ class PlayerEquipmentPacket extends RakNetDataPacket{ public function encode(){ $this->reset(); $this->putInt($this->eid); - $this->putShort($this->item); + $this->putShort(BlockAPI::convertHighItemIdsToOldItemIds($this->PROTOCOL, $this->item)); $this->putShort($this->meta); if($this->PROTOCOL === ProtocolInfo::CURRENT_PROTOCOL){$this->putByte($this->slot);} } diff --git a/src/network/protocol/packet/SendInventoryPacket.php b/src/network/protocol/packet/SendInventoryPacket.php index 8bfaef4a..78410a52 100644 --- a/src/network/protocol/packet/SendInventoryPacket.php +++ b/src/network/protocol/packet/SendInventoryPacket.php @@ -37,11 +37,11 @@ class SendInventoryPacket extends RakNetDataPacket{ $this->putByte($this->windowid); $this->putShort(count($this->slots)); foreach($this->slots as $slot){ - $this->putSlot($slot); + $this->putSlot($this->PROTOCOL, $slot); } if($this->windowid === 1 and count($this->armor) === 4){ for($s = 0; $s < 4; ++$s){ - $this->putSlot($this->armor[$s]); + $this->putSlot($this->PROTOCOL, $this->armor[$s]); } } } diff --git a/src/network/protocol/packet/UpdateBlockPacket.php b/src/network/protocol/packet/UpdateBlockPacket.php index d75eb806..76c92815 100644 --- a/src/network/protocol/packet/UpdateBlockPacket.php +++ b/src/network/protocol/packet/UpdateBlockPacket.php @@ -23,7 +23,7 @@ class UpdateBlockPacket extends RakNetDataPacket{ $this->putInt($this->x); $this->putInt($this->z); $this->putByte($this->y); - $this->putByte($this->block); + $this->putByte(BlockAPI::convertHighItemIdsToOldItemIds($this->PROTOCOL, $this->block)); $this->putByte($this->meta); } diff --git a/src/network/raknet/RakNetDataPacket.php b/src/network/raknet/RakNetDataPacket.php index ba0ab963..765b5851 100755 --- a/src/network/raknet/RakNetDataPacket.php +++ b/src/network/raknet/RakNetDataPacket.php @@ -150,8 +150,8 @@ abstract class RakNetDataPacket extends stdClass{ return ord($this->get(1)); } - protected function putSlot(Item $item){ - $this->putShort($item->getID()); + protected function putSlot(Int $protocolId, Item $item){ + $this->putShort(BlockAPI::convertHighItemIdsToOldItemIds($protocolId, $item->getID())); $this->putByte($item->count); $this->putShort($item->getMetadata()); } From c8fc4139f80a1e73f2b725b338b0e2faefc8203b Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Sat, 26 Apr 2025 15:49:12 +0800 Subject: [PATCH 24/39] Thanks to @GameHeroBrine --- src/Player.php | 2 +- src/network/PacketPool.php | 18 +- src/network/protocol/ProtocolInfo.php | 205 +++++++++++++++++- .../protocol/packet/AddEntityPacket.php | 3 + .../protocol/packet/AddItemEntityPacket.php | 3 + src/network/protocol/packet/AnimatePacket.php | 8 +- src/network/protocol/packet/ChatPacket.php | 4 +- .../protocol/packet/ChunkDataPacket.php | 8 +- .../protocol/packet/ContainerClosePacket.php | 8 +- .../protocol/packet/ContainerOpenPacket.php | 8 +- .../packet/ContainerSetContentPacket.php | 8 +- .../packet/ContainerSetDataPacket.php | 8 +- .../packet/ContainerSetSlotPacket.php | 8 +- .../protocol/packet/DropItemPacket.php | 8 +- .../protocol/packet/EntityEventPacket.php | 8 +- src/network/protocol/packet/ExplodePacket.php | 6 +- .../protocol/packet/InteractPacket.php | 8 +- .../protocol/packet/LevelEventPacket.php | 6 +- .../protocol/packet/MoveEntityPacket.php | 3 + .../packet/MoveEntityPacket_PosRot.php | 3 + .../protocol/packet/MovePlayerPacket.php | 4 +- .../protocol/packet/PlaceBlockPacket.php | 4 +- .../protocol/packet/PlayerActionPacket.php | 4 +- .../protocol/packet/PlayerEquipmentPacket.php | 8 +- .../protocol/packet/RemoveBlockPacket.php | 4 +- .../protocol/packet/RemoveEntityPacket.php | 3 + .../protocol/packet/RequestChunkPacket.php | 8 +- src/network/protocol/packet/RespawnPacket.php | 8 +- .../protocol/packet/SendInventoryPacket.php | 8 +- .../protocol/packet/SetEntityDataPacket.php | 8 +- .../protocol/packet/SetEntityMotionPacket.php | 6 +- .../protocol/packet/SetHealthPacket.php | 8 +- .../protocol/packet/TakeItemEntityPacket.php | 3 + .../protocol/packet/TileEventPacket.php | 6 +- .../protocol/packet/UpdateBlockPacket.php | 4 +- src/network/protocol/packet/UseItemPacket.php | 8 +- 36 files changed, 387 insertions(+), 40 deletions(-) diff --git a/src/Player.php b/src/Player.php index f3de9b1c..cabd1ee2 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1547,7 +1547,7 @@ class Player{ $this->close("server is full!", false); return; } - if ($packet->protocol1 < ProtocolInfo7::CURRENT_PROTOCOL_5 && $packet->protocol1 > ProtocolInfo::CURRENT_PROTOCOL) { + if ($packet->protocol1 < ProtocolInfo3::CURRENT_PROTOCOL_3 && $packet->protocol1 > ProtocolInfo::CURRENT_PROTOCOL) { if ($packet->protocol1 < ProtocolInfo::CURRENT_PROTOCOL) { $pk = new LoginStatusPacket; $pk->status = 1; diff --git a/src/network/PacketPool.php b/src/network/PacketPool.php index 5dc4fb61..eb9753db 100644 --- a/src/network/PacketPool.php +++ b/src/network/PacketPool.php @@ -43,12 +43,12 @@ class PacketPool{ self::registerPacket(AddEntityPacket::class); self::registerPacket(AddItemEntityPacket::class); self::registerPacket(AddMobPacket::class); - self::registerPacket(AddPaintingPacket::class); + self::registerPacket(AddPaintingPacket::class, [7, 8, 9, 10, 11, 12, 13, 14]); self::registerPacket(AddPlayerPacket::class); - self::registerPacket(AdventureSettingsPacket::class); + self::registerPacket(AdventureSettingsPacket::class, [7, 8, 9, 10, 11, 12, 13, 14]); self::registerPacket(AnimatePacket::class); - self::registerPacket(ChatPacket::class); + self::registerPacket(ChatPacket::class, [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); self::registerPacket(ChunkDataPacket::class); self::registerPacket(ClientConnectPacket::class); self::registerPacket(ClientHandshakePacket::class); @@ -61,7 +61,7 @@ class PacketPool{ self::registerPacket(DisconnectPacket::class); self::registerPacket(DropItemPacket::class); - self::registerPacket(EntityDataPacket::class); + self::registerPacket(EntityDataPacket::class, [7, 8, 9, 10, 11, 12, 13, 14]); self::registerPacket(EntityEventPacket::class); self::registerPacket(ExplodePacket::class); @@ -80,10 +80,10 @@ class PacketPool{ self::registerPacket(PingPacket::class); self::registerPacket(PlaceBlockPacket::class); - self::registerPacket(PlayerActionPacket::class); + self::registerPacket(PlayerActionPacket::class, [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); self::registerPacket(PlayerArmorEquipmentPacket::class, [9, 10, 11, 12, 13, 14]); self::registerPacket(PlayerEquipmentPacket::class); - self::registerPacket(PlayerInputPacket::class); + self::registerPacket(PlayerInputPacket::class, [7, 8, 9, 10, 11, 12, 13, 14]); self::registerPacket(PongPacket::class); self::registerPacket(ReadyPacket::class); @@ -98,14 +98,14 @@ class PacketPool{ self::registerPacket(ServerHandshakePacket::class); self::registerPacket(SetEntityDataPacket::class); self::registerPacket(SetEntityLinkPacket::class, [12, 13, 14]); - self::registerPacket(SetEntityMotionPacket::class); + self::registerPacket(SetEntityMotionPacket::class, [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); self::registerPacket(SetHealthPacket::class); - self::registerPacket(SetSpawnPositionPacket::class); + self::registerPacket(SetSpawnPositionPacket::class, [7, 8, 9, 10, 11, 12, 13, 14]); self::registerPacket(SetTimePacket::class); self::registerPacket(StartGamePacket::class); self::registerPacket(TakeItemEntityPacket::class); - self::registerPacket(TileEventPacket::class); + self::registerPacket(TileEventPacket::class, [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); self::registerPacket(UpdateBlockPacket::class); self::registerPacket(UseItemPacket::class); diff --git a/src/network/protocol/ProtocolInfo.php b/src/network/protocol/ProtocolInfo.php index 25f7c77d..f45534c3 100644 --- a/src/network/protocol/ProtocolInfo.php +++ b/src/network/protocol/ProtocolInfo.php @@ -218,9 +218,7 @@ abstract class ProtocolInfo9{ const PLAYER_INPUT_PACKET = 0xb9; } -abstract class ProtocolInfo7{ // MCPE 0.3 - 0.5 seem using same packet id - - const CURRENT_PROTOCOL_5 = 5; +abstract class ProtocolInfo7{ const CURRENT_PROTOCOL_7 = 7; @@ -287,6 +285,207 @@ abstract class ProtocolInfo7{ // MCPE 0.3 - 0.5 seem using same packet id 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; + const EXPLODE_PACKET = 0x98;// Change + const LEVEL_EVENT_PACKET = 0x99; + const TILE_EVENT_PACKET = 0x9a; + const ENTITY_EVENT_PACKET = 0x9b; + const REQUEST_CHUNK_PACKET = 0x9c; + const CHUNK_DATA_PACKET = 0x9d; + const PLAYER_EQUIPMENT_PACKET = 0x9e; + // const PLAYER_ARMOR_EQUIPMENT_PACKET = 0xa0; + const INTERACT_PACKET = 0x9f; + const USE_ITEM_PACKET = 0xa0; + const PLAYER_ACTION_PACKET = 0xa1; + const SET_ENTITY_DATA_PACKET = 0xa2; + const SET_ENTITY_MOTION_PACKET = 0xa3; + //const SET_ENTITY_LINK_PACKET = 0xa?; + const SET_HEALTH_PACKET = 0xa4; + //const SET_SPAWN_POSITION_PACKET = 0xa6; + const ANIMATE_PACKET = 0xa5; + const RESPAWN_PACKET = 0xa6; + const SEND_INVENTORY_PACKET = 0xa7; + const DROP_ITEM_PACKET = 0xa8; + const CONTAINER_OPEN_PACKET = 0xa9; + const CONTAINER_CLOSE_PACKET = 0xaa; + const CONTAINER_SET_SLOT_PACKET = 0xab; + const CONTAINER_SET_DATA_PACKET = 0xac; + const CONTAINER_SET_CONTENT_PACKET = 0xad; + const CONTAINER_ACK_PACKET = 0xae; + const CHAT_PACKET = 0xaf; + //const ADVENTURE_SETTINGS_PACKET = 0xb3; + //const ENTITY_DATA_PACKET = 0xb2; + //const PLAYER_INPUT_PACKET = 0xb9; +} +abstract class ProtocolInfo4{ + + const CURRENT_PROTOCOL_4 = 4; + + 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 = 0x8b; //Maybe exist // Change + const REMOVE_ENTITY_PACKET = 0x8c; + const ADD_ITEM_ENTITY_PACKET = 0x8d; + const TAKE_ITEM_ENTITY_PACKET = 0x8e; + const MOVE_ENTITY_PACKET = 0x8f;//unused ? + + const MOVE_ENTITY_PACKET_POSROT = 0x92; + const MOVE_PLAYER_PACKET = 0x93; + const PLACE_BLOCK_PACKET = 0x94; + const REMOVE_BLOCK_PACKET = 0x95;//Maybe exist + const UPDATE_BLOCK_PACKET = 0x96; + //const ADD_PAINTING_PACKET = 0x98; + const EXPLODE_PACKET = 0x97; + const LEVEL_EVENT_PACKET = 0x98; + const TILE_EVENT_PACKET = 0x99; + const ENTITY_EVENT_PACKET = 0x9a; + const REQUEST_CHUNK_PACKET = 0x9b; + const CHUNK_DATA_PACKET = 0x9c; + const PLAYER_EQUIPMENT_PACKET = 0x9d; + // const PLAYER_ARMOR_EQUIPMENT_PACKET = 0xa0; + const INTERACT_PACKET = 0x9e; + const USE_ITEM_PACKET = 0x9f; + // const PLAYER_ACTION_PACKET = 0xa1; + const SET_ENTITY_DATA_PACKET = 0xa0; + const SET_ENTITY_MOTION_PACKET = 0xa1; + //const SET_ENTITY_LINK_PACKET = 0xa?; + const SET_HEALTH_PACKET = 0xa2; + //const SET_SPAWN_POSITION_PACKET = 0xa6; + const ANIMATE_PACKET = 0xa3; + const RESPAWN_PACKET = 0xa4; + const SEND_INVENTORY_PACKET = 0xa5; + const DROP_ITEM_PACKET = 0xa6; + const CONTAINER_OPEN_PACKET = 0xa7; + const CONTAINER_CLOSE_PACKET = 0xa8; + const CONTAINER_SET_SLOT_PACKET = 0xa9; + const CONTAINER_SET_DATA_PACKET = 0xaa; + const CONTAINER_SET_CONTENT_PACKET = 0xab; + const CONTAINER_ACK_PACKET = 0xac; + //const CHAT_PACKET = 0xaf; + //const ADVENTURE_SETTINGS_PACKET = 0xb3; + //const ENTITY_DATA_PACKET = 0xb2; + //const PLAYER_INPUT_PACKET = 0xb9; +} +abstract class ProtocolInfo3{ + + const CURRENT_PROTOCOL_3 = 3; + + 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 = 0x8b; //Maybe exist + const REMOVE_ENTITY_PACKET = 0x8c; + const ADD_ITEM_ENTITY_PACKET = 0x8d; + const TAKE_ITEM_ENTITY_PACKET = 0x8e; + const MOVE_ENTITY_PACKET = 0x8f;//unused ? + + const MOVE_ENTITY_PACKET_POSROT = 0x92; + const MOVE_PLAYER_PACKET = 0x93; + const PLACE_BLOCK_PACKET = 0x94; + const REMOVE_BLOCK_PACKET = 0x95; + const UPDATE_BLOCK_PACKET = 0x96; + //const ADD_PAINTING_PACKET = 0x98; + const EXPLODE_PACKET = 0x97; + const LEVEL_EVENT_PACKET = 0x98; + //const TILE_EVENT_PACKET = 0x99; + const ENTITY_EVENT_PACKET = 0x99;//Change + const REQUEST_CHUNK_PACKET = 0x9a; + const CHUNK_DATA_PACKET = 0x9b;//Change + const PLAYER_EQUIPMENT_PACKET = 0x9c; + // const PLAYER_ARMOR_EQUIPMENT_PACKET = 0xa0; + const INTERACT_PACKET = 0x9d; + const USE_ITEM_PACKET = 0x9e; + // const PLAYER_ACTION_PACKET = 0xa1; + const SET_ENTITY_DATA_PACKET = 0x9f; + // const SET_ENTITY_MOTION_PACKET = 0xa1; + //const SET_ENTITY_LINK_PACKET = 0xa?; + const SET_HEALTH_PACKET = 0xa0; + //const SET_SPAWN_POSITION_PACKET = 0xa6; + const ANIMATE_PACKET = 0xa1; + const RESPAWN_PACKET = 0xa2; + const SEND_INVENTORY_PACKET = 0xa3; + const DROP_ITEM_PACKET = 0xa4; + const CONTAINER_OPEN_PACKET = 0xa7; + const CONTAINER_CLOSE_PACKET = 0xa8; + const CONTAINER_SET_SLOT_PACKET = 0xa9; + const CONTAINER_SET_DATA_PACKET = 0xaa; + const CONTAINER_SET_CONTENT_PACKET = 0xab; + const CONTAINER_ACK_PACKET = 0xac; + //const CHAT_PACKET = 0xaf; + //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 diff --git a/src/network/protocol/packet/AddEntityPacket.php b/src/network/protocol/packet/AddEntityPacket.php index ac2c1a75..8152f2da 100644 --- a/src/network/protocol/packet/AddEntityPacket.php +++ b/src/network/protocol/packet/AddEntityPacket.php @@ -12,6 +12,9 @@ class AddEntityPacket extends RakNetDataPacket{ public $speedZ; public function pid(){ + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::ADD_ENTITY_PACKET; + } return ProtocolInfo::ADD_ENTITY_PACKET; } diff --git a/src/network/protocol/packet/AddItemEntityPacket.php b/src/network/protocol/packet/AddItemEntityPacket.php index b9d66cf3..40a1ea39 100644 --- a/src/network/protocol/packet/AddItemEntityPacket.php +++ b/src/network/protocol/packet/AddItemEntityPacket.php @@ -11,6 +11,9 @@ class AddItemEntityPacket extends RakNetDataPacket{ public $roll; public function pid(){ + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::ADD_ITEM_ENTITY_PACKET; + } return ProtocolInfo::ADD_ITEM_ENTITY_PACKET; } diff --git a/src/network/protocol/packet/AnimatePacket.php b/src/network/protocol/packet/AnimatePacket.php index 160d4300..62fb496e 100644 --- a/src/network/protocol/packet/AnimatePacket.php +++ b/src/network/protocol/packet/AnimatePacket.php @@ -8,7 +8,13 @@ class AnimatePacket extends RakNetDataPacket{ public $eid; public function pid(){ - if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::ANIMATE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::ANIMATE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::ANIMATE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ return ProtocolInfo7::ANIMATE_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::ANIMATE_PACKET; diff --git a/src/network/protocol/packet/ChatPacket.php b/src/network/protocol/packet/ChatPacket.php index d82158cd..0318d1f2 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 < ProtocolInfo9::CURRENT_PROTOCOL_9){ + if($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::CHAT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ return ProtocolInfo7::CHAT_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::CHAT_PACKET; diff --git a/src/network/protocol/packet/ChunkDataPacket.php b/src/network/protocol/packet/ChunkDataPacket.php index b8b70523..ce3d2be6 100644 --- a/src/network/protocol/packet/ChunkDataPacket.php +++ b/src/network/protocol/packet/ChunkDataPacket.php @@ -6,7 +6,13 @@ class ChunkDataPacket extends RakNetDataPacket{ public $data; public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::CHUNK_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::CHUNK_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::CHUNK_DATA_PACKET; + }elseif($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 7060e195..21964d5c 100644 --- a/src/network/protocol/packet/ContainerClosePacket.php +++ b/src/network/protocol/packet/ContainerClosePacket.php @@ -4,7 +4,13 @@ class ContainerClosePacket extends RakNetDataPacket{ public $windowid; public function pid(){ - if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::CONTAINER_CLOSE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::CONTAINER_CLOSE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::CONTAINER_CLOSE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ return ProtocolInfo7::CONTAINER_CLOSE_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::CONTAINER_CLOSE_PACKET; diff --git a/src/network/protocol/packet/ContainerOpenPacket.php b/src/network/protocol/packet/ContainerOpenPacket.php index 13704ed3..3e4ae969 100644 --- a/src/network/protocol/packet/ContainerOpenPacket.php +++ b/src/network/protocol/packet/ContainerOpenPacket.php @@ -9,7 +9,13 @@ class ContainerOpenPacket extends RakNetDataPacket{ public $z; public function pid(){ - if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::CONTAINER_OPEN_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::CONTAINER_OPEN_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::CONTAINER_OPEN_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ return ProtocolInfo7::CONTAINER_OPEN_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::CONTAINER_OPEN_PACKET; diff --git a/src/network/protocol/packet/ContainerSetContentPacket.php b/src/network/protocol/packet/ContainerSetContentPacket.php index 9df71cdc..4e90e7d6 100644 --- a/src/network/protocol/packet/ContainerSetContentPacket.php +++ b/src/network/protocol/packet/ContainerSetContentPacket.php @@ -6,7 +6,13 @@ class ContainerSetContentPacket extends RakNetDataPacket{ public $hotbar = array(); public function pid(){ - if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::CONTAINER_SET_CONTENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::CONTAINER_SET_CONTENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::CONTAINER_SET_CONTENT_PACKET; + }elseif($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; diff --git a/src/network/protocol/packet/ContainerSetDataPacket.php b/src/network/protocol/packet/ContainerSetDataPacket.php index 959e717f..f7ca2180 100644 --- a/src/network/protocol/packet/ContainerSetDataPacket.php +++ b/src/network/protocol/packet/ContainerSetDataPacket.php @@ -6,7 +6,13 @@ class ContainerSetDataPacket extends RakNetDataPacket{ public $value; public function pid(){ - if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::CONTAINER_SET_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::CONTAINER_SET_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::CONTAINER_SET_DATA_PACKET; + }elseif($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; diff --git a/src/network/protocol/packet/ContainerSetSlotPacket.php b/src/network/protocol/packet/ContainerSetSlotPacket.php index 70fb6b39..757d6a3e 100644 --- a/src/network/protocol/packet/ContainerSetSlotPacket.php +++ b/src/network/protocol/packet/ContainerSetSlotPacket.php @@ -6,7 +6,13 @@ class ContainerSetSlotPacket extends RakNetDataPacket{ public $item; public function pid(){ - if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::CONTAINER_SET_SLOT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::CONTAINER_SET_SLOT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::CONTAINER_SET_SLOT_PACKET; + }elseif($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; diff --git a/src/network/protocol/packet/DropItemPacket.php b/src/network/protocol/packet/DropItemPacket.php index d86110cd..d5ff7dbb 100644 --- a/src/network/protocol/packet/DropItemPacket.php +++ b/src/network/protocol/packet/DropItemPacket.php @@ -6,7 +6,13 @@ class DropItemPacket extends RakNetDataPacket{ public $item; public function pid(){ - if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::DROP_ITEM_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::DROP_ITEM_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::DROP_ITEM_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ return ProtocolInfo7::DROP_ITEM_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::DROP_ITEM_PACKET; diff --git a/src/network/protocol/packet/EntityEventPacket.php b/src/network/protocol/packet/EntityEventPacket.php index 2699ea32..a0f4123e 100644 --- a/src/network/protocol/packet/EntityEventPacket.php +++ b/src/network/protocol/packet/EntityEventPacket.php @@ -14,7 +14,13 @@ class EntityEventPacket extends RakNetDataPacket{ } public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::ENTITY_EVENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::ENTITY_EVENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::ENTITY_EVENT_PACKET; + }elseif($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 24765f63..c9d787ab 100644 --- a/src/network/protocol/packet/ExplodePacket.php +++ b/src/network/protocol/packet/ExplodePacket.php @@ -8,7 +8,11 @@ class ExplodePacket extends RakNetDataPacket{ public $records; public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::EXPLODE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::EXPLODE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::EXPLODE_PACKET; } return ProtocolInfo::EXPLODE_PACKET; diff --git a/src/network/protocol/packet/InteractPacket.php b/src/network/protocol/packet/InteractPacket.php index 9d7e9dc8..cb8bf553 100644 --- a/src/network/protocol/packet/InteractPacket.php +++ b/src/network/protocol/packet/InteractPacket.php @@ -9,7 +9,13 @@ class InteractPacket extends RakNetDataPacket{ public $target; public function pid(){ - if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::INTERACT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::INTERACT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::INTERACT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ return ProtocolInfo7::INTERACT_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::INTERACT_PACKET; diff --git a/src/network/protocol/packet/LevelEventPacket.php b/src/network/protocol/packet/LevelEventPacket.php index 86db88be..bfb7e758 100644 --- a/src/network/protocol/packet/LevelEventPacket.php +++ b/src/network/protocol/packet/LevelEventPacket.php @@ -12,7 +12,11 @@ class LevelEventPacket extends RakNetDataPacket{ public $data; public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::LEVEL_EVENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::LEVEL_EVENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::LEVEL_EVENT_PACKET; } return ProtocolInfo::LEVEL_EVENT_PACKET; diff --git a/src/network/protocol/packet/MoveEntityPacket.php b/src/network/protocol/packet/MoveEntityPacket.php index be59077c..8d727ac4 100644 --- a/src/network/protocol/packet/MoveEntityPacket.php +++ b/src/network/protocol/packet/MoveEntityPacket.php @@ -3,6 +3,9 @@ class MoveEntityPacket extends RakNetDataPacket{ public function pid(){ + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::MOVE_ENTITY_PACKET; + } return ProtocolInfo::MOVE_ENTITY_PACKET; } diff --git a/src/network/protocol/packet/MoveEntityPacket_PosRot.php b/src/network/protocol/packet/MoveEntityPacket_PosRot.php index c2143c18..a1a51bf4 100644 --- a/src/network/protocol/packet/MoveEntityPacket_PosRot.php +++ b/src/network/protocol/packet/MoveEntityPacket_PosRot.php @@ -9,6 +9,9 @@ class MoveEntityPacket_PosRot extends RakNetDataPacket{ public $pitch; public function pid(){ + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::MOVE_ENTITY_PACKET_POSROT; + } return ProtocolInfo::MOVE_ENTITY_PACKET_POSROT; } diff --git a/src/network/protocol/packet/MovePlayerPacket.php b/src/network/protocol/packet/MovePlayerPacket.php index 229f2bdf..a3235417 100644 --- a/src/network/protocol/packet/MovePlayerPacket.php +++ b/src/network/protocol/packet/MovePlayerPacket.php @@ -10,7 +10,9 @@ class MovePlayerPacket extends RakNetDataPacket{ public $bodyYaw; public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::MOVE_PLAYER_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::MOVE_PLAYER_PACKET; } return ProtocolInfo::MOVE_PLAYER_PACKET; diff --git a/src/network/protocol/packet/PlaceBlockPacket.php b/src/network/protocol/packet/PlaceBlockPacket.php index 950115ef..ec25c760 100644 --- a/src/network/protocol/packet/PlaceBlockPacket.php +++ b/src/network/protocol/packet/PlaceBlockPacket.php @@ -10,7 +10,9 @@ class PlaceBlockPacket extends RakNetDataPacket{ public $face; public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL) { + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::PLACE_BLOCK_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL) { return ProtocolInfo12::PLACE_BLOCK_PACKET; } return ProtocolInfo::PLACE_BLOCK_PACKET; diff --git a/src/network/protocol/packet/PlayerActionPacket.php b/src/network/protocol/packet/PlayerActionPacket.php index ed52db1b..2ba773f1 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 < ProtocolInfo9::CURRENT_PROTOCOL_9){ + if($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::PLAYER_ACTION_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ return ProtocolInfo7::PLAYER_ACTION_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::PLAYER_ACTION_PACKET; diff --git a/src/network/protocol/packet/PlayerEquipmentPacket.php b/src/network/protocol/packet/PlayerEquipmentPacket.php index d95ef3be..7f261e98 100644 --- a/src/network/protocol/packet/PlayerEquipmentPacket.php +++ b/src/network/protocol/packet/PlayerEquipmentPacket.php @@ -7,7 +7,13 @@ class PlayerEquipmentPacket extends RakNetDataPacket{ public $slot; public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::PLAYER_EQUIPMENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::PLAYER_EQUIPMENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::PLAYER_EQUIPMENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::PLAYER_EQUIPMENT_PACKET; } return ProtocolInfo::PLAYER_EQUIPMENT_PACKET; diff --git a/src/network/protocol/packet/RemoveBlockPacket.php b/src/network/protocol/packet/RemoveBlockPacket.php index b528a236..dfb75b1d 100644 --- a/src/network/protocol/packet/RemoveBlockPacket.php +++ b/src/network/protocol/packet/RemoveBlockPacket.php @@ -7,7 +7,9 @@ class RemoveBlockPacket extends RakNetDataPacket{ public $z; public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::REMOVE_BLOCK_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::REMOVE_BLOCK_PACKET; } return ProtocolInfo::REMOVE_BLOCK_PACKET; diff --git a/src/network/protocol/packet/RemoveEntityPacket.php b/src/network/protocol/packet/RemoveEntityPacket.php index 27d6c42b..2015dd12 100644 --- a/src/network/protocol/packet/RemoveEntityPacket.php +++ b/src/network/protocol/packet/RemoveEntityPacket.php @@ -4,6 +4,9 @@ class RemoveEntityPacket extends RakNetDataPacket{ public $eid; public function pid(){ + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::REMOVE_ENTITY_PACKET; + } return ProtocolInfo::REMOVE_ENTITY_PACKET; } diff --git a/src/network/protocol/packet/RequestChunkPacket.php b/src/network/protocol/packet/RequestChunkPacket.php index 9ba9b543..15acbaeb 100644 --- a/src/network/protocol/packet/RequestChunkPacket.php +++ b/src/network/protocol/packet/RequestChunkPacket.php @@ -5,7 +5,13 @@ class RequestChunkPacket extends RakNetDataPacket{ public $chunkZ; public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::REQUEST_CHUNK_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::REQUEST_CHUNK_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::REQUEST_CHUNK_PACKET; + }elseif($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 2619fc8c..5d110f68 100644 --- a/src/network/protocol/packet/RespawnPacket.php +++ b/src/network/protocol/packet/RespawnPacket.php @@ -7,7 +7,13 @@ class RespawnPacket extends RakNetDataPacket{ public $z; public function pid(){ - if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::RESPAWN_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::RESPAWN_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::RESPAWN_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ return ProtocolInfo7::RESPAWN_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::RESPAWN_PACKET; diff --git a/src/network/protocol/packet/SendInventoryPacket.php b/src/network/protocol/packet/SendInventoryPacket.php index 78410a52..4af1f545 100644 --- a/src/network/protocol/packet/SendInventoryPacket.php +++ b/src/network/protocol/packet/SendInventoryPacket.php @@ -7,7 +7,13 @@ class SendInventoryPacket extends RakNetDataPacket{ public $armor = array(); public function pid(){ - if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::SEND_INVENTORY_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::SEND_INVENTORY_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::SEND_INVENTORY_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ return ProtocolInfo7::SEND_INVENTORY_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::SEND_INVENTORY_PACKET; diff --git a/src/network/protocol/packet/SetEntityDataPacket.php b/src/network/protocol/packet/SetEntityDataPacket.php index 0a320b9b..31c3d91e 100644 --- a/src/network/protocol/packet/SetEntityDataPacket.php +++ b/src/network/protocol/packet/SetEntityDataPacket.php @@ -5,7 +5,13 @@ class SetEntityDataPacket extends RakNetDataPacket{ public $metadata; public function pid(){ - if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::SET_ENTITY_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::SET_ENTITY_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::SET_ENTITY_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ return ProtocolInfo7::SET_ENTITY_DATA_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::SET_ENTITY_DATA_PACKET; diff --git a/src/network/protocol/packet/SetEntityMotionPacket.php b/src/network/protocol/packet/SetEntityMotionPacket.php index 6894927e..49bb6c1b 100644 --- a/src/network/protocol/packet/SetEntityMotionPacket.php +++ b/src/network/protocol/packet/SetEntityMotionPacket.php @@ -7,7 +7,11 @@ class SetEntityMotionPacket extends RakNetDataPacket{ public $speedZ; public function pid(){ - if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::SET_ENTITY_MOTION_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::SET_ENTITY_MOTION_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ return ProtocolInfo7::SET_ENTITY_MOTION_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::SET_ENTITY_MOTION_PACKET; diff --git a/src/network/protocol/packet/SetHealthPacket.php b/src/network/protocol/packet/SetHealthPacket.php index fbdbaaa1..7adb0e94 100644 --- a/src/network/protocol/packet/SetHealthPacket.php +++ b/src/network/protocol/packet/SetHealthPacket.php @@ -4,7 +4,13 @@ class SetHealthPacket extends RakNetDataPacket{ public $health; public function pid(){ - if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::SET_HEALTH_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::SET_HEALTH_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::SET_HEALTH_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ return ProtocolInfo7::SET_HEALTH_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::SET_HEALTH_PACKET; diff --git a/src/network/protocol/packet/TakeItemEntityPacket.php b/src/network/protocol/packet/TakeItemEntityPacket.php index 85221c38..5c733fdc 100644 --- a/src/network/protocol/packet/TakeItemEntityPacket.php +++ b/src/network/protocol/packet/TakeItemEntityPacket.php @@ -5,6 +5,9 @@ class TakeItemEntityPacket extends RakNetDataPacket{ public $eid; public function pid(){ + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::TAKE_ITEM_ENTITY_PACKET; + } return ProtocolInfo::TAKE_ITEM_ENTITY_PACKET; } diff --git a/src/network/protocol/packet/TileEventPacket.php b/src/network/protocol/packet/TileEventPacket.php index 1bb97bdf..34141c72 100644 --- a/src/network/protocol/packet/TileEventPacket.php +++ b/src/network/protocol/packet/TileEventPacket.php @@ -8,7 +8,11 @@ class TileEventPacket extends RakNetDataPacket{ public $case2; public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::TILE_EVENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::TILE_EVENT_PACKET; + }elseif($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 76c92815..2517953f 100644 --- a/src/network/protocol/packet/UpdateBlockPacket.php +++ b/src/network/protocol/packet/UpdateBlockPacket.php @@ -8,7 +8,9 @@ class UpdateBlockPacket extends RakNetDataPacket{ public $meta; public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::UPDATE_BLOCK_PACKET; + }elseif($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 f3f3dd74..8563dc57 100644 --- a/src/network/protocol/packet/UseItemPacket.php +++ b/src/network/protocol/packet/UseItemPacket.php @@ -16,7 +16,13 @@ class UseItemPacket extends RakNetDataPacket{ public $posZ; public function pid(){ - if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::USE_ITEM_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::USE_ITEM_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::USE_ITEM_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ return ProtocolInfo7::USE_ITEM_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::USE_ITEM_PACKET; From aad00314d745464a8df2b55e8d7bd6bee3ce9303 Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Sun, 27 Apr 2025 17:21:27 +0800 Subject: [PATCH 25/39] Fix PlaceBlock Packet handle --- src/Player.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Player.php b/src/Player.php index cabd1ee2..8674d5b8 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2264,7 +2264,7 @@ class Player{ $packet->eid = $this->eid; $data = []; - if ($packet->face >= 0 and $packet->face <= 5) { //Use Block, place + if ($packet->face >= 0 and $packet->face <= 5) { if ($this->entity->inAction === true) { $this->entity->inAction = false; $this->entity->inActionCounter = 0; @@ -2274,7 +2274,7 @@ class Player{ if ($this->blocked === true or ($this->entity->position instanceof Vector3 and $blockVector->distance($this->entity->position) > 10)) { } else { - $this->server->api->block->playerBlockAction($this, $blockVector, $packet->face, $packet->fx, $packet->fy, $packet->fz); + $this->server->api->block->playerBlockAction($this, $blockVector, $packet->face, $packet->x, $packet->y, $packet->z); break; } $target = $this->level->getBlock($blockVector); From f6ca62ff6c656adfdf2f0ebabb946131df43c74e Mon Sep 17 00:00:00 2001 From: Sunch <120295462+Sunch233@users.noreply.github.com> Date: Sun, 27 Apr 2025 19:53:50 +0800 Subject: [PATCH 26/39] typo --- src/API/BlockAPI.php | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index 5e44794d..4c08f29c 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -645,7 +645,10 @@ class BlockAPI{ } } - public static function convertHighItemIdsToOldItemIds(int $protocolId, int $itemId){ + /** + * 方块/物品id转换,防止新版本影响旧版本视觉效果 + */ + public static function convertHighItemIdsToOldItemIds(int $protocolId, int $itemId) : int{ if ($protocolId >= ProtocolInfo::CURRENT_PROTOCOL) { return $itemId; } @@ -653,18 +656,18 @@ class BlockAPI{ $idMap = []; if ($protocolId < ProtocolInfo9::CURRENT_PROTOCOL_9) { $idMap += [ - 87 => 49, - 405 => 336, - 406 => 336, + NETHERRACK => OBSIDIAN, //block + NETHER_BRICK => BRICK, //item + NETHER_QUARTZ => BRICK, //item ]; } if ($protocolId < ProtocolInfo12::CURRENT_PROTOCOL_12) { $idMap += [ - 91 => 103, - 361 => 362, - 400 => 297, - 457 => 295, - 458 => 297, + LIT_PUMPKIN => MELON_BLOCK, //block + PUMPKIN_SEEDS => MELON_SEEDS, //item + PUMPKIN_PIE => BREAD, //item + BEETROOT => BREAD, //item + BEETROOT_SEEDS => SEEDS, //item ]; } From fc5c5625cbb6e8f7920422147fbffddc12947b69 Mon Sep 17 00:00:00 2001 From: Sunch <120295462+Sunch233@users.noreply.github.com> Date: Sun, 27 Apr 2025 20:35:55 +0800 Subject: [PATCH 27/39] Use PHP-CS-fixer to standardize the code style --- .gitignore | 3 +++ .php-cs-fixer.php | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 .php-cs-fixer.php diff --git a/.gitignore b/.gitignore index b95b2968..3b0c39ad 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,9 @@ server.lock *.txt *.properties +# php-cs-fixer +/.php_cs.cache +/.php-cs-fixer.cache ############ ## Windows diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 00000000..307fb27e --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,39 @@ +in(__DIR__ . '/src'); + +return (new PhpCsFixer\Config) + ->setRules([ + 'array_indentation' => true, + 'array_syntax' => [ + 'syntax' => 'short' + ], + 'binary_operator_spaces' => [ + 'default' => 'single_space' + ], + 'blank_line_after_namespace' => true, + 'blank_line_after_opening_tag' => true, + 'cast_spaces' => [ + 'space' => 'single' + ], + 'concat_space' => [ + 'spacing' => 'one' + ], + 'elseif' => true, + 'indentation_type' => true, + 'no_closing_tag' => true, + 'no_empty_phpdoc' => true, + 'no_extra_blank_lines' => true, + 'no_superfluous_phpdoc_tags' => [ + 'allow_mixed' => true, + ], + 'no_trailing_whitespace' => true, + 'no_trailing_whitespace_in_comment' => true, + 'no_whitespace_in_blank_line' => true, + 'unary_operator_spaces' => true + ]) + ->setFinder($finder) + ->setIndent("\t") + ->setLineEnding("\n") + ->setParallelConfig(PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect()); \ No newline at end of file From 5e8e9330e2b9ce8a66bced05c804898d97f8788b Mon Sep 17 00:00:00 2001 From: Sunch <120295462+Sunch233@users.noreply.github.com> Date: Sun, 27 Apr 2025 20:36:24 +0800 Subject: [PATCH 28/39] code style fix --- src/API/AchievementAPI.php | 35 +- src/API/BanAPI.php | 2 +- src/API/BlockAPI.php | 41 +- src/API/ChatAPI.php | 2 - src/API/ConsoleAPI.php | 16 +- src/API/EntityAPI.php | 77 +- src/API/LevelAPI.php | 11 +- src/API/PlayerAPI.php | 18 +- src/API/PluginAPI.php | 21 +- src/API/ServerAPI.php | 4 +- src/API/TileAPI.php | 6 +- src/API/TimeAPI.php | 1 - src/Deprecation.php | 1 - src/Player.php | 1322 +++++++++-------- src/PocketMinecraftServer.php | 379 ++--- src/astarnavigator/TileNavigator.php | 88 +- src/config.php | 1 + src/constants/BlockIDs.php | 1 - src/constants/EntityIDs.php | 8 +- src/dependencies.php | 3 - src/entity/Animal.php | 52 +- src/entity/Breedable.php | 4 +- src/entity/Creature.php | 26 +- src/entity/Entity.php | 391 +++-- src/entity/Living.php | 126 +- src/entity/Monster.php | 19 +- src/entity/Projectile.php | 59 +- src/entity/ai/EntityAI.php | 22 +- src/entity/ai/MobController.php | 121 +- src/entity/ai/path/PathFollower.php | 17 +- src/entity/ai/tasks/TaskAttackPlayer.php | 34 +- src/entity/ai/tasks/TaskBase.php | 20 +- src/entity/ai/tasks/TaskEatTileGoal.php | 6 +- src/entity/ai/tasks/TaskFollowParent.php | 10 +- src/entity/ai/tasks/TaskLookAround.php | 6 +- src/entity/ai/tasks/TaskLookAtPlayer.php | 10 +- src/entity/ai/tasks/TaskMate.php | 23 +- src/entity/ai/tasks/TaskPanic.php | 13 +- src/entity/ai/tasks/TaskRandomWalk.php | 9 +- src/entity/ai/tasks/TaskRangedAttack.php | 31 +- src/entity/ai/tasks/TaskSwimming.php | 6 +- src/entity/ai/tasks/TaskTempt.php | 29 +- src/entity/animal/Chicken.php | 14 +- src/entity/animal/Cow.php | 8 +- src/entity/animal/Pig.php | 23 +- src/entity/animal/Sheep.php | 30 +- src/entity/monster/Creeper.php | 21 +- src/entity/monster/PigZombie.php | 1 + src/entity/monster/Skeleton.php | 21 +- src/entity/monster/Spider.php | 25 +- src/entity/monster/Zombie.php | 23 +- src/entity/object/Arrow.php | 85 +- src/entity/object/FallingSand.php | 16 +- src/entity/object/ItemEntity.php | 87 +- src/entity/object/Minecart.php | 185 ++- src/entity/object/Painting.php | 86 +- src/entity/object/PrimedTNT.php | 26 +- src/entity/object/ThrownEgg.php | 16 +- src/entity/object/ThrownSnowball.php | 3 +- src/entity/object/TripodCamera.php | 6 +- src/entity/registry/EntityRegistry.php | 8 +- src/entity/registry/PropertyEntity.php | 4 +- src/event/EventHandler.php | 2 +- src/event/EventPriority.php | 14 +- src/event/PluginEvent.php | 1 - src/event/ServerEvent.php | 10 +- src/event/server/DataPacketReceiveEvent.php | 20 +- src/event/server/DataPacketSendEvent.php | 20 +- src/event/server/PacketReceiveEvent.php | 17 +- src/event/server/PacketSendEvent.php | 17 +- src/functions.php | 3 - src/installer/Installer.php | 150 +- src/material/Block.php | 307 ++-- src/material/Item.php | 90 +- src/material/Material.php | 1 + src/material/StaticBlock.php | 51 +- src/material/block/DoorBlock.php | 35 +- src/material/block/FallableBlock.php | 4 - src/material/block/GenericBlock.php | 20 +- src/material/block/LiquidBlock.php | 65 +- src/material/block/SolidBlock.php | 2 +- src/material/block/StairBlock.php | 120 +- src/material/block/TransparentBlock.php | 4 +- src/material/block/attachable/LadderBlock.php | 18 +- .../block/attachable/SignPostBlock.php | 14 +- src/material/block/attachable/TorchBlock.php | 20 +- .../block/attachable/TrapdoorBlock.php | 28 +- .../block/attachable/WallSignBlock.php | 4 +- src/material/block/liquid/LavaBlock.php | 7 +- .../block/liquid/LiquidBlockDynamic.php | 72 +- .../block/liquid/LiquidBlockStatic.php | 11 +- src/material/block/liquid/StillLavaBlock.php | 5 +- src/material/block/liquid/StillWaterBlock.php | 3 +- src/material/block/liquid/WaterBlock.php | 7 +- src/material/block/misc/AirBlock.php | 6 +- src/material/block/misc/BedBlock.php | 58 +- src/material/block/misc/FireBlock.php | 25 +- .../block/misc/NetherReactorBlock.php | 186 +-- src/material/block/misc/TNTBlock.php | 6 +- src/material/block/nonfull/CakeBlock.php | 20 +- src/material/block/nonfull/CarpetBlock.php | 6 +- src/material/block/nonfull/ChestBlock.php | 46 +- src/material/block/nonfull/CobwebBlock.php | 10 +- src/material/block/nonfull/FarmlandBlock.php | 17 +- src/material/block/nonfull/FenceBlock.php | 12 +- src/material/block/nonfull/FenceGateBlock.php | 24 +- src/material/block/nonfull/GlassPaneBlock.php | 26 +- src/material/block/nonfull/IronBarsBlock.php | 24 +- src/material/block/nonfull/IronDoorBlock.php | 10 +- .../block/nonfull/PoweredRailBlock.php | 5 +- src/material/block/nonfull/RailBaseBlock.php | 18 +- src/material/block/nonfull/RailBlock.php | 7 +- src/material/block/nonfull/SlabBlock.php | 24 +- src/material/block/nonfull/SnowLayerBlock.php | 14 +- src/material/block/nonfull/StoneWallBlock.php | 22 +- src/material/block/nonfull/WoodDoorBlock.php | 8 +- src/material/block/nonfull/WoodSlabBlock.php | 29 +- .../nonfull/stairs/BirchWoodStairsBlock.php | 6 +- .../block/nonfull/stairs/BrickStairsBlock.php | 2 +- .../nonfull/stairs/CobblestoneStairsBlock.php | 2 +- .../nonfull/stairs/JungleWoodStairsBlock.php | 6 +- .../stairs/NetherBricksStairsBlock.php | 2 +- .../nonfull/stairs/QuartzStairsBlock.php | 2 +- .../nonfull/stairs/SandstoneStairsBlock.php | 2 +- .../nonfull/stairs/SpruceWoodStairsBlock.php | 6 +- .../nonfull/stairs/StoneBrickStairsBlock.php | 2 +- .../block/nonfull/stairs/WoodStairsBlock.php | 6 +- src/material/block/ore/CoalOreBlock.php | 14 +- src/material/block/ore/DiamondOreBlock.php | 12 +- .../block/ore/GlowingRedstoneOreBlock.php | 16 +- src/material/block/ore/GoldOreBlock.php | 10 +- src/material/block/ore/IronOreBlock.php | 10 +- src/material/block/ore/LapisOreBlock.php | 12 +- src/material/block/ore/RedstoneOreBlock.php | 10 +- src/material/block/plant/BeetrootBlock.php | 2 +- .../block/plant/BrownMushroomBlock.php | 6 +- src/material/block/plant/CactusBlock.php | 16 +- src/material/block/plant/CarrotBlock.php | 4 +- src/material/block/plant/CyanFlowerBlock.php | 7 +- src/material/block/plant/DandelionBlock.php | 2 +- src/material/block/plant/DeadBushBlock.php | 12 +- src/material/block/plant/MelonStemBlock.php | 8 +- src/material/block/plant/PotatoBlock.php | 2 +- src/material/block/plant/PumpkinStemBlock.php | 8 +- src/material/block/plant/RedMushroomBlock.php | 4 +- src/material/block/plant/SaplingBlock.php | 16 +- src/material/block/plant/SugarcaneBlock.php | 8 +- src/material/block/plant/TallGrassBlock.php | 20 +- src/material/block/plant/WheatBlock.php | 6 +- src/material/block/solid/BedrockBlock.php | 4 +- src/material/block/solid/BookshelfBlock.php | 2 +- src/material/block/solid/BricksBlock.php | 10 +- .../block/solid/BurningFurnaceBlock.php | 26 +- src/material/block/solid/ClayBlock.php | 6 +- src/material/block/solid/CoalBlock.php | 10 +- src/material/block/solid/CobblestoneBlock.php | 10 +- src/material/block/solid/DiamondBlock.php | 12 +- src/material/block/solid/DirtBlock.php | 50 +- src/material/block/solid/DoubleSlabBlock.php | 16 +- .../block/solid/DoubleWoodSlabBlock.php | 14 +- src/material/block/solid/FurnaceBlock.php | 1 - src/material/block/solid/GlassBlock.php | 6 +- .../block/solid/GlowingObsidianBlock.php | 4 +- src/material/block/solid/GlowstoneBlock.php | 12 +- src/material/block/solid/GoldBlock.php | 10 +- src/material/block/solid/GrassBlock.php | 16 +- src/material/block/solid/GravelBlock.php | 4 +- src/material/block/solid/HayBaleBlock.php | 14 +- src/material/block/solid/IceBlock.php | 22 +- src/material/block/solid/InfoUpdate2Block.php | 2 +- src/material/block/solid/InfoUpdateBlock.php | 2 +- .../block/solid/InvisibleBedrockBlock.php | 4 +- src/material/block/solid/IronBlock.php | 10 +- src/material/block/solid/LapisBlock.php | 10 +- src/material/block/solid/LeavesBlock.php | 24 +- src/material/block/solid/LitPumpkinBlock.php | 14 +- src/material/block/solid/MelonBlock.php | 8 +- src/material/block/solid/MossStoneBlock.php | 10 +- .../block/solid/NetherBricksBlock.php | 10 +- src/material/block/solid/NetherrackBlock.php | 10 +- src/material/block/solid/ObsidianBlock.php | 6 +- src/material/block/solid/PlanksBlock.php | 6 +- src/material/block/solid/PumpkinBlock.php | 8 +- src/material/block/solid/QuartzBlock.php | 14 +- src/material/block/solid/Reserved6Block.php | 2 +- src/material/block/solid/SandBlock.php | 2 +- src/material/block/solid/SandstoneBlock.php | 18 +- src/material/block/solid/SnowBlock.php | 2 +- src/material/block/solid/SoulSandBlock.php | 2 +- src/material/block/solid/SpongeBlock.php | 2 +- src/material/block/solid/StoneBlock.php | 12 +- src/material/block/solid/StoneBricksBlock.php | 16 +- src/material/block/solid/StonecutterBlock.php | 6 +- src/material/block/solid/WoodBlock.php | 18 +- src/material/block/solid/WoolBlock.php | 6 +- src/material/block/solid/WorkbenchBlock.php | 8 +- src/material/item/armor/ArmorItem.php | 10 +- src/material/item/armor/ChainBootsItem.php | 7 +- .../item/armor/ChainChestplateItem.php | 4 +- src/material/item/armor/ChainHelmetItem.php | 4 +- src/material/item/armor/ChainLeggingsItem.php | 4 +- src/material/item/armor/DiamondBootsItem.php | 4 +- .../item/armor/DiamondChestplateItem.php | 4 +- src/material/item/armor/DiamondHelmetItem.php | 4 +- .../item/armor/DiamondLeggingsItem.php | 4 +- src/material/item/armor/GoldenBootsItem.php | 4 +- .../item/armor/GoldenChestplateItem.php | 4 +- src/material/item/armor/GoldenHelmetItem.php | 4 +- .../item/armor/GoldenLeggingsItem.php | 4 +- src/material/item/armor/IronBootsItem.php | 4 +- .../item/armor/IronChestplateItem.php | 4 +- src/material/item/armor/IronHelmetItem.php | 4 +- src/material/item/armor/IronLeggingsItem.php | 4 +- src/material/item/armor/LeatherBootsItem.php | 4 +- src/material/item/armor/LeatherCapItem.php | 4 +- src/material/item/armor/LeatherPantsItem.php | 4 +- src/material/item/armor/LeatherTunicItem.php | 4 +- src/material/item/base/ItemAxe.php | 2 +- src/material/item/base/ItemHoe.php | 2 +- src/material/item/base/ItemPickaxe.php | 2 +- src/material/item/base/ItemTool.php | 6 +- src/material/item/food/CookedPorkchopItem.php | 2 +- src/material/item/food/PotatoItem.php | 2 +- src/material/item/generic/BoneItem.php | 2 +- src/material/item/generic/BucketItem.php | 8 +- src/material/item/generic/DyeItem.php | 4 +- src/material/item/generic/MinecartItem.php | 4 +- src/material/item/generic/PaintingItem.php | 68 +- src/material/item/generic/SpawnEggItem.php | 8 +- src/material/item/tool/CompassItem.php | 2 +- src/material/item/tool/DiamondAxeItem.php | 2 +- src/material/item/tool/DiamondPickaxeItem.php | 2 +- src/material/item/tool/FlintSteelItem.php | 6 +- src/material/item/tool/GoldenPickaxeItem.php | 2 +- src/material/item/tool/IronAxeItem.php | 2 +- src/material/item/tool/IronPickaxeItem.php | 2 +- src/material/item/tool/IronShovelItem.php | 2 +- src/material/item/tool/ShearsItem.php | 2 +- src/material/item/tool/StoneAxeItem.php | 2 +- src/material/item/tool/StonePickaxeItem.php | 2 +- src/material/item/tool/StoneShovelItem.php | 4 +- src/material/item/tool/WoodenAxeItem.php | 2 +- src/material/item/tool/WoodenPickaxeItem.php | 2 +- src/math/AxisAlignedBB.php | 19 +- src/math/Matrix.php | 1 - src/math/Vector3.php | 48 +- src/network/MinecraftInterface.php | 4 +- src/network/RCON.php | 19 +- src/network/UDPSocket.php | 2 +- src/network/protocol/ProtocolInfo.php | 1 - .../protocol/packet/AddEntityPacket.php | 12 +- .../protocol/packet/AddItemEntityPacket.php | 6 +- src/network/protocol/packet/AddMobPacket.php | 6 +- .../protocol/packet/AddPaintingPacket.php | 6 +- .../protocol/packet/AddPlayerPacket.php | 6 +- .../packet/AdventureSettingsPacket.php | 6 +- src/network/protocol/packet/AnimatePacket.php | 8 +- src/network/protocol/packet/ChatPacket.php | 6 +- .../protocol/packet/ChunkDataPacket.php | 6 +- .../protocol/packet/ClientConnectPacket.php | 6 +- .../protocol/packet/ClientHandshakePacket.php | 8 +- .../protocol/packet/ContainerClosePacket.php | 6 +- .../protocol/packet/ContainerOpenPacket.php | 6 +- .../packet/ContainerSetContentPacket.php | 10 +- .../packet/ContainerSetDataPacket.php | 6 +- .../packet/ContainerSetSlotPacket.php | 6 +- .../protocol/packet/DisconnectPacket.php | 8 +- .../protocol/packet/DropItemPacket.php | 6 +- .../protocol/packet/EntityDataPacket.php | 6 +- .../protocol/packet/EntityEventPacket.php | 10 +- src/network/protocol/packet/ExplodePacket.php | 6 +- .../protocol/packet/HurtArmorPacket.php | 6 +- .../protocol/packet/InteractPacket.php | 4 +- .../protocol/packet/LevelEventPacket.php | 10 +- src/network/protocol/packet/LoginPacket.php | 10 +- .../protocol/packet/LoginStatusPacket.php | 8 +- src/network/protocol/packet/MessagePacket.php | 8 +- .../protocol/packet/MoveEntityPacket.php | 4 +- .../packet/MoveEntityPacket_PosRot.php | 6 +- .../protocol/packet/MovePlayerPacket.php | 6 +- src/network/protocol/packet/PingPacket.php | 6 +- .../protocol/packet/PlayerActionPacket.php | 6 +- .../packet/PlayerArmorEquipmentPacket.php | 10 +- .../protocol/packet/PlayerEquipmentPacket.php | 6 +- .../protocol/packet/PlayerInputPacket.php | 5 +- src/network/protocol/packet/PongPacket.php | 6 +- src/network/protocol/packet/ReadyPacket.php | 8 +- .../protocol/packet/RemoveBlockPacket.php | 6 +- .../protocol/packet/RemoveEntityPacket.php | 4 +- .../protocol/packet/RemovePlayerPacket.php | 6 +- .../protocol/packet/RequestChunkPacket.php | 6 +- src/network/protocol/packet/RespawnPacket.php | 6 +- .../protocol/packet/RotateHeadPacket.php | 4 +- .../protocol/packet/SendInventoryPacket.php | 10 +- .../protocol/packet/ServerHandshakePacket.php | 10 +- .../protocol/packet/SetEntityDataPacket.php | 6 +- .../protocol/packet/SetEntityLinkPacket.php | 1 - .../protocol/packet/SetEntityMotionPacket.php | 6 +- .../protocol/packet/SetHealthPacket.php | 6 +- .../packet/SetSpawnPositionPacket.php | 6 +- src/network/protocol/packet/SetTimePacket.php | 8 +- .../protocol/packet/StartGamePacket.php | 8 +- .../protocol/packet/TakeItemEntityPacket.php | 4 +- .../protocol/packet/TileEventPacket.php | 6 +- src/network/protocol/packet/UnknownPacket.php | 8 +- .../protocol/packet/UpdateBlockPacket.php | 6 +- src/network/protocol/packet/UseItemPacket.php | 8 +- src/network/query/QueryHandler.php | 2 +- src/network/raknet/RakNetDataPacket.php | 14 +- src/network/raknet/RakNetInfo.php | 2 +- src/network/raknet/RakNetParser.php | 8 +- src/plugin/DummyPlugin.php | 16 +- src/plugin/OtherPluginRequirement.php | 2 +- src/plugin/Plugin.php | 6 +- src/plugin/phar/PharUtils.php | 13 +- src/pmf/PMF.php | 3 +- src/pmf/PMFLevel.php | 42 +- src/recipes/CraftingRecipes.php | 50 +- src/recipes/FuelData.php | 4 +- src/recipes/SmeltingData.php | 4 +- src/structure/Structure.php | 16 +- src/tests/ServerSuiteTest.php | 2 +- src/utils/AsyncMultipleQueue.php | 4 +- src/utils/Config.php | 23 - src/utils/Container.php | 1 - src/utils/LightUtils.php | 2 +- src/utils/MTRandom.php | 22 +- src/utils/MersenneTwister.php | 24 +- src/utils/RailLogic.php | 277 ++-- src/utils/Random.php | 2 +- src/utils/StopMessageThread.php | 4 +- src/utils/Utils.php | 64 +- src/world/Explosion.php | 37 +- src/world/Level.php | 745 +++++----- src/world/LevelImport.php | 58 +- src/world/MobSpawner.php | 121 +- src/world/MovingObjectPosition.php | 19 +- src/world/PocketChunkParser.php | 86 +- src/world/Position.php | 22 +- src/world/Tile.php | 167 +-- src/world/generator/SuperflatGenerator.php | 5 +- src/world/generator/VoidGenerator.php | 28 +- src/world/generator/WorldGenerator.php | 2 +- src/world/generator/noise/NoiseGenerator.php | 2 +- .../generator/noise/NoiseGeneratorOctaves.php | 30 +- .../generator/noise/NoiseGeneratorPerlin.php | 48 +- .../object/NetherReactorStructure.php | 4 +- .../generator/object/tree/BigTreeObject.php | 1 - .../generator/object/tree/PineTreeObject.php | 1 - .../object/tree/SpruceTreeObject.php | 1 - src/world/generator/vanilla/BiomeSource.php | 31 +- .../generator/vanilla/VanillaGenerator.php | 282 ++-- src/world/generator/vanilla/biome/Biome.php | 17 +- .../vanilla/feature/BirchFeature.php | 6 +- .../generator/vanilla/feature/ClayFeature.php | 23 +- .../generator/vanilla/feature/Feature.php | 6 +- .../vanilla/feature/FlowerFeature.php | 2 +- .../generator/vanilla/feature/OreFeature.php | 70 +- .../generator/vanilla/feature/PineFeature.php | 6 +- .../vanilla/feature/SpruceFeature.php | 7 +- .../generator/vanilla/feature/TreeFeature.php | 45 +- 361 files changed, 4766 insertions(+), 4650 deletions(-) diff --git a/src/API/AchievementAPI.php b/src/API/AchievementAPI.php index beabe308..526da14d 100644 --- a/src/API/AchievementAPI.php +++ b/src/API/AchievementAPI.php @@ -1,7 +1,7 @@ array( "name" => "Taking Inventory", @@ -79,20 +79,19 @@ class AchievementAPI{ "buildSword", ], ], - + ]; - + private $server; - + function __construct(){ $this->server = ServerAPI::request(); } - + /** * Add an achievement * @param string $achievementId * @param string $achievementName - * @param array $requires * @return boolean */ public static function addAchievement($achievementId, $achievementName, array $requires = []){ @@ -105,7 +104,7 @@ class AchievementAPI{ } return false; } - + public static function grantAchievement(Player $player, $achievementId){ if(isset(self::$achievements[$achievementId]) and !self::hasAchievement($player, $achievementId)){ foreach(self::$achievements[$achievementId]["requires"] as $requerimentId){ @@ -123,19 +122,19 @@ class AchievementAPI{ } return false; } - + public static function hasAchievement(Player $player, $achievementId){ if(!isset(self::$achievements[$achievementId]) or !isset($player->achievements)){ $player->achievements = []; return false; } - + if(!isset($player->achievements[$achievementId]) or !$player->achievements[$achievementId]){ return false; } return true; } - + public static function broadcastAchievement(Player $player, $achievementId){ if(isset(self::$achievements[$achievementId])){ $result = ServerAPI::request()->api->dhandle("achievement.broadcast", ["player" => $player, "achievementId" => $achievementId]); @@ -150,13 +149,13 @@ class AchievementAPI{ } return false; } - + public static function removeAchievement(Player $player, $achievementId){ if(self::hasAchievement($player, $achievementId)){ $player->achievements[$achievementId] = false; } } - + public function viewAchievements($cmd, $params, $issuer, $alias) { if(!isset($params[0])){ @@ -171,30 +170,30 @@ class AchievementAPI{ }else{ return false; } - + if(!$data){ return "Player is not found."; } - + if($data instanceof Config){ $achs = $data->get("achievements"); }else{ $achs = $data; } - + if(count($achs) <= 0){ return "Player {$params[0]} unlocked 0 achievements"; } - $output = "Unlocked Achievements(".count($achs)."/".count(self::$achievements)."): "; + $output = "Unlocked Achievements(" . count($achs) . "/" . count(self::$achievements) . "): "; foreach($achs as $achievement => $unlocked){ if($unlocked && isset(self::$achievements[$achievement])){ $info = self::$achievements[$achievement]; $output .= "{$info["name"]}, "; } } - return substr($output, 0, - 2); + return substr($output, 0, -2); } - + public function init(){ $this->server->api->console->register("ach", "", [$this, "viewAchievements"]); $this->server->api->console->alias("getplayerachievements", "ach"); diff --git a/src/API/BanAPI.php b/src/API/BanAPI.php index 7ab128b6..bab63ec3 100644 --- a/src/API/BanAPI.php +++ b/src/API/BanAPI.php @@ -14,7 +14,7 @@ class BanAPI{ private $ops; /** @var Config */ private $bannedIPs; - + public $cmdWhitelist = [];//Command WhiteList function __construct(){ diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index ddd309d9..42f2f34d 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -235,7 +235,7 @@ class BlockAPI{ break; } $itemheld = $issuer->getSlot($issuer->slot); - $output = self::getItem($itemheld->getID(), $itemheld->getMetadata()).""; + $output = self::getItem($itemheld->getID(), $itemheld->getMetadata()) . ""; break; case "setblock": if(!($issuer instanceof Player)){ @@ -259,7 +259,7 @@ class BlockAPI{ $output .= "Usage: /setblock "; break; } - + $level = $issuer->entity->level; $block = self::fromString($args[3])->getBlock(); } @@ -289,13 +289,13 @@ class BlockAPI{ break; }else{ $level->setBlock($pos, $block, true, false, true); - $output .= "Placed $block in ".implode(", ", $coords).", w:".$level->getName(); + $output .= "Placed $block in " . implode(", ", $coords) . ", w:" . $level->getName(); } break; case "give": $player = $this->server->api->player->get($args[0] ?? ""); - - if($player instanceof Player){ + + if($player instanceof Player){ $item = self::fromString($args[1] ?? ""); if(($player->gamemode & 0x01) === 0x01){ return "Player is in creative mode."; @@ -303,18 +303,18 @@ class BlockAPI{ if($item->getID() === 0){ return "You cannot give an air block to a player."; } - + if(!isset($args[2])){ $item->count = $item->getMaxStackSize(); }else{ $item->count = (int) $args[2]; } - + $player->addItem($item->getID(), $item->getMetadata(), $item->count); - $output .= "Giving ".$item->count." of ".$item->getName()." (".$item->getID().":".$item->getMetadata().") to ".$player->username; + $output .= "Giving " . $item->count . " of " . $item->getName() . " (" . $item->getID() . ":" . $item->getMetadata() . ") to " . $player->username; break; }else{ - $item = self::fromString($args[0] ?? ""); + $item = self::fromString($args[0] ?? ""); if(!($issuer instanceof Player)){ return "You cant give an item to a non-player."; } @@ -334,7 +334,7 @@ class BlockAPI{ } $issuer->addItem($item->getID(), $item->getMetadata(), $item->count); - $output .= "Giving ".$item->count." of ".$item->getName()." (".$item->getID().":".$item->getMetadata().") to ".$issuer->username; + $output .= "Giving " . $item->count . " of " . $item->getName() . " (" . $item->getID() . ":" . $item->getMetadata() . ") to " . $issuer->username; break; } } @@ -412,7 +412,6 @@ class BlockAPI{ return $this->cancelAction($target, $player, false); } - if(is_array($drops) && ($player->gamemode & 0x01) === 0x00 and count($drops) > 0){ foreach($drops as $drop){ $this->server->api->entity->drop(new Position($target->x + 0.5, $target->y, $target->z + 0.5, $target->level), BlockAPI::getItem($drop[0] & 0xFFFF, $drop[1] & 0xFFFF, $drop[2])); @@ -456,7 +455,7 @@ class BlockAPI{ return $this->cancelAction($block, $player); } } - + StaticBlock::getBlock($target->getID())::interact($target->level, $target->x, $target->y, $target->z, $player); if($target->isActivable === true){ @@ -501,14 +500,14 @@ class BlockAPI{ $hand->position($block); //$face = -1; } - + if($hand->isSolid === true && ($hand->getID() != BED_BLOCK && $hand->getID() != CARPET)){ $aabb = $hand->getAABB($block->level, $block->x, $block->y, $block->z); $playerbb = $player->entity->boundingBox; - if(($aabb->maxX > $playerbb->minX && $aabb->minX < $playerbb->maxX) && ($aabb->maxY > ($playerbb->minY+0.21) && $aabb->minY < $playerbb->maxY) && ($aabb->maxZ > $playerbb->minZ && $aabb->minZ < $playerbb->maxZ)){ + if(($aabb->maxX > $playerbb->minX && $aabb->minX < $playerbb->maxX) && ($aabb->maxY > ($playerbb->minY + 0.21) && $aabb->minY < $playerbb->maxY) && ($aabb->maxZ > $playerbb->minZ && $aabb->minZ < $playerbb->maxZ)){ return $this->cancelAction($block, $player, false); //Entity in block } - + } if($this->server->api->dhandle("player.block.place", ["player" => $player, "block" => $block, "target" => $target, "item" => $item]) === false){ @@ -537,14 +536,12 @@ class BlockAPI{ return false; } $level = $block::onUpdate($pos->level, $pos->x, $pos->y, $pos->z, $type); - + return $level; } - - public function blockUpdateAround(Position $pos, $type = BLOCK_UPDATE_NORMAL, $delay = false){ - + if($type == BLOCK_UPDATE_NORMAL){ try{ throw new Exception("Deprecated: tried updating $pos using BLOCK_UPDATE_NORMAL."); @@ -554,7 +551,7 @@ class BlockAPI{ } return; } - + if($delay !== false){ $this->scheduleBlockUpdate($pos->getSide(0), $delay, $type); $this->scheduleBlockUpdate($pos->getSide(1), $delay, $type); @@ -576,7 +573,7 @@ class BlockAPI{ if($delay < 0){ return false; } - + $index = $x . "." . $y . "." . $z . "." . $level->getName() . "." . $type; $delay = microtime(true) + $delay * 0.05; if(!isset($this->scheduledUpdates[$index])){ @@ -586,7 +583,7 @@ class BlockAPI{ } return false; } - + public function scheduleBlockUpdate(Position $pos, $delay, $type = BLOCK_UPDATE_SCHEDULED){ $type = (int) $type; if($delay < 0){ diff --git a/src/API/ChatAPI.php b/src/API/ChatAPI.php index 9f2bfac3..09cd9b7a 100644 --- a/src/API/ChatAPI.php +++ b/src/API/ChatAPI.php @@ -103,8 +103,6 @@ class ChatAPI{ /** * @param mixed $owner Can be either Player object or string username. Boolean false for broadcast. * @param string $text - * @param $whitelist - * @param $blacklist */ public function send($owner, $text, $whitelist = false, $blacklist = false){ $message = [ diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index 1efb0943..56f7624b 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -24,12 +24,12 @@ class ConsoleAPI{ $this->register("defaultgamemode", "", [$this, "defaultCommands"]); $this->server->api->console->alias("tps", "status"); - + $this->cmdWhitelist("help"); $this->cmdWhitelist("status"); $this->cmdWhitelist("?"); } - + /** * Whitelists a CMD so everyone can issue it - Even non OPs. * @param string $cmd Command to Whitelist @@ -37,7 +37,7 @@ class ConsoleAPI{ public function cmdWhitelist($cmd){ $this->server->api->ban->cmdWhitelist[strtolower(trim($cmd))] = true; } - + public function register($cmd, $help, $callback){ if(!is_callable($callback)){ return false; @@ -272,19 +272,19 @@ class ConsoleAPI{ } public static function debug($msg){ - console("[DEBUG] ".$msg, true, true, 2); + console("[DEBUG] " . $msg, true, true, 2); } public static function info($msg){ - console("[INFO] ".$msg); + console("[INFO] " . $msg); } public static function notice($msg){ - console("[NOTICE] ".$msg); + console("[NOTICE] " . $msg); } public static function warn($msg){ - console("[WARNING] ".$msg); + console("[WARNING] " . $msg); } public static function error($msg){ - console("[ERROR] ".$msg); + console("[ERROR] " . $msg); } } diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index 6d64f453..b9978dca 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -8,26 +8,25 @@ class EntityAPI{ function __construct(){ $this->entities = []; $this->server = ServerAPI::request(); - + $this->serverSpawnAnimals = $this->server->api->getProperty("spawn-animals"); $this->serverSpawnMobs = $this->server->api->getProperty("spawn-mobs"); } - + public function init(){ $this->server->api->console->register("summon", "", [$this, "commandHandler"]); $this->server->api->console->register("spawnmob", "", [$this, "commandHandler"]); $this->server->api->console->register("despawn", "", [$this, "CommandHandler"]); $this->server->api->console->register("entcnt", "", [$this, "CommandHandler"]); } - - + public function commandHandler($cmd, $args, $issuer, $alias){ $mob = [ "chicken" => 10, "cow" => 11, "pig" => 12, "sheep" => 13, - + "zombie" => 32, "creeper" => 33, "skeleton" => 34, @@ -37,7 +36,7 @@ class EntityAPI{ $output = ""; switch($cmd){ case "entcnt": - return "Total amount of entities: ". count($this->entities); + return "Total amount of entities: " . count($this->entities); case "summon": case "spawnmob": if(!($issuer instanceof Player)){ @@ -46,24 +45,24 @@ class EntityAPI{ if((count($args) < 1) or (count($args) > 3)){ return "Usage: /$cmd [amount] [baby]"; } - + if(is_int($args[0])) $type = $args[0]; else $type = $mob[strtolower($args[0])] ?? 0; if($type < 10 || $type > 36){ return "Unknown mob."; } $mobName = ucfirst(array_flip($mob)[$type]); - + if(((isset($args[1]) && strtolower($args[1]) === "baby") || (isset($args[2]) && strtolower($args[2]) === "baby")) && !Utils::in_range($type, 10, 13)){ return "$mobName cannot be a baby!"; } - + $x = round($issuer->entity->x, 2, PHP_ROUND_HALF_UP); $y = round($issuer->entity->y, 2, PHP_ROUND_HALF_UP); $z = round($issuer->entity->z, 2, PHP_ROUND_HALF_UP); $level = $issuer->entity->level; $pos = new Position($x, $y, $z, $level); - + if(count($args) === 1){//summon $this->summon($pos, ENTITY_MOB, $type); return "$mobName spawned in $x, $y, $z."; @@ -77,12 +76,12 @@ class EntityAPI{ if(isset($args[2]) and strtolower($args[2]) === 'baby'){//summon [amount] [baby] $isBaby = true; } - + for($cnt = $amount; $cnt > 0; --$cnt){ $this->summon($pos, ENTITY_MOB, $type, ["IsBaby" => $isBaby]); } - - return "$amount ".($isBaby ? "Baby " : "")."$mobName(s) spawned in $x, $y, $z."; + + return "$amount " . ($isBaby ? "Baby " : "") . "$mobName(s) spawned in $x, $y, $z."; } elseif(strtolower($args[1]) == "baby"){//summon [baby] $this->summon($pos, ENTITY_MOB, $type, ["IsBaby" => 1]); @@ -94,7 +93,7 @@ class EntityAPI{ if(!isset($args[0])){ return "/despawn "; } - + $despawnclass = 0; $despawntype = 0; switch($args[0]){ @@ -138,12 +137,12 @@ class EntityAPI{ } } } - + return "$cnt entities have been despawned!"; } return $output; } - + public function summon(Position $pos, $class, $type, array $data = []){ $entity = $this->add($pos->level, $class, $type, [ "x" => $pos->x, @@ -152,16 +151,16 @@ class EntityAPI{ ] + $data); $this->spawnToAll($entity); } - + public function getNextEID(){ return $this->eCnt++; } - + public function addRaw(Entity $e){ $eid = $e->eid; $this->entities[$eid] = $e; - $cX = (int)$this->entities[$eid]->x >> 4; - $cZ = (int)$this->entities[$eid]->z >> 4; + $cX = (int) $this->entities[$eid]->x >> 4; + $cZ = (int) $this->entities[$eid]->z >> 4; $e->level->entityListPositioned["$cX $cZ"][$eid] = $eid; $e->level->entityList[$eid] = &$this->entities[$eid]; $this->server->handle("entity.add", $this->entities[$eid]); @@ -178,7 +177,7 @@ class EntityAPI{ } return $this->addRaw($e); } - + public function spawnToAll(Entity $e){ foreach($this->server->api->player->getAll($e->level) as $player){ if($player->eid !== false and $player->eid !== $e->eid and $e->class !== ENTITY_PLAYER and $e instanceof Entity){ @@ -186,18 +185,18 @@ class EntityAPI{ } } } - + public function get($eid){ return $this->entities[$eid] ?? false; } - + public function remove($eid){ if(isset($this->entities[$eid])){ $level = $this->entities[$eid]->level; $this->entities[$eid]->closed = true; if($level instanceof Level){ - $cX = (int)$this->entities[$eid]->x >> 4; - $cZ = (int)$this->entities[$eid]->z >> 4; + $cX = (int) $this->entities[$eid]->x >> 4; + $cZ = (int) $this->entities[$eid]->z >> 4; $index = "$cX $cZ"; unset($level->entityListPositioned[$index][$eid]); if(isset($level->mobSpawner->entityAffectedPlayers[$eid])){ @@ -221,12 +220,12 @@ class EntityAPI{ unset($this->entities[$eid]); } } - + public function getRadius(Position $center, $radius = 15, $class = false){ - $minChunkX = ((int)($center->x - $radius)) >> 4; - $minChunkZ = ((int)($center->z - $radius)) >> 4; - $maxChunkX = ((int)($center->x + $radius)) >> 4; - $maxChunkZ = ((int)($center->z + $radius)) >> 4; + $minChunkX = ((int) ($center->x - $radius)) >> 4; + $minChunkZ = ((int) ($center->z - $radius)) >> 4; + $maxChunkX = ((int) ($center->x + $radius)) >> 4; + $maxChunkZ = ((int) ($center->z + $radius)) >> 4; $ents = []; //TODO also index by chunkY? for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ @@ -244,11 +243,11 @@ class EntityAPI{ } return $ents; } - + public function heal($eid, $heal, $cause){ $this->harm($eid, -$heal, $cause); } - + public function harm($eid, $attack, $cause, $force = false){ $e = $this->get($eid); if($e === false or $e->dead === true){ @@ -256,7 +255,7 @@ class EntityAPI{ } $e->setHealth($e->getHealth() - $attack, $cause, $force); } - + public function dropRawPos(Level $level, $x, $y, $z, $item, $speedX, $speedY, $speedZ){ if($item->getID() === AIR or $item->count <= 0){ return; @@ -272,7 +271,7 @@ class EntityAPI{ "item" => $item, "itemID" => $item->getID() ]; - + if($this->server->api->handle("item.drop", $data) !== false){ for($count = $item->count; $count > 0;){ $item->count = min($item->getMaxStackSize(), $count); @@ -283,7 +282,7 @@ class EntityAPI{ } } } - + public function drop(Position $pos, Item $item, $pickupDelay = 10){ if($item->getID() === AIR or $item->count <= 0){ return; @@ -310,7 +309,7 @@ class EntityAPI{ } } } - + public function spawnAll(Player $player){ foreach($player->level->entityList as $e){ if($e->class !== ENTITY_PLAYER){ @@ -318,18 +317,18 @@ class EntityAPI{ } } } - + public function getAll($level = null){ if($level instanceof Level){ return $level->entityList; } return $this->entities; } - + /** * @deprecated this function doesnt do anything */ public function updateRadius(Position $center, $radius = 15, $class = false){ - + } } diff --git a/src/API/LevelAPI.php b/src/API/LevelAPI.php index 3784363a..8f5edd9f 100644 --- a/src/API/LevelAPI.php +++ b/src/API/LevelAPI.php @@ -40,7 +40,7 @@ class LevelAPI{ console("[ERROR] Could not load level \"" . $name . "\""); return false; } - + if(PocketMinecraftServer::$KEEP_CHUNKS_LOADED){ for($X = 0; $X < 16; ++$X){ for($Z = 0; $Z < 16; ++$Z){ @@ -48,8 +48,7 @@ class LevelAPI{ } } } - - + $entities = new Config($path . "entities.yml", CONFIG_YAML); if(file_exists($path . "tileEntities.yml")){ @rename($path . "tileEntities.yml", $path . "tiles.yml"); @@ -61,13 +60,13 @@ class LevelAPI{ if(!isset($entity["id"])){ break; } - + $entity["x"] = $entity["Pos"][0]; $entity["y"] = $entity["Pos"][1]; $entity["z"] = $entity["Pos"][2]; $entity["yaw"] = $entity["Rotation"][0]; $entity["pitch"] = $entity["Rotation"][1]; - + if($entity["id"] === 64){ //Item Drop $e = $this->server->api->entity->add($this->levels[$name], ENTITY_ITEM, ENTITY_ITEM_TYPE, [ "meta" => $entity["Item"]["Damage"], @@ -105,7 +104,7 @@ class LevelAPI{ foreach($blockUpdates->getAll() as $bupdate){ if($bupdate["type"] !== BLOCK_UPDATE_RANDOM) $this->server->api->block->scheduleBlockUpdate(new Position((int) $bupdate["x"], (int) $bupdate["y"], (int) $bupdate["z"], $this->levels[$name]), (float) $bupdate["delay"], (int) $bupdate["type"]); } - + return true; } /** diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 373f3c70..dcaeeb23 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -19,7 +19,7 @@ class PlayerAPI{ $this->registerCmd("ping"); $this->registerCmd("loc"); $this->registerCmd("hotbar", ""); - + $this->server->api->console->alias("lag", "ping"); $this->server->api->console->alias("gm", "gamemode"); $this->server->api->console->alias("who", "list"); @@ -49,9 +49,9 @@ class PlayerAPI{ if($e->shotByEntity && isset($this->server->api->entity->entities[$e->shooterEID]) && $this->server->api->entity->entities[$e->shooterEID] instanceof Entity){ $message = " was shot by {$this->server->api->entity->entities[$e->shooterEID]->name}"; }else{ - $message = " was shot"; + $message = " was shot"; } - + }else{ $message = " was killed by {$e->name}"; } @@ -104,12 +104,12 @@ class PlayerAPI{ case "hotbar": if(!($issuer instanceof Player)) return "Please run this command in-game."; if(count($args) < 1) return "Slots in hotbar on server: {$issuer->slotCount}"; - + $scrw = $args[0]; if(is_numeric($scrw)){ - $sc = (int)$scrw; + $sc = (int) $scrw; if($sc < 5 || $sc > 9) return "Slot count must be between 5 and 9."; - + $issuer->setSlotCount($sc); $issuer->sendInventory(); return "Changed slot count to $sc"; @@ -235,10 +235,10 @@ class PlayerAPI{ $level = $issuer->entity->level->getName(); $compass = [0 => "X+", 1 => "Z+", 2 => "X-", 3 => "Z-", null => "null"]; $direction = $compass[$issuer->entity->getDirection()]; - + $xChunk = $x >> 4; $zChunk = $z >> 4; - + return "Your coordinates: X: $x ($xChunk), Y: $y, Z: $z ($zChunk), world: $level.\nDirection: $direction"; } return $output; @@ -462,7 +462,7 @@ class PlayerAPI{ unset($player->entity->player); //unset($player->entity); } - + $player = null; unset($player); } diff --git a/src/API/PluginAPI.php b/src/API/PluginAPI.php index 5e98b6ce..6596c783 100644 --- a/src/API/PluginAPI.php +++ b/src/API/PluginAPI.php @@ -139,8 +139,8 @@ class PluginAPI extends stdClass{ if($ext2 === "phar"){ ++$pharCnt; $pluginInfo = []; //TODO: A PluginInfo class? - $filePath = $this->pluginsPath().$file; - $p = new Phar($this->pluginsPath().$file, 0); + $filePath = $this->pluginsPath() . $file; + $p = new Phar($this->pluginsPath() . $file, 0); foreach (new RecursiveIteratorIterator($p) as $file) { $name = $file->getFileName(); $content = file_get_contents($file->getPathName()); @@ -149,23 +149,23 @@ class PluginAPI extends stdClass{ break; } } - console("[INFO] Loading PHAR plugin \"".FORMAT_GREEN.$pluginInfo["name"].FORMAT_RESET."\" ".FORMAT_AQUA.$pluginInfo["version"].FORMAT_RESET." by ".FORMAT_AQUA.$pluginInfo["author"].FORMAT_RESET); - + console("[INFO] Loading PHAR plugin \"" . FORMAT_GREEN . $pluginInfo["name"] . FORMAT_RESET . "\" " . FORMAT_AQUA . $pluginInfo["version"] . FORMAT_RESET . " by " . FORMAT_AQUA . $pluginInfo["author"] . FORMAT_RESET); + $aver = CURRENT_API_VERSION; - if(!in_array((string) CURRENT_API_VERSION, $pluginInfo["api"])){ + if(!in_array((string) CURRENT_API_VERSION, $pluginInfo["api"])){ if(is_array($pluginInfo)) $s = implode(",",$pluginInfo["api"]); else $s = $pluginInfo["api"]; console("[WARNING] API is not the same as Core, might cause bugs({$s} != {$aver})"); } - + $phr = "phar://$filePath/"; - include($phr."/src/".$pluginInfo["classLoader"]); + include($phr . "/src/" . $pluginInfo["classLoader"]); $class = $pluginInfo["CLClass"]; $loader = new $class(); $loader->loadAll($phr); - + $pluginName = PharUtils::getNameSpaceClass($pluginInfo["mainFile"]); - include($phr."/src/".$pluginInfo["mainFile"]); + include($phr . "/src/" . $pluginInfo["mainFile"]); $plugin = new $pluginName($this->server->api, false); if(!($plugin instanceof Plugin)){ console("[ERROR] Plugin \"" . $pluginInfo["name"] . "\" doesn't use the Plugin Interface"); @@ -268,7 +268,7 @@ class PluginAPI extends stdClass{ $names[] = $p[1]["name"]; $versions[] = $p[1]["version"]; } - + foreach($this->plugins as $p){ if($p[0] instanceof OtherPluginRequirement){ foreach($p[0]->getRequiredPlugins() as $required){ @@ -312,4 +312,3 @@ class RequiredPluginEntry{ //Use this as object of requirements array } } - diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index b990a8c6..2fd9ae4c 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -4,7 +4,7 @@ class ServerAPI{ private static $serverRequest = false; public $restart = false; - + /** * @var QueryAPI */ @@ -59,7 +59,7 @@ class ServerAPI{ * @var QueryHandler */ public $query; - + private $asyncCalls = []; private $server; private $config; diff --git a/src/API/TileAPI.php b/src/API/TileAPI.php index a5a48118..c4bbdbe6 100644 --- a/src/API/TileAPI.php +++ b/src/API/TileAPI.php @@ -17,7 +17,7 @@ class TileAPI{ } return false; } - + public function invalidateAll(Level $level, $x, $y, $z){ $x = (int) $x; $y = (int) $y; @@ -31,14 +31,14 @@ class TileAPI{ ++$invcnt; $tl->close(); } - + if($invcnt > 1){ ConsoleAPI::warn("{$level->getName()}: ($x $y $z) has more than 1 tile entity! Invalidated ID {$t["ID"]} (Total invaliated: $invcnt)"); } } } } - + public function get(Position $pos){ $tile = $this->server->query("SELECT * FROM tiles WHERE level = '" . $pos->level->getName() . "' AND x = {$pos->x} AND y = {$pos->y} AND z = {$pos->z};", true); if($tile !== false and $tile !== true and ($tile = $this->getByID($tile["ID"])) !== false){ diff --git a/src/API/TimeAPI.php b/src/API/TimeAPI.php index e8e9f54e..d7e0cd66 100644 --- a/src/API/TimeAPI.php +++ b/src/API/TimeAPI.php @@ -175,5 +175,4 @@ class TimeAPI{ return $this->set("sunset"); } - } diff --git a/src/Deprecation.php b/src/Deprecation.php index 7d8e66cd..c0e07788 100644 --- a/src/Deprecation.php +++ b/src/Deprecation.php @@ -1,6 +1,5 @@ bigCnt = 0; $this->MTU = min($MTU, 1492); $this->server = ServerAPI::request(); @@ -111,42 +113,41 @@ class Player{ $this->slot = 0; $this->hotbar = [0, 1, 2, 3, 4, 5, 6, 7, 8]; $this->packetStats = [0, 0]; - + $this->buffer = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->buffer->data = []; - + $this->entityMovementQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->entityMovementQueue->data = []; - + $this->blockUpdateQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->blockUpdateQueue->data = []; - + //$this->server->schedule(20 * 60, [$this, "clearQueue"], [], true); - + $this->evid[] = $this->server->event("server.close", [$this, "close"]); console("[DEBUG] New Session started with " . $ip . ":" . $port . ". MTU " . $this->MTU . ", Client ID " . $this->clientID, true, true, 2); } - public function __get($name){ - if(isset($this->{$name})){ + public function __get($name) { + if(isset($this->{$name})) { return ($this->{$name}); } return null; } - public function getSpawn(){ + public function getSpawn() { return $this->spawnPosition; } /** - * @param Vector3 $pos * * @return boolean */ - public function sleepOn(Vector3 $pos){ - foreach($this->server->api->player->getAll($this->level) as $p){ - if($p->isSleeping instanceof Vector3){ - if($pos->distance($p->isSleeping) <= 0.1){ + public function sleepOn(Vector3 $pos) { + foreach($this->server->api->player->getAll($this->level) as $p) { + if($p->isSleeping instanceof Vector3) { + if($pos->distance($p->isSleeping) <= 0.1) { return false; } } @@ -155,27 +156,27 @@ class Player{ $this->sleepingTime = 0; $this->teleport(new Position($pos->x + 0.5, $pos->y + 1, $pos->z + 0.5, $this->level), false, false, false, false); //TODO change player hitbox size after he starts sleeping - if($this->entity instanceof Entity){ + if($this->entity instanceof Entity) { $this->entity->updateMetadata(); } - + $spawnPoint = BedBlock::findStandUpPosition($this->level, $pos->x, $pos->y, $pos->z); - if($spawnPoint == null) $spawnPoint = $pos->add(0.5, 1, 0.5); - else{ + if($spawnPoint == null) { + $spawnPoint = $pos->add(0.5, 1, 0.5); + } else { $spawnPoint->x += 0.5; $spawnPoint->z += 0.5; } $this->setSpawn($spawnPoint); return true; } - - public function setSlotCount($cnt){ + + public function setSlotCount($cnt) { $this->slotCount = $cnt; $this->data->set("slot-count", $this->slotCount); } - + /** - * @param Vector3 $pos * @param float|boolean $yaw * @param float|boolean $pitch * @param float|boolean $terrain @@ -183,29 +184,29 @@ class Player{ * * @return boolean */ - public function teleport(Vector3 $pos, $yaw = false, $pitch = false, $terrain = true, $force = true){ - if($this->entity instanceof Entity and $this->level instanceof Level){ + public function teleport(Vector3 $pos, $yaw = false, $pitch = false, $terrain = true, $force = true) { + if($this->entity instanceof Entity and $this->level instanceof Level) { $this->entity->check = false; - if($yaw === false){ + if($yaw === false) { $yaw = $this->entity->yaw; } - if($pitch === false){ + if($pitch === false) { $pitch = $this->entity->pitch; } - if($this->server->api->dhandle("player.teleport", ["player" => $this, "target" => $pos]) === false){ + if($this->server->api->dhandle("player.teleport", ["player" => $this, "target" => $pos]) === false) { $this->entity->check = true; return false; } - if($pos instanceof Position and $pos->level instanceof Level and $pos->level !== $this->level){ - if($this->server->api->dhandle("player.teleport.level", ["player" => $this, "origin" => $this->level, "target" => $pos->level]) === false){ + if($pos instanceof Position and $pos->level instanceof Level and $pos->level !== $this->level) { + if($this->server->api->dhandle("player.teleport.level", ["player" => $this, "origin" => $this->level, "target" => $pos->level]) === false) { $this->entity->check = true; return false; } - foreach($this->level->entityList as $e){ - if($e->eid !== $this->entity->eid){ - if($e->isPlayer()){ + foreach($this->level->entityList as $e) { + if($e->eid !== $this->entity->eid) { + if($e->isPlayer()) { $pk = new MovePlayerPacket(); $pk->eid = $this->entity->eid; $pk->x = -256; @@ -215,7 +216,7 @@ class Player{ $pk->pitch = 0; $pk->bodyYaw = 0; $e->player->dataPacket($pk); - + $pk = new MovePlayerPacket(); $pk->eid = $e->eid; $pk->x = -256; @@ -225,8 +226,8 @@ class Player{ $pk->pitch = 0; $pk->bodyYaw = 0; $this->dataPacket($pk); - - }else{ + + } else { $pk = new MoveEntityPacket_PosRot(); $pk->eid = $e->eid; $pk->x = -256; @@ -250,9 +251,9 @@ class Player{ $this->chunksLoaded = []; $this->server->api->entity->spawnToAll($this->entity); //$this->server->api->entity->spawnAll($this); - foreach($this->level->entityList as $e){ - if($e->eid !== $this->entity->eid){ - if(!$e->isPlayer()){ + foreach($this->level->entityList as $e) { + if($e->eid !== $this->entity->eid) { + if(!$e->isPlayer()) { $pk = new MoveEntityPacket_PosRot(); $pk->eid = $e->eid; $pk->x = $e->x; @@ -264,13 +265,13 @@ class Player{ } } } - + $pk = new SetTimePacket; $pk->time = $this->level->getTime(); $this->dataPacket($pk); $terrain = true; - foreach($this->level->players as $player){ - if($player !== $this and $player->entity instanceof Entity){ + foreach($this->level->players as $player) { + if($player !== $this and $player->entity instanceof Entity) { $pk = new MoveEntityPacket_PosRot; $pk->eid = $player->entity->eid; $pk->x = $player->entity->x; @@ -297,12 +298,12 @@ class Player{ $player->sendArmor($this); } } - + $resyncpos = new Position($pos->x, $pos->y, $pos->z, $pos->level); - }else{ + } else { $resyncpos = new Vector3($pos->x, $pos->y, $pos->z); } - + $this->lastCorrect = $resyncpos; $this->entity->fallY = false; $this->entity->fallStart = false; @@ -311,23 +312,25 @@ class Player{ $this->entity->resetSpeed(); $this->entity->updateLast(); $this->entity->calculateVelocity(); - if($terrain === true){ + if($terrain === true) { $this->orderChunks(); - if($this->spawned) $this->getNextChunk($this->level); + if($this->spawned) { + $this->getNextChunk($this->level); + } } $this->entity->check = true; - if($force === true){ + if($force === true) { $this->forceMovement = $resyncpos; } } - + $pk = new SetEntityMotionPacket(); $pk->eid = 0; $pk->speedX = 0; $pk->speedY = 0; $pk->speedZ = 0; $this->dataPacket($pk); - + $pk = new MovePlayerPacket; $pk->eid = 0; $pk->x = $pos->x; @@ -340,28 +343,26 @@ class Player{ } /** - * @param integer $id - * @param array $data * * @return array|bool */ - public function dataPacket(RakNetDataPacket $packet){ - if($this->connected === false){ + public function dataPacket(RakNetDataPacket $packet) { + if($this->connected === false) { return false; } - if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY){ + if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) { return; } - + $packet->encode(); $len = strlen($packet->buffer) + 1; $MTU = $this->MTU - 24; - if($len > $MTU){ + if($len > $MTU) { return $this->directBigRawPacket($packet); } - - if(($this->bufferLen + $len) >= $MTU){ + + if(($this->bufferLen + $len) >= $MTU) { $this->sendBuffer(); } @@ -372,21 +373,23 @@ class Player{ return []; } - private function directBigRawPacket(RakNetDataPacket $packet){ - if($this->connected === false){ + private function directBigRawPacket(RakNetDataPacket $packet) { + if($this->connected === false) { return false; } $sendtime = microtime(true); $size = $this->MTU - 34; - if($size <= 0) return false; + if($size <= 0) { + return false; + } $buffer = str_split($packet->buffer, $size); $bigCnt = $this->bigCnt; $this->bigCnt = ($this->bigCnt + 1) % 0x10000; $cnts = []; $bufCount = count($buffer); - foreach($buffer as $i => $buf){ + foreach($buffer as $i => $buf) { $cnts[] = $count = $this->counter[0]++; $pk = new UnknownPacket; @@ -409,16 +412,16 @@ class Player{ return $cnts; } - public function send(RakNetPacket $packet){ - if($this->connected === true){ + public function send(RakNetPacket $packet) { + if($this->connected === true) { $packet->ip = $this->ip; $packet->port = $this->port; $this->bandwidthRaw += $this->server->send($packet); } } - public function sendBuffer(){ - if($this->bufferLen > 0 and $this->buffer instanceof RakNetPacket){ + public function sendBuffer() { + if($this->bufferLen > 0 and $this->buffer instanceof RakNetPacket) { $this->buffer->seqNumber = $this->counter[0]++; $this->recoveryQueue[$this->buffer->seqNumber] = $this->buffer; $this->buffer->sendtime = microtime(true); @@ -435,20 +438,19 @@ class Player{ * * @return Item */ - public function getSlot($slot){ - return $this->inventory[(int)$slot] ?? BlockAPI::getItem(AIR, 0, 0); + public function getSlot($slot) { + return $this->inventory[(int) $slot] ?? BlockAPI::getItem(AIR, 0, 0); } /** * @param integer $slot - * @param Item $item * @param boolean $send * * @return boolean */ - public function setSlot($slot, Item $item, $send = true){ + public function setSlot($slot, Item $item, $send = true) { $this->inventory[(int) $slot] = $item; - if($send === true){ + if($send === true) { $this->sendInventorySlot((int) $slot); } return true; @@ -457,92 +459,92 @@ class Player{ /** * @param Player|string|boolean|void $player */ - public function sendArmor($player = false){ + public function sendArmor($player = false) { $data = [ "player" => $this, "eid" => $this->eid, "slots" => [] ]; - for($i = 0; $i < 4; ++$i){ - if(isset($this->armor[$i]) and ($this->armor[$i] instanceof Item) and $this->armor[$i]->getID() > AIR){ + for($i = 0; $i < 4; ++$i) { + if(isset($this->armor[$i]) and ($this->armor[$i] instanceof Item) and $this->armor[$i]->getID() > AIR) { $data["slots"][$i] = $this->armor[$i]->getID() !== AIR ? $this->armor[$i]->getID() - 256 : 0; - }else{ + } else { $this->armor[$i] = BlockAPI::getItem(AIR, 0, 0); $data["slots"][$i] = 255; } } - if($player instanceof Player){ - if($player === $this){ + if($player instanceof Player) { + if($player === $this) { $pk = new ContainerSetContentPacket; $pk->windowid = 0x78; //Armor window id $pk->slots = $this->armor; $this->dataPacket($pk); - }else{ + } else { $pk = new PlayerArmorEquipmentPacket; $pk->eid = $this->eid; $pk->slots = $data["slots"]; $player->dataPacket($pk); } - }else{ + } else { $this->server->api->dhandle("player.armor", $data); } } public $lastOrderX = 0; public $lastOrderZ = 0; - - public function orderChunks(){ - if(!($this->entity instanceof Entity) or $this->connected === false){ + + public function orderChunks() { + if(!($this->entity instanceof Entity) or $this->connected === false) { return false; } - $X = ((int)$this->entity->x) >> 4; - $Z = ((int)$this->entity->z) >> 4; + $X = ((int) $this->entity->x) >> 4; + $Z = ((int) $this->entity->z) >> 4; $this->chunksOrder = []; $this->lastOrderX = $X; $this->lastOrderZ = $Z; - if(self::$smallChunks){ - $Y = ((int)$this->entity->y) >> 4; + if(self::$smallChunks) { + $Y = ((int) $this->entity->y) >> 4; $v = new Vector3($X, $Y, $Z); - for($x = 0; $x < 16; ++$x){ - for($z = 0; $z < 16; ++$z){ - for($y = 0; $y < 8; ++$y){ + for($x = 0; $x < 16; ++$x) { + for($z = 0; $z < 16; ++$z) { + for($y = 0; $y < 8; ++$y) { $dist = $v->distance(new Vector3($x, $y, $z)); $d = $x . ":" . $y . ":" . $z; - if(!isset($this->chunksLoaded[$d])){ + if(!isset($this->chunksLoaded[$d])) { $this->chunksOrder[$d] = $dist; } } } } - }else{ + } else { $v = new Vector2($X, $Z); - for($x = 0; $x < 16; ++$x){ - for($z = 0; $z < 16; ++$z){ + for($x = 0; $x < 16; ++$x) { + for($z = 0; $z < 16; ++$z) { $dist = $v->distance(new Vector2($x, $z)); - for($y = 0; $y < 8; ++$y){ + for($y = 0; $y < 8; ++$y) { $d = $x . ":" . $y . ":" . $z; - if(!isset($this->chunksLoaded[$d])){ + if(!isset($this->chunksLoaded[$d])) { $this->chunksOrder[$d] = $dist; } } } } } - + asort($this->chunksOrder); } - - public function loaddAllChunks(){ - for($x = 0; $x < 16; $x++){ - for($z = 0; $z < 16; $z++){ + + public function loaddAllChunks() { + for($x = 0; $x < 16; $x++) { + for($z = 0; $z < 16; $z++) { $this->useChunk($x, $z); } } } - - public function useChunk($X, $Z){ + + public function useChunk($X, $Z) { $Yndex = 0; - for($iY = 0; $iY < 8; ++$iY){ - if(isset($this->chunksOrder["$X:$iY:$Z"])){ + for($iY = 0; $iY < 8; ++$iY) { + if(isset($this->chunksOrder["$X:$iY:$Z"])) { unset($this->chunksOrder["$X:$iY:$Z"]); $this->chunksLoaded["$X:$iY:$Z"] = true; $Yndex |= 1 << $iY; @@ -552,7 +554,7 @@ class Player{ $maxX = (($X << 4) + 15); $minZ = ($Z << 4); $maxZ = (($Z << 4) + 15); - + $pk = new ChunkDataPacket; $pk->chunkX = $X; $pk->chunkZ = $Z; @@ -561,44 +563,44 @@ class Player{ $tiles = $this->server->query("SELECT ID FROM tiles WHERE spawnable = 1 AND level = '{$this->level->getName()}' AND x >= $minX AND x <= $maxX AND z >= $minZ AND z <= $maxZ;"); $this->lastChunk = false; - if($tiles !== false and $tiles !== true){ - while(($tile = $tiles->fetchArray(SQLITE3_ASSOC)) !== false){ + if($tiles !== false and $tiles !== true) { + while(($tile = $tiles->fetchArray(SQLITE3_ASSOC)) !== false) { $tile = $this->server->api->tile->getByID($tile["ID"]); - if($tile instanceof Tile){ + if($tile instanceof Tile) { $tile->spawn($this); } } } - - if($cnt === false){ + + if($cnt === false) { return false; } } - - public function getNextChunk($world){ - if($this->connected === false or $world != $this->level){ + + public function getNextChunk($world) { + if($this->connected === false or $world != $this->level) { return false; } - foreach($this->chunkCount as $count => $t){ - if(isset($this->recoveryQueue[$count]) or isset($this->resendQueue[$count])){ + foreach($this->chunkCount as $count => $t) { + if(isset($this->recoveryQueue[$count]) or isset($this->resendQueue[$count])) { $this->server->schedule(MAX_CHUNK_RATE, [$this, "getNextChunk"], $world); return; - }else{ + } else { unset($this->chunkCount[$count]); } } - if(is_array($this->lastChunk)){ + if(is_array($this->lastChunk)) { $minX = $this->lastChunk[0]; $maxX = $this->lastChunk[0] + 15; $minZ = $this->lastChunk[1]; $maxZ = $this->lastChunk[1] + 15; $tiles = $this->server->query("SELECT ID, x, y, z FROM tiles WHERE level = '{$this->level->getName()}' AND x >= $minX AND x <= $maxX AND z >= $minZ AND z <= $maxZ;"); $this->lastChunk = false; - if($tiles !== false and $tiles !== true){ - while(($tile = $tiles->fetchArray(SQLITE3_ASSOC)) !== false){ + if($tiles !== false and $tiles !== true) { + while(($tile = $tiles->fetchArray(SQLITE3_ASSOC)) !== false) { $tile = $this->server->api->tile->getByID($tile["ID"]); - if($tile instanceof Tile){ + if($tile instanceof Tile) { $tile->spawn($this); } } @@ -607,7 +609,7 @@ class Player{ $c = key($this->chunksOrder); $d = $c != null ? $this->chunksOrder[$c] : null; - if($c === null or $d === null){ + if($c === null or $d === null) { $this->server->schedule(40, [$this, "getNextChunk"], $world); return false; } @@ -623,8 +625,8 @@ class Player{ $y = $Y << 4; $this->level->useChunk($X, $Z, $this); $Yndex = 1 << $Y; - for($iY = 0; $iY < 8; ++$iY){ - if(isset($this->chunksOrder["$X:$iY:$Z"])){ + for($iY = 0; $iY < 8; ++$iY) { + if(isset($this->chunksOrder["$X:$iY:$Z"])) { unset($this->chunksOrder["$X:$iY:$Z"]); $this->chunksLoaded["$X:$iY:$Z"] = true; $Yndex |= 1 << $iY; @@ -635,28 +637,25 @@ class Player{ $pk->chunkZ = $Z; $pk->data = $this->level->getOrderedChunk($X, $Z, $Yndex); $cnt = $this->dataPacket($pk); - if($cnt === false){ + if($cnt === false) { return false; } $this->chunkCount = []; - foreach($cnt as $i => $count){ + foreach($cnt as $i => $count) { $this->chunkCount[$count] = true; } $this->lastChunk = [$x, $z]; - if($this->lastOrderX != ($this->entity->x >> 4) || $this->lastOrderZ != ($this->entity->z >> 4)){ + if($this->lastOrderX != ($this->entity->x >> 4) || $this->lastOrderZ != ($this->entity->z >> 4)) { $this->orderChunks(); } $this->server->schedule(MAX_CHUNK_RATE, [$this, "getNextChunk"], $world); } - /** - * @param Vector3 $pos - */ - public function setSpawn(Vector3 $pos){ - if(!($pos instanceof Position)){ + public function setSpawn(Vector3 $pos) { + if(!($pos instanceof Position)) { $level = $this->entity->level; - }else{ + } else { $level = $pos->level; } $this->spawnPosition = new Position($pos->x, $pos->y, $pos->z, $level); @@ -667,17 +666,17 @@ class Player{ $this->dataPacket($pk); } - public function sendInventorySlot($s){ + public function sendInventorySlot($s) { $this->sendInventory(); return true; } - public function sendInventory(){ - if(($this->gamemode & 0x01) === CREATIVE){ + public function sendInventory() { + if(($this->gamemode & 0x01) === CREATIVE) { return; } $hotbar = []; - foreach($this->hotbar as $slot){ + foreach($this->hotbar as $slot) { $hotbar[] = $slot <= -1 ? -1 : $slot + 9; } $pk = new ContainerSetContentPacket; @@ -687,32 +686,29 @@ class Player{ $this->dataPacket($pk); } /** - * @param $type - * @param $damage - * @param $count * * @return boolean */ - public function hasSpace($type, $damage, $count){ + public function hasSpace($type, $damage, $count) { $inv = $this->inventory; - while($count > 0){ + while($count > 0) { $add = 0; - foreach($inv as $s => $item){ - if($item->getID() === AIR){ + foreach($inv as $s => $item) { + if($item->getID() === AIR) { $add = min($item->getMaxStackSize(), $count); $inv[$s] = BlockAPI::getItem($type, $damage, $add); break; - }elseif($item->getID() === $type and $item->getMetadata() === $damage){ + } elseif($item->getID() === $type and $item->getMetadata() === $damage) { $add = min($item->getMaxStackSize() - $item->count, $count); - - if($add <= 0){ + + if($add <= 0) { continue; } $inv[$s] = BlockAPI::getItem($type, $damage, $item->count + $add); break; } } - if($add <= 0){ + if($add <= 0) { return false; } $count -= $add; @@ -725,13 +721,13 @@ class Player{ * * @return Item */ - public function getArmor($slot){ - return $this->armor[(int)$slot] ?? BlockAPI::getItem(AIR, 0, 0); + public function getArmor($slot) { + return $this->armor[(int) $slot] ?? BlockAPI::getItem(AIR, 0, 0); } - public function setArmor($slot, Item $armor, $send = true){ + public function setArmor($slot, Item $armor, $send = true) { $this->armor[(int) $slot] = $armor; - if($send === true){ + if($send === true) { $this->sendArmor($this); } return true; @@ -741,8 +737,8 @@ class Player{ * @param mixed $data * @param string $event */ - public function eventHandler($data, $event){ - switch($event){ + public function eventHandler($data, $event) { + switch($event) { case "entity.link": $pk = new SetEntityLinkPacket(); $pk->rider = $data["rider"] == $this->entity->eid ? 0 : $data["rider"]; @@ -751,10 +747,10 @@ class Player{ $this->dataPacket($pk); break; case "tile.update": - if($data->level === $this->level){ - if($data->class === TILE_FURNACE){ - foreach($this->windows as $id => $w){ - if($w === $data){ + if($data->level === $this->level) { + if($data->class === TILE_FURNACE) { + foreach($this->windows as $id => $w) { + if($w === $data) { $pk = new ContainerSetDataPacket; $pk->windowid = $id; $pk->property = 0; //Smelting @@ -772,9 +768,9 @@ class Player{ } break; case "tile.container.slot": - if($data["tile"]->level === $this->level){ - foreach($this->windows as $id => $w){ - if($w === $data["tile"]){ + if($data["tile"]->level === $this->level) { + foreach($this->windows as $id => $w) { + if($w === $data["tile"]) { $pk = new ContainerSetSlotPacket; $pk->windowid = $id; $pk->slot = $data["slot"] + ($data["offset"] ?? 0); @@ -785,8 +781,8 @@ class Player{ } break; case "player.armor": - if($data["player"]->level === $this->level){ - if($data["eid"] === $this->eid){ + if($data["player"]->level === $this->level) { + if($data["eid"] === $this->eid) { $this->sendArmor($this); break; } @@ -797,16 +793,16 @@ class Player{ } break; case "player.pickup": - if($data["eid"] === $this->eid){ + if($data["eid"] === $this->eid) { $data["eid"] = 0; $pk = new TakeItemEntityPacket; $pk->eid = 0; $pk->target = $data["entity"]->eid; $this->dataPacket($pk); - if(($this->gamemode & 0x01) === 0x00){ + if(($this->gamemode & 0x01) === 0x00) { $this->addItem($data["entity"]->itemID, $data["entity"]->meta, $data["entity"]->stack, false); } - switch($data["entity"]->itemID){ + switch($data["entity"]->itemID) { case WOOD: AchievementAPI::grantAchievement($this, "mineWood"); break; @@ -817,7 +813,7 @@ class Player{ AchievementAPI::grantAchievement($this, "leather"); break; } - }elseif($data["entity"]->level === $this->level){ + } elseif($data["entity"]->level === $this->level) { $pk = new TakeItemEntityPacket; $pk->eid = $data["eid"]; $pk->target = $data["entity"]->eid; @@ -825,7 +821,7 @@ class Player{ } break; case "player.equipment.change": - if($data["eid"] === $this->eid or $data["player"]->level !== $this->level){ + if($data["eid"] === $this->eid or $data["player"]->level !== $this->level) { break; } $data["slot"] = 0; @@ -839,10 +835,10 @@ class Player{ break; case "entity.motion": - if($data->eid === $this->eid or $data->level !== $this->level){ + if($data->eid === $this->eid or $data->level !== $this->level) { break; } - if(($data->speedX === 0 && $data->speedY === 0 && $data->speedZ === 0) || ($data->speedX === $data->lastSpeedX && $data->speedY === $data->lastSpeedY && $data->lastSpeedZ === $data->speedZ)){ + if(($data->speedX === 0 && $data->speedY === 0 && $data->speedZ === 0) || ($data->speedX === $data->lastSpeedX && $data->speedY === $data->lastSpeedY && $data->lastSpeedZ === $data->speedZ)) { break; } $pk = new SetEntityMotionPacket; @@ -860,12 +856,12 @@ class Player{ $this->dataPacket($pk); break; case "entity.metadata": - if($data->eid === $this->eid){ + if($data->eid === $this->eid) { $eid = 0; - }else{ + } else { $eid = $data->eid; } - if($data->level === $this->level){ + if($data->level === $this->level) { $pk = new SetEntityDataPacket; $pk->eid = $eid; $pk->metadata = $data->getMetadata(); @@ -873,12 +869,12 @@ class Player{ } break; case "entity.event": - if($data["entity"]->eid === $this->eid){ + if($data["entity"]->eid === $this->eid) { $eid = 0; - }else{ + } else { $eid = $data["entity"]->eid; } - if($data["entity"]->level === $this->level){ + if($data["entity"]->level === $this->level) { $pk = new EntityEventPacket; $pk->eid = $eid; $pk->event = $data["event"]; @@ -886,14 +882,14 @@ class Player{ } break; case "server.chat": - if(($data instanceof Container) === true){ - if(!$data->check($this->username) and !$data->check($this->iusername)){ + if(($data instanceof Container) === true) { + if(!$data->check($this->username) and !$data->check($this->iusername)) { return; - }else{ + } else { $message = $data->get(); $this->sendChat(preg_replace('/\x1b\[[0-9;]*m/', "", $message["message"]), $message["player"]); //Remove ANSI codes from chat } - }else{ + } else { $message = (string) $data; $this->sendChat(preg_replace('/\x1b\[[0-9;]*m/', "", (string) $data)); //Remove ANSI codes from chat } @@ -902,40 +898,48 @@ class Player{ } /** - * @param $type - * @param $damage * @param integer $count * @param boolean $send * * @return boolean */ - public function addItem($type, $damage, $count, $send = true){ - - foreach($this->inventory as $s => $item){ //force check the inventory for non-full stacks of this item first - if($item->getID() === $type and $item->getMetadata() === $damage){ + public function addItem($type, $damage, $count, $send = true) { + + foreach($this->inventory as $s => $item) { //force check the inventory for non-full stacks of this item first + if($item->getID() === $type and $item->getMetadata() === $damage) { $add = min($item->getMaxStackSize() - $item->count, $count); - - if($add <= 0){ + + if($add <= 0) { continue; } $item->count += $add; - if($send) $this->sendInventorySlot($s); - + if($send) { + $this->sendInventorySlot($s); + } + $count -= $add; - if($count <= 0) return true; + if($count <= 0) { + return true; + } } } $toadd = BlockAPI::getItem($type, $damage, $count); - foreach($this->inventory as $s => $item){ - if($item->getID() === AIR){ + foreach($this->inventory as $s => $item) { + if($item->getID() === AIR) { $add = min($toadd->getMaxStackSize(), $count); $this->inventory[$s] = BlockAPI::getItem($type, $damage, $add); - if($send) $this->sendInventorySlot($s); + if($send) { + $this->sendInventorySlot($s); + } $count -= $add; - if($count <= 0) return true; + if($count <= 0) { + return true; + } } } - if($count <= 0) return true; + if($count <= 0) { + return true; + } return false; } @@ -943,18 +947,18 @@ class Player{ * @param string $message * @param string $author */ - public function sendChat($message, $author = ""){ + public function sendChat($message, $author = "") { $mes = explode("\n", $message); - foreach($mes as $m){ - if(preg_match_all('#@([@A-Za-z_]{1,})#', $m, $matches, PREG_OFFSET_CAPTURE) > 0){ + foreach($mes as $m) { + if(preg_match_all('#@([@A-Za-z_]{1,})#', $m, $matches, PREG_OFFSET_CAPTURE) > 0) { $offsetshift = 0; - foreach($matches[1] as $selector){ - if($selector[0][0] === "@"){ //Escape! + foreach($matches[1] as $selector) { + if($selector[0][0] === "@") { //Escape! $m = substr_replace($m, $selector[0], $selector[1] + $offsetshift - 1, strlen($selector[0]) + 1); --$offsetshift; continue; } - switch(strtolower($selector[0])){ + switch(strtolower($selector[0])) { case "player": case "username": $m = substr_replace($m, $this->username, $selector[1] + $offsetshift - 1, strlen($selector[0]) + 1); @@ -964,7 +968,7 @@ class Player{ } } - if($m !== ""){ + if($m !== "") { $pk = new MessagePacket; $pk->source = ($author instanceof Player) ? $author->username : $author; $pk->message = TextFormat::clean($m); //Colors not implemented :( @@ -972,18 +976,18 @@ class Player{ } } } - public function makeInvisibleForOnePlayer(Player $player){ + public function makeInvisibleForOnePlayer(Player $player) { $pk = new RemoveEntityPacket; $pk->eid = $this->entity->eid; $player->dataPacket($pk); } - public function makeInvisibleForAllPlayers(){ + public function makeInvisibleForAllPlayers() { $pk = new RemoveEntityPacket; $pk->eid = $this->entity->eid; $this->server->api->player->broadcastPacket($this->server->api->player->getAll($this->level), $pk); } - public function getGamemode(){ - switch($this->gamemode){ + public function getGamemode() { + switch($this->gamemode) { case SURVIVAL: return "survival"; case CREATIVE: @@ -994,31 +998,33 @@ class Player{ return "view"; } } - - public function checkSpawnPosition(){ - if($this->server->api->dhandle("player.checkspawnpos", ["player" => $this]) === false) return; + + public function checkSpawnPosition() { + if($this->server->api->dhandle("player.checkspawnpos", ["player" => $this]) === false) { + return; + } $level = $this->spawnPosition->level; - if(!isset($this->server->api->level->levels[$level->getName()])){ + if(!isset($this->server->api->level->levels[$level->getName()])) { ConsoleAPI::warn("Level to respawn {$this->iusername} was unloaded, changing spawnpoint to default."); $level = $this->server->api->level->getDefault(); $this->spawnPosition = $level->getSpawn(); - }else{ - $x0 = floor($this->spawnPosition->x - $this->entity->width/2); - $x1 = floor($this->spawnPosition->x + $this->entity->width/2 + 1); + } else { + $x0 = floor($this->spawnPosition->x - $this->entity->width / 2); + $x1 = floor($this->spawnPosition->x + $this->entity->width / 2 + 1); $y0 = floor($this->spawnPosition->y); $y1 = floor($this->spawnPosition->y + $this->entity->height + 1); - $z0 = floor($this->spawnPosition->z - $this->entity->width/2); - $z1 = floor($this->spawnPosition->z + $this->entity->width/2 + 1); - + $z0 = floor($this->spawnPosition->z - $this->entity->width / 2); + $z1 = floor($this->spawnPosition->z + $this->entity->width / 2 + 1); + for($x = $x0; $x < $x1; ++$x) { for($z = $z0; $z < $z1; ++$z) { for($y = $y0; $y < $y1; ++$y) { $bid = $this->entity->level->level->getBlockID($x, $y, $z); - if($bid > 0 && StaticBlock::getIsSolid($bid)){ + if($bid > 0 && StaticBlock::getIsSolid($bid)) { $blockBounds = StaticBlock::$prealloc[$bid]::getCollisionBoundingBoxes($this->entity->level, $x, $y, $z, $this->entity); - - foreach($blockBounds as $blockBound){ - if($this->entity->boundingBox->intersectsWith($blockBound)){ + + foreach($blockBounds as $blockBound) { + if($this->entity->boundingBox->intersectsWith($blockBound)) { $this->sendChat("Your spawn positon is obstructed."); goto reset_spawn_pos; } @@ -1032,61 +1038,61 @@ class Player{ $this->spawnPosition = $this->server->api->level->getDefault()->getSpawn(); } } - - public function setGamemode($gm){ - if($gm < 0 or $gm > 3 or $this->gamemode === $gm){ + + public function setGamemode($gm) { + if($gm < 0 or $gm > 3 or $this->gamemode === $gm) { return false; } - if($this->server->api->dhandle("player.gamemode.change", ["player" => $this, "gamemode" => $gm]) === false){ + if($this->server->api->dhandle("player.gamemode.change", ["player" => $this, "gamemode" => $gm]) === false) { return false; } $inv = &$this->inventory; - if($gm === VIEW){ + if($gm === VIEW) { $this->armor = []; $this->sendArmor(); } - if(($this->gamemode & 0x01) === ($gm & 0x01)){ - if(($gm & 0x01) === 0x01 and ($gm & 0x02) === 0x02){ + if(($this->gamemode & 0x01) === ($gm & 0x01)) { + if(($gm & 0x01) === 0x01 and ($gm & 0x02) === 0x02) { $inv = []; - foreach(BlockAPI::$creative as $item){ + foreach(BlockAPI::$creative as $item) { $inv[] = BlockAPI::getItem(0, 0, 1); } - }elseif(($gm & 0x01) === 0x01){ + } elseif(($gm & 0x01) === 0x01) { $inv = []; - foreach(BlockAPI::$creative as $item){ + foreach(BlockAPI::$creative as $item) { $inv[] = BlockAPI::getItem($item[0], $item[1], 1); } } $this->gamemode = $gm; $this->sendChat("Your gamemode has been changed to " . $this->getGamemode() . ".\n"); - }else{ - foreach($this->inventory as $slot => $item){ + } else { + foreach($this->inventory as $slot => $item) { $inv[$slot] = BlockAPI::getItem(AIR, 0, 0); } $this->hotbar = [0, 1, 2, 3, 4, 5, 6, 7, 8]; $this->lastCorrect = $this->entity->copy(); - $this->blocked = true; + $this->blocked = true; $this->gamemode = $gm; $this->sendChat("Your gamemode has been changed to " . $this->getGamemode() . ", you've to do a forced reconnect.\n"); $this->server->schedule(30, [$this, "close"], "gamemode change", false, true); //Forces a kick } - - if($this->gamemode === SPECTATOR){ + + if($this->gamemode === SPECTATOR) { $this->makeInvisibleForAllPlayers(); } - if($this->gamemode === CREATIVE){ + if($this->gamemode === CREATIVE) { $this->server->api->player->spawnToAllPlayers($this); } - + $this->inventory = $inv; $this->sendSettings(); $this->sendInventory(); return true; } - public function sendSettings($nametags = true){ + public function sendSettings($nametags = true) { /* bit mask | flag name 0b00000001 allowInteract @@ -1099,29 +1105,29 @@ class Player{ 0b10000000 - unused */ $flags = 0; - if(($this->gamemode & 0x02) === 0x02){ + if(($this->gamemode & 0x02) === 0x02) { $flags |= 0x01; //Do not allow placing/breaking blocks, adventure mode } - if($nametags !== false){ + if($nametags !== false) { $flags |= 0x20; //Show Nametags } - + //$flags |= 0x2; //pvp //$flags |= 0x4; //pve - + $pk = new AdventureSettingsPacket; $pk->flags = $flags; $this->dataPacket($pk); } - public function measureLag(){ - if($this->connected === false){ + public function measureLag() { + if($this->connected === false) { return false; } - if($this->packetStats[1] > 2){ + if($this->packetStats[1] > 2) { $this->packetLoss = $this->packetStats[1] / max(1, $this->packetStats[0] + $this->packetStats[1]); - }else{ + } else { $this->packetLoss = 0; } $this->packetStats = [0, 0]; @@ -1134,53 +1140,53 @@ class Player{ $this->lastMeasure = microtime(true); } - public function getLag(){ + public function getLag() { return $this->lagStat * 1000; } - public function getPacketLoss(){ + public function getPacketLoss() { return $this->packetLoss; } - public function getBandwidth(){ + public function getBandwidth() { return array_sum($this->bandwidthStats) / max(1, count($this->bandwidthStats)); } - public function clearQueue(){ - if($this->connected === false){ + public function clearQueue() { + if($this->connected === false) { return false; } - if(($cnt = count($this->received)) > PLAYER_MAX_QUEUE){ + if(($cnt = count($this->received)) > PLAYER_MAX_QUEUE) { ksort($this->received); - foreach($this->received as $c => $t){ + foreach($this->received as $c => $t) { unset($this->received[$c]); --$cnt; - if($cnt <= PLAYER_MAX_QUEUE){ + if($cnt <= PLAYER_MAX_QUEUE) { break; } } } } - public function handlePacketQueues(){ - if($this->connected === false){ + public function handlePacketQueues() { + if($this->connected === false) { return false; } $time = microtime(true); - if($time > $this->timeout){ + if($time > $this->timeout) { $this->close("timeout"); return false; } - if(($ackCnt = count($this->ackQueue)) > 0){ + if(($ackCnt = count($this->ackQueue)) > 0) { rsort($this->ackQueue); $safeCount = (int) (($this->MTU - 1) / 4); $packetCnt = (int) ($ackCnt / $safeCount + 1); - for($p = 0; $p < $packetCnt; ++$p){ + for($p = 0; $p < $packetCnt; ++$p) { $pk = new RakNetPacket(RakNetInfo::ACK); $pk->packets = []; - for($c = 0; $c < $safeCount; ++$c){ - if(($k = array_pop($this->ackQueue)) === null){ + for($c = 0; $c < $safeCount; ++$c) { + if(($k = array_pop($this->ackQueue)) === null) { break; } $pk->packets[] = $k; @@ -1190,20 +1196,20 @@ class Player{ $this->ackQueue = []; } - if(($receiveCnt = count($this->receiveQueue)) > 0){ + if(($receiveCnt = count($this->receiveQueue)) > 0) { ksort($this->receiveQueue); - foreach($this->receiveQueue as $count => $packets){ + foreach($this->receiveQueue as $count => $packets) { unset($this->receiveQueue[$count]); - foreach($packets as $p){ - if($p instanceof RakNetDataPacket and $p->hasSplit === false){ - if(isset($p->messageIndex) and $p->messageIndex !== false){ - if($p->messageIndex > $this->receiveCount){ + foreach($packets as $p) { + if($p instanceof RakNetDataPacket and $p->hasSplit === false) { + if(isset($p->messageIndex) and $p->messageIndex !== false) { + if($p->messageIndex > $this->receiveCount) { $this->receiveCount = $p->messageIndex; - }elseif($p->messageIndex !== 0){ - if(isset($this->received[$p->messageIndex])){ + } elseif($p->messageIndex !== 0) { + if(isset($this->received[$p->messageIndex])) { continue; } - switch($p->pid()){ + switch($p->pid()) { case 0x01: case ProtocolInfo::PING_PACKET: case ProtocolInfo::PONG_PACKET: @@ -1223,37 +1229,38 @@ class Player{ } } - if($this->bufferLen > 0){ + if($this->bufferLen > 0) { $this->sendBuffer(); } $limit = $time - 5; //max lag - foreach($this->recoveryQueue as $count => $data){ - if($data->sendtime > $limit){ + foreach($this->recoveryQueue as $count => $data) { + if($data->sendtime > $limit) { break; } unset($this->recoveryQueue[$count]); $this->resendQueue[$count] = $data; } - if(($resendCnt = count($this->resendQueue)) > 0){ - foreach($this->resendQueue as $count => $data){ + if(($resendCnt = count($this->resendQueue)) > 0) { + foreach($this->resendQueue as $count => $data) { unset($this->resendQueue[$count]); $this->packetStats[1]++; $this->lag[] = $time - $data->sendtime; $data->sendtime = $time; $cnt = $this->send($data); - if(isset($this->chunkCount[$count])){ + if(isset($this->chunkCount[$count])) { unset($this->chunkCount[$count]); - if(!is_null($cnt) and !is_null($cnt[0])) + if(!is_null($cnt) and !is_null($cnt[0])) { $this->chunkCount[$cnt[0]] = true; + } } } } } - public function sendEntityMovementUpdateQueue(){ - if($this->entityMovementQueueLength > 0 && $this->entityMovementQueue instanceof RakNetPacket){ + public function sendEntityMovementUpdateQueue() { + if($this->entityMovementQueueLength > 0 && $this->entityMovementQueue instanceof RakNetPacket) { $this->entityMovementQueue->seqNumber = $this->counter[0]++; $this->send($this->entityMovementQueue); } @@ -1261,9 +1268,9 @@ class Player{ $this->entityMovementQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->entityMovementQueue->data = []; } - - public function sendBlockUpdateQueue(){ - if($this->blockUpdateQueueLength > 0 && $this->blockUpdateQueue instanceof RakNetPacket){ + + public function sendBlockUpdateQueue() { + if($this->blockUpdateQueueLength > 0 && $this->blockUpdateQueue instanceof RakNetPacket) { $this->blockUpdateQueue->seqNumber = $this->counter[0]++; $this->recoveryQueue[$this->blockUpdateQueue->seqNumber] = $this->blockUpdateQueue; $this->blockUpdateQueue->sendtime = microtime(true); @@ -1273,8 +1280,8 @@ class Player{ $this->blockUpdateQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->blockUpdateQueue->data = []; } - - public function addBlockUpdateIntoQueue($x, $y, $z, $id, $meta){ + + public function addBlockUpdateIntoQueue($x, $y, $z, $id, $meta) { $packet = new UpdateBlockPacket(); $packet->x = $x; $packet->y = $y; @@ -1282,32 +1289,32 @@ class Player{ $packet->block = $id; $packet->meta = $meta; $packet->encode(); - + $len = 1 + strlen($packet->buffer); $MTU = $this->MTU - 24; - - if(($this->blockUpdateQueueLength + $len) >= $MTU){ + + if(($this->blockUpdateQueueLength + $len) >= $MTU) { $this->sendBlockUpdateQueue(); } - + $packet->messageIndex = $this->counter[3]++; $packet->reliability = 2; @$this->blockUpdateQueue->data[] = $packet; $this->blockUpdateQueueLength += 6 + $len; } - - public function addEntityMovementUpdateToQueue(Entity $e){ + + public function addEntityMovementUpdateToQueue(Entity $e) { $len = 0; $packets = 0; $motionSent = false; $moveSent = false; $headSent = false; $svdYSpeed = $e->speedY; - if($e->modifySpeedY){ + if($e->modifySpeedY) { $e->speedY = $e->modifedSpeedY; } - if($e->speedX != 0 || $e->speedY != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ){ - if(!($e->speedY < 0 && $e->onGround) || $e->speedX != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ){ + if($e->speedX != 0 || $e->speedY != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ) { + if(!($e->speedY < 0 && $e->onGround) || $e->speedX != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ) { $motion = new SetEntityMotionPacket(); $motion->eid = $e->eid; $motion->speedX = $e->speedX; @@ -1319,13 +1326,13 @@ class Player{ $motionSent = true; } } - if($e->modifySpeedY){ + if($e->modifySpeedY) { $e->speedY = $svdYSpeed; $e->modifySpeedY = false; } - - if($e->x != $e->lastX || $e->y != $e->lastY || $e->z != $e->lastZ || $e->yaw != $e->lastYaw || $e->pitch != $e->lastPitch){ - if($e->headYaw != $e->lastHeadYaw){ + + if($e->x != $e->lastX || $e->y != $e->lastY || $e->z != $e->lastZ || $e->yaw != $e->lastYaw || $e->pitch != $e->lastPitch) { + if($e->headYaw != $e->lastHeadYaw) { $move = new MovePlayerPacket(); $move->eid = $e->eid; $move->x = $e->x; @@ -1335,7 +1342,7 @@ class Player{ $move->pitch = $e->pitch; $move->bodyYaw = $e->headYaw; $move->encode(); - }else{ + } else { $move = new MoveEntityPacket_PosRot(); $move->eid = $e->eid; $move->x = $e->x; @@ -1345,11 +1352,11 @@ class Player{ $move->pitch = $e->pitch; $move->encode(); } - + $len += strlen($move->buffer) + 1; ++$packets; $moveSent = true; - }else if($e->headYaw != $e->lastHeadYaw){ + } elseif($e->headYaw != $e->lastHeadYaw) { $headyaw = new RotateHeadPacket(); $headyaw->eid = $e->eid; $headyaw->yaw = $e->headYaw; @@ -1358,41 +1365,43 @@ class Player{ ++$packets; $headSent = true; } - if($packets <= 0) return; + if($packets <= 0) { + return; + } //console("Update {$e}: $packets, mot: $motionSent, mov: $moveSent, hed: $headSent"); $MTU = $this->MTU - 24; - if(($this->entityMovementQueueLength + $len) >= $MTU){ + if(($this->entityMovementQueueLength + $len) >= $MTU) { $this->sendEntityMovementUpdateQueue(); } - if($motionSent){ + if($motionSent) { $motion->messageIndex = 0; //force 0 cuz reliability 0 $motion->reliability = 0; $this->entityMovementQueue->data[] = $motion; } - if($moveSent){ + if($moveSent) { $move->messageIndex = 0; $move->reliability = 0; $this->entityMovementQueue->data[] = $move; } - if($headSent){ + if($headSent) { $headyaw->messageIndex = 0; $headyaw->reliability = 0; $this->entityMovementQueue->data[] = $headyaw; } - - $this->entityMovementQueueLength += 6*$packets + $len; + + $this->entityMovementQueueLength += 6 * $packets + $len; } - + /** * @param string $reason Reason for closing connection * @param boolean $msg Set to false to silently disconnect player. No broadcast. */ - public function close($reason = "", $msg = true){ - if($this->connected === true){ - foreach($this->evid as $ev){ + public function close($reason = "", $msg = true) { + if($this->connected === true) { + foreach($this->evid as $ev) { $this->server->deleteEvent($ev); } - if($this->username != ""){ + if($this->username != "") { $this->server->api->handle("player.quit", $this); $this->save(); } @@ -1410,7 +1419,7 @@ class Player{ $this->resendQueue = []; $this->ackQueue = []; $this->server->api->player->remove($this->CID); - if($msg === true and $this->username != "" and $this->spawned !== false){ + if($msg === true and $this->username != "" and $this->spawned !== false) { $this->server->api->chat->broadcast($this->username . " left the game: " . $reason); } $this->spawned = false; @@ -1427,8 +1436,8 @@ class Player{ } } - public function save(){ - if($this->entity instanceof Entity){ + public function save() { + if($this->entity instanceof Entity) { $this->data->set("achievements", $this->achievements); $this->data->set("position", [ "level" => $this->entity->level->getName(), @@ -1445,9 +1454,9 @@ class Player{ "z" => $this->spawnPosition->z ]); $inv = []; - foreach($this->inventory as $slot => $item){ - if($item instanceof Item){ - if($slot < (($this->gamemode & 0x01) === 0 ? PLAYER_SURVIVAL_SLOTS : PLAYER_CREATIVE_SLOTS)){ + foreach($this->inventory as $slot => $item) { + if($item instanceof Item) { + if($slot < (($this->gamemode & 0x01) === 0 ? PLAYER_SURVIVAL_SLOTS : PLAYER_CREATIVE_SLOTS)) { $inv[$slot] = [$item->getID(), $item->getMetadata(), $item->count]; } } @@ -1456,25 +1465,25 @@ class Player{ $this->data->set("hotbar", $this->hotbar); $armor = []; - foreach($this->armor as $slot => $item){ - if($item instanceof Item){ + foreach($this->armor as $slot => $item) { + if($item instanceof Item) { $armor[$slot] = [$item->getID(), $item->getMetadata()]; } } $this->data->set("armor", $armor); - if($this->entity instanceof Entity){ + if($this->entity instanceof Entity) { $this->data->set("health", $this->entity->getHealth()); } $this->data->set("gamemode", $this->gamemode); } } - public function directDataPacket(RakNetDataPacket $packet, $reliability = 0, $recover = true){ - if($this->connected === false){ + public function directDataPacket(RakNetDataPacket $packet, $reliability = 0, $recover = true) { + if($this->connected === false) { return false; } - if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY){ + if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) { return []; } @@ -1483,22 +1492,24 @@ class Player{ $pk->data[] = $packet; $pk->seqNumber = $this->counter[0]++; $pk->sendtime = microtime(true); - if($recover !== false){ + if($recover !== false) { $this->recoveryQueue[$pk->seqNumber] = $pk; } $this->send($pk); return [$pk->seqNumber]; } - public function entityTick(){ - if($this->isSleeping) ++$this->sleepingTime; - - if($this->server->difficulty == 0 && $this->entity->counter % (20 * 15) == 0){ - if($this->entity->health < 20 && $this->entity->health > 0){ + public function entityTick() { + if($this->isSleeping) { + ++$this->sleepingTime; + } + + if($this->server->difficulty == 0 && $this->entity->counter % (20 * 15) == 0) { + if($this->entity->health < 20 && $this->entity->health > 0) { $this->entity->setHealth(min(20, $this->entity->health + 1), "regeneration"); } } - + } public function sendPing() { @@ -1511,21 +1522,21 @@ class Player{ return $this->lastPing; } - public function handleDataPacket(RakNetDataPacket $packet){ - if($this->connected === false){ + public function handleDataPacket(RakNetDataPacket $packet) { + if($this->connected === false) { return; } - if(EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY){ + if(EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY) { return; } - switch($packet->pid()){ + switch($packet->pid()) { case 0x01: break; case ProtocolInfo::PONG_PACKET: $currentTime = intdiv(hrtime(true), 1_000_000); - if($currentTime > $packet->ptime){ + if($currentTime > $packet->ptime) { $this->lastPing = $currentTime - $packet->ptime; } break; @@ -1539,7 +1550,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; @@ -1549,27 +1560,27 @@ 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]; - 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); @@ -1577,55 +1588,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]; } } @@ -1635,15 +1646,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); } @@ -1652,7 +1663,6 @@ class Player{ $this->server->api->player->saveOffline($this->data); - $pk = new LoginStatusPacket; $pk->status = 0; $this->dataPacket($pk); @@ -1667,33 +1677,37 @@ 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); } - + $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){ + 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; @@ -1718,25 +1732,24 @@ class Player{ $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){ + if($this->loggedIn === false) { break; } - switch($packet->status){ + switch($packet->status) { case 1: //Spawn!! - if($this->spawned !== false){ + 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); @@ -1750,9 +1763,9 @@ class Player{ //$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"); + $array = explode("@n", (string) $this->server->motd); + foreach($array as $msg) { + $this->sendChat($msg . "\n"); } $this->sendInventory(); @@ -1768,48 +1781,50 @@ 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; @@ -1818,79 +1833,84 @@ 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){ + } elseif($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) { $this->slot = $packet->slot; $this->curHotbarIndex = $packetSlotPos; } - if(!$has){ + 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(); @@ -1902,13 +1922,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); @@ -1948,18 +1968,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; } @@ -1982,21 +2002,21 @@ 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); - - if($d >= 0.0001){ + + $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(), @@ -2008,26 +2028,29 @@ class Player{ "shootY" => $shootY, "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); - else $slotItem->count -= 1; + 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){ + + } else { + if($this->server->handle("player.action", $data) !== false) { $this->entity->inAction = true; $this->entity->inActionCounter = 0; $this->startAction = microtime(true); @@ -2037,29 +2060,31 @@ 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, ]); - + $d = [ "x" => $this->entity->x, "y" => $this->entity->y + 1.6, @@ -2083,11 +2108,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) { $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); @@ -2095,7 +2120,7 @@ class Player{ } } } - }else{ //inv desynced, resend + } else { //inv desynced, resend $this->sendInventory(); } } @@ -2110,7 +2135,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; @@ -2127,46 +2152,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; @@ -2177,36 +2202,36 @@ 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 = []; $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; @@ -2215,18 +2240,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(); @@ -2236,22 +2261,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; @@ -2259,10 +2284,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); } } @@ -2270,43 +2295,45 @@ 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; } - + $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; + + 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; } } - + $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){ + 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; @@ -2319,46 +2346,46 @@ 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; } - + //if($message == "pf"){ // 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); } } @@ -2366,14 +2393,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; @@ -2382,7 +2409,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; @@ -2399,36 +2426,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); @@ -2437,38 +2464,38 @@ 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; } @@ -2476,13 +2503,13 @@ class Player{ $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){ + "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; @@ -2490,37 +2517,37 @@ 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", [ - "tile" => $tile, - "slot" => $packet->slot, - "slotdata" => $slot, - "itemdata" => $item, - "player" => $this, - ]) === false){ + "tile" => $tile, + "slot" => $packet->slot, + "slotdata" => $slot, + "itemdata" => $item, + "player" => $this, + ]) === false) { $pk = new ContainerSetSlotPacket; $pk->windowid = $packet->windowid; $pk->slot = $packet->slot; @@ -2529,24 +2556,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); @@ -2556,27 +2583,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"]); } } @@ -2587,12 +2614,12 @@ class Player{ $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(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) { ConsoleAPI::warn("Player is riding on entity that doesnt exist in world! ({$this->iusername}, {$this->entity->linkedEntity})"); @@ -2612,58 +2639,58 @@ class Player{ 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 */ - - public function getHeldItem(){ + + public function getHeldItem() { return $this->getSlot($this->slot); } - - public function stopSleep(){ + + public function stopSleep() { $this->isSleeping = false; $this->sleepingTime = 0; - if($this->entity instanceof Entity){ + if($this->entity instanceof Entity) { $this->entity->updateMetadata(); } } - public function hasItem($type, $damage = false){ - foreach($this->inventory as $s => $item){ - if($item->getID() === $type and ($item->getMetadata() === $damage or $damage === false) and $item->count > 0){ + public function hasItem($type, $damage = false) { + foreach($this->inventory as $s => $item) { + if($item->getID() === $type and ($item->getMetadata() === $damage or $damage === false) and $item->count > 0) { return $s; } } return false; } - public function removeItem($type, $damage, $count, $send = true){ - while($count > 0){ + public function removeItem($type, $damage, $count, $send = true) { + while($count > 0) { $remove = 0; - foreach($this->inventory as $s => $item){ - if($item->getID() === $type and $item->getMetadata() === $damage){ + foreach($this->inventory as $s => $item) { + if($item->getID() === $type and $item->getMetadata() === $damage) { $remove = min($count, $item->count); - if($remove < $item->count){ + if($remove < $item->count) { $item->count -= $remove; - }else{ + } else { $this->inventory[$s] = BlockAPI::getItem(AIR, 0, 0); } - if($send === true){ + if($send === true) { $this->sendInventorySlot($s); } break; } } - if($remove <= 0){ + if($remove <= 0) { return false; } $count -= $remove; @@ -2672,22 +2699,19 @@ class Player{ } /** - * @param array $craft - * @param array $recipe - * @param $type * * @return array|bool */ - public function craftItems(array $craft, array $recipe, $type){ + public function craftItems(array $craft, array $recipe, $type) { $craftItem = [0, true, 0]; unset($craft[-1]); - - foreach($craft as $slot => $item){ - if($item instanceof Item){ + + foreach($craft as $slot => $item) { + if($item instanceof Item) { $craftItem[0] = $item->getID(); - if($item->getMetadata() !== $craftItem[1] and $craftItem[1] !== true){ + if($item->getMetadata() !== $craftItem[1] and $craftItem[1] !== true) { $craftItem[1] = false; - }else{ + } else { $craftItem[1] = $item->getMetadata(); } $craftItem[2] += $item->count; @@ -2696,11 +2720,11 @@ class Player{ } $recipeItems = []; - foreach($recipe as $slot => $item){ - if(!isset($recipeItems[$item->getID()])){ + foreach($recipe as $slot => $item) { + if(!isset($recipeItems[$item->getID()])) { $recipeItems[$item->getID()] = [$item->getID(), $item->getMetadata(), $item->count]; - }else{ - if($item->getMetadata() !== $recipeItems[$item->getID()][1]){ + } else { + if($item->getMetadata() !== $recipeItems[$item->getID()][1]) { $recipeItems[$item->getID()][1] = false; } $recipeItems[$item->getID()][2] += $item->count; @@ -2709,31 +2733,31 @@ class Player{ $res = CraftingRecipes::canCraft($craftItem, $recipeItems, $type); - if(!is_array($res) and $type === 1){ + if(!is_array($res) and $type === 1) { $res2 = CraftingRecipes::canCraft($craftItem, $recipeItems, 0); - if(is_array($res2)){ + if(is_array($res2)) { $res = $res2; } } - if(is_array($res)){ - if($this->server->api->dhandle("player.craft", ["player" => $this, "recipe" => $recipe, "craft" => $craft, "type" => $type]) === false){ + if(is_array($res)) { + if($this->server->api->dhandle("player.craft", ["player" => $this, "recipe" => $recipe, "craft" => $craft, "type" => $type]) === false) { return false; } - foreach($recipe as $slot => $item){ + foreach($recipe as $slot => $item) { $s = $this->getSlot($slot); $s->count -= $item->count; - if($s->count <= 0){ + if($s->count <= 0) { $this->setSlot($slot, BlockAPI::getItem(AIR, 0, 0), false); } } - foreach($craft as $slot => $item){ + foreach($craft as $slot => $item) { $s = $this->getSlot($slot); - if($s->count <= 0 or $s->getID() === AIR){ + 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()){ + } elseif($s->getID() == $item->getID() && $s->getMetadata() == $item->getMetadata() && ($s->count + $item->count) <= $s->getMaxStackSize()) { $this->setSlot($slot, BlockAPI::getItem($item->getID(), $item->getMetadata(), $s->count + $item->count), false); - }else{ + } else { $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; @@ -2746,10 +2770,10 @@ 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, $item, $sX, $sY, $sZ); } - + $this->sendInventory(); //force send on crafting - - switch($item->getID()){ + + switch($item->getID()) { case WORKBENCH: AchievementAPI::grantAchievement($this, "buildWorkBench"); break; @@ -2788,26 +2812,26 @@ class Player{ return $res; } - public function handlePacket(RakNetPacket $packet){ - if($this->connected === true){ + public function handlePacket(RakNetPacket $packet) { + if($this->connected === true) { $time = microtime(true); $this->timeout = $time + 20; - switch($packet->pid()){ + switch($packet->pid()) { case RakNetInfo::NACK: - foreach($packet->packets as $count){ - if(isset($this->recoveryQueue[$count])){ - $this->resendQueue[$count] =& $this->recoveryQueue[$count]; + foreach($packet->packets as $count) { + if(isset($this->recoveryQueue[$count])) { + $this->resendQueue[$count] = &$this->recoveryQueue[$count]; $this->lag[] = $time - $this->recoveryQueue[$count]->sendtime; unset($this->recoveryQueue[$count]); - }else{ + } else { ++$this->packetStats[1]; //lost and wont be recovered } } break; case RakNetInfo::ACK: - foreach($packet->packets as $count){ - if(isset($this->recoveryQueue[$count])){ + foreach($packet->packets as $count) { + if(isset($this->recoveryQueue[$count])) { $this->lag[] = $time - $this->recoveryQueue[$count]->sendtime; unset($this->recoveryQueue[$count]); unset($this->resendQueue[$count]); @@ -2834,7 +2858,7 @@ class Player{ case RakNetInfo::DATA_PACKET_F: $this->ackQueue[] = $packet->seqNumber; $this->receiveQueue[$packet->seqNumber] = []; - foreach($packet->data as $pk){ + foreach($packet->data as $pk) { $this->receiveQueue[$packet->seqNumber][] = $pk; } break; @@ -2842,9 +2866,9 @@ class Player{ } } - public function damageArmorPart($slot, $part){ + public function damageArmorPart($slot, $part) { $part->useOn($this->entity, true); - if($part->getMetadata() >= $part->getMaxDurability()){ + if($part->getMetadata() >= $part->getMaxDurability()) { $this->setArmor($slot, BlockAPI::getItem(AIR, 0, 0), false); return; } @@ -2854,8 +2878,8 @@ class Player{ /** * @return string */ - function __toString(){ - if($this->username != ""){ + public function __toString() { + if($this->username != "") { return $this->username; } return $this->clientID; @@ -2863,9 +2887,9 @@ class Player{ } -class PlayerNull extends Player{ +class PlayerNull extends Player { public static $INSTANCE; - public function __construct(){ + public function __construct() { } } diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 1b281cfc..ae369d6e 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -1,18 +1,69 @@ port = (int) $port; $this->doTick = true; $this->gamemode = (int) $gamemode; @@ -23,11 +74,10 @@ class PocketMinecraftServer{ $this->serverip = $serverip; $this->load(); } - + public static $SAVE_PLAYER_DATA = true; - - - private function load(){ + + private function load() { global $dolog; /*if(defined("DEBUG") and DEBUG >= 0){ @cli_set_process_title("NostalgiaCore ".MAJOR_VERSION); @@ -70,7 +120,7 @@ class PocketMinecraftServer{ $this->interface = new MinecraftInterface("255.255.255.255", $this->port, $this->serverip); $this->stop = false; $this->ticks = 0; - if(!defined("NO_THREADS")){ + if(!defined("NO_THREADS")) { $this->asyncThread = new AsyncMultipleQueue(); } console("[INFO] Loading extra.properties..."); @@ -85,10 +135,10 @@ class PocketMinecraftServer{ "discord-ru-smiles" => false, "discord-webhook-url" => "none", "discord-bot-name" => "NostalgiaCore Logger", - "despawn-mobs" => true, + "despawn-mobs" => true, "mob-despawn-ticks" => 18000, "16x16x16_chunk_sending" => false, - "experimental-mob-ai" => false, + "experimental-mob-ai" => false, "force-20-tps" => false, "enable-mob-pushing" => Living::$entityPushing, "keep-chunks-loaded" => self::$KEEP_CHUNKS_LOADED, @@ -107,24 +157,24 @@ class PocketMinecraftServer{ MobController::$ADVANCED = $this->extraprops->get("experimental-mob-ai"); Explosion::$enableExplosions = $this->extraprops->get("enable-explosions"); NetherReactorBlock::$enableReactor = $this->extraprops->get("enable-nether-reactor"); - if(self::$FORCE_20_TPS){ + if(self::$FORCE_20_TPS) { ConsoleAPI::warn("Forcing 20 tps. This may result in higher CPU usage!"); } - if($this->extraprops->get("discord-msg")){ - if($this->extraprops->get("discord-webhook-url") !== "none"){ + if($this->extraprops->get("discord-msg")) { + if($this->extraprops->get("discord-webhook-url") !== "none") { console("[INFO] Discord Logger is enabled."); - }else{ + } else { console("[WARNING] Discord Logger is enabled in extra.properties,"); console("[WARNING] but you didn't put the webhook url, so it won't work."); } - }elseif($this->extraprops->get("version") == null){ + } elseif($this->extraprops->get("version") == null) { console("[WARNING] Your extra.properties file is corrupted!"); console("[WARNING] To fix it - just remove it! Server will generate it again automatically."); } $dolog = $this->extraprops->get("save-console-data"); } - public function onShutdown(){ + public function onShutdown() { console("[INFO] Saving..."); $save = $this->saveEnabled; $this->saveEnabled = true; @@ -132,7 +182,7 @@ class PocketMinecraftServer{ $this->saveEnabled = $save; } - public function startDatabase(){ + public function startDatabase() { $this->preparedSQL = new stdClass(); $this->preparedSQL->entity = new stdClass(); $this->database = new SQLite3(":memory:"); @@ -154,16 +204,16 @@ class PocketMinecraftServer{ $this->preparedSQL->entity->setLevel = $this->database->prepare("UPDATE entities SET level = :level WHERE EID = :eid ;"); } - public function query($sql, $fetch = false){ + public function query($sql, $fetch = false) { $result = $this->database->query($sql) or console("[ERROR] [SQL Error] " . $this->database->lastErrorMsg() . ". Query: " . $sql, true, true, 0); - if($fetch === true and ($result instanceof SQLite3Result)){ + if($fetch === true and ($result instanceof SQLite3Result)) { $result = $result->fetchArray(SQLITE3_ASSOC); } return $result; } - public function setType($type = "normal"){ - switch(trim(strtolower($type))){ + public function setType($type = "normal") { + switch(trim(strtolower($type))) { case "normal": case "demo": $this->serverType = "MCCPP;Demo;"; @@ -174,9 +224,9 @@ class PocketMinecraftServer{ } } - public function titleTick(){ + public function titleTick() { $time = microtime(true); - if(defined("DEBUG") and DEBUG >= 0){ + if(defined("DEBUG") and DEBUG >= 0) { //echo "\x1b]0;NostalgiaCore " . MAJOR_VERSION . " | Online " . count($this->clients) . "/" . $this->maxClients . " | RAM " . round((memory_get_usage() / 1024) / 1024, 2) . "MB | U " . round(($this->interface->bandwidth[1] / max(1, $time - $this->interface->bandwidth[2])) / 1024, 2) . " D " . round(($this->interface->bandwidth[0] / max(1, $time - $this->interface->bandwidth[2])) / 1024, 2) . " kB/s | TPS " . $this->getTPS() . "\x07"; } @@ -186,18 +236,18 @@ class PocketMinecraftServer{ /** * @return float */ - public function getTPS(){ + public function getTPS() { $v = array_values($this->tickMeasure); $divval = ($v[39] - $v[0]); - if($divval === 0){ + if($divval === 0) { return 0; } $tps = 40 / $divval; return round($tps, 4); } - public function checkTicks(){ - if($this->getTPS() < 12){ + public function checkTicks() { + if($this->getTPS() < 12) { console("[WARNING] Can't keep up! Is the server overloaded?"); } } @@ -205,14 +255,14 @@ class PocketMinecraftServer{ /** * @param string $reason */ - public function close($reason = "server stop"){ + public function close($reason = "server stop") { $this->onShutdown(); - if($this->stop !== true){ - if(is_int($reason)){ + if($this->stop !== true) { + if(is_int($reason)) { $reason = "signal stop"; } - if(($this->api instanceof ServerAPI) === true){ - if(($this->api->chat instanceof ChatAPI) === true){ + if(($this->api instanceof ServerAPI) === true) { + if(($this->api->chat instanceof ChatAPI) === true) { $this->api->chat->send(false, "Stopping server..."); self::$_tmp = new StopMessageThread($this, "[INFO] Stopping server..."); //broadcast didnt want to send message to discord for some reason } @@ -220,8 +270,8 @@ class PocketMinecraftServer{ $this->stop = true; $this->trigger("server.close", $reason); $this->interface->close(); - if(!defined("NO_THREADS")){ - $this->asyncThread->synchronized(function ($t){ + if(!defined("NO_THREADS")) { + $this->asyncThread->synchronized(function ($t) { $t->stop = true; $t->notify(); }, $this->asyncThread); @@ -229,9 +279,9 @@ class PocketMinecraftServer{ } release_lock(); } - - public function send2Discord($msg){ - if($this->extraprops->get("discord-msg") and $this->extraprops->get("discord-webhook-url") !== "none"){ + + public function send2Discord($msg) { + if($this->extraprops->get("discord-msg") and $this->extraprops->get("discord-webhook-url") !== "none") { $url = $this->extraprops->get("discord-webhook-url"); $name = $this->extraprops->get("discord-bot-name"); $this->asyncOperation(ASYNC_CURL_POST, [ @@ -243,16 +293,16 @@ class PocketMinecraftServer{ ], null); } } - - public function asyncOperation($type, array $data, callable $callable = null){ - if(defined("NO_THREADS")){ + + public function asyncOperation($type, array $data, callable $callable = null) { + if(defined("NO_THREADS")) { return false; } $d = ""; $type = (int) $type; - switch($type){ + switch($type) { case ASYNC_CURL_GET: - if(isset($data["headers"])){ + if(isset($data["headers"])) { $jsstr = json_encode($data["headers"]); } $d .= Utils::writeShort(strlen($data["url"])) . $data["url"] . (isset($data["timeout"]) ? Utils::writeShort($data["timeout"]) : Utils::writeShort(10)) . (isset($data["headers"]) ? Utils::writeShort(strlen($jsstr)) . $jsstr : Utils::writeShort(1) . " "); @@ -260,7 +310,7 @@ class PocketMinecraftServer{ case ASYNC_CURL_POST: $d .= Utils::writeShort(strlen($data["url"])) . $data["url"] . (isset($data["timeout"]) ? Utils::writeShort($data["timeout"]) : Utils::writeShort(10)); $d .= Utils::writeShort(count($data["data"])); - foreach($data["data"] as $key => $value){ + foreach($data["data"] as $key => $value) { $d .= Utils::writeShort(strlen($key)) . $key . Utils::writeInt(strlen($value)) . $value; } break; @@ -273,55 +323,55 @@ class PocketMinecraftServer{ return $ID; } - public function trigger($event, $data = ""){ - if(isset($this->events[$event])){ - foreach($this->events[$event] as $evid => $ev){ - if(!is_callable($ev)){ + public function trigger($event, $data = "") { + if(isset($this->events[$event])) { + foreach($this->events[$event] as $evid => $ev) { + if(!is_callable($ev)) { $this->deleteEvent($evid); continue; } - if(is_array($ev)){ + if(is_array($ev)) { $method = $ev[1]; $ev[0]->$method($data, $event); - }else{ + } else { $ev($data, $event); } } - }elseif(isset(Deprecation::$events[$event])){ + } elseif(isset(Deprecation::$events[$event])) { $sub = ""; - if(Deprecation::$events[$event] !== false){ + if(Deprecation::$events[$event] !== false) { $sub = " Substitute \"" . Deprecation::$events[$event] . "\" found."; } console("[ERROR] Event \"$event\" has been deprecated.$sub [Trigger]"); } } - public function deleteEvent($id){ + public function deleteEvent($id) { $id = (int) $id; - if(isset($this->eventsID[$id])){ + if(isset($this->eventsID[$id])) { $ev = $this->eventsID[$id]; $this->eventsID[$id] = null; unset($this->eventsID[$id]); $this->events[$ev][$id] = null; unset($this->events[$ev][$id]); - if(count($this->events[$ev]) === 0){ + if(count($this->events[$ev]) === 0) { unset($this->events[$ev]); } } } - public function asyncOperationChecker(){ - if(defined("NO_THREADS")){ + public function asyncOperationChecker() { + if(defined("NO_THREADS")) { return false; } - if(isset($this->asyncThread->output[5])){ + if(isset($this->asyncThread->output[5])) { $offset = 0; $ID = Utils::readInt(substr($this->asyncThread->output, $offset, 4)); $offset += 4; $type = Utils::readShort(substr($this->asyncThread->output, $offset, 2)); $offset += 2; $data = []; - switch($type){ + switch($type) { case ASYNC_CURL_GET: case ASYNC_CURL_POST: $len = Utils::readInt(substr($this->asyncThread->output, $offset, 4)); @@ -333,11 +383,11 @@ class PocketMinecraftServer{ } $this->asyncThread->output = substr($this->asyncThread->output, $offset); - if(isset($this->async[$ID]) and $this->async[$ID] !== null and is_callable($this->async[$ID])){ - if(is_array($this->async[$ID])){ + if(isset($this->async[$ID]) and $this->async[$ID] !== null and is_callable($this->async[$ID])) { + if(is_array($this->async[$ID])) { $method = $this->async[$ID][1]; $result = $this->async[$ID][0]->$method($data, $type, $ID); - }else{ + } else { $result = $this->async[$ID]($data, $type, $ID); } } @@ -347,17 +397,16 @@ class PocketMinecraftServer{ /** * @param string $event - * @param callable $callable * @param integer $priority * * @return boolean */ - public function addHandler($event, callable $callable, $priority = 5){ - if(!is_callable($callable)){ + public function addHandler($event, callable $callable, $priority = 5) { + if(!is_callable($callable)) { return false; - }elseif(isset(Deprecation::$events[$event])){ + } elseif(isset(Deprecation::$events[$event])) { $sub = ""; - if(Deprecation::$events[$event] !== false){ + if(Deprecation::$events[$event] !== false) { $sub = " Substitute \"" . Deprecation::$events[$event] . "\" found."; } console("[ERROR] Event \"$event\" has been deprecated.$sub [Adding handle to " . (is_array($callable) ? get_class($callable[0]) . "::" . $callable[1] : $callable) . "]"); @@ -370,77 +419,77 @@ class PocketMinecraftServer{ return $hnid; } - public function dhandle($e, $d){ + public function dhandle($e, $d) { return $this->handle($e, $d); } - public function handle($event, &$data){ + public function handle($event, &$data) { $this->preparedSQL->selectHandlers->reset(); $this->preparedSQL->selectHandlers->clear(); $this->preparedSQL->selectHandlers->bindValue(":name", $event, SQLITE3_TEXT); $handlers = $this->preparedSQL->selectHandlers->execute(); $result = null; - if($handlers instanceof SQLite3Result){ + if($handlers instanceof SQLite3Result) { $call = []; - while(($hn = $handlers->fetchArray(SQLITE3_ASSOC)) !== false){ + while(($hn = $handlers->fetchArray(SQLITE3_ASSOC)) !== false) { $call[(int) $hn["ID"]] = true; } $handlers->finalize(); - foreach($call as $hnid => $boolean){ - if($result !== false and $result !== true){ + foreach($call as $hnid => $boolean) { + if($result !== false and $result !== true) { $called[$hnid] = true; $handler = $this->handlers[$hnid]; - if(is_array($handler)){ + if(is_array($handler)) { $method = $handler[1]; $result = $handler[0]->$method($data, $event); - }else{ + } else { $result = $handler($data, $event); } - }else{ + } else { break; } } - }elseif(isset(Deprecation::$events[$event])){ + } elseif(isset(Deprecation::$events[$event])) { $sub = ""; - if(Deprecation::$events[$event] !== false){ + if(Deprecation::$events[$event] !== false) { $sub = " Substitute \"" . Deprecation::$events[$event] . "\" found."; } console("[ERROR] Event \"$event\" has been deprecated.$sub [Handler]"); } - if($result !== false){ + if($result !== false) { $this->trigger($event, $data); } return $result; } - public function eventHandler($data, $event){ - switch($event){ + public function eventHandler($data, $event) { + switch($event) { } } - public function init(){ + public function init() { register_tick_function([$this, "tick"]); declare(ticks=5000); //Minimum TPS for main thread locks $this->loadEvents(); register_shutdown_function([$this, "dumpError"]); register_shutdown_function([$this, "close"]); - if(function_exists("pcntl_signal")){ + if(function_exists("pcntl_signal")) { pcntl_signal(SIGTERM, [$this, "close"]); pcntl_signal(SIGINT, [$this, "close"]); pcntl_signal(SIGHUP, [$this, "close"]); } - + console("[INFO] Default game type: " . strtoupper($this->getGamemode())); $this->trigger("server.start", microtime(true)); console('[INFO] Done (' . round(microtime(true) - START_TIME, 3) . 's)! For help, type "help" or "?"'); $this->process(); } - public function loadEvents(){ - if(ENABLE_ANSI === true){ + public function loadEvents() { + if(ENABLE_ANSI === true) { $this->schedule(30, [$this, "titleTick"], [], true); } $this->schedule(20 * 15, [$this, "checkTicks"], [], true); @@ -448,11 +497,11 @@ class PocketMinecraftServer{ $this->schedule(20, [$this, "asyncOperationChecker"], [], true); } - public function schedule($ticks, callable $callback, $data = [], $repeat = false, $eventName = "server.schedule"){ - if(!is_callable($callback)){ + public function schedule($ticks, callable $callback, $data = [], $repeat = false, $eventName = "server.schedule") { + if(!is_callable($callback)) { return false; } - + $chcnt = $this->scheduleCnt++; $this->schedule[$chcnt] = [$callback, $data, $eventName]; $this->query("INSERT INTO actions (ID, interval, last, repeat) VALUES(" . $chcnt . ", " . ($ticks / 20) . ", " . microtime(true) . ", " . (((bool) $repeat) === true ? 1 : 0) . ");"); @@ -462,8 +511,8 @@ class PocketMinecraftServer{ /** * @return string */ - public function getGamemode(){ - switch($this->gamemode){ + public function getGamemode() { + switch($this->gamemode) { case SURVIVAL: return "survival"; case CREATIVE: @@ -475,71 +524,69 @@ class PocketMinecraftServer{ } } - public function process() - { + public function process() { $lastLoop = 0; - if(self::$FORCE_20_TPS){ - while($this->stop === false){ + if(self::$FORCE_20_TPS) { + while($this->stop === false) { $packetcnt = 0; - while($packet = $this->interface->readPacket()){ + while($packet = $this->interface->readPacket()) { if($packet instanceof Packet) { $this->packetHandler($packet); - if(++$packetcnt > self::$PACKET_READING_LIMIT){ - ConsoleAPI::warn("Reading more than ".self::$PACKET_READING_LIMIT." packets per tick! Forcing ticking!"); + if(++$packetcnt > self::$PACKET_READING_LIMIT) { + ConsoleAPI::warn("Reading more than " . self::$PACKET_READING_LIMIT . " packets per tick! Forcing ticking!"); break; } } } - + $this->tick(); } - }else{ - while($this->stop === false){ + } else { + while($this->stop === false) { $packetcnt = 0; startReadingAgain: $packet = $this->interface->readPacket(); - if($packet instanceof Packet){ + if($packet instanceof Packet) { $this->packetHandler($packet); $lastLoop = 0; - if(++$packetcnt > self::$PACKET_READING_LIMIT){ - ConsoleAPI::warn("Reading more than ".self::$PACKET_READING_LIMIT." packets per tick! Forcing ticking!"); - }else{ + if(++$packetcnt > self::$PACKET_READING_LIMIT) { + ConsoleAPI::warn("Reading more than " . self::$PACKET_READING_LIMIT . " packets per tick! Forcing ticking!"); + } else { goto startReadingAgain; } - } elseif($this->tick() > 0){ + } elseif($this->tick() > 0) { $lastLoop = 0; - } else{ + } else { ++$lastLoop; - if($lastLoop < 16){ + if($lastLoop < 16) { usleep(1); - } elseif($lastLoop < 128){ + } elseif($lastLoop < 128) { usleep(100); - } elseif($lastLoop < 256){ + } elseif($lastLoop < 256) { usleep(512); - } else{ + } else { usleep(10000); } } $this->tick(); } } - - + } - public function packetHandler(Packet $packet){ - $data =& $packet; + public function packetHandler(Packet $packet) { + $data = &$packet; $CID = PocketMinecraftServer::clientID($packet->ip, $packet->port); - if(isset($this->clients[$CID])){ + if(isset($this->clients[$CID])) { $this->clients[$CID]->handlePacket($packet); - }else{ - if($this->handle("server.noauthpacket." . $packet->pid(), $packet) === false){ + } else { + if($this->handle("server.noauthpacket." . $packet->pid(), $packet) === false) { return; } - switch($packet->pid()){ + switch($packet->pid()) { case RakNetInfo::UNCONNECTED_PING: case RakNetInfo::UNCONNECTED_PING_OPEN_CONNECTIONS: - if($this->invisible === true){ + if($this->invisible === true) { $pk = new RakNetPacket(RakNetInfo::UNCONNECTED_PONG); $pk->pingID = $packet->pingID; $pk->serverID = $this->serverID; @@ -549,11 +596,11 @@ class PocketMinecraftServer{ $this->send($pk); break; } - if(!isset($this->custom["times_" . $CID])){ + if(!isset($this->custom["times_" . $CID])) { $this->custom["times_" . $CID] = 0; } $ln = 15; - if($this->description == "" or !str_ends_with($this->description, " ")){ + if($this->description == "" or !str_ends_with($this->description, " ")) { $this->description .= " "; } $txt = substr($this->description, $this->custom["times_" . $CID], $ln); @@ -568,14 +615,14 @@ class PocketMinecraftServer{ $this->custom["times_" . $CID] = ($this->custom["times_" . $CID] + 1) % strlen($this->description); break; case RakNetInfo::OPEN_CONNECTION_REQUEST_1: - if($packet->structure !== RakNetInfo::STRUCTURE){ + if($packet->structure !== RakNetInfo::STRUCTURE) { console("[DEBUG] Incorrect structure #" . $packet->structure . " from " . $packet->ip . ":" . $packet->port, true, true, 2); $pk = new RakNetPacket(RakNetInfo::INCOMPATIBLE_PROTOCOL_VERSION); $pk->serverID = $this->serverID; $pk->ip = $packet->ip; $pk->port = $packet->port; $this->send($pk); - }else{ + } else { $pk = new RakNetPacket(RakNetInfo::OPEN_CONNECTION_REPLY_1); $pk->serverID = $this->serverID; $pk->mtuSize = strlen($packet->buffer); @@ -585,7 +632,7 @@ class PocketMinecraftServer{ } break; case RakNetInfo::OPEN_CONNECTION_REQUEST_2: - if($this->invisible === true){ + if($this->invisible === true) { break; } @@ -601,30 +648,30 @@ class PocketMinecraftServer{ } } - public static function clientID($ip, $port){ + public static function clientID($ip, $port) { return crc32($ip . $port) ^ crc32($port . $ip . BOOTUP_RANDOM); //return $ip . ":" . $port; } - public function send(Packet $packet){ + public function send(Packet $packet) { return $this->interface->writePacket($packet); } - public function tick(){ + public function tick() { $time = microtime(true); - if($this->lastTick <= ($time - 0.05)){ + if($this->lastTick <= ($time - 0.05)) { $this->tickMeasure[] = $this->lastTick = $time; unset($this->tickMeasure[key($this->tickMeasure)]); ++$this->ticks; - - foreach($this->clients as $client){ + + foreach($this->clients as $client) { $client->handlePacketQueues(); - if($this->ticks % 40 == 0){ //2s + if($this->ticks % 40 == 0) { //2s $client->sendPing(); } } - - foreach($this->api->level->levels as $l){ + + foreach($this->api->level->levels as $l) { $l->onTick($this, $time); } $r = $this->tickerFunction($time); @@ -633,34 +680,34 @@ class PocketMinecraftServer{ return 0; } - public function tickerFunction($time){ + public function tickerFunction($time) { //actions that repeat every x time will go here $this->preparedSQL->selectActions->reset(); $this->preparedSQL->selectActions->bindValue(":time", $time, SQLITE3_FLOAT); $actions = $this->preparedSQL->selectActions->execute(); $actionCount = 0; - if($actions instanceof SQLite3Result){ - while(($action = $actions->fetchArray(SQLITE3_ASSOC)) !== false){ + if($actions instanceof SQLite3Result) { + while(($action = $actions->fetchArray(SQLITE3_ASSOC)) !== false) { $cid = $action["ID"]; $this->preparedSQL->updateAction->reset(); $this->preparedSQL->updateAction->bindValue(":time", $time, SQLITE3_FLOAT); $this->preparedSQL->updateAction->bindValue(":id", $cid, SQLITE3_INTEGER); $this->preparedSQL->updateAction->execute(); - if(isset($this->schedule[$cid]) && is_array($this->schedule[$cid]) && isset($this->schedule[$cid][0]) && is_callable($this->schedule[$cid][0])){ + if(isset($this->schedule[$cid]) && is_array($this->schedule[$cid]) && isset($this->schedule[$cid][0]) && is_callable($this->schedule[$cid][0])) { ++$actionCount; - try{ - $return = @call_user_func($this->schedule[$cid][0] ?? function(){}, $this->schedule[$cid][1], $this->schedule[$cid][2]); //somehow args can be null - }catch(TypeError $e){ - $m = $e->getMessage()."\nStack trace:\n".$e->getTraceAsString(); + try { + $return = @call_user_func($this->schedule[$cid][0] ?? function () {}, $this->schedule[$cid][1], $this->schedule[$cid][2]); //somehow args can be null + } catch(TypeError $e) { + $m = $e->getMessage() . "\nStack trace:\n" . $e->getTraceAsString(); ConsoleAPI::error($m); $return = false; } - }else{ + } else { $return = false; } - if($action["repeat"] == 0 or $return === false){ + if($action["repeat"] == 0 or $return === false) { $this->query("DELETE FROM actions WHERE ID = " . $action["ID"] . ";"); $this->schedule[$cid] = null; unset($this->schedule[$cid]); @@ -671,8 +718,8 @@ class PocketMinecraftServer{ return $actionCount; } - public function dumpError(){ - if($this->stop === true){ + public function dumpError() { + if($this->stop === true) { return; } ini_set("memory_limit", "-1"); //Fix error dump not dumped on memory problems @@ -698,18 +745,18 @@ class PocketMinecraftServer{ ]; $er["type"] = $errorConversion[$er["type"]] ?? $er["type"]; $dump .= "Error: " . var_export($er, true) . "\r\n\r\n"; - if(stripos($er["file"], "plugin") !== false){ + if(stripos($er["file"], "plugin") !== false) { $dump .= "THIS ERROR WAS CAUSED BY A PLUGIN. REPORT IT TO THE PLUGIN DEVELOPER.\r\n"; } $dump .= "Code: \r\n"; $file = @file($er["file"], FILE_IGNORE_NEW_LINES); - for($l = max(0, $er["line"] - 10); $l < $er["line"] + 10; ++$l){ + for($l = max(0, $er["line"] - 10); $l < $er["line"] + 10; ++$l) { $dump .= "[" . ($l + 1) . "] " . @$file[$l] . "\r\n"; } $dump .= "\r\n\r\n"; $dump .= "Backtrace: \r\n"; - foreach(getTrace() as $line){ + foreach(getTrace() as $line) { $dump .= "$line\r\n"; } $dump .= "\r\n\r\n"; @@ -724,21 +771,21 @@ class PocketMinecraftServer{ global $arguments; $dump .= "Parameters: " . var_export($arguments, true) . "\r\n\r\n\r\n"; $p = $this->api->getProperties(); - if($p["rcon.password"] != ""){ + if($p["rcon.password"] != "") { $p["rcon.password"] = "******"; } $dump .= "server.properties: " . var_export($p, true) . "\r\n\r\n\r\n"; - if($this->api->plugin instanceof PluginAPI){ + if($this->api->plugin instanceof PluginAPI) { $plist = $this->api->plugin->getList(); $dump .= "Loaded plugins:\r\n"; - foreach($plist as $p){ + foreach($plist as $p) { $dump .= $p["name"] . " " . $p["version"] . " by " . $p["author"] . "\r\n"; } $dump .= "\r\n\r\n"; } $extensions = []; - foreach(get_loaded_extensions() as $ext){ + foreach(get_loaded_extensions() as $ext) { $extensions[$ext] = phpversion($ext); } @@ -759,7 +806,7 @@ class PocketMinecraftServer{ console("[SEVERE] Please submit the \"{$name}.log\" file to the Bug Reporting page. Give as much info as you can.", true, true, 0); } - public function debugInfo($console = false){ + public function debugInfo($console = false) { $info = []; $info["tps"] = $this->getTPS(); $info["memory_usage"] = round((memory_get_usage() / 1024) / 1024, 2) . "MB"; @@ -773,33 +820,33 @@ class PocketMinecraftServer{ $info["actions"] = $info["actions"]["count"]; $info["garbage"] = gc_collect_cycles(); $this->handle("server.debug", $info); - if($console === true){ + if($console === true) { console("[INFO] TPS: " . $info["tps"] . ", Memory usage: " . $info["memory_usage"] . " (Peak " . $info["memory_peak_usage"] . "), Entities: " . $info["entities"] . ", Events: " . $info["events"] . ", Handlers: " . $info["handlers"] . ", Actions: " . $info["actions"] . ", Garbage: " . $info["garbage"], true, true); } return $info; } - public function checkMemory(){ + public function checkMemory() { $info = $this->debugInfo(); $data = $info["memory_usage"] . "," . $info["players"] . "," . $info["entities"]; $i = count($this->memoryStats) - 1; - if($i < 0 or $this->memoryStats[$i] !== $data){ + if($i < 0 or $this->memoryStats[$i] !== $data) { $this->memoryStats[] = $data; } } - public function event($event, callable $func){ - if(!is_callable($func)){ + public function event($event, callable $func) { + if(!is_callable($func)) { return false; - }elseif(isset(Deprecation::$events[$event])){ + } elseif(isset(Deprecation::$events[$event])) { $sub = ""; - if(Deprecation::$events[$event] !== false){ + if(Deprecation::$events[$event] !== false) { $sub = " Substitute \"" . Deprecation::$events[$event] . "\" found."; } console("[ERROR] Event \"$event\" has been deprecated.$sub [Attach to " . (is_array($func) ? get_class($func[0]) . "::" . $func[1] : $func) . "]"); } $evid = $this->evCnt++; - if(!isset($this->events[$event])){ + if(!isset($this->events[$event])) { $this->events[$event] = []; } $this->events[$event][$evid] = $func; diff --git a/src/astarnavigator/TileNavigator.php b/src/astarnavigator/TileNavigator.php index c29c4fec..24769b51 100644 --- a/src/astarnavigator/TileNavigator.php +++ b/src/astarnavigator/TileNavigator.php @@ -1,14 +1,15 @@ entity = $entity; $this->tall = $entity->height > 1; } - + public function reconstructPath($path, $current){ console("path built"); $totalPath = [$current]; - while (isset($path[(string)$current])) + while (isset($path[(string) $current])) { - $current = $path[(string)$current]; + $current = $path[(string) $current]; $totalPath[] = $current; } //foreach(array_unique($path) as $k => $p) console($k.":".$p); @@ -33,17 +34,17 @@ class TileNavigator } public function isLocBlocked(PMFLevel $level, $loc){ - + $chunkX = $loc >> 20 & 0xf; $chunkZ = $loc >> 12 & 0xf; $chunkY = $loc >> 4 & 0xf; - + $index = $chunkZ << 4 | $chunkX; - + $blockX = $loc >> 16 & 0xf; $blockZ = $loc >> 8 & 0xf; $blockY = $loc & 0xf; - + $id = $level->fastGetBlockID($chunkX, $chunkY, $chunkZ, $blockX, $blockY, $blockZ, $index); $b = StaticBlock::getIsSolid($id); if($this->tall){ @@ -53,33 +54,32 @@ class TileNavigator $id = $level->fastGetBlockID($chunkX, $chunkY, $chunkZ, $blockX, $blockY, $blockZ, $index); return $b || StaticBlock::getIsSolid($id); } - + return $b; } - - + public function navigate(Level $level, $fromX, $fromY, $fromZ, $toX, $toY, $toZ, $maxDist) { $from = $fromX << 16 | $fromZ << 8 | $fromY; $to = $toX << 16 | $toZ << 8 | $toY; - + $pmfLevel = $level->level; - + $open = new SplPriorityQueue(); //$open->insert(, 0); $open->insert($from, 0); $path = []; $gScore = []; $gScore[(string) $from] = 0; - $has = [(string)$from, true]; + $has = [(string) $from, true]; if($this->isLocBlocked($pmfLevel, $to)){ return null; } $visited = []; - $maxDist*=$maxDist; //no square root + $maxDist *= $maxDist; //no square root $closestToTarget = null; $closestToTargetDist = INF; - + while(!$open->isEmpty()) { $current = $open->top(); @@ -90,7 +90,7 @@ class TileNavigator if ($current == $to){ return $this->reconstructPath($path, $current); } - + $points = []; foreach(self::OFFSETS as $offset){ $newX = $offset[0] + $currentX; @@ -98,7 +98,7 @@ class TileNavigator $newZ = $offset[2] + $currentZ; if($newX < 0 || $newX > 255 || $newY < 0 || $newY > 255 || $newZ < 0 || $newZ > 255) continue; $new = $newX << 16 | $newZ << 8 | $newY; - + if(!$this->isLocBlocked($pmfLevel, $new)){ if($newY > 0 && !$this->isLocBlocked($pmfLevel, $new - 1)){ if(!$this->avoidFallDamage){ @@ -108,60 +108,59 @@ class TileNavigator if($this->isLocBlocked($pmfLevel, $new - 2)){ $new -= 1; $newY -= 1; - }else if($this->isLocBlocked($pmfLevel, $new - 3)){ + }elseif($this->isLocBlocked($pmfLevel, $new - 3)){ $new -= 2; $newY -= 2; - }else if($this->isLocBlocked($pmfLevel, $new - 4)){ + }elseif($this->isLocBlocked($pmfLevel, $new - 4)){ $new -= 3; $newY -= 3; }else{ - continue; + continue; } } } - $dist = ($fromX - $newX)*($fromX - $newX) + ($fromY - $newY)*($fromY - $newY) + ($fromZ - $newZ)*($fromZ - $newZ); + $dist = ($fromX - $newX) * ($fromX - $newX) + ($fromY - $newY) * ($fromY - $newY) + ($fromZ - $newZ) * ($fromZ - $newZ); if($dist < -$maxDist || $dist > $maxDist){ continue; } - + $points[] = $new; - }else if(!$this->isLocBlocked($pmfLevel, $current+1) && !$this->isLocBlocked($pmfLevel, $new+1)){ + }elseif(!$this->isLocBlocked($pmfLevel, $current + 1) && !$this->isLocBlocked($pmfLevel, $new + 1)){ $new += 1; $newY += 1; - - $dist = ($fromX - $newX)*($fromX - $newX) + ($fromY - $newY)*($fromY - $newY) + ($fromZ - $newZ)*($fromZ - $newZ); + + $dist = ($fromX - $newX) * ($fromX - $newX) + ($fromY - $newY) * ($fromY - $newY) + ($fromZ - $newZ) * ($fromZ - $newZ); if($dist < -$maxDist || $dist > $maxDist){ continue; } - + $points[] = $new; } } - + foreach($points as $neighbor) { if(isset($visited[$neighbor])){ continue; } $visited[$neighbor] = true; - + $diffX = ($toX - ($neighbor >> 16 & 0xff)); $diffY = ($toY - ($neighbor & 0xff)); $diffZ = ($toZ - ($neighbor >> 8 & 0xff)); - $targetDist = $diffX*$diffX + $diffY*$diffY + $diffZ*$diffZ; - - + $targetDist = $diffX * $diffX + $diffY * $diffY + $diffZ * $diffZ; + $diffX = ($currentX - ($neighbor >> 16 & 0xff)); $diffY = ($currentY - ($neighbor & 0xff)); $diffZ = ($currentZ - ($neighbor >> 8 & 0xff)); - - $distbetweenCost = $diffX*$diffX + $diffY*$diffY + $diffZ*$diffZ; + + $distbetweenCost = $diffX * $diffX + $diffY * $diffY + $diffZ * $diffZ; $tentativeG = $gScore[$current] + $distbetweenCost; if (!isset($has[$neighbor])) - { + { //if(isset($open[-$tentativeG])) console("overwriting $tentativeG"); //$open[-$tentativeG] = $neighbor; - + $open->insert($neighbor, -$tentativeG); $has[$neighbor] = true; } @@ -171,26 +170,25 @@ class TileNavigator } if(!isset($gScore[$neighbor]) || $distbetweenCost < $gScore[$neighbor]){ $path[$neighbor] = $current; - + if($targetDist < $closestToTargetDist){ //console("found new best to target: $neighbor($closestToTargetDist -> $targetDist)"); - + $closestToTarget = $neighbor; - $closestToTargetDist = $targetDist; + $closestToTargetDist = $targetDist; } } - + $gScore[$neighbor] = $tentativeG; } } - + if($closestToTarget != null){ return $this->reconstructPath($path, $closestToTarget); } - + return null; } - -} + } diff --git a/src/config.php b/src/config.php index b592e4d7..f199ba71 100644 --- a/src/config.php +++ b/src/config.php @@ -1,4 +1,5 @@ true, MOB_PIG => true, MOB_SHEEP => true, - + MOB_ZOMBIE => true, MOB_SKELETON => true, MOB_SPIDER => true, @@ -64,5 +64,5 @@ const CORRECT_ENTITY_CLASSES = [ ENTITY_FALLING => [ FALLING_SAND => true ] - + ]; diff --git a/src/dependencies.php b/src/dependencies.php index 5af85a1c..54260b95 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -103,7 +103,6 @@ require_once(FILE_PATH . "/src/pmf/PMF.php"); require_once(FILE_PATH . "/src/astarnavigator/TileNavigator.php"); - require_once(FILE_PATH . "/src/entity/ai/tasks/TaskBase.php"); require_once(FILE_PATH . "/src/entity/Rideable.php"); @@ -138,8 +137,6 @@ require_once(FILE_PATH . "/src/world/generator/vanilla/feature/Feature.php"); require_all(FILE_PATH . "src/"); - - $inc = get_included_files(); $inc[] = array_shift($inc); $srcdir = realpath(FILE_PATH . "src/"); diff --git a/src/entity/Animal.php b/src/entity/Animal.php index b21aab0d..f9ccf9be 100644 --- a/src/entity/Animal.php +++ b/src/entity/Animal.php @@ -1,17 +1,15 @@ setAge($data["Age"] ?? 0); @@ -20,11 +18,11 @@ abstract class Animal extends Creature implements Ageable, Breedable{ } $this->searchForClosestPlayers = true; } - + public function isPlayerValid(Player $player){ return $player->spawned && $this->isFood($player->getHeldItem()->id) && !$player->entity->dead; } - + public function handlePrePlayerSearcher(){ parent::handlePrePlayerSearcher(); if($this->closestPlayerThatCanFeedEID !== false){ @@ -33,15 +31,15 @@ abstract class Animal extends Creature implements Ageable, Breedable{ $this->closestPlayerThatCanFeedEID = false; $this->closestPlayerThatCanFeedDist = INF; }else{ - $dist = ($this->x - $player->x)*($this->x - $player->x) + ($this->y - $player->y)*($this->y - $player->y) + ($this->z - $player->z)*($this->z - $player->z); + $dist = ($this->x - $player->x) * ($this->x - $player->x) + ($this->y - $player->y) * ($this->y - $player->y) + ($this->z - $player->z) * ($this->z - $player->z); $this->closestPlayerThatCanFeedDist = $dist; } } } - + public function handlePlayerSearcher(Player $player, $dist){ parent::handlePlayerSearcher($player, $dist); - + if($this->closestPlayerThatCanFeedDist >= $dist){ if($this->isPlayerValid($player)){ $this->closestPlayerThatCanFeedDist = $dist; @@ -49,18 +47,18 @@ abstract class Animal extends Creature implements Ageable, Breedable{ } } } - + public function harm($dmg, $cause = "generic", $force = false){ $ret = parent::harm($dmg, $cause, $force); $this->inPanic |= ($ret && is_numeric($cause)); $this->inLove = false; return $ret; } - + public function isBaby(){ return $this->getAge() < 0; } - + public function breed(){ if($this->server->dhandle("entity.animal.breed", ["parent" => $this]) !== false){ $c = $this->spawnChild(); @@ -70,22 +68,22 @@ abstract class Animal extends Creature implements Ageable, Breedable{ } return false; } - + public function resetInLove(){ $this->inLove = 0; } - + public function canMate($ent){ return $ent->eid != $this->eid && $this->type === $ent->type && $this->isInLove() && $ent->isInLove(); } - + public function update($now){ parent::update($now); - + if($this->loveTimeout > 0){ --$this->loveTimeout; } - + $age = $this->getAge() + 1; //100 - fast. debug, 1 - normal if($age >= 0 && $this->isBaby()){ $this->setAge($age); @@ -99,15 +97,15 @@ abstract class Animal extends Creature implements Ageable, Breedable{ unset($this->level->entitiesInLove[$this->eid]); } } - + public function getAge(){ return $this->age; } - + public function setAge($i){ $this->age = $i; } - + public function spawnChild() { return $this->server->api->entity->add($this->level, $this->class, $this->type, [ @@ -118,24 +116,24 @@ abstract class Animal extends Creature implements Ageable, Breedable{ "Age" => -24000, ]); } - + public function getMetadata(){ $d = parent::getMetadata(); $d[14]["value"] = $this->isBaby(); return $d; } - + public function createSaveData(){ $data = parent::createSaveData(); $data["IsBaby"] = $this->isBaby(); $data["Age"] = $this->getAge(); return $data; } - + public function isInLove(){ return $this->inLove > 0; } - + public function interactWith(Entity $e, $action){ if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){ $slot = $e->player->getHeldItem(); @@ -149,14 +147,14 @@ abstract class Animal extends Creature implements Ageable, Breedable{ } parent::interactWith($e, $action); } - + public function counterUpdate(){ parent::counterUpdate(); if($this->isInLove()){ --$this->inLove; } } - + public function getDrops(){ if($this->isBaby()){ return []; diff --git a/src/entity/Breedable.php b/src/entity/Breedable.php index 59cc21ec..211c6c7e 100644 --- a/src/entity/Breedable.php +++ b/src/entity/Breedable.php @@ -2,8 +2,8 @@ interface Breedable{ public function isFood($id); //original name - + public function spawnChild(); - + public function isInLove(); } \ No newline at end of file diff --git a/src/entity/Creature.php b/src/entity/Creature.php index a8e97972..be41e0e5 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -2,12 +2,11 @@ abstract class Creature extends Living{ const CLASS_TYPE = ENTITY_MOB; - + public $inPanic; public $closestPlayerEID = false; public $closestPlayerDist = INF; - - + public function __construct(Level $level, $eid, $class, $type = 0, $data = []){ $this->inPanic = false; //force for now parent::__construct($level, $eid, $class, $type, $data); @@ -15,13 +14,12 @@ abstract class Creature extends Living{ $this->enableAutojump = true; $this->searchForClosestPlayers = true; } - + public function update($now){ $this->handlePrePlayerSearcher(); return parent::update($now); } - - + public function handlePrePlayerSearcher(){ parent::handlePrePlayerSearcher(); if($this->closestPlayerEID !== false){ @@ -30,12 +28,12 @@ abstract class Creature extends Living{ $this->closestPlayerEID = false; $this->closestPlayerDist = INF; }else{ - $dist = ($this->x - $player->x)*($this->x - $player->x) + ($this->y - $player->y)*($this->y - $player->y) + ($this->z - $player->z)*($this->z - $player->z); + $dist = ($this->x - $player->x) * ($this->x - $player->x) + ($this->y - $player->y) * ($this->y - $player->y) + ($this->z - $player->z) * ($this->z - $player->z); $this->closestPlayerDist = $dist; } } } - + public function handlePlayerSearcher(Player $player, $dist){ parent::handlePlayerSearcher($player, $dist); if($this->closestPlayerDist >= $dist){ @@ -43,13 +41,13 @@ abstract class Creature extends Living{ $this->closestPlayerDist = $dist; } } - + public function createSaveData(){ $data = parent::createSaveData(); $data["State"] = @$this->getState(); return $data; } - + public function getSpeedModifer(){ return 0.7; } @@ -71,19 +69,19 @@ abstract class Creature extends Living{ $pk->z = $this->z; $pk->yaw = $this->yaw; $pk->pitch = $this->pitch; - $pk->metadata = $this->getMetadata(); + $pk->metadata = $this->getMetadata(); $player->dataPacket($pk); - + $pk = new SetEntityMotionPacket; $pk->eid = $this->eid; $pk->speedX = $this->speedX; $pk->speedY = $this->speedY; $pk->speedZ = $this->speedZ; $player->dataPacket($pk); - + if($this->linkedEntity != 0 && $this->isRider){ $player->eventHandler(["rider" => $this->eid, "riding" => $this->linkedEntity, "type" => 0], "entity.link"); } } - + } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index e4f5383c..82678e6f 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -3,10 +3,10 @@ class Entity extends Position { - const TYPE = - 1; - const CLASS_TYPE = - 1; - const MIN_POSSIBLE_SPEED = 1/8000; //anything below will send 0 to player - + const TYPE = -1; + const CLASS_TYPE = -1; + const MIN_POSSIBLE_SPEED = 1 / 8000; //anything below will send 0 to player + public $searchForClosestPlayers = false; public $modifySpeedY = false; public $modifedSpeedY = 0.0; @@ -36,17 +36,17 @@ class Entity extends Position public $delayBeforePickup; public $x, $y, $z; public $speedX, $speedY, $speedZ, $speed; - public $lastX = 0, $lastY = 0, $lastZ = 0, $lastYaw = 0, $lastPitch = 0, $lastTime = 0, $lastHeadYaw = 0, $lastSpeedX = 0, $lastSpeedY = 0, $lastSpeedZ = 0; + public $lastX = 0, $lastY = 0, $lastZ = 0, $lastYaw = 0, $lastPitch = 0, $lastTime = 0, $lastHeadYaw = 0, $lastSpeedX = 0, $lastSpeedY = 0, $lastSpeedZ = 0; /** - * 0 = lastX, - * 1 = lastY, - * 2 = lastZ, - * 3 = lastYaw, - * 4 = lastPitch, - * 5 = lastTime. - * + * 0 = lastX, + * 1 = lastY, + * 2 = lastZ, + * 3 = lastYaw, + * 4 = lastPitch, + * 5 = lastTime. + * * It is not recommended to use this and it is left as a backwards compability. - * + * * @var array */ public $last; @@ -65,7 +65,7 @@ class Entity extends Position public $fallStart; private $state; private $tickCounter; - private $speedMeasure = array(0, 0, 0, 0, 0, 0, 0); + private $speedMeasure = [0, 0, 0, 0, 0, 0, 0]; public $server; private $isStatic; public $level; @@ -87,30 +87,30 @@ class Entity extends Position public $onGround, $inWater; public $carryoverDamage; public $gravity; - + public $stepHeight = 0.5; public $enableAutojump = false; public $yOffset = 0.0; public $noClip = false; - + public $chunkX = 0; public $chunkZ = 0; - + public $isCollidedHorizontally, $isCollidedVertically, $isCollided; /** * Amount of ticks you can be in fire until you start receiving damage * @var integer */ public $fireResistance = 1; - + public $isImmuneToFire = false; - + public $inWeb; public $inLava; public $notOnGroundTicks = 0; - + public $moveStrafing, $moveForward; - + function __construct(Level $level, $eid, $class, $type = 0, $data = []) { $this->random = new Random(); @@ -131,7 +131,7 @@ class Entity extends Position $this->status = 0; $this->health = 20; $this->hasGravity = false; - $this->dmgcounter = array(0, 0, 0); + $this->dmgcounter = [0, 0, 0]; $this->air = $this->maxAir; $this->fire = 0; $this->crouched = false; @@ -154,14 +154,14 @@ class Entity extends Position $this->yaw = isset($this->data["yaw"]) ? (float) $this->data["yaw"] : 0; $this->headYaw = $this->data["headYaw"] ?? $this->yaw; $this->pitch = isset($this->data["pitch"]) ? (float) $this->data["pitch"] : 0; - $this->position = array( + $this->position = [ "level" => $this->level, "x" => &$this->x, "y" => &$this->y, "z" => &$this->z, "yaw" => &$this->yaw, "pitch" => &$this->pitch - ); + ]; $this->moveTime = 0; $this->lookTime = 0; $this->onGround = false; @@ -176,7 +176,7 @@ class Entity extends Position $this->hasGravity = true; $this->canBeAttacked = true; $this->fireResistance = 20; - + break; case ENTITY_OBJECT: if($this->type == OBJECT_PAINTING) break; @@ -197,7 +197,7 @@ class Entity extends Position } } public function handlePrePlayerSearcher(){ - + } public function isType(){ return in_array($this->type, func_get_args()); @@ -210,12 +210,12 @@ class Entity extends Position public function attackEntity($entity, $distance){ return false; } - + public function setInWeb(){ $this->inWeb = true; $this->fallDistance = 0; } - + public function addVelocity($vX, $vY = 0, $vZ = 0) { if($vX instanceof Vector3){ @@ -225,11 +225,11 @@ class Entity extends Position $this->speedY += $vY; $this->speedZ += $vZ; } - + public function isPushable(){ return $this->isPlayer(); } - + public function applyCollision(Entity $collided){ if(!($this->isPlayer() && $collided->isPlayer()) && $this->eid != $collided->eid){ $diffX = $collided->x - $this->x; @@ -239,7 +239,7 @@ class Entity extends Position $sqrtMax = sqrt($maxDiff); $diffX /= $sqrtMax; $diffZ /= $sqrtMax; - + $col = (($v = 1 / $sqrtMax) > 1 ? 1 : $v); $diffX *= $col; $diffZ *= $col; @@ -250,7 +250,7 @@ class Entity extends Position } } } - + public function isMovingHorizontally() { return ($this->speedX >= self::MIN_POSSIBLE_SPEED || $this->speedX <= -self::MIN_POSSIBLE_SPEED) || ($this->speedZ >= self::MIN_POSSIBLE_SPEED || $this->speedZ <= -self::MIN_POSSIBLE_SPEED); @@ -345,26 +345,26 @@ class Entity extends Position if(self::$keepInventory){ return []; } - for($i = 0; $i < PLAYER_SURVIVAL_SLOTS; ++ $i){ + for($i = 0; $i < PLAYER_SURVIVAL_SLOTS; ++$i){ $slot = $this->player->getSlot($i); $this->player->setSlot($i, BlockAPI::getItem(AIR, 0, 0)); if($slot->getID() !== AIR and $slot->count > 0){ - $inv[] = array( + $inv[] = [ $slot->getID(), $slot->getMetadata(), $slot->count - ); + ]; } } - for($re = 0; $re < 4; $re ++){ + for($re = 0; $re < 4; $re++){ $slot = $this->player->getArmor($re); $this->player->setArmor($re, BlockAPI::getItem(AIR, 0, 0)); if($slot->getID() !== AIR and $slot->count > 0){ - $inv[] = array( + $inv[] = [ $slot->getID(), $slot->getMetadata(), $slot->count - ); + ]; } } return $inv; @@ -378,11 +378,11 @@ class Entity extends Position $this->server->api->entity->drop($this, BlockAPI::getItem($drop[0] & 0xFFFF, $drop[1] & 0xFFFF, $drop[2] & 0xFF)); } } - + public function canSee(Entity $e){ return $this->level->rayTraceBlocks(new Vector3($this->x, $this->y + $this->getEyeHeight(), $this->z), new Vector3($e->x, $e->y + $e->getEyeHeight(), $e->z)) == null; } - + /** * Can entity be collided with or not * @return boolean @@ -390,12 +390,12 @@ class Entity extends Position public function isPickable(){ return $this->isPlayer() && !$this->dead; } - + public function handleLavaMovement(){ //TODO maybe try merging with water? $this->inLava = $this->level->isLavaInBB($this->boundingBox->expand(-0.1, -0.4, -0.1)); return $this->inLava; } - + public function handleWaterMovement(){ if($this->level->handleMaterialAcceleration($this->boundingBox->expand(0, -0.4, 0)->contract(0.001, 0.001, 0.001), 0, $this)){ $this->fallDistance = 0; @@ -406,39 +406,39 @@ class Entity extends Position }else{ $this->fire = 0; } - + }else{ $this->inWater = false; } - + return $this->inWater; } - + public function environmentUpdate($time) { $hasUpdate = $this->class === ENTITY_MOB; // force true for mobs - + $tickDiff = ($time - $this->lastUpdate) / 0.05; if($this->attackTimeout > 0) $this->attackTimeout -= $tickDiff; if($this->attackTimeout < 0) $this->attackTimeout = 0; - + if($this->isPlayer() && $this->player->spawned === true && $this->player->blocked !== true && !$this->dead){ $myBB = $this->boundingBox->grow(1, 0.5, 1); foreach($this->server->api->entity->getRadius($this, 2, false) as $item){ if($item->class === ENTITY_ITEM && !$item->closed && $item->spawntime > 0 && $item->delayBeforePickup <= 0){ - if($item->boundingBox->intersectsWith($myBB)){ - if((($this->player->gamemode & 0x01) === 1 || $this->player->hasSpace($item->itemID, $item->meta, $item->stack) === true) && $this->server->api->dhandle("player.pickup", array( + if($item->boundingBox->intersectsWith($myBB)){ + if((($this->player->gamemode & 0x01) === 1 || $this->player->hasSpace($item->itemID, $item->meta, $item->stack) === true) && $this->server->api->dhandle("player.pickup", [ "eid" => $this->player->eid, "player" => $this->player, "entity" => $item, "block" => $item->itemID, "meta" => $item->meta, "target" => $item->eid - )) !== false){ + ]) !== false){ $item->close(); } } - }else if($item->class == ENTITY_OBJECT && !$item->closed && $item->type == OBJECT_ARROW && $item->shotByPlayer && $item->inGround && $item->shake <= 0 && $item->boundingBox->intersectsWith($myBB)){ + }elseif($item->class == ENTITY_OBJECT && !$item->closed && $item->type == OBJECT_ARROW && $item->shotByPlayer && $item->inGround && $item->shake <= 0 && $item->boundingBox->intersectsWith($myBB)){ if(($this->player->gamemode & 0x01) == 1 || $this->player->hasSpace(ARROW, 0, 1)){ $this->player->addItem(ARROW, 0, 1); $item->close(); @@ -463,7 +463,7 @@ class Entity extends Position $this->air = $this->maxAir; return false; } - + $this->handleWaterMovement(); if($this->fire > 0){ //TODO move somewhere if(!$this->isImmuneToFire){ @@ -475,7 +475,7 @@ class Entity extends Position $this->fire -= 4; if($this->fire <= 0) $this->fire = 0; } - + if($this->fire <= 0){ $this->updateMetadata(); } else{ @@ -487,16 +487,16 @@ class Entity extends Position if(!$this->isImmuneToFire){ $this->harm(4, "fire"); $oldOnFire = $this->fire; - if($oldOnFire < 20*30) $this->fire = 20*30; //30 seconds + if($oldOnFire < 20 * 30) $this->fire = 20 * 30; //30 seconds } } - + if($this->isInVoid()){ $this->outOfWorld(); $hasUpdate = true; } - - if(!($this instanceof Painting) && !($this->isPlayer() && $this->player->isSleeping !== false)){ //TODO better way to fix + + if(!($this instanceof Painting) && !($this->isPlayer() && $this->player->isSleeping !== false)){ //TODO better way to fix $x = floor($this->x); $y = floor($this->y + $this->getEyeHeight()); $z = floor($this->z); @@ -504,14 +504,14 @@ class Entity extends Position $this->harm(1, "suffocation"); } } - + //air damage if($this->isPlayer() || $this instanceof Living){ $d = $this->y + $this->getEyeHeight(); $x = floor($this->x); $y = floor($d); $z = floor($this->z); - + $id = $this->level->level->getBlockID($x, $y, $z); if($id == WATER || $id == STILL_WATER){ $f = LiquidBlock::getPercentAir($this->level->level->getBlockDamage($x, $y, $z)) - 0.1111111; @@ -531,28 +531,28 @@ class Entity extends Position } return $hasUpdate; } - + public function moveFlying($strafe, $forward, $speed){ //TODO rename? - $v4 = $strafe*$strafe + $forward*$forward; - + $v4 = $strafe * $strafe + $forward * $forward; + if($v4 >= 0.0001){ $v4 = sqrt($v4); if($v4 < 1) $v4 = 1; - + $v4 = $speed / $v4; $strafe *= $v4; $forward *= $v4; - - $v5 = sin($this->yaw * M_PI/180); - $v6 = cos($this->yaw * M_PI/180); - - $this->speedX += $strafe*$v6 - $forward*$v5; - $this->speedZ += $forward*$v6 + $strafe*$v5; + + $v5 = sin($this->yaw * M_PI / 180); + $v6 = cos($this->yaw * M_PI / 180); + + $this->speedX += $strafe * $v6 - $forward * $v5; + $this->speedZ += $forward * $v6 + $strafe * $v5; } } - + public function updateEntityMovement(){} - + public function doBlocksCollision(){ $minY = floor($this->boundingBox->minY + 0.001); $maxY = floor($this->boundingBox->maxY - 0.001); @@ -560,91 +560,88 @@ class Entity extends Position $minZ = floor($this->boundingBox->minZ + 0.001); $maxX = floor($this->boundingBox->maxX - 0.001); $maxZ = floor($this->boundingBox->maxZ - 0.001); - + for($x = $minX; $x <= $maxX; ++$x){ for($y = $minY; $y <= $maxY; ++$y){ for($z = $minZ; $z <= $maxZ; ++$z){ $id = $this->level->level->getBlockID($x, $y, $z); - + if($id > 0){ StaticBlock::$prealloc[$id]::onEntityCollidedWithBlock($this->level, $x, $y, $z, $this); } } } } - + } - + public function move($dx, $dy, $dz){ $movX = $this->x; $movY = $this->y; $movZ = $this->z; - + if($this->inWeb){ $this->inWeb = false; $dx *= 0.25; $dy *= 0.05; $dz *= 0.25; - + $this->speedX = $this->speedY = $this->speedZ = 0; } - - + $savedDX = $dx; $savedDY = $dy; $savedDZ = $dz; - + $oldBB = clone $this->boundingBox; - - + $aaBBs = $this->level->getCubes($this, $this->boundingBox->addCoord($dx, $dy, $dz)); foreach($aaBBs as $bb){ $dy = $bb->calculateYOffset($this->boundingBox, $dy); } $this->boundingBox->offset(0, $dy, 0); - + foreach($aaBBs as $bb){ $dx = $bb->calculateXOffset($this->boundingBox, $dx); } $this->boundingBox->offset($dx, 0, 0); - + foreach($aaBBs as $bb){ $dz = $bb->calculateZOffset($this->boundingBox, $dz); } $this->boundingBox->offset(0, 0, $dz); - - + $fallingFlag = $this->onGround || $savedDY != $dy && $savedDY < 0; - + if($this->stepHeight > 0 && $fallingFlag && ($savedDX != $dx || $savedDZ != $dz)){ $cx = $dx; $cy = $dy; $cz = $dz; - + $dx = $savedDX; $dy = $this->stepHeight; $dz = $savedDZ; $aabb1 = clone $this->boundingBox; $this->boundingBox->setBB($oldBB); - + $aaBBs = $this->level->getCubes($this, $this->boundingBox->addCoord($dx, $dy, $dz)); - + foreach($aaBBs as $bb){ $dy = $bb->calculateYOffset($this->boundingBox, $dy); } $this->boundingBox->offset(0, $dy, 0); - + foreach($aaBBs as $bb){ $dx = $bb->calculateXOffset($this->boundingBox, $dx); } $this->boundingBox->offset($dx, 0, 0); - + foreach($aaBBs as $bb){ $dz = $bb->calculateZOffset($this->boundingBox, $dz); } $this->boundingBox->offset(0, 0, $dz); - - if ($cx*$cx + $cz*$cz >= $dx*$dx + $dz*$dz) + + if ($cx * $cx + $cz * $cz >= $dx * $dx + $dz * $dz) { $dx = $cx; $dy = $cy; @@ -655,7 +652,7 @@ class Entity extends Position $this->modifedSpeedY = 0.5; } } - + $this->x = ($this->boundingBox->minX + $this->boundingBox->maxX) / 2; $this->y = $this->boundingBox->minY + $this->yOffset; $this->z = ($this->boundingBox->minZ + $this->boundingBox->maxZ) / 2; @@ -664,49 +661,46 @@ class Entity extends Position $this->onGround = $savedDY != $dy && $savedDY < 0.0; $this->isCollided = $this->isCollidedHorizontally || $this->isCollidedVertically; $this->updateFallState($this->speedY); - - + if($savedDX != $dx) $this->speedX = 0; if($savedDY != $dy) $this->speedY = 0; if($savedDZ != $dz) $this->speedZ = 0; - - - + //TODO more stuff -> onEntityWalking - + $this->doBlocksCollision(); - + $oldFire = $this->fire; if($this->level->isBoundingBoxOnFire($this->boundingBox->contract(0.001, 0.001, 0.001))){ $this->harm(1, "fire"); if(!$this->inWater){ ++$this->fire; - if($this->fire == 0) $this->fire = 8*20; + if($this->fire == 0) $this->fire = 8 * 20; } }elseif($this->fire <= 0){ $this->fire = -$this->fireResistance; } - + if($this->inWater && $this->fire > 0){ $this->fire = -$this->fireResistance; } - + if(($oldFire > 0 && $this->fire <= 0) || ($oldFire <= 0 && $this->fire > 0)){ $this->updateMetadata(); //TODO rewrite metadata } } - + //in MCP it is called isOffsetPositionInLiquid, in 0.8 - isFree public function isFree($offsetX, $offsetY, $offsetZ){ $offsetBB = $this->boundingBox->getOffsetBoundingBox($offsetX, $offsetY, $offsetZ); - + $minX = floor($offsetBB->minX); $minY = floor($offsetBB->minY); $minZ = floor($offsetBB->minZ); $maxX = floor($offsetBB->maxX + 1); $maxY = floor($offsetBB->maxY + 1); $maxZ = floor($offsetBB->maxZ + 1); - + $hasLiquid = false; $result = false; for($x = $minX; $x < $maxX; ++$x){ @@ -724,20 +718,20 @@ class Entity extends Position if($result) return !$hasLiquid; return false; } - + public function isInVoid(){ return $this->y < -1.6; } - + public function handlePlayerSearcher(Player $player, $dist){ - + } - + public function update($now){ if($this->closed === true){ return false; } - + if($this->check === false){ $this->lastUpdate = $now; return; @@ -748,7 +742,7 @@ class Entity extends Position return false; } ++$this->counter; - + if($this->isStatic === false){ if(!$this->isPlayer()){ $this->updateLast(); @@ -761,18 +755,18 @@ class Entity extends Position }elseif ($this->lastYaw != $this->yaw || $this->lastPitch != $this->pitch || $this->lastHeadYaw != $this->headYaw) { $update = true; } - + if($update === true){ $hasUpdate = true; } - + } else{ $prevGroundState = $this->onGround; $this->onGround = false; $this->speedX = -($this->lastX - $this->x); $this->speedY = -($this->lastY - $this->y); $this->speedZ = -($this->lastZ - $this->z); - + $contractedCollisionBB = $this->boundingBox->contract(0.001, 0.001, 0.001); $fireMinX = floor($contractedCollisionBB->minX); $fireMinY = floor($contractedCollisionBB->minY); @@ -780,13 +774,13 @@ class Entity extends Position $fireMaxX = floor($contractedCollisionBB->maxX + 1); $fireMaxY = floor($contractedCollisionBB->maxY + 1); $fireMaxZ = floor($contractedCollisionBB->maxZ + 1); - + $bbbottom = $this->boundingBox->addCoord(0, -0.05, 0); $bbbottom->minX = round($this->x, 3) - $this->radius; $bbbottom->maxX = round($this->x, 3) + $this->radius; $bbbottom->minZ = round($this->z, 3) - $this->radius; $bbbottom->maxZ = round($this->z, 3) + $this->radius; - + $handleFire = false; $handleCactus = false; for($x = floor($this->boundingBox->minX); $x < ceil($this->boundingBox->maxX); ++$x){ @@ -800,11 +794,11 @@ class Entity extends Position ++$intersects; } } - - if($id == WATER || $id === STILL_WATER || $id === COBWEB || $id == LAVA || $id === STILL_LAVA){ + + if($id == WATER || $id === STILL_WATER || $id === COBWEB || $id == LAVA || $id === STILL_LAVA){ $this->notOnGroundTicks = 0; } - + intersects: if($y <= floor($this->boundingBox->minY) && !$this->onGround){ if($intersects > 0) $this->onGround = count($bounds) > 0; @@ -815,23 +809,23 @@ class Entity extends Position $meta = $block[1]; $handleFire = $handleFire || (($id == FIRE || $id == STILL_LAVA || $id == LAVA) && $x >= $fireMinX && $x < $fireMaxX && $y >= $fireMinY && $y < $fireMaxY && $z >= $fireMinZ && $z < $fireMaxZ); $handleCactus = $handleCactus || ($id == CACTUS && $x >= $fireMinX && $x < $fireMaxX && $y >= $fireMinY && $y < $fireMaxY && $z >= $fireMinZ && $z < $fireMaxZ); - + if($id === WATER || $id === STILL_WATER || $id === COBWEB){ $this->fallDistance = 0; $this->fallStart = $this->y; } } - + } } } - + if($prevGroundState == $this->onGround && !$this->onGround && $this->player->isSleeping == false && !$this->dead){ //isSleeping may be a vector ++$this->notOnGroundTicks; - }else if($this->onGround){ + }elseif($this->onGround){ $this->notOnGroundTicks = 0; } - + if($this->notOnGroundTicks > 80){ //~70 ticks is needed to reach from 127 to 0 if(($this->player->gamemode & 1) != CREATIVE && !$this->player->blocked && $this->player->spawned){ //survival(0), adventure(2) if(!Entity::$allowFly){ @@ -839,21 +833,20 @@ class Entity extends Position } } } - - + if($this->isOnLadder()){ $this->fallDistance = 0; $this->notOnGroundTicks = 0; $this->fallStart = $this->y; } - + if(!$this->onGround && $prevGroundState){ $this->fallStart = $this->y; } - - $this->updateFallState(($this->speedY <=> 0)*0.1); + + $this->updateFallState(($this->speedY <=> 0) * 0.1); if($this->onGround) $this->fallDistance = 0; - + if($handleCactus){ $this->harm(1, "cactus"); } @@ -862,48 +855,47 @@ class Entity extends Position $this->harm(1, "fire"); if(!$this->inWater){ ++$this->fire; - if($this->fire == 0) $this->fire = 8*20; + if($this->fire == 0) $this->fire = 8 * 20; } }elseif($this->fire <= 0){ $this->fire = -$this->fireResistance; } - + if($this->inWater && $this->fire > 0){ $this->fire = -$this->fireResistance; } - + if(($oldFire > 0 && $this->fire <= 0) || ($oldFire <= 0 && $this->fire > 0)){ $this->updateMetadata(); //TODO rewrite metadata } - - $hasUpdate = true; + + $hasUpdate = true; } } - - + $this->counterUpdate(); - + if($this->isPlayer()){ $this->updateMovement(); } - + if($this->isPlayer()){ $this->player->entityTick(); } - + $this->needsUpdate = $hasUpdate; $this->lastUpdate = $now; } public function isOnLadder(){ - $x = (int)$this->x; - $y = (int)$this->boundingBox->minY; - $z = (int)$this->z; + $x = (int) $this->x; + $y = (int) $this->boundingBox->minY; + $z = (int) $this->z; return $this->level->level->getBlockID($x, $y, $z) === LADDER; } - + public function fall(){ if($this->isPlayer()){ - + $x = floor($this->x); $y = floor($this->y - 1); //TODO not 1 $z = floor($this->z); @@ -912,18 +904,18 @@ class Entity extends Position $clz = StaticBlock::getBlock($bid); $clz::fallOn($this->level, $x, $y, $z, $this, ceil($this->fallStart - $this->y)); } - + $dmg = ceil($this->fallStart - $this->y - 3); if($dmg > 0){ $this->harm($dmg, "fall"); } } } - + public function canBeShot(){ return $this->isPlayer(); } - + public function updateFallState($fallTick){ if($this->onGround && $this->fallDistance > 0){ $this->fall(); @@ -931,9 +923,9 @@ class Entity extends Position }elseif($fallTick < 0){ $this->fallDistance -= $fallTick; } - + } - + public function updateMovement() { if($this->closed === true){ @@ -966,7 +958,7 @@ class Entity extends Position } $this->lastUpdate = $now; } - + /** * Handle fall out of world */ @@ -978,11 +970,11 @@ class Entity extends Position $this->close(); } } - + public function getEyeHeight(){ //TODO in vanilla player's eyeHeight is 0.12 return $this->isPlayer() ? $this->height - 0.12 : $this->height * 0.85; } - + public function interactWith(Entity $e, $action) { if($this->isPlayer() and ($this->server->api->getProperty("pvp") == false or $this->server->difficulty <= 0 or ($this->player->gamemode & 0x01) === 0x01)){ @@ -1059,7 +1051,7 @@ class Entity extends Position ], 1 => [ "type" => 1, - "value" => (int)$this->air + "value" => (int) $this->air ], 14 => [ "type" => 0, @@ -1099,7 +1091,7 @@ class Entity extends Position public function spawn($player) { - if(! ($player instanceof Player)){ + if(!($player instanceof Player)){ $player = $this->server->api->player->get($player); } if($player->eid === $this->eid or $this->closed !== false or ($player->level !== $this->level and $this->class !== ENTITY_PLAYER)){ @@ -1155,13 +1147,13 @@ class Entity extends Position $pk->did = -$this->data["Tile"]; $player->dataPacket($pk); } - + if($this->linkedEntity != 0 && $this->isRider){ $player->eventHandler(["rider" => $this->eid, "riding" => $this->linkedEntity, "type" => 0], "entity.link"); //TODO fix it } - + } - + public function counterUpdate(){ if($this->knockbackTime > 0){ --$this->knockbackTime; @@ -1178,13 +1170,12 @@ class Entity extends Position if($this->idleTime > 0){ --$this->idleTime; } - - + if($this->inAction){ ++$this->inActionCounter; } } - + public function close() { if($this->closed === false){ @@ -1224,7 +1215,7 @@ class Entity extends Position public function updateAABB() { $this->boundingBox->setBounds( - $this->x - $this->radius, $this->y - $this->yOffset, $this->z - $this->radius, + $this->x - $this->radius, $this->y - $this->yOffset, $this->z - $this->radius, $this->x + $this->radius, $this->y + $this->height - $this->yOffset, $this->z + $this->radius ); } @@ -1234,7 +1225,7 @@ class Entity extends Position $this->sendMoveUpdate(); $this->updateAABB(); } - + public function setPosition(Vector3 $pos, $yaw = false, $pitch = false, $headYaw = false) { $this->x = $pos->x; @@ -1246,12 +1237,12 @@ class Entity extends Position if($pitch !== false){ $this->pitch = $pitch; } - + if($headYaw !== false) $this->headYaw = $headYaw; } public function inBlockNonVector($x, $y, $z, $radius = 0.8) { - return $y == ceil($this->y) || $y == (ceil($this->y)+1) && max(abs($x - ($this->x-0.5)), abs($z - ($this->z-0.5))); + return $y == ceil($this->y) || $y == (ceil($this->y) + 1) && max(abs($x - ($this->x - 0.5)), abs($z - ($this->z - 0.5))); } public function inBlock(Vector3 $block, $radius = 0.8) { @@ -1275,7 +1266,7 @@ class Entity extends Position { $me = new Vector2($this->x - 0.5, $this->z - 0.5); $diff = $this->y - $pos->y; - if($me->distance(new Vector2($pos->x, $pos->z)) < $radius and $diff > - 0.7 and $diff < 1.6){ + if($me->distance(new Vector2($pos->x, $pos->z)) < $radius and $diff > -0.7 and $diff < 1.6){ return true; } return false; @@ -1283,7 +1274,7 @@ class Entity extends Position public function resetSpeed() { - $this->speedMeasure = array(0, 0, 0, 0, 0, 0, 0); + $this->speedMeasure = [0, 0, 0, 0, 0, 0, 0]; } public function getSpeed() @@ -1333,7 +1324,7 @@ class Entity extends Position "speedX" => $this->speedX, "speedY" => $this->speedY, "speedZ" => $this->speedZ, - + ]; if($this->class === ENTITY_OBJECT){ $data["TileX"] = $this->x; @@ -1349,12 +1340,12 @@ class Entity extends Position "Damage" => $this->meta, "Count" => $this->stack, ]; - + $data["id"] = 64; //ty shoghicp } return $data; } - + public function updateLast() { $this->lastX = $this->x; @@ -1387,30 +1378,30 @@ class Entity extends Position // $dmg = 0; // break; case 1: - $dmg = (int)($dmg / 3) + 1; + $dmg = (int) ($dmg / 3) + 1; break; case 3: - $dmg = 3 * (int)($dmg / 2); + $dmg = 3 * (int) ($dmg / 2); break; } } } - + $dmg = $this->applyArmor($dmg, $cause); - $ret = $this->setHealth(max(- 128, $this->getHealth() - ((int) $dmg)), $cause, $force); - + $ret = $this->setHealth(max(-128, $this->getHealth() - ((int) $dmg)), $cause, $force); + if($ret && is_numeric($cause) && ($entity = $this->server->api->entity->get($cause)) != false){ //TODO simplify $this->attackTimeout = 10; } - + if ($ret != false && $this->hasKnockback && is_numeric($cause) && ($entity = $this->server->api->entity->get($cause)) != false) { - + $d = $entity->x - $this->x; for ($d1 = $entity->z - $this->z; $d * $d + $d1 * $d1 < 0.0001; $d1 = (lcg_value() - lcg_value()) * 0.01) { $d = (lcg_value() - lcg_value()) * 0.01; } - + $this->knockBack($d, $d1); if($this->isPlayer()){ $pk = new SetEntityMotionPacket(); @@ -1442,7 +1433,7 @@ class Entity extends Position { return $this->setHealth(min(20, $this->getHealth() + ((int) $health)), $cause); } - + public function stopRiding(){ if(isset($this->level->entityList[$this->linkedEntity])){ $e = $this->level->entityList[$this->linkedEntity]; @@ -1457,28 +1448,28 @@ class Entity extends Position $this->linkedEntity = 0; } } - + public function setPos($x, $y, $z){ $this->x = $x; $this->y = $y; $this->z = $z; - + $this->boundingBox->minX = $x - $this->radius; $this->boundingBox->minY = ($y - $this->yOffset); //TODO what is this + $this->ySize; $this->boundingBox->minZ = $z - $this->radius; - + $this->boundingBox->maxX = $x + $this->radius; $this->boundingBox->maxY = $this->boundingBox->minY + $this->height; $this->boundingBox->maxZ = $z + $this->radius; } - + public function setRiding(Entity $e, $type = 0) { if(!isset($this->level->entityList[$e->eid])){ ConsoleAPI::warn("Tried linking $this with $e that doesnt exist in the world!"); return; } - + if($this->linkedEntity == $e->eid || $e->eid == $this->eid){ $this->linkedEntity = 0; $e->linkedEntity = 0; @@ -1489,8 +1480,7 @@ class Entity extends Position $this->isRider = true; $this->server->api->dhandle("entity.link", ["rider" => $this->eid, "riding" => $e->eid, "type" => 0]); } - - + } public function isPlayer() @@ -1539,7 +1529,7 @@ class Entity extends Position } return $damage; } - + public function setHealth($health, $cause = "generic", $force = false, $allowHarm = true) { $health = (int) $health; @@ -1556,14 +1546,14 @@ class Entity extends Position }elseif($health === $this->health and !$this->dead){ $harm = true; } - - if($this->server->api->dhandle("entity.health.change", array( + + if($this->server->api->dhandle("entity.health.change", [ "entity" => $this, "eid" => $this->eid, "health" => $health, "cause" => $cause - )) !== false or $force === true){ - $this->health = min(127, max(- 127, $health)); + ]) !== false or $force === true){ + $this->health = min(127, max(-127, $health)); if($harm === true && $allowHarm){ $pk = new EntityEventPacket; $pk->eid = $this->eid; @@ -1599,7 +1589,7 @@ class Entity extends Position $this->height = $h; $this->radius = $w / 2; } - + public function makeDead($cause){ if($this->server->api->dhandle("entity.death", ["entity" => $this, "cause" => $cause]) === false) return false; $this->spawnDrops(); @@ -1644,11 +1634,11 @@ class Entity extends Position } } } - + public function getAttackDamage(){ return 0; } - + public function setSpeed($s) { $this->speed = $s; @@ -1681,14 +1671,14 @@ class Entity extends Position { return "Entity(x={$this->x},y={$this->y},z={$this->z},level=" . $this->level->getName() . ",class={$this->class},type={$this->type})"; } - + /** * Debug */ public function printSpeed($add = ""){ ConsoleAPI::debug("$add {$this->speedX}:{$this->speedY}:{$this->speedZ}"); } - + /* * Deprecated methods. * Those methods were left only for compability with older plugins @@ -1702,8 +1692,7 @@ class Entity extends Position { throw new Exception("Use getHeightOf or getWidthOf method instead of this."); } - - + /** * * @deprecated Use {@link getHeight} or {@link getWidth} instead diff --git a/src/entity/Living.php b/src/entity/Living.php index d00f9bfb..90735cc5 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -1,12 +1,11 @@ target = false; $this->ai = new EntityAI($this); $this->pathFinder = new TileNavigator($this); @@ -30,43 +29,43 @@ abstract class Living extends Entity implements Pathfindable{ $this->canBeAttacked = true; $this->hasGravity = true; $this->hasKnockback = true; - + //if(self::$despawnMobs) $this->server->schedule(self::$despawnTimer, [$this, "close"]); //900*20 } - + public function isPickable(){ return !$this->dead; } - + public function fall(){ $dmg = ceil($this->fallDistance - 3); if($dmg > 0){ $this->harm($dmg, "fall"); } } - + public function handlePlayerSearcher(Player $player, $dist){ - + } - + public function getAIMoveSpeed(){ return $this->aiMoveSpeed; } - + public function getVerticalFaceSpeed(){ return 40; //unused in 0.8 but may be useful in 0.9/0.10 } - + public function setAIMoveSpeed($speed){ $this->aiMoveSpeed = $speed; $this->moveForward = $speed; } - + public function hasPath(){ return $this->path != null; } public function eatGrass(){} - + public function close(){ parent::close(); unset($this->pathFollower->entity); @@ -76,22 +75,22 @@ abstract class Living extends Entity implements Pathfindable{ unset($this->parent); unset($this->pathFinder->entity); } - + public function canBeShot(){ return true; } - + public function isPushable(){ return !$this->dead; } - + public function collideHandler(){ $bb = $this->boundingBox->expand(0.2, 0, 0.2); - $minChunkX = ((int)($bb->minX)) >> 4; - $minChunkZ = ((int)($bb->minZ)) >> 4; - $maxChunkX = ((int)($bb->minX)) >> 4; - $maxChunkZ = ((int)($bb->minZ)) >> 4; - + $minChunkX = ((int) ($bb->minX)) >> 4; + $minChunkZ = ((int) ($bb->minZ)) >> 4; + $maxChunkX = ((int) ($bb->minX)) >> 4; + $maxChunkZ = ((int) ($bb->minZ)) >> 4; + //TODO also index by chunkY? for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){ @@ -106,14 +105,14 @@ abstract class Living extends Entity implements Pathfindable{ } } } - + public function update($now){ if(self::$despawnMobs && ++$this->ticksExisted > self::$despawnTimer){ $this->close(); return; } if($this->closed) return; - + $check = $this->level->mobSpawner->checkDespawn($this); if($check){ $this->close(); @@ -121,7 +120,7 @@ abstract class Living extends Entity implements Pathfindable{ } parent::update($now); } - + public $pathEIDS = []; private static $lastPathEID = 10000000; public static $pathfind = true; @@ -139,22 +138,21 @@ abstract class Living extends Entity implements Pathfindable{ } if(self::$pathfind){ $this->path = $this->pathFinder->navigate( - $this->level, - (int)$this->x, (int)$this->y, (int)$this->z, - //(int)$pl->entity->x, (int)$pl->entity->y, (int)$pl->entity->z, - (int)$this->x + mt_rand(-16, 16), (int)$this->y, (int)$this->z + mt_rand(-16, 16), + $this->level, + (int) $this->x, (int) $this->y, (int) $this->z, + //(int)$pl->entity->x, (int)$pl->entity->y, (int)$pl->entity->z, + (int) $this->x + mt_rand(-16, 16), (int) $this->y, (int) $this->z + mt_rand(-16, 16), 16 ); } - - + /*if($this->path){ console("Found path of length ".count($this->path)); - + foreach($this->path as $node){ $eid = self::$lastPathEID++; $this->pathEIDS[] = $eid; - + $pk = new AddItemEntityPacket(); $pk->eid = $eid; $pk->item = BlockAPI::getItem(GOLD_BLOCK, 0, 1); @@ -171,8 +169,7 @@ abstract class Living extends Entity implements Pathfindable{ } $this->pathFollower->followPath(); } - - + //not exactly here if($this->jumping){ if(!$this->inWater && !$this->inLava){ @@ -186,12 +183,11 @@ abstract class Living extends Entity implements Pathfindable{ }else{ $this->jumpTicks = 0; } - - + $this->ai->mobController->movementTick(); $this->ai->mobController->rotateTick(); $this->ai->mobController->jumpTick(); - + if(abs($this->speedX) < self::MIN_POSSIBLE_SPEED) $this->speedX = 0; if(abs($this->speedZ) < self::MIN_POSSIBLE_SPEED) $this->speedZ = 0; if(abs($this->speedY) < self::MIN_POSSIBLE_SPEED) $this->speedY = 0; @@ -203,24 +199,23 @@ abstract class Living extends Entity implements Pathfindable{ $this->landMovementFactor = $savedLandFactor; //Yaw rotation in 1.5 is handled in a bit different place but hopefully this will work too $this->ai->mobController->updateHeadYaw(); - + if(self::$entityPushing){ $this->collideHandler(); } } - + public function jump(){ $this->speedY = 0.42; - + //TODO also set AirBorne? } - + public function counterUpdate(){ parent::counterUpdate(); if($this->jumpTicks > 0) --$this->jumpTicks; } - - + public function moveEntityWithHeading($strafe, $forward){ if($this->inWater){ //also check is player, and can it fly or not $prevPosY = $this->y; @@ -230,8 +225,7 @@ abstract class Living extends Entity implements Pathfindable{ $this->speedY *= 0.8; $this->speedZ *= 0.8; $this->speedY -= 0.02; - - + if($this->isCollidedHorizontally && $this->isFree($this->speedX, $this->speedY + 0.6 - $this->y + $prevPosY, $this->speedZ)){ $this->speedY = 0.3; } @@ -243,69 +237,69 @@ abstract class Living extends Entity implements Pathfindable{ $this->speedY *= 0.5; $this->speedZ *= 0.5; $this->speedY -= 0.02; - + if($this->isCollidedHorizontally && $this->isFree($this->speedX, $this->speedY + 0.6 - $this->y + $prevPosY, $this->speedZ)){ $this->speedY = 0.3; } }else{ $friction = 0.91; - + if($this->onGround){ $friction = 0.546; $blockAt = $this->level->level->getBlockID(floor($this->x), floor($this->boundingBox->minY) - 1, floor($this->z)); - + if($blockAt > 0) $friction = StaticBlock::getSlipperiness($blockAt) * 0.91; } - - $var8 = 0.16277 / ($friction*$friction*$friction); - + + $var8 = 0.16277 / ($friction * $friction * $friction); + if($this->onGround){ $var5 = $this->getAIMoveSpeed(); //in vanilla it returns either this.seed or calls getBaseSpeed(depending on useNewAi) $var5 *= $var8; //in vanilla it also multiplies by speedModifer }else{ $var5 = $this->jumpMovementFactor; } - + $this->moveFlying($strafe, $forward, $var5); //recalculating friction, might be not neccessary $friction = 0.91; - + if($this->onGround){ $friction = 0.546; $blockAt = $this->level->level->getBlockID(floor($this->x), floor($this->boundingBox->minY) - 1, floor($this->z)); - + if($blockAt > 0) $friction = StaticBlock::getSlipperiness($blockAt) * 0.91; } - + if($this->isOnLadder()){ $speedY = $this->speedY; $this->fallDistance = 0; if($speedY < -0.15) $this->speedY = -0.15; } - + $this->move($this->speedX, $this->speedY, $this->speedZ); - + if($this->isOnLadder() && $this->isCollidedHorizontally){ $this->speedY = 0.2; } - + $this->speedY -= 0.08; //gravity - + $this->speedY *= 0.98; $this->speedX *= $friction; $this->speedZ *= $friction; - + if($this->isCollidedHorizontally && $this->enableAutojump){ $this->ai->mobController->setJumping(true); //non vanilla } } } - + public function sendMoveUpdate(){ if($this->counter % 3 != 0){ return; } parent::sendMoveUpdate(); - + } } diff --git a/src/entity/Monster.php b/src/entity/Monster.php index be046207..6e4468e2 100644 --- a/src/entity/Monster.php +++ b/src/entity/Monster.php @@ -1,17 +1,18 @@ spawned && !$player->entity->dead; } - + public function handlePrePlayerSearcher(){ parent::handlePrePlayerSearcher(); if($this->closestPlayerToAttackEID !== false){ @@ -35,15 +36,15 @@ class Monster extends Creature{ $this->closestPlayerToAttackEID = false; $this->closestPlayerToAttackDist = INF; }else{ - $dist = ($this->x - $player->x)*($this->x - $player->x) + ($this->y - $player->y)*($this->y - $player->y) + ($this->z - $player->z)*($this->z - $player->z); + $dist = ($this->x - $player->x) * ($this->x - $player->x) + ($this->y - $player->y) * ($this->y - $player->y) + ($this->z - $player->z) * ($this->z - $player->z); $this->closestPlayerToAttackDist = $dist; } } } - + public function handlePlayerSearcher(Player $player, $dist){ parent::handlePlayerSearcher($player, $dist); - + if($this->closestPlayerToAttackDist >= $dist){ if($this->isPlayerValid($player)){ $this->closestPlayerToAttackDist = $dist; @@ -51,5 +52,5 @@ class Monster extends Creature{ } } } - + } diff --git a/src/entity/Projectile.php b/src/entity/Projectile.php index 3c37424b..ed9df100 100644 --- a/src/entity/Projectile.php +++ b/src/entity/Projectile.php @@ -1,4 +1,5 @@ gravity = 0.03; $this->setSize(0.25, 0.25); - + $this->x -= cos($this->yaw / 180 * M_PI) * 0.16; $this->y -= 0.1; $this->z -= cos($this->yaw / 180 * M_PI) * 0.16; @@ -25,7 +26,7 @@ abstract class Projectile extends Entity{ $this->xTile = $data["xTile"] ?? $this->xTile; $this->yTile = $data["yTile"] ?? $this->yTile; $this->zTile = $data["zTile"] ?? $this->zTile; - + if($shooter instanceof Entity){ $this->shooterEID = $shooter->eid; if($shooter->isPlayer()){ @@ -44,9 +45,9 @@ abstract class Projectile extends Entity{ $this->shoot(, , , $throwPower, 1.0);*/ } } - + } - + public function shoot($d, $d1, $d2, $f, $f1){ //unchecked, may be not same as arrow $f2 = sqrt($d * $d + $d1 * $d1 + $d2 * $d2); $d /= $f2; @@ -66,7 +67,7 @@ abstract class Projectile extends Entity{ $this->pitch = (atan2($d1, $f3) * 180) / M_PI; $this->updatePosition(); } - + public function update($now){ $this->lastX = $this->x; $this->lastY = $this->y; @@ -88,32 +89,32 @@ abstract class Projectile extends Entity{ $this->fire -= 4; if($this->fire <= 0) $this->fire = 0; } - + if($this->fire <= 0){ $this->updateMetadata(); } } - + if($this->handleLavaMovement()){ if(!$this->isImmuneToFire){ $this->harm(4, "fire"); $oldOnFire = $this->fire; - if($oldOnFire < 20*30) $this->fire = 20*30; //30 seconds + if($oldOnFire < 20 * 30) $this->fire = 20 * 30; //30 seconds } } - + if($this->isInVoid()){ $this->outOfWorld(); } //Entity::update end - + if($this->shake > 0) --$this->shake; - + if(!$this->inGround){ ++$this->ticksInAir; goto CHECK_COLLISIONS; //TODO dont use gotos } - + $blockID = $this->level->level->getBlockID($this->xTile, $this->yTile, $this->zTile); if($this->inTile != $blockID){ $this->speedX += (lcg_value() * 0.2); @@ -122,7 +123,7 @@ abstract class Projectile extends Entity{ $this->inGround = false; $this->ticksInAir = $this->ticksInGround = 0; CHECK_COLLISIONS: - + $start = new Vector3($this->x, $this->y, $this->z); $end = new Vector3($this->x + $this->speedX, $this->y + $this->speedY, $this->z + $this->speedZ); /** @@ -135,44 +136,44 @@ abstract class Projectile extends Entity{ }else{ $end = new Vector3($this->x + $this->speedX, $this->y + $this->speedY, $this->z + $this->speedZ); } - + $entities = $this->level->getEntitiesInAABB($this->boundingBox->addCoord($this->speedX, $this->speedY, $this->speedZ)->expand(1, 1, 1)); $bestDist = 0; $bestEnt = null; - + foreach($entities as $eid => $ent){ if($eid != $this->eid && $ent->isPickable() && ($eid != $this->shooterEID || $this->ticksInAir >= 5)){ - + $v12 = $ent->boundingBox->expand(0.3, 0.3, 0.3); $v13 = $v12->calculateIntercept($start, $end); - + if($v13 != null){ $dist = $start->distance($v13->hitVector); - + if($dist < $bestDist || $bestDist == 0){ $bestEnt = $ent; } } } } - + if($bestEnt != null){ $v4 = MovingObjectPosition::fromEntity($bestEnt); } - + if($v4 != null){ $this->onHit($v4); } $this->x += $this->speedX; $this->y += $this->speedY; $this->z += $this->speedZ; - $v21 = sqrt($this->speedX*$this->speedX + $this->speedZ*$this->speedZ); + $v21 = sqrt($this->speedX * $this->speedX + $this->speedZ * $this->speedZ); $this->yaw = atan2($this->speedX, $this->speedZ) * 180 / M_PI; $this->pitch = atan2($this->speedY, $v21) * 180 / M_PI; - + $this->pitch = $this->lastPitch + ($this->pitch - $this->lastPitch) * 0.2; $this->yaw = $this->lastYaw + ($this->yaw - $this->lastYaw) * 0.2; - + $v24 = $this->inWater ? 0.8 : 0.99; $this->speedX *= $v24; $this->speedY *= $v24; @@ -183,19 +184,19 @@ abstract class Projectile extends Entity{ $this->boundingBox->setBounds($this->x - $v7, $this->y - $this->yOffset /*+ $this->ySize*/, $this->z - $v7, $this->x + $v7, $this->y - $this->yOffset + $v8 /*+ $this->ySize*/, $this->z + $v7); } } - + public function onHit(MovingObjectPosition $hitResult){ - + } public function createSaveData(){ $data = parent::createSaveData(); - + $data["inTile"] = $this->inTile; $data["inGround"] = $this->inGround; $data["xTile"] = $this->xTile; $data["yTile"] = $this->yTile; $data["zTile"] = $this->zTile; - + return $data; } public function spawn($player) @@ -212,5 +213,5 @@ abstract class Projectile extends Entity{ $pk->did = 0; $player->dataPacket($pk); } - + } \ No newline at end of file diff --git a/src/entity/ai/EntityAI.php b/src/entity/ai/EntityAI.php index a8c9d883..d7f1d820 100644 --- a/src/entity/ai/EntityAI.php +++ b/src/entity/ai/EntityAI.php @@ -10,29 +10,27 @@ class EntityAI * @var Living */ public $entity; - + /** * @var TaskBase[] */ protected $tasks; - + public $lastTask; - + public function __construct($entity){ $this->entity = $entity; $this->tasks = []; $this->mobController = new MobController($entity); } - - + public function canSee(Entity $entity){ //TODO caches return $this->entity->canSee($entity); } - + /** * Add a task for entity - * @param TaskBase $task */ public function addTask(TaskBase $task){ $this->tasks[$task->__toString()] = $task; @@ -44,16 +42,16 @@ class EntityAI } return false; } - + /** - * + * * @param mixed $id classname * @return TaskBase | false */ public function getTask($id){ return $this->tasks[$id] ?? false; } - + public function isStarted($id){ $task = $this->getTask($id); return $task instanceof TaskBase && $task->isStarted; @@ -75,10 +73,10 @@ class EntityAI } } } - + public function __destruct(){ unset($this->entity); } - + } diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index fd6d772e..13b4efa0 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -14,24 +14,24 @@ class MobController * @var Living */ public $entity; - + public $finalYaw, $finalPitch, $finalHeadYaw; - + public $headYaw; public $someTicker = 0; - + protected $jumping; - + public $moveToX, $moveToY, $moveToZ; public $speedMultiplier; public $updateMove = false; - + public $lookX, $lookY, $lookZ; public $deltaLookYaw, $deltaLookPitch; public $isLooking = false; - + public $headYawIsYaw = false; - + public function __construct($e){ $this->entity = $e; } @@ -41,11 +41,11 @@ class MobController public function isJumping(){ return $this->jumping; } - + public function setJumping($b){ $this->jumping = $b; } - + public function isRotationCompleted(){ return $this->finalHeadYaw === $this->entity->headYaw; } @@ -56,7 +56,7 @@ class MobController $this->speedMultiplier = $speed; $this->updateMove = true; } - + public function setMovingOffset($x, $y, $z, $speed){ $this->moveToX = $this->entity->x + ($x); $this->moveToY = $this->entity->y + ($y); @@ -64,50 +64,50 @@ class MobController $this->speedMultiplier = $speed; $this->updateMove = true; } - + public function canJump(){ return $this->isJumping() && $this->jumpTimeout <= 0 && $this->entity->onGround; } public static function limitAngle2($old, $newA, $limit){ $v4 = Utils::wrapAngleTo180($old - $newA); - + if($v4 < -$limit) $v4 = -$limit; if($v4 >= $limit) $v4 = $limit; - + return $old - $v4; } public static function limitAngle($old, $newA, $limit){ $v4 = Utils::wrapAngleTo180($newA - $old); - + if($v4 > $limit) $v4 = $limit; if($v4 < -$limit) $v4 = -$limit; - + return $old + $v4; } - + public function jumpTick(){ $this->entity->jumping = $this->jumping; $this->jumping = false; } - + public function movementTick(){ $this->entity->moveForward = 0; if($this->updateMove){ $this->updateMove = false; $v1 = floor($this->entity->boundingBox->minY + 0.5); - + $diffX = $this->moveToX - $this->entity->x; $diffZ = $this->moveToZ - $this->entity->z; $diffY = $this->moveToY - $v1; - - $v8 = $diffX*$diffX + $diffY*$diffY + $diffZ*$diffZ; - + + $v8 = $diffX * $diffX + $diffY * $diffY + $diffZ * $diffZ; + if($v8 >= 0.00000025){ $v10 = (atan2($diffZ, $diffX) * 180 / M_PI) - 90; $this->entity->yaw = self::limitAngle($this->entity->yaw, $v10, 30); $this->entity->setAIMoveSpeed($this->speedMultiplier * $this->entity->getSpeed() * $this->entity->getSpeedModifer()); - - if($diffY > 0 && $diffX*$diffX + $diffZ*$diffZ < 1) $this->setJumping(true); + + if($diffY > 0 && $diffX * $diffX + $diffZ * $diffZ < 1) $this->setJumping(true); } } //TODO handle jumps somewhere @@ -117,11 +117,11 @@ class MobController //} //if($this->jumpTimeout > 0) --$this->jumpTimeout; } - + public function updateHeadYaw(){ $diffX = $this->entity->x - $this->entity->lastX; $diffZ = $this->entity->z - $this->entity->lastZ; - if(($diffX*$diffX + $diffZ*$diffZ) > 0.00000025){ + if(($diffX * $diffX + $diffZ * $diffZ) > 0.00000025){ $this->entity->renderYawOffset = $this->entity->yaw; $this->entity->headYaw = self::limitAngle2($this->entity->renderYawOffset, $this->entity->headYaw, 75); $this->headYaw = $this->entity->headYaw; @@ -133,7 +133,7 @@ class MobController $this->headYaw = $this->entity->headYaw; }else{ ++$this->someTicker; - + if($this->someTicker > 10){ $v5 = max(1 - ($this->someTicker - 10) / 10, 0) * 75; } @@ -141,53 +141,53 @@ class MobController $this->entity->renderYawOffset = self::limitAngle2($this->entity->headYaw, $this->entity->renderYawOffset, $v5); } } - + public function rotateTick(){ //TODO handle more rotation - + $this->entity->pitch = 0; if($this->isLooking){ $this->isLooking = false; - + $diffX = $this->lookX - $this->entity->x; $diffY = $this->lookY - ($this->entity->y + $this->entity->getEyeHeight()); $diffZ = $this->lookZ - $this->entity->z; - $distance = sqrt($diffX*$diffX + $diffZ*$diffZ); - - $v9 = (atan2($diffZ, $diffX)*180 / M_PI) - 90; - $v10 = -(atan2($diffY, $distance)*180 / M_PI); - + $distance = sqrt($diffX * $diffX + $diffZ * $diffZ); + + $v9 = (atan2($diffZ, $diffX) * 180 / M_PI) - 90; + $v10 = -(atan2($diffY, $distance) * 180 / M_PI); + $this->entity->pitch = self::limitAngle($this->entity->pitch, $v10, $this->deltaLookPitch); $this->entity->headYaw = self::limitAngle($this->entity->headYaw, $v9, $this->deltaLookYaw); }else{ $this->entity->headYaw = self::limitAngle($this->entity->headYaw, $this->entity->renderYawOffset, 10); } - + if($this->headYawIsYaw) $this->entity->yaw = $this->entity->headYaw; $this->headYawIsYaw = false; - + /* Some stuff for pathfinder - nc doesnt have it now * float var11 = MathHelper.wrapAngleTo180_float(this.entity.rotationYawHead - this.entity.renderYawOffset); - if (!this.entity.getNavigator().noPath()) - { - if (var11 < -75.0F) - { - this.entity.rotationYawHead = this.entity.renderYawOffset - 75.0F; - } + if (!this.entity.getNavigator().noPath()) + { + if (var11 < -75.0F) + { + this.entity.rotationYawHead = this.entity.renderYawOffset - 75.0F; + } - if (var11 > 75.0F) - { - this.entity.rotationYawHead = this.entity.renderYawOffset + 75.0F; - } - } + if (var11 > 75.0F) + { + this.entity.rotationYawHead = this.entity.renderYawOffset + 75.0F; + } + } */ - + //$this->entity->lastHeadYaw = $this->entity->headYaw; - //$w180 = Utils::wrapAngleTo180($this->finalHeadYaw - $this->entity->headYaw); + //$w180 = Utils::wrapAngleTo180($this->finalHeadYaw - $this->entity->headYaw); //$w180min = min(abs($w180), 20)*Utils::getSign($w180); //$this->entity->headYaw = Utils::wrapAngleTo360($this->entity->headYaw + $w180min); } - + public function setLookPosition($posX, $posY, $posZ, $lookYaw, $lookPitch){ $this->lookX = $posX; $this->lookY = $posY; @@ -196,9 +196,9 @@ class MobController $this->deltaLookPitch = $lookPitch; $this->isLooking = true; } - + public function faceEntity($x, $y, $z){ - $len = sqrt($x*$x + $z*$z + $y*$y); + $len = sqrt($x * $x + $z * $z + $y * $y); //$d = $len == 0 ?//$v->subtract($this->entity)->normalize(); if($len == 0){ $dx = 0; @@ -207,38 +207,37 @@ class MobController $dx = $x / $len; $dz = $z / $len; } - - - $tan = $dz == 0 ? ($dx < 0 ? 180 : 0) : (90 - rad2deg(atan($dx / $dz))); + + $tan = $dz == 0 ? ($dx < 0 ? 180 : 0) : (90 - rad2deg(atan($dx / $dz))); $thetaOffset = $dz < 0 ? 90 : 270; $calcYaw = ($thetaOffset + $tan); $this->finalHeadYaw = $this->entity->yaw = Utils::wrapAngleTo360($calcYaw); } - + public function lookOffset($x, $y, $z, $pitch = true){ $tan = $z == 0 ? ($x < 0 ? 180 : 0) : (90 - rad2deg(atan($x / $z))); /*arctan(infinity) = pi/2 = (90deg) - 90 = 0*/ $thetaOffset = $z < 0 ? 90 : 270; $calcYaw = $tan + $thetaOffset; - + $this->entity->yaw = $this->finalHeadYaw = Utils::wrapAngleTo360($calcYaw); - + if($pitch){ $diff = sqrt($x * $x + $z * $z); $calcPitch = $diff == 0 ? ($y < 0 ? -90 : 90) : rad2deg(atan($y / $diff)); $this->entity->pitch = $calcPitch; } - + //$this->entity->server->query("UPDATE entities SET pitch = ".$this->entity->pitch.", yaw = ".$this->entity->yaw." WHERE EID = ".$this->entity->eid.";"); return true; } - + public function lookOn($x, $y = 0, $z = 0, $pitch = true){ if($x instanceof Vector3){ return $this->lookOn($x->x, $x->y + $x->getEyeHeight(), $x->z, $pitch); } return $this->lookOffset($x - $this->entity->x, ($this->entity->y + $this->entity->height) - $y, $z - $this->entity->z, $pitch); } - + public function __destruct(){ unset($this->entity); } diff --git a/src/entity/ai/path/PathFollower.php b/src/entity/ai/path/PathFollower.php index c17cde93..3066c4fe 100644 --- a/src/entity/ai/path/PathFollower.php +++ b/src/entity/ai/path/PathFollower.php @@ -5,16 +5,16 @@ class PathFollower{ * @var Living */ public $entity; - + public function __construct(Living $entity){ $this->entity = $entity; } - + public function followPath(){ - + if(!isset($this->entity) || !($this->entity instanceof Living)) return; if(!$this->entity->hasPath()) return; - + if($this->entity->currentNode == null){ $this->entity->currentNode = $this->entity->path[$this->entity->currentIndex]; } @@ -27,14 +27,13 @@ class PathFollower{ //console("next"); $this->entity->currentNode = null; } - - + if($this->entity->currentIndex >= count($this->entity->path)){ console("path finished."); $this->entity->currentNode = null; $this->entity->path = null; $this->entity->currentIndex = 0; - + /*foreach($this->entity->pathEIDS as $eid){ $pk = new RemoveEntityPacket(); $pk->eid = $eid; @@ -42,9 +41,9 @@ class PathFollower{ $player->dataPacket($pk); } }*/ - + } } - + } diff --git a/src/entity/ai/tasks/TaskAttackPlayer.php b/src/entity/ai/tasks/TaskAttackPlayer.php index 8760cd11..213a0eb2 100644 --- a/src/entity/ai/tasks/TaskAttackPlayer.php +++ b/src/entity/ai/tasks/TaskAttackPlayer.php @@ -4,20 +4,19 @@ class TaskAttackPlayer extends TaskBase { public $speedMultiplier; public $rangeSquared; - + public $attackCounter; - + public function __construct($speed, $range){ $this->speedMultiplier = $speed; - $this->rangeSquared = $range*$range; + $this->rangeSquared = $range * $range; } - + public function onStart(EntityAI $ai) { $this->selfCounter = 1; } - - + public function onUpdate(EntityAI $ai) { if(!$this->isTargetValid($ai)){ @@ -27,7 +26,7 @@ class TaskAttackPlayer extends TaskBase } $ai->mobController->setMovingTarget($ai->entity->target->x, $ai->entity->target->y, $ai->entity->target->z, $this->speedMultiplier); $ai->mobController->setLookPosition($ai->entity->target->x, $ai->entity->target->y + 0.12, $ai->entity->target->z, 10, $ai->entity->getVerticalFaceSpeed()); - + --$this->attackCounter; $v1 = $ai->entity->width * $ai->entity->width * 4; if($ai->entity instanceof Creeper){ @@ -38,16 +37,15 @@ class TaskAttackPlayer extends TaskBase $xDiff = ($t->x - $e->x); $yDiff = ($t->y - $e->y); $zDiff = ($t->z - $e->z); - $dist = ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff); + $dist = ($xDiff * $xDiff + $yDiff * $yDiff + $zDiff * $zDiff); if($dist <= $v1){ if($this->attackCounter <= 0){ $this->attackCounter = $e->attackEntity($t, sqrt($dist)) ? 20 : 0; } } - + } - - + public function isTargetValid(EntityAI $ai){ $e = $ai->entity; if($e->target instanceof Entity && !$e->target->closed && !$e->target->dead){ @@ -55,11 +53,11 @@ class TaskAttackPlayer extends TaskBase $xDiff = ($t->x - $e->x); $yDiff = ($t->y - $e->y); $zDiff = ($t->z - $e->z); - return ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff) <= $this->rangeSquared; + return ($xDiff * $xDiff + $yDiff * $yDiff + $zDiff * $zDiff) <= $this->rangeSquared; } return false; } - + public function tryTargeting(EntityAI $ai){ $e = $ai->entity; if($e->target instanceof Entity){ @@ -67,25 +65,25 @@ class TaskAttackPlayer extends TaskBase $xDiff = ($t->x - $e->x); $yDiff = ($t->y - $e->y); $zDiff = ($t->z - $e->z); - if(($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff) <= $this->rangeSquared){ + if(($xDiff * $xDiff + $yDiff * $yDiff + $zDiff * $zDiff) <= $this->rangeSquared){ return true; } } - + $closestTarget = $e->closestPlayerToAttackDist <= $this->rangeSquared ? $e->level->entityList[$e->closestPlayerToAttackEID] : null; - + if($closestTarget != null){ $e->target = $closestTarget; //TODO dont save entity object ? return true; } return false; } - + public function canBeExecuted(EntityAI $ai) { return $this->tryTargeting($ai); } - + public function onEnd(EntityAI $ai) { $ai->entity->target = false; diff --git a/src/entity/ai/tasks/TaskBase.php b/src/entity/ai/tasks/TaskBase.php index 14f776eb..dd2eba25 100644 --- a/src/entity/ai/tasks/TaskBase.php +++ b/src/entity/ai/tasks/TaskBase.php @@ -3,45 +3,41 @@ abstract class TaskBase { public $isStarted = false, $selfCounter = 0; - + public function __construct(){} - + /** * Executed when entity starts task. It is recommended to set counter here. - * @param EntityAI $ai */ abstract function onStart(EntityAI $ai); - + /** * Every tick until counter will be not equal to 0. It is recommended to update value of counter here. - * @param EntityAI $ai */ abstract function onUpdate(EntityAI $ai); - + /** * On task end. - * @param EntityAI $ai */ abstract function onEnd(EntityAI $ai); - + /** * Can start the task - * @param EntityAI $ai */ abstract function canBeExecuted(EntityAI $ai); - + /** * @return number ticks */ function getIdleTimeAfterEnd(){ return 0; } - + function reset(){ $this->isStarted = false; $this->selfCounter = 0; } - + final function __toString(){ return get_class($this); } diff --git a/src/entity/ai/tasks/TaskEatTileGoal.php b/src/entity/ai/tasks/TaskEatTileGoal.php index 5af5d21e..4f69a7a8 100644 --- a/src/entity/ai/tasks/TaskEatTileGoal.php +++ b/src/entity/ai/tasks/TaskEatTileGoal.php @@ -10,7 +10,7 @@ class TaskEatTileGoal extends TaskBase public function onEnd(EntityAI $ai) { - + } public function onUpdate(EntityAI $ai) @@ -18,7 +18,7 @@ class TaskEatTileGoal extends TaskBase if($ai->isStarted("TaskPanic")){ $this->reset(); return false; - } + } if (--$this->selfCounter == 4) { @@ -27,7 +27,7 @@ class TaskEatTileGoal extends TaskBase if($id === TALL_GRASS){ $ai->entity->level->fastSetBlockUpdate($ai->entity->x, $ai->entity->y, $ai->entity->z, AIR, 0); $ai->entity->eatGrass(); - + }elseif($idb === GRASS){ $ai->entity->level->fastSetBlockUpdate($ai->entity->x, $ai->entity->y - 1, $ai->entity->z, DIRT, 0); $ai->entity->eatGrass(); diff --git a/src/entity/ai/tasks/TaskFollowParent.php b/src/entity/ai/tasks/TaskFollowParent.php index 99dcdbe6..9c5a989d 100644 --- a/src/entity/ai/tasks/TaskFollowParent.php +++ b/src/entity/ai/tasks/TaskFollowParent.php @@ -5,7 +5,7 @@ class TaskFollowParent extends TaskBase public function __construct($speed){ $this->speedMultiplier = $speed; } - + public function onStart(EntityAI $ai) { $this->selfCounter = 1; @@ -23,19 +23,19 @@ class TaskFollowParent extends TaskBase $ai->entity->parent = null; return false; } - + $dist = $ai->entity->distanceSquared($ai->entity->parent); - + if($dist > 256 || $dist < 9){ //3-16 blocks $this->reset(); return false; } $parent = $ai->entity->parent; - + $ai->mobController->setLookPosition($parent->x, $parent->y + $parent->getEyeHeight(), $parent->z, 10, $ai->entity->getVerticalFaceSpeed()); $ai->mobController->setMovingTarget($parent->x, $parent->y, $parent->z, $this->speedMultiplier); } - + public function canBeExecuted(EntityAI $ai) { return $ai->entity->isBaby() && ($ai->entity->parent instanceof Entity) && !$ai->entity->parent->dead; //TODO find a new parent if the current one is dead diff --git a/src/entity/ai/tasks/TaskLookAround.php b/src/entity/ai/tasks/TaskLookAround.php index a931d98a..6463503a 100644 --- a/src/entity/ai/tasks/TaskLookAround.php +++ b/src/entity/ai/tasks/TaskLookAround.php @@ -3,7 +3,7 @@ class TaskLookAround extends TaskBase { public $lookOffsetX, $lookOffsetZ; - + public function onStart(EntityAI $ai) { $this->selfCounter = 20 + lcg_value(); @@ -19,9 +19,9 @@ class TaskLookAround extends TaskBase public function canBeExecuted(EntityAI $ai) { - return !$ai->entity->inPanic && !$ai->entity->isMoving() && lcg_value() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskAttackPlayer") && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/ + return !$ai->entity->inPanic && !$ai->entity->isMoving() && lcg_value() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskAttackPlayer") && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/ } - + public function onUpdate(EntityAI $ai) { --$this->selfCounter; diff --git a/src/entity/ai/tasks/TaskLookAtPlayer.php b/src/entity/ai/tasks/TaskLookAtPlayer.php index 174304a9..7fa7b816 100644 --- a/src/entity/ai/tasks/TaskLookAtPlayer.php +++ b/src/entity/ai/tasks/TaskLookAtPlayer.php @@ -6,13 +6,13 @@ class TaskLookAtPlayer extends TaskBase{ public function __construct($distance){ $this->distance = $distance; } - + public function canBeExecuted(EntityAI $ai){ - return !$ai->entity->inPanic && lcg_value() < 0.02 && !$ai->entity->isMovingHorizontally() && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->isStarted("TaskPanic") && !$ai->entity->hasPath(); + return !$ai->entity->inPanic && lcg_value() < 0.02 && !$ai->entity->isMovingHorizontally() && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->isStarted("TaskPanic") && !$ai->entity->hasPath(); } protected function findTarget(Creature $e, $r){ - return $e->closestPlayerDist > $r*$r ? null : $e->level->entityList[$e->closestPlayerEID]; + return $e->closestPlayerDist > $r * $r ? null : $e->level->entityList[$e->closestPlayerEID]; } public function onStart(EntityAI $ai){ @@ -22,7 +22,7 @@ class TaskLookAtPlayer extends TaskBase{ $this->onEnd($ai); return; } - + $this->selfCounter = mt_rand(20, 60); } @@ -35,7 +35,7 @@ class TaskLookAtPlayer extends TaskBase{ $ai->mobController->setLookPosition($this->target->x, $this->target->y + $this->target->getEyeHeight(), $this->target->z, 10, $ai->entity->getVerticalFaceSpeed()); $this->selfCounter--; } - + public function onEnd(EntityAI $ai){ unset($this->target); $ai->entity->pitch = 0; diff --git a/src/entity/ai/tasks/TaskMate.php b/src/entity/ai/tasks/TaskMate.php index 5bfdd26b..d073bd1a 100644 --- a/src/entity/ai/tasks/TaskMate.php +++ b/src/entity/ai/tasks/TaskMate.php @@ -5,9 +5,9 @@ class TaskMate extends TaskBase public function __construct($speed){ $this->speedMultiplier = $speed; } - + public $targetMateID; - + public function onStart(EntityAI $ai) { $this->selfCounter = 60; @@ -27,17 +27,16 @@ class TaskMate extends TaskBase $this->onEnd($ai); return; } - - + $ai->mobController->setLookPosition($movTo->x, $movTo->y + $movTo->getEyeHeight(), $movTo->z, 10, $ai->entity->getVerticalFaceSpeed()); $ai->mobController->setMovingTarget($movTo->x, $movTo->y, $movTo->z, $this->speedMultiplier); - + --$this->selfCounter; if($this->selfCounter <= 0 && $ai->entity->distanceSquared($movTo) < 9){ $this->createBaby($ai, $movTo); } } - + public function createBaby(EntityAI $ai, Entity $entpar2){ /** * @var Animal $baby @@ -45,20 +44,20 @@ class TaskMate extends TaskBase $baby = $ai->entity->breed(); //this.theAnimal.setGrowingAge(6000); TODO growing age? //this.targetMate.setGrowingAge(6000); - + $ai->entity->loveTimeout = 6000; $entpar2->loveTimeout = 6000; - + $ai->entity->resetInLove(); $entpar2->resetInLove(); $baby->setAge(-24000); } - + public function findMate(EntityAI $ai){ - $distance = 8.0*8.0; + $distance = 8.0 * 8.0; $minDist = $distance; //TODO check bb intersection? $matedEID = null; - + foreach($ai->entity->level->entitiesInLove as $eid => $_){ $e = $ai->entity->level->entityList[$eid] ?? false; if($e instanceof Entity){ @@ -71,7 +70,7 @@ class TaskMate extends TaskBase } return $matedEID; } - + public function canBeExecuted(EntityAI $ai) { return $ai->entity->isInLove() && ($this->targetMateID = $this->findMate($ai)) != null; diff --git a/src/entity/ai/tasks/TaskPanic.php b/src/entity/ai/tasks/TaskPanic.php index a010802d..a906ec3f 100644 --- a/src/entity/ai/tasks/TaskPanic.php +++ b/src/entity/ai/tasks/TaskPanic.php @@ -3,12 +3,12 @@ class TaskPanic extends TaskBase { protected $randX = 2, $randZ = 2; - + public $speedMultiplier; public function __construct($speedMultiplier){ $this->speedMultiplier = $speedMultiplier; } - + public function onStart(EntityAI $ai) { $this->selfCounter = 60; @@ -20,16 +20,16 @@ class TaskPanic extends TaskBase $ai->entity->inPanic = false; $this->reset(); } - + public function regenerateRandXZ(){ $this->randX = (mt_rand(0, 1) ? -1 : 1); $this->randZ = (mt_rand(0, 1) ? -1 : 1); } - + public function reset(){ $this->randX = $this->randZ = 2; } - + public function onUpdate(EntityAI $ai) { --$this->selfCounter; @@ -42,6 +42,5 @@ class TaskPanic extends TaskBase return $ai->entity instanceof Animal && $ai->entity->inPanic && $ai->entity->knockbackTime <= 0; } - -} + } diff --git a/src/entity/ai/tasks/TaskRandomWalk.php b/src/entity/ai/tasks/TaskRandomWalk.php index 4b68a746..e81d2263 100644 --- a/src/entity/ai/tasks/TaskRandomWalk.php +++ b/src/entity/ai/tasks/TaskRandomWalk.php @@ -7,7 +7,7 @@ class TaskRandomWalk extends TaskBase public function __construct($speedMultiplier){ $this->speedMultiplier = $speedMultiplier; } - + public function onStart(EntityAI $ai) { $this->x = mt_rand(-1, 1); @@ -30,7 +30,7 @@ class TaskRandomWalk extends TaskBase $this->reset(); return false; //TODO Better way: block movement } - + --$this->selfCounter; $ai->mobController->setMovingOffset($this->x, 0, $this->z, $this->speedMultiplier); } @@ -40,8 +40,7 @@ class TaskRandomWalk extends TaskBase if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt") || $ai->isStarted("TaskAttackPlayer") || $ai->isStarted("TaskRangedAttack")) { return false; } // i really need mutexBits - return !$ai->entity->inPanic && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskEatTileGoal") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskFollowParent") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0; + return !$ai->entity->inPanic && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskEatTileGoal") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskFollowParent") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0; } - -} + } diff --git a/src/entity/ai/tasks/TaskRangedAttack.php b/src/entity/ai/tasks/TaskRangedAttack.php index 04d53630..dfe2dee1 100644 --- a/src/entity/ai/tasks/TaskRangedAttack.php +++ b/src/entity/ai/tasks/TaskRangedAttack.php @@ -7,10 +7,10 @@ class TaskRangedAttack extends \TaskBase public $seenTicks = 0; public function __construct($speed, $range){ $this->speedMultiplier = $speed; - $this->rangeSquared = $range*$range; + $this->rangeSquared = $range * $range; $this->server = ServerAPI::request(); } - + public function onStart(EntityAI $ai) { $this->selfCounter = 1; @@ -20,7 +20,7 @@ class TaskRangedAttack extends \TaskBase public function onEnd(EntityAI $ai) { $ai->entity->target = false; - + } public function onUpdate(EntityAI $ai) @@ -36,24 +36,23 @@ class TaskRangedAttack extends \TaskBase }else{ $this->seenTicks = 0; } - - + $dist = $ai->entity->distanceSquared($ai->entity->target); - + if($dist > 100 || $this->seenTicks < 20){ $ai->mobController->setMovingTarget($ai->entity->target->x, $ai->entity->target->y, $ai->entity->target->z, $this->speedMultiplier); }else{ $ai->mobController->headYawIsYaw = true; } - + $ai->mobController->setLookPosition($ai->entity->target->x, $ai->entity->target->y + 0.12, $ai->entity->target->z, 10, $ai->entity->getVerticalFaceSpeed()); - + if($this->attackCounter <= 0 && $this->seenTicks > 0){ $this->rangedAttack($ai->entity, $ai->entity->target); $this->attackCounter = 60; } } - + public function rangedAttack($selfEntity, $target){ $d = [ "x" => $selfEntity->x, @@ -88,8 +87,7 @@ class TaskRangedAttack extends \TaskBase $this->server->api->entity->spawnToAll($arrow); } } - - + public function isTargetValid(EntityAI $ai){ $e = $ai->entity; if($e->target instanceof Entity && !$e->target->closed && !$e->target->dead){ @@ -97,11 +95,11 @@ class TaskRangedAttack extends \TaskBase $xDiff = ($t->x - $e->x); $yDiff = ($t->y - $e->y); $zDiff = ($t->z - $e->z); - return ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff) <= $this->rangeSquared; + return ($xDiff * $xDiff + $yDiff * $yDiff + $zDiff * $zDiff) <= $this->rangeSquared; } return false; } - + public function tryTargeting(EntityAI $ai){ $e = $ai->entity; if($e->target instanceof Entity){ @@ -109,21 +107,20 @@ class TaskRangedAttack extends \TaskBase $xDiff = ($t->x - $e->x); $yDiff = ($t->y - $e->y); $zDiff = ($t->z - $e->z); - if(($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff) <= $this->rangeSquared){ + if(($xDiff * $xDiff + $yDiff * $yDiff + $zDiff * $zDiff) <= $this->rangeSquared){ return true; } } $closestTarget = $e->closestPlayerToAttackDist <= $this->rangeSquared ? $e->level->entityList[$e->closestPlayerToAttackEID] : null; - + if($closestTarget != null){ $e->target = $closestTarget; //TODO dont save entity object ? return true; } return false; } - - + public function canBeExecuted(EntityAI $ai) { return $this->tryTargeting($ai); diff --git a/src/entity/ai/tasks/TaskSwimming.php b/src/entity/ai/tasks/TaskSwimming.php index 61842afb..ad9ecdc7 100644 --- a/src/entity/ai/tasks/TaskSwimming.php +++ b/src/entity/ai/tasks/TaskSwimming.php @@ -9,7 +9,7 @@ class TaskSwimming extends TaskBase public function onEnd(EntityAI $ai) { - + } public function onUpdate(EntityAI $ai) @@ -18,12 +18,12 @@ class TaskSwimming extends TaskBase $this->reset(); return; } - + if(lcg_value() < 0.8){ #1.5.2 method $ai->mobController->setJumping(true); } } - + public function canBeExecuted(EntityAI $ai) { return $ai->entity->inWater || $ai->entity->inLava; diff --git a/src/entity/ai/tasks/TaskTempt.php b/src/entity/ai/tasks/TaskTempt.php index eee2f39a..5dd67c66 100644 --- a/src/entity/ai/tasks/TaskTempt.php +++ b/src/entity/ai/tasks/TaskTempt.php @@ -4,19 +4,19 @@ class TaskTempt extends TaskBase { public function __construct($speed){ $this->speedMultiplier = $speed; - $this->targetDistance = 10*10; + $this->targetDistance = 10 * 10; } - + public function onStart(EntityAI $ai) { $this->selfCounter = 1; } - + public function onEnd(EntityAI $ai) { $ai->entity->target = false; } - + public function onUpdate(EntityAI $ai) { if(!$this->isTargetValid($ai) || $ai->entity->inPanic || $ai->isStarted("TaskMate")){ @@ -24,22 +24,21 @@ class TaskTempt extends TaskBase $this->onEnd($ai); return false; } - + $target = $ai->entity->target; $xdiff = ($target->x - $ai->entity->x); $ydiff = ($target->y - $ai->entity->y); $zdiff = ($target->z - $ai->entity->z); - $dist = $xdiff*$xdiff + $ydiff*$ydiff + $zdiff*$zdiff; + $dist = $xdiff * $xdiff + $ydiff * $ydiff + $zdiff * $zdiff; $ai->mobController->setLookPosition($target->x, $target->y + $target->getEyeHeight(), $target->z, 30, $ai->entity->getVerticalFaceSpeed()); if($dist >= 6.25){ $ai->mobController->setMovingTarget($target->x, $target->y, $target->z, $this->speedMultiplier); }else{ $ai->mobController->headYawIsYaw = true; } - - + } - + public function isTargetValid(EntityAI $ai){ $e = $ai->entity; if($e->target instanceof Entity && !$e->target->closed && $e->isFood($e->target->player->getHeldItem()->id)){ @@ -47,16 +46,16 @@ class TaskTempt extends TaskBase $xDiff = ($t->x - $e->x); $yDiff = ($t->y - $e->y); $zDiff = ($t->z - $e->z); - return ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff) <= $this->targetDistance; + return ($xDiff * $xDiff + $yDiff * $yDiff + $zDiff * $zDiff) <= $this->targetDistance; } return false; } - + public function canBeExecuted(EntityAI $ai) { return !$ai->entity->inPanic && !$ai->isStarted("TaskMate") && $this->tryTargeting($ai); } - + public function tryTargeting(EntityAI $ai){ $e = $ai->entity; if($e->target instanceof Entity){ @@ -64,13 +63,13 @@ class TaskTempt extends TaskBase $xDiff = ($t->x - $e->x); $yDiff = ($t->y - $e->y); $zDiff = ($t->z - $e->z); - if(($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff) <= $this->targetDistance){ + if(($xDiff * $xDiff + $yDiff * $yDiff + $zDiff * $zDiff) <= $this->targetDistance){ return true; } } - + $closestTarget = $e->closestPlayerThatCanFeedDist <= $this->targetDistance ? $e->level->entityList[$e->closestPlayerThatCanFeedEID] : null; - + if($closestTarget != null){ $e->target = $closestTarget; //TODO dont save entity object ? return true; diff --git a/src/entity/animal/Chicken.php b/src/entity/animal/Chicken.php index 2b19cd1a..8f66e5f8 100644 --- a/src/entity/animal/Chicken.php +++ b/src/entity/animal/Chicken.php @@ -10,7 +10,7 @@ class Chicken extends Animal{ $this->setHealth($this->data["Health"] ?? 4, "generic"); $this->setName('Chicken'); $this->setSpeed(0.25); - + $this->ai->addTask(new TaskRandomWalk(1.0)); $this->ai->addTask(new TaskLookAtPlayer(6)); $this->ai->addTask(new TaskPanic(1.5)); @@ -23,27 +23,27 @@ class Chicken extends Animal{ public function isFood($id){ return $id === PUMPKIN_SEEDS || $id === MELON_SEEDS || $id === BEETROOT_SEEDS || $id === WHEAT_SEEDS; } - + public function updateEntityMovement(){ parent::updateEntityMovement(); - + if(!$this->onGround && $this->speedY < 0) $this->speedY *= 0.6; - + if($this->timeUntilEgg-- <= 0 && !$this->isBaby()){ $this->dropAnEgg(); $this->timeUntilEgg = mt_rand(0,6000) + 6000; } } - + public function fall(){} //chickens have no fall dmg? - + public function dropAnEgg(){ if($this->closed){ return; } ServerAPI::request()->api->entity->drop(new Position($this->x + 0.5, $this->y, $this->z + 0.5, $this->level), BlockAPI::getItem(EGG, 0, 1)); } - + public function getDrops(){ return $this->isBaby() ? parent::getDrops() : [ [FEATHER, 0, mt_rand(0,2)], diff --git a/src/entity/animal/Cow.php b/src/entity/animal/Cow.php index 441ff126..86a5358a 100644 --- a/src/entity/animal/Cow.php +++ b/src/entity/animal/Cow.php @@ -17,11 +17,11 @@ class Cow extends Animal{ $this->ai->addTask(new TaskMate(1.0)); $this->ai->addTask(new TaskFollowParent(1.1)); } - + public function isFood($id){ return $id === WHEAT; } - + public function interactWith(Entity $e, $action){ if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){ $slot = $e->player->getHeldItem(); @@ -33,9 +33,9 @@ class Cow extends Animal{ } parent::interactWith($e, $action); } - + public function getDrops(){ - return $this->isBaby() ? parent::getDrops() : [ + return $this->isBaby() ? parent::getDrops() : [ [LEATHER, 0, mt_rand(0,2)], [($this->fire > 0 ? STEAK : BEEF), 0, mt_rand(0, 3)] ]; diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index d8bfe219..9b48b6f8 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -1,7 +1,8 @@ setSize(0.9, 0.9); parent::__construct($level, $eid, $class, $type, $data); @@ -9,7 +10,7 @@ class Pig extends Animal implements Rideable{ $this->server = ServerAPI::request(); $this->setName("Pig"); $this->setSpeed(0.25); - + $this->ai->addTask(new TaskRandomWalk(1.0)); $this->ai->addTask(new TaskLookAtPlayer(6)); $this->ai->addTask(new TaskPanic(1.5)); @@ -25,18 +26,18 @@ class Pig extends Animal implements Rideable{ public function isSaddled(){ return (boolean) $this->getState(); } - + /** * @param boolean $value */ public function setSaddled($value = null){ $this->setState($value === null ? !$this->getState() : $value); } - + public function canRide($e){ return $this->isSaddled() && $this->linkedEntity == 0 && $e->linkedEntity == 0; } - + public function updateEntityMovement(){ /*if($this->linkedEntity != 0){ $e = $this->level->entityList[$this->linkedEntity] ?? false; @@ -47,10 +48,10 @@ class Pig extends Animal implements Rideable{ $this->yaw = $e->headYaw; } }*/ - + parent::updateEntityMovement(); } - + public function interactWith(Entity $e, $action) { if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){ @@ -59,7 +60,7 @@ class Pig extends Animal implements Rideable{ $e->setRiding($this); return true; } - + if($slot->getID() === SADDLE){ if(!$this->isSaddled()){ $e->player->removeItem($slot->getID(), 0, 1); @@ -67,15 +68,15 @@ class Pig extends Animal implements Rideable{ } return true; //avoid further interactions } - + } return parent::interactWith($e, $action); } - + public function isFood($id){ return $id === POTATO || $id === CARROT || $id === BEETROOT; } - + public function getDrops(){ return $this->isBaby() ? parent::getDrops() : ($this->isSaddled() ? [ [($this->fire > 0 ? COOKED_PORKCHOP : RAW_PORKCHOP), 0, mt_rand(0,2)], diff --git a/src/entity/animal/Sheep.php b/src/entity/animal/Sheep.php index d79c8701..5cd80c1b 100644 --- a/src/entity/animal/Sheep.php +++ b/src/entity/animal/Sheep.php @@ -3,7 +3,7 @@ class Sheep extends Animal{ public $color; const TYPE = MOB_SHEEP; - + function __construct(Level $level, $eid, $class, $type = 0, $data = []){ $this->setSize(0.9, 1.3); parent::__construct($level, $eid, $class, $type, $data); @@ -12,7 +12,7 @@ class Sheep extends Animal{ $this->data["Sheared"] = $this->data["Sheared"] ?? 0; $this->data["Color"] = $this->data["Color"] ?? $this->sheepColor(); $this->setSpeed(0.25); - + $this->ai->addTask(new TaskRandomWalk(1.0)); $this->ai->addTask(new TaskLookAtPlayer(6)); $this->ai->addTask(new TaskPanic(1.5)); @@ -23,14 +23,14 @@ class Sheep extends Animal{ $this->ai->addTask(new TaskMate(1.0)); $this->ai->addTask(new TaskFollowParent(1.0)); } - + public function createSaveData(){ $data = parent::createSaveData(); $data["Color"] = @$this->data["Color"]; $data["Sheared"] = @$this->data["Sheared"]; return $data; } - + public function eatGrass(){ $this->setSheared(0); if($this->isBaby()){ @@ -39,24 +39,24 @@ class Sheep extends Animal{ $this->setAge($age); } } - + public function setSheared($v = null){ $this->data["Sheared"] = $v === null ? !$this->isSheared() : $v; $this->updateMetadata(); } - + public function isSheared(){ return $this->data["Sheared"]; } - + public function getColor(){ return $this->data["Color"]; //color === 16 -> color = 0, color === 17 -> color = 1 ... } - + public function switchColorMeta($meta){ return abs($meta - 15); } - + public function setColor($meta){ $this->data["Color"] = $meta; } @@ -65,11 +65,11 @@ class Sheep extends Animal{ [WOOL, $this->getColor(), 1] ]; } - + public function isFood($id){ return $id === WHEAT; } - + public function interactWith(Entity $e, $action){ if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){ $slot = $e->player->getHeldItem(); @@ -79,7 +79,7 @@ class Sheep extends Animal{ $this->setSheared(1); $speedX = (lcg_value() * 0.2 - 0.1) + (lcg_value() - lcg_value()) * 0.1; $speedZ = (lcg_value() * 0.2 - 0.1) + (lcg_value() - lcg_value()) * 0.1; - $speedY = 0.2 + (lcg_value()) * 0.05; + $speedY = 0.2 + (lcg_value()) * 0.05; $this->server->api->entity->dropRawPos($this->level, $this->x, $this->y + 1, $this->z, BlockAPI::getItem(WOOL, $this->getColor(), mt_rand(1, 3)), $speedX, $speedY, $speedZ); if($slot->getMetadata() >= $slot->getMaxDurability()){ $e->player->removeItem($slot->getID(), $slot->getMetadata(), $slot->count, true); @@ -90,7 +90,7 @@ class Sheep extends Animal{ return true; }elseif($slot->getID() === DYE){ $this->setColor($this->switchColorMeta($slot->getMetadata())); - + if(($e->player->gamemode & 0x01) === SURVIVAL){ $e->player->removeItem($slot->getID(), $slot->getMetadata(), 1, true); } @@ -98,7 +98,7 @@ class Sheep extends Animal{ } return parent::interactWith($e, $action); } - + public function getMetadata(){ $d = parent::getMetadata(); if(!isset($this->data["Sheared"])){ @@ -107,7 +107,7 @@ class Sheep extends Animal{ $d[16]["value"] = ($this->data["Sheared"] << 4) | ($this->getColor() & 0x0F); return $d; } - + public function sheepColor(){ //a method from 0.8.1 $c = mt_rand(0,100); if($c <= 4){ diff --git a/src/entity/monster/Creeper.php b/src/entity/monster/Creeper.php index b1279f9c..a8539b11 100644 --- a/src/entity/monster/Creeper.php +++ b/src/entity/monster/Creeper.php @@ -1,4 +1,5 @@ ignited = 0; $this->setSpeed(0.25); $this->timeUntilExplode = $this->isIgnited() ? self::EXPL_TIME : 0; - + $this->ai->addTask(new TaskRandomWalk(1.0)); $this->ai->addTask(new TaskAttackPlayer(1.25, 16)); $this->ai->addTask(new TaskLookAround()); $this->ai->addTask(new TaskSwimming()); } - + public function getAttackDamage(){ return 0; } - + public function setIgnited($v = null){ - $this->setState($v === null ? !$this->getState() : $v); + $this->setState($v === null ? !$this->getState() : $v); } - - - + /** * @return boolean */ public function isIgnited(){ return $this->getState() > 0; } - + public function interactWith(Entity $e, $action){ if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){ $slot = $e->player->getHeldItem(); @@ -51,12 +50,12 @@ class Creeper extends Monster{ } return parent::interactWith($e, $action); } - + public function ignite(){ $this->setIgnited(1); $this->timeUntilExplode = self::EXPL_TIME; } - + public function attackEntity($entity, $distance){ if(Utils::distance_noroot($entity, $this) <= 49 && !$this->isIgnited() && $this->ai->canSee($entity)){ $this->ignite(); @@ -88,7 +87,7 @@ class Creeper extends Monster{ $this->close(); $explosion->explode(); } - + public function getDrops(){ return [ [GUNPOWDER, 0, mt_rand(0,2)] diff --git a/src/entity/monster/PigZombie.php b/src/entity/monster/PigZombie.php index 812901ce..c508d1a8 100644 --- a/src/entity/monster/PigZombie.php +++ b/src/entity/monster/PigZombie.php @@ -1,4 +1,5 @@ setName("Skeleton"); $this->ai->removeTask("TaskAttackPlayer"); $this->setSpeed(0.25); - + $this->ai->addTask(new TaskRandomWalk(1.0)); $this->ai->addTask(new TaskLookAround()); $this->ai->addTask(new TaskSwimming()); $this->ai->addTask(new TaskRangedAttack(1.0, 16)); } - + public function getAttackDamage(){ return 0; } - + public function updateBurning(){ if($this->fire > 0 || !$this->level->isDay() || $this->inWater){ return false; } //bad fix for burning in water - [$block, $meta] = $this->level->level->getBlock((int)$this->x, (int)$this->y, (int)$this->z); + [$block, $meta] = $this->level->level->getBlock((int) $this->x, (int) $this->y, (int) $this->z); if($block == WATER || $block == STILL_WATER){ - $v16 = ((int)$this->y + 1) - LiquidBlock::getPercentAir($meta); - if($this->boundingBox->intersectsWith(new AxisAlignedBB((int)$this->x, (int)$this->y, (int)$this->z, (int)$this->x + 1, $v16, (int)$this->z + 1))){ + $v16 = ((int) $this->y + 1) - LiquidBlock::getPercentAir($meta); + if($this->boundingBox->intersectsWith(new AxisAlignedBB((int) $this->x, (int) $this->y, (int) $this->z, (int) $this->x + 1, $v16, (int) $this->z + 1))){ return false; } } - - for($y = (int)$this->y; $y < 129; $y++){ + + for($y = (int) $this->y; $y < 129; $y++){ $block = $this->level->level->getBlockID($this->x, $y, $this->z); if(StaticBlock::getIsSolid($block)){ return false; @@ -49,12 +50,12 @@ class Skeleton extends Monster{ return false; } } - + public function updateEntityMovement(){ $this->updateBurning(); return parent::updateEntityMovement(); } - + public function getDrops(){ return [ [ARROW, 0, mt_rand(0,2)], diff --git a/src/entity/monster/Spider.php b/src/entity/monster/Spider.php index 1752638f..081187fb 100644 --- a/src/entity/monster/Spider.php +++ b/src/entity/monster/Spider.php @@ -1,8 +1,9 @@ setSize(1.4, 0.9); parent::__construct($level, $eid, $class, $type, $data); $this->setHealth($this->data["Health"] ?? 8, "generic"); @@ -11,35 +12,35 @@ class Spider extends Monster{ $this->ai->addTask(new TaskRandomWalk(1.2)); $this->ai->addTask(new TaskLookAround()); $this->ai->addTask(new TaskSwimming()); - $this->ai->addTask(new TaskAttackPlayer(1.2, 16)); + $this->ai->addTask(new TaskAttackPlayer(1.2, 16)); } - + public function getAttackDamage(){ return 2; } - + public function attackEntity($entity, $distance){ if($distance > 2.0 && $distance < 6.0){ if($this->onGround){ $diffX = $entity->x - $this->x; $diffZ = $entity->z - $this->z; - $dist = sqrt($diffX*$diffX + $diffZ*$diffZ); - - $this->speedX = $diffX / $dist*0.5*0.8 + $this->speedX*0.2; - $this->speedZ = $diffZ / $dist*0.5*0.8 + $this->speedZ*0.2; + $dist = sqrt($diffX * $diffX + $diffZ * $diffZ); + + $this->speedX = $diffX / $dist * 0.5 * 0.8 + $this->speedX * 0.2; + $this->speedZ = $diffZ / $dist * 0.5 * 0.8 + $this->speedZ * 0.2; $this->speedY = 0.4; } return false; }else{ return parent::attackEntity($entity, $distance); } - + } - + public function isOnLadder(){ return $this->isCollidedHorizontally; } - + public function getDrops(){ return [ [STRING, 0, mt_rand(0,2)] diff --git a/src/entity/monster/Zombie.php b/src/entity/monster/Zombie.php index bc38cf3a..7cfb4885 100644 --- a/src/entity/monster/Zombie.php +++ b/src/entity/monster/Zombie.php @@ -1,37 +1,38 @@ setSize(0.6, 1.85); parent::__construct($level, $eid, $class, $type, $data); $this->setHealth($this->data["Health"] ?? 12, "generic"); $this->setName("Zombie"); $this->setSpeed(0.23); - + $this->ai->addTask(new TaskRandomWalk(1.0)); $this->ai->addTask(new TaskLookAround()); $this->ai->addTask(new TaskSwimming()); $this->ai->addTask(new TaskAttackPlayer(1.0, 16)); } - + public function getArmorValue(){ return 2; } - + public function updateBurning(){ if($this->fire > 0 || !$this->level->isDay() || $this->inWater){ return false; } //bad fix for burning in water - [$block, $meta] = $this->level->level->getBlock((int)$this->x, (int)$this->y, (int)$this->z); + [$block, $meta] = $this->level->level->getBlock((int) $this->x, (int) $this->y, (int) $this->z); if($block == WATER || $block == STILL_WATER){ - $v16 = ((int)$this->y + 1) - LiquidBlock::getPercentAir($meta); - if($this->boundingBox->intersectsWith(new AxisAlignedBB((int)$this->x, (int)$this->y, (int)$this->z, (int)$this->x + 1, $v16, (int)$this->z + 1))){ + $v16 = ((int) $this->y + 1) - LiquidBlock::getPercentAir($meta); + if($this->boundingBox->intersectsWith(new AxisAlignedBB((int) $this->x, (int) $this->y, (int) $this->z, (int) $this->x + 1, $v16, (int) $this->z + 1))){ return false; } } - - for($y = (int)$this->y; $y < 129; $y++){ + + for($y = (int) $this->y; $y < 129; $y++){ $block = $this->level->level->getBlockID($this->x, $y, $this->z); if(StaticBlock::getIsSolid($block)){ return false; @@ -48,7 +49,7 @@ class Zombie extends Monster{ return false; } } - + public function getAttackDamage(){ return 4; } @@ -57,7 +58,7 @@ class Zombie extends Monster{ $this->updateBurning(); return parent::updateEntityMovement(); } - + public function getDrops(){ return [ [CARROT, 0, Utils::chance(0.83) ? 1 : 0], diff --git a/src/entity/object/Arrow.php b/src/entity/object/Arrow.php index d671113e..cac786cf 100644 --- a/src/entity/object/Arrow.php +++ b/src/entity/object/Arrow.php @@ -3,7 +3,7 @@ class Arrow extends Entity{ const TYPE = OBJECT_ARROW; const CLASS_TYPE = ENTITY_OBJECT; - + public $shooterEID = 0; public $xTile, $yTile, $zTile; public $inTile, $inData; @@ -13,7 +13,7 @@ class Arrow extends Entity{ public $criticial = false; public $groundTicks = 0; public $shotByPlayer = false; - + function __construct(Level $level, $eid, $class, $type = 0, $data = []){ parent::__construct($level, $eid, $class, $type, $data); $this->gravity = 0.05; @@ -32,12 +32,12 @@ class Arrow extends Entity{ $this->yTile = $data["yTile"] ?? $this->yTile; $this->zTile = $data["zTile"] ?? $this->zTile; $this->shotByPlayer = $data["shotByPlayer"] ?? $this->shotByPlayer; - + //$this->server->schedule(1210, array($this, "update")); //Despawn } public function createSaveData(){ $data = parent::createSaveData(); - + $data["inTile"] = $this->inTile; $data["inData"] = $this->inData; $data["inGround"] = $this->inGround; @@ -57,7 +57,7 @@ class Arrow extends Entity{ $pk->pitch = $this->pitch; $this->server->api->player->broadcastPacket($this->level->players, $pk); } - + public function shoot($d, $d1, $d2, $f, $f1){ //original name from 0.8.1 IDA decompilation, var names are taken from b1.7.3 $f2 = sqrt($d * $d + $d1 * $d1 + $d2 * $d2); $d /= $f2; @@ -79,7 +79,7 @@ class Arrow extends Entity{ //$this->update(); $this->groundTicks = 0; } - + public function update($now){ $this->lastX = $this->x; $this->lastY = $this->y; @@ -101,40 +101,39 @@ class Arrow extends Entity{ $this->fire -= 4; if($this->fire <= 0) $this->fire = 0; } - + if($this->fire <= 0){ $this->updateMetadata(); } } - + if($this->handleLavaMovement()){ if(!$this->isImmuneToFire){ $this->harm(4, "fire"); $oldOnFire = $this->fire; - if($oldOnFire < 20*30) $this->fire = 20*30; //30 seconds + if($oldOnFire < 20 * 30) $this->fire = 20 * 30; //30 seconds } } - + if($this->isInVoid()){ $this->outOfWorld(); } //Entity::update end - - + if($this->lastPitch == $this->lastYaw && $this->lastYaw == 0){ - $v1 = sqrt($this->speedX*$this->speedX + $this->speedZ*$this->speedZ); + $v1 = sqrt($this->speedX * $this->speedX + $this->speedZ * $this->speedZ); $this->lastYaw = $this->yaw = atan2($this->speedX, $this->speedZ) * 180 / M_PI; $this->lastPitch = $this->pitch = atan2($this->speedY, $v1) * 180 / M_PI; } - + if($this->shake > 0){ --$this->shake; } - + if($this->inGround){ [$blockID, $blockMeta] = $this->level->level->getBlock($this->xTile, $this->yTile, $this->zTile); $this->speedZ = $this->speedX = $this->speedY = 0; - + if($blockID == $this->inTile && $blockMeta == $this->inData){ ++$this->groundTicks; if($this->groundTicks >= 1200){ //TODO customizeable? @@ -147,7 +146,7 @@ class Arrow extends Entity{ $this->speedZ *= lcg_value() * 0.2; $this->groundTicks = $this->airTicks = 0; } - + }else{ ++$this->airTicks; $start = new Vector3($this->x, $this->y, $this->z); @@ -162,42 +161,40 @@ class Arrow extends Entity{ }else{ $end = new Vector3($this->x + $this->speedX, $this->y + $this->speedY, $this->z + $this->speedZ); } - - + $entities = $this->level->getEntitiesInAABB($this->boundingBox->addCoord($this->speedX, $this->speedY, $this->speedZ)->expand(1, 1, 1)); $bestDist = 0; $bestEnt = null; - + foreach($entities as $eid => $ent){ if($eid != $this->eid && $ent->isPickable() && ($eid != $this->shooterEID || $this->airTicks >= 5)){ - + $v12 = $ent->boundingBox->expand(0.3, 0.3, 0.3); $v13 = $v12->calculateIntercept($start, $end); - + if($v13 != null){ $dist = $start->distance($v13->hitVector); - + if($dist < $bestDist || $bestDist == 0){ $bestEnt = $ent; } } } } - + if($bestEnt != null){ $v4 = MovingObjectPosition::fromEntity($bestEnt); } - + if($v4 != null){ if($v4->entityHit != null){ - $v49 = sqrt($this->speedY*$this->speedY + $this->speedX*$this->speedX + $this->speedZ*$this->speedZ); - $damage = ceil($v49+$v49); - + $v49 = sqrt($this->speedY * $this->speedY + $this->speedX * $this->speedX + $this->speedZ * $this->speedZ); + $damage = ceil($v49 + $v49); + if($this->criticial){ $damage += mt_rand(0, $damage / 2 + 1); } - - + if($v4->entityHit->harm($damage, $this->eid)){ //vanilla seems to increase arrow count if $v4->entity is mob $this->close(); @@ -213,36 +210,36 @@ class Arrow extends Entity{ $this->xTile = $v4->blockX; $this->yTile = $v4->blockY; $this->zTile = $v4->blockZ; - + [$this->inTile, $this->inData] = $this->level->level->getBlock($this->xTile, $this->yTile, $this->zTile); - + $this->speedX = $v4->hitVector->x - $this->x; $this->speedY = $v4->hitVector->y - $this->y; $this->speedZ = $v4->hitVector->z - $this->z; - - $v21 = sqrt($this->speedX*$this->speedX + $this->speedY*$this->speedY + $this->speedZ*$this->speedZ); + + $v21 = sqrt($this->speedX * $this->speedX + $this->speedY * $this->speedY + $this->speedZ * $this->speedZ); if($v21 != 0){ $this->x -= $this->speedX / $v21 * 0.05; $this->y -= $this->speedY / $v21 * 0.05; $this->z -= $this->speedZ / $v21 * 0.05; } - + $this->inGround = true; $this->shake = 7; $this->criticial = false; } } - + $this->x += $this->speedX; $this->y += $this->speedY; $this->z += $this->speedZ; - $v21 = sqrt($this->speedX*$this->speedX + $this->speedZ*$this->speedZ); + $v21 = sqrt($this->speedX * $this->speedX + $this->speedZ * $this->speedZ); $this->yaw = atan2($this->speedX, $this->speedZ) * 180 / M_PI; $this->pitch = atan2($this->speedY, $v21) * 180 / M_PI; - + $this->pitch = $this->lastPitch + ($this->pitch - $this->lastPitch) * 0.2; $this->yaw = $this->lastYaw + ($this->yaw - $this->lastYaw) * 0.2; - + $v24 = $this->inWater ? 0.8 : 0.99; $this->speedX *= $v24; $this->speedY *= $v24; @@ -252,14 +249,12 @@ class Arrow extends Entity{ $v7 = $this->width / 2; $v8 = $this->height; $this->boundingBox->setBounds($this->x - $v7, $this->y - $this->yOffset /*+ $this->ySize*/, $this->z - $v7, $this->x + $v7, $this->y - $this->yOffset + $v8 /*+ $this->ySize*/, $this->z + $v7); - + $this->doBlocksCollision(); } - - - + } - + public function spawn($player){ if($this->type === OBJECT_ARROW){ $pk = new AddEntityPacket; @@ -268,7 +263,7 @@ class Arrow extends Entity{ $pk->x = $this->x; $pk->y = $this->y; $pk->z = $this->z; - $pk->did = 1; + $pk->did = 1; $pk->speedX = $this->speedX; $pk->speedY = $this->speedY; $pk->speedZ = $this->speedZ; diff --git a/src/entity/object/FallingSand.php b/src/entity/object/FallingSand.php index 517d4ca7..745d289b 100644 --- a/src/entity/object/FallingSand.php +++ b/src/entity/object/FallingSand.php @@ -3,9 +3,9 @@ class FallingSand extends Entity{ const TYPE = FALLING_SAND; const CLASS_TYPE = ENTITY_FALLING; - + public $fallTime = 0; - + public function __construct($level, $eid, $class, $type = 0, $data = []){ parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(PHP_INT_MAX, "generic"); @@ -15,14 +15,14 @@ class FallingSand extends Entity{ $this->hasGravity = true; $this->gravity = 0.04; } - + public function isPickable(){ return !$this->dead; } - + public function update($now){ if($this->closed) return; - + if( $this->data["Tile"] == AIR) { $this->close(); return; @@ -33,15 +33,15 @@ class FallingSand extends Entity{ ++$this->fallTime; $this->speedY -= 0.04; $this->move($this->speedX, $this->speedY, $this->speedZ); - + $this->speedX *= 0.98; $this->speedY *= 0.98; $this->speedZ *= 0.98; - + $x = floor($this->x); $y = floor($this->y); $z = floor($this->z); - + if($this->onGround){ $this->speedX *= 0.7; $this->speedZ *= 0.7; diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index 9af57bee..f290fe07 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -4,10 +4,10 @@ class ItemEntity extends Entity{ const TYPE = ENTITY_ITEM_TYPE; const CLASS_TYPE = ENTITY_ITEM; public static $searchRadiusX = 0.5, $searchRadiusY = 0.0, $searchRadiusZ = 0.5; - + public $meta, $stack, $itemID; - - public function __construct(Level $level, $eid, $class, $type = 0, $data = array()) + + public function __construct(Level $level, $eid, $class, $type = 0, $data = []) { parent::__construct($level, $eid, $class, $type, $data); $this->setSize(0.25, 0.25); @@ -27,21 +27,21 @@ class ItemEntity extends Entity{ $this->delayBeforePickup = 40; //in vanilla it is 0 by default $this->stepHeight = 0; } - + public function counterUpdate(){ parent::counterUpdate(); if($this->delayBeforePickup > 0) --$this->delayBeforePickup; } - + public function searchForOtherItemsNearby(){ $ents = $this->level->getEntitiesInAABBOfType($this->boundingBox->expand(self::$searchRadiusX, self::$searchRadiusY, self::$searchRadiusZ), ENTITY_ITEM); - + foreach($ents as $e){ $this->tryCombining($e); } - + } - + public function spawn($player) { $pk = new AddItemEntityPacket(); @@ -55,7 +55,7 @@ class ItemEntity extends Entity{ $pk->item = BlockAPI::getItem($this->itemID, $this->meta, $this->stack); $pk->metadata = $this->getMetadata(); $player->dataPacket($pk); - + $pk = new SetEntityMotionPacket(); $pk->eid = $this->eid; $pk->speedX = $this->speedX; @@ -63,16 +63,15 @@ class ItemEntity extends Entity{ $pk->speedZ = $this->speedZ; $player->dataPacket($pk); } - - + public function tryCombining(Entity $another){ - + if($another->eid == $this->eid) return false; - + if(!$another->closed && !$this->closed){ if($another->itemID == $this->itemID && $another->meta == $this->meta){ if(($another->stack + $this->stack) > 64) return false; //TODO dynamic stack size - + $another->stack += $this->stack; $another->age = min($this->age, $another->age); $this->close(); @@ -84,29 +83,29 @@ class ItemEntity extends Entity{ $xFloor = floor($x); $yFloor = floor($y); $zFloor = floor($z); - + $id = $this->level->level->getBlockID($xFloor, $yFloor, $zFloor); - + if(StaticBlock::getIsSolid($id)){ $xDiff = $x - $xFloor; - + $id = $this->level->level->getBlockID($xFloor - 1, $yFloor, $zFloor); $xNeg = StaticBlock::getIsSolid($id); - + $id = $this->level->level->getBlockID($xFloor + 1, $yFloor, $zFloor); $xPos = StaticBlock::getIsSolid($id); - + $id = $this->level->level->getBlockID($xFloor, $yFloor - 1, $zFloor); $yNeg = StaticBlock::getIsSolid($id); - + $id = $this->level->level->getBlockID($xFloor, $yFloor + 1, $zFloor); $yPos = StaticBlock::getIsSolid($id); - + $id = $this->level->level->getBlockID($xFloor - 1, $yFloor, $zFloor - 1); $zNeg = StaticBlock::getIsSolid($id); - + $zPos = $this->level->level->getBlockID($xFloor + 1, $yFloor, $zFloor + 1); - + if($xNeg || $xDiff >= 9999.0){ //TODO not needed check? $v15 = 9999.0; $v16 = -1; @@ -114,22 +113,22 @@ class ItemEntity extends Entity{ $v15 = $xDiff; $v16 = 0; } - + if(!$xPos){ $v17 = 1 - $xDiff; - + if($v17 < $v15){ $v15 = $v17; $v16 = 1; } } - + $yDiff = $y - $yFloor; if(!$yNeg && $yDiff < $v15){ $v15 = $yDiff; $v16 = 2; } - + if(!$yPos){ $v19 = 1.0 - $yDiff; if($v19 < $v15){ @@ -137,17 +136,17 @@ class ItemEntity extends Entity{ $v16 = 3; } } - + $zDiff = $z - $zFloor; if(!$zNeg && $zDiff < $v15){ $v15 = $zDiff; $v16 = 4; } - + if(!StaticBlock::getIsSolid($zPos) && ((1.0 - $zDiff) < $v15)){ $v16 = 5; } - + $v21 = lcg_value() * 0.2 + 0.1; switch($v16){ case 0: @@ -166,24 +165,24 @@ class ItemEntity extends Entity{ $this->speedZ = $v21; return 0; } - + return 0; } } - + public function handleWaterMovement(){ return $this->level->handleMaterialAcceleration($this->boundingBox, 0, $this); } - + public function updateEntityMovement(){ //TODO custom update( method $this->speedY -= 0.04; //$this->noClip = false; $this->checkInTile($this->x, $this->y, $this->z); $this->move($this->speedX, $this->speedY, $this->speedZ); - - $var1 = (int)$this->x != (int)$this->lastX || (int)$this->y != (int)$this->lastY || (int)$this->z != (int)$this->lastZ; - + + $var1 = (int) $this->x != (int) $this->lastX || (int) $this->y != (int) $this->lastY || (int) $this->z != (int) $this->lastZ; + if($var1 || $this->counter % 25 == 0){ $blockIDAt = $this->level->level->getBlockID(floor($this->x), floor($this->y), floor($this->z)); if($blockIDAt == LAVA || $blockIDAt == STILL_LAVA){ @@ -191,30 +190,30 @@ class ItemEntity extends Entity{ $this->speedX = (lcg_value() - lcg_value()) * 0.2; $this->speedZ = (lcg_value() - lcg_value()) * 0.2; } - + //$this->searchForOtherItemsNearby(); //not in vanilla 0.8.1 } - + if($this->closed) return; - + $friction = 0.98; if($this->onGround){ $friction = 0.588; $v3 = $this->level->level->getBlockID($this->x, floor($this->boundingBox->minY) - 1, $this->z); if($v3 > 0) $friction = StaticBlock::getSlipperiness($v3); } - + $this->speedX *= $friction; $this->speedY *= 0.98; $this->speedZ *= $friction; - + if($this->onGround) $this->speedY *= -0.5; - + if(abs($this->speedX) < self::MIN_POSSIBLE_SPEED) $this->speedX = 0; if(abs($this->speedZ) < self::MIN_POSSIBLE_SPEED) $this->speedZ = 0; if(abs($this->speedY) < self::MIN_POSSIBLE_SPEED) $this->speedY = 0; - + ++$this->age; - //TODO despawn after age >= 6000 ?; + //TODO despawn after age >= 6000 ?; } } diff --git a/src/entity/object/Minecart.php b/src/entity/object/Minecart.php index f10ab47e..5361e47e 100644 --- a/src/entity/object/Minecart.php +++ b/src/entity/object/Minecart.php @@ -4,7 +4,7 @@ class Minecart extends Vehicle{ const TYPE = OBJECT_MINECART; /** * A minecart rotation matrix - * @var int[][][] + * @var int[][][] */ private static $matrix = [ [ @@ -48,14 +48,14 @@ class Minecart extends Vehicle{ [1, 0, 0] ] ]; - + private $hurtTime = 0; //syncentdata 17 int private $damage = 0; //syncentdata 19 float - + private $minecartX = 0, $minecartY = 0, $minecartZ = 0; private $turnProgress = 0; public $isInReverse = false; - + function __construct(Level $level, $eid, $class, $type = 0, $data = []){ parent::__construct($level, $eid, $class, $type, $data); $this->canBeAttacked = true; @@ -67,76 +67,76 @@ class Minecart extends Vehicle{ $this->yOffset = $this->height / 2; $this->stepHeight = 0; } - + public function isPickable(){ return !$this->dead; } - + public function getDrops(){ return [ [MINECART, 0, 1] ]; } - + public function getPos($x, $y, $z){ $blockX = floor($x); $blockY = floor($y); $blockZ = floor($z); - + if(RailBaseBlock::isRailBlock($this->level, $blockX, $blockY - 1, $blockZ)) --$blockY; - + [$id, $meta] = $this->level->level->getBlock($blockX, $blockY, $blockZ); if(RailBaseBlock::isRailID($id)){ - + if($id == POWERED_RAIL) $meta &= 7; - + $mat = self::$matrix[$meta]; $v13 = 0; $v15 = $blockX + 0.5 + $mat[0][0] * 0.5; $v17 = $blockY + 0.5 + $mat[0][1] * 0.5; $v19 = $blockZ + 0.5 + $mat[0][2] * 0.5; - + $v21 = $blockX + 0.5 + $mat[1][0] * 0.5; $v23 = $blockY + 0.5 + $mat[1][1] * 0.5; $v25 = $blockZ + 0.5 + $mat[1][2] * 0.5; - + $v27 = $v21 - $v15; $v29 = ($v23 - $v17) * 2; $v31 = $v25 - $v19; - + if($v27 == 0){ $x = $blockX + 0.5; $v13 = $z - $blockZ; - }else if($v31 == 0){ + }elseif($v31 == 0){ $z = $blockZ + 0.5; $v13 = $x - $blockX; }else{ $v33 = $x - $v15; $v35 = $z - $v19; - $v13 = ($v33*$v27 + $v35*$v31) * 2; + $v13 = ($v33 * $v27 + $v35 * $v31) * 2; } - + $x = $v15 + $v27 * $v13; $y = $v17 + $v29 * $v13; $z = $v19 + $v31 * $v13; - + if($v29 < 0) ++$y; if($v29 > 0) $y += 0.5; return [$x, $y, $z]; } - + return false; } - + public function moveAlongTrack($x, $y, $z, $maxSpeed, $boost, $id, $meta){ $this->fallDistance = 0; $vec = $this->getPos($this->x, $this->y, $this->z); $this->y = $y; - + if($id == POWERED_RAIL) $meta &= 7; - + if($meta >= 2 && $meta <= 5) $this->y = $y + 1; - + switch($meta){ case 2: $this->speedX -= $boost; @@ -151,53 +151,53 @@ class Minecart extends Vehicle{ $this->speedZ -= $boost; break; } - + $mat = self::$matrix[$meta]; $matXDiff = $mat[1][0] - $mat[0][0]; $matZDiff = $mat[1][2] - $mat[0][2]; - $matDiffTotal = sqrt($matXDiff*$matXDiff + $matZDiff*$matZDiff); - + $matDiffTotal = sqrt($matXDiff * $matXDiff + $matZDiff * $matZDiff); + if(($this->speedZ * $matZDiff + $this->speedX * $matXDiff) < 0){ $matXDiff = -$matXDiff; $matZDiff = -$matZDiff; } - $speedTotal = sqrt($this->speedZ*$this->speedZ + $this->speedX*$this->speedX); + $speedTotal = sqrt($this->speedZ * $this->speedZ + $this->speedX * $this->speedX); if($speedTotal > 2) $speedTotal = 2; $this->speedX = ($speedTotal * $matXDiff) / $matDiffTotal; $this->speedZ = ($speedTotal * $matZDiff) / $matDiffTotal; - + if($this->linkedEntity != 0 && !$this->isRider){ $rider = $this->level->entityList[$this->linkedEntity] ?? false; if($rider instanceof Entity && ($rider->isPlayer() || $rider->class == ENTITY_MOB)){ if($rider->moveForward > 0){ $v32 = sin($this->yaw * M_PI / 180); $v33 = cos($this->yaw * M_PI / 180); - - if($this->speedZ*$this->speedZ + $this->speedX*$this->speedX < 0.01){ - $this->speedX -= $v32*0.1; - $this->speedZ += $v33*0.1; + + if($this->speedZ * $this->speedZ + $this->speedX * $this->speedX < 0.01){ + $this->speedX -= $v32 * 0.1; + $this->speedZ += $v33 * 0.1; } } } } - + $v38 = ($x + 0.5) + ($mat[0][0] * 0.5); $v39 = ($z + 0.5) + ($mat[0][2] * 0.5); $v40 = (($x + 0.5) + ($mat[1][0] * 0.5)) - $v38; $v41 = (($z + 0.5) + ($mat[1][2] * 0.5)) - $v39; - + if($v40 == 0){ $v42 = $this->z - $z; - }else if($v41 == 0){ + }elseif($v41 == 0){ $v42 = $this->x - $x; }else{ $v44 = (($this->z - $v39) * $v41) + (($this->x - $v38) * $v40); $v42 = $v44 + $v44; } - + $this->x = $v38 + ($v40 * $v42); $this->z = $v39 + ($v41 * $v42); - + $this->setPos($this->x, $this->y + $this->yOffset + 0.00001, $this->z); $dx = $this->speedX; $dz = $this->speedZ; @@ -205,55 +205,55 @@ class Minecart extends Vehicle{ $dx *= 0.75; $dz *= 0.75; } - + if($dx < -$maxSpeed) $dx = -$maxSpeed; - else if($dx > $maxSpeed) $dx = $maxSpeed; - + elseif($dx > $maxSpeed) $dx = $maxSpeed; + if($dz < -$maxSpeed) $dz = -$maxSpeed; - else if($dz > $maxSpeed) $dz = $maxSpeed; - + elseif($dz > $maxSpeed) $dz = $maxSpeed; + $this->move($dx, 0, $dz); - + if($mat[0][1]){ if((floor($this->x) - $x) == $mat[0][0] && (floor($this->z) - $z) == $mat[0][2]){ $this->setPos($this->x, $mat[0][1] + $this->y, $this->z); - }else if($mat[1][1]){ + }elseif($mat[1][1]){ goto mat_1_1; } - }else if($mat[1][1]){ + }elseif($mat[1][1]){ mat_1_1: if((floor($this->x) - $x) == $mat[1][0] && (floor($this->z) - $z) == $mat[1][2]){ $this->setPos($this->x, $mat[1][1] + $this->y, $this->z); } } - + $this->applyNaturalSlowdown(); $vec2 = $this->getPos($this->x, $this->y, $this->z); if($vec2 !== false && $vec !== false){ $yDiff = ($vec[1] - $vec2[1]) * 0.05; - $totalSpeed = sqrt($this->speedZ*$this->speedZ + $this->speedX*$this->speedX); - + $totalSpeed = sqrt($this->speedZ * $this->speedZ + $this->speedX * $this->speedX); + if($totalSpeed > 0){ $this->speedX = ($this->speedX / $totalSpeed) * ($totalSpeed + $yDiff); - $this->speedZ = ($this->speedZ / $totalSpeed) * ($totalSpeed + $yDiff); + $this->speedZ = ($this->speedZ / $totalSpeed) * ($totalSpeed + $yDiff); } $this->setPos($this->x, $vec2[1], $this->z); } - + if(floor($this->x) != $x || floor($this->z) != $z){ //this breaks everything - $totalSpeed = sqrt($this->speedZ*$this->speedZ + $this->speedX*$this->speedX); + $totalSpeed = sqrt($this->speedZ * $this->speedZ + $this->speedX * $this->speedX); $this->speedX = (floor($this->x) - $x) * $totalSpeed; $this->speedZ = (floor($this->z) - $z) * $totalSpeed; } - + if($id == POWERED_RAIL){ - $totalSpeed = sqrt($this->speedZ*$this->speedZ + $this->speedX*$this->speedX); + $totalSpeed = sqrt($this->speedZ * $this->speedZ + $this->speedX * $this->speedX); if($totalSpeed > 0.01){ $this->speedX += (($this->speedX / $totalSpeed) * 0.06); $this->speedZ += (($this->speedZ / $totalSpeed) * 0.06); return; } - + if($meta == 1){ //TODO Level::isSolidBlockingTile if(StaticBlock::getIsSolid($this->level->level->getBlockID($x - 1, $y, $z))){ @@ -265,7 +265,7 @@ class Minecart extends Vehicle{ $v63 = -0.02; } $this->speedX = $v63; - }else if($meta == 0){ + }elseif($meta == 0){ //TODO Level::isSolidBlockingTile if(StaticBlock::getIsSolid($this->level->level->getBlockID($x, $y, $z - 1))){ return; @@ -278,58 +278,57 @@ class Minecart extends Vehicle{ $this->speedZ = $v64; } } - + } - - + public function applyNaturalSlowdown(){ $mult = $this->linkedEntity != 0 && !$this->isRider ? 0.997 : 0.96; - + $this->speedX *= $mult; $this->speedY = 0; $this->speedZ *= $mult; } - + public function comeOffTrack($topSpeed){ if($this->speedX < -$topSpeed) $this->speedX = -$topSpeed; - else if($this->speedX > $topSpeed) $this->speedX = $topSpeed; - + elseif($this->speedX > $topSpeed) $this->speedX = $topSpeed; + if($this->speedZ < -$topSpeed) $this->speedZ = -$topSpeed; - else if($this->speedZ > $topSpeed) $this->speedZ = $topSpeed; - + elseif($this->speedZ > $topSpeed) $this->speedZ = $topSpeed; + if($this->onGround){ $this->speedX *= 0.5; $this->speedY *= 0.5; $this->speedZ *= 0.5; } - + $this->move($this->speedX, $this->speedY, $this->speedZ); - + if(!$this->onGround){ $this->speedX *= 0.95; $this->speedY *= 0.95; $this->speedZ *= 0.95; } - + } public function applyCollision(Entity $collided){ $diffX = $collided->x - $this->x; $diffZ = $collided->z - $this->z; - $dist = $diffX*$diffX + $diffZ*$diffZ; + $dist = $diffX * $diffX + $diffZ * $diffZ; if($dist >= 0.0001){ $sqrtMax = sqrt($dist); $diffX /= $sqrtMax; $diffZ /= $sqrtMax; - + $col = (($v = 1 / $sqrtMax) > 1 ? 1 : $v); $diffX *= $col; $diffZ *= $col; $diffX *= 0.1; $diffZ *= 0.1; - + $diffX *= 0.5; $diffZ *= 0.5; - + $this->addVelocity(-$diffX, 0, -$diffZ); $collided->addVelocity($diffX / 4, 0, $diffZ / 4); } @@ -341,18 +340,18 @@ class Minecart extends Vehicle{ } $this->updateLast(); //$this->updatePosition(); - + $this->speedY -= 0.04; //TODO port stuff - + $blockX = floor($this->x); $blockY = floor($this->y); $blockZ = floor($this->z); - + if(RailBaseBlock::isRailBlock($this->level, $blockX, $blockY - 1, $blockZ)){ --$blockY; } - + [$id, $meta] = $this->level->level->getBlock($blockX, $blockY, $blockZ); if(RailBaseBlock::isRailID($id)){ $this->moveAlongTrack($blockX, $blockY, $blockZ, 0.4, 0.0078125, $id, $meta); @@ -362,32 +361,32 @@ class Minecart extends Vehicle{ } $this->doBlocksCollision(); - + $this->pitch = 0; $diffX = $this->lastX - $this->x; $diffZ = $this->lastZ - $this->z; - - if($diffX*$diffX + $diffZ*$diffZ > 0.001){ + + if($diffX * $diffX + $diffZ * $diffZ > 0.001){ $this->yaw = atan2($diffZ, $diffX) * 180 / M_PI; - + if($this->isInReverse) $this->yaw += 180; } - + $yw = fmod($this->yaw - $this->lastYaw, 360); if($yw >= 180) $yw -= 360; if($yw < 180) $yw += 360; - + if($yw < -170 || $yw >= 170){ $this->isInReverse = !$this->isInReverse; $this->yaw = $this->yaw + 180; } - + $bb = $this->boundingBox->expand(0.2, 0, 0.2); - $minChunkX = ((int)($bb->minX - 2)) >> 4; - $minChunkZ = ((int)($bb->minZ - 2)) >> 4; - $maxChunkX = ((int)($bb->minX + 2)) >> 4; - $maxChunkZ = ((int)($bb->minZ + 2)) >> 4; - + $minChunkX = ((int) ($bb->minX - 2)) >> 4; + $minChunkZ = ((int) ($bb->minZ - 2)) >> 4; + $maxChunkX = ((int) ($bb->minX + 2)) >> 4; + $maxChunkZ = ((int) ($bb->minZ + 2)) >> 4; + //TODO also index by chunkY? for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){ @@ -401,14 +400,14 @@ class Minecart extends Vehicle{ }else{ $e->applyCollision($this); } - + } } } } } } - + public function close() { parent::close(); @@ -421,11 +420,11 @@ class Minecart extends Vehicle{ } } } - + public function isPushable(){ return false; //TODO replace with true } - + public function spawn($player){ $pk = new AddEntityPacket; $pk->eid = $this->eid; @@ -436,7 +435,7 @@ class Minecart extends Vehicle{ $pk->yaw = $this->yaw; $pk->pitch = $this->pitch; $player->dataPacket($pk); - + $pk = new SetEntityMotionPacket; $pk->eid = $this->eid; $pk->speedX = $this->speedX; @@ -444,7 +443,7 @@ class Minecart extends Vehicle{ $pk->speedZ = $this->speedZ; $player->dataPacket($pk); } - + public function interactWith(Entity $e, $action){ if($action === InteractPacket::ACTION_HOLD && $e->isPlayer() && $this->canRide($e)){ $e->setRiding($this); diff --git a/src/entity/object/Painting.php b/src/entity/object/Painting.php index 21f30927..a0e7b99e 100644 --- a/src/entity/object/Painting.php +++ b/src/entity/object/Painting.php @@ -3,23 +3,23 @@ class Painting extends Entity{ const CLASS_TYPE = ENTITY_OBJECT; const TYPE = OBJECT_PAINTING; - + public $motive; public $direction; public $xPos, $yPos, $zPos; - + public $isValid = true; - + public function __construct(Level $level, $eid, $class, $type = 0, $data = []){ parent::__construct($level, $eid, $class, $type, $data); $this->x = $this->data["TileX"] ?? $this->x; $this->y = $this->data["TileY"] ?? $this->y; $this->z = $this->data["TileZ"] ?? $this->z; - - $this->xPos = $this->data["xPos"] ?? (int)$this->x; - $this->zPos = $this->data["zPos"] ?? (int)$this->z; - $this->yPos = $this->data["yPos"] ?? (int)$this->y; - + + $this->xPos = $this->data["xPos"] ?? (int) $this->x; + $this->zPos = $this->data["zPos"] ?? (int) $this->z; + $this->yPos = $this->data["yPos"] ?? (int) $this->y; + $this->health = 1; $this->canBeAttacked = true; $this->width = 1; @@ -30,30 +30,30 @@ class Painting extends Entity{ if($dir === false){ $dir = floor($this->yaw / 90); } - + if($mot === false){ $this->setRandomMotive($dir); - + }else{ $this->motive = $mot; $this->direction = $dir; $this->setDirection($dir); } } - + public $counter = 0; public function update($now){ - if($this->closed === true){ - return false; - } - + if($this->closed === true){ + return false; + } + if(++$this->counter >= 100){ $this->counter = 0; if(!$this->survives() && !$this->dead){ //maybe dont check entity collision? $this->makeDead("nosurvive"); } } - + return true; } @@ -62,34 +62,34 @@ class Painting extends Entity{ foreach(PaintingItem::$motives as $name => $motive){ $this->motive = $name; $this->setDirection($dir); - + if($this->survives()){ $tochoose[] = $name; } } if(count($tochoose) > 0){ - $ind = mt_rand(0, count($tochoose)-1); + $ind = mt_rand(0, count($tochoose) - 1); }else{ $this->isValid = false; return; } - + $this->motive = $tochoose[$ind]; $this->setDirection($dir); } - + public static function offs($n){ if($n == 32 || $n == 64) return 0.5; return 0; } - + const DIRECTION_OPPOSITE = [2, 3, 0, 1]; public function setDirection($dir){ $this->direction = $dir; $this->lastYaw = $this->yaw = $dir * 90; - $size = PaintingItem::$motives[$this->motive] ?? array(1, 1); - $width = $size[0]*16; - $height = $size[1]*16; + $size = PaintingItem::$motives[$this->motive] ?? [1, 1]; + $width = $size[0] * 16; + $height = $size[1] * 16; if($dir == 2 || $dir == 0){ $v10 = 2; $v9 = $width; @@ -98,28 +98,28 @@ class Painting extends Entity{ $v9 = 2; $v10 = $width; } - + $v12 = $v9 * 0.03125; $v13 = $height * 0.03125; $v14 = $v10 * 0.03125; $v15 = $this->xPos + 0.5; $v16 = $this->yPos + 0.5; $v17 = $this->zPos + 0.5; //XXX check - + if($dir == 2){ $v17 = $v17 - 0.5625; $v15 = $v15 - self::offs($width); - }else if($dir == 1){ + }elseif($dir == 1){ $v15 = $v15 - 0.5625; $v17 = $v17 + self::offs($width); - }else if($dir == 3){ + }elseif($dir == 3){ $v15 = $v15 + 0.5625; $v17 = $v17 - self::offs($width); }else{ $v17 = $v17 + 0.5625; $v15 = $v15 + self::offs($width); } - + $v23 = $v16 + self::offs($height); $this->setPos($v15, $v23, $v17); $this->boundingBox->minX = ($v15 - $v12) + 0.03125; @@ -129,35 +129,35 @@ class Painting extends Entity{ $this->boundingBox->maxY = ($v23 + $v13) - 0.03125; $this->boundingBox->maxZ = ($v17 + $v14) - 0.03125; } - + public function getDrops(){ return [ [PAINTING, 0, 1] ]; } - + public function isPickable(){ return true; } - + public function survives(){ $cubes = $this->level->getCubes($this, $this->boundingBox); if(count($cubes) == 0){ - - $size = PaintingItem::$motives[$this->motive] ?? array(1, 1); + + $size = PaintingItem::$motives[$this->motive] ?? [1, 1]; $width = $size[0]; - $width2 = $width*16/32; + $width2 = $width * 16 / 32; $height = $size[1]; - $height2 = $height*16/32; + $height2 = $height * 16 / 32; if($width < 1) $width = 1; if($height < 1) $height = 1; $positionX = $this->xPos; $positionZ = $this->zPos; if($this->direction == 2 || $this->direction == 0) $positionX = floor($this->x - $width2); if($this->direction == 1 || $this->direction == 3) $positionZ = floor($this->z - $width2); - + $minY = floor($this->y - $height2); - + for($off = 0; $off < $width; ++$off){ for($yoff = 0; $yoff < $height; ++$yoff){ $y = $minY + $yoff; @@ -166,13 +166,13 @@ class Painting extends Entity{ }else{ $id = $this->level->level->getBlockId($positionX + $off, $y, $this->zPos); } - + if(!StaticBlock::getIsSolid($id)){ return false; } } } - + $ents = $this->level->getEntitiesInAABBOfType($this->boundingBox, ENTITY_OBJECT); foreach($ents as $e){ if($e->eid == $this->eid) continue; @@ -182,13 +182,13 @@ class Painting extends Entity{ } return true; } - + return false; } - + public function createSaveData(){ $data = parent::createSaveData(); - + $data["Motive"] = $this->motive; $data["Direction"] = $this->direction; $data["xPos"] = $this->xPos; diff --git a/src/entity/object/PrimedTNT.php b/src/entity/object/PrimedTNT.php index f88adb7e..b6be4d6c 100644 --- a/src/entity/object/PrimedTNT.php +++ b/src/entity/object/PrimedTNT.php @@ -14,11 +14,11 @@ class PrimedTNT extends Entity{ $this->yOffset = $this->height / 2; $this->setHealth(10000000, "generic"); } - + public function isPickable(){ return !$this->dead; } - + public function getMetadata(){ $d = parent::getMetadata(); $d[16]["value"] = (int) $this->data["fuse"]; @@ -26,34 +26,34 @@ class PrimedTNT extends Entity{ } public function createSaveData(){ $data = parent::createSaveData(); - + $data["fuse"] = $this->data["fuse"]; $data["power"] = $this->data["power"]; - + return $data; } - + public function update($now){ if($this->closed) return; $this->lastX = $this->x; $this->lastY = $this->y; $this->lastZ = $this->z; - + $this->speedY -= 0.04; $this->move($this->speedX, $this->speedY, $this->speedZ); - $this->speedX *- 0.98; + $this->speedX * -0.98; $this->speedY *= 0.98; $this->speedZ *= 0.98; - + if($this->onGround){ $this->speedX *= 0.7; $this->speedZ *= 0.7; - + $this->speedY *= -0.5; } - + $tickDiff = ($now - $this->lastUpdate) / 0.05; - + $this->data["fuse"] -= $tickDiff; $this->updateMetadata(); if($this->data["fuse"] <= 0){ @@ -61,10 +61,10 @@ class PrimedTNT extends Entity{ $explosion = new Explosion($this, $this->data["power"]); $explosion->explode(); } - + $this->lastUpdate = $now; } - + public function spawn($player){ $pk = new AddEntityPacket; $pk->eid = $this->eid; diff --git a/src/entity/object/ThrownEgg.php b/src/entity/object/ThrownEgg.php index 661bbe0f..98794845 100644 --- a/src/entity/object/ThrownEgg.php +++ b/src/entity/object/ThrownEgg.php @@ -1,32 +1,32 @@ typeOfHit == 1){ $hitResult->entityHit->harm(0, $this->eid); } - + $rand = mt_rand(0, 7); if($rand == 0){ $count = mt_rand(0, 31) == 0 ? 4 : 1; $data = [ "x" => $this->x, "y" => $this->y, - "z" => $this->z, + "z" => $this->z, "yaw" => $this->yaw, "pitch" => 0, "IsBaby" => true ]; for($i = 0; $i < $count; ++$i){ $chicken = $this->server->api->entity->add($this->level, ENTITY_MOB, MOB_CHICKEN, $data); - + $this->server->api->entity->spawnToAll($chicken); } } - + $this->close(); } - - -} \ No newline at end of file + + } \ No newline at end of file diff --git a/src/entity/object/ThrownSnowball.php b/src/entity/object/ThrownSnowball.php index a6758cd3..81104061 100644 --- a/src/entity/object/ThrownSnowball.php +++ b/src/entity/object/ThrownSnowball.php @@ -1,11 +1,12 @@ typeOfHit == 1){ $hitResult->entityHit->harm(0, $this->eid); } - + $this->close(); } } \ No newline at end of file diff --git a/src/entity/object/TripodCamera.php b/src/entity/object/TripodCamera.php index 6855cd3c..f69339d8 100644 --- a/src/entity/object/TripodCamera.php +++ b/src/entity/object/TripodCamera.php @@ -2,16 +2,16 @@ class TripodCamera extends Entity{ const TYPE = OBJECT_TRIPOD_CAMERA; - + function __construct(Level $level, $eid, $class, $type = 0, $data = []){ parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(PHP_INT_MAX, "generic"); } - + public function isPickable(){ return !$this->dead; } - + public function interactWith(Entity $e, $action){ if($e->isPlayer() and $action === InteractPacket::ACTION_HOLD){ //todo diff --git a/src/entity/registry/EntityRegistry.php b/src/entity/registry/EntityRegistry.php index 565c65b2..b2a8641d 100644 --- a/src/entity/registry/EntityRegistry.php +++ b/src/entity/registry/EntityRegistry.php @@ -1,8 +1,8 @@ isAbstract()){ //self::$entityList[$className::TYPE] = $className; - + self::$entityList->addEntity(new PropertyEntity($className::CLASS_TYPE, $className::TYPE, $className)); //self::$shortNames[$className] = $class->getShortName(); what is this even supposed to do? //console("[INFO] Registered a ".$className); diff --git a/src/entity/registry/PropertyEntity.php b/src/entity/registry/PropertyEntity.php index d2cf972b..999620be 100644 --- a/src/entity/registry/PropertyEntity.php +++ b/src/entity/registry/PropertyEntity.php @@ -2,13 +2,13 @@ class PropertyEntity{ private $class,$type,$name; - + function __construct($class, $type, $name){ $this->class = $class; $this->type = $type; $this->name = $name; } - + public function getEntityClass(){ return $this->class; } diff --git a/src/event/EventHandler.php b/src/event/EventHandler.php index 7751306c..96f5b7d3 100644 --- a/src/event/EventHandler.php +++ b/src/event/EventHandler.php @@ -16,7 +16,7 @@ abstract class EventHandler{ }else{ return BaseEvent::ALLOW; } - } + } } } diff --git a/src/event/EventPriority.php b/src/event/EventPriority.php index f42e8d73..6de6a27e 100644 --- a/src/event/EventPriority.php +++ b/src/event/EventPriority.php @@ -2,11 +2,11 @@ /** * - * ____ _ _ __ __ _ __ __ ____ - * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \ + * ____ _ _ __ __ _ __ __ ____ + * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \ * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) | - * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/ - * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_| + * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/ + * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_| * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by @@ -15,7 +15,7 @@ * * @author PocketMine Team * @link http://www.pocketmine.net/ - * + * * */ @@ -44,9 +44,9 @@ abstract class EventPriority{ const HIGHEST = 1; /** * Event is listened to purely for monitoring the outcome of an event. - * + * * No modifications to the event should be made under this priority */ const MONITOR = 0; - + } \ No newline at end of file diff --git a/src/event/PluginEvent.php b/src/event/PluginEvent.php index 3887bb0d..0796e7bb 100644 --- a/src/event/PluginEvent.php +++ b/src/event/PluginEvent.php @@ -1,6 +1,5 @@ packet = $packet; $this->player = $player; } - + public function getPacket(){ return $this->packet; } - + public function getPlayer(){ return $this->player; } diff --git a/src/event/server/DataPacketSendEvent.php b/src/event/server/DataPacketSendEvent.php index 5e55a29b..62f2e6a0 100644 --- a/src/event/server/DataPacketSendEvent.php +++ b/src/event/server/DataPacketSendEvent.php @@ -2,11 +2,11 @@ /** * - * ____ _ _ __ __ _ __ __ ____ - * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \ + * ____ _ _ __ __ _ __ __ ____ + * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \ * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) | - * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/ - * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_| + * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/ + * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_| * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by @@ -15,26 +15,26 @@ * * @author PocketMine Team * @link http://www.pocketmine.net/ - * + * * */ class DataPacketSendEvent extends ServerEvent implements CancellableEvent{ public static $handlers; public static $handlerPriority; - + private $packet; - private $player; - + private $player; + public function __construct(Player $player, RakNetDataPacket $packet){ $this->packet = $packet; $this->player = $player; } - + public function getPacket(){ return $this->packet; } - + public function getPlayer(){ return $this->player; } diff --git a/src/event/server/PacketReceiveEvent.php b/src/event/server/PacketReceiveEvent.php index cb26cbf0..60b8b354 100644 --- a/src/event/server/PacketReceiveEvent.php +++ b/src/event/server/PacketReceiveEvent.php @@ -2,11 +2,11 @@ /** * - * ____ _ _ __ __ _ __ __ ____ - * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \ + * ____ _ _ __ __ _ __ __ ____ + * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \ * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) | - * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/ - * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_| + * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/ + * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_| * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by @@ -15,21 +15,20 @@ * * @author PocketMine Team * @link http://www.pocketmine.net/ - * + * * */ class PacketReceiveEvent extends ServerEvent implements CancellableEvent{ public static $handlers; public static $handlerPriority; - + private $packet; - - + public function __construct(Packet $packet){ $this->packet = $packet; } - + public function getPacket(){ return $this->packet; } diff --git a/src/event/server/PacketSendEvent.php b/src/event/server/PacketSendEvent.php index becb15e4..cea83d3d 100644 --- a/src/event/server/PacketSendEvent.php +++ b/src/event/server/PacketSendEvent.php @@ -2,11 +2,11 @@ /** * - * ____ _ _ __ __ _ __ __ ____ - * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \ + * ____ _ _ __ __ _ __ __ ____ + * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \ * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) | - * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/ - * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_| + * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/ + * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_| * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by @@ -15,21 +15,20 @@ * * @author PocketMine Team * @link http://www.pocketmine.net/ - * + * * */ class PacketSendEvent extends ServerEvent implements CancellableEvent{ public static $handlers; public static $handlerPriority; - + private $packet; - - + public function __construct(Packet $packet){ $this->packet = $packet; } - + public function getPacket(){ return $this->packet; } diff --git a/src/functions.php b/src/functions.php index 1838b9a3..3a937596 100644 --- a/src/functions.php +++ b/src/functions.php @@ -11,7 +11,6 @@ if(!function_exists("cli_set_process_title")){ } } - function dummy(){ } @@ -69,8 +68,6 @@ function nullsafe(&$a, $null){ return $a ?? $null; } - - function require_all($path, &$count = 0){ $dir = dir($path . "/"); $dirs = []; diff --git a/src/installer/Installer.php b/src/installer/Installer.php index d732b689..fe4ec2c4 100644 --- a/src/installer/Installer.php +++ b/src/installer/Installer.php @@ -8,7 +8,7 @@ const DEFAULT_MEMORY = 128; const DEFAULT_PLAYERS = 20; const DEFAULT_GAMEMODE = SURVIVAL; - + private $lang, $config; public function __construct(){ echo "[*] NostalgiaCore set-up wizard\n"; @@ -25,8 +25,8 @@ } }while($lang == false); $this->lang = new InstallerLang($lang); - echo "[*] ".$this->lang->language_has_been_selected."\n"; - echo "[?] ".$this->lang->skip_installer." (y/N): "; + echo "[*] " . $this->lang->language_has_been_selected . "\n"; + echo "[?] " . $this->lang->skip_installer . " (y/N): "; if(strtolower($this->getInput()) === "y"){ return; } @@ -34,14 +34,14 @@ $this->welcome(); $this->generateBaseConfig(); $this->generateUserFiles(); - + $this->networkFunctions(); - + $this->endWizard(); } - + private function welcome(){ - echo $this->lang->welcome_to_pocketmine."\n"; + echo $this->lang->welcome_to_pocketmine . "\n"; echo <<lang->accept_license." (y/N): "; + echo "\n[?] " . $this->lang->accept_license . " (y/N): "; if(strtolower($this->getInput("n")) != "y"){ - echo "[!] ".$this->lang->you_have_to_accept_the_license."\n"; + echo "[!] " . $this->lang->you_have_to_accept_the_license . "\n"; sleep(5); exit(0); } - echo "[*] ".$this->lang->setting_up_server_now."\n"; - echo "[*] ".$this->lang->default_values_info."\n"; - echo "[*] ".$this->lang->server_properties."\n"; - + echo "[*] " . $this->lang->setting_up_server_now . "\n"; + echo "[*] " . $this->lang->default_values_info . "\n"; + echo "[*] " . $this->lang->server_properties . "\n"; + } - + private function generateBaseConfig(){ $config = new Config(DATA_PATH . "server.properties", CONFIG_PROPERTIES); - echo "[?] ".$this->lang->name_your_server." (".self::DEFAULT_NAME."): "; + echo "[?] " . $this->lang->name_your_server . " (" . self::DEFAULT_NAME . "): "; $config->set("server-name", $this->getInput(self::DEFAULT_NAME)); - echo "[*] ".$this->lang->port_warning."\n"; + echo "[*] " . $this->lang->port_warning . "\n"; do{ - echo "[?] ".$this->lang->server_port." (".self::DEFAULT_PORT."): "; + echo "[?] " . $this->lang->server_port . " (" . self::DEFAULT_PORT . "): "; $port = (int) $this->getInput(self::DEFAULT_PORT); if($port <= 0 or $port > 65535){ - echo "[!] ".$this->lang->invalid_port."\n"; + echo "[!] " . $this->lang->invalid_port . "\n"; } }while($port <= 0 or $port > 65535); $config->set("server-port", $port); - echo "[*] ".$this->lang->ram_warning."\n"; - echo "[?] ".$this->lang->server_ram." (".self::DEFAULT_MEMORY."): "; - $config->set("memory-limit", ((int) $this->getInput(self::DEFAULT_MEMORY))."M"); - echo "[*] ".$this->lang->gamemode_info."\n"; + echo "[*] " . $this->lang->ram_warning . "\n"; + echo "[?] " . $this->lang->server_ram . " (" . self::DEFAULT_MEMORY . "): "; + $config->set("memory-limit", ((int) $this->getInput(self::DEFAULT_MEMORY)) . "M"); + echo "[*] " . $this->lang->gamemode_info . "\n"; do{ - echo "[?] ".$this->lang->default_gamemode.": (".self::DEFAULT_GAMEMODE."): "; + echo "[?] " . $this->lang->default_gamemode . ": (" . self::DEFAULT_GAMEMODE . "): "; $gamemode = (int) $this->getInput(self::DEFAULT_GAMEMODE); }while($gamemode < 0 or $gamemode > 3); $config->set("gamemode", $gamemode); - echo "[?] ".$this->lang->max_players." (".self::DEFAULT_PLAYERS."): "; + echo "[?] " . $this->lang->max_players . " (" . self::DEFAULT_PLAYERS . "): "; $config->set("max-players", (int) $this->getInput(self::DEFAULT_PLAYERS)); - echo "[*] ".$this->lang->spawn_protection_info."\n"; - echo "[?] ".$this->lang->spawn_protection." (Y/n): "; + echo "[*] " . $this->lang->spawn_protection_info . "\n"; + echo "[?] " . $this->lang->spawn_protection . " (Y/n): "; if(strtolower($this->getInput("y")) == "n"){ $config->set("spawn-protection", -1); }else{ @@ -95,82 +95,80 @@ LICENSE; } $config->save(); } - + private function generateUserFiles(){ - echo "[*] ".$this->lang->op_info."\n"; - echo "[?] ".$this->lang->op_who.": "; + echo "[*] " . $this->lang->op_info . "\n"; + echo "[?] " . $this->lang->op_who . ": "; $op = strtolower($this->getInput("")); if($op === ""){ - echo "[!] ".$this->lang->op_warning."\n"; + echo "[!] " . $this->lang->op_warning . "\n"; }else{ - $ops = new Config(DATA_PATH."ops.txt", CONFIG_LIST); + $ops = new Config(DATA_PATH . "ops.txt", CONFIG_LIST); $ops->set($op, true); $ops->save(); } - echo "[*] ".$this->lang->whitelist_info."\n"; - echo "[?] ".$this->lang->whitelist_enable." (y/N): "; + echo "[*] " . $this->lang->whitelist_info . "\n"; + echo "[?] " . $this->lang->whitelist_enable . " (y/N): "; $config = new Config(DATA_PATH . "server.properties", CONFIG_PROPERTIES); if(strtolower($this->getInput("n")) === "y"){ - echo "[!] ".$this->lang->whitelist_warning."\n"; + echo "[!] " . $this->lang->whitelist_warning . "\n"; $config->set("white-list", true); }else{ $config->set("white-list", false); } $config->save(); } - + private function networkFunctions(){ $config = new Config(DATA_PATH . "server.properties", CONFIG_PROPERTIES); - echo "[!] ".$this->lang->query_warning1."\n"; - echo "[!] ".$this->lang->query_warning2."\n"; - echo "[?] ".$this->lang->query_disable." (y/N): "; + echo "[!] " . $this->lang->query_warning1 . "\n"; + echo "[!] " . $this->lang->query_warning2 . "\n"; + echo "[?] " . $this->lang->query_disable . " (y/N): "; if(strtolower($this->getInput("n")) === "y"){ $config->set("enable-query", false); }else{ $config->set("enable-query", true); } - - echo "[*] ".$this->lang->rcon_info."\n"; - echo "[?] ".$this->lang->rcon_enable." (y/N): "; + + echo "[*] " . $this->lang->rcon_info . "\n"; + echo "[?] " . $this->lang->rcon_enable . " (y/N): "; if(strtolower($this->getInput("n")) === "y"){ $config->set("enable-rcon", true); $password = substr(base64_encode(Utils::getRandomBytes(20, false)), 3, 10); $config->set("rcon.password", $password); - echo "[*] ".$this->lang->rcon_password.": ".$password."\n"; + echo "[*] " . $this->lang->rcon_password . ": " . $password . "\n"; }else{ $config->set("enable-rcon", false); } $config->save(); - - - echo "[*] ".$this->lang->ip_get."\n"; - + + echo "[*] " . $this->lang->ip_get . "\n"; + $externalIP = Utils::getIP(); $internalIP = gethostbyname(trim(`hostname`)); - - echo "[!] ".$this->lang->get("ip_warning", array("{{EXTERNAL_IP}}", "{{INTERNAL_IP}}"), array($externalIP, $internalIP))."\n"; - echo "[!] ".$this->lang->ip_confirm; + + echo "[!] " . $this->lang->get("ip_warning", ["{{EXTERNAL_IP}}", "{{INTERNAL_IP}}"], [$externalIP, $internalIP]) . "\n"; + echo "[!] " . $this->lang->ip_confirm; $this->getInput(); } - + private function endWizard(){ - echo "[*] ".$this->lang->you_have_finished."\n"; - echo "[*] ".$this->lang->pocketmine_plugins."\n"; - echo "[*] ".$this->lang->pocketmine_will_start."\n\n\n"; + echo "[*] " . $this->lang->you_have_finished . "\n"; + echo "[*] " . $this->lang->pocketmine_plugins . "\n"; + echo "[*] " . $this->lang->pocketmine_will_start . "\n\n\n"; sleep(4); } - + private function getInput($default = ""){ $input = trim(fgets(STDIN)); return $input === "" ? $default:$input; } - } class InstallerLang{ - public static $languages = array( + public static $languages = [ "ru" => "Pyccĸий", "en" => "English", "es" => "Español", @@ -180,7 +178,7 @@ LICENSE; //"vi" => "Tiếng Việt", "ko" => "한국어", "fr" => "Français", - "it" => "Italiano", + "it" => "Italiano", //"lv" => "Latviešu", "nl" => "Nederlands", //"pt" => "Português", @@ -189,18 +187,18 @@ LICENSE; "tr" => "Türkçe", "uk" => "Український" //"et" => "Eesti", - ); - private $texts = array(); + ]; + private $texts = []; private $lang; private $langfile; public function __construct($lang = ""){ - if(file_exists(FILE_PATH."src/lang/Installer/".$lang.".ini")){ + if(file_exists(FILE_PATH . "src/lang/Installer/" . $lang . ".ini")){ $this->lang = $lang; - $this->langfile = FILE_PATH."src/lang/Installer/".$lang.".ini"; + $this->langfile = FILE_PATH . "src/lang/Installer/" . $lang . ".ini"; }else{ - $l = glob(FILE_PATH."src/lang/Installer/".$lang."_*.ini"); + $l = glob(FILE_PATH . "src/lang/Installer/" . $lang . "_*.ini"); if(count($l) > 0){ - $files = array(); + $files = []; foreach($l as $file){ $files[$file] = filesize($file); } @@ -209,38 +207,38 @@ LICENSE; $l = key($files); $l = substr($l, strrpos($l, "/") + 1, -4); $this->lang = isset(self::$languages[$l]) ? $l:$lang; - $this->langfile = FILE_PATH."src/lang/Installer/".$l.".ini"; + $this->langfile = FILE_PATH . "src/lang/Installer/" . $l . ".ini"; }else{ $this->lang = "en"; - $this->langfile = FILE_PATH."src/lang/Installer/en.ini"; + $this->langfile = FILE_PATH . "src/lang/Installer/en.ini"; } } - - $this->loadLang(FILE_PATH."src/lang/Installer/en.ini", "en"); + + $this->loadLang(FILE_PATH . "src/lang/Installer/en.ini", "en"); if($this->lang !== "en"){ $this->loadLang($this->langfile, $this->lang); } } - + public function getLang(){ return ($this->lang); } - + public function loadLang($langfile, $lang = "en"){ - $this->texts[$lang] = array(); - $texts = explode("\n", str_replace(array("\r", "\/\/"), array("", "//"), file_get_contents($langfile))); + $this->texts[$lang] = []; + $texts = explode("\n", str_replace(["\r", "\/\/"], ["", "//"], file_get_contents($langfile))); foreach($texts as $line){ $line = trim($line); if($line === ""){ continue; } $line = explode("=", $line); - $this->texts[$lang][array_shift($line)] = str_replace(array("\\n", "\\N",), "\n", implode("=", $line)); + $this->texts[$lang][array_shift($line)] = str_replace(["\\n", "\\N",], "\n", implode("=", $line)); } } - - public function get($name, $search = array(), $replace = array()){ + + public function get($name, $search = [], $replace = []){ if(!isset($this->texts[$this->lang][$name])){ if($this->lang !== "en" and isset($this->texts["en"][$name])){ return $this->texts["en"][$name]; @@ -253,10 +251,10 @@ LICENSE; return $this->texts[$this->lang][$name]; } } - + public function __get($name){ return $this->get($name); } - } + } /***REM_END***/ diff --git a/src/material/Block.php b/src/material/Block.php index 459c1e91..e83a913f 100755 --- a/src/material/Block.php +++ b/src/material/Block.php @@ -1,138 +1,138 @@ "AirBlock", - STONE => "StoneBlock", - GRASS => "GrassBlock", - DIRT => "DirtBlock", - COBBLESTONE => "CobblestoneBlock", - PLANKS => "PlanksBlock", - SAPLING => "SaplingBlock", - BEDROCK => "BedrockBlock", - WATER => "WaterBlock", - STILL_WATER => "StillWaterBlock", - LAVA => "LavaBlock", - STILL_LAVA => "StillLavaBlock", - SAND => "SandBlock", - GRAVEL => "GravelBlock", - GOLD_ORE => "GoldOreBlock", - IRON_ORE => "IronOreBlock", - COAL_ORE => "CoalOreBlock", - WOOD => "WoodBlock", - LEAVES => "LeavesBlock", - SPONGE => "SpongeBlock", - GLASS => "GlassBlock", - LAPIS_ORE => "LapisOreBlock", - LAPIS_BLOCK => "LapisBlock", - SANDSTONE => "SandstoneBlock", - BED_BLOCK => "BedBlock", - POWERED_RAIL => "PoweredRailBlock", - COBWEB => "CobwebBlock", - TALL_GRASS => "TallGrassBlock", - DEAD_BUSH => "DeadBushBlock", - WOOL => "WoolBlock", - DANDELION => "DandelionBlock", - CYAN_FLOWER => "CyanFlowerBlock", - BROWN_MUSHROOM => "BrownMushroomBlock", - RED_MUSHROOM => "RedMushRoomBlock", - GOLD_BLOCK => "GoldBlock", - IRON_BLOCK => "IronBlock", - DOUBLE_SLAB => "DoubleSlabBlock", - SLAB => "SlabBlock", - BRICKS_BLOCK => "BricksBlock", - TNT => "TNTBlock", - BOOKSHELF => "BookshelfBlock", - MOSS_STONE => "MossStoneBlock", - OBSIDIAN => "ObsidianBlock", - TORCH => "TorchBlock", - FIRE => "FireBlock", + public static $class = [ + AIR => "AirBlock", + STONE => "StoneBlock", + GRASS => "GrassBlock", + DIRT => "DirtBlock", + COBBLESTONE => "CobblestoneBlock", + PLANKS => "PlanksBlock", + SAPLING => "SaplingBlock", + BEDROCK => "BedrockBlock", + WATER => "WaterBlock", + STILL_WATER => "StillWaterBlock", + LAVA => "LavaBlock", + STILL_LAVA => "StillLavaBlock", + SAND => "SandBlock", + GRAVEL => "GravelBlock", + GOLD_ORE => "GoldOreBlock", + IRON_ORE => "IronOreBlock", + COAL_ORE => "CoalOreBlock", + WOOD => "WoodBlock", + LEAVES => "LeavesBlock", + SPONGE => "SpongeBlock", + GLASS => "GlassBlock", + LAPIS_ORE => "LapisOreBlock", + LAPIS_BLOCK => "LapisBlock", + SANDSTONE => "SandstoneBlock", + BED_BLOCK => "BedBlock", + POWERED_RAIL => "PoweredRailBlock", + COBWEB => "CobwebBlock", + TALL_GRASS => "TallGrassBlock", + DEAD_BUSH => "DeadBushBlock", + WOOL => "WoolBlock", + DANDELION => "DandelionBlock", + CYAN_FLOWER => "CyanFlowerBlock", + BROWN_MUSHROOM => "BrownMushroomBlock", + RED_MUSHROOM => "RedMushRoomBlock", + GOLD_BLOCK => "GoldBlock", + IRON_BLOCK => "IronBlock", + DOUBLE_SLAB => "DoubleSlabBlock", + SLAB => "SlabBlock", + BRICKS_BLOCK => "BricksBlock", + TNT => "TNTBlock", + BOOKSHELF => "BookshelfBlock", + MOSS_STONE => "MossStoneBlock", + OBSIDIAN => "ObsidianBlock", + TORCH => "TorchBlock", + FIRE => "FireBlock", - WOOD_STAIRS => "WoodStairsBlock", - CHEST => "ChestBlock", + WOOD_STAIRS => "WoodStairsBlock", + CHEST => "ChestBlock", - DIAMOND_ORE => "DiamondOreBlock", - DIAMOND_BLOCK => "DiamondBlock", - WORKBENCH => "WorkbenchBlock", - WHEAT_BLOCK => "WheatBlock", - FARMLAND => "FarmlandBlock", - FURNACE => "FurnaceBlock", - BURNING_FURNACE => "BurningFurnaceBlock", - SIGN_POST => "SignPostBlock", - WOOD_DOOR_BLOCK => "WoodDoorBlock", - LADDER => "LadderBlock", - RAIL => "RailBlock", - COBBLESTONE_STAIRS => "CobblestoneStairsBlock", - WALL_SIGN => "WallSignBlock", + DIAMOND_ORE => "DiamondOreBlock", + DIAMOND_BLOCK => "DiamondBlock", + WORKBENCH => "WorkbenchBlock", + WHEAT_BLOCK => "WheatBlock", + FARMLAND => "FarmlandBlock", + FURNACE => "FurnaceBlock", + BURNING_FURNACE => "BurningFurnaceBlock", + SIGN_POST => "SignPostBlock", + WOOD_DOOR_BLOCK => "WoodDoorBlock", + LADDER => "LadderBlock", + RAIL => "RailBlock", + COBBLESTONE_STAIRS => "CobblestoneStairsBlock", + WALL_SIGN => "WallSignBlock", - IRON_DOOR_BLOCK => "IronDoorBlock", - REDSTONE_ORE => "RedstoneOreBlock", - GLOWING_REDSTONE_ORE => "GlowingRedstoneOreBlock", + IRON_DOOR_BLOCK => "IronDoorBlock", + REDSTONE_ORE => "RedstoneOreBlock", + GLOWING_REDSTONE_ORE => "GlowingRedstoneOreBlock", - SNOW_LAYER => "SnowLayerBlock", - ICE => "IceBlock", - SNOW_BLOCK => "SnowBlock", - CACTUS => "CactusBlock", - CLAY_BLOCK => "ClayBlock", - SUGARCANE_BLOCK => "SugarcaneBlock", + SNOW_LAYER => "SnowLayerBlock", + ICE => "IceBlock", + SNOW_BLOCK => "SnowBlock", + CACTUS => "CactusBlock", + CLAY_BLOCK => "ClayBlock", + SUGARCANE_BLOCK => "SugarcaneBlock", - FENCE => "FenceBlock", - PUMPKIN => "PumpkinBlock", - NETHERRACK => "NetherrackBlock", - SOUL_SAND => "SoulSandBlock", - GLOWSTONE_BLOCK => "GlowstoneBlock", + FENCE => "FenceBlock", + PUMPKIN => "PumpkinBlock", + NETHERRACK => "NetherrackBlock", + SOUL_SAND => "SoulSandBlock", + GLOWSTONE_BLOCK => "GlowstoneBlock", - LIT_PUMPKIN => "LitPumpkinBlock", - INVISIBLE_BEDROCK => "InvisibleBedrockBlock", - - CAKE_BLOCK => "CakeBlock", - - TRAPDOOR => "TrapdoorBlock", + LIT_PUMPKIN => "LitPumpkinBlock", + INVISIBLE_BEDROCK => "InvisibleBedrockBlock", - STONE_BRICKS => "StoneBricksBlock", + CAKE_BLOCK => "CakeBlock", - IRON_BARS => "IronBarsBlock", - GLASS_PANE => "GlassPaneBlock", - MELON_BLOCK => "MelonBlock", - PUMPKIN_STEM => "PumpkinStemBlock", - MELON_STEM => "MelonStemBlock", + TRAPDOOR => "TrapdoorBlock", - FENCE_GATE => "FenceGateBlock", - BRICK_STAIRS => "BrickStairsBlock", - STONE_BRICK_STAIRS => "StoneBrickStairsBlock", + STONE_BRICKS => "StoneBricksBlock", - NETHER_BRICKS => "NetherBricksBlock", + IRON_BARS => "IronBarsBlock", + GLASS_PANE => "GlassPaneBlock", + MELON_BLOCK => "MelonBlock", + PUMPKIN_STEM => "PumpkinStemBlock", + MELON_STEM => "MelonStemBlock", - NETHER_BRICKS_STAIRS => "NetherBricksStairsBlock", + FENCE_GATE => "FenceGateBlock", + BRICK_STAIRS => "BrickStairsBlock", + STONE_BRICK_STAIRS => "StoneBrickStairsBlock", - SANDSTONE_STAIRS => "SandstoneStairsBlock", - - SPRUCE_WOOD_STAIRS => "SpruceWoodStairsBlock", - BIRCH_WOOD_STAIRS => "BirchWoodStairsBlock", - JUNGLE_WOOD_STAIRS => "JungleWoodStairsBlock", - STONE_WALL => "StoneWallBlock", - - CARROT_BLOCK => "CarrotBlock", - POTATO_BLOCK => "PotatoBlock", + NETHER_BRICKS => "NetherBricksBlock", - QUARTZ_BLOCK => "QuartzBlock", - QUARTZ_STAIRS => "QuartzStairsBlock", - DOUBLE_WOOD_SLAB => "DoubleWoodSlabBlock", - WOOD_SLAB => "WoodSlabBlock", - - HAY_BALE => "HayBaleBlock", - CARPET => "CarpetBlock", - - COAL_BLOCK => "CoalBlock", - - BEETROOT_BLOCK => "BeetrootBlock", - STONECUTTER => "StonecutterBlock", - GLOWING_OBSIDIAN => "GlowingObsidianBlock", - NETHER_REACTOR => "NetherReactorBlock", - INFO_UPDATE => "InfoUpdateBlock", - INFO_UPDATE2 => "InfoUpdate2Block", - RESERVED6 => "Reserved6Block", - ); + NETHER_BRICKS_STAIRS => "NetherBricksStairsBlock", + + SANDSTONE_STAIRS => "SandstoneStairsBlock", + + SPRUCE_WOOD_STAIRS => "SpruceWoodStairsBlock", + BIRCH_WOOD_STAIRS => "BirchWoodStairsBlock", + JUNGLE_WOOD_STAIRS => "JungleWoodStairsBlock", + STONE_WALL => "StoneWallBlock", + + CARROT_BLOCK => "CarrotBlock", + POTATO_BLOCK => "PotatoBlock", + + QUARTZ_BLOCK => "QuartzBlock", + QUARTZ_STAIRS => "QuartzStairsBlock", + DOUBLE_WOOD_SLAB => "DoubleWoodSlabBlock", + WOOD_SLAB => "WoodSlabBlock", + + HAY_BALE => "HayBaleBlock", + CARPET => "CarpetBlock", + + COAL_BLOCK => "CoalBlock", + + BEETROOT_BLOCK => "BeetrootBlock", + STONECUTTER => "StonecutterBlock", + GLOWING_OBSIDIAN => "GlowingObsidianBlock", + NETHER_REACTOR => "NetherReactorBlock", + INFO_UPDATE => "InfoUpdateBlock", + INFO_UPDATE2 => "InfoUpdate2Block", + RESERVED6 => "Reserved6Block", + ]; public $id; public $meta; public $name; @@ -155,50 +155,50 @@ abstract class Block extends Position{ public $z = 0; public $slipperiness; public static function interact(Level $level, $x, $y, $z, Player $player){} - + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){} - + public static function getAABB(Level $level, $x, $y, $z){ return StaticBlock::getAABB(static::$blockID, $x, $y, $z); } - + public static function containsX($id, $v){ return !($v == null) && $v->y >= StaticBlock::$minYs[$id] && $v->y <= StaticBlock::$maxYs[$id] && $v->z >= StaticBlock::$minZs[$id] && $v->z <= StaticBlock::$maxZs[$id]; } - + public static function containsY($id, $v){ return !($v == null) && $v->x >= StaticBlock::$minXs[$id] && $v->x <= StaticBlock::$maxXs[$id] && $v->z >= StaticBlock::$minZs[$id] && $v->z <= StaticBlock::$maxZs[$id]; } - + public static function containsZ($id, $v){ return !($v == null) && $v->x >= StaticBlock::$minXs[$id] && $v->x <= StaticBlock::$maxXs[$id] && $v->y >= StaticBlock::$minYs[$id] && $v->y <= StaticBlock::$maxYs[$id]; } - + public static function updateShape(Level $level, $x, $y, $z){ - + } - + public static function clip(Level $level, $x, $y, $z, Vector3 $start, Vector3 $end){ static::updateShape($level, $x, $y, $z); $id = $level->level->getBlockID($x, $y, $z); - + $start = $start->subtract($x, $y, $z); $end = $end->subtract($x, $y, $z); - + $v7 = $start->clipX($end, StaticBlock::$minXs[$id]); $v8 = $start->clipX($end, StaticBlock::$maxXs[$id]); $v9 = $start->clipY($end, StaticBlock::$minYs[$id]); $v10 = $start->clipY($end, StaticBlock::$maxYs[$id]); $v11 = $start->clipZ($end, StaticBlock::$minZs[$id]); $v12 = $start->clipZ($end, StaticBlock::$maxZs[$id]); - + if(!self::containsX($id, $v7)) $v7 = null; if(!self::containsX($id, $v8)) $v8 = null; if(!self::containsY($id, $v9)) $v9 = null; if(!self::containsY($id, $v10)) $v10 = null; if(!self::containsZ($id, $v11)) $v11 = null; if(!self::containsZ($id, $v12)) $v12 = null; - + $v13 = null; if($v7 != null && ($v13 == null || $start->distanceSquared($v7) < $start->distanceSquared($v13))) $v13 = $v7; if($v8 != null && ($v13 == null || $start->distanceSquared($v8) < $start->distanceSquared($v13))) $v13 = $v8; @@ -206,9 +206,9 @@ abstract class Block extends Position{ if($v10 != null && ($v13 == null || $start->distanceSquared($v10) < $start->distanceSquared($v13))) $v13 = $v10; if($v11 != null && ($v13 == null || $start->distanceSquared($v11) < $start->distanceSquared($v13))) $v13 = $v11; if($v12 != null && ($v13 == null || $start->distanceSquared($v12) < $start->distanceSquared($v13))) $v13 = $v12; - + if($v13 == null) return null; - + $v14 = -1; if($v13 == $v7) $v14 = 4; if($v13 == $v8) $v14 = 5; @@ -216,10 +216,10 @@ abstract class Block extends Position{ if($v13 == $v10) $v14 = 1; if($v13 == $v11) $v14 = 2; if($v13 == $v12) $v14 = 3; - + return MovingObjectPosition::fromBlock($x, $y, $z, $v14, $v13->add($x, $y, $z)); } - + public static function onPlace(Level $level, $x, $y, $z){} public static function addVelocityToEntity(Level $level, $x, $y, $z, Entity $entity, Vector3 $velocityVector){} public static function onRandomTick(Level $level, $x, $y, $z){} @@ -229,8 +229,7 @@ abstract class Block extends Position{ return [static::getAABB($level, $x, $y, $z)]; } public static function onEntityCollidedWithBlock(Level $level, $x, $y, $z, Entity $entity){} - - + public function __construct($id, $meta = 0, $name = "Unknown"){ $this->id = (int) $id; $this->meta = (int) $meta; @@ -240,15 +239,15 @@ abstract class Block extends Position{ $this->slipperiness = 0.6; $this->boundingBox = new AxisAlignedBB($this->x, $this->y, $this->z, $this->x + 1, $this->y + 1, $this->z + 1); } - + final public function getHardness(){ return ($this->hardness); } - + final public function getName(){ return $this->name; } - + final public function getID(){ return $this->id; } @@ -258,7 +257,7 @@ abstract class Block extends Position{ final public function getMetadata(){ return $this->meta & 0x0F; } - + final public function position(Position $v){ $this->level = $v->level; $this->x = (int) $v->x; @@ -266,24 +265,24 @@ abstract class Block extends Position{ $this->z = (int) $v->z; $this->boundingBox->setBounds($this->x, $this->y, $this->z, $this->x + 1, $this->y + 1, $this->z + 1); } - + public function getDrops(Item $item, Player $player){ if(!isset(Block::$class[$this->id])){ //Unknown blocks - return array(); + return []; }else{ - return array( - array($this->id, $this->meta, 1), - ); + return [ + [$this->id, $this->meta, 1], + ]; } } - + public function getBreakTime(Item $item, Player $player){ if(($player->gamemode & 0x01) === 0x01){ return 0.15; } return $this->breakTime; } - + public function getSide($side, $step = 1){ $v = parent::getSide($side, $step); if($this->level instanceof Level){ @@ -291,17 +290,17 @@ abstract class Block extends Position{ } return $v; } - + final public function __toString(){ - return "Block ". $this->name ." (".$this->id.":".$this->meta.")"; + return "Block " . $this->name . " (" . $this->id . ":" . $this->meta . ")"; } - + abstract function isBreakable(Item $item, Player $player); - + abstract function onBreak(Item $item, Player $player); - + abstract function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz); - + abstract function onActivate(Item $item, Player $player); } diff --git a/src/material/Item.php b/src/material/Item.php index b7997318..91aaf975 100755 --- a/src/material/Item.php +++ b/src/material/Item.php @@ -5,12 +5,12 @@ class Item{ const TOOL_PICKAXE = 1; const TOOL_AXE = 2; const TOOL_SHOVEL = 3; - const TOOL_HOE = 4; - + const TOOL_HOE = 4; + const DEF_DAMAGE = 1; - - public static $class = array( - + + public static $class = [ + //armor LEATHER_CAP => "LeatherCapItem", LEATHER_TUNIC => "LeatherTunicItem", @@ -32,7 +32,7 @@ class Item{ GOLDEN_CHESTPLATE => "GoldenChestplateItem", GOLDEN_LEGGINGS => "GoldenLeggingsItem", GOLDEN_BOOTS => "GoldenBootsItem", - + //food APPLE => "AppleItem", MUSHROOM_STEW => "MushroomStewItem", @@ -51,7 +51,7 @@ class Item{ PUMPKIN_PIE => "PumpkinPieItem", BEETROOT => "BeetrootItem", BEETROOT_SOUP => "BeetrootSoupItem", - + //generic ARROW => "ArrowItem", COAL => "CoalItem", @@ -95,7 +95,7 @@ class Item{ QUARTZ => "QuartzItem", CAMERA => "CameraItem", BEETROOT_SEEDS => "BeetrootSeedsItem", - + //tool IRON_SHOVEL => "IronShovelItem", IRON_PICKAXE => "IronPickaxeItem", @@ -127,21 +127,21 @@ class Item{ COMPASS => "CompassItem", CLOCK => "ClockItem", SHEARS => "ShearsItem", - - ); + + ]; public $block; public $id; public $meta; public $count; /** - * @var int + * @var int * Max stack size of the item. Use Item::getMaxStackSize to get stack size for specific item. */ public $maxStackSize = 64; public $durability = 0; public $name; public $isActivable = false; - + public function __construct($id, $meta = 0, $count = 1, $name = "Unknown"){ $this->id = (int) $id; $this->meta = (int) $meta; @@ -155,19 +155,19 @@ class Item{ $this->maxStackSize = 1; } } - + public function isPickaxe(){ return false; } - + public function getName(){ return $this->name; } - + public function isPlaceable(){ return (($this->block instanceof Block) and $this->block->isPlaceable === true); } - + public function getBlock(){ if($this->block instanceof Block){ return $this->block; @@ -175,23 +175,23 @@ class Item{ return BlockAPI::get(AIR); } } - + public function getID(){ return $this->id; } - + public function getMetadata(){ return $this->meta; - } - + } + public function isArmor(){ return false; } - + public function getMaxStackSize(){ return $this->maxStackSize; } - + public function getFuelTime(){ if(!isset(FuelData::$duration[$this->id])){ return false; @@ -201,24 +201,24 @@ class Item{ } return false; } - + public function getSmeltItem(){ if(!isset(SmeltingData::$product[$this->id])){ return false; } - + if(isset(SmeltingData::$product[$this->id][0]) and !is_array(SmeltingData::$product[$this->id][0])){ return BlockAPI::getItem(SmeltingData::$product[$this->id][0], SmeltingData::$product[$this->id][1]); } - + if(!isset(SmeltingData::$product[$this->id][$this->meta])){ return false; } - + return BlockAPI::getItem(SmeltingData::$product[$this->id][$this->meta][0], SmeltingData::$product[$this->id][$this->meta][1]); - + } - + public function useOn($object, $force = false){ if($force){ if(($object instanceof Entity) and !$this->isSword()){ @@ -230,16 +230,16 @@ class Item{ } return false; } - + public function isTool(){ return false; } - + public function getMaxDurability(){ if(!$this->isTool() and $this->id !== BOW){ return false; } - + $levels = [ //TODO rewrite(item usage too) 2 => 33, //GOLD 1 => 60, //WOODEN @@ -256,14 +256,14 @@ class Item{ } return $levels[$type]; } - + public function getLevel(){ return false; } - + //TODO remove? public function getPickaxeLevel(){ //Returns false or level of the pickaxe - return match ($this->id) { + return match ($this->id) { WOODEN_PICKAXE => 1, GOLDEN_PICKAXE => 2, STONE_PICKAXE => 3, @@ -272,7 +272,7 @@ class Item{ default => false, }; } - + public function isAxe(){ return false; } @@ -280,11 +280,11 @@ class Item{ public function isSword(){ return false; } - + public function isShovel(){ return false; } - + public function isHoe(){ return false; } @@ -292,23 +292,23 @@ class Item{ public function isShears(){ return ($this->id === SHEARS); } - + public function __toString(){ - return "Item ". $this->name ." (".$this->id.":".$this->meta.")"; + return "Item " . $this->name . " (" . $this->id . ":" . $this->meta . ")"; } - + public function getDamageAgainstOf($e){ return Item::DEF_DAMAGE; } - + public function getDestroySpeed(Block $block, Player $player){ return 1; } - + public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ return false; } - + public static function getFoodHeal($id){ return match($id){ APPLE => 4, @@ -328,9 +328,9 @@ class Item{ POTATO => 1, BAKED_POTATO => 6, BEETROOT => 1, - + default => 0 }; } - + } diff --git a/src/material/Material.php b/src/material/Material.php index 674ce90b..0608a472 100644 --- a/src/material/Material.php +++ b/src/material/Material.php @@ -1,4 +1,5 @@ getID()] = $b->isSolid; self::$isTransparent[$b->getID()] = $b->isTransparent; self::$isFlowable[$b->getID()] = $b->isFlowable; @@ -45,7 +45,7 @@ class StaticBlock FireBlock::setFlammabilityAndCatchingChance($b->getID(), 0, 0); self::setBlockBounds($b->getID(), 0, 0, 0, 1, 1, 1); } - + self::setBlockBounds(BED_BLOCK, 0, 0, 0, 1, 0.5625, 1); //Cake: has bounds based on world state //Chest: has bounds based on world state @@ -68,16 +68,16 @@ class StaticBlock self::setBlockBounds(SLAB, 0, 0, 0, 1, 0.5, 1); self::setBlockBounds(RAIL, 0, 0, 0, 1, 0.125, 1); self::setBlockBounds(POWERED_RAIL, 0, 0, 0, 1, 0.125, 1); - + self::setBlockBounds(DANDELION, 0.3, 0.0, 0.3, 0.7, 0.6, 0.7); //extends Bush self::setBlockBounds(ROSE, 0.3, 0.0, 0.3, 0.7, 0.6, 0.7); //extends Bush - + self::setBlockBounds(SUGARCANE_BLOCK, 0.5 - 0.375, 0, 0.5 - 0.375, 0.5 + 0.375, 1, 0.5 + 0.375); self::setBlockBounds(SNOW_LAYER, 0, 0, 0, 1, 0.125, 1); self::setBlockBounds(CARPET, 0, 0, 0, 1, 0, 1); //Stairs: based on different factors //Stone wall: based on state - + //Fire related stuff FireBlock::setFlammabilityAndCatchingChance(PLANKS, 5, 20); FireBlock::setFlammabilityAndCatchingChance(DOUBLE_WOOD_SLAB, 5, 20); @@ -101,29 +101,29 @@ class StaticBlock FireBlock::setFlammabilityAndCatchingChance(HAY_BALE, 60, 20); FireBlock::setFlammabilityAndCatchingChance(SPONGE, 30, 60); } - + public static function setBlockBounds($blockID, $minX, $minY, $minZ, $maxX, $maxY, $maxZ){ self::$maxXs[$blockID] = $maxX; self::$maxYs[$blockID] = $maxY; self::$maxZs[$blockID] = $maxZ; - + self::$minXs[$blockID] = $minX; self::$minYs[$blockID] = $minY; self::$minZs[$blockID] = $minZ; } - + public static function getAABB($id, $x, $y, $z){ return new AxisAlignedBB(self::$minXs[$id] + $x, self::$minYs[$id] + $y, self::$minZs[$id] + $z, self::$maxXs[$id] + $x, self::$maxYs[$id] + $y, self::$maxZs[$id] + $z); //TODO get bb from self::$boundingBoxes ? } - + public static function getBlock($id){ return self::$prealloc[$id] ?? self::$prealloc[0]; //accessing preallocated instances is faster Block::$class[$id] ?? Block::$class[0]; } - + public static function getHardness($id){ - return self::$hardness[$id] ?? StaticBlock::DEFAULT_HARDNESS; + return self::$hardness[$id] ?? StaticBlock::DEFAULT_HARDNESS; } - + //TODO: use static block min/max public static function getBoundingBoxForBlockCoords($id, $x, $y, $z){ /**@var AxisAlignedBB $bb*/ @@ -134,40 +134,39 @@ class StaticBlock $bb = clone $bb; return $bb->setBounds($x + $bb->minX, $y + $bb->minY, $z + $bb->minZ, $x + $bb->maxX, $y + $bb->maxY, $z + $bb->maxZ); } - - + public static function getSlipperiness($id){ return self::$slipperiness[$id] ?? StaticBlock::DEFAULT_SLIPPERINESS; } - + public static function getIsSolid($id){ return self::$isSolid[$id] ?? false; } - + public static function getIsTransparent($id){ return self::$isTransparent[$id] ?? false; } - + public static function getIsFlowable($id){ return self::$isFlowable[$id] ?? false; } - + public static function getIsReplaceable($id){ return self::$isReplaceable[$id] ?? false; } - + public static function getIsPlaceable($id){ return self::$isPlaceable[$id] ?? false; } - + public static function getHasPhysics($id){ - return self::$hasPhysics[$id] ?? false; + return self::$hasPhysics[$id] ?? false; } - + public static function getIsLiquid($id){ return self::$isLiquid[$id] ?? false; } - + public static function getIsFullBlock($id){ return self::$isFullBlock[$id] ?? false; } diff --git a/src/material/block/DoorBlock.php b/src/material/block/DoorBlock.php index abfe20a6..fdfdb0c6 100644 --- a/src/material/block/DoorBlock.php +++ b/src/material/block/DoorBlock.php @@ -86,14 +86,13 @@ class DoorBlock extends TransparentBlock{ } break; } - - + return [$aabb->offset($x, $y, $z)]; } - + public static function getCompositeData(Level $level, $x, $y, $z){ $myMeta = $level->level->getBlockDamage($x, $y, $z); - + if(($myMeta & 8) != 0){ $metaLower = $level->level->getBlockDamage($x, $y - 1, $z); $metaUpper = $myMeta; @@ -101,19 +100,18 @@ class DoorBlock extends TransparentBlock{ $metaLower = $myMeta; $metaUpper = $level->level->getBlockDamage($x, $y + 1, $z); } - + return $metaLower & 7 | (($myMeta & 8) != 0 ? 8 : 0) | (($metaUpper & 1 != 0) ? 16 : 0); } - public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ if($level->level->getBlockID($x, $y - 1, $z) == AIR){ //Replace with common break method $level->fastSetBlockUpdate($x, $y, $z, 0, 0); $id = $level->level->getBlockID($x, $y, $z); - - if($id == 64) ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(324, 0, 1)); - elseif($id == 71) ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(330, 0, 1)); - + + if($id == 64) ServerAPI::request()->api->entity->drop(new Position($x + 0.5, $y, $z + 0.5, $level), BlockAPI::getItem(324, 0, 1)); + elseif($id == 71) ServerAPI::request()->api->entity->drop(new Position($x + 0.5, $y, $z + 0.5, $level), BlockAPI::getItem(330, 0, 1)); + $top = $level->level->getBlockID($x, $y + 1, $z); if($top == IRON_DOOR_BLOCK || $top == DOOR_BLOCK){ $level->fastSetBlockUpdate($x, $y + 1, $z, 0, 0); @@ -121,12 +119,7 @@ class DoorBlock extends TransparentBlock{ } } - /** - * @param Item $item - * @param Player $player - * @param Block $block - * @param Block $target * @param integer $face * @param integer $fx * @param integer $fy @@ -142,12 +135,12 @@ class DoorBlock extends TransparentBlock{ return false; } $direction = $player->entity->getDirection(); - $face = array( + $face = [ 0 => 3, 1 => 4, 2 => 2, 3 => 5, - ); + ]; $next = $this->getSide($face[(($direction + 2) % 4)]); $next2 = $this->getSide($face[$direction]); $metaUp = 0x08; @@ -155,17 +148,15 @@ class DoorBlock extends TransparentBlock{ $metaUp |= 0x01; } $this->level->setBlock($blockUp, BlockAPI::get($this->id, $metaUp), true, false, true); //Top - + $this->meta = $direction & 0x03; $this->level->setBlock($block, $this, true, false, true); //Bottom - return true; + return true; } return false; } /** - * @param Item $item - * @param Player $player * * @return boolean */ @@ -186,8 +177,6 @@ class DoorBlock extends TransparentBlock{ } /** - * @param Item $item - * @param Player $player * * @return boolean */ diff --git a/src/material/block/FallableBlock.php b/src/material/block/FallableBlock.php index ca8113db..32a0051f 100644 --- a/src/material/block/FallableBlock.php +++ b/src/material/block/FallableBlock.php @@ -12,10 +12,6 @@ class FallableBlock extends SolidBlock{ } /** - * @param Item $item - * @param Player $player - * @param Block $block - * @param Block $target * @param int $face * @param int $fx * @param int $fy diff --git a/src/material/block/GenericBlock.php b/src/material/block/GenericBlock.php index e0780dd1..cd707a57 100644 --- a/src/material/block/GenericBlock.php +++ b/src/material/block/GenericBlock.php @@ -1,6 +1,5 @@ level->getBlock($x, $y, $z); $down = $level->level->getBlockID($x, $y - 1, $z); if($down == AIR || StaticBlock::getIsLiquid($down)){ - $data = array( + $data = [ "x" => $x + 0.5, "y" => $y - 0.5, "z" => $z + 0.5, "Tile" => $id, - ); + ]; $server = ServerAPI::request(); $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); $e = $server->api->entity->add($level, ENTITY_FALLING, FALLING_SAND, $data); $server->api->entity->spawnToAll($e); } } - + /** - * @param integer $type * * @return boolean */ @@ -86,8 +76,6 @@ class GenericBlock extends Block{ } /** - * @param Item $item - * @param Player $player * * @return boolean */ diff --git a/src/material/block/LiquidBlock.php b/src/material/block/LiquidBlock.php index feafe045..99367580 100644 --- a/src/material/block/LiquidBlock.php +++ b/src/material/block/LiquidBlock.php @@ -21,14 +21,14 @@ class LiquidBlock extends TransparentBlock{ return match(static::$blockID){ WATER, STILL_WATER => $id == WATER || $id == STILL_WATER ? $meta : -1, LAVA, STILL_LAVA => $id == LAVA || $id == STILL_LAVA ? $meta : -1, - default=> -1 + default => -1 }; } - + public static function onPlace(Level $level, $x, $y, $z){ static::updateLiquid($level, $x, $y, $z); } - + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ static::updateLiquid($level, $x, $y, $z); } @@ -40,10 +40,10 @@ class LiquidBlock extends TransparentBlock{ $f = ($meta + 1) / 9; return $f; } - + public static function getRenderedDepth(Level $level, $x, $y, $z){ [$id, $meta] = $level->level->getBlock($x, $y, $z); - + $cond = match($id){ WATER, STILL_WATER => static::$blockID == WATER || static::$blockID == STILL_WATER, LAVA, STILL_LAVA => static::$blockID == LAVA || static::$blockID == STILL_LAVA, @@ -52,23 +52,23 @@ class LiquidBlock extends TransparentBlock{ if(!$cond) return -1; return $meta >= 8 ? 0 : $meta; } - + public static function shouldRenderFace(Level $level, $x, $y, $z, $face){ $id = $level->level->getBlockID($x, $y, $z); - + if($id == ICE) return false; if((($id == WATER || $id == STILL_WATER) && (static::$blockID == WATER || static::$blockID == STILL_WATER))) return false; if((($id == LAVA || $id == STILL_LAVA) && (static::$blockID == LAVA || static::$blockID == STILL_LAVA))) return false; - + if($face == 1) return true; return StaticBlock::getIsSolid($id); //return parent::shouldRenderFace($level, $x, $y, $z, $face); } - + public static function getFlow(Level $level, $x, $y, $z){ $flowVector = new Vector3(0, 0, 0); $v6 = static::getRenderedDepth($level, $x, $y, $z); - + for($v7 = 0; $v7 < 4; ++$v7){ $v8 = $x; $v10 = $z; @@ -86,12 +86,12 @@ class LiquidBlock extends TransparentBlock{ ++$v10; break; } - + $v11 = static::getRenderedDepth($level, $v8, $y, $v10); if($v11 < 0){ if(!StaticBlock::getIsSolid($level->level->getBlockID($v8, $y, $v10))){ //TODO material.blocksMovement $v11 = static::getRenderedDepth($level, $v8, $y - 1, $v10); - + if($v11 >= 0){ $v12 = $v11 - ($v6 - 8); $flowVector->x += ($v8 - $x) * $v12; @@ -99,29 +99,29 @@ class LiquidBlock extends TransparentBlock{ $flowVector->z += ($v10 - $z) * $v12; } } - }else if($v11 >= 0){ + }elseif($v11 >= 0){ $v12 = $v11 - $v6; $flowVector->x += ($v8 - $x) * $v12; $flowVector->y += ($y - $y) * $v12; $flowVector->z += ($v10 - $z) * $v12; } } - + $meta = $level->level->getBlockDamage($x, $y, $z); - + if($meta >= 8){ $v13 = //TODO more vanilla way to do it static::shouldRenderFace($level, $x, $y, $z - 1, 2) || static::shouldRenderFace($level, $x, $y, $z + 1, 3) || static::shouldRenderFace($level, $x - 1, $y, $z, 4) || static::shouldRenderFace($level, $x + 1, $y, $z, 5) || - + static::shouldRenderFace($level, $x, $y + 1, $z - 1, 2) || static::shouldRenderFace($level, $x, $y + 1, $z + 1, 3) || static::shouldRenderFace($level, $x - 1, $y + 1, $z, 4) || static::shouldRenderFace($level, $x + 1, $y + 1, $z, 5) ; - + if($v13){ $ln = $flowVector->length(); if($ln){ @@ -141,37 +141,37 @@ class LiquidBlock extends TransparentBlock{ }else{ $flowVector->x = $flowVector->y = $flowVector->z = 0; } - + return $flowVector; } - + public static function addVelocityToEntity(Level $level, $x, $y, $z, Entity $entity, Vector3 $velocityVector){ $flow = static::getFlow($level, $x, $y, $z); $velocityVector->x += $flow->x * 0.5; $velocityVector->y += $flow->y * 0.5; $velocityVector->z += $flow->z * 0.5; } - + public static function getTickDelay(){ throw new RuntimeException("If you see this, something bad happened"); } - + public static function updateLiquid(Level $level, $x, $y, $z){ [$id, $meta] = $level->level->getBlock($x, $y, $z); if($id != LAVA && $id != STILL_LAVA) return; - + $zNeg = $level->level->getBlockID($x, $y, $z - 1); $zPos = $level->level->getBlockID($x, $y, $z + 1); $xNeg = $level->level->getBlockID($x - 1, $y, $z); $xPos = $level->level->getBlockID($x + 1, $y, $z); $yPos = $level->level->getBlockID($x, $y + 1, $z); - + if( $zNeg == WATER || $zNeg == STILL_WATER - || $zPos == WATER || $zPos == STILL_WATER - || $xNeg == WATER || $xNeg == STILL_WATER - || $xPos == WATER || $xPos == STILL_WATER - || $yPos == WATER || $yPos == STILL_WATER + || $zPos == WATER || $zPos == STILL_WATER + || $xNeg == WATER || $xNeg == STILL_WATER + || $xPos == WATER || $xPos == STILL_WATER + || $yPos == WATER || $yPos == STILL_WATER ) { if($meta){ //if($meta > 4) -> fizz & ret @@ -179,17 +179,16 @@ class LiquidBlock extends TransparentBlock{ }else{ $replacement = OBSIDIAN; } - + $level->fastSetBlockUpdate($x, $y, $z, $replacement, true); } } - - + public function getDrops(Item $item, Player $player){ - return array(); + return []; } - + public function getLiquidHeight(){ - return (($this->meta >= 8 ? 0 : $this->meta)+1) / 9; + return (($this->meta >= 8 ? 0 : $this->meta) + 1) / 9; } } \ No newline at end of file diff --git a/src/material/block/SolidBlock.php b/src/material/block/SolidBlock.php index 9fddfcee..8ca26113 100644 --- a/src/material/block/SolidBlock.php +++ b/src/material/block/SolidBlock.php @@ -11,7 +11,7 @@ class SolidBlock extends GenericBlock{ $this->isSolid = true; $this->isFullBlock = true; } - + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ return [new AxisAlignedBB($x, $y, $z, $x + 1, $y + 1, $z + 1)]; } diff --git a/src/material/block/StairBlock.php b/src/material/block/StairBlock.php index 9af56b50..f5a69373 100644 --- a/src/material/block/StairBlock.php +++ b/src/material/block/StairBlock.php @@ -15,26 +15,26 @@ class StairBlock extends TransparentBlock{ } $this->hardness = 30; } - + public static function isBlocksStairsID($id){ return $id > 0 && StaticBlock::getBlock($id) instanceof StairBlock; } - + public static function isStairsAtXYZAndAreTheirMetadataSame(Level $level, $x, $y, $z, $meta){ $block = $level->level->getBlock($x, $y, $z); return self::isBlocksStairsID($block[0]) && $block[1] == $meta; } - + public static function setBaseShape(Level $level, $x, $y, $z){ $meta = $level->level->getBlockDamage($x, $y, $z); - + if(($meta & 4) != 0){ return new AxisAlignedBB(0, 0.5, 0, 1, 1, 1); }else{ return new AxisAlignedBB(0, 0, 0, 1, 0.5, 1); } } - + const DEAD_SAPCES = [ [2, 6], [3, 7], @@ -45,72 +45,72 @@ class StairBlock extends TransparentBlock{ [0, 1], [4, 5] ]; - + public static function updateShape(Level $level, $x, $y, $z){ if(self::$complexState){ $id = $level->level->getBlockID($x, $y, $z); $cid = self::$complexStateID; - StaticBlock::setBlockBounds($id, + StaticBlock::setBlockBounds($id, 0.5 * ($cid % 2), - 0.5 * ((int)($cid / 2) % 2), - 0.5 * ((int)($cid / 4) % 2), + 0.5 * ((int) ($cid / 2) % 2), + 0.5 * ((int) ($cid / 4) % 2), 0.5 + 0.5 * ($cid % 2), - 0.5 + 0.5 * ((int)($cid / 2) % 2), - 0.5 + 0.5 * ((int)($cid / 4) % 2) + 0.5 + 0.5 * ((int) ($cid / 2) % 2), + 0.5 + 0.5 * ((int) ($cid / 4) % 2) ); }else{ StaticBlock::setBlockBounds($level->level->getBlockID($x, $y, $z), 0, 0, 0, 1, 1, 1); } } - + static $complexState = false; static $complexStateID = 0; public static function clip(Level $level, $x, $y, $z, Vector3 $start, Vector3 $end){ $v7 = []; - + $v8 = $level->level->getBlockDamage($x, $y, $z); $v9 = $v8 & 3; $v10 = ($v8 & 4) == 4; $v11 = self::DEAD_SAPCES[$v9 + ($v10 ? 4 : 0)]; self::$complexState = true; - + for($v12 = 0; $v12 < 8; ++$v12){ self::$complexStateID = $v12; - + for($v15 = 0; $v15 < /*count($v11}*/ 2; ++$v15){ $v16 = $v11[$v15]; if($v16 == $v12){ //wat } } - + $v7[$v12] = parent::clip($level, $x, $y, $z, $start, $end); } - + for($v14 = 0; $v14 < /*count($v11}*/ 2; ++$v14){ $v15 = $v11[$v14]; $v7[$v15] = null; } - + $v24 = 0; $v22 = null; - + for($v17 = 0; $v17 < count($v7); ++$v17){ $v18 = $v7[$v17]; - + if($v18 != null){ $v19 = $v18->hitVector->distanceSquared($end); - + if($v19 > $v24){ $v22 = $v18; $v24 = $v19; } } } - + return $v22; } - + public static function setStepShape(Level $level, $x, $y, $z){ $blockMeta = $level->level->getBlockDamage($x, $y, $z); $metaAnd3 = $blockMeta & 3; @@ -120,13 +120,13 @@ class StairBlock extends TransparentBlock{ $minY = 0.0; $maxY = 0.5; } - + $minX = 0; $maxX = 1; $minZ = 0; $maxZ = 0.5; $v13 = true; - + switch($metaAnd3){ case 0: $minX = 0.5; @@ -134,10 +134,10 @@ class StairBlock extends TransparentBlock{ $blockNearby = $level->level->getBlock($x + 1, $y, $z); $blockIDNearby = $blockNearby[0]; $blockMetaNearby = $blockNearby[1]; - + if(self::isBlocksStairsID($blockIDNearby) && (($blockMeta & 4) == ($blockMetaNearby & 4))){ $blockMetaNearbyAnd3 = $blockMetaNearby & 3; - + if($blockMetaNearbyAnd3 == 3 && !self::isStairsAtXYZAndAreTheirMetadataSame($level, $x, $y, $z + 1, $blockMeta)){ $maxZ = 0.5; $v13 = false; @@ -153,10 +153,10 @@ class StairBlock extends TransparentBlock{ $blockNearby = $level->level->getBlock($x - 1, $y, $z); $blockIDNearby = $blockNearby[0]; $blockMetaNearby = $blockNearby[1]; - + if(self::isBlocksStairsID($blockIDNearby) && (($blockMeta & 4) == ($blockMetaNearby & 4))){ $blockMetaNearbyAnd3 = $blockMetaNearby & 3; - + if($blockMetaNearbyAnd3 == 3 && !self::isStairsAtXYZAndAreTheirMetadataSame($level, $x, $y, $z + 1, $blockMeta)){ $maxZ = 0.5; $v13 = false; @@ -172,10 +172,10 @@ class StairBlock extends TransparentBlock{ $blockNearby = $level->level->getBlock($x, $y, $z + 1); $blockIDNearby = $blockNearby[0]; $blockMetaNearby = $blockNearby[1]; - + if(self::isBlocksStairsID($blockIDNearby) && (($blockMeta & 4) == ($blockMetaNearby & 4))){ $blockMetaNearbyAnd3 = $blockMetaNearby & 3; - + if($blockMetaNearbyAnd3 == 1 && !self::isStairsAtXYZAndAreTheirMetadataSame($level, $x + 1, $y, $z, $blockMeta)){ $maxX = 0.5; $v13 = false; @@ -191,7 +191,7 @@ class StairBlock extends TransparentBlock{ $blockMetaNearby = $blockNearby[1]; if(self::isBlocksStairsID($blockIDNearby) && (($blockMeta & 4) == ($blockMetaNearby & 4))){ $blockMetaNearbyAnd3 = $blockMetaNearby & 3; - + if($blockMetaNearbyAnd3 == 1 && !self::isStairsAtXYZAndAreTheirMetadataSame($level, $x + 1, $y, $z, $blockMeta)){ $maxX = 0.5; $v13 = false; @@ -203,34 +203,34 @@ class StairBlock extends TransparentBlock{ break; } return [new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ), $v13]; - + } - + public static function setInnerPieceShape(Level $level, $x, $y, $z){ $meta = $level->level->getBlockDamage($x, $y, $z); $metaAnd3 = $meta & 3; $minY = 0.5; $maxY = 1.0; - + if(($meta & 4) != 0){ $minY = 0.0; $maxY = 0.5; } - + $minX = 0.0; $maxX = 0.5; $minZ = 0.5; $maxZ = 1.0; $v13 = false; - + switch($metaAnd3){ case 0: $idNearby = $level->level->getBlockID($x - 1, $y, $z); $metaNearby = $level->level->getBlockDamage($x - 1, $y, $z); - + if(self::isBlocksStairsID($idNearby) && ($meta & 4) == ($metaNearby & 4)){ $metaNearbyAnd3 = $metaNearby & 3; - + if($metaNearbyAnd3 == 3 && !self::isStairsAtXYZAndAreTheirMetadataSame($level, $x, $y, $z - 1, $meta)){ $minZ = 0; $maxZ = 0.5; @@ -245,7 +245,7 @@ class StairBlock extends TransparentBlock{ case 1: $idNearby = $level->level->getBlockID($x + 1, $y, $z); $metaNearby = $level->level->getBlockDamage($x + 1, $y, $z); - + if(self::isBlocksStairsID($idNearby) && ($meta & 4) == ($metaNearby & 4)){ $minX = 0.5; $maxX = 1.0; @@ -261,17 +261,17 @@ class StairBlock extends TransparentBlock{ $v13 = true; } } - + break; case 2: $idNearby = $level->level->getBlockID($x, $y, $z - 1); $metaNearby = $level->level->getBlockDamage($x, $y, $z - 1); - + if(self::isBlocksStairsID($idNearby) && ($meta & 4) == ($metaNearby & 4)){ $minZ = 0; $maxZ = 0.5; $metaNearbyAnd3 = $metaNearby & 3; - + if($metaNearbyAnd3 == 1 && !self::isStairsAtXYZAndAreTheirMetadataSame($level, $x - 1, $y, $z, $meta)){ $v13 = true; }elseif($metaNearbyAnd3 == 0 && !self::isStairsAtXYZAndAreTheirMetadataSame($level, $x + 1, $y, $z, $meta)){ @@ -280,16 +280,16 @@ class StairBlock extends TransparentBlock{ $v13 = true; } } - + break; case 3: - + $idNearby = $level->level->getBlockID($x, $y, $z + 1); $metaNearby = $level->level->getBlockDamage($x, $y, $z + 1); - + if(self::isBlocksStairsID($idNearby) && ($meta & 4) == ($metaNearby & 4)){ $metaNearbyAnd3 = $metaNearby & 3; - + if($metaNearbyAnd3 == 1 && !self::isStairsAtXYZAndAreTheirMetadataSame($level, $x - 1, $y, $z, $meta)){ $v13 = true; }elseif($metaNearbyAnd3 == 0 && !self::isStairsAtXYZAndAreTheirMetadataSame($level, $x + 1, $y, $z, $meta)){ @@ -300,10 +300,10 @@ class StairBlock extends TransparentBlock{ } break; } - + return [new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ), $v13]; } - + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ $bbs = [self::setBaseShape($level, $x, $y, $z)->offset($x, $y, $z)]; $ret = self::setStepShape($level, $x, $y, $z); @@ -314,15 +314,11 @@ class StairBlock extends TransparentBlock{ $bbs[] = $ret2[0]->offset($x, $y, $z); } } - + return $bbs; } - + /** - * @param Item $item - * @param Player $player - * @param Block $block - * @param Block $target * @param int $face * @param int $fx * @param int $fy @@ -331,12 +327,12 @@ class StairBlock extends TransparentBlock{ * @return bool|mixed */ public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - $faces = array( + $faces = [ 0 => 0, 1 => 2, 2 => 1, 3 => 3, - ); + ]; $this->meta = $faces[$player->entity->getDirection()] & 0x03; if(($fy > 0.5 and $face !== 1) or $face === 0){ $this->meta |= 0x04; //Upside-down stairs @@ -346,18 +342,16 @@ class StairBlock extends TransparentBlock{ } /** - * @param Item $item - * @param Player $player * * @return array */ public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 1){ - return array( - array($this->id, 0, 1), - ); + return [ + [$this->id, 0, 1], + ]; }else{ - return array(); + return []; } } } diff --git a/src/material/block/TransparentBlock.php b/src/material/block/TransparentBlock.php index 16b8ffc3..678dbb1f 100644 --- a/src/material/block/TransparentBlock.php +++ b/src/material/block/TransparentBlock.php @@ -13,11 +13,11 @@ class TransparentBlock extends GenericBlock{ $this->isFlowable = false; $this->isTransparent = true; $this->isReplaceable = false; - $this->isPlaceable = true; + $this->isPlaceable = true; $this->isSolid = true; $this->boundingBox->setBounds($this->x, $this->y, $this->z, $this->x + 1, $this->y + 1, $this->z + 1); } - + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ return []; } diff --git a/src/material/block/attachable/LadderBlock.php b/src/material/block/attachable/LadderBlock.php index 82ef14b3..571d18d4 100644 --- a/src/material/block/attachable/LadderBlock.php +++ b/src/material/block/attachable/LadderBlock.php @@ -4,7 +4,7 @@ class LadderBlock extends TransparentBlock{ public static $blockID; public static function getAABB(Level $level, $x, $y, $z){ [$id, $meta] = $level->level->getBlock($x, $y, $z); - + switch($meta){ case 2: StaticBlock::setBlockBounds($id, 0.0, 0.0, 0.875, 1.0, 1.0, 1.0); @@ -18,9 +18,9 @@ class LadderBlock extends TransparentBlock{ case 5: StaticBlock::setBlockBounds($id, 0, 0.0, 0.0, 0.125, 1.0, 1.0); break; - + } - + return parent::getAABB($level, $x, $y, $z); } public function __construct($meta = 0){ @@ -43,7 +43,7 @@ class LadderBlock extends TransparentBlock{ public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ $side = $level->level->getBlockDamage($x, $y, $z); - + $attached = match($side){ 3 => $level->level->getBlockID($x, $y, $z - 1), 2 => $level->level->getBlockID($x, $y, $z + 1), @@ -51,7 +51,7 @@ class LadderBlock extends TransparentBlock{ 4 => $level->level->getBlockID($x + 1, $y, $z), default => 0 //TODO }; - + if($attached == AIR){ //Replace with common break method ServerAPI::request()->api->entity->drop(new Position($x, $y, $z, $level), BlockAPI::getItem(LADDER, 0, 1)); $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); @@ -59,8 +59,8 @@ class LadderBlock extends TransparentBlock{ } public function getDrops(Item $item, Player $player){ - return array( - array($this->id, 0, 1), - ); - } + return [ + [$this->id, 0, 1], + ]; + } } diff --git a/src/material/block/attachable/SignPostBlock.php b/src/material/block/attachable/SignPostBlock.php index a635eccc..ea7e15b2 100644 --- a/src/material/block/attachable/SignPostBlock.php +++ b/src/material/block/attachable/SignPostBlock.php @@ -17,7 +17,7 @@ class SignPostBlock extends TransparentBlock{ $this->isFullBlock = false; $this->hardness = 5; } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ if(($target->isSolid || $target->getID() === SIGN_POST || $target->getID() === WALL_SIGN) && $face !== 0){ if(!isset(self::$faces[$face])){ @@ -32,7 +32,7 @@ class SignPostBlock extends TransparentBlock{ } return false; } - + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ if($level->level->getBlockID($x, $y - 1, $z) === AIR){ //Replace with common break method ServerAPI::request()->api->entity->drop(new Position($x, $y, $z, $level), BlockAPI::getItem(SIGN, 0, 1)); @@ -40,15 +40,15 @@ class SignPostBlock extends TransparentBlock{ $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true, true); } } - + public function onBreak(Item $item, Player $player){ $this->level->setBlock($this, new AirBlock(), true, true, true); return true; } public function getDrops(Item $item, Player $player){ - return array( - array(SIGN, 0, 1), - ); - } + return [ + [SIGN, 0, 1], + ]; + } } \ No newline at end of file diff --git a/src/material/block/attachable/TorchBlock.php b/src/material/block/attachable/TorchBlock.php index 761ba9d7..69c0f5be 100644 --- a/src/material/block/attachable/TorchBlock.php +++ b/src/material/block/attachable/TorchBlock.php @@ -6,15 +6,15 @@ class TorchBlock extends FlowableBlock implements LightingBlock{ parent::__construct(TORCH, $meta, "Torch"); $this->hardness = 0; } - + public function getMaxLightValue(){ return 15; } - + public static function getAABB(Level $level, $x, $y, $z){ return null; } - + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ $side = $level->level->getBlockDamage($x, $y, $z); $attach = match($side){ @@ -22,9 +22,9 @@ class TorchBlock extends FlowableBlock implements LightingBlock{ 2 => $level->level->getBlockID($x + 1, $y, $z), 3 => $level->level->getBlockID($x, $y, $z - 1), 4 => $level->level->getBlockID($x, $y, $z + 1), - default => $level->level->getBlockID($x, $y - 1, $z) + default => $level->level->getBlockID($x, $y - 1, $z) }; - + if(StaticBlock::getIsTransparent($attach) && !($side === 0 && $attach === FENCE)){ //Replace with common break method ServerAPI::request()->api->entity->drop(new Position($x, $y, $z, $level), BlockAPI::getItem(TORCH, 0, 1)); $level->fastSetBlockUpdate($x, $y, $z, 0, 0); @@ -33,13 +33,13 @@ class TorchBlock extends FlowableBlock implements LightingBlock{ public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ if($target->isTransparent === false and $face !== 0){ - $faces = array( + $faces = [ 1 => 5, 2 => 4, 3 => 3, 4 => 2, 5 => 1, - ); + ]; $this->meta = $faces[$face]; $this->level->setBlock($block, $this, true, false, true); return true; @@ -51,8 +51,8 @@ class TorchBlock extends FlowableBlock implements LightingBlock{ return false; } public function getDrops(Item $item, Player $player){ - return array( - array($this->id, 0, 1), - ); + return [ + [$this->id, 0, 1], + ]; } } \ No newline at end of file diff --git a/src/material/block/attachable/TrapdoorBlock.php b/src/material/block/attachable/TrapdoorBlock.php index 6e932bde..48d2de84 100644 --- a/src/material/block/attachable/TrapdoorBlock.php +++ b/src/material/block/attachable/TrapdoorBlock.php @@ -17,25 +17,25 @@ class TrapdoorBlock extends TransparentBlock{ $id = $target->getID(); return $id === SLAB || $id === GLOWSTONE || $id === SLAB || $id === WOOD_SLAB || (!$target->isTransparent || $target instanceof StairBlock); } - + public static function isOpen($meta){ return ($meta >> 2) & 1; } - + public static function getAABB(Level $level, $x, $y, $z){ static::updateShape($level, $x, $y, $z); return StaticBlock::getAABB(static::$blockID, $x, $y, $z); } - + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ return [static::getAABB($level, $x, $y, $z)]; } - + public static function updateShape(Level $level, $x, $y, $z){ [$id, $meta] = $level->level->getBlock($x, $y, $z); - + StaticBlock::setBlockBounds($id, 0.0, 0.0, 0.0, 1.0, 0.1875, 1.0); - + if(static::isOpen($meta)){ $facing = $meta & 3; switch($facing){ @@ -56,24 +56,24 @@ class TrapdoorBlock extends TransparentBlock{ } } } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ if(($this->canAttachTo($target)) and $face !== 0 and $face !== 1){ - $faces = array( + $faces = [ 2 => 0, 3 => 1, 4 => 2, 5 => 3, - ); + ]; $this->meta = $faces[$face] & 0x03; $this->level->setBlock($block, $this, true, false, true); return true; } } public function getDrops(Item $item, Player $player){ - return array( - array($this->id, 0, 1), - ); + return [ + [$this->id, 0, 1], + ]; } public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ $attach = match($level->level->getBlockDamage($x, $y, $z)){ @@ -83,13 +83,13 @@ class TrapdoorBlock extends TransparentBlock{ 3, 7 => $level->level->getBlockID($x - 1, $y, $z), default => 0 }; - + if($attach == AIR){ //Replace with common break method ServerAPI::request()->api->entity->drop(new Position($x, $y, $z, $level), BlockAPI::getItem(TRAPDOOR, 0, 1)); $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); } } - + public function onActivate(Item $item, Player $player){ $this->meta ^= 0x04; $this->level->setBlock($this, $this, true, false, true); diff --git a/src/material/block/attachable/WallSignBlock.php b/src/material/block/attachable/WallSignBlock.php index 02ca1aa5..1a61c615 100644 --- a/src/material/block/attachable/WallSignBlock.php +++ b/src/material/block/attachable/WallSignBlock.php @@ -10,7 +10,7 @@ class WallSignBlock extends SignPostBlock{ TransparentBlock::__construct(WALL_SIGN, $meta, "Wall Sign"); $this->isSolid = false; } - + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ $attached = match($level->level->getBlockDamage($x, $y, $z)){ 2 => $level->level->getBlockID($x, $y, $z + 1), @@ -19,7 +19,7 @@ class WallSignBlock extends SignPostBlock{ 5 => $level->level->getBlockID($x - 1, $y, $z), default => WALL_SIGN }; - + if(!StaticBlock::getIsSolid($attached) && $attached != SIGN_POST && $attached != WALL_SIGN){ $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true, true); (ServerAPI::request())->api->entity->drop(new Position($x + 0.5, $y + 0.5, $z + 0.5, $level), BlockAPI::getItem(SIGN, 0, 1)); diff --git a/src/material/block/liquid/LavaBlock.php b/src/material/block/liquid/LavaBlock.php index 9329d736..ed80bbf1 100644 --- a/src/material/block/liquid/LavaBlock.php +++ b/src/material/block/liquid/LavaBlock.php @@ -1,4 +1,5 @@ hardness = 500; } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $ret = $this->level->setBlock($this, $this, true, false, true); return $ret; } - + public static function getTickDelay(){ return 30; } - + public function getMaxLightValue(){ return 15; } diff --git a/src/material/block/liquid/LiquidBlockDynamic.php b/src/material/block/liquid/LiquidBlockDynamic.php index c4028f30..5d35973b 100644 --- a/src/material/block/liquid/LiquidBlockDynamic.php +++ b/src/material/block/liquid/LiquidBlockDynamic.php @@ -4,55 +4,55 @@ class LiquidBlockDynamic extends LiquidBlock{ public function __construct($id, $meta = 0, $name = "Unknown"){ parent::__construct($id, $meta, $name); } - + public static $blockID = 0; public static $sourcesAround = 0; public static $spread = [0, 0, 0, 0]; - + public static function getHighest(Level $level, $x, $y, $z, $highest){ $depth = static::getDepth($level, $x, $y, $z); if($depth < 0) return $highest; if($depth == 0) ++static::$sourcesAround; if($depth >= 8) $depth = 0; - + return $highest >= 0 && $depth >= $highest ? $highest : $depth; } - + public static function setStatic(Level $level, $x, $y, $z){ $meta = $level->level->getBlockDamage($x, $y, $z); - + $level->fastSetBlockUpdate($x, $y, $z, static::$blockID + 1, $meta); } - + public static function isWaterBlocking(Level $level, $x, $y, $z){ $id = $level->level->getBlockID($x, $y, $z); - + if($id == AIR) return false; - + if($id == DOOR_BLOCK || $id == SIGN_POST || $id == WALL_SIGN || $id == LADDER || $id == SUGARCANE_BLOCK) return true; - + if($id == CARPET || $id == SNOW_LAYER || $id == RAIL || $id == POWERED_RAIL) return false; //TODO Tile::getThickness() > 0 - + //TODO materials //if(!StaticBlock::getIsFlowable($id)) return true; return StaticBlock::getIsSolid($id); } - + public static function canSpreadTo(Level $level, $x, $y, $z){ $id = $level->level->getBlockID($x, $y, $z); if($id == LAVA || $id == STILL_LAVA) return false; if((static::$blockID == WATER) && ($id == WATER || $id == STILL_WATER)) return false; - + return static::isWaterBlocking($level, $x, $y, $z) ^ 1; } - + public static function getSpread(Level $level, $x, $y, $z){ for($i = 0; $i < 4; ++$i){ static::$spread[$i] = 1000; $xs = $x; $ys = $y; $zs = $z; - + switch($i){ case 0: --$xs; @@ -67,13 +67,13 @@ class LiquidBlockDynamic extends LiquidBlock{ ++$zs; break; } - + if(static::isWaterBlocking($level, $xs, $ys, $zs)) continue; [$id, $meta] = $level->level->getBlock($xs, $ys, $zs); if(((static::$blockID == WATER && ($id == WATER || $id == STILL_WATER)) || (static::$blockID == LAVA && ($id == LAVA || $id == STILL_LAVA))) && $meta == 0){ continue; } - + if(!static::isWaterBlocking($level, $xs, $ys - 1, $zs)) static::$spread[$i] = 0; else static::$spread[$i] = static::getSlopeDistance($level, $xs, $ys, $zs, 1, $i); } @@ -85,18 +85,18 @@ class LiquidBlockDynamic extends LiquidBlock{ for($i = 0; $i < 4; ++$i) $ba[$i] = ($i1 == static::$spread[$i]); return $ba; } - + public static function getSlopeDistance(Level $level, $x, $y, $z, $l, $i1){ $j1 = 1000; for($k1 = 0; $k1 < 4; ++$k1){ - if($k1 == 0 && $i1 == 1 || $k1 == 1 && $i1 == 0 || $k1 == 2 && $i1 == 3 || $k1 == 3 && $i1 == 2){ + if($k1 == 0 && $i1 == 1 || $k1 == 1 && $i1 == 0 || $k1 == 2 && $i1 == 3 || $k1 == 3 && $i1 == 2){ continue; } - + $xs = $x; $ys = $y; $zs = $z; - + switch($k1){ case 0: --$xs; @@ -111,10 +111,10 @@ class LiquidBlockDynamic extends LiquidBlock{ ++$zs; break; } - + if(static::isWaterBlocking($level, $xs, $ys, $zs)) continue; [$id, $meta] = $level->level->getBlock($xs, $ys, $zs); - + if( ((static::$blockID == WATER && ($id == WATER || $id == STILL_WATER)) || (static::$blockID == LAVA && ($id == LAVA || $id == STILL_LAVA))) && @@ -122,20 +122,20 @@ class LiquidBlockDynamic extends LiquidBlock{ ){ continue; } - + if(!static::isWaterBlocking($level, $xs, $ys - 1, $zs)) return $l; - + if($l >= 4) continue; $k2 = static::getSlopeDistance($level, $xs, $ys, $zs, $l + 1, $k1); if($k2 < $j1) $j1 = $k2; } return $j1; } - + public static function trySpreadTo(Level $level, $x, $y, $z, $meta){ if(static::canSpreadTo($level, $x, $y, $z)){ [$id, $meta2] = $level->level->getBlock($x, $y, $z); - + if($id > 0){ if(($id == LAVA || $id == STILL_LAVA) && (static::$blockID == LAVA || static::$blockID == STILL_LAVA)); //fizz else{ @@ -147,23 +147,23 @@ class LiquidBlockDynamic extends LiquidBlock{ } } } - + $level->fastSetBlockUpdate($x, $y, $z, static::$blockID, $meta, true); } } - + public static function onPlace(Level $level, $x, $y, $z){ static::updateLiquid($level, $x, $y, $z); $id = $level->level->getBlockID($x, $y, $z); if($id == static::$blockID) ServerAPI::request()->api->block->scheduleBlockUpdateXYZ($level, $x, $y, $z, BLOCK_UPDATE_SCHEDULED, static::getTickDelay()); } - + public static function onUpdate(Level $level, $x, $y, $z, $type){ $id = $level->level->getBlockID($x, $y, $z); $depth = static::getDepth($level, $x, $y, $z); $flowAdd = (static::$blockID == LAVA) ? 2 : 1; $flag = true; - + if($depth > 0){ static::$sourcesAround = 0; $highest = static::getHighest($level, $x - 1, $y, $z, -100); @@ -174,19 +174,19 @@ class LiquidBlockDynamic extends LiquidBlock{ if($j1 >= 8 || $highest < 0) $j1 = -1; $l1 = static::getDepth($level, $x, $y + 1, $z); if($l1 >= 0) $j1 = ($l1 >= 8 ? $l1 : $l1 + 8); - + if(static::$sourcesAround >= 2 && static::$blockID == WATER){ $idBot = $level->level->getBlockID($x, $y - 1, $z); if($idBot > 0){ if(StaticBlock::getIsSolid($idBot) || (($idBot == WATER || $idBot == STILL_WATER) && $level->level->getBlockDamage($x, $y, $z) == 0)) $j1 = 0; } } - + //if(static::$blockID == LAVA && $depth < 8 && $j1 < 8 && $j1 > $depth && mt_rand(0, 4) != 0){ TODO fix later // $j1 = $depth; // $flag = false; //} - + if($j1 != $depth){ $depth = $j1; if($depth < 0) $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); @@ -195,16 +195,16 @@ class LiquidBlockDynamic extends LiquidBlock{ ServerAPI::request()->api->block->scheduleBlockUpdateXYZ($level, $x, $y, $z, BLOCK_UPDATE_SCHEDULED, static::getTickDelay()); $level->updateNeighborsAt($x, $y, $z, static::$blockID); //TODO check is needed } - }else if($flag){ + }elseif($flag){ static::setStatic($level, $x, $y, $z); } }else{ static::setStatic($level, $x, $y, $z); } - + if(static::canSpreadTo($level, $x, $y - 1, $z)){ $level->fastSetBlockUpdate($x, $y - 1, $z, static::$blockID, ($depth >= 8 ? $depth : $depth + 8) & 0xf, true); - }else if($depth >= 0 && ($depth == 0 || static::isWaterBlocking($level, $x, $y - 1, $z))){ + }elseif($depth >= 0 && ($depth == 0 || static::isWaterBlocking($level, $x, $y - 1, $z))){ $flags = static::getSpread($level, $x, $y, $z); $k1 = $depth >= 8 ? 1 : ($depth + $flowAdd); if($k1 >= 8) return; diff --git a/src/material/block/liquid/LiquidBlockStatic.php b/src/material/block/liquid/LiquidBlockStatic.php index 552c08eb..12265b5d 100644 --- a/src/material/block/liquid/LiquidBlockStatic.php +++ b/src/material/block/liquid/LiquidBlockStatic.php @@ -6,13 +6,13 @@ class LiquidBlockStatic extends LiquidBlock{ } public static $blockID = 0; //TODO: tick: try spread fire if lava - + public static function setDynamic(Level $level, $x, $y, $z){ [$id, $meta] = $level->level->getBlock($x, $y, $z); $dynamicID = $id - 1; //very unsafe - $level->fastSetBlockUpdate($x, $y, $z, $dynamicID, $meta); + $level->fastSetBlockUpdate($x, $y, $z, $dynamicID, $meta); } - + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ $oldID = $level->level->getBlockID($x, $y, $z); static::updateLiquid($level, $x, $y, $z); @@ -21,6 +21,5 @@ class LiquidBlockStatic extends LiquidBlock{ static::setDynamic($level, $x, $y, $z); } } - - -} \ No newline at end of file + + } \ No newline at end of file diff --git a/src/material/block/liquid/StillLavaBlock.php b/src/material/block/liquid/StillLavaBlock.php index 2a8e89cf..2ca2daa2 100644 --- a/src/material/block/liquid/StillLavaBlock.php +++ b/src/material/block/liquid/StillLavaBlock.php @@ -1,4 +1,5 @@ hardness = 500; } - + public static function getTickDelay(){ return 30; } - + } \ No newline at end of file diff --git a/src/material/block/liquid/StillWaterBlock.php b/src/material/block/liquid/StillWaterBlock.php index 632177a3..9e79836b 100644 --- a/src/material/block/liquid/StillWaterBlock.php +++ b/src/material/block/liquid/StillWaterBlock.php @@ -1,6 +1,5 @@ hardness = 500; } - + public static function getTickDelay(){ return 5; } diff --git a/src/material/block/liquid/WaterBlock.php b/src/material/block/liquid/WaterBlock.php index c9e5c3e9..3356ca72 100644 --- a/src/material/block/liquid/WaterBlock.php +++ b/src/material/block/liquid/WaterBlock.php @@ -1,19 +1,20 @@ hardness = 500; } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $ret = $this->level->setBlock($this, $this, true, false, true); return $ret; } - + public static function getTickDelay(){ return 5; } diff --git a/src/material/block/misc/AirBlock.php b/src/material/block/misc/AirBlock.php index 9d6545b9..defdaf3f 100644 --- a/src/material/block/misc/AirBlock.php +++ b/src/material/block/misc/AirBlock.php @@ -2,7 +2,7 @@ class AirBlock extends TransparentBlock{ public static $blockID; - + public function __construct(){ parent::__construct(AIR, 0, "Air"); $this->isActivable = false; @@ -15,7 +15,7 @@ class AirBlock extends TransparentBlock{ $this->isSolid = false; $this->isFullBlock = true; $this->hardness = 0; - + } - + } \ No newline at end of file diff --git a/src/material/block/misc/BedBlock.php b/src/material/block/misc/BedBlock.php index dcfb3729..db72ca0c 100644 --- a/src/material/block/misc/BedBlock.php +++ b/src/material/block/misc/BedBlock.php @@ -20,37 +20,37 @@ class BedBlock extends TransparentBlock{ $this->isFullBlock = false; $this->hardness = 1; } - + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ return [static::getAABB($level, $x, $y, $z)]; } public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ - + $meta = $level->level->getBlockDamage($x, $y, $z); $dir = $meta & 3; $isHead = ($meta >> 3) & 1; - + $offX = self::HEAD_DIRECTION_OFFSETS[$dir][0]; $offZ = self::HEAD_DIRECTION_OFFSETS[$dir][1]; - + if($isHead){ if($level->level->getBlockID($x - $offX, $y, $z - $offZ) != BED_BLOCK){ $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); } - }else if($level->level->getBlockID($x + $offX, $y, $z + $offZ) != BED_BLOCK){ + }elseif($level->level->getBlockID($x + $offX, $y, $z + $offZ) != BED_BLOCK){ $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); - + ServerAPI::request()->api->entity->dropRawPos( - $level, - $x + (lcg_value() * 0.7) + 0.15, - $y + (lcg_value() * 0.7) + 0.15, - $z + (lcg_value() * 0.7) + 0.15, - BlockAPI::getItem(BED, 0, 1), + $level, + $x + (lcg_value() * 0.7) + 0.15, + $y + (lcg_value() * 0.7) + 0.15, + $z + (lcg_value() * 0.7) + 0.15, + BlockAPI::getItem(BED, 0, 1), lcg_value() * 0.2 - 0.1, 0.2, lcg_value() * 0.2 - 0.1 ); - + } - + } public static function findStandUpPosition(Level $level, $x, $y, $z){ $blockMeta = $level->level->getBlockDamage($x, $y, $z); @@ -60,7 +60,7 @@ class BedBlock extends TransparentBlock{ $minZ = $z - self::HEAD_DIRECTION_OFFSETS[$direction][1] * $v7 - 1; $maxX = $minX + 2; $maxZ = $minZ + 2; - + for($xCheck = $minX; $xCheck <= $maxX; ++$xCheck){ for($zCheck = $minZ; $zCheck <= $maxZ; ++$zCheck){ $idCheck = $level->level->getBlockID($xCheck, $y - 1, $zCheck); @@ -70,10 +70,10 @@ class BedBlock extends TransparentBlock{ } } } - + return null; } - + public function onActivate(Item $item, Player $player){ if(ServerAPI::request()->api->time->getPhase($player->level) !== "night"){ $pk = new ChatPacket; @@ -81,12 +81,12 @@ class BedBlock extends TransparentBlock{ $player->dataPacket($pk); return true; } - + $blockNorth = $this->getSide(2); //Gets the blocks around them $blockSouth = $this->getSide(3); $blockEast = $this->getSide(5); $blockWest = $this->getSide(4); - if(($this->meta & 0x08) === 0x08){ //This is the Top part of bed + if(($this->meta & 0x08) === 0x08){ //This is the Top part of bed $b = $this; }else{ //Bottom Part of Bed if($blockNorth->getID() === $this->id and ($blockNorth->meta & 0x08) === 0x08){ @@ -112,11 +112,11 @@ class BedBlock extends TransparentBlock{ } return true; } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $down = $this->getSide(0); if($down->isTransparent === false){ - + $d = $player->entity->getDirection(); $next = $this->getSide(self::$faces[(($d + 3) % 4)]); $downNext = $next->getSide(0); @@ -128,15 +128,15 @@ class BedBlock extends TransparentBlock{ } } return false; - } - + } + public function onBreak(Item $item, Player $player){ $blockNorth = $this->getSide(2); //Gets the blocks around them $blockSouth = $this->getSide(3); $blockEast = $this->getSide(5); $blockWest = $this->getSide(4); - - if(($this->meta & 0x08) === 0x08){ //This is the Top part of bed + + if(($this->meta & 0x08) === 0x08){ //This is the Top part of bed switch($this->meta & 0x7){ case 0: if($blockNorth->id === $this->id) $this->level->setBlock($blockNorth, new AirBlock(), true, false, true); @@ -170,11 +170,11 @@ class BedBlock extends TransparentBlock{ $this->level->setBlock($this, new AirBlock(), true, false, true); return true; } - + public function getDrops(Item $item, Player $player){ - return array( - array(BED, 0, 1), - ); + return [ + [BED, 0, 1], + ]; } - + } \ No newline at end of file diff --git a/src/material/block/misc/FireBlock.php b/src/material/block/misc/FireBlock.php index 8e3b9365..778f1f29 100644 --- a/src/material/block/misc/FireBlock.php +++ b/src/material/block/misc/FireBlock.php @@ -4,7 +4,7 @@ class FireBlock extends FlowableBlock implements LightingBlock{ public static $blockID; public static $flammability = []; public static $fireCatchingChance = []; - + public function __construct($meta = 0){ parent::__construct(FIRE, $meta, "Fire"); $this->isReplaceable = true; @@ -19,39 +19,39 @@ class FireBlock extends FlowableBlock implements LightingBlock{ self::$flammability[$blockID] = $flammability; self::$fireCatchingChance[$blockID] = $v; } - + public static function canBurn(Level $level, $x, $y, $z){ return self::$flammability[$level->level->getBlockID($x, $y, $z)] > 0; } - + public static function onRandomTick(Level $level, $x, $y, $z){ if($level->level->getBlockID($x, $y - 1, $z) !== NETHERRACK){ $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); } } - + public static function onPlace(Level $level, $x, $y, $z){ ServerAPI::request()->api->block->scheduleBlockUpdateXYZ($level, $x, $y, $z, BLOCK_UPDATE_SCHEDULED, 30); } - + public function getDrops(Item $item, Player $player){ - return array(); + return []; } public function getMaxLightValue(){ return 15; } - + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ $b = $level->level->getBlockID($x, $y - 1, $z); if(!StaticBlock::getIsSolid($b)) $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); //TODO more vanilla later? } - + public static function onUpdate(Level $level, $x, $y, $z, $type){ if($type === BLOCK_UPDATE_SCHEDULED){ $idBelow = $level->level->getBlockID($x, $y - 1, $z); [$id, $meta] = $level->level->getBlock($x, $y, $z); $alwaysBurn = $idBelow == NETHERRACK; - + if($meta < 15){ $newMeta = $meta + 1; //TODO better formula if($newMeta > 15) $newMeta = 15; @@ -65,20 +65,19 @@ class FireBlock extends FlowableBlock implements LightingBlock{ } } $chance = self::$fireCatchingChance[$idBelow]; - + if(mt_rand(0, 249) < $chance){ //TODO ignite tnt $level->fastSetBlockUpdate($x, $y - 1, $z, 0, 0, true); goto REMOVE_FIRE; } - + ServerAPI::request()->api->block->scheduleBlockUpdateXYZ($level, $x, $y, $z, BLOCK_UPDATE_SCHEDULED, 30); //TODO looks like it also adds mt_rand(0, 9) to it } } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ parent::place($item, $player, $block, $target, $face, $fx, $fy, $fz); $this->level->scheduleBlockUpdate($this, 30, BLOCK_UPDATE_SCHEDULED); - - + } } \ No newline at end of file diff --git a/src/material/block/misc/NetherReactorBlock.php b/src/material/block/misc/NetherReactorBlock.php index 8c1ff0d0..52d79589 100644 --- a/src/material/block/misc/NetherReactorBlock.php +++ b/src/material/block/misc/NetherReactorBlock.php @@ -7,7 +7,7 @@ class NetherReactorBlock extends SolidBlock{ parent::__construct(NETHER_REACTOR, $meta, "Nether Reactor"); $this->isActivable = true; } - + public function onActivate(Item $item, Player $player){ //if(($item->getID() === IRON_SWORD || $item->getID() === WOODEN_SWORD || $item->getID() === STONE_SWORD || $item->getID() === DIAMOND_SWORD || $item->getID() === GOLD_SWORD) /*&& $player->gamemode === 0*/){ if($this->getMetadata() === 0 && $this->isCorrect($this->getX(),$this->getY(),$this->getZ()) && $this->getY() < 101 && NetherReactorBlock::$enableReactor){ @@ -15,58 +15,58 @@ class NetherReactorBlock extends SolidBlock{ $this->meta = 1; $this->level->setBlock($this,$this); $server = ServerAPI::request(); - $server->schedule(40, array($this, "glow"), 1); - $server->schedule(60, array($this, "glow"), 2); - $server->schedule(80, array($this, "glow"), 3); - $server->schedule(140, array($this, "glow"), 4); - $server->schedule(200, array($this, "spawnItems"), [0,15,2,true]); //200 - $server->schedule(260, array($this, "spawnItems"), [0,15,"checkPigmen",true]); - $server->schedule(300, array($this, "spawnItems"), [0,15,"checkPigmen",true]); - $server->schedule(340, array($this, "spawnItems"), [11,20,"checkPigmen",false]); - $server->schedule(400, array($this, "spawnItems"), [0,10,"checkPigmen",false]); - $server->schedule(500, array($this, "spawnItems"), [17,32, "checkPigmen",false]); //500 - $server->schedule(580, array($this, "spawnItems"), [17,32, "checkPigmen",false]); - $server->schedule(620, array($this, "spawnItems"), [1,32, "checkPigmen",false]); - $server->schedule(660, array($this, "spawnItems"), [1,32,"checkPigmen",false]); - $server->schedule(700, array($this, "spawnItems"), [1,32,"checkPigmen",false]); - $server->schedule(860, array($this, "glow"), 5); - $server->schedule(880, array($this, "glow"), 6); - $server->schedule(900, array($this, "glow"), 7); - $server->schedule(920, array($this, "destroy")); + $server->schedule(40, [$this, "glow"], 1); + $server->schedule(60, [$this, "glow"], 2); + $server->schedule(80, [$this, "glow"], 3); + $server->schedule(140, [$this, "glow"], 4); + $server->schedule(200, [$this, "spawnItems"], [0,15,2,true]); //200 + $server->schedule(260, [$this, "spawnItems"], [0,15,"checkPigmen",true]); + $server->schedule(300, [$this, "spawnItems"], [0,15,"checkPigmen",true]); + $server->schedule(340, [$this, "spawnItems"], [11,20,"checkPigmen",false]); + $server->schedule(400, [$this, "spawnItems"], [0,10,"checkPigmen",false]); + $server->schedule(500, [$this, "spawnItems"], [17,32, "checkPigmen",false]); //500 + $server->schedule(580, [$this, "spawnItems"], [17,32, "checkPigmen",false]); + $server->schedule(620, [$this, "spawnItems"], [1,32, "checkPigmen",false]); + $server->schedule(660, [$this, "spawnItems"], [1,32,"checkPigmen",false]); + $server->schedule(700, [$this, "spawnItems"], [1,32,"checkPigmen",false]); + $server->schedule(860, [$this, "glow"], 5); + $server->schedule(880, [$this, "glow"], 6); + $server->schedule(900, [$this, "glow"], 7); + $server->schedule(920, [$this, "destroy"]); return true; } //} - + } - + public function destroy(){ $this->level->setBlock(new Vector3($this->x, $this->y, $this->z),new NetherReactorBlock(2)); - $this->decay($this->x-8, $this->y-3, $this->z-8, 0, 17, 16, 2, 34, 1, 0, 17, 1); - $this->decay($this->x-8, $this->y-3, $this->z-8, 1, 16, 1, 2, 34, 1, 0, 17, 16); - $this->decay($this->x-8, $this->y-3, $this->z-8, 3, 14, 10, 8, 34, 1, 3, 14, 1); - $this->decay($this->x-8, $this->y-3, $this->z-8, 4, 13, 1, 8, 34, 1, 3, 14, 10); - $this->decay($this->x-8, $this->y-3, $this->z-8, 5, 12, 6, 14, 34, 1, 5, 12, 1); - $this->decay($this->x-8, $this->y-3, $this->z-8, 6, 11, 1, 14, 34, 1, 5, 12, 16); + $this->decay($this->x - 8, $this->y - 3, $this->z - 8, 0, 17, 16, 2, 34, 1, 0, 17, 1); + $this->decay($this->x - 8, $this->y - 3, $this->z - 8, 1, 16, 1, 2, 34, 1, 0, 17, 16); + $this->decay($this->x - 8, $this->y - 3, $this->z - 8, 3, 14, 10, 8, 34, 1, 3, 14, 1); + $this->decay($this->x - 8, $this->y - 3, $this->z - 8, 4, 13, 1, 8, 34, 1, 3, 14, 10); + $this->decay($this->x - 8, $this->y - 3, $this->z - 8, 5, 12, 6, 14, 34, 1, 5, 12, 1); + $this->decay($this->x - 8, $this->y - 3, $this->z - 8, 6, 11, 1, 14, 34, 1, 5, 12, 16); } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 1){ return [[NETHER_REACTOR, 0, 1]]; } } - + private function decay($x, $y, $z, $aOne, $aTwo, $aThree, $bOne, $bTwo, $bThree, $cOne, $cTwo, $cThree) { for($a = $aOne; $a < $aTwo; $a += $aThree) { for($b = $bOne; $b < $bTwo; $b += $bThree) { for($c = $cOne; $c < $cTwo; $c += $cThree) { - if ($this->level->level->getBlockID($x+$a, $y+$b, $z+$c) === NETHERRACK && lcg_value() > 0.75){ - $this->level->fastSetBlockUpdate($x+$a, $y+$b, $z+$c, 0, 0); + if ($this->level->level->getBlockID($x + $a, $y + $b, $z + $c) === NETHERRACK && lcg_value() > 0.75){ + $this->level->fastSetBlockUpdate($x + $a, $y + $b, $z + $c, 0, 0); } } } } } - + private function pigmenCheck($x,$y,$z) { $pigCount = 0; $server = ServerAPI::request(); @@ -88,28 +88,28 @@ class NetherReactorBlock extends SolidBlock{ $forceAmount = $data[3]; $server = ServerAPI::request(); if(!$forceAmount){ - $spawnNumber = $minAmount + floor(lcg_value()*($maxAmount-$minAmount+1)); + $spawnNumber = $minAmount + floor(lcg_value() * ($maxAmount - $minAmount + 1)); } else{ $spawnNumber = $maxAmount; } for($i = 0; $i < $spawnNumber; $i++) { - $randomRange = floor(lcg_value()*5+3); - $shiftX = cos(floor(lcg_value()*360)*(pi()/180)); - $shiftZ = sin(floor(lcg_value()*360)*(pi()/180)); + $randomRange = floor(lcg_value() * 5 + 3); + $shiftX = cos(floor(lcg_value() * 360) * (pi() / 180)); + $shiftZ = sin(floor(lcg_value() * 360) * (pi() / 180)); if(Utils::chance(5)) $randomID = $this->rarePossibleLoot[array_rand($this->rarePossibleLoot)]; else $randomID = $this->possibleLoot[array_rand($this->possibleLoot)]; - $server->api->entity->drop(new Position($x+($shiftX*$randomRange)+0.5, $y, $z+($shiftZ*$randomRange)+0.5, $this->level), BlockAPI::getItem($randomID, 0, 1)); + $server->api->entity->drop(new Position($x + ($shiftX * $randomRange) + 0.5, $y, $z + ($shiftZ * $randomRange) + 0.5, $this->level), BlockAPI::getItem($randomID, 0, 1)); } for($i = 0; $i < $pigmen; $i++) { - $randomRange = floor(lcg_value()*5+3); - $shiftX = cos(floor(lcg_value()*360)*(pi()/180)); - $shiftZ = sin(floor(lcg_value()*360)*(pi()/180)); - $data = array( - "x" => $x+($shiftX*$randomRange)+0.5, - "y" => $y, - "z" => $z+($shiftZ*$randomRange)+0.5, - ); + $randomRange = floor(lcg_value() * 5 + 3); + $shiftX = cos(floor(lcg_value() * 360) * (pi() / 180)); + $shiftZ = sin(floor(lcg_value() * 360) * (pi() / 180)); + $data = [ + "x" => $x + ($shiftX * $randomRange) + 0.5, + "y" => $y, + "z" => $z + ($shiftZ * $randomRange) + 0.5, + ]; $e = $server->api->entity->add($this->level, ENTITY_MOB, MOB_PIGMAN, $data); $server->api->entity->spawnToAll($e); } @@ -120,63 +120,63 @@ class NetherReactorBlock extends SolidBlock{ $z = $this->z; switch($part){ case 1: - $this->level->setBlock(new Vector3($x, $y-1, $z),new GlowingObsidianBlock); - $this->level->setBlock(new Vector3($x+1, $y-1, $z),new GlowingObsidianBlock); - $this->level->setBlock(new Vector3($x-1, $y-1, $z),new GlowingObsidianBlock); - $this->level->setBlock(new Vector3($x, $y-1, $z+1),new GlowingObsidianBlock); - $this->level->setBlock(new Vector3($x, $y-1, $z-1),new GlowingObsidianBlock); + $this->level->setBlock(new Vector3($x, $y - 1, $z),new GlowingObsidianBlock); + $this->level->setBlock(new Vector3($x + 1, $y - 1, $z),new GlowingObsidianBlock); + $this->level->setBlock(new Vector3($x - 1, $y - 1, $z),new GlowingObsidianBlock); + $this->level->setBlock(new Vector3($x, $y - 1, $z + 1),new GlowingObsidianBlock); + $this->level->setBlock(new Vector3($x, $y - 1, $z - 1),new GlowingObsidianBlock); break; case 2: - $this->level->setBlock(new Vector3($x+1, $y, $z+1),new GlowingObsidianBlock); - $this->level->setBlock(new Vector3($x+1, $y, $z-1),new GlowingObsidianBlock); - $this->level->setBlock(new Vector3($x-1, $y, $z+1),new GlowingObsidianBlock); - $this->level->setBlock(new Vector3($x-1, $y, $z-1),new GlowingObsidianBlock); + $this->level->setBlock(new Vector3($x + 1, $y, $z + 1),new GlowingObsidianBlock); + $this->level->setBlock(new Vector3($x + 1, $y, $z - 1),new GlowingObsidianBlock); + $this->level->setBlock(new Vector3($x - 1, $y, $z + 1),new GlowingObsidianBlock); + $this->level->setBlock(new Vector3($x - 1, $y, $z - 1),new GlowingObsidianBlock); break; case 3: - $this->level->setBlock(new Vector3($x, $y+1, $z),new GlowingObsidianBlock); - $this->level->setBlock(new Vector3($x+1, $y+1, $z),new GlowingObsidianBlock); - $this->level->setBlock(new Vector3($x-1, $y+1, $z),new GlowingObsidianBlock); - $this->level->setBlock(new Vector3($x, $y+1, $z+1),new GlowingObsidianBlock); - $this->level->setBlock(new Vector3($x, $y+1, $z-1),new GlowingObsidianBlock); + $this->level->setBlock(new Vector3($x, $y + 1, $z),new GlowingObsidianBlock); + $this->level->setBlock(new Vector3($x + 1, $y + 1, $z),new GlowingObsidianBlock); + $this->level->setBlock(new Vector3($x - 1, $y + 1, $z),new GlowingObsidianBlock); + $this->level->setBlock(new Vector3($x, $y + 1, $z + 1),new GlowingObsidianBlock); + $this->level->setBlock(new Vector3($x, $y + 1, $z - 1),new GlowingObsidianBlock); break; case 4: - $this->level->setBlock(new Vector3($x+1, $y-1, $z+1),new GlowingObsidianBlock); - $this->level->setBlock(new Vector3($x+1, $y-1, $z-1),new GlowingObsidianBlock); - $this->level->setBlock(new Vector3($x-1, $y-1, $z+1),new GlowingObsidianBlock); - $this->level->setBlock(new Vector3($x-1, $y-1, $z-1),new GlowingObsidianBlock); + $this->level->setBlock(new Vector3($x + 1, $y - 1, $z + 1),new GlowingObsidianBlock); + $this->level->setBlock(new Vector3($x + 1, $y - 1, $z - 1),new GlowingObsidianBlock); + $this->level->setBlock(new Vector3($x - 1, $y - 1, $z + 1),new GlowingObsidianBlock); + $this->level->setBlock(new Vector3($x - 1, $y - 1, $z - 1),new GlowingObsidianBlock); break; case 5: - $this->level->setBlock(new Vector3($x, $y+1, $z),new ObsidianBlock); - $this->level->setBlock(new Vector3($x+1, $y+1, $z),new ObsidianBlock); - $this->level->setBlock(new Vector3($x-1, $y+1, $z),new ObsidianBlock); - $this->level->setBlock(new Vector3($x, $y+1, $z+1),new ObsidianBlock); - $this->level->setBlock(new Vector3($x, $y+1, $z-1),new ObsidianBlock); - $this->level->setBlock(new Vector3($x+1, $y+1, $z+1),new ObsidianBlock); - $this->level->setBlock(new Vector3($x+1, $y+1, $z-1),new ObsidianBlock); - $this->level->setBlock(new Vector3($x-1, $y+1, $z+1),new ObsidianBlock); - $this->level->setBlock(new Vector3($x-1, $y+1, $z-1),new ObsidianBlock); + $this->level->setBlock(new Vector3($x, $y + 1, $z),new ObsidianBlock); + $this->level->setBlock(new Vector3($x + 1, $y + 1, $z),new ObsidianBlock); + $this->level->setBlock(new Vector3($x - 1, $y + 1, $z),new ObsidianBlock); + $this->level->setBlock(new Vector3($x, $y + 1, $z + 1),new ObsidianBlock); + $this->level->setBlock(new Vector3($x, $y + 1, $z - 1),new ObsidianBlock); + $this->level->setBlock(new Vector3($x + 1, $y + 1, $z + 1),new ObsidianBlock); + $this->level->setBlock(new Vector3($x + 1, $y + 1, $z - 1),new ObsidianBlock); + $this->level->setBlock(new Vector3($x - 1, $y + 1, $z + 1),new ObsidianBlock); + $this->level->setBlock(new Vector3($x - 1, $y + 1, $z - 1),new ObsidianBlock); break; case 6: $this->level->setBlock(new Vector3($x, $y, $z), new NetherReactorBlock(2)); - $this->level->setBlock(new Vector3($x+1, $y, $z),new ObsidianBlock); - $this->level->setBlock(new Vector3($x-1, $y, $z),new ObsidianBlock); - $this->level->setBlock(new Vector3($x, $y, $z+1),new ObsidianBlock); - $this->level->setBlock(new Vector3($x, $y, $z-1),new ObsidianBlock); - $this->level->setBlock(new Vector3($x+1, $y, $z+1),new ObsidianBlock); - $this->level->setBlock(new Vector3($x+1, $y, $z-1),new ObsidianBlock); - $this->level->setBlock(new Vector3($x-1, $y, $z+1),new ObsidianBlock); - $this->level->setBlock(new Vector3($x-1, $y, $z-1),new ObsidianBlock); + $this->level->setBlock(new Vector3($x + 1, $y, $z),new ObsidianBlock); + $this->level->setBlock(new Vector3($x - 1, $y, $z),new ObsidianBlock); + $this->level->setBlock(new Vector3($x, $y, $z + 1),new ObsidianBlock); + $this->level->setBlock(new Vector3($x, $y, $z - 1),new ObsidianBlock); + $this->level->setBlock(new Vector3($x + 1, $y, $z + 1),new ObsidianBlock); + $this->level->setBlock(new Vector3($x + 1, $y, $z - 1),new ObsidianBlock); + $this->level->setBlock(new Vector3($x - 1, $y, $z + 1),new ObsidianBlock); + $this->level->setBlock(new Vector3($x - 1, $y, $z - 1),new ObsidianBlock); break; case 7: - $this->level->setBlock(new Vector3($x, $y-1, $z),new ObsidianBlock); - $this->level->setBlock(new Vector3($x+1, $y-1, $z),new ObsidianBlock); - $this->level->setBlock(new Vector3($x-1, $y-1, $z),new ObsidianBlock); - $this->level->setBlock(new Vector3($x, $y-1, $z+1),new ObsidianBlock); - $this->level->setBlock(new Vector3($x, $y-1, $z-1),new ObsidianBlock); - $this->level->setBlock(new Vector3($x+1, $y-1, $z+1),new ObsidianBlock); - $this->level->setBlock(new Vector3($x+1, $y-1, $z-1),new ObsidianBlock); - $this->level->setBlock(new Vector3($x-1, $y-1, $z+1),new ObsidianBlock); - $this->level->setBlock(new Vector3($x-1, $y-1, $z-1),new ObsidianBlock); + $this->level->setBlock(new Vector3($x, $y - 1, $z),new ObsidianBlock); + $this->level->setBlock(new Vector3($x + 1, $y - 1, $z),new ObsidianBlock); + $this->level->setBlock(new Vector3($x - 1, $y - 1, $z),new ObsidianBlock); + $this->level->setBlock(new Vector3($x, $y - 1, $z + 1),new ObsidianBlock); + $this->level->setBlock(new Vector3($x, $y - 1, $z - 1),new ObsidianBlock); + $this->level->setBlock(new Vector3($x + 1, $y - 1, $z + 1),new ObsidianBlock); + $this->level->setBlock(new Vector3($x + 1, $y - 1, $z - 1),new ObsidianBlock); + $this->level->setBlock(new Vector3($x - 1, $y - 1, $z + 1),new ObsidianBlock); + $this->level->setBlock(new Vector3($x - 1, $y - 1, $z - 1),new ObsidianBlock); break; } } @@ -218,18 +218,18 @@ class NetherReactorBlock extends SolidBlock{ } return true; } - + private $possibleLoot = [ GLOWSTONE_DUST, QUARTZ, CACTUS, SUGARCANE, BROWN_MUSHROOM, RED_MUSHROOM, PUMPKIN_SEEDS, MELON_SEEDS ]; - + private $rarePossibleLoot = [ BOW, BED, BOWL, ARROW, WOODEN_DOOR, FEATHER, PAINTING, BONE, DANDELION ]; - + private $core = [ -1 => ["GCG", "CCC", "GCG"], 0 => ["C C", " R ", "C C",], - 1 =>[" C ", "CCC", " C "] + 1 => [" C ", "CCC", " C "] ]; } diff --git a/src/material/block/misc/TNTBlock.php b/src/material/block/misc/TNTBlock.php index aeef0412..80018057 100644 --- a/src/material/block/misc/TNTBlock.php +++ b/src/material/block/misc/TNTBlock.php @@ -7,19 +7,19 @@ class TNTBlock extends SolidBlock{ $this->hardness = 0; $this->isActivable = true; } - + public function onActivate(Item $item, Player $player){ if($item->getID() === FLINT_STEEL){ if(($player->gamemode & 0x01) === 0){ $item->useOn($this); } - $data = array( + $data = [ "x" => $this->x + 0.5, "y" => $this->y, "z" => $this->z + 0.5, "power" => 3, "fuse" => 20 * 4, //4 seconds - ); + ]; $this->level->fastSetBlockUpdate($this->x, $this->y, $this->z, 0, 0, true); $e = ServerAPI::request()->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_PRIMEDTNT, $data); ServerAPI::request()->api->entity->spawnToAll($e); diff --git a/src/material/block/nonfull/CakeBlock.php b/src/material/block/nonfull/CakeBlock.php index f7eded78..fc9740e9 100644 --- a/src/material/block/nonfull/CakeBlock.php +++ b/src/material/block/nonfull/CakeBlock.php @@ -9,18 +9,18 @@ class CakeBlock extends TransparentBlock{ $this->meta = $meta & 0x07; $this->hardness = 2.5; } - + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ $data = $level->level->getBlockDamage($x, $y, $z); - return [new AxisAlignedBB($x + (2*$data + 1) * 0.0625, $y, $z + 0.0625, $x + 0.9375, $y + 0.5, $z + 0.9375)]; + return [new AxisAlignedBB($x + (2 * $data + 1) * 0.0625, $y, $z + 0.0625, $x + 0.9375, $y + 0.5, $z + 0.9375)]; } - + public static function updateShape(Level $level, $x, $y, $z){ [$id, $data] = $level->level->getBlock($x, $y, $z); - + StaticBlock::setBlockBounds($id, (2 * $data + 1) * 0.0625, 0.0, 0.0625, 0.9375, 0.5, 0.9375); } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $down = $this->getSide(0); if($down->getID() !== AIR){ @@ -29,17 +29,17 @@ class CakeBlock extends TransparentBlock{ } return false; } - + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ if($level->level->getBlockID($x, $y - 1, $z) == AIR){ //Replace with common break method $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); } } - + public function getDrops(Item $item, Player $player){ - return array(); + return []; } - + public function onActivate(Item $item, Player $player){ if($player->entity->getHealth() < 20){ ++$this->meta; @@ -53,5 +53,5 @@ class CakeBlock extends TransparentBlock{ } return false; } - + } \ No newline at end of file diff --git a/src/material/block/nonfull/CarpetBlock.php b/src/material/block/nonfull/CarpetBlock.php index 60b93a98..bfe18df4 100644 --- a/src/material/block/nonfull/CarpetBlock.php +++ b/src/material/block/nonfull/CarpetBlock.php @@ -4,7 +4,7 @@ class CarpetBlock extends FlowableBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(CARPET, $meta, "Carpet"); - $names = array( + $names = [ 0 => "White Carpet", 1 => "Orange Carpet", 2 => "Magenta Carpet", @@ -21,10 +21,10 @@ class CarpetBlock extends FlowableBlock{ 13 => "Green Carpet", 14 => "Red Carpet", 15 => "Black Carpet", - ); + ]; $this->name = $names[$this->meta]; $this->hardness = 0; - $this->isFullBlock = false; + $this->isFullBlock = false; $this->isSolid = true; } public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ diff --git a/src/material/block/nonfull/ChestBlock.php b/src/material/block/nonfull/ChestBlock.php index 344daff4..b3a3422d 100644 --- a/src/material/block/nonfull/ChestBlock.php +++ b/src/material/block/nonfull/ChestBlock.php @@ -11,19 +11,19 @@ class ChestBlock extends TransparentBlock{ public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ return [new AxisAlignedBB($x + 0.025, $y, $z + 0.025, $x + 0.975, $y + 0.95, $z + 0.975)]; } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $server = ServerAPI::request(); - $faces = array( + $faces = [ 0 => 4, 1 => 2, 2 => 5, 3 => 3, - ); + ]; $chest = false; $this->meta = $faces[$player->entity->getDirection()]; - + for($side = 2; $side <= 5; ++$side){ if(($this->meta === 4 or $this->meta === 5) and ($side === 4 or $side === 5)){ continue; @@ -40,22 +40,22 @@ class ChestBlock extends TransparentBlock{ } $this->level->setBlock($block, $this, true, false, true); - $tile = $server->api->tile->add($this->level, TILE_CHEST, $this->x, $this->y, $this->z, array( - "Items" => array(), + $tile = $server->api->tile->add($this->level, TILE_CHEST, $this->x, $this->y, $this->z, [ + "Items" => [], "id" => TILE_CHEST, "x" => $this->x, "y" => $this->y, "z" => $this->z - )); + ]); $server->api->tile->spawnToAll($tile); - + if($chest instanceof Tile){ $chest->pairWith($tile); $tile->pairWith($chest); } return true; } - + public function onBreak(Item $item, Player $player){ $t = ServerAPI::request()->api->tile->get($this); if($t !== false){ @@ -64,48 +64,46 @@ class ChestBlock extends TransparentBlock{ $this->level->setBlock($this, new AirBlock(), true, true, true); return true; } - + public function onActivate(Item $item, Player $player){ $top = $this->getSide(1); if($top->isTransparent !== true){ return true; } - + $server = ServerAPI::request(); $t = $server->api->tile->get($this); $chest = false; if($t !== false){ $chest = $t; }else{ - $chest = $server->api->tile->add($this->level, TILE_CHEST, $this->x, $this->y, $this->z, array( - "Items" => array(), + $chest = $server->api->tile->add($this->level, TILE_CHEST, $this->x, $this->y, $this->z, [ + "Items" => [], "id" => TILE_CHEST, "x" => $this->x, "y" => $this->y, - "z" => $this->z - )); + "z" => $this->z + ]); } - - - + if(($player->gamemode & 0x01) === 0x01){ return true; } - - $chest->openInventory($player); + + $chest->openInventory($player); return true; } public function getDrops(Item $item, Player $player){ - $drops = array( - array($this->id, 0, 1), - ); + $drops = [ + [$this->id, 0, 1], + ]; $t = ServerAPI::request()->api->tile->get($this); if($t !== false and $t->class === TILE_CHEST){ for($s = 0; $s < CHEST_SLOTS; ++$s){ $slot = $t->getSlot($s); if($slot->getID() > AIR and $slot->count > 0){ - $drops[] = array($slot->getID(), $slot->getMetadata(), $slot->count); + $drops[] = [$slot->getID(), $slot->getMetadata(), $slot->count]; } } } diff --git a/src/material/block/nonfull/CobwebBlock.php b/src/material/block/nonfull/CobwebBlock.php index 4b6a9226..558dbd69 100644 --- a/src/material/block/nonfull/CobwebBlock.php +++ b/src/material/block/nonfull/CobwebBlock.php @@ -3,16 +3,16 @@ class CobwebBlock extends FlowableBlock{ public static $blockID; public function __construct(){ - parent::__construct(COBWEB, 0, "Cobweb"); + parent::__construct(COBWEB, 0, "Cobweb"); $this->isSolid = true; $this->isFullBlock = false; $this->hardness = 25; } - + public static function getAABB(Level $level, $x, $y, $z){ return null; } - + public function getDrops(Item $item, Player $player){ if ($item->isSword()){ return [ @@ -21,11 +21,11 @@ class CobwebBlock extends FlowableBlock{ } elseif ($item->isShears()){ return [ - array(COBWEB, 0, 1), + [COBWEB, 0, 1], ]; } } - + public static function onEntityCollidedWithBlock(Level $level, $x, $y, $z, Entity $entity){ $entity->setInWeb(); } diff --git a/src/material/block/nonfull/FarmlandBlock.php b/src/material/block/nonfull/FarmlandBlock.php index 580f78d3..dac2bdf6 100644 --- a/src/material/block/nonfull/FarmlandBlock.php +++ b/src/material/block/nonfull/FarmlandBlock.php @@ -7,27 +7,27 @@ class FarmlandBlock extends TransparentBlock{ $this->hardness = 3; } public function getDrops(Item $item, Player $player){ - return array( - array(DIRT, 0, 1), - ); + return [ + [DIRT, 0, 1], + ]; } public function hasCrops(){ //TODO vanilla 0.8.1 detection method $b = $this->getSide(1); return $b->isTransparent && $b->id != 0; } - + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ return [new AxisAlignedBB($x, $y, $z, $x + 1, $y + 1, $z + 1)]; } - + public static function fallOn(Level $level, $x, $y, $z, Entity $entity, $fallDistance){ $rv = lcg_value(); if($rv < ($fallDistance - 0.5)){ $level->fastSetBlockUpdate($x, $y, $z, DIRT, 0); } } - + public static function onRandomTick(Level $level, $x, $y, $z){ $meta = $level->level->getBlockDamage($x, $y, $z); $b = $level->level->getBlockID($x, $y + 1, $z); @@ -41,8 +41,7 @@ class FarmlandBlock extends TransparentBlock{ $level->fastSetBlockUpdate($x, $y, $z, DIRT, 0, true); } } - - + } public static function checkWaterStatic(Level $level, $x, $y, $z) @@ -58,7 +57,7 @@ class FarmlandBlock extends TransparentBlock{ } } } - + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ if(!StaticBlock::getIsTransparent($level->level->getBlockID($x, $y + 1, $z))){ $level->fastSetBlockUpdate($x, $y, $z, DIRT, 0, true); diff --git a/src/material/block/nonfull/FenceBlock.php b/src/material/block/nonfull/FenceBlock.php index 98442208..47b17932 100644 --- a/src/material/block/nonfull/FenceBlock.php +++ b/src/material/block/nonfull/FenceBlock.php @@ -7,16 +7,16 @@ class FenceBlock extends TransparentBlock{ $this->isFullBlock = false; $this->hardness = 15; } - + public static function canConnectTo(Level $level, $x, $y, $z){ $id = $level->level->getBlockID($x, $y, $z); - + if($id != FENCE && $id != FENCE_GATE){ return StaticBlock::getIsSolid($id) && $id != PUMPKIN; //TODO check var6 != null && var6.blockMaterial.isOpaque() && var6.renderAsNormalBlock() ? var6.blockMaterial != Material.pumpkin : false; } return true; } - + public static function getAABB(Level $level, $x, $y, $z){ $v9 = self::canConnectTo($level, $x, $y, $z - 1); $v10 = self::canConnectTo($level, $x, $y, $z + 1); @@ -29,7 +29,7 @@ class FenceBlock extends TransparentBlock{ StaticBlock::setBlockBounds(static::$blockID, $minX, 0, $minZ, $maxX, 1.5, $maxZ); return parent::getAABB($level, $x, $y, $z); } - + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ $v8 = self::canConnectTo($level, $x, $y, $z - 1); $v9 = self::canConnectTo($level, $x, $y, $z + 1); @@ -47,11 +47,11 @@ class FenceBlock extends TransparentBlock{ $v15 = 0.625; if($v10) $v12 = 0; if($v11) $v13 = 1; - + if($v10 || $v11 || !$v8 && !$v9){ $arr[] = new AxisAlignedBB($x + $v12, $y + 0, $z + $v14, $x + $v13, $y + 1.5, $z + $v15); } return $arr; } - + } \ No newline at end of file diff --git a/src/material/block/nonfull/FenceGateBlock.php b/src/material/block/nonfull/FenceGateBlock.php index be286bf9..0de9a354 100644 --- a/src/material/block/nonfull/FenceGateBlock.php +++ b/src/material/block/nonfull/FenceGateBlock.php @@ -7,21 +7,21 @@ class FenceGateBlock extends TransparentBlock{ if($aabb == null) return []; return [$aabb]; } - + public static function getAABB(Level $level, $x, $y, $z){ $data = $level->level->getBlockDamage($x, $y, $z); - + if($data & 4){ return null; } - + if($data != 2 && $data != 0){ return new AxisAlignedBB($x + 0.375, $y, $z, $x + 0.625, $y + 1.5, $z + 1.0); }else{ return new AxisAlignedBB($x, $y, $z + 0.375, $x + 1, $y + 1.5, $z + 0.625); } } - + public function __construct($meta = 0){ parent::__construct(FENCE_GATE, $meta, "Fence Gate"); $this->isActivable = true; @@ -33,29 +33,29 @@ class FenceGateBlock extends TransparentBlock{ $this->hardness = 15; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - $faces = array( + $faces = [ 0 => 3, 1 => 0, 2 => 1, 3 => 2, - ); + ]; $this->meta = $faces[$player->entity->getDirection()] & 0x03; $this->level->setBlock($block, $this, true, false, true); return true; } public function getDrops(Item $item, Player $player){ - return array( - array($this->id, 0, 1), - ); + return [ + [$this->id, 0, 1], + ]; } public function onActivate(Item $item, Player $player){ - + $meta = $this->meta; if(($meta & 4) != 0){ $meta ^= 4; }else{ $direction = ($player->entity->yaw * 4 / 360) + 0.5; - $blockDirection = (int)$direction; + $blockDirection = (int) $direction; if($direction < $blockDirection) --$blockDirection; $blockDirection &= 3; if(($meta & 3) == (($blockDirection + 2) & 3)){ @@ -63,7 +63,7 @@ class FenceGateBlock extends TransparentBlock{ } $meta |= 4; } - + $this->level->fastSetBlockUpdate($this->x, $this->y, $this->z, $this->id, $meta); $players = $this->level->players; unset($players[$player->CID]); diff --git a/src/material/block/nonfull/GlassPaneBlock.php b/src/material/block/nonfull/GlassPaneBlock.php index 0af71ac4..959cdd22 100644 --- a/src/material/block/nonfull/GlassPaneBlock.php +++ b/src/material/block/nonfull/GlassPaneBlock.php @@ -9,33 +9,33 @@ class GlassPaneBlock extends TransparentBlock{ } public static function updateShape(Level $level, $x, $y, $z){ $id = $level->level->getBlockID($x, $y, $z); - + $minX = 0.4375; $maxX = 0.5625; $minZ = 0.4375; $maxZ = 0.5625; - + $var9 = self::canConnectTo($level->level->getBlockID($x, $y, $z - 1)); $var10 = self::canConnectTo($level->level->getBlockID($x, $y, $z + 1)); $var11 = self::canConnectTo($level->level->getBlockID($x - 1, $y, $z)); $var12 = self::canConnectTo($level->level->getBlockID($x + 1, $y, $z)); - + if((!$var11 || !$var12) && ($var11 || $var12 || $var9 || $var10)){ if($var11 && !$var12) $minX = 0; - else if(!$var11 && $var12) $maxX = 1; + elseif(!$var11 && $var12) $maxX = 1; }else{ $minX = 0; $maxX = 1; } - + if((!$var9 || !$var10) && ($var11 || $var12 || $var9 || $var10)){ if($var9 && !$var10) $minZ = 0; - else if(!$var9 && $var10) $maxZ = 1; + elseif(!$var9 && $var10) $maxZ = 1; }else{ $minZ = 0; $maxZ = 1; } - + StaticBlock::setBlockBounds($id, $minX, 0, $minZ, $maxX, 1, $maxZ); } public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ @@ -51,7 +51,7 @@ class GlassPaneBlock extends TransparentBlock{ }else{ $arr[] = $aabb->addMinMax(0, 0, 0.4375, 1, 1, 0.5625); } - + if((!$var8 || !$var9) && ($var10 || $var11 || $var8 || $var9)){ if($var8 && !$var9) $arr[] = $aabb->addMinMax(0.4375, 0, 0, 0.5625, 1, 0.5); elseif(!$var8 && $var9) $arr[] = $aabb->addMinMax(0.4375, 0, 0.5, 0.5625, 1, 1); @@ -60,14 +60,14 @@ class GlassPaneBlock extends TransparentBlock{ } return $arr; } - + public static function canConnectTo($blockID) : bool{ return StaticBlock::getIsSolid($blockID) || $blockID == GLASS_PANE || $blockID == GLASS; } - + public function getDrops(Item $item, Player $player){ - return array( - array(GLASS_PANE, 0, 0), - ); + return [ + [GLASS_PANE, 0, 0], + ]; } } \ No newline at end of file diff --git a/src/material/block/nonfull/IronBarsBlock.php b/src/material/block/nonfull/IronBarsBlock.php index feeca1cc..3666bc6f 100644 --- a/src/material/block/nonfull/IronBarsBlock.php +++ b/src/material/block/nonfull/IronBarsBlock.php @@ -7,39 +7,39 @@ class IronBarsBlock extends TransparentBlock{ $this->isFullBlock = false; $this->isSolid = false; } - + public static function updateShape(Level $level, $x, $y, $z){ $id = $level->level->getBlockID($x, $y, $z); - + $minX = 0.4375; $maxX = 0.5625; $minZ = 0.4375; $maxZ = 0.5625; - + $var9 = self::canConnectTo($level->level->getBlockID($x, $y, $z - 1)); $var10 = self::canConnectTo($level->level->getBlockID($x, $y, $z + 1)); $var11 = self::canConnectTo($level->level->getBlockID($x - 1, $y, $z)); $var12 = self::canConnectTo($level->level->getBlockID($x + 1, $y, $z)); - + if((!$var11 || !$var12) && ($var11 || $var12 || $var9 || $var10)){ if($var11 && !$var12) $minX = 0; - else if(!$var11 && $var12) $maxX = 1; + elseif(!$var11 && $var12) $maxX = 1; }else{ $minX = 0; $maxX = 1; } - + if((!$var9 || !$var10) && ($var11 || $var12 || $var9 || $var10)){ if($var9 && !$var10) $minZ = 0; - else if(!$var9 && $var10) $maxZ = 1; + elseif(!$var9 && $var10) $maxZ = 1; }else{ $minZ = 0; $maxZ = 1; } - + StaticBlock::setBlockBounds($id, $minX, 0, $minZ, $maxX, 1, $maxZ); } - + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ $var8 = self::canConnectTo($level->level->getBlockID($x, $y, $z - 1)); $var9 = self::canConnectTo($level->level->getBlockID($x, $y, $z + 1)); @@ -53,7 +53,7 @@ class IronBarsBlock extends TransparentBlock{ }else{ $arr[] = $aabb->addMinMax(0, 0, 0.4375, 1, 1, 0.5625); } - + if((!$var8 || !$var9) && ($var10 || $var11 || $var8 || $var9)){ if($var8 && !$var9) $arr[] = $aabb->addMinMax(0.4375, 0, 0, 0.5625, 1, 0.5); elseif(!$var8 && $var9) $arr[] = $aabb->addMinMax(0.4375, 0, 0.5, 0.5625, 1, 1); @@ -62,9 +62,9 @@ class IronBarsBlock extends TransparentBlock{ } return $arr; } - + public static function canConnectTo($blockID) : bool{ return StaticBlock::getIsSolid($blockID) || $blockID == IRON_BARS || $blockID == GLASS; } - + } \ No newline at end of file diff --git a/src/material/block/nonfull/IronDoorBlock.php b/src/material/block/nonfull/IronDoorBlock.php index 5ac59aae..fd3b24f3 100644 --- a/src/material/block/nonfull/IronDoorBlock.php +++ b/src/material/block/nonfull/IronDoorBlock.php @@ -21,14 +21,14 @@ class IronDoorBlock extends DoorBlock{ default => 25, }; } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 1){ - return array( - array(IRON_DOOR, 0, 1), - ); + return [ + [IRON_DOOR, 0, 1], + ]; }else{ - return array(); + return []; } } } diff --git a/src/material/block/nonfull/PoweredRailBlock.php b/src/material/block/nonfull/PoweredRailBlock.php index 0fd8a32a..0de0c7cd 100644 --- a/src/material/block/nonfull/PoweredRailBlock.php +++ b/src/material/block/nonfull/PoweredRailBlock.php @@ -1,12 +1,13 @@ hardness = 0.7; - $this->isFullBlock = false; + $this->isFullBlock = false; $this->isSolid = false; } - + } \ No newline at end of file diff --git a/src/material/block/nonfull/RailBaseBlock.php b/src/material/block/nonfull/RailBaseBlock.php index f74a085c..c6886596 100644 --- a/src/material/block/nonfull/RailBaseBlock.php +++ b/src/material/block/nonfull/RailBaseBlock.php @@ -2,24 +2,24 @@ abstract class RailBaseBlock extends FlowableBlock //TODO move some methods here { - + public static function isRailBlock(Level $l, $x, $y, $z){ $id = $l->level->getBlockID($x, $y, $z); return $id === POWERED_RAIL || $id === RAIL; } - + public static function isRailID($id){ return $id == POWERED_RAIL || $id == RAIL; } - + public static function getAABB(Level $level, $x, $y, $z){ return null; } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $down = $this->getSide(0); if($down->getID() !== AIR and $down instanceof SolidBlock){ - + $this->level->setBlock($block, $this, true, false, true); $logic = new RailLogic($this); $logic->place(false, true); @@ -27,16 +27,16 @@ abstract class RailBaseBlock extends FlowableBlock //TODO move some methods here } return false; } - + public function updateState(){} - + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ [$id, $meta] = $level->level->getBlock($x, $y, $z); if($id === POWERED_RAIL){ $meta &= 7; } if( - ($level->level->getBlockID($x, $y - 1, $z) === 0) || + ($level->level->getBlockID($x, $y - 1, $z) === 0) || (($meta == 2) && $level->level->getBlockID($x + 1, $y, $z) === 0) || (($meta == 3) && $level->level->getBlockID($x - 1, $y, $z) === 0) || (($meta == 4) && $level->level->getBlockID($x, $y, $z - 1) === 0) || @@ -45,7 +45,7 @@ abstract class RailBaseBlock extends FlowableBlock //TODO move some methods here $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); ServerAPI::request()->api->entity->drop(new Position($x, $y, $z, $level), BlockAPI::getItem($id, $meta, 1)); }else{ - //TODO fix me pls $this->updateState(); + //TODO fix me pls $this->updateState(); } } } diff --git a/src/material/block/nonfull/RailBlock.php b/src/material/block/nonfull/RailBlock.php index 9d63e231..b129d8af 100644 --- a/src/material/block/nonfull/RailBlock.php +++ b/src/material/block/nonfull/RailBlock.php @@ -1,19 +1,20 @@ hardness = 0.7; - $this->isFullBlock = false; + $this->isFullBlock = false; $this->isSolid = false; } - + public function updateState(){ $logic = (new RailLogic($this)); if($logic->countPotentialConnections() == 3){ $logic->place(false, false); } } - + } \ No newline at end of file diff --git a/src/material/block/nonfull/SlabBlock.php b/src/material/block/nonfull/SlabBlock.php index 3319bb7b..9a6cec6d 100644 --- a/src/material/block/nonfull/SlabBlock.php +++ b/src/material/block/nonfull/SlabBlock.php @@ -12,19 +12,19 @@ class SlabBlock extends TransparentBlock{ 6 => "Quartz", 7 => "", ]; - + public function __construct($meta = 0){ parent::__construct(SLAB, $meta, "Slab"); - $this->name = (($this->meta & 0x08) === 0x08 ? "Upper ":"") . self::$NAMES[$this->meta & 0x07] . " Slab"; + $this->name = (($this->meta & 0x08) === 0x08 ? "Upper ":"") . self::$NAMES[$this->meta & 0x07] . " Slab"; if(($this->meta & 0x08) === 0x08){ $this->isFullBlock = true; }else{ $this->isFullBlock = false; - } + } $this->hardness = 30; } public static function updateShape(Level $level, $x, $y, $z){ - + [$id, $meta] = $level->level->getBlock($x, $y, $z); if($meta & 0x08 == 0x08){ @@ -35,11 +35,11 @@ class SlabBlock extends TransparentBlock{ else StaticBlock::setBlockBounds($id, 0, 0.5, 0, 1, 1, 1); } } - + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ self::updateShape($level, $x, $y, $z); $id = $level->level->getBlockID($x, $y, $z); - + return [ new AxisAlignedBB( $x + StaticBlock::$minXs[$id], $y + StaticBlock::$minYs[$id], $z + StaticBlock::$minZs[$id], @@ -47,7 +47,7 @@ class SlabBlock extends TransparentBlock{ ) ]; } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $this->meta &= 0x07; if($face === 0){ @@ -103,14 +103,14 @@ class SlabBlock extends TransparentBlock{ default => 10, }; } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 1){ - return array( - array($this->id, $this->meta & 0x07, 1), - ); + return [ + [$this->id, $this->meta & 0x07, 1], + ]; }else{ - return array(); + return []; } } } diff --git a/src/material/block/nonfull/SnowLayerBlock.php b/src/material/block/nonfull/SnowLayerBlock.php index 3e9961a3..16807094 100644 --- a/src/material/block/nonfull/SnowLayerBlock.php +++ b/src/material/block/nonfull/SnowLayerBlock.php @@ -20,20 +20,20 @@ class SnowLayerBlock extends FlowableBlock{ } return false; } - + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ if($level->level->getBlockID($x, $y - 1, $z) == AIR){ //Replace with common break method $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); } } - + public function getDrops(Item $item, Player $player){ if($item->isShovel() !== false){ - return array( - array(SNOWBALL, 0, 1), - ); + return [ + [SNOWBALL, 0, 1], + ]; } - - return array(); + + return []; } } \ No newline at end of file diff --git a/src/material/block/nonfull/StoneWallBlock.php b/src/material/block/nonfull/StoneWallBlock.php index 730bc480..16f41faa 100644 --- a/src/material/block/nonfull/StoneWallBlock.php +++ b/src/material/block/nonfull/StoneWallBlock.php @@ -15,45 +15,45 @@ class StoneWallBlock extends TransparentBlock{ } return []; } - + public static function connectsTo(Level $level, $x, $y, $z){ $id = $level->level->getBlockID($x, $y, $z); - + if($id == STONE_WALL || $id == FENCE_GATE) return true; if($id == 0) return false; - + if(StaticBlock::getIsSolid($id) && !StaticBlock::getIsTransparent($id)){ //XXX in vanilla it uses Material->isSolidBlocking() and Tile->isCubeShaped() return true; //XXX in vanilla it returns v7->material != Material::vegetable; } return false; } - + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ return [static::getAABB($level, $x, $y, $z)]; } - + public static function getAABB(Level $level, $x, $y, $z){ static::updateShape($level, $x, $y, $z); StaticBlock::$maxYs[$level->level->getBlockID($x, $y, $z)] = 1.5; return parent::getAABB($level, $x, $y, $z); } - + public static function updateShape(Level $level, $x, $y, $z){ $zNeg = self::connectsTo($level, $x, $y, $z - 1); $zPos = self::connectsTo($level, $x, $y, $z + 1); $xNeg = self::connectsTo($level, $x - 1, $y, $z); $xPos = self::connectsTo($level, $x + 1, $y, $z); - + $minX = 0.25; $maxY = 1.0; $maxX = 0.75; - + $minZ = $zNeg ? 0 : 0.25; $maxZ = $zPos ? 1.0 : 0.75; - + if($xNeg) $minX = 0; if($xPos) $maxX = 1.0; - + if($zNeg){ if($zPos && !$xNeg){ if(!$xPos){ @@ -62,7 +62,7 @@ class StoneWallBlock extends TransparentBlock{ $minX = 0.3125; } } - }else if(!$zPos && $zNeg){ + }elseif(!$zPos && $zNeg){ if($xPos){ $maxY = 0.8125; $maxZ = 0.6875; diff --git a/src/material/block/nonfull/WoodDoorBlock.php b/src/material/block/nonfull/WoodDoorBlock.php index 7109a6b6..43c5827f 100644 --- a/src/material/block/nonfull/WoodDoorBlock.php +++ b/src/material/block/nonfull/WoodDoorBlock.php @@ -7,10 +7,10 @@ class WoodDoorBlock extends DoorBlock{ $this->isActivable = true; $this->hardness = 15; } - + public function getDrops(Item $item, Player $player){ - return array( - array(WOODEN_DOOR, 0, 1), - ); + return [ + [WOODEN_DOOR, 0, 1], + ]; } } diff --git a/src/material/block/nonfull/WoodSlabBlock.php b/src/material/block/nonfull/WoodSlabBlock.php index d39f0931..e2f6c84e 100644 --- a/src/material/block/nonfull/WoodSlabBlock.php +++ b/src/material/block/nonfull/WoodSlabBlock.php @@ -1,28 +1,29 @@ "Oak", 1 => "Spruce", 2 => "Birch", 3 => "Jungle", - ); - $this->name = (($this->meta & 0x08) === 0x08 ? "Upper ":"") . $names[$this->meta & 0x07] . " Wooden Slab"; + ]; + $this->name = (($this->meta & 0x08) === 0x08 ? "Upper ":"") . $names[$this->meta & 0x07] . " Wooden Slab"; if(($this->meta & 0x08) === 0x08){ $this->isFullBlock = true; }else{ $this->isFullBlock = false; - } + } $this->hardness = 15; } - + public static function updateShape(Level $level, $x, $y, $z){ - + [$id, $meta] = $level->level->getBlock($x, $y, $z); - + if($meta & 0x08 == 0x08){ StaticBlock::setBlockBounds($id, 0, 0, 0, 1, 1, 1); }else{ @@ -31,11 +32,11 @@ class WoodSlabBlock extends TransparentBlock{ else StaticBlock::setBlockBounds($id, 0, 0.5, 0, 1, 1, 1); } } - + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ self::updateShape($level, $x, $y, $z); $id = $level->level->getBlockID($x, $y, $z); - + return [ new AxisAlignedBB( $x + StaticBlock::$minXs[$id], $y + StaticBlock::$minYs[$id], $z + StaticBlock::$minZs[$id], @@ -43,7 +44,7 @@ class WoodSlabBlock extends TransparentBlock{ ) ]; } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $this->meta &= 0x07; if($face === 0){ @@ -99,10 +100,10 @@ class WoodSlabBlock extends TransparentBlock{ default => 3, }; } - + public function getDrops(Item $item, Player $player){ - return array( - array($this->id, $this->meta & 0x07, 1), - ); + return [ + [$this->id, $this->meta & 0x07, 1], + ]; } } diff --git a/src/material/block/nonfull/stairs/BirchWoodStairsBlock.php b/src/material/block/nonfull/stairs/BirchWoodStairsBlock.php index 619208b7..5cebc8dd 100644 --- a/src/material/block/nonfull/stairs/BirchWoodStairsBlock.php +++ b/src/material/block/nonfull/stairs/BirchWoodStairsBlock.php @@ -7,8 +7,8 @@ class BirchWoodStairsBlock extends StairBlock{ } public function getDrops(Item $item, Player $player){ - return array( - array($this->id, 0, 1), - ); + return [ + [$this->id, 0, 1], + ]; } } \ No newline at end of file diff --git a/src/material/block/nonfull/stairs/BrickStairsBlock.php b/src/material/block/nonfull/stairs/BrickStairsBlock.php index 133a5f0b..52ee5a7f 100644 --- a/src/material/block/nonfull/stairs/BrickStairsBlock.php +++ b/src/material/block/nonfull/stairs/BrickStairsBlock.php @@ -5,5 +5,5 @@ class BrickStairsBlock extends StairBlock{ public function __construct($meta = 0){ parent::__construct(BRICK_STAIRS, $meta, "Brick Stairs"); } - + } \ No newline at end of file diff --git a/src/material/block/nonfull/stairs/CobblestoneStairsBlock.php b/src/material/block/nonfull/stairs/CobblestoneStairsBlock.php index 54d1710a..dbe84da3 100644 --- a/src/material/block/nonfull/stairs/CobblestoneStairsBlock.php +++ b/src/material/block/nonfull/stairs/CobblestoneStairsBlock.php @@ -5,5 +5,5 @@ class CobblestoneStairsBlock extends StairBlock{ public function __construct($meta = 0){ parent::__construct(COBBLESTONE_STAIRS, $meta, "Cobblestone Stairs"); } - + } \ No newline at end of file diff --git a/src/material/block/nonfull/stairs/JungleWoodStairsBlock.php b/src/material/block/nonfull/stairs/JungleWoodStairsBlock.php index 76c4bc68..c466fda2 100644 --- a/src/material/block/nonfull/stairs/JungleWoodStairsBlock.php +++ b/src/material/block/nonfull/stairs/JungleWoodStairsBlock.php @@ -7,8 +7,8 @@ class JungleWoodStairsBlock extends StairBlock{ } public function getDrops(Item $item, Player $player){ - return array( - array($this->id, 0, 1), - ); + return [ + [$this->id, 0, 1], + ]; } } \ No newline at end of file diff --git a/src/material/block/nonfull/stairs/NetherBricksStairsBlock.php b/src/material/block/nonfull/stairs/NetherBricksStairsBlock.php index 0b984590..d798d1e5 100644 --- a/src/material/block/nonfull/stairs/NetherBricksStairsBlock.php +++ b/src/material/block/nonfull/stairs/NetherBricksStairsBlock.php @@ -5,5 +5,5 @@ class NetherBricksStairsBlock extends StairBlock{ public function __construct($meta = 0){ parent::__construct(NETHER_BRICKS_STAIRS, $meta, "Nether Bricks Stairs"); } - + } \ No newline at end of file diff --git a/src/material/block/nonfull/stairs/QuartzStairsBlock.php b/src/material/block/nonfull/stairs/QuartzStairsBlock.php index 062b60d0..b9f7ac8d 100644 --- a/src/material/block/nonfull/stairs/QuartzStairsBlock.php +++ b/src/material/block/nonfull/stairs/QuartzStairsBlock.php @@ -5,5 +5,5 @@ class QuartzStairsBlock extends StairBlock{ public function __construct($meta = 0){ parent::__construct(QUARTZ_STAIRS, $meta, "Quartz Stairs"); } - + } \ No newline at end of file diff --git a/src/material/block/nonfull/stairs/SandstoneStairsBlock.php b/src/material/block/nonfull/stairs/SandstoneStairsBlock.php index 8512e628..89e44b6d 100644 --- a/src/material/block/nonfull/stairs/SandstoneStairsBlock.php +++ b/src/material/block/nonfull/stairs/SandstoneStairsBlock.php @@ -5,5 +5,5 @@ class SandstoneStairsBlock extends StairBlock{ public function __construct($meta = 0){ parent::__construct(SANDSTONE_STAIRS, $meta, "Sandstone Stairs"); } - + } \ No newline at end of file diff --git a/src/material/block/nonfull/stairs/SpruceWoodStairsBlock.php b/src/material/block/nonfull/stairs/SpruceWoodStairsBlock.php index f001a888..8468e0e2 100644 --- a/src/material/block/nonfull/stairs/SpruceWoodStairsBlock.php +++ b/src/material/block/nonfull/stairs/SpruceWoodStairsBlock.php @@ -7,8 +7,8 @@ class SpruceWoodStairsBlock extends StairBlock{ } public function getDrops(Item $item, Player $player){ - return array( - array($this->id, 0, 1), - ); + return [ + [$this->id, 0, 1], + ]; } } \ No newline at end of file diff --git a/src/material/block/nonfull/stairs/StoneBrickStairsBlock.php b/src/material/block/nonfull/stairs/StoneBrickStairsBlock.php index b43efbb7..0a13b42b 100644 --- a/src/material/block/nonfull/stairs/StoneBrickStairsBlock.php +++ b/src/material/block/nonfull/stairs/StoneBrickStairsBlock.php @@ -5,5 +5,5 @@ class StoneBrickStairsBlock extends StairBlock{ public function __construct($meta = 0){ parent::__construct(STONE_BRICK_STAIRS, $meta, "Stone Brick Stairs"); } - + } \ No newline at end of file diff --git a/src/material/block/nonfull/stairs/WoodStairsBlock.php b/src/material/block/nonfull/stairs/WoodStairsBlock.php index 0421dac4..d3cc9e10 100644 --- a/src/material/block/nonfull/stairs/WoodStairsBlock.php +++ b/src/material/block/nonfull/stairs/WoodStairsBlock.php @@ -21,8 +21,8 @@ class WoodStairsBlock extends StairBlock{ } public function getDrops(Item $item, Player $player){ - return array( - array($this->id, 0, 1), - ); + return [ + [$this->id, 0, 1], + ]; } } diff --git a/src/material/block/ore/CoalOreBlock.php b/src/material/block/ore/CoalOreBlock.php index 12e5c262..8bc6ce59 100644 --- a/src/material/block/ore/CoalOreBlock.php +++ b/src/material/block/ore/CoalOreBlock.php @@ -6,7 +6,7 @@ class CoalOreBlock extends SolidBlock{ parent::__construct(COAL_ORE, 0, "Coal Ore"); $this->hardness = 15; } - + public function getBreakTime(Item $item, Player $player){ if(($player->gamemode & 0x01) === 0x01){ return 0.20; @@ -20,15 +20,15 @@ class CoalOreBlock extends SolidBlock{ default => 15, }; } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 1){ - return array( - array(COAL, 0, 1), - ); + return [ + [COAL, 0, 1], + ]; }else{ - return array(); + return []; } } - + } diff --git a/src/material/block/ore/DiamondOreBlock.php b/src/material/block/ore/DiamondOreBlock.php index dec08e78..8b4fc682 100644 --- a/src/material/block/ore/DiamondOreBlock.php +++ b/src/material/block/ore/DiamondOreBlock.php @@ -6,7 +6,7 @@ class DiamondOreBlock extends SolidBlock{ parent::__construct(DIAMOND_ORE, 0, "Diamond Ore"); $this->hardness = 15; } - + public function getBreakTime(Item $item, Player $player){ if(($player->gamemode & 0x01) === 0x01){ return 0.20; @@ -17,14 +17,14 @@ class DiamondOreBlock extends SolidBlock{ default => 15, }; } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 4){ - return array( - array(DIAMOND, 0, 1), - ); + return [ + [DIAMOND, 0, 1], + ]; }else{ - return array(); + return []; } } } diff --git a/src/material/block/ore/GlowingRedstoneOreBlock.php b/src/material/block/ore/GlowingRedstoneOreBlock.php index 33dd8e0c..ed9ad90e 100644 --- a/src/material/block/ore/GlowingRedstoneOreBlock.php +++ b/src/material/block/ore/GlowingRedstoneOreBlock.php @@ -6,11 +6,11 @@ class GlowingRedstoneOreBlock extends SolidBlock implements LightingBlock{ parent::__construct(GLOWING_REDSTONE_ORE, 0, "Glowing Redstone Ore"); $this->hardness = 15; } - + public static function onRandomTick(Level $level, $x, $y, $z){ $level->fastSetBlockUpdate($x, $y, $z, REDSTONE_ORE, 0); } - + public function getMaxLightValue(){ return 9; } @@ -25,15 +25,15 @@ class GlowingRedstoneOreBlock extends SolidBlock implements LightingBlock{ default => 15, }; } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 4){ - return array( - array(REDSTONE_DUST, 0, mt_rand(4, 5)), - ); + return [ + [REDSTONE_DUST, 0, mt_rand(4, 5)], + ]; }else{ - return array(); + return []; } } - + } diff --git a/src/material/block/ore/GoldOreBlock.php b/src/material/block/ore/GoldOreBlock.php index c835dbc1..2de507f6 100644 --- a/src/material/block/ore/GoldOreBlock.php +++ b/src/material/block/ore/GoldOreBlock.php @@ -17,14 +17,14 @@ class GoldOreBlock extends SolidBlock{ default => 15, }; } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 4){ - return array( - array(GOLD_ORE, 0, 1), - ); + return [ + [GOLD_ORE, 0, 1], + ]; }else{ - return array(); + return []; } } } diff --git a/src/material/block/ore/IronOreBlock.php b/src/material/block/ore/IronOreBlock.php index d8d001ff..abb023de 100644 --- a/src/material/block/ore/IronOreBlock.php +++ b/src/material/block/ore/IronOreBlock.php @@ -18,14 +18,14 @@ class IronOreBlock extends SolidBlock{ default => 15, }; } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= ItemTool::STONE_LEVEL){ - return array( - array(IRON_ORE, 0, 1), - ); + return [ + [IRON_ORE, 0, 1], + ]; }else{ - return array(); + return []; } } } diff --git a/src/material/block/ore/LapisOreBlock.php b/src/material/block/ore/LapisOreBlock.php index ecb3e83f..5130db86 100644 --- a/src/material/block/ore/LapisOreBlock.php +++ b/src/material/block/ore/LapisOreBlock.php @@ -6,7 +6,7 @@ class LapisOreBlock extends SolidBlock{ parent::__construct(LAPIS_ORE, 0, "Lapis Ore"); $this->hardness = 15; } - + public function getBreakTime(Item $item, Player $player){ if(($player->gamemode & 0x01) === 0x01){ return 0.20; @@ -21,12 +21,12 @@ class LapisOreBlock extends SolidBlock{ public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 3){ - return array( - array(DYE, 4, mt_rand(4, 8)), - ); + return [ + [DYE, 4, mt_rand(4, 8)], + ]; }else{ - return array(); + return []; } } - + } diff --git a/src/material/block/ore/RedstoneOreBlock.php b/src/material/block/ore/RedstoneOreBlock.php index 471f87ae..dfe5c77e 100644 --- a/src/material/block/ore/RedstoneOreBlock.php +++ b/src/material/block/ore/RedstoneOreBlock.php @@ -6,18 +6,18 @@ class RedstoneOreBlock extends SolidBlock{ parent::__construct(REDSTONE_ORE, 0, "Redstone Ore"); $this->hardness = 15; } - + public static function interact(Level $level, $x, $y, $z, Player $player){ $level->fastSetBlockUpdate($x, $y, $z, GLOWING_REDSTONE_ORE, 0); } public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 4){ - return array( - array(REDSTONE_DUST, 0, mt_rand(4, 5)), - ); + return [ + [REDSTONE_DUST, 0, mt_rand(4, 5)], + ]; }else{ - return array(); + return []; } } } \ No newline at end of file diff --git a/src/material/block/plant/BeetrootBlock.php b/src/material/block/plant/BeetrootBlock.php index c1b51d73..423ed15f 100644 --- a/src/material/block/plant/BeetrootBlock.php +++ b/src/material/block/plant/BeetrootBlock.php @@ -46,7 +46,7 @@ class BeetrootBlock extends FlowableBlock{ $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } } - + public function getDrops(Item $item, Player $player){ $drops = []; if($this->meta >= 0x07){ diff --git a/src/material/block/plant/BrownMushroomBlock.php b/src/material/block/plant/BrownMushroomBlock.php index 495d8a73..aae803a7 100644 --- a/src/material/block/plant/BrownMushroomBlock.php +++ b/src/material/block/plant/BrownMushroomBlock.php @@ -9,11 +9,11 @@ class BrownMushroomBlock extends FlowableBlock{ public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ if(StaticBlock::getIsTransparent($level->level->getBlockID($x, $y - 1, $z))){ //Replace with common break method - ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(BROWN_MUSHROOM)); + ServerAPI::request()->api->entity->drop(new Position($x + 0.5, $y, $z + 0.5, $level), BlockAPI::getItem(BROWN_MUSHROOM)); $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $down = $this->getSide(0); if($down->isTransparent === false){ @@ -21,5 +21,5 @@ class BrownMushroomBlock extends FlowableBlock{ return true; } return false; - } + } } \ No newline at end of file diff --git a/src/material/block/plant/CactusBlock.php b/src/material/block/plant/CactusBlock.php index 4dd97196..debb3dc2 100644 --- a/src/material/block/plant/CactusBlock.php +++ b/src/material/block/plant/CactusBlock.php @@ -7,14 +7,14 @@ class CactusBlock extends TransparentBlock{ $this->isFullBlock = false; $this->hardness = 2; } - + public static function onEntityCollidedWithBlock(Level $level, $x, $y, $z, Entity $entity){ $entity->harm(1, "cactus"); } public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ return [new AxisAlignedBB($x + 0.0625, $y, $z + 0.0625, $x + 1 - 0.0625, $y + 1 - 0.0625, $z + 1 - 0.0625)]; } - + public static function onRandomTick(Level $level, $x, $y, $z){ //$b = $level->level->getBlock($x, $y - 1, $z); $underID = $level->level->getBlockID($x, $y - 1, $z); @@ -38,7 +38,7 @@ class CactusBlock extends TransparentBlock{ return BLOCK_UPDATE_RANDOM; } } - + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ $down = $level->level->getBlockID($x, $y - 1, $z); $b0 = $level->level->getBlockID($x, $y, $z - 1); @@ -50,7 +50,7 @@ class CactusBlock extends TransparentBlock{ ServerAPI::request()->api->entity->drop(new Position($x + 0.5, $y, $z + 0.5, $level), BlockAPI::getItem(CACTUS)); } } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $down = $this->getSide(0); if($down->getID() === SAND or $down->getID() === CACTUS){ @@ -67,10 +67,10 @@ class CactusBlock extends TransparentBlock{ } return false; } - + public function getDrops(Item $item, Player $player){ - return array( - array($this->id, 0, 1), - ); + return [ + [$this->id, 0, 1], + ]; } } \ No newline at end of file diff --git a/src/material/block/plant/CarrotBlock.php b/src/material/block/plant/CarrotBlock.php index 498399f4..b7fe8619 100644 --- a/src/material/block/plant/CarrotBlock.php +++ b/src/material/block/plant/CarrotBlock.php @@ -17,7 +17,7 @@ class CarrotBlock extends FlowableBlock{ } return false; } - + public function onActivate(Item $item, Player $player){ if($item->getID() === DYE and $item->getMetadata() === 0x0F){ //Bonemeal $this->meta += mt_rand(0, 3) + 2; @@ -48,7 +48,7 @@ class CarrotBlock extends FlowableBlock{ $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } } - + public function getDrops(Item $item, Player $player){ $drops = []; if($this->meta >= 0x07){ diff --git a/src/material/block/plant/CyanFlowerBlock.php b/src/material/block/plant/CyanFlowerBlock.php index 8486ede6..60de9639 100644 --- a/src/material/block/plant/CyanFlowerBlock.php +++ b/src/material/block/plant/CyanFlowerBlock.php @@ -7,7 +7,7 @@ class CyanFlowerBlock extends FlowableBlock{ $this->isActivable = true; $this->hardness = 0; } - + public static function getAABB(Level $level, $x, $y, $z){ return null; } @@ -21,17 +21,14 @@ class CyanFlowerBlock extends FlowableBlock{ return false; } - public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ $downId = $level->level->getBlockID($x, $y - 1, $z); if(StaticBlock::getIsTransparent($downId) and $downId !== FARMLAND){ //Replace with common break method - ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(CYAN_FLOWER)); + ServerAPI::request()->api->entity->drop(new Position($x + 0.5, $y, $z + 0.5, $level), BlockAPI::getItem(CYAN_FLOWER)); $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } } - - public function onActivate(Item $item, Player $player){ if($item->getID() === DYE and $item->getMetadata() === 0x0F){ if(($player->gamemode & 0x01) === 0){ diff --git a/src/material/block/plant/DandelionBlock.php b/src/material/block/plant/DandelionBlock.php index 57f5ef64..69f39eef 100644 --- a/src/material/block/plant/DandelionBlock.php +++ b/src/material/block/plant/DandelionBlock.php @@ -22,7 +22,7 @@ class DandelionBlock extends FlowableBlock{ public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ $downId = $level->level->getBlockID($x, $y - 1, $z); if(StaticBlock::getIsTransparent($downId) and $downId !== FARMLAND){ //Replace with common break method - ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(CYAN_FLOWER)); + ServerAPI::request()->api->entity->drop(new Position($x + 0.5, $y, $z + 0.5, $level), BlockAPI::getItem(CYAN_FLOWER)); $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } } diff --git a/src/material/block/plant/DeadBushBlock.php b/src/material/block/plant/DeadBushBlock.php index 4f7d49b7..b3be7cdc 100644 --- a/src/material/block/plant/DeadBushBlock.php +++ b/src/material/block/plant/DeadBushBlock.php @@ -7,33 +7,33 @@ class DeadBushBlock extends FlowableBlock{ //$this->isReplaceable = true; $this->hardness = 0; } - + public static function getAABB(Level $level, $x, $y, $z){ return null; } - + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ if(StaticBlock::getIsTransparent($level->level->getBlockID($x, $y - 1, $z))){ //Replace with common break method $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $down = $this->getSide(0); if($down->getID() == 12){ $this->level->setBlock($block, $this, true, false, true); return true; - } + } return false; } - + public function getDrops(Item $item, Player $player){ if($item->isShears()){ return [ [DEAD_BUSH, 0, 1], ]; } - + return []; } } \ No newline at end of file diff --git a/src/material/block/plant/MelonStemBlock.php b/src/material/block/plant/MelonStemBlock.php index a9e3c542..4dee856f 100644 --- a/src/material/block/plant/MelonStemBlock.php +++ b/src/material/block/plant/MelonStemBlock.php @@ -24,7 +24,7 @@ class MelonStemBlock extends FlowableBlock{ //$this->level->setBlock($this, $this, true, false, true); $level->fastSetBlockUpdate($x, $y, $z, $block[0], $block[1] + 1); }else{ - + $position = new AirBlock(); //feke block $position->x = $x; $position->y = $y; @@ -46,11 +46,11 @@ class MelonStemBlock extends FlowableBlock{ } public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ if($level->level->getBlockID($x, $y - 1, $z) != FARMLAND){ - ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(MELON_SEEDS, 0, mt_rand(0, 2))); + ServerAPI::request()->api->entity->drop(new Position($x + 0.5, $y, $z + 0.5, $level), BlockAPI::getItem(MELON_SEEDS, 0, mt_rand(0, 2))); $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } } - + public function onActivate(Item $item, Player $player){ if($item->getID() === DYE and $item->getMetadata() === 0x0F){ //Bonemeal $this->meta += mt_rand(0, 3) + 2; @@ -65,7 +65,7 @@ class MelonStemBlock extends FlowableBlock{ } return false; } - + public function getDrops(Item $item, Player $player){ $drops = []; if($this->meta >= 0x07){ diff --git a/src/material/block/plant/PotatoBlock.php b/src/material/block/plant/PotatoBlock.php index 3ab969fe..62d44ac6 100644 --- a/src/material/block/plant/PotatoBlock.php +++ b/src/material/block/plant/PotatoBlock.php @@ -48,7 +48,7 @@ class PotatoBlock extends FlowableBlock{ $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } } - + public function getDrops(Item $item, Player $player){ $drops = []; if($this->meta >= 0x07){ diff --git a/src/material/block/plant/PumpkinStemBlock.php b/src/material/block/plant/PumpkinStemBlock.php index c7492623..00eed1cb 100644 --- a/src/material/block/plant/PumpkinStemBlock.php +++ b/src/material/block/plant/PumpkinStemBlock.php @@ -24,7 +24,7 @@ class PumpkinStemBlock extends FlowableBlock{ //$this->level->setBlock($this, $this, true, false, true); $level->fastSetBlockUpdate($x, $y, $z, $block[0], $block[1] + 1); }else{ - + $position = new AirBlock(); //feke block $position->x = $x; $position->y = $y; @@ -46,11 +46,11 @@ class PumpkinStemBlock extends FlowableBlock{ } public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ if($level->level->getBlockID($x, $y - 1, $z) != FARMLAND){ - ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(PUMPKIN_SEEDS, 0, mt_rand(0, 2))); + ServerAPI::request()->api->entity->drop(new Position($x + 0.5, $y, $z + 0.5, $level), BlockAPI::getItem(PUMPKIN_SEEDS, 0, mt_rand(0, 2))); $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } } - + public function onActivate(Item $item, Player $player){ if($item->getID() === DYE and $item->getMetadata() === 0x0F){ //Bonemeal $this->meta += mt_rand(0, 3) + 2; @@ -65,7 +65,7 @@ class PumpkinStemBlock extends FlowableBlock{ } return false; } - + public function getDrops(Item $item, Player $player){ $drops = []; if($this->meta >= 0x07){ diff --git a/src/material/block/plant/RedMushroomBlock.php b/src/material/block/plant/RedMushroomBlock.php index 29bdc225..3ba418b5 100644 --- a/src/material/block/plant/RedMushroomBlock.php +++ b/src/material/block/plant/RedMushroomBlock.php @@ -9,7 +9,7 @@ class RedMushroomBlock extends FlowableBlock{ public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ if(StaticBlock::getIsTransparent($level->level->getBlockID($x, $y - 1, $z))){ //Replace with common break method - ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(RED_MUSHROOM)); + ServerAPI::request()->api->entity->drop(new Position($x + 0.5, $y, $z + 0.5, $level), BlockAPI::getItem(RED_MUSHROOM)); $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } } @@ -21,5 +21,5 @@ class RedMushroomBlock extends FlowableBlock{ return true; } return false; - } + } } \ No newline at end of file diff --git a/src/material/block/plant/SaplingBlock.php b/src/material/block/plant/SaplingBlock.php index a6acde15..fd830844 100644 --- a/src/material/block/plant/SaplingBlock.php +++ b/src/material/block/plant/SaplingBlock.php @@ -7,16 +7,16 @@ class SaplingBlock extends FlowableBlock{ const BIRCH = 2; const JUNGLE = 3; const BURN_TIME = 5; - + public function __construct($meta = SaplingBlock::OAK){ parent::__construct(SAPLING, $meta, "Sapling"); $this->isActivable = true; - $names = array( + $names = [ 0 => "Oak Sapling", 1 => "Spruce Sapling", 2 => "Birch Sapling", 3 => "Jungle Sapling", - ); + ]; $this->name = $names[$this->meta & 0x03]; $this->hardness = 0; } @@ -32,7 +32,7 @@ class SaplingBlock extends FlowableBlock{ } return false; } - + public function onActivate(Item $item, Player $player){ if($item->getID() === DYE and $item->getMetadata() === 0x0F){ //Bonemeal TreeObject::growTree($this->level, $this, new Random(), $this->meta & 0x03); @@ -47,7 +47,7 @@ class SaplingBlock extends FlowableBlock{ $downID = $level->level->getBlockID($x, $y - 1, $z); if(StaticBlock::getIsTransparent($downID) && $downID !== FARMLAND){ //Replace with common break method [$id, $meta] = $level->level->getBlock($x, $y, $z); - ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem($id, $meta)); + ServerAPI::request()->api->entity->drop(new Position($x + 0.5, $y, $z + 0.5, $level), BlockAPI::getItem($id, $meta)); $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } } @@ -64,8 +64,8 @@ class SaplingBlock extends FlowableBlock{ } } public function getDrops(Item $item, Player $player){ - return array( - array($this->id, $this->meta & 0x03, 1), - ); + return [ + [$this->id, $this->meta & 0x03, 1], + ]; } } diff --git a/src/material/block/plant/SugarcaneBlock.php b/src/material/block/plant/SugarcaneBlock.php index d4928460..660f1393 100644 --- a/src/material/block/plant/SugarcaneBlock.php +++ b/src/material/block/plant/SugarcaneBlock.php @@ -7,7 +7,7 @@ class SugarcaneBlock extends FlowableBlock{ $this->isActivable = true; $this->hardness = 0; } - + public function getDrops(Item $item, Player $player){ return [ [SUGARCANE, 0, 1], @@ -28,7 +28,7 @@ class SugarcaneBlock extends FlowableBlock{ $this->meta = 0; $this->level->fastSetBlockUpdateMeta($this->x, $this->y, $this->z, $this->meta, true); } - + if(($player->gamemode & 0x01) === 0){ $player->removeItem(DYE,0x0F,1); } @@ -50,7 +50,7 @@ class SugarcaneBlock extends FlowableBlock{ } } if(StaticBlock::getIsTransparent($down) && $down != SUGARCANE_BLOCK){ //Replace with common break method - ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(SUGARCANE)); + ServerAPI::request()->api->entity->drop(new Position($x + 0.5, $y, $z + 0.5, $level), BlockAPI::getItem(SUGARCANE)); $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); } } @@ -80,7 +80,7 @@ class SugarcaneBlock extends FlowableBlock{ $x = $this->x; $y = $this->y; $z = $this->z; - + $b0 = $level->level->getBlockID($x, $y - 1, $z - 1); $b1 = $level->level->getBlockID($x, $y - 1, $z + 1); $b2 = $level->level->getBlockID($x - 1, $y - 1, $z); diff --git a/src/material/block/plant/TallGrassBlock.php b/src/material/block/plant/TallGrassBlock.php index df8ce39f..94ec709d 100644 --- a/src/material/block/plant/TallGrassBlock.php +++ b/src/material/block/plant/TallGrassBlock.php @@ -5,11 +5,11 @@ class TallGrassBlock extends FlowableBlock{ public function __construct($meta = 1){ parent::__construct(TALL_GRASS, $meta, "Tall Grass"); $this->isReplaceable = true; - $names = array( + $names = [ 0 => "Dead Shrub", 1 => "Tall Grass", 2 => "Fern", - ); + ]; $this->name = $names[$this->meta & 0x03]; $this->hardness = 0; } @@ -17,28 +17,28 @@ class TallGrassBlock extends FlowableBlock{ public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ $downID = $level->level->getBlockID($x, $y - 1, $z); if(StaticBlock::getIsTransparent($downID) && $downID !== FARMLAND){ //Replace with common break method - if(Utils::chance(15)) ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(WHEAT_SEEDS)); + if(Utils::chance(15)) ServerAPI::request()->api->entity->drop(new Position($x + 0.5, $y, $z + 0.5, $level), BlockAPI::getItem(WHEAT_SEEDS)); $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } } - + public static function getAABB(Level $level, $x, $y, $z){ return null; } - + public function getDrops(Item $item, Player $player){ - $drops = array(); - if($item->isShears()) $drops[] = array($this->id, $this->meta & 0x03, 1); - elseif(Utils::chance(15)) $drops[] = array(WHEAT_SEEDS, 0, 1); + $drops = []; + if($item->isShears()) $drops[] = [$this->id, $this->meta & 0x03, 1]; + elseif(Utils::chance(15)) $drops[] = [WHEAT_SEEDS, 0, 1]; return $drops; } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $down = $this->getSide(0); if($down->getID() == GRASS or $down->getID() == DIRT || $down->getID() == FARMLAND){ $this->level->setBlock($block, $this, true, false, true); return true; - } + } return false; } diff --git a/src/material/block/plant/WheatBlock.php b/src/material/block/plant/WheatBlock.php index 53bf67f4..36f8d247 100644 --- a/src/material/block/plant/WheatBlock.php +++ b/src/material/block/plant/WheatBlock.php @@ -17,7 +17,7 @@ class WheatBlock extends FlowableBlock{ } return false; } - + public function onActivate(Item $item, Player $player){ if($item->getID() === DYE and $item->getMetadata() === 0x0F){ //Bonemeal $this->meta += mt_rand(0, 3) + 2; @@ -48,7 +48,7 @@ class WheatBlock extends FlowableBlock{ $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } } - + public function getDrops(Item $item, Player $player){ $drops = []; if($this->meta >= 0x07){ @@ -59,7 +59,7 @@ class WheatBlock extends FlowableBlock{ $drops[] = [WHEAT_SEEDS, 0, 1]; } } - + return $drops; } } \ No newline at end of file diff --git a/src/material/block/solid/BedrockBlock.php b/src/material/block/solid/BedrockBlock.php index 8974deae..d214a6d8 100644 --- a/src/material/block/solid/BedrockBlock.php +++ b/src/material/block/solid/BedrockBlock.php @@ -7,12 +7,12 @@ class BedrockBlock extends SolidBlock{ $this->breakable = false; $this->hardness = 18000000; } - + public function isBreakable(Item $item, Player $player){ if(($player->gamemode & 0x01) === 0x01){ return true; } return false; } - + } \ No newline at end of file diff --git a/src/material/block/solid/BookshelfBlock.php b/src/material/block/solid/BookshelfBlock.php index 8f19b6fe..4e501fe0 100644 --- a/src/material/block/solid/BookshelfBlock.php +++ b/src/material/block/solid/BookshelfBlock.php @@ -6,5 +6,5 @@ class BookshelfBlock extends SolidBlock{ parent::__construct(BOOKSHELF, 0, "Bookshelf"); $this->hardness = 7.5; } - + } \ No newline at end of file diff --git a/src/material/block/solid/BricksBlock.php b/src/material/block/solid/BricksBlock.php index 97f448b2..653d398e 100644 --- a/src/material/block/solid/BricksBlock.php +++ b/src/material/block/solid/BricksBlock.php @@ -20,14 +20,14 @@ class BricksBlock extends SolidBlock{ default => 10, }; } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 1){ - return array( - array(BRICKS_BLOCK, 0, 1), - ); + return [ + [BRICKS_BLOCK, 0, 1], + ]; }else{ - return array(); + return []; } } } diff --git a/src/material/block/solid/BurningFurnaceBlock.php b/src/material/block/solid/BurningFurnaceBlock.php index 0adb007c..1cfc3039 100644 --- a/src/material/block/solid/BurningFurnaceBlock.php +++ b/src/material/block/solid/BurningFurnaceBlock.php @@ -9,12 +9,12 @@ class BurningFurnaceBlock extends SolidBlock implements LightingBlock{ } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - $faces = array( + $faces = [ 0 => 4, 1 => 2, 2 => 5, 3 => 3, - ); + ]; $this->meta = $faces[$player->entity->getDirection()]; $this->level->setBlock($block, $this, true, false, true); return true; @@ -35,23 +35,23 @@ class BurningFurnaceBlock extends SolidBlock implements LightingBlock{ if($t !== false){ $furnace = $t; }else{ - $furnace = $server->api->tile->add($this->level, TILE_FURNACE, $this->x, $this->y, $this->z, array( - "Items" => array(), + $furnace = $server->api->tile->add($this->level, TILE_FURNACE, $this->x, $this->y, $this->z, [ + "Items" => [], "id" => TILE_FURNACE, "x" => $this->x, "y" => $this->y, - "z" => $this->z - )); + "z" => $this->z + ]); } - + if(($player->gamemode & 0x01) === 0x01){ return true; } - + $furnace->openInventory($player); return true; } - + public function getBreakTime(Item $item, Player $player){ if(($player->gamemode & 0x01) === 0x01){ return 0.20; @@ -65,18 +65,18 @@ class BurningFurnaceBlock extends SolidBlock implements LightingBlock{ default => 17.5, }; } - + public function getDrops(Item $item, Player $player){ - $drops = array(); + $drops = []; if($item->getPickaxeLevel() >= 1){ - $drops[] = array(FURNACE, 0, 1); + $drops[] = [FURNACE, 0, 1]; } $t = ServerAPI::request()->api->tile->get($this); if($t !== false and $t->class === TILE_FURNACE){ for($s = 0; $s < FURNACE_SLOTS; ++$s){ $slot = $t->getSlot($s); if($slot->getID() > AIR and $slot->count > 0){ - $drops[] = array($slot->getID(), $slot->getMetadata(), $slot->count); + $drops[] = [$slot->getID(), $slot->getMetadata(), $slot->count]; } } } diff --git a/src/material/block/solid/ClayBlock.php b/src/material/block/solid/ClayBlock.php index 3ca8b2a4..7ce626de 100644 --- a/src/material/block/solid/ClayBlock.php +++ b/src/material/block/solid/ClayBlock.php @@ -8,8 +8,8 @@ class ClayBlock extends SolidBlock{ } public function getDrops(Item $item, Player $player){ - return array( - array(CLAY, 0, 4), - ); + return [ + [CLAY, 0, 4], + ]; } } \ No newline at end of file diff --git a/src/material/block/solid/CoalBlock.php b/src/material/block/solid/CoalBlock.php index cf7a252e..0995aa26 100644 --- a/src/material/block/solid/CoalBlock.php +++ b/src/material/block/solid/CoalBlock.php @@ -20,14 +20,14 @@ class CoalBlock extends SolidBlock{ default => 25, }; } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 1){ - return array( - array(COAL_BLOCK, 0, 1), - ); + return [ + [COAL_BLOCK, 0, 1], + ]; }else{ - return array(); + return []; } } } diff --git a/src/material/block/solid/CobblestoneBlock.php b/src/material/block/solid/CobblestoneBlock.php index 895953d1..0990c10d 100644 --- a/src/material/block/solid/CobblestoneBlock.php +++ b/src/material/block/solid/CobblestoneBlock.php @@ -20,14 +20,14 @@ class CobblestoneBlock extends SolidBlock{ default => 10, }; } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 1){ - return array( - array(COBBLESTONE, 0, 1), - ); + return [ + [COBBLESTONE, 0, 1], + ]; }else{ - return array(); + return []; } } } diff --git a/src/material/block/solid/DiamondBlock.php b/src/material/block/solid/DiamondBlock.php index 2ffeed39..7f073778 100644 --- a/src/material/block/solid/DiamondBlock.php +++ b/src/material/block/solid/DiamondBlock.php @@ -6,7 +6,7 @@ class DiamondBlock extends SolidBlock{ parent::__construct(DIAMOND_BLOCK, 0, "Diamond Block"); $this->hardness = 30; } - + public function getBreakTime(Item $item, Player $player){ if(($player->gamemode & 0x01) === 0x01){ return 0.20; @@ -17,14 +17,14 @@ class DiamondBlock extends SolidBlock{ default => 25, }; } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 4){ - return array( - array(DIAMOND_BLOCK, 0, 1), - ); + return [ + [DIAMOND_BLOCK, 0, 1], + ]; }else{ - return array(); + return []; } } } diff --git a/src/material/block/solid/DirtBlock.php b/src/material/block/solid/DirtBlock.php index bf96b7c2..64b43604 100644 --- a/src/material/block/solid/DirtBlock.php +++ b/src/material/block/solid/DirtBlock.php @@ -32,33 +32,33 @@ class DirtBlock extends SolidBlock{ } public static function getGrassInRadius(Level $level, $x, $y, $z){ //umwut (although kinda faster than for loop =D) - if($level->level->getBlockID($x+1, $y, $z+1) == 2) return true; - if($level->level->getBlockID($x+1, $y, $z) == 2) return true; - if($level->level->getBlockID($x+1, $y, $z-1) == 2) return true; - if($level->level->getBlockID($x, $y, $z+1) == 2) return true; - if($level->level->getBlockID($x, $y, $z-1) == 2) return true; - if($level->level->getBlockID($x-1, $y, $z+1) == 2) return true; - if($level->level->getBlockID($x-1, $y, $z) == 2) return true; - if($level->level->getBlockID($x-1, $y, $z-1) == 2) return true; + if($level->level->getBlockID($x + 1, $y, $z + 1) == 2) return true; + if($level->level->getBlockID($x + 1, $y, $z) == 2) return true; + if($level->level->getBlockID($x + 1, $y, $z - 1) == 2) return true; + if($level->level->getBlockID($x, $y, $z + 1) == 2) return true; + if($level->level->getBlockID($x, $y, $z - 1) == 2) return true; + if($level->level->getBlockID($x - 1, $y, $z + 1) == 2) return true; + if($level->level->getBlockID($x - 1, $y, $z) == 2) return true; + if($level->level->getBlockID($x - 1, $y, $z - 1) == 2) return true; - if($level->level->getBlockID($x+1, $y-1, $z+1) == 2) return true; - if($level->level->getBlockID($x+1, $y-1, $z) == 2) return true; - if($level->level->getBlockID($x+1, $y-1, $z-1) == 2) return true; - if($level->level->getBlockID($x, $y-1, $z+1) == 2) return true; - if($level->level->getBlockID($x, $y-1, $z) == 2) return true; - if($level->level->getBlockID($x, $y-1, $z-1) == 2) return true; - if($level->level->getBlockID($x-1, $y-1, $z+1) == 2) return true; - if($level->level->getBlockID($x-1, $y-1, $z) == 2) return true; - if($level->level->getBlockID($x-1, $y-1, $z-1) == 2) return true; + if($level->level->getBlockID($x + 1, $y - 1, $z + 1) == 2) return true; + if($level->level->getBlockID($x + 1, $y - 1, $z) == 2) return true; + if($level->level->getBlockID($x + 1, $y - 1, $z - 1) == 2) return true; + if($level->level->getBlockID($x, $y - 1, $z + 1) == 2) return true; + if($level->level->getBlockID($x, $y - 1, $z) == 2) return true; + if($level->level->getBlockID($x, $y - 1, $z - 1) == 2) return true; + if($level->level->getBlockID($x - 1, $y - 1, $z + 1) == 2) return true; + if($level->level->getBlockID($x - 1, $y - 1, $z) == 2) return true; + if($level->level->getBlockID($x - 1, $y - 1, $z - 1) == 2) return true; - if($level->level->getBlockID($x+1, $y+1, $z+1) == 2) return true; - if($level->level->getBlockID($x+1, $y+1, $z) == 2) return true; - if($level->level->getBlockID($x+1, $y+1, $z-1) == 2) return true; - if($level->level->getBlockID($x, $y+1, $z+1) == 2) return true; - if($level->level->getBlockID($x, $y+1, $z-1) == 2) return true; - if($level->level->getBlockID($x-1, $y+1, $z+1) == 2) return true; - if($level->level->getBlockID($x-1, $y+1, $z) == 2) return true; - if($level->level->getBlockID($x-1, $y+1, $z-1) == 2) return true; + if($level->level->getBlockID($x + 1, $y + 1, $z + 1) == 2) return true; + if($level->level->getBlockID($x + 1, $y + 1, $z) == 2) return true; + if($level->level->getBlockID($x + 1, $y + 1, $z - 1) == 2) return true; + if($level->level->getBlockID($x, $y + 1, $z + 1) == 2) return true; + if($level->level->getBlockID($x, $y + 1, $z - 1) == 2) return true; + if($level->level->getBlockID($x - 1, $y + 1, $z + 1) == 2) return true; + if($level->level->getBlockID($x - 1, $y + 1, $z) == 2) return true; + if($level->level->getBlockID($x - 1, $y + 1, $z - 1) == 2) return true; return false; } diff --git a/src/material/block/solid/DoubleSlabBlock.php b/src/material/block/solid/DoubleSlabBlock.php index 4b6f28db..8211f796 100755 --- a/src/material/block/solid/DoubleSlabBlock.php +++ b/src/material/block/solid/DoubleSlabBlock.php @@ -4,7 +4,7 @@ class DoubleSlabBlock extends SolidBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(DOUBLE_SLAB, $meta, "Double Slab"); - $names = array( + $names = [ 0 => "Stone", 1 => "Sandstone", 2 => "Wooden", @@ -13,7 +13,7 @@ class DoubleSlabBlock extends SolidBlock{ 5 => "Stone Brick", 6 => "Quartz", 7 => "Smooth Stone", - ); + ]; $this->name = "Double " . $names[$this->meta & 0x07] . " Slab"; $this->hardness = 30; } @@ -31,15 +31,15 @@ class DoubleSlabBlock extends SolidBlock{ default => 10, }; } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 1){ - return array( - array(SLAB, $this->meta & 0x07, 2), - ); + return [ + [SLAB, $this->meta & 0x07, 2], + ]; }else{ - return array(); + return []; } } - + } diff --git a/src/material/block/solid/DoubleWoodSlabBlock.php b/src/material/block/solid/DoubleWoodSlabBlock.php index 7ff69ef1..61b4648f 100644 --- a/src/material/block/solid/DoubleWoodSlabBlock.php +++ b/src/material/block/solid/DoubleWoodSlabBlock.php @@ -4,12 +4,12 @@ class DoubleWoodSlabBlock extends SolidBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(DOUBLE_WOOD_SLAB, $meta, "Double Wooden Slab"); - $names = array( + $names = [ 0 => "Oak", 1 => "Spruce", 2 => "Birch", 3 => "Jungle", - ); + ]; $this->name = "Double " . $names[$this->meta & 0x07] . " Wooden Slab"; $this->hardness = 15; } @@ -27,11 +27,11 @@ class DoubleWoodSlabBlock extends SolidBlock{ default => 3, }; } - + public function getDrops(Item $item, Player $player){ - return array( - array(WOOD_SLAB, $this->meta & 0x07, 2), - ); + return [ + [WOOD_SLAB, $this->meta & 0x07, 2], + ]; } - + } diff --git a/src/material/block/solid/FurnaceBlock.php b/src/material/block/solid/FurnaceBlock.php index 06f2db28..bbe31df5 100644 --- a/src/material/block/solid/FurnaceBlock.php +++ b/src/material/block/solid/FurnaceBlock.php @@ -4,7 +4,6 @@ require_once("BurningFurnaceBlock.php"); /***REM_END***/ - class FurnaceBlock extends BurningFurnaceBlock{ public static $blockID; public function __construct($meta = 0){ diff --git a/src/material/block/solid/GlassBlock.php b/src/material/block/solid/GlassBlock.php index a6149c59..6b133b3e 100644 --- a/src/material/block/solid/GlassBlock.php +++ b/src/material/block/solid/GlassBlock.php @@ -6,11 +6,11 @@ class GlassBlock extends TransparentBlock{ parent::__construct(GLASS, 0, "Glass"); $this->hardness = 1.5; } - + public function getDrops(Item $item, Player $player){ - return array(); + return []; } - + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ return [new AxisAlignedBB($x, $y, $z, $x + 1, $y + 1, $z + 1)]; } diff --git a/src/material/block/solid/GlowingObsidianBlock.php b/src/material/block/solid/GlowingObsidianBlock.php index ea88b779..47411822 100644 --- a/src/material/block/solid/GlowingObsidianBlock.php +++ b/src/material/block/solid/GlowingObsidianBlock.php @@ -5,9 +5,9 @@ class GlowingObsidianBlock extends SolidBlock implements LightingBlock{ public function __construct($meta = 0){ parent::__construct(GLOWING_OBSIDIAN, $meta, "Glowing Obsidian"); } - + public function getMaxLightValue(){ return 12; } - + } \ No newline at end of file diff --git a/src/material/block/solid/GlowstoneBlock.php b/src/material/block/solid/GlowstoneBlock.php index 177cbb3f..531ed733 100644 --- a/src/material/block/solid/GlowstoneBlock.php +++ b/src/material/block/solid/GlowstoneBlock.php @@ -6,17 +6,17 @@ class GlowstoneBlock extends TransparentBlock implements LightingBlock{ parent::__construct(GLOWSTONE_BLOCK, 0, "Glowstone"); $this->hardness = 1.5; } - + public function getMaxLightValue(){ return 15; } - + public function getDrops(Item $item, Player $player){ - return array( - array(GLOWSTONE_DUST, 0, mt_rand(2, 4)), - ); + return [ + [GLOWSTONE_DUST, 0, mt_rand(2, 4)], + ]; } - + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ return [new AxisAlignedBB($x, $y, $z, $x + 1, $y + 1, $z + 1)]; } diff --git a/src/material/block/solid/GoldBlock.php b/src/material/block/solid/GoldBlock.php index 80006170..460c2b02 100644 --- a/src/material/block/solid/GoldBlock.php +++ b/src/material/block/solid/GoldBlock.php @@ -17,14 +17,14 @@ class GoldBlock extends SolidBlock{ default => 15, }; } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 4){ - return array( - array(GOLD_BLOCK, 0, 1), - ); + return [ + [GOLD_BLOCK, 0, 1], + ]; }else{ - return array(); + return []; } } } diff --git a/src/material/block/solid/GrassBlock.php b/src/material/block/solid/GrassBlock.php index 9b689ff6..47ae9f0d 100644 --- a/src/material/block/solid/GrassBlock.php +++ b/src/material/block/solid/GrassBlock.php @@ -9,9 +9,9 @@ class GrassBlock extends SolidBlock{ } public function getDrops(Item $item, Player $player){ - return array( - array(DIRT, 0, 1), - ); + return [ + [DIRT, 0, 1], + ]; } public function onActivate(Item $item, Player $player){ @@ -34,13 +34,13 @@ class GrassBlock extends SolidBlock{ } return false; } - + public function seedsDrop(){ $chance = lcg_value() * 100; if($chance <= 1){ - ServerAPI::request()->api->entity->drop(new Position($this->x+0.5, $this->y+1, $this->z+0.5, $this->level), BlockAPI::getItem(BEETROOT_SEEDS,0,1), 10); + ServerAPI::request()->api->entity->drop(new Position($this->x + 0.5, $this->y + 1, $this->z + 0.5, $this->level), BlockAPI::getItem(BEETROOT_SEEDS,0,1), 10); }elseif($chance > 1 and $chance <= 16){ - ServerAPI::request()->api->entity->drop(new Position($this->x+0.5, $this->y+1, $this->z+0.5, $this->level), BlockAPI::getItem(SEEDS,0,1), 10); + ServerAPI::request()->api->entity->drop(new Position($this->x + 0.5, $this->y + 1, $this->z + 0.5, $this->level), BlockAPI::getItem(SEEDS,0,1), 10); } } public static function onRandomTick(Level $level, $x, $y, $z){ @@ -51,12 +51,12 @@ class GrassBlock extends SolidBlock{ $x = $x + mt_rand(0, 2) - 1; $y = $y + mt_rand(0, 4) - 3; $z = $z + mt_rand(0, 2) - 1; - + $blockUp = $level->level->getBlockID($x, $y + 1, $z); if(StaticBlock::getIsTransparent($blockUp) && !StaticBlock::getIsLiquid($blockUp) && !($blockUp === 60) && $level->level->getBlockID($x, $y, $z) === DIRT){ $level->fastSetBlockUpdate($x, $y, $z, GRASS, 0); } - + } } } diff --git a/src/material/block/solid/GravelBlock.php b/src/material/block/solid/GravelBlock.php index f35adb53..144792f3 100644 --- a/src/material/block/solid/GravelBlock.php +++ b/src/material/block/solid/GravelBlock.php @@ -6,11 +6,11 @@ class GravelBlock extends FallableBlock{ parent::__construct(GRAVEL, 0, "Gravel"); $this->hardness = 3; } - + public function getDrops(Item $item, Player $player){ return [ [mt_rand(1,10) == 1 ? FLINT : GRAVEL, 0, 1], ]; } - + } \ No newline at end of file diff --git a/src/material/block/solid/HayBaleBlock.php b/src/material/block/solid/HayBaleBlock.php index 869eebe5..dd6d52a8 100644 --- a/src/material/block/solid/HayBaleBlock.php +++ b/src/material/block/solid/HayBaleBlock.php @@ -6,16 +6,16 @@ class HayBaleBlock extends SolidBlock{ parent::__construct(HAY_BALE, $meta, "Hay Bale"); $this->hardness = 10; } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - $faces = array( + $faces = [ 0 => 0, 1 => 0, 2 => 0b1000, 3 => 0b1000, 4 => 0b0100, 5 => 0b0100, - ); + ]; $this->meta = ($this->meta & 0x03) | $faces[$face]; $this->level->setBlock($block, $this, true, false, true); @@ -23,9 +23,9 @@ class HayBaleBlock extends SolidBlock{ } public function getDrops(Item $item, Player $player){ - return array( - array($this->id, 0, 1), - ); + return [ + [$this->id, 0, 1], + ]; } - + } \ No newline at end of file diff --git a/src/material/block/solid/IceBlock.php b/src/material/block/solid/IceBlock.php index e5e7860d..28701ad6 100644 --- a/src/material/block/solid/IceBlock.php +++ b/src/material/block/solid/IceBlock.php @@ -7,17 +7,17 @@ class IceBlock extends TransparentBlock{ $this->hardness = 2.5; $this->slipperiness = 0.98; } - + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ return [new AxisAlignedBB($x, $y, $z, $x + 1, $y + 1, $z + 1)]; } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $ret = $this->level->setBlock($this, $this, true, false, true); $this->level->scheduleBlockUpdate(new Position($this, 0, 0, $this->level), Utils::getRandomUpdateTicks(), BLOCK_UPDATE_RANDOM); return $ret; } - + public function onBreak(Item $item, Player $player){ if(($player->gamemode & 0x01) === 0){ $this->level->setBlock($this, new WaterBlock(), true, false, true); @@ -27,20 +27,20 @@ class IceBlock extends TransparentBlock{ } return true; } - - private function scanForNearbyLightSources($offsetX, $offsetY, $offsetZ){ + + private function scanForNearbyLightSources($offsetX, $offsetY, $offsetZ){ for($x = -$offsetX; $x <= $offsetX; ++$x){ //i hope it is possible to optimize it for($z = -$offsetZ; $z <= $offsetZ; ++$z){ for($y = -$offsetY; $y <= $offsetY; ++$y){ - $pX = $this->x+$x; - $pY = $this->y+$y; - $pZ = $this->z+$z; + $pX = $this->x + $x; + $pY = $this->y + $y; + $pZ = $this->z + $z; $block = $this->level->getBlock(new Vector3($pX, $pY, $pZ)); //D= slow what was u thinking of gameherobrine from 2022 if($block instanceof LightingBlock){ //idk is it possible to make it better return $block; } - } - } + } + } } } //public static function onUpdate(Level $level, $x, $y, $z, $type){ /*Taken from https://github.com/PocketMine/PocketMine-MP/issues/3249*/ @@ -69,6 +69,6 @@ class IceBlock extends TransparentBlock{ } public function getDrops(Item $item, Player $player){ - return array(); + return []; } } diff --git a/src/material/block/solid/InfoUpdate2Block.php b/src/material/block/solid/InfoUpdate2Block.php index 0497b861..1d9a45ea 100755 --- a/src/material/block/solid/InfoUpdate2Block.php +++ b/src/material/block/solid/InfoUpdate2Block.php @@ -7,5 +7,5 @@ class InfoUpdate2Block extends SolidBlock{ $this->breakable = true; $this->hardness = 0; } - + } \ No newline at end of file diff --git a/src/material/block/solid/InfoUpdateBlock.php b/src/material/block/solid/InfoUpdateBlock.php index 8b088190..81af7276 100755 --- a/src/material/block/solid/InfoUpdateBlock.php +++ b/src/material/block/solid/InfoUpdateBlock.php @@ -7,5 +7,5 @@ class InfoUpdateBlock extends SolidBlock{ $this->breakable = true; $this->hardness = 0; } - + } \ No newline at end of file diff --git a/src/material/block/solid/InvisibleBedrockBlock.php b/src/material/block/solid/InvisibleBedrockBlock.php index dede980e..2fd0843d 100755 --- a/src/material/block/solid/InvisibleBedrockBlock.php +++ b/src/material/block/solid/InvisibleBedrockBlock.php @@ -7,12 +7,12 @@ class InvisibleBedrockBlock extends SolidBlock{ $this->breakable = false; $this->hardness = 3600000; } - + public function isBreakable(Item $item, Player $player){ if(($player->gamemode & 0x01) === 0x01){ return true; } return false; } - + } \ No newline at end of file diff --git a/src/material/block/solid/IronBlock.php b/src/material/block/solid/IronBlock.php index 504b94a1..5db4b364 100644 --- a/src/material/block/solid/IronBlock.php +++ b/src/material/block/solid/IronBlock.php @@ -18,14 +18,14 @@ class IronBlock extends SolidBlock{ default => 25, }; } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 3){ - return array( - array(IRON_BLOCK, 0, 1), - ); + return [ + [IRON_BLOCK, 0, 1], + ]; }else{ - return array(); + return []; } } } diff --git a/src/material/block/solid/LapisBlock.php b/src/material/block/solid/LapisBlock.php index 1d4c4dc6..7d0e790c 100644 --- a/src/material/block/solid/LapisBlock.php +++ b/src/material/block/solid/LapisBlock.php @@ -18,14 +18,14 @@ class LapisBlock extends SolidBlock{ default => 15, }; } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 3){ - return array( - array(LAPIS_BLOCK, 0, 1), - ); + return [ + [LAPIS_BLOCK, 0, 1], + ]; }else{ - return array(); + return []; } } diff --git a/src/material/block/solid/LeavesBlock.php b/src/material/block/solid/LeavesBlock.php index 35391300..c4e3dc3e 100644 --- a/src/material/block/solid/LeavesBlock.php +++ b/src/material/block/solid/LeavesBlock.php @@ -7,22 +7,22 @@ class LeavesBlock extends TransparentBlock{ const BIRCH = 2; public function __construct($meta = 0){ parent::__construct(LEAVES, $meta, "Leaves"); - $names = array( + $names = [ LeavesBlock::OAK => "Oak Leaves", LeavesBlock::SPRUCE => "Spruce Leaves", LeavesBlock::BIRCH => "Birch Leaves", 3 => "", - ); + ]; $this->name = $names[$this->meta & 0x03]; $this->hardness = 1; } - + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ return [new AxisAlignedBB($x, $y, $z, $x + 1, $y + 1, $z + 1)]; } - + public static function createIndex($x, $y, $z){ - return $x.".".$y.".".$z; + return $x . "." . $y . "." . $z; } public static function findLog(Level $level, $x, $y, $z, array $visited, $distance){ //port from newest pocketmine $index = self::createIndex($x, $y, $z); @@ -50,7 +50,7 @@ class LeavesBlock extends TransparentBlock{ $meta = $b[1]; if(($meta & 0b00001100) === 0x08){ $meta &= 0x03; - $visited = array(); + $visited = []; if(!self::findLog($level, $x, $y, $z, $visited, 0)){ //$this->level->setBlock($this, new AirBlock(), false, false, true); $level->fastSetBlockUpdate($x, $y, $z, 0, 0); @@ -72,22 +72,22 @@ class LeavesBlock extends TransparentBlock{ $level->fastSetBlockUpdate($x, $y, $z, $id, $meta); //TODO maybe use $level->level->setBlock directly, the client doesnt need to know thism info. } } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $this->meta |= 0x04; $this->level->setBlock($this, $this, true, false, true); } - + public function getDrops(Item $item, Player $player){ - $drops = array(); + $drops = []; if($item->isShears()){ - $drops[] = array(LEAVES, $this->meta & 0x03, 1); + $drops[] = [LEAVES, $this->meta & 0x03, 1]; }else{ if(mt_rand(1,20) === 1){ //Saplings - $drops[] = array(SAPLING, $this->meta & 0x03, 1); + $drops[] = [SAPLING, $this->meta & 0x03, 1]; } if(($this->meta & 0x03) === LeavesBlock::OAK and mt_rand(1,100) === 1){ //Apples - $drops[] = array(APPLE, 0, 1); + $drops[] = [APPLE, 0, 1]; } } return $drops; diff --git a/src/material/block/solid/LitPumpkinBlock.php b/src/material/block/solid/LitPumpkinBlock.php index 6775794a..2edd0c11 100644 --- a/src/material/block/solid/LitPumpkinBlock.php +++ b/src/material/block/solid/LitPumpkinBlock.php @@ -8,17 +8,16 @@ class LitPumpkinBlock extends SolidBlock implements LightingBlock{ 2 => 3, 3 => 0, ]; - - + public function __construct($meta = 0){ parent::__construct(LIT_PUMPKIN, $meta, "Jack o'Lantern"); $this->hardness = 5; } - + public function getMaxLightValue(){ return 15; } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $id = $this->level->level->getBlockID($this->x, $this->y, $this->z); if(($id == 0 || $id == SNOW_LAYER) && $this->level->isTopSolidBlocking($this->x, $this->y - 1, $this->z)){ @@ -28,12 +27,11 @@ class LitPumpkinBlock extends SolidBlock implements LightingBlock{ } return false; } - + public function getDrops(Item $item, Player $player){ return [ [JACK_O_LANTERN, 0, 1] ]; } - - -} \ No newline at end of file + + } \ No newline at end of file diff --git a/src/material/block/solid/MelonBlock.php b/src/material/block/solid/MelonBlock.php index 4b46e393..3eaa724e 100644 --- a/src/material/block/solid/MelonBlock.php +++ b/src/material/block/solid/MelonBlock.php @@ -7,11 +7,11 @@ class MelonBlock extends TransparentBlock{ $this->hardness = 5; } public function getDrops(Item $item, Player $player){ - return array( - array(MELON_SLICE, 0, mt_rand(3, 7)), - ); + return [ + [MELON_SLICE, 0, mt_rand(3, 7)], + ]; } - + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ return [new AxisAlignedBB($x, $y, $z, $x + 1, $y + 1, $z + 1)]; } diff --git a/src/material/block/solid/MossStoneBlock.php b/src/material/block/solid/MossStoneBlock.php index 495de40d..c738c615 100644 --- a/src/material/block/solid/MossStoneBlock.php +++ b/src/material/block/solid/MossStoneBlock.php @@ -20,14 +20,14 @@ class MossStoneBlock extends SolidBlock{ default => 10, }; } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 1){ - return array( - array(MOSS_STONE, 0, 1), - ); + return [ + [MOSS_STONE, 0, 1], + ]; }else{ - return array(); + return []; } } } diff --git a/src/material/block/solid/NetherBricksBlock.php b/src/material/block/solid/NetherBricksBlock.php index bf1c7947..8211ad7b 100644 --- a/src/material/block/solid/NetherBricksBlock.php +++ b/src/material/block/solid/NetherBricksBlock.php @@ -20,14 +20,14 @@ class NetherBricksBlock extends SolidBlock{ default => 10, }; } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 1){ - return array( - array(NETHER_BRICKS, 0, 1), - ); + return [ + [NETHER_BRICKS, 0, 1], + ]; }else{ - return array(); + return []; } } } diff --git a/src/material/block/solid/NetherrackBlock.php b/src/material/block/solid/NetherrackBlock.php index 4700a083..f93af992 100644 --- a/src/material/block/solid/NetherrackBlock.php +++ b/src/material/block/solid/NetherrackBlock.php @@ -6,7 +6,7 @@ class NetherrackBlock extends SolidBlock{ parent::__construct(NETHERRACK, 0, "Netherrack"); $this->hardness = 2; } - + public function getBreakTime(Item $item, Player $player){ if(($player->gamemode & 0x01) === 0x01){ return 0.20; @@ -23,11 +23,11 @@ class NetherrackBlock extends SolidBlock{ public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 1){ - return array( - array(NETHERRACK, 0, 1), - ); + return [ + [NETHERRACK, 0, 1], + ]; }else{ - return array(); + return []; } } } diff --git a/src/material/block/solid/ObsidianBlock.php b/src/material/block/solid/ObsidianBlock.php index dc0ff733..71453cc2 100644 --- a/src/material/block/solid/ObsidianBlock.php +++ b/src/material/block/solid/ObsidianBlock.php @@ -4,9 +4,9 @@ class ObsidianBlock extends SolidBlock{ public static $blockID; public function __construct(){ parent::__construct(OBSIDIAN, 0, "Obsidian"); - $this->hardness = 6000; + $this->hardness = 6000; } - + public function getBreakTime(Item $item, Player $player){ if(($player->gamemode & 0x01) === 0x01){ return 0.20; @@ -17,7 +17,7 @@ class ObsidianBlock extends SolidBlock{ return 250; } } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 5){ return [ diff --git a/src/material/block/solid/PlanksBlock.php b/src/material/block/solid/PlanksBlock.php index ed0ba041..2380c10f 100644 --- a/src/material/block/solid/PlanksBlock.php +++ b/src/material/block/solid/PlanksBlock.php @@ -4,14 +4,14 @@ class PlanksBlock extends SolidBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(PLANKS, $meta, "Wooden Planks"); - $names = array( + $names = [ WoodBlock::OAK => "Oak Wooden Planks", WoodBlock::SPRUCE => "Spruce Wooden Planks", WoodBlock::BIRCH => "Birch Wooden Planks", WoodBlock::JUNGLE => "Jungle Wooden Planks", - ); + ]; $this->name = $names[$this->meta & 0x03]; $this->hardness = 15; } - + } \ No newline at end of file diff --git a/src/material/block/solid/PumpkinBlock.php b/src/material/block/solid/PumpkinBlock.php index 25239421..cfba83d3 100644 --- a/src/material/block/solid/PumpkinBlock.php +++ b/src/material/block/solid/PumpkinBlock.php @@ -6,17 +6,17 @@ class PumpkinBlock extends SolidBlock{ parent::__construct(PUMPKIN, $meta, "Pumpkin"); $this->hardness = 5; } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - $faces = array( + $faces = [ 0 => 1, 1 => 2, 2 => 3, 3 => 0, - ); + ]; $this->meta = $faces[$player->entity->getDirection()]; $this->level->setBlock($block, $this, true, false, true); return true; } - + } \ No newline at end of file diff --git a/src/material/block/solid/QuartzBlock.php b/src/material/block/solid/QuartzBlock.php index 1c7bcfb7..7eeb9fb1 100644 --- a/src/material/block/solid/QuartzBlock.php +++ b/src/material/block/solid/QuartzBlock.php @@ -4,12 +4,12 @@ class QuartzBlock extends SolidBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(QUARTZ_BLOCK, $meta, "Quartz Block"); - $names = array( + $names = [ 0 => "Quartz Block", 1 => "Chiseled Quartz Block", 2 => "Quartz Pillar", 3 => "Quartz Pillar", - ); + ]; $this->name = $names[$this->meta & 0x03]; } @@ -26,14 +26,14 @@ class QuartzBlock extends SolidBlock{ default => 4, }; } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 1){ - return array( - array(QUARTZ_BLOCK, $this->meta & 0x03, 1), - ); + return [ + [QUARTZ_BLOCK, $this->meta & 0x03, 1], + ]; }else{ - return array(); + return []; } } } diff --git a/src/material/block/solid/Reserved6Block.php b/src/material/block/solid/Reserved6Block.php index 1a9d6bd6..65117a4e 100644 --- a/src/material/block/solid/Reserved6Block.php +++ b/src/material/block/solid/Reserved6Block.php @@ -7,5 +7,5 @@ class Reserved6Block extends SolidBlock{ $this->breakable = true; $this->hardness = 0; } - + } \ No newline at end of file diff --git a/src/material/block/solid/SandBlock.php b/src/material/block/solid/SandBlock.php index fa41be00..67f92029 100644 --- a/src/material/block/solid/SandBlock.php +++ b/src/material/block/solid/SandBlock.php @@ -6,5 +6,5 @@ class SandBlock extends FallableBlock{ parent::__construct(SAND, 0, "Sand"); $this->hardness = 2.5; } - + } \ No newline at end of file diff --git a/src/material/block/solid/SandstoneBlock.php b/src/material/block/solid/SandstoneBlock.php index c6533998..790ebe24 100644 --- a/src/material/block/solid/SandstoneBlock.php +++ b/src/material/block/solid/SandstoneBlock.php @@ -4,15 +4,15 @@ class SandstoneBlock extends SolidBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(SANDSTONE, $meta, "Sandstone"); - $names = array( + $names = [ 0 => "Sandstone", 1 => "Chiseled Sandstone", 2 => "Smooth Sandstone", - ); - $this->name = $names[$this->meta & 0x03]; + ]; + $this->name = $names[$this->meta & 0x03]; $this->hardness = 4; } - + public function getBreakTime(Item $item, Player $player){ if(($player->gamemode & 0x01) === 0x01){ return 0.20; @@ -29,12 +29,12 @@ class SandstoneBlock extends SolidBlock{ public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 1){ - return array( - array(SANDSTONE, $this->meta & 0x03, 1), - ); + return [ + [SANDSTONE, $this->meta & 0x03, 1], + ]; }else{ - return array(); + return []; } } - + } diff --git a/src/material/block/solid/SnowBlock.php b/src/material/block/solid/SnowBlock.php index 2e024c8f..8fdea37a 100644 --- a/src/material/block/solid/SnowBlock.php +++ b/src/material/block/solid/SnowBlock.php @@ -15,5 +15,5 @@ class SnowBlock extends SolidBlock{ } return []; } - + } \ No newline at end of file diff --git a/src/material/block/solid/SoulSandBlock.php b/src/material/block/solid/SoulSandBlock.php index f5ff6e20..cb4d30aa 100644 --- a/src/material/block/solid/SoulSandBlock.php +++ b/src/material/block/solid/SoulSandBlock.php @@ -6,5 +6,5 @@ class SoulSandBlock extends SolidBlock{ parent::__construct(SOUL_SAND, 0, "Soul Sand"); $this->hardness = 2.5; } - + } \ No newline at end of file diff --git a/src/material/block/solid/SpongeBlock.php b/src/material/block/solid/SpongeBlock.php index 39b4c237..25a7c060 100644 --- a/src/material/block/solid/SpongeBlock.php +++ b/src/material/block/solid/SpongeBlock.php @@ -6,5 +6,5 @@ class SpongeBlock extends SolidBlock{ parent::__construct(SPONGE, 0, "Sponge"); $this->hardness = 3; } - + } diff --git a/src/material/block/solid/StoneBlock.php b/src/material/block/solid/StoneBlock.php index 51504b66..3f56c022 100644 --- a/src/material/block/solid/StoneBlock.php +++ b/src/material/block/solid/StoneBlock.php @@ -20,15 +20,15 @@ class StoneBlock extends SolidBlock{ default => 7.5, }; } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 1){ - return array( - array(COBBLESTONE, 0, 1), - ); + return [ + [COBBLESTONE, 0, 1], + ]; }else{ - return array(); + return []; } } - + } diff --git a/src/material/block/solid/StoneBricksBlock.php b/src/material/block/solid/StoneBricksBlock.php index 58299703..741ac21f 100644 --- a/src/material/block/solid/StoneBricksBlock.php +++ b/src/material/block/solid/StoneBricksBlock.php @@ -4,12 +4,12 @@ class StoneBricksBlock extends SolidBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(STONE_BRICKS, $meta, "Stone Bricks"); - $names = array( + $names = [ 0 => "Stone Bricks", 1 => "Mossy Stone Bricks", 2 => "Cracked Stone Bricks", 3 => "Chiseled Stone Bricks", - ); + ]; $this->name = $names[$this->meta & 0x03]; $this->hardness = 30; } @@ -27,15 +27,15 @@ class StoneBricksBlock extends SolidBlock{ default => 7.5, }; } - + public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 1){ - return array( - array(STONE_BRICKS, $this->meta & 0x03, 1), - ); + return [ + [STONE_BRICKS, $this->meta & 0x03, 1], + ]; }else{ - return array(); + return []; } } - + } diff --git a/src/material/block/solid/StonecutterBlock.php b/src/material/block/solid/StonecutterBlock.php index b5fc3fe7..6a75f9dd 100644 --- a/src/material/block/solid/StonecutterBlock.php +++ b/src/material/block/solid/StonecutterBlock.php @@ -6,7 +6,7 @@ class StonecutterBlock extends SolidBlock{ parent::__construct(STONECUTTER, $meta, "Stonecutter"); $this->isActivable = true; } - + public function onActivate(Item $item, Player $player){ $player->toCraft[-1] = 2; return true; @@ -20,6 +20,6 @@ class StonecutterBlock extends SolidBlock{ }else{ return []; } - - } + + } } \ No newline at end of file diff --git a/src/material/block/solid/WoodBlock.php b/src/material/block/solid/WoodBlock.php index e9711141..369b7483 100644 --- a/src/material/block/solid/WoodBlock.php +++ b/src/material/block/solid/WoodBlock.php @@ -6,28 +6,28 @@ class WoodBlock extends SolidBlock{ const SPRUCE = 1; const BIRCH = 2; const JUNGLE = 3; - + public function __construct($meta = 0){ parent::__construct(WOOD, $meta, "Wood"); - $names = array( + $names = [ WoodBlock::OAK => "Oak Wood", WoodBlock::SPRUCE => "Spruce Wood", WoodBlock::BIRCH => "Birch Wood", WoodBlock::JUNGLE => "Jungle Wood", - ); + ]; $this->name = $names[$this->meta & 0x03]; $this->hardness = 10; } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - $faces = array( + $faces = [ 0 => 0, 1 => 0, 2 => 0b1000, 3 => 0b1000, 4 => 0b0100, 5 => 0b0100, - ); + ]; $this->meta = ($this->meta & 0x03) | $faces[$face]; $this->level->setBlock($block, $this, true, false, true); @@ -51,8 +51,8 @@ class WoodBlock extends SolidBlock{ } } public function getDrops(Item $item, Player $player){ - return array( - array($this->id, $this->meta & 0x03, 1), - ); + return [ + [$this->id, $this->meta & 0x03, 1], + ]; } } \ No newline at end of file diff --git a/src/material/block/solid/WoolBlock.php b/src/material/block/solid/WoolBlock.php index 3bfb0de8..c4a370e1 100644 --- a/src/material/block/solid/WoolBlock.php +++ b/src/material/block/solid/WoolBlock.php @@ -4,7 +4,7 @@ class WoolBlock extends SolidBlock{ public static $blockID; public function __construct($meta = 0){ parent::__construct(WOOL, $meta, "Wool"); - $names = array( + $names = [ 0 => "White Wool", 1 => "Orange Wool", 2 => "Magenta Wool", @@ -21,9 +21,9 @@ class WoolBlock extends SolidBlock{ 13 => "Green Wool", 14 => "Red Wool", 15 => "Black Wool", - ); + ]; $this->name = $names[$this->meta]; $this->hardness = 4; } - + } \ No newline at end of file diff --git a/src/material/block/solid/WorkbenchBlock.php b/src/material/block/solid/WorkbenchBlock.php index db605a65..ba8720bc 100644 --- a/src/material/block/solid/WorkbenchBlock.php +++ b/src/material/block/solid/WorkbenchBlock.php @@ -7,15 +7,15 @@ class WorkbenchBlock extends SolidBlock{ $this->isActivable = true; $this->hardness = 15; } - + public function onActivate(Item $item, Player $player){ $player->toCraft[-1] = 1; return true; } public function getDrops(Item $item, Player $player){ - return array( - array($this->id, 0, 1), - ); + return [ + [$this->id, 0, 1], + ]; } } \ No newline at end of file diff --git a/src/material/item/armor/ArmorItem.php b/src/material/item/armor/ArmorItem.php index ebb5536d..a9078948 100644 --- a/src/material/item/armor/ArmorItem.php +++ b/src/material/item/armor/ArmorItem.php @@ -1,17 +1,17 @@ getMaterialDurability() * $this->getBaseDurability(); } diff --git a/src/material/item/armor/ChainBootsItem.php b/src/material/item/armor/ChainBootsItem.php index 73083238..afd02dcd 100644 --- a/src/material/item/armor/ChainBootsItem.php +++ b/src/material/item/armor/ChainBootsItem.php @@ -5,11 +5,11 @@ class ChainBootsItem extends ArmorItem{ public function __construct($meta = 0, $count = 1){ parent::__construct(CHAIN_BOOTS, $meta, $count, "Chain Boots"); } - + public function getMaterialDurability(){ return Material::CHAIN; } - + public function getBaseDurability(){ return 13; } @@ -18,5 +18,4 @@ class ChainBootsItem extends ArmorItem{ return 1; } - -} \ No newline at end of file + } \ No newline at end of file diff --git a/src/material/item/armor/ChainChestplateItem.php b/src/material/item/armor/ChainChestplateItem.php index ae28d0c5..19a6d9bd 100644 --- a/src/material/item/armor/ChainChestplateItem.php +++ b/src/material/item/armor/ChainChestplateItem.php @@ -5,11 +5,11 @@ class ChainChestplateItem extends ArmorItem{ public function __construct($meta = 0, $count = 1){ parent::__construct(CHAIN_CHESTPLATE, $meta, $count, "Chain Chestplate"); } - + public function getMaterialDurability(){ return Material::CHAIN; } - + public function getBaseDurability(){ return 16; } diff --git a/src/material/item/armor/ChainHelmetItem.php b/src/material/item/armor/ChainHelmetItem.php index df7c83d9..b78cb920 100644 --- a/src/material/item/armor/ChainHelmetItem.php +++ b/src/material/item/armor/ChainHelmetItem.php @@ -5,11 +5,11 @@ class ChainHelmetItem extends ArmorItem{ public function __construct($meta = 0, $count = 1){ parent::__construct(CHAIN_HELMET, $meta, $count, "Chain Helmet"); } - + public function getMaterialDurability(){ return Material::CHAIN; } - + public function getBaseDurability(){ return 11; } diff --git a/src/material/item/armor/ChainLeggingsItem.php b/src/material/item/armor/ChainLeggingsItem.php index 9d1800b1..de13dc83 100644 --- a/src/material/item/armor/ChainLeggingsItem.php +++ b/src/material/item/armor/ChainLeggingsItem.php @@ -5,11 +5,11 @@ class ChainLeggingsItem extends ArmorItem{ public function __construct($meta = 0, $count = 1){ parent::__construct(CHAIN_LEGGINGS, $meta, $count, "Chain Leggings"); } - + public function getMaterialDurability(){ return Material::CHAIN; } - + public function getBaseDurability(){ return 15; } diff --git a/src/material/item/armor/DiamondBootsItem.php b/src/material/item/armor/DiamondBootsItem.php index 4591f9d8..75d8c129 100644 --- a/src/material/item/armor/DiamondBootsItem.php +++ b/src/material/item/armor/DiamondBootsItem.php @@ -5,11 +5,11 @@ class DiamondBootsItem extends ArmorItem{ public function __construct($meta = 0, $count = 1){ parent::__construct(DIAMOND_BOOTS, $meta, $count, "Diamond Boots"); } - + public function getMaterialDurability(){ return Material::DIAMOND; } - + public function getBaseDurability(){ return 13; } diff --git a/src/material/item/armor/DiamondChestplateItem.php b/src/material/item/armor/DiamondChestplateItem.php index fad4edce..3a6b8a05 100644 --- a/src/material/item/armor/DiamondChestplateItem.php +++ b/src/material/item/armor/DiamondChestplateItem.php @@ -5,11 +5,11 @@ class DiamondChestplateItem extends ArmorItem{ public function __construct($meta = 0, $count = 1){ parent::__construct(DIAMOND_CHESTPLATE, $meta, $count, "Diamond Chestplate"); } - + public function getMaterialDurability(){ return Material::DIAMOND; } - + public function getBaseDurability(){ return 16; } diff --git a/src/material/item/armor/DiamondHelmetItem.php b/src/material/item/armor/DiamondHelmetItem.php index b0e53cbc..514986f0 100644 --- a/src/material/item/armor/DiamondHelmetItem.php +++ b/src/material/item/armor/DiamondHelmetItem.php @@ -5,11 +5,11 @@ class DiamondHelmetItem extends ArmorItem{ public function __construct($meta = 0, $count = 1){ parent::__construct(DIAMOND_HELMET, $meta, $count, "Diamond Helmet"); } - + public function getMaterialDurability(){ return Material::DIAMOND; } - + public function getBaseDurability(){ return 11; } diff --git a/src/material/item/armor/DiamondLeggingsItem.php b/src/material/item/armor/DiamondLeggingsItem.php index ef2e005d..55eb9356 100644 --- a/src/material/item/armor/DiamondLeggingsItem.php +++ b/src/material/item/armor/DiamondLeggingsItem.php @@ -5,11 +5,11 @@ class DiamondLeggingsItem extends ArmorItem{ public function __construct($meta = 0, $count = 1){ parent::__construct(DIAMOND_LEGGINGS, $meta, $count, "Diamond Leggings"); } - + public function getMaterialDurability(){ return Material::DIAMOND; } - + public function getBaseDurability(){ return 15; } diff --git a/src/material/item/armor/GoldenBootsItem.php b/src/material/item/armor/GoldenBootsItem.php index 1ac0f9a4..8f9b7581 100644 --- a/src/material/item/armor/GoldenBootsItem.php +++ b/src/material/item/armor/GoldenBootsItem.php @@ -5,11 +5,11 @@ class GoldenBootsItem extends ArmorItem{ public function __construct($meta = 0, $count = 1){ parent::__construct(GOLDEN_BOOTS, $meta, $count, "Golden Boots"); } - + public function getMaterialDurability(){ return Material::GOLD; } - + public function getBaseDurability(){ return 13; } diff --git a/src/material/item/armor/GoldenChestplateItem.php b/src/material/item/armor/GoldenChestplateItem.php index 739617b6..05b3aed0 100644 --- a/src/material/item/armor/GoldenChestplateItem.php +++ b/src/material/item/armor/GoldenChestplateItem.php @@ -5,11 +5,11 @@ class GoldenChestplateItem extends ArmorItem{ public function __construct($meta = 0, $count = 1){ parent::__construct(GOLDEN_CHESTPLATE, $meta, $count, "Golden Chestplate"); } - + public function getMaterialDurability(){ return Material::GOLD; } - + public function getBaseDurability(){ return 16; } diff --git a/src/material/item/armor/GoldenHelmetItem.php b/src/material/item/armor/GoldenHelmetItem.php index c7ee4711..11a94256 100644 --- a/src/material/item/armor/GoldenHelmetItem.php +++ b/src/material/item/armor/GoldenHelmetItem.php @@ -5,11 +5,11 @@ class GoldenHelmetItem extends ArmorItem{ public function __construct($meta = 0, $count = 1){ parent::__construct(GOLDEN_HELMET, $meta, $count, "Golden Helmet"); } - + public function getMaterialDurability(){ return Material::GOLD; } - + public function getBaseDurability(){ return 11; } diff --git a/src/material/item/armor/GoldenLeggingsItem.php b/src/material/item/armor/GoldenLeggingsItem.php index ee9dd964..d2fb35a0 100644 --- a/src/material/item/armor/GoldenLeggingsItem.php +++ b/src/material/item/armor/GoldenLeggingsItem.php @@ -5,11 +5,11 @@ class GoldenLeggingsItem extends ArmorItem{ public function __construct($meta = 0, $count = 1){ parent::__construct(GOLDEN_LEGGINGS, $meta, $count, "Golden Leggings"); } - + public function getMaterialDurability(){ return Material::GOLD; } - + public function getBaseDurability(){ return 15; } diff --git a/src/material/item/armor/IronBootsItem.php b/src/material/item/armor/IronBootsItem.php index 7f548bb9..6ed18894 100644 --- a/src/material/item/armor/IronBootsItem.php +++ b/src/material/item/armor/IronBootsItem.php @@ -5,11 +5,11 @@ class IronBootsItem extends ArmorItem{ public function __construct($meta = 0, $count = 1){ parent::__construct(IRON_BOOTS, $meta, $count, "Iron Boots"); } - + public function getMaterialDurability(){ return Material::IRON; } - + public function getBaseDurability(){ return 13; } diff --git a/src/material/item/armor/IronChestplateItem.php b/src/material/item/armor/IronChestplateItem.php index 05ca1086..d7229d4b 100644 --- a/src/material/item/armor/IronChestplateItem.php +++ b/src/material/item/armor/IronChestplateItem.php @@ -5,11 +5,11 @@ class IronChestplateItem extends ArmorItem{ public function __construct($meta = 0, $count = 1){ parent::__construct(IRON_CHESTPLATE, $meta, $count, "Iron Chestplate"); } - + public function getMaterialDurability(){ return Material::IRON; } - + public function getBaseDurability(){ return 16; } diff --git a/src/material/item/armor/IronHelmetItem.php b/src/material/item/armor/IronHelmetItem.php index 70c85f5e..78300ed4 100644 --- a/src/material/item/armor/IronHelmetItem.php +++ b/src/material/item/armor/IronHelmetItem.php @@ -5,11 +5,11 @@ class IronHelmetItem extends ArmorItem{ public function __construct($meta = 0, $count = 1){ parent::__construct(IRON_HELMET, $meta, $count, "Iron Helmet"); } - + public function getMaterialDurability(){ return Material::IRON; } - + public function getBaseDurability(){ return 11; } diff --git a/src/material/item/armor/IronLeggingsItem.php b/src/material/item/armor/IronLeggingsItem.php index f646899d..a7734651 100644 --- a/src/material/item/armor/IronLeggingsItem.php +++ b/src/material/item/armor/IronLeggingsItem.php @@ -5,11 +5,11 @@ class IronLeggingsItem extends ArmorItem{ public function __construct($meta = 0, $count = 1){ parent::__construct(IRON_LEGGINGS, $meta, $count, "Iron Leggings"); } - + public function getMaterialDurability(){ return Material::IRON; } - + public function getBaseDurability(){ return 15; } diff --git a/src/material/item/armor/LeatherBootsItem.php b/src/material/item/armor/LeatherBootsItem.php index 6ec78a24..8bbf9ed5 100644 --- a/src/material/item/armor/LeatherBootsItem.php +++ b/src/material/item/armor/LeatherBootsItem.php @@ -5,11 +5,11 @@ class LeatherBootsItem extends ArmorItem{ public function __construct($meta = 0, $count = 1){ parent::__construct(LEATHER_BOOTS, $meta, $count, "Leather Boots"); } - + public function getMaterialDurability(){ return Material::LEATHER; } - + public function getBaseDurability(){ return 13; } diff --git a/src/material/item/armor/LeatherCapItem.php b/src/material/item/armor/LeatherCapItem.php index 9a146839..2950c741 100755 --- a/src/material/item/armor/LeatherCapItem.php +++ b/src/material/item/armor/LeatherCapItem.php @@ -5,11 +5,11 @@ class LeatherCapItem extends ArmorItem{ public function __construct($meta = 0, $count = 1){ parent::__construct(LEATHER_CAP, $meta, $count, "Leather Cap"); } - + public function getMaterialDurability(){ return Material::LEATHER; } - + public function getBaseDurability(){ return 11; } diff --git a/src/material/item/armor/LeatherPantsItem.php b/src/material/item/armor/LeatherPantsItem.php index 4fc3bc74..a1ad3b88 100644 --- a/src/material/item/armor/LeatherPantsItem.php +++ b/src/material/item/armor/LeatherPantsItem.php @@ -5,11 +5,11 @@ class LeatherPantsItem extends ArmorItem{ public function __construct($meta = 0, $count = 1){ parent::__construct(LEATHER_PANTS, $meta, $count, "Leather Pants"); } - + public function getMaterialDurability(){ return Material::LEATHER; } - + public function getBaseDurability(){ return 15; } diff --git a/src/material/item/armor/LeatherTunicItem.php b/src/material/item/armor/LeatherTunicItem.php index ceaa939a..2be71577 100644 --- a/src/material/item/armor/LeatherTunicItem.php +++ b/src/material/item/armor/LeatherTunicItem.php @@ -5,11 +5,11 @@ class LeatherTunicItem extends ArmorItem{ public function __construct($meta = 0, $count = 1){ parent::__construct(LEATHER_TUNIC, $meta, $count, "Leather Tunic"); } - + public function getMaterialDurability(){ return Material::LEATHER; } - + public function getBaseDurability(){ return 16; } diff --git a/src/material/item/base/ItemAxe.php b/src/material/item/base/ItemAxe.php index 5efc1d5b..d911950f 100644 --- a/src/material/item/base/ItemAxe.php +++ b/src/material/item/base/ItemAxe.php @@ -5,7 +5,7 @@ abstract class ItemAxe extends ItemTool public function isAxe(){ return true; } - + public function getLevel(){ return match ($this->id) { WOODEN_AXE => 1, diff --git a/src/material/item/base/ItemHoe.php b/src/material/item/base/ItemHoe.php index 277b75fb..09675cca 100644 --- a/src/material/item/base/ItemHoe.php +++ b/src/material/item/base/ItemHoe.php @@ -5,7 +5,7 @@ abstract class ItemHoe extends ItemTool public function isHoe(){ return true; } - + public function useOn($object, $force = false){ if(($object instanceof Block) and ($object->getID() === GRASS or $object->getID() === DIRT)){ $this->meta++; diff --git a/src/material/item/base/ItemPickaxe.php b/src/material/item/base/ItemPickaxe.php index 91666c54..87ed2025 100644 --- a/src/material/item/base/ItemPickaxe.php +++ b/src/material/item/base/ItemPickaxe.php @@ -2,7 +2,7 @@ abstract class ItemPickaxe extends ItemTool { - + public function isTool(){ return true; } diff --git a/src/material/item/base/ItemTool.php b/src/material/item/base/ItemTool.php index e5a3af25..8875cca6 100644 --- a/src/material/item/base/ItemTool.php +++ b/src/material/item/base/ItemTool.php @@ -7,15 +7,15 @@ abstract class ItemTool extends Item const STONE_LEVEL = 3; const IRON_LEVEL = 4; const DIAMOND_LEVEL = 5; - + public function isTool(){ return true; } public function useOn($object, $force = false){ - + if($this->isSword() && !($object instanceof Entity)){ $this->meta += 2; - }else if(($object instanceof Entity) && !$this->isSword()){ + }elseif(($object instanceof Entity) && !$this->isSword()){ $this->meta += 2; }else{ $this->meta++; diff --git a/src/material/item/food/CookedPorkchopItem.php b/src/material/item/food/CookedPorkchopItem.php index 6e5fe0fa..9c12d603 100644 --- a/src/material/item/food/CookedPorkchopItem.php +++ b/src/material/item/food/CookedPorkchopItem.php @@ -4,5 +4,5 @@ class CookedPorkchopItem extends Item{ public function __construct($meta = 0, $count = 1){ parent::__construct(COOKED_PORKCHOP, 0, $count, "Cooked Porkchop"); } - + } \ No newline at end of file diff --git a/src/material/item/food/PotatoItem.php b/src/material/item/food/PotatoItem.php index 9aaa46a1..5ca017cc 100755 --- a/src/material/item/food/PotatoItem.php +++ b/src/material/item/food/PotatoItem.php @@ -5,5 +5,5 @@ class PotatoItem extends Item{ $this->block = BlockAPI::get(POTATO_BLOCK); parent::__construct(POTATO, 0, $count, "Potato"); } - + } \ No newline at end of file diff --git a/src/material/item/generic/BoneItem.php b/src/material/item/generic/BoneItem.php index 3a30ebfa..098c0803 100755 --- a/src/material/item/generic/BoneItem.php +++ b/src/material/item/generic/BoneItem.php @@ -4,5 +4,5 @@ class BoneItem extends Item{ public function __construct($meta = 0, $count = 1){ parent::__construct(BONE, 0, $count, "Bone"); } - + } \ No newline at end of file diff --git a/src/material/item/generic/BucketItem.php b/src/material/item/generic/BucketItem.php index 79e9d765..18823a31 100644 --- a/src/material/item/generic/BucketItem.php +++ b/src/material/item/generic/BucketItem.php @@ -1,24 +1,24 @@ "Bucket", 1 => "Milk Bucket", 8 => "Water Bucket", 10 => "Lava Bucket" - ); + ]; public function __construct($meta = 0, $count = 1){ parent::__construct(BUCKET, $meta, $count, "Bucket"); $this->isActivable = true; $this->maxStackSize = 16; $this->name = BucketItem::$possiblenames[$this->meta]; } - + public function getMaxStackSize(){ if($this->getMetadata() == 0) return 16; return 1; } - + public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ if($this->meta === AIR){ if($target instanceof LiquidBlock && $target->getMetadata() == 0){ diff --git a/src/material/item/generic/DyeItem.php b/src/material/item/generic/DyeItem.php index 10ca822e..e2630745 100755 --- a/src/material/item/generic/DyeItem.php +++ b/src/material/item/generic/DyeItem.php @@ -3,7 +3,7 @@ class DyeItem extends Item{ public function __construct($meta = 0, $count = 1){ parent::__construct(DYE, $meta, $count, "Dye"); - $names = array( + $names = [ 0 => "Inc Sac", 1 => "Rose Red", 2 => "Cactus Green", @@ -20,7 +20,7 @@ class DyeItem extends Item{ 13 => "Magenta Dye", 14 => "Orange Dye", 15 => "Bone Meal", - ); + ]; $this->name = $names[$this->meta]; } } \ No newline at end of file diff --git a/src/material/item/generic/MinecartItem.php b/src/material/item/generic/MinecartItem.php index b8923748..d3807928 100755 --- a/src/material/item/generic/MinecartItem.php +++ b/src/material/item/generic/MinecartItem.php @@ -12,8 +12,8 @@ class MinecartItem extends Item{ } $server = ServerAPI::request(); $data = [ - "x" => $target->getX() + 0.5, - "y" => $target->getY() + 0.5, + "x" => $target->getX() + 0.5, + "y" => $target->getY() + 0.5, "z" => $target->getZ() + 0.5, ]; $e = $server->api->entity->add($level, ENTITY_OBJECT, OBJECT_MINECART, $data); diff --git a/src/material/item/generic/PaintingItem.php b/src/material/item/generic/PaintingItem.php index 4ce688c8..f8e134be 100644 --- a/src/material/item/generic/PaintingItem.php +++ b/src/material/item/generic/PaintingItem.php @@ -5,44 +5,44 @@ class PaintingItem extends Item{ parent::__construct(PAINTING, 0, $count, "Painting"); $this->isActivable = true; } - public static $motives = array( + public static $motives = [ // Motive Width Height - "Kebab" => array(1, 1), - "Aztec" => array(1, 1), - "Alban" => array(1, 1), - "Aztec2" => array(1, 1), - "Bomb" => array(1, 1), - "Plant" => array(1, 1), - "Wasteland" => array(1, 1), - "Wanderer" => array(1, 2), - "Graham" => array(1, 2), - "Pool" => array(2, 1), - "Courbet" => array(2, 1), - "Sunset" => array(2, 1), - "Sea" => array(2, 1), - "Creebet" => array(2, 1), - "Match" => array(2, 2), - "Bust" => array(2, 2), - "Stage" => array(2, 2), - "Void" => array(2, 2), - "SkullAndRoses" => array(2, 2), + "Kebab" => [1, 1], + "Aztec" => [1, 1], + "Alban" => [1, 1], + "Aztec2" => [1, 1], + "Bomb" => [1, 1], + "Plant" => [1, 1], + "Wasteland" => [1, 1], + "Wanderer" => [1, 2], + "Graham" => [1, 2], + "Pool" => [2, 1], + "Courbet" => [2, 1], + "Sunset" => [2, 1], + "Sea" => [2, 1], + "Creebet" => [2, 1], + "Match" => [2, 2], + "Bust" => [2, 2], + "Stage" => [2, 2], + "Void" => [2, 2], + "SkullAndRoses" => [2, 2], //"Wither" => array(2, 2), - "Fighters" => array(4, 2), - "Skeleton" => array(4, 3), - "DonkeyKong" => array(4, 3), - "Pointer" => array(4, 4), - "Pigscene" => array(4, 4), - "BurningSkull" => array(4, 4), - ); - private static $direction = array(2, 0, 1, 3); - private static $right = array(4, 5, 3, 2); - + "Fighters" => [4, 2], + "Skeleton" => [4, 3], + "DonkeyKong" => [4, 3], + "Pointer" => [4, 4], + "Pigscene" => [4, 4], + "BurningSkull" => [4, 4], + ]; + private static $direction = [2, 0, 1, 3]; + private static $right = [4, 5, 3, 2]; + public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ if($target->isTransparent === false and $face > 1 and $block->isSolid === false){ $server = ServerAPI::request(); - + if($face < 2 || $face > 5) return; - $data = array( + $data = [ "x" => $target->x, "y" => $target->y, "z" => $target->z, @@ -55,8 +55,8 @@ class PaintingItem extends Item{ "xPos" => $target->x, "yPos" => $target->y, "zPos" => $target->z, - ); - + ]; + $painting = new Painting($level, 0, ENTITY_OBJECT, OBJECT_PAINTING, $data); if(!$painting->isValid){ $player->sendInventory(); //force resync diff --git a/src/material/item/generic/SpawnEggItem.php b/src/material/item/generic/SpawnEggItem.php index f41e9a01..7d755b4f 100644 --- a/src/material/item/generic/SpawnEggItem.php +++ b/src/material/item/generic/SpawnEggItem.php @@ -6,19 +6,19 @@ class SpawnEggItem extends Item{ $this->meta = $meta; $this->isActivable = true; } - + public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $ageable = $this->meta === MOB_CHICKEN || $this->meta === MOB_COW || $this->meta === MOB_SHEEP || $this->meta === MOB_PIG; - $data = array( + $data = [ "x" => $block->x + 0.5, "y" => $block->y, "z" => $block->z + 0.5, "IsBaby" => $ageable ? mt_rand(0, 5) == 0 ? 1 : 0 : 0 - ); + ]; $e = ServerAPI::request()->api->entity->add($block->level, ENTITY_MOB, $this->meta, $data); ServerAPI::request()->api->entity->spawnToAll($e); if(($player->gamemode & 0x01) === 0){ - -- $this->count; + --$this->count; } return true; } diff --git a/src/material/item/tool/CompassItem.php b/src/material/item/tool/CompassItem.php index 9c2e1f83..f462f941 100755 --- a/src/material/item/tool/CompassItem.php +++ b/src/material/item/tool/CompassItem.php @@ -5,5 +5,5 @@ class CompassItem extends Item{ parent::__construct(COMPASS, 0, $count, "Compass"); $this->maxStackSize = 1; } - + } \ No newline at end of file diff --git a/src/material/item/tool/DiamondAxeItem.php b/src/material/item/tool/DiamondAxeItem.php index 7cb03b24..f602c05b 100644 --- a/src/material/item/tool/DiamondAxeItem.php +++ b/src/material/item/tool/DiamondAxeItem.php @@ -4,7 +4,7 @@ class DiamondAxeItem extends ItemAxe{ public function __construct($meta = 0, $count = 1){ parent::__construct(DIAMOND_AXE, $meta, $count, "Diamond Axe"); } - + public function getDamageAgainstOf($e){ return 6; } diff --git a/src/material/item/tool/DiamondPickaxeItem.php b/src/material/item/tool/DiamondPickaxeItem.php index b90fc882..608f26fc 100644 --- a/src/material/item/tool/DiamondPickaxeItem.php +++ b/src/material/item/tool/DiamondPickaxeItem.php @@ -4,7 +4,7 @@ class DiamondPickaxeItem extends ItemPickaxe{ public function __construct($meta = 0, $count = 1){ parent::__construct(DIAMOND_PICKAXE, $meta, $count, "Diamond Pickaxe"); } - + public function getDamageAgainstOf($e){ return 5; } diff --git a/src/material/item/tool/FlintSteelItem.php b/src/material/item/tool/FlintSteelItem.php index 134b160a..9de35dbb 100644 --- a/src/material/item/tool/FlintSteelItem.php +++ b/src/material/item/tool/FlintSteelItem.php @@ -6,19 +6,19 @@ class FlintSteelItem extends ItemTool{ $this->isActivable = true; $this->maxStackSize = 1; } - + public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ if(($player->gamemode & 0x01) === 0 and $this->useOn($block) and $this->getMetadata() >= $this->getMaxDurability()){ $player->setSlot($player->slot, new Item(AIR, 0, 0), false); } - + if($block->getID() === AIR && $target->isSolid){ $level->fastSetBlockUpdate($block->x, $block->y, $block->z, FIRE, 0, true); return true; } return false; } - + public function useOn($object, $force = false){ ++$this->meta; return true; diff --git a/src/material/item/tool/GoldenPickaxeItem.php b/src/material/item/tool/GoldenPickaxeItem.php index 7f0b2cb6..a8d41066 100755 --- a/src/material/item/tool/GoldenPickaxeItem.php +++ b/src/material/item/tool/GoldenPickaxeItem.php @@ -4,7 +4,7 @@ class GoldenPickaxeItem extends ItemPickaxe{ public function __construct($meta = 0, $count = 1){ parent::__construct(GOLDEN_PICKAXE, $meta, $count, "Golden Pickaxe"); } - + public function getDamageAgainstOf($e){ return 2; } diff --git a/src/material/item/tool/IronAxeItem.php b/src/material/item/tool/IronAxeItem.php index d8c5dc86..3a374466 100644 --- a/src/material/item/tool/IronAxeItem.php +++ b/src/material/item/tool/IronAxeItem.php @@ -4,7 +4,7 @@ class IronAxeItem extends ItemAxe{ public function __construct($meta = 0, $count = 1){ parent::__construct(IRON_AXE, $meta, $count, "Iron Axe"); } - + public function getDamageAgainstOf($e){ return 5; } diff --git a/src/material/item/tool/IronPickaxeItem.php b/src/material/item/tool/IronPickaxeItem.php index 620e5ea4..0e6332a2 100644 --- a/src/material/item/tool/IronPickaxeItem.php +++ b/src/material/item/tool/IronPickaxeItem.php @@ -4,7 +4,7 @@ class IronPickaxeItem extends ItemPickaxe{ public function __construct($meta = 0, $count = 1){ parent::__construct(IRON_PICKAXE, $meta, $count, "Iron Pickaxe"); } - + public function getDamageAgainstOf($e){ return 4; } diff --git a/src/material/item/tool/IronShovelItem.php b/src/material/item/tool/IronShovelItem.php index 321a714f..6f4cffda 100644 --- a/src/material/item/tool/IronShovelItem.php +++ b/src/material/item/tool/IronShovelItem.php @@ -4,7 +4,7 @@ class IronShovelItem extends ItemShovel{ public function __construct($meta = 0, $count = 1){ parent::__construct(IRON_SHOVEL, $meta, $count, "Iron Shovel"); } - + public function getDamageAgainstOf($e){ return 3; } diff --git a/src/material/item/tool/ShearsItem.php b/src/material/item/tool/ShearsItem.php index 059ede72..53ff3a11 100755 --- a/src/material/item/tool/ShearsItem.php +++ b/src/material/item/tool/ShearsItem.php @@ -4,7 +4,7 @@ class ShearsItem extends ItemTool{ public function __construct($meta = 0, $count = 1){ parent::__construct(SHEARS, $meta, $count, "Shears"); } - + public function useOn($object, $force = false){ if(($object instanceof Sheep) and $this->id === SHEARS){ $this->meta++; diff --git a/src/material/item/tool/StoneAxeItem.php b/src/material/item/tool/StoneAxeItem.php index b3f948f7..791e26f3 100644 --- a/src/material/item/tool/StoneAxeItem.php +++ b/src/material/item/tool/StoneAxeItem.php @@ -4,7 +4,7 @@ class StoneAxeItem extends ItemAxe{ public function __construct($meta = 0, $count = 1){ parent::__construct(STONE_AXE, $meta, $count, "Stone Axe"); } - + public function getDamageAgainstOf($e){ return 4; } diff --git a/src/material/item/tool/StonePickaxeItem.php b/src/material/item/tool/StonePickaxeItem.php index d7428123..22890581 100644 --- a/src/material/item/tool/StonePickaxeItem.php +++ b/src/material/item/tool/StonePickaxeItem.php @@ -4,7 +4,7 @@ class StonePickaxeItem extends ItemPickaxe{ public function __construct($meta = 0, $count = 1){ parent::__construct(STONE_PICKAXE, $meta, $count, "Stone Pickaxe"); } - + public function getDamageAgainstOf($e){ return 3; } diff --git a/src/material/item/tool/StoneShovelItem.php b/src/material/item/tool/StoneShovelItem.php index c23e42c6..d780f961 100644 --- a/src/material/item/tool/StoneShovelItem.php +++ b/src/material/item/tool/StoneShovelItem.php @@ -4,9 +4,9 @@ class StoneShovelItem extends ItemShovel{ public function __construct($meta = 0, $count = 1){ parent::__construct(STONE_SHOVEL, $meta, $count, "Stone Shovel"); } - + public function getDamageAgainstOf($e){ return 2; } - + } \ No newline at end of file diff --git a/src/material/item/tool/WoodenAxeItem.php b/src/material/item/tool/WoodenAxeItem.php index c5f5f49d..b573d1bf 100644 --- a/src/material/item/tool/WoodenAxeItem.php +++ b/src/material/item/tool/WoodenAxeItem.php @@ -4,7 +4,7 @@ class WoodenAxeItem extends ItemAxe{ public function __construct($meta = 0, $count = 1){ parent::__construct(WOODEN_AXE, $meta, $count, "Wooden Axe"); } - + public function getDamageAgainstOf($e){ return 3; } diff --git a/src/material/item/tool/WoodenPickaxeItem.php b/src/material/item/tool/WoodenPickaxeItem.php index a6f9fd21..e173e3c6 100644 --- a/src/material/item/tool/WoodenPickaxeItem.php +++ b/src/material/item/tool/WoodenPickaxeItem.php @@ -4,7 +4,7 @@ class WoodenPickaxeItem extends ItemPickaxe{ public function __construct($meta = 0, $count = 1){ parent::__construct(WOODEN_PICKAXE, $meta, $count, "Wooden Pickaxe"); } - + public function getDamageAgainstOf($e){ return 2; } diff --git a/src/math/AxisAlignedBB.php b/src/math/AxisAlignedBB.php index 187700ca..d778842b 100644 --- a/src/math/AxisAlignedBB.php +++ b/src/math/AxisAlignedBB.php @@ -1,4 +1,5 @@ minX = $minX; $this->minY = $minY; @@ -30,7 +31,7 @@ class AxisAlignedBB{ public function addMinMax($minX, $minY, $minZ, $maxX, $maxY, $maxZ){ return new AxisAlignedBB($this->minX + $minX, $this->minY + $minY, $this->minZ + $minZ, $this->maxX + $maxX, $this->maxY + $maxY, $this->maxZ + $maxZ); } - + public function addCoord($x, $y, $z){ $minX = $this->minX; $minY = $this->minY; @@ -169,7 +170,7 @@ class AxisAlignedBB{ return $z; } - + public function intersectsWith(AxisAlignedBB $bb){ return $bb->maxX >= $this->minX && $bb->minX <= $this->maxX && $bb->maxZ >= $this->minZ && $bb->minZ <= $this->maxZ && $bb->maxY >= $this->minY && $bb->minY <= $this->maxY; } @@ -180,7 +181,7 @@ class AxisAlignedBB{ if($y < $this->minY or $y > $this->maxY){ return false; } - + return $z > $this->minZ and $z < $this->maxZ; } public function isXYZInside($x, $y, $z){ @@ -190,7 +191,7 @@ class AxisAlignedBB{ if($y < $this->minY or $y > $this->maxY){ return false; } - + return $z > $this->minZ and $z < $this->maxZ; } public function isVectorInside(Vector3 $vector){ @@ -219,10 +220,8 @@ class AxisAlignedBB{ public function isVectorInXY(Vector3 $vector){ return $vector->x >= $this->minX and $vector->x <= $this->maxX and $vector->y >= $this->minY and $vector->y <= $this->maxY; } - + /** - * @param Vector3 $pos1 - * @param Vector3 $pos2 * @return NULL|MovingObjectPosition */ public function calculateIntercept(Vector3 $pos1, Vector3 $pos2){ @@ -301,9 +300,9 @@ class AxisAlignedBB{ return MovingObjectPosition::fromBlock(0, 0, 0, $f, $vector); } - + public function __toString(){ return "AABB min: {$this->minX}, {$this->minY}, {$this->minZ}, max: {$this->maxX}, {$this->maxY}, {$this->maxZ}"; } - + } \ No newline at end of file diff --git a/src/math/Matrix.php b/src/math/Matrix.php index d2f3b245..8d74c09f 100644 --- a/src/math/Matrix.php +++ b/src/math/Matrix.php @@ -148,7 +148,6 @@ class Matrix implements ArrayAccess{ }; } - //Computation of the determinant of 2x2 and 3x3 matrices public function isSquare(){ diff --git a/src/math/Vector3.php b/src/math/Vector3.php index 03dcb015..eff4b30d 100644 --- a/src/math/Vector3.php +++ b/src/math/Vector3.php @@ -2,27 +2,27 @@ class Vector3{ public $x, $y, $z; - + public function __construct($x = 0, $y = 0, $z = 0){ $this->x = $x; $this->y = $y; $this->z = $z; } - + public function setXYZ($x, $y, $z){ $this->x = $x; $this->y = $y; $this->z = $z; } - + public function copy(){ return new Vector3($this->x, $this->y, $this->z); } - + public function toArray(){ return ["x" => $this->x, "y" => $this->y, "z" => $this->z]; } - + public function getFloorX(){ return (int) $this->x; } @@ -104,7 +104,7 @@ class Vector3{ } public function getSide($side, $step = 1){ - return match ((int)$side) { + return match ((int) $side) { 0 => new Vector3($this->x, $this->y - $step, $this->z), 1 => new Vector3($this->x, $this->y + $step, $this->z), 2 => new Vector3($this->x, $this->y, $this->z - $step), @@ -127,7 +127,7 @@ class Vector3{ if($x instanceof Vector3){ return $this->distanceSquared($x->x, $x->y, $x->z); }else{ - return ($this->x - $x)*($this->x - $x) + ($this->y - $y)*($this->y - $y) + ($this->z - $z)*($this->z - $z); + return ($this->x - $x) * ($this->x - $x) + ($this->y - $y) * ($this->y - $y) + ($this->z - $z) * ($this->z - $z); } } @@ -170,21 +170,19 @@ class Vector3{ $this->x * $v->y - $this->y * $v->x ); } - + public static function fromArray($arr){ return new Vector3($arr[0], $arr[1], $arr[2]); } - + public function __toString(){ return "Vector3(x=" . $this->x . ",y=" . $this->y . ",z=" . $this->z . ")"; } - - + /** * Returns a new vector with x value equal to the second parameter, along the line between this vector and the * passed in vector, or null if not possible. * - * @param Vector3 $v * @param float $x * * @return Vector3 @@ -193,13 +191,13 @@ class Vector3{ $xDiff = $v->x - $this->x; $yDiff = $v->y - $this->y; $zDiff = $v->z - $this->z; - + if(($xDiff * $xDiff) < 0.0000001){ return null; } - + $f = ($x - $this->x) / $xDiff; - + if($f < 0 or $f > 1){ return null; }else{ @@ -209,12 +207,11 @@ class Vector3{ public function getIntermediateWithXValue(Vector3 $v, $y){ return $this->clipX($v, $y); } - + /** * Returns a new vector with y value equal to the second parameter, along the line between this vector and the * passed in vector, or null if not possible. * - * @param Vector3 $v * @param float $y * * @return Vector3 @@ -223,20 +220,20 @@ class Vector3{ $xDiff = $v->x - $this->x; $yDiff = $v->y - $this->y; $zDiff = $v->z - $this->z; - + if(($yDiff * $yDiff) < 0.0000001){ return null; } - + $f = ($y - $this->y) / $yDiff; - + if($f < 0 or $f > 1){ return null; }else{ return new Vector3($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f); } } - + public function getIntermediateWithYValue(Vector3 $v, $y){ return $this->clipY($v, $y); } @@ -244,7 +241,6 @@ class Vector3{ * Returns a new vector with z value equal to the second parameter, along the line between this vector and the * passed in vector, or null if not possible. * - * @param Vector3 $v * @param float $z * * @return Vector3 @@ -253,20 +249,20 @@ class Vector3{ $xDiff = $v->x - $this->x; $yDiff = $v->y - $this->y; $zDiff = $v->z - $this->z; - + if(($zDiff * $zDiff) < 0.0000001){ return null; } - + $f = ($z - $this->z) / $zDiff; - + if($f < 0 or $f > 1){ return null; }else{ return new Vector3($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f); } } - + public function getIntermediateWithZValue(Vector3 $v, $y){ return $this->clipZ($v, $y); } diff --git a/src/network/MinecraftInterface.php b/src/network/MinecraftInterface.php index ad91322e..f3c084a9 100644 --- a/src/network/MinecraftInterface.php +++ b/src/network/MinecraftInterface.php @@ -55,7 +55,7 @@ class MinecraftInterface{ $packet = new QueryPacket; $packet->ip = $source; $packet->port = $port; - $packet->buffer =& $buffer; + $packet->buffer = &$buffer; if(EventHandler::callEvent(new PacketReceiveEvent($packet)) === BaseEvent::DENY){ return false; } @@ -64,7 +64,7 @@ class MinecraftInterface{ $packet = new Packet(); $packet->ip = $source; $packet->port = $port; - $packet->buffer =& $buffer; + $packet->buffer = &$buffer; EventHandler::callEvent(new PacketReceiveEvent($packet)); return false; } diff --git a/src/network/RCON.php b/src/network/RCON.php index 60f41149..9492cc57 100644 --- a/src/network/RCON.php +++ b/src/network/RCON.php @@ -10,7 +10,7 @@ class RCON{ * @var Array[RCONInstance] */ private $workers; - + private $socket, $password, $threads, $clientsPerThread; public function __construct($password, $port = 19132, $interface = "0.0.0.0", $threads = 1, $clientsPerThread = 50){ @@ -83,7 +83,7 @@ class RCONInstance extends Thread{ $this->maxClients = (int) $maxClients; $this->start(); } - + public function close(){ $this->stop = true; } @@ -108,7 +108,7 @@ class RCONInstance extends Thread{ }else{ socket_set_nonblock($client); socket_set_option($client, SOL_SOCKET, SO_KEEPALIVE, 1); - + $id = $nextClientId++; $clients[$id] = $client; $authenticated[$id] = false; @@ -121,7 +121,7 @@ class RCONInstance extends Thread{ $disconnect[$id] = $sock; continue; } - + switch($packetType){ case 3: //Login if($authenticated[$id]){ @@ -160,19 +160,18 @@ class RCONInstance extends Thread{ } } } - + foreach($authenticated as $id => $status){ if(!isset($disconnect[$id]) and !$authenticated[$id] and $timeouts[$id] < microtime(true)){ //Timeout $disconnect[$id] = $clients[$id]; } } - + foreach($disconnect as $id => $client){ $this->disconnectClient($client); unset($clients[$id], $authenticated[$id], $timeouts[$id]); } - - + } foreach($clients as $client){ $this->disconnectClient($client); @@ -180,7 +179,7 @@ class RCONInstance extends Thread{ unset($this->socket, $this->cmd, $this->response, $this->stop); exit(0); } - + private function disconnectClient($client){ socket_getpeername($client, $ip, $port); @socket_set_option($client, SOL_SOCKET, SO_LINGER, ["l_onoff" => 1, "l_linger" => 1]); @@ -190,7 +189,7 @@ class RCONInstance extends Thread{ @socket_close($client); console("Disconnected client: /$ip:$port"); } - + private function readPacket($client, &$size, &$requestID, &$packetType, &$payload){ @socket_set_nonblock($client); $d = @socket_read($client, 4); diff --git a/src/network/UDPSocket.php b/src/network/UDPSocket.php index 15cf0b3c..772df211 100644 --- a/src/network/UDPSocket.php +++ b/src/network/UDPSocket.php @@ -3,7 +3,7 @@ class UDPSocket{ public $connected, $sock, $server, $port; - + function __construct($server, $port, $listen = false, $serverip = "0.0.0.0"){ $this->server = $server; $this->port = $port; diff --git a/src/network/protocol/ProtocolInfo.php b/src/network/protocol/ProtocolInfo.php index fc6c6b8f..e20f7461 100644 --- a/src/network/protocol/ProtocolInfo.php +++ b/src/network/protocol/ProtocolInfo.php @@ -1,6 +1,5 @@ reset(); $this->putInt($this->eid); @@ -28,9 +28,9 @@ class AddEntityPacket extends RakNetDataPacket{ $this->putFloat($this->z); $this->putInt($this->did); if($this->did > 0){ - $this->putShort((int)($this->speedX * 8000)); - $this->putShort((int)($this->speedY * 8000)); - $this->putShort((int)($this->speedZ * 8000)); + $this->putShort((int) ($this->speedX * 8000)); + $this->putShort((int) ($this->speedY * 8000)); + $this->putShort((int) ($this->speedZ * 8000)); } } diff --git a/src/network/protocol/packet/AddItemEntityPacket.php b/src/network/protocol/packet/AddItemEntityPacket.php index fb68f60a..60cd6143 100644 --- a/src/network/protocol/packet/AddItemEntityPacket.php +++ b/src/network/protocol/packet/AddItemEntityPacket.php @@ -9,15 +9,15 @@ class AddItemEntityPacket extends RakNetDataPacket{ public $yaw; public $pitch; public $roll; - + public function pid(){ return ProtocolInfo::ADD_ITEM_ENTITY_PACKET; } - + public function decode(){ } - + public function encode(){ $this->reset(); $this->putInt($this->eid); diff --git a/src/network/protocol/packet/AddMobPacket.php b/src/network/protocol/packet/AddMobPacket.php index 5b2d9a90..4a469196 100644 --- a/src/network/protocol/packet/AddMobPacket.php +++ b/src/network/protocol/packet/AddMobPacket.php @@ -9,15 +9,15 @@ class AddMobPacket extends RakNetDataPacket{ public $pitch; public $yaw; public $metadata; - + public function pid(){ return ProtocolInfo::ADD_MOB_PACKET; } - + public function decode(){ } - + public function encode(){ $this->reset(); $this->putInt($this->eid); diff --git a/src/network/protocol/packet/AddPaintingPacket.php b/src/network/protocol/packet/AddPaintingPacket.php index bd50ab0a..96d8af2f 100644 --- a/src/network/protocol/packet/AddPaintingPacket.php +++ b/src/network/protocol/packet/AddPaintingPacket.php @@ -7,15 +7,15 @@ class AddPaintingPacket extends RakNetDataPacket{ public $z; public $direction; public $title; - + public function pid(){ return ProtocolInfo::ADD_PAINTING_PACKET; } - + public function decode(){ } - + public function encode(){ $this->reset(); $this->putInt($this->eid); diff --git a/src/network/protocol/packet/AddPlayerPacket.php b/src/network/protocol/packet/AddPlayerPacket.php index 044f119a..709c1a30 100644 --- a/src/network/protocol/packet/AddPlayerPacket.php +++ b/src/network/protocol/packet/AddPlayerPacket.php @@ -23,15 +23,15 @@ class AddPlayerPacket extends RakNetDataPacket{ *@var array */ public $metadata; - + public function pid(){ return ProtocolInfo::ADD_PLAYER_PACKET; } - + public function decode(){ } - + public function encode(){ $this->reset(); $this->putLong($this->clientID); diff --git a/src/network/protocol/packet/AdventureSettingsPacket.php b/src/network/protocol/packet/AdventureSettingsPacket.php index b395975c..9cf23907 100644 --- a/src/network/protocol/packet/AdventureSettingsPacket.php +++ b/src/network/protocol/packet/AdventureSettingsPacket.php @@ -2,15 +2,15 @@ class AdventureSettingsPacket extends RakNetDataPacket{ public $flags; - + public function pid(){ return ProtocolInfo::ADVENTURE_SETTINGS_PACKET; } - + public function decode(){ } - + public function encode(){ $this->reset(); $this->putInt($this->flags); diff --git a/src/network/protocol/packet/AnimatePacket.php b/src/network/protocol/packet/AnimatePacket.php index a8ea6820..3f537d26 100644 --- a/src/network/protocol/packet/AnimatePacket.php +++ b/src/network/protocol/packet/AnimatePacket.php @@ -3,19 +3,19 @@ class AnimatePacket extends RakNetDataPacket{ const ANIM_SWING_HAND = 0x1; const ANIM_STOP_SLEEP = 0x3; - + public $action; public $eid; - + public function pid(){ return ProtocolInfo::ANIMATE_PACKET; } - + public function decode(){ $this->action = $this->getByte(); $this->eid = $this->getInt(); } - + public function encode(){ $this->reset(); $this->putByte($this->action); diff --git a/src/network/protocol/packet/ChatPacket.php b/src/network/protocol/packet/ChatPacket.php index cbe5998a..183bbae3 100644 --- a/src/network/protocol/packet/ChatPacket.php +++ b/src/network/protocol/packet/ChatPacket.php @@ -2,15 +2,15 @@ class ChatPacket extends RakNetDataPacket{ public $message; - + public function pid(){ return ProtocolInfo::CHAT_PACKET; } - + public function decode(){ } - + public function encode(){ $this->reset(); $this->putString($this->message); diff --git a/src/network/protocol/packet/ChunkDataPacket.php b/src/network/protocol/packet/ChunkDataPacket.php index 024bebbb..77d61816 100644 --- a/src/network/protocol/packet/ChunkDataPacket.php +++ b/src/network/protocol/packet/ChunkDataPacket.php @@ -4,15 +4,15 @@ class ChunkDataPacket extends RakNetDataPacket{ public $chunkX; public $chunkZ; public $data; - + public function pid(){ return ProtocolInfo::CHUNK_DATA_PACKET; } - + public function decode(){ } - + public function encode(){ $this->reset(); $this->putInt($this->chunkX); diff --git a/src/network/protocol/packet/ClientConnectPacket.php b/src/network/protocol/packet/ClientConnectPacket.php index f4a95243..b71e80e6 100644 --- a/src/network/protocol/packet/ClientConnectPacket.php +++ b/src/network/protocol/packet/ClientConnectPacket.php @@ -8,13 +8,13 @@ class ClientConnectPacket extends RakNetDataPacket{ public function pid(){ return ProtocolInfo::CLIENT_CONNECT_PACKET; } - + public function decode(){ $this->clientID = $this->getLong(); $this->session = $this->getLong(); $this->unknown1 = $this->get(1); - } - + } + public function encode(){ } diff --git a/src/network/protocol/packet/ClientHandshakePacket.php b/src/network/protocol/packet/ClientHandshakePacket.php index 7d807d9c..9cce1aa2 100644 --- a/src/network/protocol/packet/ClientHandshakePacket.php +++ b/src/network/protocol/packet/ClientHandshakePacket.php @@ -13,7 +13,7 @@ class ClientHandshakePacket extends RakNetDataPacket{ public function pid(){ return ProtocolInfo::CLIENT_HANDSHAKE_PACKET; } - + public function decode(){ $this->cookie = $this->get(4); $this->security = $this->get(1); @@ -23,10 +23,10 @@ class ClientHandshakePacket extends RakNetDataPacket{ $this->timestamp = $this->get(2); $this->session2 = $this->getLong(); $this->session = $this->getLong(); - } - + } + public function encode(){ - + } } \ No newline at end of file diff --git a/src/network/protocol/packet/ContainerClosePacket.php b/src/network/protocol/packet/ContainerClosePacket.php index 008d8097..77623187 100644 --- a/src/network/protocol/packet/ContainerClosePacket.php +++ b/src/network/protocol/packet/ContainerClosePacket.php @@ -2,15 +2,15 @@ class ContainerClosePacket extends RakNetDataPacket{ public $windowid; - + public function pid(){ return ProtocolInfo::CONTAINER_CLOSE_PACKET; } - + public function decode(){ $this->windowid = $this->getByte(); } - + public function encode(){ $this->reset(); $this->putByte($this->windowid); diff --git a/src/network/protocol/packet/ContainerOpenPacket.php b/src/network/protocol/packet/ContainerOpenPacket.php index 92eecd78..b1d8a9df 100644 --- a/src/network/protocol/packet/ContainerOpenPacket.php +++ b/src/network/protocol/packet/ContainerOpenPacket.php @@ -7,15 +7,15 @@ class ContainerOpenPacket extends RakNetDataPacket{ public $x; public $y; public $z; - + public function pid(){ return ProtocolInfo::CONTAINER_OPEN_PACKET; } - + public function decode(){ } - + public function encode(){ $this->reset(); $this->putByte($this->windowid); diff --git a/src/network/protocol/packet/ContainerSetContentPacket.php b/src/network/protocol/packet/ContainerSetContentPacket.php index a4afe1db..d27c9b61 100644 --- a/src/network/protocol/packet/ContainerSetContentPacket.php +++ b/src/network/protocol/packet/ContainerSetContentPacket.php @@ -2,13 +2,13 @@ class ContainerSetContentPacket extends RakNetDataPacket{ public $windowid; - public $slots = array(); - public $hotbar = array(); - + public $slots = []; + public $hotbar = []; + public function pid(){ return ProtocolInfo::CONTAINER_SET_CONTENT_PACKET; } - + public function decode(){ $this->windowid = $this->getByte(); $count = $this->getShort(); @@ -22,7 +22,7 @@ class ContainerSetContentPacket extends RakNetDataPacket{ } } } - + public function encode(){ $this->reset(); $this->putByte($this->windowid); diff --git a/src/network/protocol/packet/ContainerSetDataPacket.php b/src/network/protocol/packet/ContainerSetDataPacket.php index 0d93c4e1..f6e4c967 100644 --- a/src/network/protocol/packet/ContainerSetDataPacket.php +++ b/src/network/protocol/packet/ContainerSetDataPacket.php @@ -4,15 +4,15 @@ class ContainerSetDataPacket extends RakNetDataPacket{ public $windowid; public $property; public $value; - + public function pid(){ return ProtocolInfo::CONTAINER_SET_DATA_PACKET; } - + public function decode(){ } - + public function encode(){ $this->reset(); $this->putByte($this->windowid); diff --git a/src/network/protocol/packet/ContainerSetSlotPacket.php b/src/network/protocol/packet/ContainerSetSlotPacket.php index fab25153..b258408e 100644 --- a/src/network/protocol/packet/ContainerSetSlotPacket.php +++ b/src/network/protocol/packet/ContainerSetSlotPacket.php @@ -4,17 +4,17 @@ class ContainerSetSlotPacket extends RakNetDataPacket{ public $windowid; public $slot; public $item; - + public function pid(){ return ProtocolInfo::CONTAINER_SET_SLOT_PACKET; } - + public function decode(){ $this->windowid = $this->getByte(); $this->slot = $this->getShort(); $this->item = $this->getSlot(); } - + public function encode(){ $this->reset(); $this->putByte($this->windowid); diff --git a/src/network/protocol/packet/DisconnectPacket.php b/src/network/protocol/packet/DisconnectPacket.php index f0cc9fe8..17a4effb 100644 --- a/src/network/protocol/packet/DisconnectPacket.php +++ b/src/network/protocol/packet/DisconnectPacket.php @@ -4,13 +4,13 @@ class DisconnectPacket extends RakNetDataPacket{ public function pid(){ return ProtocolInfo::DISCONNECT_PACKET; } - + public function decode(){ - } - + } + public function encode(){ - $this->reset(); + $this->reset(); } } \ No newline at end of file diff --git a/src/network/protocol/packet/DropItemPacket.php b/src/network/protocol/packet/DropItemPacket.php index 41531160..12e1acc6 100644 --- a/src/network/protocol/packet/DropItemPacket.php +++ b/src/network/protocol/packet/DropItemPacket.php @@ -4,17 +4,17 @@ class DropItemPacket extends RakNetDataPacket{ public $eid; public $unknown; public $item; - + public function pid(){ return ProtocolInfo::DROP_ITEM_PACKET; } - + public function decode(){ $this->eid = $this->getInt(); $this->unknown = $this->getByte(); $this->item = $this->getSlot(); } - + public function encode(){ } diff --git a/src/network/protocol/packet/EntityDataPacket.php b/src/network/protocol/packet/EntityDataPacket.php index 01b2cafb..e834aa17 100644 --- a/src/network/protocol/packet/EntityDataPacket.php +++ b/src/network/protocol/packet/EntityDataPacket.php @@ -5,18 +5,18 @@ class EntityDataPacket extends RakNetDataPacket{ public $y; public $z; public $namedtag; - + public function pid(){ return ProtocolInfo::ENTITY_DATA_PACKET; } - + public function decode(){ $this->x = $this->getShort(); $this->y = $this->getByte(); $this->z = $this->getShort(); $this->namedtag = $this->get(true); } - + public function encode(){ $this->reset(); $this->putShort($this->x); diff --git a/src/network/protocol/packet/EntityEventPacket.php b/src/network/protocol/packet/EntityEventPacket.php index 05cf7770..24ae3442 100644 --- a/src/network/protocol/packet/EntityEventPacket.php +++ b/src/network/protocol/packet/EntityEventPacket.php @@ -3,25 +3,25 @@ class EntityEventPacket extends RakNetDataPacket{ public $eid; public $event; - + const ENTITY_DAMAGE = 2; const ENTITY_DEAD = 3; const ENTITY_ANIM_10 = 10; - + public function __construct($eid = null, $event = null){ $this->eid = $eid; $this->event = $event; } - + public function pid(){ return ProtocolInfo::ENTITY_EVENT_PACKET; } - + public function decode(){ $this->eid = $this->getInt(); $this->event = $this->getByte(); } - + public function encode(){ $this->reset(); $this->putInt($this->eid); diff --git a/src/network/protocol/packet/ExplodePacket.php b/src/network/protocol/packet/ExplodePacket.php index 1d60b72a..4a70d00e 100644 --- a/src/network/protocol/packet/ExplodePacket.php +++ b/src/network/protocol/packet/ExplodePacket.php @@ -6,15 +6,15 @@ class ExplodePacket extends RakNetDataPacket{ public $z; public $radius; public $records; - + public function pid(){ return ProtocolInfo::EXPLODE_PACKET; } - + public function decode(){ } - + public function encode(){ $this->reset(); $this->putFloat($this->x); diff --git a/src/network/protocol/packet/HurtArmorPacket.php b/src/network/protocol/packet/HurtArmorPacket.php index e23aac3e..1473c4f7 100644 --- a/src/network/protocol/packet/HurtArmorPacket.php +++ b/src/network/protocol/packet/HurtArmorPacket.php @@ -2,15 +2,15 @@ class HurtArmorPacket extends RakNetDataPacket{ public $health; - + public function pid(){ return ProtocolInfo::HURT_ARMOR_PACKET; } - + public function decode(){ } - + public function encode(){ $this->reset(); $this->putByte($this->health); diff --git a/src/network/protocol/packet/InteractPacket.php b/src/network/protocol/packet/InteractPacket.php index 2d33773d..de954ccd 100644 --- a/src/network/protocol/packet/InteractPacket.php +++ b/src/network/protocol/packet/InteractPacket.php @@ -11,13 +11,13 @@ class InteractPacket extends RakNetDataPacket{ public function pid(){ return ProtocolInfo::INTERACT_PACKET; } - + public function decode(){ $this->action = $this->getByte(); $this->eid = $this->getInt(); $this->target = $this->getInt(); } - + public function encode(){ $this->reset(); $this->putByte($this->action); diff --git a/src/network/protocol/packet/LevelEventPacket.php b/src/network/protocol/packet/LevelEventPacket.php index 50727e45..f38304d9 100644 --- a/src/network/protocol/packet/LevelEventPacket.php +++ b/src/network/protocol/packet/LevelEventPacket.php @@ -1,24 +1,24 @@ reset(); $this->putShort($this->evid); diff --git a/src/network/protocol/packet/LoginPacket.php b/src/network/protocol/packet/LoginPacket.php index 34f04955..e5f23dfb 100644 --- a/src/network/protocol/packet/LoginPacket.php +++ b/src/network/protocol/packet/LoginPacket.php @@ -6,21 +6,21 @@ class LoginPacket extends RakNetDataPacket{ public $protocol2; public $clientId; public $loginData; - + public function pid(){ return ProtocolInfo::LOGIN_PACKET; } - + public function decode(){ $this->username = $this->getString(); $this->protocol1 = $this->getInt(); $this->protocol2 = $this->getInt(); $this->clientId = $this->getInt(); $this->loginData = $this->getString(); - } - + } + public function encode(){ - + } } \ No newline at end of file diff --git a/src/network/protocol/packet/LoginStatusPacket.php b/src/network/protocol/packet/LoginStatusPacket.php index bd501917..ef02b0d2 100644 --- a/src/network/protocol/packet/LoginStatusPacket.php +++ b/src/network/protocol/packet/LoginStatusPacket.php @@ -2,15 +2,15 @@ class LoginStatusPacket extends RakNetDataPacket{ public $status; - + public function pid(){ return ProtocolInfo::LOGIN_STATUS_PACKET; } - + public function decode(){ - } - + } + public function encode(){ $this->reset(); $this->putInt($this->status); diff --git a/src/network/protocol/packet/MessagePacket.php b/src/network/protocol/packet/MessagePacket.php index dcb1b0bb..ab29f912 100644 --- a/src/network/protocol/packet/MessagePacket.php +++ b/src/network/protocol/packet/MessagePacket.php @@ -3,16 +3,16 @@ class MessagePacket extends RakNetDataPacket{ public $source; public $message; - + public function pid(){ return ProtocolInfo::MESSAGE_PACKET; } - + public function decode(){ $this->source = $this->getString(); $this->message = $this->getString(); - } - + } + public function encode(){ $this->reset(); $this->putString($this->source); diff --git a/src/network/protocol/packet/MoveEntityPacket.php b/src/network/protocol/packet/MoveEntityPacket.php index be59077c..76f62187 100644 --- a/src/network/protocol/packet/MoveEntityPacket.php +++ b/src/network/protocol/packet/MoveEntityPacket.php @@ -5,11 +5,11 @@ class MoveEntityPacket extends RakNetDataPacket{ public function pid(){ return ProtocolInfo::MOVE_ENTITY_PACKET; } - + public function decode(){ } - + public function encode(){ $this->reset(); } diff --git a/src/network/protocol/packet/MoveEntityPacket_PosRot.php b/src/network/protocol/packet/MoveEntityPacket_PosRot.php index c2143c18..ef92f985 100644 --- a/src/network/protocol/packet/MoveEntityPacket_PosRot.php +++ b/src/network/protocol/packet/MoveEntityPacket_PosRot.php @@ -7,11 +7,11 @@ class MoveEntityPacket_PosRot extends RakNetDataPacket{ public $z; public $yaw; public $pitch; - + public function pid(){ return ProtocolInfo::MOVE_ENTITY_PACKET_POSROT; } - + public function decode(){ $this->get(7); $this->eid = $this->getInt(); @@ -21,7 +21,7 @@ class MoveEntityPacket_PosRot extends RakNetDataPacket{ $this->yaw = $this->getFloat(); $this->pitch = $this->getFloat(); } - + public function encode(){ $this->reset(); $this->putInt($this->eid); diff --git a/src/network/protocol/packet/MovePlayerPacket.php b/src/network/protocol/packet/MovePlayerPacket.php index 5325cf76..57d71021 100644 --- a/src/network/protocol/packet/MovePlayerPacket.php +++ b/src/network/protocol/packet/MovePlayerPacket.php @@ -8,11 +8,11 @@ class MovePlayerPacket extends RakNetDataPacket{ public $yaw; public $pitch; public $bodyYaw; - + public function pid(){ return ProtocolInfo::MOVE_PLAYER_PACKET; } - + public function decode(){ $this->eid = $this->getInt(); $this->x = $this->getFloat(); @@ -22,7 +22,7 @@ class MovePlayerPacket extends RakNetDataPacket{ $this->pitch = $this->getFloat(); $this->bodyYaw = $this->getFloat(); } - + public function encode(){ $this->reset(); $this->putInt($this->eid); diff --git a/src/network/protocol/packet/PingPacket.php b/src/network/protocol/packet/PingPacket.php index 77e380dc..92eefb03 100644 --- a/src/network/protocol/packet/PingPacket.php +++ b/src/network/protocol/packet/PingPacket.php @@ -6,11 +6,11 @@ class PingPacket extends RakNetDataPacket{ public function pid(){ return ProtocolInfo::PING_PACKET; } - + public function decode(){ $this->time = $this->getLong(); - } - + } + public function encode(){ $this->reset(); $this->putLong($this->time); diff --git a/src/network/protocol/packet/PlayerActionPacket.php b/src/network/protocol/packet/PlayerActionPacket.php index cdcc64ad..2f8cff20 100644 --- a/src/network/protocol/packet/PlayerActionPacket.php +++ b/src/network/protocol/packet/PlayerActionPacket.php @@ -7,11 +7,11 @@ class PlayerActionPacket extends RakNetDataPacket{ public $z; public $face; public $eid; - + public function pid(){ return ProtocolInfo::PLAYER_ACTION_PACKET; } - + public function decode(){ $this->action = $this->getInt(); $this->x = $this->getInt(); @@ -20,7 +20,7 @@ class PlayerActionPacket extends RakNetDataPacket{ $this->face = $this->getInt(); $this->eid = $this->getInt(); } - + public function encode(){ } diff --git a/src/network/protocol/packet/PlayerArmorEquipmentPacket.php b/src/network/protocol/packet/PlayerArmorEquipmentPacket.php index d32be316..faa519f7 100644 --- a/src/network/protocol/packet/PlayerArmorEquipmentPacket.php +++ b/src/network/protocol/packet/PlayerArmorEquipmentPacket.php @@ -2,20 +2,20 @@ class PlayerArmorEquipmentPacket extends RakNetDataPacket{ public $eid; - public $slots = array(); - + public $slots = []; + public function pid(){ return ProtocolInfo::PLAYER_ARMOR_EQUIPMENT_PACKET; } - + public function decode(){ - $this->eid = $this->getInt(); + $this->eid = $this->getInt(); $this->slots[0] = $this->getByte(); $this->slots[1] = $this->getByte(); $this->slots[2] = $this->getByte(); $this->slots[3] = $this->getByte(); } - + public function encode(){ $this->reset(); $this->putInt($this->eid); diff --git a/src/network/protocol/packet/PlayerEquipmentPacket.php b/src/network/protocol/packet/PlayerEquipmentPacket.php index 06e65fee..b9596f64 100644 --- a/src/network/protocol/packet/PlayerEquipmentPacket.php +++ b/src/network/protocol/packet/PlayerEquipmentPacket.php @@ -5,18 +5,18 @@ class PlayerEquipmentPacket extends RakNetDataPacket{ public $item; public $meta; public $slot; - + public function pid(){ return ProtocolInfo::PLAYER_EQUIPMENT_PACKET; } - + public function decode(){ $this->eid = $this->getInt(); $this->item = $this->getShort(); $this->meta = $this->getShort(); $this->slot = $this->getSignedByte(); } - + public function encode(){ $this->reset(); $this->putInt($this->eid); diff --git a/src/network/protocol/packet/PlayerInputPacket.php b/src/network/protocol/packet/PlayerInputPacket.php index f80f18bc..fdc79731 100644 --- a/src/network/protocol/packet/PlayerInputPacket.php +++ b/src/network/protocol/packet/PlayerInputPacket.php @@ -1,9 +1,10 @@ isJumping = $this->getByte() != 0; $this->isSneaking = $this->getByte() != 0; } - + } diff --git a/src/network/protocol/packet/PongPacket.php b/src/network/protocol/packet/PongPacket.php index ab1a1948..ab9da4fa 100644 --- a/src/network/protocol/packet/PongPacket.php +++ b/src/network/protocol/packet/PongPacket.php @@ -7,12 +7,12 @@ class PongPacket extends RakNetDataPacket{ public function pid(){ return ProtocolInfo::PONG_PACKET; } - + public function decode(){ $this->ptime = $this->getLong(); $this->time = $this->getLong(); - } - + } + public function encode(){ $this->reset(); $this->putLong($this->ptime); diff --git a/src/network/protocol/packet/ReadyPacket.php b/src/network/protocol/packet/ReadyPacket.php index 2fa12b5e..08cd53a1 100644 --- a/src/network/protocol/packet/ReadyPacket.php +++ b/src/network/protocol/packet/ReadyPacket.php @@ -2,15 +2,15 @@ class ReadyPacket extends RakNetDataPacket{ public $status; - + public function pid(){ return ProtocolInfo::READY_PACKET; } - + public function decode(){ $this->status = $this->getByte(); - } - + } + public function encode(){ } diff --git a/src/network/protocol/packet/RemoveBlockPacket.php b/src/network/protocol/packet/RemoveBlockPacket.php index d3e3a969..2f77ea64 100644 --- a/src/network/protocol/packet/RemoveBlockPacket.php +++ b/src/network/protocol/packet/RemoveBlockPacket.php @@ -5,18 +5,18 @@ class RemoveBlockPacket extends RakNetDataPacket{ public $x; public $y; public $z; - + public function pid(){ return ProtocolInfo::REMOVE_BLOCK_PACKET; } - + public function decode(){ $this->eid = $this->getInt(); $this->x = $this->getInt(); $this->z = $this->getInt(); $this->y = $this->getByte(); } - + public function encode(){ } diff --git a/src/network/protocol/packet/RemoveEntityPacket.php b/src/network/protocol/packet/RemoveEntityPacket.php index 27d6c42b..edeef1ea 100644 --- a/src/network/protocol/packet/RemoveEntityPacket.php +++ b/src/network/protocol/packet/RemoveEntityPacket.php @@ -6,11 +6,11 @@ class RemoveEntityPacket extends RakNetDataPacket{ public function pid(){ return ProtocolInfo::REMOVE_ENTITY_PACKET; } - + public function decode(){ } - + public function encode(){ $this->reset(); $this->putInt($this->eid); diff --git a/src/network/protocol/packet/RemovePlayerPacket.php b/src/network/protocol/packet/RemovePlayerPacket.php index f23cd4df..553a46b7 100644 --- a/src/network/protocol/packet/RemovePlayerPacket.php +++ b/src/network/protocol/packet/RemovePlayerPacket.php @@ -3,15 +3,15 @@ class RemovePlayerPacket extends RakNetDataPacket{ public $eid; public $clientID; - + public function pid(){ return ProtocolInfo::REMOVE_PLAYER_PACKET; } - + public function decode(){ } - + public function encode(){ $this->reset(); $this->putInt($this->eid); diff --git a/src/network/protocol/packet/RequestChunkPacket.php b/src/network/protocol/packet/RequestChunkPacket.php index d0650748..dad75477 100644 --- a/src/network/protocol/packet/RequestChunkPacket.php +++ b/src/network/protocol/packet/RequestChunkPacket.php @@ -3,16 +3,16 @@ class RequestChunkPacket extends RakNetDataPacket{ public $chunkX; public $chunkZ; - + public function pid(){ return ProtocolInfo::REQUEST_CHUNK_PACKET; } - + public function decode(){ $this->chunkX = $this->getInt(); $this->chunkZ = $this->getInt(); } - + public function encode(){ } diff --git a/src/network/protocol/packet/RespawnPacket.php b/src/network/protocol/packet/RespawnPacket.php index 1d3c208e..51c6eab1 100644 --- a/src/network/protocol/packet/RespawnPacket.php +++ b/src/network/protocol/packet/RespawnPacket.php @@ -5,18 +5,18 @@ class RespawnPacket extends RakNetDataPacket{ public $x; public $y; public $z; - + public function pid(){ return ProtocolInfo::RESPAWN_PACKET; } - + public function decode(){ $this->eid = $this->getInt(); $this->x = $this->getFloat(); $this->y = $this->getFloat(); $this->z = $this->getFloat(); } - + public function encode(){ $this->reset(); $this->putInt($this->eid); diff --git a/src/network/protocol/packet/RotateHeadPacket.php b/src/network/protocol/packet/RotateHeadPacket.php index 21cc7cf0..3a77c468 100644 --- a/src/network/protocol/packet/RotateHeadPacket.php +++ b/src/network/protocol/packet/RotateHeadPacket.php @@ -13,13 +13,13 @@ class RotateHeadPacket extends RakNetDataPacket{ public function pid(){ return ProtocolInfo::ROTATE_HEAD_PACKET; } - + public function decode(){ $this->get(7); //id + data $this->eid = $this->getInt(); $this->yaw = $this->getByte(); } - + public function encode(){ $this->reset(); $this->putInt($this->eid); diff --git a/src/network/protocol/packet/SendInventoryPacket.php b/src/network/protocol/packet/SendInventoryPacket.php index 1bddca97..79b931ea 100644 --- a/src/network/protocol/packet/SendInventoryPacket.php +++ b/src/network/protocol/packet/SendInventoryPacket.php @@ -3,13 +3,13 @@ class SendInventoryPacket extends RakNetDataPacket{ public $eid; public $windowid; - public $slots = array(); - public $armor = array(); - + public $slots = []; + public $armor = []; + public function pid(){ return ProtocolInfo::SEND_INVENTORY_PACKET; } - + public function decode(){ $this->eid = $this->getInt(); $this->windowid = $this->getByte(); @@ -23,7 +23,7 @@ class SendInventoryPacket extends RakNetDataPacket{ } } } - + public function encode(){ $this->reset(); $this->putInt($this->eid); diff --git a/src/network/protocol/packet/ServerHandshakePacket.php b/src/network/protocol/packet/ServerHandshakePacket.php index 4931aa23..2b1a9384 100644 --- a/src/network/protocol/packet/ServerHandshakePacket.php +++ b/src/network/protocol/packet/ServerHandshakePacket.php @@ -8,17 +8,17 @@ class ServerHandshakePacket extends RakNetDataPacket{ public function pid(){ return ProtocolInfo::SERVER_HANDSHAKE_PACKET; } - + public function decode(){ - } - + } + public function encode(){ $this->reset(); $this->put("\x04\x3f\x57\xfe"); //cookie $this->put("\xcd"); //Security flags $this->putShort($this->port); - $this->putDataArray(array( + $this->putDataArray([ "\xf5\xff\xff\xf5", "\xff\xff\xff\xff", "\xff\xff\xff\xff", @@ -29,7 +29,7 @@ class ServerHandshakePacket extends RakNetDataPacket{ "\xff\xff\xff\xff", "\xff\xff\xff\xff", "\xff\xff\xff\xff", - )); + ]); $this->put("\x00\x00"); $this->putLong($this->session); $this->putLong($this->session2); diff --git a/src/network/protocol/packet/SetEntityDataPacket.php b/src/network/protocol/packet/SetEntityDataPacket.php index 8626885d..886721ae 100644 --- a/src/network/protocol/packet/SetEntityDataPacket.php +++ b/src/network/protocol/packet/SetEntityDataPacket.php @@ -3,15 +3,15 @@ class SetEntityDataPacket extends RakNetDataPacket{ public $eid; public $metadata; - + public function pid(){ return ProtocolInfo::SET_ENTITY_DATA_PACKET; } - + public function decode(){ } - + public function encode(){ $this->reset(); $this->putInt($this->eid); diff --git a/src/network/protocol/packet/SetEntityLinkPacket.php b/src/network/protocol/packet/SetEntityLinkPacket.php index 34de37c1..5c066c82 100644 --- a/src/network/protocol/packet/SetEntityLinkPacket.php +++ b/src/network/protocol/packet/SetEntityLinkPacket.php @@ -21,7 +21,6 @@ class SetEntityLinkPacket extends RakNetDataPacket{ $this->putInt($this->type); } - public function pid(){ return ProtocolInfo::SET_ENTITY_LINK_PACKET; } diff --git a/src/network/protocol/packet/SetEntityMotionPacket.php b/src/network/protocol/packet/SetEntityMotionPacket.php index e4cf70b9..e375fd2c 100644 --- a/src/network/protocol/packet/SetEntityMotionPacket.php +++ b/src/network/protocol/packet/SetEntityMotionPacket.php @@ -5,15 +5,15 @@ class SetEntityMotionPacket extends RakNetDataPacket{ public $speedX; public $speedY; public $speedZ; - + public function pid(){ return ProtocolInfo::SET_ENTITY_MOTION_PACKET; } - + public function decode(){ } - + public function encode(){ $this->reset(); $this->putInt($this->eid); diff --git a/src/network/protocol/packet/SetHealthPacket.php b/src/network/protocol/packet/SetHealthPacket.php index 42e52f81..0d63fdff 100644 --- a/src/network/protocol/packet/SetHealthPacket.php +++ b/src/network/protocol/packet/SetHealthPacket.php @@ -2,15 +2,15 @@ class SetHealthPacket extends RakNetDataPacket{ public $health; - + public function pid(){ return ProtocolInfo::SET_HEALTH_PACKET; } - + public function decode(){ $this->health = $this->getByte(); } - + public function encode(){ $this->reset(); $this->putByte($this->health); diff --git a/src/network/protocol/packet/SetSpawnPositionPacket.php b/src/network/protocol/packet/SetSpawnPositionPacket.php index 3da187ea..02808bcf 100644 --- a/src/network/protocol/packet/SetSpawnPositionPacket.php +++ b/src/network/protocol/packet/SetSpawnPositionPacket.php @@ -4,15 +4,15 @@ class SetSpawnPositionPacket extends RakNetDataPacket{ public $x; public $z; public $y; - + public function pid(){ return ProtocolInfo::SET_SPAWN_POSITION_PACKET; } - + public function decode(){ } - + public function encode(){ $this->reset(); $this->putInt($this->x); diff --git a/src/network/protocol/packet/SetTimePacket.php b/src/network/protocol/packet/SetTimePacket.php index 302afd9e..6e07e072 100644 --- a/src/network/protocol/packet/SetTimePacket.php +++ b/src/network/protocol/packet/SetTimePacket.php @@ -3,15 +3,15 @@ class SetTimePacket extends RakNetDataPacket{ public $time; public $started = true; - + public function pid(){ return ProtocolInfo::SET_TIME_PACKET; } - + public function decode(){ - } - + } + public function encode(){ $this->reset(); $this->putInt($this->time); diff --git a/src/network/protocol/packet/StartGamePacket.php b/src/network/protocol/packet/StartGamePacket.php index a746ea3a..32aa8990 100644 --- a/src/network/protocol/packet/StartGamePacket.php +++ b/src/network/protocol/packet/StartGamePacket.php @@ -8,15 +8,15 @@ class StartGamePacket extends RakNetDataPacket{ public $x; public $y; public $z; - + public function pid(){ return ProtocolInfo::START_GAME_PACKET; } - + public function decode(){ - } - + } + public function encode(){ $this->reset(); $this->putInt($this->seed); diff --git a/src/network/protocol/packet/TakeItemEntityPacket.php b/src/network/protocol/packet/TakeItemEntityPacket.php index 85221c38..cd0af938 100644 --- a/src/network/protocol/packet/TakeItemEntityPacket.php +++ b/src/network/protocol/packet/TakeItemEntityPacket.php @@ -7,11 +7,11 @@ class TakeItemEntityPacket extends RakNetDataPacket{ public function pid(){ return ProtocolInfo::TAKE_ITEM_ENTITY_PACKET; } - + public function decode(){ } - + public function encode(){ $this->reset(); $this->putInt($this->target); diff --git a/src/network/protocol/packet/TileEventPacket.php b/src/network/protocol/packet/TileEventPacket.php index e2266896..f0d75d37 100644 --- a/src/network/protocol/packet/TileEventPacket.php +++ b/src/network/protocol/packet/TileEventPacket.php @@ -6,15 +6,15 @@ class TileEventPacket extends RakNetDataPacket{ public $z; public $case1; public $case2; - + public function pid(){ return ProtocolInfo::TILE_EVENT_PACKET; } - + public function decode(){ } - + public function encode(){ $this->reset(); $this->putInt($this->x); diff --git a/src/network/protocol/packet/UnknownPacket.php b/src/network/protocol/packet/UnknownPacket.php index 08b89139..d85cb81c 100644 --- a/src/network/protocol/packet/UnknownPacket.php +++ b/src/network/protocol/packet/UnknownPacket.php @@ -2,19 +2,19 @@ class UnknownPacket extends RakNetDataPacket{ public $packetID = -1; - + public function __construct($pid = -1){ $this->packetID = $pid; } - + public function pid(){ return $this->packetID; } - + public function decode(){ } - + public function encode(){ } diff --git a/src/network/protocol/packet/UpdateBlockPacket.php b/src/network/protocol/packet/UpdateBlockPacket.php index 020fc63b..43e3e301 100644 --- a/src/network/protocol/packet/UpdateBlockPacket.php +++ b/src/network/protocol/packet/UpdateBlockPacket.php @@ -6,15 +6,15 @@ class UpdateBlockPacket extends RakNetDataPacket{ public $y; public $block; public $meta; - + public function pid(){ return ProtocolInfo::UPDATE_BLOCK_PACKET; } - + public function decode(){ } - + public function encode(){ $this->reset(); $this->putInt($this->x); diff --git a/src/network/protocol/packet/UseItemPacket.php b/src/network/protocol/packet/UseItemPacket.php index 0136ed5b..94d9ac9f 100644 --- a/src/network/protocol/packet/UseItemPacket.php +++ b/src/network/protocol/packet/UseItemPacket.php @@ -14,11 +14,11 @@ class UseItemPacket extends RakNetDataPacket{ public $posX; public $posY; public $posZ; - + public function pid(){ return ProtocolInfo::USE_ITEM_PACKET; } - + public function decode(){ $this->x = $this->getInt(); $this->y = $this->getInt(); @@ -32,9 +32,9 @@ class UseItemPacket extends RakNetDataPacket{ $this->fz = $this->getFloat(); $this->posX = $this->getFloat(); $this->posY = $this->getFloat(); - $this->posZ = $this->getFloat(); + $this->posZ = $this->getFloat(); } - + public function encode(){ } diff --git a/src/network/query/QueryHandler.php b/src/network/query/QueryHandler.php index 7530d3b6..41bb81aa 100644 --- a/src/network/query/QueryHandler.php +++ b/src/network/query/QueryHandler.php @@ -19,7 +19,7 @@ class QueryHandler{ The Query protocol is built on top of the existing Minecraft PE UDP network stack. Because the 0xFE packet does not exist in the MCPE protocol, we can identify Query packets and remove them from the packet queue. - + Then, the Query class handles itself sending the packets in raw form, because packets can conflict with the MCPE ones. */ diff --git a/src/network/raknet/RakNetDataPacket.php b/src/network/raknet/RakNetDataPacket.php index 5f64ac47..ada14791 100755 --- a/src/network/raknet/RakNetDataPacket.php +++ b/src/network/raknet/RakNetDataPacket.php @@ -12,7 +12,7 @@ abstract class RakNetDataPacket extends stdClass{ public $splitID; public $splitIndex; private $offset = 0; - + abstract public function encode(); abstract public function decode(); @@ -20,11 +20,11 @@ abstract class RakNetDataPacket extends stdClass{ public function getBuffer(){ return $this->buffer; } - + public function getOffset(){ return $this->offset; } - + public function setBuffer($buffer = ""){ $this->buffer = $buffer; $this->offset = 0; @@ -123,12 +123,12 @@ abstract class RakNetDataPacket extends stdClass{ protected function getShort($unsigned = false){ return Utils::readShort($this->get(2), $unsigned); } - + public function getSignedByte(){ $b = ord($this->get(1)); - return $b >= 0x80 ? ($b-256) : $b; + return $b >= 0x80 ? ($b - 256) : $b; } - + protected function getByte(){ return ord($this->get(1)); } @@ -144,7 +144,7 @@ abstract class RakNetDataPacket extends stdClass{ } protected function putByte($v){ - $this->buffer .= chr((int)$v); + $this->buffer .= chr((int) $v); } protected function getString(){ diff --git a/src/network/raknet/RakNetInfo.php b/src/network/raknet/RakNetInfo.php index 634e2e76..dabb8b75 100644 --- a/src/network/raknet/RakNetInfo.php +++ b/src/network/raknet/RakNetInfo.php @@ -38,7 +38,7 @@ abstract class RakNetInfo{ const ACK = 0xc0; public static function isValid($pid){ - return match ((int)$pid) { + return match ((int) $pid) { RakNetInfo::UNCONNECTED_PING, RakNetInfo::UNCONNECTED_PING_OPEN_CONNECTIONS, RakNetInfo::OPEN_CONNECTION_REQUEST_1, RakNetInfo::OPEN_CONNECTION_REPLY_1, RakNetInfo::OPEN_CONNECTION_REQUEST_2, RakNetInfo::OPEN_CONNECTION_REPLY_2, RakNetInfo::INCOMPATIBLE_PROTOCOL_VERSION, RakNetInfo::UNCONNECTED_PONG, RakNetInfo::ADVERTISE_SYSTEM, RakNetInfo::DATA_PACKET_0, RakNetInfo::DATA_PACKET_1, RakNetInfo::DATA_PACKET_2, RakNetInfo::DATA_PACKET_3, RakNetInfo::DATA_PACKET_4, RakNetInfo::DATA_PACKET_5, RakNetInfo::DATA_PACKET_6, RakNetInfo::DATA_PACKET_7, RakNetInfo::DATA_PACKET_8, RakNetInfo::DATA_PACKET_9, RakNetInfo::DATA_PACKET_A, RakNetInfo::DATA_PACKET_B, RakNetInfo::DATA_PACKET_C, RakNetInfo::DATA_PACKET_D, RakNetInfo::DATA_PACKET_E, RakNetInfo::DATA_PACKET_F, RakNetInfo::NACK, RakNetInfo::ACK => true, default => false, }; diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php index 1b346cf3..6dc14659 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -7,7 +7,7 @@ class RakNetParser{ private $offset; public function __construct(&$buffer){ - $this->buffer =& $buffer; + $this->buffer = &$buffer; $this->offset = 0; if(strlen($this->buffer) > 0){ $this->parse(); @@ -18,7 +18,7 @@ class RakNetParser{ private function parse(){ $this->packet = new RakNetPacket(ord($this->get(1))); - $this->packet->buffer =& $this->buffer; + $this->packet->buffer = &$this->buffer; $this->packet->length = strlen($this->buffer); switch($this->packet->pid()){ case RakNetInfo::UNCONNECTED_PING: @@ -163,7 +163,7 @@ class RakNetParser{ if(strlen($buffer) < ($length - 1)){ return false; } - + $data = match($pid){ ProtocolInfo::PING_PACKET => new PingPacket(), ProtocolInfo::PONG_PACKET => new PongPacket(), @@ -222,7 +222,7 @@ class RakNetParser{ ProtocolInfo::PLAYER_INPUT_PACKET => new PlayerInputPacket(), default => new UnknownPacket($pid) }; - + $data->reliability = $reliability; $data->hasSplit = $hasSplit; $data->messageIndex = $messageIndex; diff --git a/src/plugin/DummyPlugin.php b/src/plugin/DummyPlugin.php index 34d8abcc..6ab9554a 100644 --- a/src/plugin/DummyPlugin.php +++ b/src/plugin/DummyPlugin.php @@ -1,13 +1,13 @@ fp)){ if(($this->fp = @fopen($file, "c+b")) === false){ return false; diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index f00388e6..0439b5bb 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -40,7 +40,7 @@ class PMFLevel extends PMF{ if(!is_dir($dirname)){ @mkdir($dirname , 0755); } - + for($index = 0; $index < $cnt; ++$index){ $this->chunks[$index] = false; $this->chunkChange[$index] = false; @@ -94,14 +94,14 @@ class PMFLevel extends PMF{ $this->backupLocTable(); } - + public function backupLocTable(){ $dir = dirname($this->file); if(is_file("$dir/loctable.pmf")){ $val = copy("$dir/loctable.pmf", "$dir/loctable.pmf.old"); if($val === false) ConsoleAPI::warn("Failed to backup loctable data!"); } - + $file = fopen("$dir/loctable.pmf", "wb"); try{ $cnt = pow($this->levelData["width"], 2); @@ -111,7 +111,7 @@ class PMFLevel extends PMF{ }finally{ fclose($file); } - + } public function getXZ($index, &$X = null, &$Z = null){ @@ -212,7 +212,7 @@ class PMFLevel extends PMF{ unset($this->chunks[$index], $this->chunkChange[$index]); return true; } - + public function isChunkLoaded($X, $Z){ $index = self::getIndex($X, $Z); if(!isset($this->chunks[$index]) or $this->chunks[$index] === false){ @@ -351,16 +351,16 @@ class PMFLevel extends PMF{ public function fastGetBlockID($chunkX, $chunkY, $chunkZ, $blockX, $blockY, $blockZ, $index){ return ($this->chunks[$index][$chunkY] === false) ? 0 : ord($this->chunks[$index][$chunkY][$blockY + ($blockX << 5) + ($blockZ << 9)]); } - + public function getBlockID($x, $y, $z){ if($y > 127 || $y < 0){ return 0; } - + if($x < 0 || $x > 255 || $z < 0 || $z > 255){ return INVISIBLE_BEDROCK; } - + $X = $x >> 4; $Z = $z >> 4; $Y = $y >> 4; @@ -371,9 +371,9 @@ class PMFLevel extends PMF{ $aX = $x & 0xf; $aZ = $z & 0xf; $aY = $y & 0xf; - + $b = ord($this->chunks[$index][$Y][($aY + ($aX << 5) + ($aZ << 9))]); - + return $b; } @@ -381,12 +381,12 @@ class PMFLevel extends PMF{ if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127){ return false; } - + $X = $x >> 4; $Z = $z >> 4; $Y = $y >> 4; $block &= 0xFF; - + $index = self::getIndex($X, $Z); $aX = $x & 0xf; $aZ = $z & 0xf; @@ -398,7 +398,7 @@ class PMFLevel extends PMF{ $this->chunks[$index][$Y][$bind] = chr($block); if($block > 0) StaticBlock::getBlock($block)::onPlace($this->level, $x, $y, $z); } - + if(!isset($this->chunkChange[$index][$Y])){ $this->chunkChange[$index][$Y] = 1; }else{ @@ -434,7 +434,7 @@ class PMFLevel extends PMF{ $Z = $z >> 4; $Y = $y >> 4; $damage &= 0x0F; - + $index = self::getIndex($X, $Z); $aX = $x & 0xf; $aZ = $z & 0xf; @@ -467,11 +467,11 @@ class PMFLevel extends PMF{ if($x < 0 || $x > 255 || $z < 0 || $z > 255){ return [INVISIBLE_BEDROCK, 0]; } - + $X = $x >> 4; $Z = $z >> 4; $Y = $y >> 4; - + $index = self::getIndex($X, $Z); if(!isset($this->chunks[$index]) || $this->chunks[$index] === false){ if($this->loadChunk($X, $Z) === false){ @@ -484,12 +484,12 @@ class PMFLevel extends PMF{ $aX = $x & 0xf; $aZ = $z & 0xf; $aY = $y & 0xf; - + $b = ord($this->chunks[$index][$Y][($aY + ($aX << 5) + ($aZ << 9))]); - + $m = ord($this->chunks[$index][$Y][(($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9))]); $m = ($y & 1) ? $m >> 4 : $m & 0xf; - + return [$b, $m]; } @@ -517,7 +517,7 @@ class PMFLevel extends PMF{ $mindex = (int) (($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9)); $old_b = ord($this->chunks[$index][$Y][$bindex]); $old_m = ord($this->chunks[$index][$Y][$mindex]); - + $m = ($y & 1) ? (($meta << 4) | ($old_m & 0x0F)) : (($old_m & 0xF0) | $meta); if($old_b !== $block or $old_m !== $m){ @@ -529,7 +529,7 @@ class PMFLevel extends PMF{ ++$this->chunkChange[$index][$Y]; } $this->chunkChange[$index][-1] = true; - + if($block > 0) StaticBlock::getBlock($block)::onPlace($this->level, $x, $y, $z); return true; } diff --git a/src/recipes/CraftingRecipes.php b/src/recipes/CraftingRecipes.php index 47064577..ef976bac 100644 --- a/src/recipes/CraftingRecipes.php +++ b/src/recipes/CraftingRecipes.php @@ -1,9 +1,9 @@ CLAY_BLOCK:0x1", "WOODEN_PLANKS:?x4=>WORKBENCH:0x1", "GLOWSTONE_DUST:?x4=>GLOWSTONE_BLOCK:0x1", @@ -73,7 +73,7 @@ class CraftingRecipes{ ]; private static $big = [ // Probably means only craftable on crafting bench. Name it better! - // Building + // Building "WOOL:?x3,WOODEN_PLANKS:?x3=>BED:0x1", "WOODEN_PLANKS:?x8=>CHEST:0x1", "STICK:?x6=>FENCE:0x2", @@ -199,47 +199,47 @@ class CraftingRecipes{ private static $recipes = []; - public static function init(){ + public static function init() { $server = ServerAPI::request(); $id = 1; - foreach(CraftingRecipes::$small as $recipe){ + foreach(CraftingRecipes::$small as $recipe) { $recipe = CraftingRecipes::parseRecipe($recipe); $recipe[3] = 0; //Type CraftingRecipes::$recipes[$id] = $recipe; ++$id; } - foreach(CraftingRecipes::$big as $recipe){ + foreach(CraftingRecipes::$big as $recipe) { $recipe = CraftingRecipes::parseRecipe($recipe); $recipe[3] = 1; CraftingRecipes::$recipes[$id] = $recipe; ++$id; } - foreach(CraftingRecipes::$stone as $recipe){ + foreach(CraftingRecipes::$stone as $recipe) { $recipe = CraftingRecipes::parseRecipe($recipe); $recipe[3] = 2; CraftingRecipes::$recipes[$id] = $recipe; ++$id; } - foreach(CraftingRecipes::$recipes as $id => $recipe){ + foreach(CraftingRecipes::$recipes as $id => $recipe) { $server->query("INSERT INTO recipes (id, type, recipe) VALUES (" . $id . ", " . $recipe[3] . ", '" . $recipe[2] . "');"); } } - private static function parseRecipe($recipe){ + private static function parseRecipe($recipe) { $recipe = explode("=>", $recipe); $recipeItems = []; - foreach(explode(",", $recipe[0]) as $item){ + foreach(explode(",", $recipe[0]) as $item) { $item = explode("x", $item); $id = explode(":", $item[0]); $meta = array_pop($id); $id = $id[0]; $it = BlockAPI::fromString($id); - if(!isset($recipeItems[$it->getID()])){ + if(!isset($recipeItems[$it->getID()])) { $recipeItems[$it->getID()] = [$it->getID(), $meta === "?" ? false : intval($meta) & 0xFFFF, intval($item[1])]; - }else{ - if($it->getMetadata() !== $recipeItems[$it->getID()][1]){ + } else { + if($it->getMetadata() !== $recipeItems[$it->getID()][1]) { $recipeItems[$it->getID()][1] = false; } $recipeItems[$it->getID()][2] += $it->count; @@ -256,7 +256,7 @@ class CraftingRecipes{ $craftItem = [$it->getID(), intval($meta) & 0xFFFF, intval($item[1])]; $recipeString = ""; - foreach($recipeItems as $item){ + foreach($recipeItems as $item) { $recipeString .= $item[0] . "x" . $item[2] . ","; } $recipeString = substr($recipeString, 0, -1) . "=>" . $craftItem[0] . "x" . $craftItem[2]; @@ -264,11 +264,11 @@ class CraftingRecipes{ return [$recipeItems, $craftItem, $recipeString]; } - public static function canCraft(array $craftItem, array $recipeItems, $type){ + public static function canCraft(array $craftItem, array $recipeItems, $type) { ksort($recipeItems); $recipeString = ""; - foreach($recipeItems as $item){ - if($craftItem[0] === CAKE && $item[0] === BUCKET && $item[1] === 1){ //some dark magic with recipe happened in mcpe, pmmp restores it back to normal + foreach($recipeItems as $item) { + if($craftItem[0] === CAKE && $item[0] === BUCKET && $item[1] === 1) { //some dark magic with recipe happened in mcpe, pmmp restores it back to normal $item[2] = 3; } $recipeString .= $item[0] . "x" . $item[2] . ","; @@ -276,33 +276,33 @@ class CraftingRecipes{ $recipeString = substr($recipeString, 0, -1) . "=>" . $craftItem[0] . "x" . $craftItem[2]; $server = ServerAPI::request(); $result = $server->query("SELECT id FROM recipes WHERE type == " . $type . " AND recipe == '" . $recipeString . "';"); - if($result instanceof SQLite3Result){ + if($result instanceof SQLite3Result) { $continue = true; - while(($r = $result->fetchArray(SQLITE3_NUM)) !== false){ + while(($r = $result->fetchArray(SQLITE3_NUM)) !== false) { $continue = true; $recipe = CraftingRecipes::$recipes[$r[0]]; - foreach($recipe[0] as $item){ - if(!isset($recipeItems[$item[0]])){ + foreach($recipe[0] as $item) { + if(!isset($recipeItems[$item[0]])) { $continue = false; break; } $oitem = $recipeItems[$item[0]]; - if($craftItem[0] === CAKE && $oitem[0] === BUCKET && $item[1] === 1){ //some dark magic with recipe happened in mcpe, pmmp restores it back to normal x2 + if($craftItem[0] === CAKE && $oitem[0] === BUCKET && $item[1] === 1) { //some dark magic with recipe happened in mcpe, pmmp restores it back to normal x2 $oitem[2] = 3; } - if(($oitem[1] !== $item[1] and $item[1] !== false) or $oitem[2] !== $item[2]){ + if(($oitem[1] !== $item[1] and $item[1] !== false) or $oitem[2] !== $item[2]) { $continue = false; break; } } - if($continue === false or $craftItem[0] !== $recipe[1][0]){ + if($continue === false or $craftItem[0] !== $recipe[1][0]) { $continue = false; continue; } $continue = $recipe; break; } - }else{ + } else { return true; } return $continue; diff --git a/src/recipes/FuelData.php b/src/recipes/FuelData.php index e0c61bab..d9025730 100644 --- a/src/recipes/FuelData.php +++ b/src/recipes/FuelData.php @@ -1,6 +1,6 @@ 80, @@ -27,4 +27,4 @@ class FuelData{ BUCKET => 1000, ]; -} \ No newline at end of file +} diff --git a/src/recipes/SmeltingData.php b/src/recipes/SmeltingData.php index 1dd447d3..544abd5b 100644 --- a/src/recipes/SmeltingData.php +++ b/src/recipes/SmeltingData.php @@ -1,6 +1,6 @@ [STONE, 0], @@ -17,4 +17,4 @@ class SmeltingData{ RAW_CHICKEN => [COOKED_CHICKEN, 0], POTATO => [BAKED_POTATO, 0], ]; -} \ No newline at end of file +} diff --git a/src/structure/Structure.php b/src/structure/Structure.php index eaa51259..c7bc25c0 100644 --- a/src/structure/Structure.php +++ b/src/structure/Structure.php @@ -11,7 +11,7 @@ abstract class Structure{ private $structure; public $api, $pm; public $mapSymToID, $width, $radius; - + public function __construct($struct, $width, $symToID = []){ $this->structure = $struct; $this->mapSymToID = $symToID; @@ -19,16 +19,16 @@ abstract class Structure{ $this->api = $this->pm->api; $this->width = $width; $this->radius = $width / 2; - + } - + public function addMapping($sym, $id){ $this->mapSymToID[$sym] = $id; } public function getMappingFor($sym){ return $this->mapSymToID[$sym] ?? Structure::MAP_NO_KEY; } - + protected function placeBlock($level, $sym, $tv){ if($level instanceof Level){ $idm = $this->getMappingFor($sym); @@ -39,9 +39,9 @@ abstract class Structure{ $block = $this->api->block->get($idm[0], $idm[1], $tv); $level->setBlock($tv, $block, true, false, true); } - + } - + public function build($level, $centerX, $centerY, $centerZ){ $tempVector = new Vector3(0,0,0); $x = $centerX - $this->radius; @@ -56,8 +56,8 @@ abstract class Structure{ ++$z; $x = $centerX - $this->radius; } - $z = $centerZ - $this->radius; + $z = $centerZ - $this->radius; } } - + } \ No newline at end of file diff --git a/src/tests/ServerSuiteTest.php b/src/tests/ServerSuiteTest.php index e033c274..0762ec4d 100644 --- a/src/tests/ServerSuiteTest.php +++ b/src/tests/ServerSuiteTest.php @@ -1,4 +1,5 @@ getGamemode(), "survival"); - //Everything done! $server->close(); } diff --git a/src/utils/AsyncMultipleQueue.php b/src/utils/AsyncMultipleQueue.php index 5581a63b..f43cee27 100644 --- a/src/utils/AsyncMultipleQueue.php +++ b/src/utils/AsyncMultipleQueue.php @@ -111,8 +111,8 @@ class Async extends Thread{ } } } - + public function __destruct(){} - + public function run(){} }*/ diff --git a/src/utils/Config.php b/src/utils/Config.php index 23b3c153..a196879a 100644 --- a/src/utils/Config.php +++ b/src/utils/Config.php @@ -168,9 +168,6 @@ class Config{ return $content; } - /** - * @param $content - */ private function parseProperties($content){ if(preg_match_all('/([a-zA-Z0-9\-_\.]*)=([^\r\n]*)/u', $content, $matches) > 0){ //false or 0 matches foreach($matches[1] as $i => $k){ @@ -199,9 +196,6 @@ class Config{ return preg_replace("#^([ ]*)([a-zA-Z_]{1}[^\:]*)\:#m", "$1\"$2\":", $str); } - /** - * @param $content - */ private function parseList($content){ foreach(explode("\n", trim(str_replace("\r\n", "\n", $content))) as $v){ $v = trim($v); @@ -213,8 +207,6 @@ class Config{ } /** - * @param $default - * @param $data * * @return integer */ @@ -250,7 +242,6 @@ class Config{ } /** - * @param $k * * @return boolean|mixed */ @@ -258,16 +249,11 @@ class Config{ return $this->get($k); } - /** - * @param $k - * @param $v - */ public function __set($k, $v){ $this->set($k, $v); } /** - * @param $k * * @return boolean|mixed */ @@ -280,7 +266,6 @@ class Config{ } /** - * @param $k * @param bool $v */ public function set($k, $v = true){ @@ -288,7 +273,6 @@ class Config{ } /** - * @param $k * * @return boolean */ @@ -297,7 +281,6 @@ class Config{ } /** - * @param $k * @param bool $lowercase If set, searches Config in single-case / lowercase. * * @return boolean @@ -312,16 +295,10 @@ class Config{ } } - /** - * @param $k - */ public function __unset($k){ $this->remove($k); } - /** - * @param $k - */ public function remove($k){ unset($this->config[$k]); } diff --git a/src/utils/Container.php b/src/utils/Container.php index 0d22f2c0..36cfb41a 100644 --- a/src/utils/Container.php +++ b/src/utils/Container.php @@ -43,7 +43,6 @@ class Container{ return true; } - public function __toString(){ return $this->payload; } diff --git a/src/utils/LightUtils.php b/src/utils/LightUtils.php index 83820d8f..253d7834 100644 --- a/src/utils/LightUtils.php +++ b/src/utils/LightUtils.php @@ -13,7 +13,7 @@ class LightUtils{ $diffX = ($source->x - $block->x); $diffY = ($source->y - $block->y); $diffZ = ($source->z - $block->z); - $distance = $diffX*$diffX + $diffY*$diffY + $diffZ*$diffZ; + $distance = $diffX * $diffX + $diffY * $diffY + $diffZ * $diffZ; $result = floor($source->getMaxLightValue() - $distance); return max($result, 0); } diff --git a/src/utils/MTRandom.php b/src/utils/MTRandom.php index 5cc5d5c3..462f4075 100644 --- a/src/utils/MTRandom.php +++ b/src/utils/MTRandom.php @@ -8,35 +8,35 @@ class MTRandom public $mt; public $index = 0; public function __construct($seed = null){ - $this->setSeed($seed == null ? (int)microtime(1) : $seed); + $this->setSeed($seed == null ? (int) microtime(1) : $seed); } - + public function setSeed($seed){ $this->mt[0] = $seed & 0xffffffff; for($this->index = 1; $this->index < 624; ++$this->index){ - $this->mt[$this->index] = (0x6c078965 * ($this->mt[$this->index-1] >> 30 ^ $this->mt[$this->index - 1]) + $this->index) & 0xffffffff; + $this->mt[$this->index] = (0x6c078965 * ($this->mt[$this->index - 1] >> 30 ^ $this->mt[$this->index - 1]) + $this->index) & 0xffffffff; } } public function genRandInt(){ if($this->index >= 624 || $this->index < 0){ if($this->index >= 625 || $this->index < 0) $this->setSeed(4357); - + for($kk = 0; $kk < 227; ++$kk){ $y = ($this->mt[$kk] & 0x80000000) | ($this->mt[$kk + 1] & 0x7fffffff); - $this->mt[$kk] = $this->mt[$kk+397] ^ ($y >> 1) ^ MTRandom::MAG[$y & 0x1]; + $this->mt[$kk] = $this->mt[$kk + 397] ^ ($y >> 1) ^ MTRandom::MAG[$y & 0x1]; } - + for(;$kk < 623; ++$kk){ $y = ($this->mt[$kk] & 0x80000000) | ($this->mt[$kk + 1] & 0x7fffffff); - $this->mt[$kk] = $this->mt[$kk-227] ^ ($y >> 1) ^ MTRandom::MAG[$y & 0x1]; + $this->mt[$kk] = $this->mt[$kk - 227] ^ ($y >> 1) ^ MTRandom::MAG[$y & 0x1]; } - + $y = ($this->mt[623] & 0x80000000) | ($this->mt[0] & 0x7fffffff); $this->mt[623] = $this->mt[396] ^ ($y >> 1) ^ MTRandom::MAG[$y & 0x1]; $this->index = 0; } - + $y = $this->mt[$this->index++]; $y ^= ($y >> 11); $y ^= ($y << 7) & 0x9d2c5680; @@ -44,11 +44,11 @@ class MTRandom $y ^= ($y >> 18); return $y; } - + public function nextInt($bound = null){ return $bound == null ? $this->genRandInt() >> 1 : $this->genRandInt() % $bound; } - + public function nextFloat(){ return $this->genRandInt() / 0xffffffff; } diff --git a/src/utils/MersenneTwister.php b/src/utils/MersenneTwister.php index e35c7f25..bc521acf 100644 --- a/src/utils/MersenneTwister.php +++ b/src/utils/MersenneTwister.php @@ -17,10 +17,10 @@ and floats when we must. Only the class `twister' is part of the API; everything else is private. - + http://kingfisher.nfshost.com/sw/twister/ - - + + Who made such a weird names? - GameHerobrine */ const N = 624; @@ -42,16 +42,14 @@ foreach([16, 31, 32] as $n){ $val = MASK31 | (MASK31 << 1); define("MASK32", $val); - class MersenneTwister{ - - + static $MAG_01 = [0, MATRIX_A]; - + const N = N; //the class constant is not used anywhere in this namespace, //but it makes the API cleaner. - + //^^ u probably wanted to say makes code look weirder function __construct(){ @@ -191,7 +189,7 @@ class MersenneTwister{ return $y & 0xFFFFFFFF; } - + /* generates a random number on [0,1]-real-interval */ function real_closed(){ @@ -325,15 +323,15 @@ class MersenneTwister{ function nextInt($bound = null){ return $bound == null ? ($this->int32() >> 1) : ($this->int32() % $bound); } - + function nextFloat(){ return $this->int32() * 2.32830644e-10; } - + function setSeed($seed){ $this->init_with_integer($seed); } - + } function signed2unsigned($signed_integer){ @@ -389,7 +387,7 @@ function force_32_bit_int($x){ /* takes 2 integers, treats them as unsigned 32-bit integers, and adds them. - + it works by splitting each integer into 2 "half-integers", then adding the high and low half-integers separately. diff --git a/src/utils/RailLogic.php b/src/utils/RailLogic.php index 92eb05ab..1966b7c5 100644 --- a/src/utils/RailLogic.php +++ b/src/utils/RailLogic.php @@ -1,16 +1,17 @@ getID(); $meta = $rail->getMetadata(); $this->level = $rail->level; @@ -18,190 +19,251 @@ class RailLogic $this->y = $rail->y; $this->z = $rail->z; $this->isStraight = $id === POWERED_RAIL; - + $this->updateConnections($meta); } - public function getRail($v){ - if(($b = $this->level->getBlock($v)) instanceof RailBaseBlock){ //TODO git rid of getBlock + public function getRail($v) { + if(($b = $this->level->getBlock($v)) instanceof RailBaseBlock) { //TODO git rid of getBlock return new RailLogic($b); - }elseif(($b = $this->level->getBlockWithoutVector($v->x, $v->y + 1, $v->z)) instanceof RailBaseBlock){ + } elseif(($b = $this->level->getBlockWithoutVector($v->x, $v->y + 1, $v->z)) instanceof RailBaseBlock) { return new RailLogic($b); - }elseif(($b = $this->level->getBlockWithoutVector($v->x, $v->y - 1, $v->z)) instanceof RailBaseBlock){ + } elseif(($b = $this->level->getBlockWithoutVector($v->x, $v->y - 1, $v->z)) instanceof RailBaseBlock) { return new RailLogic($b); } - + return null; } - - public function hasRail($x, $y, $z){ + + public function hasRail($x, $y, $z) { return RailBaseBlock::isRailBlock($this->level, $x, $y, $z) || RailBaseBlock::isRailBlock($this->level, $x, $y + 1, $z) || RailBaseBlock::isRailBlock($this->level, $x, $y - 1, $z); } - - public function countPotentialConnections(){ + + public function countPotentialConnections() { return $this->hasRail($this->x, $this->y, $this->z - 1) + $this->hasRail($this->x, $this->y, $this->z + 1) + $this->hasRail($this->x - 1, $this->y, $this->z) + $this->hasRail($this->x + 1, $this->y, $this->z); } - - public function removeSoftConnections(){ - for($ind = 0; $ind < count($this->railPositions); ++$ind){ + + public function removeSoftConnections() { + for($ind = 0; $ind < count($this->railPositions); ++$ind) { $logic = $this->getRail($this->railPositions[$ind]); - if($logic != null && $this->connectsTo($logic)){ + if($logic != null && $this->connectsTo($logic)) { $this->railPositions[$ind] = new Vector3($logic->x, $logic->y, $logic->z); - }else{ + } else { unset($this->railPositions[$ind--]); $this->railPositions = array_values($this->railPositions); //reindexing } } } - - public function connectsTo(RailLogic $logic){ - foreach($this->railPositions as $rpos){ - if(($rpos->x === $logic->x) && ($rpos->z === $logic->z)){ + + public function connectsTo(RailLogic $logic) { + foreach($this->railPositions as $rpos) { + if(($rpos->x === $logic->x) && ($rpos->z === $logic->z)) { return true; } } return false; } - - public function canConnectTo(RailLogic $rail){ + + public function canConnectTo(RailLogic $rail) { return $this->connectsTo($rail) || !(count($this->railPositions) == 2); } - - public function hasNeighborRail($x, $y, $z){ + + public function hasNeighborRail($x, $y, $z) { $logic = $this->getRail(new Vector3($x, $y, $z)); - if($logic === null){ + if($logic === null) { return false; - }else{ + } else { $logic->removeSoftConnections(); //i place $this here and was searching why isnt it working correctly for 2 hours return $logic->canConnectTo($this); } - + } - - public function place($b, $b1){ + + public function place($b, $b1) { $hasZneg = $this->hasNeighborRail($this->x, $this->y, $this->z - 1); $hasZpos = $this->hasNeighborRail($this->x, $this->y, $this->z + 1); $hasXneg = $this->hasNeighborRail($this->x - 1, $this->y, $this->z); $hasXpos = $this->hasNeighborRail($this->x + 1, $this->y, $this->z); $state = -1; - if(($hasZneg || $hasZpos) && !($hasXneg && $hasXpos)){ + if(($hasZneg || $hasZpos) && !($hasXneg && $hasXpos)) { $state = 0; } - if(($hasXneg || $hasXpos) && !($hasZneg && $hasZpos)){ + if(($hasXneg || $hasXpos) && !($hasZneg && $hasZpos)) { $state = 1; } - - if(!$this->isStraight){ - if($hasZpos && $hasXpos && !($hasZneg && $hasXneg)) $state = 6; - if($hasZpos && $hasXneg && !($hasZneg && $hasXpos)) $state = 7; - if($hasZneg && $hasXneg && !($hasZpos && $hasXpos)) $state = 8; - if($hasZneg && $hasXpos && !($hasZpos && $hasXneg)) $state = 9; + + if(!$this->isStraight) { + if($hasZpos && $hasXpos && !($hasZneg && $hasXneg)) { + $state = 6; + } + if($hasZpos && $hasXneg && !($hasZneg && $hasXpos)) { + $state = 7; + } + if($hasZneg && $hasXneg && !($hasZpos && $hasXpos)) { + $state = 8; + } + if($hasZneg && $hasXpos && !($hasZpos && $hasXneg)) { + $state = 9; + } } - - if($state === -1){ - if($hasZneg || $hasZpos) $state = 0; - if($hasXneg || $hasXpos) $state = 1; - - if(!$this->isStraight){ - if($b){ - if($hasZpos && $hasXpos) $state = 6; - if($hasXneg && $hasZpos) $state = 7; - if($hasXpos && $hasZneg) $state = 9; - if($hasZneg && $hasXneg) $state = 8; - }else{ - if($hasZneg && $hasXneg) $state = 8; - if($hasXpos && $hasZneg) $state = 9; - if($hasXneg && $hasZpos) $state = 7; - if($hasZpos && $hasXpos) $state = 6; + + if($state === -1) { + if($hasZneg || $hasZpos) { + $state = 0; + } + if($hasXneg || $hasXpos) { + $state = 1; + } + + if(!$this->isStraight) { + if($b) { + if($hasZpos && $hasXpos) { + $state = 6; + } + if($hasXneg && $hasZpos) { + $state = 7; + } + if($hasXpos && $hasZneg) { + $state = 9; + } + if($hasZneg && $hasXneg) { + $state = 8; + } + } else { + if($hasZneg && $hasXneg) { + $state = 8; + } + if($hasXpos && $hasZneg) { + $state = 9; + } + if($hasXneg && $hasZpos) { + $state = 7; + } + if($hasZpos && $hasXpos) { + $state = 6; + } } } } - - if($state === 0){ - if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z - 1)) $state = 4; - if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z + 1)) $state = 5; - }elseif($state === 1){ - if(RailBaseBlock::isRailBlock($this->level, $this->x + 1, $this->y + 1, $this->z)) $state = 2; - if(RailBaseBlock::isRailBlock($this->level, $this->x - 1, $this->y + 1, $this->z)) $state = 3; - }elseif($state < 0) $state = 0; - + if($state === 0) { + if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z - 1)) { + $state = 4; + } + if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z + 1)) { + $state = 5; + } + } elseif($state === 1) { + if(RailBaseBlock::isRailBlock($this->level, $this->x + 1, $this->y + 1, $this->z)) { + $state = 2; + } + if(RailBaseBlock::isRailBlock($this->level, $this->x - 1, $this->y + 1, $this->z)) { + $state = 3; + } + } elseif($state < 0) { + $state = 0; + } + $this->updateConnections($state); $meta = $state; - if($this->isStraight){ + if($this->isStraight) { $meta = $this->level->level->getBlockDamage($this->x, $this->x, $this->z) & 8 | $state; } - - if($b1 || $this->level->level->getBlockDamage($this->x, $this->y, $this->z)){ + + if($b1 || $this->level->level->getBlockDamage($this->x, $this->y, $this->z)) { //this.logicWorld.setBlockMetadataWithNotify(this.railX, this.railY, this.railZ, var8, 3); $bl = $this->level->getBlockWithoutVector($this->x, $this->y, $this->z); $bl->setMetadata($meta); $this->level->setBlock(new Vector3($this->x, $this->y, $this->z), $bl, true, false, true); - foreach($this->railPositions as $rpos){ + foreach($this->railPositions as $rpos) { $logic = $this->getRail($rpos); - if($logic !== null){ + if($logic !== null) { $logic->removeSoftConnections(); - if($logic->canConnectTo($this)){ + if($logic->canConnectTo($this)) { $logic->connectTo($this); } - + } } - + } - + } - - public function hasConnection($x, $y, $z){ - foreach($this->railPositions as $rpos){ - if($rpos->x === $x && $rpos->z === $z) return true; + + public function hasConnection($x, $y, $z) { + foreach($this->railPositions as $rpos) { + if($rpos->x === $x && $rpos->z === $z) { + return true; + } } return false; } - - public function connectTo(RailLogic $logic){ + + public function connectTo(RailLogic $logic) { $this->railPositions[] = new Vector3($logic->x, $logic->y, $logic->z); - + $hasZneg = $this->hasConnection($this->x, $this->y, $this->z - 1); $hasZpos = $this->hasConnection($this->x, $this->y, $this->z + 1); $hasXneg = $this->hasConnection($this->x - 1, $this->y, $this->z); $hasXpos = $this->hasConnection($this->x + 1, $this->y, $this->z); $state = -1; - - if($hasZneg || $hasZpos) $state = 0; - if($hasXneg || $hasXpos) $state = 1; - - if(!$this->isStraight){ - if($hasZpos && $hasXpos && !($hasZneg && $hasXneg)) $state = 6; - if($hasZpos && $hasXneg && !($hasZneg && $hasXpos)) $state = 7; - if($hasZneg && $hasXneg && !($hasZpos && $hasXpos)) $state = 8; - if($hasZneg && $hasXpos && !($hasZpos && $hasXneg)) $state = 9; + + if($hasZneg || $hasZpos) { + $state = 0; + } + if($hasXneg || $hasXpos) { + $state = 1; + } + + if(!$this->isStraight) { + if($hasZpos && $hasXpos && !($hasZneg && $hasXneg)) { + $state = 6; + } + if($hasZpos && $hasXneg && !($hasZneg && $hasXpos)) { + $state = 7; + } + if($hasZneg && $hasXneg && !($hasZpos && $hasXpos)) { + $state = 8; + } + if($hasZneg && $hasXpos && !($hasZpos && $hasXneg)) { + $state = 9; + } + } + + if($state == 0) { + if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z - 1)) { + $state = 4; + } + if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z + 1)) { + $state = 5; + } + } elseif($state == 1) { + if(RailBaseBlock::isRailBlock($this->level, $this->x + 1, $this->y + 1, $this->z)) { + $state = 2; + } + if(RailBaseBlock::isRailBlock($this->level, $this->x - 1, $this->y + 1, $this->z)) { + $state = 3; + } + } elseif($state < 0) { + $state = 0; } - if($state == 0){ - if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z - 1)) $state = 4; - if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z + 1)) $state = 5; - }elseif($state == 1){ - if(RailBaseBlock::isRailBlock($this->level, $this->x + 1, $this->y + 1, $this->z)) $state = 2; - if(RailBaseBlock::isRailBlock($this->level, $this->x - 1, $this->y + 1, $this->z)) $state = 3; - }elseif($state < 0) $state = 0; - $meta = $state; - - if($this->isStraight){ + + if($this->isStraight) { $meta = $this->level->level->getBlockDamage($this->x, $this->x, $this->z) & 8 | $state; } - + //$b = $this->level->getBlockWithoutVector($this->x, $this->y, $this->z); //$b->setMetadata($meta); //$this->level->setBlock(new Vector3($this->x, $this->y, $this->z), $b, true, false, true); $this->level->fastSetBlockUpdateMeta($this->x, $this->y, $this->z, $meta, true); } - - public function updateConnections($meta){ - + + public function updateConnections($meta) { + $this->railPositions = []; - - switch($meta){ + + switch($meta) { case 0: $this->railPositions[] = new Vector3($this->x, $this->y, $this->z - 1); $this->railPositions[] = new Vector3($this->x, $this->y, $this->z + 1); @@ -245,4 +307,3 @@ class RailLogic } } } - diff --git a/src/utils/Random.php b/src/utils/Random.php index f2b146df..b0011fef 100644 --- a/src/utils/Random.php +++ b/src/utils/Random.php @@ -6,7 +6,7 @@ class Random{ private $x, $y, $z, $w; private $haveNextNextGaussian = false; private $nextNextGaussian = 0; - + public $state; public $i, $j; public function __construct($seed = false){ diff --git a/src/utils/StopMessageThread.php b/src/utils/StopMessageThread.php index 2c07172e..fff741bb 100644 --- a/src/utils/StopMessageThread.php +++ b/src/utils/StopMessageThread.php @@ -9,9 +9,9 @@ class StopMessageThread extends Thread $this->name = $server->extraprops->get("discord-bot-name"); $this->start(); } - + } - + public function run(){ Utils::curl_post($this->page, [ "username" => $this->name, diff --git a/src/utils/Utils.php b/src/utils/Utils.php index a6f8a08c..42a6f8b0 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -21,25 +21,25 @@ class Utils{ default => $id, }; } - + public static function wrapAngleTo360($angle) { $angle = fmod($angle, 360); return $angle < 0 ? $angle + 360 : $angle; } - + public static function getSeedNumeric($seed){ if($seed === "") return false; - elseif(is_int($seed)) return (int)$seed; + elseif(is_int($seed)) return (int) $seed; else{ $i = 0; for($j = 0; $j < strlen($seed); ++$j){ $i = $i * 31 + ord($seed[$j]); } - return (int)$i; + return (int) $i; } } - + public static function sint32($r){ $r &= 0xFFFFFFFF; if ($r & 0x80000000) @@ -49,11 +49,11 @@ class Utils{ } return $r; } - + public static function wrapAngleTo180($angle) { $angle = fmod($angle, 360); - + if($angle >= 180) $angle -= 360; if($angle < -180) $angle += 360; return $angle; @@ -61,22 +61,22 @@ class Utils{ public static function getSign($v){ return $v <=> 0; } - + public static function clampDegrees($v){ return floor(($v % 360 + 360) % 360); } - + /** * PHP8 has internal function for doing it: {@link str_ends_with} */ public static function endsWith($str, $check) { return str_ends_with($str, $check); } - + public static function hasEmoji($s){ return preg_match(Utils::emojiRegex, $s); } - + public static function getCallableIdentifier(callable $variable){ if(is_array($variable)){ return sha1(strtolower(get_class($variable[0])) . "::" . strtolower($variable[1])); @@ -84,11 +84,11 @@ class Utils{ return sha1(strtolower($variable)); } } - + public static function in_range($num, $min, $max){ return $num >= $min && $num <= $max; } - + public static function getUniqueID($raw = false, $extra = ""){ $machine = php_uname("a"); $machine .= file_exists("/proc/cpuinfo") ? `cat /proc/cpuinfo | grep "model name"` : ""; @@ -181,21 +181,21 @@ class Utils{ return Utils::$ip; } - + public static function makeHeaders($json){ $arr = json_decode($json); $rarr = []; foreach ($arr as $key => $value){ - $rarr[] = $key.": ".$value; + $rarr[] = $key . ": " . $value; } return $rarr; } - + public static function curl_get($page, $timeout = 10, $headers = " "){ if(Utils::$online === false){ return false; } - + if($headers != " "){ $headers = Utils::makeHeaders($headers); }else{ @@ -431,7 +431,7 @@ class Utils{ } return $data; } - + public static function readTriad($str){ return strlen($str) < 3 ? false : @unpack("N", "\x00$str")[1]; } @@ -444,11 +444,11 @@ class Utils{ } return $raw; } - + public static function writeLTriad($value){ return substr(pack("V", $value), 0, -1); } - + public static function writeTriad($value){ return substr(pack("N", $value), 1); } @@ -456,11 +456,11 @@ class Utils{ public static function getRandomBytes($length = 16, $secure = true, $raw = true, $startEntropy = "", &$rounds = 0, &$drop = 0){ return $raw ? random_bytes($length) : bin2hex(random_bytes($length)); //nobody would ever notice other parameters } - + public static function chance($i){//GameHerobrine's code return lcg_value() <= $i / 100; } - + /** * @deprecated use lcg_value instead */ @@ -481,10 +481,10 @@ class Utils{ if($pos2 instanceof Vector3){ $pos2 = $pos2->toArray(); } - + return abs($pos2["x"] - $pos1["x"]) + abs($pos2["y"] - $pos1["y"]) + abs($pos2["z"] - $pos1["z"]); } - + /** * Euclidian distance, but without square roots */ @@ -498,13 +498,11 @@ class Utils{ $pX = ($pos1["x"] - $pos2["x"]); $pY = ($pos1["y"] - $pos2["y"]); $pZ = ($pos1["z"] - $pos2["z"]); - return ($pX*$pX) + ($pY*$pY) + ($pZ*$pZ); + return ($pX * $pX) + ($pY * $pY) + ($pZ * $pZ); } - + /** - * - * @param $pos1 - * @param $pos2 + * * @return number */ public static function distance($pos1, $pos2){ @@ -514,9 +512,9 @@ class Utils{ if($pos2 instanceof Vector3){ $pos2 = $pos2->toArray(); } - return sqrt(($pos1["x"] - $pos2["x"])*($pos1["x"] - $pos2["x"]) + ($pos1["y"] - $pos2["y"])*($pos1["y"] - $pos2["y"]) + ($pos1["z"] - $pos2["z"])*($pos1["z"] - $pos2["z"])); + return sqrt(($pos1["x"] - $pos2["x"]) * ($pos1["x"] - $pos2["x"]) + ($pos1["y"] - $pos2["y"]) * ($pos1["y"] - $pos2["y"]) + ($pos1["z"] - $pos2["z"]) * ($pos1["z"] - $pos2["z"])); } - + public static function angle3D($pos1, $pos2){ if($pos1 instanceof Vector3){ $pos1 = $pos1->toArray(); @@ -572,8 +570,8 @@ class Utils{ } public static function readInt($str){ - if(strlen($str) <= 0) return; - + if(strlen($str) <= 0) return; + return @unpack("N", $str)[1] << 32 >> 32; //php has no signed long unpack } diff --git a/src/world/Explosion.php b/src/world/Explosion.php index e44c7bd3..b92e614d 100644 --- a/src/world/Explosion.php +++ b/src/world/Explosion.php @@ -1,7 +1,7 @@ DIRT, STONE => COBBLESTONE, @@ -18,7 +18,7 @@ class Explosion{ private $rays = 16; private $air; private $nullPlayer; - + public function __construct(Position $center, $size){ $this->level = $center->level; $this->source = $center; @@ -26,12 +26,12 @@ class Explosion{ $this->air = BlockAPI::getItem(AIR, 0, 1); $this->nullPlayer = new PlayerNull(); } - + public function sub_expl($i, $mRays, $j, $k){ $vx = $i / $mRays * 2 - 1; $vy = $j / $mRays * 2 - 1; $vz = $k / $mRays * 2 - 1; - $vlen = sqrt($vx*$vx + $vy*$vy + $vz*$vz); + $vlen = sqrt($vx * $vx + $vy * $vy + $vz * $vz); if($vlen != 0){ $vx = $vx / $vlen * $this->stepLen; $vy = $vy / $vlen * $this->stepLen; @@ -39,7 +39,7 @@ class Explosion{ }else{ $vx = $vy = $vz = 0; } - + $px = $this->source->x; $py = $this->source->y; $pz = $this->source->z; @@ -64,19 +64,19 @@ class Explosion{ $pz += $vz; } } - + public function explode(){ $radius = 2 * $this->size; $server = ServerAPI::request(); if(!Explosion::$enableExplosions){ /*Disable Explosions*/ - foreach($server->api->entity->getRadius($this->source, $radius+1) as $entity){ + foreach($server->api->entity->getRadius($this->source, $radius + 1) as $entity){ $distance = $this->source->distance($entity); $distByRad = $distance / $this->size; if($distByRad <= 1 && $distance != 0){ $diffX = ($entity->x - $this->source->x) / $distance; $diffY = ($entity->y + $entity->getEyeHeight() - $this->source->y) / $distance; $diffZ = ($entity->z - $this->source->z) / $distance; - + $impact = (1 - $distByRad) * 0.5; //TODO calculate block density around the entity instead of 0.5 $damage = (int) (($impact * $impact + $impact) * 8 * $this->size + 1); if($damage > 0){ @@ -84,7 +84,7 @@ class Explosion{ $entity->speedX = $diffX * $impact; $entity->speedY = $diffY * $impact; $entity->speedZ = $diffZ * $impact; - + if($entity->isPlayer()){ $pk = new SetEntityMotionPacket(); $pk->eid = 0; //XXX change @@ -112,7 +112,7 @@ class Explosion{ ]) === false){ return false; } - + $mRays = $this->rays - 1; $i = 0; for($j = 0; $j <= $mRays; ++$j){ @@ -126,21 +126,21 @@ class Explosion{ $this->sub_expl($i, $mRays, $j, $k); } } - + $j = 0; for($i = 1; $i < $mRays; ++$i){ for($k = 0; $k <= $mRays; ++$k){ $this->sub_expl($i, $mRays, $j, $k); } } - + $j = $mRays; for($i = 1; $i < $mRays; ++$i){ for($k = 0; $k <= $mRays; ++$k){ $this->sub_expl($i, $mRays, $j, $k); } } - + $k = 0; for($i = 1; $i < $mRays; ++$i){ for($j = 1; $j < $mRays; ++$j){ @@ -154,8 +154,7 @@ class Explosion{ } } //if($i == 0 or $i == $mRays or $j == 0 or $j == $mRays or $k == 0 or $k == $mRays){ - - + $send = []; foreach($server->api->entity->getRadius($this->source, $radius) as $entity){ $distance = $this->source->distance($entity); @@ -164,15 +163,15 @@ class Explosion{ $diffX = ($entity->x - $this->source->x) / $distance; $diffY = ($entity->y + $entity->getEyeHeight() - $this->source->y) / $distance; $diffZ = ($entity->z - $this->source->z) / $distance; - - $impact = (1 - $distByRad) * 0.5; //TODO calculate block density around the entity instead of 0.5 + + $impact = (1 - $distByRad) * 0.5; //TODO calculate block density around the entity instead of 0.5 $damage = (int) (($impact * $impact + $impact) * 8 * $this->size + 1); if($damage > 0){ $entity->harm($damage, "explosion"); $entity->speedX = $diffX * $impact; $entity->speedY = $diffY * $impact; $entity->speedZ = $diffZ * $impact; - + if($entity->isPlayer()){ $pk = new SetEntityMotionPacket(); $pk->eid = 0; //XXX change @@ -184,7 +183,7 @@ class Explosion{ } } } - + foreach($this->affectedBlocks as $xyz => $idm){ $id = $idm >> 8 & 0xff; $meta = $idm & 0x0f; diff --git a/src/world/Level.php b/src/world/Level.php index fbec0a71..510c3485 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -1,32 +1,52 @@ true, FARMLAND => true, @@ -45,8 +65,8 @@ class Level{ ICE => true, LEAVES => true ]; - - public function __construct(PMFLevel $level, Config $entities, Config $tiles, Config $blockUpdates, $name){ + + public function __construct(PMFLevel $level, Config $entities, Config $tiles, Config $blockUpdates, $name) { $this->server = ServerAPI::request(); $this->level = $level; $this->level->level = $this; @@ -69,176 +89,208 @@ class Level{ $this->randInt2 = 0x3C6EF35F; } - public function close(){ + public function close() { $this->__destruct(); } - - public function isTopSolidBlocking($x, $y, $z){ + + public function isTopSolidBlocking($x, $y, $z) { $idmeta = $this->level->getBlock($x, $y, $z); $id = $idmeta[0]; $meta = $idmeta[1]; - if($id == 0) return false; - if(StaticBlock::getIsTransparent($id)) return false; - + if($id == 0) { + return false; + } + if(StaticBlock::getIsTransparent($id)) { + return false; + } + return true; } - public function rayTraceBlocks(Vector3 $start, Vector3 $end){ - + public function rayTraceBlocks(Vector3 $start, Vector3 $end) { + $par3 = false; //TODO move to params? $par4 = true; - + $xStart = floor($start->x); $yStart = floor($start->y); $zStart = floor($start->z); $xEnd = floor($end->x); $yEnd = floor($end->y); $zEnd = floor($end->z); - + [$startID, $startMeta] = $this->level->getBlock($xStart, $yStart, $zStart); $block = StaticBlock::getBlock($startID); //$block::updateShape($this, $xStart, $yStart, $zStart); //TODO better way to do it $aabb = $block::getAABB($this, $xStart, $yStart, $zStart); - if($startID > 0 && $aabb != null){ //TODO also block::canColideCheck + if($startID > 0 && $aabb != null) { //TODO also block::canColideCheck $v14 = $block::clip($this, $xStart, $yStart, $zStart, $start, $end); - if($v14 != null) return $v14; + if($v14 != null) { + return $v14; + } } - - for($i = 0; $i <= 200; ++$i){ - - if(is_nan($start->x) || is_nan($start->y) || is_nan($start->z) || ($xStart == $xEnd && $yStart == $yEnd && $zStart == $zEnd)){ //also add checks for nan? + + for($i = 0; $i <= 200; ++$i) { + + if(is_nan($start->x) || is_nan($start->y) || is_nan($start->z) || ($xStart == $xEnd && $yStart == $yEnd && $zStart == $zEnd)) { //also add checks for nan? return null; } - + $v39 = $v40 = $v41 = true; $v15 = $v17 = $v19 = 999.0; //nice mojang - - if($xEnd > $xStart) $v15 = $xStart + 1; - elseif($xEnd < $xStart) $v15 = $xStart; - else $v39 = false; - - if($yEnd > $yStart) $v17 = $yStart + 1; - elseif($yEnd < $yStart) $v17 = $yStart; - else $v40 = false; - - if($zEnd > $zStart) $v19 = $zStart + 1; - elseif($zEnd < $zStart) $v19 = $zStart; - else $v41 = false; - + + if($xEnd > $xStart) { + $v15 = $xStart + 1; + } elseif($xEnd < $xStart) { + $v15 = $xStart; + } else { + $v39 = false; + } + + if($yEnd > $yStart) { + $v17 = $yStart + 1; + } elseif($yEnd < $yStart) { + $v17 = $yStart; + } else { + $v40 = false; + } + + if($zEnd > $zStart) { + $v19 = $zStart + 1; + } elseif($zEnd < $zStart) { + $v19 = $zStart; + } else { + $v41 = false; + } + $v21 = $v23 = $v25 = 999.0; //nice mojang x2 $v27 = $end->x - $start->x; $v29 = $end->y - $start->y; $v31 = $end->z - $start->z; - - if($v39) $v21 = $v27 == 0 ? ($v15 - $start->x)*INF : ($v15 - $start->x) / $v27; - if($v40) $v23 = $v29 == 0 ? ($v17 - $start->y)*INF : ($v17 - $start->y) / $v29; - if($v41) $v25 = $v31 == 0 ? ($v19 - $start->z)*INF : ($v19 - $start->z) / $v31; - - if($v21 < $v23 && $v21 < $v25){ + + if($v39) { + $v21 = $v27 == 0 ? ($v15 - $start->x) * INF : ($v15 - $start->x) / $v27; + } + if($v40) { + $v23 = $v29 == 0 ? ($v17 - $start->y) * INF : ($v17 - $start->y) / $v29; + } + if($v41) { + $v25 = $v31 == 0 ? ($v19 - $start->z) * INF : ($v19 - $start->z) / $v31; + } + + if($v21 < $v23 && $v21 < $v25) { $v42 = $xEnd > $xStart ? 4 : 5; - + $start->x = $v15; $start->y += $v29 * $v21; $start->z += $v31 * $v21; - }elseif($v23 < $v25){ + } elseif($v23 < $v25) { $v42 = $yEnd > $yStart ? 0 : 1; - + $start->x += $v27 * $v23; $start->y = $v17; $start->z += $v31 * $v23; - }else{ + } else { $v42 = $zEnd > $zStart ? 2 : 3; - + $start->x += $v27 * $v25; $start->y += $v29 * $v25; $start->z = $v19; } - + $xStart = floor($start->x); - if($v42 == 5) --$xStart; - + if($v42 == 5) { + --$xStart; + } + $yStart = floor($start->y); - if($v42 == 1) --$yStart; - + if($v42 == 1) { + --$yStart; + } + $zStart = floor($start->z); - if($v42 == 3) --$zStart; - + if($v42 == 3) { + --$zStart; + } + [$blockID, $blockMeta] = $this->level->getBlock($xStart, $yStart, $zStart); $block = StaticBlock::getBlock($blockID); $aabb = $block::getAABB($this, $xStart, $yStart, $zStart); - if($blockID > 0 && $aabb != null){ //TODO also block::canColideCheck + if($blockID > 0 && $aabb != null) { //TODO also block::canColideCheck $v38 = $block::clip($this, $xStart, $yStart, $zStart, $start, $end); - if($v38 != null) return $v38; + if($v38 != null) { + return $v38; + } } } - + return null; } - - public function isLavaInBB($aabb){ + + public function isLavaInBB($aabb) { $minX = floor($aabb->minX); $maxX = floor($aabb->maxX + 1); $minY = floor($aabb->minY); $maxY = floor($aabb->maxY + 1); $minZ = floor($aabb->minZ); $maxZ = floor($aabb->maxZ + 1); - - for($x = $minX; $x < $maxX; ++$x){ - for($y = $minY; $y < $maxY; ++$y){ - for($z = $minZ; $z < $maxZ; ++$z){ + + for($x = $minX; $x < $maxX; ++$x) { + for($y = $minY; $y < $maxY; ++$y) { + for($z = $minZ; $z < $maxZ; ++$z) { $blockId = $this->level->getBlockID($x, $y, $z); - - if($blockId == LAVA || $blockId == STILL_LAVA) return true; + + if($blockId == LAVA || $blockId == STILL_LAVA) { + return true; + } } } } - + return false; } - - public function handleMaterialAcceleration(AxisAlignedBB $aabb, $materialType, Entity $entity){ + + public function handleMaterialAcceleration(AxisAlignedBB $aabb, $materialType, Entity $entity) { $minX = floor($aabb->minX); $maxX = ceil($aabb->maxX); $minY = floor($aabb->minY); $maxY = ceil($aabb->maxY); $minZ = floor($aabb->minZ); $maxZ = ceil($aabb->maxZ); - + //1.5.2 checks that all chunks exist, not needed here i think - + $appliedVelocity = false; $velocityVec = new Vector3(0, 0, 0); - for($x = $minX; $x < $maxX; ++$x){ - for($y = $minY; $y < $maxY; ++$y){ - for($z = $minZ; $z < $maxZ; ++$z){ + for($x = $minX; $x < $maxX; ++$x) { + for($y = $minY; $y < $maxY; ++$y) { + for($z = $minZ; $z < $maxZ; ++$z) { [$block, $meta] = $this->level->getBlock($x, $y, $z); - if(($materialType == 0 && ($block == WATER || $block == STILL_WATER)) || ($materialType == 1 && ($block == LAVA || $block == STILL_LAVA))){ //TODO better material system + if(($materialType == 0 && ($block == WATER || $block == STILL_WATER)) || ($materialType == 1 && ($block == LAVA || $block == STILL_LAVA))) { //TODO better material system $v16 = ($y + 1) - LiquidBlock::getPercentAir($meta); - if($maxY >= $v16){ + if($maxY >= $v16) { $appliedVelocity = true; StaticBlock::$prealloc[$block]::addVelocityToEntity($this, $x, $y, $z, $entity, $velocityVec); } - + } } } } - - if($velocityVec->length() > 0){ //also checks is player flying + + if($velocityVec->length() > 0) { //also checks is player flying $velocityVec = $velocityVec->normalize(); //TODO do not use vec methods $v18 = 0.014; $entity->speedX += $velocityVec->x * $v18; $entity->speedY += $velocityVec->y * $v18; $entity->speedZ += $velocityVec->z * $v18; } - + return $appliedVelocity; } /** - * @param Entity $e - * @param AxisAlignedBB $aABB * @return AxisAlignedBB[] */ public function getCubes(Entity $e, AxisAlignedBB $aABB) { @@ -249,74 +301,76 @@ class Level{ $y1 = floor($aABB->maxY + 1); $z0 = floor($aABB->minZ); $z1 = floor($aABB->maxZ + 1); - + for($x = $x0; $x < $x1; ++$x) { for($z = $z0; $z < $z1; ++$z) { for($y = $y0 - 1; $y < $y1; ++$y) { $bid = $this->level->getBlockID($x, $y, $z); - if($bid > 0){ + if($bid > 0) { $blockBounds = StaticBlock::$prealloc[$bid]::getCollisionBoundingBoxes($this, $x, $y, $z, $e); //StaticBlock::getBoundingBoxForBlockCoords($b, $x, $y, $z); - - foreach($blockBounds as $blockBound){ - if($aABB->intersectsWith($blockBound)) $aABBs[] = $blockBound; + + foreach($blockBounds as $blockBound) { + if($aABB->intersectsWith($blockBound)) { + $aABBs[] = $blockBound; + } } } } } } - + return $aABBs; } - - public function __destruct(){ - if(isset($this->level)){ + + public function __destruct() { + if(isset($this->level)) { $this->save(false, false, false, false); $this->level->close(); unset($this->level); } unset($this->mobSpawner->level); } - - public function isDay(){ + + public function isDay() { return $this->getTime() % 19200 < TimeAPI::$phases["sunset"]; } - public function isNight(){ + public function isNight() { $t = $this->getTime() % 19200; return $t < TimeAPI::$phases["sunrise"] && $t > TimeAPI::$phases["sunset"]; } - public function save($force = false, $entities = true, $tiles = true, $blockupdates = true){ - if(!isset($this->level)){ + public function save($force = false, $entities = true, $tiles = true, $blockupdates = true) { + if(!isset($this->level)) { return false; } - if($this->server->saveEnabled === false and $force === false){ + if($this->server->saveEnabled === false and $force === false) { return; } - if($entities){ + if($entities) { $entities = []; - - foreach($this->entityList as $entity){ - if($entity instanceof Entity){ + + foreach($this->entityList as $entity) { + if($entity instanceof Entity) { $entities[] = $entity->createSaveData(); } } $this->entities->setAll($entities); $this->entities->save(); } - if($tiles){ + if($tiles) { $tiles = []; - foreach($this->server->api->tile->getAll($this) as $tile){ + foreach($this->server->api->tile->getAll($this) as $tile) { $tiles[] = $tile->data; } $this->tiles->setAll($tiles); $this->tiles->save(); } - if($blockupdates){ + if($blockupdates) { $blockUpdates = []; $updates = $this->server->query("SELECT x,y,z,type,delay FROM blockUpdates WHERE level = '" . $this->getName() . "';"); - if($updates !== false and $updates !== true){ + if($updates !== false and $updates !== true) { $timeu = microtime(true); - while(($bupdate = $updates->fetchArray(SQLITE3_ASSOC)) !== false){ + while(($bupdate = $updates->fetchArray(SQLITE3_ASSOC)) !== false) { $bupdate["delay"] = max(1, ($bupdate["delay"] - $timeu) * 20); $blockUpdates[] = $bupdate; } @@ -332,96 +386,98 @@ class Level{ $this->nextSave = microtime(true) + 45; } - public function getName(){ + public function getName() { return $this->name;//return $this->level->getData("name"); } - public function useChunk($X, $Z, Player $player){ - if(!isset($this->usedChunks[$X . "." . $Z])){ + public function useChunk($X, $Z, Player $player) { + if(!isset($this->usedChunks[$X . "." . $Z])) { $this->usedChunks[$X . "." . $Z] = []; } $this->usedChunks[$X . "." . $Z][$player->CID] = true; - if(isset($this->level)){ + if(isset($this->level)) { $this->level->loadChunk($X, $Z); } } - public function freeAllChunks(Player $player){ - foreach($this->usedChunks as $i => $c){ + public function freeAllChunks(Player $player) { + foreach($this->usedChunks as $i => $c) { unset($this->usedChunks[$i][$player->CID]); } } - public function freeChunk($X, $Z, Player $player){ + public function freeChunk($X, $Z, Player $player) { unset($this->usedChunks[$X . "." . $Z][$player->CID]); } - - public function checkCollisionsFor(Entity $e){ - if($e->level->getName() != $this->getName()){ + + public function checkCollisionsFor(Entity $e) { + if($e->level->getName() != $this->getName()) { return false; //not the same world } - foreach($this->entityList as $e1){ - if($e->boundingBox->intersectsWith($e1->boundingBox) && $e1->isCollidable){ + foreach($this->entityList as $e1) { + if($e->boundingBox->intersectsWith($e1->boundingBox) && $e1->isCollidable) { $e->onCollideWith($e1); $e1->onCollideWith($e); } } } - public function isObstructed($e){ - + public function isObstructed($e) { + } - - public function checkSleep(){ //TODO events? - if(count($this->players) == 0) return false; - if($this->server->api->time->getPhase($this) === "night"){ //TODO vanilla - foreach($this->players as $p){ - if(!$p->isSleeping || $p->sleepingTime < 100){ + + public function checkSleep() { //TODO events? + if(count($this->players) == 0) { + return false; + } + if($this->server->api->time->getPhase($this) === "night") { //TODO vanilla + foreach($this->players as $p) { + if(!$p->isSleeping || $p->sleepingTime < 100) { return false; } } $this->server->api->time->set("day", $this); } - foreach($this->players as $p){ + foreach($this->players as $p) { $p->stopSleep(); } } - - public function checkThings(){ - if(!isset($this->level)){ + + public function checkThings() { + if(!isset($this->level)) { return false; } $now = microtime(true); $this->players = $this->server->api->player->getAll($this); - - if(count($this->changedCount) > 0){ + + if(count($this->changedCount) > 0) { arsort($this->changedCount); $reorder = false; - foreach($this->changedCount as $index => $count){ - if($count < 582){//Optimal value, calculated using the relation between minichunks and single packets + foreach($this->changedCount as $index => $count) { + if($count < 582) {//Optimal value, calculated using the relation between minichunks and single packets break; } - foreach($this->players as $p){ + foreach($this->players as $p) { unset($p->chunksLoaded[$index]); } $reorder = true; unset($this->changedBlocks[$index]); } $this->changedCount = []; - - if($reorder){ - foreach($this->players as $p){ + + if($reorder) { + foreach($this->players as $p) { $p->orderChunks(); } } - - if(count($this->changedBlocks) > 0){ - foreach($this->changedBlocks as $i => $blocks){ - foreach($blocks as $b){ + + if(count($this->changedBlocks) > 0) { + foreach($this->changedBlocks as $i => $blocks) { + foreach($blocks as $b) { $pk = new UpdateBlockPacket; $pk->x = ($b >> 32) & 0xff; $pk->y = ($b >> 24) & 0xff; $pk->z = ($b >> 16) & 0xff; - $pk->block =($b >> 8) & 0xff; + $pk->block = ($b >> 8) & 0xff; $pk->meta = $b & 0xff; $this->server->api->player->broadcastPacket($this->players, $pk); } @@ -430,34 +486,34 @@ class Level{ $this->changedBlocks = []; } } - - if($this->nextSave < $now){ + + if($this->nextSave < $now) { $this->save(false, true, true, true); } } - public function isSpawnChunk($X, $Z){ + public function isSpawnChunk($X, $Z) { $spawnX = $this->level->getData("spawnX") >> 4; $spawnZ = $this->level->getData("spawnZ") >> 4; return abs($X - $spawnX) <= 1 and abs($Z - $spawnZ) <= 1; } - public function getBlockRaw(Vector3 $pos){ + public function getBlockRaw(Vector3 $pos) { $b = $this->level->getBlock($pos->x, $pos->y, $pos->z); return BlockAPI::get($b[0], $b[1], new Position($pos->x, $pos->y, $pos->z, $this)); } - public function setBlockRaw(Vector3 $pos, Block $block, $direct = true, $send = true){ - if(($ret = $this->level->setBlock($pos->x, $pos->y, $pos->z, $block->getID(), $block->getMetadata())) === true and $send !== false){ - if($direct === true){ + public function setBlockRaw(Vector3 $pos, Block $block, $direct = true, $send = true) { + if(($ret = $this->level->setBlock($pos->x, $pos->y, $pos->z, $block->getID(), $block->getMetadata())) === true and $send !== false) { + if($direct === true) { $this->addBlockToSendQueue($pos->x, $pos->y, $pos->z, $block->id, $block->meta); - }elseif($direct === false){ - if(!($pos instanceof Position)){ + } elseif($direct === false) { + if(!($pos instanceof Position)) { $pos = new Position($pos->x, $pos->y, $pos->z, $this); } $block->position($pos); $i = ($pos->x >> 4) . ":" . ($pos->y >> 4) . ":" . ($pos->z >> 4); - if(!isset($this->changedBlocks[$i])){ + if(!isset($this->changedBlocks[$i])) { $this->changedBlocks[$i] = []; $this->changedCount[$i] = 0; } @@ -467,59 +523,75 @@ class Level{ } return $ret; } - public function fastSetBlockUpdateMeta($x, $y, $z, $meta, $updateBlock = false){ + public function fastSetBlockUpdateMeta($x, $y, $z, $meta, $updateBlock = false) { $this->level->setBlockDamage($x, $y, $z, $meta); $id = $this->level->getBlockID($x, $y, $z); $this->addBlockToSendQueue($x, $y, $z, $id, $meta); - if($updateBlock){ + if($updateBlock) { $this->updateNeighborsAt($x, $y, $z, $id); } } - - public function updateNeighborsAt($x, $y, $z, $oldID){ + + public function updateNeighborsAt($x, $y, $z, $oldID) { $block = $this->level->getBlockID($x - 1, $y, $z); - if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x - 1, $y, $z, $x, $y, $z, $oldID); + if($block) { + StaticBlock::getBlock($block)::neighborChanged($this, $x - 1, $y, $z, $x, $y, $z, $oldID); + } $block = $this->level->getBlockID($x + 1, $y, $z); - if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x + 1, $y, $z, $x, $y, $z, $oldID); - + if($block) { + StaticBlock::getBlock($block)::neighborChanged($this, $x + 1, $y, $z, $x, $y, $z, $oldID); + } + $block = $this->level->getBlockID($x, $y - 1, $z); - if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x, $y - 1, $z, $x, $y, $z, $oldID); + if($block) { + StaticBlock::getBlock($block)::neighborChanged($this, $x, $y - 1, $z, $x, $y, $z, $oldID); + } $block = $this->level->getBlockID($x, $y + 1, $z); - if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x, $y + 1, $z, $x, $y, $z, $oldID); - + if($block) { + StaticBlock::getBlock($block)::neighborChanged($this, $x, $y + 1, $z, $x, $y, $z, $oldID); + } + $block = $this->level->getBlockID($x, $y, $z - 1); - if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x, $y, $z - 1, $x, $y, $z, $oldID); + if($block) { + StaticBlock::getBlock($block)::neighborChanged($this, $x, $y, $z - 1, $x, $y, $z, $oldID); + } $block = $this->level->getBlockID($x, $y, $z + 1); - if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x, $y, $z + 1, $x, $y, $z, $oldID); + if($block) { + StaticBlock::getBlock($block)::neighborChanged($this, $x, $y, $z + 1, $x, $y, $z, $oldID); + } } - - public function fastSetBlockUpdate($x, $y, $z, $id, $meta, $updateBlocksAround = false, $tiles = false){ + + public function fastSetBlockUpdate($x, $y, $z, $id, $meta, $updateBlocksAround = false, $tiles = false) { $oldID = $this->level->getBlockID($x, $y, $z); - + $this->level->setBlock($x, $y, $z, $id, $meta); - - if($tiles){ //TODO rewrite + + if($tiles) { //TODO rewrite $this->server->api->tile->invalidateAll($this, $x, $y, $z); } - if($updateBlocksAround){ + if($updateBlocksAround) { self::updateNeighborsAt($x, $y, $z, $oldID); } $this->addBlockToSendQueue($x, $y, $z, $id, $meta); } - - public function onTick(PocketMinecraftServer $server, $currentTime){ - if(!$this->stopTime) ++$this->time; - for($cX = 0; $cX < 16; ++$cX){ - for($cZ = 0; $cZ < 16; ++$cZ){ + + public function onTick(PocketMinecraftServer $server, $currentTime) { + if(!$this->stopTime) { + ++$this->time; + } + for($cX = 0; $cX < 16; ++$cX) { + for($cZ = 0; $cZ < 16; ++$cZ) { $index = $this->level->getIndex($cX, $cZ); - if(!isset($this->level->chunks[$index]) || $this->level->chunks[$index] === false) continue; - for($c = 0; $c <= 20; ++$c){ + if(!isset($this->level->chunks[$index]) || $this->level->chunks[$index] === false) { + continue; + } + for($c = 0; $c <= 20; ++$c) { $xyz = mt_rand(0, 0xffffffff) >> 2; $x = $xyz & 0xf; $z = ($xyz >> 8) & 0xf; $y = ($xyz >> 16) & 0x7f; $id = $this->level->fastGetBlockID($cX, $y >> 4, $cZ, $x, $y & 0xf, $z, $index); - if(isset(self::$randomUpdateBlocks[$id])){ + if(isset(self::$randomUpdateBlocks[$id])) { $cl = StaticBlock::$prealloc[$id]; $cl::onRandomTick($this, ($cX << 4) + $x, $y, $z + ($cZ << 4)); } @@ -528,100 +600,99 @@ class Level{ } $this->totalMobsAmount = 0; $post = []; - - foreach($this->entityList as $k => $e){ - if(!($e instanceof Entity)){ + + foreach($this->entityList as $k => $e) { + if(!($e instanceof Entity)) { unset($this->entityList[$k]); unset($this->server->entities[$k]); //TODO try to remove from $entityListPositioned? continue; } - + $dd = CORRECT_ENTITY_CLASSES[$e->class] ?? false; - if($dd === false || ($dd !== true && !isset($dd[$e->type]))){ + if($dd === false || ($dd !== true && !isset($dd[$e->type]))) { ConsoleAPI::warn("Entity $k has invalid entity! {$e->class} {$e->type}"); $e->close(); - + unset($this->entityList[$k]); unset($this->server->entities[$k]); - - $curChunkX = (int)$e->x >> 4; - $curChunkZ = (int)$e->z >> 4; + + $curChunkX = (int) $e->x >> 4; + $curChunkZ = (int) $e->z >> 4; $index = "$curChunkX $curChunkZ"; - - if(isset($this->entityListPositioned[$index][$k])){ + + if(isset($this->entityListPositioned[$index][$k])) { unset($this->entityListPositioned[$index][$k]); } - + continue; } - $curChunkX = (int)$e->x >> 4; - $curChunkZ = (int)$e->z >> 4; - if($e->class === ENTITY_MOB && !$e->isPlayer()){ + $curChunkX = (int) $e->x >> 4; + $curChunkZ = (int) $e->z >> 4; + if($e->class === ENTITY_MOB && !$e->isPlayer()) { ++$this->totalMobsAmount; } - if($e->isPlayer() || $e->needsUpdate){ + if($e->isPlayer() || $e->needsUpdate) { $e->update($currentTime); - if(!$e->isPlayer()) $post[] = $k; + if(!$e->isPlayer()) { + $post[] = $k; + } } - - if($e instanceof Entity){ - $newChunkX = (int)$e->x >> 4; - $newChunkZ = (int)$e->z >> 4; - if($e->chunkX != $newChunkX || $e->chunkZ != $newChunkZ){ + + if($e instanceof Entity) { + $newChunkX = (int) $e->x >> 4; + $newChunkZ = (int) $e->z >> 4; + if($e->chunkX != $newChunkX || $e->chunkZ != $newChunkZ) { $oldIndex = "{$e->chunkX} {$e->chunkZ}"; unset($this->entityListPositioned[$oldIndex][$e->eid]); - - if($e->level == $this){ + + if($e->level == $this) { $e->chunkX = $newChunkX; $e->chunkZ = $newChunkZ; $newIndex = "$newChunkX $newChunkZ"; $this->entityListPositioned[$newIndex][$e->eid] = $e->eid; } } - if($e->level != $this && isset($this->entityListPositioned["$curChunkX $curChunkZ"])){ + if($e->level != $this && isset($this->entityListPositioned["$curChunkX $curChunkZ"])) { unset($this->entityListPositioned["$curChunkX $curChunkZ"][$e->eid]); - }elseif($curChunkX != $newChunkX || $curChunkZ != $newChunkZ){ + } elseif($curChunkX != $newChunkX || $curChunkZ != $newChunkZ) { $index = "$curChunkX $curChunkZ"; //while creating index like $curChunkX << 32 | $curChunkZ is faster, placing it inside list is slow $newIndex = "$newChunkX $newChunkZ"; unset($this->entityListPositioned[$index][$e->eid]); $this->entityListPositioned[$newIndex][$e->eid] = $e->eid; //set to e->eid to avoid possible memory leaks } - - if($e->searchForClosestPlayers){ + + if($e->searchForClosestPlayers) { $e->handlePrePlayerSearcher(); - - foreach($this->players as $player){ - $dist = ($e->x - $player->entity->x)*($e->x - $player->entity->x) + ($e->y - $player->entity->y)*($e->y - $player->entity->y) + ($e->z - $player->entity->z)*($e->z - $player->entity->z); + + foreach($this->players as $player) { + $dist = ($e->x - $player->entity->x) * ($e->x - $player->entity->x) + ($e->y - $player->entity->y) * ($e->y - $player->entity->y) + ($e->z - $player->entity->z) * ($e->z - $player->entity->z); $e->handlePlayerSearcher($player, $dist); } } - - - - }elseif(isset($this->entityListPositioned["$curChunkX $curChunkZ"])){ + + } elseif(isset($this->entityListPositioned["$curChunkX $curChunkZ"])) { unset($this->entityListPositioned["$curChunkX $curChunkZ"][$k]); } } - + $this->checkSleep(); - - if($server->ticks % 100 === 0){ + + if($server->ticks % 100 === 0) { $this->mobSpawner->handle(); } - - - foreach($this->players as $player){ - foreach($post as $eid){ + + foreach($this->players as $player) { + foreach($post as $eid) { $e = $this->entityList[$eid] ?? false; - if(!($e instanceof Entity)){ + if(!($e instanceof Entity)) { continue; } $player->addEntityMovementUpdateToQueue($e); } $player->sendEntityMovementUpdateQueue(); - - foreach($this->queuedBlockUpdates as $ind => $update){ + + foreach($this->queuedBlockUpdates as $ind => $update) { $x = $update[0]; $y = $update[1]; $z = $update[2]; @@ -632,42 +703,44 @@ class Level{ } $player->sendBlockUpdateQueue(); } - + $this->queuedBlockUpdates = []; } - - public function isBoundingBoxOnFire(AxisAlignedBB $bb){ + + public function isBoundingBoxOnFire(AxisAlignedBB $bb) { $minX = floor($bb->minX); $maxX = floor($bb->maxX + 1); $minY = floor($bb->minY); $maxY = floor($bb->maxY + 1); $minZ = floor($bb->minZ); $maxZ = floor($bb->maxZ + 1); - - for($x = $minX; $x < $maxX; ++$x){ - for($y = $minY; $y < $maxY; ++$y){ - for($z = $minZ; $z < $maxZ; ++$z){ + + for($x = $minX; $x < $maxX; ++$x) { + for($y = $minY; $y < $maxY; ++$y) { + for($z = $minZ; $z < $maxZ; ++$z) { $blockAt = $this->level->getBlockID($x, $y, $z); - if($blockAt == FIRE || $blockAt == STILL_LAVA || $blockAt == LAVA) return true; + if($blockAt == FIRE || $blockAt == STILL_LAVA || $blockAt == LAVA) { + return true; + } } } } - + return false; } - public function getEntitiesInAABBOfType(AxisAlignedBB $bb, $class){ - $minChunkX = ((int)($bb->minX)) >> 4; - $minChunkZ = ((int)($bb->minZ)) >> 4; - $maxChunkX = ((int)($bb->maxX)) >> 4; - $maxChunkZ = ((int)($bb->maxZ)) >> 4; + public function getEntitiesInAABBOfType(AxisAlignedBB $bb, $class) { + $minChunkX = ((int) ($bb->minX)) >> 4; + $minChunkZ = ((int) ($bb->minZ)) >> 4; + $maxChunkX = ((int) ($bb->maxX)) >> 4; + $maxChunkZ = ((int) ($bb->maxZ)) >> 4; $ents = []; - for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ - for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){ + for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX) { + for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ) { $ind = "$chunkX $chunkZ"; - foreach($this->entityListPositioned[$ind] ?? [] as $ind2 => $entid){ - if(isset($this->entityList[$entid]) && $this->entityList[$entid] instanceof Entity && $this->entityList[$entid]->class === $class && $this->entityList[$entid]->boundingBox->intersectsWith($bb)){ + foreach($this->entityListPositioned[$ind] ?? [] as $ind2 => $entid) { + if(isset($this->entityList[$entid]) && $this->entityList[$entid] instanceof Entity && $this->entityList[$entid]->class === $class && $this->entityList[$entid]->boundingBox->intersectsWith($bb)) { $ents[$entid] = $this->entityList[$entid]; - }elseif(!isset($this->entityList[$entid])){ + } elseif(!isset($this->entityList[$entid])) { ConsoleAPI::debug("Removing entity from level array at index $ind/$ind2: $entid"); unset($this->entityListPositioned[$ind][$ind2]); } @@ -677,23 +750,22 @@ class Level{ return $ents; } /** - * @param AxisAlignedBB $bb * @return Entity[] */ - public function getEntitiesInAABB(AxisAlignedBB $bb){ - $minChunkX = ((int)($bb->minX)) >> 4; - $minChunkZ = ((int)($bb->minZ)) >> 4; - $maxChunkX = ((int)($bb->maxX)) >> 4; - $maxChunkZ = ((int)($bb->maxZ)) >> 4; + public function getEntitiesInAABB(AxisAlignedBB $bb) { + $minChunkX = ((int) ($bb->minX)) >> 4; + $minChunkZ = ((int) ($bb->minZ)) >> 4; + $maxChunkX = ((int) ($bb->maxX)) >> 4; + $maxChunkZ = ((int) ($bb->maxZ)) >> 4; $ents = []; //TODO also index by chunkY? - for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ - for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){ + for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX) { + for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ) { $ind = "$chunkX $chunkZ"; - foreach($this->entityListPositioned[$ind] ?? [] as $ind2 => $entid){ - if(isset($this->entityList[$entid]) && $this->entityList[$entid] instanceof Entity && $this->entityList[$entid]->boundingBox->intersectsWith($bb)){ + foreach($this->entityListPositioned[$ind] ?? [] as $ind2 => $entid) { + if(isset($this->entityList[$entid]) && $this->entityList[$entid] instanceof Entity && $this->entityList[$entid]->boundingBox->intersectsWith($bb)) { $ents[$entid] = $this->entityList[$entid]; - }elseif(!isset($this->entityList[$entid])){ + } elseif(!isset($this->entityList[$entid])) { ConsoleAPI::debug("Removing entity from level array at index $ind/$ind2: $entid"); unset($this->entityListPositioned[$ind][$ind2]); } @@ -702,147 +774,147 @@ class Level{ } return $ents; } - - public function addBlockToSendQueue($x, $y, $z, $id, $meta){ - if(!$this->forceDisableBlockQueue){ + + public function addBlockToSendQueue($x, $y, $z, $id, $meta) { + if(!$this->forceDisableBlockQueue) { $this->queuedBlockUpdates["$x $y $z"] = [$x, $y, $z, $id, $meta]; } } - - public function setBlock(Vector3 $pos, Block $block, $update = true, $tiles = false, $direct = false){ - if(!isset($this->level) or (($pos instanceof Position) and $pos->level !== $this) or $pos->x < 0 or $pos->y < 0 or $pos->z < 0){ + + public function setBlock(Vector3 $pos, Block $block, $update = true, $tiles = false, $direct = false) { + if(!isset($this->level) or (($pos instanceof Position) and $pos->level !== $this) or $pos->x < 0 or $pos->y < 0 or $pos->z < 0) { return false; } $oldID = $this->level->getBlockID($pos->x, $pos->y, $pos->z); $ret = $this->level->setBlock($pos->x, $pos->y, $pos->z, $block->getID(), $block->getMetadata()); - if($ret === true){ - if(!($pos instanceof Position)){ + if($ret === true) { + if(!($pos instanceof Position)) { $pos = new Position($pos->x, $pos->y, $pos->z, $this); } $block->position($pos); - if($direct === true){ + if($direct === true) { $this->addBlockToSendQueue($pos->x, $pos->y, $pos->z, $block->id, $block->meta); - }else{ + } else { $i = ($pos->x >> 4) . ":" . ($pos->y >> 4) . ":" . ($pos->z >> 4); - if(!isset($this->changedBlocks[$i])){ + if(!isset($this->changedBlocks[$i])) { $this->changedBlocks[$i] = []; $this->changedCount[$i] = 0; } $this->changedBlocks[$i][] = ($block->x & 0xff) << 32 | ($block->y & 0xff) << 24 | ($block->z & 0xff) << 16 | ($block->getID() & 0xff) << 8 | ($block->meta & 0xff); ++$this->changedCount[$i]; } - - if($tiles === true){ + + if($tiles === true) { $this->server->api->tile->invalidateAll($this, $pos->x, $pos->y, $pos->z); } - - if($update === true){ + + if($update === true) { $this->updateNeighborsAt($pos->x, $pos->y, $pos->z, $oldID); } - + } return $ret; } - public function getMiniChunk($X, $Z, $Y){ - if(!isset($this->level)){ + public function getMiniChunk($X, $Z, $Y) { + if(!isset($this->level)) { return false; } return $this->level->getMiniChunk($X, $Z, $Y); } - public function setMiniChunk($X, $Z, $Y, $data){ - if(!isset($this->level)){ + public function setMiniChunk($X, $Z, $Y, $data) { + if(!isset($this->level)) { return false; } $this->changedCount[$X . ":" . $Y . ":" . $Z] = 4096; return $this->level->setMiniChunk($X, $Z, $Y, $data); } - public function loadChunk($X, $Z){ - if(!isset($this->level)){ + public function loadChunk($X, $Z) { + if(!isset($this->level)) { return false; } return $this->level->loadChunk($X, $Z); } - public function unloadChunk($X, $Z, $force = false){ - if(!isset($this->level)){ + public function unloadChunk($X, $Z, $force = false) { + if(!isset($this->level)) { return false; } - if($force !== true and $this->isSpawnChunk($X, $Z)){ + if($force !== true and $this->isSpawnChunk($X, $Z)) { return false; } return $this->level->unloadChunk($X, $Z, $this->server->saveEnabled); } - public function getOrderedChunk($X, $Z, $Yndex){ - if(!isset($this->level)){ + public function getOrderedChunk($X, $Z, $Yndex) { + if(!isset($this->level)) { return false; } $raw = []; - for($Y = 0; $Y < 8; ++$Y){ - if(($Yndex & (1 << $Y)) > 0){ + for($Y = 0; $Y < 8; ++$Y) { + if(($Yndex & (1 << $Y)) > 0) { $raw[$Y] = $this->level->getMiniChunk($X, $Z, $Y); } } $ordered = ""; $flag = chr($Yndex); - for($j = 0; $j < 256; ++$j){ + for($j = 0; $j < 256; ++$j) { $ordered .= $flag; - foreach($raw as $mini){ + foreach($raw as $mini) { $ordered .= substr($mini, $j << 5, 24); //16 + 8 } } return $ordered; } - public function getOrderedMiniChunk($X, $Z, $Y){ - if(!isset($this->level)){ + public function getOrderedMiniChunk($X, $Z, $Y) { + if(!isset($this->level)) { return false; } $raw = $this->level->getMiniChunk($X, $Z, $Y); $ordered = ""; $flag = chr(1 << $Y); - for($j = 0; $j < 256; ++$j){ + for($j = 0; $j < 256; ++$j) { $ordered .= $flag . substr($raw, $j << 5, 24); //16 + 8 } return $ordered; } - public function getSafeSpawn($spawn = false){ - if($spawn === false){ + public function getSafeSpawn($spawn = false) { + if($spawn === false) { $spawn = $this->getSpawn(); } - if($spawn instanceof Vector3){ + if($spawn instanceof Vector3) { $x = (int) round($spawn->x); $fy = $y = (int) round($spawn->y); $z = (int) round($spawn->z); - if($x < 0 || $x > 255 || $z < 0 || $z > 255){ + if($x < 0 || $x > 255 || $z < 0 || $z > 255) { return new Position($x, 128, $z, $this); } - for(; $y > 0; --$y){ + for(; $y > 0; --$y) { $v = new Vector3($x, $y, $z); $b = $this->getBlock($v->getSide(0)); - if($b === false){ + if($b === false) { return $spawn; - }elseif(!($b instanceof AirBlock)){ + } elseif(!($b instanceof AirBlock)) { break; } } - if($y == 0){ + if($y == 0) { return new Position($x, $fy, $z, $this); //force tp to default if pos is empty } - for(; $y < 128; ++$y){ + for(; $y < 128; ++$y) { $v = new Vector3($x, $y, $z); - if($this->getBlock($v->getSide(1)) instanceof AirBlock){ - if($this->getBlock($v) instanceof AirBlock){ + if($this->getBlock($v->getSide(1)) instanceof AirBlock) { + if($this->getBlock($v) instanceof AirBlock) { return new Position($x, $y, $z, $this); } - }else{ + } else { ++$y; } } @@ -851,13 +923,13 @@ class Level{ return false; } - public function getSpawn(){ - if(!isset($this->level)){ + public function getSpawn() { + if(!isset($this->level)) { return false; } return new Position($this->level->getData("spawnX"), $this->level->getData("spawnY"), $this->level->getData("spawnZ"), $this); } - + /** * @param number $x * @param number $y @@ -865,26 +937,25 @@ class Level{ * @param boolean $positionfy assign coordinates to block or not * @return GenericBlock | false if failed */ - - public function getBlockWithoutVector($x, $y, $z, $positionfy = true){ - $b = $this->level->getBlock((int)$x, (int)$y, (int)$z); + + public function getBlockWithoutVector($x, $y, $z, $positionfy = true) { + $b = $this->level->getBlock((int) $x, (int) $y, (int) $z); return BlockAPI::get($b[0], $b[1], $positionfy ? new Position($x, $y, $z, $this) : false); } - + /** * Recommended to use {@link getBlockWithoutVector()} if you dont have the vector - * @param Vector3 $pos * @return Block|false if failed */ - public function getBlock(Vector3 $pos){ - if(!isset($this->level) or ($pos instanceof Position) and $pos->level !== $this){ + public function getBlock(Vector3 $pos) { + if(!isset($this->level) or ($pos instanceof Position) and $pos->level !== $this) { return false; } return $this->getBlockWithoutVector($pos->x, $pos->y, $pos->z); } - public function setSpawn(Vector3 $pos){ - if(!isset($this->level)){ + public function setSpawn(Vector3 $pos) { + if(!isset($this->level)) { return false; } $this->level->setData("spawnX", $pos->x); @@ -892,69 +963,69 @@ class Level{ $this->level->setData("spawnZ", $pos->z); } - public function getTime(){ + public function getTime() { return (int) ($this->time); } - public function setTime($time){ + public function setTime($time) { $this->startTime = $this->time = (int) $time; $this->startCheck = microtime(true); $this->checkTime(); } - public function checkTime(){ - if(!isset($this->level)){ + public function checkTime() { + if(!isset($this->level)) { return false; } $now = microtime(true); - if($this->stopTime){ + if($this->stopTime) { $time = $this->startTime; - }else{ + } else { $time = $this->startTime + ($now - $this->startCheck) * 20; } - if($this->server->api->dhandle("time.change", ["level" => $this, "time" => $time]) !== false){ //send time to player every 5 ticks + if($this->server->api->dhandle("time.change", ["level" => $this, "time" => $time]) !== false) { //send time to player every 5 ticks $this->time = $time; $pk = new SetTimePacket; $pk->time = (int) $this->time; $pk->started = $this->stopTime == false; $this->server->api->player->broadcastPacket($this->players, $pk); - }else{ + } else { $this->time -= 20 * 13; } } - - public function isTimeStopped(){ + + public function isTimeStopped() { return $this->stopTime; } - - public function stopTime(){ + + public function stopTime() { $this->stopTime = true; $this->startCheck = 0; $this->checkTime(); } - public function startTime(){ + public function startTime() { $this->stopTime = false; $this->startCheck = microtime(true); $this->checkTime(); } - public function getSeed(){ - if(!isset($this->level)){ + public function getSeed() { + if(!isset($this->level)) { return false; } return (int) $this->level->getData("seed"); } - public function setSeed($seed){ - if(!isset($this->level)){ + public function setSeed($seed) { + if(!isset($this->level)) { return false; } $this->level->setData("seed", (int) $seed); } - public function scheduleBlockUpdate(Position $pos, $delay, $type = BLOCK_UPDATE_SCHEDULED){ - if(!isset($this->level)){ + public function scheduleBlockUpdate(Position $pos, $delay, $type = BLOCK_UPDATE_SCHEDULED) { + if(!isset($this->level)) { return false; } return $this->server->api->block->scheduleBlockUpdate($pos, $delay, $type); diff --git a/src/world/LevelImport.php b/src/world/LevelImport.php index 49f21773..b42743dc 100644 --- a/src/world/LevelImport.php +++ b/src/world/LevelImport.php @@ -1,33 +1,33 @@ path = $path; } - public function import(){ - if(file_exists($this->path . "tileEntities.dat")){ //OldPM + public function import() { + if(file_exists($this->path . "tileEntities.dat")) { //OldPM $level = unserialize(file_get_contents($this->path . "level.dat")); console("[INFO] Importing OldPM level \"" . $level["LevelName"] . "\" to PMF format"); $entities = new Config($this->path . "entities.yml", CONFIG_YAML, unserialize(file_get_contents($this->path . "entities.dat"))); $entities->save(); $tiles = new Config($this->path . "tiles.yml", CONFIG_YAML, unserialize(file_get_contents($this->path . "tileEntities.dat"))); $tiles->save(); - }elseif(file_exists($this->path . "chunks.dat") and file_exists($this->path . "level.dat")){ //Pocket + } elseif(file_exists($this->path . "chunks.dat") and file_exists($this->path . "level.dat")) { //Pocket $nbt = new NBT(); $nbt->load(substr(file_get_contents($this->path . "level.dat"), 8)); $level = array_shift($nbt->tree); - if($level["LevelName"] == ""){ + if($level["LevelName"] == "") { $level["LevelName"] = "world" . time(); } console("[INFO] Importing Pocket level \"" . $level["LevelName"] . "\" to PMF format"); unset($level["Player"]); $nbt->load(substr(file_get_contents($this->path . "entities.dat"), 12)); $entities = array_shift($nbt->tree); - if(!isset($entities["TileEntities"])){ + if(!isset($entities["TileEntities"])) { $entities["TileEntities"] = []; } $tiles = $entities["TileEntities"]; @@ -36,7 +36,7 @@ class LevelImport{ $entities->save(); $tiles = new Config($this->path . "tiles.yml", CONFIG_YAML, $tiles); $tiles->save(); - }else{ + } else { return false; } @@ -54,8 +54,8 @@ class LevelImport{ $chunks = new PocketChunkParser(); $chunks->loadFile($this->path . "chunks.dat"); $chunks->loadMap(); - for($Z = 0; $Z < 16; ++$Z){ - for($X = 0; $X < 16; ++$X){ + for($Z = 0; $Z < 16; ++$Z) { + for($X = 0; $X < 16; ++$X) { $chunk = [ 0 => "", 1 => "", @@ -66,18 +66,18 @@ class LevelImport{ 6 => "", 7 => "" ]; - for($z = 0; $z < 16; ++$z){ - for($x = 0; $x < 16; ++$x){ + for($z = 0; $z < 16; ++$z) { + for($x = 0; $x < 16; ++$x) { $block = $chunks->getChunkColumn($X, $Z, $x, $z, 0); $meta = $chunks->getChunkColumn($X, $Z, $x, $z, 1); - for($Y = 0; $Y < 8; ++$Y){ + for($Y = 0; $Y < 8; ++$Y) { $chunk[$Y] .= substr($block, $Y << 4, 16); $chunk[$Y] .= substr($meta, $Y << 3, 8); $chunk[$Y] .= "\x00\x00\x00\x00\x00\x00\x00\x00"; } } } - foreach($chunk as $Y => $data){ + foreach($chunk as $Y => $data) { $pmf->setMiniChunk($X, $Z, $Y, $data); } $pmf->saveChunk($X, $Z); @@ -86,15 +86,29 @@ class LevelImport{ } $chunks->map = null; $chunks = null; - if(file_exists($this->path . "level.dat")) @unlink($this->path . "level.dat"); - if(file_exists($this->path . "level.dat_old")) @unlink($this->path . "level.dat_old"); - if(file_exists($this->path . "player.dat")) @unlink($this->path . "player.dat"); - if(file_exists($this->path . "entities.dat")) @unlink($this->path . "entities.dat"); - if(file_exists($this->path . "chunks.dat")) @unlink($this->path . "chunks.dat"); - if(file_exists($this->path . "chunks.dat.gz")) @unlink($this->path . "chunks.dat.gz"); - if(file_exists($this->path . "tiles.dat")) @unlink($this->path . "tiles.dat"); + if(file_exists($this->path . "level.dat")) { + @unlink($this->path . "level.dat"); + } + if(file_exists($this->path . "level.dat_old")) { + @unlink($this->path . "level.dat_old"); + } + if(file_exists($this->path . "player.dat")) { + @unlink($this->path . "player.dat"); + } + if(file_exists($this->path . "entities.dat")) { + @unlink($this->path . "entities.dat"); + } + if(file_exists($this->path . "chunks.dat")) { + @unlink($this->path . "chunks.dat"); + } + if(file_exists($this->path . "chunks.dat.gz")) { + @unlink($this->path . "chunks.dat.gz"); + } + if(file_exists($this->path . "tiles.dat")) { + @unlink($this->path . "tiles.dat"); + } unset($chunks, $level, $entities, $tiles, $nbt); return true; } -} \ No newline at end of file +} diff --git a/src/world/MobSpawner.php b/src/world/MobSpawner.php index 48d63306..dd8aa69e 100644 --- a/src/world/MobSpawner.php +++ b/src/world/MobSpawner.php @@ -1,146 +1,149 @@ server = ServerAPI::request(); $this->level = $level; } - public function countEntities(){ + public function countEntities() { return $this->level->totalMobsAmount; } - - public function checkDespawn(Living $living){ - if(!isset($this->entityAffectedPlayers[$living->eid])){ + + public function checkDespawn(Living $living) { + if(!isset($this->entityAffectedPlayers[$living->eid])) { return false; } - + $playerID = $this->entityAffectedPlayers[$living->eid]; $player = $this->level->entityList[$playerID] ?? false; - if($player === false){ + if($player === false) { //TODO try retargetting other player? return true; - }else{ + } else { $diffX = $living->x - $player->x; $diffZ = $living->z - $player->z; - $dist = $diffX*$diffX + $diffZ*$diffZ; - if($dist <= 512){ + $dist = $diffX * $diffX + $diffZ * $diffZ; + if($dist <= 512) { return false; } - + return $dist > 4096 || mt_rand($dist, 4096) == $dist; //force despawn 64 blocks away or despawn randomly } - + } - - public function handle(){ - if($this->countEntities() > self::$MOB_LIMIT || count($this->level->players) <= 0){ + + public function handle() { + if($this->countEntities() > self::$MOB_LIMIT || count($this->level->players) <= 0) { return false; //not spawning } $svd = $this->totalMobsPerPlayer; $this->totalMobsPerPlayer = min(ceil(self::$MOB_LIMIT / count($this->level->players)), self::$maxMobsNearPlayerAtOnce); - if($svd != $this->totalMobsPerPlayer) ConsoleAPI::debug("Changed total mobs per player from $svd to {$this->totalMobsPerPlayer}."); - + if($svd != $this->totalMobsPerPlayer) { + ConsoleAPI::debug("Changed total mobs per player from $svd to {$this->totalMobsPerPlayer}."); + } + return $this->spawnMobs(); } - public function spawnMobs(){ + public function spawnMobs() { $phase = $this->server->api->time->getPhase($this->level); - if(self::$spawnAnimals && ($phase == "day" || $phase == "sunrise")){ //Animal + if(self::$spawnAnimals && ($phase == "day" || $phase == "sunrise")) { //Animal $type = mt_rand(10, 13); $baby = false; $grassOnly = true; - }elseif(self::$spawnMobs && ($phase == "night" || $phase == "sunset") && $this->server->difficulty > 0){ //Monster, true night + } elseif(self::$spawnMobs && ($phase == "night" || $phase == "sunset") && $this->server->difficulty > 0) { //Monster, true night $type = mt_rand(32, 35); $grassOnly = false; $baby = 2; - }else{ + } else { return false; } - - foreach($this->level->players as $player){ - if(isset($this->playerAffectedEIDS[$player->entity->eid]) && count($this->playerAffectedEIDS[$player->entity->eid]) > $this->totalMobsPerPlayer){ + + foreach($this->level->players as $player) { + if(isset($this->playerAffectedEIDS[$player->entity->eid]) && count($this->playerAffectedEIDS[$player->entity->eid]) > $this->totalMobsPerPlayer) { continue; } - + $x = mt_rand($player->entity->x - 32, $player->entity->x + 32); $z = mt_rand($player->entity->z - 32, $player->entity->z + 32); - $diffX = $x-$player->entity->x; - $diffZ = $z-$player->entity->z; - $dist = $diffX*$diffX + $diffZ*$diffZ; - if($dist < 768){ + $diffX = $x - $player->entity->x; + $diffZ = $z - $player->entity->z; + $dist = $diffX * $diffX + $diffZ * $diffZ; + if($dist < 768) { continue; } - + $cnt = mt_rand(1, 3); - - for($i = 0; $i < $cnt; ++$i){ - + + for($i = 0; $i < $cnt; ++$i) { + $xMob = $x + mt_rand(-3, 3); $zMob = $z + mt_rand(-3, 3); - - + $y = $this->getSafeY($xMob, $zMob, $grassOnly, $type >= 32 && $type <= 36 && $type != 35); - if(!$y || $y < 0){ + if(!$y || $y < 0) { continue; } - + $data = $this->genPosData($xMob, $y + 0.5, $zMob); - if($baby != 2) $data["IsBaby"] = $baby; - + if($baby != 2) { + $data["IsBaby"] = $baby; + } + $e = $this->server->api->entity->add($this->level, 2, $type, $data); - - if($e instanceof Entity){ + + if($e instanceof Entity) { $this->server->api->entity->spawnToAll($e); ConsoleAPI::debug("$type spawned at $xMob, $y, $zMob"); } - if(!isset($this->playerAffectedEIDS[$player->entity->eid])){ + if(!isset($this->playerAffectedEIDS[$player->entity->eid])) { $this->playerAffectedEIDS[$player->entity->eid] = [$e->eid => true]; - }else{ + } else { $this->playerAffectedEIDS[$player->entity->eid][$e->eid] = true; } - + $this->entityAffectedPlayers[$e->eid] = $player->entity->eid; - + } } - - + return true; } - - private function genPosData($x, $y, $z){ + + private function genPosData($x, $y, $z) { return [ "x" => $x + 0.5, "y" => $y, "z" => $z + 0.5 ]; } - - protected function getSafeY($x, $z, $grassOnly = false, $highMob = false){ //first safe block //TODO check boundingbox + + protected function getSafeY($x, $z, $grassOnly = false, $highMob = false) { //first safe block //TODO check boundingbox $allowed = []; - for($y = 0; $y < 128; ++$y){ + for($y = 0; $y < 128; ++$y) { $b = $this->level->level->getBlockID($x, $y, $z); $b2 = $this->level->level->getBlockID($x, $y + 1, $z); $b1 = $this->level->level->getBlockID($x, $y - 1, $z); if( - !StaticBlock::getIsSolid($b) && !StaticBlock::getIsLiquid($b) && + !StaticBlock::getIsSolid($b) && !StaticBlock::getIsLiquid($b) && (StaticBlock::getIsSolid($b1) && (!$grassOnly || $b1 === GRASS) && (!$highMob || !StaticBlock::getIsSolid($b2) && !StaticBlock::getIsLiquid($b2))) - ){ + ) { $allowed[] = $y; } } - + return empty($allowed) ? -1 : $allowed[mt_rand(0, count($allowed) - 1)]; } } - diff --git a/src/world/MovingObjectPosition.php b/src/world/MovingObjectPosition.php index 2e6a6dfc..a470538d 100644 --- a/src/world/MovingObjectPosition.php +++ b/src/world/MovingObjectPosition.php @@ -1,5 +1,6 @@ blockX} {$this->blockY} {$this->blockZ}, side: {$this->sideHit} hitVec: {$this->hitVector} entityHit: {$this->entityHit}}"; } - + /** * @param int $x * @param int $y * @param int $z * @param int $side - * @param Vector3 $hitVector * * @return MovingObjectPosition */ - public static function fromBlock($x, $y, $z, $side, Vector3 $hitVector){ + public static function fromBlock($x, $y, $z, $side, Vector3 $hitVector) { $ob = new MovingObjectPosition; $ob->typeOfHit = 0; $ob->blockX = $x; @@ -49,15 +49,14 @@ class MovingObjectPosition{ } /** - * @param Entity $entity * * @return MovingObjectPosition */ - public static function fromEntity(Entity $entity){ + public static function fromEntity(Entity $entity) { $ob = new MovingObjectPosition; $ob->typeOfHit = 1; $ob->entityHit = $entity; $ob->hitVector = new Vector3($entity->x, $entity->y, $entity->z); return $ob; } -} \ No newline at end of file +} diff --git a/src/world/PocketChunkParser.php b/src/world/PocketChunkParser.php index 2c85bb4a..0917aa62 100644 --- a/src/world/PocketChunkParser.php +++ b/src/world/PocketChunkParser.php @@ -1,24 +1,26 @@ raw = gzinflate(file_get_contents($file . ".gz")); $r = @gzinflate($this->raw); - if($r !== false and $r != ""){ + if($r !== false and $r != "") { $this->raw = $r; } @unlink($file . ".gz"); file_put_contents($file, $this->raw); - }elseif(!file_exists($file)){ + } elseif(!file_exists($file)) { return false; - }else{ + } else { $this->raw = file_get_contents($file); } $this->file = $file; @@ -26,32 +28,32 @@ class PocketChunkParser{ return true; } - public function loadRaw($raw, $file){ + public function loadRaw($raw, $file) { $this->file = $file; $this->raw = $raw; $this->chunkLength = $this->sectorLength * ord($this->raw[0]); return true; } - public function getChunk($X, $Z){ + public function getChunk($X, $Z) { $X = (int) $X; $Z = (int) $Z; return substr($this->raw, $this->getOffset($X, $Z), $this->chunkLength); } - private function getOffset($X, $Z){ + private function getOffset($X, $Z) { return $this->location[$X + ($Z << 5)]; } - public function loadMap(){ - if($this->raw == ""){ + public function loadMap() { + if($this->raw == "") { return false; } $this->loadLocationTable(); console("[DEBUG] Loading chunks...", true, true, 2); - for($x = 0; $x < 16; ++$x){ + for($x = 0; $x < 16; ++$x) { $this->map[$x] = []; - for($z = 0; $z < 16; ++$z){ + for($z = 0; $z < 16; ++$z) { $this->map[$x][$z] = $this->parseChunk($x, $z); } } @@ -59,13 +61,13 @@ class PocketChunkParser{ console("[DEBUG] Chunks loaded!", true, true, 2); } - private function loadLocationTable(){ + private function loadLocationTable() { $this->location = []; console("[DEBUG] Loading Chunk Location table...", true, true, 2); - for($offset = 0; $offset < 0x1000; $offset += 4){ + for($offset = 0; $offset < 0x1000; $offset += 4) { $data = Utils::readLInt(substr($this->raw, $offset, 4)); $sectors = $data & 0xff; - if($sectors === 0){ + if($sectors === 0) { continue; } $sectorLocation = $data >> 8; @@ -73,7 +75,7 @@ class PocketChunkParser{ } } - public function parseChunk($X, $Z){ + public function parseChunk($X, $Z) { $X = (int) $X; $Z = (int) $Z; $offset = $this->getOffset($X, $Z); @@ -85,9 +87,9 @@ class PocketChunkParser{ 2 => [], //SkyLight 3 => [], //BlockLight ]; - foreach($chunk as $section => &$data){ + foreach($chunk as $section => &$data) { $l = $section === 0 ? 128 : 64; - for($i = 0; $i < 256; ++$i){ + for($i = 0; $i < 256; ++$i) { $data[$i] = substr($this->raw, $offset, $l); $offset += $l; } @@ -95,13 +97,13 @@ class PocketChunkParser{ return $chunk; } - public function saveMap($final = false){ + public function saveMap($final = false) { console("[DEBUG] Saving chunks...", true, true, 2); $fp = fopen($this->file, "r+b"); flock($fp, LOCK_EX); - foreach($this->map as $x => $d){ - foreach($d as $z => $chunk){ + foreach($this->map as $x => $d) { + foreach($d as $z => $chunk) { fseek($fp, $this->getOffset($x, $z)); fwrite($fp, $this->writeChunk($x, $z), $this->chunkLength); } @@ -112,41 +114,41 @@ class PocketChunkParser{ file_put_contents($this->file . ".gz", gzdeflate(gzdeflate(file_get_contents($this->file), 9), 9)); //Double compression for flat maps $compressed = filesize($this->file . ".gz"); console("[DEBUG] Saved chunks.dat.gz with " . round(($compressed / $original) * 100, 2) . "% (" . round($compressed / 1024, 2) . "KB) of the original size", true, true, 2); - if($final === true){ + if($final === true) { @unlink($this->file); } } - public function writeChunk($X, $Z){ + public function writeChunk($X, $Z) { $X = (int) $X; $Z = (int) $Z; - if(!isset($this->map[$X][$Z])){ + if(!isset($this->map[$X][$Z])) { return false; } $chunk = ""; - foreach($this->map[$X][$Z] as $section => $data){ - for($i = 0; $i < 256; ++$i){ + foreach($this->map[$X][$Z] as $section => $data) { + for($i = 0; $i < 256; ++$i) { $chunk .= $data[$i]; } } return Utils::writeLInt(strlen($chunk)) . $chunk; } - public function getFloor($x, $z){ + public function getFloor($x, $z) { $X = $x >> 4; $Z = $z >> 4; $aX = $x - ($X << 4); $aZ = $z - ($Z << 4); $index = $aZ + ($aX << 4); - for($y = 127; $y <= 0; --$y){ - if($this->map[$X][$Z][0][$index][$y] !== "\x00"){ + for($y = 127; $y <= 0; --$y) { + if($this->map[$X][$Z][0][$index][$y] !== "\x00") { break; } } return $y; } - public function getBlock($x, $y, $z){ + public function getBlock($x, $y, $z) { $x = (int) $x; $y = (int) $y; $z = (int) $z; @@ -157,20 +159,20 @@ class PocketChunkParser{ $index = $aZ + ($aX << 4); $block = ord($this->map[$X][$Z][0][$index][$y]); $meta = ord($this->map[$X][$Z][1][$index][$y >> 1]); - if(($y & 1) === 0){ + if(($y & 1) === 0) { $meta = $meta & 0x0F; - }else{ + } else { $meta = $meta >> 4; } return [$block, $meta]; } - public function getChunkColumn($X, $Z, $x, $z, $type = 0){ + public function getChunkColumn($X, $Z, $x, $z, $type = 0) { $index = $z + ($x << 4); return $this->map[$X][$Z][$type][$index]; } - public function setBlock($x, $y, $z, $block, $meta = 0){ + public function setBlock($x, $y, $z, $block, $meta = 0) { $x = (int) $x; $y = (int) $y; $z = (int) $z; @@ -181,12 +183,12 @@ class PocketChunkParser{ $index = $aZ + ($aX << 4); $this->map[$X][$Z][0][$index][$y] = chr($block); $old_meta = ord($this->map[$X][$Z][1][$index][$y >> 1]); - if(($y & 1) === 0){ + if(($y & 1) === 0) { $meta = ($old_meta & 0xF0) | ($meta & 0x0F); - }else{ + } else { $meta = (($meta << 4) & 0xF0) | ($old_meta & 0x0F); } $this->map[$X][$Z][1][$index][$y >> 1] = chr($meta); } -} \ No newline at end of file +} diff --git a/src/world/Position.php b/src/world/Position.php index e83861b2..d7308c2d 100644 --- a/src/world/Position.php +++ b/src/world/Position.php @@ -1,13 +1,13 @@ x, $x->y, $x->z); - }else{ + } else { $this->x = $x; $this->y = $y; $this->z = $z; @@ -15,25 +15,25 @@ class Position extends Vector3{ $this->level = $level; } - public function setXYZLevel($x, $y, $z, Level $level){ + public function setXYZLevel($x, $y, $z, Level $level) { $this->x = $x; $this->y = $y; $this->z = $z; $this->level = $level; } - public function getSide($side, $step = 1){ + public function getSide($side, $step = 1) { return new Position(parent::getSide($side, $step), 0, 0, $this->level); } - public function distance($x = 0, $y = 0, $z = 0){ - if(($x instanceof Position) and $x->level !== $this->level){ + public function distance($x = 0, $y = 0, $z = 0) { + if(($x instanceof Position) and $x->level !== $this->level) { return PHP_INT_MAX; } return parent::distance($x, $y, $z); } - - public function __toString(){ + + public function __toString() { return "Position(level=" . $this->level->getName() . ",x=" . $this->x . ",y=" . $this->y . ",z=" . $this->z . ")"; } -} \ No newline at end of file +} diff --git a/src/world/Tile.php b/src/world/Tile.php index e270a68b..92cb0f36 100644 --- a/src/world/Tile.php +++ b/src/world/Tile.php @@ -1,6 +1,6 @@ server = ServerAPI::request(); $this->level = $level; $this->normal = true; $this->class = $class; $this->data = $data; $this->closed = false; - if($class === false){ + if($class === false) { $this->closed = true; } $this->name = ""; @@ -35,83 +37,83 @@ class Tile extends Position{ $this->y = (int) $y; $this->z = (int) $z; $this->server->query("INSERT OR REPLACE INTO tiles (ID, level, class, x, y, z) VALUES (" . $this->id . ", '" . $this->level->getName() . "', '" . $this->class . "', " . $this->x . ", " . $this->y . ", " . $this->z . ");"); - switch($this->class){ + switch($this->class) { case TILE_CHEST: case TILE_SIGN: $this->server->query("UPDATE tiles SET spawnable = 1 WHERE ID = " . $this->id . ";"); break; case TILE_FURNACE: - if(!isset($this->data["BurnTime"]) or $this->data["BurnTime"] < 0){ + if(!isset($this->data["BurnTime"]) or $this->data["BurnTime"] < 0) { $this->data["BurnTime"] = 0; } - if(!isset($this->data["CookTime"]) or $this->data["CookTime"] < 0 or ($this->data["BurnTime"] === 0 and $this->data["CookTime"] > 0)){ + if(!isset($this->data["CookTime"]) or $this->data["CookTime"] < 0 or ($this->data["BurnTime"] === 0 and $this->data["CookTime"] > 0)) { $this->data["CookTime"] = 0; } - if(!isset($this->data["MaxTime"])){ + if(!isset($this->data["MaxTime"])) { $this->data["MaxTime"] = $this->data["BurnTime"]; $this->data["BurnTicks"] = 0; } - if($this->data["BurnTime"] > 0){ + if($this->data["BurnTime"] > 0) { $this->update(); } break; } } - public function update(){ - if($this->closed === true){ + public function update() { + if($this->closed === true) { return false; } - if($this->class === TILE_FURNACE){ + if($this->class === TILE_FURNACE) { $fuel = $this->getSlot(1); $raw = $this->getSlot(0); $product = $this->getSlot(2); $smelt = $raw->getSmeltItem(); $canSmelt = ($smelt !== false and $raw->count > 0 and (($product->getID() === $smelt->getID() and $product->getMetadata() === $smelt->getMetadata() and $product->count < $product->getMaxStackSize()) or $product->getID() === AIR)); - if($this->data["BurnTime"] <= 0 and $canSmelt and $fuel->getFuelTime() !== false and $fuel->count > 0){ + if($this->data["BurnTime"] <= 0 and $canSmelt and $fuel->getFuelTime() !== false and $fuel->count > 0) { $this->lastUpdate = microtime(true); $this->data["MaxTime"] = $this->data["BurnTime"] = floor($fuel->getFuelTime() * 20); $this->data["BurnTicks"] = 0; --$fuel->count; - if($fuel->count === 0){ + if($fuel->count === 0) { $fuel = BlockAPI::getItem(AIR, 0, 0); } $this->setSlot(1, $fuel, false); $current = $this->level->getBlock($this); - if($current->getID() === FURNACE){ + if($current->getID() === FURNACE) { $this->level->setBlock($this, BlockAPI::get(BURNING_FURNACE, $current->getMetadata()), true, false, true); } } - if($this->data["BurnTime"] > 0){ + if($this->data["BurnTime"] > 0) { $ticks = (microtime(true) - $this->lastUpdate) * 20; $this->data["BurnTime"] -= $ticks; $this->data["BurnTicks"] = ceil(($this->data["BurnTime"] / $this->data["MaxTime"]) * 200); - if($smelt !== false and $canSmelt){ + if($smelt !== false and $canSmelt) { $this->data["CookTime"] += $ticks; - if($this->data["CookTime"] >= 200){ //10 seconds + if($this->data["CookTime"] >= 200) { //10 seconds $product = BlockAPI::getItem($smelt->getID(), $smelt->getMetadata(), $product->count + 1); $this->setSlot(2, $product, false); --$raw->count; - if($raw->count === 0){ + if($raw->count === 0) { $raw = BlockAPI::getItem(AIR, 0, 0); } $this->setSlot(0, $raw, false); $this->data["CookTime"] -= 200; } - }elseif($this->data["BurnTime"] <= 0){ + } elseif($this->data["BurnTime"] <= 0) { $this->data["BurnTime"] = 0; $this->data["CookTime"] = 0; $this->data["BurnTicks"] = 0; - }else{ + } else { $this->data["CookTime"] = 0; } $this->server->schedule(2, [$this, "update"]); $this->scheduledUpdate = true; - }else{ + } else { $current = $this->level->getBlock($this); - if($current->getID() === BURNING_FURNACE){ + if($current->getID() === BURNING_FURNACE) { $this->level->setBlock($this, BlockAPI::get(FURNACE, $current->getMetadata()), true, false, true); } $this->data["CookTime"] = 0; @@ -124,28 +126,28 @@ class Tile extends Position{ $this->lastUpdate = microtime(true); } - public function getSlot($s){ + public function getSlot($s) { $i = $this->getSlotIndex($s); - if($i === false or $i < 0){ + if($i === false or $i < 0) { return BlockAPI::getItem(AIR, 0, 0); - }else{ + } else { return BlockAPI::getItem($this->data["Items"][$i]["id"], $this->data["Items"][$i]["Damage"], $this->data["Items"][$i]["Count"]); } } - public function getSlotIndex($s){ - if($this->class !== TILE_CHEST and $this->class !== TILE_FURNACE){ + public function getSlotIndex($s) { + if($this->class !== TILE_CHEST and $this->class !== TILE_FURNACE) { return false; } - foreach($this->data["Items"] as $i => $slot){ - if($slot["Slot"] === $s){ + foreach($this->data["Items"] as $i => $slot) { + if($slot["Slot"] === $s) { return $i; } } return -1; } - public function setSlot($s, Item $item, $update = true, $offset = 0){ + public function setSlot($s, Item $item, $update = true, $offset = 0) { $i = $this->getSlotIndex($s); $d = [ "Count" => $item->count, @@ -153,15 +155,15 @@ class Tile extends Position{ "id" => $item->getID(), "Damage" => $item->getMetadata(), ]; - if($i === false){ + if($i === false) { return false; - }elseif($item->getID() === AIR or $item->count <= 0){ - if($i >= 0){ + } elseif($item->getID() === AIR or $item->count <= 0) { + if($i >= 0) { unset($this->data["Items"][$i]); } - }elseif($i < 0){ + } elseif($i < 0) { $this->data["Items"][] = $d; - }else{ + } else { $this->data["Items"][$i] = $d; } $this->server->api->dhandle("tile.container.slot", [ @@ -171,14 +173,14 @@ class Tile extends Position{ "slotdata" => $item, ]); - if($update === true and $this->scheduledUpdate === false){ + if($update === true and $this->scheduledUpdate === false) { $this->update(); } return true; } - public function pairWith(Tile $tile){ - if($this->isPaired() or $tile->isPaired()){ + public function pairWith(Tile $tile) { + if($this->isPaired() or $tile->isPaired()) { return false; } @@ -194,18 +196,18 @@ class Tile extends Position{ $this->server->handle("tile.update", $tile); } - public function isPaired(){ - if($this->class !== TILE_CHEST){ + public function isPaired() { + if($this->class !== TILE_CHEST) { return false; } - if(!isset($this->data["pairx"]) or !isset($this->data["pairz"])){ + if(!isset($this->data["pairx"]) or !isset($this->data["pairz"])) { return false; } return true; } - public function unpair(){ - if(!$this->isPaired()){ + public function unpair() { + if(!$this->isPaired()) { return false; } @@ -214,36 +216,36 @@ class Tile extends Position{ $this->server->api->tile->spawnToAll($this); $this->server->handle("tile.update", $this); - if($tile instanceof Tile){ + if($tile instanceof Tile) { $this->server->api->tile->spawnToAll($tile); $this->server->handle("tile.update", $tile); } } - public function getPair(){ - if($this->isPaired()){ + public function getPair() { + if($this->isPaired()) { return $this->server->api->tile->get(new Position((int) $this->data["pairx"], $this->y, (int) $this->data["pairz"], $this->level)); } return false; } - public function openInventory(Player $player){ - if($this->class === TILE_CHEST){ + public function openInventory(Player $player) { + if($this->class === TILE_CHEST) { $player->windowCnt++; $player->windowCnt = $id = max(2, $player->windowCnt % 99); - if(($pair = $this->getPair()) !== false){ - if(($pair->x + ($pair->z << 13)) > ($this->x + ($this->z << 13))){ //Order them correctly + if(($pair = $this->getPair()) !== false) { + if(($pair->x + ($pair->z << 13)) > ($this->x + ($this->z << 13))) { //Order them correctly $player->windows[$id] = [ $pair, $this ]; - }else{ + } else { $player->windows[$id] = [ $this, $pair ]; } - }else{ + } else { $player->windows[$id] = $this; } @@ -257,9 +259,9 @@ class Tile extends Position{ $player->dataPacket($pk); $slots = []; - if(is_array($player->windows[$id])){ + if(is_array($player->windows[$id])) { $all = $this->server->api->player->getAll($this->level); - foreach($player->windows[$id] as $ob){ + foreach($player->windows[$id] as $ob) { $pk = new TileEventPacket; $pk->x = $ob->x; $pk->y = $ob->y; @@ -267,16 +269,16 @@ class Tile extends Position{ $pk->case1 = 1; $pk->case2 = 2; $this->server->api->player->broadcastPacket($all, $pk); - for($s = 0; $s < CHEST_SLOTS; ++$s){ + for($s = 0; $s < CHEST_SLOTS; ++$s) { $slot = $ob->getSlot($s); - if($slot->getID() > AIR and $slot->count > 0){ + if($slot->getID() > AIR and $slot->count > 0) { $slots[] = $slot; - }else{ + } else { $slots[] = BlockAPI::getItem(AIR, 0, 0); } } } - }else{ + } else { $pk = new TileEventPacket; $pk->x = $this->x; $pk->y = $this->y; @@ -284,11 +286,11 @@ class Tile extends Position{ $pk->case1 = 1; $pk->case2 = 2; $this->server->api->player->broadcastPacket($this->server->api->player->getAll($this->level), $pk); - for($s = 0; $s < CHEST_SLOTS; ++$s){ + for($s = 0; $s < CHEST_SLOTS; ++$s) { $slot = $this->getSlot($s); - if($slot->getID() > AIR and $slot->count > 0){ + if($slot->getID() > AIR and $slot->count > 0) { $slots[] = $slot; - }else{ + } else { $slots[] = BlockAPI::getItem(AIR, 0, 0); } } @@ -299,7 +301,7 @@ class Tile extends Position{ $pk->slots = $slots; $player->dataPacket($pk); return true; - }elseif($this->class === TILE_FURNACE){ + } elseif($this->class === TILE_FURNACE) { $player->windowCnt++; $player->windowCnt = $id = max(2, $player->windowCnt % 99); $player->windows[$id] = $this; @@ -314,11 +316,11 @@ class Tile extends Position{ $player->dataPacket($pk); $slots = []; - for($s = 0; $s < FURNACE_SLOTS; ++$s){ + for($s = 0; $s < FURNACE_SLOTS; ++$s) { $slot = $this->getSlot($s); - if($slot->getID() > AIR and $slot->count > 0){ + if($slot->getID() > AIR and $slot->count > 0) { $slots[] = $slot; - }else{ + } else { $slots[] = BlockAPI::getItem(AIR, 0, 0); } } @@ -330,14 +332,14 @@ class Tile extends Position{ } } - public function spawn($player){ - if($this->closed){ + public function spawn($player) { + if($this->closed) { return false; } - if(!($player instanceof Player)){ + if(!($player instanceof Player)) { $player = $this->server->api->player->get($player); } - switch($this->class){ + switch($this->class) { case TILE_CHEST: $nbt = new NBT(); $nbt->write(chr(NBT::TAG_COMPOUND) . "\x00\x00"); @@ -358,7 +360,7 @@ class Tile extends Position{ $nbt->writeTAG_String("z"); $nbt->writeTAG_Int((int) $this->z); - if($this->isPaired()){ + if($this->isPaired()) { $nbt->write(chr(NBT::TAG_INT)); $nbt->writeTAG_String("pairx"); $nbt->writeTAG_Int((int) $this->data["pairx"]); @@ -425,8 +427,8 @@ class Tile extends Position{ } } - public function setText($line1 = "", $line2 = "", $line3 = "", $line4 = ""){ - if($this->class !== TILE_SIGN){ + public function setText($line1 = "", $line2 = "", $line3 = "", $line4 = "") { + if($this->class !== TILE_SIGN) { return false; } $this->data["Text1"] = $line1; @@ -438,7 +440,7 @@ class Tile extends Position{ return true; } - public function getText(){ + public function getText() { return [ $this->data["Text1"], $this->data["Text2"], @@ -447,24 +449,23 @@ class Tile extends Position{ ]; } - public function __destruct(){ + public function __destruct() { $this->close(); } - public function close(){ - if($this->closed === false){ + public function close() { + if($this->closed === false) { $this->closed = true; $this->server->api->tile->remove($this->id); } } - public function getName(){ + public function getName() { return $this->name; } - - public function setPosition(Vector3 $pos){ - if($pos instanceof Position){ + public function setPosition(Vector3 $pos) { + if($pos instanceof Position) { $this->level = $pos->level; $this->server->query("UPDATE tiles SET level = '" . $this->level->getName() . "' WHERE ID = " . $this->id . ";"); } diff --git a/src/world/generator/SuperflatGenerator.php b/src/world/generator/SuperflatGenerator.php index a36d4415..f6f32c0f 100644 --- a/src/world/generator/SuperflatGenerator.php +++ b/src/world/generator/SuperflatGenerator.php @@ -35,7 +35,7 @@ class SuperflatGenerator implements LevelGenerator{ $this->populators[] = new MineshaftPopulator(isset($this->options["mineshaft"]["chance"]) ? floatval($this->options["mineshaft"]["chance"]) : 0.01); }*/ } - + public function parsePreset($preset){ $this->preset = $preset; $preset = explode(";", $preset); @@ -61,7 +61,6 @@ class SuperflatGenerator implements LevelGenerator{ $this->structure[$y] = new AirBlock(); } - for($Y = 0; $Y < 8; ++$Y){ $this->chunks[$Y] = ""; $startY = $Y << 4; @@ -132,7 +131,7 @@ class SuperflatGenerator implements LevelGenerator{ $end = 128 + $spawn[0]; for($x = $start; $x <= $end; ++$x){ for($z = $start; $z <= $end; ++$z){ - if(floor(sqrt(($x - 128)*($x - 128)+ ($z - 128)*($z - 128))) <= $spawn[0]){ + if(floor(sqrt(($x - 128) * ($x - 128) + ($z - 128) * ($z - 128))) <= $spawn[0]){ $this->level->setBlockRaw(new Vector3($x, $this->floorLevel - 1, $z), $spawn[1], null); } } diff --git a/src/world/generator/VoidGenerator.php b/src/world/generator/VoidGenerator.php index afa04d02..d485d40b 100644 --- a/src/world/generator/VoidGenerator.php +++ b/src/world/generator/VoidGenerator.php @@ -2,11 +2,11 @@ /** * - * ____ _ _ __ __ _ __ __ ____ - * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \ + * ____ _ _ __ __ _ __ __ ____ + * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \ * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) | - * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/ - * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_| + * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/ + * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_| * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by @@ -15,7 +15,7 @@ * * @author PocketMine Team * @link http://www.pocketmine.net/ - * + * * */ @@ -24,37 +24,37 @@ require_once("LevelGenerator.php"); /***REM_END***/ class VoidGenerator implements LevelGenerator{ - private $level, $random, $structure, $chunks, $options, $floorLevel, $populators = array(); - - public function __construct(array $options = array()){ + private $level, $random, $structure, $chunks, $options, $floorLevel, $populators = []; + + public function __construct(array $options = []){ $this->options = $options; /*if(isset($this->options["mineshaft"])){ $this->populators[] = new MineshaftPopulator(isset($this->options["mineshaft"]["chance"]) ? floatval($this->options["mineshaft"]["chance"]) : 0.01); }*/ } - + public function init(Level $level, Random $random){ $this->level = $level; $this->random = $random; } - + public function generateChunk($chunkX, $chunkZ){ for($Y = 0; $Y < 8; ++$Y){ $this->level->setMiniChunk($chunkX, $chunkZ, $Y, $this->chunks[$Y]); } } - - public function populateChunk($chunkX, $chunkZ){ + + public function populateChunk($chunkX, $chunkZ){ foreach($this->populators as $populator){ $this->random->setSeed((int) ($chunkX * 0xdead + $chunkZ * 0xbeef) ^ $this->level->getSeed()); $populator->populate($this->level, $chunkX, $chunkZ, $this->random); } } - + public function populateLevel(){ $this->random->setSeed($this->level->getSeed()); } - + public function getSpawn(){ return new Vector3(128, $this->floorLevel, 128); } diff --git a/src/world/generator/WorldGenerator.php b/src/world/generator/WorldGenerator.php index 18c0d1d7..c5855ebe 100644 --- a/src/world/generator/WorldGenerator.php +++ b/src/world/generator/WorldGenerator.php @@ -35,7 +35,7 @@ class WorldGenerator{ for($X = 0; $X < $this->width; ++$X){ $this->generator->generateChunk($X, $Z); } - + console("[NOTICE] Generating level " . ceil((($Z + 1) / $this->width) * 100) . "%"); }//$this->generator->generateChunk(7, 5); console("[NOTICE] Populating level"); diff --git a/src/world/generator/noise/NoiseGenerator.php b/src/world/generator/noise/NoiseGenerator.php index 227a6693..c23f0494 100644 --- a/src/world/generator/noise/NoiseGenerator.php +++ b/src/world/generator/noise/NoiseGenerator.php @@ -1,5 +1,5 @@ generatorCollection = array(); + public function __construct(MTRandom $random, $octaves) { + $this->generatorCollection = []; $this->octaves = (int) $octaves; - for($o = 0; $o < $this->octaves; ++$o){ + for($o = 0; $o < $this->octaves; ++$o) { $this->generatorCollection[$o] = new NoiseGeneratorPerlin($random); } } - - public function getValue($x, $y){ + + public function getValue($x, $y) { $noise = 0; $scale = 1; - for($i = 0; $i < $this->octaves; ++$i){ + for($i = 0; $i < $this->octaves; ++$i) { $noise += $this->generatorCollection[$i]->getValue($x * $scale, $y * $scale) / $scale; $scale /= 2; } return $noise; } - - public function generateNoiseOctaves($int1, $int2, $int3, $int4, $int5, $int6, $par1 = false, $par2 = false, $par3 = false){ - if($par1 === false or $par2 === false or $par3 === false){ + + public function generateNoiseOctaves($int1, $int2, $int3, $int4, $int5, $int6, $par1 = false, $par2 = false, $par3 = false) { + if($par1 === false or $par2 === false or $par3 === false) { return $this->generateNoiseOctaves($int1, 10, $int2, $int3, 1, $int4, $int5, 1, $int6); } - + $floats = array_fill(0, $int4 * $int5 * $int6, 0); $d1 = 1; - - for($j = 0; $j < $this->octaves; ++$j){ + + for($j = 0; $j < $this->octaves; ++$j) { $d2 = $int1 * $d1 * $par1; $d3 = $int2 * $d1 * $par2; $d4 = $int3 * $d1 * $par3; @@ -43,7 +43,7 @@ class NoiseGeneratorOctaves extends NoiseGenerator{ $d4 -= $l2; $l1 %= 16777216; $l2 %= 16777216; - + $d2 += $l1; $d4 += $l2; $this->generatorCollection[$j]->populateNoiseArray($floats, $d2, $d3, $d4, $int4, $int5, $int6, $par1 * $d1, $par2 * $d1, $par3 * $d1, $d1); @@ -51,4 +51,4 @@ class NoiseGeneratorOctaves extends NoiseGenerator{ } return $floats; } -} \ No newline at end of file +} diff --git a/src/world/generator/noise/NoiseGeneratorPerlin.php b/src/world/generator/noise/NoiseGeneratorPerlin.php index edad5c3f..85d0435c 100644 --- a/src/world/generator/noise/NoiseGeneratorPerlin.php +++ b/src/world/generator/noise/NoiseGeneratorPerlin.php @@ -4,21 +4,23 @@ require_once("NoiseGenerator.php"); /***REM_END***/ -class NoiseGeneratorPerlin extends NoiseGenerator{ +class NoiseGeneratorPerlin extends NoiseGenerator { - public $xCoord, $yCoord, $zCoord; + public $xCoord; + public $yCoord; + public $zCoord; private $permutations = []; - public function __construct(MTRandom $random){ + public function __construct(MTRandom $random) { $this->xCoord = $random->nextFloat() * 256; $this->yCoord = $random->nextFloat() * 256; $this->zCoord = $random->nextFloat() * 256; - for($i = 0; $i < 512; ++$i){ + for($i = 0; $i < 512; ++$i) { $this->permutations[$i] = $i < 256 ? $i : 0; } - for($i = 0; $i < 256; ++$i){ + for($i = 0; $i < 256; ++$i) { $j = $random->nextInt(256 - $i) + $i; $k = $this->permutations[$i]; $this->permutations[$i] = $this->permutations[$j]; @@ -27,8 +29,8 @@ class NoiseGeneratorPerlin extends NoiseGenerator{ } } - - public function getValue($d, $d1, $d2 = 0){ + + public function getValue($d, $d1, $d2 = 0) { $d3 = $d + $this->xCoord; $d4 = $d1 + $this->yCoord; $d5 = $d2 + $this->zCoord; @@ -44,14 +46,14 @@ class NoiseGeneratorPerlin extends NoiseGenerator{ $d6 = $d3 * $d3 * $d3 * ($d3 * ($d3 * 6 - 15) + 10); $d7 = $d4 * $d4 * $d4 * ($d4 * ($d4 * 6 - 15) + 10); $d8 = $d5 * $d5 * $d5 * ($d5 * ($d5 * 6 - 15) + 10); - + $k1 = (int) ($this->permutations[$l] + $i1); $l1 = (int) ($this->permutations[$k1] + $j1); $i2 = (int) ($this->permutations[$k1 + 1] + $j1); $j2 = (int) ($this->permutations[$l + 1] + $i1); $k2 = (int) ($this->permutations[$j2] + $j1); $l2 = (int) ($this->permutations[$j2 + 1] + $j1); - + return self::curve($d8, self::curve($d7, self::curve($d6, self::grad3D($this->permutations[$l1], $d3, $d4, $d5), self::grad3D($this->permutations[$k2], $d3 - 1, $d4, $d5)), @@ -63,19 +65,19 @@ class NoiseGeneratorPerlin extends NoiseGenerator{ ) ); } - - public function populateNoiseArray(&$floats, $par1, $par2, $par3, $int1, $int2, $int3, $par4, $par5, $par6, $par7){ - if($int2 === 1){ + + public function populateNoiseArray(&$floats, $par1, $par2, $par3, $int1, $int2, $int3, $par4, $par5, $par6, $par7) { + if($int2 === 1) { $n = 0; $d3 = 1 / $par7; - for($i1 = 0; $i1 < $int1; ++$i1){ + for($i1 = 0; $i1 < $int1; ++$i1) { $d4 = $par1 + $i1 * $par4 + $this->xCoord; $i2 = floor($d4); $i3 = $i2 & 0xFF; $d4 -= $i2; $d5 = $d4 * $d4 * $d4 * ($d4 * ($d4 * 6 - 15) + 10); - for($i4 = 0; $i4 < $int3; ++$i4){ + for($i4 = 0; $i4 < $int3; ++$i4) { $d6 = $par3 + $i4 * $par6 + $this->zCoord; $i5 = floor($d6); $i6 = $i5 & 0xFF; @@ -101,28 +103,28 @@ class NoiseGeneratorPerlin extends NoiseGenerator{ $n = 0; $i = 0; - for($i4 = 0; $i4 < $int1; ++$i4){ + for($i4 = 0; $i4 < $int1; ++$i4) { $d6 = $par1 + $i4 * $par4 + $this->xCoord; $i5 = floor($d6); $i6 = $i5 & 0xFF; $d6 -= $i5; $d7 = $d6 * $d6 * $d6 * ($d6 * ($d6 * 6 - 15) + 10); - for($i12 = 0; $i12 < $int3; ++$i12){ + for($i12 = 0; $i12 < $int3; ++$i12) { $d12 = $par3 + $i12 * $par6 + $this->zCoord; $i13 = floor($d12); $i14 = $i13 & 0xFF; $d12 -= $i13; $d13 = $d12 * $d12 * $d12 * ($d12 * ($d12 * 6 - 15) + 10); - for($i15 = 0; $i15 < $int2; ++$i15){ + for($i15 = 0; $i15 < $int2; ++$i15) { $d14 = $par2 + $i15 * $par5 + $this->yCoord; $i16 = floor($d14); $i17 = $i16 & 0xff; $d14 -= $i16; $d15 = $d14 * $d14 * $d14 * ($d14 * ($d14 * 6 - 15) + 10); - if($i15 == 0 or $i17 != $m){ + if($i15 == 0 or $i17 != $m) { $m = $i17; $i7 = $this->permutations[$i6] + $i17; $i8 = $this->permutations[$i7] + $i14; @@ -145,22 +147,22 @@ class NoiseGeneratorPerlin extends NoiseGenerator{ } } - public static final function curve($par1, $par2, $par3){ + final public static function curve($par1, $par2, $par3) { return $par2 + $par1 * ($par3 - $par2); } - public static function grad2D($int, $par1, $par2){ + public static function grad2D($int, $par1, $par2) { $i = $int & 0x0F; $d1 = (1 - (($i & 0x08) >> 3)) * $par1; - $d2 = ($i == 12 || $i == 14) ? $par1 : (($i >= 4)*$par2); + $d2 = ($i == 12 || $i == 14) ? $par1 : (($i >= 4) * $par2); return (($i & 0x01) ? -$d1 : $d1) + (($i & 0x02) ? -$d2 : $d2); } - public static function grad3D($int, $par1, $par2, $par3){ + public static function grad3D($int, $par1, $par2, $par3) { $i = $int & 0x0F; $d1 = $i < 8 ? $par1 : $par2; $d2 = ($i == 12 or $i == 14) ? $par1 : ($i < 4 ? $par2 : $par3); return (($i & 0x01) ? -$d1 : $d1) + (($i & 0x02) ? -$d2 : $d2); } -} \ No newline at end of file +} diff --git a/src/world/generator/object/NetherReactorStructure.php b/src/world/generator/object/NetherReactorStructure.php index 30b0624d..78006ab9 100644 --- a/src/world/generator/object/NetherReactorStructure.php +++ b/src/world/generator/object/NetherReactorStructure.php @@ -1,7 +1,7 @@ [ @@ -591,7 +591,7 @@ class NetherReactorStructure{ " N ", ], ]; - + public static function buildReactor($level, $x, $y, $z){ /*use CENTER positions*/ $offsetX = 0; $offsetZ = 0; diff --git a/src/world/generator/object/tree/BigTreeObject.php b/src/world/generator/object/tree/BigTreeObject.php index 4616ac26..d2edd493 100644 --- a/src/world/generator/object/tree/BigTreeObject.php +++ b/src/world/generator/object/tree/BigTreeObject.php @@ -61,5 +61,4 @@ class BigTreeObject extends TreeObject{ */ } - } \ No newline at end of file diff --git a/src/world/generator/object/tree/PineTreeObject.php b/src/world/generator/object/tree/PineTreeObject.php index 91642259..e745c0a4 100644 --- a/src/world/generator/object/tree/PineTreeObject.php +++ b/src/world/generator/object/tree/PineTreeObject.php @@ -69,5 +69,4 @@ class PineTreeObject extends TreeObject{ } } - } \ No newline at end of file diff --git a/src/world/generator/object/tree/SpruceTreeObject.php b/src/world/generator/object/tree/SpruceTreeObject.php index b496454b..70dacdeb 100644 --- a/src/world/generator/object/tree/SpruceTreeObject.php +++ b/src/world/generator/object/tree/SpruceTreeObject.php @@ -60,5 +60,4 @@ class SpruceTreeObject extends TreeObject{ } } - } \ No newline at end of file diff --git a/src/world/generator/vanilla/BiomeSource.php b/src/world/generator/vanilla/BiomeSource.php index 821d7f00..ad7569e4 100644 --- a/src/world/generator/vanilla/BiomeSource.php +++ b/src/world/generator/vanilla/BiomeSource.php @@ -9,7 +9,7 @@ class BiomeSource */ public $temperatureNoise, $rainfallNoise, $detailNoise; /** - * + * * @var float[] $tempNoises * @var float[] $rainfallNoises * @var float[] $detailNoises @@ -20,61 +20,60 @@ class BiomeSource $this->rainfallNoise = new NoiseGeneratorOctaves(new MTRandom($level->getSeed() * 39811), 4); $this->detailNoise = new NoiseGeneratorOctaves(new MTRandom($level->getSeed() * 543321), 2); } - + public function getBiome($x, $z){ return $this->getBiomeBlock($x, $z, 1, 1)[0]; } - + public function getTemperatureBlock($x, $z, $xSize, $zSize){ $this->temperatureNoises = $this->temperatureNoise->generateNoiseOctaves($x, $z, $xSize, $zSize, 0.025, 0.025, 0.25); $this->detailNoises = $this->detailNoise->generateNoiseOctaves($x, $z, $xSize, $zSize, 0.25, 0.25, 0.588); - + $index = 0; - + for($blockX = 0; $blockX < $xSize; ++$blockX){ for($blockZ = 0; $blockZ < $zSize; ++$blockZ){ //float f = 1.0f - ((((this.detailNoises[index] * 1.1f) + 0.5f) * 0.01f) + (((this.temperatureNoises[index] * 0.15f) + 0.7f) * 0.99f)); //float f1 = 1.0f - (f*f); $f = 1 - (((($this->detailNoises[$index] * 1.1) + 0.5) * 0.01) + ((($this->temperatureNoises[$index] * 0.15) + 0.7) * 0.99)); - $f1 = 1 - ($f*$f); + $f1 = 1 - ($f * $f); if($f1 < 0) $f1 = 0; elseif($f1 > 1) $f1 = 1; - + $this->temperatureNoises[$index++] = $f1; } } - + return $this->temperatureNoises; } - - + public function getBiomeBlock($x, $z, $xSize, $zSize){ $this->temperatureNoises = $this->temperatureNoise->generateNoiseOctaves($x, $z, $xSize, $zSize, 0.025, 0.025, 0.25); $this->rainfallNoises = $this->rainfallNoise->generateNoiseOctaves($x, $z, $xSize, $zSize, 0.05, 0.05, 0.3333); $this->detailNoises = $this->detailNoise->generateNoiseOctaves($x, $z, $xSize, $zSize, 0.25, 0.25, 0.588); - + $localBiomeArray = []; $index = 0; - + for($blockX = 0; $blockX < $xSize; ++$blockX){ for($blockZ = 0; $blockZ < $zSize; ++$blockZ){ $rain = ((($this->detailNoises[$index] * 1.1) + 0.5) * 0.002) + ((($this->rainfallNoises[$index] * 0.15) + 0.5) * 0.998); $f1 = 1 - ((((($this->detailNoises[$index]) * 1.1) + 0.5) * 0.01) + ((($this->temperatureNoises[$index] * 0.15) + 0.7) * 0.99)); $blockTemp = 1 - ($f1 * $f1); - + if($blockTemp < 0) $blockTemp = 0; elseif($blockTemp > 1) $blockTemp = 1; - + if($rain < 0) $rain = 0; elseif($rain > 1) $rain = 1; - + $this->temperatureNoises[$index] = $blockTemp; $this->rainfallNoises[$index] = $rain; $localBiomeArray[$index++] = Biome::getBiome($blockTemp, $rain); } } - + return $localBiomeArray; } } diff --git a/src/world/generator/vanilla/VanillaGenerator.php b/src/world/generator/vanilla/VanillaGenerator.php index 9ec18d45..4d18f2d6 100644 --- a/src/world/generator/vanilla/VanillaGenerator.php +++ b/src/world/generator/vanilla/VanillaGenerator.php @@ -1,8 +1,8 @@ level = $level; $this->biomeSource = new BiomeSource($level); $this->rand = new MTRandom($level->getSeed()); @@ -62,22 +76,20 @@ class VanillaGenerator implements LevelGenerator $this->treeNoise = new NoiseGeneratorOctaves($this->rand, 8); } - public function getSpawn() - { + public function getSpawn() { return new Vector3(128, 64, 128); } - public function populateChunk($chunkX, $chunkZ) - { + public function populateChunk($chunkX, $chunkZ) { $chunkXWorld = $chunkX * 16; $chunkZWorld = $chunkZ * 16; //HeavyTile::instaFall = 1; TODO instant sand/gravel fall $biome = $this->biomeSource->getBiome($chunkXWorld + 16, $chunkZWorld + 16); $this->rand->setSeed($this->level->getSeed()); - $i1 = (int)(((int)($this->rand->nextInt() / 2)) * 2 + 1); //why php integers are so big....??????????????????????????????????????? - $j1 = (int)(((int)($this->rand->nextInt() / 2)) * 2 + 1); + $i1 = (int) (((int) ($this->rand->nextInt() / 2)) * 2 + 1); //why php integers are so big....??????????????????????????????????????? + $j1 = (int) (((int) ($this->rand->nextInt() / 2)) * 2 + 1); $this->rand->setSeed((($chunkX * $i1 + $chunkZ * $j1) & 0xffffffff) ^ $this->level->getSeed()); - for($i2 = 0; $i2 < 10; ++$i2){ + for($i2 = 0; $i2 < 10; ++$i2) { ClayFeature::place($this->level, $this->rand, $chunkXWorld + $this->rand->nextInt(16), $this->rand->nextInt(128), $chunkZWorld + $this->rand->nextInt(16)); } for ($i3 = 0; $i3 < 20; ++$i3) { @@ -89,30 +101,37 @@ class VanillaGenerator implements LevelGenerator for ($i5 = 0; $i5 < 20; ++$i5) { OreFeature::place($this->level, $this->rand, $chunkXWorld + $this->rand->nextInt(16), $this->rand->nextInt(128), $chunkZWorld + $this->rand->nextInt(16), COAL_ORE, 16); } - for($i6 = 0; $i6 < 20; ++$i6){ + for($i6 = 0; $i6 < 20; ++$i6) { OreFeature::place($this->level, $this->rand, $chunkXWorld + $this->rand->nextInt(16), $this->rand->nextInt(64), $chunkZWorld + $this->rand->nextInt(16), IRON_ORE, 8); } - for($i7 = 0; $i7 < 2; ++$i7){ + for($i7 = 0; $i7 < 2; ++$i7) { OreFeature::place($this->level, $this->rand, $chunkXWorld + $this->rand->nextInt(16), $this->rand->nextInt(32), $chunkZWorld + $this->rand->nextInt(16), GOLD_ORE, 8); } - for($i8 = 0; $i8 < 8; ++$i8){ - OreFeature::place($this->level, $this->rand, $chunkXWorld + $this->rand->nextInt(16), $this->rand->nextInt(16), $chunkZWorld + $this->rand->nextInt(16), REDSTONE_ORE, 7); + for($i8 = 0; $i8 < 8; ++$i8) { + OreFeature::place($this->level, $this->rand, $chunkXWorld + $this->rand->nextInt(16), $this->rand->nextInt(16), $chunkZWorld + $this->rand->nextInt(16), REDSTONE_ORE, 7); } - - OreFeature::place($this->level, $this->rand, $chunkXWorld + $this->rand->nextInt(16), $this->rand->nextInt(16), $chunkZWorld + $this->rand->nextInt(16), DIAMOND_ORE, 7); - OreFeature::place($this->level, $this->rand, $chunkXWorld + $this->rand->nextInt(16), $this->rand->nextInt(16) + $this->rand->nextInt(16), $chunkZWorld + $this->rand->nextInt(16), LAPIS_ORE, 6); - - + + OreFeature::place($this->level, $this->rand, $chunkXWorld + $this->rand->nextInt(16), $this->rand->nextInt(16), $chunkZWorld + $this->rand->nextInt(16), DIAMOND_ORE, 7); + OreFeature::place($this->level, $this->rand, $chunkXWorld + $this->rand->nextInt(16), $this->rand->nextInt(16) + $this->rand->nextInt(16), $chunkZWorld + $this->rand->nextInt(16), LAPIS_ORE, 6); + $sample = (int) (((($this->treeNoise->getValue($chunkXWorld * 0.5, $chunkZWorld * 0.5) / 8) + ($this->rand->nextFloat() * 4)) + 4) / 3); $treesAmount = $this->rand->nextInt(10) == 0; - if($biome == Biome::$forest) $treesAmount += $sample + 2; - elseif($biome == Biome::$rainForest) $treesAmount += $sample + 2; - elseif($biome == Biome::$seasonalForest) $treesAmount += $sample + 1; - elseif($biome == Biome::$taiga) $treesAmount += $sample + 1; - elseif($biome == Biome::$desert) $treesAmount -= 20; - elseif($biome == Biome::$tundra) $treesAmount -= 20; - elseif($biome == Biome::$plains) $treesAmount -= 20; - for($l8 = 0; $l8 < $treesAmount; ++$l8){ + if($biome == Biome::$forest) { + $treesAmount += $sample + 2; + } elseif($biome == Biome::$rainForest) { + $treesAmount += $sample + 2; + } elseif($biome == Biome::$seasonalForest) { + $treesAmount += $sample + 1; + } elseif($biome == Biome::$taiga) { + $treesAmount += $sample + 1; + } elseif($biome == Biome::$desert) { + $treesAmount -= 20; + } elseif($biome == Biome::$tundra) { + $treesAmount -= 20; + } elseif($biome == Biome::$plains) { + $treesAmount -= 20; + } + for($l8 = 0; $l8 < $treesAmount; ++$l8) { $l12 = $chunkXWorld + $this->rand->nextInt(16) + 8; $j15 = $chunkZWorld + $this->rand->nextInt(16) + 8; /** @@ -121,76 +140,79 @@ class VanillaGenerator implements LevelGenerator $tree = $biome->getTreeFeature($this->rand); $tree->place($this->level, $this->rand, $l12, $this->getHeightValue($l12, $j15), $j15); } - for($i9 = 0; $i9 < 2; ++$i9){ + for($i9 = 0; $i9 < 2; ++$i9) { $i13 = $chunkXWorld + $this->rand->nextInt(16) + 8; $k15 = $this->rand->nextInt(128); $l17 = $chunkZWorld + $this->rand->nextInt(16) + 8; Feature::$FLOWER_YELLOW->place($this->level, $this->rand, $i13, $k15, $l17); } - if($this->rand->nextInt(2) == 0){ + if($this->rand->nextInt(2) == 0) { $j9 = $chunkXWorld + $this->rand->nextInt(16) + 8; $j13 = $this->rand->nextInt(128); $l15 = $chunkZWorld + $this->rand->nextInt(16) + 8; Feature::$FLOWER_RED->place($this->level, $this->rand, $j9, $j13, $l15); } - if($this->rand->nextInt(4) == 0){ + if($this->rand->nextInt(4) == 0) { $j9 = $chunkXWorld + $this->rand->nextInt(16) + 8; $j13 = $this->rand->nextInt(128); $l15 = $chunkZWorld + $this->rand->nextInt(16) + 8; Feature::$MUSHROOM_BROWN->place($this->level, $this->rand, $j9, $j13, $l15); } - if($this->rand->nextInt(8) == 0){ + if($this->rand->nextInt(8) == 0) { $j9 = $chunkXWorld + $this->rand->nextInt(16) + 8; $j13 = $this->rand->nextInt(128); $l15 = $chunkZWorld + $this->rand->nextInt(16) + 8; Feature::$MUSHROOM_RED->place($this->level, $this->rand, $j9, $j13, $l15); } - + } - public function generateChunk($chunkX, $chunkZ) - { + public function generateChunk($chunkX, $chunkZ) { $this->rand->setSeed(341872712 * $chunkX + 132899541 * $chunkZ); $this->biomes = $this->biomeSource->getBiomeBlock($chunkX * 16, $chunkZ * 16, 16, 16); $chunkz = array_fill(0, 8, EMPTY_MINI_CHUNK); //[$blockY >> 4][($blockY & 0xf) + ($blockX << 5) + ($blockZ << 9)]. y&f+x&f+z&f $this->prepareHeights($chunkX, $chunkZ, $chunkz, $this->biomes, $this->biomeSource->temperatureNoises); $this->buildSurfaces($chunkX, $chunkZ, $chunkz, $this->biomes); $this->generateHeightmap($chunkX, $chunkZ, $chunkz); - - for($Y = 0; $Y < 8; ++$Y){ + + for($Y = 0; $Y < 8; ++$Y) { $index = ($chunkZ << 4) + $chunkX; $this->level->level->chunks[$index][$Y] = $chunkz[$Y]; $this->level->level->chunkChange[$index][$Y] = 8192; $this->level->level->locationTable[$index][0] |= 1 << $Y; //TODO mv out of loop } $this->level->level->chunkChange[$index][-1] = true; - + } - - public function populateLevel() - {} - public function setHeightValue($x, $z, $hv){ - if($x > 255 || $x < 0 || $z > 255 || $z < 0) return; + + public function populateLevel() { + } + public function setHeightValue($x, $z, $hv) { + if($x > 255 || $x < 0 || $z > 255 || $z < 0) { + return; + } $cX = $x >> 4; $cZ = $z >> 4; $bX = $x & 0xf; $bZ = $z & 0xf; $this->heightMap[$cX + ($cZ * 16)][$bX + ($bZ * 16)] = $hv; } - public function getHeightValue($x, $z){ - if($x > 255 || $x < 0 || $z > 255 || $z < 0) return 0; + public function getHeightValue($x, $z) { + if($x > 255 || $x < 0 || $z > 255 || $z < 0) { + return 0; + } $cX = $x >> 4; $cZ = $z >> 4; $bX = $x & 0xf; $bZ = $z & 0xf; return $this->heightMap[$cX + ($cZ * 16)][$bX + ($bZ * 16)]; } - public function generateHeightmap($x, $z, &$chunkz){ + public function generateHeightmap($x, $z, &$chunkz) { $heightmapCPtr = EMPTY_16x16_ARR; - for($blockX = 0; $blockX < 16; ++$blockX){ - for($blockZ = 0; $blockZ < 16; ++$blockZ){ - for($Y = 7; $Y >= 0; --$Y){ - for($cY = 15; $cY >= 0; --$cY){ - $blockY = $Y*16 + $cY; + for($blockX = 0; $blockX < 16; ++$blockX) { + for($blockZ = 0; $blockZ < 16; ++$blockZ) { + for($Y = 7; $Y >= 0; --$Y) { + for($cY = 15; $cY >= 0; --$cY) { + $blockY = $Y * 16 + $cY; $blockID = ord($chunkz[$Y][$cY + ($blockX << 5) + ($blockZ << 9)]); if($blockID > 0) { $heightmapCPtr[$blockX + ($blockZ * 16)] = $blockY + 1 ; @@ -202,7 +224,7 @@ class VanillaGenerator implements LevelGenerator } $this->heightMap[$x + ($z * 16)] = $heightmapCPtr; } - + /**Vanilla Functions Implementation starts here**/ /** * Decorate terrain with grass, water & other stuff. Uses ZXY placement format @@ -211,53 +233,53 @@ class VanillaGenerator implements LevelGenerator * @param array $chunks * @param Biome[] $biomes */ - public function buildSurfaces($chunkX, $chunkZ, &$chunks, $biomes){ + public function buildSurfaces($chunkX, $chunkZ, &$chunks, $biomes) { $this->sandNoises = $this->beachNoise->generateNoiseOctaves($chunkX * 16, $chunkZ * 16, 0, 16, 16, 1, 0.03125, 0.03125, 1); $this->gravelNoises = $this->beachNoise->generateNoiseOctaves($chunkX * 16, 109.01, $chunkZ * 16, 16, 1, 16, 0.03125, 1, 0.03125); $this->surfaceDepthNoises = $this->surfaceDepthNoise->generateNoiseOctaves($chunkX * 16, $chunkZ * 16, 0, 16, 16, 1, 0.0625, 0.0625, 0.0625); - for($blockX = 0; $blockX < 16; ++$blockX){ - for($blockZ = 0; $blockZ < 16; ++$blockZ){ + for($blockX = 0; $blockX < 16; ++$blockX) { + for($blockZ = 0; $blockZ < 16; ++$blockZ) { /** @var Biome $biome **/ $biome = $biomes[$blockX + ($blockZ * 16)]; $z = ($this->sandNoises[$blockX + ($blockZ * 16)] + ($this->rand->nextFloat() * 0.2)) > 0; $z2 = ($this->gravelNoises[$blockX + ($blockZ * 16)] + ($this->rand->nextFloat() * 0.2)) > 3; - $nextFloat = (int)(($this->surfaceDepthNoises[$blockX + ($blockZ * 16)] / 3) + 3 + ($this->rand->nextFloat() * 0.25)); + $nextFloat = (int) (($this->surfaceDepthNoises[$blockX + ($blockZ * 16)] / 3) + 3 + ($this->rand->nextFloat() * 0.25)); $i = -1; $b = $biome->topBlock; $b2 = $biome->fillerBlock; - for($blockY = 127; $blockY >= 0; --$blockY){ - if($blockY <= $this->rand->nextInt(5)){ + for($blockY = 127; $blockY >= 0; --$blockY) { + if($blockY <= $this->rand->nextInt(5)) { $chunks[$blockY >> 4][($blockY & 0xf) + ($blockZ << 5) + ($blockX << 9)] = "\x07"; - }else{ + } else { $b3 = ord($chunks[$blockY >> 4][($blockY & 0xf) + ($blockZ << 5) + ($blockX << 9)]); - if($b3 == 0){ + if($b3 == 0) { $i = -1; - }elseif($b3 == STONE){ - if($i == -1){ - if($nextFloat > 0){ - if($blockY >= 60 && $blockY <= 65){ - if($z){ + } elseif($b3 == STONE) { + if($i == -1) { + if($nextFloat > 0) { + if($blockY >= 60 && $blockY <= 65) { + if($z) { $b2 = $b = SAND; - }elseif($z2){ + } elseif($z2) { $b = 0; $b2 = GRAVEL; - }else{ + } else { $b = $biome->topBlock; $b2 = $biome->fillerBlock; } } - }else{ + } else { $b = 0; $b2 = STONE; } - + $b = $blockY < 64 && $b == 0 ? STILL_WATER : $b; $i = $nextFloat; $chunks[$blockY >> 4][($blockY & 0xf) + ($blockZ << 5) + ($blockX << 9)] = $blockY >= 63 ? chr($b) : chr($b2); - }elseif($i > 0){ + } elseif($i > 0) { --$i; $chunks[$blockY >> 4][($blockY & 0xf) + ($blockZ << 5) + ($blockX << 9)] = chr($b2); - if($i == 0 && $b2 == SAND){ + if($i == 0 && $b2 == SAND) { $i = $this->rand->nextInt(4); $b2 = SANDSTONE; } @@ -268,40 +290,39 @@ class VanillaGenerator implements LevelGenerator } } } - - - public function prepareHeights($chunkX, $chunkZ, &$chunks, $biomes, $temperatures){ + + public function prepareHeights($chunkX, $chunkZ, &$chunks, $biomes, $temperatures) { $this->heights = $this->getHeights($chunkX * 4, 0, $chunkZ * 4, 5, 17, 5); //if($chunkX == 15 && $chunkZ == 15) var_dump($this->heights); - for($unkX = 0; $unkX < 4; ++$unkX){ - for($unkZ = 0; $unkZ < 4; ++$unkZ){ - for($unkY = 0; $unkY < 16; ++$unkY){ + for($unkX = 0; $unkX < 4; ++$unkX) { + for($unkZ = 0; $unkZ < 4; ++$unkZ) { + for($unkY = 0; $unkY < 16; ++$unkY) { $f = $this->heights[(((($unkX) * 5) + $unkZ) * 17) + $unkY]; $f2 = $this->heights[(((($unkX) * 5) + $unkZ + 1) * 17) + $unkY]; $f3 = $this->heights[(((($unkX + 1) * 5) + $unkZ) * 17) + $unkY]; $f4 = $this->heights[(((($unkX + 1) * 5) + $unkZ + 1) * 17) + $unkY]; - + $f5 = ($this->heights[(((($unkX) * 5) + ($unkZ)) * 17) + ($unkY + 1)] - $f) * 0.125; $f6 = ($this->heights[(((($unkX) * 5) + ($unkZ + 1)) * 17) + ($unkY + 1)] - $f2) * 0.125; $f7 = ($this->heights[(((($unkX + 1) * 5) + ($unkZ)) * 17) + ($unkY + 1)] - $f3) * 0.125; $f8 = ($this->heights[(((($unkX + 1) * 5) + ($unkZ + 1)) * 17) + ($unkY + 1)] - $f4) * 0.125; - - for($unkYY = 0; $unkYY < 8; ++$unkYY){ + + for($unkYY = 0; $unkYY < 8; ++$unkYY) { $f9 = $f; $f10 = $f2; $f11 = ($f3 - $f) * 0.25; $f12 = ($f4 - $f2) * 0.25; - for($unkXX = 0; $unkXX < 4; ++$unkXX){ + for($unkXX = 0; $unkXX < 4; ++$unkXX) { $f13 = $f9; $f14 = ($f10 - $f9) * 0.25; - for($unkZZ = 0; $unkZZ < 4; ++$unkZZ){ + for($unkZZ = 0; $unkZZ < 4; ++$unkZZ) { $d15 = $temperatures[((($unkX * 4) + $unkXX) * 16) + ($unkZ * 4) + $unkZZ]; $i3 = $f13 > 0; //true -> STONE, false -> AIR - - if(!$i3 && ($unkY * 8) + $unkYY < 64){ + + if(!$i3 && ($unkY * 8) + $unkYY < 64) { $i3 = $d15 < 0.5 && (($unkY * 8) + $unkYY) >= 63 ? ICE : STILL_WATER; } - + $fx = ($unkXX + ($unkX * 4)); $fy = (($unkY * 8) + $unkYY); $fz = ($unkZZ + ($unkZ * 4)); @@ -320,9 +341,9 @@ class VanillaGenerator implements LevelGenerator } } } - - public function getHeights($chunkX, $chunkY, $chunkZ, $scaleX, $scaleY, $scaleZ){ - $heights = array_fill(0, $scaleX*$scaleY*$scaleZ, 0); + + public function getHeights($chunkX, $chunkY, $chunkZ, $scaleX, $scaleY, $scaleZ) { + $heights = array_fill(0, $scaleX * $scaleY * $scaleZ, 0); $rainNoises = $this->biomeSource->rainfallNoises; $tempNoises = $this->biomeSource->temperatureNoises; $this->biomeNoises = $this->biomeNoise->generateNoiseOctaves($chunkX, $chunkZ, $scaleX, $scaleZ, 1.121, 1.121, 0.5); @@ -331,56 +352,72 @@ class VanillaGenerator implements LevelGenerator $this->upperInterpolationNoises = $this->upperInterpolationNoise->generateNoiseOctaves($chunkX, $chunkY, $chunkZ, $scaleX, $scaleY, $scaleZ, 684.41, 684.41, 684.41); $this->lowerInterpolationNoises = $this->lowerInterpolationNoise->generateNoiseOctaves($chunkX, $chunkY, $chunkZ, $scaleX, $scaleY, $scaleZ, 684.41, 684.41, 684.41); $k1 = $l1 = 0; - $i2 = ((int)(16 / $scaleX)); - for($j2 = 0; $j2 < $scaleX; ++$j2){ - $k2 = (int)($j2 * $i2 + $i2 / 2); - for($l2 = 0; $l2 < $scaleZ; ++$l2){ - $i3 = (int)($l2 * $i2 + $i2 / 2); + $i2 = ((int) (16 / $scaleX)); + for($j2 = 0; $j2 < $scaleX; ++$j2) { + $k2 = (int) ($j2 * $i2 + $i2 / 2); + for($l2 = 0; $l2 < $scaleZ; ++$l2) { + $i3 = (int) ($l2 * $i2 + $i2 / 2); $d2 = $tempNoises[$k2 * 16 + $i3]; $d3 = $rainNoises[$k2 * 16 + $i3] * $d2; $d4 = 1 - $d3; $d4 *= $d4; $d4 *= $d4; $d4 = 1 - $d4; - + $d5 = (($this->biomeNoises[$l1] + 256) / 512); $d5 *= $d4; - if($d5 > 1) $d5 = 1; + if($d5 > 1) { + $d5 = 1; + } $d6 = $this->depthNoises[$l1] / 8000; - if($d6 < 0) $d6 = -$d6 * 0.3; - + if($d6 < 0) { + $d6 = -$d6 * 0.3; + } + $d6 = $d6 * 3 - 2; - if($d6 < 0){ + if($d6 < 0) { $d6 /= 2; - if($d6 < -1) $d6 = -1; + if($d6 < -1) { + $d6 = -1; + } $d6 /= 1.4; $d6 /= 2; $d5 = 0; - }else{ - if($d6 > 1) $d6 = 1; + } else { + if($d6 > 1) { + $d6 = 1; + } $d6 /= 8; } - - if($d5 < 0) $d5 = 0; - + + if($d5 < 0) { + $d5 = 0; + } + $d5 += 0.5; $d6 = ($d6 * $scaleY) / 16; $d7 = ($scaleY / 2 + $d6 * 4); ++$l1; - for($j3 = 0; $j3 < $scaleY; ++$j3){ + for($j3 = 0; $j3 < $scaleY; ++$j3) { $d8 = 0; - $d9 = (((float)$j3 - $d7) * 12) / $d5; - if($d9 < 0) $d9 *= 4; + $d9 = (((float) $j3 - $d7) * 12) / $d5; + if($d9 < 0) { + $d9 *= 4; + } $d10 = $this->upperInterpolationNoises[$k1] / 512; $d11 = $this->lowerInterpolationNoises[$k1] / 512; $d12 = ($this->interpolationNoises[$k1] / 10 + 1) / 2; - - if($d12 < 0) $d8 = $d10; - elseif($d12 > 1) $d8 = $d11; - else $d8 = $d10 + ($d11 - $d10) * $d12; - + + if($d12 < 0) { + $d8 = $d10; + } elseif($d12 > 1) { + $d8 = $d11; + } else { + $d8 = $d10 + ($d11 - $d10) * $d12; + } + $d8 -= $d9; - if($j3 > $scaleY - 4){ + if($j3 > $scaleY - 4) { $d13 = ($j3 - ($scaleY - 4)) / 3; $d8 = $d8 * (1 - $d13) + -10 * $d13; } @@ -389,9 +426,8 @@ class VanillaGenerator implements LevelGenerator } } } - + return $heights; } } - diff --git a/src/world/generator/vanilla/biome/Biome.php b/src/world/generator/vanilla/biome/Biome.php index a3f38ac2..5219d011 100644 --- a/src/world/generator/vanilla/biome/Biome.php +++ b/src/world/generator/vanilla/biome/Biome.php @@ -20,8 +20,7 @@ class Biome * @var Biome $tundra */ public static $rainForest, $swampLand, $seasonalForest, $forest, $savanna, $shrubland, $taiga, $desert, $plains, $iceDesert, $tundra; - - + public static function init(){ Biome::$rainForest = new Biome("Rainforest"); Biome::$swampLand = new Biome("Swampland"); @@ -48,26 +47,26 @@ class Biome public static function getBiome($temp, $rain){ $i = ((int) ($temp * 63)); $j = ((int) ($rain * 63)); - return Biome::$biomes[$i + $j*64]; + return Biome::$biomes[$i + $j * 64]; } public static function __getBiome($temp, $rain){ $rain *= $temp; if($temp < 0.1) return Biome::$tundra; - + if($rain < 0.2){ if($temp < 0.5) return Biome::$tundra; if($temp < 0.95) return Biome::$savanna; else return Biome::$desert; } - + if($rain > 0.5 && $temp < 0.7) return Biome::$swampLand; if($temp < 0.5) return Biome::$taiga; - + if($temp < 0.97){ if($rain < 0.45) return Biome::$shrubland; else return Biome::$forest; } - + if($rain < 0.45) return Biome::$plains; if($rain < 0.9) return Biome::$seasonalForest; return Biome::$rainForest; @@ -75,12 +74,12 @@ class Biome public function __construct($name){ $this->name = $name; } - + public function getTreeFeature(MTRandom $rand){ $rand->nextInt(); //it is necessary return Feature::$TREE; } - + public $name; public $topBlock = GRASS, $fillerBlock = DIRT; } diff --git a/src/world/generator/vanilla/feature/BirchFeature.php b/src/world/generator/vanilla/feature/BirchFeature.php index f98bdf9e..cc927f7f 100644 --- a/src/world/generator/vanilla/feature/BirchFeature.php +++ b/src/world/generator/vanilla/feature/BirchFeature.php @@ -13,7 +13,7 @@ class BirchFeature extends Feature if($i >= (($y + 1) + $nextInt) - 2){ $i2 = 2; } - + for($i3 = $x - $i2; $i3 <= $x + $i2 && $z2; ++$i3){ for($i4 = $z - $i2; $i4 <= $z + $i2 && $z2; ++$i4){ if($i >= 0 && $i < 128){ @@ -44,7 +44,7 @@ class BirchFeature extends Feature } } } - + for($i12 = 0; $i12 < $nextInt; ++$i12){ $blockID = $level->level->getBlockID($x, $y + $i12, $z); if($blockID == 0 || $blockID == LEAVES){ @@ -56,7 +56,7 @@ class BirchFeature extends Feature return false; } return false; - + } } diff --git a/src/world/generator/vanilla/feature/ClayFeature.php b/src/world/generator/vanilla/feature/ClayFeature.php index 32e51e0c..07c9506c 100644 --- a/src/world/generator/vanilla/feature/ClayFeature.php +++ b/src/world/generator/vanilla/feature/ClayFeature.php @@ -1,4 +1,5 @@ level->getBlockID($x, $y, $z); if($id != WATER && $id != STILL_WATER) return false; - + $nextFloat = $rand->nextFloat() * 3.1416; - $sin = (float)($x + 8 + ((sin($nextFloat) * $size) / 8)); - $sin2 = (float)(($x + 8) - ((sin($nextFloat) * $size) / 8)); - $cos = (float)($z + 8 + ((cos($nextFloat) * $size) / 8)); - $cos2 = (float)(($z + 8) - ((cos($nextFloat) * $size) / 8)); - + $sin = (float) ($x + 8 + ((sin($nextFloat) * $size) / 8)); + $sin2 = (float) (($x + 8) - ((sin($nextFloat) * $size) / 8)); + $cos = (float) ($z + 8 + ((cos($nextFloat) * $size) / 8)); + $cos2 = (float) (($z + 8) - ((cos($nextFloat) * $size) / 8)); + $nextInt = $y + $rand->nextInt(3) + 2; $nextInt2 = $y + $rand->nextInt(3) + 2; - + for($i = 0; $i <= $size; ++$i){ $d = $sin + ((($sin2 - $sin) * $i) / $size); $d2 = $nextInt + ((($nextInt2 - $nextInt) * $i) / $size); $d3 = $cos + ((($cos2 - $cos) * $i) / $size); - + $nextFloat = ($rand->nextFloat() * $size) / 16; $sin3 = (((sin(($i * 3.1416) / $size) + 1) * $nextFloat) + 1); $sin4 = (((sin(($i * 3.1416) / $size) + 1) * $nextFloat) + 1);//TODO optimize a bit? @@ -30,21 +31,21 @@ class ClayFeature $floor4 = floor($d2 + ($sin4 / 2.0)); $floor5 = floor($d3 - ($sin3 / 2.0)); $floor6 = floor($d3 + ($sin3 / 2.0)); - + for($i2 = $floor; $i2 <= $floor2; ++$i2){ for($i3 = $floor3; $i3 <= $floor4; ++$i3){ for($i4 = $floor5; $i4 <= $floor6; ++$i4){ $d4 = (($i2 + 0.5) - $d) / ($sin3 / 2); $d5 = (($i3 + 0.5) - $d2) / ($sin4 / 2); $d6 = (($i4 + 0.5) - $d3) / ($sin3 / 2); - if(($d4*$d4)+($d5*$d5)+($d6*$d6)<1 && $level->level->getBlockID($i2, $i3, $i4) == SAND){ + if(($d4 * $d4) + ($d5 * $d5) + ($d6 * $d6) < 1 && $level->level->getBlockID($i2, $i3, $i4) == SAND){ $level->level->setBlockID($i2, $i3, $i4, $clayid); } } } } } - + } } diff --git a/src/world/generator/vanilla/feature/Feature.php b/src/world/generator/vanilla/feature/Feature.php index f52426ad..d38ce7c8 100644 --- a/src/world/generator/vanilla/feature/Feature.php +++ b/src/world/generator/vanilla/feature/Feature.php @@ -5,7 +5,7 @@ abstract class Feature public static $TREE, $BIRCH_TREE, $PINE_TREE, $SPRUCE_TREE; public static $FLOWER_RED, $FLOWER_YELLOW, $MUSHROOM_BROWN, $MUSHROOM_RED; public static function init(){ - Feature::$TREE = new TreeFeature(); + Feature::$TREE = new TreeFeature(); Feature::$BIRCH_TREE = new BirchFeature(); Feature::$PINE_TREE = new PineFeature(); Feature::$SPRUCE_TREE = new SpruceFeature(); @@ -13,10 +13,10 @@ abstract class Feature Feature::$FLOWER_YELLOW = new FlowerFeature(DANDELION); Feature::$MUSHROOM_BROWN = new FlowerFeature(BROWN_MUSHROOM); Feature::$MUSHROOM_RED = new FlowerFeature(RED_MUSHROOM); - + } public function place(Level $level, MTRandom $rand, $x, $y, $z){ //TODO pass VanillaGenerator instance and update heightmap on ceratin block placement - + } } diff --git a/src/world/generator/vanilla/feature/FlowerFeature.php b/src/world/generator/vanilla/feature/FlowerFeature.php index 2bdacb8f..6b9e02bf 100644 --- a/src/world/generator/vanilla/feature/FlowerFeature.php +++ b/src/world/generator/vanilla/feature/FlowerFeature.php @@ -6,7 +6,7 @@ class FlowerFeature extends Feature public function __construct($id){ $this->id = $id; } - + public function place(Level $level, MTRandom $rand, $x, $y, $z){ //TODO pass VanillaGenerator instance and update heightmap on ceratin block placement for($i = 0; $i < 64; ++$i){ $xPos = ($x + $rand->nextInt(8)) - $rand->nextInt(8); diff --git a/src/world/generator/vanilla/feature/OreFeature.php b/src/world/generator/vanilla/feature/OreFeature.php index e4aa83d4..82f464f7 100644 --- a/src/world/generator/vanilla/feature/OreFeature.php +++ b/src/world/generator/vanilla/feature/OreFeature.php @@ -4,49 +4,49 @@ class OreFeature { public static function place(Level $level, MTRandom $rand, $x, $y, $z, $id, $amount){ $nextFloat = $rand->nextFloat() * 3.1415927; - + $sin = ($x + 8 + ((sin($nextFloat) * $amount) / 8)); $sin2 = (($x + 8) - ((sin($nextFloat) * $amount) / 8)); - + $cos = ($z + 8 + ((cos($nextFloat) * $amount) / 8)); $cos2 = (($z + 8) - ((cos($nextFloat) * $amount) / 8)); - + $nextInt = $y + $rand->nextInt(3) + 2; $nextInt2 = $y + $rand->nextInt(3) + 2; for($i = 0; $i <= $amount; ++$i){ $d = $sin + ((($sin2 - $sin) * $i) / $amount); - $d2 = $nextInt + ((($nextInt2 - $nextInt) * $i) / $amount); - $d3 = $cos + ((($cos2 - $cos) * $i) / $amount); - - $nextFloat = ($rand->nextFloat() * $amount) / 16; - $sin3 = (((sin(($i * 3.1415927) / $amount) + 1) * $nextFloat) + 1.0); - $sin4 = (((sin(($i * 3.1415927) / $amount) + 1) * $nextFloat) + 1.0); - - $floor = floor($d - ($sin3 / 2)); - $floor2 = floor($d2 - ($sin4 / 2)); - $floor3 = floor($d3 - ($sin3 / 2.0)); - $floor4 = floor($d + ($sin3 / 2.0)); - $floor5 = floor($d2 + ($sin4 / 2.0)); - $floor6 = floor($d3 + ($sin3 / 2.0)); - - for($i2 = $floor; $i2 <= $floor4; ++$i2){ - $d4 = (($i2 + 0.5) - $d) / ($sin3 / 2); - $d4sqrd = $d4*$d4; - if($d4sqrd < 1){ - for($i3 = $floor2; $i3 <= $floor5; ++$i3){ - $d5 = (($i3 + 0.5) - $d2) / ($sin4 / 2); - $d5sqrd = $d5*$d5; - if($d4sqrd+$d5sqrd < 1){ - for($i4 = $floor3; $i4 <= $floor6; ++$i4){ - $d6 = (($i4 + 0.5) - $d3) / ($sin3 / 2); - if($d4sqrd+$d5sqrd+($d6*$d6) < 1 && $level->level->getBlockID($i2, $i3, $i4) == STONE){ - $level->level->setBlockID($i2, $i3, $i4, $id); - } - } - } - } - } - } + $d2 = $nextInt + ((($nextInt2 - $nextInt) * $i) / $amount); + $d3 = $cos + ((($cos2 - $cos) * $i) / $amount); + + $nextFloat = ($rand->nextFloat() * $amount) / 16; + $sin3 = (((sin(($i * 3.1415927) / $amount) + 1) * $nextFloat) + 1.0); + $sin4 = (((sin(($i * 3.1415927) / $amount) + 1) * $nextFloat) + 1.0); + + $floor = floor($d - ($sin3 / 2)); + $floor2 = floor($d2 - ($sin4 / 2)); + $floor3 = floor($d3 - ($sin3 / 2.0)); + $floor4 = floor($d + ($sin3 / 2.0)); + $floor5 = floor($d2 + ($sin4 / 2.0)); + $floor6 = floor($d3 + ($sin3 / 2.0)); + + for($i2 = $floor; $i2 <= $floor4; ++$i2){ + $d4 = (($i2 + 0.5) - $d) / ($sin3 / 2); + $d4sqrd = $d4 * $d4; + if($d4sqrd < 1){ + for($i3 = $floor2; $i3 <= $floor5; ++$i3){ + $d5 = (($i3 + 0.5) - $d2) / ($sin4 / 2); + $d5sqrd = $d5 * $d5; + if($d4sqrd + $d5sqrd < 1){ + for($i4 = $floor3; $i4 <= $floor6; ++$i4){ + $d6 = (($i4 + 0.5) - $d3) / ($sin3 / 2); + if($d4sqrd + $d5sqrd + ($d6 * $d6) < 1 && $level->level->getBlockID($i2, $i3, $i4) == STONE){ + $level->level->setBlockID($i2, $i3, $i4, $id); + } + } + } + } + } + } } } } diff --git a/src/world/generator/vanilla/feature/PineFeature.php b/src/world/generator/vanilla/feature/PineFeature.php index 115473d9..38ff7c6d 100644 --- a/src/world/generator/vanilla/feature/PineFeature.php +++ b/src/world/generator/vanilla/feature/PineFeature.php @@ -32,7 +32,7 @@ class PineFeature extends Feature } } if(!$flag) return false; - + $i2 = $level->level->getBlockID($x, $y - 1, $z); if(($i2 != GRASS && $i2 != DIRT) || $y >= 128 - $l - 1) return false; $level->level->setBlockID($x, $y - 1, $z, DIRT); @@ -47,7 +47,7 @@ class PineFeature extends Feature } } } - + if($k2 >= 1 && $i3 == $y + $i1 + 1){ --$k2; continue; @@ -56,7 +56,7 @@ class PineFeature extends Feature ++$k2; } } - + for($j3 = 0; $j3 < $l - 1; ++$j3){ $i4 = $level->level->getBlockID($x, $y + $j3, $z); if($i4 == 0 || $i4 == LEAVES){ diff --git a/src/world/generator/vanilla/feature/SpruceFeature.php b/src/world/generator/vanilla/feature/SpruceFeature.php index c32c7d6f..6a05710c 100644 --- a/src/world/generator/vanilla/feature/SpruceFeature.php +++ b/src/world/generator/vanilla/feature/SpruceFeature.php @@ -1,4 +1,5 @@ 128){ return false; } - + for($i3 = $y; $i3 <= $y + 1 + $nextInt && $z2; ++$i3){ if($i3 - $y < $nextInt2){ $i = 0; @@ -30,7 +31,7 @@ class SpruceFeature extends Feature } } } - + if($z2){ $blockID = $level->level->getBlockID($x, $y - 1, $z); if(($blockID == GRASS || $blockID == DIRT) && $y < (128 - $nextInt) - 1){ @@ -49,7 +50,7 @@ class SpruceFeature extends Feature } } } - + if($nextInt4 >= $i6){ $nextInt4 = $i7; $i7 = 1; diff --git a/src/world/generator/vanilla/feature/TreeFeature.php b/src/world/generator/vanilla/feature/TreeFeature.php index 2b4f5a2a..b30b45ea 100644 --- a/src/world/generator/vanilla/feature/TreeFeature.php +++ b/src/world/generator/vanilla/feature/TreeFeature.php @@ -1,38 +1,42 @@ nextInt(3) + 4; $z2 = true; - if($y < 1 || $y + $nextInt + 1 > 128) return false; - for($i = $y; $i <= $y + 1 + $nextInt; ++$i){ + if($y < 1 || $y + $nextInt + 1 > 128) { + return false; + } + for($i = $y; $i <= $y + 1 + $nextInt; ++$i) { $i2 = ($i == $y) ? 0 : 1; - if($i >= (($y + 1) + $nextInt) - 2) $i2 = 2; - - for($i3 = $x - $i2; $i3 <= $x + $i2 && $z2; ++$i3){ - for($i4 = $z - $i2; $i4 <= $z + $i2 && $z2; ++$i4){ - if($i >= 0 && $i < 128){ + if($i >= (($y + 1) + $nextInt) - 2) { + $i2 = 2; + } + + for($i3 = $x - $i2; $i3 <= $x + $i2 && $z2; ++$i3) { + for($i4 = $z - $i2; $i4 <= $z + $i2 && $z2; ++$i4) { + if($i >= 0 && $i < 128) { $tileID = $level->level->getBlockID($i3, $i, $i4); - if($tileID != 0 && $tileID != LEAVES){ + if($tileID != 0 && $tileID != LEAVES) { return; } - }else{ + } else { return; } } } } - if($z2){ + if($z2) { $tileID = $level->level->getBlockID($x, $y - 1, $z); - if(($tileID == GRASS || $tileID == DIRT) && $y < (128 - $nextInt) - 1){ + if(($tileID == GRASS || $tileID == DIRT) && $y < (128 - $nextInt) - 1) { $level->level->setBlockID($x, $y - 1, $z, DIRT); } - for($i5 = ($y - 3) + $nextInt; $i5 <= $y + $nextInt; ++$i5){ + for($i5 = ($y - 3) + $nextInt; $i5 <= $y + $nextInt; ++$i5) { $i6 = $i5 - ($y + $nextInt); - $i7 = (int)(1 - ($i6 / 2)); - for($i8 = $x - $i7; $i8 <= $x + $i7; ++$i8){ + $i7 = (int) (1 - ($i6 / 2)); + for($i8 = $x - $i7; $i8 <= $x + $i7; ++$i8) { $i9 = $i8 - $x; - for($i10 = $z - $i7; $i10 <= $z + $i7; ++$i10){ + for($i10 = $z - $i7; $i10 <= $z + $i7; ++$i10) { $i11 = $i10 - $z; if((abs($i9) != $i7 || abs($i11) != $i7 || ($rand->nextInt(2) != 0 && $i6 != 0))/* && !Block.FULL_OPAQUE[world.getBlockIDAt(i8, i5, i10)]TODO opaque?*/) { $level->level->setBlockID($i8, $i5, $i10, LEAVES); @@ -40,13 +44,12 @@ class TreeFeature extends Feature } } } - for($i12 = 0; $i12 < $nextInt; ++$i12){ + for($i12 = 0; $i12 < $nextInt; ++$i12) { $tileID = $level->level->getBlockID($x, $y + $i12, $z); - if($tileID == 0 || $tileID == LEAVES){ + if($tileID == 0 || $tileID == LEAVES) { $level->level->setBlockID($x, $y + $i12, $z, WOOD); } } } } } - From 27554d29bd5184f60c4697a08093bc11ead2fbca Mon Sep 17 00:00:00 2001 From: Sunch <120295462+Sunch233@users.noreply.github.com> Date: Sun, 27 Apr 2025 20:37:30 +0800 Subject: [PATCH 29/39] CodeStyle: use Github CI --- .github/workflows/main.yml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 00000000..ff0eb9fb --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,27 @@ +name: CI + +on: + push: + pull_request: + workflow_dispatch: + +jobs: + codestyle: + name: Code Style checks + runs-on: ubuntu-latest + strategy: + fail-fast: false + + steps: + - uses: actions/checkout@v4 + + - name: Setup PHP and tools + uses: shivammathur/setup-php@2.31.1 + with: + php-version: 8.0 + tools: php-cs-fixer:3.60 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Run PHP-CS-Fixer + run: php-cs-fixer fix --dry-run --diff --ansi From 0049ac9b046365ed48842d9639f6d87c96827f23 Mon Sep 17 00:00:00 2001 From: Sunch <120295462+Sunch233@users.noreply.github.com> Date: Sun, 27 Apr 2025 20:49:19 +0800 Subject: [PATCH 30/39] better logic --- src/API/BlockAPI.php | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index 4c08f29c..208cd1a4 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -649,11 +649,18 @@ class BlockAPI{ * 方块/物品id转换,防止新版本影响旧版本视觉效果 */ public static function convertHighItemIdsToOldItemIds(int $protocolId, int $itemId) : int{ - if ($protocolId >= ProtocolInfo::CURRENT_PROTOCOL) { + if ($protocolId >= ProtocolInfo12::CURRENT_PROTOCOL_12) { return $itemId; } - $idMap = []; + $idMap = [ //default for protocol < 12 + LIT_PUMPKIN => MELON_BLOCK, //block + PUMPKIN_SEEDS => MELON_SEEDS, //item + PUMPKIN_PIE => BREAD, //item + BEETROOT => BREAD, //item + BEETROOT_SEEDS => SEEDS, //item + ]; + if ($protocolId < ProtocolInfo9::CURRENT_PROTOCOL_9) { $idMap += [ NETHERRACK => OBSIDIAN, //block @@ -661,15 +668,6 @@ class BlockAPI{ NETHER_QUARTZ => BRICK, //item ]; } - if ($protocolId < ProtocolInfo12::CURRENT_PROTOCOL_12) { - $idMap += [ - LIT_PUMPKIN => MELON_BLOCK, //block - PUMPKIN_SEEDS => MELON_SEEDS, //item - PUMPKIN_PIE => BREAD, //item - BEETROOT => BREAD, //item - BEETROOT_SEEDS => SEEDS, //item - ]; - } return $idMap[$itemId] ?? $itemId; } From a992bb614ca0d134fd2994c2c10f41f7715032a4 Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Mon, 28 Apr 2025 17:56:50 +0800 Subject: [PATCH 31/39] ContainerAckPacket implement --- src/network/PacketPool.php | 1 + src/network/protocol/ProtocolInfo.php | 3 +- .../protocol/packet/ContainerAckPacket.php | 46 +++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 src/network/protocol/packet/ContainerAckPacket.php diff --git a/src/network/PacketPool.php b/src/network/PacketPool.php index eb9753db..fa7ed5be 100644 --- a/src/network/PacketPool.php +++ b/src/network/PacketPool.php @@ -57,6 +57,7 @@ class PacketPool{ self::registerPacket(ContainerSetContentPacket::class); self::registerPacket(ContainerSetDataPacket::class); self::registerPacket(ContainerSetSlotPacket::class); + self::registerPacket(ContainerAckPacket::class); self::registerPacket(DisconnectPacket::class); self::registerPacket(DropItemPacket::class); diff --git a/src/network/protocol/ProtocolInfo.php b/src/network/protocol/ProtocolInfo.php index f45534c3..fd30aec1 100644 --- a/src/network/protocol/ProtocolInfo.php +++ b/src/network/protocol/ProtocolInfo.php @@ -73,7 +73,6 @@ abstract class ProtocolInfo{ } /*Unused: - * 0xb5 * 0x96 * 0x17 * 0x14 @@ -142,7 +141,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 = 0xb4; const CHAT_PACKET = 0xb5; const ADVENTURE_SETTINGS_PACKET = 0xb6; const ENTITY_DATA_PACKET = 0xb7; diff --git a/src/network/protocol/packet/ContainerAckPacket.php b/src/network/protocol/packet/ContainerAckPacket.php new file mode 100644 index 00000000..71db5313 --- /dev/null +++ b/src/network/protocol/packet/ContainerAckPacket.php @@ -0,0 +1,46 @@ +PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::CONTAINER_ACK_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::CONTAINER_ACK_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::CONTAINER_ACK_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + return ProtocolInfo9::CONTAINER_ACK_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::CONTAINER_ACK_PACKET; + } + return ProtocolInfo::CONTAINER_ACK_PACKET; + } + + public function decode(){ + $this->unknwonubyte1 = $this->getByte(); + $this->unknownshort = $this->getShort(); + $this->write1 = $this->get(); + $this->write0 = $this->get(); + ConsoleAPI::debug($this->unknwonubyte1); + ConsoleAPI::debug($this->unknownshort); + ConsoleAPI::debug($this->write1); + ConsoleAPI::debug($this->write0); + } + + public function encode(){ + $this->putByte($this->unknwonubyte1); + $this->putShort($this->unknownshort); + $this->put($this->write1); + $this->put($this->write0); + ConsoleAPI::debug($this->unknwonubyte1); + ConsoleAPI::debug($this->unknownshort); + ConsoleAPI::debug($this->write1); + ConsoleAPI::debug($this->write0); + } + +} \ No newline at end of file From 2b90f6499e8d0891b276e0266787f40a292dff90 Mon Sep 17 00:00:00 2001 From: Sunch <120295462+Sunch233@users.noreply.github.com> Date: Mon, 28 Apr 2025 19:49:29 +0800 Subject: [PATCH 32/39] more canonical codestyle --- .php-cs-fixer.php | 3 - src/API/AchievementAPI.php | 2 +- src/API/BlockAPI.php | 8 +- src/API/ConsoleAPI.php | 10 +- src/API/EntityAPI.php | 4 +- src/API/PluginAPI.php | 10 +- src/Player.php | 1076 ++++++++--------- src/PocketMinecraftServer.php | 348 +++--- src/installer/Installer.php | 90 +- src/material/Block.php | 2 +- src/material/Item.php | 2 +- src/material/block/solid/LeavesBlock.php | 2 +- src/plugin/DummyPlugin.php | 10 +- src/plugin/OtherPluginRequirement.php | 2 +- src/recipes/CraftingRecipes.php | 46 +- src/recipes/FuelData.php | 4 +- src/recipes/SmeltingData.php | 4 +- src/utils/RailLogic.php | 218 ++-- src/utils/Utils.php | 2 +- src/world/Level.php | 524 ++++---- src/world/LevelImport.php | 58 +- src/world/MobSpawner.php | 65 +- src/world/MovingObjectPosition.php | 12 +- src/world/PocketChunkParser.php | 86 +- src/world/Position.php | 20 +- src/world/Tile.php | 162 ++- .../generator/noise/NoiseGeneratorOctaves.php | 18 +- .../generator/noise/NoiseGeneratorPerlin.php | 38 +- .../generator/vanilla/VanillaGenerator.php | 211 ++-- .../generator/vanilla/feature/TreeFeature.php | 40 +- 30 files changed, 1388 insertions(+), 1689 deletions(-) diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index 307fb27e..6d0926a1 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -17,9 +17,6 @@ return (new PhpCsFixer\Config) 'cast_spaces' => [ 'space' => 'single' ], - 'concat_space' => [ - 'spacing' => 'one' - ], 'elseif' => true, 'indentation_type' => true, 'no_closing_tag' => true, diff --git a/src/API/AchievementAPI.php b/src/API/AchievementAPI.php index 526da14d..be89f3b7 100644 --- a/src/API/AchievementAPI.php +++ b/src/API/AchievementAPI.php @@ -184,7 +184,7 @@ class AchievementAPI{ if(count($achs) <= 0){ return "Player {$params[0]} unlocked 0 achievements"; } - $output = "Unlocked Achievements(" . count($achs) . "/" . count(self::$achievements) . "): "; + $output = "Unlocked Achievements(".count($achs)."/".count(self::$achievements)."): "; foreach($achs as $achievement => $unlocked){ if($unlocked && isset(self::$achievements[$achievement])){ $info = self::$achievements[$achievement]; diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index 42f2f34d..7368f121 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -235,7 +235,7 @@ class BlockAPI{ break; } $itemheld = $issuer->getSlot($issuer->slot); - $output = self::getItem($itemheld->getID(), $itemheld->getMetadata()) . ""; + $output = self::getItem($itemheld->getID(), $itemheld->getMetadata()).""; break; case "setblock": if(!($issuer instanceof Player)){ @@ -289,7 +289,7 @@ class BlockAPI{ break; }else{ $level->setBlock($pos, $block, true, false, true); - $output .= "Placed $block in " . implode(", ", $coords) . ", w:" . $level->getName(); + $output .= "Placed $block in ".implode(", ", $coords).", w:".$level->getName(); } break; case "give": @@ -311,7 +311,7 @@ class BlockAPI{ } $player->addItem($item->getID(), $item->getMetadata(), $item->count); - $output .= "Giving " . $item->count . " of " . $item->getName() . " (" . $item->getID() . ":" . $item->getMetadata() . ") to " . $player->username; + $output .= "Giving ".$item->count." of ".$item->getName()." (".$item->getID().":".$item->getMetadata().") to ".$player->username; break; }else{ $item = self::fromString($args[0] ?? ""); @@ -334,7 +334,7 @@ class BlockAPI{ } $issuer->addItem($item->getID(), $item->getMetadata(), $item->count); - $output .= "Giving " . $item->count . " of " . $item->getName() . " (" . $item->getID() . ":" . $item->getMetadata() . ") to " . $issuer->username; + $output .= "Giving ".$item->count." of ".$item->getName()." (".$item->getID().":".$item->getMetadata().") to ".$issuer->username; break; } } diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index 56f7624b..804486c4 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -272,19 +272,19 @@ class ConsoleAPI{ } public static function debug($msg){ - console("[DEBUG] " . $msg, true, true, 2); + console("[DEBUG] ".$msg, true, true, 2); } public static function info($msg){ - console("[INFO] " . $msg); + console("[INFO] ".$msg); } public static function notice($msg){ - console("[NOTICE] " . $msg); + console("[NOTICE] ".$msg); } public static function warn($msg){ - console("[WARNING] " . $msg); + console("[WARNING] ".$msg); } public static function error($msg){ - console("[ERROR] " . $msg); + console("[ERROR] ".$msg); } } diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index b9978dca..26d691a4 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -36,7 +36,7 @@ class EntityAPI{ $output = ""; switch($cmd){ case "entcnt": - return "Total amount of entities: " . count($this->entities); + return "Total amount of entities: ". count($this->entities); case "summon": case "spawnmob": if(!($issuer instanceof Player)){ @@ -81,7 +81,7 @@ class EntityAPI{ $this->summon($pos, ENTITY_MOB, $type, ["IsBaby" => $isBaby]); } - return "$amount " . ($isBaby ? "Baby " : "") . "$mobName(s) spawned in $x, $y, $z."; + return "$amount ".($isBaby ? "Baby " : "")."$mobName(s) spawned in $x, $y, $z."; } elseif(strtolower($args[1]) == "baby"){//summon [baby] $this->summon($pos, ENTITY_MOB, $type, ["IsBaby" => 1]); diff --git a/src/API/PluginAPI.php b/src/API/PluginAPI.php index 6596c783..facda55b 100644 --- a/src/API/PluginAPI.php +++ b/src/API/PluginAPI.php @@ -139,8 +139,8 @@ class PluginAPI extends stdClass{ if($ext2 === "phar"){ ++$pharCnt; $pluginInfo = []; //TODO: A PluginInfo class? - $filePath = $this->pluginsPath() . $file; - $p = new Phar($this->pluginsPath() . $file, 0); + $filePath = $this->pluginsPath().$file; + $p = new Phar($this->pluginsPath().$file, 0); foreach (new RecursiveIteratorIterator($p) as $file) { $name = $file->getFileName(); $content = file_get_contents($file->getPathName()); @@ -149,7 +149,7 @@ class PluginAPI extends stdClass{ break; } } - console("[INFO] Loading PHAR plugin \"" . FORMAT_GREEN . $pluginInfo["name"] . FORMAT_RESET . "\" " . FORMAT_AQUA . $pluginInfo["version"] . FORMAT_RESET . " by " . FORMAT_AQUA . $pluginInfo["author"] . FORMAT_RESET); + console("[INFO] Loading PHAR plugin \"".FORMAT_GREEN.$pluginInfo["name"].FORMAT_RESET."\" ".FORMAT_AQUA.$pluginInfo["version"].FORMAT_RESET." by ".FORMAT_AQUA.$pluginInfo["author"].FORMAT_RESET); $aver = CURRENT_API_VERSION; if(!in_array((string) CURRENT_API_VERSION, $pluginInfo["api"])){ @@ -159,13 +159,13 @@ class PluginAPI extends stdClass{ } $phr = "phar://$filePath/"; - include($phr . "/src/" . $pluginInfo["classLoader"]); + include($phr."/src/".$pluginInfo["classLoader"]); $class = $pluginInfo["CLClass"]; $loader = new $class(); $loader->loadAll($phr); $pluginName = PharUtils::getNameSpaceClass($pluginInfo["mainFile"]); - include($phr . "/src/" . $pluginInfo["mainFile"]); + include($phr."/src/".$pluginInfo["mainFile"]); $plugin = new $pluginName($this->server->api, false); if(!($plugin instanceof Plugin)){ console("[ERROR] Plugin \"" . $pluginInfo["name"] . "\" doesn't use the Plugin Interface"); diff --git a/src/Player.php b/src/Player.php index 136dbbac..f293319f 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1,10 +1,8 @@ bigCnt = 0; $this->MTU = min($MTU, 1492); $this->server = ServerAPI::request(); @@ -129,14 +126,14 @@ class Player { console("[DEBUG] New Session started with " . $ip . ":" . $port . ". MTU " . $this->MTU . ", Client ID " . $this->clientID, true, true, 2); } - public function __get($name) { - if(isset($this->{$name})) { + public function __get($name){ + if(isset($this->{$name})){ return ($this->{$name}); } return null; } - public function getSpawn() { + public function getSpawn(){ return $this->spawnPosition; } @@ -144,10 +141,10 @@ class Player { * * @return boolean */ - public function sleepOn(Vector3 $pos) { - foreach($this->server->api->player->getAll($this->level) as $p) { - if($p->isSleeping instanceof Vector3) { - if($pos->distance($p->isSleeping) <= 0.1) { + public function sleepOn(Vector3 $pos){ + foreach($this->server->api->player->getAll($this->level) as $p){ + if($p->isSleeping instanceof Vector3){ + if($pos->distance($p->isSleeping) <= 0.1){ return false; } } @@ -156,14 +153,13 @@ class Player { $this->sleepingTime = 0; $this->teleport(new Position($pos->x + 0.5, $pos->y + 1, $pos->z + 0.5, $this->level), false, false, false, false); //TODO change player hitbox size after he starts sleeping - if($this->entity instanceof Entity) { + if($this->entity instanceof Entity){ $this->entity->updateMetadata(); } $spawnPoint = BedBlock::findStandUpPosition($this->level, $pos->x, $pos->y, $pos->z); - if($spawnPoint == null) { - $spawnPoint = $pos->add(0.5, 1, 0.5); - } else { + if($spawnPoint == null) $spawnPoint = $pos->add(0.5, 1, 0.5); + else{ $spawnPoint->x += 0.5; $spawnPoint->z += 0.5; } @@ -171,7 +167,7 @@ class Player { return true; } - public function setSlotCount($cnt) { + public function setSlotCount($cnt){ $this->slotCount = $cnt; $this->data->set("slot-count", $this->slotCount); } @@ -184,29 +180,29 @@ class Player { * * @return boolean */ - public function teleport(Vector3 $pos, $yaw = false, $pitch = false, $terrain = true, $force = true) { - if($this->entity instanceof Entity and $this->level instanceof Level) { + public function teleport(Vector3 $pos, $yaw = false, $pitch = false, $terrain = true, $force = true){ + if($this->entity instanceof Entity and $this->level instanceof Level){ $this->entity->check = false; - if($yaw === false) { + if($yaw === false){ $yaw = $this->entity->yaw; } - if($pitch === false) { + if($pitch === false){ $pitch = $this->entity->pitch; } - if($this->server->api->dhandle("player.teleport", ["player" => $this, "target" => $pos]) === false) { + if($this->server->api->dhandle("player.teleport", ["player" => $this, "target" => $pos]) === false){ $this->entity->check = true; return false; } - if($pos instanceof Position and $pos->level instanceof Level and $pos->level !== $this->level) { - if($this->server->api->dhandle("player.teleport.level", ["player" => $this, "origin" => $this->level, "target" => $pos->level]) === false) { + if($pos instanceof Position and $pos->level instanceof Level and $pos->level !== $this->level){ + if($this->server->api->dhandle("player.teleport.level", ["player" => $this, "origin" => $this->level, "target" => $pos->level]) === false){ $this->entity->check = true; return false; } - foreach($this->level->entityList as $e) { - if($e->eid !== $this->entity->eid) { - if($e->isPlayer()) { + foreach($this->level->entityList as $e){ + if($e->eid !== $this->entity->eid){ + if($e->isPlayer()){ $pk = new MovePlayerPacket(); $pk->eid = $this->entity->eid; $pk->x = -256; @@ -227,7 +223,7 @@ class Player { $pk->bodyYaw = 0; $this->dataPacket($pk); - } else { + }else{ $pk = new MoveEntityPacket_PosRot(); $pk->eid = $e->eid; $pk->x = -256; @@ -251,9 +247,9 @@ class Player { $this->chunksLoaded = []; $this->server->api->entity->spawnToAll($this->entity); //$this->server->api->entity->spawnAll($this); - foreach($this->level->entityList as $e) { - if($e->eid !== $this->entity->eid) { - if(!$e->isPlayer()) { + foreach($this->level->entityList as $e){ + if($e->eid !== $this->entity->eid){ + if(!$e->isPlayer()){ $pk = new MoveEntityPacket_PosRot(); $pk->eid = $e->eid; $pk->x = $e->x; @@ -270,8 +266,8 @@ class Player { $pk->time = $this->level->getTime(); $this->dataPacket($pk); $terrain = true; - foreach($this->level->players as $player) { - if($player !== $this and $player->entity instanceof Entity) { + foreach($this->level->players as $player){ + if($player !== $this and $player->entity instanceof Entity){ $pk = new MoveEntityPacket_PosRot; $pk->eid = $player->entity->eid; $pk->x = $player->entity->x; @@ -300,7 +296,7 @@ class Player { } $resyncpos = new Position($pos->x, $pos->y, $pos->z, $pos->level); - } else { + }else{ $resyncpos = new Vector3($pos->x, $pos->y, $pos->z); } @@ -312,14 +308,12 @@ class Player { $this->entity->resetSpeed(); $this->entity->updateLast(); $this->entity->calculateVelocity(); - if($terrain === true) { + if($terrain === true){ $this->orderChunks(); - if($this->spawned) { - $this->getNextChunk($this->level); - } + if($this->spawned) $this->getNextChunk($this->level); } $this->entity->check = true; - if($force === true) { + if($force === true){ $this->forceMovement = $resyncpos; } } @@ -346,23 +340,23 @@ class Player { * * @return array|bool */ - public function dataPacket(RakNetDataPacket $packet) { - if($this->connected === false) { + public function dataPacket(RakNetDataPacket $packet){ + if($this->connected === false){ return false; } - if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) { + if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY){ return; } $packet->encode(); $len = strlen($packet->buffer) + 1; $MTU = $this->MTU - 24; - if($len > $MTU) { + if($len > $MTU){ return $this->directBigRawPacket($packet); } - if(($this->bufferLen + $len) >= $MTU) { + if(($this->bufferLen + $len) >= $MTU){ $this->sendBuffer(); } @@ -373,23 +367,21 @@ class Player { return []; } - private function directBigRawPacket(RakNetDataPacket $packet) { - if($this->connected === false) { + private function directBigRawPacket(RakNetDataPacket $packet){ + if($this->connected === false){ return false; } $sendtime = microtime(true); $size = $this->MTU - 34; - if($size <= 0) { - return false; - } + if($size <= 0) return false; $buffer = str_split($packet->buffer, $size); $bigCnt = $this->bigCnt; $this->bigCnt = ($this->bigCnt + 1) % 0x10000; $cnts = []; $bufCount = count($buffer); - foreach($buffer as $i => $buf) { + foreach($buffer as $i => $buf){ $cnts[] = $count = $this->counter[0]++; $pk = new UnknownPacket; @@ -412,16 +404,16 @@ class Player { return $cnts; } - public function send(RakNetPacket $packet) { - if($this->connected === true) { + public function send(RakNetPacket $packet){ + if($this->connected === true){ $packet->ip = $this->ip; $packet->port = $this->port; $this->bandwidthRaw += $this->server->send($packet); } } - public function sendBuffer() { - if($this->bufferLen > 0 and $this->buffer instanceof RakNetPacket) { + public function sendBuffer(){ + if($this->bufferLen > 0 and $this->buffer instanceof RakNetPacket){ $this->buffer->seqNumber = $this->counter[0]++; $this->recoveryQueue[$this->buffer->seqNumber] = $this->buffer; $this->buffer->sendtime = microtime(true); @@ -438,7 +430,7 @@ class Player { * * @return Item */ - public function getSlot($slot) { + public function getSlot($slot){ return $this->inventory[(int) $slot] ?? BlockAPI::getItem(AIR, 0, 0); } @@ -448,9 +440,9 @@ class Player { * * @return boolean */ - public function setSlot($slot, Item $item, $send = true) { + public function setSlot($slot, Item $item, $send = true){ $this->inventory[(int) $slot] = $item; - if($send === true) { + if($send === true){ $this->sendInventorySlot((int) $slot); } return true; @@ -459,41 +451,41 @@ class Player { /** * @param Player|string|boolean|void $player */ - public function sendArmor($player = false) { + public function sendArmor($player = false){ $data = [ "player" => $this, "eid" => $this->eid, "slots" => [] ]; - for($i = 0; $i < 4; ++$i) { - if(isset($this->armor[$i]) and ($this->armor[$i] instanceof Item) and $this->armor[$i]->getID() > AIR) { + for($i = 0; $i < 4; ++$i){ + if(isset($this->armor[$i]) and ($this->armor[$i] instanceof Item) and $this->armor[$i]->getID() > AIR){ $data["slots"][$i] = $this->armor[$i]->getID() !== AIR ? $this->armor[$i]->getID() - 256 : 0; - } else { + }else{ $this->armor[$i] = BlockAPI::getItem(AIR, 0, 0); $data["slots"][$i] = 255; } } - if($player instanceof Player) { - if($player === $this) { + if($player instanceof Player){ + if($player === $this){ $pk = new ContainerSetContentPacket; $pk->windowid = 0x78; //Armor window id $pk->slots = $this->armor; $this->dataPacket($pk); - } else { + }else{ $pk = new PlayerArmorEquipmentPacket; $pk->eid = $this->eid; $pk->slots = $data["slots"]; $player->dataPacket($pk); } - } else { + }else{ $this->server->api->dhandle("player.armor", $data); } } public $lastOrderX = 0; public $lastOrderZ = 0; - public function orderChunks() { - if(!($this->entity instanceof Entity) or $this->connected === false) { + public function orderChunks(){ + if(!($this->entity instanceof Entity) or $this->connected === false){ return false; } $X = ((int) $this->entity->x) >> 4; @@ -501,28 +493,28 @@ class Player { $this->chunksOrder = []; $this->lastOrderX = $X; $this->lastOrderZ = $Z; - if(self::$smallChunks) { + if(self::$smallChunks){ $Y = ((int) $this->entity->y) >> 4; $v = new Vector3($X, $Y, $Z); - for($x = 0; $x < 16; ++$x) { - for($z = 0; $z < 16; ++$z) { - for($y = 0; $y < 8; ++$y) { + for($x = 0; $x < 16; ++$x){ + for($z = 0; $z < 16; ++$z){ + for($y = 0; $y < 8; ++$y){ $dist = $v->distance(new Vector3($x, $y, $z)); $d = $x . ":" . $y . ":" . $z; - if(!isset($this->chunksLoaded[$d])) { + if(!isset($this->chunksLoaded[$d])){ $this->chunksOrder[$d] = $dist; } } } } - } else { + }else{ $v = new Vector2($X, $Z); - for($x = 0; $x < 16; ++$x) { - for($z = 0; $z < 16; ++$z) { + for($x = 0; $x < 16; ++$x){ + for($z = 0; $z < 16; ++$z){ $dist = $v->distance(new Vector2($x, $z)); - for($y = 0; $y < 8; ++$y) { + for($y = 0; $y < 8; ++$y){ $d = $x . ":" . $y . ":" . $z; - if(!isset($this->chunksLoaded[$d])) { + if(!isset($this->chunksLoaded[$d])){ $this->chunksOrder[$d] = $dist; } } @@ -533,18 +525,18 @@ class Player { asort($this->chunksOrder); } - public function loaddAllChunks() { - for($x = 0; $x < 16; $x++) { - for($z = 0; $z < 16; $z++) { + public function loaddAllChunks(){ + for($x = 0; $x < 16; $x++){ + for($z = 0; $z < 16; $z++){ $this->useChunk($x, $z); } } } - public function useChunk($X, $Z) { + public function useChunk($X, $Z){ $Yndex = 0; - for($iY = 0; $iY < 8; ++$iY) { - if(isset($this->chunksOrder["$X:$iY:$Z"])) { + for($iY = 0; $iY < 8; ++$iY){ + if(isset($this->chunksOrder["$X:$iY:$Z"])){ unset($this->chunksOrder["$X:$iY:$Z"]); $this->chunksLoaded["$X:$iY:$Z"] = true; $Yndex |= 1 << $iY; @@ -563,44 +555,44 @@ class Player { $tiles = $this->server->query("SELECT ID FROM tiles WHERE spawnable = 1 AND level = '{$this->level->getName()}' AND x >= $minX AND x <= $maxX AND z >= $minZ AND z <= $maxZ;"); $this->lastChunk = false; - if($tiles !== false and $tiles !== true) { - while(($tile = $tiles->fetchArray(SQLITE3_ASSOC)) !== false) { + if($tiles !== false and $tiles !== true){ + while(($tile = $tiles->fetchArray(SQLITE3_ASSOC)) !== false){ $tile = $this->server->api->tile->getByID($tile["ID"]); - if($tile instanceof Tile) { + if($tile instanceof Tile){ $tile->spawn($this); } } } - if($cnt === false) { + if($cnt === false){ return false; } } - public function getNextChunk($world) { - if($this->connected === false or $world != $this->level) { + public function getNextChunk($world){ + if($this->connected === false or $world != $this->level){ return false; } - foreach($this->chunkCount as $count => $t) { - if(isset($this->recoveryQueue[$count]) or isset($this->resendQueue[$count])) { + foreach($this->chunkCount as $count => $t){ + if(isset($this->recoveryQueue[$count]) or isset($this->resendQueue[$count])){ $this->server->schedule(MAX_CHUNK_RATE, [$this, "getNextChunk"], $world); return; - } else { + }else{ unset($this->chunkCount[$count]); } } - if(is_array($this->lastChunk)) { + if(is_array($this->lastChunk)){ $minX = $this->lastChunk[0]; $maxX = $this->lastChunk[0] + 15; $minZ = $this->lastChunk[1]; $maxZ = $this->lastChunk[1] + 15; $tiles = $this->server->query("SELECT ID, x, y, z FROM tiles WHERE level = '{$this->level->getName()}' AND x >= $minX AND x <= $maxX AND z >= $minZ AND z <= $maxZ;"); $this->lastChunk = false; - if($tiles !== false and $tiles !== true) { - while(($tile = $tiles->fetchArray(SQLITE3_ASSOC)) !== false) { + if($tiles !== false and $tiles !== true){ + while(($tile = $tiles->fetchArray(SQLITE3_ASSOC)) !== false){ $tile = $this->server->api->tile->getByID($tile["ID"]); - if($tile instanceof Tile) { + if($tile instanceof Tile){ $tile->spawn($this); } } @@ -609,7 +601,7 @@ class Player { $c = key($this->chunksOrder); $d = $c != null ? $this->chunksOrder[$c] : null; - if($c === null or $d === null) { + if($c === null or $d === null){ $this->server->schedule(40, [$this, "getNextChunk"], $world); return false; } @@ -625,8 +617,8 @@ class Player { $y = $Y << 4; $this->level->useChunk($X, $Z, $this); $Yndex = 1 << $Y; - for($iY = 0; $iY < 8; ++$iY) { - if(isset($this->chunksOrder["$X:$iY:$Z"])) { + for($iY = 0; $iY < 8; ++$iY){ + if(isset($this->chunksOrder["$X:$iY:$Z"])){ unset($this->chunksOrder["$X:$iY:$Z"]); $this->chunksLoaded["$X:$iY:$Z"] = true; $Yndex |= 1 << $iY; @@ -637,25 +629,25 @@ class Player { $pk->chunkZ = $Z; $pk->data = $this->level->getOrderedChunk($X, $Z, $Yndex); $cnt = $this->dataPacket($pk); - if($cnt === false) { + if($cnt === false){ return false; } $this->chunkCount = []; - foreach($cnt as $i => $count) { + foreach($cnt as $i => $count){ $this->chunkCount[$count] = true; } $this->lastChunk = [$x, $z]; - if($this->lastOrderX != ($this->entity->x >> 4) || $this->lastOrderZ != ($this->entity->z >> 4)) { + if($this->lastOrderX != ($this->entity->x >> 4) || $this->lastOrderZ != ($this->entity->z >> 4)){ $this->orderChunks(); } $this->server->schedule(MAX_CHUNK_RATE, [$this, "getNextChunk"], $world); } - public function setSpawn(Vector3 $pos) { - if(!($pos instanceof Position)) { + public function setSpawn(Vector3 $pos){ + if(!($pos instanceof Position)){ $level = $this->entity->level; - } else { + }else{ $level = $pos->level; } $this->spawnPosition = new Position($pos->x, $pos->y, $pos->z, $level); @@ -666,17 +658,17 @@ class Player { $this->dataPacket($pk); } - public function sendInventorySlot($s) { + public function sendInventorySlot($s){ $this->sendInventory(); return true; } - public function sendInventory() { - if(($this->gamemode & 0x01) === CREATIVE) { + public function sendInventory(){ + if(($this->gamemode & 0x01) === CREATIVE){ return; } $hotbar = []; - foreach($this->hotbar as $slot) { + foreach($this->hotbar as $slot){ $hotbar[] = $slot <= -1 ? -1 : $slot + 9; } $pk = new ContainerSetContentPacket; @@ -689,26 +681,26 @@ class Player { * * @return boolean */ - public function hasSpace($type, $damage, $count) { + public function hasSpace($type, $damage, $count){ $inv = $this->inventory; - while($count > 0) { + while($count > 0){ $add = 0; - foreach($inv as $s => $item) { - if($item->getID() === AIR) { + foreach($inv as $s => $item){ + if($item->getID() === AIR){ $add = min($item->getMaxStackSize(), $count); $inv[$s] = BlockAPI::getItem($type, $damage, $add); break; - } elseif($item->getID() === $type and $item->getMetadata() === $damage) { + }elseif($item->getID() === $type and $item->getMetadata() === $damage){ $add = min($item->getMaxStackSize() - $item->count, $count); - if($add <= 0) { + if($add <= 0){ continue; } $inv[$s] = BlockAPI::getItem($type, $damage, $item->count + $add); break; } } - if($add <= 0) { + if($add <= 0){ return false; } $count -= $add; @@ -721,13 +713,13 @@ class Player { * * @return Item */ - public function getArmor($slot) { + public function getArmor($slot){ return $this->armor[(int) $slot] ?? BlockAPI::getItem(AIR, 0, 0); } - public function setArmor($slot, Item $armor, $send = true) { + public function setArmor($slot, Item $armor, $send = true){ $this->armor[(int) $slot] = $armor; - if($send === true) { + if($send === true){ $this->sendArmor($this); } return true; @@ -737,8 +729,8 @@ class Player { * @param mixed $data * @param string $event */ - public function eventHandler($data, $event) { - switch($event) { + public function eventHandler($data, $event){ + switch($event){ case "entity.link": $pk = new SetEntityLinkPacket(); $pk->rider = $data["rider"] == $this->entity->eid ? 0 : $data["rider"]; @@ -747,10 +739,10 @@ class Player { $this->dataPacket($pk); break; case "tile.update": - if($data->level === $this->level) { - if($data->class === TILE_FURNACE) { - foreach($this->windows as $id => $w) { - if($w === $data) { + if($data->level === $this->level){ + if($data->class === TILE_FURNACE){ + foreach($this->windows as $id => $w){ + if($w === $data){ $pk = new ContainerSetDataPacket; $pk->windowid = $id; $pk->property = 0; //Smelting @@ -768,9 +760,9 @@ class Player { } break; case "tile.container.slot": - if($data["tile"]->level === $this->level) { - foreach($this->windows as $id => $w) { - if($w === $data["tile"]) { + if($data["tile"]->level === $this->level){ + foreach($this->windows as $id => $w){ + if($w === $data["tile"]){ $pk = new ContainerSetSlotPacket; $pk->windowid = $id; $pk->slot = $data["slot"] + ($data["offset"] ?? 0); @@ -781,8 +773,8 @@ class Player { } break; case "player.armor": - if($data["player"]->level === $this->level) { - if($data["eid"] === $this->eid) { + if($data["player"]->level === $this->level){ + if($data["eid"] === $this->eid){ $this->sendArmor($this); break; } @@ -793,16 +785,16 @@ class Player { } break; case "player.pickup": - if($data["eid"] === $this->eid) { + if($data["eid"] === $this->eid){ $data["eid"] = 0; $pk = new TakeItemEntityPacket; $pk->eid = 0; $pk->target = $data["entity"]->eid; $this->dataPacket($pk); - if(($this->gamemode & 0x01) === 0x00) { + if(($this->gamemode & 0x01) === 0x00){ $this->addItem($data["entity"]->itemID, $data["entity"]->meta, $data["entity"]->stack, false); } - switch($data["entity"]->itemID) { + switch($data["entity"]->itemID){ case WOOD: AchievementAPI::grantAchievement($this, "mineWood"); break; @@ -813,7 +805,7 @@ class Player { AchievementAPI::grantAchievement($this, "leather"); break; } - } elseif($data["entity"]->level === $this->level) { + }elseif($data["entity"]->level === $this->level){ $pk = new TakeItemEntityPacket; $pk->eid = $data["eid"]; $pk->target = $data["entity"]->eid; @@ -821,7 +813,7 @@ class Player { } break; case "player.equipment.change": - if($data["eid"] === $this->eid or $data["player"]->level !== $this->level) { + if($data["eid"] === $this->eid or $data["player"]->level !== $this->level){ break; } $data["slot"] = 0; @@ -835,10 +827,10 @@ class Player { break; case "entity.motion": - if($data->eid === $this->eid or $data->level !== $this->level) { + if($data->eid === $this->eid or $data->level !== $this->level){ break; } - if(($data->speedX === 0 && $data->speedY === 0 && $data->speedZ === 0) || ($data->speedX === $data->lastSpeedX && $data->speedY === $data->lastSpeedY && $data->lastSpeedZ === $data->speedZ)) { + if(($data->speedX === 0 && $data->speedY === 0 && $data->speedZ === 0) || ($data->speedX === $data->lastSpeedX && $data->speedY === $data->lastSpeedY && $data->lastSpeedZ === $data->speedZ)){ break; } $pk = new SetEntityMotionPacket; @@ -856,12 +848,12 @@ class Player { $this->dataPacket($pk); break; case "entity.metadata": - if($data->eid === $this->eid) { + if($data->eid === $this->eid){ $eid = 0; - } else { + }else{ $eid = $data->eid; } - if($data->level === $this->level) { + if($data->level === $this->level){ $pk = new SetEntityDataPacket; $pk->eid = $eid; $pk->metadata = $data->getMetadata(); @@ -869,12 +861,12 @@ class Player { } break; case "entity.event": - if($data["entity"]->eid === $this->eid) { + if($data["entity"]->eid === $this->eid){ $eid = 0; - } else { + }else{ $eid = $data["entity"]->eid; } - if($data["entity"]->level === $this->level) { + if($data["entity"]->level === $this->level){ $pk = new EntityEventPacket; $pk->eid = $eid; $pk->event = $data["event"]; @@ -882,14 +874,14 @@ class Player { } break; case "server.chat": - if(($data instanceof Container) === true) { - if(!$data->check($this->username) and !$data->check($this->iusername)) { + if(($data instanceof Container) === true){ + if(!$data->check($this->username) and !$data->check($this->iusername)){ return; - } else { + }else{ $message = $data->get(); $this->sendChat(preg_replace('/\x1b\[[0-9;]*m/', "", $message["message"]), $message["player"]); //Remove ANSI codes from chat } - } else { + }else{ $message = (string) $data; $this->sendChat(preg_replace('/\x1b\[[0-9;]*m/', "", (string) $data)); //Remove ANSI codes from chat } @@ -903,43 +895,33 @@ class Player { * * @return boolean */ - public function addItem($type, $damage, $count, $send = true) { + public function addItem($type, $damage, $count, $send = true){ - foreach($this->inventory as $s => $item) { //force check the inventory for non-full stacks of this item first - if($item->getID() === $type and $item->getMetadata() === $damage) { + foreach($this->inventory as $s => $item){ //force check the inventory for non-full stacks of this item first + if($item->getID() === $type and $item->getMetadata() === $damage){ $add = min($item->getMaxStackSize() - $item->count, $count); - if($add <= 0) { + if($add <= 0){ continue; } $item->count += $add; - if($send) { - $this->sendInventorySlot($s); - } + if($send) $this->sendInventorySlot($s); $count -= $add; - if($count <= 0) { - return true; - } + if($count <= 0) return true; } } $toadd = BlockAPI::getItem($type, $damage, $count); - foreach($this->inventory as $s => $item) { - if($item->getID() === AIR) { + foreach($this->inventory as $s => $item){ + if($item->getID() === AIR){ $add = min($toadd->getMaxStackSize(), $count); $this->inventory[$s] = BlockAPI::getItem($type, $damage, $add); - if($send) { - $this->sendInventorySlot($s); - } + if($send) $this->sendInventorySlot($s); $count -= $add; - if($count <= 0) { - return true; - } + if($count <= 0) return true; } } - if($count <= 0) { - return true; - } + if($count <= 0) return true; return false; } @@ -947,18 +929,18 @@ class Player { * @param string $message * @param string $author */ - public function sendChat($message, $author = "") { + public function sendChat($message, $author = ""){ $mes = explode("\n", $message); - foreach($mes as $m) { - if(preg_match_all('#@([@A-Za-z_]{1,})#', $m, $matches, PREG_OFFSET_CAPTURE) > 0) { + foreach($mes as $m){ + if(preg_match_all('#@([@A-Za-z_]{1,})#', $m, $matches, PREG_OFFSET_CAPTURE) > 0){ $offsetshift = 0; - foreach($matches[1] as $selector) { - if($selector[0][0] === "@") { //Escape! + foreach($matches[1] as $selector){ + if($selector[0][0] === "@"){ //Escape! $m = substr_replace($m, $selector[0], $selector[1] + $offsetshift - 1, strlen($selector[0]) + 1); --$offsetshift; continue; } - switch(strtolower($selector[0])) { + switch(strtolower($selector[0])){ case "player": case "username": $m = substr_replace($m, $this->username, $selector[1] + $offsetshift - 1, strlen($selector[0]) + 1); @@ -968,7 +950,7 @@ class Player { } } - if($m !== "") { + if($m !== ""){ $pk = new MessagePacket; $pk->source = ($author instanceof Player) ? $author->username : $author; $pk->message = TextFormat::clean($m); //Colors not implemented :( @@ -976,18 +958,18 @@ class Player { } } } - public function makeInvisibleForOnePlayer(Player $player) { + public function makeInvisibleForOnePlayer(Player $player){ $pk = new RemoveEntityPacket; $pk->eid = $this->entity->eid; $player->dataPacket($pk); } - public function makeInvisibleForAllPlayers() { + public function makeInvisibleForAllPlayers(){ $pk = new RemoveEntityPacket; $pk->eid = $this->entity->eid; $this->server->api->player->broadcastPacket($this->server->api->player->getAll($this->level), $pk); } - public function getGamemode() { - switch($this->gamemode) { + public function getGamemode(){ + switch($this->gamemode){ case SURVIVAL: return "survival"; case CREATIVE: @@ -999,16 +981,14 @@ class Player { } } - public function checkSpawnPosition() { - if($this->server->api->dhandle("player.checkspawnpos", ["player" => $this]) === false) { - return; - } + public function checkSpawnPosition(){ + if($this->server->api->dhandle("player.checkspawnpos", ["player" => $this]) === false) return; $level = $this->spawnPosition->level; - if(!isset($this->server->api->level->levels[$level->getName()])) { + if(!isset($this->server->api->level->levels[$level->getName()])){ ConsoleAPI::warn("Level to respawn {$this->iusername} was unloaded, changing spawnpoint to default."); $level = $this->server->api->level->getDefault(); $this->spawnPosition = $level->getSpawn(); - } else { + }else{ $x0 = floor($this->spawnPosition->x - $this->entity->width / 2); $x1 = floor($this->spawnPosition->x + $this->entity->width / 2 + 1); $y0 = floor($this->spawnPosition->y); @@ -1020,11 +1000,11 @@ class Player { for($z = $z0; $z < $z1; ++$z) { for($y = $y0; $y < $y1; ++$y) { $bid = $this->entity->level->level->getBlockID($x, $y, $z); - if($bid > 0 && StaticBlock::getIsSolid($bid)) { + if($bid > 0 && StaticBlock::getIsSolid($bid)){ $blockBounds = StaticBlock::$prealloc[$bid]::getCollisionBoundingBoxes($this->entity->level, $x, $y, $z, $this->entity); - foreach($blockBounds as $blockBound) { - if($this->entity->boundingBox->intersectsWith($blockBound)) { + foreach($blockBounds as $blockBound){ + if($this->entity->boundingBox->intersectsWith($blockBound)){ $this->sendChat("Your spawn positon is obstructed."); goto reset_spawn_pos; } @@ -1039,36 +1019,36 @@ class Player { } } - public function setGamemode($gm) { - if($gm < 0 or $gm > 3 or $this->gamemode === $gm) { + public function setGamemode($gm){ + if($gm < 0 or $gm > 3 or $this->gamemode === $gm){ return false; } - if($this->server->api->dhandle("player.gamemode.change", ["player" => $this, "gamemode" => $gm]) === false) { + if($this->server->api->dhandle("player.gamemode.change", ["player" => $this, "gamemode" => $gm]) === false){ return false; } $inv = &$this->inventory; - if($gm === VIEW) { + if($gm === VIEW){ $this->armor = []; $this->sendArmor(); } - if(($this->gamemode & 0x01) === ($gm & 0x01)) { - if(($gm & 0x01) === 0x01 and ($gm & 0x02) === 0x02) { + if(($this->gamemode & 0x01) === ($gm & 0x01)){ + if(($gm & 0x01) === 0x01 and ($gm & 0x02) === 0x02){ $inv = []; - foreach(BlockAPI::$creative as $item) { + foreach(BlockAPI::$creative as $item){ $inv[] = BlockAPI::getItem(0, 0, 1); } - } elseif(($gm & 0x01) === 0x01) { + }elseif(($gm & 0x01) === 0x01){ $inv = []; - foreach(BlockAPI::$creative as $item) { + foreach(BlockAPI::$creative as $item){ $inv[] = BlockAPI::getItem($item[0], $item[1], 1); } } $this->gamemode = $gm; $this->sendChat("Your gamemode has been changed to " . $this->getGamemode() . ".\n"); - } else { - foreach($this->inventory as $slot => $item) { + }else{ + foreach($this->inventory as $slot => $item){ $inv[$slot] = BlockAPI::getItem(AIR, 0, 0); } $this->hotbar = [0, 1, 2, 3, 4, 5, 6, 7, 8]; @@ -1079,10 +1059,10 @@ class Player { $this->server->schedule(30, [$this, "close"], "gamemode change", false, true); //Forces a kick } - if($this->gamemode === SPECTATOR) { + if($this->gamemode === SPECTATOR){ $this->makeInvisibleForAllPlayers(); } - if($this->gamemode === CREATIVE) { + if($this->gamemode === CREATIVE){ $this->server->api->player->spawnToAllPlayers($this); } @@ -1092,7 +1072,7 @@ class Player { return true; } - public function sendSettings($nametags = true) { + public function sendSettings($nametags = true){ /* bit mask | flag name 0b00000001 allowInteract @@ -1105,11 +1085,11 @@ class Player { 0b10000000 - unused */ $flags = 0; - if(($this->gamemode & 0x02) === 0x02) { + if(($this->gamemode & 0x02) === 0x02){ $flags |= 0x01; //Do not allow placing/breaking blocks, adventure mode } - if($nametags !== false) { + if($nametags !== false){ $flags |= 0x20; //Show Nametags } @@ -1121,13 +1101,13 @@ class Player { $this->dataPacket($pk); } - public function measureLag() { - if($this->connected === false) { + public function measureLag(){ + if($this->connected === false){ return false; } - if($this->packetStats[1] > 2) { + if($this->packetStats[1] > 2){ $this->packetLoss = $this->packetStats[1] / max(1, $this->packetStats[0] + $this->packetStats[1]); - } else { + }else{ $this->packetLoss = 0; } $this->packetStats = [0, 0]; @@ -1140,53 +1120,53 @@ class Player { $this->lastMeasure = microtime(true); } - public function getLag() { + public function getLag(){ return $this->lagStat * 1000; } - public function getPacketLoss() { + public function getPacketLoss(){ return $this->packetLoss; } - public function getBandwidth() { + public function getBandwidth(){ return array_sum($this->bandwidthStats) / max(1, count($this->bandwidthStats)); } - public function clearQueue() { - if($this->connected === false) { + public function clearQueue(){ + if($this->connected === false){ return false; } - if(($cnt = count($this->received)) > PLAYER_MAX_QUEUE) { + if(($cnt = count($this->received)) > PLAYER_MAX_QUEUE){ ksort($this->received); - foreach($this->received as $c => $t) { + foreach($this->received as $c => $t){ unset($this->received[$c]); --$cnt; - if($cnt <= PLAYER_MAX_QUEUE) { + if($cnt <= PLAYER_MAX_QUEUE){ break; } } } } - public function handlePacketQueues() { - if($this->connected === false) { + public function handlePacketQueues(){ + if($this->connected === false){ return false; } $time = microtime(true); - if($time > $this->timeout) { + if($time > $this->timeout){ $this->close("timeout"); return false; } - if(($ackCnt = count($this->ackQueue)) > 0) { + if(($ackCnt = count($this->ackQueue)) > 0){ rsort($this->ackQueue); $safeCount = (int) (($this->MTU - 1) / 4); $packetCnt = (int) ($ackCnt / $safeCount + 1); - for($p = 0; $p < $packetCnt; ++$p) { + for($p = 0; $p < $packetCnt; ++$p){ $pk = new RakNetPacket(RakNetInfo::ACK); $pk->packets = []; - for($c = 0; $c < $safeCount; ++$c) { - if(($k = array_pop($this->ackQueue)) === null) { + for($c = 0; $c < $safeCount; ++$c){ + if(($k = array_pop($this->ackQueue)) === null){ break; } $pk->packets[] = $k; @@ -1196,20 +1176,20 @@ class Player { $this->ackQueue = []; } - if(($receiveCnt = count($this->receiveQueue)) > 0) { + if(($receiveCnt = count($this->receiveQueue)) > 0){ ksort($this->receiveQueue); - foreach($this->receiveQueue as $count => $packets) { + foreach($this->receiveQueue as $count => $packets){ unset($this->receiveQueue[$count]); - foreach($packets as $p) { - if($p instanceof RakNetDataPacket and $p->hasSplit === false) { - if(isset($p->messageIndex) and $p->messageIndex !== false) { - if($p->messageIndex > $this->receiveCount) { + foreach($packets as $p){ + if($p instanceof RakNetDataPacket and $p->hasSplit === false){ + if(isset($p->messageIndex) and $p->messageIndex !== false){ + if($p->messageIndex > $this->receiveCount){ $this->receiveCount = $p->messageIndex; - } elseif($p->messageIndex !== 0) { - if(isset($this->received[$p->messageIndex])) { + }elseif($p->messageIndex !== 0){ + if(isset($this->received[$p->messageIndex])){ continue; } - switch($p->pid()) { + switch($p->pid()){ case 0x01: case ProtocolInfo::PING_PACKET: case ProtocolInfo::PONG_PACKET: @@ -1229,38 +1209,37 @@ class Player { } } - if($this->bufferLen > 0) { + if($this->bufferLen > 0){ $this->sendBuffer(); } $limit = $time - 5; //max lag - foreach($this->recoveryQueue as $count => $data) { - if($data->sendtime > $limit) { + foreach($this->recoveryQueue as $count => $data){ + if($data->sendtime > $limit){ break; } unset($this->recoveryQueue[$count]); $this->resendQueue[$count] = $data; } - if(($resendCnt = count($this->resendQueue)) > 0) { - foreach($this->resendQueue as $count => $data) { + if(($resendCnt = count($this->resendQueue)) > 0){ + foreach($this->resendQueue as $count => $data){ unset($this->resendQueue[$count]); $this->packetStats[1]++; $this->lag[] = $time - $data->sendtime; $data->sendtime = $time; $cnt = $this->send($data); - if(isset($this->chunkCount[$count])) { + if(isset($this->chunkCount[$count])){ unset($this->chunkCount[$count]); - if(!is_null($cnt) and !is_null($cnt[0])) { + if(!is_null($cnt) and !is_null($cnt[0])) $this->chunkCount[$cnt[0]] = true; - } } } } } - public function sendEntityMovementUpdateQueue() { - if($this->entityMovementQueueLength > 0 && $this->entityMovementQueue instanceof RakNetPacket) { + public function sendEntityMovementUpdateQueue(){ + if($this->entityMovementQueueLength > 0 && $this->entityMovementQueue instanceof RakNetPacket){ $this->entityMovementQueue->seqNumber = $this->counter[0]++; $this->send($this->entityMovementQueue); } @@ -1269,8 +1248,8 @@ class Player { $this->entityMovementQueue->data = []; } - public function sendBlockUpdateQueue() { - if($this->blockUpdateQueueLength > 0 && $this->blockUpdateQueue instanceof RakNetPacket) { + public function sendBlockUpdateQueue(){ + if($this->blockUpdateQueueLength > 0 && $this->blockUpdateQueue instanceof RakNetPacket){ $this->blockUpdateQueue->seqNumber = $this->counter[0]++; $this->recoveryQueue[$this->blockUpdateQueue->seqNumber] = $this->blockUpdateQueue; $this->blockUpdateQueue->sendtime = microtime(true); @@ -1281,7 +1260,7 @@ class Player { $this->blockUpdateQueue->data = []; } - public function addBlockUpdateIntoQueue($x, $y, $z, $id, $meta) { + public function addBlockUpdateIntoQueue($x, $y, $z, $id, $meta){ $packet = new UpdateBlockPacket(); $packet->x = $x; $packet->y = $y; @@ -1293,7 +1272,7 @@ class Player { $len = 1 + strlen($packet->buffer); $MTU = $this->MTU - 24; - if(($this->blockUpdateQueueLength + $len) >= $MTU) { + if(($this->blockUpdateQueueLength + $len) >= $MTU){ $this->sendBlockUpdateQueue(); } @@ -1303,18 +1282,18 @@ class Player { $this->blockUpdateQueueLength += 6 + $len; } - public function addEntityMovementUpdateToQueue(Entity $e) { + public function addEntityMovementUpdateToQueue(Entity $e){ $len = 0; $packets = 0; $motionSent = false; $moveSent = false; $headSent = false; $svdYSpeed = $e->speedY; - if($e->modifySpeedY) { + if($e->modifySpeedY){ $e->speedY = $e->modifedSpeedY; } - if($e->speedX != 0 || $e->speedY != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ) { - if(!($e->speedY < 0 && $e->onGround) || $e->speedX != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ) { + if($e->speedX != 0 || $e->speedY != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ){ + if(!($e->speedY < 0 && $e->onGround) || $e->speedX != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ){ $motion = new SetEntityMotionPacket(); $motion->eid = $e->eid; $motion->speedX = $e->speedX; @@ -1326,13 +1305,13 @@ class Player { $motionSent = true; } } - if($e->modifySpeedY) { + if($e->modifySpeedY){ $e->speedY = $svdYSpeed; $e->modifySpeedY = false; } - if($e->x != $e->lastX || $e->y != $e->lastY || $e->z != $e->lastZ || $e->yaw != $e->lastYaw || $e->pitch != $e->lastPitch) { - if($e->headYaw != $e->lastHeadYaw) { + if($e->x != $e->lastX || $e->y != $e->lastY || $e->z != $e->lastZ || $e->yaw != $e->lastYaw || $e->pitch != $e->lastPitch){ + if($e->headYaw != $e->lastHeadYaw){ $move = new MovePlayerPacket(); $move->eid = $e->eid; $move->x = $e->x; @@ -1342,7 +1321,7 @@ class Player { $move->pitch = $e->pitch; $move->bodyYaw = $e->headYaw; $move->encode(); - } else { + }else{ $move = new MoveEntityPacket_PosRot(); $move->eid = $e->eid; $move->x = $e->x; @@ -1356,7 +1335,7 @@ class Player { $len += strlen($move->buffer) + 1; ++$packets; $moveSent = true; - } elseif($e->headYaw != $e->lastHeadYaw) { + }elseif($e->headYaw != $e->lastHeadYaw){ $headyaw = new RotateHeadPacket(); $headyaw->eid = $e->eid; $headyaw->yaw = $e->headYaw; @@ -1365,25 +1344,23 @@ class Player { ++$packets; $headSent = true; } - if($packets <= 0) { - return; - } + if($packets <= 0) return; //console("Update {$e}: $packets, mot: $motionSent, mov: $moveSent, hed: $headSent"); $MTU = $this->MTU - 24; - if(($this->entityMovementQueueLength + $len) >= $MTU) { + if(($this->entityMovementQueueLength + $len) >= $MTU){ $this->sendEntityMovementUpdateQueue(); } - if($motionSent) { + if($motionSent){ $motion->messageIndex = 0; //force 0 cuz reliability 0 $motion->reliability = 0; $this->entityMovementQueue->data[] = $motion; } - if($moveSent) { + if($moveSent){ $move->messageIndex = 0; $move->reliability = 0; $this->entityMovementQueue->data[] = $move; } - if($headSent) { + if($headSent){ $headyaw->messageIndex = 0; $headyaw->reliability = 0; $this->entityMovementQueue->data[] = $headyaw; @@ -1396,12 +1373,12 @@ class Player { * @param string $reason Reason for closing connection * @param boolean $msg Set to false to silently disconnect player. No broadcast. */ - public function close($reason = "", $msg = true) { - if($this->connected === true) { - foreach($this->evid as $ev) { + public function close($reason = "", $msg = true){ + if($this->connected === true){ + foreach($this->evid as $ev){ $this->server->deleteEvent($ev); } - if($this->username != "") { + if($this->username != ""){ $this->server->api->handle("player.quit", $this); $this->save(); } @@ -1419,7 +1396,7 @@ class Player { $this->resendQueue = []; $this->ackQueue = []; $this->server->api->player->remove($this->CID); - if($msg === true and $this->username != "" and $this->spawned !== false) { + if($msg === true and $this->username != "" and $this->spawned !== false){ $this->server->api->chat->broadcast($this->username . " left the game: " . $reason); } $this->spawned = false; @@ -1436,8 +1413,8 @@ class Player { } } - public function save() { - if($this->entity instanceof Entity) { + public function save(){ + if($this->entity instanceof Entity){ $this->data->set("achievements", $this->achievements); $this->data->set("position", [ "level" => $this->entity->level->getName(), @@ -1454,9 +1431,9 @@ class Player { "z" => $this->spawnPosition->z ]); $inv = []; - foreach($this->inventory as $slot => $item) { - if($item instanceof Item) { - if($slot < (($this->gamemode & 0x01) === 0 ? PLAYER_SURVIVAL_SLOTS : PLAYER_CREATIVE_SLOTS)) { + foreach($this->inventory as $slot => $item){ + if($item instanceof Item){ + if($slot < (($this->gamemode & 0x01) === 0 ? PLAYER_SURVIVAL_SLOTS : PLAYER_CREATIVE_SLOTS)){ $inv[$slot] = [$item->getID(), $item->getMetadata(), $item->count]; } } @@ -1465,25 +1442,25 @@ class Player { $this->data->set("hotbar", $this->hotbar); $armor = []; - foreach($this->armor as $slot => $item) { - if($item instanceof Item) { + foreach($this->armor as $slot => $item){ + if($item instanceof Item){ $armor[$slot] = [$item->getID(), $item->getMetadata()]; } } $this->data->set("armor", $armor); - if($this->entity instanceof Entity) { + if($this->entity instanceof Entity){ $this->data->set("health", $this->entity->getHealth()); } $this->data->set("gamemode", $this->gamemode); } } - public function directDataPacket(RakNetDataPacket $packet, $reliability = 0, $recover = true) { - if($this->connected === false) { + public function directDataPacket(RakNetDataPacket $packet, $reliability = 0, $recover = true){ + if($this->connected === false){ return false; } - if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY) { + if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY){ return []; } @@ -1492,20 +1469,18 @@ class Player { $pk->data[] = $packet; $pk->seqNumber = $this->counter[0]++; $pk->sendtime = microtime(true); - if($recover !== false) { + if($recover !== false){ $this->recoveryQueue[$pk->seqNumber] = $pk; } $this->send($pk); return [$pk->seqNumber]; } - public function entityTick() { - if($this->isSleeping) { - ++$this->sleepingTime; - } + public function entityTick(){ + if($this->isSleeping) ++$this->sleepingTime; - if($this->server->difficulty == 0 && $this->entity->counter % (20 * 15) == 0) { - if($this->entity->health < 20 && $this->entity->health > 0) { + if($this->server->difficulty == 0 && $this->entity->counter % (20 * 15) == 0){ + if($this->entity->health < 20 && $this->entity->health > 0){ $this->entity->setHealth(min(20, $this->entity->health + 1), "regeneration"); } } @@ -1522,21 +1497,21 @@ class Player { return $this->lastPing; } - public function handleDataPacket(RakNetDataPacket $packet) { - if($this->connected === false) { + public function handleDataPacket(RakNetDataPacket $packet){ + if($this->connected === false){ return; } - if(EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY) { + if(EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY){ return; } - switch($packet->pid()) { + switch($packet->pid()){ case 0x01: break; case ProtocolInfo::PONG_PACKET: $currentTime = intdiv(hrtime(true), 1_000_000); - if($currentTime > $packet->ptime) { + if($currentTime > $packet->ptime){ $this->lastPing = $currentTime - $packet->ptime; } break; @@ -1550,7 +1525,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; @@ -1560,27 +1535,27 @@ 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]; - 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); @@ -1588,55 +1563,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]; } } @@ -1646,15 +1621,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); } @@ -1677,27 +1652,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); } @@ -1707,7 +1678,7 @@ 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; @@ -1741,12 +1712,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; } @@ -1764,8 +1735,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(); @@ -1781,50 +1752,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; @@ -1833,53 +1802,49 @@ 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; - } elseif($packet->slot > 0) { + }elseif($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; @@ -1890,27 +1855,25 @@ class Player { $this->slot = $packet->slot; $this->curHotbarIndex = $packetSlotPos; } - if(!$has) { + 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(); @@ -1922,13 +1885,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); @@ -1968,18 +1931,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; } @@ -2002,17 +1965,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); - if($d >= 0.0001) { + if($d >= 0.0001){ $shootX = $x / $d; $shootY = $y / $d; $shootZ = $z / $d; @@ -2029,19 +1992,16 @@ 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); - } else { - $slotItem->count -= 1; - } + if($slotItem !== false){ + if($slotItem->count == 1) $this->inventory[$this->slot] = BlockAPI::getItem(AIR, 0, 0); + else $slotItem->count -= 1; //$this->sendInventory(); } } @@ -2049,8 +2009,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); @@ -2060,26 +2020,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, @@ -2112,7 +2070,7 @@ class Player { $this->server->api->entity->spawnToAll($e); 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); @@ -2120,7 +2078,7 @@ class Player { } } } - } else { //inv desynced, resend + }else{ //inv desynced, resend $this->sendInventory(); } } @@ -2135,7 +2093,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; @@ -2152,46 +2110,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; @@ -2202,28 +2160,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 = []; @@ -2240,18 +2198,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(); @@ -2261,22 +2219,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; @@ -2284,10 +2242,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); } } @@ -2295,11 +2253,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; } @@ -2309,18 +2267,16 @@ 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; @@ -2333,7 +2289,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; @@ -2346,33 +2302,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; } @@ -2381,11 +2337,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); } } @@ -2393,14 +2349,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; @@ -2409,7 +2365,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; @@ -2426,36 +2382,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); @@ -2464,38 +2420,38 @@ 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; } @@ -2509,7 +2465,7 @@ class Player { "slotdata" => $slot, "itemdata" => $item, "player" => $this - ]) === false) { + ]) === false){ $pk = new ContainerSetSlotPacket; $pk->windowid = $packet->windowid; $pk->slot = $packet->slot; @@ -2518,24 +2474,24 @@ class Player { 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); @@ -2547,7 +2503,7 @@ class Player { "slotdata" => $slot, "itemdata" => $item, "player" => $this, - ]) === false) { + ]) === false){ $pk = new ContainerSetSlotPacket; $pk->windowid = $packet->windowid; $pk->slot = $packet->slot; @@ -2556,24 +2512,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); @@ -2583,27 +2539,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"]); } } @@ -2615,11 +2571,11 @@ 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) { ConsoleAPI::warn("Player is riding on entity that doesnt exist in world! ({$this->iusername}, {$this->entity->linkedEntity})"); @@ -2652,45 +2608,45 @@ class Player { * @return Item */ - public function getHeldItem() { + public function getHeldItem(){ return $this->getSlot($this->slot); } - public function stopSleep() { + public function stopSleep(){ $this->isSleeping = false; $this->sleepingTime = 0; - if($this->entity instanceof Entity) { + if($this->entity instanceof Entity){ $this->entity->updateMetadata(); } } - public function hasItem($type, $damage = false) { - foreach($this->inventory as $s => $item) { - if($item->getID() === $type and ($item->getMetadata() === $damage or $damage === false) and $item->count > 0) { + public function hasItem($type, $damage = false){ + foreach($this->inventory as $s => $item){ + if($item->getID() === $type and ($item->getMetadata() === $damage or $damage === false) and $item->count > 0){ return $s; } } return false; } - public function removeItem($type, $damage, $count, $send = true) { - while($count > 0) { + public function removeItem($type, $damage, $count, $send = true){ + while($count > 0){ $remove = 0; - foreach($this->inventory as $s => $item) { - if($item->getID() === $type and $item->getMetadata() === $damage) { + foreach($this->inventory as $s => $item){ + if($item->getID() === $type and $item->getMetadata() === $damage){ $remove = min($count, $item->count); - if($remove < $item->count) { + if($remove < $item->count){ $item->count -= $remove; - } else { + }else{ $this->inventory[$s] = BlockAPI::getItem(AIR, 0, 0); } - if($send === true) { + if($send === true){ $this->sendInventorySlot($s); } break; } } - if($remove <= 0) { + if($remove <= 0){ return false; } $count -= $remove; @@ -2702,16 +2658,16 @@ class Player { * * @return array|bool */ - public function craftItems(array $craft, array $recipe, $type) { + public function craftItems(array $craft, array $recipe, $type){ $craftItem = [0, true, 0]; unset($craft[-1]); - foreach($craft as $slot => $item) { - if($item instanceof Item) { + foreach($craft as $slot => $item){ + if($item instanceof Item){ $craftItem[0] = $item->getID(); - if($item->getMetadata() !== $craftItem[1] and $craftItem[1] !== true) { + if($item->getMetadata() !== $craftItem[1] and $craftItem[1] !== true){ $craftItem[1] = false; - } else { + }else{ $craftItem[1] = $item->getMetadata(); } $craftItem[2] += $item->count; @@ -2720,11 +2676,11 @@ class Player { } $recipeItems = []; - foreach($recipe as $slot => $item) { - if(!isset($recipeItems[$item->getID()])) { + foreach($recipe as $slot => $item){ + if(!isset($recipeItems[$item->getID()])){ $recipeItems[$item->getID()] = [$item->getID(), $item->getMetadata(), $item->count]; - } else { - if($item->getMetadata() !== $recipeItems[$item->getID()][1]) { + }else{ + if($item->getMetadata() !== $recipeItems[$item->getID()][1]){ $recipeItems[$item->getID()][1] = false; } $recipeItems[$item->getID()][2] += $item->count; @@ -2733,31 +2689,31 @@ class Player { $res = CraftingRecipes::canCraft($craftItem, $recipeItems, $type); - if(!is_array($res) and $type === 1) { + if(!is_array($res) and $type === 1){ $res2 = CraftingRecipes::canCraft($craftItem, $recipeItems, 0); - if(is_array($res2)) { + if(is_array($res2)){ $res = $res2; } } - if(is_array($res)) { - if($this->server->api->dhandle("player.craft", ["player" => $this, "recipe" => $recipe, "craft" => $craft, "type" => $type]) === false) { + if(is_array($res)){ + if($this->server->api->dhandle("player.craft", ["player" => $this, "recipe" => $recipe, "craft" => $craft, "type" => $type]) === false){ return false; } - foreach($recipe as $slot => $item) { + foreach($recipe as $slot => $item){ $s = $this->getSlot($slot); $s->count -= $item->count; - if($s->count <= 0) { + if($s->count <= 0){ $this->setSlot($slot, BlockAPI::getItem(AIR, 0, 0), false); } } - foreach($craft as $slot => $item) { + foreach($craft as $slot => $item){ $s = $this->getSlot($slot); - if($s->count <= 0 or $s->getID() === AIR) { + if($s->count <= 0 or $s->getID() === AIR){ $this->setSlot($slot, BlockAPI::getItem($item->getID(), $item->getMetadata(), $item->count), false); - } elseif($s->getID() == $item->getID() && $s->getMetadata() == $item->getMetadata() && ($s->count + $item->count) <= $s->getMaxStackSize()) { + }elseif($s->getID() == $item->getID() && $s->getMetadata() == $item->getMetadata() && ($s->count + $item->count) <= $s->getMaxStackSize()){ $this->setSlot($slot, BlockAPI::getItem($item->getID(), $item->getMetadata(), $s->count + $item->count), false); - } else { + }else{ $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; @@ -2773,7 +2729,7 @@ class Player { $this->sendInventory(); //force send on crafting - switch($item->getID()) { + switch($item->getID()){ case WORKBENCH: AchievementAPI::grantAchievement($this, "buildWorkBench"); break; @@ -2812,26 +2768,26 @@ class Player { return $res; } - public function handlePacket(RakNetPacket $packet) { - if($this->connected === true) { + public function handlePacket(RakNetPacket $packet){ + if($this->connected === true){ $time = microtime(true); $this->timeout = $time + 20; - switch($packet->pid()) { + switch($packet->pid()){ case RakNetInfo::NACK: - foreach($packet->packets as $count) { - if(isset($this->recoveryQueue[$count])) { + foreach($packet->packets as $count){ + if(isset($this->recoveryQueue[$count])){ $this->resendQueue[$count] = &$this->recoveryQueue[$count]; $this->lag[] = $time - $this->recoveryQueue[$count]->sendtime; unset($this->recoveryQueue[$count]); - } else { + }else{ ++$this->packetStats[1]; //lost and wont be recovered } } break; case RakNetInfo::ACK: - foreach($packet->packets as $count) { - if(isset($this->recoveryQueue[$count])) { + foreach($packet->packets as $count){ + if(isset($this->recoveryQueue[$count])){ $this->lag[] = $time - $this->recoveryQueue[$count]->sendtime; unset($this->recoveryQueue[$count]); unset($this->resendQueue[$count]); @@ -2858,7 +2814,7 @@ class Player { case RakNetInfo::DATA_PACKET_F: $this->ackQueue[] = $packet->seqNumber; $this->receiveQueue[$packet->seqNumber] = []; - foreach($packet->data as $pk) { + foreach($packet->data as $pk){ $this->receiveQueue[$packet->seqNumber][] = $pk; } break; @@ -2866,9 +2822,9 @@ class Player { } } - public function damageArmorPart($slot, $part) { + public function damageArmorPart($slot, $part){ $part->useOn($this->entity, true); - if($part->getMetadata() >= $part->getMaxDurability()) { + if($part->getMetadata() >= $part->getMaxDurability()){ $this->setArmor($slot, BlockAPI::getItem(AIR, 0, 0), false); return; } @@ -2878,8 +2834,8 @@ class Player { /** * @return string */ - public function __toString() { - if($this->username != "") { + function __toString(){ + if($this->username != ""){ return $this->username; } return $this->clientID; @@ -2887,9 +2843,9 @@ class Player { } -class PlayerNull extends Player { +class PlayerNull extends Player{ public static $INSTANCE; - public function __construct() { + public function __construct(){ } } diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index ae369d6e..b0ae2dc8 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -1,69 +1,18 @@ port = (int) $port; $this->doTick = true; $this->gamemode = (int) $gamemode; @@ -77,7 +26,7 @@ class PocketMinecraftServer { public static $SAVE_PLAYER_DATA = true; - private function load() { + private function load(){ global $dolog; /*if(defined("DEBUG") and DEBUG >= 0){ @cli_set_process_title("NostalgiaCore ".MAJOR_VERSION); @@ -120,7 +69,7 @@ class PocketMinecraftServer { $this->interface = new MinecraftInterface("255.255.255.255", $this->port, $this->serverip); $this->stop = false; $this->ticks = 0; - if(!defined("NO_THREADS")) { + if(!defined("NO_THREADS")){ $this->asyncThread = new AsyncMultipleQueue(); } console("[INFO] Loading extra.properties..."); @@ -157,24 +106,24 @@ class PocketMinecraftServer { MobController::$ADVANCED = $this->extraprops->get("experimental-mob-ai"); Explosion::$enableExplosions = $this->extraprops->get("enable-explosions"); NetherReactorBlock::$enableReactor = $this->extraprops->get("enable-nether-reactor"); - if(self::$FORCE_20_TPS) { + if(self::$FORCE_20_TPS){ ConsoleAPI::warn("Forcing 20 tps. This may result in higher CPU usage!"); } - if($this->extraprops->get("discord-msg")) { - if($this->extraprops->get("discord-webhook-url") !== "none") { + if($this->extraprops->get("discord-msg")){ + if($this->extraprops->get("discord-webhook-url") !== "none"){ console("[INFO] Discord Logger is enabled."); - } else { + }else{ console("[WARNING] Discord Logger is enabled in extra.properties,"); console("[WARNING] but you didn't put the webhook url, so it won't work."); } - } elseif($this->extraprops->get("version") == null) { + }elseif($this->extraprops->get("version") == null){ console("[WARNING] Your extra.properties file is corrupted!"); console("[WARNING] To fix it - just remove it! Server will generate it again automatically."); } $dolog = $this->extraprops->get("save-console-data"); } - public function onShutdown() { + public function onShutdown(){ console("[INFO] Saving..."); $save = $this->saveEnabled; $this->saveEnabled = true; @@ -182,7 +131,7 @@ class PocketMinecraftServer { $this->saveEnabled = $save; } - public function startDatabase() { + public function startDatabase(){ $this->preparedSQL = new stdClass(); $this->preparedSQL->entity = new stdClass(); $this->database = new SQLite3(":memory:"); @@ -204,16 +153,16 @@ class PocketMinecraftServer { $this->preparedSQL->entity->setLevel = $this->database->prepare("UPDATE entities SET level = :level WHERE EID = :eid ;"); } - public function query($sql, $fetch = false) { + public function query($sql, $fetch = false){ $result = $this->database->query($sql) or console("[ERROR] [SQL Error] " . $this->database->lastErrorMsg() . ". Query: " . $sql, true, true, 0); - if($fetch === true and ($result instanceof SQLite3Result)) { + if($fetch === true and ($result instanceof SQLite3Result)){ $result = $result->fetchArray(SQLITE3_ASSOC); } return $result; } - public function setType($type = "normal") { - switch(trim(strtolower($type))) { + public function setType($type = "normal"){ + switch(trim(strtolower($type))){ case "normal": case "demo": $this->serverType = "MCCPP;Demo;"; @@ -224,9 +173,9 @@ class PocketMinecraftServer { } } - public function titleTick() { + public function titleTick(){ $time = microtime(true); - if(defined("DEBUG") and DEBUG >= 0) { + if(defined("DEBUG") and DEBUG >= 0){ //echo "\x1b]0;NostalgiaCore " . MAJOR_VERSION . " | Online " . count($this->clients) . "/" . $this->maxClients . " | RAM " . round((memory_get_usage() / 1024) / 1024, 2) . "MB | U " . round(($this->interface->bandwidth[1] / max(1, $time - $this->interface->bandwidth[2])) / 1024, 2) . " D " . round(($this->interface->bandwidth[0] / max(1, $time - $this->interface->bandwidth[2])) / 1024, 2) . " kB/s | TPS " . $this->getTPS() . "\x07"; } @@ -236,18 +185,18 @@ class PocketMinecraftServer { /** * @return float */ - public function getTPS() { + public function getTPS(){ $v = array_values($this->tickMeasure); $divval = ($v[39] - $v[0]); - if($divval === 0) { + if($divval === 0){ return 0; } $tps = 40 / $divval; return round($tps, 4); } - public function checkTicks() { - if($this->getTPS() < 12) { + public function checkTicks(){ + if($this->getTPS() < 12){ console("[WARNING] Can't keep up! Is the server overloaded?"); } } @@ -255,14 +204,14 @@ class PocketMinecraftServer { /** * @param string $reason */ - public function close($reason = "server stop") { + public function close($reason = "server stop"){ $this->onShutdown(); - if($this->stop !== true) { - if(is_int($reason)) { + if($this->stop !== true){ + if(is_int($reason)){ $reason = "signal stop"; } - if(($this->api instanceof ServerAPI) === true) { - if(($this->api->chat instanceof ChatAPI) === true) { + if(($this->api instanceof ServerAPI) === true){ + if(($this->api->chat instanceof ChatAPI) === true){ $this->api->chat->send(false, "Stopping server..."); self::$_tmp = new StopMessageThread($this, "[INFO] Stopping server..."); //broadcast didnt want to send message to discord for some reason } @@ -270,8 +219,8 @@ class PocketMinecraftServer { $this->stop = true; $this->trigger("server.close", $reason); $this->interface->close(); - if(!defined("NO_THREADS")) { - $this->asyncThread->synchronized(function ($t) { + if(!defined("NO_THREADS")){ + $this->asyncThread->synchronized(function ($t){ $t->stop = true; $t->notify(); }, $this->asyncThread); @@ -280,8 +229,8 @@ class PocketMinecraftServer { release_lock(); } - public function send2Discord($msg) { - if($this->extraprops->get("discord-msg") and $this->extraprops->get("discord-webhook-url") !== "none") { + public function send2Discord($msg){ + if($this->extraprops->get("discord-msg") and $this->extraprops->get("discord-webhook-url") !== "none"){ $url = $this->extraprops->get("discord-webhook-url"); $name = $this->extraprops->get("discord-bot-name"); $this->asyncOperation(ASYNC_CURL_POST, [ @@ -294,15 +243,15 @@ class PocketMinecraftServer { } } - public function asyncOperation($type, array $data, callable $callable = null) { - if(defined("NO_THREADS")) { + public function asyncOperation($type, array $data, callable $callable = null){ + if(defined("NO_THREADS")){ return false; } $d = ""; $type = (int) $type; - switch($type) { + switch($type){ case ASYNC_CURL_GET: - if(isset($data["headers"])) { + if(isset($data["headers"])){ $jsstr = json_encode($data["headers"]); } $d .= Utils::writeShort(strlen($data["url"])) . $data["url"] . (isset($data["timeout"]) ? Utils::writeShort($data["timeout"]) : Utils::writeShort(10)) . (isset($data["headers"]) ? Utils::writeShort(strlen($jsstr)) . $jsstr : Utils::writeShort(1) . " "); @@ -310,7 +259,7 @@ class PocketMinecraftServer { case ASYNC_CURL_POST: $d .= Utils::writeShort(strlen($data["url"])) . $data["url"] . (isset($data["timeout"]) ? Utils::writeShort($data["timeout"]) : Utils::writeShort(10)); $d .= Utils::writeShort(count($data["data"])); - foreach($data["data"] as $key => $value) { + foreach($data["data"] as $key => $value){ $d .= Utils::writeShort(strlen($key)) . $key . Utils::writeInt(strlen($value)) . $value; } break; @@ -323,55 +272,55 @@ class PocketMinecraftServer { return $ID; } - public function trigger($event, $data = "") { - if(isset($this->events[$event])) { - foreach($this->events[$event] as $evid => $ev) { - if(!is_callable($ev)) { + public function trigger($event, $data = ""){ + if(isset($this->events[$event])){ + foreach($this->events[$event] as $evid => $ev){ + if(!is_callable($ev)){ $this->deleteEvent($evid); continue; } - if(is_array($ev)) { + if(is_array($ev)){ $method = $ev[1]; $ev[0]->$method($data, $event); - } else { + }else{ $ev($data, $event); } } - } elseif(isset(Deprecation::$events[$event])) { + }elseif(isset(Deprecation::$events[$event])){ $sub = ""; - if(Deprecation::$events[$event] !== false) { + if(Deprecation::$events[$event] !== false){ $sub = " Substitute \"" . Deprecation::$events[$event] . "\" found."; } console("[ERROR] Event \"$event\" has been deprecated.$sub [Trigger]"); } } - public function deleteEvent($id) { + public function deleteEvent($id){ $id = (int) $id; - if(isset($this->eventsID[$id])) { + if(isset($this->eventsID[$id])){ $ev = $this->eventsID[$id]; $this->eventsID[$id] = null; unset($this->eventsID[$id]); $this->events[$ev][$id] = null; unset($this->events[$ev][$id]); - if(count($this->events[$ev]) === 0) { + if(count($this->events[$ev]) === 0){ unset($this->events[$ev]); } } } - public function asyncOperationChecker() { - if(defined("NO_THREADS")) { + public function asyncOperationChecker(){ + if(defined("NO_THREADS")){ return false; } - if(isset($this->asyncThread->output[5])) { + if(isset($this->asyncThread->output[5])){ $offset = 0; $ID = Utils::readInt(substr($this->asyncThread->output, $offset, 4)); $offset += 4; $type = Utils::readShort(substr($this->asyncThread->output, $offset, 2)); $offset += 2; $data = []; - switch($type) { + switch($type){ case ASYNC_CURL_GET: case ASYNC_CURL_POST: $len = Utils::readInt(substr($this->asyncThread->output, $offset, 4)); @@ -383,11 +332,11 @@ class PocketMinecraftServer { } $this->asyncThread->output = substr($this->asyncThread->output, $offset); - if(isset($this->async[$ID]) and $this->async[$ID] !== null and is_callable($this->async[$ID])) { - if(is_array($this->async[$ID])) { + if(isset($this->async[$ID]) and $this->async[$ID] !== null and is_callable($this->async[$ID])){ + if(is_array($this->async[$ID])){ $method = $this->async[$ID][1]; $result = $this->async[$ID][0]->$method($data, $type, $ID); - } else { + }else{ $result = $this->async[$ID]($data, $type, $ID); } } @@ -401,12 +350,12 @@ class PocketMinecraftServer { * * @return boolean */ - public function addHandler($event, callable $callable, $priority = 5) { - if(!is_callable($callable)) { + public function addHandler($event, callable $callable, $priority = 5){ + if(!is_callable($callable)){ return false; - } elseif(isset(Deprecation::$events[$event])) { + }elseif(isset(Deprecation::$events[$event])){ $sub = ""; - if(Deprecation::$events[$event] !== false) { + if(Deprecation::$events[$event] !== false){ $sub = " Substitute \"" . Deprecation::$events[$event] . "\" found."; } console("[ERROR] Event \"$event\" has been deprecated.$sub [Adding handle to " . (is_array($callable) ? get_class($callable[0]) . "::" . $callable[1] : $callable) . "]"); @@ -419,64 +368,64 @@ class PocketMinecraftServer { return $hnid; } - public function dhandle($e, $d) { + public function dhandle($e, $d){ return $this->handle($e, $d); } - public function handle($event, &$data) { + public function handle($event, &$data){ $this->preparedSQL->selectHandlers->reset(); $this->preparedSQL->selectHandlers->clear(); $this->preparedSQL->selectHandlers->bindValue(":name", $event, SQLITE3_TEXT); $handlers = $this->preparedSQL->selectHandlers->execute(); $result = null; - if($handlers instanceof SQLite3Result) { + if($handlers instanceof SQLite3Result){ $call = []; - while(($hn = $handlers->fetchArray(SQLITE3_ASSOC)) !== false) { + while(($hn = $handlers->fetchArray(SQLITE3_ASSOC)) !== false){ $call[(int) $hn["ID"]] = true; } $handlers->finalize(); - foreach($call as $hnid => $boolean) { - if($result !== false and $result !== true) { + foreach($call as $hnid => $boolean){ + if($result !== false and $result !== true){ $called[$hnid] = true; $handler = $this->handlers[$hnid]; - if(is_array($handler)) { + if(is_array($handler)){ $method = $handler[1]; $result = $handler[0]->$method($data, $event); - } else { + }else{ $result = $handler($data, $event); } - } else { + }else{ break; } } - } elseif(isset(Deprecation::$events[$event])) { + }elseif(isset(Deprecation::$events[$event])){ $sub = ""; - if(Deprecation::$events[$event] !== false) { + if(Deprecation::$events[$event] !== false){ $sub = " Substitute \"" . Deprecation::$events[$event] . "\" found."; } console("[ERROR] Event \"$event\" has been deprecated.$sub [Handler]"); } - if($result !== false) { + if($result !== false){ $this->trigger($event, $data); } return $result; } - public function eventHandler($data, $event) { - switch($event) { + public function eventHandler($data, $event){ + switch($event){ } } - public function init() { + public function init(){ register_tick_function([$this, "tick"]); declare(ticks=5000); //Minimum TPS for main thread locks $this->loadEvents(); register_shutdown_function([$this, "dumpError"]); register_shutdown_function([$this, "close"]); - if(function_exists("pcntl_signal")) { + if(function_exists("pcntl_signal")){ pcntl_signal(SIGTERM, [$this, "close"]); pcntl_signal(SIGINT, [$this, "close"]); pcntl_signal(SIGHUP, [$this, "close"]); @@ -488,8 +437,8 @@ class PocketMinecraftServer { $this->process(); } - public function loadEvents() { - if(ENABLE_ANSI === true) { + public function loadEvents(){ + if(ENABLE_ANSI === true){ $this->schedule(30, [$this, "titleTick"], [], true); } $this->schedule(20 * 15, [$this, "checkTicks"], [], true); @@ -497,8 +446,8 @@ class PocketMinecraftServer { $this->schedule(20, [$this, "asyncOperationChecker"], [], true); } - public function schedule($ticks, callable $callback, $data = [], $repeat = false, $eventName = "server.schedule") { - if(!is_callable($callback)) { + public function schedule($ticks, callable $callback, $data = [], $repeat = false, $eventName = "server.schedule"){ + if(!is_callable($callback)){ return false; } @@ -511,8 +460,8 @@ class PocketMinecraftServer { /** * @return string */ - public function getGamemode() { - switch($this->gamemode) { + public function getGamemode(){ + switch($this->gamemode){ case SURVIVAL: return "survival"; case CREATIVE: @@ -524,16 +473,17 @@ class PocketMinecraftServer { } } - public function process() { + public function process() + { $lastLoop = 0; - if(self::$FORCE_20_TPS) { - while($this->stop === false) { + if(self::$FORCE_20_TPS){ + while($this->stop === false){ $packetcnt = 0; - while($packet = $this->interface->readPacket()) { + while($packet = $this->interface->readPacket()){ if($packet instanceof Packet) { $this->packetHandler($packet); - if(++$packetcnt > self::$PACKET_READING_LIMIT) { - ConsoleAPI::warn("Reading more than " . self::$PACKET_READING_LIMIT . " packets per tick! Forcing ticking!"); + if(++$packetcnt > self::$PACKET_READING_LIMIT){ + ConsoleAPI::warn("Reading more than ".self::$PACKET_READING_LIMIT." packets per tick! Forcing ticking!"); break; } } @@ -541,30 +491,30 @@ class PocketMinecraftServer { $this->tick(); } - } else { - while($this->stop === false) { + }else{ + while($this->stop === false){ $packetcnt = 0; startReadingAgain: $packet = $this->interface->readPacket(); - if($packet instanceof Packet) { + if($packet instanceof Packet){ $this->packetHandler($packet); $lastLoop = 0; - if(++$packetcnt > self::$PACKET_READING_LIMIT) { - ConsoleAPI::warn("Reading more than " . self::$PACKET_READING_LIMIT . " packets per tick! Forcing ticking!"); - } else { + if(++$packetcnt > self::$PACKET_READING_LIMIT){ + ConsoleAPI::warn("Reading more than ".self::$PACKET_READING_LIMIT." packets per tick! Forcing ticking!"); + }else{ goto startReadingAgain; } - } elseif($this->tick() > 0) { + } elseif($this->tick() > 0){ $lastLoop = 0; - } else { + } else{ ++$lastLoop; - if($lastLoop < 16) { + if($lastLoop < 16){ usleep(1); - } elseif($lastLoop < 128) { + } elseif($lastLoop < 128){ usleep(100); - } elseif($lastLoop < 256) { + } elseif($lastLoop < 256){ usleep(512); - } else { + } else{ usleep(10000); } } @@ -574,19 +524,19 @@ class PocketMinecraftServer { } - public function packetHandler(Packet $packet) { + public function packetHandler(Packet $packet){ $data = &$packet; $CID = PocketMinecraftServer::clientID($packet->ip, $packet->port); - if(isset($this->clients[$CID])) { + if(isset($this->clients[$CID])){ $this->clients[$CID]->handlePacket($packet); - } else { - if($this->handle("server.noauthpacket." . $packet->pid(), $packet) === false) { + }else{ + if($this->handle("server.noauthpacket." . $packet->pid(), $packet) === false){ return; } - switch($packet->pid()) { + switch($packet->pid()){ case RakNetInfo::UNCONNECTED_PING: case RakNetInfo::UNCONNECTED_PING_OPEN_CONNECTIONS: - if($this->invisible === true) { + if($this->invisible === true){ $pk = new RakNetPacket(RakNetInfo::UNCONNECTED_PONG); $pk->pingID = $packet->pingID; $pk->serverID = $this->serverID; @@ -596,11 +546,11 @@ class PocketMinecraftServer { $this->send($pk); break; } - if(!isset($this->custom["times_" . $CID])) { + if(!isset($this->custom["times_" . $CID])){ $this->custom["times_" . $CID] = 0; } $ln = 15; - if($this->description == "" or !str_ends_with($this->description, " ")) { + if($this->description == "" or !str_ends_with($this->description, " ")){ $this->description .= " "; } $txt = substr($this->description, $this->custom["times_" . $CID], $ln); @@ -615,14 +565,14 @@ class PocketMinecraftServer { $this->custom["times_" . $CID] = ($this->custom["times_" . $CID] + 1) % strlen($this->description); break; case RakNetInfo::OPEN_CONNECTION_REQUEST_1: - if($packet->structure !== RakNetInfo::STRUCTURE) { + if($packet->structure !== RakNetInfo::STRUCTURE){ console("[DEBUG] Incorrect structure #" . $packet->structure . " from " . $packet->ip . ":" . $packet->port, true, true, 2); $pk = new RakNetPacket(RakNetInfo::INCOMPATIBLE_PROTOCOL_VERSION); $pk->serverID = $this->serverID; $pk->ip = $packet->ip; $pk->port = $packet->port; $this->send($pk); - } else { + }else{ $pk = new RakNetPacket(RakNetInfo::OPEN_CONNECTION_REPLY_1); $pk->serverID = $this->serverID; $pk->mtuSize = strlen($packet->buffer); @@ -632,7 +582,7 @@ class PocketMinecraftServer { } break; case RakNetInfo::OPEN_CONNECTION_REQUEST_2: - if($this->invisible === true) { + if($this->invisible === true){ break; } @@ -648,30 +598,30 @@ class PocketMinecraftServer { } } - public static function clientID($ip, $port) { + public static function clientID($ip, $port){ return crc32($ip . $port) ^ crc32($port . $ip . BOOTUP_RANDOM); //return $ip . ":" . $port; } - public function send(Packet $packet) { + public function send(Packet $packet){ return $this->interface->writePacket($packet); } - public function tick() { + public function tick(){ $time = microtime(true); - if($this->lastTick <= ($time - 0.05)) { + if($this->lastTick <= ($time - 0.05)){ $this->tickMeasure[] = $this->lastTick = $time; unset($this->tickMeasure[key($this->tickMeasure)]); ++$this->ticks; - foreach($this->clients as $client) { + foreach($this->clients as $client){ $client->handlePacketQueues(); - if($this->ticks % 40 == 0) { //2s + if($this->ticks % 40 == 0){ //2s $client->sendPing(); } } - foreach($this->api->level->levels as $l) { + foreach($this->api->level->levels as $l){ $l->onTick($this, $time); } $r = $this->tickerFunction($time); @@ -680,34 +630,34 @@ class PocketMinecraftServer { return 0; } - public function tickerFunction($time) { + public function tickerFunction($time){ //actions that repeat every x time will go here $this->preparedSQL->selectActions->reset(); $this->preparedSQL->selectActions->bindValue(":time", $time, SQLITE3_FLOAT); $actions = $this->preparedSQL->selectActions->execute(); $actionCount = 0; - if($actions instanceof SQLite3Result) { - while(($action = $actions->fetchArray(SQLITE3_ASSOC)) !== false) { + if($actions instanceof SQLite3Result){ + while(($action = $actions->fetchArray(SQLITE3_ASSOC)) !== false){ $cid = $action["ID"]; $this->preparedSQL->updateAction->reset(); $this->preparedSQL->updateAction->bindValue(":time", $time, SQLITE3_FLOAT); $this->preparedSQL->updateAction->bindValue(":id", $cid, SQLITE3_INTEGER); $this->preparedSQL->updateAction->execute(); - if(isset($this->schedule[$cid]) && is_array($this->schedule[$cid]) && isset($this->schedule[$cid][0]) && is_callable($this->schedule[$cid][0])) { + if(isset($this->schedule[$cid]) && is_array($this->schedule[$cid]) && isset($this->schedule[$cid][0]) && is_callable($this->schedule[$cid][0])){ ++$actionCount; - try { - $return = @call_user_func($this->schedule[$cid][0] ?? function () {}, $this->schedule[$cid][1], $this->schedule[$cid][2]); //somehow args can be null - } catch(TypeError $e) { - $m = $e->getMessage() . "\nStack trace:\n" . $e->getTraceAsString(); + try{ + $return = @call_user_func($this->schedule[$cid][0] ?? function(){}, $this->schedule[$cid][1], $this->schedule[$cid][2]); //somehow args can be null + }catch(TypeError $e){ + $m = $e->getMessage()."\nStack trace:\n".$e->getTraceAsString(); ConsoleAPI::error($m); $return = false; } - } else { + }else{ $return = false; } - if($action["repeat"] == 0 or $return === false) { + if($action["repeat"] == 0 or $return === false){ $this->query("DELETE FROM actions WHERE ID = " . $action["ID"] . ";"); $this->schedule[$cid] = null; unset($this->schedule[$cid]); @@ -718,8 +668,8 @@ class PocketMinecraftServer { return $actionCount; } - public function dumpError() { - if($this->stop === true) { + public function dumpError(){ + if($this->stop === true){ return; } ini_set("memory_limit", "-1"); //Fix error dump not dumped on memory problems @@ -745,18 +695,18 @@ class PocketMinecraftServer { ]; $er["type"] = $errorConversion[$er["type"]] ?? $er["type"]; $dump .= "Error: " . var_export($er, true) . "\r\n\r\n"; - if(stripos($er["file"], "plugin") !== false) { + if(stripos($er["file"], "plugin") !== false){ $dump .= "THIS ERROR WAS CAUSED BY A PLUGIN. REPORT IT TO THE PLUGIN DEVELOPER.\r\n"; } $dump .= "Code: \r\n"; $file = @file($er["file"], FILE_IGNORE_NEW_LINES); - for($l = max(0, $er["line"] - 10); $l < $er["line"] + 10; ++$l) { + for($l = max(0, $er["line"] - 10); $l < $er["line"] + 10; ++$l){ $dump .= "[" . ($l + 1) . "] " . @$file[$l] . "\r\n"; } $dump .= "\r\n\r\n"; $dump .= "Backtrace: \r\n"; - foreach(getTrace() as $line) { + foreach(getTrace() as $line){ $dump .= "$line\r\n"; } $dump .= "\r\n\r\n"; @@ -771,21 +721,21 @@ class PocketMinecraftServer { global $arguments; $dump .= "Parameters: " . var_export($arguments, true) . "\r\n\r\n\r\n"; $p = $this->api->getProperties(); - if($p["rcon.password"] != "") { + if($p["rcon.password"] != ""){ $p["rcon.password"] = "******"; } $dump .= "server.properties: " . var_export($p, true) . "\r\n\r\n\r\n"; - if($this->api->plugin instanceof PluginAPI) { + if($this->api->plugin instanceof PluginAPI){ $plist = $this->api->plugin->getList(); $dump .= "Loaded plugins:\r\n"; - foreach($plist as $p) { + foreach($plist as $p){ $dump .= $p["name"] . " " . $p["version"] . " by " . $p["author"] . "\r\n"; } $dump .= "\r\n\r\n"; } $extensions = []; - foreach(get_loaded_extensions() as $ext) { + foreach(get_loaded_extensions() as $ext){ $extensions[$ext] = phpversion($ext); } @@ -806,7 +756,7 @@ class PocketMinecraftServer { console("[SEVERE] Please submit the \"{$name}.log\" file to the Bug Reporting page. Give as much info as you can.", true, true, 0); } - public function debugInfo($console = false) { + public function debugInfo($console = false){ $info = []; $info["tps"] = $this->getTPS(); $info["memory_usage"] = round((memory_get_usage() / 1024) / 1024, 2) . "MB"; @@ -820,33 +770,33 @@ class PocketMinecraftServer { $info["actions"] = $info["actions"]["count"]; $info["garbage"] = gc_collect_cycles(); $this->handle("server.debug", $info); - if($console === true) { + if($console === true){ console("[INFO] TPS: " . $info["tps"] . ", Memory usage: " . $info["memory_usage"] . " (Peak " . $info["memory_peak_usage"] . "), Entities: " . $info["entities"] . ", Events: " . $info["events"] . ", Handlers: " . $info["handlers"] . ", Actions: " . $info["actions"] . ", Garbage: " . $info["garbage"], true, true); } return $info; } - public function checkMemory() { + public function checkMemory(){ $info = $this->debugInfo(); $data = $info["memory_usage"] . "," . $info["players"] . "," . $info["entities"]; $i = count($this->memoryStats) - 1; - if($i < 0 or $this->memoryStats[$i] !== $data) { + if($i < 0 or $this->memoryStats[$i] !== $data){ $this->memoryStats[] = $data; } } - public function event($event, callable $func) { - if(!is_callable($func)) { + public function event($event, callable $func){ + if(!is_callable($func)){ return false; - } elseif(isset(Deprecation::$events[$event])) { + }elseif(isset(Deprecation::$events[$event])){ $sub = ""; - if(Deprecation::$events[$event] !== false) { + if(Deprecation::$events[$event] !== false){ $sub = " Substitute \"" . Deprecation::$events[$event] . "\" found."; } console("[ERROR] Event \"$event\" has been deprecated.$sub [Attach to " . (is_array($func) ? get_class($func[0]) . "::" . $func[1] : $func) . "]"); } $evid = $this->evCnt++; - if(!isset($this->events[$event])) { + if(!isset($this->events[$event])){ $this->events[$event] = []; } $this->events[$event][$evid] = $func; diff --git a/src/installer/Installer.php b/src/installer/Installer.php index fe4ec2c4..b2697189 100644 --- a/src/installer/Installer.php +++ b/src/installer/Installer.php @@ -25,8 +25,8 @@ } }while($lang == false); $this->lang = new InstallerLang($lang); - echo "[*] " . $this->lang->language_has_been_selected . "\n"; - echo "[?] " . $this->lang->skip_installer . " (y/N): "; + echo "[*] ".$this->lang->language_has_been_selected."\n"; + echo "[?] ".$this->lang->skip_installer." (y/N): "; if(strtolower($this->getInput()) === "y"){ return; } @@ -41,7 +41,7 @@ } private function welcome(){ - echo $this->lang->welcome_to_pocketmine . "\n"; + echo $this->lang->welcome_to_pocketmine."\n"; echo <<lang->accept_license . " (y/N): "; + echo "\n[?] ".$this->lang->accept_license." (y/N): "; if(strtolower($this->getInput("n")) != "y"){ - echo "[!] " . $this->lang->you_have_to_accept_the_license . "\n"; + echo "[!] ".$this->lang->you_have_to_accept_the_license."\n"; sleep(5); exit(0); } - echo "[*] " . $this->lang->setting_up_server_now . "\n"; - echo "[*] " . $this->lang->default_values_info . "\n"; - echo "[*] " . $this->lang->server_properties . "\n"; + echo "[*] ".$this->lang->setting_up_server_now."\n"; + echo "[*] ".$this->lang->default_values_info."\n"; + echo "[*] ".$this->lang->server_properties."\n"; } private function generateBaseConfig(){ $config = new Config(DATA_PATH . "server.properties", CONFIG_PROPERTIES); - echo "[?] " . $this->lang->name_your_server . " (" . self::DEFAULT_NAME . "): "; + echo "[?] ".$this->lang->name_your_server." (".self::DEFAULT_NAME."): "; $config->set("server-name", $this->getInput(self::DEFAULT_NAME)); - echo "[*] " . $this->lang->port_warning . "\n"; + echo "[*] ".$this->lang->port_warning."\n"; do{ - echo "[?] " . $this->lang->server_port . " (" . self::DEFAULT_PORT . "): "; + echo "[?] ".$this->lang->server_port." (".self::DEFAULT_PORT."): "; $port = (int) $this->getInput(self::DEFAULT_PORT); if($port <= 0 or $port > 65535){ - echo "[!] " . $this->lang->invalid_port . "\n"; + echo "[!] ".$this->lang->invalid_port."\n"; } }while($port <= 0 or $port > 65535); $config->set("server-port", $port); - echo "[*] " . $this->lang->ram_warning . "\n"; - echo "[?] " . $this->lang->server_ram . " (" . self::DEFAULT_MEMORY . "): "; - $config->set("memory-limit", ((int) $this->getInput(self::DEFAULT_MEMORY)) . "M"); - echo "[*] " . $this->lang->gamemode_info . "\n"; + echo "[*] ".$this->lang->ram_warning."\n"; + echo "[?] ".$this->lang->server_ram." (".self::DEFAULT_MEMORY."): "; + $config->set("memory-limit", ((int) $this->getInput(self::DEFAULT_MEMORY))."M"); + echo "[*] ".$this->lang->gamemode_info."\n"; do{ - echo "[?] " . $this->lang->default_gamemode . ": (" . self::DEFAULT_GAMEMODE . "): "; + echo "[?] ".$this->lang->default_gamemode.": (".self::DEFAULT_GAMEMODE."): "; $gamemode = (int) $this->getInput(self::DEFAULT_GAMEMODE); }while($gamemode < 0 or $gamemode > 3); $config->set("gamemode", $gamemode); - echo "[?] " . $this->lang->max_players . " (" . self::DEFAULT_PLAYERS . "): "; + echo "[?] ".$this->lang->max_players." (".self::DEFAULT_PLAYERS."): "; $config->set("max-players", (int) $this->getInput(self::DEFAULT_PLAYERS)); - echo "[*] " . $this->lang->spawn_protection_info . "\n"; - echo "[?] " . $this->lang->spawn_protection . " (Y/n): "; + echo "[*] ".$this->lang->spawn_protection_info."\n"; + echo "[?] ".$this->lang->spawn_protection." (Y/n): "; if(strtolower($this->getInput("y")) == "n"){ $config->set("spawn-protection", -1); }else{ @@ -97,21 +97,21 @@ LICENSE; } private function generateUserFiles(){ - echo "[*] " . $this->lang->op_info . "\n"; - echo "[?] " . $this->lang->op_who . ": "; + echo "[*] ".$this->lang->op_info."\n"; + echo "[?] ".$this->lang->op_who.": "; $op = strtolower($this->getInput("")); if($op === ""){ - echo "[!] " . $this->lang->op_warning . "\n"; + echo "[!] ".$this->lang->op_warning."\n"; }else{ - $ops = new Config(DATA_PATH . "ops.txt", CONFIG_LIST); + $ops = new Config(DATA_PATH."ops.txt", CONFIG_LIST); $ops->set($op, true); $ops->save(); } - echo "[*] " . $this->lang->whitelist_info . "\n"; - echo "[?] " . $this->lang->whitelist_enable . " (y/N): "; + echo "[*] ".$this->lang->whitelist_info."\n"; + echo "[?] ".$this->lang->whitelist_enable." (y/N): "; $config = new Config(DATA_PATH . "server.properties", CONFIG_PROPERTIES); if(strtolower($this->getInput("n")) === "y"){ - echo "[!] " . $this->lang->whitelist_warning . "\n"; + echo "[!] ".$this->lang->whitelist_warning."\n"; $config->set("white-list", true); }else{ $config->set("white-list", false); @@ -121,42 +121,42 @@ LICENSE; private function networkFunctions(){ $config = new Config(DATA_PATH . "server.properties", CONFIG_PROPERTIES); - echo "[!] " . $this->lang->query_warning1 . "\n"; - echo "[!] " . $this->lang->query_warning2 . "\n"; - echo "[?] " . $this->lang->query_disable . " (y/N): "; + echo "[!] ".$this->lang->query_warning1."\n"; + echo "[!] ".$this->lang->query_warning2."\n"; + echo "[?] ".$this->lang->query_disable." (y/N): "; if(strtolower($this->getInput("n")) === "y"){ $config->set("enable-query", false); }else{ $config->set("enable-query", true); } - echo "[*] " . $this->lang->rcon_info . "\n"; - echo "[?] " . $this->lang->rcon_enable . " (y/N): "; + echo "[*] ".$this->lang->rcon_info."\n"; + echo "[?] ".$this->lang->rcon_enable." (y/N): "; if(strtolower($this->getInput("n")) === "y"){ $config->set("enable-rcon", true); $password = substr(base64_encode(Utils::getRandomBytes(20, false)), 3, 10); $config->set("rcon.password", $password); - echo "[*] " . $this->lang->rcon_password . ": " . $password . "\n"; + echo "[*] ".$this->lang->rcon_password.": ".$password."\n"; }else{ $config->set("enable-rcon", false); } $config->save(); - echo "[*] " . $this->lang->ip_get . "\n"; + echo "[*] ".$this->lang->ip_get."\n"; $externalIP = Utils::getIP(); $internalIP = gethostbyname(trim(`hostname`)); - echo "[!] " . $this->lang->get("ip_warning", ["{{EXTERNAL_IP}}", "{{INTERNAL_IP}}"], [$externalIP, $internalIP]) . "\n"; - echo "[!] " . $this->lang->ip_confirm; + echo "[!] ".$this->lang->get("ip_warning", ["{{EXTERNAL_IP}}", "{{INTERNAL_IP}}"], [$externalIP, $internalIP])."\n"; + echo "[!] ".$this->lang->ip_confirm; $this->getInput(); } private function endWizard(){ - echo "[*] " . $this->lang->you_have_finished . "\n"; - echo "[*] " . $this->lang->pocketmine_plugins . "\n"; - echo "[*] " . $this->lang->pocketmine_will_start . "\n\n\n"; + echo "[*] ".$this->lang->you_have_finished."\n"; + echo "[*] ".$this->lang->pocketmine_plugins."\n"; + echo "[*] ".$this->lang->pocketmine_will_start."\n\n\n"; sleep(4); } @@ -192,11 +192,11 @@ LICENSE; private $lang; private $langfile; public function __construct($lang = ""){ - if(file_exists(FILE_PATH . "src/lang/Installer/" . $lang . ".ini")){ + if(file_exists(FILE_PATH."src/lang/Installer/".$lang.".ini")){ $this->lang = $lang; - $this->langfile = FILE_PATH . "src/lang/Installer/" . $lang . ".ini"; + $this->langfile = FILE_PATH."src/lang/Installer/".$lang.".ini"; }else{ - $l = glob(FILE_PATH . "src/lang/Installer/" . $lang . "_*.ini"); + $l = glob(FILE_PATH."src/lang/Installer/".$lang."_*.ini"); if(count($l) > 0){ $files = []; foreach($l as $file){ @@ -207,14 +207,14 @@ LICENSE; $l = key($files); $l = substr($l, strrpos($l, "/") + 1, -4); $this->lang = isset(self::$languages[$l]) ? $l:$lang; - $this->langfile = FILE_PATH . "src/lang/Installer/" . $l . ".ini"; + $this->langfile = FILE_PATH."src/lang/Installer/".$l.".ini"; }else{ $this->lang = "en"; - $this->langfile = FILE_PATH . "src/lang/Installer/en.ini"; + $this->langfile = FILE_PATH."src/lang/Installer/en.ini"; } } - $this->loadLang(FILE_PATH . "src/lang/Installer/en.ini", "en"); + $this->loadLang(FILE_PATH."src/lang/Installer/en.ini", "en"); if($this->lang !== "en"){ $this->loadLang($this->langfile, $this->lang); } diff --git a/src/material/Block.php b/src/material/Block.php index e83a913f..f723cb60 100755 --- a/src/material/Block.php +++ b/src/material/Block.php @@ -292,7 +292,7 @@ abstract class Block extends Position{ } final public function __toString(){ - return "Block " . $this->name . " (" . $this->id . ":" . $this->meta . ")"; + return "Block ". $this->name ." (".$this->id.":".$this->meta.")"; } abstract function isBreakable(Item $item, Player $player); diff --git a/src/material/Item.php b/src/material/Item.php index 91aaf975..22e1b392 100755 --- a/src/material/Item.php +++ b/src/material/Item.php @@ -294,7 +294,7 @@ class Item{ } public function __toString(){ - return "Item " . $this->name . " (" . $this->id . ":" . $this->meta . ")"; + return "Item ". $this->name ." (".$this->id.":".$this->meta.")"; } public function getDamageAgainstOf($e){ diff --git a/src/material/block/solid/LeavesBlock.php b/src/material/block/solid/LeavesBlock.php index c4e3dc3e..2862884c 100644 --- a/src/material/block/solid/LeavesBlock.php +++ b/src/material/block/solid/LeavesBlock.php @@ -22,7 +22,7 @@ class LeavesBlock extends TransparentBlock{ } public static function createIndex($x, $y, $z){ - return $x . "." . $y . "." . $z; + return $x.".".$y.".".$z; } public static function findLog(Level $level, $x, $y, $z, array $visited, $distance){ //port from newest pocketmine $index = self::createIndex($x, $y, $z); diff --git a/src/plugin/DummyPlugin.php b/src/plugin/DummyPlugin.php index 6ab9554a..8b5b4e51 100644 --- a/src/plugin/DummyPlugin.php +++ b/src/plugin/DummyPlugin.php @@ -1,13 +1,13 @@ $recipe) { + foreach(CraftingRecipes::$recipes as $id => $recipe){ $server->query("INSERT INTO recipes (id, type, recipe) VALUES (" . $id . ", " . $recipe[3] . ", '" . $recipe[2] . "');"); } } - private static function parseRecipe($recipe) { + private static function parseRecipe($recipe){ $recipe = explode("=>", $recipe); $recipeItems = []; - foreach(explode(",", $recipe[0]) as $item) { + foreach(explode(",", $recipe[0]) as $item){ $item = explode("x", $item); $id = explode(":", $item[0]); $meta = array_pop($id); $id = $id[0]; $it = BlockAPI::fromString($id); - if(!isset($recipeItems[$it->getID()])) { + if(!isset($recipeItems[$it->getID()])){ $recipeItems[$it->getID()] = [$it->getID(), $meta === "?" ? false : intval($meta) & 0xFFFF, intval($item[1])]; - } else { - if($it->getMetadata() !== $recipeItems[$it->getID()][1]) { + }else{ + if($it->getMetadata() !== $recipeItems[$it->getID()][1]){ $recipeItems[$it->getID()][1] = false; } $recipeItems[$it->getID()][2] += $it->count; @@ -256,7 +256,7 @@ class CraftingRecipes { $craftItem = [$it->getID(), intval($meta) & 0xFFFF, intval($item[1])]; $recipeString = ""; - foreach($recipeItems as $item) { + foreach($recipeItems as $item){ $recipeString .= $item[0] . "x" . $item[2] . ","; } $recipeString = substr($recipeString, 0, -1) . "=>" . $craftItem[0] . "x" . $craftItem[2]; @@ -264,11 +264,11 @@ class CraftingRecipes { return [$recipeItems, $craftItem, $recipeString]; } - public static function canCraft(array $craftItem, array $recipeItems, $type) { + public static function canCraft(array $craftItem, array $recipeItems, $type){ ksort($recipeItems); $recipeString = ""; - foreach($recipeItems as $item) { - if($craftItem[0] === CAKE && $item[0] === BUCKET && $item[1] === 1) { //some dark magic with recipe happened in mcpe, pmmp restores it back to normal + foreach($recipeItems as $item){ + if($craftItem[0] === CAKE && $item[0] === BUCKET && $item[1] === 1){ //some dark magic with recipe happened in mcpe, pmmp restores it back to normal $item[2] = 3; } $recipeString .= $item[0] . "x" . $item[2] . ","; @@ -276,33 +276,33 @@ class CraftingRecipes { $recipeString = substr($recipeString, 0, -1) . "=>" . $craftItem[0] . "x" . $craftItem[2]; $server = ServerAPI::request(); $result = $server->query("SELECT id FROM recipes WHERE type == " . $type . " AND recipe == '" . $recipeString . "';"); - if($result instanceof SQLite3Result) { + if($result instanceof SQLite3Result){ $continue = true; - while(($r = $result->fetchArray(SQLITE3_NUM)) !== false) { + while(($r = $result->fetchArray(SQLITE3_NUM)) !== false){ $continue = true; $recipe = CraftingRecipes::$recipes[$r[0]]; - foreach($recipe[0] as $item) { - if(!isset($recipeItems[$item[0]])) { + foreach($recipe[0] as $item){ + if(!isset($recipeItems[$item[0]])){ $continue = false; break; } $oitem = $recipeItems[$item[0]]; - if($craftItem[0] === CAKE && $oitem[0] === BUCKET && $item[1] === 1) { //some dark magic with recipe happened in mcpe, pmmp restores it back to normal x2 + if($craftItem[0] === CAKE && $oitem[0] === BUCKET && $item[1] === 1){ //some dark magic with recipe happened in mcpe, pmmp restores it back to normal x2 $oitem[2] = 3; } - if(($oitem[1] !== $item[1] and $item[1] !== false) or $oitem[2] !== $item[2]) { + if(($oitem[1] !== $item[1] and $item[1] !== false) or $oitem[2] !== $item[2]){ $continue = false; break; } } - if($continue === false or $craftItem[0] !== $recipe[1][0]) { + if($continue === false or $craftItem[0] !== $recipe[1][0]){ $continue = false; continue; } $continue = $recipe; break; } - } else { + }else{ return true; } return $continue; diff --git a/src/recipes/FuelData.php b/src/recipes/FuelData.php index d9025730..e0c61bab 100644 --- a/src/recipes/FuelData.php +++ b/src/recipes/FuelData.php @@ -1,6 +1,6 @@ 80, @@ -27,4 +27,4 @@ class FuelData { BUCKET => 1000, ]; -} +} \ No newline at end of file diff --git a/src/recipes/SmeltingData.php b/src/recipes/SmeltingData.php index 544abd5b..1dd447d3 100644 --- a/src/recipes/SmeltingData.php +++ b/src/recipes/SmeltingData.php @@ -1,6 +1,6 @@ [STONE, 0], @@ -17,4 +17,4 @@ class SmeltingData { RAW_CHICKEN => [COOKED_CHICKEN, 0], POTATO => [BAKED_POTATO, 0], ]; -} +} \ No newline at end of file diff --git a/src/utils/RailLogic.php b/src/utils/RailLogic.php index 1966b7c5..9e8de956 100644 --- a/src/utils/RailLogic.php +++ b/src/utils/RailLogic.php @@ -1,9 +1,8 @@ getID(); $meta = $rail->getMetadata(); $this->level = $rail->level; @@ -22,163 +21,125 @@ class RailLogic { $this->updateConnections($meta); } - public function getRail($v) { - if(($b = $this->level->getBlock($v)) instanceof RailBaseBlock) { //TODO git rid of getBlock + public function getRail($v){ + if(($b = $this->level->getBlock($v)) instanceof RailBaseBlock){ //TODO git rid of getBlock return new RailLogic($b); - } elseif(($b = $this->level->getBlockWithoutVector($v->x, $v->y + 1, $v->z)) instanceof RailBaseBlock) { + }elseif(($b = $this->level->getBlockWithoutVector($v->x, $v->y + 1, $v->z)) instanceof RailBaseBlock){ return new RailLogic($b); - } elseif(($b = $this->level->getBlockWithoutVector($v->x, $v->y - 1, $v->z)) instanceof RailBaseBlock) { + }elseif(($b = $this->level->getBlockWithoutVector($v->x, $v->y - 1, $v->z)) instanceof RailBaseBlock){ return new RailLogic($b); } return null; } - public function hasRail($x, $y, $z) { + public function hasRail($x, $y, $z){ return RailBaseBlock::isRailBlock($this->level, $x, $y, $z) || RailBaseBlock::isRailBlock($this->level, $x, $y + 1, $z) || RailBaseBlock::isRailBlock($this->level, $x, $y - 1, $z); } - public function countPotentialConnections() { + public function countPotentialConnections(){ return $this->hasRail($this->x, $this->y, $this->z - 1) + $this->hasRail($this->x, $this->y, $this->z + 1) + $this->hasRail($this->x - 1, $this->y, $this->z) + $this->hasRail($this->x + 1, $this->y, $this->z); } - public function removeSoftConnections() { - for($ind = 0; $ind < count($this->railPositions); ++$ind) { + public function removeSoftConnections(){ + for($ind = 0; $ind < count($this->railPositions); ++$ind){ $logic = $this->getRail($this->railPositions[$ind]); - if($logic != null && $this->connectsTo($logic)) { + if($logic != null && $this->connectsTo($logic)){ $this->railPositions[$ind] = new Vector3($logic->x, $logic->y, $logic->z); - } else { + }else{ unset($this->railPositions[$ind--]); $this->railPositions = array_values($this->railPositions); //reindexing } } } - public function connectsTo(RailLogic $logic) { - foreach($this->railPositions as $rpos) { - if(($rpos->x === $logic->x) && ($rpos->z === $logic->z)) { + public function connectsTo(RailLogic $logic){ + foreach($this->railPositions as $rpos){ + if(($rpos->x === $logic->x) && ($rpos->z === $logic->z)){ return true; } } return false; } - public function canConnectTo(RailLogic $rail) { + public function canConnectTo(RailLogic $rail){ return $this->connectsTo($rail) || !(count($this->railPositions) == 2); } - public function hasNeighborRail($x, $y, $z) { + public function hasNeighborRail($x, $y, $z){ $logic = $this->getRail(new Vector3($x, $y, $z)); - if($logic === null) { + if($logic === null){ return false; - } else { + }else{ $logic->removeSoftConnections(); //i place $this here and was searching why isnt it working correctly for 2 hours return $logic->canConnectTo($this); } } - public function place($b, $b1) { + public function place($b, $b1){ $hasZneg = $this->hasNeighborRail($this->x, $this->y, $this->z - 1); $hasZpos = $this->hasNeighborRail($this->x, $this->y, $this->z + 1); $hasXneg = $this->hasNeighborRail($this->x - 1, $this->y, $this->z); $hasXpos = $this->hasNeighborRail($this->x + 1, $this->y, $this->z); $state = -1; - if(($hasZneg || $hasZpos) && !($hasXneg && $hasXpos)) { + if(($hasZneg || $hasZpos) && !($hasXneg && $hasXpos)){ $state = 0; } - if(($hasXneg || $hasXpos) && !($hasZneg && $hasZpos)) { + if(($hasXneg || $hasXpos) && !($hasZneg && $hasZpos)){ $state = 1; } - if(!$this->isStraight) { - if($hasZpos && $hasXpos && !($hasZneg && $hasXneg)) { - $state = 6; - } - if($hasZpos && $hasXneg && !($hasZneg && $hasXpos)) { - $state = 7; - } - if($hasZneg && $hasXneg && !($hasZpos && $hasXpos)) { - $state = 8; - } - if($hasZneg && $hasXpos && !($hasZpos && $hasXneg)) { - $state = 9; - } + if(!$this->isStraight){ + if($hasZpos && $hasXpos && !($hasZneg && $hasXneg)) $state = 6; + if($hasZpos && $hasXneg && !($hasZneg && $hasXpos)) $state = 7; + if($hasZneg && $hasXneg && !($hasZpos && $hasXpos)) $state = 8; + if($hasZneg && $hasXpos && !($hasZpos && $hasXneg)) $state = 9; } - if($state === -1) { - if($hasZneg || $hasZpos) { - $state = 0; - } - if($hasXneg || $hasXpos) { - $state = 1; - } + if($state === -1){ + if($hasZneg || $hasZpos) $state = 0; + if($hasXneg || $hasXpos) $state = 1; - if(!$this->isStraight) { - if($b) { - if($hasZpos && $hasXpos) { - $state = 6; - } - if($hasXneg && $hasZpos) { - $state = 7; - } - if($hasXpos && $hasZneg) { - $state = 9; - } - if($hasZneg && $hasXneg) { - $state = 8; - } - } else { - if($hasZneg && $hasXneg) { - $state = 8; - } - if($hasXpos && $hasZneg) { - $state = 9; - } - if($hasXneg && $hasZpos) { - $state = 7; - } - if($hasZpos && $hasXpos) { - $state = 6; - } + if(!$this->isStraight){ + if($b){ + if($hasZpos && $hasXpos) $state = 6; + if($hasXneg && $hasZpos) $state = 7; + if($hasXpos && $hasZneg) $state = 9; + if($hasZneg && $hasXneg) $state = 8; + }else{ + if($hasZneg && $hasXneg) $state = 8; + if($hasXpos && $hasZneg) $state = 9; + if($hasXneg && $hasZpos) $state = 7; + if($hasZpos && $hasXpos) $state = 6; } } } - if($state === 0) { - if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z - 1)) { - $state = 4; - } - if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z + 1)) { - $state = 5; - } - } elseif($state === 1) { - if(RailBaseBlock::isRailBlock($this->level, $this->x + 1, $this->y + 1, $this->z)) { - $state = 2; - } - if(RailBaseBlock::isRailBlock($this->level, $this->x - 1, $this->y + 1, $this->z)) { - $state = 3; - } - } elseif($state < 0) { - $state = 0; - } + if($state === 0){ + if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z - 1)) $state = 4; + if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z + 1)) $state = 5; + }elseif($state === 1){ + if(RailBaseBlock::isRailBlock($this->level, $this->x + 1, $this->y + 1, $this->z)) $state = 2; + if(RailBaseBlock::isRailBlock($this->level, $this->x - 1, $this->y + 1, $this->z)) $state = 3; + }elseif($state < 0) $state = 0; $this->updateConnections($state); $meta = $state; - if($this->isStraight) { + if($this->isStraight){ $meta = $this->level->level->getBlockDamage($this->x, $this->x, $this->z) & 8 | $state; } - if($b1 || $this->level->level->getBlockDamage($this->x, $this->y, $this->z)) { + if($b1 || $this->level->level->getBlockDamage($this->x, $this->y, $this->z)){ //this.logicWorld.setBlockMetadataWithNotify(this.railX, this.railY, this.railZ, var8, 3); $bl = $this->level->getBlockWithoutVector($this->x, $this->y, $this->z); $bl->setMetadata($meta); $this->level->setBlock(new Vector3($this->x, $this->y, $this->z), $bl, true, false, true); - foreach($this->railPositions as $rpos) { + foreach($this->railPositions as $rpos){ $logic = $this->getRail($rpos); - if($logic !== null) { + if($logic !== null){ $logic->removeSoftConnections(); - if($logic->canConnectTo($this)) { + if($logic->canConnectTo($this)){ $logic->connectTo($this); } @@ -189,16 +150,14 @@ class RailLogic { } - public function hasConnection($x, $y, $z) { - foreach($this->railPositions as $rpos) { - if($rpos->x === $x && $rpos->z === $z) { - return true; - } + public function hasConnection($x, $y, $z){ + foreach($this->railPositions as $rpos){ + if($rpos->x === $x && $rpos->z === $z) return true; } return false; } - public function connectTo(RailLogic $logic) { + public function connectTo(RailLogic $logic){ $this->railPositions[] = new Vector3($logic->x, $logic->y, $logic->z); $hasZneg = $this->hasConnection($this->x, $this->y, $this->z - 1); @@ -207,49 +166,27 @@ class RailLogic { $hasXpos = $this->hasConnection($this->x + 1, $this->y, $this->z); $state = -1; - if($hasZneg || $hasZpos) { - $state = 0; - } - if($hasXneg || $hasXpos) { - $state = 1; + if($hasZneg || $hasZpos) $state = 0; + if($hasXneg || $hasXpos) $state = 1; + + if(!$this->isStraight){ + if($hasZpos && $hasXpos && !($hasZneg && $hasXneg)) $state = 6; + if($hasZpos && $hasXneg && !($hasZneg && $hasXpos)) $state = 7; + if($hasZneg && $hasXneg && !($hasZpos && $hasXpos)) $state = 8; + if($hasZneg && $hasXpos && !($hasZpos && $hasXneg)) $state = 9; } - if(!$this->isStraight) { - if($hasZpos && $hasXpos && !($hasZneg && $hasXneg)) { - $state = 6; - } - if($hasZpos && $hasXneg && !($hasZneg && $hasXpos)) { - $state = 7; - } - if($hasZneg && $hasXneg && !($hasZpos && $hasXpos)) { - $state = 8; - } - if($hasZneg && $hasXpos && !($hasZpos && $hasXneg)) { - $state = 9; - } - } - - if($state == 0) { - if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z - 1)) { - $state = 4; - } - if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z + 1)) { - $state = 5; - } - } elseif($state == 1) { - if(RailBaseBlock::isRailBlock($this->level, $this->x + 1, $this->y + 1, $this->z)) { - $state = 2; - } - if(RailBaseBlock::isRailBlock($this->level, $this->x - 1, $this->y + 1, $this->z)) { - $state = 3; - } - } elseif($state < 0) { - $state = 0; - } + if($state == 0){ + if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z - 1)) $state = 4; + if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z + 1)) $state = 5; + }elseif($state == 1){ + if(RailBaseBlock::isRailBlock($this->level, $this->x + 1, $this->y + 1, $this->z)) $state = 2; + if(RailBaseBlock::isRailBlock($this->level, $this->x - 1, $this->y + 1, $this->z)) $state = 3; + }elseif($state < 0) $state = 0; $meta = $state; - if($this->isStraight) { + if($this->isStraight){ $meta = $this->level->level->getBlockDamage($this->x, $this->x, $this->z) & 8 | $state; } @@ -259,11 +196,11 @@ class RailLogic { $this->level->fastSetBlockUpdateMeta($this->x, $this->y, $this->z, $meta, true); } - public function updateConnections($meta) { + public function updateConnections($meta){ $this->railPositions = []; - switch($meta) { + switch($meta){ case 0: $this->railPositions[] = new Vector3($this->x, $this->y, $this->z - 1); $this->railPositions[] = new Vector3($this->x, $this->y, $this->z + 1); @@ -307,3 +244,4 @@ class RailLogic { } } } + diff --git a/src/utils/Utils.php b/src/utils/Utils.php index 42a6f8b0..c3cb47f3 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -186,7 +186,7 @@ class Utils{ $arr = json_decode($json); $rarr = []; foreach ($arr as $key => $value){ - $rarr[] = $key . ": " . $value; + $rarr[] = $key.": ".$value; } return $rarr; } diff --git a/src/world/Level.php b/src/world/Level.php index 510c3485..200d91a9 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -1,6 +1,6 @@ true ]; - public function __construct(PMFLevel $level, Config $entities, Config $tiles, Config $blockUpdates, $name) { + public function __construct(PMFLevel $level, Config $entities, Config $tiles, Config $blockUpdates, $name){ $this->server = ServerAPI::request(); $this->level = $level; $this->level->level = $this; @@ -89,25 +69,21 @@ class Level { $this->randInt2 = 0x3C6EF35F; } - public function close() { + public function close(){ $this->__destruct(); } - public function isTopSolidBlocking($x, $y, $z) { + public function isTopSolidBlocking($x, $y, $z){ $idmeta = $this->level->getBlock($x, $y, $z); $id = $idmeta[0]; $meta = $idmeta[1]; - if($id == 0) { - return false; - } - if(StaticBlock::getIsTransparent($id)) { - return false; - } + if($id == 0) return false; + if(StaticBlock::getIsTransparent($id)) return false; return true; } - public function rayTraceBlocks(Vector3 $start, Vector3 $end) { + public function rayTraceBlocks(Vector3 $start, Vector3 $end){ $par3 = false; //TODO move to params? $par4 = true; @@ -124,74 +100,54 @@ class Level { //$block::updateShape($this, $xStart, $yStart, $zStart); //TODO better way to do it $aabb = $block::getAABB($this, $xStart, $yStart, $zStart); - if($startID > 0 && $aabb != null) { //TODO also block::canColideCheck + if($startID > 0 && $aabb != null){ //TODO also block::canColideCheck $v14 = $block::clip($this, $xStart, $yStart, $zStart, $start, $end); - if($v14 != null) { - return $v14; - } + if($v14 != null) return $v14; } - for($i = 0; $i <= 200; ++$i) { + for($i = 0; $i <= 200; ++$i){ - if(is_nan($start->x) || is_nan($start->y) || is_nan($start->z) || ($xStart == $xEnd && $yStart == $yEnd && $zStart == $zEnd)) { //also add checks for nan? + if(is_nan($start->x) || is_nan($start->y) || is_nan($start->z) || ($xStart == $xEnd && $yStart == $yEnd && $zStart == $zEnd)){ //also add checks for nan? return null; } $v39 = $v40 = $v41 = true; $v15 = $v17 = $v19 = 999.0; //nice mojang - if($xEnd > $xStart) { - $v15 = $xStart + 1; - } elseif($xEnd < $xStart) { - $v15 = $xStart; - } else { - $v39 = false; - } + if($xEnd > $xStart) $v15 = $xStart + 1; + elseif($xEnd < $xStart) $v15 = $xStart; + else $v39 = false; - if($yEnd > $yStart) { - $v17 = $yStart + 1; - } elseif($yEnd < $yStart) { - $v17 = $yStart; - } else { - $v40 = false; - } + if($yEnd > $yStart) $v17 = $yStart + 1; + elseif($yEnd < $yStart) $v17 = $yStart; + else $v40 = false; - if($zEnd > $zStart) { - $v19 = $zStart + 1; - } elseif($zEnd < $zStart) { - $v19 = $zStart; - } else { - $v41 = false; - } + if($zEnd > $zStart) $v19 = $zStart + 1; + elseif($zEnd < $zStart) $v19 = $zStart; + else $v41 = false; $v21 = $v23 = $v25 = 999.0; //nice mojang x2 $v27 = $end->x - $start->x; $v29 = $end->y - $start->y; $v31 = $end->z - $start->z; - if($v39) { - $v21 = $v27 == 0 ? ($v15 - $start->x) * INF : ($v15 - $start->x) / $v27; - } - if($v40) { - $v23 = $v29 == 0 ? ($v17 - $start->y) * INF : ($v17 - $start->y) / $v29; - } - if($v41) { - $v25 = $v31 == 0 ? ($v19 - $start->z) * INF : ($v19 - $start->z) / $v31; - } + if($v39) $v21 = $v27 == 0 ? ($v15 - $start->x) * INF : ($v15 - $start->x) / $v27; + if($v40) $v23 = $v29 == 0 ? ($v17 - $start->y) * INF : ($v17 - $start->y) / $v29; + if($v41) $v25 = $v31 == 0 ? ($v19 - $start->z) * INF : ($v19 - $start->z) / $v31; - if($v21 < $v23 && $v21 < $v25) { + if($v21 < $v23 && $v21 < $v25){ $v42 = $xEnd > $xStart ? 4 : 5; $start->x = $v15; $start->y += $v29 * $v21; $start->z += $v31 * $v21; - } elseif($v23 < $v25) { + }elseif($v23 < $v25){ $v42 = $yEnd > $yStart ? 0 : 1; $start->x += $v27 * $v23; $start->y = $v17; $start->z += $v31 * $v23; - } else { + }else{ $v42 = $zEnd > $zStart ? 2 : 3; $start->x += $v27 * $v25; @@ -200,36 +156,28 @@ class Level { } $xStart = floor($start->x); - if($v42 == 5) { - --$xStart; - } + if($v42 == 5) --$xStart; $yStart = floor($start->y); - if($v42 == 1) { - --$yStart; - } + if($v42 == 1) --$yStart; $zStart = floor($start->z); - if($v42 == 3) { - --$zStart; - } + if($v42 == 3) --$zStart; [$blockID, $blockMeta] = $this->level->getBlock($xStart, $yStart, $zStart); $block = StaticBlock::getBlock($blockID); $aabb = $block::getAABB($this, $xStart, $yStart, $zStart); - if($blockID > 0 && $aabb != null) { //TODO also block::canColideCheck + if($blockID > 0 && $aabb != null){ //TODO also block::canColideCheck $v38 = $block::clip($this, $xStart, $yStart, $zStart, $start, $end); - if($v38 != null) { - return $v38; - } + if($v38 != null) return $v38; } } return null; } - public function isLavaInBB($aabb) { + public function isLavaInBB($aabb){ $minX = floor($aabb->minX); $maxX = floor($aabb->maxX + 1); $minY = floor($aabb->minY); @@ -237,14 +185,12 @@ class Level { $minZ = floor($aabb->minZ); $maxZ = floor($aabb->maxZ + 1); - for($x = $minX; $x < $maxX; ++$x) { - for($y = $minY; $y < $maxY; ++$y) { - for($z = $minZ; $z < $maxZ; ++$z) { + for($x = $minX; $x < $maxX; ++$x){ + for($y = $minY; $y < $maxY; ++$y){ + for($z = $minZ; $z < $maxZ; ++$z){ $blockId = $this->level->getBlockID($x, $y, $z); - if($blockId == LAVA || $blockId == STILL_LAVA) { - return true; - } + if($blockId == LAVA || $blockId == STILL_LAVA) return true; } } } @@ -252,7 +198,7 @@ class Level { return false; } - public function handleMaterialAcceleration(AxisAlignedBB $aabb, $materialType, Entity $entity) { + public function handleMaterialAcceleration(AxisAlignedBB $aabb, $materialType, Entity $entity){ $minX = floor($aabb->minX); $maxX = ceil($aabb->maxX); $minY = floor($aabb->minY); @@ -264,13 +210,13 @@ class Level { $appliedVelocity = false; $velocityVec = new Vector3(0, 0, 0); - for($x = $minX; $x < $maxX; ++$x) { - for($y = $minY; $y < $maxY; ++$y) { - for($z = $minZ; $z < $maxZ; ++$z) { + for($x = $minX; $x < $maxX; ++$x){ + for($y = $minY; $y < $maxY; ++$y){ + for($z = $minZ; $z < $maxZ; ++$z){ [$block, $meta] = $this->level->getBlock($x, $y, $z); - if(($materialType == 0 && ($block == WATER || $block == STILL_WATER)) || ($materialType == 1 && ($block == LAVA || $block == STILL_LAVA))) { //TODO better material system + if(($materialType == 0 && ($block == WATER || $block == STILL_WATER)) || ($materialType == 1 && ($block == LAVA || $block == STILL_LAVA))){ //TODO better material system $v16 = ($y + 1) - LiquidBlock::getPercentAir($meta); - if($maxY >= $v16) { + if($maxY >= $v16){ $appliedVelocity = true; StaticBlock::$prealloc[$block]::addVelocityToEntity($this, $x, $y, $z, $entity, $velocityVec); } @@ -280,7 +226,7 @@ class Level { } } - if($velocityVec->length() > 0) { //also checks is player flying + if($velocityVec->length() > 0){ //also checks is player flying $velocityVec = $velocityVec->normalize(); //TODO do not use vec methods $v18 = 0.014; $entity->speedX += $velocityVec->x * $v18; @@ -306,13 +252,11 @@ class Level { for($z = $z0; $z < $z1; ++$z) { for($y = $y0 - 1; $y < $y1; ++$y) { $bid = $this->level->getBlockID($x, $y, $z); - if($bid > 0) { + if($bid > 0){ $blockBounds = StaticBlock::$prealloc[$bid]::getCollisionBoundingBoxes($this, $x, $y, $z, $e); //StaticBlock::getBoundingBoxForBlockCoords($b, $x, $y, $z); - foreach($blockBounds as $blockBound) { - if($aABB->intersectsWith($blockBound)) { - $aABBs[] = $blockBound; - } + foreach($blockBounds as $blockBound){ + if($aABB->intersectsWith($blockBound)) $aABBs[] = $blockBound; } } } @@ -322,8 +266,8 @@ class Level { return $aABBs; } - public function __destruct() { - if(isset($this->level)) { + public function __destruct(){ + if(isset($this->level)){ $this->save(false, false, false, false); $this->level->close(); unset($this->level); @@ -331,46 +275,46 @@ class Level { unset($this->mobSpawner->level); } - public function isDay() { + public function isDay(){ return $this->getTime() % 19200 < TimeAPI::$phases["sunset"]; } - public function isNight() { + public function isNight(){ $t = $this->getTime() % 19200; return $t < TimeAPI::$phases["sunrise"] && $t > TimeAPI::$phases["sunset"]; } - public function save($force = false, $entities = true, $tiles = true, $blockupdates = true) { - if(!isset($this->level)) { + public function save($force = false, $entities = true, $tiles = true, $blockupdates = true){ + if(!isset($this->level)){ return false; } - if($this->server->saveEnabled === false and $force === false) { + if($this->server->saveEnabled === false and $force === false){ return; } - if($entities) { + if($entities){ $entities = []; - foreach($this->entityList as $entity) { - if($entity instanceof Entity) { + foreach($this->entityList as $entity){ + if($entity instanceof Entity){ $entities[] = $entity->createSaveData(); } } $this->entities->setAll($entities); $this->entities->save(); } - if($tiles) { + if($tiles){ $tiles = []; - foreach($this->server->api->tile->getAll($this) as $tile) { + foreach($this->server->api->tile->getAll($this) as $tile){ $tiles[] = $tile->data; } $this->tiles->setAll($tiles); $this->tiles->save(); } - if($blockupdates) { + if($blockupdates){ $blockUpdates = []; $updates = $this->server->query("SELECT x,y,z,type,delay FROM blockUpdates WHERE level = '" . $this->getName() . "';"); - if($updates !== false and $updates !== true) { + if($updates !== false and $updates !== true){ $timeu = microtime(true); - while(($bupdate = $updates->fetchArray(SQLITE3_ASSOC)) !== false) { + while(($bupdate = $updates->fetchArray(SQLITE3_ASSOC)) !== false){ $bupdate["delay"] = max(1, ($bupdate["delay"] - $timeu) * 20); $blockUpdates[] = $bupdate; } @@ -386,77 +330,75 @@ class Level { $this->nextSave = microtime(true) + 45; } - public function getName() { + public function getName(){ return $this->name;//return $this->level->getData("name"); } - public function useChunk($X, $Z, Player $player) { - if(!isset($this->usedChunks[$X . "." . $Z])) { + public function useChunk($X, $Z, Player $player){ + if(!isset($this->usedChunks[$X . "." . $Z])){ $this->usedChunks[$X . "." . $Z] = []; } $this->usedChunks[$X . "." . $Z][$player->CID] = true; - if(isset($this->level)) { + if(isset($this->level)){ $this->level->loadChunk($X, $Z); } } - public function freeAllChunks(Player $player) { - foreach($this->usedChunks as $i => $c) { + public function freeAllChunks(Player $player){ + foreach($this->usedChunks as $i => $c){ unset($this->usedChunks[$i][$player->CID]); } } - public function freeChunk($X, $Z, Player $player) { + public function freeChunk($X, $Z, Player $player){ unset($this->usedChunks[$X . "." . $Z][$player->CID]); } - public function checkCollisionsFor(Entity $e) { - if($e->level->getName() != $this->getName()) { + public function checkCollisionsFor(Entity $e){ + if($e->level->getName() != $this->getName()){ return false; //not the same world } - foreach($this->entityList as $e1) { - if($e->boundingBox->intersectsWith($e1->boundingBox) && $e1->isCollidable) { + foreach($this->entityList as $e1){ + if($e->boundingBox->intersectsWith($e1->boundingBox) && $e1->isCollidable){ $e->onCollideWith($e1); $e1->onCollideWith($e); } } } - public function isObstructed($e) { + public function isObstructed($e){ } - public function checkSleep() { //TODO events? - if(count($this->players) == 0) { - return false; - } - if($this->server->api->time->getPhase($this) === "night") { //TODO vanilla - foreach($this->players as $p) { - if(!$p->isSleeping || $p->sleepingTime < 100) { + public function checkSleep(){ //TODO events? + if(count($this->players) == 0) return false; + if($this->server->api->time->getPhase($this) === "night"){ //TODO vanilla + foreach($this->players as $p){ + if(!$p->isSleeping || $p->sleepingTime < 100){ return false; } } $this->server->api->time->set("day", $this); } - foreach($this->players as $p) { + foreach($this->players as $p){ $p->stopSleep(); } } - public function checkThings() { - if(!isset($this->level)) { + public function checkThings(){ + if(!isset($this->level)){ return false; } $now = microtime(true); $this->players = $this->server->api->player->getAll($this); - if(count($this->changedCount) > 0) { + if(count($this->changedCount) > 0){ arsort($this->changedCount); $reorder = false; - foreach($this->changedCount as $index => $count) { - if($count < 582) {//Optimal value, calculated using the relation between minichunks and single packets + foreach($this->changedCount as $index => $count){ + if($count < 582){//Optimal value, calculated using the relation between minichunks and single packets break; } - foreach($this->players as $p) { + foreach($this->players as $p){ unset($p->chunksLoaded[$index]); } $reorder = true; @@ -464,15 +406,15 @@ class Level { } $this->changedCount = []; - if($reorder) { - foreach($this->players as $p) { + if($reorder){ + foreach($this->players as $p){ $p->orderChunks(); } } - if(count($this->changedBlocks) > 0) { - foreach($this->changedBlocks as $i => $blocks) { - foreach($blocks as $b) { + if(count($this->changedBlocks) > 0){ + foreach($this->changedBlocks as $i => $blocks){ + foreach($blocks as $b){ $pk = new UpdateBlockPacket; $pk->x = ($b >> 32) & 0xff; $pk->y = ($b >> 24) & 0xff; @@ -487,33 +429,33 @@ class Level { } } - if($this->nextSave < $now) { + if($this->nextSave < $now){ $this->save(false, true, true, true); } } - public function isSpawnChunk($X, $Z) { + public function isSpawnChunk($X, $Z){ $spawnX = $this->level->getData("spawnX") >> 4; $spawnZ = $this->level->getData("spawnZ") >> 4; return abs($X - $spawnX) <= 1 and abs($Z - $spawnZ) <= 1; } - public function getBlockRaw(Vector3 $pos) { + public function getBlockRaw(Vector3 $pos){ $b = $this->level->getBlock($pos->x, $pos->y, $pos->z); return BlockAPI::get($b[0], $b[1], new Position($pos->x, $pos->y, $pos->z, $this)); } - public function setBlockRaw(Vector3 $pos, Block $block, $direct = true, $send = true) { - if(($ret = $this->level->setBlock($pos->x, $pos->y, $pos->z, $block->getID(), $block->getMetadata())) === true and $send !== false) { - if($direct === true) { + public function setBlockRaw(Vector3 $pos, Block $block, $direct = true, $send = true){ + if(($ret = $this->level->setBlock($pos->x, $pos->y, $pos->z, $block->getID(), $block->getMetadata())) === true and $send !== false){ + if($direct === true){ $this->addBlockToSendQueue($pos->x, $pos->y, $pos->z, $block->id, $block->meta); - } elseif($direct === false) { - if(!($pos instanceof Position)) { + }elseif($direct === false){ + if(!($pos instanceof Position)){ $pos = new Position($pos->x, $pos->y, $pos->z, $this); } $block->position($pos); $i = ($pos->x >> 4) . ":" . ($pos->y >> 4) . ":" . ($pos->z >> 4); - if(!isset($this->changedBlocks[$i])) { + if(!isset($this->changedBlocks[$i])){ $this->changedBlocks[$i] = []; $this->changedCount[$i] = 0; } @@ -523,75 +465,59 @@ class Level { } return $ret; } - public function fastSetBlockUpdateMeta($x, $y, $z, $meta, $updateBlock = false) { + public function fastSetBlockUpdateMeta($x, $y, $z, $meta, $updateBlock = false){ $this->level->setBlockDamage($x, $y, $z, $meta); $id = $this->level->getBlockID($x, $y, $z); $this->addBlockToSendQueue($x, $y, $z, $id, $meta); - if($updateBlock) { + if($updateBlock){ $this->updateNeighborsAt($x, $y, $z, $id); } } - public function updateNeighborsAt($x, $y, $z, $oldID) { + public function updateNeighborsAt($x, $y, $z, $oldID){ $block = $this->level->getBlockID($x - 1, $y, $z); - if($block) { - StaticBlock::getBlock($block)::neighborChanged($this, $x - 1, $y, $z, $x, $y, $z, $oldID); - } + if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x - 1, $y, $z, $x, $y, $z, $oldID); $block = $this->level->getBlockID($x + 1, $y, $z); - if($block) { - StaticBlock::getBlock($block)::neighborChanged($this, $x + 1, $y, $z, $x, $y, $z, $oldID); - } + if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x + 1, $y, $z, $x, $y, $z, $oldID); $block = $this->level->getBlockID($x, $y - 1, $z); - if($block) { - StaticBlock::getBlock($block)::neighborChanged($this, $x, $y - 1, $z, $x, $y, $z, $oldID); - } + if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x, $y - 1, $z, $x, $y, $z, $oldID); $block = $this->level->getBlockID($x, $y + 1, $z); - if($block) { - StaticBlock::getBlock($block)::neighborChanged($this, $x, $y + 1, $z, $x, $y, $z, $oldID); - } + if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x, $y + 1, $z, $x, $y, $z, $oldID); $block = $this->level->getBlockID($x, $y, $z - 1); - if($block) { - StaticBlock::getBlock($block)::neighborChanged($this, $x, $y, $z - 1, $x, $y, $z, $oldID); - } + if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x, $y, $z - 1, $x, $y, $z, $oldID); $block = $this->level->getBlockID($x, $y, $z + 1); - if($block) { - StaticBlock::getBlock($block)::neighborChanged($this, $x, $y, $z + 1, $x, $y, $z, $oldID); - } + if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x, $y, $z + 1, $x, $y, $z, $oldID); } - public function fastSetBlockUpdate($x, $y, $z, $id, $meta, $updateBlocksAround = false, $tiles = false) { + public function fastSetBlockUpdate($x, $y, $z, $id, $meta, $updateBlocksAround = false, $tiles = false){ $oldID = $this->level->getBlockID($x, $y, $z); $this->level->setBlock($x, $y, $z, $id, $meta); - if($tiles) { //TODO rewrite + if($tiles){ //TODO rewrite $this->server->api->tile->invalidateAll($this, $x, $y, $z); } - if($updateBlocksAround) { + if($updateBlocksAround){ self::updateNeighborsAt($x, $y, $z, $oldID); } $this->addBlockToSendQueue($x, $y, $z, $id, $meta); } - public function onTick(PocketMinecraftServer $server, $currentTime) { - if(!$this->stopTime) { - ++$this->time; - } - for($cX = 0; $cX < 16; ++$cX) { - for($cZ = 0; $cZ < 16; ++$cZ) { + public function onTick(PocketMinecraftServer $server, $currentTime){ + if(!$this->stopTime) ++$this->time; + for($cX = 0; $cX < 16; ++$cX){ + for($cZ = 0; $cZ < 16; ++$cZ){ $index = $this->level->getIndex($cX, $cZ); - if(!isset($this->level->chunks[$index]) || $this->level->chunks[$index] === false) { - continue; - } - for($c = 0; $c <= 20; ++$c) { + if(!isset($this->level->chunks[$index]) || $this->level->chunks[$index] === false) continue; + for($c = 0; $c <= 20; ++$c){ $xyz = mt_rand(0, 0xffffffff) >> 2; $x = $xyz & 0xf; $z = ($xyz >> 8) & 0xf; $y = ($xyz >> 16) & 0x7f; $id = $this->level->fastGetBlockID($cX, $y >> 4, $cZ, $x, $y & 0xf, $z, $index); - if(isset(self::$randomUpdateBlocks[$id])) { + if(isset(self::$randomUpdateBlocks[$id])){ $cl = StaticBlock::$prealloc[$id]; $cl::onRandomTick($this, ($cX << 4) + $x, $y, $z + ($cZ << 4)); } @@ -601,8 +527,8 @@ class Level { $this->totalMobsAmount = 0; $post = []; - foreach($this->entityList as $k => $e) { - if(!($e instanceof Entity)) { + foreach($this->entityList as $k => $e){ + if(!($e instanceof Entity)){ unset($this->entityList[$k]); unset($this->server->entities[$k]); //TODO try to remove from $entityListPositioned? @@ -610,7 +536,7 @@ class Level { } $dd = CORRECT_ENTITY_CLASSES[$e->class] ?? false; - if($dd === false || ($dd !== true && !isset($dd[$e->type]))) { + if($dd === false || ($dd !== true && !isset($dd[$e->type]))){ ConsoleAPI::warn("Entity $k has invalid entity! {$e->class} {$e->type}"); $e->close(); @@ -621,7 +547,7 @@ class Level { $curChunkZ = (int) $e->z >> 4; $index = "$curChunkX $curChunkZ"; - if(isset($this->entityListPositioned[$index][$k])) { + if(isset($this->entityListPositioned[$index][$k])){ unset($this->entityListPositioned[$index][$k]); } @@ -629,70 +555,68 @@ class Level { } $curChunkX = (int) $e->x >> 4; $curChunkZ = (int) $e->z >> 4; - if($e->class === ENTITY_MOB && !$e->isPlayer()) { + if($e->class === ENTITY_MOB && !$e->isPlayer()){ ++$this->totalMobsAmount; } - if($e->isPlayer() || $e->needsUpdate) { + if($e->isPlayer() || $e->needsUpdate){ $e->update($currentTime); - if(!$e->isPlayer()) { - $post[] = $k; - } + if(!$e->isPlayer()) $post[] = $k; } - if($e instanceof Entity) { + if($e instanceof Entity){ $newChunkX = (int) $e->x >> 4; $newChunkZ = (int) $e->z >> 4; - if($e->chunkX != $newChunkX || $e->chunkZ != $newChunkZ) { + if($e->chunkX != $newChunkX || $e->chunkZ != $newChunkZ){ $oldIndex = "{$e->chunkX} {$e->chunkZ}"; unset($this->entityListPositioned[$oldIndex][$e->eid]); - if($e->level == $this) { + if($e->level == $this){ $e->chunkX = $newChunkX; $e->chunkZ = $newChunkZ; $newIndex = "$newChunkX $newChunkZ"; $this->entityListPositioned[$newIndex][$e->eid] = $e->eid; } } - if($e->level != $this && isset($this->entityListPositioned["$curChunkX $curChunkZ"])) { + if($e->level != $this && isset($this->entityListPositioned["$curChunkX $curChunkZ"])){ unset($this->entityListPositioned["$curChunkX $curChunkZ"][$e->eid]); - } elseif($curChunkX != $newChunkX || $curChunkZ != $newChunkZ) { + }elseif($curChunkX != $newChunkX || $curChunkZ != $newChunkZ){ $index = "$curChunkX $curChunkZ"; //while creating index like $curChunkX << 32 | $curChunkZ is faster, placing it inside list is slow $newIndex = "$newChunkX $newChunkZ"; unset($this->entityListPositioned[$index][$e->eid]); $this->entityListPositioned[$newIndex][$e->eid] = $e->eid; //set to e->eid to avoid possible memory leaks } - if($e->searchForClosestPlayers) { + if($e->searchForClosestPlayers){ $e->handlePrePlayerSearcher(); - foreach($this->players as $player) { + foreach($this->players as $player){ $dist = ($e->x - $player->entity->x) * ($e->x - $player->entity->x) + ($e->y - $player->entity->y) * ($e->y - $player->entity->y) + ($e->z - $player->entity->z) * ($e->z - $player->entity->z); $e->handlePlayerSearcher($player, $dist); } } - } elseif(isset($this->entityListPositioned["$curChunkX $curChunkZ"])) { + }elseif(isset($this->entityListPositioned["$curChunkX $curChunkZ"])){ unset($this->entityListPositioned["$curChunkX $curChunkZ"][$k]); } } $this->checkSleep(); - if($server->ticks % 100 === 0) { + if($server->ticks % 100 === 0){ $this->mobSpawner->handle(); } - foreach($this->players as $player) { - foreach($post as $eid) { + foreach($this->players as $player){ + foreach($post as $eid){ $e = $this->entityList[$eid] ?? false; - if(!($e instanceof Entity)) { + if(!($e instanceof Entity)){ continue; } $player->addEntityMovementUpdateToQueue($e); } $player->sendEntityMovementUpdateQueue(); - foreach($this->queuedBlockUpdates as $ind => $update) { + foreach($this->queuedBlockUpdates as $ind => $update){ $x = $update[0]; $y = $update[1]; $z = $update[2]; @@ -707,7 +631,7 @@ class Level { $this->queuedBlockUpdates = []; } - public function isBoundingBoxOnFire(AxisAlignedBB $bb) { + public function isBoundingBoxOnFire(AxisAlignedBB $bb){ $minX = floor($bb->minX); $maxX = floor($bb->maxX + 1); $minY = floor($bb->minY); @@ -715,32 +639,30 @@ class Level { $minZ = floor($bb->minZ); $maxZ = floor($bb->maxZ + 1); - for($x = $minX; $x < $maxX; ++$x) { - for($y = $minY; $y < $maxY; ++$y) { - for($z = $minZ; $z < $maxZ; ++$z) { + for($x = $minX; $x < $maxX; ++$x){ + for($y = $minY; $y < $maxY; ++$y){ + for($z = $minZ; $z < $maxZ; ++$z){ $blockAt = $this->level->getBlockID($x, $y, $z); - if($blockAt == FIRE || $blockAt == STILL_LAVA || $blockAt == LAVA) { - return true; - } + if($blockAt == FIRE || $blockAt == STILL_LAVA || $blockAt == LAVA) return true; } } } return false; } - public function getEntitiesInAABBOfType(AxisAlignedBB $bb, $class) { + public function getEntitiesInAABBOfType(AxisAlignedBB $bb, $class){ $minChunkX = ((int) ($bb->minX)) >> 4; $minChunkZ = ((int) ($bb->minZ)) >> 4; $maxChunkX = ((int) ($bb->maxX)) >> 4; $maxChunkZ = ((int) ($bb->maxZ)) >> 4; $ents = []; - for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX) { - for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ) { + for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ + for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){ $ind = "$chunkX $chunkZ"; - foreach($this->entityListPositioned[$ind] ?? [] as $ind2 => $entid) { - if(isset($this->entityList[$entid]) && $this->entityList[$entid] instanceof Entity && $this->entityList[$entid]->class === $class && $this->entityList[$entid]->boundingBox->intersectsWith($bb)) { + foreach($this->entityListPositioned[$ind] ?? [] as $ind2 => $entid){ + if(isset($this->entityList[$entid]) && $this->entityList[$entid] instanceof Entity && $this->entityList[$entid]->class === $class && $this->entityList[$entid]->boundingBox->intersectsWith($bb)){ $ents[$entid] = $this->entityList[$entid]; - } elseif(!isset($this->entityList[$entid])) { + }elseif(!isset($this->entityList[$entid])){ ConsoleAPI::debug("Removing entity from level array at index $ind/$ind2: $entid"); unset($this->entityListPositioned[$ind][$ind2]); } @@ -752,20 +674,20 @@ class Level { /** * @return Entity[] */ - public function getEntitiesInAABB(AxisAlignedBB $bb) { + public function getEntitiesInAABB(AxisAlignedBB $bb){ $minChunkX = ((int) ($bb->minX)) >> 4; $minChunkZ = ((int) ($bb->minZ)) >> 4; $maxChunkX = ((int) ($bb->maxX)) >> 4; $maxChunkZ = ((int) ($bb->maxZ)) >> 4; $ents = []; //TODO also index by chunkY? - for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX) { - for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ) { + for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ + for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){ $ind = "$chunkX $chunkZ"; - foreach($this->entityListPositioned[$ind] ?? [] as $ind2 => $entid) { - if(isset($this->entityList[$entid]) && $this->entityList[$entid] instanceof Entity && $this->entityList[$entid]->boundingBox->intersectsWith($bb)) { + foreach($this->entityListPositioned[$ind] ?? [] as $ind2 => $entid){ + if(isset($this->entityList[$entid]) && $this->entityList[$entid] instanceof Entity && $this->entityList[$entid]->boundingBox->intersectsWith($bb)){ $ents[$entid] = $this->entityList[$entid]; - } elseif(!isset($this->entityList[$entid])) { + }elseif(!isset($this->entityList[$entid])){ ConsoleAPI::debug("Removing entity from level array at index $ind/$ind2: $entid"); unset($this->entityListPositioned[$ind][$ind2]); } @@ -775,28 +697,28 @@ class Level { return $ents; } - public function addBlockToSendQueue($x, $y, $z, $id, $meta) { - if(!$this->forceDisableBlockQueue) { + public function addBlockToSendQueue($x, $y, $z, $id, $meta){ + if(!$this->forceDisableBlockQueue){ $this->queuedBlockUpdates["$x $y $z"] = [$x, $y, $z, $id, $meta]; } } - public function setBlock(Vector3 $pos, Block $block, $update = true, $tiles = false, $direct = false) { - if(!isset($this->level) or (($pos instanceof Position) and $pos->level !== $this) or $pos->x < 0 or $pos->y < 0 or $pos->z < 0) { + public function setBlock(Vector3 $pos, Block $block, $update = true, $tiles = false, $direct = false){ + if(!isset($this->level) or (($pos instanceof Position) and $pos->level !== $this) or $pos->x < 0 or $pos->y < 0 or $pos->z < 0){ return false; } $oldID = $this->level->getBlockID($pos->x, $pos->y, $pos->z); $ret = $this->level->setBlock($pos->x, $pos->y, $pos->z, $block->getID(), $block->getMetadata()); - if($ret === true) { - if(!($pos instanceof Position)) { + if($ret === true){ + if(!($pos instanceof Position)){ $pos = new Position($pos->x, $pos->y, $pos->z, $this); } $block->position($pos); - if($direct === true) { + if($direct === true){ $this->addBlockToSendQueue($pos->x, $pos->y, $pos->z, $block->id, $block->meta); - } else { + }else{ $i = ($pos->x >> 4) . ":" . ($pos->y >> 4) . ":" . ($pos->z >> 4); - if(!isset($this->changedBlocks[$i])) { + if(!isset($this->changedBlocks[$i])){ $this->changedBlocks[$i] = []; $this->changedCount[$i] = 0; } @@ -804,11 +726,11 @@ class Level { ++$this->changedCount[$i]; } - if($tiles === true) { + if($tiles === true){ $this->server->api->tile->invalidateAll($this, $pos->x, $pos->y, $pos->z); } - if($update === true) { + if($update === true){ $this->updateNeighborsAt($pos->x, $pos->y, $pos->z, $oldID); } @@ -816,105 +738,105 @@ class Level { return $ret; } - public function getMiniChunk($X, $Z, $Y) { - if(!isset($this->level)) { + public function getMiniChunk($X, $Z, $Y){ + if(!isset($this->level)){ return false; } return $this->level->getMiniChunk($X, $Z, $Y); } - public function setMiniChunk($X, $Z, $Y, $data) { - if(!isset($this->level)) { + public function setMiniChunk($X, $Z, $Y, $data){ + if(!isset($this->level)){ return false; } $this->changedCount[$X . ":" . $Y . ":" . $Z] = 4096; return $this->level->setMiniChunk($X, $Z, $Y, $data); } - public function loadChunk($X, $Z) { - if(!isset($this->level)) { + public function loadChunk($X, $Z){ + if(!isset($this->level)){ return false; } return $this->level->loadChunk($X, $Z); } - public function unloadChunk($X, $Z, $force = false) { - if(!isset($this->level)) { + public function unloadChunk($X, $Z, $force = false){ + if(!isset($this->level)){ return false; } - if($force !== true and $this->isSpawnChunk($X, $Z)) { + if($force !== true and $this->isSpawnChunk($X, $Z)){ return false; } return $this->level->unloadChunk($X, $Z, $this->server->saveEnabled); } - public function getOrderedChunk($X, $Z, $Yndex) { - if(!isset($this->level)) { + public function getOrderedChunk($X, $Z, $Yndex){ + if(!isset($this->level)){ return false; } $raw = []; - for($Y = 0; $Y < 8; ++$Y) { - if(($Yndex & (1 << $Y)) > 0) { + for($Y = 0; $Y < 8; ++$Y){ + if(($Yndex & (1 << $Y)) > 0){ $raw[$Y] = $this->level->getMiniChunk($X, $Z, $Y); } } $ordered = ""; $flag = chr($Yndex); - for($j = 0; $j < 256; ++$j) { + for($j = 0; $j < 256; ++$j){ $ordered .= $flag; - foreach($raw as $mini) { + foreach($raw as $mini){ $ordered .= substr($mini, $j << 5, 24); //16 + 8 } } return $ordered; } - public function getOrderedMiniChunk($X, $Z, $Y) { - if(!isset($this->level)) { + public function getOrderedMiniChunk($X, $Z, $Y){ + if(!isset($this->level)){ return false; } $raw = $this->level->getMiniChunk($X, $Z, $Y); $ordered = ""; $flag = chr(1 << $Y); - for($j = 0; $j < 256; ++$j) { + for($j = 0; $j < 256; ++$j){ $ordered .= $flag . substr($raw, $j << 5, 24); //16 + 8 } return $ordered; } - public function getSafeSpawn($spawn = false) { - if($spawn === false) { + public function getSafeSpawn($spawn = false){ + if($spawn === false){ $spawn = $this->getSpawn(); } - if($spawn instanceof Vector3) { + if($spawn instanceof Vector3){ $x = (int) round($spawn->x); $fy = $y = (int) round($spawn->y); $z = (int) round($spawn->z); - if($x < 0 || $x > 255 || $z < 0 || $z > 255) { + if($x < 0 || $x > 255 || $z < 0 || $z > 255){ return new Position($x, 128, $z, $this); } - for(; $y > 0; --$y) { + for(; $y > 0; --$y){ $v = new Vector3($x, $y, $z); $b = $this->getBlock($v->getSide(0)); - if($b === false) { + if($b === false){ return $spawn; - } elseif(!($b instanceof AirBlock)) { + }elseif(!($b instanceof AirBlock)){ break; } } - if($y == 0) { + if($y == 0){ return new Position($x, $fy, $z, $this); //force tp to default if pos is empty } - for(; $y < 128; ++$y) { + for(; $y < 128; ++$y){ $v = new Vector3($x, $y, $z); - if($this->getBlock($v->getSide(1)) instanceof AirBlock) { - if($this->getBlock($v) instanceof AirBlock) { + if($this->getBlock($v->getSide(1)) instanceof AirBlock){ + if($this->getBlock($v) instanceof AirBlock){ return new Position($x, $y, $z, $this); } - } else { + }else{ ++$y; } } @@ -923,8 +845,8 @@ class Level { return false; } - public function getSpawn() { - if(!isset($this->level)) { + public function getSpawn(){ + if(!isset($this->level)){ return false; } return new Position($this->level->getData("spawnX"), $this->level->getData("spawnY"), $this->level->getData("spawnZ"), $this); @@ -938,7 +860,7 @@ class Level { * @return GenericBlock | false if failed */ - public function getBlockWithoutVector($x, $y, $z, $positionfy = true) { + public function getBlockWithoutVector($x, $y, $z, $positionfy = true){ $b = $this->level->getBlock((int) $x, (int) $y, (int) $z); return BlockAPI::get($b[0], $b[1], $positionfy ? new Position($x, $y, $z, $this) : false); } @@ -947,15 +869,15 @@ class Level { * Recommended to use {@link getBlockWithoutVector()} if you dont have the vector * @return Block|false if failed */ - public function getBlock(Vector3 $pos) { - if(!isset($this->level) or ($pos instanceof Position) and $pos->level !== $this) { + public function getBlock(Vector3 $pos){ + if(!isset($this->level) or ($pos instanceof Position) and $pos->level !== $this){ return false; } return $this->getBlockWithoutVector($pos->x, $pos->y, $pos->z); } - public function setSpawn(Vector3 $pos) { - if(!isset($this->level)) { + public function setSpawn(Vector3 $pos){ + if(!isset($this->level)){ return false; } $this->level->setData("spawnX", $pos->x); @@ -963,69 +885,69 @@ class Level { $this->level->setData("spawnZ", $pos->z); } - public function getTime() { + public function getTime(){ return (int) ($this->time); } - public function setTime($time) { + public function setTime($time){ $this->startTime = $this->time = (int) $time; $this->startCheck = microtime(true); $this->checkTime(); } - public function checkTime() { - if(!isset($this->level)) { + public function checkTime(){ + if(!isset($this->level)){ return false; } $now = microtime(true); - if($this->stopTime) { + if($this->stopTime){ $time = $this->startTime; - } else { + }else{ $time = $this->startTime + ($now - $this->startCheck) * 20; } - if($this->server->api->dhandle("time.change", ["level" => $this, "time" => $time]) !== false) { //send time to player every 5 ticks + if($this->server->api->dhandle("time.change", ["level" => $this, "time" => $time]) !== false){ //send time to player every 5 ticks $this->time = $time; $pk = new SetTimePacket; $pk->time = (int) $this->time; $pk->started = $this->stopTime == false; $this->server->api->player->broadcastPacket($this->players, $pk); - } else { + }else{ $this->time -= 20 * 13; } } - public function isTimeStopped() { + public function isTimeStopped(){ return $this->stopTime; } - public function stopTime() { + public function stopTime(){ $this->stopTime = true; $this->startCheck = 0; $this->checkTime(); } - public function startTime() { + public function startTime(){ $this->stopTime = false; $this->startCheck = microtime(true); $this->checkTime(); } - public function getSeed() { - if(!isset($this->level)) { + public function getSeed(){ + if(!isset($this->level)){ return false; } return (int) $this->level->getData("seed"); } - public function setSeed($seed) { - if(!isset($this->level)) { + public function setSeed($seed){ + if(!isset($this->level)){ return false; } $this->level->setData("seed", (int) $seed); } - public function scheduleBlockUpdate(Position $pos, $delay, $type = BLOCK_UPDATE_SCHEDULED) { - if(!isset($this->level)) { + public function scheduleBlockUpdate(Position $pos, $delay, $type = BLOCK_UPDATE_SCHEDULED){ + if(!isset($this->level)){ return false; } return $this->server->api->block->scheduleBlockUpdate($pos, $delay, $type); diff --git a/src/world/LevelImport.php b/src/world/LevelImport.php index b42743dc..49f21773 100644 --- a/src/world/LevelImport.php +++ b/src/world/LevelImport.php @@ -1,33 +1,33 @@ path = $path; } - public function import() { - if(file_exists($this->path . "tileEntities.dat")) { //OldPM + public function import(){ + if(file_exists($this->path . "tileEntities.dat")){ //OldPM $level = unserialize(file_get_contents($this->path . "level.dat")); console("[INFO] Importing OldPM level \"" . $level["LevelName"] . "\" to PMF format"); $entities = new Config($this->path . "entities.yml", CONFIG_YAML, unserialize(file_get_contents($this->path . "entities.dat"))); $entities->save(); $tiles = new Config($this->path . "tiles.yml", CONFIG_YAML, unserialize(file_get_contents($this->path . "tileEntities.dat"))); $tiles->save(); - } elseif(file_exists($this->path . "chunks.dat") and file_exists($this->path . "level.dat")) { //Pocket + }elseif(file_exists($this->path . "chunks.dat") and file_exists($this->path . "level.dat")){ //Pocket $nbt = new NBT(); $nbt->load(substr(file_get_contents($this->path . "level.dat"), 8)); $level = array_shift($nbt->tree); - if($level["LevelName"] == "") { + if($level["LevelName"] == ""){ $level["LevelName"] = "world" . time(); } console("[INFO] Importing Pocket level \"" . $level["LevelName"] . "\" to PMF format"); unset($level["Player"]); $nbt->load(substr(file_get_contents($this->path . "entities.dat"), 12)); $entities = array_shift($nbt->tree); - if(!isset($entities["TileEntities"])) { + if(!isset($entities["TileEntities"])){ $entities["TileEntities"] = []; } $tiles = $entities["TileEntities"]; @@ -36,7 +36,7 @@ class LevelImport { $entities->save(); $tiles = new Config($this->path . "tiles.yml", CONFIG_YAML, $tiles); $tiles->save(); - } else { + }else{ return false; } @@ -54,8 +54,8 @@ class LevelImport { $chunks = new PocketChunkParser(); $chunks->loadFile($this->path . "chunks.dat"); $chunks->loadMap(); - for($Z = 0; $Z < 16; ++$Z) { - for($X = 0; $X < 16; ++$X) { + for($Z = 0; $Z < 16; ++$Z){ + for($X = 0; $X < 16; ++$X){ $chunk = [ 0 => "", 1 => "", @@ -66,18 +66,18 @@ class LevelImport { 6 => "", 7 => "" ]; - for($z = 0; $z < 16; ++$z) { - for($x = 0; $x < 16; ++$x) { + for($z = 0; $z < 16; ++$z){ + for($x = 0; $x < 16; ++$x){ $block = $chunks->getChunkColumn($X, $Z, $x, $z, 0); $meta = $chunks->getChunkColumn($X, $Z, $x, $z, 1); - for($Y = 0; $Y < 8; ++$Y) { + for($Y = 0; $Y < 8; ++$Y){ $chunk[$Y] .= substr($block, $Y << 4, 16); $chunk[$Y] .= substr($meta, $Y << 3, 8); $chunk[$Y] .= "\x00\x00\x00\x00\x00\x00\x00\x00"; } } } - foreach($chunk as $Y => $data) { + foreach($chunk as $Y => $data){ $pmf->setMiniChunk($X, $Z, $Y, $data); } $pmf->saveChunk($X, $Z); @@ -86,29 +86,15 @@ class LevelImport { } $chunks->map = null; $chunks = null; - if(file_exists($this->path . "level.dat")) { - @unlink($this->path . "level.dat"); - } - if(file_exists($this->path . "level.dat_old")) { - @unlink($this->path . "level.dat_old"); - } - if(file_exists($this->path . "player.dat")) { - @unlink($this->path . "player.dat"); - } - if(file_exists($this->path . "entities.dat")) { - @unlink($this->path . "entities.dat"); - } - if(file_exists($this->path . "chunks.dat")) { - @unlink($this->path . "chunks.dat"); - } - if(file_exists($this->path . "chunks.dat.gz")) { - @unlink($this->path . "chunks.dat.gz"); - } - if(file_exists($this->path . "tiles.dat")) { - @unlink($this->path . "tiles.dat"); - } + if(file_exists($this->path . "level.dat")) @unlink($this->path . "level.dat"); + if(file_exists($this->path . "level.dat_old")) @unlink($this->path . "level.dat_old"); + if(file_exists($this->path . "player.dat")) @unlink($this->path . "player.dat"); + if(file_exists($this->path . "entities.dat")) @unlink($this->path . "entities.dat"); + if(file_exists($this->path . "chunks.dat")) @unlink($this->path . "chunks.dat"); + if(file_exists($this->path . "chunks.dat.gz")) @unlink($this->path . "chunks.dat.gz"); + if(file_exists($this->path . "tiles.dat")) @unlink($this->path . "tiles.dat"); unset($chunks, $level, $entities, $tiles, $nbt); return true; } -} +} \ No newline at end of file diff --git a/src/world/MobSpawner.php b/src/world/MobSpawner.php index dd8aa69e..53bf5850 100644 --- a/src/world/MobSpawner.php +++ b/src/world/MobSpawner.php @@ -1,9 +1,7 @@ server = ServerAPI::request(); $this->level = $level; } - public function countEntities() { + public function countEntities(){ return $this->level->totalMobsAmount; } - public function checkDespawn(Living $living) { - if(!isset($this->entityAffectedPlayers[$living->eid])) { + public function checkDespawn(Living $living){ + if(!isset($this->entityAffectedPlayers[$living->eid])){ return false; } $playerID = $this->entityAffectedPlayers[$living->eid]; $player = $this->level->entityList[$playerID] ?? false; - if($player === false) { + if($player === false){ //TODO try retargetting other player? return true; - } else { + }else{ $diffX = $living->x - $player->x; $diffZ = $living->z - $player->z; $dist = $diffX * $diffX + $diffZ * $diffZ; - if($dist <= 512) { + if($dist <= 512){ return false; } @@ -43,35 +41,33 @@ class MobSpawner { } - public function handle() { - if($this->countEntities() > self::$MOB_LIMIT || count($this->level->players) <= 0) { + public function handle(){ + if($this->countEntities() > self::$MOB_LIMIT || count($this->level->players) <= 0){ return false; //not spawning } $svd = $this->totalMobsPerPlayer; $this->totalMobsPerPlayer = min(ceil(self::$MOB_LIMIT / count($this->level->players)), self::$maxMobsNearPlayerAtOnce); - if($svd != $this->totalMobsPerPlayer) { - ConsoleAPI::debug("Changed total mobs per player from $svd to {$this->totalMobsPerPlayer}."); - } + if($svd != $this->totalMobsPerPlayer) ConsoleAPI::debug("Changed total mobs per player from $svd to {$this->totalMobsPerPlayer}."); return $this->spawnMobs(); } - public function spawnMobs() { + public function spawnMobs(){ $phase = $this->server->api->time->getPhase($this->level); - if(self::$spawnAnimals && ($phase == "day" || $phase == "sunrise")) { //Animal + if(self::$spawnAnimals && ($phase == "day" || $phase == "sunrise")){ //Animal $type = mt_rand(10, 13); $baby = false; $grassOnly = true; - } elseif(self::$spawnMobs && ($phase == "night" || $phase == "sunset") && $this->server->difficulty > 0) { //Monster, true night + }elseif(self::$spawnMobs && ($phase == "night" || $phase == "sunset") && $this->server->difficulty > 0){ //Monster, true night $type = mt_rand(32, 35); $grassOnly = false; $baby = 2; - } else { + }else{ return false; } - foreach($this->level->players as $player) { - if(isset($this->playerAffectedEIDS[$player->entity->eid]) && count($this->playerAffectedEIDS[$player->entity->eid]) > $this->totalMobsPerPlayer) { + foreach($this->level->players as $player){ + if(isset($this->playerAffectedEIDS[$player->entity->eid]) && count($this->playerAffectedEIDS[$player->entity->eid]) > $this->totalMobsPerPlayer){ continue; } @@ -80,36 +76,34 @@ class MobSpawner { $diffX = $x - $player->entity->x; $diffZ = $z - $player->entity->z; $dist = $diffX * $diffX + $diffZ * $diffZ; - if($dist < 768) { + if($dist < 768){ continue; } $cnt = mt_rand(1, 3); - for($i = 0; $i < $cnt; ++$i) { + for($i = 0; $i < $cnt; ++$i){ $xMob = $x + mt_rand(-3, 3); $zMob = $z + mt_rand(-3, 3); $y = $this->getSafeY($xMob, $zMob, $grassOnly, $type >= 32 && $type <= 36 && $type != 35); - if(!$y || $y < 0) { + if(!$y || $y < 0){ continue; } $data = $this->genPosData($xMob, $y + 0.5, $zMob); - if($baby != 2) { - $data["IsBaby"] = $baby; - } + if($baby != 2) $data["IsBaby"] = $baby; $e = $this->server->api->entity->add($this->level, 2, $type, $data); - if($e instanceof Entity) { + if($e instanceof Entity){ $this->server->api->entity->spawnToAll($e); ConsoleAPI::debug("$type spawned at $xMob, $y, $zMob"); } - if(!isset($this->playerAffectedEIDS[$player->entity->eid])) { + if(!isset($this->playerAffectedEIDS[$player->entity->eid])){ $this->playerAffectedEIDS[$player->entity->eid] = [$e->eid => true]; - } else { + }else{ $this->playerAffectedEIDS[$player->entity->eid][$e->eid] = true; } @@ -121,7 +115,7 @@ class MobSpawner { return true; } - private function genPosData($x, $y, $z) { + private function genPosData($x, $y, $z){ return [ "x" => $x + 0.5, "y" => $y, @@ -129,9 +123,9 @@ class MobSpawner { ]; } - protected function getSafeY($x, $z, $grassOnly = false, $highMob = false) { //first safe block //TODO check boundingbox + protected function getSafeY($x, $z, $grassOnly = false, $highMob = false){ //first safe block //TODO check boundingbox $allowed = []; - for($y = 0; $y < 128; ++$y) { + for($y = 0; $y < 128; ++$y){ $b = $this->level->level->getBlockID($x, $y, $z); $b2 = $this->level->level->getBlockID($x, $y + 1, $z); $b1 = $this->level->level->getBlockID($x, $y - 1, $z); @@ -139,7 +133,7 @@ class MobSpawner { !StaticBlock::getIsSolid($b) && !StaticBlock::getIsLiquid($b) && (StaticBlock::getIsSolid($b1) && (!$grassOnly || $b1 === GRASS) && (!$highMob || !StaticBlock::getIsSolid($b2) && !StaticBlock::getIsLiquid($b2))) - ) { + ){ $allowed[] = $y; } } @@ -147,3 +141,4 @@ class MobSpawner { return empty($allowed) ? -1 : $allowed[mt_rand(0, count($allowed) - 1)]; } } + diff --git a/src/world/MovingObjectPosition.php b/src/world/MovingObjectPosition.php index a470538d..b3f535f5 100644 --- a/src/world/MovingObjectPosition.php +++ b/src/world/MovingObjectPosition.php @@ -1,6 +1,6 @@ blockX} {$this->blockY} {$this->blockZ}, side: {$this->sideHit} hitVec: {$this->hitVector} entityHit: {$this->entityHit}}"; } @@ -37,7 +37,7 @@ class MovingObjectPosition { * * @return MovingObjectPosition */ - public static function fromBlock($x, $y, $z, $side, Vector3 $hitVector) { + public static function fromBlock($x, $y, $z, $side, Vector3 $hitVector){ $ob = new MovingObjectPosition; $ob->typeOfHit = 0; $ob->blockX = $x; @@ -52,11 +52,11 @@ class MovingObjectPosition { * * @return MovingObjectPosition */ - public static function fromEntity(Entity $entity) { + public static function fromEntity(Entity $entity){ $ob = new MovingObjectPosition; $ob->typeOfHit = 1; $ob->entityHit = $entity; $ob->hitVector = new Vector3($entity->x, $entity->y, $entity->z); return $ob; } -} +} \ No newline at end of file diff --git a/src/world/PocketChunkParser.php b/src/world/PocketChunkParser.php index 0917aa62..2c85bb4a 100644 --- a/src/world/PocketChunkParser.php +++ b/src/world/PocketChunkParser.php @@ -1,26 +1,24 @@ raw = gzinflate(file_get_contents($file . ".gz")); $r = @gzinflate($this->raw); - if($r !== false and $r != "") { + if($r !== false and $r != ""){ $this->raw = $r; } @unlink($file . ".gz"); file_put_contents($file, $this->raw); - } elseif(!file_exists($file)) { + }elseif(!file_exists($file)){ return false; - } else { + }else{ $this->raw = file_get_contents($file); } $this->file = $file; @@ -28,32 +26,32 @@ class PocketChunkParser { return true; } - public function loadRaw($raw, $file) { + public function loadRaw($raw, $file){ $this->file = $file; $this->raw = $raw; $this->chunkLength = $this->sectorLength * ord($this->raw[0]); return true; } - public function getChunk($X, $Z) { + public function getChunk($X, $Z){ $X = (int) $X; $Z = (int) $Z; return substr($this->raw, $this->getOffset($X, $Z), $this->chunkLength); } - private function getOffset($X, $Z) { + private function getOffset($X, $Z){ return $this->location[$X + ($Z << 5)]; } - public function loadMap() { - if($this->raw == "") { + public function loadMap(){ + if($this->raw == ""){ return false; } $this->loadLocationTable(); console("[DEBUG] Loading chunks...", true, true, 2); - for($x = 0; $x < 16; ++$x) { + for($x = 0; $x < 16; ++$x){ $this->map[$x] = []; - for($z = 0; $z < 16; ++$z) { + for($z = 0; $z < 16; ++$z){ $this->map[$x][$z] = $this->parseChunk($x, $z); } } @@ -61,13 +59,13 @@ class PocketChunkParser { console("[DEBUG] Chunks loaded!", true, true, 2); } - private function loadLocationTable() { + private function loadLocationTable(){ $this->location = []; console("[DEBUG] Loading Chunk Location table...", true, true, 2); - for($offset = 0; $offset < 0x1000; $offset += 4) { + for($offset = 0; $offset < 0x1000; $offset += 4){ $data = Utils::readLInt(substr($this->raw, $offset, 4)); $sectors = $data & 0xff; - if($sectors === 0) { + if($sectors === 0){ continue; } $sectorLocation = $data >> 8; @@ -75,7 +73,7 @@ class PocketChunkParser { } } - public function parseChunk($X, $Z) { + public function parseChunk($X, $Z){ $X = (int) $X; $Z = (int) $Z; $offset = $this->getOffset($X, $Z); @@ -87,9 +85,9 @@ class PocketChunkParser { 2 => [], //SkyLight 3 => [], //BlockLight ]; - foreach($chunk as $section => &$data) { + foreach($chunk as $section => &$data){ $l = $section === 0 ? 128 : 64; - for($i = 0; $i < 256; ++$i) { + for($i = 0; $i < 256; ++$i){ $data[$i] = substr($this->raw, $offset, $l); $offset += $l; } @@ -97,13 +95,13 @@ class PocketChunkParser { return $chunk; } - public function saveMap($final = false) { + public function saveMap($final = false){ console("[DEBUG] Saving chunks...", true, true, 2); $fp = fopen($this->file, "r+b"); flock($fp, LOCK_EX); - foreach($this->map as $x => $d) { - foreach($d as $z => $chunk) { + foreach($this->map as $x => $d){ + foreach($d as $z => $chunk){ fseek($fp, $this->getOffset($x, $z)); fwrite($fp, $this->writeChunk($x, $z), $this->chunkLength); } @@ -114,41 +112,41 @@ class PocketChunkParser { file_put_contents($this->file . ".gz", gzdeflate(gzdeflate(file_get_contents($this->file), 9), 9)); //Double compression for flat maps $compressed = filesize($this->file . ".gz"); console("[DEBUG] Saved chunks.dat.gz with " . round(($compressed / $original) * 100, 2) . "% (" . round($compressed / 1024, 2) . "KB) of the original size", true, true, 2); - if($final === true) { + if($final === true){ @unlink($this->file); } } - public function writeChunk($X, $Z) { + public function writeChunk($X, $Z){ $X = (int) $X; $Z = (int) $Z; - if(!isset($this->map[$X][$Z])) { + if(!isset($this->map[$X][$Z])){ return false; } $chunk = ""; - foreach($this->map[$X][$Z] as $section => $data) { - for($i = 0; $i < 256; ++$i) { + foreach($this->map[$X][$Z] as $section => $data){ + for($i = 0; $i < 256; ++$i){ $chunk .= $data[$i]; } } return Utils::writeLInt(strlen($chunk)) . $chunk; } - public function getFloor($x, $z) { + public function getFloor($x, $z){ $X = $x >> 4; $Z = $z >> 4; $aX = $x - ($X << 4); $aZ = $z - ($Z << 4); $index = $aZ + ($aX << 4); - for($y = 127; $y <= 0; --$y) { - if($this->map[$X][$Z][0][$index][$y] !== "\x00") { + for($y = 127; $y <= 0; --$y){ + if($this->map[$X][$Z][0][$index][$y] !== "\x00"){ break; } } return $y; } - public function getBlock($x, $y, $z) { + public function getBlock($x, $y, $z){ $x = (int) $x; $y = (int) $y; $z = (int) $z; @@ -159,20 +157,20 @@ class PocketChunkParser { $index = $aZ + ($aX << 4); $block = ord($this->map[$X][$Z][0][$index][$y]); $meta = ord($this->map[$X][$Z][1][$index][$y >> 1]); - if(($y & 1) === 0) { + if(($y & 1) === 0){ $meta = $meta & 0x0F; - } else { + }else{ $meta = $meta >> 4; } return [$block, $meta]; } - public function getChunkColumn($X, $Z, $x, $z, $type = 0) { + public function getChunkColumn($X, $Z, $x, $z, $type = 0){ $index = $z + ($x << 4); return $this->map[$X][$Z][$type][$index]; } - public function setBlock($x, $y, $z, $block, $meta = 0) { + public function setBlock($x, $y, $z, $block, $meta = 0){ $x = (int) $x; $y = (int) $y; $z = (int) $z; @@ -183,12 +181,12 @@ class PocketChunkParser { $index = $aZ + ($aX << 4); $this->map[$X][$Z][0][$index][$y] = chr($block); $old_meta = ord($this->map[$X][$Z][1][$index][$y >> 1]); - if(($y & 1) === 0) { + if(($y & 1) === 0){ $meta = ($old_meta & 0xF0) | ($meta & 0x0F); - } else { + }else{ $meta = (($meta << 4) & 0xF0) | ($old_meta & 0x0F); } $this->map[$X][$Z][1][$index][$y >> 1] = chr($meta); } -} +} \ No newline at end of file diff --git a/src/world/Position.php b/src/world/Position.php index d7308c2d..dff85467 100644 --- a/src/world/Position.php +++ b/src/world/Position.php @@ -1,13 +1,13 @@ x, $x->y, $x->z); - } else { + }else{ $this->x = $x; $this->y = $y; $this->z = $z; @@ -15,25 +15,25 @@ class Position extends Vector3 { $this->level = $level; } - public function setXYZLevel($x, $y, $z, Level $level) { + public function setXYZLevel($x, $y, $z, Level $level){ $this->x = $x; $this->y = $y; $this->z = $z; $this->level = $level; } - public function getSide($side, $step = 1) { + public function getSide($side, $step = 1){ return new Position(parent::getSide($side, $step), 0, 0, $this->level); } - public function distance($x = 0, $y = 0, $z = 0) { - if(($x instanceof Position) and $x->level !== $this->level) { + public function distance($x = 0, $y = 0, $z = 0){ + if(($x instanceof Position) and $x->level !== $this->level){ return PHP_INT_MAX; } return parent::distance($x, $y, $z); } - public function __toString() { + public function __toString(){ return "Position(level=" . $this->level->getName() . ",x=" . $this->x . ",y=" . $this->y . ",z=" . $this->z . ")"; } -} +} \ No newline at end of file diff --git a/src/world/Tile.php b/src/world/Tile.php index 92cb0f36..2ffefb84 100644 --- a/src/world/Tile.php +++ b/src/world/Tile.php @@ -1,6 +1,6 @@ server = ServerAPI::request(); $this->level = $level; $this->normal = true; $this->class = $class; $this->data = $data; $this->closed = false; - if($class === false) { + if($class === false){ $this->closed = true; } $this->name = ""; @@ -37,83 +35,83 @@ class Tile extends Position { $this->y = (int) $y; $this->z = (int) $z; $this->server->query("INSERT OR REPLACE INTO tiles (ID, level, class, x, y, z) VALUES (" . $this->id . ", '" . $this->level->getName() . "', '" . $this->class . "', " . $this->x . ", " . $this->y . ", " . $this->z . ");"); - switch($this->class) { + switch($this->class){ case TILE_CHEST: case TILE_SIGN: $this->server->query("UPDATE tiles SET spawnable = 1 WHERE ID = " . $this->id . ";"); break; case TILE_FURNACE: - if(!isset($this->data["BurnTime"]) or $this->data["BurnTime"] < 0) { + if(!isset($this->data["BurnTime"]) or $this->data["BurnTime"] < 0){ $this->data["BurnTime"] = 0; } - if(!isset($this->data["CookTime"]) or $this->data["CookTime"] < 0 or ($this->data["BurnTime"] === 0 and $this->data["CookTime"] > 0)) { + if(!isset($this->data["CookTime"]) or $this->data["CookTime"] < 0 or ($this->data["BurnTime"] === 0 and $this->data["CookTime"] > 0)){ $this->data["CookTime"] = 0; } - if(!isset($this->data["MaxTime"])) { + if(!isset($this->data["MaxTime"])){ $this->data["MaxTime"] = $this->data["BurnTime"]; $this->data["BurnTicks"] = 0; } - if($this->data["BurnTime"] > 0) { + if($this->data["BurnTime"] > 0){ $this->update(); } break; } } - public function update() { - if($this->closed === true) { + public function update(){ + if($this->closed === true){ return false; } - if($this->class === TILE_FURNACE) { + if($this->class === TILE_FURNACE){ $fuel = $this->getSlot(1); $raw = $this->getSlot(0); $product = $this->getSlot(2); $smelt = $raw->getSmeltItem(); $canSmelt = ($smelt !== false and $raw->count > 0 and (($product->getID() === $smelt->getID() and $product->getMetadata() === $smelt->getMetadata() and $product->count < $product->getMaxStackSize()) or $product->getID() === AIR)); - if($this->data["BurnTime"] <= 0 and $canSmelt and $fuel->getFuelTime() !== false and $fuel->count > 0) { + if($this->data["BurnTime"] <= 0 and $canSmelt and $fuel->getFuelTime() !== false and $fuel->count > 0){ $this->lastUpdate = microtime(true); $this->data["MaxTime"] = $this->data["BurnTime"] = floor($fuel->getFuelTime() * 20); $this->data["BurnTicks"] = 0; --$fuel->count; - if($fuel->count === 0) { + if($fuel->count === 0){ $fuel = BlockAPI::getItem(AIR, 0, 0); } $this->setSlot(1, $fuel, false); $current = $this->level->getBlock($this); - if($current->getID() === FURNACE) { + if($current->getID() === FURNACE){ $this->level->setBlock($this, BlockAPI::get(BURNING_FURNACE, $current->getMetadata()), true, false, true); } } - if($this->data["BurnTime"] > 0) { + if($this->data["BurnTime"] > 0){ $ticks = (microtime(true) - $this->lastUpdate) * 20; $this->data["BurnTime"] -= $ticks; $this->data["BurnTicks"] = ceil(($this->data["BurnTime"] / $this->data["MaxTime"]) * 200); - if($smelt !== false and $canSmelt) { + if($smelt !== false and $canSmelt){ $this->data["CookTime"] += $ticks; - if($this->data["CookTime"] >= 200) { //10 seconds + if($this->data["CookTime"] >= 200){ //10 seconds $product = BlockAPI::getItem($smelt->getID(), $smelt->getMetadata(), $product->count + 1); $this->setSlot(2, $product, false); --$raw->count; - if($raw->count === 0) { + if($raw->count === 0){ $raw = BlockAPI::getItem(AIR, 0, 0); } $this->setSlot(0, $raw, false); $this->data["CookTime"] -= 200; } - } elseif($this->data["BurnTime"] <= 0) { + }elseif($this->data["BurnTime"] <= 0){ $this->data["BurnTime"] = 0; $this->data["CookTime"] = 0; $this->data["BurnTicks"] = 0; - } else { + }else{ $this->data["CookTime"] = 0; } $this->server->schedule(2, [$this, "update"]); $this->scheduledUpdate = true; - } else { + }else{ $current = $this->level->getBlock($this); - if($current->getID() === BURNING_FURNACE) { + if($current->getID() === BURNING_FURNACE){ $this->level->setBlock($this, BlockAPI::get(FURNACE, $current->getMetadata()), true, false, true); } $this->data["CookTime"] = 0; @@ -126,28 +124,28 @@ class Tile extends Position { $this->lastUpdate = microtime(true); } - public function getSlot($s) { + public function getSlot($s){ $i = $this->getSlotIndex($s); - if($i === false or $i < 0) { + if($i === false or $i < 0){ return BlockAPI::getItem(AIR, 0, 0); - } else { + }else{ return BlockAPI::getItem($this->data["Items"][$i]["id"], $this->data["Items"][$i]["Damage"], $this->data["Items"][$i]["Count"]); } } - public function getSlotIndex($s) { - if($this->class !== TILE_CHEST and $this->class !== TILE_FURNACE) { + public function getSlotIndex($s){ + if($this->class !== TILE_CHEST and $this->class !== TILE_FURNACE){ return false; } - foreach($this->data["Items"] as $i => $slot) { - if($slot["Slot"] === $s) { + foreach($this->data["Items"] as $i => $slot){ + if($slot["Slot"] === $s){ return $i; } } return -1; } - public function setSlot($s, Item $item, $update = true, $offset = 0) { + public function setSlot($s, Item $item, $update = true, $offset = 0){ $i = $this->getSlotIndex($s); $d = [ "Count" => $item->count, @@ -155,15 +153,15 @@ class Tile extends Position { "id" => $item->getID(), "Damage" => $item->getMetadata(), ]; - if($i === false) { + if($i === false){ return false; - } elseif($item->getID() === AIR or $item->count <= 0) { - if($i >= 0) { + }elseif($item->getID() === AIR or $item->count <= 0){ + if($i >= 0){ unset($this->data["Items"][$i]); } - } elseif($i < 0) { + }elseif($i < 0){ $this->data["Items"][] = $d; - } else { + }else{ $this->data["Items"][$i] = $d; } $this->server->api->dhandle("tile.container.slot", [ @@ -173,14 +171,14 @@ class Tile extends Position { "slotdata" => $item, ]); - if($update === true and $this->scheduledUpdate === false) { + if($update === true and $this->scheduledUpdate === false){ $this->update(); } return true; } - public function pairWith(Tile $tile) { - if($this->isPaired() or $tile->isPaired()) { + public function pairWith(Tile $tile){ + if($this->isPaired() or $tile->isPaired()){ return false; } @@ -196,18 +194,18 @@ class Tile extends Position { $this->server->handle("tile.update", $tile); } - public function isPaired() { - if($this->class !== TILE_CHEST) { + public function isPaired(){ + if($this->class !== TILE_CHEST){ return false; } - if(!isset($this->data["pairx"]) or !isset($this->data["pairz"])) { + if(!isset($this->data["pairx"]) or !isset($this->data["pairz"])){ return false; } return true; } - public function unpair() { - if(!$this->isPaired()) { + public function unpair(){ + if(!$this->isPaired()){ return false; } @@ -216,36 +214,36 @@ class Tile extends Position { $this->server->api->tile->spawnToAll($this); $this->server->handle("tile.update", $this); - if($tile instanceof Tile) { + if($tile instanceof Tile){ $this->server->api->tile->spawnToAll($tile); $this->server->handle("tile.update", $tile); } } - public function getPair() { - if($this->isPaired()) { + public function getPair(){ + if($this->isPaired()){ return $this->server->api->tile->get(new Position((int) $this->data["pairx"], $this->y, (int) $this->data["pairz"], $this->level)); } return false; } - public function openInventory(Player $player) { - if($this->class === TILE_CHEST) { + public function openInventory(Player $player){ + if($this->class === TILE_CHEST){ $player->windowCnt++; $player->windowCnt = $id = max(2, $player->windowCnt % 99); - if(($pair = $this->getPair()) !== false) { - if(($pair->x + ($pair->z << 13)) > ($this->x + ($this->z << 13))) { //Order them correctly + if(($pair = $this->getPair()) !== false){ + if(($pair->x + ($pair->z << 13)) > ($this->x + ($this->z << 13))){ //Order them correctly $player->windows[$id] = [ $pair, $this ]; - } else { + }else{ $player->windows[$id] = [ $this, $pair ]; } - } else { + }else{ $player->windows[$id] = $this; } @@ -259,9 +257,9 @@ class Tile extends Position { $player->dataPacket($pk); $slots = []; - if(is_array($player->windows[$id])) { + if(is_array($player->windows[$id])){ $all = $this->server->api->player->getAll($this->level); - foreach($player->windows[$id] as $ob) { + foreach($player->windows[$id] as $ob){ $pk = new TileEventPacket; $pk->x = $ob->x; $pk->y = $ob->y; @@ -269,16 +267,16 @@ class Tile extends Position { $pk->case1 = 1; $pk->case2 = 2; $this->server->api->player->broadcastPacket($all, $pk); - for($s = 0; $s < CHEST_SLOTS; ++$s) { + for($s = 0; $s < CHEST_SLOTS; ++$s){ $slot = $ob->getSlot($s); - if($slot->getID() > AIR and $slot->count > 0) { + if($slot->getID() > AIR and $slot->count > 0){ $slots[] = $slot; - } else { + }else{ $slots[] = BlockAPI::getItem(AIR, 0, 0); } } } - } else { + }else{ $pk = new TileEventPacket; $pk->x = $this->x; $pk->y = $this->y; @@ -286,11 +284,11 @@ class Tile extends Position { $pk->case1 = 1; $pk->case2 = 2; $this->server->api->player->broadcastPacket($this->server->api->player->getAll($this->level), $pk); - for($s = 0; $s < CHEST_SLOTS; ++$s) { + for($s = 0; $s < CHEST_SLOTS; ++$s){ $slot = $this->getSlot($s); - if($slot->getID() > AIR and $slot->count > 0) { + if($slot->getID() > AIR and $slot->count > 0){ $slots[] = $slot; - } else { + }else{ $slots[] = BlockAPI::getItem(AIR, 0, 0); } } @@ -301,7 +299,7 @@ class Tile extends Position { $pk->slots = $slots; $player->dataPacket($pk); return true; - } elseif($this->class === TILE_FURNACE) { + }elseif($this->class === TILE_FURNACE){ $player->windowCnt++; $player->windowCnt = $id = max(2, $player->windowCnt % 99); $player->windows[$id] = $this; @@ -316,11 +314,11 @@ class Tile extends Position { $player->dataPacket($pk); $slots = []; - for($s = 0; $s < FURNACE_SLOTS; ++$s) { + for($s = 0; $s < FURNACE_SLOTS; ++$s){ $slot = $this->getSlot($s); - if($slot->getID() > AIR and $slot->count > 0) { + if($slot->getID() > AIR and $slot->count > 0){ $slots[] = $slot; - } else { + }else{ $slots[] = BlockAPI::getItem(AIR, 0, 0); } } @@ -332,14 +330,14 @@ class Tile extends Position { } } - public function spawn($player) { - if($this->closed) { + public function spawn($player){ + if($this->closed){ return false; } - if(!($player instanceof Player)) { + if(!($player instanceof Player)){ $player = $this->server->api->player->get($player); } - switch($this->class) { + switch($this->class){ case TILE_CHEST: $nbt = new NBT(); $nbt->write(chr(NBT::TAG_COMPOUND) . "\x00\x00"); @@ -360,7 +358,7 @@ class Tile extends Position { $nbt->writeTAG_String("z"); $nbt->writeTAG_Int((int) $this->z); - if($this->isPaired()) { + if($this->isPaired()){ $nbt->write(chr(NBT::TAG_INT)); $nbt->writeTAG_String("pairx"); $nbt->writeTAG_Int((int) $this->data["pairx"]); @@ -427,8 +425,8 @@ class Tile extends Position { } } - public function setText($line1 = "", $line2 = "", $line3 = "", $line4 = "") { - if($this->class !== TILE_SIGN) { + public function setText($line1 = "", $line2 = "", $line3 = "", $line4 = ""){ + if($this->class !== TILE_SIGN){ return false; } $this->data["Text1"] = $line1; @@ -440,7 +438,7 @@ class Tile extends Position { return true; } - public function getText() { + public function getText(){ return [ $this->data["Text1"], $this->data["Text2"], @@ -449,23 +447,23 @@ class Tile extends Position { ]; } - public function __destruct() { + public function __destruct(){ $this->close(); } - public function close() { - if($this->closed === false) { + public function close(){ + if($this->closed === false){ $this->closed = true; $this->server->api->tile->remove($this->id); } } - public function getName() { + public function getName(){ return $this->name; } - public function setPosition(Vector3 $pos) { - if($pos instanceof Position) { + public function setPosition(Vector3 $pos){ + if($pos instanceof Position){ $this->level = $pos->level; $this->server->query("UPDATE tiles SET level = '" . $this->level->getName() . "' WHERE ID = " . $this->id . ";"); } diff --git a/src/world/generator/noise/NoiseGeneratorOctaves.php b/src/world/generator/noise/NoiseGeneratorOctaves.php index 10328dd5..b7ef7323 100644 --- a/src/world/generator/noise/NoiseGeneratorOctaves.php +++ b/src/world/generator/noise/NoiseGeneratorOctaves.php @@ -4,36 +4,36 @@ require_once("NoiseGenerator.php"); /***REM_END***/ -class NoiseGeneratorOctaves extends NoiseGenerator { +class NoiseGeneratorOctaves extends NoiseGenerator{ public $octaves; private $generatorCollection; - public function __construct(MTRandom $random, $octaves) { + public function __construct(MTRandom $random, $octaves){ $this->generatorCollection = []; $this->octaves = (int) $octaves; - for($o = 0; $o < $this->octaves; ++$o) { + for($o = 0; $o < $this->octaves; ++$o){ $this->generatorCollection[$o] = new NoiseGeneratorPerlin($random); } } - public function getValue($x, $y) { + public function getValue($x, $y){ $noise = 0; $scale = 1; - for($i = 0; $i < $this->octaves; ++$i) { + for($i = 0; $i < $this->octaves; ++$i){ $noise += $this->generatorCollection[$i]->getValue($x * $scale, $y * $scale) / $scale; $scale /= 2; } return $noise; } - public function generateNoiseOctaves($int1, $int2, $int3, $int4, $int5, $int6, $par1 = false, $par2 = false, $par3 = false) { - if($par1 === false or $par2 === false or $par3 === false) { + public function generateNoiseOctaves($int1, $int2, $int3, $int4, $int5, $int6, $par1 = false, $par2 = false, $par3 = false){ + if($par1 === false or $par2 === false or $par3 === false){ return $this->generateNoiseOctaves($int1, 10, $int2, $int3, 1, $int4, $int5, 1, $int6); } $floats = array_fill(0, $int4 * $int5 * $int6, 0); $d1 = 1; - for($j = 0; $j < $this->octaves; ++$j) { + for($j = 0; $j < $this->octaves; ++$j){ $d2 = $int1 * $d1 * $par1; $d3 = $int2 * $d1 * $par2; $d4 = $int3 * $d1 * $par3; @@ -51,4 +51,4 @@ class NoiseGeneratorOctaves extends NoiseGenerator { } return $floats; } -} +} \ No newline at end of file diff --git a/src/world/generator/noise/NoiseGeneratorPerlin.php b/src/world/generator/noise/NoiseGeneratorPerlin.php index 85d0435c..2d812a8f 100644 --- a/src/world/generator/noise/NoiseGeneratorPerlin.php +++ b/src/world/generator/noise/NoiseGeneratorPerlin.php @@ -4,23 +4,21 @@ require_once("NoiseGenerator.php"); /***REM_END***/ -class NoiseGeneratorPerlin extends NoiseGenerator { +class NoiseGeneratorPerlin extends NoiseGenerator{ - public $xCoord; - public $yCoord; - public $zCoord; + public $xCoord, $yCoord, $zCoord; private $permutations = []; - public function __construct(MTRandom $random) { + public function __construct(MTRandom $random){ $this->xCoord = $random->nextFloat() * 256; $this->yCoord = $random->nextFloat() * 256; $this->zCoord = $random->nextFloat() * 256; - for($i = 0; $i < 512; ++$i) { + for($i = 0; $i < 512; ++$i){ $this->permutations[$i] = $i < 256 ? $i : 0; } - for($i = 0; $i < 256; ++$i) { + for($i = 0; $i < 256; ++$i){ $j = $random->nextInt(256 - $i) + $i; $k = $this->permutations[$i]; $this->permutations[$i] = $this->permutations[$j]; @@ -30,7 +28,7 @@ class NoiseGeneratorPerlin extends NoiseGenerator { } - public function getValue($d, $d1, $d2 = 0) { + public function getValue($d, $d1, $d2 = 0){ $d3 = $d + $this->xCoord; $d4 = $d1 + $this->yCoord; $d5 = $d2 + $this->zCoord; @@ -66,18 +64,18 @@ class NoiseGeneratorPerlin extends NoiseGenerator { ); } - public function populateNoiseArray(&$floats, $par1, $par2, $par3, $int1, $int2, $int3, $par4, $par5, $par6, $par7) { - if($int2 === 1) { + public function populateNoiseArray(&$floats, $par1, $par2, $par3, $int1, $int2, $int3, $par4, $par5, $par6, $par7){ + if($int2 === 1){ $n = 0; $d3 = 1 / $par7; - for($i1 = 0; $i1 < $int1; ++$i1) { + for($i1 = 0; $i1 < $int1; ++$i1){ $d4 = $par1 + $i1 * $par4 + $this->xCoord; $i2 = floor($d4); $i3 = $i2 & 0xFF; $d4 -= $i2; $d5 = $d4 * $d4 * $d4 * ($d4 * ($d4 * 6 - 15) + 10); - for($i4 = 0; $i4 < $int3; ++$i4) { + for($i4 = 0; $i4 < $int3; ++$i4){ $d6 = $par3 + $i4 * $par6 + $this->zCoord; $i5 = floor($d6); $i6 = $i5 & 0xFF; @@ -103,28 +101,28 @@ class NoiseGeneratorPerlin extends NoiseGenerator { $n = 0; $i = 0; - for($i4 = 0; $i4 < $int1; ++$i4) { + for($i4 = 0; $i4 < $int1; ++$i4){ $d6 = $par1 + $i4 * $par4 + $this->xCoord; $i5 = floor($d6); $i6 = $i5 & 0xFF; $d6 -= $i5; $d7 = $d6 * $d6 * $d6 * ($d6 * ($d6 * 6 - 15) + 10); - for($i12 = 0; $i12 < $int3; ++$i12) { + for($i12 = 0; $i12 < $int3; ++$i12){ $d12 = $par3 + $i12 * $par6 + $this->zCoord; $i13 = floor($d12); $i14 = $i13 & 0xFF; $d12 -= $i13; $d13 = $d12 * $d12 * $d12 * ($d12 * ($d12 * 6 - 15) + 10); - for($i15 = 0; $i15 < $int2; ++$i15) { + for($i15 = 0; $i15 < $int2; ++$i15){ $d14 = $par2 + $i15 * $par5 + $this->yCoord; $i16 = floor($d14); $i17 = $i16 & 0xff; $d14 -= $i16; $d15 = $d14 * $d14 * $d14 * ($d14 * ($d14 * 6 - 15) + 10); - if($i15 == 0 or $i17 != $m) { + if($i15 == 0 or $i17 != $m){ $m = $i17; $i7 = $this->permutations[$i6] + $i17; $i8 = $this->permutations[$i7] + $i14; @@ -147,22 +145,22 @@ class NoiseGeneratorPerlin extends NoiseGenerator { } } - final public static function curve($par1, $par2, $par3) { + public static final function curve($par1, $par2, $par3){ return $par2 + $par1 * ($par3 - $par2); } - public static function grad2D($int, $par1, $par2) { + public static function grad2D($int, $par1, $par2){ $i = $int & 0x0F; $d1 = (1 - (($i & 0x08) >> 3)) * $par1; $d2 = ($i == 12 || $i == 14) ? $par1 : (($i >= 4) * $par2); return (($i & 0x01) ? -$d1 : $d1) + (($i & 0x02) ? -$d2 : $d2); } - public static function grad3D($int, $par1, $par2, $par3) { + public static function grad3D($int, $par1, $par2, $par3){ $i = $int & 0x0F; $d1 = $i < 8 ? $par1 : $par2; $d2 = ($i == 12 or $i == 14) ? $par1 : ($i < 4 ? $par2 : $par3); return (($i & 0x01) ? -$d1 : $d1) + (($i & 0x02) ? -$d2 : $d2); } -} +} \ No newline at end of file diff --git a/src/world/generator/vanilla/VanillaGenerator.php b/src/world/generator/vanilla/VanillaGenerator.php index 4d18f2d6..91289d8f 100644 --- a/src/world/generator/vanilla/VanillaGenerator.php +++ b/src/world/generator/vanilla/VanillaGenerator.php @@ -2,7 +2,8 @@ define("EMPTY_MINI_CHUNK", str_repeat("\x00", 8192)); define("EMPTY_16x16_ARR", array_fill(0, 256, 0)); -class VanillaGenerator implements LevelGenerator { +class VanillaGenerator implements LevelGenerator +{ /** * @var Level */ @@ -22,14 +23,7 @@ class VanillaGenerator implements LevelGenerator { * @var NoiseGeneratorOctaves $depthNoise * @var NoiseGeneratorOctaves $treeNoise */ - private $upperInterpolationNoise; - private $lowerInterpolationNoise; - private $interpolationNoise; - private $beachNoise; - private $surfaceDepthNoise; - private $biomeNoise; - private $depthNoise; - private $treeNoise; + private $upperInterpolationNoise, $lowerInterpolationNoise, $interpolationNoise, $beachNoise, $surfaceDepthNoise, $biomeNoise, $depthNoise, $treeNoise; /** * @var float[] $biomeNoises * @var float[] $depthNoises @@ -42,16 +36,7 @@ class VanillaGenerator implements LevelGenerator { * @var float[] $gravelNoises * @var float[] $surfaceDepthNoises */ - private $biomeNoises; - private $depthNoises; - private $interpolationNoises; - private $upperInterpolationNoises; - private $lowerInterpolationNoises; - private $biomes; - private $heights; - private $sandNoises; - private $gravelNoises; - private $surfaceDepthNoises; + private $biomeNoises, $depthNoises, $interpolationNoises, $upperInterpolationNoises, $lowerInterpolationNoises, $biomes, $heights, $sandNoises, $gravelNoises, $surfaceDepthNoises; /** * @var BiomeSource */ @@ -59,10 +44,12 @@ class VanillaGenerator implements LevelGenerator { private $heightMap = []; - public function __construct(array $options = []) { + public function __construct(array $options = []) + { } - public function init(Level $level, Random $random) { + public function init(Level $level, Random $random) + { $this->level = $level; $this->biomeSource = new BiomeSource($level); $this->rand = new MTRandom($level->getSeed()); @@ -76,11 +63,13 @@ class VanillaGenerator implements LevelGenerator { $this->treeNoise = new NoiseGeneratorOctaves($this->rand, 8); } - public function getSpawn() { + public function getSpawn() + { return new Vector3(128, 64, 128); } - public function populateChunk($chunkX, $chunkZ) { + public function populateChunk($chunkX, $chunkZ) + { $chunkXWorld = $chunkX * 16; $chunkZWorld = $chunkZ * 16; //HeavyTile::instaFall = 1; TODO instant sand/gravel fall @@ -89,7 +78,7 @@ class VanillaGenerator implements LevelGenerator { $i1 = (int) (((int) ($this->rand->nextInt() / 2)) * 2 + 1); //why php integers are so big....??????????????????????????????????????? $j1 = (int) (((int) ($this->rand->nextInt() / 2)) * 2 + 1); $this->rand->setSeed((($chunkX * $i1 + $chunkZ * $j1) & 0xffffffff) ^ $this->level->getSeed()); - for($i2 = 0; $i2 < 10; ++$i2) { + for($i2 = 0; $i2 < 10; ++$i2){ ClayFeature::place($this->level, $this->rand, $chunkXWorld + $this->rand->nextInt(16), $this->rand->nextInt(128), $chunkZWorld + $this->rand->nextInt(16)); } for ($i3 = 0; $i3 < 20; ++$i3) { @@ -101,37 +90,29 @@ class VanillaGenerator implements LevelGenerator { for ($i5 = 0; $i5 < 20; ++$i5) { OreFeature::place($this->level, $this->rand, $chunkXWorld + $this->rand->nextInt(16), $this->rand->nextInt(128), $chunkZWorld + $this->rand->nextInt(16), COAL_ORE, 16); } - for($i6 = 0; $i6 < 20; ++$i6) { + for($i6 = 0; $i6 < 20; ++$i6){ OreFeature::place($this->level, $this->rand, $chunkXWorld + $this->rand->nextInt(16), $this->rand->nextInt(64), $chunkZWorld + $this->rand->nextInt(16), IRON_ORE, 8); } - for($i7 = 0; $i7 < 2; ++$i7) { + for($i7 = 0; $i7 < 2; ++$i7){ OreFeature::place($this->level, $this->rand, $chunkXWorld + $this->rand->nextInt(16), $this->rand->nextInt(32), $chunkZWorld + $this->rand->nextInt(16), GOLD_ORE, 8); } - for($i8 = 0; $i8 < 8; ++$i8) { - OreFeature::place($this->level, $this->rand, $chunkXWorld + $this->rand->nextInt(16), $this->rand->nextInt(16), $chunkZWorld + $this->rand->nextInt(16), REDSTONE_ORE, 7); + for($i8 = 0; $i8 < 8; ++$i8){ + OreFeature::place($this->level, $this->rand, $chunkXWorld + $this->rand->nextInt(16), $this->rand->nextInt(16), $chunkZWorld + $this->rand->nextInt(16), REDSTONE_ORE, 7); } - OreFeature::place($this->level, $this->rand, $chunkXWorld + $this->rand->nextInt(16), $this->rand->nextInt(16), $chunkZWorld + $this->rand->nextInt(16), DIAMOND_ORE, 7); - OreFeature::place($this->level, $this->rand, $chunkXWorld + $this->rand->nextInt(16), $this->rand->nextInt(16) + $this->rand->nextInt(16), $chunkZWorld + $this->rand->nextInt(16), LAPIS_ORE, 6); + OreFeature::place($this->level, $this->rand, $chunkXWorld + $this->rand->nextInt(16), $this->rand->nextInt(16), $chunkZWorld + $this->rand->nextInt(16), DIAMOND_ORE, 7); + OreFeature::place($this->level, $this->rand, $chunkXWorld + $this->rand->nextInt(16), $this->rand->nextInt(16) + $this->rand->nextInt(16), $chunkZWorld + $this->rand->nextInt(16), LAPIS_ORE, 6); $sample = (int) (((($this->treeNoise->getValue($chunkXWorld * 0.5, $chunkZWorld * 0.5) / 8) + ($this->rand->nextFloat() * 4)) + 4) / 3); $treesAmount = $this->rand->nextInt(10) == 0; - if($biome == Biome::$forest) { - $treesAmount += $sample + 2; - } elseif($biome == Biome::$rainForest) { - $treesAmount += $sample + 2; - } elseif($biome == Biome::$seasonalForest) { - $treesAmount += $sample + 1; - } elseif($biome == Biome::$taiga) { - $treesAmount += $sample + 1; - } elseif($biome == Biome::$desert) { - $treesAmount -= 20; - } elseif($biome == Biome::$tundra) { - $treesAmount -= 20; - } elseif($biome == Biome::$plains) { - $treesAmount -= 20; - } - for($l8 = 0; $l8 < $treesAmount; ++$l8) { + if($biome == Biome::$forest) $treesAmount += $sample + 2; + elseif($biome == Biome::$rainForest) $treesAmount += $sample + 2; + elseif($biome == Biome::$seasonalForest) $treesAmount += $sample + 1; + elseif($biome == Biome::$taiga) $treesAmount += $sample + 1; + elseif($biome == Biome::$desert) $treesAmount -= 20; + elseif($biome == Biome::$tundra) $treesAmount -= 20; + elseif($biome == Biome::$plains) $treesAmount -= 20; + for($l8 = 0; $l8 < $treesAmount; ++$l8){ $l12 = $chunkXWorld + $this->rand->nextInt(16) + 8; $j15 = $chunkZWorld + $this->rand->nextInt(16) + 8; /** @@ -140,25 +121,25 @@ class VanillaGenerator implements LevelGenerator { $tree = $biome->getTreeFeature($this->rand); $tree->place($this->level, $this->rand, $l12, $this->getHeightValue($l12, $j15), $j15); } - for($i9 = 0; $i9 < 2; ++$i9) { + for($i9 = 0; $i9 < 2; ++$i9){ $i13 = $chunkXWorld + $this->rand->nextInt(16) + 8; $k15 = $this->rand->nextInt(128); $l17 = $chunkZWorld + $this->rand->nextInt(16) + 8; Feature::$FLOWER_YELLOW->place($this->level, $this->rand, $i13, $k15, $l17); } - if($this->rand->nextInt(2) == 0) { + if($this->rand->nextInt(2) == 0){ $j9 = $chunkXWorld + $this->rand->nextInt(16) + 8; $j13 = $this->rand->nextInt(128); $l15 = $chunkZWorld + $this->rand->nextInt(16) + 8; Feature::$FLOWER_RED->place($this->level, $this->rand, $j9, $j13, $l15); } - if($this->rand->nextInt(4) == 0) { + if($this->rand->nextInt(4) == 0){ $j9 = $chunkXWorld + $this->rand->nextInt(16) + 8; $j13 = $this->rand->nextInt(128); $l15 = $chunkZWorld + $this->rand->nextInt(16) + 8; Feature::$MUSHROOM_BROWN->place($this->level, $this->rand, $j9, $j13, $l15); } - if($this->rand->nextInt(8) == 0) { + if($this->rand->nextInt(8) == 0){ $j9 = $chunkXWorld + $this->rand->nextInt(16) + 8; $j13 = $this->rand->nextInt(128); $l15 = $chunkZWorld + $this->rand->nextInt(16) + 8; @@ -166,7 +147,8 @@ class VanillaGenerator implements LevelGenerator { } } - public function generateChunk($chunkX, $chunkZ) { + public function generateChunk($chunkX, $chunkZ) + { $this->rand->setSeed(341872712 * $chunkX + 132899541 * $chunkZ); $this->biomes = $this->biomeSource->getBiomeBlock($chunkX * 16, $chunkZ * 16, 16, 16); $chunkz = array_fill(0, 8, EMPTY_MINI_CHUNK); //[$blockY >> 4][($blockY & 0xf) + ($blockX << 5) + ($blockZ << 9)]. y&f+x&f+z&f @@ -174,7 +156,7 @@ class VanillaGenerator implements LevelGenerator { $this->buildSurfaces($chunkX, $chunkZ, $chunkz, $this->biomes); $this->generateHeightmap($chunkX, $chunkZ, $chunkz); - for($Y = 0; $Y < 8; ++$Y) { + for($Y = 0; $Y < 8; ++$Y){ $index = ($chunkZ << 4) + $chunkX; $this->level->level->chunks[$index][$Y] = $chunkz[$Y]; $this->level->level->chunkChange[$index][$Y] = 8192; @@ -184,34 +166,30 @@ class VanillaGenerator implements LevelGenerator { } - public function populateLevel() { - } - public function setHeightValue($x, $z, $hv) { - if($x > 255 || $x < 0 || $z > 255 || $z < 0) { - return; - } + public function populateLevel() + {} + public function setHeightValue($x, $z, $hv){ + if($x > 255 || $x < 0 || $z > 255 || $z < 0) return; $cX = $x >> 4; $cZ = $z >> 4; $bX = $x & 0xf; $bZ = $z & 0xf; $this->heightMap[$cX + ($cZ * 16)][$bX + ($bZ * 16)] = $hv; } - public function getHeightValue($x, $z) { - if($x > 255 || $x < 0 || $z > 255 || $z < 0) { - return 0; - } + public function getHeightValue($x, $z){ + if($x > 255 || $x < 0 || $z > 255 || $z < 0) return 0; $cX = $x >> 4; $cZ = $z >> 4; $bX = $x & 0xf; $bZ = $z & 0xf; return $this->heightMap[$cX + ($cZ * 16)][$bX + ($bZ * 16)]; } - public function generateHeightmap($x, $z, &$chunkz) { + public function generateHeightmap($x, $z, &$chunkz){ $heightmapCPtr = EMPTY_16x16_ARR; - for($blockX = 0; $blockX < 16; ++$blockX) { - for($blockZ = 0; $blockZ < 16; ++$blockZ) { - for($Y = 7; $Y >= 0; --$Y) { - for($cY = 15; $cY >= 0; --$cY) { + for($blockX = 0; $blockX < 16; ++$blockX){ + for($blockZ = 0; $blockZ < 16; ++$blockZ){ + for($Y = 7; $Y >= 0; --$Y){ + for($cY = 15; $cY >= 0; --$cY){ $blockY = $Y * 16 + $cY; $blockID = ord($chunkz[$Y][$cY + ($blockX << 5) + ($blockZ << 9)]); if($blockID > 0) { @@ -233,12 +211,12 @@ class VanillaGenerator implements LevelGenerator { * @param array $chunks * @param Biome[] $biomes */ - public function buildSurfaces($chunkX, $chunkZ, &$chunks, $biomes) { + public function buildSurfaces($chunkX, $chunkZ, &$chunks, $biomes){ $this->sandNoises = $this->beachNoise->generateNoiseOctaves($chunkX * 16, $chunkZ * 16, 0, 16, 16, 1, 0.03125, 0.03125, 1); $this->gravelNoises = $this->beachNoise->generateNoiseOctaves($chunkX * 16, 109.01, $chunkZ * 16, 16, 1, 16, 0.03125, 1, 0.03125); $this->surfaceDepthNoises = $this->surfaceDepthNoise->generateNoiseOctaves($chunkX * 16, $chunkZ * 16, 0, 16, 16, 1, 0.0625, 0.0625, 0.0625); - for($blockX = 0; $blockX < 16; ++$blockX) { - for($blockZ = 0; $blockZ < 16; ++$blockZ) { + for($blockX = 0; $blockX < 16; ++$blockX){ + for($blockZ = 0; $blockZ < 16; ++$blockZ){ /** @var Biome $biome **/ $biome = $biomes[$blockX + ($blockZ * 16)]; $z = ($this->sandNoises[$blockX + ($blockZ * 16)] + ($this->rand->nextFloat() * 0.2)) > 0; @@ -247,28 +225,28 @@ class VanillaGenerator implements LevelGenerator { $i = -1; $b = $biome->topBlock; $b2 = $biome->fillerBlock; - for($blockY = 127; $blockY >= 0; --$blockY) { - if($blockY <= $this->rand->nextInt(5)) { + for($blockY = 127; $blockY >= 0; --$blockY){ + if($blockY <= $this->rand->nextInt(5)){ $chunks[$blockY >> 4][($blockY & 0xf) + ($blockZ << 5) + ($blockX << 9)] = "\x07"; - } else { + }else{ $b3 = ord($chunks[$blockY >> 4][($blockY & 0xf) + ($blockZ << 5) + ($blockX << 9)]); - if($b3 == 0) { + if($b3 == 0){ $i = -1; - } elseif($b3 == STONE) { - if($i == -1) { - if($nextFloat > 0) { - if($blockY >= 60 && $blockY <= 65) { - if($z) { + }elseif($b3 == STONE){ + if($i == -1){ + if($nextFloat > 0){ + if($blockY >= 60 && $blockY <= 65){ + if($z){ $b2 = $b = SAND; - } elseif($z2) { + }elseif($z2){ $b = 0; $b2 = GRAVEL; - } else { + }else{ $b = $biome->topBlock; $b2 = $biome->fillerBlock; } } - } else { + }else{ $b = 0; $b2 = STONE; } @@ -276,10 +254,10 @@ class VanillaGenerator implements LevelGenerator { $b = $blockY < 64 && $b == 0 ? STILL_WATER : $b; $i = $nextFloat; $chunks[$blockY >> 4][($blockY & 0xf) + ($blockZ << 5) + ($blockX << 9)] = $blockY >= 63 ? chr($b) : chr($b2); - } elseif($i > 0) { + }elseif($i > 0){ --$i; $chunks[$blockY >> 4][($blockY & 0xf) + ($blockZ << 5) + ($blockX << 9)] = chr($b2); - if($i == 0 && $b2 == SAND) { + if($i == 0 && $b2 == SAND){ $i = $this->rand->nextInt(4); $b2 = SANDSTONE; } @@ -291,12 +269,12 @@ class VanillaGenerator implements LevelGenerator { } } - public function prepareHeights($chunkX, $chunkZ, &$chunks, $biomes, $temperatures) { + public function prepareHeights($chunkX, $chunkZ, &$chunks, $biomes, $temperatures){ $this->heights = $this->getHeights($chunkX * 4, 0, $chunkZ * 4, 5, 17, 5); //if($chunkX == 15 && $chunkZ == 15) var_dump($this->heights); - for($unkX = 0; $unkX < 4; ++$unkX) { - for($unkZ = 0; $unkZ < 4; ++$unkZ) { - for($unkY = 0; $unkY < 16; ++$unkY) { + for($unkX = 0; $unkX < 4; ++$unkX){ + for($unkZ = 0; $unkZ < 4; ++$unkZ){ + for($unkY = 0; $unkY < 16; ++$unkY){ $f = $this->heights[(((($unkX) * 5) + $unkZ) * 17) + $unkY]; $f2 = $this->heights[(((($unkX) * 5) + $unkZ + 1) * 17) + $unkY]; $f3 = $this->heights[(((($unkX + 1) * 5) + $unkZ) * 17) + $unkY]; @@ -307,19 +285,19 @@ class VanillaGenerator implements LevelGenerator { $f7 = ($this->heights[(((($unkX + 1) * 5) + ($unkZ)) * 17) + ($unkY + 1)] - $f3) * 0.125; $f8 = ($this->heights[(((($unkX + 1) * 5) + ($unkZ + 1)) * 17) + ($unkY + 1)] - $f4) * 0.125; - for($unkYY = 0; $unkYY < 8; ++$unkYY) { + for($unkYY = 0; $unkYY < 8; ++$unkYY){ $f9 = $f; $f10 = $f2; $f11 = ($f3 - $f) * 0.25; $f12 = ($f4 - $f2) * 0.25; - for($unkXX = 0; $unkXX < 4; ++$unkXX) { + for($unkXX = 0; $unkXX < 4; ++$unkXX){ $f13 = $f9; $f14 = ($f10 - $f9) * 0.25; - for($unkZZ = 0; $unkZZ < 4; ++$unkZZ) { + for($unkZZ = 0; $unkZZ < 4; ++$unkZZ){ $d15 = $temperatures[((($unkX * 4) + $unkXX) * 16) + ($unkZ * 4) + $unkZZ]; $i3 = $f13 > 0; //true -> STONE, false -> AIR - if(!$i3 && ($unkY * 8) + $unkYY < 64) { + if(!$i3 && ($unkY * 8) + $unkYY < 64){ $i3 = $d15 < 0.5 && (($unkY * 8) + $unkYY) >= 63 ? ICE : STILL_WATER; } @@ -342,7 +320,7 @@ class VanillaGenerator implements LevelGenerator { } } - public function getHeights($chunkX, $chunkY, $chunkZ, $scaleX, $scaleY, $scaleZ) { + public function getHeights($chunkX, $chunkY, $chunkZ, $scaleX, $scaleY, $scaleZ){ $heights = array_fill(0, $scaleX * $scaleY * $scaleZ, 0); $rainNoises = $this->biomeSource->rainfallNoises; $tempNoises = $this->biomeSource->temperatureNoises; @@ -353,9 +331,9 @@ class VanillaGenerator implements LevelGenerator { $this->lowerInterpolationNoises = $this->lowerInterpolationNoise->generateNoiseOctaves($chunkX, $chunkY, $chunkZ, $scaleX, $scaleY, $scaleZ, 684.41, 684.41, 684.41); $k1 = $l1 = 0; $i2 = ((int) (16 / $scaleX)); - for($j2 = 0; $j2 < $scaleX; ++$j2) { + for($j2 = 0; $j2 < $scaleX; ++$j2){ $k2 = (int) ($j2 * $i2 + $i2 / 2); - for($l2 = 0; $l2 < $scaleZ; ++$l2) { + for($l2 = 0; $l2 < $scaleZ; ++$l2){ $i3 = (int) ($l2 * $i2 + $i2 / 2); $d2 = $tempNoises[$k2 * 16 + $i3]; $d3 = $rainNoises[$k2 * 16 + $i3] * $d2; @@ -366,58 +344,42 @@ class VanillaGenerator implements LevelGenerator { $d5 = (($this->biomeNoises[$l1] + 256) / 512); $d5 *= $d4; - if($d5 > 1) { - $d5 = 1; - } + if($d5 > 1) $d5 = 1; $d6 = $this->depthNoises[$l1] / 8000; - if($d6 < 0) { - $d6 = -$d6 * 0.3; - } + if($d6 < 0) $d6 = -$d6 * 0.3; $d6 = $d6 * 3 - 2; - if($d6 < 0) { + if($d6 < 0){ $d6 /= 2; - if($d6 < -1) { - $d6 = -1; - } + if($d6 < -1) $d6 = -1; $d6 /= 1.4; $d6 /= 2; $d5 = 0; - } else { - if($d6 > 1) { - $d6 = 1; - } + }else{ + if($d6 > 1) $d6 = 1; $d6 /= 8; } - if($d5 < 0) { - $d5 = 0; - } + if($d5 < 0) $d5 = 0; $d5 += 0.5; $d6 = ($d6 * $scaleY) / 16; $d7 = ($scaleY / 2 + $d6 * 4); ++$l1; - for($j3 = 0; $j3 < $scaleY; ++$j3) { + for($j3 = 0; $j3 < $scaleY; ++$j3){ $d8 = 0; $d9 = (((float) $j3 - $d7) * 12) / $d5; - if($d9 < 0) { - $d9 *= 4; - } + if($d9 < 0) $d9 *= 4; $d10 = $this->upperInterpolationNoises[$k1] / 512; $d11 = $this->lowerInterpolationNoises[$k1] / 512; $d12 = ($this->interpolationNoises[$k1] / 10 + 1) / 2; - if($d12 < 0) { - $d8 = $d10; - } elseif($d12 > 1) { - $d8 = $d11; - } else { - $d8 = $d10 + ($d11 - $d10) * $d12; - } + if($d12 < 0) $d8 = $d10; + elseif($d12 > 1) $d8 = $d11; + else $d8 = $d10 + ($d11 - $d10) * $d12; $d8 -= $d9; - if($j3 > $scaleY - 4) { + if($j3 > $scaleY - 4){ $d13 = ($j3 - ($scaleY - 4)) / 3; $d8 = $d8 * (1 - $d13) + -10 * $d13; } @@ -431,3 +393,4 @@ class VanillaGenerator implements LevelGenerator { } } + diff --git a/src/world/generator/vanilla/feature/TreeFeature.php b/src/world/generator/vanilla/feature/TreeFeature.php index b30b45ea..3ffd91fd 100644 --- a/src/world/generator/vanilla/feature/TreeFeature.php +++ b/src/world/generator/vanilla/feature/TreeFeature.php @@ -1,42 +1,39 @@ nextInt(3) + 4; $z2 = true; - if($y < 1 || $y + $nextInt + 1 > 128) { - return false; - } - for($i = $y; $i <= $y + 1 + $nextInt; ++$i) { + if($y < 1 || $y + $nextInt + 1 > 128) return false; + for($i = $y; $i <= $y + 1 + $nextInt; ++$i){ $i2 = ($i == $y) ? 0 : 1; - if($i >= (($y + 1) + $nextInt) - 2) { - $i2 = 2; - } + if($i >= (($y + 1) + $nextInt) - 2) $i2 = 2; - for($i3 = $x - $i2; $i3 <= $x + $i2 && $z2; ++$i3) { - for($i4 = $z - $i2; $i4 <= $z + $i2 && $z2; ++$i4) { - if($i >= 0 && $i < 128) { + for($i3 = $x - $i2; $i3 <= $x + $i2 && $z2; ++$i3){ + for($i4 = $z - $i2; $i4 <= $z + $i2 && $z2; ++$i4){ + if($i >= 0 && $i < 128){ $tileID = $level->level->getBlockID($i3, $i, $i4); - if($tileID != 0 && $tileID != LEAVES) { + if($tileID != 0 && $tileID != LEAVES){ return; } - } else { + }else{ return; } } } } - if($z2) { + if($z2){ $tileID = $level->level->getBlockID($x, $y - 1, $z); - if(($tileID == GRASS || $tileID == DIRT) && $y < (128 - $nextInt) - 1) { + if(($tileID == GRASS || $tileID == DIRT) && $y < (128 - $nextInt) - 1){ $level->level->setBlockID($x, $y - 1, $z, DIRT); } - for($i5 = ($y - 3) + $nextInt; $i5 <= $y + $nextInt; ++$i5) { + for($i5 = ($y - 3) + $nextInt; $i5 <= $y + $nextInt; ++$i5){ $i6 = $i5 - ($y + $nextInt); $i7 = (int) (1 - ($i6 / 2)); - for($i8 = $x - $i7; $i8 <= $x + $i7; ++$i8) { + for($i8 = $x - $i7; $i8 <= $x + $i7; ++$i8){ $i9 = $i8 - $x; - for($i10 = $z - $i7; $i10 <= $z + $i7; ++$i10) { + for($i10 = $z - $i7; $i10 <= $z + $i7; ++$i10){ $i11 = $i10 - $z; if((abs($i9) != $i7 || abs($i11) != $i7 || ($rand->nextInt(2) != 0 && $i6 != 0))/* && !Block.FULL_OPAQUE[world.getBlockIDAt(i8, i5, i10)]TODO opaque?*/) { $level->level->setBlockID($i8, $i5, $i10, LEAVES); @@ -44,12 +41,13 @@ class TreeFeature extends Feature { } } } - for($i12 = 0; $i12 < $nextInt; ++$i12) { + for($i12 = 0; $i12 < $nextInt; ++$i12){ $tileID = $level->level->getBlockID($x, $y + $i12, $z); - if($tileID == 0 || $tileID == LEAVES) { + if($tileID == 0 || $tileID == LEAVES){ $level->level->setBlockID($x, $y + $i12, $z, WOOD); } } } } } + From 1e85a6576438155a07d913860af6e19fbef074ff Mon Sep 17 00:00:00 2001 From: Sunch <120295462+Sunch233@users.noreply.github.com> Date: Tue, 29 Apr 2025 19:21:59 +0800 Subject: [PATCH 33/39] cs fix --- src/API/BlockAPI.php | 28 +- src/API/PlayerAPI.php | 20 +- src/Player.php | 2754 ++++++++--------- src/network/PacketPool.php | 2 +- src/network/protocol/ProtocolInfo.php | 702 ++--- .../protocol/packet/AddEntityPacket.php | 6 +- .../protocol/packet/AddItemEntityPacket.php | 6 +- .../protocol/packet/AddPaintingPacket.php | 6 +- .../packet/AdventureSettingsPacket.php | 14 +- src/network/protocol/packet/AnimatePacket.php | 26 +- src/network/protocol/packet/ChatPacket.php | 18 +- .../protocol/packet/ChunkDataPacket.php | 18 +- .../protocol/packet/ContainerAckPacket.php | 76 +- .../protocol/packet/ContainerClosePacket.php | 26 +- .../protocol/packet/ContainerOpenPacket.php | 26 +- .../packet/ContainerSetContentPacket.php | 22 +- .../packet/ContainerSetDataPacket.php | 22 +- .../packet/ContainerSetSlotPacket.php | 26 +- .../protocol/packet/DropItemPacket.php | 26 +- .../protocol/packet/EntityDataPacket.php | 14 +- .../protocol/packet/EntityEventPacket.php | 18 +- src/network/protocol/packet/ExplodePacket.php | 14 +- .../protocol/packet/HurtArmorPacket.php | 6 +- .../protocol/packet/InteractPacket.php | 22 +- .../protocol/packet/LevelEventPacket.php | 14 +- src/network/protocol/packet/LoginPacket.php | 4 +- src/network/protocol/packet/MessagePacket.php | 12 +- .../protocol/packet/MoveEntityPacket.php | 6 +- .../packet/MoveEntityPacket_PosRot.php | 6 +- .../protocol/packet/MovePlayerPacket.php | 22 +- .../protocol/packet/PlaceBlockPacket.php | 68 +- .../protocol/packet/PlayerActionPacket.php | 14 +- .../packet/PlayerArmorEquipmentPacket.php | 6 +- .../protocol/packet/PlayerEquipmentPacket.php | 18 +- .../protocol/packet/PlayerInputPacket.php | 10 +- .../protocol/packet/RemoveBlockPacket.php | 10 +- .../protocol/packet/RemoveEntityPacket.php | 6 +- .../protocol/packet/RequestChunkPacket.php | 18 +- src/network/protocol/packet/RespawnPacket.php | 26 +- .../protocol/packet/RotateHeadPacket.php | 6 +- .../protocol/packet/SendInventoryPacket.php | 26 +- .../protocol/packet/SetEntityDataPacket.php | 24 +- .../protocol/packet/SetEntityLinkPacket.php | 6 +- .../protocol/packet/SetEntityMotionPacket.php | 18 +- .../protocol/packet/SetHealthPacket.php | 26 +- .../packet/SetSpawnPositionPacket.php | 14 +- .../protocol/packet/TakeItemEntityPacket.php | 6 +- .../protocol/packet/TileEventPacket.php | 14 +- .../protocol/packet/UpdateBlockPacket.php | 10 +- src/network/protocol/packet/UseItemPacket.php | 32 +- src/network/raknet/RakNetDataPacket.php | 24 +- src/network/raknet/RakNetParser.php | 462 +-- 52 files changed, 2400 insertions(+), 2406 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index fde6521f..951e05da 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -645,12 +645,12 @@ class BlockAPI{ /** * 方块/物品id转换,防止新版本影响旧版本视觉效果 */ - public static function convertHighItemIdsToOldItemIds(int $protocolId, int $itemId) : int{ - if ($protocolId >= ProtocolInfo12::CURRENT_PROTOCOL_12) { - return $itemId; - } + public static function convertHighItemIdsToOldItemIds(int $protocolId, int $itemId) : int{ + if ($protocolId >= ProtocolInfo12::CURRENT_PROTOCOL_12) { + return $itemId; + } - $idMap = [ //default for protocol < 12 + $idMap = [ //default for protocol < 12 LIT_PUMPKIN => MELON_BLOCK, //block PUMPKIN_SEEDS => MELON_SEEDS, //item PUMPKIN_PIE => BREAD, //item @@ -658,14 +658,14 @@ class BlockAPI{ BEETROOT_SEEDS => SEEDS, //item ]; - if ($protocolId < ProtocolInfo9::CURRENT_PROTOCOL_9) { - $idMap += [ - NETHERRACK => OBSIDIAN, //block - NETHER_BRICK => BRICK, //item - NETHER_QUARTZ => BRICK, //item - ]; - } + if ($protocolId < ProtocolInfo9::CURRENT_PROTOCOL_9) { + $idMap += [ + NETHERRACK => OBSIDIAN, //block + NETHER_BRICK => BRICK, //item + NETHER_QUARTZ => BRICK, //item + ]; + } - return $idMap[$itemId] ?? $itemId; - } + return $idMap[$itemId] ?? $itemId; + } } diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 8605300d..2bba2486 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -327,16 +327,16 @@ class PlayerAPI{ return $o; } - /** - * @return int - */ - public static function decodeProtocol($ip){ - foreach(ServerAPI::request()->clients as $p) { - if($p->ip == $ip){ - return $p->PROTOCOL; - } - } - } + /** + * @return int + */ + public static function decodeProtocol($ip){ + foreach(ServerAPI::request()->clients as $p) { + if($p->ip == $ip){ + return $p->PROTOCOL; + } + } + } public function add($CID){ if(isset($this->server->clients[$CID])){ diff --git a/src/Player.php b/src/Player.php index 777d3667..8bf86ecf 100755 --- a/src/Player.php +++ b/src/Player.php @@ -78,7 +78,7 @@ class Player{ public $blockUpdateQueueLength = 0; public $PROTOCOL = ProtocolInfo::CURRENT_PROTOCOL; - + /** * Sent by a client while it is linked to some entity. * @var boolean $isJumping @@ -350,13 +350,13 @@ class Player{ if(EventHandler::callEvent(new DataPacketSendEvent($this, $packet)) === BaseEvent::DENY){ return; } - - $packet->PROTOCOL = $this->PROTOCOL; + + $packet->PROTOCOL = $this->PROTOCOL; $packet->encode(); - if($packet->pid() == 163 or $packet->pid() == 164 && $this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return; - } + if($packet->pid() == 163 or $packet->pid() == 164 && $this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return; + } $len = strlen($packet->buffer) + 1; $MTU = $this->MTU - 24; if($len > $MTU){ @@ -1209,9 +1209,9 @@ class Player{ } $this->received[$p->messageIndex] = true; } - $p->PROTOCOL = $this->PROTOCOL; + $p->PROTOCOL = $this->PROTOCOL; $p->decode(); - $this->handleDataPacket($p); + $this->handleDataPacket($p); } } } @@ -1275,7 +1275,7 @@ class Player{ $packet->z = $z; $packet->block = $id; $packet->meta = $meta; - $packet->PROTOCOL = $this->PROTOCOL; + $packet->PROTOCOL = $this->PROTOCOL; $packet->encode(); $len = 1 + strlen($packet->buffer); @@ -1308,7 +1308,7 @@ class Player{ $motion->speedX = $e->speedX; $motion->speedY = $e->speedY; $motion->speedZ = $e->speedZ; - $motion->PROTOCOL = $this->PROTOCOL; + $motion->PROTOCOL = $this->PROTOCOL; $motion->encode(); $len += 1 + strlen($motion->buffer); ++$packets; @@ -1330,7 +1330,7 @@ class Player{ $move->yaw = $e->yaw; $move->pitch = $e->pitch; $move->bodyYaw = $e->headYaw; - $move->PROTOCOL = $this->PROTOCOL; + $move->PROTOCOL = $this->PROTOCOL; $move->encode(); }else{ $move = new MoveEntityPacket_PosRot(); @@ -1340,19 +1340,19 @@ class Player{ $move->z = $e->z; $move->yaw = $e->yaw; $move->pitch = $e->pitch; - $move->PROTOCOL = $this->PROTOCOL; + $move->PROTOCOL = $this->PROTOCOL; $move->encode(); } $len += strlen($move->buffer) + 1; ++$packets; $moveSent = true; - - }else if($this->PROTOCOL > ProtocolInfo12::CURRENT_PROTOCOL_12 && $e->headYaw != $e->lastHeadYaw){ + + }elseif($this->PROTOCOL > ProtocolInfo12::CURRENT_PROTOCOL_12 && $e->headYaw != $e->lastHeadYaw){ $headyaw = new RotateHeadPacket(); $headyaw->eid = $e->eid; $headyaw->yaw = $e->headYaw; - $headyaw->PROTOCOL = $this->PROTOCOL; + $headyaw->PROTOCOL = $this->PROTOCOL; $headyaw->encode(); $len += strlen($headyaw->buffer) + 1; ++$packets; @@ -1468,7 +1468,6 @@ class Player{ $this->data->set("gamemode", $this->gamemode); } } - public function directDataPacket(RakNetDataPacket $packet, $reliability = 0, $recover = true){ if($this->connected === false){ @@ -1479,7 +1478,7 @@ class Player{ return []; } - $packet->PROTOCOL = $this->PROTOCOL; + $packet->PROTOCOL = $this->PROTOCOL; $packet->encode(); $pk = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $pk->data[] = $packet; @@ -1502,1371 +1501,1366 @@ class Player{ } } - public function sendPing() { - $pk = new PingPacket; - $pk->time = intdiv(hrtime(true), 1_000_000); - $this->directDataPacket($pk); - } + 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 getPing() { - return $this->lastPing; - } - public function handleDataPacket(RakNetDataPacket $packet) - { - if ($this->connected === false) { - return; - } - - if (EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY) { - return; - } - - 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 < ProtocolInfo3::CURRENT_PROTOCOL_3 && $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; - } - - $internalPid = $packet->getInternalPid(); - - switch ($internalPid) { - 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; - } - - - 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::PLACE_BLOCK_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 = []; - - if ($packet->face >= 0 and $packet->face <= 5) { - 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)) { - - } else { - $this->server->api->block->playerBlockAction($this, $blockVector, $packet->face, $packet->x, $packet->y, $packet->z); - 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; - } - 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; - } - } + { + if ($this->connected === false) { + return; + } + + if (EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY) { + return; + } + + 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 < ProtocolInfo3::CURRENT_PROTOCOL_3 && $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; + } + + $internalPid = $packet->getInternalPid(); + + switch ($internalPid) { + 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; + } elseif ($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::PLACE_BLOCK_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 = []; + + if ($packet->face >= 0 and $packet->face <= 5) { + 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)) { + + } else { + $this->server->api->block->playerBlockAction($this, $blockVector, $packet->face, $packet->x, $packet->y, $packet->z); + 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; + } + 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 diff --git a/src/network/PacketPool.php b/src/network/PacketPool.php index fa7ed5be..9c63130c 100644 --- a/src/network/PacketPool.php +++ b/src/network/PacketPool.php @@ -57,7 +57,7 @@ class PacketPool{ self::registerPacket(ContainerSetContentPacket::class); self::registerPacket(ContainerSetDataPacket::class); self::registerPacket(ContainerSetSlotPacket::class); - self::registerPacket(ContainerAckPacket::class); + self::registerPacket(ContainerAckPacket::class); self::registerPacket(DisconnectPacket::class); self::registerPacket(DropItemPacket::class); diff --git a/src/network/protocol/ProtocolInfo.php b/src/network/protocol/ProtocolInfo.php index ce7312b8..81f4986f 100644 --- a/src/network/protocol/ProtocolInfo.php +++ b/src/network/protocol/ProtocolInfo.php @@ -78,411 +78,411 @@ abstract class ProtocolInfo{ */ abstract class ProtocolInfo12{ - const CURRENT_PROTOCOL_12 = 12; + const CURRENT_PROTOCOL_12 = 12; - const PING_PACKET = 0x00; + const PING_PACKET = 0x00; - const PONG_PACKET = 0x03; + const PONG_PACKET = 0x03; - const CLIENT_CONNECT_PACKET = 0x09; - const SERVER_HANDSHAKE_PACKET = 0x10; + 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 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 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 = 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 MOVE_ENTITY_PACKET_POSROT = 0x93; + const ROTATE_HEAD_PACKET = 0xff; + 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 = 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 = 0xb4; - const CHAT_PACKET = 0xb5; - const ADVENTURE_SETTINGS_PACKET = 0xb6; - const ENTITY_DATA_PACKET = 0xb7; - const PLAYER_INPUT_PACKET = 0xb9; + 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 = 0xb4; + 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 CURRENT_PROTOCOL_9 = 9; - const PING_PACKET = 0x00; + const PING_PACKET = 0x00; - const PONG_PACKET = 0x03; + const PONG_PACKET = 0x03; - const CLIENT_CONNECT_PACKET = 0x09; - const SERVER_HANDSHAKE_PACKET = 0x10; + 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 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 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 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; + 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; } abstract class ProtocolInfo7{ - const CURRENT_PROTOCOL_7 = 7; + const CURRENT_PROTOCOL_7 = 7; - const PING_PACKET = 0x00; + const PING_PACKET = 0x00; - const PONG_PACKET = 0x03; + const PONG_PACKET = 0x03; - const CLIENT_CONNECT_PACKET = 0x09; - const SERVER_HANDSHAKE_PACKET = 0x10; + 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 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 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;//Change - const USE_ITEM_PACKET = 0xa1; - const PLAYER_ACTION_PACKET = 0xa2; - const SET_ENTITY_DATA_PACKET = 0xa3; - const SET_ENTITY_MOTION_PACKET = 0xa4; - //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; - 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 = 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 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 SET_ENTITY_DATA_PACKET = 0xa3; + const SET_ENTITY_MOTION_PACKET = 0xa4; + //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; + 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 = 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; } abstract class ProtocolInfo5{ - const CURRENT_PROTOCOL_5 = 5; + const CURRENT_PROTOCOL_5 = 5; - const PING_PACKET = 0x00; + const PING_PACKET = 0x00; - const PONG_PACKET = 0x03; + const PONG_PACKET = 0x03; - const CLIENT_CONNECT_PACKET = 0x09; - const SERVER_HANDSHAKE_PACKET = 0x10; + 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 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 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 = 0x98;// Change - const LEVEL_EVENT_PACKET = 0x99; - const TILE_EVENT_PACKET = 0x9a; - const ENTITY_EVENT_PACKET = 0x9b; - const REQUEST_CHUNK_PACKET = 0x9c; - const CHUNK_DATA_PACKET = 0x9d; - const PLAYER_EQUIPMENT_PACKET = 0x9e; - // const PLAYER_ARMOR_EQUIPMENT_PACKET = 0xa0; - const INTERACT_PACKET = 0x9f; - const USE_ITEM_PACKET = 0xa0; - const PLAYER_ACTION_PACKET = 0xa1; - const SET_ENTITY_DATA_PACKET = 0xa2; - const SET_ENTITY_MOTION_PACKET = 0xa3; - //const SET_ENTITY_LINK_PACKET = 0xa?; - const SET_HEALTH_PACKET = 0xa4; - //const SET_SPAWN_POSITION_PACKET = 0xa6; - const ANIMATE_PACKET = 0xa5; - const RESPAWN_PACKET = 0xa6; - const SEND_INVENTORY_PACKET = 0xa7; - const DROP_ITEM_PACKET = 0xa8; - const CONTAINER_OPEN_PACKET = 0xa9; - const CONTAINER_CLOSE_PACKET = 0xaa; - const CONTAINER_SET_SLOT_PACKET = 0xab; - const CONTAINER_SET_DATA_PACKET = 0xac; - const CONTAINER_SET_CONTENT_PACKET = 0xad; - const CONTAINER_ACK_PACKET = 0xae; - const CHAT_PACKET = 0xaf; - //const ADVENTURE_SETTINGS_PACKET = 0xb3; - //const ENTITY_DATA_PACKET = 0xb2; - //const PLAYER_INPUT_PACKET = 0xb9; + 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 = 0x98;// Change + const LEVEL_EVENT_PACKET = 0x99; + const TILE_EVENT_PACKET = 0x9a; + const ENTITY_EVENT_PACKET = 0x9b; + const REQUEST_CHUNK_PACKET = 0x9c; + const CHUNK_DATA_PACKET = 0x9d; + const PLAYER_EQUIPMENT_PACKET = 0x9e; + // const PLAYER_ARMOR_EQUIPMENT_PACKET = 0xa0; + const INTERACT_PACKET = 0x9f; + const USE_ITEM_PACKET = 0xa0; + const PLAYER_ACTION_PACKET = 0xa1; + const SET_ENTITY_DATA_PACKET = 0xa2; + const SET_ENTITY_MOTION_PACKET = 0xa3; + //const SET_ENTITY_LINK_PACKET = 0xa?; + const SET_HEALTH_PACKET = 0xa4; + //const SET_SPAWN_POSITION_PACKET = 0xa6; + const ANIMATE_PACKET = 0xa5; + const RESPAWN_PACKET = 0xa6; + const SEND_INVENTORY_PACKET = 0xa7; + const DROP_ITEM_PACKET = 0xa8; + const CONTAINER_OPEN_PACKET = 0xa9; + const CONTAINER_CLOSE_PACKET = 0xaa; + const CONTAINER_SET_SLOT_PACKET = 0xab; + const CONTAINER_SET_DATA_PACKET = 0xac; + const CONTAINER_SET_CONTENT_PACKET = 0xad; + const CONTAINER_ACK_PACKET = 0xae; + const CHAT_PACKET = 0xaf; + //const ADVENTURE_SETTINGS_PACKET = 0xb3; + //const ENTITY_DATA_PACKET = 0xb2; + //const PLAYER_INPUT_PACKET = 0xb9; } abstract class ProtocolInfo4{ - const CURRENT_PROTOCOL_4 = 4; + const CURRENT_PROTOCOL_4 = 4; - const PING_PACKET = 0x00; + const PING_PACKET = 0x00; - const PONG_PACKET = 0x03; + const PONG_PACKET = 0x03; - const CLIENT_CONNECT_PACKET = 0x09; - const SERVER_HANDSHAKE_PACKET = 0x10; + 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 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 = 0x8b; //Maybe exist // Change - const REMOVE_ENTITY_PACKET = 0x8c; - const ADD_ITEM_ENTITY_PACKET = 0x8d; - const TAKE_ITEM_ENTITY_PACKET = 0x8e; - const MOVE_ENTITY_PACKET = 0x8f;//unused ? + const ADD_ENTITY_PACKET = 0x8b; //Maybe exist // Change + const REMOVE_ENTITY_PACKET = 0x8c; + const ADD_ITEM_ENTITY_PACKET = 0x8d; + const TAKE_ITEM_ENTITY_PACKET = 0x8e; + const MOVE_ENTITY_PACKET = 0x8f;//unused ? - const MOVE_ENTITY_PACKET_POSROT = 0x92; - const MOVE_PLAYER_PACKET = 0x93; - const PLACE_BLOCK_PACKET = 0x94; - const REMOVE_BLOCK_PACKET = 0x95;//Maybe exist - const UPDATE_BLOCK_PACKET = 0x96; - //const ADD_PAINTING_PACKET = 0x98; - const EXPLODE_PACKET = 0x97; - const LEVEL_EVENT_PACKET = 0x98; - const TILE_EVENT_PACKET = 0x99; - const ENTITY_EVENT_PACKET = 0x9a; - const REQUEST_CHUNK_PACKET = 0x9b; - const CHUNK_DATA_PACKET = 0x9c; - const PLAYER_EQUIPMENT_PACKET = 0x9d; - // const PLAYER_ARMOR_EQUIPMENT_PACKET = 0xa0; - const INTERACT_PACKET = 0x9e; - const USE_ITEM_PACKET = 0x9f; - // const PLAYER_ACTION_PACKET = 0xa1; - const SET_ENTITY_DATA_PACKET = 0xa0; - const SET_ENTITY_MOTION_PACKET = 0xa1; - //const SET_ENTITY_LINK_PACKET = 0xa?; - const SET_HEALTH_PACKET = 0xa2; - //const SET_SPAWN_POSITION_PACKET = 0xa6; - const ANIMATE_PACKET = 0xa3; - const RESPAWN_PACKET = 0xa4; - const SEND_INVENTORY_PACKET = 0xa5; - const DROP_ITEM_PACKET = 0xa6; - const CONTAINER_OPEN_PACKET = 0xa7; - const CONTAINER_CLOSE_PACKET = 0xa8; - const CONTAINER_SET_SLOT_PACKET = 0xa9; - const CONTAINER_SET_DATA_PACKET = 0xaa; - const CONTAINER_SET_CONTENT_PACKET = 0xab; - const CONTAINER_ACK_PACKET = 0xac; - //const CHAT_PACKET = 0xaf; - //const ADVENTURE_SETTINGS_PACKET = 0xb3; - //const ENTITY_DATA_PACKET = 0xb2; - //const PLAYER_INPUT_PACKET = 0xb9; + const MOVE_ENTITY_PACKET_POSROT = 0x92; + const MOVE_PLAYER_PACKET = 0x93; + const PLACE_BLOCK_PACKET = 0x94; + const REMOVE_BLOCK_PACKET = 0x95;//Maybe exist + const UPDATE_BLOCK_PACKET = 0x96; + //const ADD_PAINTING_PACKET = 0x98; + const EXPLODE_PACKET = 0x97; + const LEVEL_EVENT_PACKET = 0x98; + const TILE_EVENT_PACKET = 0x99; + const ENTITY_EVENT_PACKET = 0x9a; + const REQUEST_CHUNK_PACKET = 0x9b; + const CHUNK_DATA_PACKET = 0x9c; + const PLAYER_EQUIPMENT_PACKET = 0x9d; + // const PLAYER_ARMOR_EQUIPMENT_PACKET = 0xa0; + const INTERACT_PACKET = 0x9e; + const USE_ITEM_PACKET = 0x9f; + // const PLAYER_ACTION_PACKET = 0xa1; + const SET_ENTITY_DATA_PACKET = 0xa0; + const SET_ENTITY_MOTION_PACKET = 0xa1; + //const SET_ENTITY_LINK_PACKET = 0xa?; + const SET_HEALTH_PACKET = 0xa2; + //const SET_SPAWN_POSITION_PACKET = 0xa6; + const ANIMATE_PACKET = 0xa3; + const RESPAWN_PACKET = 0xa4; + const SEND_INVENTORY_PACKET = 0xa5; + const DROP_ITEM_PACKET = 0xa6; + const CONTAINER_OPEN_PACKET = 0xa7; + const CONTAINER_CLOSE_PACKET = 0xa8; + const CONTAINER_SET_SLOT_PACKET = 0xa9; + const CONTAINER_SET_DATA_PACKET = 0xaa; + const CONTAINER_SET_CONTENT_PACKET = 0xab; + const CONTAINER_ACK_PACKET = 0xac; + //const CHAT_PACKET = 0xaf; + //const ADVENTURE_SETTINGS_PACKET = 0xb3; + //const ENTITY_DATA_PACKET = 0xb2; + //const PLAYER_INPUT_PACKET = 0xb9; } abstract class ProtocolInfo3{ - const CURRENT_PROTOCOL_3 = 3; + const CURRENT_PROTOCOL_3 = 3; - const PING_PACKET = 0x00; + const PING_PACKET = 0x00; - const PONG_PACKET = 0x03; + const PONG_PACKET = 0x03; - const CLIENT_CONNECT_PACKET = 0x09; - const SERVER_HANDSHAKE_PACKET = 0x10; + 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 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 = 0x8b; //Maybe exist - const REMOVE_ENTITY_PACKET = 0x8c; - const ADD_ITEM_ENTITY_PACKET = 0x8d; - const TAKE_ITEM_ENTITY_PACKET = 0x8e; - const MOVE_ENTITY_PACKET = 0x8f;//unused ? + const ADD_ENTITY_PACKET = 0x8b; //Maybe exist + const REMOVE_ENTITY_PACKET = 0x8c; + const ADD_ITEM_ENTITY_PACKET = 0x8d; + const TAKE_ITEM_ENTITY_PACKET = 0x8e; + const MOVE_ENTITY_PACKET = 0x8f;//unused ? - const MOVE_ENTITY_PACKET_POSROT = 0x92; - const MOVE_PLAYER_PACKET = 0x93; - const PLACE_BLOCK_PACKET = 0x94; - const REMOVE_BLOCK_PACKET = 0x95; - const UPDATE_BLOCK_PACKET = 0x96; - //const ADD_PAINTING_PACKET = 0x98; - const EXPLODE_PACKET = 0x97; - const LEVEL_EVENT_PACKET = 0x98; - //const TILE_EVENT_PACKET = 0x99; - const ENTITY_EVENT_PACKET = 0x99;//Change - const REQUEST_CHUNK_PACKET = 0x9a; - const CHUNK_DATA_PACKET = 0x9b;//Change - const PLAYER_EQUIPMENT_PACKET = 0x9c; - // const PLAYER_ARMOR_EQUIPMENT_PACKET = 0xa0; - const INTERACT_PACKET = 0x9d; - const USE_ITEM_PACKET = 0x9e; - // const PLAYER_ACTION_PACKET = 0xa1; - const SET_ENTITY_DATA_PACKET = 0x9f; - // const SET_ENTITY_MOTION_PACKET = 0xa1; - //const SET_ENTITY_LINK_PACKET = 0xa?; - const SET_HEALTH_PACKET = 0xa0; - //const SET_SPAWN_POSITION_PACKET = 0xa6; - const ANIMATE_PACKET = 0xa1; - const RESPAWN_PACKET = 0xa2; - const SEND_INVENTORY_PACKET = 0xa3; - const DROP_ITEM_PACKET = 0xa4; - const CONTAINER_OPEN_PACKET = 0xa7; - const CONTAINER_CLOSE_PACKET = 0xa8; - const CONTAINER_SET_SLOT_PACKET = 0xa9; - const CONTAINER_SET_DATA_PACKET = 0xaa; - const CONTAINER_SET_CONTENT_PACKET = 0xab; - const CONTAINER_ACK_PACKET = 0xac; - //const CHAT_PACKET = 0xaf; - //const ADVENTURE_SETTINGS_PACKET = 0xb3; - //const ENTITY_DATA_PACKET = 0xb2; - //const PLAYER_INPUT_PACKET = 0xb9; + const MOVE_ENTITY_PACKET_POSROT = 0x92; + const MOVE_PLAYER_PACKET = 0x93; + const PLACE_BLOCK_PACKET = 0x94; + const REMOVE_BLOCK_PACKET = 0x95; + const UPDATE_BLOCK_PACKET = 0x96; + //const ADD_PAINTING_PACKET = 0x98; + const EXPLODE_PACKET = 0x97; + const LEVEL_EVENT_PACKET = 0x98; + //const TILE_EVENT_PACKET = 0x99; + const ENTITY_EVENT_PACKET = 0x99;//Change + const REQUEST_CHUNK_PACKET = 0x9a; + const CHUNK_DATA_PACKET = 0x9b;//Change + const PLAYER_EQUIPMENT_PACKET = 0x9c; + // const PLAYER_ARMOR_EQUIPMENT_PACKET = 0xa0; + const INTERACT_PACKET = 0x9d; + const USE_ITEM_PACKET = 0x9e; + // const PLAYER_ACTION_PACKET = 0xa1; + const SET_ENTITY_DATA_PACKET = 0x9f; + // const SET_ENTITY_MOTION_PACKET = 0xa1; + //const SET_ENTITY_LINK_PACKET = 0xa?; + const SET_HEALTH_PACKET = 0xa0; + //const SET_SPAWN_POSITION_PACKET = 0xa6; + const ANIMATE_PACKET = 0xa1; + const RESPAWN_PACKET = 0xa2; + const SEND_INVENTORY_PACKET = 0xa3; + const DROP_ITEM_PACKET = 0xa4; + const CONTAINER_OPEN_PACKET = 0xa7; + const CONTAINER_CLOSE_PACKET = 0xa8; + const CONTAINER_SET_SLOT_PACKET = 0xa9; + const CONTAINER_SET_DATA_PACKET = 0xaa; + const CONTAINER_SET_CONTENT_PACKET = 0xab; + const CONTAINER_ACK_PACKET = 0xac; + //const CHAT_PACKET = 0xaf; + //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"); diff --git a/src/network/protocol/packet/AddEntityPacket.php b/src/network/protocol/packet/AddEntityPacket.php index 1295767c..93b3abc6 100644 --- a/src/network/protocol/packet/AddEntityPacket.php +++ b/src/network/protocol/packet/AddEntityPacket.php @@ -12,9 +12,9 @@ class AddEntityPacket extends RakNetDataPacket{ public $speedZ; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::ADD_ENTITY_PACKET; - } + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::ADD_ENTITY_PACKET; + } return ProtocolInfo::ADD_ENTITY_PACKET; } diff --git a/src/network/protocol/packet/AddItemEntityPacket.php b/src/network/protocol/packet/AddItemEntityPacket.php index 720a5041..8e10eba3 100644 --- a/src/network/protocol/packet/AddItemEntityPacket.php +++ b/src/network/protocol/packet/AddItemEntityPacket.php @@ -11,9 +11,9 @@ class AddItemEntityPacket extends RakNetDataPacket{ public $roll; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::ADD_ITEM_ENTITY_PACKET; - } + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::ADD_ITEM_ENTITY_PACKET; + } return ProtocolInfo::ADD_ITEM_ENTITY_PACKET; } diff --git a/src/network/protocol/packet/AddPaintingPacket.php b/src/network/protocol/packet/AddPaintingPacket.php index d5fb4dce..405f22d4 100644 --- a/src/network/protocol/packet/AddPaintingPacket.php +++ b/src/network/protocol/packet/AddPaintingPacket.php @@ -9,9 +9,9 @@ class AddPaintingPacket extends RakNetDataPacket{ public $title; public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::ADD_PAINTING_PACKET; - } + 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 1474a75e..8c1937e4 100644 --- a/src/network/protocol/packet/AdventureSettingsPacket.php +++ b/src/network/protocol/packet/AdventureSettingsPacket.php @@ -4,13 +4,13 @@ class AdventureSettingsPacket extends RakNetDataPacket{ public $flags; public function pid(){ - 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; - } + 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; + } return ProtocolInfo::ADVENTURE_SETTINGS_PACKET; } diff --git a/src/network/protocol/packet/AnimatePacket.php b/src/network/protocol/packet/AnimatePacket.php index 2c5744c9..1222d2a8 100644 --- a/src/network/protocol/packet/AnimatePacket.php +++ b/src/network/protocol/packet/AnimatePacket.php @@ -8,19 +8,19 @@ class AnimatePacket extends RakNetDataPacket{ public $eid; public function pid(){ - if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ - return ProtocolInfo3::ANIMATE_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::ANIMATE_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::ANIMATE_PACKET; - }elseif($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; - } + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::ANIMATE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::ANIMATE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::ANIMATE_PACKET; + }elseif($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; + } return ProtocolInfo::ANIMATE_PACKET; } diff --git a/src/network/protocol/packet/ChatPacket.php b/src/network/protocol/packet/ChatPacket.php index 2207a25f..4d4b10dd 100644 --- a/src/network/protocol/packet/ChatPacket.php +++ b/src/network/protocol/packet/ChatPacket.php @@ -4,15 +4,15 @@ class ChatPacket extends RakNetDataPacket{ public $message; public function pid(){ - if($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::CHAT_PACKET; - }elseif($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; - } + if($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::CHAT_PACKET; + }elseif($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; + } return ProtocolInfo::CHAT_PACKET; } diff --git a/src/network/protocol/packet/ChunkDataPacket.php b/src/network/protocol/packet/ChunkDataPacket.php index f996c216..c727f583 100644 --- a/src/network/protocol/packet/ChunkDataPacket.php +++ b/src/network/protocol/packet/ChunkDataPacket.php @@ -6,15 +6,15 @@ class ChunkDataPacket extends RakNetDataPacket{ public $data; public function pid(){ - if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ - return ProtocolInfo3::CHUNK_DATA_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::CHUNK_DATA_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::CHUNK_DATA_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::CHUNK_DATA_PACKET; - } + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::CHUNK_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::CHUNK_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::CHUNK_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::CHUNK_DATA_PACKET; + } return ProtocolInfo::CHUNK_DATA_PACKET; } diff --git a/src/network/protocol/packet/ContainerAckPacket.php b/src/network/protocol/packet/ContainerAckPacket.php index 71db5313..ae42a290 100644 --- a/src/network/protocol/packet/ContainerAckPacket.php +++ b/src/network/protocol/packet/ContainerAckPacket.php @@ -1,46 +1,46 @@ PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::CONTAINER_ACK_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::CONTAINER_ACK_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::CONTAINER_ACK_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ - return ProtocolInfo9::CONTAINER_ACK_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::CONTAINER_ACK_PACKET; - } - return ProtocolInfo::CONTAINER_ACK_PACKET; - } + public function pid(){ + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::CONTAINER_ACK_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::CONTAINER_ACK_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::CONTAINER_ACK_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + return ProtocolInfo9::CONTAINER_ACK_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::CONTAINER_ACK_PACKET; + } + return ProtocolInfo::CONTAINER_ACK_PACKET; + } - public function decode(){ - $this->unknwonubyte1 = $this->getByte(); - $this->unknownshort = $this->getShort(); - $this->write1 = $this->get(); - $this->write0 = $this->get(); - ConsoleAPI::debug($this->unknwonubyte1); - ConsoleAPI::debug($this->unknownshort); - ConsoleAPI::debug($this->write1); - ConsoleAPI::debug($this->write0); - } + public function decode(){ + $this->unknwonubyte1 = $this->getByte(); + $this->unknownshort = $this->getShort(); + $this->write1 = $this->get(); + $this->write0 = $this->get(); + ConsoleAPI::debug($this->unknwonubyte1); + ConsoleAPI::debug($this->unknownshort); + ConsoleAPI::debug($this->write1); + ConsoleAPI::debug($this->write0); + } - public function encode(){ - $this->putByte($this->unknwonubyte1); - $this->putShort($this->unknownshort); - $this->put($this->write1); - $this->put($this->write0); - ConsoleAPI::debug($this->unknwonubyte1); - ConsoleAPI::debug($this->unknownshort); - ConsoleAPI::debug($this->write1); - ConsoleAPI::debug($this->write0); - } + public function encode(){ + $this->putByte($this->unknwonubyte1); + $this->putShort($this->unknownshort); + $this->put($this->write1); + $this->put($this->write0); + ConsoleAPI::debug($this->unknwonubyte1); + ConsoleAPI::debug($this->unknownshort); + ConsoleAPI::debug($this->write1); + ConsoleAPI::debug($this->write0); + } } \ No newline at end of file diff --git a/src/network/protocol/packet/ContainerClosePacket.php b/src/network/protocol/packet/ContainerClosePacket.php index 97a2b5bf..913fcdb4 100644 --- a/src/network/protocol/packet/ContainerClosePacket.php +++ b/src/network/protocol/packet/ContainerClosePacket.php @@ -4,19 +4,19 @@ class ContainerClosePacket extends RakNetDataPacket{ public $windowid; public function pid(){ - if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ - return ProtocolInfo3::CONTAINER_CLOSE_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::CONTAINER_CLOSE_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::CONTAINER_CLOSE_PACKET; - }elseif($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; - } + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::CONTAINER_CLOSE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::CONTAINER_CLOSE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::CONTAINER_CLOSE_PACKET; + }elseif($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; + } return ProtocolInfo::CONTAINER_CLOSE_PACKET; } diff --git a/src/network/protocol/packet/ContainerOpenPacket.php b/src/network/protocol/packet/ContainerOpenPacket.php index c775b674..8c6f44da 100644 --- a/src/network/protocol/packet/ContainerOpenPacket.php +++ b/src/network/protocol/packet/ContainerOpenPacket.php @@ -9,19 +9,19 @@ class ContainerOpenPacket extends RakNetDataPacket{ public $z; public function pid(){ - if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ - return ProtocolInfo3::CONTAINER_OPEN_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::CONTAINER_OPEN_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::CONTAINER_OPEN_PACKET; - }elseif($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; - } + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::CONTAINER_OPEN_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::CONTAINER_OPEN_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::CONTAINER_OPEN_PACKET; + }elseif($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; + } return ProtocolInfo::CONTAINER_OPEN_PACKET; } diff --git a/src/network/protocol/packet/ContainerSetContentPacket.php b/src/network/protocol/packet/ContainerSetContentPacket.php index 82840abb..663187b5 100644 --- a/src/network/protocol/packet/ContainerSetContentPacket.php +++ b/src/network/protocol/packet/ContainerSetContentPacket.php @@ -6,19 +6,19 @@ class ContainerSetContentPacket extends RakNetDataPacket{ public $hotbar = []; public function pid(){ - if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ - return ProtocolInfo3::CONTAINER_SET_CONTENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::CONTAINER_SET_CONTENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::CONTAINER_SET_CONTENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::CONTAINER_SET_CONTENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::CONTAINER_SET_CONTENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::CONTAINER_SET_CONTENT_PACKET; + }elseif($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; - } + return ProtocolInfo9::CONTAINER_SET_CONTENT_PACKET; + }elseif($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 77c5f711..1a6c73ea 100644 --- a/src/network/protocol/packet/ContainerSetDataPacket.php +++ b/src/network/protocol/packet/ContainerSetDataPacket.php @@ -6,19 +6,19 @@ class ContainerSetDataPacket extends RakNetDataPacket{ public $value; public function pid(){ - if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ - return ProtocolInfo3::CONTAINER_SET_DATA_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::CONTAINER_SET_DATA_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::CONTAINER_SET_DATA_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::CONTAINER_SET_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::CONTAINER_SET_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::CONTAINER_SET_DATA_PACKET; + }elseif($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; - } + return ProtocolInfo9::CONTAINER_SET_DATA_PACKET; + }elseif($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 61bee052..f6e8d18d 100644 --- a/src/network/protocol/packet/ContainerSetSlotPacket.php +++ b/src/network/protocol/packet/ContainerSetSlotPacket.php @@ -6,19 +6,19 @@ class ContainerSetSlotPacket extends RakNetDataPacket{ public $item; public function pid(){ - if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ - return ProtocolInfo3::CONTAINER_SET_SLOT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::CONTAINER_SET_SLOT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::CONTAINER_SET_SLOT_PACKET; - }elseif($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; - } + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::CONTAINER_SET_SLOT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::CONTAINER_SET_SLOT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::CONTAINER_SET_SLOT_PACKET; + }elseif($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; + } return ProtocolInfo::CONTAINER_SET_SLOT_PACKET; } diff --git a/src/network/protocol/packet/DropItemPacket.php b/src/network/protocol/packet/DropItemPacket.php index 2f2de32c..0217d602 100644 --- a/src/network/protocol/packet/DropItemPacket.php +++ b/src/network/protocol/packet/DropItemPacket.php @@ -6,19 +6,19 @@ class DropItemPacket extends RakNetDataPacket{ public $item; public function pid(){ - if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ - return ProtocolInfo3::DROP_ITEM_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::DROP_ITEM_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::DROP_ITEM_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::DROP_ITEM_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ - return ProtocolInfo9::DROP_ITEM_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::DROP_ITEM_PACKET; - } + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::DROP_ITEM_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::DROP_ITEM_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::DROP_ITEM_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::DROP_ITEM_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + return ProtocolInfo9::DROP_ITEM_PACKET; + }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 9e9ff78fa..87512adb 100644 --- a/src/network/protocol/packet/EntityDataPacket.php +++ b/src/network/protocol/packet/EntityDataPacket.php @@ -7,13 +7,13 @@ class EntityDataPacket extends RakNetDataPacket{ public $namedtag; public function pid(){ - 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; - } + 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; + } return ProtocolInfo::ENTITY_DATA_PACKET; } diff --git a/src/network/protocol/packet/EntityEventPacket.php b/src/network/protocol/packet/EntityEventPacket.php index 4dbd4f0c..78daf662 100644 --- a/src/network/protocol/packet/EntityEventPacket.php +++ b/src/network/protocol/packet/EntityEventPacket.php @@ -14,15 +14,15 @@ class EntityEventPacket extends RakNetDataPacket{ } public function pid(){ - if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ - return ProtocolInfo3::ENTITY_EVENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::ENTITY_EVENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::ENTITY_EVENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::ENTITY_EVENT_PACKET; - } + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::ENTITY_EVENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::ENTITY_EVENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::ENTITY_EVENT_PACKET; + }elseif($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 cac8d6e8..c0bfae10 100644 --- a/src/network/protocol/packet/ExplodePacket.php +++ b/src/network/protocol/packet/ExplodePacket.php @@ -8,13 +8,13 @@ class ExplodePacket extends RakNetDataPacket{ public $records; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::EXPLODE_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::EXPLODE_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::EXPLODE_PACKET; - } + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::EXPLODE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::EXPLODE_PACKET; + }elseif($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 5f038955..a8ddcf98 100644 --- a/src/network/protocol/packet/HurtArmorPacket.php +++ b/src/network/protocol/packet/HurtArmorPacket.php @@ -4,9 +4,9 @@ class HurtArmorPacket extends RakNetDataPacket{ public $health; public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::HURT_ARMOR_PACKET; - } + 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 91f60f72..844edc7c 100644 --- a/src/network/protocol/packet/InteractPacket.php +++ b/src/network/protocol/packet/InteractPacket.php @@ -9,17 +9,17 @@ class InteractPacket extends RakNetDataPacket{ public $target; public function pid(){ - if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ - return ProtocolInfo3::INTERACT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::INTERACT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::INTERACT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::INTERACT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::INTERACT_PACKET; - } + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::INTERACT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::INTERACT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::INTERACT_PACKET; + }elseif($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/LevelEventPacket.php b/src/network/protocol/packet/LevelEventPacket.php index 2c0deda3..94d5b4cc 100644 --- a/src/network/protocol/packet/LevelEventPacket.php +++ b/src/network/protocol/packet/LevelEventPacket.php @@ -12,13 +12,13 @@ class LevelEventPacket extends RakNetDataPacket{ public $data; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::LEVEL_EVENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::LEVEL_EVENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::LEVEL_EVENT_PACKET; - } + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::LEVEL_EVENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::LEVEL_EVENT_PACKET; + }elseif($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 ba0f5919..9bd59938 100644 --- a/src/network/protocol/packet/LoginPacket.php +++ b/src/network/protocol/packet/LoginPacket.php @@ -17,8 +17,8 @@ class LoginPacket extends RakNetDataPacket{ $this->protocol2 = $this->getInt(); $this->clientId = $this->getInt();//null $this->loginData = $this->getString(); - $this->PROTOCOL = $this->protocol1; - } + $this->PROTOCOL = $this->protocol1; + } public function encode(){ diff --git a/src/network/protocol/packet/MessagePacket.php b/src/network/protocol/packet/MessagePacket.php index 50f72746..5af2520e 100644 --- a/src/network/protocol/packet/MessagePacket.php +++ b/src/network/protocol/packet/MessagePacket.php @@ -9,17 +9,17 @@ class MessagePacket extends RakNetDataPacket{ } public function decode(){ - if($this->PROTOCOL > ProtocolInfo9::CURRENT_PROTOCOL_9){ //0.6.1 and below From NostalgiaCore-BackPort Author:Gameherobrine - $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(); - if($this->PROTOCOL > ProtocolInfo9::CURRENT_PROTOCOL_9){ //0.6.1 and below From NostalgiaCore-BackPort Author:Gameherobrine - $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/MoveEntityPacket.php b/src/network/protocol/packet/MoveEntityPacket.php index 332748f6..1272ea68 100644 --- a/src/network/protocol/packet/MoveEntityPacket.php +++ b/src/network/protocol/packet/MoveEntityPacket.php @@ -3,9 +3,9 @@ class MoveEntityPacket extends RakNetDataPacket{ public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::MOVE_ENTITY_PACKET; - } + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::MOVE_ENTITY_PACKET; + } return ProtocolInfo::MOVE_ENTITY_PACKET; } diff --git a/src/network/protocol/packet/MoveEntityPacket_PosRot.php b/src/network/protocol/packet/MoveEntityPacket_PosRot.php index ce0ecae9..6d4ff310 100644 --- a/src/network/protocol/packet/MoveEntityPacket_PosRot.php +++ b/src/network/protocol/packet/MoveEntityPacket_PosRot.php @@ -9,9 +9,9 @@ class MoveEntityPacket_PosRot extends RakNetDataPacket{ public $pitch; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::MOVE_ENTITY_PACKET_POSROT; - } + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::MOVE_ENTITY_PACKET_POSROT; + } return ProtocolInfo::MOVE_ENTITY_PACKET_POSROT; } diff --git a/src/network/protocol/packet/MovePlayerPacket.php b/src/network/protocol/packet/MovePlayerPacket.php index 58cb258a..05153c50 100644 --- a/src/network/protocol/packet/MovePlayerPacket.php +++ b/src/network/protocol/packet/MovePlayerPacket.php @@ -10,11 +10,11 @@ class MovePlayerPacket extends RakNetDataPacket{ public $bodyYaw; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::MOVE_PLAYER_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::MOVE_PLAYER_PACKET; - } + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::MOVE_PLAYER_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::MOVE_PLAYER_PACKET; + } return ProtocolInfo::MOVE_PLAYER_PACKET; } @@ -25,9 +25,9 @@ class MovePlayerPacket extends RakNetDataPacket{ $this->z = $this->getFloat(); $this->yaw = $this->getFloat(); $this->pitch = $this->getFloat(); - if($this->PROTOCOL > 13){ - $this->bodyYaw = $this->getFloat(); - } + if($this->PROTOCOL > 13){ + $this->bodyYaw = $this->getFloat(); + } } public function encode(){ @@ -38,9 +38,9 @@ class MovePlayerPacket extends RakNetDataPacket{ $this->putFloat($this->z); $this->putFloat($this->yaw); $this->putFloat($this->pitch); - if($this->PROTOCOL > 13){ - $this->putFloat($this->bodyYaw); - } + if($this->PROTOCOL > 13){ + $this->putFloat($this->bodyYaw); + } } } \ No newline at end of file diff --git a/src/network/protocol/packet/PlaceBlockPacket.php b/src/network/protocol/packet/PlaceBlockPacket.php index ec25c760..f31b749d 100644 --- a/src/network/protocol/packet/PlaceBlockPacket.php +++ b/src/network/protocol/packet/PlaceBlockPacket.php @@ -1,42 +1,42 @@ PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::PLACE_BLOCK_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL) { - return ProtocolInfo12::PLACE_BLOCK_PACKET; - } - return ProtocolInfo::PLACE_BLOCK_PACKET; - } + public function pid(){ + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::PLACE_BLOCK_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL) { + return ProtocolInfo12::PLACE_BLOCK_PACKET; + } + return ProtocolInfo::PLACE_BLOCK_PACKET; + } - public function decode(){ - $this->eid = $this->getInt(); - $this->x = $this->getInt(); - $this->z = $this->getInt(); - $this->y = $this->getByte(); - $this->block = $this->getByte(); - $this->meta = $this->getByte(); - $this->face = $this->getByte(); - } + public function decode(){ + $this->eid = $this->getInt(); + $this->x = $this->getInt(); + $this->z = $this->getInt(); + $this->y = $this->getByte(); + $this->block = $this->getByte(); + $this->meta = $this->getByte(); + $this->face = $this->getByte(); + } - public function encode(){ - $this->reset(); - $this->putInt($this->eid); - $this->putInt($this->x); - $this->putInt($this->z); - $this->putByte($this->y); - $this->putByte(BlockAPI::convertHighItemIdsToOldItemIds($this->PROTOCOL, $this->block)); - $this->putByte($this->meta); - $this->putByte($this->face); - } + public function encode(){ + $this->reset(); + $this->putInt($this->eid); + $this->putInt($this->x); + $this->putInt($this->z); + $this->putByte($this->y); + $this->putByte(BlockAPI::convertHighItemIdsToOldItemIds($this->PROTOCOL, $this->block)); + $this->putByte($this->meta); + $this->putByte($this->face); + } } \ No newline at end of file diff --git a/src/network/protocol/packet/PlayerActionPacket.php b/src/network/protocol/packet/PlayerActionPacket.php index d2bea662..edc7ee4b 100644 --- a/src/network/protocol/packet/PlayerActionPacket.php +++ b/src/network/protocol/packet/PlayerActionPacket.php @@ -9,13 +9,13 @@ class PlayerActionPacket extends RakNetDataPacket{ public $eid; public function pid(){ - if($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::PLAYER_ACTION_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::PLAYER_ACTION_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::PLAYER_ACTION_PACKET; - } + if($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::PLAYER_ACTION_PACKET; + }elseif($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/PlayerArmorEquipmentPacket.php b/src/network/protocol/packet/PlayerArmorEquipmentPacket.php index 5e2b8e90..41424a25 100644 --- a/src/network/protocol/packet/PlayerArmorEquipmentPacket.php +++ b/src/network/protocol/packet/PlayerArmorEquipmentPacket.php @@ -5,9 +5,9 @@ class PlayerArmorEquipmentPacket extends RakNetDataPacket{ public $slots = []; public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::PLAYER_ARMOR_EQUIPMENT_PACKET; - } + 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 ee06a539..d313f7ce 100644 --- a/src/network/protocol/packet/PlayerEquipmentPacket.php +++ b/src/network/protocol/packet/PlayerEquipmentPacket.php @@ -7,15 +7,15 @@ class PlayerEquipmentPacket extends RakNetDataPacket{ public $slot; public function pid(){ - if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ - return ProtocolInfo3::PLAYER_EQUIPMENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::PLAYER_EQUIPMENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::PLAYER_EQUIPMENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::PLAYER_EQUIPMENT_PACKET; - } + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::PLAYER_EQUIPMENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::PLAYER_EQUIPMENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::PLAYER_EQUIPMENT_PACKET; + }elseif($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 ebd335f6..618dba70 100644 --- a/src/network/protocol/packet/PlayerInputPacket.php +++ b/src/network/protocol/packet/PlayerInputPacket.php @@ -8,11 +8,11 @@ class PlayerInputPacket extends RakNetDataPacket{ } public function pid(){ - if($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ - return ProtocolInfo9::PLAYER_INPUT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::PLAYER_INPUT_PACKET; - } + if($this->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 909657b9..1964cf4b 100644 --- a/src/network/protocol/packet/RemoveBlockPacket.php +++ b/src/network/protocol/packet/RemoveBlockPacket.php @@ -7,11 +7,11 @@ class RemoveBlockPacket extends RakNetDataPacket{ public $z; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::REMOVE_BLOCK_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::REMOVE_BLOCK_PACKET; - } + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::REMOVE_BLOCK_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ + return ProtocolInfo12::REMOVE_BLOCK_PACKET; + } return ProtocolInfo::REMOVE_BLOCK_PACKET; } diff --git a/src/network/protocol/packet/RemoveEntityPacket.php b/src/network/protocol/packet/RemoveEntityPacket.php index 5ef3e0b6..bbf7ea95 100644 --- a/src/network/protocol/packet/RemoveEntityPacket.php +++ b/src/network/protocol/packet/RemoveEntityPacket.php @@ -4,9 +4,9 @@ class RemoveEntityPacket extends RakNetDataPacket{ public $eid; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::REMOVE_ENTITY_PACKET; - } + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::REMOVE_ENTITY_PACKET; + } return ProtocolInfo::REMOVE_ENTITY_PACKET; } diff --git a/src/network/protocol/packet/RequestChunkPacket.php b/src/network/protocol/packet/RequestChunkPacket.php index 7d2ef64c..70c5e050 100644 --- a/src/network/protocol/packet/RequestChunkPacket.php +++ b/src/network/protocol/packet/RequestChunkPacket.php @@ -5,15 +5,15 @@ class RequestChunkPacket extends RakNetDataPacket{ public $chunkZ; public function pid(){ - if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ - return ProtocolInfo3::REQUEST_CHUNK_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::REQUEST_CHUNK_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::REQUEST_CHUNK_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::REQUEST_CHUNK_PACKET; - } + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::REQUEST_CHUNK_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::REQUEST_CHUNK_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::REQUEST_CHUNK_PACKET; + }elseif($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 adc61798..544193bc 100644 --- a/src/network/protocol/packet/RespawnPacket.php +++ b/src/network/protocol/packet/RespawnPacket.php @@ -7,19 +7,19 @@ class RespawnPacket extends RakNetDataPacket{ public $z; public function pid(){ - if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ - return ProtocolInfo3::RESPAWN_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::RESPAWN_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::RESPAWN_PACKET; - }elseif($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; - } + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::RESPAWN_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::RESPAWN_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::RESPAWN_PACKET; + }elseif($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; + } return ProtocolInfo::RESPAWN_PACKET; } diff --git a/src/network/protocol/packet/RotateHeadPacket.php b/src/network/protocol/packet/RotateHeadPacket.php index 3827c023..634e3851 100644 --- a/src/network/protocol/packet/RotateHeadPacket.php +++ b/src/network/protocol/packet/RotateHeadPacket.php @@ -11,9 +11,9 @@ class RotateHeadPacket extends RakNetDataPacket{ */ public $rawYaw = false; public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::ROTATE_HEAD_PACKET; - } + 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 8ac9f4bb..c762c7ee 100644 --- a/src/network/protocol/packet/SendInventoryPacket.php +++ b/src/network/protocol/packet/SendInventoryPacket.php @@ -7,19 +7,19 @@ class SendInventoryPacket extends RakNetDataPacket{ public $armor = []; public function pid(){ - if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ - return ProtocolInfo3::SEND_INVENTORY_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::SEND_INVENTORY_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::SEND_INVENTORY_PACKET; - }elseif($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; - } + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::SEND_INVENTORY_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::SEND_INVENTORY_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::SEND_INVENTORY_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ + return ProtocolInfo7::SEND_INVENTORY_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ + return ProtocolInfo9::SEND_INVENTORY_PACKET; + }elseif($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 d6f205bc..cb1c80d4 100644 --- a/src/network/protocol/packet/SetEntityDataPacket.php +++ b/src/network/protocol/packet/SetEntityDataPacket.php @@ -5,17 +5,17 @@ class SetEntityDataPacket extends RakNetDataPacket{ public $metadata; public function pid(){ - if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ - return ProtocolInfo3::SET_ENTITY_DATA_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::SET_ENTITY_DATA_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::SET_ENTITY_DATA_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::SET_ENTITY_DATA_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::SET_ENTITY_DATA_PACKET; - } + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::SET_ENTITY_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::SET_ENTITY_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::SET_ENTITY_DATA_PACKET; + }elseif($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; } @@ -25,7 +25,7 @@ class SetEntityDataPacket extends RakNetDataPacket{ public function encode(){ $this->reset(); - if($this->PROTOCOL >= ProtocolInfo7::CURRENT_PROTOCOL_7) + if($this->PROTOCOL >= ProtocolInfo7::CURRENT_PROTOCOL_7) $this->putInt($this->eid); $this->put(Utils::writeMetadata($this->metadata)); } diff --git a/src/network/protocol/packet/SetEntityLinkPacket.php b/src/network/protocol/packet/SetEntityLinkPacket.php index 68eaf805..c473ef13 100644 --- a/src/network/protocol/packet/SetEntityLinkPacket.php +++ b/src/network/protocol/packet/SetEntityLinkPacket.php @@ -22,9 +22,9 @@ class SetEntityLinkPacket extends RakNetDataPacket{ } public function pid(){ - if($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::SET_ENTITY_LINK_PACKET; - } + 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 78116795..1b58c07f 100644 --- a/src/network/protocol/packet/SetEntityMotionPacket.php +++ b/src/network/protocol/packet/SetEntityMotionPacket.php @@ -7,15 +7,15 @@ class SetEntityMotionPacket extends RakNetDataPacket{ public $speedZ; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::SET_ENTITY_MOTION_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::SET_ENTITY_MOTION_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::SET_ENTITY_MOTION_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::SET_ENTITY_MOTION_PACKET; - } + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::SET_ENTITY_MOTION_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::SET_ENTITY_MOTION_PACKET; + }elseif($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 0cb33616..02f36425 100644 --- a/src/network/protocol/packet/SetHealthPacket.php +++ b/src/network/protocol/packet/SetHealthPacket.php @@ -4,19 +4,19 @@ class SetHealthPacket extends RakNetDataPacket{ public $health; public function pid(){ - if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ - return ProtocolInfo3::SET_HEALTH_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::SET_HEALTH_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::SET_HEALTH_PACKET; - }elseif($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; - } + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::SET_HEALTH_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::SET_HEALTH_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::SET_HEALTH_PACKET; + }elseif($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; + } return ProtocolInfo::SET_HEALTH_PACKET; } diff --git a/src/network/protocol/packet/SetSpawnPositionPacket.php b/src/network/protocol/packet/SetSpawnPositionPacket.php index 2e767d32..ae9c7571 100644 --- a/src/network/protocol/packet/SetSpawnPositionPacket.php +++ b/src/network/protocol/packet/SetSpawnPositionPacket.php @@ -6,13 +6,13 @@ class SetSpawnPositionPacket extends RakNetDataPacket{ public $y; public function pid(){ - 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; - } + 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; + } return ProtocolInfo::SET_SPAWN_POSITION_PACKET; } diff --git a/src/network/protocol/packet/TakeItemEntityPacket.php b/src/network/protocol/packet/TakeItemEntityPacket.php index 22d8c892..314674eb 100644 --- a/src/network/protocol/packet/TakeItemEntityPacket.php +++ b/src/network/protocol/packet/TakeItemEntityPacket.php @@ -5,9 +5,9 @@ class TakeItemEntityPacket extends RakNetDataPacket{ public $eid; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::TAKE_ITEM_ENTITY_PACKET; - } + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::TAKE_ITEM_ENTITY_PACKET; + } return ProtocolInfo::TAKE_ITEM_ENTITY_PACKET; } diff --git a/src/network/protocol/packet/TileEventPacket.php b/src/network/protocol/packet/TileEventPacket.php index a15e5c73..af225590 100644 --- a/src/network/protocol/packet/TileEventPacket.php +++ b/src/network/protocol/packet/TileEventPacket.php @@ -8,13 +8,13 @@ class TileEventPacket extends RakNetDataPacket{ public $case2; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::TILE_EVENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::TILE_EVENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::TILE_EVENT_PACKET; - } + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::TILE_EVENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::TILE_EVENT_PACKET; + }elseif($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 239aa472..271ba18d 100644 --- a/src/network/protocol/packet/UpdateBlockPacket.php +++ b/src/network/protocol/packet/UpdateBlockPacket.php @@ -8,11 +8,11 @@ class UpdateBlockPacket extends RakNetDataPacket{ public $meta; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::UPDATE_BLOCK_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::UPDATE_BLOCK_PACKET; - } + if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::UPDATE_BLOCK_PACKET; + }elseif($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 19fd3128..379cd58f 100644 --- a/src/network/protocol/packet/UseItemPacket.php +++ b/src/network/protocol/packet/UseItemPacket.php @@ -16,17 +16,17 @@ class UseItemPacket extends RakNetDataPacket{ public $posZ; public function pid(){ - if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ - return ProtocolInfo3::USE_ITEM_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ - return ProtocolInfo4::USE_ITEM_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::USE_ITEM_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::USE_ITEM_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ - return ProtocolInfo12::USE_ITEM_PACKET; - } + if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ + return ProtocolInfo3::USE_ITEM_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + return ProtocolInfo4::USE_ITEM_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + return ProtocolInfo5::USE_ITEM_PACKET; + }elseif($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; } @@ -41,11 +41,11 @@ class UseItemPacket extends RakNetDataPacket{ $this->fx = $this->getFloat(); $this->fy = $this->getFloat(); $this->fz = $this->getFloat(); - if ($this->PROTOCOL > ProtocolInfo9::CURRENT_PROTOCOL_9) { - $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(){ diff --git a/src/network/raknet/RakNetDataPacket.php b/src/network/raknet/RakNetDataPacket.php index b3d8da8b..4c882c4f 100755 --- a/src/network/raknet/RakNetDataPacket.php +++ b/src/network/raknet/RakNetDataPacket.php @@ -15,7 +15,7 @@ abstract class RakNetDataPacket extends stdClass{ private $offset = 0; public $PROTOCOL = ProtocolInfo::CURRENT_PROTOCOL; - + abstract public function encode(); abstract public function decode(); @@ -37,21 +37,21 @@ abstract class RakNetDataPacket extends stdClass{ $this->setBuffer(chr($this->pid())); } - public function isPacketExist(int $protocol) : int{ + public function isPacketExist(int $protocol) : int{ - $exist = true; - return $exist; - } + $exist = true; + return $exist; + } 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; - } + 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); diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php index af5498a1..20b08b58 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -4,13 +4,13 @@ class RakNetParser{ public $packet; private $buffer; - private $source; + private $source; private $offset; - private $isparse = false; + private $isparse = false; public function __construct(&$buffer, $source){ - $this->source =& $source; - $this->buffer =& $buffer; + $this->source = &$source; + $this->buffer = &$buffer; $this->offset = 0; if(strlen($this->buffer) > 0){ $this->parse(); @@ -59,8 +59,8 @@ class RakNetParser{ case RakNetInfo::DATA_PACKET_F: $this->packet->seqNumber = $this->getLTriad(); $this->packet->data = []; - $this->packet->ip = $this->source; - $PROTOCOL = PlayerAPI::decodeProtocol($this->packet->ip); + $this->packet->ip = $this->source; + $PROTOCOL = PlayerAPI::decodeProtocol($this->packet->ip); while(!$this->feof() and ($pk = $this->parseDataPacket($PROTOCOL)) instanceof RakNetDataPacket){ $this->packet->data[] = $pk; @@ -184,234 +184,234 @@ 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; - } + 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($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($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; - } + 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 179baa471a4311067121d7d2bf91883fcdecc43a Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <121912649+yefengeeeeeeeeeee@users.noreply.github.com> Date: Tue, 29 Apr 2025 21:35:07 +0800 Subject: [PATCH 34/39] Blame yefengeee moment blame --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f2fb0184..ffbe02f9 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.7.4, 0.7.6, 0.8.0, 0.8.1 (maybe 0.8.2) +Supported MCPE versions: 0.3.0 ~ 0.8.1 (maybe 0.8.2) (Special thx to GullCraft Network and DartMiner43) From 93df0e3f0de339642d4a03b637450ef176762dfe Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Wed, 30 Apr 2025 20:36:06 +0800 Subject: [PATCH 35/39] Avoid Crash if server sending the packet which didnt exist in older version serverfullpacket: Who m I --- src/Player.php | 6 +- src/network/PacketPool.php | 7 + src/network/raknet/RakNetDataPacket.php | 6 - src/network/raknet/RakNetParser.php | 230 ------------------------ 4 files changed, 12 insertions(+), 237 deletions(-) diff --git a/src/Player.php b/src/Player.php index 8bf86ecf..43092aa1 100755 --- a/src/Player.php +++ b/src/Player.php @@ -351,7 +351,11 @@ class Player{ return; } - $packet->PROTOCOL = $this->PROTOCOL; + if(PacketPool::isPacketExist($packet->pid(), $this->PROTOCOL) != true){ + return; + } + + $packet->PROTOCOL = $this->PROTOCOL; $packet->encode(); if($packet->pid() == 163 or $packet->pid() == 164 && $this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ diff --git a/src/network/PacketPool.php b/src/network/PacketPool.php index 9c63130c..83c7eb4c 100644 --- a/src/network/PacketPool.php +++ b/src/network/PacketPool.php @@ -35,6 +35,13 @@ class PacketPool{ return $pk; } + public static function isPacketExist($pid, $protocol) : bool{ + if(isset(self::$packetPool[$protocol][$pid])) { + return true; + } + return false; + } + public static function init() : void{ if(self::$isInit){ return; diff --git a/src/network/raknet/RakNetDataPacket.php b/src/network/raknet/RakNetDataPacket.php index 4c882c4f..43a3a5b5 100755 --- a/src/network/raknet/RakNetDataPacket.php +++ b/src/network/raknet/RakNetDataPacket.php @@ -37,12 +37,6 @@ 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 20b08b58..bc0d939b 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -6,7 +6,6 @@ class RakNetParser{ private $buffer; private $source; private $offset; - private $isparse = false; public function __construct(&$buffer, $source){ $this->source = &$source; @@ -184,235 +183,6 @@ 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 9903b8b31b3b808e2e8823eecfbbce5950a51c71 Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Sat, 3 May 2025 13:27:46 +0800 Subject: [PATCH 36/39] Fixed Wrong ProtocolVersion && Some Packet Fixed --- src/Player.php | 2 +- src/network/PacketPool.php | 14 +++++++------- src/network/protocol/ProtocolInfo.php | 8 ++++---- .../protocol/packet/AddEntityPacket.php | 2 +- .../protocol/packet/AddItemEntityPacket.php | 2 +- .../packet/AdventureSettingsPacket.php | 2 +- src/network/protocol/packet/AnimatePacket.php | 8 ++++---- src/network/protocol/packet/ChatPacket.php | 6 +++--- .../protocol/packet/ChunkDataPacket.php | 6 +++--- .../protocol/packet/ContainerAckPacket.php | 8 ++++---- .../protocol/packet/ContainerClosePacket.php | 8 ++++---- .../protocol/packet/ContainerOpenPacket.php | 18 +++++++++++------- .../packet/ContainerSetContentPacket.php | 8 ++++---- .../protocol/packet/ContainerSetDataPacket.php | 8 ++++---- .../protocol/packet/ContainerSetSlotPacket.php | 8 ++++---- src/network/protocol/packet/DropItemPacket.php | 8 ++++---- .../protocol/packet/EntityDataPacket.php | 2 +- .../protocol/packet/EntityEventPacket.php | 6 +++--- src/network/protocol/packet/ExplodePacket.php | 6 +++--- src/network/protocol/packet/InteractPacket.php | 8 ++++---- .../protocol/packet/LevelEventPacket.php | 6 +++--- .../protocol/packet/MoveEntityPacket.php | 2 +- .../packet/MoveEntityPacket_PosRot.php | 2 +- .../protocol/packet/MovePlayerPacket.php | 2 +- .../protocol/packet/PlaceBlockPacket.php | 2 +- .../protocol/packet/PlayerActionPacket.php | 6 +++--- .../protocol/packet/PlayerEquipmentPacket.php | 6 +++--- .../protocol/packet/RemoveBlockPacket.php | 2 +- .../protocol/packet/RemoveEntityPacket.php | 2 +- .../protocol/packet/RequestChunkPacket.php | 6 +++--- src/network/protocol/packet/RespawnPacket.php | 8 ++++---- .../protocol/packet/SendInventoryPacket.php | 8 ++++---- .../protocol/packet/SetEntityDataPacket.php | 10 +++++----- .../protocol/packet/SetEntityMotionPacket.php | 8 ++++---- .../protocol/packet/SetHealthPacket.php | 8 ++++---- .../protocol/packet/SetSpawnPositionPacket.php | 2 +- .../protocol/packet/TakeItemEntityPacket.php | 2 +- .../protocol/packet/TileEventPacket.php | 6 +++--- .../protocol/packet/UpdateBlockPacket.php | 2 +- src/network/protocol/packet/UseItemPacket.php | 8 ++++---- 40 files changed, 120 insertions(+), 116 deletions(-) diff --git a/src/Player.php b/src/Player.php index 43092aa1..f3e02972 100755 --- a/src/Player.php +++ b/src/Player.php @@ -358,7 +358,7 @@ class Player{ $packet->PROTOCOL = $this->PROTOCOL; $packet->encode(); - if($packet->pid() == 163 or $packet->pid() == 164 && $this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ + if($packet->pid() == 163 or $packet->pid() == 164 && $this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ return; } $len = strlen($packet->buffer) + 1; diff --git a/src/network/PacketPool.php b/src/network/PacketPool.php index 83c7eb4c..8774e022 100644 --- a/src/network/PacketPool.php +++ b/src/network/PacketPool.php @@ -50,12 +50,12 @@ class PacketPool{ self::registerPacket(AddEntityPacket::class); self::registerPacket(AddItemEntityPacket::class); self::registerPacket(AddMobPacket::class); - self::registerPacket(AddPaintingPacket::class, [7, 8, 9, 10, 11, 12, 13, 14]); + self::registerPacket(AddPaintingPacket::class, [8, 9, 10, 11, 12, 13, 14]); self::registerPacket(AddPlayerPacket::class); - self::registerPacket(AdventureSettingsPacket::class, [7, 8, 9, 10, 11, 12, 13, 14]); + self::registerPacket(AdventureSettingsPacket::class, [8, 9, 10, 11, 12, 13, 14]); self::registerPacket(AnimatePacket::class); - self::registerPacket(ChatPacket::class, [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); + self::registerPacket(ChatPacket::class, [7, 8, 9, 10, 11, 12, 13, 14]); self::registerPacket(ChunkDataPacket::class); self::registerPacket(ClientConnectPacket::class); self::registerPacket(ClientHandshakePacket::class); @@ -69,7 +69,7 @@ class PacketPool{ self::registerPacket(DisconnectPacket::class); self::registerPacket(DropItemPacket::class); - self::registerPacket(EntityDataPacket::class, [7, 8, 9, 10, 11, 12, 13, 14]); + self::registerPacket(EntityDataPacket::class, [8, 9, 10, 11, 12, 13, 14]); self::registerPacket(EntityEventPacket::class); self::registerPacket(ExplodePacket::class); @@ -88,10 +88,10 @@ class PacketPool{ self::registerPacket(PingPacket::class); self::registerPacket(PlaceBlockPacket::class); - self::registerPacket(PlayerActionPacket::class, [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); + self::registerPacket(PlayerActionPacket::class, [7, 8, 9, 10, 11, 12, 13, 14]); self::registerPacket(PlayerArmorEquipmentPacket::class, [9, 10, 11, 12, 13, 14]); self::registerPacket(PlayerEquipmentPacket::class); - self::registerPacket(PlayerInputPacket::class, [7, 8, 9, 10, 11, 12, 13, 14]); + self::registerPacket(PlayerInputPacket::class, [8, 9, 10, 11, 12, 13, 14]); self::registerPacket(PongPacket::class); self::registerPacket(ReadyPacket::class); @@ -108,7 +108,7 @@ class PacketPool{ self::registerPacket(SetEntityLinkPacket::class, [12, 13, 14]); self::registerPacket(SetEntityMotionPacket::class, [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); self::registerPacket(SetHealthPacket::class); - self::registerPacket(SetSpawnPositionPacket::class, [7, 8, 9, 10, 11, 12, 13, 14]); + self::registerPacket(SetSpawnPositionPacket::class, [8, 9, 10, 11, 12, 13, 14]); self::registerPacket(SetTimePacket::class); self::registerPacket(StartGamePacket::class); diff --git a/src/network/protocol/ProtocolInfo.php b/src/network/protocol/ProtocolInfo.php index 81f4986f..708fa6ec 100644 --- a/src/network/protocol/ProtocolInfo.php +++ b/src/network/protocol/ProtocolInfo.php @@ -216,9 +216,9 @@ abstract class ProtocolInfo9{ const PLAYER_INPUT_PACKET = 0xb9; } -abstract class ProtocolInfo7{ +abstract class ProtocolInfo8{ - const CURRENT_PROTOCOL_7 = 7; + const CURRENT_PROTOCOL_8 = 8; const PING_PACKET = 0x00; @@ -283,9 +283,9 @@ abstract class ProtocolInfo7{ const ENTITY_DATA_PACKET = 0xb2; const PLAYER_INPUT_PACKET = 0xb9; } -abstract class ProtocolInfo5{ +abstract class ProtocolInfo6{ - const CURRENT_PROTOCOL_5 = 5; + const CURRENT_PROTOCOL_6 = 6; const PING_PACKET = 0x00; diff --git a/src/network/protocol/packet/AddEntityPacket.php b/src/network/protocol/packet/AddEntityPacket.php index 93b3abc6..d01d2fd7 100644 --- a/src/network/protocol/packet/AddEntityPacket.php +++ b/src/network/protocol/packet/AddEntityPacket.php @@ -12,7 +12,7 @@ class AddEntityPacket extends RakNetDataPacket{ public $speedZ; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + if($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::ADD_ENTITY_PACKET; } return ProtocolInfo::ADD_ENTITY_PACKET; diff --git a/src/network/protocol/packet/AddItemEntityPacket.php b/src/network/protocol/packet/AddItemEntityPacket.php index 8e10eba3..0ba0e2c6 100644 --- a/src/network/protocol/packet/AddItemEntityPacket.php +++ b/src/network/protocol/packet/AddItemEntityPacket.php @@ -11,7 +11,7 @@ class AddItemEntityPacket extends RakNetDataPacket{ public $roll; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + if($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::ADD_ITEM_ENTITY_PACKET; } return ProtocolInfo::ADD_ITEM_ENTITY_PACKET; diff --git a/src/network/protocol/packet/AdventureSettingsPacket.php b/src/network/protocol/packet/AdventureSettingsPacket.php index 8c1937e4..c19c89da 100644 --- a/src/network/protocol/packet/AdventureSettingsPacket.php +++ b/src/network/protocol/packet/AdventureSettingsPacket.php @@ -5,7 +5,7 @@ class AdventureSettingsPacket extends RakNetDataPacket{ public function pid(){ if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::ADVENTURE_SETTINGS_PACKET; + return ProtocolInfo8::ADVENTURE_SETTINGS_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::ADVENTURE_SETTINGS_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ diff --git a/src/network/protocol/packet/AnimatePacket.php b/src/network/protocol/packet/AnimatePacket.php index 1222d2a8..84b980cf 100644 --- a/src/network/protocol/packet/AnimatePacket.php +++ b/src/network/protocol/packet/AnimatePacket.php @@ -10,12 +10,12 @@ class AnimatePacket extends RakNetDataPacket{ public function pid(){ if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ return ProtocolInfo3::ANIMATE_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + }elseif($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::ANIMATE_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::ANIMATE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::ANIMATE_PACKET; }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::ANIMATE_PACKET; + return ProtocolInfo8::ANIMATE_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::ANIMATE_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ diff --git a/src/network/protocol/packet/ChatPacket.php b/src/network/protocol/packet/ChatPacket.php index 4d4b10dd..cc750b03 100644 --- a/src/network/protocol/packet/ChatPacket.php +++ b/src/network/protocol/packet/ChatPacket.php @@ -4,10 +4,10 @@ class ChatPacket extends RakNetDataPacket{ public $message; public function pid(){ - if($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::CHAT_PACKET; + if($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::CHAT_PACKET; }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::CHAT_PACKET; + return ProtocolInfo8::CHAT_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::CHAT_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ diff --git a/src/network/protocol/packet/ChunkDataPacket.php b/src/network/protocol/packet/ChunkDataPacket.php index c727f583..1c6cbe1e 100644 --- a/src/network/protocol/packet/ChunkDataPacket.php +++ b/src/network/protocol/packet/ChunkDataPacket.php @@ -8,10 +8,10 @@ class ChunkDataPacket extends RakNetDataPacket{ public function pid(){ if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ return ProtocolInfo3::CHUNK_DATA_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + }elseif($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::CHUNK_DATA_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::CHUNK_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::CHUNK_DATA_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::CHUNK_DATA_PACKET; } diff --git a/src/network/protocol/packet/ContainerAckPacket.php b/src/network/protocol/packet/ContainerAckPacket.php index ae42a290..93309e64 100644 --- a/src/network/protocol/packet/ContainerAckPacket.php +++ b/src/network/protocol/packet/ContainerAckPacket.php @@ -7,12 +7,12 @@ class ContainerAckPacket extends RakNetDataPacket{ public $write0; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + if($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::CONTAINER_ACK_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::CONTAINER_ACK_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::CONTAINER_ACK_PACKET; }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::CONTAINER_ACK_PACKET; + return ProtocolInfo8::CONTAINER_ACK_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::CONTAINER_ACK_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ diff --git a/src/network/protocol/packet/ContainerClosePacket.php b/src/network/protocol/packet/ContainerClosePacket.php index 913fcdb4..be516472 100644 --- a/src/network/protocol/packet/ContainerClosePacket.php +++ b/src/network/protocol/packet/ContainerClosePacket.php @@ -6,12 +6,12 @@ class ContainerClosePacket extends RakNetDataPacket{ public function pid(){ if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ return ProtocolInfo3::CONTAINER_CLOSE_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + }elseif($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::CONTAINER_CLOSE_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::CONTAINER_CLOSE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::CONTAINER_CLOSE_PACKET; }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::CONTAINER_CLOSE_PACKET; + return ProtocolInfo8::CONTAINER_CLOSE_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::CONTAINER_CLOSE_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ diff --git a/src/network/protocol/packet/ContainerOpenPacket.php b/src/network/protocol/packet/ContainerOpenPacket.php index 8c6f44da..afdb66ed 100644 --- a/src/network/protocol/packet/ContainerOpenPacket.php +++ b/src/network/protocol/packet/ContainerOpenPacket.php @@ -11,12 +11,12 @@ class ContainerOpenPacket extends RakNetDataPacket{ public function pid(){ if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ return ProtocolInfo3::CONTAINER_OPEN_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + }elseif($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::CONTAINER_OPEN_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::CONTAINER_OPEN_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::CONTAINER_OPEN_PACKET; }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::CONTAINER_OPEN_PACKET; + return ProtocolInfo8::CONTAINER_OPEN_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::CONTAINER_OPEN_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ @@ -34,9 +34,13 @@ class ContainerOpenPacket extends RakNetDataPacket{ $this->putByte($this->windowid); $this->putByte($this->type); $this->putByte($this->slots); - $this->putInt($this->x);//String - $this->putInt($this->y); - $this->putInt($this->z); + if ($this->PROTOCOL <= ProtocolInfo9::CURRENT_PROTOCOL_9){ + $this->putString(); + }else{ + $this->putInt($this->x); + $this->putInt($this->y); + $this->putInt($this->z); + } } } \ No newline at end of file diff --git a/src/network/protocol/packet/ContainerSetContentPacket.php b/src/network/protocol/packet/ContainerSetContentPacket.php index 663187b5..97a5ce78 100644 --- a/src/network/protocol/packet/ContainerSetContentPacket.php +++ b/src/network/protocol/packet/ContainerSetContentPacket.php @@ -8,12 +8,12 @@ class ContainerSetContentPacket extends RakNetDataPacket{ public function pid(){ if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ return ProtocolInfo3::CONTAINER_SET_CONTENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + }elseif($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::CONTAINER_SET_CONTENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::CONTAINER_SET_CONTENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::CONTAINER_SET_CONTENT_PACKET; }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::CONTAINER_SET_CONTENT_PACKET; + return ProtocolInfo8::CONTAINER_SET_CONTENT_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::CONTAINER_SET_CONTENT_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ diff --git a/src/network/protocol/packet/ContainerSetDataPacket.php b/src/network/protocol/packet/ContainerSetDataPacket.php index 1a6c73ea..50cbaa1e 100644 --- a/src/network/protocol/packet/ContainerSetDataPacket.php +++ b/src/network/protocol/packet/ContainerSetDataPacket.php @@ -8,12 +8,12 @@ class ContainerSetDataPacket extends RakNetDataPacket{ public function pid(){ if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ return ProtocolInfo3::CONTAINER_SET_DATA_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + }elseif($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::CONTAINER_SET_DATA_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::CONTAINER_SET_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::CONTAINER_SET_DATA_PACKET; }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::CONTAINER_SET_DATA_PACKET; + return ProtocolInfo8::CONTAINER_SET_DATA_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::CONTAINER_SET_DATA_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ diff --git a/src/network/protocol/packet/ContainerSetSlotPacket.php b/src/network/protocol/packet/ContainerSetSlotPacket.php index f6e8d18d..7a5d8ef6 100644 --- a/src/network/protocol/packet/ContainerSetSlotPacket.php +++ b/src/network/protocol/packet/ContainerSetSlotPacket.php @@ -8,12 +8,12 @@ class ContainerSetSlotPacket extends RakNetDataPacket{ public function pid(){ if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ return ProtocolInfo3::CONTAINER_SET_SLOT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + }elseif($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::CONTAINER_SET_SLOT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::CONTAINER_SET_SLOT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::CONTAINER_SET_SLOT_PACKET; }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::CONTAINER_SET_SLOT_PACKET; + return ProtocolInfo8::CONTAINER_SET_SLOT_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::CONTAINER_SET_SLOT_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ diff --git a/src/network/protocol/packet/DropItemPacket.php b/src/network/protocol/packet/DropItemPacket.php index 0217d602..364afd11 100644 --- a/src/network/protocol/packet/DropItemPacket.php +++ b/src/network/protocol/packet/DropItemPacket.php @@ -8,12 +8,12 @@ class DropItemPacket extends RakNetDataPacket{ public function pid(){ if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ return ProtocolInfo3::DROP_ITEM_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + }elseif($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::DROP_ITEM_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::DROP_ITEM_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::DROP_ITEM_PACKET; }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::DROP_ITEM_PACKET; + return ProtocolInfo8::DROP_ITEM_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::DROP_ITEM_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ diff --git a/src/network/protocol/packet/EntityDataPacket.php b/src/network/protocol/packet/EntityDataPacket.php index 87512adb..b78c7358 100644 --- a/src/network/protocol/packet/EntityDataPacket.php +++ b/src/network/protocol/packet/EntityDataPacket.php @@ -8,7 +8,7 @@ class EntityDataPacket extends RakNetDataPacket{ public function pid(){ if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::ENTITY_DATA_PACKET; + return ProtocolInfo8::ENTITY_DATA_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::ENTITY_DATA_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ diff --git a/src/network/protocol/packet/EntityEventPacket.php b/src/network/protocol/packet/EntityEventPacket.php index 78daf662..4ddb4d8c 100644 --- a/src/network/protocol/packet/EntityEventPacket.php +++ b/src/network/protocol/packet/EntityEventPacket.php @@ -16,10 +16,10 @@ class EntityEventPacket extends RakNetDataPacket{ public function pid(){ if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ return ProtocolInfo3::ENTITY_EVENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + }elseif($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::ENTITY_EVENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::ENTITY_EVENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::ENTITY_EVENT_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::ENTITY_EVENT_PACKET; } diff --git a/src/network/protocol/packet/ExplodePacket.php b/src/network/protocol/packet/ExplodePacket.php index c0bfae10..5d0930f8 100644 --- a/src/network/protocol/packet/ExplodePacket.php +++ b/src/network/protocol/packet/ExplodePacket.php @@ -8,10 +8,10 @@ class ExplodePacket extends RakNetDataPacket{ public $records; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + if($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::EXPLODE_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::EXPLODE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::EXPLODE_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::EXPLODE_PACKET; } diff --git a/src/network/protocol/packet/InteractPacket.php b/src/network/protocol/packet/InteractPacket.php index 844edc7c..3f07246e 100644 --- a/src/network/protocol/packet/InteractPacket.php +++ b/src/network/protocol/packet/InteractPacket.php @@ -11,12 +11,12 @@ class InteractPacket extends RakNetDataPacket{ public function pid(){ if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ return ProtocolInfo3::INTERACT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + }elseif($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::INTERACT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::INTERACT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::INTERACT_PACKET; }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::INTERACT_PACKET; + return ProtocolInfo8::INTERACT_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::INTERACT_PACKET; } diff --git a/src/network/protocol/packet/LevelEventPacket.php b/src/network/protocol/packet/LevelEventPacket.php index 94d5b4cc..4dba836b 100644 --- a/src/network/protocol/packet/LevelEventPacket.php +++ b/src/network/protocol/packet/LevelEventPacket.php @@ -12,10 +12,10 @@ class LevelEventPacket extends RakNetDataPacket{ public $data; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + if($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::LEVEL_EVENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::LEVEL_EVENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::LEVEL_EVENT_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::LEVEL_EVENT_PACKET; } diff --git a/src/network/protocol/packet/MoveEntityPacket.php b/src/network/protocol/packet/MoveEntityPacket.php index 1272ea68..18032c02 100644 --- a/src/network/protocol/packet/MoveEntityPacket.php +++ b/src/network/protocol/packet/MoveEntityPacket.php @@ -3,7 +3,7 @@ class MoveEntityPacket extends RakNetDataPacket{ public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + if($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::MOVE_ENTITY_PACKET; } return ProtocolInfo::MOVE_ENTITY_PACKET; diff --git a/src/network/protocol/packet/MoveEntityPacket_PosRot.php b/src/network/protocol/packet/MoveEntityPacket_PosRot.php index 6d4ff310..5ec9632d 100644 --- a/src/network/protocol/packet/MoveEntityPacket_PosRot.php +++ b/src/network/protocol/packet/MoveEntityPacket_PosRot.php @@ -9,7 +9,7 @@ class MoveEntityPacket_PosRot extends RakNetDataPacket{ public $pitch; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + if($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::MOVE_ENTITY_PACKET_POSROT; } return ProtocolInfo::MOVE_ENTITY_PACKET_POSROT; diff --git a/src/network/protocol/packet/MovePlayerPacket.php b/src/network/protocol/packet/MovePlayerPacket.php index 05153c50..276ae2e0 100644 --- a/src/network/protocol/packet/MovePlayerPacket.php +++ b/src/network/protocol/packet/MovePlayerPacket.php @@ -10,7 +10,7 @@ class MovePlayerPacket extends RakNetDataPacket{ public $bodyYaw; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + if($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::MOVE_PLAYER_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::MOVE_PLAYER_PACKET; diff --git a/src/network/protocol/packet/PlaceBlockPacket.php b/src/network/protocol/packet/PlaceBlockPacket.php index f31b749d..743738bd 100644 --- a/src/network/protocol/packet/PlaceBlockPacket.php +++ b/src/network/protocol/packet/PlaceBlockPacket.php @@ -10,7 +10,7 @@ class PlaceBlockPacket extends RakNetDataPacket{ public $face; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + if($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::PLACE_BLOCK_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL) { return ProtocolInfo12::PLACE_BLOCK_PACKET; diff --git a/src/network/protocol/packet/PlayerActionPacket.php b/src/network/protocol/packet/PlayerActionPacket.php index edc7ee4b..9e423e71 100644 --- a/src/network/protocol/packet/PlayerActionPacket.php +++ b/src/network/protocol/packet/PlayerActionPacket.php @@ -9,10 +9,10 @@ class PlayerActionPacket extends RakNetDataPacket{ public $eid; public function pid(){ - if($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::PLAYER_ACTION_PACKET; + if($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::PLAYER_ACTION_PACKET; }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::PLAYER_ACTION_PACKET; + return ProtocolInfo8::PLAYER_ACTION_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::PLAYER_ACTION_PACKET; } diff --git a/src/network/protocol/packet/PlayerEquipmentPacket.php b/src/network/protocol/packet/PlayerEquipmentPacket.php index d313f7ce..c7806eb5 100644 --- a/src/network/protocol/packet/PlayerEquipmentPacket.php +++ b/src/network/protocol/packet/PlayerEquipmentPacket.php @@ -9,10 +9,10 @@ class PlayerEquipmentPacket extends RakNetDataPacket{ public function pid(){ if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ return ProtocolInfo3::PLAYER_EQUIPMENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + }elseif($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::PLAYER_EQUIPMENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::PLAYER_EQUIPMENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::PLAYER_EQUIPMENT_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::PLAYER_EQUIPMENT_PACKET; } diff --git a/src/network/protocol/packet/RemoveBlockPacket.php b/src/network/protocol/packet/RemoveBlockPacket.php index 1964cf4b..f7ba3de5 100644 --- a/src/network/protocol/packet/RemoveBlockPacket.php +++ b/src/network/protocol/packet/RemoveBlockPacket.php @@ -7,7 +7,7 @@ class RemoveBlockPacket extends RakNetDataPacket{ public $z; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + if($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::REMOVE_BLOCK_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::REMOVE_BLOCK_PACKET; diff --git a/src/network/protocol/packet/RemoveEntityPacket.php b/src/network/protocol/packet/RemoveEntityPacket.php index bbf7ea95..c835d9a9 100644 --- a/src/network/protocol/packet/RemoveEntityPacket.php +++ b/src/network/protocol/packet/RemoveEntityPacket.php @@ -4,7 +4,7 @@ class RemoveEntityPacket extends RakNetDataPacket{ public $eid; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + if($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::REMOVE_ENTITY_PACKET; } return ProtocolInfo::REMOVE_ENTITY_PACKET; diff --git a/src/network/protocol/packet/RequestChunkPacket.php b/src/network/protocol/packet/RequestChunkPacket.php index 70c5e050..33c1ecc2 100644 --- a/src/network/protocol/packet/RequestChunkPacket.php +++ b/src/network/protocol/packet/RequestChunkPacket.php @@ -7,10 +7,10 @@ class RequestChunkPacket extends RakNetDataPacket{ public function pid(){ if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ return ProtocolInfo3::REQUEST_CHUNK_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + }elseif($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::REQUEST_CHUNK_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::REQUEST_CHUNK_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::REQUEST_CHUNK_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::REQUEST_CHUNK_PACKET; } diff --git a/src/network/protocol/packet/RespawnPacket.php b/src/network/protocol/packet/RespawnPacket.php index 544193bc..c88a856a 100644 --- a/src/network/protocol/packet/RespawnPacket.php +++ b/src/network/protocol/packet/RespawnPacket.php @@ -9,12 +9,12 @@ class RespawnPacket extends RakNetDataPacket{ public function pid(){ if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ return ProtocolInfo3::RESPAWN_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + }elseif($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::RESPAWN_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::RESPAWN_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::RESPAWN_PACKET; }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::RESPAWN_PACKET; + return ProtocolInfo8::RESPAWN_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::RESPAWN_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ diff --git a/src/network/protocol/packet/SendInventoryPacket.php b/src/network/protocol/packet/SendInventoryPacket.php index c762c7ee..e1e2ef0a 100644 --- a/src/network/protocol/packet/SendInventoryPacket.php +++ b/src/network/protocol/packet/SendInventoryPacket.php @@ -9,12 +9,12 @@ class SendInventoryPacket extends RakNetDataPacket{ public function pid(){ if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ return ProtocolInfo3::SEND_INVENTORY_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + }elseif($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::SEND_INVENTORY_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::SEND_INVENTORY_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::SEND_INVENTORY_PACKET; }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::SEND_INVENTORY_PACKET; + return ProtocolInfo8::SEND_INVENTORY_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::SEND_INVENTORY_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ diff --git a/src/network/protocol/packet/SetEntityDataPacket.php b/src/network/protocol/packet/SetEntityDataPacket.php index cb1c80d4..eac162d3 100644 --- a/src/network/protocol/packet/SetEntityDataPacket.php +++ b/src/network/protocol/packet/SetEntityDataPacket.php @@ -7,12 +7,12 @@ class SetEntityDataPacket extends RakNetDataPacket{ public function pid(){ if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ return ProtocolInfo3::SET_ENTITY_DATA_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + }elseif($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::SET_ENTITY_DATA_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::SET_ENTITY_DATA_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::SET_ENTITY_DATA_PACKET; }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::SET_ENTITY_DATA_PACKET; + return ProtocolInfo8::SET_ENTITY_DATA_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::SET_ENTITY_DATA_PACKET; } @@ -25,7 +25,7 @@ class SetEntityDataPacket extends RakNetDataPacket{ public function encode(){ $this->reset(); - if($this->PROTOCOL >= ProtocolInfo7::CURRENT_PROTOCOL_7) + if($this->PROTOCOL >= ProtocolInfo8::CURRENT_PROTOCOL_8) $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 1b58c07f..5bd44bf6 100644 --- a/src/network/protocol/packet/SetEntityMotionPacket.php +++ b/src/network/protocol/packet/SetEntityMotionPacket.php @@ -7,12 +7,12 @@ class SetEntityMotionPacket extends RakNetDataPacket{ public $speedZ; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + if($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::SET_ENTITY_MOTION_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::SET_ENTITY_MOTION_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::SET_ENTITY_MOTION_PACKET; }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::SET_ENTITY_MOTION_PACKET; + return ProtocolInfo8::SET_ENTITY_MOTION_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::SET_ENTITY_MOTION_PACKET; } diff --git a/src/network/protocol/packet/SetHealthPacket.php b/src/network/protocol/packet/SetHealthPacket.php index 02f36425..0ef18bdb 100644 --- a/src/network/protocol/packet/SetHealthPacket.php +++ b/src/network/protocol/packet/SetHealthPacket.php @@ -6,12 +6,12 @@ class SetHealthPacket extends RakNetDataPacket{ public function pid(){ if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ return ProtocolInfo3::SET_HEALTH_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + }elseif($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::SET_HEALTH_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::SET_HEALTH_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::SET_HEALTH_PACKET; }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::SET_HEALTH_PACKET; + return ProtocolInfo8::SET_HEALTH_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::SET_HEALTH_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ diff --git a/src/network/protocol/packet/SetSpawnPositionPacket.php b/src/network/protocol/packet/SetSpawnPositionPacket.php index ae9c7571..a072a131 100644 --- a/src/network/protocol/packet/SetSpawnPositionPacket.php +++ b/src/network/protocol/packet/SetSpawnPositionPacket.php @@ -7,7 +7,7 @@ class SetSpawnPositionPacket extends RakNetDataPacket{ public function pid(){ if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::SET_SPAWN_POSITION_PACKET; + return ProtocolInfo8::SET_SPAWN_POSITION_PACKET; }elseif($this->PROTOCOL < ProtocolInfo12::CURRENT_PROTOCOL_12){ return ProtocolInfo9::SET_SPAWN_POSITION_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ diff --git a/src/network/protocol/packet/TakeItemEntityPacket.php b/src/network/protocol/packet/TakeItemEntityPacket.php index 314674eb..5c3e9eae 100644 --- a/src/network/protocol/packet/TakeItemEntityPacket.php +++ b/src/network/protocol/packet/TakeItemEntityPacket.php @@ -5,7 +5,7 @@ class TakeItemEntityPacket extends RakNetDataPacket{ public $eid; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + if($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::TAKE_ITEM_ENTITY_PACKET; } return ProtocolInfo::TAKE_ITEM_ENTITY_PACKET; diff --git a/src/network/protocol/packet/TileEventPacket.php b/src/network/protocol/packet/TileEventPacket.php index af225590..ef33178b 100644 --- a/src/network/protocol/packet/TileEventPacket.php +++ b/src/network/protocol/packet/TileEventPacket.php @@ -8,10 +8,10 @@ class TileEventPacket extends RakNetDataPacket{ public $case2; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + if($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::TILE_EVENT_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::TILE_EVENT_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::TILE_EVENT_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::TILE_EVENT_PACKET; } diff --git a/src/network/protocol/packet/UpdateBlockPacket.php b/src/network/protocol/packet/UpdateBlockPacket.php index 271ba18d..d1e85f11 100644 --- a/src/network/protocol/packet/UpdateBlockPacket.php +++ b/src/network/protocol/packet/UpdateBlockPacket.php @@ -8,7 +8,7 @@ class UpdateBlockPacket extends RakNetDataPacket{ public $meta; public function pid(){ - if($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + if($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::UPDATE_BLOCK_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::UPDATE_BLOCK_PACKET; diff --git a/src/network/protocol/packet/UseItemPacket.php b/src/network/protocol/packet/UseItemPacket.php index 379cd58f..593df77d 100644 --- a/src/network/protocol/packet/UseItemPacket.php +++ b/src/network/protocol/packet/UseItemPacket.php @@ -18,12 +18,12 @@ class UseItemPacket extends RakNetDataPacket{ public function pid(){ if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ return ProtocolInfo3::USE_ITEM_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo5::CURRENT_PROTOCOL_5){ + }elseif($this->PROTOCOL < ProtocolInfo6::CURRENT_PROTOCOL_6){ return ProtocolInfo4::USE_ITEM_PACKET; - }elseif($this->PROTOCOL < ProtocolInfo7::CURRENT_PROTOCOL_7){ - return ProtocolInfo5::USE_ITEM_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::USE_ITEM_PACKET; }elseif($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9){ - return ProtocolInfo7::USE_ITEM_PACKET; + return ProtocolInfo8::USE_ITEM_PACKET; }elseif($this->PROTOCOL < ProtocolInfo::CURRENT_PROTOCOL){ return ProtocolInfo12::USE_ITEM_PACKET; } From a365acc7277c5253fe1d0d99c760aac012ecfa6f Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Sat, 3 May 2025 15:32:20 +0800 Subject: [PATCH 37/39] Thanks to @Gameherobrine --- src/network/PacketPool.php | 4 ++-- .../protocol/packet/ContainerAckPacket.php | 12 ++++++++++++ .../protocol/packet/ContainerOpenPacket.php | 3 ++- .../packet/ContainerSetContentPacket.php | 8 +++++++- src/network/protocol/packet/DropItemPacket.php | 6 +++++- src/network/protocol/packet/LoginPacket.php | 3 ++- .../protocol/packet/MoveEntityPacket.php | 2 +- .../protocol/packet/PlayerActionPacket.php | 9 ++++++++- src/network/protocol/packet/ReadyPacket.php | 4 +++- .../protocol/packet/RemoveBlockPacket.php | 7 ++++++- .../protocol/packet/RequestChunkPacket.php | 5 ++++- .../protocol/packet/SetEntityDataPacket.php | 1 - src/network/protocol/packet/SetTimePacket.php | 4 +++- src/network/protocol/packet/UseItemPacket.php | 17 ++++++++++++++++- 14 files changed, 71 insertions(+), 14 deletions(-) diff --git a/src/network/PacketPool.php b/src/network/PacketPool.php index 8774e022..4f691b52 100644 --- a/src/network/PacketPool.php +++ b/src/network/PacketPool.php @@ -55,7 +55,7 @@ class PacketPool{ self::registerPacket(AdventureSettingsPacket::class, [8, 9, 10, 11, 12, 13, 14]); self::registerPacket(AnimatePacket::class); - self::registerPacket(ChatPacket::class, [7, 8, 9, 10, 11, 12, 13, 14]); + self::registerPacket(ChatPacket::class, [6, 7, 8, 9, 10, 11, 12, 13, 14]); self::registerPacket(ChunkDataPacket::class); self::registerPacket(ClientConnectPacket::class); self::registerPacket(ClientHandshakePacket::class); @@ -88,7 +88,7 @@ class PacketPool{ self::registerPacket(PingPacket::class); self::registerPacket(PlaceBlockPacket::class); - self::registerPacket(PlayerActionPacket::class, [7, 8, 9, 10, 11, 12, 13, 14]); + self::registerPacket(PlayerActionPacket::class, [6, 7, 8, 9, 10, 11, 12, 13, 14]); self::registerPacket(PlayerArmorEquipmentPacket::class, [9, 10, 11, 12, 13, 14]); self::registerPacket(PlayerEquipmentPacket::class); self::registerPacket(PlayerInputPacket::class, [8, 9, 10, 11, 12, 13, 14]); diff --git a/src/network/protocol/packet/ContainerAckPacket.php b/src/network/protocol/packet/ContainerAckPacket.php index 93309e64..9b411215 100644 --- a/src/network/protocol/packet/ContainerAckPacket.php +++ b/src/network/protocol/packet/ContainerAckPacket.php @@ -2,6 +2,7 @@ class ContainerAckPacket extends RakNetDataPacket{ public $unknwonubyte1; + public $unknwonubyte2; // Only exist in Version 0.3.3 && 0.4.0 public $unknownshort; public $write1; public $write0; @@ -24,6 +25,10 @@ class ContainerAckPacket extends RakNetDataPacket{ public function decode(){ $this->unknwonubyte1 = $this->getByte(); $this->unknownshort = $this->getShort(); + if($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + $this->unknwonubyte2 = $this->getByte(); + return; + } $this->write1 = $this->get(); $this->write0 = $this->get(); ConsoleAPI::debug($this->unknwonubyte1); @@ -33,8 +38,15 @@ class ContainerAckPacket extends RakNetDataPacket{ } public function encode(){ + if($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + $this->reset(); + } $this->putByte($this->unknwonubyte1); $this->putShort($this->unknownshort); + if($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + $this->putByte($this->unknwonubyte2); + return; + } $this->put($this->write1); $this->put($this->write0); ConsoleAPI::debug($this->unknwonubyte1); diff --git a/src/network/protocol/packet/ContainerOpenPacket.php b/src/network/protocol/packet/ContainerOpenPacket.php index afdb66ed..e2fca894 100644 --- a/src/network/protocol/packet/ContainerOpenPacket.php +++ b/src/network/protocol/packet/ContainerOpenPacket.php @@ -7,6 +7,7 @@ class ContainerOpenPacket extends RakNetDataPacket{ public $x; public $y; public $z; + public $unknownstring; public function pid(){ if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ @@ -35,7 +36,7 @@ class ContainerOpenPacket extends RakNetDataPacket{ $this->putByte($this->type); $this->putByte($this->slots); if ($this->PROTOCOL <= ProtocolInfo9::CURRENT_PROTOCOL_9){ - $this->putString(); + $this->putString($this->unknownstring); }else{ $this->putInt($this->x); $this->putInt($this->y); diff --git a/src/network/protocol/packet/ContainerSetContentPacket.php b/src/network/protocol/packet/ContainerSetContentPacket.php index 97a5ce78..115058e6 100644 --- a/src/network/protocol/packet/ContainerSetContentPacket.php +++ b/src/network/protocol/packet/ContainerSetContentPacket.php @@ -28,6 +28,9 @@ class ContainerSetContentPacket extends RakNetDataPacket{ for($s = 0; $s < $count and !$this->feof(); ++$s){ $this->slots[$s] = $this->getSlot(); } + if($this->PROTOCOL <= ProtocolInfo9::CURRENT_PROTOCOL_9){ + return; + } if($this->windowid === 0){ $count = $this->getShort(); for($s = 0; $s < $count and !$this->feof(); ++$s){ @@ -43,8 +46,11 @@ class ContainerSetContentPacket extends RakNetDataPacket{ foreach($this->slots as $slot){ $this->putSlot($this->PROTOCOL, $slot); } + if($this->PROTOCOL <= ProtocolInfo9::CURRENT_PROTOCOL_9){ + return; + } if($this->windowid === 0 and count($this->hotbar) > 0){ - $this->putShort(count($this->hotbar));//null + $this->putShort(count($this->hotbar)); foreach($this->hotbar as $slot){ $this->putInt($slot); } diff --git a/src/network/protocol/packet/DropItemPacket.php b/src/network/protocol/packet/DropItemPacket.php index 364afd11..b3fccc5c 100644 --- a/src/network/protocol/packet/DropItemPacket.php +++ b/src/network/protocol/packet/DropItemPacket.php @@ -23,13 +23,17 @@ class DropItemPacket extends RakNetDataPacket{ } public function decode(){ + //if($this->PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8)$this->reset(); $this->eid = $this->getInt(); $this->unknown = $this->getByte(); $this->item = $this->getSlot(); } public function encode(){ - + /*$this->reset(); + $this->putInt($this->eid); + $this->putByte($this->unknown); + $this->putSlot($this->item);*/ } } \ No newline at end of file diff --git a/src/network/protocol/packet/LoginPacket.php b/src/network/protocol/packet/LoginPacket.php index 9bd59938..0d4225a6 100644 --- a/src/network/protocol/packet/LoginPacket.php +++ b/src/network/protocol/packet/LoginPacket.php @@ -12,12 +12,13 @@ class LoginPacket extends RakNetDataPacket{ } public function decode(){ + //if($this->PROTOCOL <= ProtocolInfo9::CURRENT_PROTOCOL_9)$this->reset(); $this->username = $this->getString(); $this->protocol1 = $this->getInt(); $this->protocol2 = $this->getInt(); $this->clientId = $this->getInt();//null $this->loginData = $this->getString(); - $this->PROTOCOL = $this->protocol1; + $this->PROTOCOL = $this->protocol1; } public function encode(){ diff --git a/src/network/protocol/packet/MoveEntityPacket.php b/src/network/protocol/packet/MoveEntityPacket.php index 18032c02..d4cbbf93 100644 --- a/src/network/protocol/packet/MoveEntityPacket.php +++ b/src/network/protocol/packet/MoveEntityPacket.php @@ -14,7 +14,7 @@ class MoveEntityPacket extends RakNetDataPacket{ } public function encode(){ - $this->reset(); + $this->reset(); //null } } \ No newline at end of file diff --git a/src/network/protocol/packet/PlayerActionPacket.php b/src/network/protocol/packet/PlayerActionPacket.php index 9e423e71..945347cc 100644 --- a/src/network/protocol/packet/PlayerActionPacket.php +++ b/src/network/protocol/packet/PlayerActionPacket.php @@ -20,6 +20,7 @@ class PlayerActionPacket extends RakNetDataPacket{ } public function decode(){ + //if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9)$this->reset(); $this->action = $this->getInt(); $this->x = $this->getInt(); $this->y = $this->getInt(); @@ -29,7 +30,13 @@ class PlayerActionPacket extends RakNetDataPacket{ } public function encode(){ - + /*$this->reset(); + $this->putInt($this->action); + $this->putInt($this->x); + $this->putInt($this->y); + $this->putInt($this->z); + $this->putInt($this->face); + $this->putInt($this->eid);*/ } } \ No newline at end of file diff --git a/src/network/protocol/packet/ReadyPacket.php b/src/network/protocol/packet/ReadyPacket.php index 08cd53a1..e6eae824 100644 --- a/src/network/protocol/packet/ReadyPacket.php +++ b/src/network/protocol/packet/ReadyPacket.php @@ -8,11 +8,13 @@ class ReadyPacket extends RakNetDataPacket{ } public function decode(){ + //if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9)$this->reset(); $this->status = $this->getByte(); } public function encode(){ - + /*$this->reset(); + $this->putByte($this->status);*/ } } \ No newline at end of file diff --git a/src/network/protocol/packet/RemoveBlockPacket.php b/src/network/protocol/packet/RemoveBlockPacket.php index f7ba3de5..4c01228c 100644 --- a/src/network/protocol/packet/RemoveBlockPacket.php +++ b/src/network/protocol/packet/RemoveBlockPacket.php @@ -16,6 +16,7 @@ class RemoveBlockPacket extends RakNetDataPacket{ } public function decode(){ + //if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9)$this->reset(); $this->eid = $this->getInt(); $this->x = $this->getInt(); $this->z = $this->getInt(); @@ -23,7 +24,11 @@ class RemoveBlockPacket extends RakNetDataPacket{ } public function encode(){ - + /*$this->reset(); + $this->putInt($this->eid); + $this->putInt($this->x); + $this->putInt($this->z); + $this->putInt($this->y);*/ } } \ No newline at end of file diff --git a/src/network/protocol/packet/RequestChunkPacket.php b/src/network/protocol/packet/RequestChunkPacket.php index 33c1ecc2..8bdeea7c 100644 --- a/src/network/protocol/packet/RequestChunkPacket.php +++ b/src/network/protocol/packet/RequestChunkPacket.php @@ -18,12 +18,15 @@ class RequestChunkPacket extends RakNetDataPacket{ } public function decode(){ + //if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9)$this->reset(); $this->chunkX = $this->getInt(); $this->chunkZ = $this->getInt(); } public function encode(){ - + /*$this->reset(); + $this->putInt($this->chunkX); + $this->putInt($this->chunkZ);*/ } } \ No newline at end of file diff --git a/src/network/protocol/packet/SetEntityDataPacket.php b/src/network/protocol/packet/SetEntityDataPacket.php index eac162d3..160d35c4 100644 --- a/src/network/protocol/packet/SetEntityDataPacket.php +++ b/src/network/protocol/packet/SetEntityDataPacket.php @@ -25,7 +25,6 @@ class SetEntityDataPacket extends RakNetDataPacket{ public function encode(){ $this->reset(); - if($this->PROTOCOL >= ProtocolInfo8::CURRENT_PROTOCOL_8) $this->putInt($this->eid); $this->put(Utils::writeMetadata($this->metadata)); } diff --git a/src/network/protocol/packet/SetTimePacket.php b/src/network/protocol/packet/SetTimePacket.php index c586e97c..afbc9fd2 100644 --- a/src/network/protocol/packet/SetTimePacket.php +++ b/src/network/protocol/packet/SetTimePacket.php @@ -15,7 +15,9 @@ class SetTimePacket extends RakNetDataPacket{ public function encode(){ $this->reset(); $this->putInt($this->time);//long - $this->putByte($this->started ? 0x80:0x00); + if($this->PROTOCOL >= ProtocolInfo8::CURRENT_PROTOCOL_8){ + $this->putByte($this->started ? 0x80:0x00); + } } } \ No newline at end of file diff --git a/src/network/protocol/packet/UseItemPacket.php b/src/network/protocol/packet/UseItemPacket.php index 593df77d..b0d8777c 100644 --- a/src/network/protocol/packet/UseItemPacket.php +++ b/src/network/protocol/packet/UseItemPacket.php @@ -15,6 +15,8 @@ class UseItemPacket extends RakNetDataPacket{ public $posY; public $posZ; + public $unknownInt1; + public function pid(){ if($this->PROTOCOL < ProtocolInfo4::CURRENT_PROTOCOL_4){ return ProtocolInfo3::USE_ITEM_PACKET; @@ -31,6 +33,7 @@ class UseItemPacket extends RakNetDataPacket{ } public function decode(){ + //if($this->PROTOCOL < ProtocolInfo9::CURRENT_PROTOCOL_9)$this->reset(); $this->x = $this->getInt(); $this->y = $this->getInt(); $this->z = $this->getInt(); @@ -38,6 +41,10 @@ class UseItemPacket extends RakNetDataPacket{ $this->item = $this->getShort(); $this->meta = $this->getByte(); //Mojang: fix this $this->eid = $this->getInt(); + if ($this->PROTOCOL > ProtocolInfo9::CURRENT_PROTOCOL_9) { + $this->unknownInt1 = $this->getInt(); + return; + } $this->fx = $this->getFloat(); $this->fy = $this->getFloat(); $this->fz = $this->getFloat(); @@ -49,7 +56,15 @@ class UseItemPacket extends RakNetDataPacket{ } public function encode(){ - + /*$this->reset(); + $this->putInt($this->x); + $this->putInt($this->y); + $this->putInt($this->z); + $this->putInt($this->face); + $this->putShort($this->item); + $this->putByte($this->meta); + $this->putInt($this->eid); + $this->putInt($this->unknownInt1);*/ } } \ No newline at end of file From e3f0ed0744b769576badad5c66212f94cdf3b13f Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Sat, 3 May 2025 16:18:21 +0800 Subject: [PATCH 38/39] Thanks to @ArkQuark --- src/network/protocol/packet/SetTimePacket.php | 2 +- src/network/protocol/packet/StartGamePacket.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/network/protocol/packet/SetTimePacket.php b/src/network/protocol/packet/SetTimePacket.php index afbc9fd2..4f055bc0 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);//long + $this->putInt($this->time);// long in 0.6.1 if($this->PROTOCOL >= ProtocolInfo8::CURRENT_PROTOCOL_8){ $this->putByte($this->started ? 0x80:0x00); } diff --git a/src/network/protocol/packet/StartGamePacket.php b/src/network/protocol/packet/StartGamePacket.php index fffb16da..c81898a8 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);//long + $this->putInt($this->seed);// long in 0.6.1 $this->putInt($this->generator); $this->putInt($this->gamemode); $this->putInt($this->eid); From de985906607aa254497630780a886bfbd7d04ba3 Mon Sep 17 00:00:00 2001 From: yefengeeeeeeeeeee <403749250@qq.com> Date: Sat, 3 May 2025 16:19:35 +0800 Subject: [PATCH 39/39] SignUpdatePacket implement --- src/network/PacketPool.php | 1 + src/network/protocol/ProtocolInfo.php | 9 +++-- .../protocol/packet/ContainerAckPacket.php | 4 +++ .../protocol/packet/ContainerOpenPacket.php | 1 + .../protocol/packet/SignUpdatePacket.php | 34 +++++++++++++++++++ 5 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 src/network/protocol/packet/SignUpdatePacket.php diff --git a/src/network/PacketPool.php b/src/network/PacketPool.php index 4f691b52..3571c697 100644 --- a/src/network/PacketPool.php +++ b/src/network/PacketPool.php @@ -110,6 +110,7 @@ class PacketPool{ self::registerPacket(SetHealthPacket::class); self::registerPacket(SetSpawnPositionPacket::class, [8, 9, 10, 11, 12, 13, 14]); self::registerPacket(SetTimePacket::class); + self::registerPacket(SignUpdatePacket::class, [6, 7, 8, 9]); self::registerPacket(StartGamePacket::class); self::registerPacket(TakeItemEntityPacket::class); diff --git a/src/network/protocol/ProtocolInfo.php b/src/network/protocol/ProtocolInfo.php index 708fa6ec..79fc5a36 100644 --- a/src/network/protocol/ProtocolInfo.php +++ b/src/network/protocol/ProtocolInfo.php @@ -197,7 +197,7 @@ abstract class ProtocolInfo9{ 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_ENTITY_LINK_PACKET = 0xa?; const SET_HEALTH_PACKET = 0xa8;// Change const SET_SPAWN_POSITION_PACKET = 0xa9; const ANIMATE_PACKET = 0xaa; @@ -210,7 +210,8 @@ abstract class ProtocolInfo9{ const CONTAINER_SET_DATA_PACKET = 0xb1; const CONTAINER_SET_CONTENT_PACKET = 0xb2; const CONTAINER_ACK_PACKET = 0xb3; - const CHAT_PACKET = 0xb4;//12 change + const CHAT_PACKET = 0xb4; + const SIGN_UPDATE_PACKET = 0xb5; const ADVENTURE_SETTINGS_PACKET = 0xb6; const ENTITY_DATA_PACKET = 0xb7; const PLAYER_INPUT_PACKET = 0xb9; @@ -278,7 +279,8 @@ abstract class ProtocolInfo8{ const CONTAINER_SET_DATA_PACKET = 0xae; const CONTAINER_SET_CONTENT_PACKET = 0xaf; const CONTAINER_ACK_PACKET = 0xb; - const CHAT_PACKET = 0xb1;//12 change + const CHAT_PACKET = 0xb1; + const SIGN_UPDATE_PACKET = 0xb2; const ADVENTURE_SETTINGS_PACKET = 0xb3; const ENTITY_DATA_PACKET = 0xb2; const PLAYER_INPUT_PACKET = 0xb9; @@ -346,6 +348,7 @@ abstract class ProtocolInfo6{ const CONTAINER_SET_CONTENT_PACKET = 0xad; const CONTAINER_ACK_PACKET = 0xae; const CHAT_PACKET = 0xaf; + const SIGN_UPDATE_PACKET = 0xb0; //const ADVENTURE_SETTINGS_PACKET = 0xb3; //const ENTITY_DATA_PACKET = 0xb2; //const PLAYER_INPUT_PACKET = 0xb9; diff --git a/src/network/protocol/packet/ContainerAckPacket.php b/src/network/protocol/packet/ContainerAckPacket.php index 9b411215..d748a73b 100644 --- a/src/network/protocol/packet/ContainerAckPacket.php +++ b/src/network/protocol/packet/ContainerAckPacket.php @@ -35,6 +35,8 @@ class ContainerAckPacket extends RakNetDataPacket{ ConsoleAPI::debug($this->unknownshort); ConsoleAPI::debug($this->write1); ConsoleAPI::debug($this->write0); + + ConsoleAPI::debug($this->unknwonubyte2); } public function encode(){ @@ -53,6 +55,8 @@ class ContainerAckPacket extends RakNetDataPacket{ ConsoleAPI::debug($this->unknownshort); ConsoleAPI::debug($this->write1); ConsoleAPI::debug($this->write0); + + ConsoleAPI::debug($this->unknwonubyte2); } } \ No newline at end of file diff --git a/src/network/protocol/packet/ContainerOpenPacket.php b/src/network/protocol/packet/ContainerOpenPacket.php index e2fca894..93241c93 100644 --- a/src/network/protocol/packet/ContainerOpenPacket.php +++ b/src/network/protocol/packet/ContainerOpenPacket.php @@ -37,6 +37,7 @@ class ContainerOpenPacket extends RakNetDataPacket{ $this->putByte($this->slots); if ($this->PROTOCOL <= ProtocolInfo9::CURRENT_PROTOCOL_9){ $this->putString($this->unknownstring); + ConsoleAPI::debug($this->unknownstring); }else{ $this->putInt($this->x); $this->putInt($this->y); diff --git a/src/network/protocol/packet/SignUpdatePacket.php b/src/network/protocol/packet/SignUpdatePacket.php new file mode 100644 index 00000000..1ba4542d --- /dev/null +++ b/src/network/protocol/packet/SignUpdatePacket.php @@ -0,0 +1,34 @@ +PROTOCOL < ProtocolInfo8::CURRENT_PROTOCOL_8){ + return ProtocolInfo6::SIGN_UPDATE_PACKET; + }elseif($this->PROTOCOL < ProtocolInfo9::SIGN_UPDATE_PACKET) { + return ProtocolInfo8::SIGN_UPDATE_PACKET; + } + return ProtocolInfo9::SIGN_UPDATE_PACKET; + } + + public function decode(){ + //$this->reset(); + $this->x = $this->getShort(); + $this->y = $this->getByte(); + $this->z = $this->getShort(); + $this->content = $this->getString(); + } + + public function encode(){ + //$this->reset(); + $this->putShort($this->x); + $this->putByte($this->y); + $this->putShort($this->z); + $this->putString($this->content); + } + +} \ No newline at end of file