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 #include "Defines.h"
8 #include "Entities/Minecart.h"
9 
10 #ifdef __APPLE__
11  #define LUA_USE_MACOSX
12 #else
13  #define LUA_USE_POSIX
14 #endif
15 
16 #include "PluginLua.h"
17 #include "../CommandOutput.h"
18 #include "PluginManager.h"
19 #include "../Item.h"
20 #include "../Root.h"
21 #include "../WebAdmin.h"
22 
23 extern "C"
24 {
25  #include "lua/src/lauxlib.h"
26 }
27 
28 #undef TOLUA_TEMPLATE_BIND
29 #include "tolua++/include/tolua++.h"
30 
31 
32 
33 
34 
36 // cPluginLua:
37 
38 cPluginLua::cPluginLua(const AString & a_PluginDirectory, cDeadlockDetect & a_DeadlockDetect) :
39  cPlugin(a_PluginDirectory),
40  m_LuaState(fmt::format(FMT_STRING("plugin {}"), a_PluginDirectory)),
41  m_DeadlockDetect(a_DeadlockDetect)
42 {
43  m_LuaState.TrackInDeadlockDetect(a_DeadlockDetect);
44 }
45 
46 
47 
48 
49 
51 {
52  Close();
54 }
55 
56 
57 
58 
59 
61 {
62  cOperation op(*this);
63  // If already closed, bail out:
64  if (!op().IsValid())
65  {
66  ASSERT(m_HookMap.empty());
67  return;
68  }
69 
70  // Remove the web tabs:
71  ClearWebTabs();
72 
73  // Release all the references in the hook map:
74  m_HookMap.clear();
75 
76  // Close the Lua engine:
77  op().Close();
78 }
79 
80 
81 
82 
83 
84 bool cPluginLua::Load(void)
85 {
86  cOperation op(*this);
87  if (!op().IsValid())
88  {
91 
92  // Inject the identification global variables into the state:
93  lua_pushlightuserdata(m_LuaState, this);
95 
96  // Add the plugin's folder to the package.path and package.cpath variables (#693):
97  m_LuaState.AddPackagePath("path", GetLocalFolder() + "/?.lua");
98  #ifdef _WIN32
99  m_LuaState.AddPackagePath("cpath", GetLocalFolder() + "\\?.dll");
100  #else
101  m_LuaState.AddPackagePath("cpath", 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 = 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);
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(fmt::format(FMT_STRING("Failed to load file {}."), *itr));
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 esTNTMinecart: hook->Call(&a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, static_cast<cMinecartWithTNT *> (a_SourceData), cLuaState::Return, res); break;
440  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;
441  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;
442  case esMax:
443  {
444  ASSERT(!"Invalid explosion source");
445  return false;
446  }
447  }
448  if (res)
449  {
450  return true;
451  }
452  }
453  return false;
454 }
455 
456 
457 
458 
459 
460 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)
461 {
462  cOperation op(*this);
463  if (!op().IsValid())
464  {
465  return false;
466  }
467  bool res = false;
469  for (auto & hook: hooks)
470  {
471  switch (a_Source)
472  {
473  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;
474  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;
475  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;
476  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;
477  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;
478  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;
479  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;
480  case esTNTMinecart: hook->Call(&a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, static_cast<cMinecartWithTNT *> (a_SourceData), cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break;
481  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;
482  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;
483  case esMax:
484  {
485  ASSERT(!"Invalid explosion source");
486  return false;
487  }
488  }
489  if (res)
490  {
491  return true;
492  }
493  }
494  return false;
495 }
496 
497 
498 
499 
500 
501 bool cPluginLua::OnHandshake(cClientHandle & a_Client, const AString & a_Username)
502 {
503  return CallSimpleHooks(cPluginManager::HOOK_HANDSHAKE, &a_Client, a_Username);
504 }
505 
506 
507 
508 
509 
510 bool cPluginLua::OnHopperPullingItem(cWorld & a_World, cHopperEntity & a_Hopper, int a_DstSlotNum, cBlockEntityWithItems & a_SrcEntity, int a_SrcSlotNum)
511 {
512  return CallSimpleHooks(cPluginManager::HOOK_HOPPER_PULLING_ITEM, &a_World, &a_Hopper, a_DstSlotNum, &a_SrcEntity, a_SrcSlotNum);
513 }
514 
515 
516 
517 
518 
519 bool cPluginLua::OnHopperPushingItem(cWorld & a_World, cHopperEntity & a_Hopper, int a_SrcSlotNum, cBlockEntityWithItems & a_DstEntity, int a_DstSlotNum)
520 {
521  return CallSimpleHooks(cPluginManager::HOOK_HOPPER_PUSHING_ITEM, &a_World, &a_Hopper, a_SrcSlotNum, &a_DstEntity, a_DstSlotNum);
522 }
523 
524 
525 
526 
527 
528 bool cPluginLua::OnDropSpense(cWorld & a_World, cDropSpenserEntity & a_DropSpenser, int a_SlotNum)
529 {
530  return CallSimpleHooks(cPluginManager::HOOK_DROPSPENSE, &a_World, &a_DropSpenser, a_SlotNum);
531 }
532 
533 
534 
535 
536 
537 bool cPluginLua::OnKilled(cEntity & a_Victim, TakeDamageInfo & a_TDI, AString & a_DeathMessage)
538 {
539  cOperation op(*this);
540  if (!op().IsValid())
541  {
542  return false;
543  }
544  bool res = false;
545  auto & hooks = m_HookMap[cPluginManager::HOOK_KILLED];
546  for (auto & hook: hooks)
547  {
548  hook->Call(&a_Victim, &a_TDI, a_DeathMessage, cLuaState::Return, res, a_DeathMessage);
549  if (res)
550  {
551  return true;
552  }
553  }
554  return false;
555 }
556 
557 
558 
559 
560 
561 bool cPluginLua::OnKilling(cEntity & a_Victim, cEntity * a_Killer, TakeDamageInfo & a_TDI)
562 {
563  return CallSimpleHooks(cPluginManager::HOOK_KILLING, &a_Victim, a_Killer, &a_TDI);
564 }
565 
566 
567 
568 
569 
570 bool cPluginLua::OnLogin(cClientHandle & a_Client, UInt32 a_ProtocolVersion, const AString & a_Username)
571 {
572  return CallSimpleHooks(cPluginManager::HOOK_LOGIN, &a_Client, a_ProtocolVersion, a_Username);
573 }
574 
575 
576 
577 
578 
579 bool cPluginLua::OnLoginForge(cClientHandle & a_Client, const AStringMap & a_Mods)
580 {
581  return CallSimpleHooks(cPluginManager::HOOK_LOGIN_FORGE, &a_Client, a_Mods);
582 }
583 
584 
585 
586 
587 
588 bool cPluginLua::OnPlayerAnimation(cPlayer & a_Player, int a_Animation)
589 {
590  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_ANIMATION, &a_Player, a_Animation);
591 }
592 
593 
594 
595 
596 
597 bool cPluginLua::OnPlayerBreakingBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
598 {
599  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_BREAKING_BLOCK, &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_BlockType, a_BlockMeta);
600 }
601 
602 
603 
604 
605 
606 bool cPluginLua::OnPlayerBrokenBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
607 {
608  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_BROKEN_BLOCK, &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_BlockType, a_BlockMeta);
609 }
610 
611 
612 
613 
614 
616 {
618 }
619 
620 
621 
622 
623 
625 {
627 }
628 
629 
630 
631 
632 
633 bool cPluginLua::OnPlayerFoodLevelChange(cPlayer & a_Player, int a_NewFoodLevel)
634 {
635  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_FOOD_LEVEL_CHANGE, &a_Player, a_NewFoodLevel);
636 }
637 
638 
639 
640 
641 
642 bool cPluginLua::OnPlayerFished(cPlayer & a_Player, const cItems & a_Reward, const int ExperienceAmount)
643 {
644  cItems reward(a_Reward);
645  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_FISHED, &a_Player, &reward, ExperienceAmount);
646 }
647 
648 
649 
650 
651 
652 bool cPluginLua::OnPlayerFishing(cPlayer & a_Player, cItems & a_Reward, int & ExperienceAmount)
653 {
654  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_FISHING, &a_Player, &a_Reward, &ExperienceAmount);
655 }
656 
657 
658 
659 
660 
662 {
664 }
665 
666 
667 
668 
669 
670 bool cPluginLua::OnPlayerLeftClick(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, char a_Status)
671 {
672  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_LEFT_CLICK, &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_Status);
673 }
674 
675 
676 
677 
678 
679 bool cPluginLua::OnPlayerMoving(cPlayer & a_Player, const Vector3d & a_OldPosition, const Vector3d & a_NewPosition, bool a_PreviousIsOnGround)
680 {
681  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_MOVING, &a_Player, a_OldPosition, a_NewPosition, a_PreviousIsOnGround);
682 }
683 
684 
685 
686 
687 
688 bool cPluginLua::OnEntityTeleport(cEntity & a_Entity, const Vector3d & a_OldPosition, const Vector3d & a_NewPosition)
689 {
690  return CallSimpleHooks(cPluginManager::HOOK_ENTITY_TELEPORT, &a_Entity, a_OldPosition, a_NewPosition);
691 }
692 
693 
694 
695 
696 
698 {
699  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_OPENING_WINDOW, &a_Player, &a_Window);
700 }
701 
702 
703 
704 
705 
706 bool cPluginLua::OnPlayerPlacedBlock(cPlayer & a_Player, const sSetBlock & a_BlockChange)
707 {
709  &a_Player,
710  a_BlockChange.GetX(), a_BlockChange.GetY(), a_BlockChange.GetZ(),
711  a_BlockChange.m_BlockType, a_BlockChange.m_BlockMeta
712  );
713 }
714 
715 
716 
717 
718 
719 bool cPluginLua::OnPlayerPlacingBlock(cPlayer & a_Player, const sSetBlock & a_BlockChange)
720 {
722  &a_Player,
723  a_BlockChange.GetX(), a_BlockChange.GetY(), a_BlockChange.GetZ(),
724  a_BlockChange.m_BlockType, a_BlockChange.m_BlockMeta
725  );
726 }
727 
728 
729 
730 
731 
733 {
735  &a_Player);
736 }
737 
738 
739 
740 
741 
742 bool cPluginLua::OnPlayerRightClick(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ)
743 {
744  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_RIGHT_CLICK, &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ);
745 }
746 
747 
748 
749 
750 
752 {
754 }
755 
756 
757 
758 
759 
761 {
763 }
764 
765 
766 
767 
768 
770 {
772 }
773 
774 
775 
776 
777 
779 {
781 }
782 
783 
784 
785 
786 
787 bool cPluginLua::OnPlayerUsedBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
788 {
789  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);
790 }
791 
792 
793 
794 
795 
796 bool cPluginLua::OnPlayerUsedItem(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ)
797 {
798  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_USED_ITEM, &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ);
799 }
800 
801 
802 
803 
804 
805 bool cPluginLua::OnPlayerUsingBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
806 {
807  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);
808 }
809 
810 
811 
812 
813 
814 bool cPluginLua::OnPlayerUsingItem(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ)
815 {
816  return CallSimpleHooks(cPluginManager::HOOK_PLAYER_USING_ITEM, &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ);
817 }
818 
819 
820 
821 
822 
823 bool cPluginLua::OnPluginMessage(cClientHandle & a_Client, const AString & a_Channel, const ContiguousByteBufferView a_Message)
824 {
825  return CallSimpleHooks(cPluginManager::HOOK_PLUGIN_MESSAGE, &a_Client, a_Channel, a_Message);
826 }
827 
828 
829 
830 
831 
833 {
834  cOperation op(*this);
835  if (!op().IsValid())
836  {
837  return false;
838  }
839  bool res = false;
841  for (auto & hook: hooks)
842  {
843  bool ret = false;
844  hook->Call(cLuaState::Return, ret);
845  res = res || ret;
846  }
847  return res;
848 }
849 
850 
851 
852 
853 
854 bool cPluginLua::OnPostCrafting(cPlayer & a_Player, cCraftingGrid & a_Grid, cCraftingRecipe & a_Recipe)
855 {
856  return CallSimpleHooks(cPluginManager::HOOK_POST_CRAFTING, &a_Player, &a_Grid, &a_Recipe);
857 }
858 
859 
860 
861 
862 
863 bool cPluginLua::OnPreCrafting(cPlayer & a_Player, cCraftingGrid & a_Grid, cCraftingRecipe & a_Recipe)
864 {
865  return CallSimpleHooks(cPluginManager::HOOK_PRE_CRAFTING, &a_Player, &a_Grid, &a_Recipe);
866 }
867 
868 
869 
870 
871 
872 bool cPluginLua::OnProjectileHitBlock(cProjectileEntity & a_Projectile, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_Face, const Vector3d & a_BlockHitPos)
873 {
874  return CallSimpleHooks(cPluginManager::HOOK_PROJECTILE_HIT_BLOCK, &a_Projectile, a_BlockX, a_BlockY, a_BlockZ, a_Face, a_BlockHitPos);
875 }
876 
877 
878 
879 
880 
882 {
883  return CallSimpleHooks(cPluginManager::HOOK_PROJECTILE_HIT_ENTITY, &a_Projectile, &a_HitEntity);
884 }
885 
886 
887 
888 
889 
890 bool cPluginLua::OnServerPing(cClientHandle & a_ClientHandle, AString & a_ServerDescription, int & a_OnlinePlayersCount, int & a_MaxPlayersCount, AString & a_Favicon)
891 {
892  cOperation op(*this);
893  if (!op().IsValid())
894  {
895  return false;
896  }
897  bool res = false;
899  for (auto & hook: hooks)
900  {
901  hook->Call(&a_ClientHandle, a_ServerDescription, a_OnlinePlayersCount, a_MaxPlayersCount, a_Favicon, cLuaState::Return, res, a_ServerDescription, a_OnlinePlayersCount, a_MaxPlayersCount, a_Favicon);
902  if (res)
903  {
904  return true;
905  }
906  }
907  return false;
908 }
909 
910 
911 
912 
913 
914 bool cPluginLua::OnSpawnedEntity(cWorld & a_World, cEntity & a_Entity)
915 {
916  return CallSimpleHooks(cPluginManager::HOOK_SPAWNED_ENTITY, &a_World, &a_Entity);
917 }
918 
919 
920 
921 
922 
923 bool cPluginLua::OnSpawnedMonster(cWorld & a_World, cMonster & a_Monster)
924 {
925  return CallSimpleHooks(cPluginManager::HOOK_SPAWNED_MONSTER, &a_World, &a_Monster);
926 }
927 
928 
929 
930 
931 
932 bool cPluginLua::OnSpawningEntity(cWorld & a_World, cEntity & a_Entity)
933 {
934  return CallSimpleHooks(cPluginManager::HOOK_SPAWNING_ENTITY, &a_World, &a_Entity);
935 }
936 
937 
938 
939 
940 
941 bool cPluginLua::OnSpawningMonster(cWorld & a_World, cMonster & a_Monster)
942 {
943  return CallSimpleHooks(cPluginManager::HOOK_SPAWNING_MONSTER, &a_World, &a_Monster);
944 }
945 
946 
947 
948 
949 
951 {
952  return CallSimpleHooks(cPluginManager::HOOK_TAKE_DAMAGE, &a_Receiver, &a_TDI);
953 }
954 
955 
956 
957 
958 
960  cWorld & a_World,
961  int a_BlockX, int a_BlockY, int a_BlockZ,
962  const AString & a_Line1, const AString & a_Line2, const AString & a_Line3, const AString & a_Line4,
963  cPlayer * a_Player
964 )
965 {
966  return CallSimpleHooks(cPluginManager::HOOK_UPDATED_SIGN, &a_World, a_BlockX, a_BlockY, a_BlockZ, a_Line1, a_Line2, a_Line3, a_Line4, a_Player);
967 }
968 
969 
970 
971 
972 
974  cWorld & a_World,
975  int a_BlockX, int a_BlockY, int a_BlockZ,
976  AString & a_Line1, AString & a_Line2, AString & a_Line3, AString & a_Line4,
977  cPlayer * a_Player
978 )
979 {
980  cOperation op(*this);
981  if (!op().IsValid())
982  {
983  return false;
984  }
985  bool res = false;
987  for (auto & hook: hooks)
988  {
989  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);
990  if (res)
991  {
992  return true;
993  }
994  }
995  return false;
996 }
997 
998 
999 
1000 
1001 
1003 {
1005 }
1006 
1007 
1008 
1009 
1010 
1011 bool cPluginLua::OnWeatherChanging(cWorld & a_World, eWeather & a_NewWeather)
1012 {
1013  cOperation op(*this);
1014  if (!op().IsValid())
1015  {
1016  return false;
1017  }
1018  bool res = false;
1020  for (auto & hook: hooks)
1021  {
1022  hook->Call(&a_World, a_NewWeather, cLuaState::Return, res, a_NewWeather);
1023  if (res)
1024  {
1025  return true;
1026  }
1027  }
1028  return false;
1029 }
1030 
1031 
1032 
1033 
1034 
1036 {
1038 }
1039 
1040 
1041 
1042 
1043 
1044 bool cPluginLua::OnWorldTick(cWorld & a_World, std::chrono::milliseconds a_Dt, std::chrono::milliseconds a_LastTickDurationMSec)
1045 {
1046  return CallSimpleHooks(cPluginManager::HOOK_WORLD_TICK, &a_World, a_Dt, a_LastTickDurationMSec);
1047 }
1048 
1049 
1050 
1051 
1052 
1054 {
1055  const char * FnName = GetHookFnName(a_HookType);
1056  if (FnName == nullptr)
1057  {
1058  // Unknown hook ID
1059  LOGWARNING("Plugin %s wants to add an unknown hook ID (%d). The plugin need not work properly.",
1060  GetName().c_str(), a_HookType
1061  );
1063  return false;
1064  }
1065 
1066  // Check if the function is available
1067  if (m_LuaState.HasFunction(FnName))
1068  {
1069  return true;
1070  }
1071 
1072  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.",
1073  GetName().c_str(), a_HookType, FnName
1074  );
1076  return false;
1077 }
1078 
1079 
1080 
1081 
1082 
1083 const char * cPluginLua::GetHookFnName(int a_HookType)
1084 {
1085  switch (a_HookType)
1086  {
1087  case cPluginManager::HOOK_BLOCK_SPREAD: return "OnBlockSpread";
1088  case cPluginManager::HOOK_BLOCK_TO_PICKUPS: return "OnBlockToPickups";
1089  case cPluginManager::HOOK_CHAT: return "OnChat";
1090  case cPluginManager::HOOK_CHUNK_AVAILABLE: return "OnChunkAvailable";
1091  case cPluginManager::HOOK_CHUNK_GENERATED: return "OnChunkGenerated";
1092  case cPluginManager::HOOK_CHUNK_GENERATING: return "OnChunkGenerating";
1093  case cPluginManager::HOOK_CHUNK_UNLOADED: return "OnChunkUnloaded";
1094  case cPluginManager::HOOK_CHUNK_UNLOADING: return "OnChunkUnloading";
1095  case cPluginManager::HOOK_COLLECTING_PICKUP: return "OnCollectingPickup";
1096  case cPluginManager::HOOK_CRAFTING_NO_RECIPE: return "OnCraftingNoRecipe";
1097  case cPluginManager::HOOK_DISCONNECT: return "OnDisconnect";
1098  case cPluginManager::HOOK_PLAYER_ANIMATION: return "OnPlayerAnimation";
1099  case cPluginManager::HOOK_ENTITY_ADD_EFFECT: return "OnEntityAddEffect";
1100  case cPluginManager::HOOK_ENTITY_CHANGING_WORLD: return "OnEntityChangingWorld";
1101  case cPluginManager::HOOK_ENTITY_CHANGED_WORLD: return "OnEntityChangedWorld";
1102  case cPluginManager::HOOK_ENTITY_TELEPORT: return "OnEntityTeleport";
1103  case cPluginManager::HOOK_EXECUTE_COMMAND: return "OnExecuteCommand";
1104  case cPluginManager::HOOK_HANDSHAKE: return "OnHandshake";
1105  case cPluginManager::HOOK_KILLING: return "OnKilling";
1106  case cPluginManager::HOOK_LOGIN: return "OnLogin";
1107  case cPluginManager::HOOK_LOGIN_FORGE: return "OnLoginForge";
1108  case cPluginManager::HOOK_PLAYER_BREAKING_BLOCK: return "OnPlayerBreakingBlock";
1109  case cPluginManager::HOOK_PLAYER_BROKEN_BLOCK: return "OnPlayerBrokenBlock";
1110  case cPluginManager::HOOK_PLAYER_EATING: return "OnPlayerEating";
1111  case cPluginManager::HOOK_PLAYER_JOINED: return "OnPlayerJoined";
1112  case cPluginManager::HOOK_PLAYER_LEFT_CLICK: return "OnPlayerLeftClick";
1113  case cPluginManager::HOOK_PLAYER_MOVING: return "OnPlayerMoving";
1114  case cPluginManager::HOOK_PLAYER_OPENING_WINDOW: return "OnPlayerOpeningWindow";
1115  case cPluginManager::HOOK_PLAYER_PLACED_BLOCK: return "OnPlayerPlacedBlock";
1116  case cPluginManager::HOOK_PLAYER_PLACING_BLOCK: return "OnPlayerPlacingBlock";
1117  case cPluginManager::HOOK_PLAYER_CROUCHED: return "OnPlayerCrouched";
1118  case cPluginManager::HOOK_PLAYER_RIGHT_CLICK: return "OnPlayerRightClick";
1119  case cPluginManager::HOOK_PLAYER_RIGHT_CLICKING_ENTITY: return "OnPlayerRightClickingEntity";
1120  case cPluginManager::HOOK_PLAYER_SHOOTING: return "OnPlayerShooting";
1121  case cPluginManager::HOOK_PLAYER_SPAWNED: return "OnPlayerSpawned";
1122  case cPluginManager::HOOK_PLAYER_TOSSING_ITEM: return "OnPlayerTossingItem";
1123  case cPluginManager::HOOK_PLAYER_USED_BLOCK: return "OnPlayerUsedBlock";
1124  case cPluginManager::HOOK_PLAYER_USED_ITEM: return "OnPlayerUsedItem";
1125  case cPluginManager::HOOK_PLAYER_USING_BLOCK: return "OnPlayerUsingBlock";
1126  case cPluginManager::HOOK_PLAYER_USING_ITEM: return "OnPlayerUsingItem";
1127  case cPluginManager::HOOK_PLUGIN_MESSAGE: return "OnPluginMessage";
1128  case cPluginManager::HOOK_PLUGINS_LOADED: return "OnPluginsLoaded";
1129  case cPluginManager::HOOK_POST_CRAFTING: return "OnPostCrafting";
1130  case cPluginManager::HOOK_PRE_CRAFTING: return "OnPreCrafting";
1131  case cPluginManager::HOOK_SERVER_PING: return "OnServerPing";
1132  case cPluginManager::HOOK_SPAWNED_ENTITY: return "OnSpawnedEntity";
1133  case cPluginManager::HOOK_SPAWNED_MONSTER: return "OnSpawnedMonster";
1134  case cPluginManager::HOOK_SPAWNING_ENTITY: return "OnSpawningEntity";
1135  case cPluginManager::HOOK_SPAWNING_MONSTER: return "OnSpawningMonster";
1136  case cPluginManager::HOOK_TAKE_DAMAGE: return "OnTakeDamage";
1137  case cPluginManager::HOOK_TICK: return "OnTick";
1138  case cPluginManager::HOOK_UPDATED_SIGN: return "OnUpdatedSign";
1139  case cPluginManager::HOOK_UPDATING_SIGN: return "OnUpdatingSign";
1140  case cPluginManager::HOOK_WEATHER_CHANGED: return "OnWeatherChanged";
1141  case cPluginManager::HOOK_WEATHER_CHANGING: return "OnWeatherChanging";
1142  case cPluginManager::HOOK_WORLD_TICK: return "OnWorldTick";
1143 
1145  {
1146  // Satisfy a warning that all enum values should be used in a switch
1147  // but don't want a default branch, so that we catch new hooks missing from this list.
1148  break;
1149  }
1150  } // switch (a_Hook)
1151  LOGWARNING("Requested name of an unknown hook type function: %d (max is %d)", a_HookType, cPluginManager::HOOK_MAX);
1152  ASSERT(!"Unknown hook requested!");
1153  return nullptr;
1154 }
1155 
1156 
1157 
1158 
1159 
1160 bool cPluginLua::AddHookCallback(int a_HookType, cLuaState::cCallbackPtr && a_Callback)
1161 {
1162  m_HookMap[a_HookType].push_back(std::move(a_Callback));
1163  return true;
1164 }
1165 
1166 
1167 
1168 
1169 
1171  const AString & a_FunctionName,
1172  cLuaState & a_ForeignState,
1173  int a_ParamStart,
1174  int a_ParamEnd
1175 )
1176 {
1177  cOperation op(*this);
1178 
1179  // Call the function:
1180  int NumReturns = op().CallFunctionWithForeignParams(a_FunctionName, a_ForeignState, a_ParamStart, a_ParamEnd);
1181  if (NumReturns < 0)
1182  {
1183  // The call has failed, an error has already been output to the log, so just silently bail out with the same error
1184  return NumReturns;
1185  }
1186 
1187  // Copy all the return values:
1188  int Top = lua_gettop(m_LuaState);
1189  int res = a_ForeignState.CopyStackFrom(m_LuaState, Top - NumReturns + 1, Top);
1190 
1191  // Remove the return values off this stack:
1192  if (NumReturns > 0)
1193  {
1194  lua_pop(m_LuaState, NumReturns);
1195  }
1196 
1197  return res;
1198 }
1199 
1200 
1201 
1202 
1203 
1205 {
1206  auto webAdmin = cRoot::Get()->GetWebAdmin();
1207  if (webAdmin != nullptr) // can be nullptr when shutting down the server
1208  {
1209  webAdmin->RemoveAllPluginWebTabs(m_Name);
1210  }
1211 }
1212 
1213 
1214 
1215 
#define LUA_PLUGIN_INSTANCE_VAR_NAME
Definition: PluginLua.h:16
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
eWeather
Definition: Defines.h:160
eExplosionSource
The source of an explosion.
Definition: Defines.h:309
@ esMonster
Definition: Defines.h:313
@ esOther
Definition: Defines.h:314
@ esWitherSkull
Definition: Defines.h:319
@ esEnderCrystal
Definition: Defines.h:311
@ esMax
Definition: Defines.h:320
@ esPlugin
Definition: Defines.h:315
@ esTNTMinecart
Definition: Defines.h:317
@ esPrimedTNT
Definition: Defines.h:316
@ esBed
Definition: Defines.h:310
@ esWitherBirth
Definition: Defines.h:318
@ esGhastFireball
Definition: Defines.h:312
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc.
Definition: Defines.h:38
eSpreadSource
Definition: Defines.h:339
unsigned int UInt32
Definition: Globals.h:157
std::basic_string_view< std::byte > ContiguousByteBufferView
Definition: Globals.h:376
#define ASSERT(x)
Definition: Globals.h:276
void LOGWARNING(std::string_view a_Format, const Args &... args)
Definition: LoggerSimple.h:67
void LOGINFO(std::string_view a_Format, const Args &... args)
Definition: LoggerSimple.h:61
std::vector< AString > AStringVector
Definition: StringUtils.h:12
std::string AString
Definition: StringUtils.h:11
std::map< AString, AString > AStringMap
A string dictionary, used for key-value pairs.
Definition: StringUtils.h:16
Implements custom fmtlib formatting for cChunkCoords.
Definition: ChunkDef.h:104
Encapsulates a Lua state and provides some syntactic sugar for common operations.
Definition: LuaState.h:56
void AddPackagePath(const AString &a_PathVariable, const AString &a_Path)
Adds the specified path to package.
Definition: LuaState.cpp:611
void LogStackTrace(int a_StartingDepth=0)
Logs all items in the current stack trace to the server console.
Definition: LuaState.cpp:2134
std::unique_ptr< cCallback > cCallbackPtr
Definition: LuaState.h:326
bool Call(const FnT &a_Function, Args &&... args)
Call the specified Lua function.
Definition: LuaState.h:751
bool HasFunction(const char *a_FunctionName)
Returns true if a_FunctionName is a valid Lua function that can be called.
Definition: LuaState.cpp:709
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:637
static const cRet Return
Definition: LuaState.h:445
void RegisterAPILibs(void)
Registers all the API libraries that MCS provides into m_LuaState.
Definition: LuaState.cpp:498
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:2257
void UntrackInDeadlockDetect(cDeadlockDetect &a_DeadlockDetect)
Removes this object's CS from the DeadlockDetect's tracked CSs.
Definition: LuaState.cpp:2496
void TrackInDeadlockDetect(cDeadlockDetect &a_DeadlockDetect)
Adds this object's CS to the DeadlockDetect's tracked CSs.
Definition: LuaState.cpp:2487
void Create(void)
Creates the m_LuaState, if not created already.
Definition: LuaState.cpp:480
Definition: Plugin.h:20
AString m_Name
The name of the plugin, used to identify the plugin in the system and for inter-plugin calls.
Definition: Plugin.h:147
AString GetLocalFolder(void) const
Returns the folder relative to the MCS Executable, from which the plugin is loaded.
Definition: Plugin.cpp:46
const AString & GetName(void) const
Definition: Plugin.h:118
cPluginManager::ePluginStatus m_Status
Definition: Plugin.h:144
virtual void Unload(void)
Unloads the plugin.
Definition: Plugin.cpp:31
void SetLoadError(const AString &a_LoadError)
Sets m_LoadError to the specified string and m_Status to psError.
Definition: Plugin.cpp:55
virtual bool OnPlayerFished(cPlayer &a_Player, const cItems &a_Reward, const int ExperienceAmount) override
Definition: PluginLua.cpp:642
virtual bool OnChat(cPlayer &a_Player, AString &a_Message) override
Definition: PluginLua.cpp:272
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
virtual bool OnSpawningMonster(cWorld &a_World, cMonster &a_Monster) override
Definition: PluginLua.cpp:941
virtual bool OnChunkAvailable(cWorld &a_World, int a_ChunkX, int a_ChunkZ) override
Definition: PluginLua.cpp:296
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:1053
virtual void Tick(float a_Dt) override
Definition: PluginLua.cpp:212
virtual bool OnPostCrafting(cPlayer &a_Player, cCraftingGrid &a_Grid, cCraftingRecipe &a_Recipe) override
Definition: PluginLua.cpp:854
virtual bool OnWorldStarted(cWorld &a_World) override
Definition: PluginLua.cpp:1035
virtual bool OnChunkGenerated(cWorld &a_World, int a_ChunkX, int a_ChunkZ, cChunkDesc *a_ChunkDesc) override
Definition: PluginLua.cpp:305
virtual bool OnLogin(cClientHandle &a_Client, UInt32 a_ProtocolVersion, const AString &a_Username) override
Definition: PluginLua.cpp:570
virtual bool OnPlayerMoving(cPlayer &a_Player, const Vector3d &a_OldPosition, const Vector3d &a_NewPosition, bool a_PreviousIsOnGround) override
Definition: PluginLua.cpp:679
virtual bool OnSpawnedEntity(cWorld &a_World, cEntity &a_Entity) override
Definition: PluginLua.cpp:914
virtual bool OnPlayerDestroyed(cPlayer &a_Player) override
Definition: PluginLua.cpp:615
void Close(void)
Releases all Lua references, notifies and removes all m_Resettables[] and closes the m_LuaState.
Definition: PluginLua.cpp:60
virtual bool OnCollectingPickup(cPlayer &a_Player, cPickup &a_Pickup) override
Definition: PluginLua.cpp:341
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:1083
virtual bool OnKilled(cEntity &a_Victim, TakeDamageInfo &a_TDI, AString &a_DeathMessage) override
Definition: PluginLua.cpp:537
virtual ~cPluginLua() override
Definition: PluginLua.cpp:50
virtual void OnDisable(void) override
Called as the last call into the plugin before it is unloaded.
Definition: PluginLua.cpp:198
bool AddHookCallback(int a_HookType, cLuaState::cCallbackPtr &&a_Callback)
Adds a Lua callback to be called for the specified hook.
Definition: PluginLua.cpp:1160
virtual bool OnDisconnect(cClientHandle &a_Client, const AString &a_Reason) override
Definition: PluginLua.cpp:359
virtual bool OnPlayerSpawned(cPlayer &a_Player) override
Definition: PluginLua.cpp:769
virtual bool OnPreCrafting(cPlayer &a_Player, cCraftingGrid &a_Grid, cCraftingRecipe &a_Recipe) override
Definition: PluginLua.cpp:863
virtual bool OnPluginsLoaded(void) override
Definition: PluginLua.cpp:832
virtual bool OnSpawnedMonster(cWorld &a_World, cMonster &a_Monster) override
Definition: PluginLua.cpp:923
virtual bool OnPlayerRightClickingEntity(cPlayer &a_Player, cEntity &a_Entity) override
Definition: PluginLua.cpp:751
virtual bool OnKilling(cEntity &a_Victim, cEntity *a_Killer, TakeDamageInfo &a_TDI) override
Definition: PluginLua.cpp:561
virtual bool OnWeatherChanging(cWorld &a_World, eWeather &a_NewWeather) override
Definition: PluginLua.cpp:1011
virtual bool OnServerPing(cClientHandle &a_ClientHandle, AString &a_ServerDescription, int &a_OnlinePlayersCount, int &a_MaxPlayersCount, AString &a_Favicon) override
Definition: PluginLua.cpp:890
virtual bool OnBrewingCompleting(cWorld &a_World, cBrewingstandEntity &a_BrewingstandEntity) override
Definition: PluginLua.cpp:263
int CallFunctionFromForeignState(const AString &a_FunctionName, cLuaState &a_ForeignState, int a_ParamStart, int a_ParamEnd)
Calls a function in this plugin's LuaState with parameters copied over from a_ForeignState.
Definition: PluginLua.cpp:1170
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:460
virtual bool OnPlayerJoined(cPlayer &a_Player) override
Definition: PluginLua.cpp:661
virtual bool OnHopperPushingItem(cWorld &a_World, cHopperEntity &a_Hopper, int a_SrcSlotNum, cBlockEntityWithItems &a_DstEntity, int a_DstSlotNum) override
Definition: PluginLua.cpp:519
virtual bool OnPlayerOpeningWindow(cPlayer &a_Player, cWindow &a_Window) override
Definition: PluginLua.cpp:697
virtual bool OnPlayerAnimation(cPlayer &a_Player, int a_Animation) override
Definition: PluginLua.cpp:588
virtual bool OnPlayerUsedItem(cPlayer &a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override
Definition: PluginLua.cpp:796
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:959
virtual bool OnEntityChangedWorld(cEntity &a_Entity, cWorld &a_World) override
Definition: PluginLua.cpp:386
virtual bool OnPlayerBrokenBlock(cPlayer &a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override
Definition: PluginLua.cpp:606
virtual bool OnLoginForge(cClientHandle &a_Client, const AStringMap &a_Mods) override
Definition: PluginLua.cpp:579
virtual bool OnPluginMessage(cClientHandle &a_Client, const AString &a_Channel, ContiguousByteBufferView a_Message) override
Definition: PluginLua.cpp:823
virtual bool OnPlayerUsingItem(cPlayer &a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override
Definition: PluginLua.cpp:814
cPluginLua(const AString &a_PluginDirectory, cDeadlockDetect &a_DeadlockDetect)
Definition: PluginLua.cpp:38
virtual bool OnHopperPullingItem(cWorld &a_World, cHopperEntity &a_Hopper, int a_DstSlotNum, cBlockEntityWithItems &a_SrcEntity, int a_SrcSlotNum) override
Definition: PluginLua.cpp:510
cLuaState m_LuaState
The plugin's Lua state.
Definition: PluginLua.h:174
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 OnSpawningEntity(cWorld &a_World, cEntity &a_Entity) override
Definition: PluginLua.cpp:932
virtual bool OnPlayerFoodLevelChange(cPlayer &a_Player, int a_NewFoodLevel) override
Definition: PluginLua.cpp:633
virtual bool OnProjectileHitEntity(cProjectileEntity &a_Projectile, cEntity &a_HitEntity) override
Definition: PluginLua.cpp:881
virtual bool OnPlayerEating(cPlayer &a_Player) override
Definition: PluginLua.cpp:624
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:193
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 OnPlayerUsedBlock(cPlayer &a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override
Definition: PluginLua.cpp:787
virtual bool OnChunkUnloading(cWorld &a_World, int a_ChunkX, int a_ChunkZ) override
Definition: PluginLua.cpp:332
virtual bool OnPlayerShooting(cPlayer &a_Player) override
Definition: PluginLua.cpp:760
virtual bool OnChunkUnloaded(cWorld &a_World, int a_ChunkX, int a_ChunkZ) override
Definition: PluginLua.cpp:323
virtual bool OnPlayerUsingBlock(cPlayer &a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override
Definition: PluginLua.cpp:805
virtual bool OnHandshake(cClientHandle &a_Client, const AString &a_Username) override
Definition: PluginLua.cpp:501
virtual bool OnEntityChangingWorld(cEntity &a_Entity, cWorld &a_World) override
Definition: PluginLua.cpp:377
virtual bool OnPlayerPlacingBlock(cPlayer &a_Player, const sSetBlock &a_BlockChange) override
Definition: PluginLua.cpp:719
virtual bool OnPlayerBreakingBlock(cPlayer &a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override
Definition: PluginLua.cpp:597
virtual bool OnEntityTeleport(cEntity &a_Entity, const Vector3d &a_OldPosition, const Vector3d &a_NewPosition) override
Definition: PluginLua.cpp:688
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:872
virtual bool OnExecuteCommand(cPlayer *a_Player, const AStringVector &a_Split, const AString &a_EntireCommand, cPluginManager::CommandResult &a_Result) override
Definition: PluginLua.cpp:395
void ClearWebTabs(void)
Removes all WebTabs currently registered for this plugin from the WebAdmin.
Definition: PluginLua.cpp:1204
virtual bool OnCraftingNoRecipe(cPlayer &a_Player, cCraftingGrid &a_Grid, cCraftingRecipe &a_Recipe) override
Definition: PluginLua.cpp:350
virtual bool OnWeatherChanged(cWorld &a_World) override
Definition: PluginLua.cpp:1002
virtual bool OnPlayerFishing(cPlayer &a_Player, cItems &a_Reward, int &ExperienceAmount) override
Definition: PluginLua.cpp:652
virtual bool OnChunkGenerating(cWorld &a_World, int a_ChunkX, int a_ChunkZ, cChunkDesc *a_ChunkDesc) override
Definition: PluginLua.cpp:314
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:973
virtual bool OnPlayerCrouched(cPlayer &a_Player) override
Definition: PluginLua.cpp:732
virtual bool Load(void) override
Loads and initializes the plugin.
Definition: PluginLua.cpp:84
virtual bool OnPlayerLeftClick(cPlayer &a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, char a_Status) override
Definition: PluginLua.cpp:670
cDeadlockDetect & m_DeadlockDetect
The DeadlockDetect object to which the plugin's CS is tracked.
Definition: PluginLua.h:180
virtual bool OnTakeDamage(cEntity &a_Receiver, TakeDamageInfo &a_TakeDamageInfo) override
Definition: PluginLua.cpp:950
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
cHookMap m_HookMap
Hooks that the plugin has registered.
Definition: PluginLua.h:177
virtual bool OnPlayerPlacedBlock(cPlayer &a_Player, const sSetBlock &a_BlockChange) override
Definition: PluginLua.cpp:706
virtual bool OnWorldTick(cWorld &a_World, std::chrono::milliseconds a_Dt, std::chrono::milliseconds a_LastTickDurationMSec) override
Definition: PluginLua.cpp:1044
virtual bool OnPlayerRightClick(cPlayer &a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override
Definition: PluginLua.cpp:742
virtual void Unload(void) override
Unloads the plugin.
Definition: PluginLua.cpp:187
virtual bool OnBrewingCompleted(cWorld &a_World, cBrewingstandEntity &a_BrewingstandEntity) override
Definition: PluginLua.cpp:254
virtual bool OnDropSpense(cWorld &a_World, cDropSpenserEntity &a_DropSpenser, int a_SlotNum) override
Definition: PluginLua.cpp:528
virtual bool OnPlayerTossingItem(cPlayer &a_Player) override
Definition: PluginLua.cpp:778
A RAII-style mutex lock for accessing the internal LuaState.
Definition: PluginLua.h:39
@ HOOK_ENTITY_CHANGING_WORLD
Definition: PluginManager.h:97
@ HOOK_PLAYER_RIGHT_CLICKING_ENTITY
@ HOOK_PLAYER_FOOD_LEVEL_CHANGE
@ psLoaded
The plugin has been loaded successfully.
Definition: PluginManager.h:62
NIBBLETYPE m_BlockMeta
Definition: ChunkDef.h:391
int GetX(void) const
Returns the absolute X coord of the stored block.
Definition: ChunkDef.h:419
int GetY(void) const
Returns the absolute Y coord of the stored block.
Definition: ChunkDef.h:423
BLOCKTYPE m_BlockType
Definition: ChunkDef.h:390
int GetZ(void) const
Returns the absolute Z coord of the stored block.
Definition: ChunkDef.h:426
Definition: Entity.h:76
Definition: Pickup.h:20
Definition: Player.h:29
Definition: Item.h:37
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
static AStringVector GetFolderContents(const AString &a_Folder)
Returns the list of all items in the specified folder (files, folders, nix pipes, whatever's there).
Definition: File.cpp:498
static cRoot * Get()
Definition: Root.h:52
cWebAdmin * GetWebAdmin(void)
Definition: Root.h:110
Represents a UI window.
Definition: Window.h:54
T x
Definition: Vector3.h:17
T y
Definition: Vector3.h:17
T z
Definition: Vector3.h:17
void RemoveAllPluginWebTabs(const AString &a_PluginName)
Removes all WebTabs registered by the specified plugin.
Definition: WebAdmin.cpp:167
Definition: World.h:53