Skip to content
Open
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
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,13 +22,21 @@ open class ItemRecipesPage(val stack: ItemStack) : PagedGuidePage {
val pages: MutableList<Gui> = mutableListOf()

init {
val recipes = mutableListOf<RebarRecipe>()
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T : RebarRecipe>(key: NamespacedKey) : RecipeType<T>(key) {
Expand All @@ -13,7 +15,12 @@ abstract class ConfigurableRecipeType<T : RebarRecipe>(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}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<RebarRecipe> { 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<RebarRecipe> { it.priority }.thenBy { it.key })

if (multiOutputRecipes.isNotEmpty()) {
return multiOutputRecipes.first()
Expand All @@ -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<RebarRecipe> { 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<RebarRecipe> { it.priority }.thenBy { it.key })

if (multiOutputRecipes.isNotEmpty()) {
return multiOutputRecipes.first()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -42,4 +43,15 @@ interface RebarRecipe : Keyed {
}

fun display(): Gui?

companion object {
private val priorities = MapMaker().weakKeys().makeMap<RebarRecipe, Double>()

@JvmStatic
var RebarRecipe.priority: Double
get() = priorities.getOrDefault(this, 0.0)
set(value) {
priorities[this] = value
}
}
}
3 changes: 0 additions & 3 deletions rebar/src/main/resources/lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,6 @@ guide:
<guidearrow> <guideinsn>Right click</guideinsn> to see usages
<guidearrow> <guideinsn>Shift left click</guideinsn> to research
<guidearrow> <guideinsn>Shift right click</guideinsn> to see other research unlocks
admin: |-
<guidearrow> <guideinsn>Press Q</guideinsn> to cheat in one item <#ff0000>(admin only)</#ff0000>
<guidearrow> <guideinsn>Middle click</guideinsn> to cheat in a stack <#ff0000>(admin only)</#ff0000>
error: "<red>Item failed to load"
amount: "%name%: <gray>%amount%%breakdown%"
amount-breakdown: " (%stacks%x%stack-size% + %remainder%)"
Expand Down