package moe.nea.notenoughupdates.rei import io.github.moulberry.repo.data.NEUItem import java.util.stream.Stream import me.shedaniel.math.Rectangle import me.shedaniel.rei.api.client.entry.renderer.EntryRenderer import me.shedaniel.rei.api.client.gui.widgets.Tooltip import me.shedaniel.rei.api.client.gui.widgets.TooltipContext import me.shedaniel.rei.api.common.entry.EntrySerializer import me.shedaniel.rei.api.common.entry.EntryStack import me.shedaniel.rei.api.common.entry.comparison.ComparisonContext import me.shedaniel.rei.api.common.entry.type.EntryDefinition import me.shedaniel.rei.api.common.entry.type.EntryType import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes import net.minecraft.client.util.math.MatrixStack import net.minecraft.item.ItemStack import net.minecraft.nbt.NbtCompound import net.minecraft.registry.tag.TagKey import net.minecraft.text.Text import net.minecraft.util.Identifier import moe.nea.notenoughupdates.rei.NEUReiPlugin.Companion.asItemEntry import moe.nea.notenoughupdates.repo.ItemCache.asItemStack import moe.nea.notenoughupdates.repo.ItemCache.getIdentifier import moe.nea.notenoughupdates.repo.RepoManager import moe.nea.notenoughupdates.util.SkyblockId object SBItemEntryDefinition : EntryDefinition { override fun equals(o1: NEUItem?, o2: NEUItem?, context: ComparisonContext?): Boolean { return o1 === o2 } override fun cheatsAs(entry: EntryStack?, value: NEUItem?): ItemStack? { return value?.asItemStack() } override fun getValueType(): Class = NEUItem::class.java override fun getType(): EntryType = EntryType.deferred(NEUReiPlugin.SKYBLOCK_ITEM_TYPE_ID) override fun getRenderer(): EntryRenderer = object : EntryRenderer { override fun render( entry: EntryStack, matrices: MatrixStack, bounds: Rectangle, mouseX: Int, mouseY: Int, delta: Float ) { VanillaEntryTypes.ITEM.definition.renderer .render( entry.asItemEntry(), matrices, bounds, mouseX, mouseY, delta ) } override fun getTooltip(entry: EntryStack, tooltipContext: TooltipContext): Tooltip? { return VanillaEntryTypes.ITEM.definition.renderer .getTooltip(entry.asItemEntry(), tooltipContext) } } override fun getSerializer(): EntrySerializer { return object : EntrySerializer { override fun supportSaving(): Boolean = true override fun supportReading(): Boolean = true override fun read(tag: NbtCompound): NEUItem? { return RepoManager.getNEUItem(SkyblockId(tag.getString("SKYBLOCK_ID"))) } override fun save(entry: EntryStack, value: NEUItem?): NbtCompound { return NbtCompound().apply { putString("SKYBLOCK_ID", value?.skyblockItemId ?: "null") } } } } override fun getTagsFor(entry: EntryStack?, value: NEUItem?): Stream>? { return Stream.empty() } override fun asFormattedText(entry: EntryStack, value: NEUItem): Text { return VanillaEntryTypes.ITEM.definition.asFormattedText(entry.asItemEntry(), value.asItemStack()) } override fun hash(entry: EntryStack, value: NEUItem?, context: ComparisonContext): Long { // Repo items are immutable, and get replaced entirely when loaded from disk return System.identityHashCode(value) * 31L } override fun wildcard(entry: EntryStack?, value: NEUItem?): NEUItem? { return value } override fun normalize(entry: EntryStack?, value: NEUItem?): NEUItem? { return value } override fun copy(entry: EntryStack?, value: NEUItem?): NEUItem? { return value } override fun isEmpty(entry: EntryStack?, value: NEUItem?): Boolean { return false } override fun getIdentifier(entry: EntryStack?, value: NEUItem?): Identifier { return value?.getIdentifier() ?: Identifier.of("skyblockitem", "null")!! } }