Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Questions while studying events


WeAthFolD
 Share

Recommended Posts

I saw codes like this in ItemBow's code:

 

    public void onPlayerStoppedUsing(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer, int par4)
    {
        int var6 = this.getMaxItemUseDuration(par1ItemStack) - par4;

        ArrowLooseEvent event = new ArrowLooseEvent(par3EntityPlayer, par1ItemStack, var6);
        MinecraftForge.EVENT_BUS.post(event);
        if (event.isCanceled())
        {
            return;
        }

 

    public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
    	
        ArrowNockEvent event = new ArrowNockEvent(par3EntityPlayer, par1ItemStack);
        MinecraftForge.EVENT_BUS.post(event);
        if (event.isCanceled())
        {
            return event.result;
        }

 

Judging from the code, I think those two events' function is to determine whether the movement of shooting arrow is normally continuing, if not the arrow will not be shot and action is cancelled.

 

but the two events' classes are so simple that I don't really understand how the func is done.

 

Question 1 : When and where are the events "setCancel"ed, and how does Forge EventBus handle this during the player's movement?

Question 2: How do I do similar task in my Items? Do I need to just simply copy and paste the event code and do "MinecraftForge.EVENT_BUS.register()"?

Question 3: What can I do with events mainly ?

 

Thanks a lot for you time of reading this topic and answering ;D

Link to comment
Share on other sites

The way events work is that you register "Listener" methods to the EventBus. These methods then get called by the EventBus when a new Event is posted.

Example:

public class Foo {
    public Foo() {
        MinecraftForge.EVENT_BUS.register(this);
    }

    @ForgeSubscribe
    public void onLivingDeath(LivingDeathEvent event) {
        if (event.living instanceof EntityCreeper) {
            event.setCanceled(true);
        }
    }
}

That will make Creepers unable to die.

 

Spread out over the Minecraft Code there are calls like this:

return MinecraftForge.EVENT_BUS.post(new LivingDeathEvent(entity, src));, sometimes wrapped in Helper methods in ForgeEventFactory. Whenever that happens, all the Methods registered for that event get executed. If the event is cancelled, the action is then also cancelled.

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

 Share



×
×
  • Create New...

Important Information

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