From 32131a1812ad43c0679bbe97f05ca5a2d54c981a Mon Sep 17 00:00:00 2001 From: AlmTech Software Date: Wed, 8 Jan 2020 22:02:16 +0100 Subject: [PATCH] Crafting progress & added new file for backend --- gamemode/engine/lib/server/sv_crafting.lua | 66 ++++++++++++++++++++++ gamemode/engine/lib/sh_recipe.lua | 26 ++------- gamemode/init.lua | 2 + 3 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 gamemode/engine/lib/server/sv_crafting.lua diff --git a/gamemode/engine/lib/server/sv_crafting.lua b/gamemode/engine/lib/server/sv_crafting.lua new file mode 100644 index 0000000..889c60b --- /dev/null +++ b/gamemode/engine/lib/server/sv_crafting.lua @@ -0,0 +1,66 @@ +-- __ _ _______ _ __ +-- / / /\ | | |__ __| | | \ \ +-- / / / \ | |_ __ ___ | | ___ ___| |__ \ \ +-- < < / /\ \ | | '_ ` _ \| |/ _ \/ __| '_ \ > > +-- \ \ / ____ \| | | | | | | | __/ (__| | | | / / +-- \_\ /_/ \_\_|_| |_| |_|_|\___|\___|_| |_| /_/ + +Quantum.Server.Crafting = {} + + +local function setPlayerIsCrafting( pl, iscrafting ) + pl.iscrafting = iscrafting + pl:SetNWBool( "Quantum_Craft_IsCrafting", 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() ) + end +end + +function Quantum.Server.Crafting.MakeItem( pl, itemid ) + local recipe = Quantum.Recipe.Get( itemid ) + local char = Quantum.Server.Char.GetCurrentCharacter( pl ) + local inv = Quantum.Server.Char.GetInventory( char ) + + if( recipe != nil ) then + local canMake, failedReq = Quantum.Recipe.CanMake( inv, itemid ) + + if( canMake ) then + + cancelCrafting( pl ) -- stop the crafting if the player is allready crafting something + -- 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 + + -- create item + end) + + else + -- Dont make the item + Quantum.Server.Notify.Deny( pl, "You don't 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 + if( ply.isloaded ) then + if( isPlayerCrafting( ply ) ) then + if( isMoving( mv:GetVelocity() ) ) then + cancelCrafting( ply ) -- if so then make the player stop crafting + end + end + end +end) diff --git a/gamemode/engine/lib/sh_recipe.lua b/gamemode/engine/lib/sh_recipe.lua index f42a5b4..ac51369 100644 --- a/gamemode/engine/lib/sh_recipe.lua +++ b/gamemode/engine/lib/sh_recipe.lua @@ -13,10 +13,11 @@ function Quantum.Recipe.Add( itemid, station, tbl ) if( Quantum.Item.Get( itemid ) == nil ) then return end local returnTbl = { - name = tbl.name || "Secret Recipe" -- name of the recipe - station = station, - creates = itemid -- what the recipe creates - amount = tbl.amount -- how much you get from 1 craft + name = tbl.name || "Secret Recipe", -- name of the recipe + station = station, -- where you can craft it ( at what "crafting table" can you make it at ) + creates = itemid, -- what the recipe creates + delay = tbl.delay || Quantum.MinCraftDelay, + amount = tbl.amount || 1, -- how much you get from 1 craft recipe = tbl.recipe || {} } @@ -62,21 +63,4 @@ function Quantum.Recipe.CanMake( inv, itemid ) return #failedReq <= 0, failedReq end -end - -function Quantum.Recipe.MakeItem( pl, itemid ) - local recipe = Quantum.Recipe.Get( itemid ) - local char = Quantum.Server.Char.GetCurrentCharacter( pl ) - local inv = Quantum.Server.Char.GetInventory( char ) - - if( recipe != nil ) then - local canMake, failedReq = Quantum.Recipe.CanMake( inv, itemid ) - - if( canMake ) then - -- create item - else - -- Dont make the item - return - end - end end \ No newline at end of file diff --git a/gamemode/init.lua b/gamemode/init.lua index 50b6dfe..33880d6 100644 --- a/gamemode/init.lua +++ b/gamemode/init.lua @@ -14,6 +14,7 @@ if SERVER then AddCSLuaFile( "settings/sh_settings.lua" ) AddCSLuaFile( "settings/sh_items.lua" ) AddCSLuaFile( "settings/sh_effects.lua" ) + AddCSLuaFile( "settings/sh_recipes.lua" ) AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) @@ -114,6 +115,7 @@ if SERVER then local function loadAllItemsAndEffects() include( "settings/sh_items.lua" ) include( "settings/sh_effects.lua" ) + include( "settings/sh_recipes.lua" ) end local function loadPlugins()