diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/MatterManager.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/MatterManager.kt index 42c099dae..02690d8a7 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/MatterManager.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/MatterManager.kt @@ -492,7 +492,7 @@ object MatterManager { if (recipes == null || recipes.isEmpty() || !recipes.all { it.allowBacktrack } || !recipes.all { it.inputs.all { it.all { it.item == item } } }) { if (makeCommentary) - commentary[item] = TextComponent("Item '${item.registryName}' has no recipes") + comment(item, TextComponent("Item '${item.registryName}' has no recipes")) return Result.MISSING } @@ -519,7 +519,7 @@ object MatterManager { if (minimal == null) { if (makeCommentary) - commentary[item] = TextComponent("Item '${item.registryName}' has no valid backtracking recipes, and no output recipes") + comment(item, TextComponent("Item '${item.registryName}' has no valid backtracking recipes, and no output recipes")) return Result.MISSING } @@ -528,7 +528,7 @@ object MatterManager { changes = true determinedValues[item] = result - commentary[item] = TextComponent("Matter value backtracked from ${foundRecipe!!.formattedName}") + comment(item, TextComponent("Matter value backtracked from ${foundRecipe!!.formattedName}")) return Result(result) } @@ -588,7 +588,7 @@ object MatterManager { recipesLoop@ for (recipe in recipes) { if (recipe.inputs.isEmpty()) { // TODO: should we ignore empty recipes? - commentary[item] = TextComponent("${recipe.formattedName} is empty") + comment(item, TextComponent("${recipe.formattedName} is empty")) continue } @@ -604,8 +604,8 @@ object MatterManager { val ivalue = determineValue(input.item) if (ivalue.isMissing) { - if (recipe.isCritical || item !in commentary) - commentary[item] = TextComponent("Input '${input.item.registryName}' at input slot $i in ${recipe.formattedName} has no matter value") + if (recipe.isCritical) + comment(item, TextComponent("Input '${input.item.registryName}' at input slot $i in ${recipe.formattedName} has no matter value")) if (recipe.isCritical) { return Result.MISSING @@ -613,8 +613,8 @@ object MatterManager { continue@recipesLoop } } else if (ivalue.isSkipped) { - if (recipe.isCritical || item !in commentary) - commentary[item] = TextComponent("Input '${input.item.registryName}' at input slot $i in ${recipe.formattedName} is recursive") + if (recipe.isCritical) + comment(item, TextComponent("Input '${input.item.registryName}' at input slot $i in ${recipe.formattedName} is recursive")) hadSkips = true continue@recipesLoop @@ -627,7 +627,7 @@ object MatterManager { } if (minimal == null || !minimal.hasMatterValue) { - commentary[item] = TextComponent("'${recipe.formattedName}' has invalid input at slot $i (possible inputs: ${inputs.joinToString(", ", transform = { it.item.registryName.toString() }) }) (???)") + comment(item, TextComponent("'${recipe.formattedName}' has invalid input at slot $i (possible inputs: ${inputs.joinToString(", ", transform = { it.item.registryName.toString() }) }) (???)")) return Result.MISSING } @@ -645,7 +645,7 @@ object MatterManager { } if (!accumulatedMatter.isPositive || accumulatedComplexity <= 0.0) { - commentary[item] = TextComponent("${recipe.formattedName} ended up with negative matter value and/or complexity (???)") + comment(item, TextComponent("${recipe.formattedName} ended up with negative matter value and/or complexity (???)")) return Result.MISSING } @@ -667,8 +667,7 @@ object MatterManager { } } - if (item !in commentary) - commentary[item] = TextComponent("'${item.registryName}' ended up with no valid recipes (???)") + comment(item, TextComponent("'${item.registryName}' ended up with no valid recipes (???)")) return Result.MISSING } @@ -677,7 +676,7 @@ object MatterManager { changes = true determinedValues[item] = result - commentary[item] = TextComponent("Matter value derived from ${minimalRecipe!!.formattedName}") + comment(item, TextComponent("Matter value derived from ${minimalRecipe!!.formattedName}")) return Result(result) } @@ -811,8 +810,8 @@ object MatterManager { } for (item in toDetermine) { - if (item !in commentary) { - commentary[item] = TextComponent("Item ${item.registryName} was never visited") + if (item !in commentary || commentary[item]!!.isEmpty()) { + comment(item, TextComponent("Item ${item.registryName} was never visited")) } } @@ -977,10 +976,12 @@ object MatterManager { } if (event.flags.isAdvanced) { - val commentary = getCommentary(event.itemStack.item)?.withStyle(ChatFormatting.DARK_GRAY) + val commentary = getCommentary(event.itemStack.item) if (commentary != null) { - event.toolTip.add(commentary) + for (value in commentary) { + event.toolTip.add(value.withStyle(ChatFormatting.DARK_GRAY)) + } } } } @@ -1060,11 +1061,22 @@ object MatterManager { @JvmStatic val recipeFinders get() = Resolver.delegate.get() @JvmStatic val recipeFindersKey get() = Resolver.delegate.key - private val commentary = Reference2ObjectOpenHashMap() + private val commentary = Reference2ObjectOpenHashMap>() @JvmStatic - fun getCommentary(item: Item): MutableComponent? { - return commentary[item]?.copy() + fun getCommentary(item: Item): List? { + return commentary[item]?.let { source -> + ArrayList(source.size) + .also { for (value in source) it.add(value.copy()) } + } + } + + private fun comment(item: Item, comment: Component) { + val target = commentary.computeIfAbsent(item, Reference2ObjectFunction { ArrayList(1) }) + + if (comment !in target) { + target.add(comment) + } } fun reloadEvent(event: AddReloadListenerEvent) { @@ -1121,9 +1133,9 @@ object MatterManager { val (key, item) = entry if (!value.hasMatterValue) { - writer.write(arrayOf(key.location().toString(), "", "", commentary[item]?.string ?: "").joinToString(";", transform = ::transformQuotes)) + writer.write(arrayOf(key.location().toString(), "", "", commentary[item]?.joinToString("\n", transform = { transformQuotes(it.string) }) ?: "").joinToString(";", transform = ::transformQuotes)) } else { - writer.write(arrayOf(key.location().toString(), value.matter.toString(), value.complexity.toString(), commentary[item]?.string ?: "").joinToString(";", transform = ::transformQuotes)) + writer.write(arrayOf(key.location().toString(), value.matter.toString(), value.complexity.toString(), commentary[item]?.joinToString("\n", transform = { transformQuotes(it.string) }) ?: "").joinToString(";", transform = ::transformQuotes)) } writer.write("\n")