diff options
author | Linnea Gräf <nea@nea.moe> | 2024-02-14 20:46:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-14 20:46:25 +0100 |
commit | eb4b815095666bd9d13074a052c4329ba9ac7da7 (patch) | |
tree | 8171cb34e221c2deacc5549d55d6b28fa1ea3d5b /src/main/java/at/hannibal2 | |
parent | 029514e0324d8ae72ad181c06bcf081c5c2a6304 (diff) | |
download | skyhanni-eb4b815095666bd9d13074a052c4329ba9ac7da7.tar.gz skyhanni-eb4b815095666bd9d13074a052c4329ba9ac7da7.tar.bz2 skyhanni-eb4b815095666bd9d13074a052c4329ba9ac7da7.zip |
Add mod identification for outgoing mod calls and show this data in /shchathistory. #987
Diffstat (limited to 'src/main/java/at/hannibal2')
7 files changed, 73 insertions, 55 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/data/ChatManager.kt b/src/main/java/at/hannibal2/skyhanni/data/ChatManager.kt index d631e8e26..7d0bb6728 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/ChatManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/ChatManager.kt @@ -9,6 +9,8 @@ import at.hannibal2.skyhanni.features.chat.ChatFilterGui import at.hannibal2.skyhanni.utils.IdentityCharacteristics import at.hannibal2.skyhanni.utils.LorenzLogger import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.ReflectionUtils.getClassInstance +import at.hannibal2.skyhanni.utils.ReflectionUtils.getModContainer import at.hannibal2.skyhanni.utils.ReflectionUtils.makeAccessible import net.minecraft.client.Minecraft import net.minecraft.client.gui.ChatLine @@ -65,6 +67,8 @@ object ChatManager { var actionKind: ActionKind, var actionReason: String?, val modified: IChatComponent?, + val hoverInfo: List<String> = listOf(), + val hoverExtraInfo: List<String> = listOf(), ) @SubscribeEvent(priority = EventPriority.LOW, receiveCanceled = true) @@ -85,12 +89,28 @@ object ChatManager { val message = packet.message val component = ChatComponentText(message) - messageHistory[IdentityCharacteristics(component)] = - MessageFilteringResult(component, ActionKind.OUTGOING, null, null) + val originatingModCall = event.findOriginatingModCall() + val originatingModContainer = originatingModCall?.getClassInstance()?.getModContainer() + val hoverInfo = listOf( + "§7Message created by §a${originatingModCall?.toString() ?: "§cprobably minecraft"}", + "§7Mod id: §a${originatingModContainer?.modId}", + "§7Mod name: §a${originatingModContainer?.name}" + ) + val stackTrace = + Thread.currentThread().stackTrace.map { + "§7 §2${it.className}§7.§a${it.methodName}§7" + + if (it.fileName == null) "" else "(§b${it.fileName}§7:§3${it.lineNumber}§7)" + } + val result = MessageFilteringResult( + component, ActionKind.OUTGOING, null, null, + hoverInfo = hoverInfo, + hoverExtraInfo = hoverInfo + listOf("") + stackTrace + ) + + messageHistory[IdentityCharacteristics(component)] = result if (MessageSendToServerEvent(message).postAndCatch()) { event.isCanceled = true - messageHistory[IdentityCharacteristics(component)] = - MessageFilteringResult(component, ActionKind.OUTGOING_BLOCKED, null, null) + messageHistory[IdentityCharacteristics(component)] = result.copy(actionKind = ActionKind.OUTGOING_BLOCKED) } } diff --git a/src/main/java/at/hannibal2/skyhanni/events/PacketEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/PacketEvent.kt index 7d356707e..1c9905ce6 100644 --- a/src/main/java/at/hannibal2/skyhanni/events/PacketEvent.kt +++ b/src/main/java/at/hannibal2/skyhanni/events/PacketEvent.kt @@ -26,6 +26,18 @@ abstract class PacketEvent : LorenzEvent() { data class SendEvent(override val packet: Packet<*>) : PacketEvent() { override val direction = Direction.OUTBOUND + + fun findOriginatingModCall(skipSkyhanni: Boolean = false): StackTraceElement? { + val nonMinecraftOriginatingStack = Thread.currentThread().stackTrace + // Skip calls before the event is being called + .dropWhile { it.className != "net.minecraft.client.network.NetHandlerPlayClient" } + // Limit the remaining callstack until only the main entrypoint to hide the relauncher + .takeWhile { !it.className.endsWith(".Main") } + // Drop minecraft or skyhanni call frames + .dropWhile { it.className.startsWith("net.minecraft.") || (skipSkyhanni && it.className.startsWith("at.hannibal2.skyhanni.")) } + .firstOrNull() + return nonMinecraftOriginatingStack + } } enum class Direction { diff --git a/src/main/java/at/hannibal2/skyhanni/features/chat/ChatFilterGui.kt b/src/main/java/at/hannibal2/skyhanni/features/chat/ChatFilterGui.kt index e40854c9d..0e9392ea0 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/chat/ChatFilterGui.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/chat/ChatFilterGui.kt @@ -7,6 +7,7 @@ import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.OSUtils import io.github.moulberry.moulconfig.internal.GlScissorStack import io.github.moulberry.moulconfig.internal.RenderUtils +import io.github.moulberry.notenoughupdates.util.Utils import net.minecraft.client.Minecraft import net.minecraft.client.gui.GuiScreen import net.minecraft.client.gui.GuiUtilRenderComponents @@ -27,18 +28,19 @@ class ChatFilterGui(private val history: List<ChatManager.MessageFilteringResult history.sumOf { splitLine(it.message).size * 10 + if (it.modified != null) splitLine(it.modified).size * 10 else 0 } } - override fun drawScreen(mouseX: Int, mouseY: Int, partialTicks: Float) { - super.drawScreen(mouseX, mouseY, partialTicks) + override fun drawScreen(originalMouseX: Int, originalMouseY: Int, partialTicks: Float) { + super.drawScreen(originalMouseX, originalMouseY, partialTicks) drawDefaultBackground() + var queuedTooltip: List<String>? = null GlStateManager.pushMatrix() val l = (width / 2.0 - w / 2.0).toInt() val t = (height / 2.0 - h / 2.0).toInt() GlStateManager.translate(l + 0.0, t + 0.0, 0.0) RenderUtils.drawFloatingRectDark(0, 0, w, h) GlStateManager.translate(5.0, 5.0 - scroll, 0.0) - var mouseX = mouseX - l - val isMouseButtonDown = mouseX in 0..w && mouseY in t..(t + h) && Mouse.isButtonDown(0) - var mouseY = mouseY - (t - scroll).toInt() + var mouseX = originalMouseX - l + val isMouseButtonDown = mouseX in 0..w && originalMouseY in t..(t + h) && Mouse.isButtonDown(0) + var mouseY = originalMouseY - (t - scroll).toInt() val sr = ScaledResolution(mc) GlScissorStack.push(l + 5, t + 5, w + l - 5, h + t - 5, sr) @@ -60,7 +62,12 @@ class ChatFilterGui(private val history: List<ChatManager.MessageFilteringResult ChatManager.ActionKind.maxLength + reasonMaxLength + 10, ) } - if (mouseX in 0..w && mouseY in 0..(size * 10) && (isMouseButtonDown && !wasMouseButtonDown)) { + val isHovered = mouseX in 0..w && mouseY in 0..(size * 10) + if (isHovered && msg.hoverInfo.isNotEmpty()) + queuedTooltip = msg.hoverInfo + if (isHovered && KeyboardManager.isShiftKeyDown() && msg.hoverExtraInfo.isNotEmpty()) + queuedTooltip = msg.hoverExtraInfo + if (isHovered && (isMouseButtonDown && !wasMouseButtonDown)) { if (KeyboardManager.isShiftKeyDown()) { OSUtils.copyToClipboard(IChatComponent.Serializer.componentToJson(msg.message)) ChatUtils.chat("Copied structured chat line to clipboard", false) @@ -75,6 +82,9 @@ class ChatFilterGui(private val history: List<ChatManager.MessageFilteringResult GlScissorStack.pop(sr) wasMouseButtonDown = isMouseButtonDown GlStateManager.popMatrix() + if (queuedTooltip != null) { + Utils.drawHoveringText(queuedTooltip, originalMouseX, originalMouseY, width, height, -1, mc.fontRendererObj) + } GlStateManager.color(1f, 1f, 1f, 1f) } diff --git a/src/main/java/at/hannibal2/skyhanni/mixins/hooks/EntityPlayerSPHook.kt b/src/main/java/at/hannibal2/skyhanni/mixins/hooks/EntityPlayerSPHook.kt deleted file mode 100644 index ea115119f..000000000 --- a/src/main/java/at/hannibal2/skyhanni/mixins/hooks/EntityPlayerSPHook.kt +++ /dev/null @@ -1,7 +0,0 @@ -package at.hannibal2.skyhanni.mixins.hooks - -import at.hannibal2.skyhanni.test.CommandsSendToServerLogger - -fun sendChatMessage(message: String) { - CommandsSendToServerLogger.logCommandsToServer(message) -}
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinEntityPlayerSP.java b/src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinEntityPlayerSP.java deleted file mode 100644 index db41c5cce..000000000 --- a/src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinEntityPlayerSP.java +++ /dev/null @@ -1,17 +0,0 @@ -package at.hannibal2.skyhanni.mixins.transformers; - -import at.hannibal2.skyhanni.mixins.hooks.EntityPlayerSPHookKt; -import net.minecraft.client.entity.EntityPlayerSP; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -@Mixin(EntityPlayerSP.class) -public class MixinEntityPlayerSP { - - @Inject(method = "sendChatMessage", at = @At(value = "HEAD")) - private void sendChatMessage_inject(String message, CallbackInfo ci) { - EntityPlayerSPHookKt.sendChatMessage(message); - } -} diff --git a/src/main/java/at/hannibal2/skyhanni/test/CommandsSendToServerLogger.kt b/src/main/java/at/hannibal2/skyhanni/test/CommandsSendToServerLogger.kt index 88ce05746..e69de29bb 100644 --- a/src/main/java/at/hannibal2/skyhanni/test/CommandsSendToServerLogger.kt +++ b/src/main/java/at/hannibal2/skyhanni/test/CommandsSendToServerLogger.kt @@ -1,21 +0,0 @@ -package at.hannibal2.skyhanni.test - -import at.hannibal2.skyhanni.SkyHanniMod -import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent - -class CommandsSendToServerLogger { - companion object { - - fun logCommandsToServer(command: String) { - if (SkyHanniMod.feature.dev.debug.commandLogs) { - Exception("command send to server: '$command'").printStackTrace() - } - } - } - - @SubscribeEvent - fun onConfigFix(event: ConfigUpdaterMigrator.ConfigFixEvent) { - event.move(3, "dev.commandLogs", "dev.debug.commandLogs") - } -} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ReflectionUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ReflectionUtils.kt index 2cf613b39..5ef72ddd7 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ReflectionUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ReflectionUtils.kt @@ -1,5 +1,7 @@ package at.hannibal2.skyhanni.utils +import net.minecraftforge.fml.common.Loader +import net.minecraftforge.fml.common.ModContainer import java.lang.reflect.Constructor import java.lang.reflect.Field import java.lang.reflect.Modifier @@ -59,4 +61,23 @@ object ReflectionUtils { javaClass.getDeclaredField("modifiers").makeAccessible().set(this, modifiers and (Modifier.FINAL.inv())) return this } + + fun StackTraceElement.getClassInstance(): Class<*> { + return Class.forName(this.className) + } + + private val packageLookup by lazy { + Loader.instance().modList + .flatMap { mod -> mod.ownedPackages.map { it to mod } } + .toMap() + } + + val Class<*>.shPackageName + get() = + canonicalName.substringBeforeLast('.') + + fun Class<*>.getModContainer(): ModContainer? { + return packageLookup[shPackageName] + } + } |