-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeAssist.lua
More file actions
79 lines (73 loc) · 3.04 KB
/
TreeAssist.lua
File metadata and controls
79 lines (73 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
--init a plugin
--Author Lorain.Li
g_Plugin = nil
a_LeavesMap = nil
function Initialize(a_Plugin)
a_Plugin:SetName("TreeAssist")
a_Plugin:SetVersion(4)
g_Plugin = a_Plugin
cPluginManager:AddHook(cPluginManager.HOOK_PLAYER_BREAKING_BLOCK, MyOnPlayerBreakingBlock)
LOG("TreeAssist v".. g_Plugin:GetVersion() .." is loaded")
return true
end
function OnDisable()
LOG("TreeAssist v" .. g_Plugin:GetVersion() .. " is disabling")
end
function MyOnPlayerBreakingBlock(a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_BlockType, a_BlockMeta)
local EquippedItem = a_Player:GetEquippedItem()
if (not ItemCategory.IsAxe(EquippedItem.m_ItemType)) then
return false
end
local a_World = a_Player:GetWorld()
a_LeavesMap = {}
CollectWood(a_World, a_BlockX, a_BlockY, a_BlockZ ,a_BlockType, a_BlockMeta % 4, a_LeavesMap, a_BlockX, a_BlockZ)
end
function CollectWood(a_World, a_BlockX, a_BlockY, a_BlockZ ,a_BlockType, a_BlockMeta, a_LeavesMap ,a_BlockCX, a_BlockCZ)
if(a_BlockX < a_BlockCX - 5 or a_BlockX > a_BlockCX + 5) then
return
end
if(a_BlockZ < a_BlockCZ - 5 or a_BlockZ > a_BlockCZ + 5) then
return
end
local a_BlockID = a_World:GetBlock(a_BlockX, a_BlockY, a_BlockZ)
local a_BlockMT = a_World:GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) % 4
if(not (BlockIsWood(a_BlockID) or BlockIsLeaves(a_BlockID))) then
return
end
--is wood but type is diffirent
if(BlockIsWood(a_BlockID) and a_BlockMT ~= a_BlockMeta) then
return
end
if(BlockIsWood(a_BlockID)) then
local a_Items = cItems()
a_Items:Add(a_BlockType, 1, a_BlockMeta)
--collect log
a_World:DigBlock(a_BlockX, a_BlockY, a_BlockZ)
a_World:SpawnItemPickups(a_Items, a_BlockX, a_BlockY, a_BlockZ, math.random())
--plant a sapling
if(CanPlantSapling(a_World:GetBlock(a_BlockX, a_BlockY - 1, a_BlockZ))) then
a_World:SetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_SAPLING, a_BlockMeta, false)
end
end
if(BlockIsLeaves(a_BlockID)) then
if(a_LeavesMap[a_BlockX .. ":" .. a_BlockY .. ":" .. a_BlockZ] == true) then
return
else
a_LeavesMap[a_BlockX .. ":" .. a_BlockY .. ":" .. a_BlockZ] = true
end
end
CollectWood(a_World, a_BlockX - 1, a_BlockY, a_BlockZ,a_BlockType, a_BlockMeta, a_LeavesMap, a_BlockCX, a_BlockCZ)
CollectWood(a_World, a_BlockX + 1, a_BlockY, a_BlockZ,a_BlockType, a_BlockMeta, a_LeavesMap, a_BlockCX, a_BlockCZ)
CollectWood(a_World, a_BlockX, a_BlockY, a_BlockZ - 1,a_BlockType, a_BlockMeta, a_LeavesMap, a_BlockCX, a_BlockCZ)
CollectWood(a_World, a_BlockX, a_BlockY, a_BlockZ + 1,a_BlockType, a_BlockMeta, a_LeavesMap, a_BlockCX, a_BlockCZ)
CollectWood(a_World, a_BlockX, a_BlockY + 1, a_BlockZ,a_BlockType, a_BlockMeta, a_LeavesMap, a_BlockCX, a_BlockCZ)
end
function BlockIsWood(a_BlockID)
return ((a_BlockID == E_BLOCK_LOG) or (a_BlockID == E_BLOCK_NEW_LOG))
end
function BlockIsLeaves( a_BlockID )
return ((a_BlockID == E_BLOCK_LEAVES) or (a_BlockID == E_BLOCK_NEW_LEAVES))
end
function CanPlantSapling( a_BlockID )
return (a_BlockID == E_BLOCK_DIRT) or (a_BlockID == E_BLOCK_GRASS)
end