Advanced Functionality - Swackyy/Ohmega GitHub Wiki
Active state
Using the AccessoryHelper
(1.21) class, we can allow an accessory to be "toggled" on and off by any means necessary, usually through the use of a key-bind.
To do this, we must first override the onUse
function:
(From the example Angel Ring accessory item (1.21 source))
// Toggle the accessory being active when the keybind is pressed
@Override
public void onUse(Player player, ItemStack stack) {
AccessoryHelper.toggle(player, stack);
}
Now, every time the key-bind is pressed, data on the item containing its active state will be toggled, however it will remain active when unequipped, which may not be desired.
This can be fixed by overriding the onUnequip
method:
// Deactivates when unequipped
@Override
public void onUnequip(Player player, ItemStack stack) {
AccessoryHelper.deactivate(player, stack);
}
If you desire to immediately activate the accessory when it is equipped, you can also do so by overriding the onEquip
function and calling the AccessoryHelper.activate
function.
Usages of the active state
You can also change the behaviour of your accessory in any way needed depending on if it is active or not (of course if you couldn't, it'd be pointless), using the AccessoryHelper.isActive
function
One common use for this is to apply the enchantment glint onto the item when equipped:
(From the example Angel Ring accessory item (1.21 source))
// Makes the accessory have the enchanted glint when equipped
// No super as it may be confusing if active when enchanted, also it is not intended to have any enchantments.
@Override
public boolean isFoil(@NotNull ItemStack stack) {
return AccessoryHelper.isActive(stack);
}
Attribute Modifiers
Default attribute modifiers can be applied either when the accessory is equipped, or only when it is active (see Active state section above)
To do this, override the addDefaultAttributeModifiers
function, which supplies you with a ModifierBuilder
(custom class) to easily do this.
- You can use the
ModifierBuilder#addModifier
function to add an effect when the accessory is equipped - You can use the
ModifierBuilder#addModifierActiveOnly
function to add an effect only when the accessory is active
An example of this can be seen below:
(From the example Angel Ring accessory item (1.21 source))
// Adds modifiers to be applied when the accessory is equipped
@Override
public void addDefaultAttributeModifiers(ModifierBuilder builder) {
// This modifier is only applied when the accessory is active
builder.addModifier(Attributes.ATTACK_DAMAGE, new AttributeModifier(ResourceLocation.fromNamespaceAndPath(Ohmega.MODID, "angel_ring.effect.strength"), 1, AttributeModifier.Operation.ADD_MULTIPLIED_BASE));
// This modifier is always applied
builder.addModifierActiveOnly(Attributes.MAX_HEALTH, new AttributeModifier(ResourceLocation.fromNamespaceAndPath(Ohmega.MODID, "angel_ring.effect.health_boost"), 0.2, AttributeModifier.Operation.ADD_MULTIPLIED_BASE));
}
Events
As of the v1.2.0 update, 8 events can be used to further manipulate the behaviour of accessories, even those from another mod. A simple understanding of the Forge Event system is required with these.
The event types are listed below, with descriptions:
BindAccessoriesEvent
(1.2.0+) -> Allows you to bind vanilla or other mods' items to anIAccessory
, making it equippable in the accessory slots. Here is an exampleAccessoryAttributeModifiersEvent
-> Allows you to add or remove attribute modifiers, covered here, to/from accessoriesAccessoryCanEquipEvent
-> Allows you to decide if an accessory can be equippedAccessoryCanUnequipEvent
-> Allows you to decide if an accessory can be unequippedAccessoryEquipEvent
-> Add extra code here to run when an accessory is equippedAccessoryTickEvent
(HasPRE
andPOST
phase) -> Called once per tick (1/20th of a second) per accessory that is equippedAccessoryUnequipEvent
-> Add extra code here to run when an accessory is unequippedAccessoryUseEvent
-> Called when the key-bind corresponding to an accessory is pressed
You can see all of these events and JavaDocs above some of their classes in this directory (1.21 branch)