Advertisement
TheIncgi

Tree Harvester - Advanced Macros (Minecraft)

Dec 26th, 2018
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.30 KB | None | 0 0
  1. --For use with the Advanced Macros Minecraft mod
  2. HarvestTree = HarvestTree or {}
  3.  
  4. local PathFinder = run("~macros/projects/pathFinder A/PathFinder.lua")
  5. HarvestTree.pathFinder = PathFinder:new()
  6. HarvestTree.pathFinder.stepSize = .5
  7. HarvestTree.pathFinder.DEBUG = false
  8. HarvestTree.pathFinder.speedFactor = .75
  9. HarvestTree.pathFinder.successDist = 1
  10. --log("Loaded path finder")
  11. local ALLOW_REPAIR = true
  12. local AXE_TYPE = "minecraft:stone_axe"
  13. local MATERIAL_TYPE = "minecraft:cobblestone"
  14. local MAX_RADIUS = 20 --blocks
  15. local LOG_NAME = "minecraft:log"
  16. local LOG_TYPE = 2
  17. local SAPLING_NAME = "minecraft:sapling"
  18. local SAPLING_TYPE = 2
  19. local LOG = "birch"
  20.  
  21. --log(PathFinder)
  22. function HarvestTree.selectItem(sitem, prefSlot, optInv)
  23.   return HarvestTree.selectItemD(sitem, -1, prefSlot, optInv)
  24. end
  25. function HarvestTree.selectItemD(sitem, sdmg, prefSlot, optInv)
  26.   local inv = optInv or openInventory()
  27.   local map = inv.mapping.inventory
  28.   prefSlot = prefSlot or 1
  29.  
  30.   for i,j in pairs(map.hotbar) do
  31.     local item = inv.getSlot(j)
  32.     if item and( item.id==sitem and( sdmg==-1 or sdmg==item.dmg)) then
  33.      
  34.       setHotbar(i)
  35.       return j
  36.     end
  37.   end
  38.   for i,j in pairs(map.main) do
  39.     local item = inv.getSlot( j )
  40.     if item and (item.id==sitem and( sdmg==-1 or sdmg==item.dmg))  then
  41.       setHotbar(prefSlot)
  42.       local p = map.hotbar[prefSlot]
  43.       inv.click(j)
  44.       sleep(40)
  45.       inv.click(p)
  46.       sleep(40)
  47.       if inv.getHeld() then
  48.         inv.click(j)
  49.         sleep(40)
  50.       end
  51.       if not optInv then inv.close() end
  52.       return prefSlot
  53.     end
  54.  
  55.   end
  56.   return false
  57. end
  58.  
  59. function HarvestTree.needsRepair( slot, optInv )
  60.   local inv = optInv or openInventory()
  61.   local item = inv.getSlot(slot)
  62.   if item then
  63.     local remaining = item.maxDmg - item.dmg
  64.     return remaining < 20
  65.   else
  66.     return true --so broken its not there!
  67.   end
  68. end
  69.  
  70. function HarvestTree.matchesLog(block)
  71.   return block.id == LOG_NAME and block.dmg == LOG_TYPE
  72. end
  73.  
  74. function HarvestTree.breakTree(pos)
  75.   local x, y, z = table.unpack(pos)
  76.   HarvestTree.to(x, y, z)
  77.   HarvestTree.selectItem( AXE_TYPE )
  78.   lookAt(x+.5,y+.5,z+.5)
  79.   waitTick()
  80.   attack(-1)
  81.   for dy = 0, 6 do
  82.     if dy == 2 then
  83.       forward(-1)
  84.       local m = os.millis()+500
  85.       while PathFinder.distFunc({getPlayerPos()}, pos) > .5 and os.millis() < m do
  86.         lookAt(x+.5,y+dy+.5,z+.5)
  87.         log({getPlayerPos()},pos)
  88.         waitTick()
  89.       end
  90.       forward(0)
  91.     end
  92.     sleep(150)
  93.     if(HarvestTree.matchesLog(getBlock(x,y+dy,z)))then
  94.       lookAt(x+.5,y+dy+.5,z+.5)
  95.       while HarvestTree.matchesLog(getBlock(x,y+dy,z)) do
  96.         waitTick()
  97.       end
  98.     end
  99.   end
  100.   attack(1)
  101.   sleep(100)
  102.   if HarvestTree.selectItemD( SAPLING_NAME, SAPLING_TYPE ) then
  103.     lookAt(x+.5,y,z+.5)
  104.     waitTick()
  105.     use(40)
  106.   else
  107.     log("No sapling located")
  108.   end
  109. end
  110.  
  111. function HarvestTree.findTree(maxRad)
  112.   maxRad = maxRad or MAX_RADIUS
  113.   local px, py, pz = getPlayerBlockPos()
  114.   local m = false
  115.   local mdist = maxRad + 1000
  116.   for dx = -maxRad, maxRad do
  117.     for dz = -maxRad, maxRad do
  118.       local x,y,z = px+dx, py, pz+dz
  119.       local b = getBlock(x,y,z)
  120.       if b then
  121.         if b.id == LOG_NAME and b.dmg==LOG_TYPE then
  122.           local dist = PathFinder.distFunc(px,py,pz, x,y,z)
  123.           if dist < mdist then
  124.             m = {x,y,z}
  125.             mdist = dist
  126.           end
  127.         end
  128.       end
  129.     end
  130.   end
  131.   return m
  132. end
  133.  
  134. function HarvestTree.repairItem(itemSlot, materialSlot)
  135.   log("Need reapir")
  136.   local axeSlot = HarvestTree.selectItem( AXE_TYPE, 4 )
  137.   local matSlot = HarvestTree.selectItem( MATERIAL_TYPE, 5 )
  138.   local inv = openInventory()
  139.   local map = inv.mapping.inventory
  140.   inv.click(axeSlot)
  141.   sleep(40)
  142.   inv.click(map.craftingIn[1])
  143.   sleep(40)
  144.   inv.click(matSlot)
  145.   sleep(40)
  146.   inv.click(map.craftingIn[2])
  147.   sleep(40)
  148.   inv.quick(map.craftingOut)
  149.   sleep(40)
  150.   if(inv.getSlot(map.craftingIn[2]))then
  151.     inv.quick(map.craftingIn[2])
  152.   end
  153.   inv.close()
  154.   sleep(2000)
  155. end
  156.  
  157. function HarvestTree.findItems()
  158.   local _,py = getPlayerBlockPos()
  159.   local eList = getEntityList()
  160.   local out = {}
  161.   for a,b in pairs( eList )do
  162.     if(b.name:lower():find(LOG))then
  163.       local p = getEntity(b.id).pos
  164.       if p[2] == py then
  165.         out[#out+1] = p
  166.       end
  167.     end
  168.   end
  169.   return out
  170. end
  171.  
  172. function HarvestTree.collectItem(x,y,z)
  173.   HarvestTree.to(x,y,z)
  174.   sleep(350)
  175. end
  176.  
  177. function HarvestTree.replant(x, z)
  178. end
  179.  
  180. local function _lookAt(p)
  181.   local eh = getPlayer().eyeHeight
  182.   local x,y,z = table.unpack(p)
  183.   lookAt(x,y+eh,z) waitTick()
  184. end
  185.  
  186. function HarvestTree.to(x,y,z)
  187.   local px, py, pz = getPlayerBlockPos()
  188.   local result = HarvestTree.pathFinder:to(x,py,z, px,py,pz)
  189.   forward(-1)
  190.   for a,b in pairs(result) do
  191.     log(b.pos)
  192.     while PathFinder.distFunc(b.pos, {getPlayerPos()}) > HarvestTree.pathFinder.stepSize do
  193.       _lookAt(b.pos)
  194.     end
  195.   end
  196.   forward(0)
  197.   --log(result)
  198. end
  199.  
  200. function HarvestTree.step()
  201.   HarvestTree.stepMod = (HarvestTree.stepMod or 0)+1
  202.   local stepMod = HarvestTree.stepMod
  203.  
  204.   local axeSlot = HarvestTree.selectItem( AXE_TYPE )
  205.   if not axeSlot then
  206.     log("&aHarvest Tree &7 &Uno "..AXE_TYPE.." found");
  207.     HarvestTree.active = false
  208.     return
  209.   end
  210.   if HarvestTree.needsRepair( axeSlot ) then
  211.     if ALLOW_REPAIR == false then
  212.       HarvestTree.active = false
  213.       log("&aHarvest Tree&7 is exiting due to &Ulow durability")
  214.       return
  215.     end
  216.     local materialSlot = HarvestTree.selectItem( MATERIAL_TYPE )
  217.     if materialSlot then
  218.       HarvestTree.repairItem( axeSlot, materialSlot )
  219.       waitTick()
  220.       if HarvestTree.needsRepair( axeSlot ) then
  221.         HarvestTree.active = false
  222.         log("&aHarvest Tree &7was unable to repair your &Ntool", AXE_TYPE )
  223.         return
  224.       end
  225.     else
  226.       HarvestTree.active = false
  227.       log("&aHarvest Tree &7was unable to repair your &U&Ntool&7 due to &U&NMissing material",AXE_TYPE, MATERIAL_TYPE)
  228.       return
  229.     end
  230.   end
  231.   local nearest = HarvestTree.findTree( MAX_RAD )
  232.   if nearest then
  233.     HarvestTree.breakTree( nearest )
  234.   end
  235.   if(stepMod%3==0)then
  236.     for a,b in pairs( HarvestTree.findItems() ) do
  237.       log("Item: ",b)
  238.       HarvestTree.collectItem( table.unpack(b) )
  239.     end
  240.   end
  241. end
  242.  
  243. function HarvestTree.toggle()
  244.   HarvestTree.active = not HarvestTree.active
  245.   if not HarvestTree.active then return end
  246.   log("&eTree Auto-harvester: &aENABLED")
  247.   while HarvestTree.active do
  248.     a,b = pcall(HarvestTree.step)
  249.     if not a then log("&c"..b) HarvestTree.active = false break end
  250.     waitTick()
  251.     sleep(250)
  252.   end
  253.   log("&eTree Auto-harvester: &cDISABLED")
  254. end
  255.  
  256.  
  257. HarvestTree.toggle()
  258. --HarvestTree.repairItem()
  259. --log(HarvestTree.selectItem( AXE_TYPE, 4 ))
  260.  
  261. --log("Player:")
  262. --log{getPlayerPos()}
  263. --log("Tree:")
  264. --log(HarvestTree.findTree())
  265.  
  266. --local tx,ty,tz = table.unpack(HarvestTree.findTree())--table.unpack(HarvestTree.findTree())
  267. --tx,ty,tz = tx, ty, tz
  268.  
  269. --hud3D.clearAll()
  270. --HarvestTree.to(tx, ty, tz)
  271.  
  272. --if HarvestTree.target and not HarvestTree.target.destroy then HarvestTree.target = nil end
  273. --HarvestTree.target = HarvestTree.target or hud3D.newBlock(tx, ty, tz)
  274. --HarvestTree.target.setWidth(.1)
  275. --HarvestTree.target.xray()
  276. --HarvestTree.target.enableDraw()
  277.  
  278. log("Done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement