Class BagHealth

java.lang.Object
valorless.havenbags.features.BagHealth

public class BagHealth extends Object
Tracks and manages "bag health" (durability) for HavenBags.

Bag durability is stored on the bag ItemStack using PDC keys:

  • size - bag slot count (used to resolve max health)
  • damage - accumulated damage points
Current health is calculated as maxHealth - damage.

Configuration is loaded from protect-bags.bag-health:

  • enabled - whether the system is active
  • health.default - default max health for any bag size without an explicit override
  • health.<size> - per-size max health overrides (where <size> is the bag slot count)
  • damage-delay - minimum time (seconds) between applying damage in isBagSafe(Item)

To avoid rapidly applying damage from frequent events (e.g. EntityDamageEvent), isBagSafe(Item) is rate-limited using an in-memory timestamp map.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    addDamage(org.bukkit.inventory.ItemStack bag, int damageToAdd)
    Adds the specified amount of damage to the bag's current damage value.
    static int
    getCurrentHealth(org.bukkit.inventory.ItemStack bag)
    Gets the current health of the bag.
    static int
    getDamage(org.bukkit.inventory.ItemStack bag)
    Gets the current damage value of the bag from its PersistentDataContainer.
    static int
    getMaxHealth(org.bukkit.inventory.ItemStack bag)
    Gets the maximum health of the bag based on its size.
    static void
    Initializes the bag health system by loading the configuration values.
    static boolean
    isBagSafe(org.bukkit.entity.Item bag)
    Checks the bag's health and applies damage if necessary.
    static boolean
    Checks if bag health is enabled in the config.
    static void
    Reloads the bag health configuration from the plugin config.
    static void
    setDamage(org.bukkit.inventory.ItemStack bag, int damage)
    Sets the damage value of the bag in its PersistentDataContainer.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BagHealth

      public BagHealth()
  • Method Details

    • init

      public static void init()
      Initializes the bag health system by loading the configuration values.

      This method should be called during plugin startup to ensure that the health values and enabled state are set before any bags are processed.

    • reload

      public static void reload()
      Reloads the bag health configuration from the plugin config.

      Reads:

      • protect-bags.bag-health.enabled
      • protect-bags.bag-health.health.default
      • protect-bags.bag-health.health.<size> (any numeric keys under health)
      • protect-bags.bag-health.damage-delay
      Non-numeric keys (besides default) will be ignored with a warning.
    • isEnabled

      public static boolean isEnabled()
      Checks if bag health is enabled in the config.
      Returns:
      true if bag health is enabled, otherwise false
    • getDamage

      public static int getDamage(org.bukkit.inventory.ItemStack bag)
      Gets the current damage value of the bag from its PersistentDataContainer.

      The value is stored under the PDC key "damage". If no damage is stored, returns 0.

      Parameters:
      bag - The ItemStack representing the bag to check for damage
      Returns:
      The current damage value of the bag, or 0 if unset
    • setDamage

      public static void setDamage(org.bukkit.inventory.ItemStack bag, int damage)
      Sets the damage value of the bag in its PersistentDataContainer.

      The value is stored under the PDC key "damage".

      Parameters:
      bag - The ItemStack representing the bag to set damage for
      damage - The damage value to set for the bag
    • addDamage

      public static void addDamage(org.bukkit.inventory.ItemStack bag, int damageToAdd)
      Adds the specified amount of damage to the bag's current damage value.

      If the bag has no current damage, it will be treated as 0.

      Parameters:
      bag - The ItemStack representing the bag to add damage to
      damageToAdd - The amount of damage to add to the bag's current damage value
    • getCurrentHealth

      public static int getCurrentHealth(org.bukkit.inventory.ItemStack bag)
      Gets the current health of the bag.

      Current health is calculated as maxHealth - damage. The max health is resolved by bag size (stored under PDC key "size") using a per-size override when present, otherwise the default.

      Parameters:
      bag - The ItemStack representing the bag to get current health for
      Returns:
      The current health of the bag
    • getMaxHealth

      public static int getMaxHealth(org.bukkit.inventory.ItemStack bag)
      Gets the maximum health of the bag based on its size.

      Bag size (slot count) is read from the PDC key "size". If there is no configured value for that size, defaultHealth is returned.

      Parameters:
      bag - The ItemStack representing the bag to get max health for
      Returns:
      The maximum health of the bag based on its size
    • isBagSafe

      public static boolean isBagSafe(org.bukkit.entity.Item bag)
      Checks the bag's health and applies damage if necessary.

      This method is intended to be called from damage events for dropped bag entities. Damage application is rate-limited by protect-bags.bag-health.damage-delay.

      Parameters:
      bag - The dropped item entity representing the bag to check and apply damage to
      Returns:
      true if the bag is still healthy after applying damage (or during cooldown), or false if it is broken