Jump to content

{Solved} Right click action going through twice


BigSpidey

Recommended Posts

My onItemRightClick event keeps going through twice, and even with diagnostics I can't find out why. Any help is appreciate, thank you! {note that many of the contents of this code are simply placeholders}

....................................................................

public class BakugoQuirk extends ItemBase 
{
    public BakugoQuirk(String name) 
    {
        super(name);
        this.setMaxDamage(500);
        this.isDamageable();
    }
    
    public void onUpdate(ItemStack item, World worldIn, Entity playerIn)
    {
        if (item.getItemDamage() != 0)
        {
            item.setItemDamage(499);    //Item damage gain per tick
        }
    }
    
    public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) 
    {
        ItemStack item = playerIn.getHeldItem(handIn);
        Vec3d aim = playerIn.getLookVec();
        
        System.out.println("testing for first condition, ItemDamage is " + item.getItemDamage());
        if(item.getItemDamage() == 0)
        {
            playerIn.sendMessage(new TextComponentString("100% charge attack"));
            EntityLargeFireball fireball = new EntityLargeFireball(worldIn, playerIn, 1,1,1);
            fireball.setPosition(playerIn.posX + aim.x * 1.5D, playerIn.posY + aim.y * 1.5D , playerIn.posZ + aim.z * 1.5D);
            fireball.accelerationX = aim.x * 0.1; 
            fireball.accelerationY = aim.y * 0.1; 
            fireball.accelerationZ = aim.z * 0.1; 
            worldIn.spawnEntity(fireball);
            item.setItemDamage(499);
            System.out.println("ItemDamage is " + item.getItemDamage() + " after test");
        }
        else if(item.getItemDamage() > 0)
        {
            System.out.println("Going into second condition with " + item.getItemDamage() + " Item Damage");
            EntityLargeFireball fireball = new EntityLargeFireball(worldIn, playerIn, 1,1,1);
            fireball.setPosition(playerIn.posX + aim.x * 1.5D, playerIn.posY + aim.y * 1.5D , playerIn.posZ + aim.z * 1.5D);
            fireball.accelerationX = aim.x * 0.1; 
            fireball.accelerationY = aim.y * 0.1; 
            fireball.accelerationZ = aim.z * 0.1; 
            worldIn.spawnEntity(fireball);
            playerIn.sendMessage(new TextComponentString("50%-100% charge attack"));
            item.setItemDamage(item.getMaxDamage() - 1);
        }
        
        

    
    
        return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, item);
    }

 

 

 

EDIT {SOLVED}:

Just ended up adding a broad 

"if (worldIn.isRemote == false )"

over much of the code including the entity spawn and the chat message

Edited by BigSpidey
Link to comment
Share on other sites

The method is likely being called once on the server and once on the client. Add a println statement and check if world.isRemote is true or false. True means it's the client, false means it's the server. My guess is you probably need it to run on both sides, so there's no problem.

  • Like 1
Link to comment
Share on other sites

It’s also fired for both hands I believe

Edited by Cadiboo
  • Like 1

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

7 hours ago, Cadiboo said:

It’s also fired for both hands I believe

You are confusing this method with the right click event. The event will indeed fire for both hands. The Item#onItemRightClick will only fire for the hand holding the item.

 

15 hours ago, BigSpidey said:

            worldIn.spawnEntity(fireball);

You can't do this without first checking whether the side the action is happening on is a server. Otherwise it will create a "ghost" entity on the client that will stay there untill relog. As @Daeruin said

7 hours ago, Daeruin said:

check if world.isRemote is true or false. True means it's the client, false means it's the server

 

15 hours ago, BigSpidey said:

            playerIn.sendMessage(new TextComponentString("100% charge attack"));

Same with this, you need to check the side. Otherwise you are sending the message twice.

  • Like 1
Link to comment
Share on other sites

3 hours ago, V0idWa1k3r said:

You are confusing this method with the right click event. The event will indeed fire for both hands. The Item#onItemRightClick will only fire for the hand holding the item.

 

You can't do this without first checking whether the side the action is happening on is a server. Otherwise it will create a "ghost" entity on the client that will stay there untill relog. As @Daeruin said

 

Same with this, you need to check the side. Otherwise you are sending the message twice.

Thanks to everyone here, I have solved the problem! 

Link to comment
Share on other sites

Please post your final code and mark the topic as solved to help people in the future

  • Like 1

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

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.



×
×
  • Create New...

Important Information

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