Jump to content

[1.8.9] Keybind toggle moving forward


BuluBulu27

Recommended Posts

Hello,

 

I recently wanted to give modding a try, so here I am trying to figure out some basics.

I basically want to start by making an AutoWalk, a keybind that toggles "moving forward" for both SinglePlayer and Multiplayer, on Client Side.

 

Will there be any difference between SP and MP for that kind of mod?

How can I toggle the movement?

 

So far, here's what I have: a rip off of multiple sources I have found online to help me with that, but I couldn't find anything updated to 1.8.9.

 

KeyBindings.java

import org.lwjgl.input.Keyboard;
import net.minecraft.client.settings.KeyBinding;
import net.minecraftforge.fml.client.registry.ClientRegistry;

public class KeyBindings {
    public static KeyBinding keepForward;
    public static void init() {
        keepForward = new KeyBinding("key.keepForward", Keyboard.KEY_O, "key.categories.Autowalk");
        ClientRegistry.registerKeyBinding(keepForward);
    }
}

KeyInputHandler.java

import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.InputEvent;

public class KeyInputHandler {
    @SubscribeEvent
    public void onKeyInput(InputEvent.KeyInputEvent event) {
        if(KeyBindings.keepForward.isPressed())
            System.out.println("FORWAAAAAARD!!!");
    }
}

AutoWalk.java

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;


@Mod(modid = AutoWalk.MODID, version = AutoWalk.VERSION)
public class AutoWalk
{
    public static final String MODID = "autowalk";
    public static final String VERSION = "1.0";
    
    @Mod.EventHandler
    public void preInit(FMLPreInitializationEvent event) {
    Configuration config = new Configuration(event.getSuggestedConfigurationFile());
    MinecraftForge.EVENT_BUS.register(new com.bulubulu.KeyInputHandler());
    com.bulubulu.KeyBindings.init();
    }

    @Mod.EventHandler
    public void init(FMLInitializationEvent event) {}

    @Mod.EventHandler
    public void postInit(FMLPostInitializationEvent event) {}

}

 

I hope some of you can shed some light on the matter.

 

Thanks!

It will eventually work.

Link to comment
Share on other sites

I think the easiest way to do this is to essentially "press" the forward button. To do this, you need to get the forward button's KeyBinding.

 

In your onKeyInput listener, after you check if the key is pressed, get the forward button's KeyBinding from the gameSettings class. Check if the forward KeyBinding is pressed, and if it is set it to unpressed. Likewise, if it's not pressed, set it to pressed.

 

For example:

public class KeyInputHandler {
    @SubscribeEvent
    public void onKeyInput(InputEvent.KeyInputEvent event) {
        if(KeyBindings.keepForward.isPressed())
            KeyBinding bind = FMLClientHandler.instance().getClient().gameSettings.keyBindForward;
            if (bind.isKeyDown()) { //Player is going forwards, make them stop
                KeyBinding.setKeyBindState(bind.getKeyCode(), false);
            } else { //Player is not going forwards, make them start
                KeyBinding.setKeyBindState(bind.getKeyCode(), true);
            }
    }
}

Did I help you? Don't be afraid to give that 'thank you' button a good smack for me!

It's been looking at me weirdly for a while now.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.