Exemplo n.º 1
0
 private static function writeShapelessRecipe(ShapelessRecipe $recipe, BinaryStream $stream)
 {
     $stream->putInt($recipe->getIngredientCount());
     foreach ($recipe->getIngredientList() as $item) {
         $stream->putSlot($item);
     }
     $stream->putInt(1);
     $stream->putSlot($recipe->getResult());
     $stream->putUUID($recipe->getId());
     return CraftingDataPacket::ENTRY_SHAPELESS;
 }
Exemplo n.º 2
0
 /**
  * @param ShapelessRecipe $recipe
  * @return bool
  */
 public function matchRecipe(ShapelessRecipe $recipe)
 {
     if (!isset($this->recipeLookup[$idx = $recipe->getResult()->getId() . ":" . $recipe->getResult()->getDamage()])) {
         return false;
     }
     $hash = "";
     $ingredients = $recipe->getIngredientList();
     usort($ingredients, [$this, "sort"]);
     foreach ($ingredients as $item) {
         $hash .= $item->getId() . ":" . ($item->getDamage() === null ? "?" : $item->getDamage()) . "x" . $item->getCount() . ",";
     }
     if (isset($this->recipeLookup[$idx][$hash])) {
         return true;
     }
     $hasRecipe = null;
     foreach ($this->recipeLookup[$idx] as $recipe) {
         if ($recipe instanceof ShapelessRecipe) {
             if ($recipe->getIngredientCount() !== count($ingredients)) {
                 continue;
             }
             $checkInput = $recipe->getIngredientList();
             foreach ($ingredients as $item) {
                 $amount = $item->getCount();
                 foreach ($checkInput as $k => $checkItem) {
                     if ($checkItem->equals($item, $checkItem->getDamage() === null ? false : true, $checkItem->getCompoundTag() === null ? false : true)) {
                         $remove = min($checkItem->getCount(), $amount);
                         $checkItem->setCount($checkItem->getCount() - $remove);
                         if ($checkItem->getCount() === 0) {
                             unset($checkInput[$k]);
                         }
                         $amount -= $remove;
                         if ($amount === 0) {
                             break;
                         }
                     }
                 }
             }
             if (count($checkInput) === 0) {
                 $hasRecipe = $recipe;
                 break;
             }
         }
         if ($hasRecipe instanceof Recipe) {
             break;
         }
     }
     return $hasRecipe !== null;
 }