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
60
61
62
63
64
65
66
67
|
--[[
A simple inventory that holds 1 item
]]
local itm = nrequire("item.lua")
local ste = nrequire("utility/stream.lua")
local inventory = nrequire("inventory/inventory.lua")
local slots = {
"Head",
"Shoulders",
"Chest",
"Arms",
"Hands",
"Legs",
"Belt",
"Feet",
"Back",
}
for k,v in pairs(slots) do
local inv = {}
inv.Name = "inv_" .. v
inv.FindPlaceFor = function(self, item)
if self.item == nil then return {} else return nil end
end
inv.CanFitIn = function(self,position,item)
if self.item == nil then return true else return "Inventory slot occupied by a(n)" .. self.item.Name end
end
inv.Put = function(self,pos,item)
self.item = item
end
inv.Has = function(self,prt)
if type(prt) == "string" then
if self.item ~= nil and self.item.Name == prt then return {} else return nil end
elseif type(prt) == "function" then
if prt(self.item) then return {} else return nil end
end
error(string.format("Passed a %s to %s:Has(), expected string or function",type(prt),self.Name))
end
inv.Remove = function(self,pos)
self.item = nil
end
inv.Get = function(self,pos)
return self.item
end
inv.Serialize = function(self)
if self.item then
local data = ste.CreateStream()
local itemname = self.item.Name
local itemdata = self.item:Serialize()
data:WriteString(itemname)
data:WriteString(itemdata)
return data:ToString()
end
return ""
end
inv.DeSerialize = function(self,str)
if str == "" then
return table.Copy(self)
else
local data = ste.CreateStream(str)
local itemname = data:ReadString()
local itemdata = data:ReadString()
self.item = itm.GetItemFromData(itemname,itemdata)
end
end
inventory.RegisterInventory(inv)
end
|