Benny/gamemodes/benny/gamemode/inventory.lua

202 lines
4.2 KiB
Lua
Raw Normal View History

2024-03-07 21:19:30 -05:00
---------------------
-- 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)
2024-03-07 21:19:30 -05:00
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)
2024-03-07 21:19:30 -05:00
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 )
2024-03-07 21:19:30 -05:00
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")
2024-03-07 21:19:30 -05:00
Player(id):GetInventory():Sync()
end
end )
if SERVER then
util.AddNetworkString("AEINV_InvSync")
else
net.Receive("AEINV_InvSync", function()
print("[inventory] sync start:")
2024-03-07 21:19:30 -05:00
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)
2024-03-07 21:19:30 -05:00
inv[key] = true
end
else
print("\ti don't have an inventory, maybe you asked too early!!")
2024-03-07 21:19:30 -05:00
end
print("\tsync done")
2024-03-07 21:19:30 -05:00
end)
end
do
2024-09-20 16:20:22 -04:00
local qt2 = {
["slot1"] = 1,
["slot2"] = 2,
["slot3"] = 3,
["slot4"] = 4,
["slot5"] = 5,
["slot6"] = 6,
["slot7"] = 7,
["slot8"] = 8,
["slot9"] = 9,
["slot0"] = 0,
}
2024-03-07 21:19:30 -05:00
local qt = {
2024-09-20 16:20:22 -04:00
["invprev"] = -1,
["invnext"] = 1,
2024-03-07 21:19:30 -05:00
}
hook.Add( "PlayerBindPress", "Benny_PlayerBindPress_Original", function( ply, bind, pressed, code )
2024-09-20 16:20:22 -04:00
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
2024-03-07 21:19:30 -05:00
return true
end
end)
end
2024-09-20 16:20:22 -04:00
hook.Add( "PlayerSwitchWeapon", "Benny_PlayerSwitchWeapon_Goat", function( ply, old, ent )
if ent.BennyItemHandler then return true end -- what happened?
2024-03-12 18:43:31 -04:00
local wep = ply:HandlerCheck()
2024-09-20 16:20:22 -04:00
if !wep then print("PlayerSwitchWeapon Benny Handler Missing!!") return false end
if ent:IsValid() and ent.Goat then
local inv = ply:GetInventory():GetWeighted()
2024-03-07 21:19:30 -05:00
2024-09-20 16:20:22 -04:00
if ent.Goat == 0 then wep:SetDesireR( NULL ) return false end
local ent = inv[ent.Goat]
if ent then
wep:SetDesireR( ent )
2024-03-07 21:19:30 -05:00
end
end
2024-09-20 16:20:22 -04:00
return false
2024-03-07 21:19:30 -05:00
end)
2024-09-20 16:20:22 -04:00
for i=0, 9 do
local tent = {}
2024-09-21 14:08:48 -04:00
--tent.Base = "goat"
2024-09-20 16:20:22 -04:00
tent.Goat = i
weapons.Register( tent, "goat_" .. i )
end