Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --For use with the Advanced Macros Minecraft mod
- HarvestTree = HarvestTree or {}
- local PathFinder = run("~macros/projects/pathFinder A/PathFinder.lua")
- HarvestTree.pathFinder = PathFinder:new()
- HarvestTree.pathFinder.stepSize = .5
- HarvestTree.pathFinder.DEBUG = false
- HarvestTree.pathFinder.speedFactor = .75
- HarvestTree.pathFinder.successDist = 1
- --log("Loaded path finder")
- local ALLOW_REPAIR = true
- local AXE_TYPE = "minecraft:stone_axe"
- local MATERIAL_TYPE = "minecraft:cobblestone"
- local MAX_RADIUS = 20 --blocks
- local LOG_NAME = "minecraft:log"
- local LOG_TYPE = 2
- local SAPLING_NAME = "minecraft:sapling"
- local SAPLING_TYPE = 2
- local LOG = "birch"
- --log(PathFinder)
- function HarvestTree.selectItem(sitem, prefSlot, optInv)
- return HarvestTree.selectItemD(sitem, -1, prefSlot, optInv)
- end
- function HarvestTree.selectItemD(sitem, sdmg, prefSlot, optInv)
- local inv = optInv or openInventory()
- local map = inv.mapping.inventory
- prefSlot = prefSlot or 1
- for i,j in pairs(map.hotbar) do
- local item = inv.getSlot(j)
- if item and( item.id==sitem and( sdmg==-1 or sdmg==item.dmg)) then
- setHotbar(i)
- return j
- end
- end
- for i,j in pairs(map.main) do
- local item = inv.getSlot( j )
- if item and (item.id==sitem and( sdmg==-1 or sdmg==item.dmg)) then
- setHotbar(prefSlot)
- local p = map.hotbar[prefSlot]
- inv.click(j)
- sleep(40)
- inv.click(p)
- sleep(40)
- if inv.getHeld() then
- inv.click(j)
- sleep(40)
- end
- if not optInv then inv.close() end
- return prefSlot
- end
- end
- return false
- end
- function HarvestTree.needsRepair( slot, optInv )
- local inv = optInv or openInventory()
- local item = inv.getSlot(slot)
- if item then
- local remaining = item.maxDmg - item.dmg
- return remaining < 20
- else
- return true --so broken its not there!
- end
- end
- function HarvestTree.matchesLog(block)
- return block.id == LOG_NAME and block.dmg == LOG_TYPE
- end
- function HarvestTree.breakTree(pos)
- local x, y, z = table.unpack(pos)
- HarvestTree.to(x, y, z)
- HarvestTree.selectItem( AXE_TYPE )
- lookAt(x+.5,y+.5,z+.5)
- waitTick()
- attack(-1)
- for dy = 0, 6 do
- if dy == 2 then
- forward(-1)
- local m = os.millis()+500
- while PathFinder.distFunc({getPlayerPos()}, pos) > .5 and os.millis() < m do
- lookAt(x+.5,y+dy+.5,z+.5)
- log({getPlayerPos()},pos)
- waitTick()
- end
- forward(0)
- end
- sleep(150)
- if(HarvestTree.matchesLog(getBlock(x,y+dy,z)))then
- lookAt(x+.5,y+dy+.5,z+.5)
- while HarvestTree.matchesLog(getBlock(x,y+dy,z)) do
- waitTick()
- end
- end
- end
- attack(1)
- sleep(100)
- if HarvestTree.selectItemD( SAPLING_NAME, SAPLING_TYPE ) then
- lookAt(x+.5,y,z+.5)
- waitTick()
- use(40)
- else
- log("No sapling located")
- end
- end
- function HarvestTree.findTree(maxRad)
- maxRad = maxRad or MAX_RADIUS
- local px, py, pz = getPlayerBlockPos()
- local m = false
- local mdist = maxRad + 1000
- for dx = -maxRad, maxRad do
- for dz = -maxRad, maxRad do
- local x,y,z = px+dx, py, pz+dz
- local b = getBlock(x,y,z)
- if b then
- if b.id == LOG_NAME and b.dmg==LOG_TYPE then
- local dist = PathFinder.distFunc(px,py,pz, x,y,z)
- if dist < mdist then
- m = {x,y,z}
- mdist = dist
- end
- end
- end
- end
- end
- return m
- end
- function HarvestTree.repairItem(itemSlot, materialSlot)
- log("Need reapir")
- local axeSlot = HarvestTree.selectItem( AXE_TYPE, 4 )
- local matSlot = HarvestTree.selectItem( MATERIAL_TYPE, 5 )
- local inv = openInventory()
- local map = inv.mapping.inventory
- inv.click(axeSlot)
- sleep(40)
- inv.click(map.craftingIn[1])
- sleep(40)
- inv.click(matSlot)
- sleep(40)
- inv.click(map.craftingIn[2])
- sleep(40)
- inv.quick(map.craftingOut)
- sleep(40)
- if(inv.getSlot(map.craftingIn[2]))then
- inv.quick(map.craftingIn[2])
- end
- inv.close()
- sleep(2000)
- end
- function HarvestTree.findItems()
- local _,py = getPlayerBlockPos()
- local eList = getEntityList()
- local out = {}
- for a,b in pairs( eList )do
- if(b.name:lower():find(LOG))then
- local p = getEntity(b.id).pos
- if p[2] == py then
- out[#out+1] = p
- end
- end
- end
- return out
- end
- function HarvestTree.collectItem(x,y,z)
- HarvestTree.to(x,y,z)
- sleep(350)
- end
- function HarvestTree.replant(x, z)
- end
- local function _lookAt(p)
- local eh = getPlayer().eyeHeight
- local x,y,z = table.unpack(p)
- lookAt(x,y+eh,z) waitTick()
- end
- function HarvestTree.to(x,y,z)
- local px, py, pz = getPlayerBlockPos()
- local result = HarvestTree.pathFinder:to(x,py,z, px,py,pz)
- forward(-1)
- for a,b in pairs(result) do
- log(b.pos)
- while PathFinder.distFunc(b.pos, {getPlayerPos()}) > HarvestTree.pathFinder.stepSize do
- _lookAt(b.pos)
- end
- end
- forward(0)
- --log(result)
- end
- function HarvestTree.step()
- HarvestTree.stepMod = (HarvestTree.stepMod or 0)+1
- local stepMod = HarvestTree.stepMod
- local axeSlot = HarvestTree.selectItem( AXE_TYPE )
- if not axeSlot then
- log("&aHarvest Tree &7 &Uno "..AXE_TYPE.." found");
- HarvestTree.active = false
- return
- end
- if HarvestTree.needsRepair( axeSlot ) then
- if ALLOW_REPAIR == false then
- HarvestTree.active = false
- log("&aHarvest Tree&7 is exiting due to &Ulow durability")
- return
- end
- local materialSlot = HarvestTree.selectItem( MATERIAL_TYPE )
- if materialSlot then
- HarvestTree.repairItem( axeSlot, materialSlot )
- waitTick()
- if HarvestTree.needsRepair( axeSlot ) then
- HarvestTree.active = false
- log("&aHarvest Tree &7was unable to repair your &Ntool", AXE_TYPE )
- return
- end
- else
- HarvestTree.active = false
- log("&aHarvest Tree &7was unable to repair your &U&Ntool&7 due to &U&NMissing material",AXE_TYPE, MATERIAL_TYPE)
- return
- end
- end
- local nearest = HarvestTree.findTree( MAX_RAD )
- if nearest then
- HarvestTree.breakTree( nearest )
- end
- if(stepMod%3==0)then
- for a,b in pairs( HarvestTree.findItems() ) do
- log("Item: ",b)
- HarvestTree.collectItem( table.unpack(b) )
- end
- end
- end
- function HarvestTree.toggle()
- HarvestTree.active = not HarvestTree.active
- if not HarvestTree.active then return end
- log("&eTree Auto-harvester: &aENABLED")
- while HarvestTree.active do
- a,b = pcall(HarvestTree.step)
- if not a then log("&c"..b) HarvestTree.active = false break end
- waitTick()
- sleep(250)
- end
- log("&eTree Auto-harvester: &cDISABLED")
- end
- HarvestTree.toggle()
- --HarvestTree.repairItem()
- --log(HarvestTree.selectItem( AXE_TYPE, 4 ))
- --log("Player:")
- --log{getPlayerPos()}
- --log("Tree:")
- --log(HarvestTree.findTree())
- --local tx,ty,tz = table.unpack(HarvestTree.findTree())--table.unpack(HarvestTree.findTree())
- --tx,ty,tz = tx, ty, tz
- --hud3D.clearAll()
- --HarvestTree.to(tx, ty, tz)
- --if HarvestTree.target and not HarvestTree.target.destroy then HarvestTree.target = nil end
- --HarvestTree.target = HarvestTree.target or hud3D.newBlock(tx, ty, tz)
- --HarvestTree.target.setWidth(.1)
- --HarvestTree.target.xray()
- --HarvestTree.target.enableDraw()
- log("Done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement