--[[ 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 col = nrequire("config/colortheme.lua") print("Got invnetory table, it is:") PrintTable(inventory) local slots = { "Head", "Shoulders", "Chest", "Arms", "Left Hand", "Right Hand", "Dual", "Legs", "Belt", "Gloves", "Feet", "Back", "Ring 1", "Ring 2", "Ring 3", "Ring 4", "Ring 5", "Ring 6", } local inv = {} if SERVER then inv = nrequire("sv_equipment.lua") end if CLIENT then inv = nrequire("cl_equipment.lua") end inv.Name = "Equipment" inv.equiped = {} inv.FindPlaceFor = function(self, item) --Make sure it's equipable if not item.Equipable then return nil end --If this is a dual weielding weapon if item.Equipable == "Dual" then if self.equiped["Left Hand"] == nil and self.equiped["Right Hand"] == nil then return {"Dual"} else return nil end end --If this item is a left or right handed, make sure we don't have a dual equiped if item.Equipable == "Left Hand" or item.Equipable == "Right Hand" then if self.equiped["Dual"] ~= nil then return nil elseif self.equiped[item.Equipable] ~= nil then return nil else return {item.Equipable} end end --If this item is a ring if item.Equipable == "Ring" then for i = 1,6 do if self.equiped["Ring " .. i] == nil then return {"Ring " .. i} end end return nil end --Otherwise, just check if the slot is empty if self.equiped[item.Equipable] == nil and slots[self.equiped] ~= nil then return {item.Equipable} else return nil end end inv.CanFitIn = function(self,position,item) return (position[1] == item.Equipable) and (self.equiped[position[1]] == nil) end inv.Put = function(self,position,item) self.equiped[position[1]] = item if item.onEquip then item:onEquip(self.Owner) end end inv.Has = function(self,string_or_compare_func) if type(string_or_compare_func) == "string" then for k,v in pairs(self.equiped) do if v.Name == string_or_compare_func then return k end end return nil elseif type(string_or_compare_func) == "function" then for k,v in pairs(self.equiped) do if string_or_compare_func(v.Name) then return k end end return nil end error(string.format("equipment:Has() called with a %s, expected string or function.",type(string_or_compare_func))) end inv.Remove = function(self,position) local item = self.equiped[position[1]] if item.onUnEquip then item:onUnEquip(self.Owner) end self.equiped[position[1]] = nil end inv.Get = function(self,position) return self.equiped[position[1]] end inv.Serialize = function(self) local tbl = {} for k,v in pairs(self.equiped) do tbl[k] = v:Serialize() end return util.TableToJSON(tbl) end inv.DeSerialize = function(self,data) print("deserializeing, data was",data) if data ~= nil and data ~= "" then local tbl = util.JSONToTable(data) local cpy = table.Copy(self) for k,v in pairs(tbl) do cpy.equiped[k] = itm.GetItemFromData(v) end else return table.Copy(self) end end inventory.RegisterInventory(inv)