Skip to content
This repository was archived by the owner on Apr 10, 2026. It is now read-only.

Commit 63b86da

Browse files
committed
fix: consolidate ingredient choices in ShapelessRecipe
1 parent 3a63fbb commit 63b86da

3 files changed

Lines changed: 52 additions & 3 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ dependencies {
2525
compileOnly(kotlin("stdlib")) // loaded through library loader
2626
compileOnly(kotlin("reflect")) // loaded through library loader
2727
compileOnlyAndTestImpl("io.papermc.paper:paper-api:1.21.1-R0.1-SNAPSHOT")
28-
compileOnlyAndTestImpl("com.github.Slimefun:Slimefun4:5374034c87")
28+
compileOnlyAndTestImpl("com.github.slimefun:Slimefun4:experimental-SNAPSHOT")
2929
compileOnly("net.guizhanss:SlimefunTranslation:e6da231617")
3030
compileOnly("com.github.schntgaispock:SlimeHUD:1.3.0")
3131
compileOnly("com.github.SlimefunGuguProject:InfinityExpansion:bebf0bd0f9")

src/main/kotlin/net/guizhanss/fastmachines/core/recipes/loaders/VanillaRecipeLoader.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package net.guizhanss.fastmachines.core.recipes.loaders
44

55
import net.guizhanss.fastmachines.core.recipes.raw.RawRecipe
66
import net.guizhanss.fastmachines.implementation.items.machines.base.BaseFastMachine
7+
import net.guizhanss.fastmachines.utils.consolidate
78
import net.guizhanss.fastmachines.utils.items.asFMRecipeChoice
89
import net.guizhanss.fastmachines.utils.reflections.resultItem
910
import org.bukkit.Bukkit
@@ -52,13 +53,13 @@ class VanillaRecipeLoader<T : Recipe>(
5253
}
5354

5455
is ShapelessRecipe -> {
55-
rawRecipes.add(RawRecipe(recipe.choiceList.map { it.asFMRecipeChoice() }, listOf(recipe.resultItem)))
56+
val ingredients = recipe.choiceList.map { it.asFMRecipeChoice() }.consolidate()
57+
rawRecipes.add(RawRecipe(ingredients, listOf(recipe.resultItem)))
5658
}
5759

5860
is CookingRecipe<*> -> {
5961
rawRecipes.add(RawRecipe(listOf(recipe.inputChoice.asFMRecipeChoice()), listOf(recipe.result)))
6062
}
6163
}
6264
}
63-
6465
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package net.guizhanss.fastmachines.utils
2+
3+
import net.guizhanss.fastmachines.core.recipes.choices.ExactChoice
4+
import net.guizhanss.fastmachines.core.recipes.choices.RecipeChoice
5+
import kotlin.collections.component1
6+
import kotlin.collections.component2
7+
import kotlin.collections.forEach
8+
9+
/**
10+
* Consolidate a list of [RecipeChoice] by merging [ExactChoice] with the same item.
11+
*
12+
* Other types of [RecipeChoice] will remain unchanged.
13+
*/
14+
fun List<RecipeChoice>.consolidate(): List<RecipeChoice> {
15+
val exactChoices = mutableListOf<ExactChoice>()
16+
val otherChoices = mutableListOf<RecipeChoice>()
17+
18+
this.forEach { choice ->
19+
when (choice) {
20+
is ExactChoice -> exactChoices.add(choice)
21+
else -> otherChoices.add(choice)
22+
}
23+
}
24+
25+
val mergedExactChoices = exactChoices.groupBy { it.item }.flatMap { (item, itemChoices) ->
26+
val totalAmount = itemChoices.sumOf { it.amount }
27+
val maxStackSize = item.baseItem.maxStackSize
28+
29+
if (totalAmount <= maxStackSize) {
30+
listOf(ExactChoice(item, totalAmount))
31+
} else {
32+
// Split into multiple choices if exceeding max stack size
33+
val fullStacks = totalAmount / maxStackSize
34+
val remainder = totalAmount % maxStackSize
35+
36+
buildList {
37+
repeat(fullStacks) {
38+
add(ExactChoice(item, maxStackSize))
39+
}
40+
if (remainder > 0) {
41+
add(ExactChoice(item, remainder))
42+
}
43+
}
44+
}
45+
}
46+
47+
return mergedExactChoices + otherChoices
48+
}

0 commit comments

Comments
 (0)