JheffersonMarques / MorePeripherals

Adds new peripherals for CC:Tweaked
MIT License
8 stars 2 forks source link

Trading interface getTrades() returns not a table #42

Closed YuanEternalR closed 4 months ago

YuanEternalR commented 4 months ago

As the title, here is my code.

-- Assuming td=peripheral.wrap("left") is already defined at the beginning

while true do
    print("Trying to get trades...")
    local success, tradesData = pcall(td.getTrades)
    if success then
        print("Success:", success)
        print("Type of trades:", type(tradesData))
        print("Trades data:", tradesData)
        if type(tradesData) == "table" then
            -- If the type of tradeData is table, then use directly.
            local trades = tradesData
            print("Trades found.")
            print("Trades data:", textutils.serialize(trades))
            -- Looking for knockback II enchanted book.
            local knockbackTradeFound = false
            for _, trade in ipairs(trades) do
                for _, offer in ipairs(trade) do
                    if offer and offer.id == "minecraft:enchanted_book" and offer.enchanted_book.level == 2 then
                        knockbackTradeFound = true
                        break
                    end
                end
                if knockbackTradeFound then
                    break
                end
            end
            if knockbackTradeFound then
                print("Knockback trade found!")
                break
            end
        else
            print("Invalid trades data.")
        end
    else
        print("Failed to get trades.")
    end
    os.sleep(0.1)
end

And its output messages are:"Trying to get trades...""Success:true""Type of trades:boolean""Trades data:true"and"Invalid trades data." If this means that getTrades() returns not a table?Or there are some problems in my code. THX for pointing out.

YuanEternalR commented 4 months ago

Alright, I know it.The return not includes NBT data.