Jump to content

Modify the keybindings


zerozhou

Recommended Posts

I'm going to write a potion that perform like confusion. That I want to swap the client's keybindings that when he press "w" and want to move forward and then he actually press "s" that he move backward.

I'm searching for the code in the src but get confusion :'( :-[....

Could someone point out the code I'm looking for? Or some simple idea about that?

Link to comment
Share on other sites

Hi

 

This is some background info that might be useful...

http://greyminecraftcoder.blogspot.com.au/2013/10/user-input.html

 

EntityPlayerSP.movementInput uses the method MovementInputFromOptions.updatePlayerMoveState(), so if you create your own class ConfusedMovementInput extends MovementInputFromOptions, and store it in EntityPlayerSP.movementInput, you can turn confusion on or off

 

eg

 

public class ConfusedMovementInput extends MovementInputFromOptions {

public ConfusedMovementInput(MovementInput interceptedMovementInput) {
underlyingMovementInput = interceptedMovementInput;
}

@Override
public void updatePlayerMoveState() {
  underlyingMovementInput.updatedPlayerMoveState();

        this.jump = underlyingMovementInput.jump;
        this.sneak = underlyingMovementInput.sneak;

        if (!confused) {
          this.moveStrafe = underlyingMovementInput.moveStrafe;
          this.moveForward = underlyingMovementInput.moveForward;
        } else {
            // swap moveStrafe, moveForward, or make them negative, etc
        }

}

public void setConfusion(boolean newConfused) {
  confused = newConfused;
}

protected MovementInput underlyingMovementInput;
private boolean confused = false;

}

 

And somewhere else in your code you execute

 

ConfusedMovementInput myInput = new ConfusedMovementInput(entityPlayerSP.movementInput);

entityPlayerSP.movementInput =myInput;

 

and then myInput.setConfusion(true/false) to turn confusion on or off.

 

You can access entityPlayerSP from

Minecraft.getMinecraft().thePlayer

 

I haven't tried to execute this code, but you get the idea?  The mix of composition and inheritance is a bit clumsy because of the private GameSettings but it should still work.  I have used something very similar for other keybindings (https://gist.github.com/TheGreyGhost/7033821) and it worked great.

 

-TGG

 

 

 

Link to comment
Share on other sites

I've tried your code. But it seems not working.

I try to replace the movement input while the player join the world. And as the log I see that I replace it successfully. But the updatePlayerMoveState() of the new class did not invoke. dont know why.

I make a small change the it inhert from MovementInput, not MovementInputFromOptions. dont know if that is related.

Link to comment
Share on other sites

Hi

 

Interesting.  I tried it in my mod and it works fine.  When I'm holding an item, left and right are swapped.  When I'm not holding an item, left and right are normal.

 

-TGG

 

/**
* User: The Grey Ghost
* Date: 10/11/13
*/
public class ConfusedMovementInput extends MovementInput
{
  public ConfusedMovementInput(MovementInput interceptedMovementInput)
  {
    underlyingMovementInput = interceptedMovementInput;
  }

  @Override
  public void updatePlayerMoveState() {
    underlyingMovementInput.updatePlayerMoveState();

    this.jump = underlyingMovementInput.jump;
    this.sneak = underlyingMovementInput.sneak;

    if (!confused) {
      this.moveStrafe = underlyingMovementInput.moveStrafe;
      this.moveForward = underlyingMovementInput.moveForward;
    } else {
      this.moveStrafe = -underlyingMovementInput.moveStrafe;           //swap left and right
      this.moveForward = underlyingMovementInput.moveForward;
    }
  }

  public void setConfusion(boolean newConfused) {
    confused = newConfused;
  }

  protected MovementInput underlyingMovementInput;
  private boolean confused = false;
}

/**
* User: The Grey Ghost
* Date: 2/11/13
*/
public class ClientTickHandler implements ITickHandler {

  public EnumSet<TickType> ticks()
  {
    return EnumSet.of(TickType.CLIENT);
  }

  public void tickStart(EnumSet<TickType> type, Object... tickData)
  {
    if (!type.contains(TickType.CLIENT)) return;

    EntityClientPlayerMP player = Minecraft.getMinecraft().thePlayer;
    if (player != null) {
      if (player.movementInput instanceof ConfusedMovementInput) {
      } else {
        SpeedyToolsMod.confusedMovementInput = new ConfusedMovementInput(player.movementInput);
        player.movementInput = SpeedyToolsMod.confusedMovementInput;
      }

      ItemStack heldItem = player.getHeldItem();
      if (heldItem != null) {
        SpeedyToolsMod.confusedMovementInput.setConfusion(true);
      } else {
        SpeedyToolsMod.confusedMovementInput.setConfusion(false);
      }
    }



  }

  public void tickEnd(EnumSet<TickType> type, Object... tickData)
  {
    }

  }

  public String getLabel()
  {
    return "ClientTickHandler";
  }

}

@Mod(modid="SpeedyToolsMod", name="Speedy Tools Mod", version="0.0.1")
@NetworkMod(clientSideRequired=true, serverSideRequired=true, channels={"SpeedyTools"}, packetHandler = PacketHandler.class)
public class SpeedyToolsMod {

  // The instance of your mod that Forge uses.
  @Mod.Instance("SpeedyToolsMod")
  public static speedytools.SpeedyToolsMod instance;


  public static ConfusedMovementInput confusedMovementInput;

 

}

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.