Cuberite
A lightweight, fast and extensible game server for Minecraft
LuaChunkStay.cpp
Go to the documentation of this file.
1 
2 // LuaChunkStay.cpp
3 
4 // Implements the cLuaChunkStay class representing a cChunkStay binding for plugins, used by cWorld:ChunkStay() Lua API
5 
6 #include "Globals.h"
7 #include "LuaChunkStay.h"
8 #include "PluginLua.h"
9 
10 
11 
12 
13 
15 {
16 }
17 
18 
19 
20 
21 
22 bool cLuaChunkStay::AddChunks(const cLuaState::cStackTable & a_ChunkCoordsTable)
23 {
24  // This function is expected to be called just once, with all the coords in a table
25  ASSERT(m_Chunks.empty());
26 
27  // Add each set of coords:
28  a_ChunkCoordsTable.ForEachArrayElement([=](cLuaState & a_LuaState, int a_Index)
29  {
30  if (!lua_istable(a_LuaState, -1))
31  {
32  LOGWARNING("%s: Element #%d is not a table (got %s). Ignoring the element.",
33  __FUNCTION__, a_Index, lua_typename(a_LuaState, -1)
34  );
35  a_LuaState.LogStackTrace();
36  lua_pop(a_LuaState, 1);
37  return false;
38  }
39  AddChunkCoord(a_LuaState, a_Index);
40  return false;
41  }
42  );
43 
44  // If there are no chunks, log a warning and return failure:
45  if (m_Chunks.empty())
46  {
47  LOGWARNING("%s: No valid chunk coords.", __FUNCTION__);
48  a_ChunkCoordsTable.GetLuaState().LogStackTrace();
49  return false;
50  }
51 
52  // All ok
53  return true;
54 }
55 
56 
57 
58 
59 
61 {
62  // Check that the element has 2 coords:
63  int NumCoords = luaL_getn(L, -1);
64  if (NumCoords != 2)
65  {
66  LOGWARNING("%s: Element #%d doesn't contain 2 coords (got %d). Ignoring the element.",
67  __FUNCTION__, a_Index, NumCoords
68  );
69  return;
70  }
71 
72  // Read the two coords from the element:
73  lua_rawgeti(L, -1, 1);
74  lua_rawgeti(L, -2, 2);
75  int ChunkX = luaL_checkint(L, -2);
76  int ChunkZ = luaL_checkint(L, -1);
77  lua_pop(L, 2);
78 
79  // Check that a coord is not yet present:
80  for (cChunkCoordsVector::iterator itr = m_Chunks.begin(), end = m_Chunks.end(); itr != end; ++itr)
81  {
82  if ((itr->m_ChunkX == ChunkX) && (itr->m_ChunkZ == ChunkZ))
83  {
84  LOGWARNING("%s: Element #%d is a duplicate, ignoring it.",
85  __FUNCTION__, a_Index
86  );
87  return;
88  }
89  } // for itr - m_Chunks[]
90 
91  m_Chunks.emplace_back(ChunkX, ChunkZ);
92 }
93 
94 
95 
96 
97 
98 void cLuaChunkStay::Enable(cChunkMap & a_ChunkMap, cLuaState::cCallbackPtr a_OnChunkAvailable, cLuaState::cCallbackPtr a_OnAllChunksAvailable)
99 {
100  m_OnChunkAvailable = std::move(a_OnChunkAvailable);
101  m_OnAllChunksAvailable = std::move(a_OnAllChunksAvailable);
102 
103  // Enable the ChunkStay:
104  Super::Enable(a_ChunkMap);
105 }
106 
107 
108 
109 
110 
111 void cLuaChunkStay::OnChunkAvailable(int a_ChunkX, int a_ChunkZ)
112 {
113  if (m_OnChunkAvailable != nullptr)
114  {
115  m_OnChunkAvailable->Call(a_ChunkX, a_ChunkZ);
116  }
117 }
118 
119 
120 
121 
122 
124 {
125  if (m_OnAllChunksAvailable != nullptr)
126  {
127  // Call the callback:
128  m_OnAllChunksAvailable->Call();
129 
130  // Remove the callback references - they won't be needed anymore
131  m_OnChunkAvailable.reset();
132  m_OnAllChunksAvailable.reset();
133  }
134 
135  // Disable the ChunkStay by returning true
136  return true;
137 }
138 
139 
140 
141 
142 
144 {
145  // This object is no longer needed, delete it
146  delete this;
147 }
148 
149 
150 
151 
#define ASSERT(x)
Definition: Globals.h:276
void LOGWARNING(std::string_view a_Format, const Args &... args)
Definition: LoggerSimple.h:67
cLuaState::cCallbackPtr m_OnChunkAvailable
The Lua function to call in OnChunkAvailable.
Definition: LuaChunkStay.h:46
cLuaState::cCallbackPtr m_OnAllChunksAvailable
The Lua function to call in OnAllChunksAvailable.
Definition: LuaChunkStay.h:49
bool AddChunks(const cLuaState::cStackTable &a_ChunkCoords)
Adds chunks in the specified Lua table.
void AddChunkCoord(cLuaState &a_LuaState, int a_Index)
Adds a single chunk coord from the table at the top of the Lua stack.
virtual void OnChunkAvailable(int a_ChunkX, int a_ChunkZ) override
Called when a specific chunk become available.
virtual bool OnAllChunksAvailable(void) override
Called once all of the contained chunks are available.
void Enable(cChunkMap &a_ChunkMap, cLuaState::cCallbackPtr a_OnChunkAvailable, cLuaState::cCallbackPtr a_OnAllChunksAvailable)
Enables the ChunkStay for the specified chunkmap, with the specified Lua callbacks.
virtual void OnDisabled(void) override
Called by the ChunkMap when the ChunkStay is disabled.
Encapsulates a Lua state and provides some syntactic sugar for common operations.
Definition: LuaState.h:56
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
Represents a table on the Lua stack.
Definition: LuaState.h:516
cLuaState & GetLuaState(void) const
Definition: LuaState.h:533
void ForEachArrayElement(cFunctionRef< bool(cLuaState &a_LuaState, int a_Index)> a_ElementCallback) const
Iterates over all array elements in the table consecutively and calls the a_ElementCallback for each.
Definition: LuaState.cpp:386
void Enable(cChunkMap &a_ChunkMap)
Enables the ChunkStay on the specified chunkmap, causing it to load and generate chunks.
Definition: ChunkStay.cpp:80
cChunkCoordsVector m_Chunks
The list of chunks to lock from unloading.
Definition: ChunkStay.h:85