diff --git a/src/main/java/com/minecrafttas/mctcommon/networking/Client.java b/src/main/java/com/minecrafttas/mctcommon/networking/Client.java index c8989a1f..53641469 100644 --- a/src/main/java/com/minecrafttas/mctcommon/networking/Client.java +++ b/src/main/java/com/minecrafttas/mctcommon/networking/Client.java @@ -14,6 +14,7 @@ import java.nio.channels.CompletionHandler; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Marker; @@ -53,9 +54,9 @@ public class Client { */ private boolean local = false; - public enum Side { - CLIENT, SERVER; + CLIENT, + SERVER; } /** @@ -71,7 +72,7 @@ public enum Side { public Client(String host, int port, PacketID[] packetIDs, String name, boolean local) throws Exception { LOGGER.info(Client, "Connecting server to {}:{}", host, port); this.socket = AsynchronousSocketChannel.open(); - this.socket.connect(new InetSocketAddress(host, port)).get(); + this.socket.connect(new InetSocketAddress(host, port)).get(2, TimeUnit.SECONDS); this.socket.setOption(StandardSocketOptions.SO_KEEPALIVE, true); this.socket.setOption(StandardSocketOptions.TCP_NODELAY, true); @@ -173,7 +174,7 @@ public void completed(Integer result, Object attachment) { readBuffer.clear().limit(4); socket.read(readBuffer, null, this); } catch (Throwable exc) { - if(exc instanceof ExecutionException && !isClosed()) { + if (exc instanceof ExecutionException && !isClosed()) { LOGGER.debug(getLoggerMarker(), "{} terminated the connection!", getOppositeSide().name()); try { close(); @@ -189,7 +190,7 @@ public void completed(Integer result, Object attachment) { @Override public void failed(Throwable exc, Object attachment) { if (exc instanceof AsynchronousCloseException || exc instanceof IOException) { - if(isClosed()) { + if (isClosed()) { return; } LOGGER.debug(getLoggerMarker(), "{} terminated the connection!", getOppositeSide().name()); @@ -199,7 +200,7 @@ public void failed(Throwable exc, Object attachment) { LOGGER.error(getLoggerMarker(), "Attempted to close connection but failed", e); } } else { - if(isClosed()) { + if (isClosed()) { return; } LOGGER.error(getLoggerMarker(), "Something went wrong, terminating connection!", exc); @@ -221,7 +222,7 @@ public void failed(Throwable exc, Object attachment) { * @throws Exception Networking exception */ public void send(ByteBufferBuilder bufferBuilder) throws Exception { - if(bufferBuilder.getPacketID() != null && bufferBuilder.getPacketID().shouldTrace()) + if (bufferBuilder.getPacketID() != null && bufferBuilder.getPacketID().shouldTrace()) LOGGER.trace(getLoggerMarker(), "Sending a {} packet to the {} with content:\n{}", bufferBuilder.getPacketID(), getOppositeSide(), bufferBuilder.getPacketContent()); // wait for previous buffer to send if (this.future != null && !this.future.isDone()) @@ -264,7 +265,7 @@ private void close() throws IOException { private Marker getLoggerMarker() { return side == Side.CLIENT ? Client : Server; } - + private Side getOppositeSide() { return side == Side.CLIENT ? Side.SERVER : Side.CLIENT; } diff --git a/src/main/java/com/minecrafttas/tasmod/TASmodClient.java b/src/main/java/com/minecrafttas/tasmod/TASmodClient.java index ffd81dae..b4edba1c 100644 --- a/src/main/java/com/minecrafttas/tasmod/TASmodClient.java +++ b/src/main/java/com/minecrafttas/tasmod/TASmodClient.java @@ -6,6 +6,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; +import java.util.concurrent.TimeoutException; import org.apache.logging.log4j.Level; @@ -243,11 +244,12 @@ public void onPlayerJoinedClientSide(EntityPlayerSP player) { try { // connect to server and authenticate client = new Client(IP, PORT, TASmodPackets.values(), mc.getSession().getUsername(), local); + } catch (TimeoutException e) { + mc.getConnection().getNetworkManager().closeChannel(null); } catch (Exception e) { LOGGER.error("Unable to connect TASmod client: {}", e.getMessage()); e.printStackTrace(); } - ticksyncClient.setEnabled(true); }); } } @@ -298,7 +300,6 @@ private void initializeCustomPacketHandler() { } catch (Exception e) { LOGGER.error("Unable to connect TASmod client: {}", e); } - ticksyncClient.setEnabled(true); } } diff --git a/src/main/java/com/minecrafttas/tasmod/gui/GuiMultiplayerTimeOut.java b/src/main/java/com/minecrafttas/tasmod/gui/GuiMultiplayerTimeOut.java deleted file mode 100644 index a36a7c96..00000000 --- a/src/main/java/com/minecrafttas/tasmod/gui/GuiMultiplayerTimeOut.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.minecrafttas.tasmod.gui; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.GuiButton; -import net.minecraft.client.gui.GuiMainMenu; -import net.minecraft.client.gui.GuiMultiplayer; -import net.minecraft.client.gui.GuiScreen; -import net.minecraft.client.gui.ScaledResolution; -import net.minecraft.client.resources.I18n; - -public class GuiMultiplayerTimeOut extends GuiScreen { - private GuiScreen previous; - - public GuiMultiplayerTimeOut() { - previous = new GuiMainMenu(); - } - - @Override - public void initGui() { - this.buttonList.add(new GuiButton(0, width / 2 - 100, height / 2 + 70, "Continue")); - super.initGui(); - } - - @Override - public void drawScreen(int mouseX, int mouseY, float partialTicks) { - this.drawDefaultBackground(); - - ScaledResolution scaled = new ScaledResolution(Minecraft.getMinecraft()); - int width = scaled.getScaledWidth(); - int height = scaled.getScaledHeight(); - - drawCenteredString(fontRenderer, I18n.format("TASmod: Timed out"), width / 2, height / 4 + 50 + -16, 0xFFFFFF); - drawCenteredString(fontRenderer, I18n.format("Lost or could not make a connection to the TASmod on the server side"), width / 2, height / 4 + 50 + -6, 0xFFFFFF); - drawCenteredString(fontRenderer, I18n.format("Possible Cause:"), width / 2, height / 4 + 50 + 14, 0xFFFFFF); - drawCenteredString(fontRenderer, I18n.format("The server has no TASmod installed or the server lagged too much."), width / 2, height / 4 + 50 + 24, 0xFFFFFF); - drawCenteredString(fontRenderer, I18n.format("It's also possible to get this message in singleplayer if the integrated server stopped responding."), width / 2, height / 4 + 50 + 34, 0xFFFFFF); - super.drawScreen(mouseX, mouseY, partialTicks); - } - - @Override - protected void actionPerformed(GuiButton button) { - if (button.id == 0) { - Minecraft.getMinecraft().displayGuiScreen(new GuiMultiplayer(previous)); - } - } -} diff --git a/src/main/java/com/minecrafttas/tasmod/gui/GuiMultiplayerWarn.java b/src/main/java/com/minecrafttas/tasmod/gui/GuiMultiplayerWarn.java deleted file mode 100644 index e079ed48..00000000 --- a/src/main/java/com/minecrafttas/tasmod/gui/GuiMultiplayerWarn.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.minecrafttas.tasmod.gui; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.GuiButton; -import net.minecraft.client.gui.GuiMultiplayer; -import net.minecraft.client.gui.GuiScreen; -import net.minecraft.client.gui.ScaledResolution; -import net.minecraft.client.resources.I18n; - -public class GuiMultiplayerWarn extends GuiScreen { - private GuiScreen previous; - - public GuiMultiplayerWarn(GuiScreen screen) { - previous = screen; - } - - @Override - public void initGui() { - this.buttonList.add(new GuiButton(0, width / 2 - 100, height / 2 + 70, "Continue")); - super.initGui(); - } - - @Override - public void drawScreen(int mouseX, int mouseY, float partialTicks) { - this.drawDefaultBackground(); - - ScaledResolution scaled = new ScaledResolution(Minecraft.getMinecraft()); - int width = scaled.getScaledWidth(); - int height = scaled.getScaledHeight(); - - drawCenteredString(fontRenderer, I18n.format("WARNING"), width / 2, height / 4 + 50 + -16, 0xCE0000); - drawCenteredString(fontRenderer, I18n.format("Do NOT join a server that has not installed the TASmod (e.g. Hypixel)."), width / 2, height / 4 + 50 + -6, 0xFFFFFF); - drawCenteredString(fontRenderer, I18n.format("You will softlock your game for a few seconds then disconnect!"), width / 2, height / 4 + 50 + 4, 0xFFFFFF); - drawCenteredString(fontRenderer, I18n.format("This mod only works together with a server."), width / 2, height / 4 + 50 + 14, 0xFFFFFF); - - super.drawScreen(mouseX, mouseY, partialTicks); - } - - @Override - protected void actionPerformed(GuiButton button) { - if (button.id == 0) { - Minecraft.getMinecraft().displayGuiScreen(new GuiMultiplayer(previous)); - } - } -} diff --git a/src/main/java/com/minecrafttas/tasmod/mixin/MixinTimer.java b/src/main/java/com/minecrafttas/tasmod/mixin/MixinTimer.java index 9462ad79..518e3483 100644 --- a/src/main/java/com/minecrafttas/tasmod/mixin/MixinTimer.java +++ b/src/main/java/com/minecrafttas/tasmod/mixin/MixinTimer.java @@ -1,6 +1,5 @@ package com.minecrafttas.tasmod.mixin; - import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -21,36 +20,36 @@ * */ public class MixinTimer { - + @Shadow - private int elapsedTicks; + private int elapsedTicks; @Shadow - private float renderPartialTicks; + private float renderPartialTicks; @Shadow - private float elapsedPartialTicks; + private float elapsedPartialTicks; @Shadow - private long lastSyncSysClock; + private long lastSyncSysClock; @Shadow - private float tickLength; - + private float tickLength; + @Unique private long millisLastTick; @Unique private long lastGameLoop; @Unique private float lastTickDuration; - + @Inject(method = "updateTimer", at = @At("HEAD"), cancellable = true) public void inject_tick(CallbackInfo ci) { - if (TASmodClient.client != null && !TASmodClient.client.isClosed() && TASmodClient.ticksyncClient.isEnabled()) { + if (TASmodClient.client != null && !TASmodClient.client.isClosed()) { lastSyncSysClock = Minecraft.getSystemTime(); // update the tick tracker so that after returning to scheduling the client won't catch up all ticks (max 10) this.elapsedTicks = 0; // do not do any ticks long newGameLoop = Minecraft.getSystemTime(); if (TickSyncClient.shouldTick.compareAndSet(true, false)) { this.elapsedTicks++; this.lastTickDuration = newGameLoop - this.millisLastTick; - if(TASmodClient.tickratechanger.advanceTick) { - lastTickDuration = TASmodClient.tickratechanger.millisecondsPerTick; // Keep the lastTick duration steady during tickadvance, since it grows larger the longer you wait in tickrate 0 + if (TASmodClient.tickratechanger.advanceTick) { + lastTickDuration = TASmodClient.tickratechanger.millisecondsPerTick; // Keep the lastTick duration steady during tickadvance, since it grows larger the longer you wait in tickrate 0 } this.millisLastTick = newGameLoop; // Update millisLastTick this.renderPartialTicks = 0; // Reset after the tick diff --git a/src/main/java/com/minecrafttas/tasmod/mixin/events/MixinGuiMainMenu.java b/src/main/java/com/minecrafttas/tasmod/mixin/events/MixinGuiMainMenu.java deleted file mode 100644 index 6a3e847b..00000000 --- a/src/main/java/com/minecrafttas/tasmod/mixin/events/MixinGuiMainMenu.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.minecrafttas.tasmod.mixin.events; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; - -import com.minecrafttas.tasmod.gui.GuiMultiplayerWarn; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.GuiMainMenu; -import net.minecraft.client.gui.GuiScreen; - -@Mixin(GuiMainMenu.class) -public class MixinGuiMainMenu extends GuiScreen { - - @Redirect(method = "actionPerformed", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Minecraft;displayGuiScreen(Lnet/minecraft/client/gui/GuiScreen;)V", ordinal = 3)) - public void redirect_openGuiMultiplayer(Minecraft mc, GuiScreen screen) { - mc.displayGuiScreen(new GuiMultiplayerWarn((GuiMainMenu)(Object)this)); - } -} diff --git a/src/main/java/com/minecrafttas/tasmod/ticksync/TickSyncClient.java b/src/main/java/com/minecrafttas/tasmod/ticksync/TickSyncClient.java index 0b93279e..2faddab1 100644 --- a/src/main/java/com/minecrafttas/tasmod/ticksync/TickSyncClient.java +++ b/src/main/java/com/minecrafttas/tasmod/ticksync/TickSyncClient.java @@ -25,8 +25,6 @@ public class TickSyncClient implements ClientPacketHandler, EventClientTickPost public static final AtomicBoolean shouldTick = new AtomicBoolean(true); - private boolean enabled = false; - @Override public PacketID[] getAcceptedPacketIDs() { return new TASmodPackets[] { TASmodPackets.TICKSYNC }; @@ -52,7 +50,7 @@ public void onClientPacket(PacketID id, ByteBuffer buf, String username) { */ @Override public void onClientTickPost(Minecraft mc) { - if (TASmodClient.client == null || TASmodClient.client.isClosed() || !enabled) { + if (TASmodClient.client == null || TASmodClient.client.isClosed()) { return; } @@ -62,12 +60,4 @@ public void onClientTickPost(Minecraft mc) { LOGGER.error("Unable to send packet to server:", e); } } - - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } } diff --git a/src/main/resources/tasmod.mixin.json b/src/main/resources/tasmod.mixin.json index 25231ee5..27430db4 100644 --- a/src/main/resources/tasmod.mixin.json +++ b/src/main/resources/tasmod.mixin.json @@ -41,7 +41,6 @@ "MixinTextfield", // Events - "events.MixinGuiMainMenu", "events.MixinGuiIngame", "events.MixinEntityRenderer",