diff --git a/README.md b/README.md index fee7af4..48486aa 100644 --- a/README.md +++ b/README.md @@ -11,10 +11,21 @@ With AutoLan, your singleplayer world will automatically open to LAN whenever yo # How to use Just install the mod, and whenever you open a singleplayer world, AutoLan will start working automatically -## Suggested mod +# Note +As mentioned in the changelog, cheats are always enabled when opening to LAN. This is because I was not able to find a reproducible way to get whether cheats were enabled or not. This is to be addressed in a future update. + +(Not that it's legal for Minecraft speedrunning anyway.) + +# Suggested mod I have another mod, [Force Port](https://modrinth.com/mod/forceport), which allows you to configure which port you want your LAN server to run on. This is useful if you want to run multiple servers at once, or for added security. # Supported versions -AutoTorcher is available for Minecraft versions 1.21.10 and above! +AutoLan supports all versions 1.21.10 and above! Due to the frequency of Minecraft updates now, each Minecraft version has its own .jar file. This is mainly to prevent crashes from each minor update. + +# Changelog +## 1.1.0 +- Optimised the CPU usage of the mod +- Open up to lan using world settings for difficulty + - Cheats are enabled always as I was not able to find a reproducible way to get whether cheats were enabled or not diff --git a/build.gradle b/build.gradle index b81a0e2..f2f531b 100644 --- a/build.gradle +++ b/build.gradle @@ -26,7 +26,6 @@ dependencies { // Fabric API. This is technically optional, but you probably want it anyway. modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" - } processResources { diff --git a/gradle.properties b/gradle.properties index b695fa3..f78b2d3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,15 +7,15 @@ org.gradle.configuration-cache=false # Fabric Properties # check these on https://fabricmc.net/develop -minecraft_version=1.21.10 -yarn_mappings=1.21.10+build.2 -loader_version=0.17.3 -loom_version=1.12-SNAPSHOT +minecraft_version=1.21.11 +yarn_mappings=1.21.11+build.4 +loader_version=0.18.4 +loom_version=1.14-SNAPSHOT # Mod Properties -mod_version=1.0.0+1.21.10 +mod_version=1.1.0+1.21.11 maven_group=me.imgalvin archives_base_name=auto-lan # Dependencies -fabric_version=0.137.0+1.21.10 \ No newline at end of file +fabric_version=0.141.1+1.21.11 \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index d4081da..23449a2 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.1-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/src/main/java/me/imgalvin/Autolan.java b/src/main/java/me/imgalvin/Autolan.java deleted file mode 100644 index f2cd263..0000000 --- a/src/main/java/me/imgalvin/Autolan.java +++ /dev/null @@ -1,29 +0,0 @@ -package me.imgalvin; - -import net.fabricmc.api.ClientModInitializer; -import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; -import net.minecraft.server.integrated.IntegratedServer; -import net.minecraft.text.Text; -import net.minecraft.world.GameMode; - -import static net.minecraft.util.NetworkUtils.findLocalPort; - -public class Autolan implements ClientModInitializer { - private boolean opened = false; - - @Override - public void onInitializeClient() { - ClientTickEvents.END_CLIENT_TICK.register(client -> { - if (!opened && client.player != null && client.getServer() != null) { - IntegratedServer server = client.getServer(); - - if (server.isSingleplayer()) { - int port = findLocalPort(); - server.openToLan(GameMode.CREATIVE, true, port); - client.player.sendMessage(Text.of("§aOpened to LAN on port " + port), false); - opened = true; - } - } - }); - } -} diff --git a/src/main/java/me/imgalvin/autolan/Autolan.java b/src/main/java/me/imgalvin/autolan/Autolan.java new file mode 100644 index 0000000..9af90ab --- /dev/null +++ b/src/main/java/me/imgalvin/autolan/Autolan.java @@ -0,0 +1,11 @@ +package me.imgalvin.autolan; + +import net.fabricmc.api.ClientModInitializer; + +public class Autolan implements ClientModInitializer { + + @Override + public void onInitializeClient() { + System.out.println("Autolan initialised!"); + } +} diff --git a/src/main/java/me/imgalvin/autolan/mixin/AutoLanMixin.java b/src/main/java/me/imgalvin/autolan/mixin/AutoLanMixin.java new file mode 100644 index 0000000..b5555f6 --- /dev/null +++ b/src/main/java/me/imgalvin/autolan/mixin/AutoLanMixin.java @@ -0,0 +1,66 @@ +package me.imgalvin.autolan.mixin; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket; +import net.minecraft.server.integrated.IntegratedServer; +import net.minecraft.text.Text; +import net.minecraft.world.GameMode; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +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.minecraft.util.NetworkUtils.findLocalPort; + +@Mixin(ClientPlayNetworkHandler.class) +public class AutoLanMixin { + + @Unique + private boolean autolan$opened = false; + + @Inject(method = "onGameJoin", at = @At("TAIL")) + private void autolan$onGameJoin(GameJoinS2CPacket packet, CallbackInfo ci) { + if (autolan$opened) return; + + MinecraftClient client = MinecraftClient.getInstance(); + IntegratedServer server = client.getServer(); + + if (server == null || !server.isSingleplayer() || client.player == null) { + return; + } + + // Get mode of the world + GameMode gameMode = null; + try { + gameMode = server.getSaveProperties().getGameMode(); + } catch (Exception e) { + try { + client.player.sendMessage( + Text.literal("Failed to get game mode: " + e.getMessage()).formatted(net.minecraft.util.Formatting.RED), + false + ); + } catch (Throwable ignored) { + } + } + + int port = findLocalPort(); + try { + server.openToLan(gameMode, true, port); + autolan$opened = true; + client.player.sendMessage( + Text.literal("Opened to LAN on port " + port + " in mode: " + gameMode + ". Note: LAN worlds automatically open with cheats enabled. This will be addressed in a future update.").formatted(net.minecraft.util.Formatting.GREEN), + false + ); + } catch (Exception e) { + try { + client.player.sendMessage( + Text.literal("Failed to open to LAN: " + e.getMessage()).formatted(net.minecraft.util.Formatting.RED), + false + ); + } catch (Throwable ignored) { + } + } + } +} diff --git a/src/main/resources/autolan.mixins.json b/src/main/resources/autolan.mixins.json new file mode 100644 index 0000000..55fea8f --- /dev/null +++ b/src/main/resources/autolan.mixins.json @@ -0,0 +1,17 @@ +{ + "required": true, + "minVersion": "0.8", + "package": "me.imgalvin.autolan.mixin", + "compatibilityLevel": "JAVA_21", + "mixins": [], + "client": [ + "AutoLanMixin" + ], + "server": [], + "injectors": { + "defaultRequire": 1 + }, + "overwrites": { + "requireAnnotations": true + } +} \ No newline at end of file diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 14f2516..19a750c 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -17,13 +17,16 @@ "environment": "client", "entrypoints": { "client": [ - "me.imgalvin.Autolan" + "me.imgalvin.autolan.Autolan" ] }, "depends": { "fabricloader": ">=0.16", - "minecraft": "1.21.10", + "minecraft": "1.21.11", "java": ">=21", "fabric-api": "*" - } + }, + "mixins": [ + "autolan.mixins.json" + ] } \ No newline at end of file