Added crafting (backend)

master
AlmTech Software 5 years ago
parent 95bced1cda
commit 18cbeb0db4
  1. 28
      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 )
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 )
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
print( "##", k, reqItem.item )
Quantum.Server.Inventory.RemoveSlotItem( pl, char, k, reqItem.amount )
-- get items
local slots = Quantum.Server.Inventory.FindItemSlots( pl, reqItem.item, inv )
-- 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 )
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( isPlayerCrafting( ply ) ) then
cancelCrafting( ply ) -- if so then make the player stop crafting
end
end

@ -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 ) .. "'"

@ -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 }

Loading…
Cancel
Save