Advertisement
demon012

Minecraft - Item Sorter

Feb 26th, 2015
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. Sorter = {
  2. mePowerSide = "back",
  3. scanTimer = os.startTimer(1),
  4. meInterfacePowerTimer = os.startTimer(30),
  5. inputChest = peripheral.wrap("left"),
  6. smeltChestDirection = "up",
  7. macerateChestDirection = "east",
  8. meInterfaceDirection = "south",
  9. itemdb_path = 'itemdb',
  10. }
  11. Sorter_mt = {__index = Sorter}
  12.  
  13. function Sorter:create()--{{{
  14. local new_Sorter = {}
  15. setmetatable(new_Sorter, Sorter_mt)
  16. return new_Sorter
  17. end--}}}
  18. function Sorter:aePower(state)--{{{
  19. if type(state) == "string" then
  20. if state == 'true' then
  21. state = true
  22. else
  23. state = false
  24. end
  25. end
  26. rs.setOutput(self.mePowerSide, state)
  27. end--}}}
  28. function Sorter:aePeriodicPower()--{{{
  29. print("periodic power activated")
  30. self:aePower(true)
  31. os.sleep(0.1)
  32. self:aePower(false)
  33. self.meInterfacePowerTimer = os.startTimer(30)
  34. end--}}}
  35. function Sorter:getItemUUID(item)--{{{
  36. itemuuid = item.id .. ":" .. item.dmg
  37. return itemuuid
  38. end--}}}
  39. function Sorter:itemInDB(itemuuid)--{{{
  40. if self.itemDB[itemuuid] ~= nil then
  41. return true
  42. else
  43. return false
  44. end
  45. end--}}}
  46. function Sorter:loadItemDB()--{{{
  47. if fs.exists(self.itemdb_path) then
  48. f = fs.open(self.itemdb_path, 'r')
  49. self.itemDB = textutils.unserialize(f.readAll())
  50. f.close()
  51. f = nil
  52. else
  53. self.itemDB = {}
  54. end
  55. end--}}}
  56. function Sorter:saveItemDB()--{{{
  57. f = fs.open(self.itemdb_path, 'w')
  58. f.write(textutils.serialize(self.itemDB))
  59. f.close()
  60. f = nil
  61. end--}}}
  62. function Sorter:scan()--{{{
  63. for slot = 1, self.inputChest.getInventorySize() do
  64. item = self.inputChest.getStackInSlot(slot)
  65. if item then
  66. itemuuid = self:getItemUUID(item)
  67. if self.itemDB[itemuuid] then
  68. if self.itemDB[itemuuid].action then
  69. if self.itemDB[itemuuid].action == "smelt" then
  70. self.inputChest.pushItem(self.smeltChestDirection, slot)
  71. elseif self.itemDB[itemuuid].action == "macerate" then
  72. self.inputChest.pushItem(self.macerateChestDirection, slot)
  73. end
  74. else
  75. if self.inputChest.pushItem(self.meInterfaceDirection, slot) then
  76. self:aePeriodicPower()
  77. end
  78. end
  79. else
  80. self.itemDB[itemuuid] = item
  81. self:saveItemDB()
  82. if self.inputChest.pushItem(self.meInterfaceDirection, slot) then
  83. self:aePeriodicPower()
  84. end
  85. end
  86. end
  87. end
  88. self.scanTimer = os.startTimer(1)
  89. end--}}}
  90.  
  91. sort = Sorter
  92. sort:loadItemDB()
  93. -- scan input chest
  94.  
  95. -- if item in list of macerate items send to macerate
  96. -- if item in smelt items send to smelt
  97. -- else store in AE
  98.  
  99. while true do
  100. local event, param1, param2, param3 = os.pullEvent()
  101. -- print("Received event: " .. event)
  102. if event == "timer" then
  103. if param1 == sort.scanTimer then
  104. sort:scan()
  105. elseif param1 == sort.meInterfacePowerTimer then
  106. sort:aePeriodicPower()
  107. end
  108. end
  109. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement