diff --git a/gamemode/engine/derma/lib/cl_menu_iteminfo.lua b/gamemode/engine/derma/lib/cl_menu_iteminfo.lua index 80f0671..5074853 100644 --- a/gamemode/engine/derma/lib/cl_menu_iteminfo.lua +++ b/gamemode/engine/derma/lib/cl_menu_iteminfo.lua @@ -21,6 +21,31 @@ local function resizePanel( p ) return p.w, p.h end +local function getEquippedItems() + local returnTbl = {} + + for equipType, equipSlot in pairs( Quantum.Client.Equipped ) do + returnTbl[ #returnTbl + 1 ] = { type = equipType, slot = equipSlot } + end + + return returnTbl +end + +local function checkIfItemIsEquippedAndRemove( page, index ) + local equipItems = getEquippedItems() + + for ie, ii in pairs( equipItems ) do + print( ie, ii ) + if( ii.slot == index ) then + if( page.equippanels[ii.type] != nil ) then + page.equippanels[ii.type].SetItem( nil ) -- remove it + else + Quantum.Error( "Func 'checkIfItemIsEquippedAndRemove': type is not valid! type=" .. tostring(ii.type) ) + end + end + end +end + function iteminfo.dropamount( p, page, itemPanel ) local index = p.index local item = p.item @@ -206,6 +231,10 @@ function iteminfo.giveoptions( p, page ) p:GetParent().SetItemAmount( amount - 1 ) options.Close() + + if( amount - 1 <= 0 ) then + checkIfItemIsEquippedAndRemove( page, index ) + end ---- USE NET ---- Quantum.Client.InventoryNet.UseItem( index ) @@ -231,6 +260,10 @@ function iteminfo.giveoptions( p, page ) p:GetParent().SetItemAmount( amount - 1 ) options.Close() + + if( amount - 1 <= 0 ) then + checkIfItemIsEquippedAndRemove( page, index ) + end ---- EAT NET ---- Quantum.Client.InventoryNet.EatItem( index ) @@ -257,6 +290,7 @@ function iteminfo.giveoptions( p, page ) iteminfo.dropamount( options, page, itemPanel ) else p:GetParent().RemoveItem() + checkIfItemIsEquippedAndRemove( page, index ) ---- DROP NET HERE ---- Quantum.Client.InventoryNet.DropItem( item.id, index, amount ) diff --git a/gamemode/engine/derma/menus/menu_charinfo.lua b/gamemode/engine/derma/menus/menu_charinfo.lua index 1739653..e829733 100644 --- a/gamemode/engine/derma/menus/menu_charinfo.lua +++ b/gamemode/engine/derma/menus/menu_charinfo.lua @@ -74,7 +74,6 @@ local function createEquipSlotPanel( equiptype, x, y, scale, parent ) p.icon:SetModel( itemTbl.model ) configureCamLookPos( p.icon ) else - print( "REMOVE" ) p.icon:SetVisible( false ) -- hide it if there is no item if( IsValid( p.icon.tooltip ) ) then p.icon.tooltip:Remove() @@ -109,7 +108,11 @@ end local function getItemInSlot( pos ) local inv = Quantum.Client.Inventory || {} - return inv[pos][1] + if( inv[pos] != nil ) then + return inv[pos][1] + else + return + end end function menu.open( dt ) @@ -205,7 +208,6 @@ function menu.open( dt ) f.equippanels[Quantum.EquipSlots.Head] = createEquipSlotPanel( Quantum.EquipSlots.Head, slotXpos, char.y + char.h/5, slotScale, f ) -- create the panel f.equippanels[Quantum.EquipSlots.Head].SetItem( getItemInSlot(equipped[ Quantum.EquipSlots.Head ]) ) -- give its current item - print( "EQUIPED: ", getItemInSlot(equipped[ Quantum.EquipSlots.Head ]) ) ---- Inventory panel ---- local inv = vgui.Create( "DPanel", f ) -- section for all of the item panels diff --git a/gamemode/engine/derma/menus/menu_main.lua b/gamemode/engine/derma/menus/menu_main.lua index 82fe0fa..baf3598 100644 --- a/gamemode/engine/derma/menus/menu_main.lua +++ b/gamemode/engine/derma/menus/menu_main.lua @@ -156,7 +156,6 @@ function main.open(dt) play.w, play.h = play:GetSize() play.y = play.y + play.h + padding*2 - print( play.y ) end play:SetTextColor( buttonTextColor ) diff --git a/gamemode/engine/lib/server/sv_inventory.lua b/gamemode/engine/lib/server/sv_inventory.lua index 3eb64a0..b93d3d8 100644 --- a/gamemode/engine/lib/server/sv_inventory.lua +++ b/gamemode/engine/lib/server/sv_inventory.lua @@ -55,16 +55,17 @@ function Quantum.Server.Inventory.EquipItem( pl, itemindex ) end end -function Quantum.Server.Inventory.UnEquipItem( pl, equipslot ) - local char = Quantum.Server.Char.GetCurrentCharacter( pl ) +function Quantum.Server.Inventory.UnEquipItem( pl, equipslot, char ) + char = char || Quantum.Server.Char.GetCurrentCharacter( pl ) + if( char.equipped[equipslot] != nil ) then local slotItem = Quantum.Server.Inventory.GetSlotItem( char, char.equipped[equipslot] ) - local itemTbl = Quantum.Item.Get( slotItem ) + local itemTbl = Quantum.Item.Get( slotItem[1] ) if( itemTbl.equipeffect != nil ) then -- remove the items effect Quantum.Effect.Remove( pl, itemTbl.equipeffect ) end - + Quantum.Debug( tostring(pl) .. " unequipped item (" .. tostring( itemTbl.id ) .. ") - (" .. tostring( char.equipped[equipslot] ) .. ")" ) char.equipped[equipslot] = nil @@ -72,9 +73,27 @@ function Quantum.Server.Inventory.UnEquipItem( pl, equipslot ) end end +function Quantum.Server.Inventory.GetEquippedItems( pl, char ) + char = char || Quantum.Server.Char.GetCurrentCharacter( pl ) + local returnTbl = {} + + for equipType, equipSlot in pairs( char.equipped ) do + returnTbl[ #returnTbl + 1 ] = { type = equipType, slot = equipSlot } + end + + return returnTbl +end + function Quantum.Server.Inventory.SetSlotItem( pl, char, pos, itemid, amount ) local setItemTbl = {} if( amount < 1 ) then + local equippedItems = Quantum.Server.Inventory.GetEquippedItems( pl, char ) + for ei, slotTbl in pairs( equippedItems ) do + if( slotTbl.slot == pos ) then + Quantum.Server.Inventory.UnEquipItem( pl, slotTbl.type ) -- unequipp the item if it was removed + end + end + setItemTbl = nil else local item = Quantum.Item.Get( itemid ) @@ -93,6 +112,27 @@ function Quantum.Server.Inventory.SetSlotItem( pl, char, pos, itemid, amount ) Quantum.Net.Inventory.SetItem( pl, pos, itemid, amount ) end +function Quantum.Server.Inventory.RemoveSlotItem( pl, char, pos, amount ) + local slotItem = Quantum.Server.Inventory.GetSlotItem( char, pos ) + local itemid = slotItem[1] + + if( slotItem == nil ) then return end + + if( amount <= 0 ) then + Quantum.Server.Inventory.SetSlotItem( pl, char, pos, itemid, 0 ) + return 0 + else + local am_diff = slotItem[2] - amount + if( am_diff <= 0 ) then + Quantum.Server.Inventory.SetSlotItem( pl, char, pos, itemid, 0 ) + return 0 + else + Quantum.Server.Inventory.SetSlotItem( pl, char, pos, itemid, am_diff ) + return am_diff + end + end +end + function Quantum.Server.Inventory.GetSlotItem( char, pos ) return char.inventory[pos] end function Quantum.Server.Inventory.FindStackable( char, item ) @@ -268,7 +308,8 @@ function Quantum.Server.Inventory.DropItem( pl, index, amount ) -- Quantum.Serve if( am_diff >= 0 ) then -- drop the item from the players inv -- remove the items am_diff from its stack - Quantum.Server.Inventory.SetSlotItem( pl, char, index, itemid, am_diff ) + --Quantum.Server.Inventory.SetSlotItem( pl, char, index, itemid, am_diff ) + Quantum.Server.Inventory.RemoveSlotItem( pl, char, index, amount ) -- spawn the item infront of the player Quantum.Server.Item.SpawnItemAtPlayer( pl, itemid, amount ) @@ -287,7 +328,7 @@ function Quantum.Server.Inventory.UseItem( pl, index ) if( item != nil || #item > 0 ) then local itemTbl = Quantum.Item.Get( item[1] ) if( itemTbl.useeffect != nil ) then - Quantum.Server.Inventory.SetSlotItem( pl, char, index, item[1], item[2] - 1 ) + Quantum.Server.Inventory.RemoveSlotItem( pl, char, index, 1 ) Quantum.Effect.Give( pl, itemTbl.useeffect ) -- call the function end end @@ -302,7 +343,7 @@ function Quantum.Server.Inventory.EatItem( pl, index ) if( item != nil || #item > 0 ) then local itemTbl = Quantum.Item.Get( item[1] ) if( itemTbl.consumeeffect != nil ) then - Quantum.Server.Inventory.SetSlotItem( pl, char, index, item[1], item[2] - 1 ) + Quantum.Server.Inventory.RemoveSlotItem( pl, char, index, 1 ) Quantum.Effect.Give( pl, itemTbl.consumeeffect ) end end diff --git a/gamemode/engine/lib/server/sv_networking.lua b/gamemode/engine/lib/server/sv_networking.lua index c67f265..9afb663 100644 --- a/gamemode/engine/lib/server/sv_networking.lua +++ b/gamemode/engine/lib/server/sv_networking.lua @@ -147,7 +147,7 @@ end local intcodeFunctions = { [Quantum.IntCode.SET_ITEM] = function( pl, index, itemid, amount ) -- if the client is trying to set an item then kick the player Quantum.Warn( "Player [" .. pl:Nick() .. " | " .. pl:SteamID() .. "] tried to use a blacklisted Intcode function called from the client!" ) - --pl:Kick( "[Quantum Security] Tried to use a invalid Intcode function. Nice try." ) + pl:Kick( "[Quantum Security] Tried to use a invalid Intcode function. Nice try." ) end, [Quantum.IntCode.DROP_ITEM] = function( pl, index, itemid, amount ) Quantum.Server.Inventory.DropItem( pl, index, amount ) @@ -169,7 +169,6 @@ net.Receive( "quantum_item_action", function( len, pl ) local itemid = net.ReadString() local amount = net.ReadInt( Quantum.calculateNeededBits( Quantum.Inventory.MaxStackSize ) ) - print( "####", intcode, index, itemid, amount ) intcodeFunctions[intcode]( pl, index, itemid, amount ) end) diff --git a/gamemode/engine/vars/sh_vars.lua b/gamemode/engine/vars/sh_vars.lua index b316400..926b4a1 100644 --- a/gamemode/engine/vars/sh_vars.lua +++ b/gamemode/engine/vars/sh_vars.lua @@ -48,6 +48,5 @@ Quantum.IntCode = { function Quantum.calculateNeededBits( n ) return math.ceil( math.log( n, 2 ) ) end function Quantum.WriteIntcode( intcode ) - print( intcode ) net.WriteInt( intcode, Quantum.IntCode.BIT_SIZE ) end \ No newline at end of file