From 03b17093498e1764ce65648dce120b4d9b4b3a13 Mon Sep 17 00:00:00 2001 From: Emanuel Pilz Date: Thu, 7 Nov 2024 05:00:39 +0100 Subject: [PATCH] chore: refactor & use inject instead of shadow in mixins --- README.md | 2 + build.gradle | 43 +---- .../java/com/emonadeo/autorun/AutoRunMod.java | 168 ----------------- .../com/emonadeo/autorun/AutoRunModMenu.java | 68 ------- .../autorun/mixin/client/InputMixin.java | 41 ----- .../mixin/client/LocalPlayerMixin.java | 37 ---- src/main/java/.gitkeep | 0 .../java/com/emonadeo/autorun/AutoRunMod.java | 171 ++++++++++++++++++ .../com/emonadeo/autorun/AutoRunModMenu.java | 70 +++++++ .../autorun/mixin/InputClientMixin.java | 45 +++++ .../autorun/mixin/LocalPlayerClientMixin.java | 27 +++ .../resources/autorun.mixins.json} | 4 +- src/main/resources/fabric.mod.json | 20 +- 13 files changed, 333 insertions(+), 363 deletions(-) delete mode 100644 src/client/java/com/emonadeo/autorun/AutoRunMod.java delete mode 100644 src/client/java/com/emonadeo/autorun/AutoRunModMenu.java delete mode 100644 src/client/java/com/emonadeo/autorun/mixin/client/InputMixin.java delete mode 100644 src/client/java/com/emonadeo/autorun/mixin/client/LocalPlayerMixin.java delete mode 100644 src/main/java/.gitkeep create mode 100644 src/main/java/com/emonadeo/autorun/AutoRunMod.java create mode 100644 src/main/java/com/emonadeo/autorun/AutoRunModMenu.java create mode 100644 src/main/java/com/emonadeo/autorun/mixin/InputClientMixin.java create mode 100644 src/main/java/com/emonadeo/autorun/mixin/LocalPlayerClientMixin.java rename src/{client/resources/autorun.client.mixins.json => main/resources/autorun.mixins.json} (54%) diff --git a/README.md b/README.md index 69b3e28..589d041 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,9 @@ This keybind can be customized in the controls options. ## FAQ ### Can I use it in my modpack? + Yes. ### Where can I download the mod? + The only official distributions are [GitHub](https://github.com/Emonadeo/AutoRun/releases), [CurseForge](https://www.curseforge.com/minecraft/mc-mods/autorun-fabric) and [Modrinth](https://modrinth.com/mod/autorun). Please let me know if you find any other website distributing this mod. diff --git a/build.gradle b/build.gradle index b5e6fa4..1c0a93c 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ plugins { - id 'fabric-loom' version '1.7-SNAPSHOT' + id 'fabric-loom' version '1.8-SNAPSHOT' id 'maven-publish' id "com.matthewprenger.cursegradle" version "1.4.0" id "com.modrinth.minotaur" version "2.+" @@ -17,34 +17,24 @@ repositories { maven { url "https://maven.terraformersmc.com/" } } -loom { - splitEnvironmentSourceSets() - - mods { - "autorun" { - sourceSet sourceSets.main - sourceSet sourceSets.client - } - } - -} - dependencies { + // Minecraft & Fabric Loader minecraft "com.mojang:minecraft:${project.minecraft_version}" mappings loom.officialMojangMappings() modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" + // Fabric API modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + // Mod Menu modImplementation("com.terraformersmc:modmenu:${project.modmenu_version}") { exclude(group: "net.fabricmc.fabric-api") } + // Cloth Config modApi("me.shedaniel.cloth:cloth-config-fabric:${project.cloth_version}") { exclude(group: "net.fabricmc.fabric-api") } - - include "me.shedaniel.cloth:cloth-config-fabric:${project.cloth_version}" } processResources { @@ -72,23 +62,6 @@ jar { } } -// configure the maven publication -publishing { - publications { - mavenJava(MavenPublication) { - from components.java - } - } - - // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. - repositories { - // Add repositories to publish to here. - // Notice: This block does NOT have the same function as the block in the top level. - // The repositories here will be used for publishing your artifact, not for - // retrieving dependencies. - } -} - curseforge { if (project.hasProperty("curseForgeApiKey") || System.getenv("curseForgeApiKey") != null) { apiKey = project.hasProperty("curseForgeApiKey") ? project.property("curseForgeApiKey") : System.getenv("curseForgeApiKey") @@ -99,7 +72,7 @@ curseforge { changelog = file("changelog.txt") addGameVersion "1.21.3" - addGameVersion "1.21" + addGameVersion "1.21.2" addGameVersion "Java 21" addGameVersion "Fabric" @@ -123,9 +96,9 @@ import com.modrinth.minotaur.dependencies.ModDependency modrinth { token = project.hasProperty("modrinthApiKey") ? project.property("modrinthApiKey") : System.getenv("modrinthApiKey") projectId = "2i7tg1Wv" - versionName = "v${project.mod_version} (Minecraft 1.21)" + versionName = "v${project.mod_version} (Minecraft 1.21.2)" versionNumber = version uploadFile = remapJar - gameVersions = ["1.21"] + gameVersions = ["1.21.2", "1.21.3"] loaders = ["fabric"] } diff --git a/src/client/java/com/emonadeo/autorun/AutoRunMod.java b/src/client/java/com/emonadeo/autorun/AutoRunMod.java deleted file mode 100644 index adb31d7..0000000 --- a/src/client/java/com/emonadeo/autorun/AutoRunMod.java +++ /dev/null @@ -1,168 +0,0 @@ -package com.emonadeo.autorun; - -import net.fabricmc.api.ClientModInitializer; -import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientEntityEvents; -import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; -import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; -import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.client.KeyMapping; -import net.minecraft.client.player.LocalPlayer; -import net.minecraft.network.chat.Component; -import net.minecraft.world.entity.player.Input; -import net.minecraft.client.Minecraft; -import org.lwjgl.glfw.GLFW; -import com.mojang.blaze3d.platform.InputConstants; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.Properties; - -public class AutoRunMod implements ClientModInitializer { - - public static final String MODID = "autorun"; - public static final File CFG_FILE = new File(FabricLoader.getInstance().getConfigDir().toFile(), - "autorun.properties"); - - public static boolean toggleAutoJump = true; - public static boolean persistAutoRun = false; - public static boolean alwaysSprint = false; - - public static boolean forward = false; - public static boolean backward = false; - public static boolean left = false; - public static boolean right = false; - public static boolean sprint = false; - - private static boolean activating = false; - private static boolean originalAutoJumpSetting = false; - - private static KeyMapping keyBinding; - - @Override - public void onInitializeClient() { - loadConfig(CFG_FILE); - // Re-save so that new properties will appear in old config files - saveConfig(CFG_FILE); - - keyBinding = KeyBindingHelper.registerKeyBinding(new KeyMapping( - "key.autorun.toggle", - InputConstants.Type.KEYSYM, - GLFW.GLFW_KEY_O, // Default to 'o' - "key.categories.movement" // Append movement category - )); - - ClientTickEvents.END_CLIENT_TICK.register(client -> { - while (keyBinding.consumeClick() && client.level != null) { - if (forward || backward || left || right) { - disableAutoRun(client); - } else { - enableAutoRun(client); - activating = true; - } - } - }); - - ClientTickEvents.END_CLIENT_TICK.register(client -> { - if (activating) { - if ((forward && !client.options.keyUp.isDown()) - || (backward && !client.options.keyDown.isDown()) - || (left && !client.options.keyLeft.isDown()) - || (right && !client.options.keyRight.isDown())) { - activating = false; - } - return; - } - - if ((forward || backward) && (client.options.keyUp.isDown() || client.options.keyDown.isDown())) { - disableAutoRun(client); - } - if ((left || right) && (client.options.keyLeft.isDown() || client.options.keyRight.isDown())) { - disableAutoRun(client); - } - }); - - ClientEntityEvents.ENTITY_UNLOAD.register((entity, clientWorld) -> { - if (entity instanceof LocalPlayer && !persistAutoRun) { - disableAutoRun(Minecraft.getInstance()); - } - }); - } - - private static void enableAutoRun(Minecraft client) { - client.player.displayClientMessage(Component.literal("Activating Auto-Run"), false); - - if (toggleAutoJump) { - originalAutoJumpSetting = client.options.autoJump().get(); - client.options.autoJump().set(true); - client.options.broadcastOptions(); - } - - Input input = client.player.input.keyPresses; - if (input.sprint() || alwaysSprint) { - sprint = true; - } - if (!input.forward() && !input.backward() && !input.left() && !input.right()) { - // Auto-Run forward if no movement key is pressed - forward = true; - return; - } - // At least 1 movement key is pressed - if (input.forward()) { - forward = true; - } - if (input.backward()) { - backward = true; - } - if (input.left()) { - left = true; - } - if (input.right()) { - right = true; - } - } - - private static void disableAutoRun(Minecraft client) { - client.player.displayClientMessage(Component.literal("Deactivating Auto-Run"), false); - - forward = false; - backward = false; - left = false; - right = false; - sprint = false; - - // Restore Auto-Jump - if (toggleAutoJump) { - client.options.autoJump().set(originalAutoJumpSetting); - client.options.broadcastOptions(); - } - } - - public static void loadConfig(File file) { - try { - Properties cfg = new Properties(); - if (!file.exists()) { - saveConfig(file); - } - cfg.load(new FileInputStream(file)); - alwaysSprint = Boolean.parseBoolean(cfg.getProperty("alwaysSprint", "false")); - persistAutoRun = Boolean.parseBoolean(cfg.getProperty("persistAutoRun", "false")); - toggleAutoJump = Boolean.parseBoolean(cfg.getProperty("toggleAutoJump", "true")); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public static void saveConfig(File file) { - try { - FileOutputStream fos = new FileOutputStream(file, false); - fos.write(("alwaysSprint=" + alwaysSprint + "\n").getBytes()); - fos.write(("persistAutoRun=" + persistAutoRun + "\n").getBytes()); - fos.write(("toggleAutoJump=" + toggleAutoJump + "\n").getBytes()); - fos.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - -} diff --git a/src/client/java/com/emonadeo/autorun/AutoRunModMenu.java b/src/client/java/com/emonadeo/autorun/AutoRunModMenu.java deleted file mode 100644 index 9ce89e8..0000000 --- a/src/client/java/com/emonadeo/autorun/AutoRunModMenu.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.emonadeo.autorun; - -import com.terraformersmc.modmenu.api.ConfigScreenFactory; -import com.terraformersmc.modmenu.api.ModMenuApi; -import me.shedaniel.clothconfig2.api.ConfigBuilder; -import me.shedaniel.clothconfig2.api.ConfigCategory; -import me.shedaniel.clothconfig2.api.ConfigEntryBuilder; -import net.minecraft.client.gui.screens.Screen; -import net.minecraft.network.chat.Component; - -public class AutoRunModMenu implements ModMenuApi, ConfigScreenFactory { - @Override - public ConfigScreenFactory getModConfigScreenFactory() { - return this; - } - - @Override - public Screen create(Screen screen) { - ConfigBuilder builder = ConfigBuilder.create() - .setParentScreen(screen) - .setTitle(Component.translatable("title." + AutoRunMod.MODID + ".config")); - - ConfigEntryBuilder entryBuilder = builder.entryBuilder(); - ConfigCategory general = builder - .getOrCreateCategory(Component.translatable("config." + AutoRunMod.MODID + ".general")); - - // Toogle Auto-Jump - general.addEntry(entryBuilder - .startBooleanToggle( - Component.translatable( - "config." + AutoRunMod.MODID + ".toggleAutoJump"), - AutoRunMod.toggleAutoJump) - .setDefaultValue(true) - .setTooltip(Component.translatable( - "config." + AutoRunMod.MODID + ".toggleAutoJump.description")) - .setSaveConsumer((value) -> AutoRunMod.toggleAutoJump = value) - .build()); - - // Persist Auto-Run - general.addEntry(entryBuilder - .startBooleanToggle( - Component.translatable( - "config." + AutoRunMod.MODID + ".persistAutoRun"), - AutoRunMod.persistAutoRun) - .setDefaultValue(false) - .setTooltip(Component.translatable( - "config." + AutoRunMod.MODID + ".persistAutoRun.description")) - .setSaveConsumer((value) -> AutoRunMod.persistAutoRun = value) - .build()); - - // Always Sprint - general.addEntry(entryBuilder - .startBooleanToggle( - Component.translatable( - "config." + AutoRunMod.MODID + ".alwaysSprint"), - AutoRunMod.alwaysSprint) - .setDefaultValue(false) - .setTooltip(Component.translatable( - "config." + AutoRunMod.MODID + ".alwaysSprint")) - .setSaveConsumer((value) -> AutoRunMod.alwaysSprint = value) - .build()); - - return builder.setSavingRunnable(() -> { - AutoRunMod.saveConfig(AutoRunMod.CFG_FILE); - AutoRunMod.loadConfig(AutoRunMod.CFG_FILE); - }).build(); - } -} diff --git a/src/client/java/com/emonadeo/autorun/mixin/client/InputMixin.java b/src/client/java/com/emonadeo/autorun/mixin/client/InputMixin.java deleted file mode 100644 index 924d43a..0000000 --- a/src/client/java/com/emonadeo/autorun/mixin/client/InputMixin.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.emonadeo.autorun.mixin.client; - -import com.emonadeo.autorun.AutoRunMod; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import net.minecraft.world.entity.player.Input; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; - -@Environment(EnvType.CLIENT) -@Mixin(Input.class) -public class InputMixin { - @Shadow - private boolean forward; - - @Shadow - private boolean backward; - - @Shadow - private boolean left; - - @Shadow - private boolean right; - - public boolean forward() { - return forward || AutoRunMod.forward; - } - - public boolean backward() { - return backward || AutoRunMod.backward; - } - - public boolean left() { - return left || AutoRunMod.left; - } - - public boolean right() { - return right || AutoRunMod.right; - } -} diff --git a/src/client/java/com/emonadeo/autorun/mixin/client/LocalPlayerMixin.java b/src/client/java/com/emonadeo/autorun/mixin/client/LocalPlayerMixin.java deleted file mode 100644 index e05c2d9..0000000 --- a/src/client/java/com/emonadeo/autorun/mixin/client/LocalPlayerMixin.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.emonadeo.autorun.mixin.client; - -import com.emonadeo.autorun.AutoRunMod; -import com.mojang.authlib.GameProfile; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import net.minecraft.client.multiplayer.ClientLevel; -import net.minecraft.client.player.AbstractClientPlayer; -import net.minecraft.client.player.LocalPlayer; -import org.spongepowered.asm.mixin.Mixin; - -@Environment(EnvType.CLIENT) -@Mixin(LocalPlayer.class) -public class LocalPlayerMixin extends AbstractClientPlayer { - public LocalPlayerMixin(ClientLevel clientLevel, GameProfile gameProfile) { - super(clientLevel, gameProfile); - } - - @Override - public boolean isSprinting() { - if (AutoRunMod.sprint) { - return true; - } - return super.isSprinting(); - } - - @Override - public void setSprinting(boolean bl) { - if (AutoRunMod.sprint) { - // Cancel out `setSprinting(false)` calls - // (e.g. when bumping into something or receiving damage) - super.setSprinting(true); - return; - } - super.setSprinting(bl); - } -} \ No newline at end of file diff --git a/src/main/java/.gitkeep b/src/main/java/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/com/emonadeo/autorun/AutoRunMod.java b/src/main/java/com/emonadeo/autorun/AutoRunMod.java new file mode 100644 index 0000000..052324f --- /dev/null +++ b/src/main/java/com/emonadeo/autorun/AutoRunMod.java @@ -0,0 +1,171 @@ +package com.emonadeo.autorun; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Properties; + +import org.lwjgl.glfw.GLFW; + +import com.mojang.blaze3d.platform.InputConstants; + +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientEntityEvents; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.client.KeyMapping; +import net.minecraft.client.Minecraft; +import net.minecraft.client.player.LocalPlayer; +import net.minecraft.network.chat.Component; +import net.minecraft.world.entity.player.Input; + +public class AutoRunMod implements ClientModInitializer { + + public static final String MODID = "autorun"; + public static final File CFG_FILE = new File(FabricLoader.getInstance().getConfigDir().toFile(), + "autorun.properties"); + + public static boolean toggleAutoJump = true; + public static boolean persistAutoRun = false; + public static boolean alwaysSprint = false; + + public static boolean forward = false; + public static boolean backward = false; + public static boolean left = false; + public static boolean right = false; + public static boolean sprint = false; + + private static boolean activating = false; + private static boolean originalAutoJumpSetting = false; + + private static KeyMapping keyBinding; + + @Override + public void onInitializeClient() { + loadConfig(CFG_FILE); + // Re-save so that new properties will appear in old config files + saveConfig(CFG_FILE); + + keyBinding = KeyBindingHelper.registerKeyBinding(new KeyMapping( + "key.autorun.toggle", + InputConstants.Type.KEYSYM, + GLFW.GLFW_KEY_O, // Default to 'o' + "key.categories.movement" // Append movement category + )); + + ClientTickEvents.END_CLIENT_TICK.register(client -> { + while (keyBinding.consumeClick() && client.level != null) { + if (forward || backward || left || right) { + disableAutoRun(client); + } else { + enableAutoRun(client); + activating = true; + } + } + }); + + ClientTickEvents.END_CLIENT_TICK.register(client -> { + if (activating) { + if ((forward && !client.options.keyUp.isDown()) + || (backward && !client.options.keyDown.isDown()) + || (left && !client.options.keyLeft.isDown()) + || (right && !client.options.keyRight.isDown())) { + activating = false; + } + return; + } + + if ((forward || backward) && (client.options.keyUp.isDown() || client.options.keyDown.isDown())) { + disableAutoRun(client); + } + if ((left || right) && (client.options.keyLeft.isDown() || client.options.keyRight.isDown())) { + disableAutoRun(client); + } + }); + + ClientEntityEvents.ENTITY_UNLOAD.register((entity, clientWorld) -> { + if (entity instanceof LocalPlayer && !persistAutoRun) { + disableAutoRun(Minecraft.getInstance()); + } + }); + } + + private static void enableAutoRun(Minecraft client) { + client.player.displayClientMessage(Component.literal("Activating Auto-Run"), false); + + if (toggleAutoJump) { + originalAutoJumpSetting = client.options.autoJump().get(); + client.options.autoJump().set(true); + client.options.broadcastOptions(); + } + + Input input = client.player.input.keyPresses; + if (input.sprint() || alwaysSprint) { + sprint = true; + } + if (!input.forward() && !input.backward() && !input.left() && !input.right()) { + // Auto-Run forward if no movement key is pressed + forward = true; + return; + } + // At least 1 movement key is pressed + if (input.forward()) { + forward = true; + } + if (input.backward()) { + backward = true; + } + if (input.left()) { + left = true; + } + if (input.right()) { + right = true; + } + } + + private static void disableAutoRun(Minecraft client) { + client.player.displayClientMessage(Component.literal("Deactivating Auto-Run"), false); + + forward = false; + backward = false; + left = false; + right = false; + sprint = false; + + // Restore Auto-Jump + if (toggleAutoJump) { + client.options.autoJump().set(originalAutoJumpSetting); + client.options.broadcastOptions(); + } + } + + public static void loadConfig(File file) { + try { + Properties cfg = new Properties(); + if (!file.exists()) { + saveConfig(file); + } + cfg.load(new FileInputStream(file)); + alwaysSprint = Boolean.parseBoolean(cfg.getProperty("alwaysSprint", "false")); + persistAutoRun = Boolean.parseBoolean(cfg.getProperty("persistAutoRun", "false")); + toggleAutoJump = Boolean.parseBoolean(cfg.getProperty("toggleAutoJump", "true")); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void saveConfig(File file) { + try { + FileOutputStream fos = new FileOutputStream(file, false); + fos.write(("alwaysSprint=" + alwaysSprint + "\n").getBytes()); + fos.write(("persistAutoRun=" + persistAutoRun + "\n").getBytes()); + fos.write(("toggleAutoJump=" + toggleAutoJump + "\n").getBytes()); + fos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/src/main/java/com/emonadeo/autorun/AutoRunModMenu.java b/src/main/java/com/emonadeo/autorun/AutoRunModMenu.java new file mode 100644 index 0000000..ab5beef --- /dev/null +++ b/src/main/java/com/emonadeo/autorun/AutoRunModMenu.java @@ -0,0 +1,70 @@ +package com.emonadeo.autorun; + +import com.terraformersmc.modmenu.api.ConfigScreenFactory; +import com.terraformersmc.modmenu.api.ModMenuApi; + +import me.shedaniel.clothconfig2.api.ConfigBuilder; +import me.shedaniel.clothconfig2.api.ConfigCategory; +import me.shedaniel.clothconfig2.api.ConfigEntryBuilder; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.network.chat.Component; + +public class AutoRunModMenu implements ModMenuApi, ConfigScreenFactory { + + @Override + public ConfigScreenFactory getModConfigScreenFactory() { + return this; + } + + @Override + public Screen create(Screen screen) { + ConfigBuilder builder = ConfigBuilder.create() + .setParentScreen(screen) + .setTitle(Component.translatable("title." + AutoRunMod.MODID + ".config")); + + ConfigEntryBuilder entryBuilder = builder.entryBuilder(); + ConfigCategory general = builder + .getOrCreateCategory(Component.translatable("config." + AutoRunMod.MODID + ".general")); + + // Toogle Auto-Jump + general.addEntry(entryBuilder + .startBooleanToggle( + Component.translatable( + "config." + AutoRunMod.MODID + ".toggleAutoJump"), + AutoRunMod.toggleAutoJump) + .setDefaultValue(true) + .setTooltip(Component.translatable( + "config." + AutoRunMod.MODID + ".toggleAutoJump.description")) + .setSaveConsumer((value) -> AutoRunMod.toggleAutoJump = value) + .build()); + + // Persist Auto-Run + general.addEntry(entryBuilder + .startBooleanToggle( + Component.translatable( + "config." + AutoRunMod.MODID + ".persistAutoRun"), + AutoRunMod.persistAutoRun) + .setDefaultValue(false) + .setTooltip(Component.translatable( + "config." + AutoRunMod.MODID + ".persistAutoRun.description")) + .setSaveConsumer((value) -> AutoRunMod.persistAutoRun = value) + .build()); + + // Always Sprint + general.addEntry(entryBuilder + .startBooleanToggle( + Component.translatable( + "config." + AutoRunMod.MODID + ".alwaysSprint"), + AutoRunMod.alwaysSprint) + .setDefaultValue(false) + .setTooltip(Component.translatable( + "config." + AutoRunMod.MODID + ".alwaysSprint")) + .setSaveConsumer((value) -> AutoRunMod.alwaysSprint = value) + .build()); + + return builder.setSavingRunnable(() -> { + AutoRunMod.saveConfig(AutoRunMod.CFG_FILE); + AutoRunMod.loadConfig(AutoRunMod.CFG_FILE); + }).build(); + } +} diff --git a/src/main/java/com/emonadeo/autorun/mixin/InputClientMixin.java b/src/main/java/com/emonadeo/autorun/mixin/InputClientMixin.java new file mode 100644 index 0000000..af2924e --- /dev/null +++ b/src/main/java/com/emonadeo/autorun/mixin/InputClientMixin.java @@ -0,0 +1,45 @@ +package com.emonadeo.autorun.mixin; + +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.CallbackInfoReturnable; + +import com.emonadeo.autorun.AutoRunMod; + +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.world.entity.player.Input; + +@Environment(EnvType.CLIENT) +@Mixin(Input.class) +public class InputClientMixin { + + @Inject(method = "forward", at = @At("HEAD"), cancellable = true) + public void forward(CallbackInfoReturnable info) { + if (AutoRunMod.forward) { + info.setReturnValue(true); + } + } + + @Inject(method = "backward", at = @At("HEAD"), cancellable = true) + public void backward(CallbackInfoReturnable info) { + if (AutoRunMod.backward) { + info.setReturnValue(true); + } + } + + @Inject(method = "right", at = @At("HEAD"), cancellable = true) + public void right(CallbackInfoReturnable info) { + if (AutoRunMod.right) { + info.setReturnValue(true); + } + } + + @Inject(method = "left", at = @At("HEAD"), cancellable = true) + public void left(CallbackInfoReturnable info) { + if (AutoRunMod.left) { + info.setReturnValue(true); + } + } +} diff --git a/src/main/java/com/emonadeo/autorun/mixin/LocalPlayerClientMixin.java b/src/main/java/com/emonadeo/autorun/mixin/LocalPlayerClientMixin.java new file mode 100644 index 0000000..13337c0 --- /dev/null +++ b/src/main/java/com/emonadeo/autorun/mixin/LocalPlayerClientMixin.java @@ -0,0 +1,27 @@ +package com.emonadeo.autorun.mixin; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +import com.emonadeo.autorun.AutoRunMod; + +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.KeyMapping; +import net.minecraft.client.Minecraft; +import net.minecraft.client.player.LocalPlayer; + +@Environment(EnvType.CLIENT) +@Mixin(LocalPlayer.class) +public class LocalPlayerClientMixin { + + @Redirect(method = "aiStep()V", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/KeyMapping;isDown()Z")) + public boolean isSprinting(KeyMapping keySprint) { + Minecraft client = Minecraft.getInstance(); + if (AutoRunMod.sprint && keySprint == client.options.keySprint) { + return true; + } + return keySprint.isDown(); + } +} \ No newline at end of file diff --git a/src/client/resources/autorun.client.mixins.json b/src/main/resources/autorun.mixins.json similarity index 54% rename from src/client/resources/autorun.client.mixins.json rename to src/main/resources/autorun.mixins.json index e1b7f91..a5dac26 100644 --- a/src/client/resources/autorun.client.mixins.json +++ b/src/main/resources/autorun.mixins.json @@ -1,9 +1,9 @@ { "required": true, "minVersion": "0.8", - "package": "com.emonadeo.autorun.mixin.client", + "package": "com.emonadeo.autorun.mixin", "compatibilityLevel": "JAVA_21", - "client": ["InputMixin"], + "client": ["InputClientMixin", "LocalPlayerClientMixin"], "injectors": { "defaultRequire": 1 } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 1438d5a..d76d977 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -1,27 +1,23 @@ { "schemaVersion": 1, "id": "autorun", + "name": "Auto-Run", "version": "${mod_version}", - "name": "AutoRun", "description": "Hands-free walking and sprinting", + "environment": "client", + "license": "CC0-1.0", + "icon": "assets/autorun/icon.png", "authors": ["Emonadeo"], "contact": { - "homepage": "https://github.com/Emonadeo", - "sources": "https://github.com/Emonadeo/AutoRun" + "homepage": "https://modrinth.com/mod/autorun", + "sources": "https://github.com/Emonadeo/autorun", + "issues": "https://github.com/Emonadeo/autorun/issues" }, - "license": "CC0-1.0", - "icon": "assets/autorun/icon.png", - "environment": "client", "entrypoints": { "client": ["com.emonadeo.autorun.AutoRunMod"], "modmenu": ["com.emonadeo.autorun.AutoRunModMenu"] }, - "mixins": [ - { - "config": "autorun.client.mixins.json", - "environment": "client" - } - ], + "mixins": ["autorun.mixins.json"], "depends": { "fabricloader": ">=0.15.10", "minecraft": ">=1.21.2-",