Benny/gamemodes/benny/gamemode/inventory.lua

207 lines
4.4 KiB
Lua

---------------------
-- Your Name is Benny
---------------------
local PT = FindMetaTable("Player")
function PT:GetItems()
return
end
InventoryMeta = {}
function InventoryMeta:Destroy()
local p = self[0].Owner
p.Inventory = nil
p:GetInventory()
end
function InventoryMeta:BugCheck()
for i, v in pairs(self) do
if i != 0 and !i:IsValid() then
self[i] = nil
end
end
end
function InventoryMeta:GetWeighted()
local itemlist = {}
for i, v in pairs(self) do
if i == 0 then continue end
table.insert( itemlist, i )
end
table.sort( itemlist, function( a, b )
return a:GetAcquisition() < b:GetAcquisition()
end)
return itemlist
end
function InventoryMeta:Sync()
if SERVER then
net.Start("AEINV_InvSync")
local count = table.Count( self )-1 -- The header is included
net.WriteUInt( count, 8 )
for key, _ in pairs( self ) do
if key == 0 then continue end
net.WriteEntity( key )
end
net.Send( self[0].Owner )
end
end
InventoryMeta.__index = InventoryMeta
function PT:GetInventory()
if !self.Inventory then
print("[inventory] new inventory created: ", self)
self.Inventory = {}
self.Inventory[0] = { Owner = self }
setmetatable( self.Inventory, InventoryMeta )
if SERVER then
for i, v in pairs( self:GetChildren() ) do
if v.AEItem then
print( "[inventory] regen: adding", v)
self.Inventory[v] = true
end
end
self.Inventory:Sync()
end
end
self.Inventory:BugCheck()
return self.Inventory
end
gameevent.Listen( "OnRequestFullUpdate" )
hook.Add( "OnRequestFullUpdate", "Benny_OnRequestFullUpdate_Inventory", function( data )
local name = data.name // Same as Player:Nick()
local steamid = data.networkid // Same as Player:SteamID()
local id = data.userid // Same as Player:UserID()
local index = data.index // Same as Entity:EntIndex() minus one
if SERVER then
print("[inventory]", Player(id), "FullUpdate resync")
Player(id):GetInventory():Sync()
end
end )
if SERVER then
util.AddNetworkString("AEINV_InvSync")
else
net.Receive("AEINV_InvSync", function()
print("[inventory] sync start:")
local p = LocalPlayer()
p.Inventory = nil
if p.GetInventory then
local inv = p:GetInventory()
local count = net.ReadUInt(8)
for i=1, count do
local key = net.ReadEntity()
print( "\tadded", key)
inv[key] = true
end
else
print("\ti don't have an inventory, maybe you asked too early!!")
end
print("\tsync done")
end)
end
do
local qt2 = {
["slot1"] = 1,
["slot2"] = 2,
["slot3"] = 3,
["slot4"] = 4,
["slot5"] = 5,
["slot6"] = 6,
["slot7"] = 7,
["slot8"] = 8,
["slot9"] = 9,
["slot0"] = 0,
}
local qt = {
["invprev"] = -1,
["invnext"] = 1,
}
hook.Add( "PlayerBindPress", "Benny_PlayerBindPress_Original", function( ply, bind, pressed, code )
if qt2[bind] then
local Num = qt2[bind]
if pressed then
local inv = ply:GetInventory():GetWeighted()
local wep = ply:HandlerCheck()
local invf = table.Flip( inv )
local NumOfActive = 0
NumOfActive = invf[wep:GetDesireR()]
if Num == NumOfActive then
Num = 0
end
input.SelectWeapon( ply:GetWeapon("goat_"..Num) )
end
return true
end
if qt[bind] then
if pressed then
local Num = 0
local inv = ply:GetInventory():GetWeighted()
local wep = ply:HandlerCheck()
local invf = table.Flip( inv )
local invc = #inv
Num = wep:GetDesireR() and invf[wep:GetDesireR()] or 0
Num = Num + qt[bind]
if Num > invc then
Num = 0
elseif Num < 0 then
Num = invc
end
input.SelectWeapon( ply:GetWeapon("goat_"..Num) )
end
return true
end
end)
end
-- breaks GESTURE_SLOT_ATTACK_AND_RELOAD and I can't fucking have that
hook.Add("DoAnimationEvent", "Benny_DoAnimationEvent_FixAnimations", function( ply, event, data )
return ACT_INVALID
end)
hook.Add( "PlayerSwitchWeapon", "Benny_PlayerSwitchWeapon_Goat", function( ply, old, ent )
if ent.BennyItemHandler then return true end -- what happened?
local wep = ply:HandlerCheck()
if !wep then print("PlayerSwitchWeapon Benny Handler Missing!!") return false end
if ent:IsValid() and ent.Goat then
local inv = ply:GetInventory():GetWeighted()
if ent.Goat == 0 then wep:SetDesireR( NULL ) return false end
local ent = inv[ent.Goat]
if ent then
wep:SetDesireR( ent )
end
end
return false
end)
for i=0, 9 do
local tent = {}
--tent.Base = "goat"
tent.Goat = i
weapons.Register( tent, "goat_" .. i )
end