Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend: Create and use primitive recipe and ingredient #2460

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package at.hannibal2.skyhanni.features.bingo

import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.NEUInternalName
import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName
import at.hannibal2.skyhanni.utils.NEUItems
import at.hannibal2.skyhanni.utils.NEUItems.getCachedIngredients
import at.hannibal2.skyhanni.utils.NEUItems.getItemStackOrNull
import at.hannibal2.skyhanni.utils.PrimitiveRecipe
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import io.github.moulberry.notenoughupdates.recipes.CraftingRecipe

object FirstMinionTier {

Expand All @@ -30,20 +29,20 @@ object FirstMinionTier {
) {
for (minionId in tierOneMinionsFiltered) {
for (recipe in NEUItems.getRecipes(minionId)) {
if (recipe !is CraftingRecipe) continue
if (!recipe.isCraftingRecipe()) continue
checkOne(recipe, help, minions, minionId)
}
}
}

private fun checkOne(
recipe: CraftingRecipe,
recipe: PrimitiveRecipe,
help: Map<NEUInternalName, Int>,
minions: MutableMap<String, NEUInternalName>,
minionId: NEUInternalName,
) {
if (recipe.getCachedIngredients().any { help.contains(it.internalItemId.asInternalName()) }) {
val name = recipe.output.itemStack.name.removeColor()
if (recipe.getCachedIngredients().any { help.contains(it.internalName) }) {
val name = recipe.output?.internalName?.getItemStackOrNull()?.displayName?.removeColor() ?: return
val abc = name.replace(" I", " 0")
minions[abc] = minionId.replace("_1", "_0")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import io.github.moulberry.notenoughupdates.recipes.CraftingRecipe
import net.minecraft.client.Minecraft
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
Expand Down Expand Up @@ -139,7 +138,7 @@ object MinionCraftHelper {

for (recipe in recipes) {
for (ingredient in recipe.getCachedIngredients()) {
val ingredientInternalName = ingredient.internalItemId.asInternalName()
val ingredientInternalName = ingredient.internalName
if (ingredientInternalName == internalName) return true

val ingredientPrimitive = NEUItems.getPrimitiveMultiplier(ingredientInternalName)
Expand Down Expand Up @@ -169,10 +168,10 @@ object MinionCraftHelper {

if (internalName.contains("_GENERATOR_")) {
for (recipe in NEUItems.getRecipes(internalName)) {
if (recipe !is CraftingRecipe) continue
if (!recipe.isCraftingRecipe()) continue

for (ingredient in recipe.getCachedIngredients()) {
val id = ingredient.internalItemId.asInternalName()
val id = ingredient.internalName
if (!id.contains("_GENERATOR_") && !allIngredients.contains(id)) {
allIngredients.add(id)
}
Expand All @@ -194,13 +193,12 @@ object MinionCraftHelper {
newDisplay.add(minionName)
val nextMinionId = minionId.addOneToId()
for (recipe in NEUItems.getRecipes(nextMinionId)) {
if (recipe !is CraftingRecipe) continue
val output = recipe.output
val internalItemId = output.internalItemId.asInternalName()
if (!internalItemId.contains("_GENERATOR_")) continue
if (!recipe.isCraftingRecipe()) continue
val output = recipe.output ?: continue
if (!output.internalName.contains("_GENERATOR_")) continue
val map = mutableMapOf<NEUInternalName, Int>()
for (input in recipe.inputs) {
val itemId = input.internalItemId.asInternalName()
for (input in recipe.ingredients) {
val itemId = input.internalName
if (minionId != itemId) {
val count = input.count.toInt()
val old = map.getOrDefault(itemId, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import at.hannibal2.skyhanni.utils.LorenzUtils.isInIsland
import at.hannibal2.skyhanni.utils.NEUInternalName
import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName
import at.hannibal2.skyhanni.utils.NEUItems
import at.hannibal2.skyhanni.utils.NEUItems.allIngredients
import at.hannibal2.skyhanni.utils.NEUItems.getItemStack
import at.hannibal2.skyhanni.utils.NEUItems.getPrice
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
Expand Down Expand Up @@ -250,13 +249,14 @@ object GardenVisitorFeatures {

val ingredients = NEUItems.getRecipes(internalName)
// TODO describe what this line does
.firstOrNull() { !it.allIngredients().first().internalItemId.contains("PEST") }
?.allIngredients() ?: emptySet()
.firstOrNull { !it.ingredients.first().internalName.contains("PEST") }
?.ingredients ?: emptySet()
if (ingredients.isEmpty()) return

// TODO change key to NEUInternalName
val requiredIngredients = mutableMapOf<String, Int>()
for (ingredient in ingredients) {
val key = ingredient.internalItemId
val key = ingredient.internalName.asString()
requiredIngredients[key] =
requiredIngredients.getOrDefault(key, 0) + ingredient.count.toInt()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ import at.hannibal2.skyhanni.events.render.gui.ReplaceItemEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.HypixelCommands
import at.hannibal2.skyhanni.utils.ItemUtils.itemName
import at.hannibal2.skyhanni.utils.NEUInternalName
import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName
import at.hannibal2.skyhanni.utils.NEUItems
import at.hannibal2.skyhanni.utils.NEUItems.allIngredients
import at.hannibal2.skyhanni.utils.NEUItems.getItemStack
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import io.github.moulberry.notenoughupdates.util.Utils
import net.minecraft.entity.player.InventoryPlayer
import net.minecraftforge.fml.common.eventhandler.EventPriority
Expand Down Expand Up @@ -74,11 +71,12 @@ object GardenVisitorSupercraft {
private fun getSupercraftForSacks(internalName: NEUInternalName, amount: Int) {
val ingredients = NEUItems.getRecipes(internalName)
// TODO describe what this line does
.firstOrNull { !it.allIngredients().first().internalItemId.contains("PEST") }
?.allIngredients() ?: return
.firstOrNull { !it.ingredients.first().internalName.contains("PEST") }
?.ingredients ?: return
// TODO change key to NEUInternalName
val ingredientReqs = mutableMapOf<String, Int>()
for (ingredient in ingredients) {
val key = ingredient.internalItemId
val key = ingredient.internalName.asString()
ingredientReqs[key] = ingredientReqs.getOrDefault(key, 0) + ingredient.count.toInt()
}
hasIngredients = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import at.hannibal2.skyhanni.utils.NEUItems.isVanillaItem
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.NumberUtil.shortFormat
import at.hannibal2.skyhanni.utils.PrimitiveItemStack.Companion.toPrimitiveStackOrNull
import at.hannibal2.skyhanni.utils.PrimitiveRecipe
import at.hannibal2.skyhanni.utils.RegexUtils.matches
import at.hannibal2.skyhanni.utils.RenderUtils.renderRenderables
import at.hannibal2.skyhanni.utils.StringUtils
import at.hannibal2.skyhanni.utils.renderables.Renderable
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import io.github.moulberry.notenoughupdates.recipes.CraftingRecipe
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.math.floor

Expand Down Expand Up @@ -71,15 +71,15 @@ object CraftableItemList {

val recipes = NEUItems.getRecipes(internalName)
for (recipe in recipes) {
if (recipe !is CraftingRecipe) continue
if (!recipe.isCraftingRecipe()) continue
val renderable = createItemRenderable(recipe, availableMaterial, pricePer, internalName) ?: continue
lines[internalName] = renderable
}
}
}

private fun createItemRenderable(
recipe: CraftingRecipe,
recipe: PrimitiveRecipe,
availableMaterial: Map<NEUInternalName, Long>,
pricePer: MutableMap<NEUInternalName, Double>,
internalName: NEUInternalName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.LorenzRarity
import at.hannibal2.skyhanni.utils.NEUInternalName
import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName
import at.hannibal2.skyhanni.utils.NEUItems
import at.hannibal2.skyhanni.utils.NEUItems.getItemStackOrNull
import at.hannibal2.skyhanni.utils.NEUItems.getNpcPriceOrNull
import at.hannibal2.skyhanni.utils.NEUItems.getPriceOrNull
import at.hannibal2.skyhanni.utils.NEUItems.getRawCraftCostOrNull
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.NumberUtil.shortFormat
import at.hannibal2.skyhanni.utils.PrimitiveIngredient
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getAbilityScrolls
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getArmorDye
Expand Down Expand Up @@ -58,7 +58,6 @@ import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.hasJalapenoBook
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.hasWoodSingularity
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.isRecombobulated
import at.hannibal2.skyhanni.utils.StringUtils.allLettersFirstUppercase
import io.github.moulberry.notenoughupdates.recipes.Ingredient
import io.github.notenoughupdates.moulconfig.observer.Property
import net.minecraft.item.ItemStack
import java.util.Locale
Expand Down Expand Up @@ -889,12 +888,12 @@ object EstimatedItemValueCalculator {

val previousTotal = totalPrice
for (ingredients in slot.value) {
val ingredient = Ingredient(NEUItems.manager, ingredients)
val ingredient = PrimitiveIngredient(ingredients)

totalPrice += if (ingredient.isCoins) {
totalPrice += if (ingredient.isCoin()) {
ingredient.count
} else {
ingredient.internalItemId.asInternalName().getPrice() * ingredient.count
ingredient.internalName.getPrice() * ingredient.count
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ object SkyHanniDebugsAndTests {
}

@SubscribeEvent
fun onSHowCraftPrice(event: LorenzToolTipEvent) {
fun onShowCraftPrice(event: LorenzToolTipEvent) {
if (!LorenzUtils.inSkyBlock) return
if (!debugConfig.showCraftPrice) return
val price = event.itemStack.getInternalNameOrNull()?.getRawCraftCostOrNull() ?: return
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/at/hannibal2/skyhanni/utils/ItemPriceUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName
import at.hannibal2.skyhanni.utils.NEUItems.getItemStackOrNull
import at.hannibal2.skyhanni.utils.NEUItems.getRecipes
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import io.github.moulberry.notenoughupdates.recipes.NeuRecipe

object ItemPriceUtils {

fun NEUInternalName.getPrice(
priceSource: ItemPriceSource = ItemPriceSource.BAZAAR_INSTANT_BUY,
pastRecipes: List<NeuRecipe> = emptyList(),
pastRecipes: List<PrimitiveRecipe> = emptyList(),
) = getPriceOrNull(priceSource, pastRecipes) ?: 0.0

fun NEUInternalName.getPriceOrNull(
priceSource: ItemPriceSource = ItemPriceSource.BAZAAR_INSTANT_BUY,
pastRecipes: List<NeuRecipe> = emptyList(),
pastRecipes: List<PrimitiveRecipe> = emptyList(),
): Double? {
when (this) {
NEUInternalName.JASPER_CRYSTAL -> return 0.0
Expand Down Expand Up @@ -60,7 +59,7 @@ object ItemPriceUtils {
// NEUItems.manager.auctionManager.getCraftCost(asString())?.craftCost
fun NEUInternalName.getRawCraftCostOrNull(
priceSource: ItemPriceSource = ItemPriceSource.BAZAAR_INSTANT_BUY,
pastRecipes: List<NeuRecipe> = emptyList(),
pastRecipes: List<PrimitiveRecipe> = emptyList(),
): Double? = getRecipes(this).filter { it !in pastRecipes }
.map { it.getRecipePrice(priceSource, pastRecipes + it) }
.filter { it >= 0 }
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.CollectionUtils.addOrPut
import at.hannibal2.skyhanni.utils.ItemPriceUtils.getPrice
import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName
import at.hannibal2.skyhanni.utils.NEUItems.getItemStackOrNull
import at.hannibal2.skyhanni.utils.NumberUtil.formatInt
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
Expand All @@ -20,7 +19,6 @@ import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.StringUtils.removeResets
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import com.google.common.collect.Lists
import io.github.moulberry.notenoughupdates.recipes.NeuRecipe
import io.github.moulberry.notenoughupdates.util.NotificationHandler
import net.minecraft.client.Minecraft
import net.minecraft.init.Items
Expand Down Expand Up @@ -463,19 +461,19 @@ object ItemUtils {
return list
}

fun neededItems(recipe: NeuRecipe): Map<NEUInternalName, Int> {
fun neededItems(recipe: PrimitiveRecipe): Map<NEUInternalName, Int> {
val neededItems = mutableMapOf<NEUInternalName, Int>()
for (ingredient in recipe.ingredients) {
val material = ingredient.internalItemId.asInternalName()
val material = ingredient.internalName
val amount = ingredient.count.toInt()
neededItems.addOrPut(material, amount)
}
return neededItems
}

fun NeuRecipe.getRecipePrice(
fun PrimitiveRecipe.getRecipePrice(
priceSource: ItemPriceSource = ItemPriceSource.BAZAAR_INSTANT_BUY,
pastRecipes: List<NeuRecipe> = emptyList(),
pastRecipes: List<PrimitiveRecipe> = emptyList(),
): Double = neededItems(this).map {
it.key.getPrice(priceSource, pastRecipes) * it.value
}.sum()
Expand Down
25 changes: 10 additions & 15 deletions src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ import io.github.moulberry.notenoughupdates.NotEnoughUpdates
import io.github.moulberry.notenoughupdates.events.ProfileDataLoadedEvent
import io.github.moulberry.notenoughupdates.overlays.AuctionSearchOverlay
import io.github.moulberry.notenoughupdates.overlays.BazaarSearchOverlay
import io.github.moulberry.notenoughupdates.recipes.CraftingRecipe
import io.github.moulberry.notenoughupdates.recipes.Ingredient
import io.github.moulberry.notenoughupdates.recipes.NeuRecipe
import io.github.moulberry.notenoughupdates.util.ItemResolutionQuery
import io.github.moulberry.notenoughupdates.util.Utils
import net.minecraft.client.Minecraft
Expand All @@ -57,8 +54,8 @@ object NEUItems {

val manager: NEUManager get() = NotEnoughUpdates.INSTANCE.manager
private val multiplierCache = mutableMapOf<NEUInternalName, PrimitiveItemStack>()
private val recipesCache = mutableMapOf<NEUInternalName, Set<NeuRecipe>>()
private val ingredientsCache = mutableMapOf<NeuRecipe, Set<Ingredient>>()
private val recipesCache = mutableMapOf<NEUInternalName, Set<PrimitiveRecipe>>()
private val ingredientsCache = mutableMapOf<PrimitiveRecipe, Set<PrimitiveIngredient>>()
private val itemIdCache = mutableMapOf<Item, List<NEUInternalName>>()

private val hypixelApiGson by lazy {
Expand Down Expand Up @@ -183,7 +180,7 @@ object NEUItems {
@Deprecated("Moved to ItemPriceUtils", ReplaceWith(""))
fun NEUInternalName.getPrice(
priceSource: ItemPriceSource = ItemPriceSource.BAZAAR_INSTANT_BUY,
pastRecipes: List<NeuRecipe> = emptyList(),
pastRecipes: List<PrimitiveRecipe> = emptyList(),
): Double = getPriceNew(priceSource, pastRecipes)

@Deprecated("Moved to ItemPriceUtils", ReplaceWith(""))
Expand All @@ -198,11 +195,11 @@ object NEUItems {
@Deprecated("Moved to ItemPriceUtils", ReplaceWith(""))
fun NEUInternalName.getPriceOrNull(
priceSource: ItemPriceSource = ItemPriceSource.BAZAAR_INSTANT_BUY,
pastRecipes: List<NeuRecipe> = emptyList(),
pastRecipes: List<PrimitiveRecipe> = emptyList(),
): Double? = this.getPriceOrNullNew(priceSource, pastRecipes)

@Deprecated("Moved to ItemPriceUtils", ReplaceWith(""))
fun NEUInternalName.getRawCraftCostOrNull(pastRecipes: List<NeuRecipe> = emptyList()): Double? =
fun NEUInternalName.getRawCraftCostOrNull(pastRecipes: List<PrimitiveRecipe> = emptyList()): Double? =
getRawCraftCostOrNullNew(ItemPriceSource.BAZAAR_INSTANT_BUY, pastRecipes)

fun NEUInternalName.getItemStackOrNull(): ItemStack? = ItemResolutionQuery(manager)
Expand Down Expand Up @@ -309,12 +306,12 @@ object NEUItems {
return internalName.makePrimitiveStack()
}
for (recipe in getRecipes(internalName)) {
if (recipe !is CraftingRecipe) continue
if (!recipe.isCraftingRecipe()) continue

val map = mutableMapOf<NEUInternalName, Int>()
for (ingredient in recipe.getCachedIngredients()) {
val count = ingredient.count.toInt()
var internalItemId = ingredient.internalItemId.asInternalName()
var internalItemId = ingredient.internalName
// ignore cactus green
if (internalName == "ENCHANTED_CACTUS_GREEN".asInternalName() && internalItemId == "INK_SACK-2".asInternalName()) {
internalItemId = "CACTUS".asInternalName()
Expand Down Expand Up @@ -356,13 +353,13 @@ object NEUItems {
return result
}

fun getRecipes(internalName: NEUInternalName): Set<NeuRecipe> {
fun getRecipes(internalName: NEUInternalName): Set<PrimitiveRecipe> {
return recipesCache.getOrPut(internalName) {
manager.getRecipesFor(internalName.asString())
PrimitiveRecipe.convertMultiple(manager.getRecipesFor(internalName.asString())).toSet()
}
}

fun NeuRecipe.getCachedIngredients() = ingredientsCache.getOrPut(this) { allIngredients() }
fun PrimitiveRecipe.getCachedIngredients() = ingredientsCache.getOrPut(this) { ingredients }

fun neuHasFocus(): Boolean {
if (AuctionSearchOverlay.shouldReplace()) return true
Expand Down Expand Up @@ -391,6 +388,4 @@ object NEUItems {
val jsonObject = ConfigManager.gson.fromJson(jsonString, JsonObject::class.java)
return manager.jsonToStack(jsonObject, false)
}

fun NeuRecipe.allIngredients(): Set<Ingredient> = ingredients
}
Loading
Loading