Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
fix: properly handle custom smithing-transform recipes (durability/di…
Browse files Browse the repository at this point in the history
…splayName/enchantments/...) (#53)
  • Loading branch information
Boy0000 authored Jan 18, 2024
1 parent c82af32 commit 002fe78
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.mineinabyss.geary.prefabs.PrefabKey
import com.mineinabyss.geary.systems.accessors.Pointer
import com.mineinabyss.geary.systems.query.GearyQuery
import com.mineinabyss.idofront.recipes.register
import com.mineinabyss.idofront.serialization.recipes.SerializableRecipeIngredients
import com.mineinabyss.looty.config.looty
import org.bukkit.Bukkit
import org.bukkit.NamespacedKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ interface ItemRecipes {
val query: ItemRecipeQuery

companion object : GearyAddonWithDefault<ItemRecipes> {
val recipes by lazy { default().query.registerRecipes() }

override fun default() = object : ItemRecipes {
override val query = ItemRecipeQuery()
}

override fun ItemRecipes.install() {
geary.pipeline.runOnOrAfter(GearyPhase.ENABLE) {
val autoDiscoveredRecipes = query.registerRecipes()
val autoDiscoveredRecipes = recipes

gearyPaper.plugin.listeners(
RecipeDiscoverySystem(autoDiscoveredRecipes),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
package com.mineinabyss.looty.features.recipes

import com.mineinabyss.geary.papermc.datastore.decodePrefabs
import com.mineinabyss.geary.papermc.datastore.*
import com.mineinabyss.geary.papermc.tracking.items.components.SetItemIgnoredProperties
import com.mineinabyss.geary.papermc.tracking.items.migration.SetItemIgnoredPropertyListener
import com.mineinabyss.geary.prefabs.PrefabKey
import com.mineinabyss.idofront.items.editItemMeta
import com.mineinabyss.idofront.messaging.broadcast
import com.mineinabyss.idofront.messaging.broadcastVal
import com.mineinabyss.idofront.nms.nbt.fastPDC
import com.mineinabyss.idofront.serialization.SerializableItemStack
import com.mineinabyss.idofront.serialization.toSerializable
import org.bukkit.Bukkit
import org.bukkit.Keyed
import org.bukkit.Material
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
import org.bukkit.event.inventory.CraftItemEvent
import org.bukkit.event.inventory.PrepareAnvilEvent
import org.bukkit.event.inventory.PrepareItemCraftEvent
import org.bukkit.event.inventory.PrepareSmithingEvent
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.ShapedRecipe
import org.bukkit.inventory.ShapelessRecipe
import org.bukkit.inventory.SmithingTransformRecipe
import org.bukkit.inventory.meta.Damageable
import java.util.*

class RecipeCraftingSystem : Listener {
/**
Expand All @@ -27,4 +47,34 @@ class RecipeCraftingSystem : Listener {
inventory.result = null
}
}

@EventHandler
fun PrepareSmithingEvent.onCustomSmithingTransform() {
// Smithing will cache the last recipe, so even with 0 input
// recipe will return as not null if say a Diamond Hoe was put in before
if (inventory.contents.any { it?.isEmpty != false }) return
// Return if no item is custom, as then vanilla should handle it fine
if (inventory.contents.none { it?.fastPDC?.hasComponentsEncoded == true }) return

val (template, mineral) = (inventory.inputTemplate ?: return) to (inventory.inputMineral ?: return)
val equipment = inventory.inputEquipment ?: return

val inputGearyEntity = equipment.fastPDC?.decodePrefabs()?.firstOrNull() ?: return
val smithingTransformRecipes = Bukkit.recipeIterator().asSequence().filter { (it as? SmithingTransformRecipe)?.result?.fastPDC?.hasComponentsEncoded == true }.filterIsInstance<SmithingTransformRecipe>()
val customRecipeResult = smithingTransformRecipes.filter { it.template.test(template) && it.addition.test(mineral) && it.base.itemStack.itemMeta?.persistentDataContainer?.decodePrefabs()?.firstOrNull() == inputGearyEntity }.firstOrNull()?.result
var recipeResultItem = (customRecipeResult ?: ItemStack.empty()).let { result?.toSerializable()?.toItemStack(it, EnumSet.of(SerializableItemStack.Properties.DISPLAY_NAME)) }

recipeResultItem = recipeResultItem?.editItemMeta {
displayName(
equipment.fastPDC?.decode<SetItemIgnoredProperties>()?.let { properties ->
persistentDataContainer.encode(properties)
if (SerializableItemStack.Properties.DISPLAY_NAME in properties.ignore && result?.itemMeta?.hasDisplayName() == true)
result?.itemMeta?.displayName()?.compact()
else displayName()?.compact()
} ?: displayName()?.compact()
)
}

result = recipeResultItem
}
}

0 comments on commit 002fe78

Please sign in to comment.