diff options
author | Lukas Pioch <lukas@zgow.de> | 2017-08-23 18:09:47 +0200 |
---|---|---|
committer | Lukas Pioch <lukas@zgow.de> | 2017-08-27 17:49:08 +0200 |
commit | a5bae9f2f37921bfcf6203282155a8779c818120 (patch) | |
tree | b51bcc42cdc65fd7d0d76fd312216ec0720c706a /src/Bindings | |
parent | Added a new param to OnPlayerEditingBook and changed the names (diff) | |
download | cuberite-books.tar cuberite-books.tar.gz cuberite-books.tar.bz2 cuberite-books.tar.lz cuberite-books.tar.xz cuberite-books.tar.zst cuberite-books.zip |
Diffstat (limited to '')
-rw-r--r-- | src/Bindings/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/Bindings/ManualBindings.cpp | 184 | ||||
-rw-r--r-- | src/Bindings/PluginManager.h | 2 |
3 files changed, 178 insertions, 10 deletions
diff --git a/src/Bindings/CMakeLists.txt b/src/Bindings/CMakeLists.txt index 7b1635a4a..15d629117 100644 --- a/src/Bindings/CMakeLists.txt +++ b/src/Bindings/CMakeLists.txt @@ -85,6 +85,7 @@ set(BINDING_DEPENDENCIES ../BlockEntities/FlowerPotEntity.h ../BlockID.h ../BlockInfo.h + ../BookContent.h ../BoundingBox.h ../ChatColor.h ../ChunkDef.h @@ -143,7 +144,6 @@ set(BINDING_DEPENDENCIES ../Vector3.h ../WebAdmin.h ../World.h - ../BookContent.h ) if (NOT MSVC) diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index 2a08e3e56..32711223d 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -3967,6 +3967,93 @@ static int tolua_cEntity_GetSpeed(lua_State * tolua_S) +static int tolua_cBookContent_AddPage(lua_State * tolua_S) +{ + // cBookContent:AddPage(string) + // cBookContent:AddPage(cCompositeChat) + + cLuaState L(tolua_S); + if ( + !L.CheckParamSelf("cBookContent") || + lua_isnil(L, 2) || + !L.CheckParamEnd(3) + ) + { + return 0; + } + + cBookContent * BookContent = nullptr; + L.GetStackValue(1, BookContent); + + // Check type of second param + if (L.GetTypeText(2) == "string") + { + AString Page; + L.GetStackValue(2, Page); + BookContent->AddPage(Page); + } + else if (L.CheckParamUserType(2, "cCompositeChat")) + { + cCompositeChat * CompositeChat = nullptr; + L.GetStackValue(2, CompositeChat); + if (BookContent->IsSigned()) + { + BookContent->AddPage(CompositeChat->CreateJsonString()); + } + else + { + BookContent->AddPage(CompositeChat->ExtractText()); + } + } + else + { + return L.ApiParamError("Expected a string or a cCompositeChat."); + } + return 0; +} + + + + + +static int tolua_cBookContent_GetPage(lua_State * tolua_S) +{ + // cBookContent::GetPage(Index) -> string + + cLuaState L(tolua_S); + if ( + !L.CheckParamSelf("cBookContent") || + !L.CheckParamNumber(2) || + !L.CheckParamEnd(3) + ) + { + return 0; + } + + cBookContent * BookContent = nullptr; + size_t Index; + L.GetStackValues(1, BookContent, Index); + + if (BookContent->GetPages().empty()) + { + return L.ApiParamError("Getting the page at the index is not possbile. The book has no pages."); + } + + // Check if the index is valid + if ((Index <= 0) || (Index > BookContent->GetPages().size())) + { + return L.ApiParamError("Index %d out of range. The valid range is %d - %d.", Index, 1, BookContent->GetPages().size()); + } + + L.Push(BookContent->GetPage(Index - 1)); + return 1; + +} + + + + + static int tolua_cBookContent_GetPages(lua_State * tolua_S) { // cBookContent::GetPages() -> table of strings @@ -3990,6 +4077,68 @@ static int tolua_cBookContent_GetPages(lua_State * tolua_S) +static int tolua_cBookContent_SetPage(lua_State * tolua_S) +{ + // cBookContent::SetPage(index, string) + // cBookContent::SetPage(index, cCompositeChat) + + cLuaState L(tolua_S); + if ( + !L.CheckParamSelf("cBookContent") || + !L.CheckParamNumber(2) || + lua_isnil(L, 3) || + !L.CheckParamEnd(4) + ) + { + return 0; + } + + cBookContent * BookContent = nullptr; + size_t Index; + L.GetStackValues(1, BookContent, Index); + + if (BookContent->GetPages().empty()) + { + return L.ApiParamError("Changing the page at the index is not possbile. The book has no pages."); + } + + // Check if the index is valid + if ((Index <= 0) || (Index > BookContent->GetPages().size())) + { + return L.ApiParamError("The index %d is out of range. The valid range is %d to %d.", Index, 1, BookContent->GetPages().size()); + } + + // Check type of third param + if (L.GetTypeText(3) == "string") + { + AString Page; + L.GetStackValue(3, Page); + BookContent->SetPage(Index - 1, Page); + } + else if (L.CheckParamUserType(3, "cCompositeChat")) + { + cCompositeChat * CompositeChat = nullptr; + L.GetStackValue(3, CompositeChat); + if (BookContent->IsSigned()) + { + BookContent->SetPage(Index - 1, CompositeChat->CreateJsonString()); + } + else + { + BookContent->SetPage(Index - 1, CompositeChat->ExtractText()); + } + } + else + { + return L.ApiParamError("Expected a string or a cCompositeChat."); + } + return 0; +} + + + + + static int tolua_cBookContent_SetPages(lua_State * tolua_S) { // cBookContent::SetPages(table) @@ -4010,9 +4159,25 @@ static int tolua_cBookContent_SetPages(lua_State * tolua_S) BookContent->ClearPages(); Pages->ForEachArrayElement([=](cLuaState & a_LuaState, int a_Index) -> bool { - AString Page; - a_LuaState.GetStackValue(-1, Page); - BookContent->AddPage(Page); + if (a_LuaState.GetTypeText(-1) == "string") + { + AString Page; + a_LuaState.GetStackValue(-1, Page); + BookContent->AddPage(Page); + } + else if (a_LuaState.CheckParamUserType(-1, "cCompositeChat")) + { + cCompositeChat * CompositeChat = nullptr; + a_LuaState.GetStackValue(-1, CompositeChat); + if (BookContent->IsSigned()) + { + BookContent->AddPage(CompositeChat->CreateJsonString()); + } + else + { + BookContent->AddPage(CompositeChat->ExtractText()); + } + } return false; } ); @@ -4051,6 +4216,14 @@ void cManualBindings::Bind(lua_State * tolua_S) tolua_function(tolua_S, "Base64Decode", tolua_Base64Decode); tolua_function(tolua_S, "md5", tolua_md5_obsolete); // OBSOLETE, use cCryptoHash.md5() instead + tolua_beginmodule(tolua_S, "cBookContent"); + tolua_function(tolua_S, "AddPage", tolua_cBookContent_AddPage); + tolua_function(tolua_S, "GetPage", tolua_cBookContent_GetPage); + tolua_function(tolua_S, "GetPages", tolua_cBookContent_GetPages); + tolua_function(tolua_S, "SetPage", tolua_cBookContent_SetPage); + tolua_function(tolua_S, "SetPages", tolua_cBookContent_SetPages); + tolua_endmodule(tolua_S); + tolua_beginmodule(tolua_S, "cBoundingBox"); tolua_function(tolua_S, "CalcLineIntersection", tolua_cBoundingBox_CalcLineIntersection); tolua_function(tolua_S, "Intersect", tolua_cBoundingBox_Intersect); @@ -4250,11 +4423,6 @@ void cManualBindings::Bind(lua_State * tolua_S) tolua_variable(tolua_S, "PostParams", tolua_get_HTTPRequest_PostParams, nullptr); tolua_endmodule(tolua_S); - tolua_beginmodule(tolua_S, "cBookContent"); - tolua_function(tolua_S, "GetPages", tolua_cBookContent_GetPages); - tolua_function(tolua_S, "SetPages", tolua_cBookContent_SetPages); - tolua_endmodule(tolua_S); - BindNetwork(tolua_S); BindRankManager(tolua_S); BindWorld(tolua_S); diff --git a/src/Bindings/PluginManager.h b/src/Bindings/PluginManager.h index d7bbf62c2..0c02404d2 100644 --- a/src/Bindings/PluginManager.h +++ b/src/Bindings/PluginManager.h @@ -248,7 +248,7 @@ public: bool CallHookHandshake (cClientHandle & a_ClientHandle, const AString & a_Username); bool CallHookHopperPullingItem (cWorld & a_World, cHopperEntity & a_Hopper, int a_DstSlotNum, cBlockEntityWithItems & a_SrcEntity, int a_SrcSlotNum); bool CallHookHopperPushingItem (cWorld & a_World, cHopperEntity & a_Hopper, int a_SrcSlotNum, cBlockEntityWithItems & a_DstEntity, int a_DstSlotNum); - bool CallHookKilled (cEntity & a_Victim, TakeDamageInfo & a_TDI, AString & a_DeathMessage); + bool CallHookKilled (cEntity & a_Victim, TakeDamageInfo & a_TDI, AString & a_DeathMessage); bool CallHookKilling (cEntity & a_Victim, cEntity * a_Killer, TakeDamageInfo & a_TDI); bool CallHookLogin (cClientHandle & a_Client, UInt32 a_ProtocolVersion, const AString & a_Username); bool CallHookPlayerAnimation (cPlayer & a_Player, int a_Animation); |