GenisysPro  for Minecraft PE/Windows 10 v1.1.x
Feature-rich server software for Minecraft PE and Windows 10 Edition
CraftingManager Class Reference

Public Member Functions

 __construct ()
 
 buildCraftingDataCache ()
 
 getCraftingDataPacket ()
 
 sort (Item $i1, Item $i2)
 
 getRecipe (UUID $id)
 
 getRecipes ()
 
 getRecipesByResult (Item $item)
 
 getFurnaceRecipes ()
 
 matchFurnaceRecipe (Item $input)
 
 matchBrewingRecipe (Item $input, Item $potion)
 
 registerShapelessRecipe (ShapelessRecipe $recipe)
 
 registerFurnaceRecipe (FurnaceRecipe $recipe)
 
 registerBrewingRecipe (BrewingRecipe $recipe)
 
 matchRecipe (ShapelessRecipe $recipe)
 
 registerRecipe (Recipe $recipe)
 

Data Fields

 $recipes = []
 
 $furnaceRecipes = []
 
 $brewingRecipes = []
 

Protected Member Functions

 registerBrewingStand ()
 

Protected Attributes

 $recipeLookup = []
 

Constructor & Destructor Documentation

◆ __construct()

__construct ( )

CraftingManager constructor.

53  {
54  $this->registerBrewingStand();
55 
56  // load recipes from src/pocketmine/resources/recipes.json
57  $recipes = new Config(Server::getInstance()->getFilePath() . "src/pocketmine/resources/recipes.json", Config::JSON, []);
58 
59  MainLogger::getLogger()->info("Loading recipes...");
60  foreach($recipes->getAll() as $recipe){
61  switch($recipe["type"]){
62  case 0:
63  // TODO: handle multiple result items
64  $first = $recipe["output"][0];
65  $result = new ShapelessRecipe(Item::get($first["id"], $first["damage"], $first["count"], $first["nbt"]));
66 
67  foreach($recipe["input"] as $ingredient){
68  $result->addIngredient(Item::get($ingredient["id"], $ingredient["damage"], $ingredient["count"], $first["nbt"]));
69  }
70  $this->registerRecipe($result);
71  break;
72  case 1:
73  // TODO: handle multiple result items
74  $first = $recipe["output"][0];
75  $result = new ShapedRecipe(Item::get($first["id"], $first["damage"], $first["count"], $first["nbt"]), $recipe["height"], $recipe["width"]);
76 
77  $shape = array_chunk($recipe["input"], $recipe["width"]);
78  foreach($shape as $y => $row){
79  foreach($row as $x => $ingredient){
80  $result->addIngredient($x, $y, Item::get($ingredient["id"], ($ingredient["damage"] < 0 ? -1 : $ingredient["damage"]), $ingredient["count"], $ingredient["nbt"]));
81  }
82  }
83  $this->registerRecipe($result);
84  break;
85  case 2:
86  case 3:
87  $result = $recipe["output"];
88  $resultItem = Item::get($result["id"], $result["damage"], $result["count"], $result["nbt"]);
89  $this->registerRecipe(new FurnaceRecipe($resultItem, Item::get($recipe["inputId"], $recipe["inputDamage"] ?? -1, 1)));
90  break;
91  default:
92  break;
93  }
94  }
95 
96  $this->buildCraftingDataCache();
97  }

Member Function Documentation

◆ buildCraftingDataCache()

buildCraftingDataCache ( )

Rebuilds the cached CraftingDataPacket.

102  {
104  $pk = new CraftingDataPacket();
105  $pk->cleanRecipes = true;
106 
107  foreach($this->recipes as $recipe){
108  if($recipe instanceof ShapedRecipe){
109  $pk->addShapedRecipe($recipe);
110  }elseif($recipe instanceof ShapelessRecipe){
111  $pk->addShapelessRecipe($recipe);
112  }
113  }
114 
115  foreach($this->furnaceRecipes as $recipe){
116  $pk->addFurnaceRecipe($recipe);
117  }
118 
119  $pk->encode();
120  $pk->isEncoded = true;
121 
122  $this->craftingDataCache = $pk;
124  }

◆ getCraftingDataPacket()

getCraftingDataPacket ( )

Returns a CraftingDataPacket for sending to players. Rebuilds the cache if it is outdated.

Returns
CraftingDataPacket
131  : CraftingDataPacket{
132  if($this->craftingDataCache === null){
133  $this->buildCraftingDataCache();
134  }
135 
136  return $this->craftingDataCache;
137  }

◆ getFurnaceRecipes()

getFurnaceRecipes ( )
Returns
FurnaceRecipe[]
331  {
332  return $this->furnaceRecipes;
333  }

◆ getRecipe()

getRecipe ( UUID  $id)
Parameters
UUID$id
Returns
Recipe
307  {
308  $index = $id->toBinary();
309  return $this->recipes[$index] ?? null;
310  }

◆ getRecipes()

getRecipes ( )
Returns
Recipe[]
315  {
316  return $this->recipes;
317  }

◆ getRecipesByResult()

getRecipesByResult ( Item  $item)
Parameters
Item$item
Returns
array
324  {
325  return @array_values($this->recipeLookup[$item->getId() . ":" . $item->getDamage()]) ?? [];
326  }

◆ matchBrewingRecipe()

matchBrewingRecipe ( Item  $input,
Item  $potion 
)
Parameters
Item$input
Item$potion
Returns
BrewingRecipe
356  {
357  $subscript = $input->getId() . ":" . ($input->getDamage() === null ? "0" : $input->getDamage()) . ":" . $potion->getId() . ":" . ($potion->getDamage() === null ? "0" : $potion->getDamage());
358  if(isset($this->brewingRecipes[$subscript])){
359  return $this->brewingRecipes[$subscript];
360  }
361  return null;
362  }

◆ matchFurnaceRecipe()

matchFurnaceRecipe ( Item  $input)
Parameters
Item$input
Returns
FurnaceRecipe
340  {
341  if(isset($this->furnaceRecipes[$input->getId() . ":" . $input->getDamage()])){
342  return $this->furnaceRecipes[$input->getId() . ":" . $input->getDamage()];
343  }elseif(isset($this->furnaceRecipes[$input->getId() . ":?"])){
344  return $this->furnaceRecipes[$input->getId() . ":?"];
345  }
346  return null;
347  }

◆ matchRecipe()

matchRecipe ( ShapelessRecipe  $recipe)
Parameters
ShapelessRecipe$recipe
Returns
bool
424  {
425  if(!isset($this->recipeLookup[$idx = $recipe->getResult()->getId() . ":" . $recipe->getResult()->getDamage()])){
426  return false;
427  }
428  $hash = "";
429  $ingredients = $recipe->getIngredientList();
430  usort($ingredients, [$this, "sort"]);
431  foreach($ingredients as $item){
432  $hash .= $item->getId() . ":" . ($item->hasAnyDamageValue() ? "?" : $item->getDamage()) . "x" . $item->getCount() . ",";
433  }
434  if(isset($this->recipeLookup[$idx][$hash])){
435  return true;
436  }
437  $hasRecipe = null;
438  foreach($this->recipeLookup[$idx] as $recipe){
439  if($recipe instanceof ShapelessRecipe){
440  if($recipe->getIngredientCount() !== count($ingredients)){
441  continue;
442  }
443  $checkInput = $recipe->getIngredientList();
444  foreach($ingredients as $item){
445  $amount = $item->getCount();
446  foreach($checkInput as $k => $checkItem){
447  if($checkItem->equals($item, !$checkItem->hasAnyDamageValue(), $checkItem->hasCompoundTag())){
448  $remove = min($checkItem->getCount(), $amount);
449  $checkItem->setCount($checkItem->getCount() - $remove);
450  if($checkItem->getCount() === 0){
451  unset($checkInput[$k]);
452  }
453  $amount -= $remove;
454  if($amount === 0){
455  break;
456  }
457  }
458  }
459  }
460  if(count($checkInput) === 0){
461  $hasRecipe = $recipe;
462  break;
463  }
464  }
465  if($hasRecipe instanceof Recipe){
466  break;
467  }
468  }
469  return $hasRecipe !== null;
470  }

◆ registerBrewingRecipe()

registerBrewingRecipe ( BrewingRecipe  $recipe)
Parameters
BrewingRecipe$recipe
413  {
414  $input = $recipe->getInput();
415  $potion = $recipe->getPotion();
416  $this->brewingRecipes[$input->getId() . ":" . ($input->getDamage() === null ? "0" : $input->getDamage()) . ":" . $potion->getId() . ":" . ($potion->getDamage() === null ? "0" : $potion->getDamage())] = $recipe;
417  }

◆ registerBrewingStand()

registerBrewingStand ( )
protected
139  {
140  //Potion
141  //WATER_BOTTLE
146 
154  //To WEAKNESS
159  //GHAST_TEAR and BLAZE_POWDER
163 
167  //SPIDER_EYE GLISTERING_MELON and PUFFERFISH
175 
182  //SUGAR MAGMA_CREAM and RABBIT_FOOT
191 
199  //GOLDEN_CARROT
205  //===================================================================分隔符=======================================================================
206  //SPLASH_POTION
207  //WATER_BOTTLE
212 
220  //To WEAKNESS
225  //GHAST_TEAR and BLAZE_POWDER
229 
233  //SPIDER_EYE GLISTERING_MELON and PUFFERFISH
241 
248  //SUGAR MAGMA_CREAM and RABBIT_FOOT
257 
265  //GOLDEN_CARROT
271  //===================================================================分隔符=======================================================================
272  //普通药水升级成喷溅
273  foreach(Potion::POTIONS as $potion => $effect){
274  $this->registerBrewingRecipe(new BrewingRecipe(Item::get(Item::SPLASH_POTION, $potion, 1), Item::get(Item::GUNPOWDER, 0, 1), Item::get(Item::POTION, $potion, 1)));
275  }
276  }

◆ registerFurnaceRecipe()

registerFurnaceRecipe ( FurnaceRecipe  $recipe)
Parameters
FurnaceRecipe$recipe
404  {
405  $input = $recipe->getInput();
406  $this->furnaceRecipes[$input->getId() . ":" . ($input->hasAnyDamageValue() ? "?" : $input->getDamage())] = $recipe;
407  $this->craftingDataCache = null;
408  }

◆ registerRecipe()

registerRecipe ( Recipe  $recipe)
Parameters
Recipe$recipe
475  {
476  $recipe->setId(UUID::fromData(++self::$RECIPE_COUNT, $recipe->getResult()->getId(), $recipe->getResult()->getDamage(), $recipe->getResult()->getCount(), $recipe->getResult()->getCompoundTag()));
477  if($recipe instanceof ShapedRecipe){
478  $this->registerShapedRecipe($recipe);
479  }elseif($recipe instanceof ShapelessRecipe){
480  $this->registerShapelessRecipe($recipe);
481  }elseif($recipe instanceof FurnaceRecipe){
482  $this->registerFurnaceRecipe($recipe);
483  }
484  }

◆ registerShapelessRecipe()

registerShapelessRecipe ( ShapelessRecipe  $recipe)
Parameters
ShapelessRecipe$recipe
388  {
389  $result = $recipe->getResult();
390  $this->recipes[$recipe->getId()->toBinary()] = $recipe;
391  $hash = "";
392  $ingredients = $recipe->getIngredientList();
393  usort($ingredients, [$this, "sort"]);
394  foreach($ingredients as $item){
395  $hash .= $item->getId() . ":" . ($item->hasAnyDamageValue() ? "?" : $item->getDamage()) . "x" . $item->getCount() . ",";
396  }
397  $this->recipeLookup[$result->getId() . ":" . $result->getDamage()][$hash] = $recipe;
398  $this->craftingDataCache = null;
399  }

◆ sort()

sort ( Item  $i1,
Item  $i2 
)
Parameters
Item$i1
Item$i2
Returns
int
284  {
285  if($i1->getId() > $i2->getId()){
286  return 1;
287  }elseif($i1->getId() < $i2->getId()){
288  return -1;
289  }elseif($i1->getDamage() > $i2->getDamage()){
290  return 1;
291  }elseif($i1->getDamage() < $i2->getDamage()){
292  return -1;
293  }elseif($i1->getCount() > $i2->getCount()){
294  return 1;
295  }elseif($i1->getCount() < $i2->getCount()){
296  return -1;
297  }else{
298  return 0;
299  }
300  }

Field Documentation

◆ $brewingRecipes

$brewingRecipes = []

◆ $furnaceRecipes

$furnaceRecipes = []

◆ $recipeLookup

$recipeLookup = []
protected

◆ $recipes

$recipes = []

The documentation for this class was generated from the following file: