|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2026 Wurst-Imperium and contributors. |
| 3 | + * |
| 4 | + * This source code is subject to the terms of the GNU General Public |
| 5 | + * License, version 3. If a copy of the GPL was not distributed with this |
| 6 | + * file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt |
| 7 | + */ |
| 8 | +package net.wurstclient.hacks; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import net.wurstclient.Category; |
| 18 | +import net.wurstclient.SearchTags; |
| 19 | +import net.wurstclient.events.UpdateListener; |
| 20 | +import net.wurstclient.hack.DontSaveState; |
| 21 | +import net.wurstclient.hack.Hack; |
| 22 | +import net.wurstclient.settings.CheckboxSetting; |
| 23 | +import net.wurstclient.settings.FileSetting; |
| 24 | +import net.wurstclient.settings.SliderSetting; |
| 25 | +import net.wurstclient.settings.SliderSetting.ValueDisplay; |
| 26 | +import net.wurstclient.settings.TextFieldSetting; |
| 27 | +import net.wurstclient.util.ChatUtils; |
| 28 | + |
| 29 | +@SearchTags({"chat spam", "chatspam", "spam chat", "message spam"}) |
| 30 | +@DontSaveState |
| 31 | +public final class ChatSpamHack extends Hack implements UpdateListener |
| 32 | +{ |
| 33 | + private static final int CHAT_LIMIT = 256; |
| 34 | + |
| 35 | + private final TextFieldSetting message = new TextFieldSetting("Message", |
| 36 | + "Chat message to send repeatedly when file mode is disabled.", "", |
| 37 | + s -> s != null && s.length() <= CHAT_LIMIT); |
| 38 | + |
| 39 | + private final SliderSetting amount = |
| 40 | + new SliderSetting("Amount", "How many times to send the message.", 10, |
| 41 | + 1, 10000, 1, ValueDisplay.INTEGER); |
| 42 | + |
| 43 | + private final SliderSetting pause = |
| 44 | + new SliderSetting("Pause", "Seconds to wait between messages.", 1, 0, |
| 45 | + 60, 0.05, ValueDisplay.DECIMAL.withSuffix("s")); |
| 46 | + |
| 47 | + private final CheckboxSetting useFile = new CheckboxSetting("Use file", |
| 48 | + "Send each line from the selected text file instead of the Message field.", |
| 49 | + false); |
| 50 | + |
| 51 | + private final FileSetting textFile = new FileSetting("File", |
| 52 | + "Select a text file to read line by line.", "chatspam", folder -> { |
| 53 | + try |
| 54 | + { |
| 55 | + Files.createDirectories(folder); |
| 56 | + Path file = folder.resolve("messages.txt"); |
| 57 | + if(Files.notExists(file)) |
| 58 | + { |
| 59 | + Files.writeString(file, |
| 60 | + "Hello from ChatSpam!\n" + "This is the second line.\n", |
| 61 | + StandardCharsets.UTF_8); |
| 62 | + } |
| 63 | + }catch(IOException e) |
| 64 | + { |
| 65 | + throw new RuntimeException(e); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + private final ArrayList<String> pendingMessages = new ArrayList<>(); |
| 70 | + private int currentIndex; |
| 71 | + private int timer; |
| 72 | + private boolean fileMode; |
| 73 | + |
| 74 | + public ChatSpamHack() |
| 75 | + { |
| 76 | + super("ChatSpam"); |
| 77 | + setCategory(Category.CHAT); |
| 78 | + addSetting(message); |
| 79 | + addSetting(amount); |
| 80 | + addSetting(pause); |
| 81 | + addSetting(useFile); |
| 82 | + addSetting(textFile); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public String getRenderName() |
| 87 | + { |
| 88 | + if(!isEnabled()) |
| 89 | + return getName(); |
| 90 | + |
| 91 | + int total = getTotalMessages(); |
| 92 | + return getName() + " [" + Math.min(currentIndex, total) + "/" + total |
| 93 | + + "]"; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public String getStatusText() |
| 98 | + { |
| 99 | + if(!isEnabled()) |
| 100 | + return null; |
| 101 | + |
| 102 | + int total = getTotalMessages(); |
| 103 | + return Math.min(currentIndex, total) + "/" + total; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + protected void onEnable() |
| 108 | + { |
| 109 | + timer = 0; |
| 110 | + currentIndex = 0; |
| 111 | + fileMode = useFile.isChecked(); |
| 112 | + pendingMessages.clear(); |
| 113 | + |
| 114 | + if(fileMode) |
| 115 | + { |
| 116 | + if(!loadMessagesFromFile()) |
| 117 | + { |
| 118 | + setEnabled(false); |
| 119 | + return; |
| 120 | + } |
| 121 | + }else |
| 122 | + { |
| 123 | + String configuredMessage = normalizeMessage(message.getValue()); |
| 124 | + if(configuredMessage.isEmpty()) |
| 125 | + { |
| 126 | + ChatUtils.error("ChatSpam: enter a message first."); |
| 127 | + setEnabled(false); |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + pendingMessages.add(configuredMessage); |
| 132 | + } |
| 133 | + |
| 134 | + EVENTS.add(UpdateListener.class, this); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + protected void onDisable() |
| 139 | + { |
| 140 | + EVENTS.remove(UpdateListener.class, this); |
| 141 | + pendingMessages.clear(); |
| 142 | + currentIndex = 0; |
| 143 | + timer = 0; |
| 144 | + fileMode = false; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public void onUpdate() |
| 149 | + { |
| 150 | + if(MC.player == null || MC.getConnection() == null) |
| 151 | + return; |
| 152 | + |
| 153 | + if(timer > 0) |
| 154 | + { |
| 155 | + timer--; |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + int total = getTotalMessages(); |
| 160 | + if(currentIndex >= total) |
| 161 | + { |
| 162 | + setEnabled(false); |
| 163 | + return; |
| 164 | + } |
| 165 | + |
| 166 | + String chatMessage = fileMode ? pendingMessages.get(currentIndex) |
| 167 | + : pendingMessages.get(0); |
| 168 | + if(chatMessage == null || chatMessage.isBlank()) |
| 169 | + { |
| 170 | + currentIndex++; |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + MC.getConnection().sendChat(chatMessage); |
| 175 | + currentIndex++; |
| 176 | + |
| 177 | + if(currentIndex >= total) |
| 178 | + { |
| 179 | + setEnabled(false); |
| 180 | + return; |
| 181 | + } |
| 182 | + |
| 183 | + timer = getPauseTicks(); |
| 184 | + } |
| 185 | + |
| 186 | + private int getTotalMessages() |
| 187 | + { |
| 188 | + if(fileMode) |
| 189 | + return pendingMessages.size(); |
| 190 | + |
| 191 | + return amount.getValueI(); |
| 192 | + } |
| 193 | + |
| 194 | + private int getPauseTicks() |
| 195 | + { |
| 196 | + return Math.max(0, (int)Math.round(pause.getValue() * 20)); |
| 197 | + } |
| 198 | + |
| 199 | + private boolean loadMessagesFromFile() |
| 200 | + { |
| 201 | + Path selectedFile = textFile.getSelectedFile(); |
| 202 | + if(selectedFile == null) |
| 203 | + { |
| 204 | + ChatUtils.error("ChatSpam: no file selected."); |
| 205 | + return false; |
| 206 | + } |
| 207 | + |
| 208 | + List<String> lines; |
| 209 | + try |
| 210 | + { |
| 211 | + lines = Files.readAllLines(selectedFile, StandardCharsets.UTF_8); |
| 212 | + }catch(IOException e) |
| 213 | + { |
| 214 | + ChatUtils.error( |
| 215 | + "ChatSpam: couldn't read " + selectedFile.getFileName() + "."); |
| 216 | + return false; |
| 217 | + } |
| 218 | + |
| 219 | + for(String line : lines) |
| 220 | + { |
| 221 | + String normalized = normalizeMessage(line); |
| 222 | + if(!normalized.isEmpty()) |
| 223 | + pendingMessages.add(normalized); |
| 224 | + } |
| 225 | + |
| 226 | + if(pendingMessages.isEmpty()) |
| 227 | + { |
| 228 | + ChatUtils.error("ChatSpam: the selected file has no messages."); |
| 229 | + return false; |
| 230 | + } |
| 231 | + |
| 232 | + return true; |
| 233 | + } |
| 234 | + |
| 235 | + private static String normalizeMessage(String message) |
| 236 | + { |
| 237 | + if(message == null) |
| 238 | + return ""; |
| 239 | + |
| 240 | + String trimmed = message.replace("\r", ""); |
| 241 | + if(trimmed.length() > CHAT_LIMIT) |
| 242 | + trimmed = trimmed.substring(0, CHAT_LIMIT); |
| 243 | + |
| 244 | + return trimmed; |
| 245 | + } |
| 246 | +} |
0 commit comments