| 1 | package com.jellypudding.offlineclient.modules.misc;
|
| 2 |
|
| 3 | import com.jellypudding.offlineclient.event.Subscribe;
|
| 4 | import com.jellypudding.offlineclient.event.events.ClientTickEvent;
|
| 5 | import com.jellypudding.offlineclient.module.Category;
|
| 6 | import com.jellypudding.offlineclient.module.Module;
|
| 7 | import com.jellypudding.offlineclient.setting.BoolSetting;
|
| 8 | import com.jellypudding.offlineclient.setting.NumberSetting;
|
| 9 | import net.minecraft.client.gui.screens.ConnectScreen;
|
| 10 | import net.minecraft.client.gui.screens.DisconnectedScreen;
|
| 11 | import net.minecraft.client.multiplayer.ServerData;
|
| 12 | import net.minecraft.client.multiplayer.resolver.ServerAddress;
|
| 13 |
|
| 14 | public final class AutoReconnect extends Module {
|
| 15 |
|
| 16 | private final NumberSetting delay = new NumberSetting("Delay",
|
| 17 | "Seconds to wait before reconnecting.", 5, 0, 60, 0.1, "s").min(0).max(600);
|
| 18 | private final BoolSetting buttons = new BoolSetting("Buttons",
|
| 19 | "Adds a reconnect button and a switch to the disconnected screen.", true);
|
| 20 |
|
| 21 | // The last server joined. Kept even whilst the module is off. Switched on
|
| 22 | // after a disconnect it still knows where to go.
|
| 23 | private static ServerData lastServer;
|
| 24 |
|
| 25 | private int countdown = -1;
|
| 26 |
|
| 27 | public AutoReconnect() {
|
| 28 | super("AutoReconnect", "Rejoins the server after you get disconnected.", Category.MISC);
|
| 29 | addSettings(delay, buttons);
|
| 30 | searchTags("rejoin", "reconnect");
|
| 31 | }
|
| 32 |
|
| 33 | // Called from the connect screen whatever the module is doing.
|
| 34 | public static void remember(ServerData server) {
|
| 35 | if (server != null) {
|
| 36 | lastServer = server;
|
| 37 | }
|
| 38 | }
|
| 39 |
|
| 40 | public boolean showsButtons() {
|
| 41 | return buttons.isOn();
|
| 42 | }
|
| 43 |
|
| 44 | // Seconds left rounded to one place. Below zero whilst nothing is counting.
|
| 45 | public double secondsLeft() {
|
| 46 | return countdown < 0 ? -1 : Math.round(countdown / 2f) / 10.0;
|
| 47 | }
|
| 48 |
|
| 49 | public static boolean canReconnect() {
|
| 50 | return lastServer != null;
|
| 51 | }
|
| 52 |
|
| 53 | public void reconnectNow() {
|
| 54 | countdown = -1;
|
| 55 | connect();
|
| 56 | }
|
| 57 |
|
| 58 | // The button on the screen flips the module and starts the wait again.
|
| 59 | public void toggleFromScreen() {
|
| 60 | countdown = -1;
|
| 61 | toggle();
|
| 62 | }
|
| 63 |
|
| 64 | @Subscribe
|
| 65 | private void onClientTick(ClientTickEvent event) {
|
| 66 | if (mc.level != null && mc.getCurrentServer() != null) {
|
| 67 | remember(mc.getCurrentServer());
|
| 68 | countdown = -1;
|
| 69 | return;
|
| 70 | }
|
| 71 |
|
| 72 | if (!(mc.gui.screen() instanceof DisconnectedScreen) || lastServer == null) {
|
| 73 | countdown = -1;
|
| 74 | return;
|
| 75 | }
|
| 76 |
|
| 77 | if (countdown == -1) {
|
| 78 | countdown = (int) Math.round(delay.getValue() * 20);
|
| 79 | }
|
| 80 | countdown--;
|
| 81 | if (countdown > 0) {
|
| 82 | return;
|
| 83 | }
|
| 84 |
|
| 85 | countdown = -1;
|
| 86 | connect();
|
| 87 | }
|
| 88 |
|
| 89 | private void connect() {
|
| 90 | if (lastServer == null || mc.gui.screen() == null) {
|
| 91 | return;
|
| 92 | }
|
| 93 | ConnectScreen.startConnecting(mc.gui.screen(), mc,
|
| 94 | ServerAddress.parseString(lastServer.ip), lastServer, false, null);
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | protected void onDisable() {
|
| 99 | countdown = -1;
|
| 100 | }
|
| 101 | }
|
| 102 |
|