/
main.lua
340 lines (250 loc) · 9.94 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
-- main.lua
GROUND_LEVEL = 63
GROUND_MIN_X = -50
GROUND_MAX_X = 50
GROUND_MIN_Z = -50
GROUND_MAX_Z = 50
MAX_BLOCK_UPDATE_PER_TICK = 50
UpdateQueue = nil
PLUGIN = nil;
CANCREATESERVER = 0;
CURRENTSERVERBLOCKX = 0;
CURRENTSERVERBLOCKY = 0;
CURRENTSERVERBLOCKZ = 0;
-- Production
PRODUCTION = 1
-- Tick is triggered by cPluginManager.HOOK_TICK
function Tick(TimeDelta)
UpdateQueue:update(MAX_BLOCK_UPDATE_PER_TICK)
end
-- Plugin initialization
function Initialize(Plugin)
PLUGIN = Plugin;
PLUGIN:SetName("GandiCraft");
PLUGIN:SetVersion(1);
-- Load the Info shared library:
dofile(cPluginManager:GetPluginsPath() .. "/InfoReg.lua");
UpdateQueue = NewUpdateQueue()
-- Hooks
cPluginManager:AddHook(cPluginManager.HOOK_WORLD_STARTED, WorldStarted);
cPluginManager:AddHook(cPluginManager.HOOK_PLAYER_JOINED, PlayerJoined);
cPluginManager:AddHook(cPluginManager.HOOK_PLAYER_USING_BLOCK, PlayerUsingBlock);
cPluginManager:AddHook(cPluginManager.HOOK_CHUNK_GENERATING, OnChunkGenerating);
cPluginManager:AddHook(cPluginManager.HOOK_WEATHER_CHANGING, OnWeatherChanging);
cPluginManager:AddHook(cPluginManager.HOOK_PLAYER_FOOD_LEVEL_CHANGE, OnPlayerFoodLevelChange);
cPluginManager:AddHook(cPluginManager.HOOK_SPAWNING_MONSTER, OnSpawningMonster);
cPluginManager:AddHook(cPluginManager.HOOK_TICK, Tick);
-- Command Bindings
cPluginManager.BindCommand("/gandi", "*", GandiCommand, " - Gandi CLI commands")
cPluginManager.BindCommand("/structure", "*", StructureCommand, " - Structure CLI commands")
-- Web request
Plugin:AddWebTab("Gandi",HandleRequest_Gandi)
-- make all players admin
cRankManager:SetDefaultRank("Admin")
ini = cIniFile();
--Create a new config if this is the first load, otherwise load the existing config file
LoadConfig();
--Create a new database if this is the first load, otherwise open the existing one
if not (cFile:IsFile(Plugin:GetLocalFolder() .. cFile:GetPathSeparator() .. config.dbname)) then -- If true, means database is deleted, or the plugin runs for the first time
LOG("[" .. Plugin:GetName() .. "] It looks like this is the first time running this plugin. Creating database...")
else
LOG("[" .. Plugin:GetName() .. "] Opening database");
end
db = sqlite3.open(PLUGIN:GetLocalFolder() .. cFile:GetPathSeparator() .. config.dbname);
ConfigureDatabase();
LOG("Initialised " .. Plugin:GetName() .. " v." .. Plugin:GetVersion())
return true
end
--
function OnDisable() -- Gets called when the plugin is unloaded, mostly when shutting down the server
LOG("[" .. PLUGIN:GetName() .. "] Disabling " .. PLUGIN:GetName() .. " v" .. PLUGIN:GetVersion());
db:close();
end
-- No monster
function OnSpawningMonster()
return true
end
--
function WorldStarted(World)
y = GROUND_LEVEL
-- just enough to fit one container
-- then it should be dynamic
for x = GROUND_MIN_X, GROUND_MAX_X
do
for z = GROUND_MIN_Z,GROUND_MAX_Z
do
setBlock(UpdateQueue, x, y, z, E_BLOCK_WOOL, E_META_WOOL_WHITE)
end
end
end
--
function PlayerJoined(Player)
-- enable flying
Player:SetCanFly(true)
-- refresh containers
LOG("player joined")
end
--
function PlayerUsingBlock(Player, BlockX, BlockY, BlockZ, BlockFace, CursorX, CursorY, CursorZ, BlockType, BlockMeta)
LOG("Using block: " .. tostring(BlockX) .. "," .. tostring(BlockY) .. "," .. tostring(BlockZ) .. " - " .. tostring(BlockType) .. " - " .. tostring(BlockMeta))
CURRENTSERVERBLOCKX = BlockX;
CURRENTSERVERBLOCKY = BlockY;
CURRENTSERVERBLOCKZ = BlockZ;
-- lever: 1->OFF 9->ON (in that orientation)
-- lever
if BlockType == 69
then
Player:SendMessage("Gandi CLI:")
Player:SendMessage("Using block: " .. tostring(BlockX) .. "," .. tostring(BlockY) .. "," .. tostring(BlockZ) .. " - " .. tostring(BlockType) .. " - " .. tostring(BlockMeta))
-- Activation lever
if BlockMeta == 14
then
CANCREATESERVER = 1
Player:SendMessage("Server can be create : " .. CANCREATESERVER)
LOG("Server can be create : " .. CANCREATESERVER)
end
-- Desactivation lever
if BlockMeta == 6
then
CANCREATESERVER = 0
Player:SendMessage("Server can not be create : " .. CANCREATESERVER)
LOG("Server can not be create : " .. CANCREATESERVER)
end
end
if BlockType == 77
then
Player:SendMessage("Gandi CLI:")
Player:SendMessage("Using block: " .. tostring(BlockX) .. "," .. tostring(BlockY) .. "," .. tostring(BlockZ) .. " - " .. tostring(BlockType) .. " - " .. tostring(BlockMeta))
end
end
--
function StructureCommand(Split, Player)
LOG("StructureCommand")
if table.getn(Split) > 0
then
LOG("Split[1]: " .. Split[1])
if Split[1] == "/structure"
then
Structure(World, Player, Split)
end
end
return true
end
--
function GandiCommand(Split, Player)
if table.getn(Split) > 0
then
LOG("Split[1]: " .. Split[1])
if Split[1] == "/gandi"
then
if table.getn(Split) > 1
then
-- Assemble a server
if Split[2] == "assemble"
then
-- /gandi assemble server
if Split[3] == "server"
then
EntireCommand = table.concat(Split, "_")
-- remove '/' at the beginning
command = string.sub(EntireCommand, 2, -1)
Player:SendMessage("Gandi CLI: " .. command)
AssembleServer(World, Player)
else
Player:SendMessage("Gandi CLI: assemble error command")
end
end
-- Disassemble a server
if Split[2] == "disassemble"
then
-- /gandi disassemble server
if Split[3] == "server"
then
EntireCommand = table.concat(Split, "_")
-- remove '/' at the beginning
command = string.sub(EntireCommand, 2, -1)
Player:SendMessage("Gandi CLI: " .. command)
DisassembleServer(World, Player)
else
Player:SendMessage("Gandi CLI: disassemble error command")
end
end
-- Create/Initialize a server
-- with a fake ip address
-- /gandi create <server name>
if Split[2] == "create"
then
command = "GandiCreateServer"
CreateServer(Split, Player)
-- Create the server via the Gandi API
-- Assign the "real life" ip address
if PRODUCTION == 1
then
Player:SendMessage("Execute Python script")
r = os.execute("python3 " .. PLUGIN:GetLocalFolder() .. "/gandiapi/" .. command .. ".py" .. " " .. Split[3] .. " &")
end
Player:SendMessage("Gandi CLI: " .. command)
-- LOG("executed: " .. command .. " -> " .. tostring(r))
end
-- Delete a server
-- /gandi delete <server name>
if Split[2] == "delete"
then
command = "GandiDeleteServer"
DeleteServer(Split, Player)
-- Delete the server via the Gandi API
if PRODUCTION == 1
then
Player:SendMessage("Execute Python script")
r = os.execute("python3 " .. PLUGIN:GetLocalFolder() .. "/gandiapi/" .. command .. ".py" .. " " .. Split[3] .. " &")
end
Player:SendMessage("Gandi CLI: " .. command)
LOG("executed: " .. command .. " -> " .. tostring(r))
end
-- (name, field, value)
-- /gandi update server01 ipv3 6.6.6.6
if Split[2] == "update"
then
Player:SendMessage("Gandi CLI: update")
UpdateServer(Split, Player)
end
if Split[2] == "get"
then
EntireCommand = table.concat(Split, "_")
-- remove '/' at the beginning
command = string.sub(EntireCommand, 2, -1)
GetServer(Split, Player)
Player:SendMessage("Gandi CLI: " .. command)
end
if Split[2] == "list"
then
EntireCommand = table.concat(Split, "_")
-- remove '/' at the beginning
command = string.sub(EntireCommand, 2, -1)
ListServer(Split, Player)
Player:SendMessage("Gandi CLI: " .. command)
end
end
end
end
return true
end
-- Generate flat world
function OnChunkGenerating(World, ChunkX, ChunkZ, ChunkDesc)
-- override the built-in chunk generator
-- to have it generate empty chunks only
ChunkDesc:SetUseDefaultBiomes(false)
ChunkDesc:SetUseDefaultComposition(false)
ChunkDesc:SetUseDefaultFinish(false)
ChunkDesc:SetUseDefaultHeight(false)
return true
end
-- Make it sunny all the time!
function OnWeatherChanging(World, Weather)
return true, wSunny
end
--
function OnPlayerFoodLevelChange(Player, NewFoodLevel)
-- Don't allow the player to get hungry
return true, Player, NewFoodLevel
end