aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/features/debug/AnimatedClothingScanner.kt
blob: 4edccfb5d27cf3397dacd783b64a9a33cef52e3c (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
191
192
193
package moe.nea.firmament.features.debug

import net.minecraft.command.argument.RegistryKeyArgumentType
import net.minecraft.component.ComponentType
import net.minecraft.entity.Entity
import net.minecraft.entity.decoration.ArmorStandEntity
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NbtElement
import net.minecraft.nbt.NbtOps
import net.minecraft.registry.RegistryKeys
import moe.nea.firmament.annotations.Subscribe
import moe.nea.firmament.commands.get
import moe.nea.firmament.commands.thenArgument
import moe.nea.firmament.commands.thenExecute
import moe.nea.firmament.commands.thenLiteral
import moe.nea.firmament.events.CommandEvent
import moe.nea.firmament.events.EntityUpdateEvent
import moe.nea.firmament.events.WorldReadyEvent
import moe.nea.firmament.util.ClipboardUtils
import moe.nea.firmament.util.MC
import moe.nea.firmament.util.math.GChainReconciliation
import moe.nea.firmament.util.math.GChainReconciliation.shortenCycle
import moe.nea.firmament.util.mc.NbtPrism
import moe.nea.firmament.util.tr

object AnimatedClothingScanner {

	data class LensOfFashionTheft<T>(
		val prism: NbtPrism,
		val component: ComponentType<T>,
	) {
		fun observe(itemStack: ItemStack): Collection<NbtElement> {
			val x = itemStack.get(component) ?: return listOf()
			val nbt = component.codecOrThrow.encodeStart(NbtOps.INSTANCE, x).orThrow
			return prism.access(nbt)
		}
	}

	var lens: LensOfFashionTheft<*>? = null
	var subject: Entity? = null
	var history: MutableList<String> = mutableListOf()
	val metaHistory: MutableList<List<String>> = mutableListOf()

	@OptIn(ExperimentalStdlibApi::class)
	@Subscribe
	fun onUpdate(event: EntityUpdateEvent) {
		val s = subject ?: return
		if (event.entity != s) return
		val l = lens ?: return
		if (event is EntityUpdateEvent.EquipmentUpdate) {
			event.newEquipment.forEach {
				val formatted = (l.observe(it.second)).joinToString()
				history.add(formatted)
				// TODO: add a slot filter
			}
		}
	}

	fun reduceHistory(reducer: (List<String>, List<String>) -> List<String>): List<String> {
		return metaHistory.fold(history, reducer).shortenCycle()
	}

	@Subscribe
	fun onSubCommand(event: CommandEvent.SubCommand) {
		event.subcommand(DeveloperFeatures.DEVELOPER_SUBCOMMAND) {
			thenLiteral("stealthisfit") {
				thenLiteral("clear") {
					thenExecute {
						subject = null
						metaHistory.clear()
						history.clear()
						MC.sendChat(tr("firmament.fitstealer.clear", "Cleared fit stealing history"))
					}
				}
				thenLiteral("copy") {
					thenExecute {
						val history = reduceHistory { a, b -> a + b }
						copyHistory(history)
						MC.sendChat(tr("firmament.fitstealer.copied", "Copied the history"))
					}
					thenLiteral("deduplicated") {
						thenExecute {
							val history = reduceHistory { a, b ->
								(a.toMutableSet() + b).toList()
							}
							copyHistory(history)
							MC.sendChat(
								tr(
									"firmament.fitstealer.copied.deduplicated",
									"Copied the deduplicated history"
								)
							)
						}
					}
					thenLiteral("merged") {
						thenExecute {
							val history = reduceHistory(GChainReconciliation::reconcileCycles)
							copyHistory(history)
							MC.sendChat(tr("firmament.fitstealer.copied.merged", "Copied the merged history"))
						}
					}
				}
				thenLiteral("target") {
					thenLiteral("self") {
						thenExecute {
							toggleObserve(MC.player!!)
						}
					}
					thenLiteral("pet") {
						thenExecute {
							source.sendFeedback(
								tr(
									"firmament.fitstealer.stealingpet",
									"Observing nearest marker armourstand"
								)
							)
							val p = MC.player!!
							val nearestPet = p.world.getEntitiesByClass(
								ArmorStandEntity::class.java,
								p.boundingBox.expand(10.0),
								{ it.isMarker })
								.minBy { it.squaredDistanceTo(p) }
							toggleObserve(nearestPet)
						}
					}
					thenExecute {
						val ent = MC.instance.targetedEntity
						if (ent == null) {
							source.sendFeedback(
								tr(
									"firmament.fitstealer.notargetundercursor",
									"No entity under cursor"
								)
							)
						} else {
							toggleObserve(ent)
						}
					}
				}
				thenLiteral("path") {
					thenArgument(
						"component",
						RegistryKeyArgumentType.registryKey(RegistryKeys.DATA_COMPONENT_TYPE)
					) { component ->
						thenArgument("path", NbtPrism.Argument) { path ->
							thenExecute {
								lens = LensOfFashionTheft(
									get(path),
									MC.unsafeGetRegistryEntry(get(component))!!,
								)
								source.sendFeedback(
									tr(
										"firmament.fitstealer.lensset",
										"Analyzing path ${get(path)} for component ${get(component).value}"
									)
								)
							}
						}
					}
				}
			}
		}
	}

	private fun copyHistory(toCopy: List<String>) {
		ClipboardUtils.setTextContent(toCopy.joinToString("\n"))
	}

	@Subscribe
	fun onWorldSwap(event: WorldReadyEvent) {
		subject = null
		if (history.isNotEmpty()) {
			metaHistory.add(history)
			history = mutableListOf()
		}
	}

	private fun toggleObserve(entity: Entity?) {
		subject = if (subject == null) entity else null
		if (subject == null) {
			metaHistory.add(history)
			history = mutableListOf()
		}
		MC.sendChat(
			subject?.let {
				tr(
					"firmament.fitstealer.targeted",
					"Observing the equipment of ${it.name}."
				)
			} ?: tr("firmament.fitstealer.targetlost", "No longer logging equipment."),
		)
	}
}