aboutsummaryrefslogtreecommitdiff
path: root/common/src/main/kotlin/moe/nea/notenoughupdates/rei/NEUReiPlugin.kt
blob: 0fb36216ebfb1ee9a2055f8e9a746f4dc8486dab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package moe.nea.notenoughupdates.rei

import com.mojang.blaze3d.vertex.PoseStack
import com.mojang.serialization.Dynamic
import io.github.moulberry.repo.data.NEUItem
import me.shedaniel.math.Point
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.plugins.REIClientPlugin
import me.shedaniel.rei.api.client.registry.entry.EntryRegistry
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.EntryTypeRegistry
import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes
import me.shedaniel.rei.api.common.util.EntryStacks
import moe.nea.notenoughupdates.LegacyTagParser
import moe.nea.notenoughupdates.NotEnoughUpdates.neuRepo
import net.minecraft.ChatFormatting
import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.NbtOps
import net.minecraft.nbt.StringTag
import net.minecraft.network.chat.Component
import net.minecraft.network.chat.TextComponent
import net.minecraft.resources.ResourceLocation
import net.minecraft.tags.TagKey
import net.minecraft.util.datafix.DataFixers.getDataFixer
import net.minecraft.util.datafix.fixes.References
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Items
import net.minecraft.world.item.enchantment.Enchantments
import java.util.concurrent.ConcurrentHashMap
import java.util.stream.Stream


class NEUReiPlugin : REIClientPlugin {

    companion object {

        fun EntryStack<NEUItem>.asItemEntry(): EntryStack<ItemStack> {
            return EntryStack.of(VanillaEntryTypes.ITEM, value.asItemStack())
        }

        fun ItemStack.appendLore(args: List<Component>) {
            val compoundTag = getOrCreateTagElement("display")
            val loreList = compoundTag.getList("Lore", StringTag.TAG_STRING.toInt())
            for (arg in args) {
                loreList.add(StringTag.valueOf(Component.Serializer.toJson(arg)))
            }
            compoundTag.put("Lore", loreList)
        }

        val cache: MutableMap<String, ItemStack> = ConcurrentHashMap()

        fun NEUItem.asItemStackNow(): ItemStack {
            val df = getDataFixer()
            val itemTag1_8_9 = CompoundTag()
            itemTag1_8_9.put("tag", LegacyTagParser.parse(this.nbttag))
            itemTag1_8_9.putString("id", this.minecraftItemId)
            itemTag1_8_9.putByte("Count", 1)
            itemTag1_8_9.putShort("Damage", this.damage.toShort())
            val itemTag_modern = try {
                df.update(
                    References.ITEM_STACK,
                    Dynamic(NbtOps.INSTANCE, itemTag1_8_9),
                    99,
                    2975
                ).value as CompoundTag
            } catch (e: Exception) {
                e.printStackTrace()
                return ItemStack(Items.PAINTING).apply {
                    appendLore(listOf(TextComponent("Exception rendering item: $skyblockItemId")))
                }
            }
            val itemInstance = ItemStack.of(itemTag_modern)
            return itemInstance.also {
                if(false)it.appendLore(
                    listOf(
                        TextComponent("Old: $minecraftItemId").withStyle {
                            it.withItalic(false).withColor(ChatFormatting.RED)
                        },
                        TextComponent("Modern: $itemTag_modern").withStyle {
                            it.withItalic(false).withColor(ChatFormatting.RED)
                        },
                    )
                )
                it.hoverName = TextComponent(this.skyblockItemId)
            }
        }

        fun NEUItem.asItemStack(): ItemStack {
            var s = cache[this.skyblockItemId]
            if (s == null) {
                s = asItemStackNow()
                cache[this.skyblockItemId] = s
            }
            return s
        }


        val hehe = ResourceLocation("notenoughupdates", "skyblockitems")
    }

    object SBItemEntryDefinition : EntryDefinition<NEUItem> {
        override fun equals(o1: NEUItem?, o2: NEUItem?, context: ComparisonContext?): Boolean {
            return o1 == o2
        }

        override fun getValueType(): Class<NEUItem> = NEUItem::class.java
        override fun getType(): EntryType<NEUItem> =
            EntryType.deferred(hehe)

        override fun getRenderer(): EntryRenderer<NEUItem> = object : EntryRenderer<NEUItem> {
            override fun render(
                entry: EntryStack<NEUItem>,
                matrices: PoseStack,
                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<NEUItem>, mouse: Point): Tooltip? {
                return VanillaEntryTypes.ITEM.definition.renderer
                    .getTooltip(entry.asItemEntry(), mouse)
            }

        }

        override fun getSerializer(): EntrySerializer<NEUItem>? {
            return null
        }

        override fun getTagsFor(entry: EntryStack<NEUItem>?, value: NEUItem?): Stream<out TagKey<*>> {
            return Stream.empty()
        }

        override fun asFormattedText(entry: EntryStack<NEUItem>, value: NEUItem): Component {
            return VanillaEntryTypes.ITEM.definition.asFormattedText(entry.asItemEntry(), value.asItemStack())
        }

        override fun hash(entry: EntryStack<NEUItem>, value: NEUItem, context: ComparisonContext): Long {
            return value.skyblockItemId.hashCode().toLong()
        }

        override fun wildcard(entry: EntryStack<NEUItem>, value: NEUItem): NEUItem {
            return value
        }

        override fun normalize(entry: EntryStack<NEUItem>, value: NEUItem): NEUItem {
            return value
        }

        override fun copy(entry: EntryStack<NEUItem>?, value: NEUItem): NEUItem {
            return value
        }

        override fun isEmpty(entry: EntryStack<NEUItem>?, value: NEUItem?): Boolean {
            return false
        }

        override fun getIdentifier(entry: EntryStack<NEUItem>?, value: NEUItem): ResourceLocation {
            return ResourceLocation("skyblockitem", value.skyblockItemId.lowercase().replace(";", "__"))
        }


    }

    override fun registerEntryTypes(registry: EntryTypeRegistry) {
        registry.register(hehe, SBItemEntryDefinition)
    }

    override fun registerEntries(registry: EntryRegistry) {
        neuRepo.items.items.values.forEach {
            registry.addEntry(EntryStack.of(SBItemEntryDefinition, it))
        }
        registry.addEntry(EntryStacks.of(ItemStack(Items.DIAMOND).also {
            it.enchant(Enchantments.ALL_DAMAGE_PROTECTION, 10)
        }))
    }
}