Jump to content

Recommended Posts

Posted

All of you faced this problem. And now me, too. I wrote some code to catch Keyboard events. like:

 

@SideOnly(Side.CLIENT)
@SubscribeEvent(priority = EventPriority.NORMAL, receiveCanceled = true)
public void onEvent(InputEvent.KeyInputEvent event) {
    if (itemIsHoldinHand.equals(ItemHoldInHand.NONE)) {
        //Do nothing if it is not hold
        return;
    }

    String keyBindingDescKey = "key.attack";
    // make local copy of key binding array
    KeyBinding[] keyBindings = Minecraft.getMinecraft().gameSettings.keyBindings;
    for (KeyBinding binding : keyBindings) {
        if (keyBindingDescKey.equals(binding.getDescriptionKey()) && /*IMPORTANT to do this on second check because its counter based and will reset other key actions when it hits*/ binding.isPressed()) { 
            return;
        }
    }
}

 

This code works well as the attack key is bound to a keyboard key like: R, A, ...

 

But at default the attack key is bound to 

 

KeyCode: -100 (Left mouse button)

 

But thats not the main problem, I knew that there is another event for catching mousecation like:

 

@SideOnly(Side.CLIENT)
@SubscribeEvent(priority = EventPriority.NORMAL, receiveCanceled = true)
public void onEvent(InputEvent.MouseInputEvent event) {
    if (itemIsHoldinHand.equals(ItemHoldInHand.NONE)) {
        //Do nothing if it is not hold
        return;
    }

    ...

    String keyBindingDescKey = "key.attack"; 
    // make local copy of key binding array  
    KeyBinding[] keyBindings = Minecraft.getMinecraft().gameSettings.keyBindings;  
    for (KeyBinding binding : keyBindings) {
       if (keyBindingDescKey.equals(binding.getDescriptionKey()) && /*This same pattern never hits....*/ binding.isPressed())
       { 
          return;  
       } 
    }
}

 

But doing the same thing as before results in nothing. 

Even trying to do:
 

@SideOnly(Side.CLIENT)
@SubscribeEvent(priority = EventPriority.NORMAL, receiveCanceled = true)
public void onEvent(InputEvent.MouseInputEvent event) {
    if (itemIsHoldinHand.equals(ItemHoldInHand.NONE)) {
        //Do nothing if it is not hold
        return;
    }
    boolean isPressed = Minecraft.getMinecraft().gameSettings.keyBindAttack.isPressed();
    if(isPressed){
        System.out.println("Hell yeah");
    }
}

 

Is getting me no result. So I am asking you guys . Is there any pattern, how to or possibility to access the left mouse click (user friendly)

PS: I did add it to the EVENT_BUS
 

    MinecraftForge.EVENT_BUS.register(ModItems.ItemGun);

 

  • Like 1
Posted

keyBindAttack doesn't work because the left click is already processed as attack. The game resets the pressed flag to false after the process, so you can't detect it when it's bound to attack.

So do you want to detect attacking? If what you need is checking whether left click button is clicked or not, you can use KeyBinding::isKeyDown().

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Posted
7 minutes ago, NoFuchsGavin said:

@Abastro So instead of using isPressed on the attack keybinding, isKeyDown() will work?

Yes. One drawback is that it can be detected continuously. I think you should take care of it manually.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Posted

@Abastro I think this will not work as I expect. I thought it's possible to override the attack that I can use an item like I wanna to. Pistol -> shoot.

 

So I need to override the default logic for the attack:

 Do not hit the block infront of the player
 Do no animation for hitting 
....

Posted
2 minutes ago, NoFuchsGavin said:

@Abastro I think this will not work as I expect. I thought it's possible to override the attack that I can use an item like I wanna to. Pistol -> shoot.

There's easier way for that: subscribe event: PlayerInteractEvent.LeftClickEmpty. Do what you want there.

When you want to block hitting an entity, you can just override Item::onLeftClickEntity.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Posted
Spoiler

There's easier way for that: subscribe event: PlayerInteractEvent.LeftClickEmpty. Do what you want there.

What is about remapping keys for attack. Attack on "H" so this will not be on the LeftClick anymore.

Posted
12 minutes ago, NoFuchsGavin said:

What is about remapping keys for attack. Attack on "H" so this will not be on the LeftClick anymore.

Actually the name of the event is somewhat misleading. It is always called when attack key/mouse is pressed.

Same for Item::onLeftClickEntity.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

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.