From 748b121703fa28b10933f4432c09391e66179118 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 28 Mar 2021 14:40:57 +0100 Subject: Unify DoWithBlockEntity (#5168) + DoWith calls now broadcast the block entity and mark the chunk dirty + Add block entity change queue to synchronise BE updates with block updates * Fixed a few incorrect assertions about BE type - Remove manual overloads --- src/Bindings/ManualBindings_World.cpp | 291 +++++++++++++++++++++++++++++++--- 1 file changed, 266 insertions(+), 25 deletions(-) (limited to 'src/Bindings/ManualBindings_World.cpp') diff --git a/src/Bindings/ManualBindings_World.cpp b/src/Bindings/ManualBindings_World.cpp index 0a748c55d..db797483d 100644 --- a/src/Bindings/ManualBindings_World.cpp +++ b/src/Bindings/ManualBindings_World.cpp @@ -12,6 +12,23 @@ #include "PluginLua.h" #include "LuaChunkStay.h" +#include "BlockEntities/BeaconEntity.h" +#include "BlockEntities/BedEntity.h" +#include "BlockEntities/BrewingstandEntity.h" +#include "BlockEntities/ChestEntity.h" +#include "BlockEntities/CommandBlockEntity.h" +#include "BlockEntities/DispenserEntity.h" +#include "BlockEntities/DropSpenserEntity.h" +#include "BlockEntities/DropperEntity.h" +#include "BlockEntities/FlowerPotEntity.h" +#include "BlockEntities/FurnaceEntity.h" +#include "BlockEntities/HopperEntity.h" +#include "BlockEntities/MobHeadEntity.h" +#include "BlockEntities/NoteEntity.h" + + + + /** Check that a Lua parameter is either a vector or 3 numbers in sequence \param L The Lua state @@ -50,6 +67,183 @@ static bool GetStackVectorOr3Numbers(cLuaState & L, int a_Index, Vector3 & a_ +/** Template for the bindings for the DoWithXYZAt(X, Y, Z) functions that don't need to check their coords. */ +template +static int DoWithBlockEntityAt(lua_State * tolua_S) +{ + cLuaState L(tolua_S); + int OffsetIndex; + + // Check params: + if ( + !L.CheckParamSelf("cWorld") || + !CheckParamVectorOr3Numbers(L, "Vector3", 2, OffsetIndex) || + !L.CheckParamFunction(OffsetIndex) || + !L.CheckParamEnd(OffsetIndex + 1) + ) + { + return 0; + } + + cWorld * Self = nullptr; + Vector3i Position; + cLuaState::cRef FnRef; + + // Get parameters: + if ( + !L.GetStackValues(1, Self) || + !GetStackVectorOr3Numbers(L, 2, Position) || + !L.GetStackValues(OffsetIndex, FnRef) + ) + { + return 0; + } + + if (Self == nullptr) + { + return L.ApiParamError("Invalid 'self'"); + } + if (!FnRef.IsValid()) + { + return L.ApiParamError("Expected a valid callback function for parameter %i", OffsetIndex); + } + + // Call the DoWith function: + bool res = Self->DoWithBlockEntityAt(Position, [&L, &FnRef](cBlockEntity & a_BlockEntity) + { + if constexpr (sizeof...(BlockTypes) != 0) + { + if (((a_BlockEntity.GetBlockType() != BlockTypes) && ...)) + { + return false; + } + } + + bool ret = false; + L.Call(FnRef, static_cast(&a_BlockEntity), cLuaState::Return, ret); + return ret; + }); + + // Push the result as the return value: + L.Push(res); + return 1; +} + + + + + +template < + class Ty1, + class Ty2, + bool (Ty1::*ForEachFn)(const cBoundingBox &, cFunctionRef) +> +static int ForEachInBox(lua_State * tolua_S) +{ + // Check params: + cLuaState L(tolua_S); + if ( + !L.CheckParamUserType(1, "cWorld") || + !L.CheckParamUserType(2, "cBoundingBox") || + !L.CheckParamFunction(3) || + !L.CheckParamEnd(4) + ) + { + return 0; + } + + // Get the params: + Ty1 * Self = nullptr; + cBoundingBox * Box = nullptr; + cLuaState::cRef FnRef; + L.GetStackValues(1, Self, Box, FnRef); + if ((Self == nullptr) || (Box == nullptr)) + { + return L.ApiParamError("Invalid world (%p) or boundingbox (%p)", static_cast(Self), static_cast(Box)); + } + if (!FnRef.IsValid()) + { + return L.ApiParamError("Expected a valid callback function for parameter #2"); + } + + bool res = (Self->*ForEachFn)(*Box, [&](Ty2 & a_Item) + { + bool ret = false; + if (!L.Call(FnRef, &a_Item, cLuaState::Return, ret)) + { + LOGWARNING("Failed to call Lua callback"); + L.LogStackTrace(); + return true; // Abort enumeration + } + + return ret; + } + ); + + // Push the result as the return value: + L.Push(res); + return 1; +} + + + + + +template +static int ForEachBlockEntityInChunk(lua_State * tolua_S) +{ + // Check params: + cLuaState L(tolua_S); + if ( + !L.CheckParamSelf("cWorld") || + !L.CheckParamNumber(2, 3) || + !L.CheckParamFunction(4) || + !L.CheckParamEnd(5) + ) + { + return 0; + } + + // Get parameters: + cWorld * Self = nullptr; + int ChunkX = 0; + int ChunkZ = 0; + cLuaState::cRef FnRef; + L.GetStackValues(1, Self, ChunkX, ChunkZ, FnRef); + if (Self == nullptr) + { + return L.ApiParamError("Error in function call '#funcname#': Invalid 'self'"); + } + if (!FnRef.IsValid()) + { + return L.ApiParamError("Expected a valid callback function for parameter #4"); + } + + // Call the ForEach function: + bool res = Self->ForEachBlockEntityInChunk(ChunkX, ChunkZ, [&L, &FnRef](cBlockEntity & a_BlockEntity) + { + if constexpr (sizeof...(BlockTypes) != 0) + { + if (((a_BlockEntity.GetBlockType() != BlockTypes) && ...)) + { + return false; + } + } + + bool ret = false; + L.Call(FnRef, static_cast(&a_BlockEntity), cLuaState::Return, ret); + return ret; + }); + + // Push the result as the return value: + L.Push(res); + return 1; +} + + + + + static int tolua_cWorld_BroadcastBlockAction(lua_State * tolua_S) { /* Function signature: @@ -566,6 +760,53 @@ static int tolua_cWorld_FastSetBlock(lua_State * tolua_S) +static int tolua_cWorld_ForEachEntityInChunk(lua_State * tolua_S) +{ + // Check params: + cLuaState L(tolua_S); + if ( + !L.CheckParamUserType(1, "cWorld") || + !L.CheckParamNumber(2, 3) || + !L.CheckParamFunction(4) || + !L.CheckParamEnd(5) + ) + { + return 0; + } + + // Get parameters: + cWorld * Self = nullptr; + int ChunkX = 0; + int ChunkZ = 0; + cLuaState::cRef FnRef; + L.GetStackValues(1, Self, ChunkX, ChunkZ, FnRef); + if (Self == nullptr) + { + return L.ApiParamError("Invalid 'self'"); + } + if (!FnRef.IsValid()) + { + return L.ApiParamError("Expected a valid callback function for parameter #4"); + } + + // Call the DoWith function: + bool res = Self->ForEachEntityInChunk(ChunkX, ChunkZ, [&](cEntity & a_Item) + { + bool ret = false; + L.Call(FnRef, &a_Item, cLuaState::Return, ret); + return ret; + } + ); + + // Push the result as the return value: + L.Push(res); + return 1; +} + + + + + static int tolua_cWorld_ForEachLoadedChunk(lua_State * tolua_S) { // Exported manually, because tolua doesn't support converting functions to functor types. @@ -1347,35 +1588,35 @@ void cManualBindings::BindWorld(lua_State * tolua_S) tolua_function(tolua_S, "BroadcastParticleEffect", tolua_cWorld_BroadcastParticleEffect); tolua_function(tolua_S, "ChunkStay", tolua_cWorld_ChunkStay); tolua_function(tolua_S, "DoExplosionAt", tolua_cWorld_DoExplosionAt); - tolua_function(tolua_S, "DoWithBeaconAt", DoWithXYZ); - tolua_function(tolua_S, "DoWithBedAt", DoWithXYZ); - tolua_function(tolua_S, "DoWithBlockEntityAt", DoWithXYZ); - tolua_function(tolua_S, "DoWithBrewingstandAt", DoWithXYZ); - tolua_function(tolua_S, "DoWithChestAt", DoWithXYZ); - tolua_function(tolua_S, "DoWithCommandBlockAt", DoWithXYZ); - tolua_function(tolua_S, "DoWithDispenserAt", DoWithXYZ); - tolua_function(tolua_S, "DoWithDropSpenserAt", DoWithXYZ); - tolua_function(tolua_S, "DoWithDropperAt", DoWithXYZ); - tolua_function(tolua_S, "DoWithEntityByID", DoWithID< cWorld, cEntity, &cWorld::DoWithEntityByID>); - tolua_function(tolua_S, "DoWithFlowerPotAt", DoWithXYZ); - tolua_function(tolua_S, "DoWithFurnaceAt", DoWithXYZ); - tolua_function(tolua_S, "DoWithHopperAt", DoWithXYZ); - tolua_function(tolua_S, "DoWithMobHeadAt", DoWithXYZ); + tolua_function(tolua_S, "DoWithBeaconAt", DoWithBlockEntityAt); + tolua_function(tolua_S, "DoWithBedAt", DoWithBlockEntityAt); + tolua_function(tolua_S, "DoWithBlockEntityAt", DoWithBlockEntityAt); + tolua_function(tolua_S, "DoWithBrewingstandAt", DoWithBlockEntityAt); + tolua_function(tolua_S, "DoWithChestAt", DoWithBlockEntityAt); + tolua_function(tolua_S, "DoWithCommandBlockAt", DoWithBlockEntityAt); + tolua_function(tolua_S, "DoWithDispenserAt", DoWithBlockEntityAt); + tolua_function(tolua_S, "DoWithDropSpenserAt", DoWithBlockEntityAt); + tolua_function(tolua_S, "DoWithDropperAt", DoWithBlockEntityAt); + tolua_function(tolua_S, "DoWithEntityByID", DoWithID); + tolua_function(tolua_S, "DoWithFlowerPotAt", DoWithBlockEntityAt); + tolua_function(tolua_S, "DoWithFurnaceAt", DoWithBlockEntityAt); + tolua_function(tolua_S, "DoWithHopperAt", DoWithBlockEntityAt); + tolua_function(tolua_S, "DoWithMobHeadAt", DoWithBlockEntityAt); tolua_function(tolua_S, "DoWithNearestPlayer", tolua_cWorld_DoWithNearestPlayer); - tolua_function(tolua_S, "DoWithNoteBlockAt", DoWithXYZ); - tolua_function(tolua_S, "DoWithPlayer", DoWith< cWorld, cPlayer, &cWorld::DoWithPlayer>); + tolua_function(tolua_S, "DoWithNoteBlockAt", DoWithBlockEntityAt); + tolua_function(tolua_S, "DoWithPlayer", DoWith); tolua_function(tolua_S, "DoWithPlayerByUUID", tolua_cWorld_DoWithPlayerByUUID); tolua_function(tolua_S, "FastSetBlock", tolua_cWorld_FastSetBlock); - tolua_function(tolua_S, "FindAndDoWithPlayer", DoWith< cWorld, cPlayer, &cWorld::FindAndDoWithPlayer>); - tolua_function(tolua_S, "ForEachBlockEntityInChunk", ForEachInChunk); - tolua_function(tolua_S, "ForEachBrewingstandInChunk", ForEachInChunk); - tolua_function(tolua_S, "ForEachChestInChunk", ForEachInChunk); - tolua_function(tolua_S, "ForEachEntity", ForEach< cWorld, cEntity, &cWorld::ForEachEntity>); - tolua_function(tolua_S, "ForEachEntityInBox", ForEachInBox< cWorld, cEntity, &cWorld::ForEachEntityInBox>); - tolua_function(tolua_S, "ForEachEntityInChunk", ForEachInChunk); - tolua_function(tolua_S, "ForEachFurnaceInChunk", ForEachInChunk); + tolua_function(tolua_S, "FindAndDoWithPlayer", DoWith); + tolua_function(tolua_S, "ForEachBlockEntityInChunk", ForEachBlockEntityInChunk); + tolua_function(tolua_S, "ForEachBrewingstandInChunk", ForEachBlockEntityInChunk); + tolua_function(tolua_S, "ForEachChestInChunk", ForEachBlockEntityInChunk); + tolua_function(tolua_S, "ForEachEntity", ForEach); + tolua_function(tolua_S, "ForEachEntityInBox", ForEachInBox); + tolua_function(tolua_S, "ForEachEntityInChunk", tolua_cWorld_ForEachEntityInChunk); + tolua_function(tolua_S, "ForEachFurnaceInChunk", ForEachBlockEntityInChunk); tolua_function(tolua_S, "ForEachLoadedChunk", tolua_cWorld_ForEachLoadedChunk); - tolua_function(tolua_S, "ForEachPlayer", ForEach< cWorld, cPlayer, &cWorld::ForEachPlayer>); + tolua_function(tolua_S, "ForEachPlayer", ForEach); tolua_function(tolua_S, "GetBlock", tolua_cWorld_GetBlock); tolua_function(tolua_S, "GetBlockBlockLight", tolua_cWorld_GetBlockBlockLight); tolua_function(tolua_S, "GetBlockInfo", tolua_cWorld_GetBlockInfo); -- cgit v1.2.3