diff options
author | nea <romangraef@gmail.com> | 2022-07-26 21:28:10 +0200 |
---|---|---|
committer | nea <romangraef@gmail.com> | 2022-07-26 21:28:10 +0200 |
commit | cc9c23914b8a928026a457b259e44bc4bb2fe655 (patch) | |
tree | 55744f66b2ccadc5ff12de60c0679b845d7e85b4 | |
parent | 3aa6b486d80654f4c3ca284f665d1293555adb72 (diff) | |
download | firmament-cc9c23914b8a928026a457b259e44bc4bb2fe655.tar.gz firmament-cc9c23914b8a928026a457b259e44bc4bb2fe655.tar.bz2 firmament-cc9c23914b8a928026a457b259e44bc4bb2fe655.zip |
refactoring
-rw-r--r-- | common/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt | 2 | ||||
-rw-r--r-- | common/src/main/kotlin/moe/nea/notenoughupdates/rei/NEUReiPlugin.kt | 158 | ||||
-rw-r--r-- | common/src/main/kotlin/moe/nea/notenoughupdates/rei/SBItemEntryDefinition.kt | 97 | ||||
-rw-r--r-- | common/src/main/kotlin/moe/nea/notenoughupdates/repo/ItemCache.kt | 76 | ||||
-rw-r--r-- | common/src/main/kotlin/moe/nea/notenoughupdates/util/ItemUtil.kt | 23 | ||||
-rw-r--r-- | common/src/main/kotlin/moe/nea/notenoughupdates/util/LegacyTagParser.kt (renamed from common/src/main/kotlin/moe/nea/notenoughupdates/LegacyTagParser.kt) | 2 |
6 files changed, 200 insertions, 158 deletions
diff --git a/common/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt b/common/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt index 615d25e..b55c5d8 100644 --- a/common/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt +++ b/common/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt @@ -15,8 +15,6 @@ object NotEnoughUpdates { } - - fun init() { } diff --git a/common/src/main/kotlin/moe/nea/notenoughupdates/rei/NEUReiPlugin.kt b/common/src/main/kotlin/moe/nea/notenoughupdates/rei/NEUReiPlugin.kt index ccb9b0b..6d960e3 100644 --- a/common/src/main/kotlin/moe/nea/notenoughupdates/rei/NEUReiPlugin.kt +++ b/common/src/main/kotlin/moe/nea/notenoughupdates/rei/NEUReiPlugin.kt @@ -1,37 +1,15 @@ 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 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 moe.nea.notenoughupdates.repo.ItemCache.asItemStack 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 java.util.concurrent.ConcurrentHashMap -import java.util.stream.Stream class NEUReiPlugin : REIClientPlugin { @@ -42,142 +20,12 @@ class NEUReiPlugin : REIClientPlugin { 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) - }, - ) - ) - } - } - - 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 cheatsAs(entry: EntryStack<NEUItem>?, value: NEUItem?): ItemStack? { - return value?.asItemStack() - } - - 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(";", "__")) - } - + val SKYBLOCK_ITEM_TYPE_ID = ResourceLocation("notenoughupdates", "skyblockitems") } override fun registerEntryTypes(registry: EntryTypeRegistry) { - registry.register(hehe, SBItemEntryDefinition) + registry.register(SKYBLOCK_ITEM_TYPE_ID, SBItemEntryDefinition) } override fun registerEntries(registry: EntryRegistry) { diff --git a/common/src/main/kotlin/moe/nea/notenoughupdates/rei/SBItemEntryDefinition.kt b/common/src/main/kotlin/moe/nea/notenoughupdates/rei/SBItemEntryDefinition.kt new file mode 100644 index 0000000..6612113 --- /dev/null +++ b/common/src/main/kotlin/moe/nea/notenoughupdates/rei/SBItemEntryDefinition.kt @@ -0,0 +1,97 @@ +package moe.nea.notenoughupdates.rei + +import com.mojang.blaze3d.vertex.PoseStack +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.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 moe.nea.notenoughupdates.rei.NEUReiPlugin.Companion.asItemEntry +import moe.nea.notenoughupdates.repo.ItemCache.asItemStack +import moe.nea.notenoughupdates.repo.ItemCache.getResourceLocation +import net.minecraft.network.chat.Component +import net.minecraft.resources.ResourceLocation +import net.minecraft.tags.TagKey +import net.minecraft.world.item.ItemStack +import java.util.stream.Stream + +object SBItemEntryDefinition : EntryDefinition<NEUItem> { + override fun equals(o1: NEUItem?, o2: NEUItem?, context: ComparisonContext?): Boolean { + return o1 == o2 + } + + override fun cheatsAs(entry: EntryStack<NEUItem>?, value: NEUItem?): ItemStack? { + return value?.asItemStack() + } + + override fun getValueType(): Class<NEUItem> = NEUItem::class.java + override fun getType(): EntryType<NEUItem> = + EntryType.deferred(NEUReiPlugin.SKYBLOCK_ITEM_TYPE_ID) + + 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 value.getResourceLocation() + } + + +} diff --git a/common/src/main/kotlin/moe/nea/notenoughupdates/repo/ItemCache.kt b/common/src/main/kotlin/moe/nea/notenoughupdates/repo/ItemCache.kt new file mode 100644 index 0000000..8b3939e --- /dev/null +++ b/common/src/main/kotlin/moe/nea/notenoughupdates/repo/ItemCache.kt @@ -0,0 +1,76 @@ +package moe.nea.notenoughupdates.repo + +import com.mojang.serialization.Dynamic +import io.github.moulberry.repo.IReloadable +import io.github.moulberry.repo.NEURepository +import io.github.moulberry.repo.data.NEUItem +import moe.nea.notenoughupdates.util.LegacyTagParser +import moe.nea.notenoughupdates.util.appendLore +import net.minecraft.nbt.CompoundTag +import net.minecraft.nbt.NbtOps +import net.minecraft.network.chat.TextComponent +import net.minecraft.resources.ResourceLocation +import net.minecraft.util.datafix.DataFixers +import net.minecraft.util.datafix.fixes.References +import net.minecraft.world.item.ItemStack +import net.minecraft.world.item.Items +import java.util.concurrent.ConcurrentHashMap + +object ItemCache : IReloadable { + val cache: MutableMap<String, ItemStack> = ConcurrentHashMap() + val df = DataFixers.getDataFixer() + var isFlawless = true + + private fun NEUItem.get10809CompoundTag(): CompoundTag = CompoundTag().apply { + put("tag", LegacyTagParser.parse(nbttag)) + putString("id", minecraftItemId) + putByte("Count", 1) + putShort("Damage", damage.toShort()) + } + + private fun CompoundTag.transformFrom10809ToModern(): CompoundTag? = + try { + df.update( + References.ITEM_STACK, + Dynamic(NbtOps.INSTANCE, this), + -1, + 2975 + ).value as CompoundTag + } catch (e: Exception) { + e.printStackTrace() + isFlawless = false + null + } + + private fun NEUItem.asItemStackNow(): ItemStack { + val oldItemTag = get10809CompoundTag() + val modernItemTag = oldItemTag.transformFrom10809ToModern() + ?: return ItemStack(Items.PAINTING).apply { + setHoverName(TextComponent(this@asItemStackNow.displayName)) + appendLore(listOf(TextComponent("Exception rendering item: $skyblockItemId"))) + } + val itemInstance = ItemStack.of(modernItemTag) + if (itemInstance.tag?.contains("Enchantments") == true) { + itemInstance.enchantmentTags.add(CompoundTag()) + } + return itemInstance + } + + fun NEUItem.asItemStack(): ItemStack { + var s = cache[this.skyblockItemId] + if (s == null) { + s = asItemStackNow() + cache[this.skyblockItemId] = s + } + return s + } + + fun NEUItem.getResourceLocation() = + ResourceLocation("skyblockitem", skyblockItemId.lowercase().replace(";", "__")) + + + + override fun reload(repository: NEURepository) { + cache.clear() + } +} diff --git a/common/src/main/kotlin/moe/nea/notenoughupdates/util/ItemUtil.kt b/common/src/main/kotlin/moe/nea/notenoughupdates/util/ItemUtil.kt new file mode 100644 index 0000000..d5b8881 --- /dev/null +++ b/common/src/main/kotlin/moe/nea/notenoughupdates/util/ItemUtil.kt @@ -0,0 +1,23 @@ +package moe.nea.notenoughupdates.util + +import net.minecraft.nbt.CompoundTag +import net.minecraft.nbt.ListTag +import net.minecraft.nbt.StringTag +import net.minecraft.network.chat.Component +import net.minecraft.world.item.ItemStack + +fun ItemStack.appendLore(args: List<Component>) { + val compoundTag = getOrCreateTagElement("display") + val loreList = compoundTag.getOrCreateList("Lore", StringTag.TAG_STRING) + for (arg in args) { + loreList.add(StringTag.valueOf(Component.Serializer.toJson(arg))) + } +} + +fun CompoundTag.getOrCreateList(label: String, tag: Byte): ListTag = getList(label, tag.toInt()).also { + put(label, it) +} + +fun CompoundTag.getOrCreateCompoundTag(label: String): CompoundTag = getCompound(label).also { + put(label, it) +} diff --git a/common/src/main/kotlin/moe/nea/notenoughupdates/LegacyTagParser.kt b/common/src/main/kotlin/moe/nea/notenoughupdates/util/LegacyTagParser.kt index 809ea4a..a4ec7e1 100644 --- a/common/src/main/kotlin/moe/nea/notenoughupdates/LegacyTagParser.kt +++ b/common/src/main/kotlin/moe/nea/notenoughupdates/util/LegacyTagParser.kt @@ -1,4 +1,4 @@ -package moe.nea.notenoughupdates +package moe.nea.notenoughupdates.util import net.minecraft.nbt.* import java.util.* |