Jump to content

Check if player has specific item equipped with hasEquipped()


Recommended Posts

So, stuck on a mod so I'll try here again. I want to check if an item is equipped and I've tried stuff like:

 

local sHockeyMask = "Base.Hat_HockeyMask";
--local sHockeyMask = "Hat_HockeyMask";

if getPlayer():hasEquipped(sHockeyMask) then
    getPlayer():addLineChatElement("Entering JSON mode", 0.3 , 0.6 , 0.9);
end

 

But regardless of what name i use (with or without module name), this doesn't trigger at all when i equip/unequip the mask.

 

So, do i really have to loop through the InventoryItems and check 1) if i have a mask and 2) if it is equipped? I thought hasEquipped() would solve it... According to the wiki, this is how it is supposed to be done and i've found other code examples that works likes this on ISOGameCharacter:

public boolean hasEquipped(String itemType)

 

Link to comment
Share on other sites

NVM: Figured out how to loop through an inventory container.

 

local itemContainer = getPlayer():getInventory();
local items = itemContainer:getItems();
for i = 0, items:size() -1, 1
do
	local item = items:get(i);
	if item:getName() == "Hockey Mask" then	-- I'm guessing display name or something, it is *not* shown in scripts definition of clothing/hats.
		if item:isEquipped() then
			getPlayer():addLineChatElement("JSON mode is on!", 0.9 , 0.9 , 0.6);
		end      
	end
end

 

For others with the same problem, the important lesson here is that the instance item like

 

item:getName()

 

will be "Hockey Mask" and not "Base.Hat_HockeyMask" or "Hat_HockeyMask".

Link to comment
Share on other sites

2 minutes ago, Ichinin said:

NVM: Figured out how to loop through an inventory container.

 

local itemContainer = getPlayer():getInventory();
local items = itemContainer:getItems();
for i = 0, items:size() -1, 1
do
	local item = items:get(i);
	if item:getName() == "Hockey Mask" then	-- I'm guessing display name or something, it is *not* shown in scripts definition of clothing/hats.
		if item:isEquipped() then
			getPlayer():addLineChatElement("JSON mode is on!", 0.9 , 0.9 , 0.6);
		end      
	end
end

 

For others with the same problem, the important lesson here is that the instance item like

 

item:getName()

 

will be "Hockey Mask" and not "Base.Hat_HockeyMask" or "Hat_HockeyMask".

 

IIRC, getName() is translated - so the code will break for someone playing in another language.

 

I typically use getFullType(), which returns "Base.Whatever".

 

https://zomboid-javadoc.com/41.78/zombie/inventory/InventoryItem.html#getFullType()

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...