diff --git a/gamemode/engine/lib/server/sv_crafting.lua b/gamemode/engine/lib/server/sv_crafting.lua index 2738b0b..45f258d 100644 --- a/gamemode/engine/lib/server/sv_crafting.lua +++ b/gamemode/engine/lib/server/sv_crafting.lua @@ -13,11 +13,14 @@ local function setPlayerIsCrafting( pl, iscrafting ) pl:SetNWBool( "Quantum_Craft_IsCrafting", iscrafting ) end -local function isPlayerCrafting( pl ) return pl.iscrafting end +local function isPlayerCrafting( pl ) + return pl.iscrafting +end local function cancelCrafting( pl ) if( timer.Exists( "Quantum_Crafting_" .. pl:SteamID64() ) ) then timer.Stop( "Quantum_Crafting_" .. pl:SteamID64() ) + setPlayerIsCrafting( pl, false ) end end @@ -38,38 +41,47 @@ function Quantum.Server.Crafting.MakeItem( pl, itemid ) -- and then craft this item instead setPlayerIsCrafting( pl, true ) - timer.Create( "Quantum_Crafting_" .. pl:SteamID64(), recipe.delay, 1, function() - -- remove the ingridients from the players inv - Quantum.Server.Inventory.FindItemSlots( pl, itemid, inv ) + if( isPlayerCrafting( pl ) ) then + + timer.Create( "Quantum_Crafting_" .. pl:SteamID64(), recipe.delay, 1, function() + -- remove the ingridients from the players inv + Quantum.Server.Inventory.FindItemSlots( pl, itemid, inv ) + + for k, reqItem in pairs( recipe.recipe ) do + + -- get items + local slots = Quantum.Server.Inventory.FindItemSlots( pl, reqItem.item, inv ) - for k, reqItem in pairs( recipe.recipe ) do - print( "##", k, reqItem.item ) - Quantum.Server.Inventory.RemoveSlotItem( pl, char, k, reqItem.amount ) - end - - -- create item + -- loop through all slots and remove them + for i, slot in pairs( slots ) do + Quantum.Server.Inventory.RemoveSlotItem( pl, char, slot, reqItem.amount ) + end + end + + -- create item - Quantum.Server.Inventory.GiveItem( pl, recipe.creates, recipe.amount ) - - end) + Quantum.Server.Inventory.GiveItem( pl, recipe.creates, recipe.amount ) + Quantum.Notify.ItemCrafted( pl, Quantum.Item.Get( recipe.creates ), recipe.amount ) + setPlayerIsCrafting( pl, false ) -- when everything is done then set this to false + end) + + end else -- Dont make the item Quantum.Notify.Deny( pl, "You don not have sufficient resources to create that item!" ) end end - - setPlayerIsCrafting( pl, false ) -- when everything is done then set this to false end local function isMoving( vec ) return vec.x != 0 || vec.y != 0 || vec.z != 0 end -hook.Add( "Move", "Quantum_Velocity_test", function( ply, mv ) -- a hook to check if the player is moving when crafting +hook.Add( "Move", "Quantum_Crafting_Velocity_Stop", function( ply, mv ) -- a hook to check if the player is moving when crafting if( ply.isloaded ) then - if( isPlayerCrafting( ply ) ) then - if( isMoving( mv:GetVelocity() ) ) then + if( isMoving( mv:GetVelocity() ) ) then + if( isPlayerCrafting( ply ) ) then cancelCrafting( ply ) -- if so then make the player stop crafting end end diff --git a/gamemode/engine/lib/server/sv_notify.lua b/gamemode/engine/lib/server/sv_notify.lua index a827213..3da208e 100644 --- a/gamemode/engine/lib/server/sv_notify.lua +++ b/gamemode/engine/lib/server/sv_notify.lua @@ -26,6 +26,19 @@ function Quantum.Notify.ItemPickup( pl, item, amount ) pl:SendLua( luaFunc ) end +function Quantum.Notify.ItemCrafted( pl, item, amount ) + local amtStr = "" + if( amount > 1 ) then amtStr = tostring(amount) .. "x " end + + local itemColor = item.rarity.color || baseClr + local itemName = item.name || "[ERROR name=nil]" + + local luaArgs = makeColorAString(baseClr) .. ",'You crafted '," .. makeColorAString(baseClr) .. ",'" .. tostring(amtStr) .. "'," .. makeColorAString(itemColor) .. "," .. "'" .. tostring(itemName) .. "'" + local luaFunc = "chat.AddText(" .. luaArgs .. ")" + + pl:SendLua( luaFunc ) +end + function Quantum.Notify.Deny( pl, text ) local luaArgs = makeColorAString( Color( 245, 20, 20 ) ) .. ",'" .. tostring( text ) .. "'" diff --git a/gamemode/settings/sh_recipes.lua b/gamemode/settings/sh_recipes.lua index d866540..e695d21 100644 --- a/gamemode/settings/sh_recipes.lua +++ b/gamemode/settings/sh_recipes.lua @@ -9,6 +9,7 @@ Quantum.Recipe.Add( "potatoe", nil, { --Quantum.Server.Crafting.MakeItem( Entity(1), "potatoe" ) name = "Legendary Potatoe Recipe", amount = 1, + delay = 5, recipe = { { item = "test2", amount = 5 }, { item = "test", amount = 2 }