diff --git a/src/API/ChatAPI.php b/src/API/ChatAPI.php index 09cd9b7a..92bffa51 100644 --- a/src/API/ChatAPI.php +++ b/src/API/ChatAPI.php @@ -35,6 +35,7 @@ class ChatAPI{ break; } $sender = ($issuer instanceof Player) ? "Server" : ucfirst($issuer); + if(Utils::hasEmoji($s)) return "Your message contains illegal characters!"; $this->server->api->chat->broadcast("[$sender] " . $s); break; case "me": @@ -52,7 +53,9 @@ class ChatAPI{ }else{ $sender = $issuer->username; } - $this->broadcast("* $sender " . implode(" ", $params)); + $msg = implode(" ", $params); + if(Utils::hasEmoji($msg)) return "Your message contains illegal characters!"; + $this->broadcast("* $sender $msg"); break; case "tell": if(!isset($params[0]) or !isset($params[1])){ @@ -80,6 +83,7 @@ class ChatAPI{ return "You can't send message to yourself."; } $mes = implode(" ", $params); + if(Utils::hasEmoji($mes)) return "Your message contains illegal characters!"; $output .= "You're whispering to " . $target . ": " . $mes . "\n"; if($target !== "Console" and $target !== "Rcon"){ $this->sendTo(false, $sender . " whispers to you: " . $mes, $target); diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index 804486c4..a0c5036b 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -53,9 +53,7 @@ class ConsoleAPI{ if(!defined("NO_THREADS")){ self::$loop->stop(); self::$loop->notify(); - //@fclose($this->loop->fp); usleep(50000); - //$this->loop->join(); } } diff --git a/src/Player.php b/src/Player.php index f3e02972..f476543b 100755 --- a/src/Player.php +++ b/src/Player.php @@ -68,7 +68,13 @@ class Player{ private $packetStats; private $chunkCount = []; private $received = []; - + + /** + * Stores packets that must be always received by clients. + * @var array + */ + public $packetAlwaysRecoverQueue = []; + public $slotCount = 7; public $entityMovementQueue; @@ -378,6 +384,42 @@ class Player{ return []; } + public function sendChunkDataPacket(RakNetDataPacket $pk){ + if($this->connected === false) return false; + if(EventHandler::callEvent(new DataPacketSendEvent($this, $pk)) === BaseEvent::DENY) return; + + $pk->encode(); + $sendtime = microtime(true); + $size = $this->MTU - 34; + if($size <= 0) return false; + $buffer = str_split($pk->buffer, $size); + $bigCnt = $this->bigCnt; + $this->bigCnt = ($this->bigCnt + 1) % 0x10000; + $cnts = []; + $bufCount = count($buffer); + foreach($buffer as $i => $buf){ + $cnts[] = $count = $this->counter[0]++; + + $pk = new UnknownPacket; + $pk->packetID = $pk->pid(); + $pk->reliability = 2; + $pk->hasSplit = true; + $pk->splitCount = $bufCount; + $pk->splitID = $bigCnt; + $pk->splitIndex = $i; + $pk->buffer = $buf; + $pk->messageIndex = $this->counter[3]++; + + $rk = new RakNetPacket(RakNetInfo::DATA_PACKET_0); + $rk->data[] = $pk; + $rk->seqNumber = $count; + $rk->sendtime = $sendtime; + $this->packetAlwaysRecoverQueue[$count] = $rk; + $this->send($rk); + } + return $cnts; + } + private function directBigRawPacket(RakNetDataPacket $packet){ if($this->connected === false){ return false; @@ -562,7 +604,7 @@ class Player{ $pk->chunkX = $X; $pk->chunkZ = $Z; $pk->data = $this->level->getOrderedChunk($X, $Z, $Yndex); - $cnt = $this->dataPacket($pk); + $cnt = $this->sendChunkDataPacket($pk); $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; @@ -584,8 +626,9 @@ class Player{ 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])){ + if(isset($this->packetAlwaysRecoverQueue[$count])){ $this->server->schedule(MAX_CHUNK_RATE, [$this, "getNextChunk"], $world); return; }else{ @@ -639,7 +682,7 @@ class Player{ $pk->chunkX = $X; $pk->chunkZ = $Z; $pk->data = $this->level->getOrderedChunk($X, $Z, $Yndex); - $cnt = $this->dataPacket($pk); + $cnt = $this->sendChunkDataPacket($pk); if($cnt === false){ return false; } @@ -688,6 +731,7 @@ class Player{ $pk->hotbar = $hotbar; $this->dataPacket($pk); } + /** * * @return boolean @@ -1228,11 +1272,17 @@ class Player{ $limit = $time - 5; //max lag foreach($this->recoveryQueue as $count => $data){ if($data->sendtime > $limit){ - break; + continue; } unset($this->recoveryQueue[$count]); $this->resendQueue[$count] = $data; } + + foreach($this->packetAlwaysRecoverQueue as $cnt => $data){ + if($time - $data->sendtime >= 5){ + $this->resendQueue[$cnt] = $data; + } + } if(($resendCnt = count($this->resendQueue)) > 0){ foreach($this->resendQueue as $count => $data){ @@ -1240,13 +1290,7 @@ class Player{ $this->packetStats[1]++; $this->lag[] = $time - $data->sendtime; $data->sendtime = $time; - - $cnt = $this->send($data); - if(isset($this->chunkCount[$count])){ - unset($this->chunkCount[$count]); - if(!is_null($cnt) and !is_null($cnt[0])) - $this->chunkCount[$cnt[0]] = true; - } + $this->send($data); } } } @@ -1842,7 +1886,7 @@ class Player{ $pk = new StartGamePacket; $pk->seed = $this->level->getSeed(); $pk->x = $this->data->get("position")["x"]; - $pk->y = $this->data->get("position")["y"]; + $pk->y = ceil($this->data->get("position")["y"]); $pk->z = $this->data->get("position")["z"]; $pk->generator = 0; $pk->gamemode = $this->gamemode & 0x01; @@ -1850,6 +1894,7 @@ class Player{ $this->dataPacket($pk); if (($this->gamemode & 0x01) === 0x01) { + $this->slot = 0; $this->hotbar = []; } elseif ($this->data->exists("hotbar")) { @@ -1872,10 +1917,10 @@ class Player{ $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->entity->x = $pk->x; + $this->entity->y = $pk->y; + $this->entity->z = $pk->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; @@ -2111,6 +2156,7 @@ class Player{ } $this->craftingItems = []; $this->toCraft = []; + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; $packet->eid = $this->eid; $data = []; $data["eid"] = $packet->eid; @@ -2127,9 +2173,23 @@ class Player{ $data["posX"] = $packet->posX; $data["posY"] = $packet->posY; $data["posZ"] = $packet->posZ; + + //nc: check item desync + $heldItem = $this->getHeldItem(); + if($heldItem->getID() != $packet->item || $heldItem->getMetadata() != $packet->meta){ + ConsoleAPI::warn("{$this->username}'s heldItem doesnt match on clientside and serverside when using item. Resending inventory."); + $this->sendInventory(); + if($packet->face >= 0 && $packet->face <= 5){ + $target = $this->level->getBlock($blockVector); + $block = $target->getSide($packet->face); + $this->addBlockUpdateIntoQueue($target->x, $target->y, $target->z, $target->getID(), $target->getMetadata()); + $this->addBlockUpdateIntoQueue($block->x, $block->y, $block->z, $block->getID(), $block->getMetadata()); + } + break; + } - 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(); @@ -2290,8 +2350,9 @@ class Player{ $packet->eid = $this->eid; $this->craftingItems = []; $this->toCraft = []; - - switch ($packet->action) { + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; + + switch($packet->action){ case 5: //Shot arrow if ($this->entity->inAction) { $arrowSlot = $this->hasItem(ARROW); @@ -2371,6 +2432,7 @@ class Player{ } $this->craftingItems = []; $this->toCraft = []; + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; $this->server->api->block->playerBlockBreak($this, $blockVector); break; case ProtocolInfo::PLAYER_ARMOR_EQUIPMENT_PACKET: @@ -2379,7 +2441,8 @@ class Player{ } $this->craftingItems = []; $this->toCraft = []; - + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; + $packet->eid = $this->eid; for ($i = 0; $i < 4; ++$i) { $s = $packet->slots[$i]; @@ -2423,6 +2486,7 @@ class Player{ $data["action"] = $packet->action; $this->craftingItems = []; $this->toCraft = []; + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; $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; @@ -2450,7 +2514,7 @@ class Player{ } $this->craftingItems = []; $this->toCraft = []; - + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; $this->checkSpawnPosition(); $this->teleport($this->spawnPosition, false, false, true, false); @@ -2488,6 +2552,7 @@ class Player{ } $this->craftingItems = []; $this->toCraft = []; + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; $packet->eid = $this->eid; if ($this->entity->inAction === true) { $this->entity->inAction = false; @@ -2535,10 +2600,8 @@ class Player{ 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); + } + $this->addCraftingResult(-1, $prevItem->getID(), $prevItem->getMetadata(), $prevItem->count); break; } } else { @@ -2549,6 +2612,7 @@ class Player{ $this->craftingItems = []; $this->toCraft = []; + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; $data["eid"] = $packet->eid; $data["unknown"] = $packet->unknown; $data["item"] = $packet->item; @@ -2581,7 +2645,8 @@ class Player{ } $this->craftingItems = []; $this->toCraft = []; - if (trim($packet->message) != "" and strlen($packet->message) <= 255) { + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; + if(trim($packet->message) != "" and strlen($packet->message) <= 255){ $message = $packet->message; if ($message[0] === "/") { //Command if ($this instanceof Player) { @@ -2618,9 +2683,10 @@ class Player{ } $this->craftingItems = []; $this->toCraft = []; - if (isset($this->windows[$packet->windowid])) { - if (is_array($this->windows[$packet->windowid])) { - foreach ($this->windows[$packet->windowid] as $ob) { + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; + 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; @@ -2649,57 +2715,31 @@ class Player{ 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; + + if($packet->windowid === 0){ //crafting + $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; + $citem = $packet->item; + if($slot->getID() == $citem->getID() && $slot->getMetadata() == $citem->getMetadata()){ + if($citem->count > $slot->count){ //item added, result + $this->addCraftingResult($packet->slot, $slot->getID(), $slot->getMetadata(), $citem->count - $slot->count); + }else if($citem->count < $slot->count){ //item removed, ingridient + $this->addCraftingIngridient($packet->slot, $slot->getID(), $slot->getMetadata(), $slot->count - $citem->count); + }else{ + //item synchronized } - $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; + }else if($citem->getID() == 0 && $slot->getID() > 0){ //client sent air, server has not air, ingridient + $this->addCraftingIngridient($packet->slot, $slot->getID(), $slot->getMetadata(), $slot->count); + }else if($slot->getID() == 0 && $citem->getID() > 0){ //client sent non-air, server has air, result + $this->addCraftingResult($packet->slot, $citem->getID(), $citem->getMetadata(), $citem->count); + }else{ //client sent result, server has ingridients + $this->addCraftingIngridient($packet->slot, $slot->getID(), $slot->getMetadata(), $slot->count); + $this->addCraftingResult($packet->slot, $citem->getID(), $citem->getMetadata(), $citem->count); } - - 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 { + }else{ $this->toCraft = []; $this->craftingItems = []; + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; } if (!isset($this->windows[$packet->windowid])) { break; @@ -2812,6 +2852,7 @@ class Player{ } $this->craftingItems = []; $this->toCraft = []; + $this->craftingType = CraftingRecipes::TYPE_INVENTORY; $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) { @@ -2883,6 +2924,150 @@ class Player{ } } + public function checkCraftAchievements($id){ + switch($id){ + case WORKBENCH: + AchievementAPI::grantAchievement($this, "buildWorkBench"); + break; + case WOODEN_PICKAXE: + AchievementAPI::grantAchievement($this, "buildPickaxe"); + break; + case FURNACE: + AchievementAPI::grantAchievement($this, "buildFurnace"); + break; + case WOODEN_HOE: + AchievementAPI::grantAchievement($this, "buildHoe"); + break; + case BREAD: + AchievementAPI::grantAchievement($this, "makeBread"); + break; + case CAKE: + AchievementAPI::grantAchievement($this, "bakeCake"); + break; + case STONE_PICKAXE: + case GOLDEN_PICKAXE: + case IRON_PICKAXE: + case DIAMOND_PICKAXE: + AchievementAPI::grantAchievement($this, "buildBetterPickaxe"); + break; + case WOODEN_SWORD: + AchievementAPI::grantAchievement($this, "buildSword"); + break; + case DIAMOND: + AchievementAPI::grantAchievement($this, "diamond"); + break; + } + } + + public $craftingType = 0; + public function tryCraft(){ + if(count($this->toCraft) <= 0 || count($this->craftingItems) <= 0) return; + + $results = []; + foreach($this->toCraft as $i => $slotz){ + $results[$i >> 16] = [$i >> 16, $i & 0xffff, array_sum($slotz)]; + } + $ingridients = []; + foreach($this->craftingItems as $i => $slotz){ + $ingridients[$i >> 16] = [$i >> 16, $i & 0xffff, array_sum($slotz)]; + } + + + $cc = CraftingRecipes::canCraft($results, $ingridients, $this->craftingType); + if(!is_array($cc) && $this->craftingType == CraftingRecipes::TYPE_CRAFTIGTABLE){ + $cc = CraftingRecipes::canCraft($results, $ingridients, CraftingRecipes::TYPE_INVENTORY); + if(!is_array($cc)){ + return; //recipe not found + } + } + + if($this->server->api->dhandle("player.craft", ["player" => $this, "ingridients" => $this->craftingItems, "results" => $this->toCraft, "type" => $this->craftingType]) === false){ + $this->toCraft = []; + $this->craftingItems = []; + return false; + } + + foreach($this->craftingItems as $i => $slotz){ + $id = $i >> 16; + $meta = $i & 0xffff; + foreach($slotz as $slot => $count){ + $slt = $this->getSlot($slot); + if($slt->getID() == $id && $slt->getMetadata() == $meta){ + $slt->count -= $count; + if($slt->count <= 0) $this->setSlot($slot, BlockAPI::getItem(0, 0, 0), false); + }else{ + ConsoleAPI::warn("{$slt->getID()} != $id && {$slt->getMetadata()} != $meta!!!"); + } + } + } + + foreach($this->toCraft as $i => $slotz){ + $id = $i >> 16; + $meta = $i & 0xffff; + $this->checkCraftAchievements($id); + foreach($slotz as $slot => $count){ + if($slot < 0){ //drop item + $item = BlockAPI::getItem($id, $meta, $count); + $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, $item, $sX, $sY, $sZ); + }else{ + $slt = $this->getSlot($slot); + if($slt->getID() == $id && $slt->getMetadata() == $meta){ + $slt->count += $count; + }else if($slt->getID() == 0){ + $this->setSlot($slot, BlockAPI::getItem($id, $meta, $count), false); + }else{ + ConsoleAPI::warn("{$slt->getID()} != $id || 0 !!!"); + } + } + } + } + + $this->sendInventory(); + $this->toCraft = []; + $this->craftingItems = []; + } + + public function addCraftingResult($slot, $id, $meta, $count){ + $id &= 0xffff; + $meta &= 0xffff; + + $index = ($id << 16) | $meta; + + if(!isset($this->toCraft[$index])) $this->toCraft[$index] = []; + if(!isset($this->toCraft[$index][$slot])) $this->toCraft[$index][$slot] = 0; + $this->toCraft[$index][$slot] += $count; + + //console("Result: $id, $meta, $count into $slot"); + if($this->tryCraft() === false){ + $this->sendInventory(); + } + } + + public function addCraftingIngridient($slot, $id, $meta, $count){ + $id &= 0xffff; + $meta &= 0xffff; + + $index = ($id << 16) | $meta; + + if(!isset($this->craftingItems[$index])) $this->craftingItems[$index] = []; + if(!isset($this->craftingItems[$index][$slot])) $this->craftingItems[$index][$slot] = 0; + $this->craftingItems[$index][$slot] += $count; + //console("Ingridient: $id, $meta, $count into $slot"); + if($this->tryCraft() === false){ + $this->sendInventory(); + } + } + 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){ @@ -2916,121 +3101,7 @@ class Player{ } return true; } - - /** - * - * @return array|bool - */ - public function craftItems(array $craft, array $recipe, $type){ - $craftItem = [0, true, 0]; - unset($craft[-1]); - - foreach($craft as $slot => $item){ - if($item instanceof Item){ - $craftItem[0] = $item->getID(); - if($item->getMetadata() !== $craftItem[1] and $craftItem[1] !== true){ - $craftItem[1] = false; - }else{ - $craftItem[1] = $item->getMetadata(); - } - $craftItem[2] += $item->count; - } - - } - - $recipeItems = []; - 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]){ - $recipeItems[$item->getID()][1] = false; - } - $recipeItems[$item->getID()][2] += $item->count; - } - } - - $res = CraftingRecipes::canCraft($craftItem, $recipeItems, $type); - - if(!is_array($res) and $type === 1){ - $res2 = CraftingRecipes::canCraft($craftItem, $recipeItems, 0); - 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){ - return false; - } - foreach($recipe as $slot => $item){ - $s = $this->getSlot($slot); - $s->count -= $item->count; - if($s->count <= 0){ - $this->setSlot($slot, BlockAPI::getItem(AIR, 0, 0), false); - } - } - foreach($craft as $slot => $item){ - $s = $this->getSlot($slot); - 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()){ - $this->setSlot($slot, BlockAPI::getItem($item->getID(), $item->getMetadata(), $s->count + $item->count), false); - }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; - $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, $item, $sX, $sY, $sZ); - } - - $this->sendInventory(); //force send on crafting - - switch($item->getID()){ - case WORKBENCH: - AchievementAPI::grantAchievement($this, "buildWorkBench"); - break; - case WOODEN_PICKAXE: - AchievementAPI::grantAchievement($this, "buildPickaxe"); - break; - case FURNACE: - AchievementAPI::grantAchievement($this, "buildFurnace"); - break; - case WOODEN_HOE: - AchievementAPI::grantAchievement($this, "buildHoe"); - break; - case BREAD: - AchievementAPI::grantAchievement($this, "makeBread"); - break; - case CAKE: - AchievementAPI::grantAchievement($this, "bakeCake"); - $this->addItem(BUCKET, 0, 3); - break; - case STONE_PICKAXE: - case GOLDEN_PICKAXE: - case IRON_PICKAXE: - case DIAMOND_PICKAXE: - AchievementAPI::grantAchievement($this, "buildBetterPickaxe"); - break; - case WOODEN_SWORD: - AchievementAPI::grantAchievement($this, "buildSword"); - break; - case DIAMOND: - AchievementAPI::grantAchievement($this, "diamond"); - break; - - } - } - } - return $res; - } - + public function handlePacket(RakNetPacket $packet){ if($this->connected === true){ $time = microtime(true); @@ -3038,8 +3109,10 @@ class Player{ switch($packet->pid()){ case RakNetInfo::NACK: foreach($packet->packets as $count){ - if(isset($this->recoveryQueue[$count])){ - $this->resendQueue[$count] = &$this->recoveryQueue[$count]; + if(isset($this->packetAlwaysRecoverQueue[$count])){ + $this->packetAlwaysRecoverQueue[$count]->sendtime = 0; //forces resend next time it will be checked + }else if(isset($this->recoveryQueue[$count])){ + $this->resendQueue[$count] =& $this->recoveryQueue[$count]; $this->lag[] = $time - $this->recoveryQueue[$count]->sendtime; unset($this->recoveryQueue[$count]); }else{ @@ -3050,7 +3123,11 @@ class Player{ case RakNetInfo::ACK: foreach($packet->packets as $count){ - if(isset($this->recoveryQueue[$count])){ + if(isset($this->packetAlwaysRecoverQueue[$count])){ + $this->lag[] = $time - $this->packetAlwaysRecoverQueue[$count]->sendtime; + unset($this->packetAlwaysRecoverQueue[$count]); + unset($this->chunkCount[$count]); + }else if(isset($this->recoveryQueue[$count])){ $this->lag[] = $time - $this->recoveryQueue[$count]->sendtime; unset($this->recoveryQueue[$count]); unset($this->resendQueue[$count]); @@ -3103,6 +3180,19 @@ class Player{ } return $this->clientID; } + + + /** + * @deprecated craft system changed + * @param array $craft + * @param array $recipe + * @param $type + * + * @return bool + */ + public function craftItems(array $craft, array $recipe, $type){ + return false; + } } diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 349a00d4..5e3db110 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -12,6 +12,7 @@ class PocketMinecraftServer{ public $doTick, $levelData, $tiles, $entities, $schedule, $scheduleCnt, $whitelist, $spawn, $difficulty, $stop, $asyncThread; public static $FORCE_20_TPS = false, $KEEP_CHUNKS_LOADED = true, $PACKET_READING_LIMIT = 100; + function __construct($name, $gamemode = SURVIVAL, $seed = false, $port = 19132, $serverip = "0.0.0.0"){ $this->port = (int) $port; $this->doTick = true; diff --git a/src/config.php b/src/config.php index f199ba71..8352e431 100644 --- a/src/config.php +++ b/src/config.php @@ -107,7 +107,7 @@ define("START_TIME", microtime(true)); define("MAJOR_VERSION", "1.1.0dev"); define("CODENAME", "懐かしさ (Nostalgia)"); //i'm not very creative - kotyaralih define("CURRENT_MINECRAFT_VERSION", "v0.8.1 alpha"); -define("CURRENT_API_VERSION", '12.1'); +define("CURRENT_API_VERSION", "12.2"); define("CURRENT_PHP_VERSION", "8.0"); $gitsha1 = false; if(file_exists(FILE_PATH . ".git/refs/heads/master")){ //Found Git information! diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index 13b4efa0..586d93fe 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -106,8 +106,10 @@ class MobController $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->entity->isCollidedHorizontally){ + $this->setJumping(true); + } } } //TODO handle jumps somewhere diff --git a/src/entity/object/FallingSand.php b/src/entity/object/FallingSand.php index 745d289b..8bb6c936 100644 --- a/src/entity/object/FallingSand.php +++ b/src/entity/object/FallingSand.php @@ -27,6 +27,18 @@ class FallingSand extends Entity{ $this->close(); return; } + //$level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); + if($this->fallTime == 0){ + $x = floor($this->x); + $y = floor($this->y); + $z = floor($this->z); + if($this->level->level->getBlockID($x, $y, $z) != $this->data["Tile"]){ + $this->close(); + return; + } + $this->level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); + } + $this->lastX = $this->x; $this->lastY = $this->y; $this->lastZ = $this->z; @@ -41,6 +53,15 @@ class FallingSand extends Entity{ $x = floor($this->x); $y = floor($this->y); $z = floor($this->z); + + if(($blockAt = $this->level->level->getBlockID($x, $y, $z)) != 0){ + //TODO vanilla-like checking? + if(StaticBlock::getIsTransparent($blockAt) && !StaticBlock::getIsLiquid($blockAt)){ + $this->close(); + ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->data["Tile"], 0, 1)); //TODO add metadata + return; + } + } if($this->onGround){ $this->speedX *= 0.7; @@ -49,12 +70,6 @@ class FallingSand extends Entity{ $this->close(); //TODO vanilla-like checking? $this->level->fastSetBlockUpdate($x, $y, $z, $this->data["Tile"], 0, true); //TODO add metadata - }elseif(($blockAt = $this->level->level->getBlockID($x, $y, $z)) != 0){ - //TODO vanilla-like checking? - if(StaticBlock::getIsTransparent($blockAt) && !StaticBlock::getIsLiquid($blockAt)){ - $this->close(); - ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->data["Tile"], 0, 1)); //TODO add metadata - } } } -} \ No newline at end of file +} diff --git a/src/entity/object/Painting.php b/src/entity/object/Painting.php index a0e7b99e..c451075f 100644 --- a/src/entity/object/Painting.php +++ b/src/entity/object/Painting.php @@ -43,10 +43,10 @@ class Painting extends Entity{ 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? diff --git a/src/material/block/GenericBlock.php b/src/material/block/GenericBlock.php index cd707a57..a2b23d1b 100644 --- a/src/material/block/GenericBlock.php +++ b/src/material/block/GenericBlock.php @@ -53,12 +53,11 @@ class GenericBlock extends Block{ if($down == AIR || StaticBlock::getIsLiquid($down)){ $data = [ "x" => $x + 0.5, - "y" => $y - 0.5, + "y" => $y, "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); } @@ -82,4 +81,4 @@ class GenericBlock extends Block{ public function onActivate(Item $item, Player $player){ return $this->isActivable; } -} \ No newline at end of file +} diff --git a/src/material/block/nonfull/FarmlandBlock.php b/src/material/block/nonfull/FarmlandBlock.php index dac2bdf6..6f30afea 100644 --- a/src/material/block/nonfull/FarmlandBlock.php +++ b/src/material/block/nonfull/FarmlandBlock.php @@ -29,19 +29,31 @@ class FarmlandBlock extends TransparentBlock{ } public static function onRandomTick(Level $level, $x, $y, $z){ - $meta = $level->level->getBlockDamage($x, $y, $z); - $b = $level->level->getBlockID($x, $y + 1, $z); - if(!StaticBlock::getIsFlowable($b)){ - $level->fastSetBlockUpdate($x, $y, $z, DIRT, 0, true); - }elseif($meta === 0 && mt_rand(0, 5) === 0){ - $water = self::checkWaterStatic($level, $x, $y, $z); - if($water){ - $level->fastSetBlockUpdate($x, $y, $z, FARMLAND, 1, true); - }elseif($b != 0 && StaticBlock::getIsTransparent($b)){ - $level->fastSetBlockUpdate($x, $y, $z, DIRT, 0, true); + for($xx = $x-4; $xx <= $x+4; ++$xx){ + for($yy = $y; $yy <= $y+1; ++$yy){ + for($zz = $z-4; $zz <= $z+4; ++$zz){ + $id = $level->level->getBlockID($xx, $yy, $zz); + if($id == WATER || $id == STILL_WATER){ + $dt = 7; + goto set_wet_level; + } + } + } + } + $dt = $level->level->getBlockDamage($x, $y, $z); + if($dt > 0){ + $dt -= 1; + set_wet_level: + $level->fastSetBlockUpdateMeta($x, $y, $z, $dt, true); + return; + } + $ab = $level->level->getBlockID($x, $y+1, $z); + if($ab != WHEAT_BLOCK){ + //nc: added carrot/potato/beetroot checks + if($ab != CARROT_BLOCK && $ab != POTATO_BLOCK && $ab != BEETROOT_BLOCK){ + $level->fastSetBlockUpdate($x, $y, $z, DIRT, true); } } - } public static function checkWaterStatic(Level $level, $x, $y, $z) @@ -50,7 +62,7 @@ class FarmlandBlock extends TransparentBlock{ for ($by = $y; $by <= $y + 1; $by++) { for ($bz = $z - 4; $bz <= $z + 4; $bz++) { $id = $level->level->getBlockID($bx, $by, $bz); - if ($id === 8 || $id === 9) { + if ($id === WATER || $id === STILL_WATER) { return true; } } diff --git a/src/material/block/solid/StonecutterBlock.php b/src/material/block/solid/StonecutterBlock.php index 6a75f9dd..2f145ac8 100644 --- a/src/material/block/solid/StonecutterBlock.php +++ b/src/material/block/solid/StonecutterBlock.php @@ -8,7 +8,7 @@ class StonecutterBlock extends SolidBlock{ } public function onActivate(Item $item, Player $player){ - $player->toCraft[-1] = 2; + $player->craftingType = CraftingRecipes::TYPE_STONECUTTER; return true; } diff --git a/src/material/block/solid/WorkbenchBlock.php b/src/material/block/solid/WorkbenchBlock.php index ba8720bc..171f5e2a 100644 --- a/src/material/block/solid/WorkbenchBlock.php +++ b/src/material/block/solid/WorkbenchBlock.php @@ -9,7 +9,7 @@ class WorkbenchBlock extends SolidBlock{ } public function onActivate(Item $item, Player $player){ - $player->toCraft[-1] = 1; + $player->craftingType = CraftingRecipes::TYPE_CRAFTIGTABLE; return true; } diff --git a/src/material/item/generic/DyeItem.php b/src/material/item/generic/DyeItem.php index e2630745..380e3f97 100755 --- a/src/material/item/generic/DyeItem.php +++ b/src/material/item/generic/DyeItem.php @@ -3,8 +3,8 @@ class DyeItem extends Item{ public function __construct($meta = 0, $count = 1){ parent::__construct(DYE, $meta, $count, "Dye"); - $names = [ - 0 => "Inc Sac", + $names = array( + 0 => "Ink Sac", 1 => "Rose Red", 2 => "Cactus Green", 3 => "Cocoa Beans", @@ -23,4 +23,4 @@ class DyeItem extends Item{ ]; $this->name = $names[$this->meta]; } -} \ No newline at end of file +} diff --git a/src/network/protocol/packet/UseItemPacket.php b/src/network/protocol/packet/UseItemPacket.php index b0d8777c..5f086013 100644 --- a/src/network/protocol/packet/UseItemPacket.php +++ b/src/network/protocol/packet/UseItemPacket.php @@ -39,7 +39,7 @@ class UseItemPacket extends RakNetDataPacket{ $this->z = $this->getInt(); $this->face = $this->getInt(); $this->item = $this->getShort(); - $this->meta = $this->getByte(); //Mojang: fix this + $this->meta = $this->getByte(); $this->eid = $this->getInt(); if ($this->PROTOCOL > ProtocolInfo9::CURRENT_PROTOCOL_9) { $this->unknownInt1 = $this->getInt(); @@ -67,4 +67,4 @@ class UseItemPacket extends RakNetDataPacket{ $this->putInt($this->unknownInt1);*/ } -} \ No newline at end of file +} diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php index bc0d939b..90677780 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -127,6 +127,7 @@ class RakNetParser{ $reliability = ($packetFlags & 0b11100000) >> 5; $hasSplit = ($packetFlags & 0b00010000) > 0; $length = (int) ceil($this->getShort() / 8); + if($reliability === 2 or $reliability === 3 or $reliability === 4 diff --git a/src/recipes/CraftingRecipes.php b/src/recipes/CraftingRecipes.php index df5b4c4c..e9ac25f6 100644 --- a/src/recipes/CraftingRecipes.php +++ b/src/recipes/CraftingRecipes.php @@ -1,7 +1,10 @@ CLAY_BLOCK:0x1", @@ -132,7 +135,7 @@ class CraftingRecipes{ "BEETROOT:?x4,BOWL:?x1=>BEETROOT_SOUP:0x1", "WOODEN_PLANKS:?x3=>BOWL:0x4", "WHEAT:?x3=>BREAD:0x1", - "WHEAT:?x3,BUCKET:1x3,EGG:?x1,SUGAR:?x2=>CAKE:0x1", + "WHEAT:?x3,BUCKET:1x3,EGG:?x1,SUGAR:?x2=>CAKE:0x1,BUCKET:0x3", "DIAMOND:?x4=>DIAMOND_BOOTS:0x1", "DIAMOND:?x8=>DIAMOND_CHESTPLATE:0x1", "DIAMOND:?x5=>DIAMOND_HELMET:0x1", @@ -197,6 +200,13 @@ class CraftingRecipes{ "COBBLESTONE:?x6=>COBBLESTONE_STAIRS:0x4", ]; + /** + * Each element structure: + * * 0 - ingridients
+ * * 1 - results
+ * * 2 - recipe string
+ * @var array[] + */ private static $recipes = []; public static function init(){ @@ -204,19 +214,19 @@ class CraftingRecipes{ $id = 1; foreach(CraftingRecipes::$small as $recipe){ $recipe = CraftingRecipes::parseRecipe($recipe); - $recipe[3] = 0; //Type + $recipe[3] = self::TYPE_INVENTORY; //Type CraftingRecipes::$recipes[$id] = $recipe; ++$id; } foreach(CraftingRecipes::$big as $recipe){ $recipe = CraftingRecipes::parseRecipe($recipe); - $recipe[3] = 1; + $recipe[3] = self::TYPE_CRAFTIGTABLE; CraftingRecipes::$recipes[$id] = $recipe; ++$id; } foreach(CraftingRecipes::$stone as $recipe){ $recipe = CraftingRecipes::parseRecipe($recipe); - $recipe[3] = 2; + $recipe[3] = self::TYPE_STONECUTTER; CraftingRecipes::$recipes[$id] = $recipe; ++$id; } @@ -227,9 +237,9 @@ class CraftingRecipes{ } private static function parseRecipe($recipe){ - $recipe = explode("=>", $recipe); + [$ingridients, $results] = explode("=>", $recipe); $recipeItems = []; - foreach(explode(",", $recipe[0]) as $item){ + foreach(explode(",", $ingridients) as $item){ $item = explode("x", $item); $id = explode(":", $item[0]); $meta = array_pop($id); @@ -246,34 +256,53 @@ class CraftingRecipes{ } } ksort($recipeItems); - $item = explode("x", $recipe[1]); - $id = explode(":", $item[0]); - $meta = array_pop($id); - $id = $id[0]; - - $it = BlockAPI::fromString($id); - - $craftItem = [$it->getID(), intval($meta) & 0xFFFF, intval($item[1])]; + + $craftItems = []; + foreach(explode(",", $results) as $item){ + $item = explode("x", $item); + $id = explode(":", $item[0]); + $meta = array_pop($id); + $id = $id[0]; + $it = BlockAPI::fromString($id); + if(!isset($craftItems[$it->getID()])){ + $craftItems[$it->getID()] = [$it->getID(), intval($meta) & 0xffff, intval($item[1])]; + }else{ + if($it->getMetadata() !== $craftItems[$it->getID()][1]){ + $craftItems[$it->getID()][1] = false; + } + $craftItems[$it->getID()][2] += $it->count; + } + } + ksort($craftItems); $recipeString = ""; + $resultString = ""; foreach($recipeItems as $item){ - $recipeString .= $item[0] . "x" . $item[2] . ","; + $recipeString .= "{$item[0]}x{$item[2]},"; } - $recipeString = substr($recipeString, 0, -1) . "=>" . $craftItem[0] . "x" . $craftItem[2]; + foreach($craftItems as $item){ + $resultString .= "{$item[0]}x{$item[2]},"; + } + + $recipeString = substr($recipeString, 0, -1) . "=>" . substr($resultString, 0, -1); - return [$recipeItems, $craftItem, $recipeString]; + return [$recipeItems, $craftItems, $recipeString]; } - public static function canCraft(array $craftItem, array $recipeItems, $type){ + public static function canCraft(array $craftItems, array $recipeItems, $type){ ksort($recipeItems); + ksort($craftItems); + $recipeString = ""; + $resultString = ""; 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] . ","; } - $recipeString = substr($recipeString, 0, -1) . "=>" . $craftItem[0] . "x" . $craftItem[2]; + foreach($craftItems as $item){ + $resultString .= "{$item[0]}x{$item[2]},"; + } + + $recipeString = substr($recipeString, 0, -1) . "=>" . substr($resultString, 0, -1); $server = ServerAPI::request(); $result = $server->query("SELECT id FROM recipes WHERE type == " . $type . " AND recipe == '" . $recipeString . "';"); if($result instanceof SQLite3Result){ @@ -281,21 +310,35 @@ class CraftingRecipes{ while(($r = $result->fetchArray(SQLITE3_NUM)) !== false){ $continue = true; $recipe = CraftingRecipes::$recipes[$r[0]]; - foreach($recipe[0] as $item){ + foreach($recipe[0] as $item){ //check ingridients 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 - $oitem[2] = 3; - } + 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){ + $continue = false; + continue; + } + foreach($recipe[1] as $item){ //check results + if(!isset($craftItems[$item[0]])){ + $continue = false; + break; + } + $oitem = $craftItems[$item[0]]; + + if(($oitem[1] !== $item[1] and $item[1] !== false) or $oitem[2] !== $item[2]){ + $continue = false; + break; + } + } + if($continue === false){ $continue = false; continue; }