Added crafting (backend)

master
AlmTech Software 5 years ago
parent 95bced1cda
commit 18cbeb0db4
  1. 44
      gamemode/engine/lib/server/sv_crafting.lua
  2. 13
      gamemode/engine/lib/server/sv_notify.lua
  3. 1
      gamemode/settings/sh_recipes.lua

@ -13,11 +13,14 @@ local function setPlayerIsCrafting( pl, iscrafting )
pl:SetNWBool( "Quantum_Craft_IsCrafting", iscrafting ) pl:SetNWBool( "Quantum_Craft_IsCrafting", iscrafting )
end end
local function isPlayerCrafting( pl ) return pl.iscrafting end local function isPlayerCrafting( pl )
return pl.iscrafting
end
local function cancelCrafting( pl ) local function cancelCrafting( pl )
if( timer.Exists( "Quantum_Crafting_" .. pl:SteamID64() ) ) then if( timer.Exists( "Quantum_Crafting_" .. pl:SteamID64() ) ) then
timer.Stop( "Quantum_Crafting_" .. pl:SteamID64() ) timer.Stop( "Quantum_Crafting_" .. pl:SteamID64() )
setPlayerIsCrafting( pl, false )
end end
end end
@ -38,38 +41,47 @@ function Quantum.Server.Crafting.MakeItem( pl, itemid )
-- and then craft this item instead -- and then craft this item instead
setPlayerIsCrafting( pl, true ) setPlayerIsCrafting( pl, true )
timer.Create( "Quantum_Crafting_" .. pl:SteamID64(), recipe.delay, 1, function() if( isPlayerCrafting( pl ) ) then
-- remove the ingridients from the players inv
Quantum.Server.Inventory.FindItemSlots( pl, itemid, inv ) 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
for k, reqItem in pairs( recipe.recipe ) do -- get items
print( "##", k, reqItem.item ) local slots = Quantum.Server.Inventory.FindItemSlots( pl, reqItem.item, inv )
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
Quantum.Server.Inventory.GiveItem( pl, recipe.creates, recipe.amount ) -- create item
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 else
-- Dont make the item -- Dont make the item
Quantum.Notify.Deny( pl, "You don not have sufficient resources to create that item!" ) Quantum.Notify.Deny( pl, "You don not have sufficient resources to create that item!" )
end end
end end
setPlayerIsCrafting( pl, false ) -- when everything is done then set this to false
end end
local function isMoving( vec ) local function isMoving( vec )
return vec.x != 0 || vec.y != 0 || vec.z != 0 return vec.x != 0 || vec.y != 0 || vec.z != 0
end 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( 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 cancelCrafting( ply ) -- if so then make the player stop crafting
end end
end end

@ -26,6 +26,19 @@ function Quantum.Notify.ItemPickup( pl, item, amount )
pl:SendLua( luaFunc ) pl:SendLua( luaFunc )
end 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 ) function Quantum.Notify.Deny( pl, text )
local luaArgs = makeColorAString( Color( 245, 20, 20 ) ) .. ",'" .. tostring( text ) .. "'" local luaArgs = makeColorAString( Color( 245, 20, 20 ) ) .. ",'" .. tostring( text ) .. "'"

@ -9,6 +9,7 @@
Quantum.Recipe.Add( "potatoe", nil, { --Quantum.Server.Crafting.MakeItem( Entity(1), "potatoe" ) Quantum.Recipe.Add( "potatoe", nil, { --Quantum.Server.Crafting.MakeItem( Entity(1), "potatoe" )
name = "Legendary Potatoe Recipe", name = "Legendary Potatoe Recipe",
amount = 1, amount = 1,
delay = 5,
recipe = { recipe = {
{ item = "test2", amount = 5 }, { item = "test2", amount = 5 },
{ item = "test", amount = 2 } { item = "test", amount = 2 }

Loading…
Cancel
Save