Changes

79 bytes removed ,  13:52, 12 January 2022
Update to 1.18
Line 13: Line 13:     
== Creating Your Own Capability ==
 
== Creating Your Own Capability ==
In general terms, a capability is declared and registered through a single method call to <code><nowiki>CapabilityManager#INSTANCE#register</nowiki></code>. One possibility is to define a static <code><nowiki>register</nowiki></code> method inside a dedicated class for the capability, but this is not required by the capability system.
+
In general terms, a capability is registered through the event <code><nowiki>RegisterCapabilitiesEvent</nowiki></code> on the <code>MOD</code> event bus via the <code>#register</code> method.
    
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
CapabilityManager.INSTANCE.register(IExampleCapability.class);
+
@SubscribeEvent
 +
public void registerCaps(RegisterCapabilitiesEvent event) {
 +
    event.register(IExampleCapability.class);
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
   Line 50: Line 53:  
By default, the capability data does not persist on death. In order to change this, the data has to be manually copied when the player entity is cloned during the respawn process.
 
By default, the capability data does not persist on death. In order to change this, the data has to be manually copied when the player entity is cloned during the respawn process.
   −
This can be done by handling the <code><nowiki>PlayerEvent$Clone</nowiki></code> event, reading the data from the original entity, and assigning it to the new entity. In this event, the <code>wasDead</code> field can be used to distinguish between respawning after death, and returning from the End. This is important because the data will already exist when returning from the End, so care has to be taken to not duplicate values in this case.
+
This can be done by handling the <code><nowiki>PlayerEvent$Clone</nowiki></code> event, reading the data from the original entity, and assigning it to the new entity. In this event, the <code>#isWasDeath</code> method can be used to distinguish between respawning after death, and returning from the End. This is important because the data will already exist when returning from the End, so care has to be taken to not duplicate values in this case.
       
[[Category:Data Storage]]
 
[[Category:Data Storage]]