Jump to content

How to execute the equivalent of left click,right click, jump etc. in java code


fernthedev

Recommended Posts

So basically I am attempting to make a mod that will allow you to use controllers for Minecraft (following the console version of Minecraft control's scheme)

I have this code, not tested yet, which you might aswell take a look at my current work and see if anything here is wrong (such as yaw needing the numbers in reversed etc.)

 

I can also make a github repo if that is easier to read than just code in plain text.

 

I am currently missing the following which I had no luck finding how to do online: (I added comments for most of my methods showing what they do in Minecraft code just incase I should change something)

  •  Left click
  • Right click
  • Jump 
  • Esc key button
  • (Optionally) Add a pointer that can be moved with the joysticks for buttons and allow using a button like X to {pick up/place item} in slots or click buttons in menus
    private ControllerButtons oldButtons;

    int scrollTime;

    int dropTime;

    public void update(IControlPlayer player) {

        if(oldButtons == null) {
            oldButtons = controller.getButtons();
        }


        if(!handler.isGuiOpen()) {
            //Equivalent to (Minecraft.getMinecraft().thePlayer.moveForward = controller.getAxes().getVERTICAL_LEFT_STICKER().getValue();)
            player.moveForward(controller.getAxes().getVERTICAL_LEFT_STICKER().getValue()); // -1 is down, 1 is up, 0 is stateless

            //Equivalent to (Minecraft.getMinecraft().thePlayer.moveStrafing = controller.getAxes().getHORIZONTAL_LEFT_STICKER().getValue();)
            player.moveStrafing(controller.getAxes().getHORIZONTAL_LEFT_STICKER().getValue()); // -1 IS DOWN, 1 IS UP, 0 IS STATELESS

            //Equivalent to (Minecraft.getMinecraft().thePlayer.rotationPitch += controller.getAxes().getVERTICAL_RIGHT_STICKER().getValue();)
            player.addRotationYaw(controller.getAxes().getHORIZONTAL_RIGHT_STICKER().getValue()); // -1 IS DOWN, 1 IS UP, 0 IS STATELESS

            //Equivalent to (Minecraft.getMinecraft().thePlayer.rotationYaw += controller.getAxes().getHORIZONTAL_RIGHT_STICKER().getValue();)
            player.addRotationPitch(controller.getAxes().getVERTICAL_RIGHT_STICKER().getValue()); // -1 is down, 1 is up, 0 is stateless
        }

        if(controller.getAxes().getLEFT_TRIGGER().getValue() > 0.5) {
            // RIGHT CLICK EQUIVALENT
        }

        if(controller.getAxes().getRIGHT_TRIGGER().getValue() > 0.5) {
            //LEFT CLICK EQUIVALENT
        }

        if(checkToggle(controller.getButtons().getA(),oldButtons.getA())) {
            // JUMP EQUIVALENT
        }

        //Closes GUI
        if(checkToggle(controller.getButtons().getB(),oldButtons.getB())) {
            if(handler.isGuiOpen()) {
                //Equivalent to (Minecraft.getMinecraft().displayGuiScreen(null);)
                handler.closeGUI();
            }
        }

        //////////////////////////////////////////////
        // Drops item
        if((dropTime == 0 || dropTime > 30) && !handler.isGuiOpen()) {
            boolean increaseTime = false;
            if (dropTime == 0 && !controller.getButtons().getB().isState() && oldButtons.getB().isState()) {
                player.dropItem();
                increaseTime = true;
            }
            if (dropTime > 30 && controller.getButtons().getB().isState()) {
                player.dropStack();
                increaseTime = true;
            }

            if (increaseTime) {
                dropTime++;
            } else {
                dropTime = 0;
            }
        }
        //////////////////////////////////////////////

        if(checkToggle(controller.getButtons().getSTART_BUTTON(),oldButtons.getSTART_BUTTON())) {
            //Would execute what Escape does
        }

        if(checkToggle(controller.getButtons().getY(),oldButtons.getY())) {
            boolean opened = Minecraft.getMinecraft().currentScreen instanceof GuiInventory;

            if(opened) {
                //Equivalent to (Minecraft.getMinecraft().displayGuiScreen(null);)
                handler.closeGUI();
            }else{
                //Equivalent to (Minecraft.getMinecraft().displayGuiScreen(new GuiInventory(Minecraft.getMinecraft().thePlayer));)
                player.openInventory();
            }
        }

        //Basically does what TAB would do
        if(checkToggle(controller.getButtons().getEXTRA_BUTTON(),oldButtons.getEXTRA_BUTTON())) {
            handler.renderPlayerList(true);
        }else{
            handler.renderPlayerList(false);
        }


        if(checkToggle(controller.getButtons().getLEFT_STICKER(),oldButtons.getLEFT_STICKER())) {
            //Basically player.setSneaking(!player.isSneaking());
            player.toggleSneak();
        }

        //Basically does (Minecraft.getMinecraft().displayGuiScreen(new GuiChat());)
        if(checkToggle(controller.getButtons().getDPAD_UP(),oldButtons.getDPAD_UP())) {
            handler.displayChat();
        }

        if(checkToggle(controller.getButtons().getRIGHT_STICKER(),oldButtons.getRIGHT_STICKER())) {
            //Basically Minecraft.getInstance().gameSettings.thirdPersonView++;
            handler.toggle3rdPerson();
        }

        ////////////////////////////////
        //Scrolls between the hotbar
        if(controller.getButtons().getBUMPER_RIGHT().isState() && (scrollTime == 0 || scrollTime > 30)) {
            player.scrollSlot(-1);
            scrollTime++;
        }else{
            scrollTime = 0;
        }

        if(controller.getButtons().getBUMPER_LEFT().isState()  && (scrollTime == 0 || scrollTime > 30)) {
            player.scrollSlot(1);
        }else{
            scrollTime = 0;
        }
        ////////////////////////////////

        oldButtons = controller.getButtons();
    }

    private boolean checkToggle(ControllerButtonState newButton, ControllerButtonState oldState) {
        return newButton.isState() != oldState.isState();
    }

 

Edited by fernthedev
Forgot to clarify completely my issues
Link to comment
Share on other sites

Ah thanks, now how do I execute, say the equivalent code left click would run or make the player jump in these events? Also the pointer that would be moved with the joystick, should I receive help here or stackoverflow? Not really sure which is the better place to ask for help on that

Link to comment
Share on other sites

12 minutes ago, diesieben07 said:

No. Did you even look at the event? It provides you with the MovementInput instance that the game uses for storing what the player should do.

I did not check due to being away from my development workspace at the moment, however as I see it, I use the instance of the event to jump by setting a value or calling a method, no? Same for walking or do I keep my old walk method and put it in the tick method or the moveinput event?

Link to comment
Share on other sites

20 hours ago, diesieben07 said:

Walking, jumping and sneaking should be handled through MovementInput.

I was able to look into InputUpdateEvent, however it's a 1.13 only thing as I see it. Did this exist before, if so, what is it's legacy name? I can't seem to find anything similar to it in the same package.

Link to comment
Share on other sites

4 minutes ago, diesieben07 said:

1.8.9 came out 3 1/2 years ago. It's no longer supported by Forge. There is no reason to use it.

Update.

There is one reason to use it, though it's not really that important anyways. It's mostly used for pvp, aka sword blocking. However have any ideas on mods that fix that problem on later versions like 1.13? Or any idea on how to achieve the same functionality without making it look somewhat fake (I know it's off topic, I might make another topic just for that)

Edited by fernthedev
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.