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/ManualBindings.cpp | |
parent | Added a new param to OnPlayerEditingBook and changed the names (diff) | |
download | cuberite-a5bae9f2f37921bfcf6203282155a8779c818120.tar cuberite-a5bae9f2f37921bfcf6203282155a8779c818120.tar.gz cuberite-a5bae9f2f37921bfcf6203282155a8779c818120.tar.bz2 cuberite-a5bae9f2f37921bfcf6203282155a8779c818120.tar.lz cuberite-a5bae9f2f37921bfcf6203282155a8779c818120.tar.xz cuberite-a5bae9f2f37921bfcf6203282155a8779c818120.tar.zst cuberite-a5bae9f2f37921bfcf6203282155a8779c818120.zip |
Diffstat (limited to 'src/Bindings/ManualBindings.cpp')
-rw-r--r-- | src/Bindings/ManualBindings.cpp | 184 |
1 files changed, 176 insertions, 8 deletions
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); |