otland / forgottenserver

A free and open-source MMORPG server emulator written in C++
https://otland.net
GNU General Public License v2.0
1.59k stars 1.06k forks source link

ItemType(item):getSlotPosition() returns the same value (48) for all slot positions #1962

Open raymondtfr opened 8 years ago

raymondtfr commented 8 years ago

Before creating an issue, please ensure:

    local slotItem = player:getSlotItem(CONST_SLOT_FEET)
    if slotItem then
        local slotPos = ItemType(slotItem):getSlotPosition()
        print("Slot position (feet): ", slotPos) -- prints the value 48

        slotItem = player:getSlotItem(CONST_SLOT_HEAD)
        if slotItem then
            slotPos = ItemType(slotItem):getSlotPosition()
            print("Slot position (head): ", slotPos) -- also prints 48
        end
    end

Expected behaviour

It should print the appropriate value for each specific equipment slot.

Actual behaviour

It prints the same value (48) for all equipment slots.

Environment

the forgotten server 1.3

dbjorkholm commented 8 years ago

It's because you're constructing an ItemType object with a userdata value instead of item id or name.

You should receive the following results when it's constructed properly:

Slot position (feet):   176
Slot position (head):   49
andersonfaaria commented 8 years ago

@ninjalulz anyway it should work when making an itemtype with a userdata.

raymondtfr commented 8 years ago

I could sense I was making something wrong and you just showed me that hehe, thanks for explaining. I thought I had searched in the sources already so I thought I could use a Item to construct an ItemType but then I checked the sources now that you explained me and found: // ItemType(id or name).

Should I close this?

@ninjalulz Is it a valid (possible) idea what @andersonfaaria mentioned?

Mistakes of mine like these makes this looks more like a support matter 💃 lol, I should be closing this?

andersonfaaria commented 8 years ago

@ninjalulz I have a question, the item class inherit the methods for itemType? if so, @raymondtfr could use slotItem:getType():getSlotPosition(); If not, he could use ItemType(slotItem:getId());

But again, the TFS should accept userdata for ItemType and MonsterType. It's a simple if thing and makes it more consistent.

Mkalo commented 8 years ago

In my opinion, you shouldn't be able to construct an ItemType with an Item userdata. The method item:getType() makes more sense for OO.

But it should be returning nil if the ItemType doesn't exist.

andersonfaaria commented 8 years ago

The question is: Do we have a Way of doing item:getType()? The function getType is from itemType, not from item. Item has a way of inheriting ItemType's methods?

Mkalo commented 8 years ago

No, itemType:getType() returns a number: https://github.com/otland/forgottenserver/blob/master/src/items.h#L45

item:getType() returns the ItemType of the item: https://github.com/otland/forgottenserver/blob/master/data/lib/core/item.lua#L1

andersonfaaria commented 8 years ago

I cant check the code right now. I'm on the cell phone. I was only assuming they were the same because of what I saw in the luascript.cpp

A-Syntax commented 7 years ago

I really don't understand the question, but this is a simple way to get all the slot id's along with the item id's of everything you have equipped.

--[[
    1 - helmet
    2 - necklace slot
    3 - backpack, bag
    4 - armor
    5 - right hand
    6 - left hand
    7 - legs
    8 - boots
    9 - ring slot
    10 - ammo slot
]]
function Player:getSlottedItems()
    local equipment, item, itemid = {}, 0, 0 
    for slot = 1, 10 do
        item = self:getSlotItem(slot)
        itemid = pushThing(item).itemid
        if itemid and itemid ~= 0 then
            equipment[slot] = itemid
        end
    end
    return equipment
end