Benny/gamemodes/benny/gamemode/items.lua

131 lines
2.9 KiB
Lua
Raw Permalink Normal View History

2024-03-06 17:47:44 -05:00
2024-03-07 20:34:16 -05:00
---------------------
-- Your Name is Benny
---------------------
local Emeta = FindMetaTable("Entity")
Emetaindex = Emetaindex or Emeta.__index
function Emeta:__index( key )
local Etable = Emeta.GetTable(self)
if Etable and Etable["BennyItem"] then
local Class = Etable.Class
if !Class then
ErrorNoHalt( "[Emeta Index] " .. tostring(self) .. " missing Class key!!" )
elseif Class[key] then
return Class[key]
end
end
return Emetaindex( self, key )
end
2024-03-06 17:47:44 -05:00
2024-10-08 20:46:36 -04:00
function Emeta:SetupInventory()
local oldinv = self:GetInventory()
if IsValid( oldinv ) then
print( "[Inventory " .. tostring(oldinv) .. "] Removing to create a new inv" )
oldinv:Remove()
end
local inventory = ents.Create( "inventory" )
if IsValid( inventory ) then
inventory:AttachToEntity( self )
inventory:Spawn()
return inventory
end
return false
end
local Itemmeta = {}
2024-03-06 17:47:44 -05:00
function Itemmeta:__tostring()
2024-03-06 17:47:44 -05:00
return "ItemDef [" .. self.ClassName .. "]"
end
function Itemmeta:Get( key )
return self.key
end
function Itemmeta:GetRaw( key )
return rawget( self, key )
end
function Itemmeta.__index( self, key )
if Itemmeta[key] then return Itemmeta[key] end
2024-03-06 17:47:44 -05:00
if rawget(self, "BaseClass") then
return rawget(self, "BaseClass")[key]
end
end
ITEMS = {}
2024-03-06 17:47:44 -05:00
local AC, IN = AddCSLuaFile, include
function InitializeItemFile( filename )
print( "[InitializeItemFile] " .. filename )
AC( "items/"..filename )
local xSuccess, xMessage = pcall(function()
IN( "items/"..filename )
end)
if !xSuccess then
error("Error in initialization of " .. filename )
print( xMessage )
end
2024-09-21 14:08:48 -04:00
end
InitPostEntity_Complete = InitPostEntity_Complete or false
function CreateItem( id, baseid )
print( "[CreateItem] " .. id )
local ITEM = {}
ITEMS[id] = ITEM
ITEM.ClassName = id
if baseid then
ITEM.Base = baseid
ITEM.BaseClass = ITEMS[baseid]
2024-03-12 18:43:31 -04:00
if !ITEMS[baseid] then
ErrorNoHalt("[CreateItem] Undefined base " .. baseid .. " for item " .. id)
2024-08-30 18:30:03 -04:00
end
end
setmetatable( ITEM, Itemmeta )
do -- Entity creation
local tent = {}
tent.Base = "b-itembase"
tent.PrintName = id
tent.ID = id
tent.Class = ITEM
tent.Spawnable = true
tent.AdminOnly = false
tent.Category = "Other"
scripted_ents.Register( tent, "b-item_" .. id )
end
return ITEM, ITEMS[baseid]
end
function FinalizeItem( id )
print( "[FinalizeItem] " .. id )
2024-08-30 18:30:03 -04:00
end
function SetupAllItems()
print( "[SetupAllItems]" )
InitializeItemFile( "base.lua" )
InitializeItemFile( "firearms.lua" )
--local files, directories = file.Find("benny/gamemode/items/*", "LUA")
--for index, filename in ipairs(files) do
-- InitializeItemFile( filename )
--end
if InitPostEntity_Complete then
for i, ent in ents.Iterator() do
if !ent.BennyItem then continue end
-- Maybe make a proper refresh function in the future
ent.Class = ITEMS[ent.ID]
ent.BaseClass = ITEMS[ent.Base]
end
end
2024-08-30 18:30:03 -04:00
end
SetupAllItems()
2024-08-30 18:30:03 -04:00
hook.Add("InitPostEntity", "Benny_InitPostEntity_Refresh", function()
InitPostEntity_Complete = true
end)