User Tools

Site Tools


tutorial:events

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
tutorial:events [2019/07/03 00:22] – clarified sheep setSheared method draylartutorial:events [2019/07/03 00:31] – tell people to return PASS in listeners draylar
Line 9: Line 9:
  
 ==== Creating a Callback Interface ==== ==== Creating a Callback Interface ====
-The callback interface is the core of your event. It stores a list of all registered listeners (which are like responses to an event)and calls them all when the action occurs. Each listener can then say "cancel this," "approve this," or "I don't care, leave it to the next guy," while doing whatever response they need to.+The callback interface is the core of your event. It stores a list of all registered listeners (which are like responses to an event) and calls them all when the action occurs. Each listener can then say "cancel this," "approve this," or "I don't care, leave it to the next guy," while doing whatever response they need to.
  
 You'll need to create an interface that has an ''Event'' instance and method for response implementation. A basic setup for our sheep shear callback is: You'll need to create an interface that has an ''Event'' instance and method for response implementation. A basic setup for our sheep shear callback is:
Line 50: Line 50:
  
 return ActionResult.PASS; return ActionResult.PASS;
 +</code>
 +
 +In the [[https://github.com/FabricMC/fabric|Fabric API]], we add Javadoc comments to the top of callback classes to document what each ActionResult does. In our case, it might be:
 +<code java [enable_line_numbers="false"]>
 +/**
 + * Callback for shearing a sheep.
 + * Called before the sheep is sheared, items are dropped, and items are damaged.
 + * Upon return:
 + * - SUCCESS cancels further processing and continues with normal shearing behavior.
 + * - PASS falls back to further processing and defaults to SUCCESS if no other listeners are available
 + * - FAIL cancels further processing and does not shear the sheep.
 +/**
 </code> </code>
  
Line 84: Line 96:
 }); });
 </code> </code>
-Note that this event also sets the sheep to be sheared manually, as it is normally canceled if we return FAIL.+Note that this event also sets the sheep to be sheared manually, as it is normally canceled if we return FAIL. If you don't //need// to cancel the event, make sure you return ''PASS'' so other listeners are allowed to operate as well. Failing to follow these "not spoken rules" may result in angry modders on your doorstep.
  
 If you enter into your game and shear a sheep, a diamond should drop instead of wool. If you enter into your game and shear a sheep, a diamond should drop instead of wool.
 {{https://i.imgur.com/dG73Z6G.mp4?400|}} {{https://i.imgur.com/dG73Z6G.mp4?400|}}
tutorial/events.txt · Last modified: 2021/05/28 00:31 by 127.0.0.1