|
| 1 | +package com.github.yournamehere; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import com.aliucord.CollectionUtils; |
| 5 | +import com.aliucord.annotations.AliucordPlugin; |
| 6 | +import com.aliucord.entities.MessageEmbedBuilder; |
| 7 | +import com.aliucord.entities.Plugin; |
| 8 | +import com.aliucord.patcher.*; |
| 9 | +import com.aliucord.wrappers.embeds.MessageEmbedWrapper; |
| 10 | +import com.discord.models.user.CoreUser; |
| 11 | +import com.discord.stores.StoreUserTyping; |
| 12 | +import com.discord.widgets.chat.list.adapter.WidgetChatListAdapterItemMessage; |
| 13 | +import com.discord.widgets.chat.list.entries.ChatListEntry; |
| 14 | +import com.discord.widgets.chat.list.entries.MessageEntry; |
| 15 | + |
| 16 | +// Aliucord Plugin annotation. Must be present on the main class of your plugin |
| 17 | +@AliucordPlugin(requiresRestart = false /* Whether your plugin requires a restart after being installed/updated */) |
| 18 | +// Plugin class. Must extend Plugin and override start and stop |
| 19 | +// Learn more: https://github.com/Aliucord/documentation/blob/main/plugin-dev/1_introduction.md#basic-plugin-structure |
| 20 | +public class MyFirstPatch extends Plugin { |
| 21 | + @Override |
| 22 | + public void start(Context context) throws Throwable { |
| 23 | + // Patch that adds an embed with message statistics to each message |
| 24 | + // Patched method is WidgetChatListAdapterItemMessage.onConfigure(int type, ChatListEntry entry) |
| 25 | + patcher.patch( |
| 26 | + // see https://docs.oracle.com/javase/tutorial/reflect/class/classNew.html |
| 27 | + WidgetChatListAdapterItemMessage.class.getDeclaredMethod("onConfigure", int.class, ChatListEntry.class), |
| 28 | + new Hook(param -> { // see https://api.xposed.info/reference/de/robv/android/xposed/XC_MethodHook.MethodHookParam.html |
| 29 | + // Obtain the second argument passed to the method, so the ChatListEntry |
| 30 | + // Because this is a Message item, it will always be a MessageEntry, so cast it to that |
| 31 | + var entry = (MessageEntry) param.args[1]; |
| 32 | + |
| 33 | + // You need to be careful when messing with messages, because they may be loading |
| 34 | + // (user sent a message, and it is currently sending) |
| 35 | + if (entry.getMessage().isLoading()) return; |
| 36 | + |
| 37 | + // Now add an embed with the statistics |
| 38 | + |
| 39 | + // This method may be called multiple times per message, e.g. if it is edited, |
| 40 | + // so first remove existing embeds |
| 41 | + CollectionUtils.removeIf(entry.getMessage().getEmbeds(), e -> { |
| 42 | + // MessageEmbed is an obfuscated class. However, Aliucord provides wrappers for commonly used |
| 43 | + // obfuscated classes, the MessageEmbedWrapper in this case. |
| 44 | + return "Message Statistics".equals(MessageEmbedWrapper.getTitle(e)); |
| 45 | + }); |
| 46 | + |
| 47 | + // Creating embeds is a pain, so Aliucord provides a convenient builder |
| 48 | + var embed = new MessageEmbedBuilder(). |
| 49 | + setTitle("Message Statistics") |
| 50 | + .addField("Length", entry.getMessage().getContent() != null ? Integer.toString(entry.getMessage().getContent().length()) : "0", false) |
| 51 | + .addField("ID", Long.toString(entry.getMessage().getId()), false).build(); |
| 52 | + |
| 53 | + entry.getMessage().getEmbeds().add(embed); |
| 54 | + }) |
| 55 | + ); |
| 56 | + |
| 57 | + // Patch that renames Juby to JoobJoob |
| 58 | + patcher.patch( |
| 59 | + CoreUser.class.getDeclaredMethod("getUsername"), |
| 60 | + new PreHook(param -> { // see https://api.xposed.info/reference/de/robv/android/xposed/XC_MethodHook.MethodHookParam.html |
| 61 | + if (((CoreUser) param.thisObject).getId() == 925141667688878090L) { |
| 62 | + // setResult() in before patches skips original method invocation |
| 63 | + param.setResult("JoobJoob"); |
| 64 | + } |
| 65 | + }) |
| 66 | + ); |
| 67 | + |
| 68 | + // Patch that hides your typing status by replacing the method and simply doing nothing |
| 69 | + patcher.patch( |
| 70 | + StoreUserTyping.class.getDeclaredMethod("setUserTyping", long.class), |
| 71 | + InsteadHook.DO_NOTHING |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void stop(Context context) { |
| 77 | + // Remove all patches |
| 78 | + patcher.unpatchAll(); |
| 79 | + } |
| 80 | +} |
0 commit comments