| 1 | package com.jellypudding.offlineclient.modules.misc;
|
| 2 |
|
| 3 | import com.jellypudding.offlineclient.event.Subscribe;
|
| 4 | import com.jellypudding.offlineclient.event.events.PacketReceiveEvent;
|
| 5 | import com.jellypudding.offlineclient.event.events.TickEvent;
|
| 6 | import com.jellypudding.offlineclient.module.Category;
|
| 7 | import com.jellypudding.offlineclient.module.Module;
|
| 8 | import com.jellypudding.offlineclient.setting.BoolSetting;
|
| 9 | import com.jellypudding.offlineclient.setting.NumberSetting;
|
| 10 | import com.jellypudding.offlineclient.setting.TextSetting;
|
| 11 | import com.jellypudding.offlineclient.util.ChatUtil;
|
| 12 | import net.minecraft.client.multiplayer.PlayerInfo;
|
| 13 | import net.minecraft.network.protocol.game.ClientboundSystemChatPacket;
|
| 14 |
|
| 15 | import java.util.ArrayList;
|
| 16 | import java.util.Collections;
|
| 17 | import java.util.List;
|
| 18 | import java.util.Locale;
|
| 19 |
|
| 20 | // Asks everyone online for a teleport one after another. Whoever says yes first wins.
|
| 21 | public final class MassTpa extends Module {
|
| 22 |
|
| 23 | private final TextSetting command = new TextSetting("Command",
|
| 24 | "The teleport command such as tpa or tpahere. Click to type it.", "tpa");
|
| 25 | private final NumberSetting delay = new NumberSetting("Delay",
|
| 26 | "Ticks between one request and the next.", 20, 1, 200, 1, " ticks").min(1);
|
| 27 | private final BoolSetting ignoreErrors = new BoolSetting("Ignore errors",
|
| 28 | "Keeps going when the server says the command does not exist.", false);
|
| 29 | private final BoolSetting stopWhenAccepted = new BoolSetting("Stop when accepted",
|
| 30 | "Stops asking once somebody accepts a request.", true);
|
| 31 |
|
| 32 | private final List<String> players = new ArrayList<>();
|
| 33 | private int next;
|
| 34 | private int timer;
|
| 35 |
|
| 36 | public MassTpa() {
|
| 37 | super("MassTPA", "Sends a teleport request to every player on the server.", Category.MISC);
|
| 38 | addSettings(command, delay, ignoreErrors, stopWhenAccepted);
|
| 39 | searchTags("mass tpa", "teleport request", "tpa spam");
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public boolean savesEnabledState() {
|
| 44 | return false;
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public String getSuffix() {
|
| 49 | return players.isEmpty() ? null : next + " of " + players.size();
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | protected void onEnable() {
|
| 54 | players.clear();
|
| 55 | next = 0;
|
| 56 | timer = 0;
|
| 57 | if (!inGame()) {
|
| 58 | setEnabled(false);
|
| 59 | return;
|
| 60 | }
|
| 61 | String own = mc.getUser().getName();
|
| 62 | for (PlayerInfo info : mc.getConnection().getOnlinePlayers()) {
|
| 63 | String name = info.getProfile().name();
|
| 64 | if (!name.equalsIgnoreCase(own)) {
|
| 65 | players.add(name);
|
| 66 | }
|
| 67 | }
|
| 68 | Collections.shuffle(players);
|
| 69 | if (players.isEmpty()) {
|
| 70 | ChatUtil.error("Nobody else is online.");
|
| 71 | setEnabled(false);
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | private String commandName() {
|
| 76 | String name = command.getValue().trim();
|
| 77 | return name.startsWith("/") ? name.substring(1) : name;
|
| 78 | }
|
| 79 |
|
| 80 | @Subscribe
|
| 81 | private void onTick(TickEvent event) {
|
| 82 | if (!inGame() || timer-- > 0) {
|
| 83 | return;
|
| 84 | }
|
| 85 | if (next >= players.size()) {
|
| 86 | ChatUtil.message("Every player has been asked.");
|
| 87 | setEnabled(false);
|
| 88 | return;
|
| 89 | }
|
| 90 | mc.getConnection().sendCommand(commandName() + " " + players.get(next++));
|
| 91 | timer = delay.getInt() - 1;
|
| 92 | }
|
| 93 |
|
| 94 | // Fired on the netty thread. The reply from the server says how it went.
|
| 95 | @Subscribe
|
| 96 | private void onPacketReceive(PacketReceiveEvent event) {
|
| 97 | if (!(event.getPacket() instanceof ClientboundSystemChatPacket packet)) {
|
| 98 | return;
|
| 99 | }
|
| 100 | String text = packet.content().getString().toLowerCase(Locale.ROOT);
|
| 101 | if (!ignoreErrors.isOn() && (text.contains("/help") || text.contains("permission"))) {
|
| 102 | mc.schedule(() -> stop("This server has no " + commandName() + " command."));
|
| 103 | } else if (stopWhenAccepted.isOn() && text.contains("accepted") && text.contains("request")) {
|
| 104 | mc.schedule(() -> stop("Somebody accepted the request."));
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | private void stop(String why) {
|
| 109 | if (isEnabled()) {
|
| 110 | ChatUtil.message(why);
|
| 111 | setEnabled(false);
|
| 112 | }
|
| 113 | }
|
| 114 | }
|
| 115 |
|