From e119203ab9e2613e0d2788011e02d58d10cd3694 Mon Sep 17 00:00:00 2001 From: AlmTech Software Date: Thu, 2 Jan 2020 16:20:43 +0100 Subject: [PATCH] Optimized dynamic network API --- gamemode/engine/lib/server/sv_inventory.lua | 31 +++++++++----------- gamemode/engine/lib/server/sv_networking.lua | 1 + 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/gamemode/engine/lib/server/sv_inventory.lua b/gamemode/engine/lib/server/sv_inventory.lua index 1753910..25a4c1f 100644 --- a/gamemode/engine/lib/server/sv_inventory.lua +++ b/gamemode/engine/lib/server/sv_inventory.lua @@ -24,26 +24,23 @@ local function isStackable( item ) end function Quantum.Server.Inventory.SetSlotItem( pl, char, pos, itemid, amount ) + local setItemTbl = {} if( amount < 1 ) then - char.inventory[pos] = nil -- remove the item - - -- Sent the new data to the client - Quantum.Net.Inventory.SetItem( pl, pos, itemid, amount ) - - return - end - local item = Quantum.Item.Get( itemid ) - if( isEquippable( item ) || !isStackable( item ) ) then - amount = 1 - char.inventory[pos] = { itemid } - -- Sent the new data to the client - Quantum.Net.Inventory.SetItem( pl, pos, itemid, amount ) + setItemTbl = nil else - amount = amount || 1 - char.inventory[pos] = { itemid, amount } - -- Sent the new data to the client - Quantum.Net.Inventory.SetItem( pl, pos, itemid, amount ) + local item = Quantum.Item.Get( itemid ) + if( isEquippable( item ) || !isStackable( item ) ) then + amount = nil + setItemTbl = { itemid } + else + amount = amount || 1 + setItemTbl = { itemid, amount } + end end + + char.inventory[pos] = setItemTbl -- remove the item + -- Sent the new data to the client + Quantum.Net.Inventory.SetItem( pl, pos, itemid, amount ) end function Quantum.Server.Inventory.GetSlotItem( char, pos ) return char.inventory[pos] end diff --git a/gamemode/engine/lib/server/sv_networking.lua b/gamemode/engine/lib/server/sv_networking.lua index d4dc816..b3d9146 100644 --- a/gamemode/engine/lib/server/sv_networking.lua +++ b/gamemode/engine/lib/server/sv_networking.lua @@ -125,6 +125,7 @@ local function WriteIntcode( intcode ) net.WriteInt( intcode, Quantum.IntCode.BI function Quantum.Net.Inventory.SetItem( pl, index, itemid, amount ) -- sends a item to the client with amount of it, this is a DYNAMIC networking solution net.Start( "quantum_item_action" ) + WriteIntcode( Quantum.IntCode.SET_ITEM ) -- write the opcode first net.WriteInt( index, calculateNeededBits( Quantum.Inventory.Size ) ) net.WriteString( itemid )