aboutsummaryrefslogtreecommitdiff
path: root/gamemode/itemsystem/loaditems.lua
blob: 2e5be4ecbded81916a0b25293cbafb0544f4b7cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
local Folder  	= GM.Folder:gsub("gamemodes/","").."/gamemode/itemsystem/items"
local insert 	= table.insert
print("Hello from loaditems.lua!")

GM.Items = GM.Items or {}
GM.Recipes = GM.Items or {}

--This is so that the client can store info about items/recepies too.
local gmitems = (GAMEMODE or GM).Items
local gmrecepies = (GAMEMODE or GM).Recipes
local itemfiles = {}
function RegisterItem(tbl)
	assert(gmitems[tbl] == nil, "Cannot register 2 items with the same name!")
	tb = debug.getinfo(2)
	src = string.sub(tb.source,2)
	itemfiles[#itemfiles + 1] = src
	insert(gmitems,tbl)
	if tbl.Recipe then
		insert(gmrecepies,tbl)
	end
	print("Registered item:", tbl.Name)
end

(GAMEMODE or GM).LoadItems = function()
	allfiles = table.Copy(itemfiles)
	for _,itemfile in pairs(allfiles) do
		f = file.Read(itemfile,"GAME")
		c = CompileString(f,itemfile)
		print("executing",c)
		if c ~= nil then
			c()
		end
	end
end

function GetItemByName(name)
	for k,v in pairs( gmitems ) do
		if (v.Name == name) then return v end
	end

	return nil
end

function GetRecipeForItem(name)
	for k,v in pairs( gmrecepies ) do
		if (v.Name == name) then return v.Recipe,v end
	end

	return nil
end

function GetItemsByClass(class)
	local Dat = {}
	for k,v in pairs( gmitems ) do
		if (v.Class == class) then table.insert(Dat,v) end
	end

	return Dat
end