tltneon / nsplugins

Plugins for NutScript 1.1 (ported from NS 1.0)
http://tltneon.github.io/nsplugins/
GNU General Public License v2.0
18 stars 47 forks source link

crafting system issue #25

Open pedroxvb opened 7 years ago

pedroxvb commented 7 years ago

when you craft something, that has multiple items, for example i want to do that :

local RECIPE = {}
RECIPE.uid = "example"
RECIPE.name = "A Skull"
RECIPE.category = nut.lang.Get( "icat_material" )
RECIPE.model = Model( "models/Gibs/HGIBS.mdl" )
RECIPE.desc = "A Skull."
--RECIPE.noBlueprint = true
RECIPE.items = {
    ["bone"] = 2,
}
RECIPE.result = {
    ["skull"] = 1,
}
RECIPES:Register( RECIPE )

It is actually set to use 2 bones to craft a skull, but it doesn't work, the craft is successfull even if i use only one bone. I mean if i use 2 bones they both disapear from my inventory and i got the skull but if i use only one bone it will give me the skull aswell and remove the single bone. I'm sorry for terrible explanation, i'm not very good at explaining things. Can you help me with this i don't know what i need to change in the code to make it properly work, i know i need to modify something in this area but i'm new to lua and i don't know how to fix this myself :

function RECIPES:Register( tbl )
    if !tbl.CanCraft then
        function tbl:CanCraft( player )
            for k, v in pairs( self.items ) do
                if !player:HasItem( k, v ) then
                    return false
                end
            end
            return true
        end
    end
    if !tbl.ProcessCraftItems then
        function tbl:ProcessCraftItems( player )

            player:EmitSound( "items/ammo_pickup.wav" )
            for k, v in pairs( self.items ) do
                for i = 1, v do
                    if player:getChar():getInv():hasItem( k ) then
                        player:getChar():getInv():remove( player:getChar():getInv():hasItem( k ):getID() )
                    end
                end
            end
            for k, v in pairs( self.result ) do
                --print(player:getChar():getInv():add(k, v))
                --if (!player:getChar():getInv():add(k, v)) then
                    for i = 1, v do
                    nut.item.spawn(k, player:getItemDropPos())
                    end
                --else
                    --netstream.Start(client, "vendorAdd", uniqueID)
                --end
            end
            player:notifyLocalized( "donecrafting", self.name )

        end
    end
    self.recipes[ tbl.uid ] = tbl
end

Hope it is clear, and thanks for any help by advance :).

Stanstar22 commented 6 years ago

Hello I know I am almost a year late, but better late than never! I managed to fix this script with just a few small edits

for k, v in pairs( self.items ) do
    if player:HasItem( k, v ) then
        if player:getChar():getInv():getItemCount(k) < v then
            return false
        end
    end
    if !player:HasItem( k, v ) then
        return false
    end
end
return true