Cuberite
A lightweight, fast and extensible game server for Minecraft
PluginLua.cpp
Go to the documentation of this file.
1 
2 // PluginLua.cpp
3 
4 // Implements the cPluginLua class representing a plugin written in Lua
5 
6 #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
7 
8 #ifdef __APPLE__
9  #define LUA_USE_MACOSX
10 #else
11  #define LUA_USE_POSIX
12 #endif
13 
14 #include "PluginLua.h"
15 #include "../CommandOutput.h"
16 #include "PluginManager.h"
17 #include "../Item.h"
18 #include "../Root.h"
19 #include "../WebAdmin.h"
20 
21 extern "C"
22 {
23  #include "lua/src/lauxlib.h"
24 }
25 
26 #undef TOLUA_TEMPLATE_BIND
27 #include "tolua++/include/tolua++.h"
28 
29 
30 
31 
32 
34 // cPluginLua:
35 
36 cPluginLua::cPluginLua(const AString & a_PluginDirectory, cDeadlockDetect & a_DeadlockDetect) :
37  cPlugin(a_PluginDirectory),
38  m_LuaState(Printf("plugin %s", a_PluginDirectory.c_str())),
39  m_DeadlockDetect(a_DeadlockDetect)
40 {
41  m_LuaState.TrackInDeadlockDetect(a_DeadlockDetect);
42 }
43 
44 
45 
46 
47 
49 {
50  Close();
52 }
53 
54 
55 
56 
57 
59 {
60  cOperation op(*this);
61  // If already closed, bail out:
62  if (!op().IsValid())
63  {
64  ASSERT(m_HookMap.empty());
65  return;
66  }
67 
68  // Remove the web tabs:
69  ClearWebTabs();
70 
71  // Release all the references in the hook map:
72  m_HookMap.clear();
73 
74  // Close the Lua engine:
75  op().Close();
76 }
77 
78 
79 
80 
81 
82 bool cPluginLua::Load(void)
83 {
84  cOperation op(*this);
85  if (!op().IsValid())
86  {
89 
90  // Inject the identification global variables into the state:
91  lua_pushlightuserdata(m_LuaState, this);
93  lua_pushstring(m_LuaState, GetName().c_str());
94  lua_setglobal(m_LuaState, LUA_PLUGIN_NAME_VAR_NAME);
95 
96  // Add the plugin's folder to the package.path and package.cpath variables (#693):
98  #ifdef _WIN32
99  m_LuaState.AddPackagePath("cpath", GetLocalFolder() + "\\?.dll");
100  #else
101  m_LuaState.AddPackagePath("cpath", FILE_IO_PREFIX + GetLocalFolder() + "/?.so");
102  #endif
103 
104  tolua_pushusertype(m_LuaState, this, "cPluginLua");
105  lua_setglobal(m_LuaState, "g_Plugin");
106  }
107 
108  std::string PluginPath = FILE_IO_PREFIX + GetLocalFolder() + "/";
109 
110  // List all Lua files for this plugin. Info.lua has a special handling - make it the last to load:
111  AStringVector Files = cFile::GetFolderContents(PluginPath.c_str());
112  AStringVector LuaFiles;
113  bool HasInfoLua = false;
114  for (AStringVector::const_iterator itr = Files.begin(), end = Files.end(); itr != end; ++itr)
115  {
116  size_t ExtensionStart = itr->find_last_of('.');
117  if ((ExtensionStart != std::string::npos) && (itr->substr(ExtensionStart) == ".lua"))
118  {
119  if (*itr == "Info.lua")
120  {
121  HasInfoLua = true;
122  }
123  else
124  {
125  LuaFiles.push_back(*itr);
126  }
127  }
128  }
129  std::sort(LuaFiles.begin(), LuaFiles.end());
130 
131  // Warn if there are no Lua files in the plugin folder:
132  if (LuaFiles.empty())
133  {
134  SetLoadError("No lua files found, plugin is probably missing.");
135  LOGWARNING("No lua files found: plugin %s is missing.", GetName().c_str());
136  Close();
137  return false;
138  }
139 
140  // Load all files in the list, including the Info.lua as last, if it exists:
141  for (AStringVector::const_iterator itr = LuaFiles.begin(), end = LuaFiles.end(); itr != end; ++itr)
142  {
143  AString Path = PluginPath + *itr;
144  if (!m_LuaState.LoadFile(Path))
145  {
146  SetLoadError(Printf("Failed to load file %s.", itr->c_str()));
147  Close();
148  return false;
149  }
150  } // for itr - Files[]
151  if (HasInfoLua)
152  {
153  AString Path = PluginPath + "Info.lua";
154  if (!m_LuaState.LoadFile(Path))
155  {
156  SetLoadError("Failed to load file Info.lua.");
157  Close();
158  return false;
159  }
160  }
161 
162  // Call the Initialize function:
163  bool res = false;
164  if (!m_LuaState.Call("Initialize", this, cLuaState::Return, res))
165  {
166  SetLoadError("Cannot call the Initialize() function.");
167  LOGWARNING("Error in plugin %s: Cannot call the Initialize() function. Plugin is temporarily disabled.", GetName().c_str());
168  Close();
169  return false;
170  }
171  if (!res)
172  {
173  SetLoadError("The Initialize() function failed.");
174  LOGINFO("Plugin %s: Initialize() call failed, plugin is temporarily disabled.", GetName().c_str());
175  Close();
176  return false;
177  }
178 
180  return true;
181 }
182 
183 
184 
185 
186 
188 {
189  ClearWebTabs();
190  super::Unload();
191  Close();
192 }
193 
194 
195 
196 
197 
199 {
200  cOperation op(*this);
201  if (!op().HasFunction("OnDisable"))
202  {
203  return;
204  }
205  op().Call("OnDisable");
206 }
207 
208 
209 
210 
211 
212 void cPluginLua::Tick(float a_Dt)
213 {
215 }
216 
217 
218 
219 
220 
221 bool cPluginLua::OnBlockSpread(cWorld & a_World, int a_BlockX, int a_BlockY, int a_BlockZ, eSpreadSource a_Source)
222 {
223  return CallSimpleHooks(cPluginManager::HOOK_BLOCK_SPREAD, &a_World, a_BlockX, a_BlockY, a_BlockZ, a_Source);
224 }
225 
226 
227 
228 
229 
231  cWorld & a_World,
232  Vector3i a_BlockPos,
233  BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta,
234  const cBlockEntity * a_BlockEntity,
235  const cEntity * a_Digger,
236  const cItem * a_Tool,
237  cItems & a_Pickups
238 )
239 {
240  // TODO: Change the hook signature to reflect the real parameters to this function, once we are allowed to make breaking API changes
241  return CallSimpleHooks(
243  &a_World,
244  a_Digger,
245  a_BlockPos.x, a_BlockPos.y, a_BlockPos.z,
246  a_BlockType, a_BlockMeta, &a_Pickups
247  );
248 }
249 
250 
251 
252 
253 
255 {
256  return CallSimpleHooks(cPluginManager::HOOK_BREWING_COMPLETED, &a_World, &a_Brewingstand);
257 }
258 
259 
260 
261 
262 
264 {
265  return CallSimpleHooks(cPluginManager::HOOK_BREWING_COMPLETING, &a_World, &a_Brewingstand);
266 }
267 
268 
269 
270 
271 
272 bool cPluginLua::OnChat(cPlayer & a_Player, AString & a_Message)
273 {
274  cOperation op(*this);
275  if (!op().IsValid())
276  {
277  return false;
278  }
279  bool res = false;
280  auto & hooks = m_HookMap[cPluginManager::HOOK_CHAT];
281  for (auto & hook: hooks)
282  {
283  hook->Call(&a_Player, a_Message, cLuaState::Return, res, a_Message);
284  if (res)
285  {
286  return true;
287  }
288  }
289  return false;
290 }
291 
292 
293 
294 
295 
296 bool cPluginLua::OnChunkAvailable(cWorld & a_World, int a_ChunkX, int a_ChunkZ)
297 {
298  return CallSimpleHooks(cPluginManager::HOOK_CHUNK_AVAILABLE, &a_World, a_ChunkX, a_ChunkZ);
299 }
300 
301 
302 
303 
304 
305 bool cPluginLua::OnChunkGenerated(cWorld & a_World, int a_ChunkX, int a_ChunkZ, cChunkDesc * a_ChunkDesc)
306 {
307  return CallSimpleHooks(cPluginManager::HOOK_CHUNK_GENERATED, &a_World, a_ChunkX, a_ChunkZ, a_ChunkDesc);
308 }
309 
310 
311 
312 
313 
314 bool cPluginLua::OnChunkGenerating(cWorld & a_World, int a_ChunkX, int a_ChunkZ, cChunkDesc * a_ChunkDesc)
315 {
316  return CallSimpleHooks(cPluginManager::HOOK_CHUNK_GENERATING, &a_World, a_ChunkX, a_ChunkZ, a_ChunkDesc);
317 }
318 
319 
320 
321 
322 
323 bool cPluginLua::OnChunkUnloaded(cWorld & a_World, int a_ChunkX, int a_ChunkZ)
324 {
325  return CallSimpleHooks(cPluginManager::HOOK_CHUNK_UNLOADED, &a_World, a_ChunkX, a_ChunkZ);
326 }
327 
328 
329 
330 
331 
332 bool cPluginLua::OnChunkUnloading(cWorld & a_World, int a_ChunkX, int a_ChunkZ)
333 {
334  return CallSimpleHooks(cPluginManager::HOOK_CHUNK_UNLOADING, &a_World, a_ChunkX, a_ChunkZ);
335 }
336 
337 
338 
339 
340 
341 bool cPluginLua::OnCollectingPickup(cPlayer & a_Player, cPickup & a_Pickup)
342 {
343  return CallSimpleHooks(cPluginManager::HOOK_COLLECTING_PICKUP, &a_Player, &a_Pickup);
344 }
345 
346 
347 
348 
349 
351 {
352  return CallSimpleHooks(cPluginManager::HOOK_CRAFTING_NO_RECIPE, &a_Player, &a_Grid, &a_Recipe);
353 }
354 
355 
356 
357 
358 
359 bool cPluginLua::OnDisconnect(cClientHandle & a_Client, const AString & a_Reason)
360 {
361  return CallSimpleHooks(cPluginManager::HOOK_DISCONNECT, &a_Client, a_Reason);
362 }
363 
364 
365 
366 
367 
368 bool cPluginLua::OnEntityAddEffect(cEntity & a_Entity, int a_EffectType, int a_EffectDurationTicks, int a_EffectIntensity, double a_DistanceModifier)
369 {
370  return CallSimpleHooks(cPluginManager::HOOK_ENTITY_ADD_EFFECT, &a_Entity, a_EffectType, a_EffectDurationTicks, a_EffectIntensity, a_DistanceModifier);
371 }
372 
373 
374 
375 
376 
378 {
379  return CallSimpleHooks(cPluginManager::HOOK_ENTITY_CHANGING_WORLD, &a_Entity, &a_World);
380 }
381 
382 
383 
384 
385 
387 {
388  return CallSimpleHooks(cPluginManager::HOOK_ENTITY_CHANGED_WORLD, &a_Entity, &a_World);
389 }
390 
391 
392 
393 
394 
395 bool cPluginLua::OnExecuteCommand(cPlayer * a_Player, const AStringVector & a_Split, const AString & a_EntireCommand, cPluginManager::CommandResult & a_Result)
396 {
397  cOperation op(*this);
398  if (!op().IsValid())
399  {
400  return false;
401  }
402  bool res = false;
404  for (auto & hook: hooks)
405  {
406  hook->Call(a_Player, a_Split, a_EntireCommand, cLuaState::Return, res, a_Result);
407  if (res)
408  {
409  return true;
410  }
411  }
412  return false;
413 }
414 
415 
416 
417 
418 
419 bool cPluginLua::OnExploded(cWorld & a_World, double a_ExplosionSize, bool a_CanCauseFire, double a_X, double a_Y, double a_Z, eExplosionSource a_Source, void * a_SourceData)
420 {
421  cOperation op(*this);
422  if (!op().IsValid())
423  {
424  return false;
425  }
426  bool res = false;
428  for (auto & hook: hooks)
429  {
430  switch (a_Source)
431  {
432  case esBed: hook->Call(&a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, static_cast<Vector3i *> (a_SourceData), cLuaState::Return, res); break;
433  case esEnderCrystal: hook->Call(&a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, static_cast<cEntity *> (a_SourceData), cLuaState::Return, res); break;
434  case esGhastFireball: hook->Call(&a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, static_cast<cGhastFireballEntity *>(a_SourceData), cLuaState::Return, res); break;
435  case esMonster: hook->Call(&a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, static_cast<cMonster *> (a_SourceData), cLuaState::Return, res); break;
436  case esOther: hook->Call(&a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, cLuaState::Return, res); break;
437  case esPlugin: hook->Call(&a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, cLuaState::Return, res); break;
438  case esPrimedTNT: hook->Call(&a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, static_cast<cTNTEntity *> (a_SourceData), cLuaState::Return, res); break;
439  case esWitherBirth: hook->Call(&a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, static_cast<cMonster *> (a_SourceData), cLuaState::Return, res); break;
440  case esWitherSkull: hook->Call(&a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, static_cast<cWitherSkullEntity *> (a_SourceData), cLuaState::Return, res); break;
441  case esMax:
442  {
443  ASSERT(!"Invalid explosion source");
444  return false;
445  }
446  }
447  if (res)
448  {
449  return true;
450  }
451  }
452  return false;
453 }
454 
455 
456 
457 
458 
459 bool cPluginLua::OnExploding(cWorld & a_World, double & a_ExplosionSize, bool & a_CanCauseFire, double a_X, double a_Y, double a_Z, eExplosionSource a_Source, void * a_SourceData)
460 {
461  cOperation op(*this);
462  if (!op().IsValid())
463  {
464  return false;
465  }
466  bool res = false;
468  for (auto & hook: hooks)
469  {
470  switch (a_Source)
471  {
472  case esBed: hook->Call(&a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, static_cast<Vector3i *> (a_SourceData), cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break;
473  case esEnderCrystal: hook->Call(&a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, static_cast<cEntity *> (a_SourceData), cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break;
474  case esGhastFireball: hook->Call(&a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, static_cast<cGhastFireballEntity *>(a_SourceData), cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break;
475  case esMonster: hook->Call(&a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, static_cast<cMonster *> (a_SourceData), cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break;
476  case esOther: hook->Call(&a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break;
477  case esPlugin: hook->Call(&a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break;
478  case esPrimedTNT: hook->Call(&a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, static_cast<cTNTEntity *> (a_SourceData), cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break;
479  case esWitherBirth: hook->Call(&a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, static_cast<cMonster *> (a_SourceData), cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break;
480  case esWitherSkull: hook->Call(&a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, static_cast<cWitherSkullEntity *> (a_SourceData), cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break;
481  case esMax:
482  {
483  ASSERT(!"Invalid explosion source");
484  return false;
485  }
486  }
487  if (res)
488  {
489  return true;
490  }
491  }
492  return false;
493 }
494 
495 
496 
497 
498 
499 bool cPluginLua::OnHandshake(cClientHandle & a_Client, const AString & a_Username)
500 {
501  return CallSimpleHooks(cPluginManager::HOOK_HANDSHAKE, &a_Client, a_Username);
502 }
503 
504 
505 
506 
507 
508 bool cPluginLua::OnHopperPullingItem(cWorld & a_World, cHopperEntity & a_Hopper, int a_DstSlotNum, cBlockEntityWithItems & a_SrcEntity, int a_SrcSlotNum)
509 {
510  return CallSimpleHooks(cPluginManager::HOOK_HOPPER_PULLING_ITEM, &a_World, &a_Hopper, a_DstSlotNum, &a_SrcEntity, a_SrcSlotNum);
511 }
512 
513 
514 
515 
516 
517 bool cPluginLua::OnHopperPushingItem(cWorld & a_World, cHopperEntity & a_Hopper, int a_SrcSlotNum, cBlockEntityWithItems & a_DstEntity, int a_DstSlotNum)
518 {
519  return CallSimpleHooks(cPluginManager::HOOK_HOPPER_PUSHING_ITEM, &a_World, &a_Hopper, a_SrcSlotNum, &a_DstEntity, a_DstSlotNum);
520 }
521 
522 
523 
524 
525 
526 bool cPluginLua::OnKilled(cEntity & a_Victim, TakeDamageInfo & a_TDI, AString & a_DeathMessage)
527 {
528  cOperation op(*this);
529  if (!op().IsValid())
530  {
531  return false;
532  }
533  bool res = false;
534  auto & hooks = m_HookMap[cPluginManager::HOOK_KILLED];
535  for (auto & hook: hooks)
536  {
537  hook->Call(&a_Victim, &a_TDI, a_DeathMessage, cLuaState::Return, res, a_DeathMessage);
538  if (res)
539  {
540  return true;
541  }
542  }
543  return false;
544 }
545 
546 
547 
548 
549 
550 bool cPluginLua::OnKilling(cEntity & a_Victim, cEntity * a_Killer, TakeDamageInfo & a_TDI)
551 {
552  return CallSimpleHooks(cPluginManager::HOOK_KILLING, &a_Victim, a_Killer, &a_TDI);
553 }
554 
555 
556 
557 
558 
559 bool cPluginLua::OnLogin(cClientHandle & a_Client, UInt32 a_ProtocolVersion, const AString & a_Username)
560 {
561  return CallSimpleHooks(cPluginManager::HOOK_LOGIN, &a_Client, a_ProtocolVersion, a_Username);
562 }
563 
564 
565 
566 
567 
568 bool cPluginLua::OnLoginForge(cClientHandle & a_Client, const AStringMap & a_Mods)
569 {
570  return CallSimpleHooks(cPluginManager::HOOK_LOGIN_FORGE, &a_Client, a_Mods);
571 }
572 
573 
574 
575 
576 
577 bool cPluginLua::OnPlayerAnimation(cPlayer & a_Player, int a_Animation)
578 {
579  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_ANIMATION, &a_Player, a_Animation);
580 }
581 
582 
583 
584 
585 
586 bool cPluginLua::OnPlayerBreakingBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
587 {
588  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_BREAKING_BLOCK, &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_BlockType, a_BlockMeta);
589 }
590 
591 
592 
593 
594 
595 bool cPluginLua::OnPlayerBrokenBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
596 {
597  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_BROKEN_BLOCK, &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_BlockType, a_BlockMeta);
598 }
599 
600 
601 
602 
603 
605 {
607 }
608 
609 
610 
611 
612 
614 {
616 }
617 
618 
619 
620 
621 
622 bool cPluginLua::OnPlayerFoodLevelChange(cPlayer & a_Player, int a_NewFoodLevel)
623 {
624  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_FOOD_LEVEL_CHANGE, &a_Player, a_NewFoodLevel);
625 }
626 
627 
628 
629 
630 
631 bool cPluginLua::OnPlayerFished(cPlayer & a_Player, const cItems & a_Reward)
632 {
633  cItems reward(a_Reward);
634  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_FISHED, &a_Player, &reward);
635 }
636 
637 
638 
639 
640 
641 bool cPluginLua::OnPlayerFishing(cPlayer & a_Player, cItems & a_Reward)
642 {
643  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_FISHING, &a_Player, &a_Reward);
644 }
645 
646 
647 
648 
649 
651 {
653 }
654 
655 
656 
657 
658 
659 bool cPluginLua::OnPlayerLeftClick(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status)
660 {
661  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_LEFT_CLICK, &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_Status);
662 }
663 
664 
665 
666 
667 
668 bool cPluginLua::OnPlayerMoving(cPlayer & a_Player, const Vector3d & a_OldPosition, const Vector3d & a_NewPosition)
669 {
670  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_MOVING, &a_Player, a_OldPosition, a_NewPosition);
671 }
672 
673 
674 
675 
676 
677 bool cPluginLua::OnEntityTeleport(cEntity & a_Entity, const Vector3d & a_OldPosition, const Vector3d & a_NewPosition)
678 {
679  return CallSimpleHooks(cPluginManager::HOOK_ENTITY_TELEPORT, &a_Entity, a_OldPosition, a_NewPosition);
680 }
681 
682 
683 
684 
685 
687 {
688  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_OPENING_WINDOW, &a_Player, &a_Window);
689 }
690 
691 
692 
693 
694 
695 bool cPluginLua::OnPlayerPlacedBlock(cPlayer & a_Player, const sSetBlock & a_BlockChange)
696 {
698  &a_Player,
699  a_BlockChange.GetX(), a_BlockChange.GetY(), a_BlockChange.GetZ(),
700  a_BlockChange.m_BlockType, a_BlockChange.m_BlockMeta
701  );
702 }
703 
704 
705 
706 
707 
708 bool cPluginLua::OnPlayerPlacingBlock(cPlayer & a_Player, const sSetBlock & a_BlockChange)
709 {
711  &a_Player,
712  a_BlockChange.GetX(), a_BlockChange.GetY(), a_BlockChange.GetZ(),
713  a_BlockChange.m_BlockType, a_BlockChange.m_BlockMeta
714  );
715 }
716 
717 
718 
719 
720 
721 bool cPluginLua::OnPlayerRightClick(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ)
722 {
723  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_RIGHT_CLICK, &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ);
724 }
725 
726 
727 
728 
729 
731 {
733 }
734 
735 
736 
737 
738 
740 {
742 }
743 
744 
745 
746 
747 
749 {
751 }
752 
753 
754 
755 
756 
758 {
760 }
761 
762 
763 
764 
765 
766 bool cPluginLua::OnPlayerUsedBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
767 {
768  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_USED_BLOCK, &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, a_BlockType, a_BlockMeta);
769 }
770 
771 
772 
773 
774 
775 bool cPluginLua::OnPlayerUsedItem(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ)
776 {
777  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_USED_ITEM, &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ);
778 }
779 
780 
781 
782 
783 
784 bool cPluginLua::OnPlayerUsingBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
785 {
786  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_USING_BLOCK, &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, a_BlockType, a_BlockMeta);
787 }
788 
789 
790 
791 
792 
793 bool cPluginLua::OnPlayerUsingItem(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ)
794 {
795  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_USING_ITEM, &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ);
796 }
797 
798 
799 
800 
801 
802 bool cPluginLua::OnPluginMessage(cClientHandle & a_Client, const AString & a_Channel, const AString & a_Message)
803 {
804  return CallSimpleHooks(cPluginManager::HOOK_PLUGIN_MESSAGE, &a_Client, a_Channel, a_Message);
805 }
806 
807 
808 
809 
810 
812 {
813  cOperation op(*this);
814  if (!op().IsValid())
815  {
816  return false;
817  }
818  bool res = false;
820  for (auto & hook: hooks)
821  {
822  bool ret = false;
823  hook->Call(cLuaState::Return, ret);
824  res = res || ret;
825  }
826  return res;
827 }
828 
829 
830 
831 
832 
833 bool cPluginLua::OnPostCrafting(cPlayer & a_Player, cCraftingGrid & a_Grid, cCraftingRecipe & a_Recipe)
834 {
835  return CallSimpleHooks(cPluginManager::HOOK_POST_CRAFTING, &a_Player, &a_Grid, &a_Recipe);
836 }
837 
838 
839 
840 
841 
842 bool cPluginLua::OnPreCrafting(cPlayer & a_Player, cCraftingGrid & a_Grid, cCraftingRecipe & a_Recipe)
843 {
844  return CallSimpleHooks(cPluginManager::HOOK_PRE_CRAFTING, &a_Player, &a_Grid, &a_Recipe);
845 }
846 
847 
848 
849 
850 
851 bool cPluginLua::OnProjectileHitBlock(cProjectileEntity & a_Projectile, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_Face, const Vector3d & a_BlockHitPos)
852 {
853  return CallSimpleHooks(cPluginManager::HOOK_PROJECTILE_HIT_BLOCK, &a_Projectile, a_BlockX, a_BlockY, a_BlockZ, a_Face, a_BlockHitPos);
854 }
855 
856 
857 
858 
859 
861 {
862  return CallSimpleHooks(cPluginManager::HOOK_PROJECTILE_HIT_ENTITY, &a_Projectile, &a_HitEntity);
863 }
864 
865 
866 
867 
868 
869 bool cPluginLua::OnServerPing(cClientHandle & a_ClientHandle, AString & a_ServerDescription, int & a_OnlinePlayersCount, int & a_MaxPlayersCount, AString & a_Favicon)
870 {
871  cOperation op(*this);
872  if (!op().IsValid())
873  {
874  return false;
875  }
876  bool res = false;
878  for (auto & hook: hooks)
879  {
880  hook->Call(&a_ClientHandle, a_ServerDescription, a_OnlinePlayersCount, a_MaxPlayersCount, a_Favicon, cLuaState::Return, res, a_ServerDescription, a_OnlinePlayersCount, a_MaxPlayersCount, a_Favicon);
881  if (res)
882  {
883  return true;
884  }
885  }
886  return false;
887 }
888 
889 
890 
891 
892 
893 bool cPluginLua::OnSpawnedEntity(cWorld & a_World, cEntity & a_Entity)
894 {
895  return CallSimpleHooks(cPluginManager::HOOK_SPAWNED_ENTITY, &a_World, &a_Entity);
896 }
897 
898 
899 
900 
901 
902 bool cPluginLua::OnSpawnedMonster(cWorld & a_World, cMonster & a_Monster)
903 {
904  return CallSimpleHooks(cPluginManager::HOOK_SPAWNED_MONSTER, &a_World, &a_Monster);
905 }
906 
907 
908 
909 
910 
911 bool cPluginLua::OnSpawningEntity(cWorld & a_World, cEntity & a_Entity)
912 {
913  return CallSimpleHooks(cPluginManager::HOOK_SPAWNING_ENTITY, &a_World, &a_Entity);
914 }
915 
916 
917 
918 
919 
920 bool cPluginLua::OnSpawningMonster(cWorld & a_World, cMonster & a_Monster)
921 {
922  return CallSimpleHooks(cPluginManager::HOOK_SPAWNING_MONSTER, &a_World, &a_Monster);
923 }
924 
925 
926 
927 
928 
930 {
931  return CallSimpleHooks(cPluginManager::HOOK_TAKE_DAMAGE, &a_Receiver, &a_TDI);
932 }
933 
934 
935 
936 
937 
939  cWorld & a_World,
940  int a_BlockX, int a_BlockY, int a_BlockZ,
941  const AString & a_Line1, const AString & a_Line2, const AString & a_Line3, const AString & a_Line4,
942  cPlayer * a_Player
943 )
944 {
945  return CallSimpleHooks(cPluginManager::HOOK_UPDATED_SIGN, &a_World, a_BlockX, a_BlockY, a_BlockZ, a_Line1, a_Line2, a_Line3, a_Line4, a_Player);
946 }
947 
948 
949 
950 
951 
953  cWorld & a_World,
954  int a_BlockX, int a_BlockY, int a_BlockZ,
955  AString & a_Line1, AString & a_Line2, AString & a_Line3, AString & a_Line4,
956  cPlayer * a_Player
957 )
958 {
959  cOperation op(*this);
960  if (!op().IsValid())
961  {
962  return false;
963  }
964  bool res = false;
966  for (auto & hook: hooks)
967  {
968  hook->Call(&a_World, a_BlockX, a_BlockY, a_BlockZ, a_Line1, a_Line2, a_Line3, a_Line4, a_Player, cLuaState::Return, res, a_Line1, a_Line2, a_Line3, a_Line4);
969  if (res)
970  {
971  return true;
972  }
973  }
974  return false;
975 }
976 
977 
978 
979 
980 
982 {
984 }
985 
986 
987 
988 
989 
990 bool cPluginLua::OnWeatherChanging(cWorld & a_World, eWeather & a_NewWeather)
991 {
992  cOperation op(*this);
993  if (!op().IsValid())
994  {
995  return false;
996  }
997  bool res = false;
999  for (auto & hook: hooks)
1000  {
1001  hook->Call(&a_World, a_NewWeather, cLuaState::Return, res, a_NewWeather);
1002  if (res)
1003  {
1004  return true;
1005  }
1006  }
1007  return false;
1008 }
1009 
1010 
1011 
1012 
1013 
1015 {
1017 }
1018 
1019 
1020 
1021 
1022 
1023 bool cPluginLua::OnWorldTick(cWorld & a_World, std::chrono::milliseconds a_Dt, std::chrono::milliseconds a_LastTickDurationMSec)
1024 {
1025  return CallSimpleHooks(cPluginManager::HOOK_WORLD_TICK, &a_World, a_Dt, a_LastTickDurationMSec);
1026 }
1027 
1028 
1029 
1030 
1031 
1033 {
1034  const char * FnName = GetHookFnName(a_HookType);
1035  if (FnName == nullptr)
1036  {
1037  // Unknown hook ID
1038  LOGWARNING("Plugin %s wants to add an unknown hook ID (%d). The plugin need not work properly.",
1039  GetName().c_str(), a_HookType
1040  );
1042  return false;
1043  }
1044 
1045  // Check if the function is available
1046  if (m_LuaState.HasFunction(FnName))
1047  {
1048  return true;
1049  }
1050 
1051  LOGWARNING("Plugin %s wants to add a hook (%d), but it doesn't provide the callback function \"%s\" for it. The plugin need not work properly.",
1052  GetName().c_str(), a_HookType, FnName
1053  );
1055  return false;
1056 }
1057 
1058 
1059 
1060 
1061 
1062 const char * cPluginLua::GetHookFnName(int a_HookType)
1063 {
1064  switch (a_HookType)
1065  {
1066  case cPluginManager::HOOK_BLOCK_SPREAD: return "OnBlockSpread";
1067  case cPluginManager::HOOK_BLOCK_TO_PICKUPS: return "OnBlockToPickups";
1068  case cPluginManager::HOOK_CHAT: return "OnChat";
1069  case cPluginManager::HOOK_CHUNK_AVAILABLE: return "OnChunkAvailable";
1070  case cPluginManager::HOOK_CHUNK_GENERATED: return "OnChunkGenerated";
1071  case cPluginManager::HOOK_CHUNK_GENERATING: return "OnChunkGenerating";
1072  case cPluginManager::HOOK_CHUNK_UNLOADED: return "OnChunkUnloaded";
1073  case cPluginManager::HOOK_CHUNK_UNLOADING: return "OnChunkUnloading";
1074  case cPluginManager::HOOK_COLLECTING_PICKUP: return "OnCollectingPickup";
1075  case cPluginManager::HOOK_CRAFTING_NO_RECIPE: return "OnCraftingNoRecipe";
1076  case cPluginManager::HOOK_DISCONNECT: return "OnDisconnect";
1077  case cPluginManager::HOOK_PLAYER_ANIMATION: return "OnPlayerAnimation";
1078  case cPluginManager::HOOK_ENTITY_ADD_EFFECT: return "OnEntityAddEffect";
1079  case cPluginManager::HOOK_ENTITY_CHANGING_WORLD: return "OnEntityChangingWorld";
1080  case cPluginManager::HOOK_ENTITY_CHANGED_WORLD: return "OnEntityChangedWorld";
1081  case cPluginManager::HOOK_ENTITY_TELEPORT: return "OnEntityTeleport";
1082  case cPluginManager::HOOK_EXECUTE_COMMAND: return "OnExecuteCommand";
1083  case cPluginManager::HOOK_HANDSHAKE: return "OnHandshake";
1084  case cPluginManager::HOOK_KILLING: return "OnKilling";
1085  case cPluginManager::HOOK_LOGIN: return "OnLogin";
1086  case cPluginManager::HOOK_LOGIN_FORGE: return "OnLoginForge";
1087  case cPluginManager::HOOK_PLAYER_BREAKING_BLOCK: return "OnPlayerBreakingBlock";
1088  case cPluginManager::HOOK_PLAYER_BROKEN_BLOCK: return "OnPlayerBrokenBlock";
1089  case cPluginManager::HOOK_PLAYER_EATING: return "OnPlayerEating";
1090  case cPluginManager::HOOK_PLAYER_JOINED: return "OnPlayerJoined";
1091  case cPluginManager::HOOK_PLAYER_LEFT_CLICK: return "OnPlayerLeftClick";
1092  case cPluginManager::HOOK_PLAYER_MOVING: return "OnPlayerMoving";
1093  case cPluginManager::HOOK_PLAYER_OPENING_WINDOW: return "OnPlayerOpeningWindow";
1094  case cPluginManager::HOOK_PLAYER_PLACED_BLOCK: return "OnPlayerPlacedBlock";
1095  case cPluginManager::HOOK_PLAYER_PLACING_BLOCK: return "OnPlayerPlacingBlock";
1096  case cPluginManager::HOOK_PLAYER_RIGHT_CLICK: return "OnPlayerRightClick";
1097  case cPluginManager::HOOK_PLAYER_RIGHT_CLICKING_ENTITY: return "OnPlayerRightClickingEntity";
1098  case cPluginManager::HOOK_PLAYER_SHOOTING: return "OnPlayerShooting";
1099  case cPluginManager::HOOK_PLAYER_SPAWNED: return "OnPlayerSpawned";
1100  case cPluginManager::HOOK_PLAYER_TOSSING_ITEM: return "OnPlayerTossingItem";
1101  case cPluginManager::HOOK_PLAYER_USED_BLOCK: return "OnPlayerUsedBlock";
1102  case cPluginManager::HOOK_PLAYER_USED_ITEM: return "OnPlayerUsedItem";
1103  case cPluginManager::HOOK_PLAYER_USING_BLOCK: return "OnPlayerUsingBlock";
1104  case cPluginManager::HOOK_PLAYER_USING_ITEM: return "OnPlayerUsingItem";
1105  case cPluginManager::HOOK_PLUGIN_MESSAGE: return "OnPluginMessage";
1106  case cPluginManager::HOOK_PLUGINS_LOADED: return "OnPluginsLoaded";
1107  case cPluginManager::HOOK_POST_CRAFTING: return "OnPostCrafting";
1108  case cPluginManager::HOOK_PRE_CRAFTING: return "OnPreCrafting";
1109  case cPluginManager::HOOK_SERVER_PING: return "OnServerPing";
1110  case cPluginManager::HOOK_SPAWNED_ENTITY: return "OnSpawnedEntity";
1111  case cPluginManager::HOOK_SPAWNED_MONSTER: return "OnSpawnedMonster";
1112  case cPluginManager::HOOK_SPAWNING_ENTITY: return "OnSpawningEntity";
1113  case cPluginManager::HOOK_SPAWNING_MONSTER: return "OnSpawningMonster";
1114  case cPluginManager::HOOK_TAKE_DAMAGE: return "OnTakeDamage";
1115  case cPluginManager::HOOK_TICK: return "OnTick";
1116  case cPluginManager::HOOK_UPDATED_SIGN: return "OnUpdatedSign";
1117  case cPluginManager::HOOK_UPDATING_SIGN: return "OnUpdatingSign";
1118  case cPluginManager::HOOK_WEATHER_CHANGED: return "OnWeatherChanged";
1119  case cPluginManager::HOOK_WEATHER_CHANGING: return "OnWeatherChanging";
1120  case cPluginManager::HOOK_WORLD_TICK: return "OnWorldTick";
1121 
1123  {
1124  // Satisfy a warning that all enum values should be used in a switch
1125  // but don't want a default branch, so that we catch new hooks missing from this list.
1126  break;
1127  }
1128  } // switch (a_Hook)
1129  LOGWARNING("Requested name of an unknown hook type function: %d (max is %d)", a_HookType, cPluginManager::HOOK_MAX);
1130  ASSERT(!"Unknown hook requested!");
1131  return nullptr;
1132 }
1133 
1134 
1135 
1136 
1137 
1138 bool cPluginLua::AddHookCallback(int a_HookType, cLuaState::cCallbackPtr && a_Callback)
1139 {
1140  m_HookMap[a_HookType].push_back(std::move(a_Callback));
1141  return true;
1142 }
1143 
1144 
1145 
1146 
1147 
1149  const AString & a_FunctionName,
1150  cLuaState & a_ForeignState,
1151  int a_ParamStart,
1152  int a_ParamEnd
1153 )
1154 {
1155  cOperation op(*this);
1156 
1157  // Call the function:
1158  int NumReturns = op().CallFunctionWithForeignParams(a_FunctionName, a_ForeignState, a_ParamStart, a_ParamEnd);
1159  if (NumReturns < 0)
1160  {
1161  // The call has failed, an error has already been output to the log, so just silently bail out with the same error
1162  return NumReturns;
1163  }
1164 
1165  // Copy all the return values:
1166  int Top = lua_gettop(m_LuaState);
1167  int res = a_ForeignState.CopyStackFrom(m_LuaState, Top - NumReturns + 1, Top);
1168 
1169  // Remove the return values off this stack:
1170  if (NumReturns > 0)
1171  {
1172  lua_pop(m_LuaState, NumReturns);
1173  }
1174 
1175  return res;
1176 }
1177 
1178 
1179 
1180 
1181 
1183 {
1184  auto webAdmin = cRoot::Get()->GetWebAdmin();
1185  if (webAdmin != nullptr) // can be nullptr when shutting down the server
1186  {
1187  webAdmin->RemoveAllPluginWebTabs(m_Name);
1188  }
1189 }
1190 
1191 
1192 
1193 
virtual bool OnPlayerPlacedBlock(cPlayer &a_Player, const sSetBlock &a_BlockChange) override
Definition: PluginLua.cpp:695
bool CallSimpleHooks(int a_HookType, Args &&...a_Args)
Calls a hook that has the simple format - single bool return value specifying whether the chain shoul...
Definition: PluginLua.h:192
virtual bool OnBrewingCompleted(cWorld &a_World, cBrewingstandEntity &a_BrewingstandEntity) override
Definition: PluginLua.cpp:254
virtual bool OnWeatherChanging(cWorld &a_World, eWeather &a_NewWeather) override
Definition: PluginLua.cpp:990
T x
Definition: Vector3.h:17
eWeather
Definition: Defines.h:151
virtual bool OnChunkUnloaded(cWorld &a_World, int a_ChunkX, int a_ChunkZ) override
Definition: PluginLua.cpp:323
int CallFunctionFromForeignState(const AString &a_FunctionName, cLuaState &a_ForeignState, int a_ParamStart, int a_ParamEnd)
Calls a function in this plugin&#39;s LuaState with parameters copied over from a_ForeignState.
Definition: PluginLua.cpp:1148
void Close(void)
Releases all Lua references, notifies and removes all m_Resettables[] and closes the m_LuaState...
Definition: PluginLua.cpp:58
virtual void OnDisable(void) override
Called as the last call into the plugin before it is unloaded.
Definition: PluginLua.cpp:198
virtual bool OnChunkAvailable(cWorld &a_World, int a_ChunkX, int a_ChunkZ) override
Definition: PluginLua.cpp:296
virtual bool OnWorldStarted(cWorld &a_World) override
Definition: PluginLua.cpp:1014
virtual bool OnProjectileHitBlock(cProjectileEntity &a_Projectile, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_Face, const Vector3d &a_BlockHitPos) override
Definition: PluginLua.cpp:851
bool Call(const FnT &a_Function, Args &&...args)
Call the specified Lua function.
Definition: LuaState.h:744
virtual bool OnPlayerOpeningWindow(cPlayer &a_Player, cWindow &a_Window) override
Definition: PluginLua.cpp:686
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:42
void ClearWebTabs(void)
Removes all WebTabs currently registered for this plugin from the WebAdmin.
Definition: PluginLua.cpp:1182
int GetY(void) const
Returns the absolute Y coord of the stored block.
Definition: ChunkDef.h:554
virtual bool OnEntityChangedWorld(cEntity &a_Entity, cWorld &a_World) override
Definition: PluginLua.cpp:386
virtual ~cPluginLua() override
Definition: PluginLua.cpp:48
#define FILE_IO_PREFIX
Definition: Globals.h:196
virtual bool OnPlayerFished(cPlayer &a_Player, const cItems &a_Reward) override
Definition: PluginLua.cpp:631
Definition: Player.h:27
cDeadlockDetect & m_DeadlockDetect
The deadlock detect in which all plugins should track their CSs.
virtual bool OnKilling(cEntity &a_Victim, cEntity *a_Killer, TakeDamageInfo &a_TDI) override
Definition: PluginLua.cpp:550
virtual bool OnEntityAddEffect(cEntity &a_Entity, int a_EffectType, int a_EffectDurationTicks, int a_EffectIntensity, double a_DistanceModifier) override
Definition: PluginLua.cpp:368
virtual bool OnSpawnedEntity(cWorld &a_World, cEntity &a_Entity) override
Definition: PluginLua.cpp:893
void LogStackTrace(int a_StartingDepth=0)
Logs all items in the current stack trace to the server console.
Definition: LuaState.cpp:1980
virtual bool OnPlayerRightClickingEntity(cPlayer &a_Player, cEntity &a_Entity) override
Definition: PluginLua.cpp:730
virtual bool OnBlockSpread(cWorld &a_World, int a_BlockX, int a_BlockY, int a_BlockZ, eSpreadSource a_Source) override
Calls the specified hook with the params given.
Definition: PluginLua.cpp:221
virtual bool OnWeatherChanged(cWorld &a_World) override
Definition: PluginLua.cpp:981
virtual bool OnPlayerShooting(cPlayer &a_Player) override
Definition: PluginLua.cpp:739
cDeadlockDetect & m_DeadlockDetect
The DeadlockDetect object to which the plugin&#39;s CS is tracked.
Definition: PluginLua.h:179
virtual bool OnPlayerTossingItem(cPlayer &a_Player) override
Definition: PluginLua.cpp:757
bool AddHookCallback(int a_HookType, cLuaState::cCallbackPtr &&a_Callback)
Adds a Lua callback to be called for the specified hook.
Definition: PluginLua.cpp:1138
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
virtual bool OnPluginMessage(cClientHandle &a_Client, const AString &a_Channel, const AString &a_Message) override
Definition: PluginLua.cpp:802
Definition: Pickup.h:18
Encapsulates a Lua state and provides some syntactic sugar for common operations. ...
Definition: LuaState.h:57
virtual bool OnCraftingNoRecipe(cPlayer &a_Player, cCraftingGrid &a_Grid, cCraftingRecipe &a_Recipe) override
Definition: PluginLua.cpp:350
int GetZ(void) const
Returns the absolute Z coord of the stored block.
Definition: ChunkDef.h:557
A RAII-style mutex lock for accessing the internal LuaState.
Definition: PluginLua.h:39
virtual bool OnServerPing(cClientHandle &a_ClientHandle, AString &a_ServerDescription, int &a_OnlinePlayersCount, int &a_MaxPlayersCount, AString &a_Favicon) override
Definition: PluginLua.cpp:869
T y
Definition: Vector3.h:17
AString GetLocalFolder(void) const
Returns the folder relative to the MCS Executable, from which the plugin is loaded.
Definition: Plugin.cpp:46
virtual bool OnPlayerLeftClick(cPlayer &a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status) override
Definition: PluginLua.cpp:659
virtual bool OnLogin(cClientHandle &a_Client, UInt32 a_ProtocolVersion, const AString &a_Username) override
Definition: PluginLua.cpp:559
T z
Definition: Vector3.h:17
virtual bool OnUpdatedSign(cWorld &a_World, int a_BlockX, int a_BlockY, int a_BlockZ, const AString &a_Line1, const AString &a_Line2, const AString &a_Line3, const AString &a_Line4, cPlayer *a_Player) override
Definition: PluginLua.cpp:938
virtual bool OnExploded(cWorld &a_World, double a_ExplosionSize, bool a_CanCauseFire, double a_X, double a_Y, double a_Z, eExplosionSource a_Source, void *a_SourceData) override
Definition: PluginLua.cpp:419
cLuaState m_LuaState
The plugin&#39;s Lua state.
Definition: PluginLua.h:173
virtual bool OnChunkGenerating(cWorld &a_World, int a_ChunkX, int a_ChunkZ, cChunkDesc *a_ChunkDesc) override
Definition: PluginLua.cpp:314
BLOCKTYPE m_BlockType
Definition: ChunkDef.h:521
virtual bool OnExploding(cWorld &a_World, double &a_ExplosionSize, bool &a_CanCauseFire, double a_X, double a_Y, double a_Z, eExplosionSource a_Source, void *a_SourceData) override
Definition: PluginLua.cpp:459
cHookMap m_HookMap
Hooks that the plugin has registered.
Definition: PluginLua.h:176
#define LUA_PLUGIN_NAME_VAR_NAME
Definition: PluginLua.h:16
virtual void Unload(void)
Unloads the plugin.
Definition: Plugin.cpp:31
std::vector< AString > AStringVector
Definition: StringUtils.h:14
virtual bool OnPlayerJoined(cPlayer &a_Player) override
Definition: PluginLua.cpp:650
int GetX(void) const
Returns the absolute X coord of the stored block.
Definition: ChunkDef.h:550
virtual bool OnUpdatingSign(cWorld &a_World, int a_BlockX, int a_BlockY, int a_BlockZ, AString &a_Line1, AString &a_Line2, AString &a_Line3, AString &a_Line4, cPlayer *a_Player) override
Definition: PluginLua.cpp:952
cPluginManager::ePluginStatus m_Status
Definition: Plugin.h:142
void SetLoadError(const AString &a_LoadError)
Sets m_LoadError to the specified string and m_Status to psError.
Definition: Plugin.cpp:55
virtual bool OnHopperPullingItem(cWorld &a_World, cHopperEntity &a_Hopper, int a_DstSlotNum, cBlockEntityWithItems &a_SrcEntity, int a_SrcSlotNum) override
Definition: PluginLua.cpp:508
virtual bool OnKilled(cEntity &a_Victim, TakeDamageInfo &a_TDI, AString &a_DeathMessage) override
Definition: PluginLua.cpp:526
Definition: World.h:65
virtual bool OnPlayerUsingBlock(cPlayer &a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override
Definition: PluginLua.cpp:784
virtual bool Load(void) override
Loads and initializes the plugin.
Definition: PluginLua.cpp:82
virtual bool OnPreCrafting(cPlayer &a_Player, cCraftingGrid &a_Grid, cCraftingRecipe &a_Recipe) override
Definition: PluginLua.cpp:842
virtual bool OnCollectingPickup(cPlayer &a_Player, cPickup &a_Pickup) override
Definition: PluginLua.cpp:341
virtual bool OnPlayerEating(cPlayer &a_Player) override
Definition: PluginLua.cpp:613
std::unique_ptr< cCallback > cCallbackPtr
Definition: LuaState.h:328
void LOGINFO(const char *a_Format, fmt::ArgList a_ArgList)
Definition: Logger.cpp:165
AString & Printf(AString &str, const char *format, fmt::ArgList args)
Output the formatted text into the string.
Definition: StringUtils.cpp:55
virtual bool OnSpawnedMonster(cWorld &a_World, cMonster &a_Monster) override
Definition: PluginLua.cpp:902
#define ASSERT(x)
Definition: Globals.h:335
std::map< AString, AString > AStringMap
A string dictionary, used for key-value pairs.
Definition: StringUtils.h:18
void LOGWARNING(const char *a_Format, fmt::ArgList a_ArgList)
Definition: Logger.cpp:174
virtual bool OnPluginsLoaded(void) override
Definition: PluginLua.cpp:811
virtual bool OnBlockToPickups(cWorld &a_World, Vector3i a_BlockPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, const cBlockEntity *a_BlockEntity, const cEntity *a_Digger, const cItem *a_Tool, cItems &a_Pickups) override
Definition: PluginLua.cpp:230
virtual bool OnPlayerFoodLevelChange(cPlayer &a_Player, int a_NewFoodLevel) override
Definition: PluginLua.cpp:622
Definition: Plugin.h:19
virtual bool OnChunkGenerated(cWorld &a_World, int a_ChunkX, int a_ChunkZ, cChunkDesc *a_ChunkDesc) override
Definition: PluginLua.cpp:305
void TrackInDeadlockDetect(cDeadlockDetect &a_DeadlockDetect)
Adds this object&#39;s CS to the DeadlockDetect&#39;s tracked CSs.
Definition: LuaState.cpp:2334
virtual bool OnHopperPushingItem(cWorld &a_World, cHopperEntity &a_Hopper, int a_SrcSlotNum, cBlockEntityWithItems &a_DstEntity, int a_DstSlotNum) override
Definition: PluginLua.cpp:517
virtual bool OnPlayerUsingItem(cPlayer &a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override
Definition: PluginLua.cpp:793
static const cRet Return
Definition: LuaState.h:446
eExplosionSource
The source of an explosion.
Definition: BlockID.h:1202
cPluginLua(const AString &a_PluginDirectory, cDeadlockDetect &a_DeadlockDetect)
Definition: PluginLua.cpp:36
virtual bool OnProjectileHitEntity(cProjectileEntity &a_Projectile, cEntity &a_HitEntity) override
Definition: PluginLua.cpp:860
std::string AString
Definition: StringUtils.h:13
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc...
Definition: Defines.h:29
virtual bool OnPlayerRightClick(cPlayer &a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override
Definition: PluginLua.cpp:721
void AddPackagePath(const AString &a_PathVariable, const AString &a_Path)
Adds the specified path to package.
Definition: LuaState.cpp:618
virtual bool OnPostCrafting(cPlayer &a_Player, cCraftingGrid &a_Grid, cCraftingRecipe &a_Recipe) override
Definition: PluginLua.cpp:833
virtual bool OnSpawningMonster(cWorld &a_World, cMonster &a_Monster) override
Definition: PluginLua.cpp:920
virtual bool OnSpawningEntity(cWorld &a_World, cEntity &a_Entity) override
Definition: PluginLua.cpp:911
virtual void Tick(float a_Dt) override
Definition: PluginLua.cpp:212
static cRoot * Get()
Definition: Root.h:51
void Create(void)
Creates the m_LuaState, if not created already.
Definition: LuaState.cpp:487
virtual bool OnHandshake(cClientHandle &a_Client, const AString &a_Username) override
Definition: PluginLua.cpp:499
bool CanAddOldStyleHook(int a_HookType)
Returns true if the plugin contains the function for the specified hook type, using the old-style reg...
Definition: PluginLua.cpp:1032
virtual bool OnWorldTick(cWorld &a_World, std::chrono::milliseconds a_Dt, std::chrono::milliseconds a_LastTickDurationMSec) override
Definition: PluginLua.cpp:1023
bool HasFunction(const char *a_FunctionName)
Returns true if a_FunctionName is a valid Lua function that can be called.
Definition: LuaState.cpp:716
virtual bool OnChat(cPlayer &a_Player, AString &a_Message) override
Definition: PluginLua.cpp:272
virtual bool OnPlayerMoving(cPlayer &a_Player, const Vector3d &a_OldPosition, const Vector3d &a_NewPosition) override
Definition: PluginLua.cpp:668
bool LoadFile(const AString &a_FileName, bool a_LogWarnings=true)
Loads the specified file Returns false and optionally logs a warning to the console if not successful...
Definition: LuaState.cpp:644
Definition: Entity.h:73
static AStringVector GetFolderContents(const AString &a_Folder)
Returns the list of all items in the specified folder (files, folders, nix pipes, whatever&#39;s there)...
Definition: File.cpp:498
void RegisterAPILibs(void)
Registers all the API libraries that MCS provides into m_LuaState.
Definition: LuaState.cpp:505
virtual bool OnDisconnect(cClientHandle &a_Client, const AString &a_Reason) override
Definition: PluginLua.cpp:359
virtual void Unload(void) override
Unloads the plugin.
Definition: PluginLua.cpp:187
unsigned int UInt32
Definition: Globals.h:113
Represents a UI window.
Definition: Window.h:53
virtual bool OnPlayerUsedBlock(cPlayer &a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override
Definition: PluginLua.cpp:766
virtual bool OnTakeDamage(cEntity &a_Receiver, TakeDamageInfo &a_TakeDamageInfo) override
Definition: PluginLua.cpp:929
void RemoveAllPluginWebTabs(const AString &a_PluginName)
Removes all WebTabs registered by the specified plugin.
Definition: WebAdmin.cpp:167
virtual bool OnLoginForge(cClientHandle &a_Client, const AStringMap &a_Mods) override
Definition: PluginLua.cpp:568
NIBBLETYPE m_BlockMeta
Definition: ChunkDef.h:522
virtual bool OnPlayerAnimation(cPlayer &a_Player, int a_Animation) override
Definition: PluginLua.cpp:577
virtual bool OnPlayerSpawned(cPlayer &a_Player) override
Definition: PluginLua.cpp:748
cWebAdmin * GetWebAdmin(void)
Definition: Root.h:113
int CopyStackFrom(cLuaState &a_SrcLuaState, int a_SrcStart, int a_SrcEnd, int a_NumAllowedNestingLevels=16)
Copies objects on the stack from the specified state.
Definition: LuaState.cpp:2103
virtual bool OnEntityTeleport(cEntity &a_Entity, const Vector3d &a_OldPosition, const Vector3d &a_NewPosition) override
Definition: PluginLua.cpp:677
virtual bool OnPlayerBrokenBlock(cPlayer &a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override
Definition: PluginLua.cpp:595
#define LUA_PLUGIN_INSTANCE_VAR_NAME
Definition: PluginLua.h:17
virtual bool OnEntityChangingWorld(cEntity &a_Entity, cWorld &a_World) override
Definition: PluginLua.cpp:377
const AString & GetName(void) const
Definition: Plugin.h:116
virtual bool OnExecuteCommand(cPlayer *a_Player, const AStringVector &a_Split, const AString &a_EntireCommand, cPluginManager::CommandResult &a_Result) override
Definition: PluginLua.cpp:395
eSpreadSource
Definition: BlockID.h:1231
virtual bool OnPlayerFishing(cPlayer &a_Player, cItems &a_Reward) override
Definition: PluginLua.cpp:641
static const char * GetHookFnName(int a_HookType)
Returns the name of Lua function that should handle the specified hook type in the older (#121) API...
Definition: PluginLua.cpp:1062
Definition: Item.h:36
The plugin has been loaded successfully.
Definition: PluginManager.h:60
void UntrackInDeadlockDetect(cDeadlockDetect &a_DeadlockDetect)
Removes this object&#39;s CS from the DeadlockDetect&#39;s tracked CSs.
Definition: LuaState.cpp:2343
virtual bool OnPlayerPlacingBlock(cPlayer &a_Player, const sSetBlock &a_BlockChange) override
Definition: PluginLua.cpp:708
virtual bool OnPlayerBreakingBlock(cPlayer &a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override
Definition: PluginLua.cpp:586
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:234
AString m_Name
The name of the plugin, used to identify the plugin in the system and for inter-plugin calls...
Definition: Plugin.h:145
virtual bool OnChunkUnloading(cWorld &a_World, int a_ChunkX, int a_ChunkZ) override
Definition: PluginLua.cpp:332
virtual bool OnPlayerDestroyed(cPlayer &a_Player) override
Definition: PluginLua.cpp:604
virtual bool OnBrewingCompleting(cWorld &a_World, cBrewingstandEntity &a_BrewingstandEntity) override
Definition: PluginLua.cpp:263
virtual bool OnPlayerUsedItem(cPlayer &a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override
Definition: PluginLua.cpp:775