mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-23 13:09:44 +00:00
Merge pull request #77 from MineDg2665/fix/crafting-bulk-recipe-match
Fixed craft issue
This commit is contained in:
commit
5023bb2286
2 changed files with 71 additions and 16 deletions
|
|
@ -3384,7 +3384,38 @@ class Player{
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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){
|
||||||
|
ConsoleAPI::warn("Craft aborted: claimed ingredient {$id}:{$meta} x{$count} not present in slot {$slot}");
|
||||||
|
$this->toCraft = [];
|
||||||
|
$this->craftingItems = [];
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($this->toCraft as $i => $slotz){
|
||||||
|
$id = $i >> 16;
|
||||||
|
$meta = $i & 0xffff;
|
||||||
|
foreach($slotz as $slot => $count){
|
||||||
|
if($slot < 0) continue;
|
||||||
|
$slt = $this->getSlot($slot);
|
||||||
|
$max = BlockAPI::getItem($id, $meta, 1)->getMaxStackSize();
|
||||||
|
$have = ($slt->getID() === $id && $slt->getMetadata() === $meta) ? $slt->count : 0;
|
||||||
|
if($have + $count > $max){
|
||||||
|
ConsoleAPI::warn("Craft aborted: output {$id}:{$meta} x{$count} does not fit slot {$slot}");
|
||||||
|
$this->toCraft = [];
|
||||||
|
$this->craftingItems = [];
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if($this->server->api->dhandle("player.craft", ["player" => $this, "ingridients" => $this->craftingItems, "results" => $this->toCraft, "type" => $this->craftingType]) === false){
|
if($this->server->api->dhandle("player.craft", ["player" => $this, "ingridients" => $this->craftingItems, "results" => $this->toCraft, "type" => $this->craftingType]) === false){
|
||||||
$this->toCraft = [];
|
$this->toCraft = [];
|
||||||
$this->craftingItems = [];
|
$this->craftingItems = [];
|
||||||
|
|
|
||||||
|
|
@ -236,14 +236,17 @@ class CraftingRecipes{
|
||||||
|
|
||||||
public static function addRecipe($recipe, $type){
|
public static function addRecipe($recipe, $type){
|
||||||
[$ingridients, $results] = explode("=>", $recipe);
|
[$ingridients, $results] = explode("=>", $recipe);
|
||||||
$results_arr = []; //indexed by id, must be rewritten in case some recipe will craft 2 items with same id but different metadata
|
$results_arr = []; //indexed by id, full [id, meta, cnt]
|
||||||
|
$results_idmeta = []; //indexed by id, "id:meta" only
|
||||||
foreach(explode(",", $results) as $res){
|
foreach(explode(",", $results) as $res){
|
||||||
[$id, $meta, $cnt] = self::fromString($res);
|
[$id, $meta, $cnt] = self::fromString($res);
|
||||||
if($meta === "?") throw new RuntimeException("Unknown metadata in result when trying to add $recipe (type: $type)");
|
if($meta === "?") throw new RuntimeException("Unknown metadata in result when trying to add $recipe (type: $type)");
|
||||||
$results_arr[$id] = "{$id}:{$meta}x{$cnt}";
|
$results_arr[$id] = [$id, $meta, $cnt];
|
||||||
|
$results_idmeta[$id] = "{$id}:{$meta}";
|
||||||
}
|
}
|
||||||
ksort($results_arr);
|
ksort($results_arr);
|
||||||
$result_index = implode(",", $results_arr);
|
ksort($results_idmeta);
|
||||||
|
$result_index = implode(",", $results_idmeta);
|
||||||
|
|
||||||
$ingridients_arr = [];
|
$ingridients_arr = [];
|
||||||
foreach(explode(",", $ingridients) as $resultstr){
|
foreach(explode(",", $ingridients) as $resultstr){
|
||||||
|
|
@ -269,17 +272,17 @@ class CraftingRecipes{
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!isset($arr[$result_index])) $arr[$result_index] = [];
|
if(!isset($arr[$result_index])) $arr[$result_index] = [];
|
||||||
$arr[$result_index][] = $ingridients_arr;
|
$arr[$result_index][] = ["in" => $ingridients_arr, "out" => array_values($results_arr)];
|
||||||
|
|
||||||
foreach($results_arr as $resitem){
|
foreach($results_idmeta as $resitem){
|
||||||
if(!isset($arr_c[$resitem])){
|
if(!isset($arr_c[$resitem])){
|
||||||
$arr_c[$resitem] = [];
|
$arr_c[$resitem] = [];
|
||||||
}
|
}
|
||||||
foreach($results_arr as $resitem2){
|
foreach($results_idmeta as $resitem2){
|
||||||
$arr_c[$resitem][$resitem2] = true;
|
$arr_c[$resitem][$resitem2] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -292,8 +295,11 @@ class CraftingRecipes{
|
||||||
*/
|
*/
|
||||||
public static function canCraft(array $craftItems, array $recipeItems, $type){
|
public static function canCraft(array $craftItems, array $recipeItems, $type){
|
||||||
$craftIndexArr = [];
|
$craftIndexArr = [];
|
||||||
|
$craftCount = [];
|
||||||
foreach($craftItems as $it){
|
foreach($craftItems as $it){
|
||||||
$craftIndexArr[$it[0]] = "{$it[0]}:{$it[1]}x{$it[2]}";
|
$k = "{$it[0]}:{$it[1]}";
|
||||||
|
$craftIndexArr[$it[0]] = $k;
|
||||||
|
$craftCount[$k] = $it[2];
|
||||||
}
|
}
|
||||||
ksort($craftIndexArr);
|
ksort($craftIndexArr);
|
||||||
$craftIndex = implode(",", $craftIndexArr);
|
$craftIndex = implode(",", $craftIndexArr);
|
||||||
|
|
@ -314,7 +320,7 @@ class CraftingRecipes{
|
||||||
ConsoleAPI::error("Tried crafting recipe with unknown type {$type}!");
|
ConsoleAPI::error("Tried crafting recipe with unknown type {$type}!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!isset($arr[$craftIndex])) { //recipe for those ingridients was not found
|
if(!isset($arr[$craftIndex])) { //recipe for those ingridients was not found
|
||||||
$allcombos = $arr_c[current($craftIndexArr)] ?? [];
|
$allcombos = $arr_c[current($craftIndexArr)] ?? [];
|
||||||
foreach($craftIndexArr as $res){
|
foreach($craftIndexArr as $res){
|
||||||
|
|
@ -326,11 +332,28 @@ class CraftingRecipes{
|
||||||
ConsoleAPI::info("Recipe with $craftIndex is not found but it might be found later. Crafting continued.");
|
ConsoleAPI::info("Recipe with $craftIndex is not found but it might be found later. Crafting continued.");
|
||||||
return true; //recipe might be found next time
|
return true; //recipe might be found next time
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($arr[$craftIndex] as $ingridients){
|
foreach($arr[$craftIndex] as $recipe){
|
||||||
|
$ingridients = $recipe["in"];
|
||||||
|
$outputs = $recipe["out"];
|
||||||
|
|
||||||
|
$multiplier = null;
|
||||||
|
$ok = true;
|
||||||
|
foreach($outputs as $out){
|
||||||
|
$k = "{$out[0]}:{$out[1]}";
|
||||||
|
if(!isset($craftCount[$k]) || $out[2] <= 0 || $craftCount[$k] % $out[2] !== 0 || ($craftCount[$k] / $out[2]) < 1){
|
||||||
|
$ok = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$m = $craftCount[$k] / $out[2];
|
||||||
|
if($multiplier === null) $multiplier = $m;
|
||||||
|
elseif($multiplier !== $m) $ok = false;
|
||||||
|
}
|
||||||
|
if(!$ok) continue;
|
||||||
|
|
||||||
foreach($ingridients as $item){
|
foreach($ingridients as $item){
|
||||||
if($item[1] === "?"){ // any metadata is allowed
|
if($item[1] === "?"){ // any metadata is allowed
|
||||||
$needcnt = $item[2];
|
$needcnt = $item[2] * $multiplier;
|
||||||
foreach($recipeItems as $idmeta => $it){
|
foreach($recipeItems as $idmeta => $it){
|
||||||
$id = $idmeta >> 16;
|
$id = $idmeta >> 16;
|
||||||
if($id != $item[0]) continue;
|
if($id != $item[0]) continue;
|
||||||
|
|
@ -345,14 +368,15 @@ class CraftingRecipes{
|
||||||
goto skip_recipe; //dont check count if no idmeta pair is in ingridients
|
goto skip_recipe; //dont check count if no idmeta pair is in ingridients
|
||||||
}
|
}
|
||||||
$it = $recipeItems[$exceptedIndex];
|
$it = $recipeItems[$exceptedIndex];
|
||||||
if($it[0] != $item[0] || $it[1] != $item[1] || $it[2] != $item[2]) {
|
$need = $item[2] * $multiplier;
|
||||||
|
if($it[0] != $item[0] || $it[1] != $item[1] || $it[2] != $need) {
|
||||||
goto skip_recipe;
|
goto skip_recipe;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//recipe is correct
|
//recipe is correct
|
||||||
return [$craftItems, $ingridients];
|
return [$craftItems, $ingridients];
|
||||||
|
|
||||||
skip_recipe:
|
skip_recipe:
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue