Items dropped which are equipped now gets unequipped (SERVER & CLIENT)

master
AlmTech Software 5 years ago
parent d27a24ef08
commit a39cf80eae
  1. 34
      gamemode/engine/derma/lib/cl_menu_iteminfo.lua
  2. 6
      gamemode/engine/derma/menus/menu_charinfo.lua
  3. 1
      gamemode/engine/derma/menus/menu_main.lua
  4. 53
      gamemode/engine/lib/server/sv_inventory.lua
  5. 3
      gamemode/engine/lib/server/sv_networking.lua
  6. 1
      gamemode/engine/vars/sh_vars.lua

@ -21,6 +21,31 @@ local function resizePanel( p )
return p.w, p.h return p.w, p.h
end 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 ) function iteminfo.dropamount( p, page, itemPanel )
local index = p.index local index = p.index
local item = p.item local item = p.item
@ -207,6 +232,10 @@ function iteminfo.giveoptions( p, page )
options.Close() options.Close()
if( amount - 1 <= 0 ) then
checkIfItemIsEquippedAndRemove( page, index )
end
---- USE NET ---- ---- USE NET ----
Quantum.Client.InventoryNet.UseItem( index ) Quantum.Client.InventoryNet.UseItem( index )
end end
@ -232,6 +261,10 @@ function iteminfo.giveoptions( p, page )
options.Close() options.Close()
if( amount - 1 <= 0 ) then
checkIfItemIsEquippedAndRemove( page, index )
end
---- EAT NET ---- ---- EAT NET ----
Quantum.Client.InventoryNet.EatItem( index ) Quantum.Client.InventoryNet.EatItem( index )
end end
@ -257,6 +290,7 @@ function iteminfo.giveoptions( p, page )
iteminfo.dropamount( options, page, itemPanel ) iteminfo.dropamount( options, page, itemPanel )
else else
p:GetParent().RemoveItem() p:GetParent().RemoveItem()
checkIfItemIsEquippedAndRemove( page, index )
---- DROP NET HERE ---- ---- DROP NET HERE ----
Quantum.Client.InventoryNet.DropItem( item.id, index, amount ) Quantum.Client.InventoryNet.DropItem( item.id, index, amount )

@ -74,7 +74,6 @@ local function createEquipSlotPanel( equiptype, x, y, scale, parent )
p.icon:SetModel( itemTbl.model ) p.icon:SetModel( itemTbl.model )
configureCamLookPos( p.icon ) configureCamLookPos( p.icon )
else else
print( "REMOVE" )
p.icon:SetVisible( false ) -- hide it if there is no item p.icon:SetVisible( false ) -- hide it if there is no item
if( IsValid( p.icon.tooltip ) ) then if( IsValid( p.icon.tooltip ) ) then
p.icon.tooltip:Remove() p.icon.tooltip:Remove()
@ -109,7 +108,11 @@ end
local function getItemInSlot( pos ) local function getItemInSlot( pos )
local inv = Quantum.Client.Inventory || {} local inv = Quantum.Client.Inventory || {}
if( inv[pos] != nil ) then
return inv[pos][1] return inv[pos][1]
else
return
end
end end
function menu.open( dt ) 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] = 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 f.equippanels[Quantum.EquipSlots.Head].SetItem( getItemInSlot(equipped[ Quantum.EquipSlots.Head ]) ) -- give its current item
print( "EQUIPED: ", getItemInSlot(equipped[ Quantum.EquipSlots.Head ]) )
---- Inventory panel ---- ---- Inventory panel ----
local inv = vgui.Create( "DPanel", f ) -- section for all of the item panels local inv = vgui.Create( "DPanel", f ) -- section for all of the item panels

@ -156,7 +156,6 @@ function main.open(dt)
play.w, play.h = play:GetSize() play.w, play.h = play:GetSize()
play.y = play.y + play.h + padding*2 play.y = play.y + play.h + padding*2
print( play.y )
end end
play:SetTextColor( buttonTextColor ) play:SetTextColor( buttonTextColor )

@ -55,11 +55,12 @@ function Quantum.Server.Inventory.EquipItem( pl, itemindex )
end end
end end
function Quantum.Server.Inventory.UnEquipItem( pl, equipslot ) function Quantum.Server.Inventory.UnEquipItem( pl, equipslot, char )
local char = Quantum.Server.Char.GetCurrentCharacter( pl ) char = char || Quantum.Server.Char.GetCurrentCharacter( pl )
if( char.equipped[equipslot] != nil ) then if( char.equipped[equipslot] != nil ) then
local slotItem = Quantum.Server.Inventory.GetSlotItem( char, char.equipped[equipslot] ) 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 if( itemTbl.equipeffect != nil ) then -- remove the items effect
Quantum.Effect.Remove( pl, itemTbl.equipeffect ) Quantum.Effect.Remove( pl, itemTbl.equipeffect )
@ -72,9 +73,27 @@ function Quantum.Server.Inventory.UnEquipItem( pl, equipslot )
end end
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 ) function Quantum.Server.Inventory.SetSlotItem( pl, char, pos, itemid, amount )
local setItemTbl = {} local setItemTbl = {}
if( amount < 1 ) then 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 setItemTbl = nil
else else
local item = Quantum.Item.Get( itemid ) 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 ) Quantum.Net.Inventory.SetItem( pl, pos, itemid, amount )
end 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.GetSlotItem( char, pos ) return char.inventory[pos] end
function Quantum.Server.Inventory.FindStackable( char, item ) 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 if( am_diff >= 0 ) then -- drop the item from the players inv
-- remove the items am_diff from its stack -- 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 -- spawn the item infront of the player
Quantum.Server.Item.SpawnItemAtPlayer( pl, itemid, amount ) Quantum.Server.Item.SpawnItemAtPlayer( pl, itemid, amount )
@ -287,7 +328,7 @@ function Quantum.Server.Inventory.UseItem( pl, index )
if( item != nil || #item > 0 ) then if( item != nil || #item > 0 ) then
local itemTbl = Quantum.Item.Get( item[1] ) local itemTbl = Quantum.Item.Get( item[1] )
if( itemTbl.useeffect != nil ) then 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 Quantum.Effect.Give( pl, itemTbl.useeffect ) -- call the function
end end
end end
@ -302,7 +343,7 @@ function Quantum.Server.Inventory.EatItem( pl, index )
if( item != nil || #item > 0 ) then if( item != nil || #item > 0 ) then
local itemTbl = Quantum.Item.Get( item[1] ) local itemTbl = Quantum.Item.Get( item[1] )
if( itemTbl.consumeeffect != nil ) then 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 ) Quantum.Effect.Give( pl, itemTbl.consumeeffect )
end end
end end

@ -147,7 +147,7 @@ end
local intcodeFunctions = { 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.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!" ) 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, end,
[Quantum.IntCode.DROP_ITEM] = function( pl, index, itemid, amount ) [Quantum.IntCode.DROP_ITEM] = function( pl, index, itemid, amount )
Quantum.Server.Inventory.DropItem( pl, index, 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 itemid = net.ReadString()
local amount = net.ReadInt( Quantum.calculateNeededBits( Quantum.Inventory.MaxStackSize ) ) local amount = net.ReadInt( Quantum.calculateNeededBits( Quantum.Inventory.MaxStackSize ) )
print( "####", intcode, index, itemid, amount )
intcodeFunctions[intcode]( pl, index, itemid, amount ) intcodeFunctions[intcode]( pl, index, itemid, amount )
end) end)

@ -48,6 +48,5 @@ Quantum.IntCode = {
function Quantum.calculateNeededBits( n ) return math.ceil( math.log( n, 2 ) ) end function Quantum.calculateNeededBits( n ) return math.ceil( math.log( n, 2 ) ) end
function Quantum.WriteIntcode( intcode ) function Quantum.WriteIntcode( intcode )
print( intcode )
net.WriteInt( intcode, Quantum.IntCode.BIT_SIZE ) net.WriteInt( intcode, Quantum.IntCode.BIT_SIZE )
end end
Loading…
Cancel
Save