summaryrefslogtreecommitdiffstats
path: root/src/Bindings
diff options
context:
space:
mode:
Diffstat (limited to 'src/Bindings')
-rw-r--r--src/Bindings/DeprecatedBindings.cpp117
-rw-r--r--src/Bindings/DiffAPIDesc.lua11
-rw-r--r--src/Bindings/LuaState.cpp42
-rw-r--r--src/Bindings/LuaState.h2
-rw-r--r--src/Bindings/LuaWindow.cpp36
-rw-r--r--src/Bindings/LuaWindow.h12
-rw-r--r--src/Bindings/ManualBindings.cpp77
-rw-r--r--src/Bindings/Plugin.h1
-rw-r--r--src/Bindings/PluginLua.cpp10
-rw-r--r--src/Bindings/PluginLua.h1
-rw-r--r--src/Bindings/PluginManager.cpp19
-rw-r--r--src/Bindings/PluginManager.h3
12 files changed, 316 insertions, 15 deletions
diff --git a/src/Bindings/DeprecatedBindings.cpp b/src/Bindings/DeprecatedBindings.cpp
index bb59fca7c..3ae1fd990 100644
--- a/src/Bindings/DeprecatedBindings.cpp
+++ b/src/Bindings/DeprecatedBindings.cpp
@@ -291,6 +291,114 @@ tolua_lerror:
+static int tolua_cBlockInfo_GetPlaceSound(lua_State * tolua_S)
+{
+ cLuaState L(tolua_S);
+ if (
+ !L.CheckParamStaticSelf("cBlockInfo") ||
+ !L.CheckParamNumber(2)
+ )
+ {
+ return 0;
+ }
+
+ L.Push("");
+ LOGWARNING("cBlockInfo:GetPlaceSound() is deprecated");
+ L.LogStackTrace(0);
+ return 1;
+}
+
+
+
+
+
+static int tolua_get_cBlockInfo_m_PlaceSound(lua_State * tolua_S)
+{
+ cLuaState L(tolua_S);
+ if (!L.CheckParamSelf("const cBlockInfo"))
+ {
+ return 0;
+ }
+
+ L.Push("");
+ LOGWARNING("cBlockInfo.m_PlaceSound is deprecated");
+ L.LogStackTrace(0);
+ return 1;
+}
+
+
+
+
+
+static int tolua_set_cBlockInfo_m_PlaceSound(lua_State * tolua_S)
+{
+ cLuaState L(tolua_S);
+ if (!L.CheckParamSelf("cBlockInfo"))
+ {
+ return 0;
+ }
+
+ LOGWARNING("cBlockInfo.m_PlaceSound is deprecated");
+ L.LogStackTrace(0);
+ return 0;
+}
+
+
+
+
+
+static int tolua_get_cItem_m_Lore(lua_State * tolua_S)
+{
+ // Maintain legacy m_Lore variable as Lore table split by ` (grave-accent)
+ cLuaState L(tolua_S);
+ if (!L.CheckParamSelf("const cItem"))
+ {
+ return 0;
+ }
+
+ const cItem * Self = nullptr;
+ L.GetStackValue(1, Self);
+
+ AString LoreString = StringJoin(Self->m_LoreTable, "`");
+
+ L.Push(LoreString);
+
+ LOGWARNING("cItem.m_Lore is deprecated, use cItem.m_LoreTable instead");
+ L.LogStackTrace(0);
+ return 1;
+}
+
+
+
+
+
+static int tolua_set_cItem_m_Lore(lua_State * tolua_S)
+{
+ // Maintain legacy m_Lore variable as Lore table split by ` (grave-accent)
+ cLuaState L(tolua_S);
+ if (
+ !L.CheckParamSelf("cItem") ||
+ !L.CheckParamString(2)
+ )
+ {
+ return 0;
+ }
+
+ cItem * Self = nullptr;
+ AString LoreString;
+ L.GetStackValues(1, Self, LoreString);
+
+ Self->m_LoreTable = StringSplit(LoreString, "`");
+
+ LOGWARNING("cItem.m_Lore is deprecated, use cItem.m_LoreTable instead");
+ L.LogStackTrace(0);
+ return 0;
+}
+
+
+
+
+
/* method: Trace of class cTracer */
static int tolua_cTracer_Trace(lua_State * a_LuaState)
{
@@ -439,6 +547,15 @@ void DeprecatedBindings::Bind(lua_State * tolua_S)
tolua_function(tolua_S, "StringToMobType", tolua_AllToLua_StringToMobType00);
+ tolua_beginmodule(tolua_S, "cBlockInfo");
+ tolua_function(tolua_S, "GetPlaceSound", tolua_cBlockInfo_GetPlaceSound);
+ tolua_variable(tolua_S, "m_PlaceSound", tolua_get_cBlockInfo_m_PlaceSound, tolua_set_cBlockInfo_m_PlaceSound);
+ tolua_endmodule(tolua_S);
+
+ tolua_beginmodule(tolua_S, "cItem");
+ tolua_variable(tolua_S, "m_Lore", tolua_get_cItem_m_Lore, tolua_set_cItem_m_Lore);
+ tolua_endmodule(tolua_S);
+
tolua_beginmodule(tolua_S, "cTracer");
tolua_function(tolua_S, "Trace", tolua_cTracer_Trace);
tolua_endmodule(tolua_S);
diff --git a/src/Bindings/DiffAPIDesc.lua b/src/Bindings/DiffAPIDesc.lua
index 8b8c340e2..54d379356 100644
--- a/src/Bindings/DiffAPIDesc.lua
+++ b/src/Bindings/DiffAPIDesc.lua
@@ -129,14 +129,17 @@ end
-- a_FunctionDoc is a single documentation item for a function, as loaded from ToLua++'s parser
local function functionDescMatchesDocs(a_FunctionDesc, a_FunctionDoc)
-- Check the number of parameters:
- local numParams
+ local numParams = 0
local numOptionalParams = 0
if (not(a_FunctionDesc.Params) or (a_FunctionDesc.Params == "")) then
numParams = 0
else
- _, numParams = string.gsub(a_FunctionDesc.Params, ",", "")
- numParams = numParams + 1
- _, numOptionalParams = string.gsub(a_FunctionDesc.Params, "%b[]", "")
+ for _, Param in pairs(a_FunctionDesc.Params) do
+ numParams = numParams + 1
+ if Param.IsOptional then
+ numOptionalParams = numOptionalParams + 1
+ end
+ end
end
local numDocParams = #(a_FunctionDoc.Params)
if ((numDocParams > numParams) or (numDocParams < numParams - numOptionalParams)) then
diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp
index e30d0ed5f..185759acc 100644
--- a/src/Bindings/LuaState.cpp
+++ b/src/Bindings/LuaState.cpp
@@ -875,6 +875,17 @@ void cLuaState::Push(const char * a_Value)
+void cLuaState::Push(const cItem & a_Item)
+{
+ ASSERT(IsValid());
+ auto c = new cItem(a_Item);
+ tolua_pushusertype_and_takeownership(m_LuaState, c, "cItem");
+}
+
+
+
+
+
void cLuaState::Push(const cNil & a_Nil)
{
ASSERT(IsValid());
@@ -1140,6 +1151,37 @@ bool cLuaState::GetStackValue(int a_StackPos, AStringMap & a_Value)
+bool cLuaState::GetStackValue(int a_StackPos, AStringVector & a_Value)
+{
+ // Retrieve all values in an array of string table:
+ if (!lua_istable(m_LuaState, a_StackPos))
+ {
+ return false;
+ }
+ cStackTable tbl(*this, a_StackPos);
+ bool isValid = true;
+ tbl.ForEachArrayElement([&](cLuaState & a_LuaState, int a_Index)
+ {
+ AString tempStr;
+ if (a_LuaState.GetStackValue(-1, tempStr))
+ {
+ a_Value.push_back(std::move(tempStr));
+ }
+ else
+ {
+ isValid = false;
+ return true;
+ }
+ return false;
+ }
+ );
+ return isValid;
+}
+
+
+
+
+
bool cLuaState::GetStackValue(int a_StackPos, bool & a_ReturnedVal)
{
a_ReturnedVal = (tolua_toboolean(m_LuaState, a_StackPos, a_ReturnedVal ? 1 : 0) > 0);
diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h
index 3d5b1e645..ffcddcfe8 100644
--- a/src/Bindings/LuaState.h
+++ b/src/Bindings/LuaState.h
@@ -614,6 +614,7 @@ public:
void Push(const AStringMap & a_Dictionary);
void Push(const AStringVector & a_Vector);
void Push(const char * a_Value);
+ void Push(const cItem & a_Item);
void Push(const cNil & a_Nil);
void Push(const cRef & a_Ref);
void Push(const Vector3d & a_Vector);
@@ -639,6 +640,7 @@ public:
// Enum values are checked for their allowed values and fail if the value is not assigned.
bool GetStackValue(int a_StackPos, AString & a_Value);
bool GetStackValue(int a_StackPos, AStringMap & a_Value);
+ bool GetStackValue(int a_StackPos, AStringVector & a_Value);
bool GetStackValue(int a_StackPos, bool & a_Value);
bool GetStackValue(int a_StackPos, cCallback & a_Callback);
bool GetStackValue(int a_StackPos, cCallbackPtr & a_Callback);
diff --git a/src/Bindings/LuaWindow.cpp b/src/Bindings/LuaWindow.cpp
index fd714390e..2802c69db 100644
--- a/src/Bindings/LuaWindow.cpp
+++ b/src/Bindings/LuaWindow.cpp
@@ -9,7 +9,7 @@
#include "PluginLua.h"
#include "Root.h"
#include "lua/src/lauxlib.h" // Needed for LUA_REFNIL
-
+#include "ClientHandle.h"
@@ -86,6 +86,19 @@ cLuaWindow::~cLuaWindow()
+void cLuaWindow::SetOnClicked(cLuaState::cCallbackPtr && a_OnClicked)
+{
+ // Only one Lua state can be a cLuaWindow object callback:
+ ASSERT(a_OnClicked->IsSameLuaState(*m_LuaState));
+
+ // Store the new reference, releasing the old one if appropriate:
+ m_OnClicked = std::move(a_OnClicked);
+}
+
+
+
+
+
void cLuaWindow::SetOnClosing(cLuaState::cCallbackPtr && a_OnClosing)
{
// Only one Lua state can be a cLuaWindow object callback:
@@ -206,3 +219,24 @@ void cLuaWindow::OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum)
+
+void cLuaWindow::Clicked(cPlayer & a_Player, int a_WindowID, short a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem)
+{
+ if (m_OnClicked != nullptr)
+ {
+ // Plugin can stop a click
+ if (m_OnClicked->Call(this, &a_Player, a_SlotNum, a_ClickAction, a_ClickedItem))
+ {
+ // Tell the client the actual state of the window
+ a_Player.GetClientHandle()->SendInventorySlot(-1, -1, a_Player.GetDraggingItem());
+ BroadcastWholeWindow();
+ return;
+ }
+ }
+
+ cWindow::Clicked(a_Player, a_WindowID, a_SlotNum, a_ClickAction, a_ClickedItem);
+}
+
+
+
+
diff --git a/src/Bindings/LuaWindow.h b/src/Bindings/LuaWindow.h
index fb21c1c4e..8f3349d00 100644
--- a/src/Bindings/LuaWindow.h
+++ b/src/Bindings/LuaWindow.h
@@ -49,6 +49,10 @@ public:
// tolua_end
+ /** Sets the Lua callback to call when the player clicks on the window.
+ The window can stop the click from propogating. */
+ void SetOnClicked(cLuaState::cCallbackPtr && a_OnClicked);
+
/** Sets the Lua callback function to call when the window is about to close */
void SetOnClosing(cLuaState::cCallbackPtr && a_OnClosing);
@@ -63,6 +67,9 @@ protected:
/** The canon Lua state that has opened the window and owns the m_LuaRef */
cLuaState * m_LuaState;
+ /** The Lua callback to call when the player clicked on a slot */
+ cLuaState::cCallbackPtr m_OnClicked;
+
/** The Lua callback to call when the window is closing for any player */
cLuaState::cCallbackPtr m_OnClosing;
@@ -80,6 +87,11 @@ protected:
// cWindow overrides:
virtual void OpenedByPlayer(cPlayer & a_Player) override;
+ virtual void Clicked(
+ cPlayer & a_Player, int a_WindowID,
+ short a_SlotNum, eClickAction a_ClickAction,
+ const cItem & a_ClickedItem
+ ) override;
virtual bool ClosedByPlayer(cPlayer & a_Player, bool a_CanRefuse) override;
virtual void Destroy(void) override;
virtual void DistributeStack(cItem & a_ItemStack, int a_Slot, cPlayer & a_Player, cSlotArea * a_ClickedArea, bool a_ShouldApply) override;
diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp
index e4410dd14..6fe133e1e 100644
--- a/src/Bindings/ManualBindings.cpp
+++ b/src/Bindings/ManualBindings.cpp
@@ -11,11 +11,6 @@
#include "PluginLua.h"
#include "PluginManager.h"
#include "LuaWindow.h"
-#include "../Root.h"
-#include "../World.h"
-#include "../Entities/Player.h"
-#include "../WebAdmin.h"
-#include "../ClientHandle.h"
#include "../BlockArea.h"
#include "../BlockEntities/BeaconEntity.h"
#include "../BlockEntities/BrewingstandEntity.h"
@@ -28,14 +23,20 @@
#include "../BlockEntities/NoteEntity.h"
#include "../BlockEntities/MobHeadEntity.h"
#include "../BlockEntities/FlowerPotEntity.h"
+#include "../BoundingBox.h"
+#include "../BuildInfo.h"
+#include "../ClientHandle.h"
+#include "../CommandOutput.h"
+#include "../CompositeChat.h"
+#include "../Entities/Player.h"
#include "../Generating/ChunkDesc.h"
+#include "../HTTP/UrlParser.h"
+#include "../Item.h"
#include "../LineBlockTracer.h"
-#include "../CompositeChat.h"
+#include "../Root.h"
#include "../StringCompression.h"
-#include "../CommandOutput.h"
-#include "../BuildInfo.h"
-#include "../HTTP/UrlParser.h"
-#include "../BoundingBox.h"
+#include "../WebAdmin.h"
+#include "../World.h"
@@ -2557,6 +2558,57 @@ static int tolua_cMojangAPI_MakeUUIDShort(lua_State * L)
+static int tolua_get_cItem_m_LoreTable(lua_State * tolua_S)
+{
+ // Check params:
+ cLuaState L(tolua_S);
+ if (!L.CheckParamSelf("const cItem"))
+ {
+ return 0;
+ }
+
+ // Get the params:
+ const cItem * Self = nullptr;
+ L.GetStackValue(1, Self);
+
+ // Push the result:
+ L.Push(Self->m_LoreTable);
+ return 1;
+}
+
+
+
+
+
+static int tolua_set_cItem_m_LoreTable(lua_State * tolua_S)
+{
+ // Check params:
+ cLuaState L(tolua_S);
+ if (
+ !L.CheckParamSelf("cItem") ||
+ !L.CheckParamTable(2)
+ )
+ {
+ return 0;
+ }
+
+ // Get the params:
+ cItem * Self = nullptr;
+ L.GetStackValue(1, Self);
+
+ // Set the value:
+ Self->m_LoreTable.clear();
+ if (!L.GetStackValue(2, Self->m_LoreTable))
+ {
+ return L.ApiParamError("cItem.m_LoreTable: Could not read value as an array of strings");
+ }
+ return 0;
+}
+
+
+
+
+
static int Lua_ItemGrid_GetSlotCoords(lua_State * L)
{
tolua_Error tolua_err;
@@ -3798,6 +3850,10 @@ void cManualBindings::Bind(lua_State * tolua_S)
tolua_function(tolua_S, "GetOutputBlockPos", tolua_cHopperEntity_GetOutputBlockPos);
tolua_endmodule(tolua_S);
+ tolua_beginmodule(tolua_S, "cItem");
+ tolua_variable(tolua_S, "m_LoreTable", tolua_get_cItem_m_LoreTable, tolua_set_cItem_m_LoreTable);
+ tolua_endmodule(tolua_S);
+
tolua_beginmodule(tolua_S, "cItemGrid");
tolua_function(tolua_S, "GetSlotCoords", Lua_ItemGrid_GetSlotCoords);
tolua_endmodule(tolua_S);
@@ -3816,6 +3872,7 @@ void cManualBindings::Bind(lua_State * tolua_S)
tolua_function(tolua_S, "new", tolua_cLuaWindow_new);
tolua_function(tolua_S, "new_local", tolua_cLuaWindow_new_local);
tolua_function(tolua_S, ".call", tolua_cLuaWindow_new_local);
+ tolua_function(tolua_S, "SetOnClicked", tolua_SetObjectCallback<cLuaWindow, &cLuaWindow::SetOnClicked>);
tolua_function(tolua_S, "SetOnClosing", tolua_SetObjectCallback<cLuaWindow, &cLuaWindow::SetOnClosing>);
tolua_function(tolua_S, "SetOnSlotChanged", tolua_SetObjectCallback<cLuaWindow, &cLuaWindow::SetOnSlotChanged>);
tolua_endmodule(tolua_S);
diff --git a/src/Bindings/Plugin.h b/src/Bindings/Plugin.h
index 3276fde67..22e8f15e2 100644
--- a/src/Bindings/Plugin.h
+++ b/src/Bindings/Plugin.h
@@ -80,6 +80,7 @@ public:
virtual bool OnPlayerJoined (cPlayer & a_Player) = 0;
virtual bool OnPlayerLeftClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status) = 0;
virtual bool OnPlayerMoving (cPlayer & a_Player, const Vector3d & a_OldPosition, const Vector3d & a_NewPosition) = 0;
+ virtual bool OnPlayerOpeningWindow(cPlayer & a_Player, cWindow & a_Window) = 0;
virtual bool OnPlayerPlacedBlock (cPlayer & a_Player, const sSetBlock & a_BlockChange) = 0;
virtual bool OnPlayerPlacingBlock (cPlayer & a_Player, const sSetBlock & a_BlockChange) = 0;
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) = 0;
diff --git a/src/Bindings/PluginLua.cpp b/src/Bindings/PluginLua.cpp
index e3aa63aa1..5af336a95 100644
--- a/src/Bindings/PluginLua.cpp
+++ b/src/Bindings/PluginLua.cpp
@@ -659,6 +659,15 @@ bool cPluginLua::OnEntityTeleport(cEntity & a_Entity, const Vector3d & a_OldPosi
+bool cPluginLua::OnPlayerOpeningWindow(cPlayer & a_Player, cWindow & a_Window)
+{
+ return CallSimpleHooks(cPluginManager::HOOK_PLAYER_OPENING_WINDOW, &a_Player, &a_Window);
+}
+
+
+
+
+
bool cPluginLua::OnPlayerPlacedBlock(cPlayer & a_Player, const sSetBlock & a_BlockChange)
{
return CallSimpleHooks(cPluginManager::HOOK_PLAYER_PLACED_BLOCK,
@@ -1056,6 +1065,7 @@ const char * cPluginLua::GetHookFnName(int a_HookType)
case cPluginManager::HOOK_PLAYER_JOINED: return "OnPlayerJoined";
case cPluginManager::HOOK_PLAYER_LEFT_CLICK: return "OnPlayerLeftClick";
case cPluginManager::HOOK_PLAYER_MOVING: return "OnPlayerMoving";
+ case cPluginManager::HOOK_PLAYER_OPENING_WINDOW: return "OnPlayerOpeningWindow";
case cPluginManager::HOOK_PLAYER_PLACED_BLOCK: return "OnPlayerPlacedBlock";
case cPluginManager::HOOK_PLAYER_PLACING_BLOCK: return "OnPlayerPlacingBlock";
case cPluginManager::HOOK_PLAYER_RIGHT_CLICK: return "OnPlayerRightClick";
diff --git a/src/Bindings/PluginLua.h b/src/Bindings/PluginLua.h
index ff5e8d726..4de5751e7 100644
--- a/src/Bindings/PluginLua.h
+++ b/src/Bindings/PluginLua.h
@@ -101,6 +101,7 @@ public:
virtual bool OnPlayerJoined (cPlayer & a_Player) override;
virtual bool OnPlayerLeftClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status) override;
virtual bool OnPlayerMoving (cPlayer & a_Player, const Vector3d & a_OldPosition, const Vector3d & a_NewPosition) override;
+ virtual bool OnPlayerOpeningWindow(cPlayer & a_Player, cWindow & a_Window) override;
virtual bool OnPlayerPlacedBlock (cPlayer & a_Player, const sSetBlock & a_BlockChange) override;
virtual bool OnPlayerPlacingBlock (cPlayer & a_Player, const sSetBlock & a_BlockChange) override;
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;
diff --git a/src/Bindings/PluginManager.cpp b/src/Bindings/PluginManager.cpp
index 066ccf9d7..1d977fcde 100644
--- a/src/Bindings/PluginManager.cpp
+++ b/src/Bindings/PluginManager.cpp
@@ -999,6 +999,25 @@ bool cPluginManager::CallHookPlayerMoving(cPlayer & a_Player, const Vector3d & a
+bool cPluginManager::CallHookPlayerOpeningWindow(cPlayer & a_Player, cWindow & a_Window)
+{
+ FIND_HOOK(HOOK_PLAYER_OPENING_WINDOW);
+ VERIFY_HOOK;
+
+ for (PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr)
+ {
+ if ((*itr)->OnPlayerOpeningWindow(a_Player, a_Window))
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
+
+
+
+
bool cPluginManager::CallHookPlayerPlacedBlock(cPlayer & a_Player, const sSetBlock & a_BlockChange)
{
FIND_HOOK(HOOK_PLAYER_PLACED_BLOCK);
diff --git a/src/Bindings/PluginManager.h b/src/Bindings/PluginManager.h
index f38ac8fa1..f3fc3551a 100644
--- a/src/Bindings/PluginManager.h
+++ b/src/Bindings/PluginManager.h
@@ -24,6 +24,7 @@ class cPickup;
class cPlayer;
class cPlugin;
class cProjectileEntity;
+class cWindow;
class cWorld;
class cSettingsRepositoryInterface;
class cDeadlockDetect;
@@ -111,6 +112,7 @@ public:
HOOK_PLAYER_JOINED,
HOOK_PLAYER_LEFT_CLICK,
HOOK_PLAYER_MOVING,
+ HOOK_PLAYER_OPENING_WINDOW,
HOOK_PLAYER_PLACED_BLOCK,
HOOK_PLAYER_PLACING_BLOCK,
HOOK_PLAYER_RIGHT_CLICK,
@@ -257,6 +259,7 @@ public:
bool CallHookPlayerJoined (cPlayer & a_Player);
bool CallHookPlayerLeftClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status);
bool CallHookPlayerMoving (cPlayer & a_Player, const Vector3d & a_OldPosition, const Vector3d & a_NewPosition);
+ bool CallHookPlayerOpeningWindow (cPlayer & a_Player, cWindow & a_Window);
bool CallHookPlayerPlacedBlock (cPlayer & a_Player, const sSetBlock & a_BlockChange);
bool CallHookPlayerPlacingBlock (cPlayer & a_Player, const sSetBlock & a_BlockChange);
bool CallHookPlayerRightClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ);