Benny/gamemodes/benny/gamemode/items.lua

115 lines
2.5 KiB
Lua

---------------------
-- 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
local Itemmeta = {}
function Itemmeta:__tostring()
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
if rawget(self, "BaseClass") then
return rawget(self, "BaseClass")[key]
end
end
ITEMS = {}
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
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]
if !ITEMS[baseid] then
ErrorNoHalt("[CreateItem] Undefined base " .. baseid .. " for item " .. id)
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 )
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
end
SetupAllItems()
hook.Add("InitPostEntity", "Benny_InitPostEntity_Refresh", function()
InitPostEntity_Complete = true
end)