Advertisement
1ng0

Mob Spawner for Turtle

Jul 21st, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.16 KB | None | 0 0
  1. ----------------------------------
  2. --    Mob Spawner for Turtle    --
  3. --                              --
  4. -- https://youtu.be/cPGYSG19Y2U --                        --
  5. ----------------------------------
  6.  
  7. spawner = peripheral.wrap("front")
  8. chest = peripheral.wrap("back")
  9. rednet.open("right")
  10. compId = 12
  11.  
  12. mobs = {}
  13. --Mobs in chest definition!
  14. function defineMobs()
  15.   mobs = {}
  16.   mobs[0] = {name = "Empty", slot = 0}
  17.   --These only if you didnt name nets with anvil
  18.   --mobs[1] = {name = "Wither skeleton", slot = 1}
  19.   --mobs[2] = {name = "Blaze", slot = 2}
  20.   --mobs[3] = {name = "Zombie", slot = 3}
  21.   --mobs[4] = {name = "Enderman", slot = 4}
  22.   --mobs[5] = {name = "Villager", slot = 5}
  23.   --mobs[6] = {name = "Magma Cube", slot = 6}
  24.   --mobs[7] = {name = "Pigman", slot = 7}
  25.   for i=1,chest.getInventorySize() do
  26.     chslot = chest.getStackInSlot(i)
  27.     if (chslot) then
  28.       mobs[i] = {name = chslot.name, slot = i}
  29.     end
  30.   end
  31. end
  32. --End mobs definition
  33. defineMobs()
  34.  
  35. currentMob = mobs[0]
  36.  
  37. spawnerSide = "east"
  38. chestSide = "west"
  39.  
  40. spawnerSafarySlotNum = 1
  41. turtleTempSlotNum = 16
  42.  
  43. function getFromSpawner()
  44.   if (spawner.getStackInSlot(spawnerSafarySlotNum)) then
  45.     return spawner.pushItemIntoSlot(spawnerSide,spawnerSafarySlotNum,1,turtleTempSlotNum)
  46.   else
  47.     return false
  48.   end
  49. end
  50.  
  51. function getFromChest(mob)
  52.   if (chest.getStackInSlot(mob.slot)) then
  53.     return chest.pushItemIntoSlot(chestSide,mob.slot,1,turtleTempSlotNum)
  54.   else
  55.     return false
  56.   end
  57. --  currentMob = mob
  58. end
  59.  
  60. function putToSpawner()
  61.   if (spawner.getStackInSlot(spawnerSafarySlotNum) == nil) then
  62.     return spawner.pullItemIntoSlot(spawnerSide,turtleTempSlotNum,1,spawnerSafarySlotNum)
  63.   else
  64.     return false
  65.   end
  66. end
  67.  
  68. function putToChest()
  69.   return chest.pullItemIntoSlot(chestSide,turtleTempSlotNum,1,currentMob.slot)
  70. --  currentMob = mobs[0]
  71. end
  72.  
  73. function fromChestToSpawner(mob)
  74.   if (mob.slot ~= 0) then
  75.     if (getFromChest(mob)) then
  76.       currentMob = mob
  77.       if (putToSpawner()) then
  78.         return true
  79.       else
  80.         print ("Unable to put safary net to spawner!")
  81.         print ("Returning safary net to chest!")
  82.         putToChest()
  83.         currentMob = mobs[0]
  84.         return false
  85.       end
  86.     else
  87.       print ("Unable to take safary net from chest!")
  88.       return false
  89.     end
  90.   else
  91.     --print("Do something with empty request, maybe just emptying spawner")
  92.     fromSpawnerToChest()
  93.     return true
  94.   end
  95. end
  96.  
  97. function fromSpawnerToChest()
  98.   if (currentMob.slot ~= 0) then
  99.     if (getFromSpawner()) then
  100.       print ("Succes getting safary net from spawner")
  101.       putToChest()
  102.       currentMob = mobs[0]
  103.     else
  104.       print ("Cant get from spawner!")
  105.       return false
  106.     end
  107.   else
  108.     print ("Current Mob data not defined, dont know where to put safary net!")
  109.     return false
  110.   end  
  111. end
  112.  
  113. function refreshAndGetMobs()
  114.   defineMobs()
  115.   return mobs
  116. end
  117.  
  118. function response(mess)
  119.   rednet.send(compId,mess)
  120. end
  121. commands = {
  122.   getAllMobs = function() return refreshAndGetMobs() end,
  123.   getCurrentMob = function() return currentMob end,
  124.   fromChestToSpawner = function(args) return fromChestToSpawner(args[1]) end,
  125.   fromSpawnerToChest = function() return fromSpawnerToChest() end,
  126.   getSpawnExact = function() return spawner.getSpawnExact() end,
  127.   setSpawnExact = function(args) return spawner.setSpawnExact(args[1]) end,
  128.   getTankInfo = function() return spawner.getTankInfo("unknown")[1] end
  129. }
  130.  
  131. while true do
  132.   message = {}
  133.   message.sId, message.command, message.distance = rednet.receive()
  134.   if (message["command"]["cname"] == "Test") then
  135.     response(mobs)
  136.   else
  137.     print ("Executing "..message["command"]["cname"].." command with args ")
  138.     response(commands[message["command"]["cname"]](message["command"]["args"]))
  139.   end
  140. end
  141.   elseif message=="remove" then
  142.     event, ID, message, dist = os.pullEvent("rednet_message")
  143.     if type(message)=="number" then
  144.       testSpawner()
  145.       print("Removing mob in slot "..message)
  146.       chest.push("up", message-1, 1)
  147.       turtle.drop()
  148.       rednet.send(1, "done")
  149.     else
  150.       print("No mob selected")
  151.     end
  152.   end
  153.   sleep(0.01)
  154. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement