Skip to content

Commit

Permalink
API: complete Divide armor wear amongst number of pieces worn
Browse files Browse the repository at this point in the history
* Added now complete feature of Divide armor wear amongst number of pieces worn
  backported minetest-mods@3ddcbae
* Added equip, unequip and get_weared_armor_elements functions
  backporting minetest-mods@294338d
* Added remove_all function
  backported from minetest-mods@e34635c
* finally fix for issue minetest-mods#24 but backported for minetest 0.4 and 4.0
  • Loading branch information
mckaygerhard committed Jan 9, 2022
1 parent e2e4963 commit 91a73dd
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
12 changes: 12 additions & 0 deletions 3d_armor/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ Adds wear to a single armor itemstack, triggers `on_damage` callbacks and
updates the necessary inventories. Also handles item destruction callbacks
and so should NOT be called from `on_unequip` to avoid an infinite loop.

armor:remove_all(player)

Removes all armors from the player's inventory without triggering any callback.

armor:equip(player, armor_name)

Equip the armor, removing the itemstack from the main inventory if there's one.

armor:unequip(player, armor_name)

Unequip the armor, adding the itemstack to the main inventory.

armor:update_skin(player_name)

Triggers a skin update with the same action as if a field with `skins_set` was submitted.
Expand Down
70 changes: 70 additions & 0 deletions 3d_armor/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,12 @@ end

armor.damage = function(self, player, index, stack, use)
local old_stack = ItemStack(stack)
local worn_armor = armor:get_weared_armor_elements(player)
local armor_worn_cnt = 0
for k,v in pairs(worn_armor) do
armor_worn_cnt = armor_worn_cnt + 1
end
use = math.ceil(use/armor_worn_cnt)
stack:add_wear(use)
self:run_callbacks("on_damage", player, index, stack)
self:set_inventory_stack(player, index, stack)
Expand All @@ -412,6 +418,70 @@ armor.damage = function(self, player, index, stack, use)
end
end

armor.get_weared_armor_elements = function(self, player)
local name, inv = self:get_valid_player(player, "[get_weared_armor]")
local weared_armor = {}
if not name then
return
end
for i=1, inv:get_size("armor") do
local item_name = inv:get_stack("armor", i):get_name()
local element = self:get_element(item_name)
if element ~= nil then
weared_armor[element] = item_name
end
end
return weared_armor
end

armor.equip = function(self, player, armor_name)
local name, inv = self:get_valid_player(player, "[equip]")
local weared_armor = self:get_weared_armor_elements(player)
local armor_element = self:get_element(armor_name)
if not name then
return
elseif self:get_element(armor_name) == nil then
return
elseif inv:contains_item("armor", ItemStack(armor_name)) then
return
end
if weared_armor[armor_element] ~= nil then
self:unequip(player, weared_armor[armor_element])
end
inv:add_item("armor", ItemStack(armor_name))
minetest.after(0, function() player:get_inventory():remove_item("main", ItemStack(armor_name)) end)
self:set_player_armor(player)
self:save_armor_inventory(player)
end

armor.unequip = function(self, player, armor_name)
local name, inv = self:get_valid_player(player, "[unequip]")
if not name then
return
elseif self:get_element(armor_name) == nil then
return
elseif not inv:contains_item("armor", ItemStack(armor_name)) then
return
end
inv:remove_item("armor", ItemStack(armor_name))
minetest.after(0, function() player:get_inventory():add_item("main", ItemStack(armor_name)) end)
self:set_player_armor(player)
self:save_armor_inventory(player)
self:save_armor_inventory(player)
end

armor.remove_all = function(self, player)
local name, armor_inv = self:get_valid_player(player, "[remove_all]")
local name, inv = self:get_valid_player(player, "[remove_all]")
if not name then
return
end
armor_inv:set_list("armor", {})
inv:set_list("armor", {})
self:set_player_armor(player)
self:save_armor_inventory(player)
end

armor.get_player_skin = function(self, name)
if (self.skin_mod == "skins" or self.skin_mod == "simple_skins") and skins.skins[name] then
return skins.skins[name]..".png"
Expand Down

0 comments on commit 91a73dd

Please sign in to comment.