MattiasBuelens

MCNet names

Jun 8th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.  
  3.     Minecraft Intranet
  4.     Name registration
  5.  
  6. --]]
  7.  
  8. mcnet = mcnet or {}
  9. mcnet.names = mcnet.names or {}
  10. local names = mcnet.names
  11.  
  12. local Names = common.newClass{}
  13.  
  14. function names.new()
  15.     return Names:new()
  16. end
  17.  
  18. -- Constructor
  19. function Names:init()
  20.     self.names = {}
  21. end
  22.  
  23. -- Registration
  24. function Names:register(name, computerID)
  25.     assert(type(name) == "string", "invalid name")
  26.     assert(type(computerID) == "number", "invalid computer ID")
  27.  
  28.     self.names[name] = computerID
  29.     return true
  30. end
  31.  
  32. function Names:unregister(name)
  33.     assert(type(name) == "string", "invalid name")
  34.  
  35.     self.names[name] = nil
  36.     return true
  37. end
  38.  
  39. -- Retrieval
  40. function Names:get(name)
  41.     assert(type(name) == "string", "invalid name")
  42.  
  43.     return self.names[name]
  44. end
  45.  
  46. function Names:getName(computerID)
  47.     assert(type(computerID) == "number", "invalid computer ID")
  48.  
  49.     for name,id in pairs(self.names) do
  50.         if (computerID == id) then
  51.             return name
  52.         end
  53.     end
  54.  
  55.     return nil
  56. end
  57.  
  58. function Names:getAll()
  59.     return common.getValues(self.names)
  60. end
  61.  
  62. function Names:getNames()
  63.     return common.getKeys(self.names)
  64. end
Advertisement
Add Comment
Please, Sign In to add comment