Problem:
The error occurs when you click with the left mouse button while interacting with equipment, and it seems related to trying to assign a negative value to a player's property (like health or inventory slots). The error message indicates that the value being assigned must be greater than or equal to 0. The right mouse button works fine, suggesting a different event handling for each button.
Solution:
The solution focuses on validating values before they are assigned to the player's properties to ensure they are never less than zero.
Key Changes:
Validation Function:
A new function validate_and_apply_property was added. This function checks if the value to be assigned is negative, and if it is, it sets the value to 0. This ensures that no property is set to a negative value.
Updated Equipment Processing:
In the function process_equipment_change, where equipment is processed and properties are updated based on the player's gear, the new validation function is used. This ensures that when equipment changes, the modified values (e.g., health bonus, inventory slots) are validated and applied correctly.
Handling on_player_armor_inventory_changed:
This event, triggered by inventory changes (which includes left-click interactions), was updated to include this validation logic before applying changes to the player's properties, preventing any negative values from being assigned.
Code Snippet for Validation:
lua
local validate_and_apply_property = function(player, property, value)
local valid_value = math.max(0, value) -- Ensures value is >= 0
if valid_value ~= value then
Adamo.debug("Invalid value for " .. property .. " corrected to " .. valid_value)
end
Adamo.player.add.to_property(player, property, valid_value)
return valid_value
end
This function ensures that any value being applied to the player's properties (e.g., health bonus, inventory slots) is never less than 0. If the value is negative, it gets corrected to 0.
Conclusion:
By adding validation before applying changes to the player's properties, the solution ensures that no negative values can be set, thus preventing the error that occurs when using the left mouse button. The code now safely handles changes to the player's equipment and stats, ensuring that they stay within valid ranges.