Jump to content

How to fire a function at right click


MGlolenstine

Recommended Posts

There are various events and methods used in different situations. What particular case do you want to use? E.g. using an item, right-clicking a block, interacting with an entity? Is it with something you control (i.e. you added the code in your own mod), or not (i.e. something from vanilla or another mod)?

 

In general, you can look through events in the net.minecraftforge.events package - most of them have good documentation and you can also use your IDE to find the places where a particular event is used.

  • Like 1
Link to comment
Share on other sites

Just now, Jay Avery said:

There are various events and methods used in different situations. What particular case do you want to use? E.g. using an item, right-clicking a block, interacting with an entity? Is it with something you control (i.e. you added the code in your own mod), or not (i.e. something from vanilla or another mod)?

 

In general, you can look through events in the net.minecraftforge.events package - most of them have good documentation and you can also use your IDE to find the places where a particular event is used.

Actually I was looking for using an item, thanks! Could I get the link to the documentation? I just don't seem to be able to get anything else than the ItemPickup event in this docs: https://mcforge.readthedocs.io/en/latest/events/intro/

 

Thanks once again!

Link to comment
Share on other sites

For an item you've coded, it's easier to override the onItemRightClick method in your item class. Otherwise you can use PlayerInteractEvent.RightClickItem. I don't know if there's a place in the docs website with all the events, but the event classes in the code have doc comments explaining how and when they are called.

  • Like 1
Link to comment
Share on other sites

12 minutes ago, Jay Avery said:

For an item you've coded, it's easier to override the onItemRightClick method in your item class. Otherwise you can use PlayerInteractEvent.RightClickItem. I don't know if there's a place in the docs website with all the events, but the event classes in the code have doc comments explaining how and when they are called.

Ok, so I tried to override the method...

My current WoodenRing.java is

package xyz.mglolenstine.SimpleRings;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.item.EntityTNTPrimed;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class WoodenRing extends Item{
    public WoodenRing(String registryName, int MaxStackSize){
        super();
        this.setRegistryName(registryName);
        this.setUnlocalizedName(registryName);
        this.setCreativeTab(CreativeTabs.REDSTONE);
        this.setMaxStackSize(MaxStackSize);
    }

    @Override
    public ItemStack onItemRightClick(ItemStack item, World world, EntityPlayer player) {
        return item;
    }
}

but the @Override has some interesting stuff to say...

5953cc7f1ea0d_Screenshotfrom2017-06-2817-34-08.png.48f3fe4cffbfc0b672f572b54bd0d5ad.png

Link to comment
Share on other sites

That means the method signature doesn't match the one in the superclass. Take a look in the Item class (net.minecraft.item.Item) and search for "onItemRightClick", and make sure you have exactly the same parameters and return type. The parameters and return type changed around a lot in the last few minecraft versions, so if you've looked at a different version it probably won't match.

  • Like 1
Link to comment
Share on other sites

7 minutes ago, Jay Avery said:

That means the method signature doesn't match the one in the superclass. Take a look in the Item class (net.minecraft.item.Item) and search for "onItemRightClick", and make sure you have exactly the same parameters and return type. The parameters and return type changed around a lot in the last few minecraft versions, so if you've looked at a different version it probably won't match.

I know that I'm gonna sound stupid because I don't know anything about this stuff, but where can I find that net.minecraft.item.Item class? In the build/tmp/recompileMc/compiled/net/minecraft/item I don't see any methods named "onItemRightClick" smh...

Link to comment
Share on other sites

In your workspace there should be something called "referenced libraries" or similar, and inside there will be a folder called something like forgeSrc. Here is what it looks like in my eclipse workspace:

5953d4352c155_ScreenShot2017-06-28at17_05_28.thumb.png.ffe580146ab86f6bc79c1e9098c6cfe8.png

You can also get directly to a particular class by right-clicking and choosing "open declaration" (again, in eclipse - but other IDEs should have something similar) - for example you can click on Item where your class extends it.

Link to comment
Share on other sites

1 minute ago, Jay Avery said:

In your workspace there should be something called "referenced libraries" or similar, and inside there will be a folder called something like forgeSrc. Here is what it looks like in my eclipse workspace:

5953d4352c155_ScreenShot2017-06-28at17_05_28.thumb.png.ffe580146ab86f6bc79c1e9098c6cfe8.png

You can also get directly to a particular class by right-clicking and choosing "open declaration" (again, in eclipse - but other IDEs should have something similar) - for example you can click on Item where your class extends it.

Aha, I see it now... Thanks!

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.

Announcements



×
×
  • Create New...

Important Information

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