DiscordActivity module

DiscordActivity: Minecraft 26.3 cheat module, full source for every implementation (1 client: Nicotine).

1 implementationMisc

Clients with this module

Full module source: 1 implementation

Every implementation below is the complete module file, verbatim. Expand a client to read its code.

NicotineMiscsrc/client/java/nicotine/mod/mods/misc/DiscordActivity.javaJava · 229 lines · 6.8 KB
SettingsPlayerDimension
DiscordActivity.javaJava · Minecraft 26.3 · 229 lines · 6.8 KB · complete file, verbatimOriginal ↗
1
package nicotine.mod.mods.misc;
2
3
import com.google.gson.JsonObject;
4
import nicotine.events.ClientTickEvent;
5
import nicotine.mod.Mod;
6
import nicotine.mod.ModCategory;
7
import nicotine.mod.option.ToggleOption;
8
import nicotine.util.EventBus;
9
import nicotine.util.Message;
10
import org.apache.commons.lang3.StringUtils;
11
import org.apache.commons.lang3.SystemUtils;
12
13
import java.io.File;
14
import java.io.FileNotFoundException;
15
import java.io.IOException;
16
import java.io.RandomAccessFile;
17
import java.net.UnixDomainSocketAddress;
18
import java.nio.ByteBuffer;
19
import java.nio.channels.SocketChannel;
20
import java.nio.charset.StandardCharsets;
21
import java.util.Arrays;
22
import java.util.UUID;
23
24
import static nicotine.util.Common.*;
25
26
public class DiscordActivity extends Mod {
27
28
    private static final String APP_ID = "1395381592364421200";
29
30
    private static SocketChannel socketChannel;
31
    private static RandomAccessFile randomAccessFile;
32
33
    private static int tickDelay = 0;
34
35
    private final ToggleOption player = new ToggleOption("Player");
36
    private final ToggleOption dimension = new ToggleOption("Dimension");
37
38
    public DiscordActivity() {
39
        super(ModCategory.Misc, "DiscordActivity");
40
        this.addOptions(Arrays.asList(player, dimension));
41
    }
42
43
    private boolean socketOpen() {
44
        if (SystemUtils.IS_OS_WINDOWS) {
45
            for (int i = 0; i < 10; i++) {
46
                try {
47
                    randomAccessFile = new RandomAccessFile("\\\\\\\\?\\\\pipe\\\\discord-ipc-" + i, "rw");
48
                } catch (FileNotFoundException e) {
49
                    continue;
50
                }
51
52
                return true;
53
            }
54
        } else {
55
            String[] envVars = { "XDG_RUNTIME_DIR", "TMPDIR", "TMP", "TEMP" };
56
            String[] subPaths = {"", "/app/com.discordapp.Discord", "/.flatpak/com.discordapp.Discord/xdg-run" };
57
58
            for (String envVar : envVars) {
59
                String path = System.getenv(envVar);
60
61
                if (path == null)
62
                    continue;
63
64
                for (String subPath : subPaths) {
65
                    for (int i = 0; i < 10; i++) {
66
                        String socketFileName = String.format("%s%s/discord-ipc-%d", path, subPath, i);
67
68
                        File socketFile = new File(socketFileName);
69
70
                        if (socketFile.exists()) {
71
                            try {
72
                                socketChannel = SocketChannel.open(UnixDomainSocketAddress.of(socketFileName));
73
                            } catch (IOException e) {
74
                                continue;
75
                            }
76
77
                            return true;
78
                        }
79
                    }
80
                }
81
            }
82
        }
83
84
        return false;
85
    }
86
87
    private void socketClose() {
88
        if (SystemUtils.IS_OS_WINDOWS) {
89
            if (randomAccessFile == null)
90
                return;
91
92
            try {
93
                randomAccessFile.close();
94
            } catch (IOException e) {
95
                e.printStackTrace();
96
            }
97
        } else {
98
            if (socketChannel == null)
99
                return;
100
101
            try {
102
                socketChannel.close();
103
            } catch (IOException e) {
104
                e.printStackTrace();
105
            }
106
        }
107
    }
108
109
    private void socketWrite(ByteBuffer buffer) {
110
        if (SystemUtils.IS_OS_WINDOWS) {
111
            try {
112
                randomAccessFile.write(buffer.array());
113
            } catch (IOException e) {
114
                if (mc.player != null)
115
                    Message.sendWarning("Can't connect to Discord!");
116
                this.toggle();
117
            }
118
        } else {
119
            try {
120
                socketChannel.write(buffer);
121
            } catch (IOException e) {
122
                if (mc.player != null)
123
                    Message.sendWarning("Can't connect to Discord!");
124
                this.toggle();
125
            }
126
        }
127
    }
128
129
    private void sendBuffer(int opcode, String jsonString) {
130
        byte[] jsonBytes = jsonString.getBytes(StandardCharsets.UTF_8);
131
132
        ByteBuffer buffer = ByteBuffer.allocate(jsonBytes.length + 8);
133
        buffer.putInt(Integer.reverseBytes(opcode));
134
        buffer.putInt(Integer.reverseBytes(jsonBytes.length));
135
        buffer.put(jsonBytes);
136
137
        buffer.rewind();
138
139
        socketWrite(buffer);
140
    }
141
142
    private void setActivity(String state, String details) {
143
        JsonObject activity = new JsonObject();
144
        if (!state.isBlank())
145
            activity.addProperty("state", state);
146
        activity.addProperty("details", details);
147
148
        JsonObject commandArgs = new JsonObject();
149
        commandArgs.addProperty("pid", ProcessHandle.current().pid());
150
        commandArgs.add("activity", activity);
151
152
        JsonObject command = new JsonObject();
153
        command.addProperty("nonce", UUID.randomUUID().toString());
154
        command.addProperty("cmd", "SET_ACTIVITY");
155
        command.add("args", commandArgs);
156
157
        String jsonString = command.toString();
158
159
        sendBuffer(1, jsonString);
160
    }
161
162
    private void handShake() {
163
        JsonObject handshake = new JsonObject();
164
        handshake.addProperty("v", 1);
165
        handshake.addProperty("client_id", APP_ID);
166
167
        sendBuffer(0, handshake.toString());
168
    }
169
170
    @Override
171
    public void toggle() {
172
        this.enabled = !this.enabled;
173
174
        if (!this.enabled) {
175
            socketClose();
176
        } else {
177
            if (!socketOpen()) {
178
                if (mc.player != null)
179
                    Message.sendWarning("Can't connect to Discord!");
180
                toggle();
181
                return;
182
            }
183
184
            handShake();
185
        }
186
    }
187
188
    @Override
189
    protected void init() {
190
        EventBus.register(ClientTickEvent.class, event -> {
191
            if (!this.enabled)
192
                return true;
193
194
            if (tickDelay <= 0) {
195
                String state = "";
196
                String details = "";
197
198
                if (mc.player == null)
199
                    details = "In main menu";
200
                else if (mc.hasSingleplayerServer())
201
                    details = "Playing single player";
202
                else if (currentServer != null)
203
                    details = "Playing on " + currentServer.ip;
204
205
                if (mc.player != null) {
206
                    if (player.enabled)
207
                        state += mc.player.getName().getString() + " | ";
208
209
                    if (dimension.enabled)
210
                        state += StringUtils.capitalize(mc.level.dimension().identifier().getPath()) + " | ";
211
212
                    if (state.length() > 3)
213
                        state = state.substring(0, state.length() - 3);
214
215
                    state = state.replace('_', ' ');
216
                }
217
218
                setActivity(state, details);
219
                tickDelay = 100;
220
            }
221
222
            tickDelay--;
223
224
225
            return true;
226
        });
227
    }
228
}
229