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

SmithingTransformRecipe inproper itemmeta handling #53

Merged
merged 1 commit into from
Jan 18, 2024
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
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() }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of moving here vs in install?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mostly because before we would need to re-register to get all custom recipes
mostly forgot to remove it from some testing tbh


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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this part necessary? the toItemStack should override as needed and keep the displayName if the ignored property is set. Is this maybe covering some case I'm not considering?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newResult is applied ontop of oldResult
this is because otherwise the finalResult will have the prefabs from oldResult aka Diamond Sickle
this means however that the finalResult will have the displayname of oldResult aka Diamond Sickle, or if renamed the renamed

so it does indeed require to fix that afterwards from my testing to get correct behaviour

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
}
}