diff --git a/rebar/src/main/kotlin/io/github/pylonmc/rebar/guide/button/ItemButton.kt b/rebar/src/main/kotlin/io/github/pylonmc/rebar/guide/button/ItemButton.kt index a5ea3aff9..630410c36 100644 --- a/rebar/src/main/kotlin/io/github/pylonmc/rebar/guide/button/ItemButton.kt +++ b/rebar/src/main/kotlin/io/github/pylonmc/rebar/guide/button/ItemButton.kt @@ -120,9 +120,6 @@ class ItemButton @JvmOverloads constructor( } else { builder.lore(Component.translatable("rebar.guide.button.item.hints.researched")) } - if (player.hasPermission("rebar.guide.cheat")) { - builder.lore(Component.translatable("rebar.guide.button.item.hints.admin")) - } } return builder diff --git a/rebar/src/main/kotlin/io/github/pylonmc/rebar/guide/pages/item/ItemRecipesPage.kt b/rebar/src/main/kotlin/io/github/pylonmc/rebar/guide/pages/item/ItemRecipesPage.kt index fe3da154b..62c57e253 100644 --- a/rebar/src/main/kotlin/io/github/pylonmc/rebar/guide/pages/item/ItemRecipesPage.kt +++ b/rebar/src/main/kotlin/io/github/pylonmc/rebar/guide/pages/item/ItemRecipesPage.kt @@ -3,6 +3,8 @@ package io.github.pylonmc.rebar.guide.pages.item import io.github.pylonmc.rebar.content.guide.RebarGuide import io.github.pylonmc.rebar.guide.pages.base.PagedGuidePage import io.github.pylonmc.rebar.recipe.FluidOrItem +import io.github.pylonmc.rebar.recipe.RebarRecipe +import io.github.pylonmc.rebar.recipe.RebarRecipe.Companion.priority import io.github.pylonmc.rebar.registry.RebarRegistry import io.github.pylonmc.rebar.util.gui.GuiItems import io.github.pylonmc.rebar.util.rebarKey @@ -20,13 +22,21 @@ open class ItemRecipesPage(val stack: ItemStack) : PagedGuidePage { val pages: MutableList = mutableListOf() init { + val recipes = mutableListOf() for (type in RebarRegistry.RECIPE_TYPES) { for (recipe in type.recipes) { if (!recipe.isHidden && recipe.isOutput(stack)) { - recipe.display()?.let { pages.add(it) } + recipes.add(recipe) } } } + recipes.sortByDescending { it.priority } + for (recipe in recipes) { + val display = recipe.display() + if (display != null) { + pages.add(display) + } + } } override fun getKey() = KEY diff --git a/rebar/src/main/kotlin/io/github/pylonmc/rebar/recipe/ConfigurableRecipeType.kt b/rebar/src/main/kotlin/io/github/pylonmc/rebar/recipe/ConfigurableRecipeType.kt index 964fe57e4..4a319ebe4 100644 --- a/rebar/src/main/kotlin/io/github/pylonmc/rebar/recipe/ConfigurableRecipeType.kt +++ b/rebar/src/main/kotlin/io/github/pylonmc/rebar/recipe/ConfigurableRecipeType.kt @@ -1,6 +1,8 @@ package io.github.pylonmc.rebar.recipe import io.github.pylonmc.rebar.config.ConfigSection +import io.github.pylonmc.rebar.config.adapter.ConfigAdapter +import io.github.pylonmc.rebar.recipe.RebarRecipe.Companion.priority import org.bukkit.NamespacedKey abstract class ConfigurableRecipeType(key: NamespacedKey) : RecipeType(key) { @@ -13,7 +15,12 @@ abstract class ConfigurableRecipeType(key: NamespacedKey) : Rec val section = config.getSectionOrThrow(key) val key = NamespacedKey.fromString(key) ?: error("Invalid key: $key") try { - addRecipe(loadRecipe(key, section)) + val recipe = loadRecipe(key, section) + val priority = section.get("priority", ConfigAdapter.DOUBLE) + if (priority != null) { + recipe.priority = priority + } + addRecipe(recipe) } catch (e: Exception) { throw IllegalArgumentException( "Failed to load recipe with key '$key' from config for recipe type ${this.key}", diff --git a/rebar/src/main/kotlin/io/github/pylonmc/rebar/recipe/IngredientCalculator.kt b/rebar/src/main/kotlin/io/github/pylonmc/rebar/recipe/IngredientCalculator.kt index af420db13..b9b013e69 100644 --- a/rebar/src/main/kotlin/io/github/pylonmc/rebar/recipe/IngredientCalculator.kt +++ b/rebar/src/main/kotlin/io/github/pylonmc/rebar/recipe/IngredientCalculator.kt @@ -3,6 +3,7 @@ package io.github.pylonmc.rebar.recipe import io.github.pylonmc.rebar.fluid.RebarFluid import io.github.pylonmc.rebar.item.ItemTypeWrapper import io.github.pylonmc.rebar.item.RebarItem +import io.github.pylonmc.rebar.recipe.RebarRecipe.Companion.priority import io.github.pylonmc.rebar.registry.RebarRegistry import org.bukkit.NamespacedKey import org.bukkit.inventory.ItemStack @@ -95,29 +96,23 @@ class IngredientCalculator private constructor() { } private fun findRecipeFor(item: RebarItem): RebarRecipe? { - // 1. if there's a recipe with the same key as the item, use that - RebarRegistry.RECIPE_TYPES - .map { it.getRecipe(item.schema.key) } - .find { it != null && it !in blacklistedRecipes } - ?.let { return it } - - // 2. if there's a recipe which produces *only* that item, use that - // 3. if there's multiple recipes which produce only that item, choose the *lowest* one lexographically + // 1. if there's a recipe which produces *only* that item, use that + // 2. if there's multiple recipes which produce only that item, choose the highest one by priority, then the *lowest* one lexographically val singleOutputRecipes = RebarRegistry.RECIPE_TYPES .flatMap { it.recipes } .filter { recipe -> recipe !in blacklistedRecipes && recipe.isOutput(item.stack) && recipe.results.size == 1 } - .sortedBy { it.key } + .sortedWith(compareByDescending { it.priority }.thenBy { it.key }) if (singleOutputRecipes.isNotEmpty()) { return singleOutputRecipes.first() } - // 4. if there's a recipe which produces the item *alongside* other things, use that - // 5. if there's multiple recipes which produce the item alongside other things, choose the *lowest* one lexographically + // 3. if there's a recipe which produces the item *alongside* other things, use that + // 4. if there's multiple recipes which produce the item alongside other things, choose the highest one by priority, then the *lowest* one lexographically val multiOutputRecipes = RebarRegistry.RECIPE_TYPES .flatMap { it.recipes } .filter { recipe -> recipe !in blacklistedRecipes && recipe.isOutput(item.stack) && recipe.results.size > 1 } - .sortedBy { it.key } + .sortedWith(compareByDescending { it.priority }.thenBy { it.key }) if (multiOutputRecipes.isNotEmpty()) { return multiOutputRecipes.first() @@ -127,29 +122,23 @@ class IngredientCalculator private constructor() { } private fun findRecipeFor(fluid: RebarFluid): RebarRecipe? { - // 1. if there's a recipe with the same key as the fluid, use that - RebarRegistry.RECIPE_TYPES - .map { it.getRecipe(fluid.key) } - .find { it != null && it !in blacklistedRecipes } - ?.let { return it } - - // 2. if there's a recipe which produces *only* that fluid, use that - // 3. if there's multiple recipes which produce only that fluid, choose the *lowest* one lexographically + // 1. if there's a recipe which produces *only* that fluid, use that + // 2. if there's multiple recipes which produce only that fluid, choose the highest one by priority, then the *lowest* one lexographically val singleOutputRecipes = RebarRegistry.RECIPE_TYPES .flatMap { it.recipes } .filter { recipe -> recipe !in blacklistedRecipes && recipe.isOutput(fluid) && recipe.results.size == 1 } - .sortedBy { it.key } + .sortedWith(compareByDescending { it.priority }.thenBy { it.key }) if (singleOutputRecipes.isNotEmpty()) { return singleOutputRecipes.first() } - // 4. if there's a recipe which produces the fluid *alongside* other things, use that - // 5. if there's multiple recipes which produce the fluid alongside other things, choose the *lowest* one lexographically + // 3. if there's a recipe which produces the fluid *alongside* other things, use that + // 4. if there's multiple recipes which produce the fluid alongside other things, choose the highest one by priority, then the *lowest* one lexographically val multiOutputRecipes = RebarRegistry.RECIPE_TYPES .flatMap { it.recipes } .filter { recipe -> recipe !in blacklistedRecipes && recipe.isOutput(fluid) && recipe.results.size > 1 } - .sortedBy { it.key } + .sortedWith(compareByDescending { it.priority }.thenBy { it.key }) if (multiOutputRecipes.isNotEmpty()) { return multiOutputRecipes.first() diff --git a/rebar/src/main/kotlin/io/github/pylonmc/rebar/recipe/RebarRecipe.kt b/rebar/src/main/kotlin/io/github/pylonmc/rebar/recipe/RebarRecipe.kt index 5bc74fe9d..ca52855c0 100644 --- a/rebar/src/main/kotlin/io/github/pylonmc/rebar/recipe/RebarRecipe.kt +++ b/rebar/src/main/kotlin/io/github/pylonmc/rebar/recipe/RebarRecipe.kt @@ -1,5 +1,6 @@ package io.github.pylonmc.rebar.recipe +import com.google.common.collect.MapMaker import io.github.pylonmc.rebar.fluid.RebarFluid import org.bukkit.Keyed import org.bukkit.inventory.ItemStack @@ -42,4 +43,15 @@ interface RebarRecipe : Keyed { } fun display(): Gui? + + companion object { + private val priorities = MapMaker().weakKeys().makeMap() + + @JvmStatic + var RebarRecipe.priority: Double + get() = priorities.getOrDefault(this, 0.0) + set(value) { + priorities[this] = value + } + } } diff --git a/rebar/src/main/resources/lang/en.yml b/rebar/src/main/resources/lang/en.yml index a2929270a..8bef2ad65 100644 --- a/rebar/src/main/resources/lang/en.yml +++ b/rebar/src/main/resources/lang/en.yml @@ -181,9 +181,6 @@ guide: Right click to see usages Shift left click to research Shift right click to see other research unlocks - admin: |- - Press Q to cheat in one item <#ff0000>(admin only) - Middle click to cheat in a stack <#ff0000>(admin only) error: "Item failed to load" amount: "%name%: %amount%%breakdown%" amount-breakdown: " (%stacks%x%stack-size% + %remainder%)"