Jump to content

Recommended Posts

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  ;)

Posted

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)

Posted

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

Posted

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.

Posted

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

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