Class BagHealth
Bag durability is stored on the bag ItemStack using PDC keys:
size- bag slot count (used to resolve max health)damage- accumulated damage points
maxHealth - damage.
Configuration is loaded from protect-bags.bag-health:
enabled- whether the system is activehealth.default- default max health for any bag size without an explicit overridehealth.<size>- per-size max health overrides (where<size>is the bag slot count)damage-delay- minimum time (seconds) between applying damage inisBagSafe(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 -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddDamage(org.bukkit.inventory.ItemStack bag, int damageToAdd) Adds the specified amount of damage to the bag's current damage value.static intgetCurrentHealth(org.bukkit.inventory.ItemStack bag) Gets the current health of the bag.static intgetDamage(org.bukkit.inventory.ItemStack bag) Gets the current damage value of the bag from its PersistentDataContainer.static intgetMaxHealth(org.bukkit.inventory.ItemStack bag) Gets the maximum health of the bag based on its size.static voidinit()Initializes the bag health system by loading the configuration values.static booleanisBagSafe(org.bukkit.entity.Item bag) Checks the bag's health and applies damage if necessary.static booleanChecks if bag health is enabled in the config.static voidreload()Reloads the bag health configuration from the plugin config.static voidsetDamage(org.bukkit.inventory.ItemStack bag, int damage) Sets the damage value of the bag in its PersistentDataContainer.
-
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.enabledprotect-bags.bag-health.health.defaultprotect-bags.bag-health.health.<size>(any numeric keys underhealth)protect-bags.bag-health.damage-delay
default) will be ignored with a warning. -
isEnabled
public static boolean isEnabled()Checks if bag health is enabled in the config.- Returns:
trueif bag health is enabled, otherwisefalse
-
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 fordamage- 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 todamageToAdd- 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,defaultHealthis 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:
trueif the bag is still healthy after applying damage (or during cooldown), orfalseif it is broken
-