First commit
This commit is contained in:
56
src/main/java/net/autoreconnect/AutoReconnect.java
Normal file
56
src/main/java/net/autoreconnect/AutoReconnect.java
Normal file
@ -0,0 +1,56 @@
|
||||
package net.autoreconnect;
|
||||
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class AutoReconnect implements ModInitializer
|
||||
{
|
||||
public static int attempt = 0;
|
||||
public static boolean connect = false;
|
||||
|
||||
private static final AtomicInteger countdown = new AtomicInteger();
|
||||
private static Timer timer = null;
|
||||
|
||||
@Override
|
||||
public void onInitialize() { }
|
||||
|
||||
public static void startCountdown(int seconds)
|
||||
{
|
||||
countdown.set(seconds);
|
||||
timer = new Timer();
|
||||
timer.scheduleAtFixedRate(new TimerTask()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
if (countdown.decrementAndGet() <= 0)
|
||||
{
|
||||
connect = true;
|
||||
cancel();
|
||||
}
|
||||
}
|
||||
}, 1000, 1000);
|
||||
}
|
||||
|
||||
private static void cancel()
|
||||
{
|
||||
if (timer == null) return;
|
||||
timer.cancel();
|
||||
timer = null;
|
||||
}
|
||||
|
||||
public static void reset()
|
||||
{
|
||||
cancel();
|
||||
attempt = 0;
|
||||
connect = false;
|
||||
}
|
||||
|
||||
public static int getCountdown()
|
||||
{
|
||||
return countdown.get();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
package net.autoreconnect.mixin;
|
||||
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.gui.DrawableHelper;
|
||||
import net.minecraft.client.gui.screen.DisconnectedScreen;
|
||||
import net.minecraft.client.util.Window;
|
||||
import net.minecraft.client.util.math.MatrixStack;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
import static net.autoreconnect.AutoReconnect.attempt;
|
||||
import static net.autoreconnect.AutoReconnect.getCountdown;
|
||||
|
||||
@Mixin(DisconnectedScreen.class)
|
||||
public class MixinDisconnectedScreen
|
||||
{
|
||||
@Inject(at = @At("RETURN"), method = "shouldCloseOnEsc", cancellable = true)
|
||||
private void shouldCloseOnEsc(CallbackInfoReturnable<Boolean> info)
|
||||
{
|
||||
info.setReturnValue(true);
|
||||
}
|
||||
|
||||
@Inject(at = @At("RETURN"), method = "render")
|
||||
private void render(MatrixStack matrices, int mouseX, int mouseY, float delta, CallbackInfo info)
|
||||
{
|
||||
Window window = MinecraftClient.getInstance().getWindow();
|
||||
DrawableHelper.drawCenteredString(matrices,
|
||||
MinecraftClient.getInstance().textRenderer,
|
||||
attempt == -1 ? "Can not reconnect!" : "Reconnecting in " + getCountdown() + "...",
|
||||
window.getScaledWidth() / 2,
|
||||
window.getScaledHeight() / 4,
|
||||
0xFF4422);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,78 @@
|
||||
package net.autoreconnect.mixin;
|
||||
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.gui.screen.*;
|
||||
import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen;
|
||||
import net.minecraft.client.network.ServerInfo;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
import static net.autoreconnect.AutoReconnect.*;
|
||||
|
||||
@Mixin(MinecraftClient.class)
|
||||
public class MixinMinecraftClient
|
||||
{
|
||||
private ServerInfo lastServerEntry;
|
||||
|
||||
@Inject(at = @At("HEAD"), method = "setCurrentServerEntry")
|
||||
private void setCurrentServerEntry(ServerInfo info, CallbackInfo ci)
|
||||
{
|
||||
if (info != null)
|
||||
{
|
||||
lastServerEntry = info;
|
||||
}
|
||||
}
|
||||
|
||||
@Inject(at = @At("RETURN"), method = "openScreen")
|
||||
private void openScreen(Screen screen, CallbackInfo info)
|
||||
{
|
||||
System.out.println(screen == null ? null : screen.getClass().getSimpleName());
|
||||
//TODO interpret disconnect reason
|
||||
if (screen instanceof DisconnectedScreen)
|
||||
{
|
||||
if (attempt < 0) return;
|
||||
switch (attempt++)
|
||||
{
|
||||
case 0:
|
||||
startCountdown(3);
|
||||
break;
|
||||
case 1:
|
||||
startCountdown(10);
|
||||
break;
|
||||
case 2:
|
||||
startCountdown(60);
|
||||
break;
|
||||
case 3:
|
||||
startCountdown(300);
|
||||
break;
|
||||
default:
|
||||
attempt = -1;
|
||||
}
|
||||
}
|
||||
else if (screen instanceof MultiplayerScreen || MinecraftClient.getInstance().player != null)
|
||||
{
|
||||
//TODO find better conditions to reset
|
||||
reset();
|
||||
}
|
||||
}
|
||||
|
||||
@Inject(at = @At("RETURN"), method = "tick")
|
||||
private void tick(CallbackInfo info)
|
||||
{
|
||||
//TODO find better way to call connect on main thread from after timer countdown finished
|
||||
if (connect)
|
||||
{
|
||||
connect = false;
|
||||
MinecraftClient mc = MinecraftClient.getInstance();
|
||||
if (lastServerEntry == null)
|
||||
{
|
||||
attempt = -1;
|
||||
return;
|
||||
}
|
||||
mc.disconnect();
|
||||
mc.openScreen(new ConnectScreen(new TitleScreen(), mc, lastServerEntry));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user