Jump to content

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


Recommended Posts

Posted (edited)

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
Posted

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

Posted

Amazing tips and help you've given me. So in the movement event I can call jump/sneak methods and use my move methods? Sorry for my repetition, I just need that fully clarified 

Posted
  On 5/26/2019 at 3:50 PM, 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.

Expand  

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?

Posted
  On 5/26/2019 at 4:06 PM, diesieben07 said:

Walking, jumping and sneaking should be handled through MovementInput.

Expand  

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.

Posted (edited)
  On 5/27/2019 at 1:00 PM, 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.

Expand  

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

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Without Network protocol fix mod, I get kicked with a Network Protocol error when on LAN. Also, both of these issues are caused by a Null Pointer Exception/Screen cannot be null in a "Client Bound Player Combat Kill Packet".
    • You need a new "items" folder at  resources/assets/yourmodid/ there you add for every item model a .json file with the exact item/block name and fill it like this if it's an item: { "model": { "type": "minecraft:model", "model": "yourmodid:item/youritem" } } and so if its a block: { "model": { "type": "minecraft:model", "model": "yourmodid:block/youritem" } } There is also a generator for it you can do namy crazy things with it which replaces the previous hard coded Item Properties implementaion method (Bow pulling animation for example). https://misode.github.io/assets/item/
    • Hello! I'm playing a modpack (custom made) with some friends, and we have the server running on BisectHosting. We encountered a bug with an entity from The Box Of Horrors mod, that would crash the game whenever someone nearby it would log in. We tried to fix it by: 1) Editing the player.dat files to change the location of the affected players (something I had done successfully previously) 2) Updating the version of the mod we had (0.0.8.2) to the latest alpha version (0.0.8.3 However, after doing both of those, none of us are able to join the server, and we get the following errors: Server side: https://pastebin.com/J5sc3VQN Client side: Internal Server Error (that's basically all I've gotten) Please help! I've tried restoring the player data to how it was before I made the changes (Bisect allows you to restore deleted files) and deleting all of my player data files and I still get the same error. Deleting Box Of Horrors causes the error: Failed to load datapacks, cannot continue with server load.
    • Hey there! I'm trying to create a simple mod for Forge 1.21.1 that adds a few custom banner patterns that don't require any banner pattern items. To be completely honest, this is my first time modding for Minecraft, so after setting up the project in Intellij, I copied the parts of the source code from this mod on CurseForge that dealt with adding and registering banner patterns, including the lang and banner_pattern .json files. From what I understand, to add a banner pattern that doesn't require a banner pattern item, I only needed to create the registries for each pattern like in here and then register it in the main java class like here on Line 54. Additionally, in the lang/en_us.json file, add in the names for each respective banner color, and in the data/minecraft/tags/banner_pattern/no_items_required.json file, add each banner pattern that does not require a banner pattern item to make a banner. The project is able to compile when loading in Forge which makes me assume that the file structure I have is correct, but on loading a Minecraft world, this error appears in console and the loom is subsequently blank. [Worker-Main-1/ERROR] [minecraft/TagLoader]: Couldn't load tag minecraft:no_item_required as it is missing following references: *lists every added entry in no_item_required.json* The message clearly states something went wrong regarding when trying to load in the registries from the mod, but I have no clue what could be wrong with the code I have. Attached below are screenshots of what I currently have. Java Main Class Banner Registry Class File Structure Error in Console upon loading a Minecraft world What the loom shows without minecraft:no_item_required    The original mod I copied still functions completely, so if anyone can figure out why the registries for the mod I'm making isn't working, that would be greatly appreciated!    
    • Please someone help me to know how can I fix this generation error in the trees! I already uninstalled and reinstalled several mods in my modpack and can't figure out what is causing it.    
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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