Add lore copy keybind

This commit is contained in:
Linnea Gräf
2024-10-07 14:42:50 +02:00
parent 67dd2f68d6
commit 709f5d59c5
2 changed files with 161 additions and 150 deletions

View File

@@ -1,21 +1,20 @@
package moe.nea.firmament.features.debug package moe.nea.firmament.features.debug
import com.mojang.serialization.JsonOps
import kotlin.jvm.optionals.getOrNull
import net.minecraft.block.SkullBlock import net.minecraft.block.SkullBlock
import net.minecraft.block.entity.SkullBlockEntity import net.minecraft.block.entity.SkullBlockEntity
import net.minecraft.client.gui.screen.Screen
import net.minecraft.component.DataComponentTypes import net.minecraft.component.DataComponentTypes
import net.minecraft.entity.Entity import net.minecraft.entity.Entity
import net.minecraft.entity.LivingEntity import net.minecraft.entity.LivingEntity
import net.minecraft.item.ItemStack import net.minecraft.item.ItemStack
import net.minecraft.item.Items import net.minecraft.item.Items
import net.minecraft.text.Text import net.minecraft.text.Text
import net.minecraft.text.TextCodecs
import net.minecraft.util.hit.BlockHitResult import net.minecraft.util.hit.BlockHitResult
import net.minecraft.util.hit.EntityHitResult import net.minecraft.util.hit.EntityHitResult
import net.minecraft.util.hit.HitResult import net.minecraft.util.hit.HitResult
import moe.nea.firmament.annotations.Subscribe import moe.nea.firmament.annotations.Subscribe
import moe.nea.firmament.events.CommandEvent
import moe.nea.firmament.events.CustomItemModelEvent import moe.nea.firmament.events.CustomItemModelEvent
import moe.nea.firmament.events.HandledScreenKeyPressedEvent import moe.nea.firmament.events.HandledScreenKeyPressedEvent
import moe.nea.firmament.events.ItemTooltipEvent import moe.nea.firmament.events.ItemTooltipEvent
@@ -29,167 +28,177 @@ import moe.nea.firmament.mixins.accessor.AccessorHandledScreen
import moe.nea.firmament.util.ClipboardUtils import moe.nea.firmament.util.ClipboardUtils
import moe.nea.firmament.util.MC import moe.nea.firmament.util.MC
import moe.nea.firmament.util.focusedItemStack import moe.nea.firmament.util.focusedItemStack
import moe.nea.firmament.util.mc.displayNameAccordingToNbt
import moe.nea.firmament.util.mc.loreAccordingToNbt
import moe.nea.firmament.util.skyBlockId import moe.nea.firmament.util.skyBlockId
object PowerUserTools : FirmamentFeature { object PowerUserTools : FirmamentFeature {
override val identifier: String override val identifier: String
get() = "power-user" get() = "power-user"
object TConfig : ManagedConfig(identifier) { object TConfig : ManagedConfig(identifier) {
val showItemIds by toggle("show-item-id") { false } val showItemIds by toggle("show-item-id") { false }
val copyItemId by keyBindingWithDefaultUnbound("copy-item-id") val copyItemId by keyBindingWithDefaultUnbound("copy-item-id")
val copyTexturePackId by keyBindingWithDefaultUnbound("copy-texture-pack-id") val copyTexturePackId by keyBindingWithDefaultUnbound("copy-texture-pack-id")
val copyNbtData by keyBindingWithDefaultUnbound("copy-nbt-data") val copyNbtData by keyBindingWithDefaultUnbound("copy-nbt-data")
val copySkullTexture by keyBindingWithDefaultUnbound("copy-skull-texture") val copyLoreData by keyBindingWithDefaultUnbound("copy-lore")
val copyEntityData by keyBindingWithDefaultUnbound("entity-data") val copySkullTexture by keyBindingWithDefaultUnbound("copy-skull-texture")
} val copyEntityData by keyBindingWithDefaultUnbound("entity-data")
}
override val config override val config
get() = TConfig get() = TConfig
var lastCopiedStack: Pair<ItemStack, Text>? = null var lastCopiedStack: Pair<ItemStack, Text>? = null
set(value) { set(value) {
field = value field = value
if (value != null) if (value != null)
lastCopiedStackViewTime = true lastCopiedStackViewTime = true
} }
var lastCopiedStackViewTime = false var lastCopiedStackViewTime = false
@Subscribe @Subscribe
fun resetLastCopiedStack(event: TickEvent) { fun resetLastCopiedStack(event: TickEvent) {
if (!lastCopiedStackViewTime) if (!lastCopiedStackViewTime)
lastCopiedStack = null lastCopiedStack = null
lastCopiedStackViewTime = false lastCopiedStackViewTime = false
} }
@Subscribe @Subscribe
fun resetLastCopiedStackOnScreenChange(event: ScreenChangeEvent) { fun resetLastCopiedStackOnScreenChange(event: ScreenChangeEvent) {
lastCopiedStack = null lastCopiedStack = null
} }
fun debugFormat(itemStack: ItemStack): Text { fun debugFormat(itemStack: ItemStack): Text {
return Text.literal(itemStack.skyBlockId?.toString() ?: itemStack.toString()) return Text.literal(itemStack.skyBlockId?.toString() ?: itemStack.toString())
} }
@Subscribe @Subscribe
fun onEntityInfo(event: WorldKeyboardEvent) { fun onEntityInfo(event: WorldKeyboardEvent) {
if (!event.matches(TConfig.copyEntityData)) return if (!event.matches(TConfig.copyEntityData)) return
val target = (MC.instance.crosshairTarget as? EntityHitResult)?.entity val target = (MC.instance.crosshairTarget as? EntityHitResult)?.entity
if (target == null) { if (target == null) {
MC.sendChat(Text.translatable("firmament.poweruser.entity.fail")) MC.sendChat(Text.translatable("firmament.poweruser.entity.fail"))
return return
} }
showEntity(target) showEntity(target)
} }
fun showEntity(target: Entity) { fun showEntity(target: Entity) {
MC.sendChat(Text.translatable("firmament.poweruser.entity.type", target.type)) MC.sendChat(Text.translatable("firmament.poweruser.entity.type", target.type))
MC.sendChat(Text.translatable("firmament.poweruser.entity.name", target.name)) MC.sendChat(Text.translatable("firmament.poweruser.entity.name", target.name))
MC.sendChat(Text.stringifiedTranslatable("firmament.poweruser.entity.position", target.pos)) MC.sendChat(Text.stringifiedTranslatable("firmament.poweruser.entity.position", target.pos))
if (target is LivingEntity) { if (target is LivingEntity) {
MC.sendChat(Text.translatable("firmament.poweruser.entity.armor")) MC.sendChat(Text.translatable("firmament.poweruser.entity.armor"))
for (armorItem in target.armorItems) { for (armorItem in target.armorItems) {
MC.sendChat(Text.translatable("firmament.poweruser.entity.armor.item", debugFormat(armorItem))) MC.sendChat(Text.translatable("firmament.poweruser.entity.armor.item", debugFormat(armorItem)))
} }
} }
MC.sendChat(Text.stringifiedTranslatable("firmament.poweruser.entity.passengers", target.passengerList.size)) MC.sendChat(Text.stringifiedTranslatable("firmament.poweruser.entity.passengers", target.passengerList.size))
target.passengerList.forEach { target.passengerList.forEach {
showEntity(it) showEntity(it)
} }
} }
@Subscribe @Subscribe
fun copyInventoryInfo(it: HandledScreenKeyPressedEvent) { fun copyInventoryInfo(it: HandledScreenKeyPressedEvent) {
if (it.screen !is AccessorHandledScreen) return if (it.screen !is AccessorHandledScreen) return
val item = it.screen.focusedItemStack ?: return val item = it.screen.focusedItemStack ?: return
if (it.matches(TConfig.copyItemId)) { if (it.matches(TConfig.copyItemId)) {
val sbId = item.skyBlockId val sbId = item.skyBlockId
if (sbId == null) { if (sbId == null) {
lastCopiedStack = Pair(item, Text.translatable("firmament.tooltip.copied.skyblockid.fail")) lastCopiedStack = Pair(item, Text.translatable("firmament.tooltip.copied.skyblockid.fail"))
return return
} }
ClipboardUtils.setTextContent(sbId.neuItem) ClipboardUtils.setTextContent(sbId.neuItem)
lastCopiedStack = lastCopiedStack =
Pair(item, Text.stringifiedTranslatable("firmament.tooltip.copied.skyblockid", sbId.neuItem)) Pair(item, Text.stringifiedTranslatable("firmament.tooltip.copied.skyblockid", sbId.neuItem))
} else if (it.matches(TConfig.copyTexturePackId)) { } else if (it.matches(TConfig.copyTexturePackId)) {
val model = CustomItemModelEvent.getModelIdentifier(item) val model = CustomItemModelEvent.getModelIdentifier(item)
if (model == null) { if (model == null) {
lastCopiedStack = Pair(item, Text.translatable("firmament.tooltip.copied.modelid.fail")) lastCopiedStack = Pair(item, Text.translatable("firmament.tooltip.copied.modelid.fail"))
return return
} }
ClipboardUtils.setTextContent(model.toString()) ClipboardUtils.setTextContent(model.toString())
lastCopiedStack = lastCopiedStack =
Pair(item, Text.stringifiedTranslatable("firmament.tooltip.copied.modelid", model.toString())) Pair(item, Text.stringifiedTranslatable("firmament.tooltip.copied.modelid", model.toString()))
} else if (it.matches(TConfig.copyNbtData)) { } else if (it.matches(TConfig.copyNbtData)) {
// TODO: copy full nbt // TODO: copy full nbt
val nbt = item.get(DataComponentTypes.CUSTOM_DATA)?.nbt?.toString() ?: "<empty>" val nbt = item.get(DataComponentTypes.CUSTOM_DATA)?.nbt?.toString() ?: "<empty>"
ClipboardUtils.setTextContent(nbt) ClipboardUtils.setTextContent(nbt)
lastCopiedStack = Pair(item, Text.translatable("firmament.tooltip.copied.nbt")) lastCopiedStack = Pair(item, Text.translatable("firmament.tooltip.copied.nbt"))
} else if (it.matches(TConfig.copySkullTexture)) { } else if (it.matches(TConfig.copyLoreData)) {
if (item.item != Items.PLAYER_HEAD) { val list = mutableListOf(item.displayNameAccordingToNbt)
lastCopiedStack = Pair(item, Text.translatable("firmament.tooltip.copied.skull-id.fail.no-skull")) list.addAll(item.loreAccordingToNbt)
return ClipboardUtils.setTextContent(list.joinToString("\n") {
} TextCodecs.CODEC.encodeStart(JsonOps.INSTANCE, it).result().getOrNull().toString()
val profile = item.get(DataComponentTypes.PROFILE) })
if (profile == null) { lastCopiedStack = Pair(item, Text.translatable("firmament.tooltip.copied.lore"))
lastCopiedStack = Pair(item, Text.translatable("firmament.tooltip.copied.skull-id.fail.no-profile")) } else if (it.matches(TConfig.copySkullTexture)) {
return if (item.item != Items.PLAYER_HEAD) {
} lastCopiedStack = Pair(item, Text.translatable("firmament.tooltip.copied.skull-id.fail.no-skull"))
val skullTexture = CustomSkyBlockTextures.getSkullTexture(profile) return
if (skullTexture == null) { }
lastCopiedStack = Pair(item, Text.translatable("firmament.tooltip.copied.skull-id.fail.no-texture")) val profile = item.get(DataComponentTypes.PROFILE)
return if (profile == null) {
} lastCopiedStack = Pair(item, Text.translatable("firmament.tooltip.copied.skull-id.fail.no-profile"))
ClipboardUtils.setTextContent(skullTexture.toString()) return
lastCopiedStack = }
Pair( val skullTexture = CustomSkyBlockTextures.getSkullTexture(profile)
item, if (skullTexture == null) {
Text.stringifiedTranslatable("firmament.tooltip.copied.skull-id", skullTexture.toString()) lastCopiedStack = Pair(item, Text.translatable("firmament.tooltip.copied.skull-id.fail.no-texture"))
) return
println("Copied skull id: $skullTexture") }
} ClipboardUtils.setTextContent(skullTexture.toString())
} lastCopiedStack =
Pair(
item,
Text.stringifiedTranslatable("firmament.tooltip.copied.skull-id", skullTexture.toString())
)
println("Copied skull id: $skullTexture")
}
}
@Subscribe @Subscribe
fun onCopyWorldInfo(it: WorldKeyboardEvent) { fun onCopyWorldInfo(it: WorldKeyboardEvent) {
if (it.matches(TConfig.copySkullTexture)) { if (it.matches(TConfig.copySkullTexture)) {
val p = MC.camera ?: return val p = MC.camera ?: return
val blockHit = p.raycast(20.0, 0.0f, false) ?: return val blockHit = p.raycast(20.0, 0.0f, false) ?: return
if (blockHit.type != HitResult.Type.BLOCK || blockHit !is BlockHitResult) { if (blockHit.type != HitResult.Type.BLOCK || blockHit !is BlockHitResult) {
MC.sendChat(Text.translatable("firmament.tooltip.copied.skull.fail")) MC.sendChat(Text.translatable("firmament.tooltip.copied.skull.fail"))
return return
} }
val blockAt = p.world.getBlockState(blockHit.blockPos)?.block val blockAt = p.world.getBlockState(blockHit.blockPos)?.block
val entity = p.world.getBlockEntity(blockHit.blockPos) val entity = p.world.getBlockEntity(blockHit.blockPos)
if (blockAt !is SkullBlock || entity !is SkullBlockEntity || entity.owner == null) { if (blockAt !is SkullBlock || entity !is SkullBlockEntity || entity.owner == null) {
MC.sendChat(Text.translatable("firmament.tooltip.copied.skull.fail")) MC.sendChat(Text.translatable("firmament.tooltip.copied.skull.fail"))
return return
} }
val id = CustomSkyBlockTextures.getSkullTexture(entity.owner!!) val id = CustomSkyBlockTextures.getSkullTexture(entity.owner!!)
if (id == null) { if (id == null) {
MC.sendChat(Text.translatable("firmament.tooltip.copied.skull.fail")) MC.sendChat(Text.translatable("firmament.tooltip.copied.skull.fail"))
} else { } else {
ClipboardUtils.setTextContent(id.toString()) ClipboardUtils.setTextContent(id.toString())
MC.sendChat(Text.stringifiedTranslatable("firmament.tooltip.copied.skull", id.toString())) MC.sendChat(Text.stringifiedTranslatable("firmament.tooltip.copied.skull", id.toString()))
} }
} }
} }
@Subscribe @Subscribe
fun addItemId(it: ItemTooltipEvent) { fun addItemId(it: ItemTooltipEvent) {
if (TConfig.showItemIds) { if (TConfig.showItemIds) {
val id = it.stack.skyBlockId ?: return val id = it.stack.skyBlockId ?: return
it.lines.add(Text.stringifiedTranslatable("firmament.tooltip.skyblockid", id.neuItem)) it.lines.add(Text.stringifiedTranslatable("firmament.tooltip.skyblockid", id.neuItem))
} }
val (item, text) = lastCopiedStack ?: return val (item, text) = lastCopiedStack ?: return
if (!ItemStack.areEqual(item, it.stack)) { if (!ItemStack.areEqual(item, it.stack)) {
lastCopiedStack = null lastCopiedStack = null
return return
} }
lastCopiedStackViewTime = true lastCopiedStackViewTime = true
it.lines.add(text) it.lines.add(text)
} }
} }

View File

@@ -183,6 +183,7 @@
"firmament.config.power-user.copy-skull-texture": "Copy Placed Skull Id", "firmament.config.power-user.copy-skull-texture": "Copy Placed Skull Id",
"firmament.config.power-user.entity-data": "Show Entity Data", "firmament.config.power-user.entity-data": "Show Entity Data",
"firmament.config.power-user.copy-nbt-data": "Copy NBT data", "firmament.config.power-user.copy-nbt-data": "Copy NBT data",
"firmament.config.power-user.copy-lore": "Copy Name + Lore",
"firmament.config.power-user": "Power Users", "firmament.config.power-user": "Power Users",
"firmament.tooltip.skyblockid": "SkyBlock Id: %s", "firmament.tooltip.skyblockid": "SkyBlock Id: %s",
"firmament.tooltip.copied.skyblockid.fail": "Failed to copy SkyBlock Id", "firmament.tooltip.copied.skyblockid.fail": "Failed to copy SkyBlock Id",
@@ -192,6 +193,7 @@
"firmament.tooltip.copied.skull": "Copied Skull Id: %s", "firmament.tooltip.copied.skull": "Copied Skull Id: %s",
"firmament.tooltip.copied.skull.fail": "Failed to copy skull id.", "firmament.tooltip.copied.skull.fail": "Failed to copy skull id.",
"firmament.tooltip.copied.nbt": "Copied NBT data", "firmament.tooltip.copied.nbt": "Copied NBT data",
"firmament.tooltip.copied.lore": "Copied Name and Lore",
"firmament.config.compatibility": "Intermod Features", "firmament.config.compatibility": "Intermod Features",
"firmament.config.compatibility.explosion-enabled": "Redirect Enhanced Explosions", "firmament.config.compatibility.explosion-enabled": "Redirect Enhanced Explosions",
"firmament.config.compatibility.explosion-power": "Enhanced Explosion Power", "firmament.config.compatibility.explosion-power": "Enhanced Explosion Power",