-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.lua
136 lines (117 loc) · 3.56 KB
/
main.lua
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
function OnTextOverlay(text)
if type(text) ~= "string" then
text = tostring(text)
end
local var = {}
var[0] = "OnTextOverlay"
var[1] = text
var.netid = -1
SendVarlist(var)
end
function getFilePath(fileName)
return os.getenv("USERPROFILE") .. "\\Desktop\\WebControl\\" .. fileName
end
function clearFile(filePath)
fileHandle = io.open(filePath, "w")
if fileHandle then
fileHandle:write("")
fileHandle:close()
else
print("Failed to open file for clearing: " .. filePath)
end
end
clearFile(getFilePath("break.txt"))
clearFile(getFilePath("place.txt"))
clearFile(getFilePath("movement.txt"))
function placeTile(x, y, id)
pkt = {}
pkt.type = 3
pkt.int_data = id
pkt.int_x = (GetLocal().pos_x // 32) + x
pkt.int_y = (GetLocal().pos_y // 32) + y
pkt.pos_x = GetLocal().pos_x
pkt.pos_y = GetLocal().pos_y
SendPacketRaw(pkt)
end
function hitTile(x, y)
pkt = {}
pkt.type = 3
pkt.int_data = 18
pkt.int_x = (GetLocal().pos_x // 32) + x
pkt.int_y = (GetLocal().pos_y // 32) + y
pkt.pos_x = GetLocal().pos_x
pkt.pos_y = GetLocal().pos_y
SendPacketRaw(pkt)
end
movementFilePath = getFilePath("movement.txt")
breakFilePath = getFilePath("break.txt")
placeFilePath = getFilePath("place.txt")
function readNewContent(fileHandle, startPos)
fileHandle:seek("set", startPos)
content = fileHandle:read("*all")
return content or "", fileHandle:seek()
end
function processAction(action)
parts = {}
for part in action:gmatch("[^_]+") do
table.insert(parts, part)
end
actionType = parts[1]
x = tonumber(parts[2])
y = tonumber(parts[3])
id = parts[4]
if actionType == "break" then
hitTile(x, y)
elseif actionType == "place" then
id_number = tonumber(id)
id_number = GetItemCount(id_number)
id_number_count = tonumber(id_number)
id_name = tonumber(id)
id_name = GetItemInfo(id_name).name
if id_number_count < 1 then
OnTextOverlay("`9No enough "..id_name.." blocks left")
else
placeTile(x, y, id)
end
end
end
function handleMovementCommand(command)
directions = {
W = {0, -1},
A = {-1, 0},
S = {0, 1},
D = {1, 0}
}
direction = directions[command]
if direction then
x, y = direction[1], direction[2]
FindPath(GetLocal().pos_x // 32 + x,GetLocal().pos_y // 32 + y)
end
end
movementFileHandle = io.open(movementFilePath, "r")
breakFileHandle = io.open(breakFilePath, "r")
placeFileHandle = io.open(placeFilePath, "r")
if not movementFileHandle or not breakFileHandle or not placeFileHandle then
print("Failed to open one or more files.")
end
lastMovementPos = 0
lastBreakPos = 0
lastPlacePos = 0
while true do
movementContent, newMovementPos = readNewContent(movementFileHandle, lastMovementPos)
lastMovementPos = newMovementPos
for line in movementContent:gmatch("[^\r\n]+") do
handleMovementCommand(line)
end
breakContent, newBreakPos = readNewContent(breakFileHandle, lastBreakPos)
lastBreakPos = newBreakPos
for line in breakContent:gmatch("[^\r\n]+") do
processAction(line)
end
placeContent, newPlacePos = readNewContent(placeFileHandle, lastPlacePos)
lastPlacePos = newPlacePos
for line in placeContent:gmatch("[^\r\n]+") do
processAction(line)
end
Sleep(1)
end