How do I add crafting and furnace recipes?
#5
Code:
PLUGIN = nil

--VARIABLES
recipes = {}

--PLUGIN
function Initialize(Plugin)
    Plugin:SetName("SmpMc")
    Plugin:SetVersion(1)

    -- Hooks
    -- cPluginManager.AddHook(cPluginManager.<hook>, <function>)
    cPluginManager.AddHook(cPluginManager.HOOK_CRAFTING_NO_RECIPE, OnCraftingNoRecipe)
    cPluginManager:AddHook(cPluginManager.HOOK_POST_CRAFTING, OnPostCrafting);

    PLUGIN = Plugin

    -- Command Bindings
    -- cPluginManager.BindCommand("/<command>", "<permission>", <function>, "<desc>")

    --Recipes
    table.insert(recipes, {{265, 264, 265, 265, -1, 265, 265, 265, 265}, makeItem(61, "§rEnhanced Furnace")})

    LOG("Initialised " .. Plugin:GetName() .. " v." .. Plugin:GetVersion())
    return true
end

function OnDisable()
    LOG(PLUGIN:GetName() .. " is shutting down...")
end

--HOOKS
function OnCraftingNoRecipe(Player, Grid, Recipe)
    r = getRecipe(Grid)
    if r == nil then return end
    Recipe:SetResult(r)
    return true
end

function OnPostCrafting(Player, Grid, Recipe)
    if getRecipe(Grid) ~= nil then Recipe:ConsumeIngredients(Grid) end
end

--UTIL
function getRecipe(Grid)
    items = {}
    table.insert(items, Grid:GetItem(0, 0).m_ItemType)
    table.insert(items, Grid:GetItem(1, 0).m_ItemType)
    table.insert(items, Grid:GetItem(2, 0).m_ItemType)
    table.insert(items, Grid:GetItem(0, 1).m_ItemType)
    table.insert(items, Grid:GetItem(1, 1).m_ItemType)
    table.insert(items, Grid:GetItem(2, 1).m_ItemType)
    table.insert(items, Grid:GetItem(0, 2).m_ItemType)
    table.insert(items, Grid:GetItem(1, 2).m_ItemType)
    table.insert(items, Grid:GetItem(2, 2).m_ItemType)

    for k,v in pairs(recipes) do
        if arrayEquals(items, v[1]) then
            return v[2]
        end
    end

    return nil
end

function makeItem(Type, Name)
    i = cItem(Type)
    i.m_CustomName = Name
    return i
end

function arrayEquals(ArrayOne, ArrayTwo)
    if #ArrayOne ~= #ArrayTwo then return false end
    for k,v in pairs(ArrayOne) do
        if ArrayTwo[k] ~= v then return false end
    end
    return true
end
This is what I have figured out so far for adding a new recipe. It works fine, except when I craft the item the ingredients aren't deleted. How do I fix this?
Reply
Thanks given by:


Messages In This Thread
RE: How do I add crafting and furnace recipes? - by Assossa - 04-02-2016, 01:01 PM



Users browsing this thread: 1 Guest(s)