First commit

This commit is contained in:
Bstn1802
2020-09-03 21:12:10 +02:00
commit 14563be594
18 changed files with 781 additions and 0 deletions

View 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();
}
}

View File

@ -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);
}
}

View File

@ -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));
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 382 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 388 KiB

View File

@ -0,0 +1,15 @@
{
"required": true,
"minVersion": "0.8",
"package": "net.autoreconnect.mixin",
"compatibilityLevel": "JAVA_8",
"mixins": [
],
"client": [
"MixinDisconnectedScreen",
"MixinMinecraftClient"
],
"injectors": {
"defaultRequire": 1
}
}

View File

@ -0,0 +1,37 @@
{
"schemaVersion": 1,
"id": "autoreconnect",
"version": "1.0.0",
"name": "Example Mod",
"description": "This is an example description! Tell everyone what your mod is about!",
"authors": [
"Bstn1802"
],
"contact": {
"homepage": "https://github.com/Bstn1802",
"sources": "https://github.com/Bstn1802/AutoReconnect"
},
"license": "CC0-1.0",
"icon": "assets/modid/icon.png",
"environment": "*",
"entrypoints": {
"main": [
"net.autoreconnect.AutoReconnect"
]
},
"mixins": [
"autoreconnect.mixins.json"
],
"depends": {
"fabricloader": ">=0.7.4",
"fabric": "*",
"minecraft": "1.16.x"
},
"suggests": {
"flamingo": "*"
}
}