Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

I've noticed that in some weapon-related mods such as iChun's portal gun, player can use both left key and right key to shoot.

I found this really good, so I started trying doing something similar. Having tried custom KeyCode and simple processing, all them works well. However I can't manage to find the related function to abort player's clicking action on left click.

I've tried all the codes below :

    public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity entity)
    {
    	return true;
    }
    
    public boolean onEntitySwing(EntityLiving entityLiving, ItemStack stack)
    {
    	return true;
    }
    
    public EnumAction getItemUseAction(ItemStack par1ItemStack)
    {
    	return EnumAction.none;
    }
    
    public boolean onBlockStartBreak(ItemStack itemstack, int i, int j, int k, EntityPlayer player)
    {
      return true;
    }
    
    public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ)
    {
      return true;
    }

 

Also tried this in EventHandler :

  @ForgeSubscribe
  public void onInteract(EntityInteractEvent event)
  {
	  ItemStack curItem = event.entityPlayer.getCurrentEquippedItem();
	  if(curItem != null && curItem.getItem() instanceof MyItem) {
                            event.setCanceled(true);
	  }
  }

But the result isn't what I expected. Though left clicking won't attack mobs or break blocks anymore, The player's right hand always keep that 'swinging' animation when I click the left mouse button.

What should I do if I want to abort the left-clicking action? Is there something I should do with EntityPlayer class or custom render?

Thanks a lot for your time reading and answering  ;)

This,placed in your item, prevents the swing animation from both clicks. That should do, combined with your cancelled event.

public boolean onEntitySwing(EntityLivingBase entityLiving, ItemStack stack)
    {
/*you can add your custom item use action for both clicks here*/
    	return true;
    }

 

    public EnumAction getItemUseAction(ItemStack par1ItemStack)
    {
    	return EnumAction.none;
    }

This prevents any animation on right click. (it is already in Item by default by the way)

  • Author

Aha, I think I've figured this out.

I tried System.out.println() in onEntitySwing(), however nothing appeared on the console in any situation.

I belive that this function is not called in update codes by mistake (Or for some special reason?)

in EntityLiving, there's a boolean named isSwingInProgress, so I tried the code below :

@Override
public void onUpdate(ItemStack par1ItemStack, World par2World,
		Entity par3Entity, int par4, boolean par5) {
	super.onUpdate(par1ItemStack, par2World, par3Entity, par4,par5);
	if(par5 && par3Entity instanceof EntityLiving) { //par5:isCurrentHeldItem
		((EntityLiving)par3Entity).isSwingInProgress = false;
	}
}

EDIT: Tested in forge 1.5.2.

Then all of the function works well. Binding with canceling event and abort interacting, this is the effect that I exactly want :D

Thanks all for answering again> >

  • Author

And please post your result... I am very curious myself.

with my code, the swing animation stays the same, but player can't break blocks or attack mobs anymore, nor interacting with them.

Aha, I think I've figured this out.

I tried System.out.println() in onEntitySwing(), however nothing appeared on the console in any situation.

I belive that this function is not called in update codes by mistake (Or for some special reason?)

That function is called if you use the right arguments. See my code for a working example.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.