Items
Every interactable item will be its own class that extends VitalItem and is annotated with @VitalItem.Info.
@VitalItem.Info(name = "My item", type = Material.COBBLESTONE)
public class MyItem extends VitalItem {
}
Now you can give this item to a player, e.g., when he joins the server.
@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
e.getPlayer().getInventory().setItem(0, myItem.getItemStack(e.getPlayer()));
}
Note that we have to call the
getItemStackmethod of ourMyIteminstance to get a copy of the item that our class defines.
Now if we want to react to an interaction, like a right-click, we can use the onRightClick lifecycle.
@VitalItem.Info(name = "My item", type = Material.COBBLESTONE)
public class MyItem extends VitalItem {
@Override
public void onRightClick(PlayerInteractEvent e) {
// ...
}
}
Cooldowns
Items also support cooldowns.
Every cooldown is tied to the player that interacted with the item and will automatically work right out of the box without you having to configure anything.
Just define the cooldown in your info annotation (millis).
@VitalItem.Info(cooldown = 1_000)
Localization
Plugins that also use vital-localization can benefit from localized items that are displayed and automatically updated when a player changes his locale!
Simply set the item name in your info annotation to your resource key, and Vital will automatically display the correct translated version of your item name, just how you provided it in your messages_*.properties files.
@VitalItem.Info(name = "servername.pluginname.item.myitem")
When you change the locale of a player via
VitalLocalizationModule.Spigot.setVitalLocale(Player, Locale), Vital will automatically translate all items of the player's inventory.
Lifecycles
| Lifecycle | Usage |
|---|---|
onLeftClick(PlayerInteractEvent) | Perform an action when this item is left-clicked while not on cooldown. |
onRightClick(PlayerInteractEvent) | Perform an action when this item is right-clicked while not on cooldown. |
onCooldown(PlayerInteractEvent) | Perform an action when this item is left- or right-clicked while on cooldown. |
onCooldownTick(Player) | Perform an action when the cooldown of this item is ticked (every 50ms / 1 game tick). |
onCooldownExpire(Player) | Perform an action when the cooldown of this item reaches 0. |