From ea386eaab12206e585e01720808dc37585569535 Mon Sep 17 00:00:00 2001 From: Bond-009 Date: Fri, 27 Mar 2020 13:03:28 +0100 Subject: Lock hopper when powered by redstone (#4347) * Lock hopper when powered by redstone * Add to manual bindings * Add hopper API documentation Co-authored-by: Mat --- Server/Plugins/APIDump/Classes/World.lua | 29 ++++++++++ src/Bindings/ManualBindings_World.cpp | 1 + src/BlockEntities/HopperEntity.cpp | 21 ++++++-- src/BlockEntities/HopperEntity.h | 8 +++ src/Chunk.cpp | 11 ++++ src/Chunk.h | 4 ++ src/ChunkMap.cpp | 18 +++++++ src/ChunkMap.h | 6 +++ .../IncrementalRedstoneSimulator/CMakeLists.txt | 1 + .../IncrementalRedstoneSimulator/HopperHandler.h | 62 ++++++++++++++++++++++ .../IncrementalRedstoneSimulator.cpp | 2 + src/World.cpp | 9 ++++ src/World.h | 4 ++ 13 files changed, 172 insertions(+), 4 deletions(-) create mode 100644 src/Simulator/IncrementalRedstoneSimulator/HopperHandler.h diff --git a/Server/Plugins/APIDump/Classes/World.lua b/Server/Plugins/APIDump/Classes/World.lua index eb0e0d17d..c35733b4a 100644 --- a/Server/Plugins/APIDump/Classes/World.lua +++ b/Server/Plugins/APIDump/Classes/World.lua @@ -939,6 +939,35 @@ function OnAllChunksAvailable() All return values from the callbacks are i }, Notes = "If there is a furnace at the specified coords, calls the CallbackFunction with the {{cFurnaceEntity}} parameter representing the furnace. The CallbackFunction has the following signature:
function Callback({{cFurnaceEntity|FurnaceEntity}})
The function returns false if there is no furnace, or if there is, it returns the bool value that the callback has returned.", }, + DoWithHopperAt = + { + Params = + { + { + Name = "BlockX", + Type = "number", + }, + { + Name = "BlockY", + Type = "number", + }, + { + Name = "BlockZ", + Type = "number", + }, + { + Name = "CallbackFunction", + Type = "function", + }, + }, + Returns = + { + { + Type = "boolean", + }, + }, + Notes = "If there is a hopper at the specified coords, calls the CallbackFunction with the {{cHopperEntity}} parameter representing the hopper. The CallbackFunction has the following signature:
function Callback({{cHopperEntity|cHopperEntity}})
The function returns false if there is no hopper, or if there is, it returns the bool value that the callback has returned.", + }, DoWithMobHeadAt = { Params = diff --git a/src/Bindings/ManualBindings_World.cpp b/src/Bindings/ManualBindings_World.cpp index 17a66b4c2..7b072e435 100644 --- a/src/Bindings/ManualBindings_World.cpp +++ b/src/Bindings/ManualBindings_World.cpp @@ -984,6 +984,7 @@ void cManualBindings::BindWorld(lua_State * tolua_S) 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, "DoWithNearestPlayer", tolua_cWorld_DoWithNearestPlayer); tolua_function(tolua_S, "DoWithNoteBlockAt", DoWithXYZ); diff --git a/src/BlockEntities/HopperEntity.cpp b/src/BlockEntities/HopperEntity.cpp index e85cfb952..aadca8311 100644 --- a/src/BlockEntities/HopperEntity.cpp +++ b/src/BlockEntities/HopperEntity.cpp @@ -20,7 +20,8 @@ cHopperEntity::cHopperEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World): super(a_BlockType, a_BlockMeta, a_Pos, ContentsWidth, ContentsHeight, a_World), m_LastMoveItemsInTick(0), - m_LastMoveItemsOutTick(0) + m_LastMoveItemsOutTick(0), + m_Locked(false) { ASSERT(a_BlockType == E_BLOCK_HOPPER); } @@ -29,6 +30,15 @@ cHopperEntity::cHopperEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vect +void cHopperEntity::SetLocked(bool a_Value) +{ + m_Locked = a_Value; +} + + + + + std::pair cHopperEntity::GetOutputBlockPos(NIBBLETYPE a_BlockMeta) { auto pos = GetPos(); @@ -69,9 +79,12 @@ bool cHopperEntity::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) Int64 CurrentTick = a_Chunk.GetWorld()->GetWorldAge(); bool isDirty = false; - isDirty = MoveItemsIn (a_Chunk, CurrentTick) || isDirty; - isDirty = MovePickupsIn(a_Chunk, CurrentTick) || isDirty; - isDirty = MoveItemsOut (a_Chunk, CurrentTick) || isDirty; + if (!m_Locked) + { + isDirty = MoveItemsIn (a_Chunk, CurrentTick) || isDirty; + isDirty = MovePickupsIn(a_Chunk, CurrentTick) || isDirty; + isDirty = MoveItemsOut (a_Chunk, CurrentTick) || isDirty; + } return isDirty; } diff --git a/src/BlockEntities/HopperEntity.h b/src/BlockEntities/HopperEntity.h index c57dce672..ef86a36bb 100644 --- a/src/BlockEntities/HopperEntity.h +++ b/src/BlockEntities/HopperEntity.h @@ -33,6 +33,8 @@ public: TICKS_PER_TRANSFER = 8, ///< How many ticks at minimum between two item transfers to or from the hopper } ; + + // tolua_end BLOCKENTITY_PROTODEF(cHopperEntity) @@ -45,6 +47,8 @@ public: Exported in ManualBindings.cpp. */ std::pair GetOutputBlockPos(NIBBLETYPE a_BlockMeta); + void SetLocked(bool a_Value); + protected: Int64 m_LastMoveItemsInTick; @@ -91,6 +95,10 @@ protected: /** Moves one piece to the specified entity's contents' slot. Returns true if contents have changed. */ bool MoveItemsToSlot(cBlockEntityWithItems & a_Entity, int a_DstSlotNum); + +private: + + bool m_Locked; } ; // tolua_export diff --git a/src/Chunk.cpp b/src/Chunk.cpp index 574c00132..9dc0e8aee 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -2177,6 +2177,17 @@ bool cChunk::DoWithFurnaceAt(int a_BlockX, int a_BlockY, int a_BlockZ, cFurnaceC +bool cChunk::DoWithHopperAt(int a_BlockX, int a_BlockY, int a_BlockZ, cHopperCallback a_Callback) +{ + return GenericDoWithBlockEntityAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); +} + + + + + bool cChunk::DoWithNoteBlockAt(int a_BlockX, int a_BlockY, int a_BlockZ, cNoteBlockCallback a_Callback) { return GenericDoWithBlockEntityAtIsValid()) + { + return false; + } + return Chunk->DoWithHopperAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); +} + + + + + bool cChunkMap::DoWithNoteBlockAt(int a_BlockX, int a_BlockY, int a_BlockZ, cNoteBlockCallback a_Callback) { int ChunkX, ChunkZ; diff --git a/src/ChunkMap.h b/src/ChunkMap.h index 3623400c8..5c0aa7688 100644 --- a/src/ChunkMap.h +++ b/src/ChunkMap.h @@ -29,6 +29,7 @@ class cDispenserEntity; class cDropperEntity; class cDropSpenserEntity; class cFurnaceEntity; +class cHopperEntity; class cNoteEntity; class cCommandBlockEntity; class cMobHeadEntity; @@ -53,6 +54,7 @@ using cDispenserCallback = cFunctionRef; using cDropperCallback = cFunctionRef; using cDropSpenserCallback = cFunctionRef; using cFurnaceCallback = cFunctionRef; +using cHopperCallback = cFunctionRef; using cNoteBlockCallback = cFunctionRef; using cCommandBlockCallback = cFunctionRef; using cMobHeadCallback = cFunctionRef; @@ -302,6 +304,10 @@ public: Returns false if there's no furnace at those coords or callback returns true, returns true if found. */ bool DoWithFurnaceAt(int a_BlockX, int a_BlockY, int a_BlockZ, cFurnaceCallback a_Callback); // Lua-accessible + /** Calls the callback for the hopper at the specified coords. + Returns false if there's no hopper at those coords or callback returns true, returns true if found. */ + bool DoWithHopperAt(int a_BlockX, int a_BlockY, int a_BlockZ, cHopperCallback a_Callback); // Lua-accessible + /** Calls the callback for the noteblock at the specified coords. Returns false if there's no noteblock at those coords or callback returns true, returns true if found. */ bool DoWithNoteBlockAt(int a_BlockX, int a_BlockY, int a_BlockZ, cNoteBlockCallback a_Callback); // Lua-accessible diff --git a/src/Simulator/IncrementalRedstoneSimulator/CMakeLists.txt b/src/Simulator/IncrementalRedstoneSimulator/CMakeLists.txt index fdce1bff5..fada3e662 100644 --- a/src/Simulator/IncrementalRedstoneSimulator/CMakeLists.txt +++ b/src/Simulator/IncrementalRedstoneSimulator/CMakeLists.txt @@ -7,6 +7,7 @@ set (HDRS CommandBlockHandler.h DoorHandler.h DropSpenserHandler.h + HopperHandler.h IncrementalRedstoneSimulator.h RedstoneHandler.h RedstoneSimulatorChunkData.h diff --git a/src/Simulator/IncrementalRedstoneSimulator/HopperHandler.h b/src/Simulator/IncrementalRedstoneSimulator/HopperHandler.h new file mode 100644 index 000000000..6012db1e1 --- /dev/null +++ b/src/Simulator/IncrementalRedstoneSimulator/HopperHandler.h @@ -0,0 +1,62 @@ + +#pragma once + +#include "RedstoneHandler.h" +#include "../../BlockEntities/HopperEntity.h" + + + + + +class cHopperHandler : public cRedstoneHandler +{ + typedef cRedstoneHandler super; +public: + + virtual unsigned char GetPowerDeliveredToPosition(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, Vector3i a_QueryPosition, BLOCKTYPE a_QueryBlockType) const override + { + UNUSED(a_World); + UNUSED(a_Position); + UNUSED(a_BlockType); + UNUSED(a_Meta); + UNUSED(a_QueryPosition); + UNUSED(a_QueryBlockType); + return 0; + } + + virtual unsigned char GetPowerLevel(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta) const override + { + UNUSED(a_World); + UNUSED(a_Position); + UNUSED(a_BlockType); + UNUSED(a_Meta); + return 0; + } + + virtual cVector3iArray Update(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, PoweringData a_PoweringData) const override + { + // LOGD("Evaluating commander the cmdblck (%d %d %d)", a_Position.x, a_Position.y, a_Position.z); + + auto Previous = static_cast(a_World.GetRedstoneSimulator())->GetChunkData()->ExchangeUpdateOncePowerData(a_Position, a_PoweringData); + if (Previous.PowerLevel != a_PoweringData.PowerLevel) + { + return {}; + } + + a_World.DoWithHopperAt(a_Position.x, a_Position.y, a_Position.z, [a_PoweringData](cHopperEntity & a_Hopper) + { + a_Hopper.SetLocked(a_PoweringData.PowerLevel != 0); + return false; + } + ); + return {}; + } + + virtual cVector3iArray GetValidSourcePositions(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta) const override + { + UNUSED(a_World); + UNUSED(a_BlockType); + UNUSED(a_Meta); + return GetAdjustedRelatives(a_Position, GetRelativeAdjacents()); + } +}; diff --git a/src/Simulator/IncrementalRedstoneSimulator/IncrementalRedstoneSimulator.cpp b/src/Simulator/IncrementalRedstoneSimulator/IncrementalRedstoneSimulator.cpp index 2c56646fe..ddd682288 100644 --- a/src/Simulator/IncrementalRedstoneSimulator/IncrementalRedstoneSimulator.cpp +++ b/src/Simulator/IncrementalRedstoneSimulator/IncrementalRedstoneSimulator.cpp @@ -25,6 +25,7 @@ #include "DropSpenserHandler.h" #include "RedstoneComparatorHandler.h" #include "TrappedChestHandler.h" +#include "HopperHandler.h" @@ -98,6 +99,7 @@ std::unique_ptr cIncrementalRedstoneSimulator::CreateComponent case E_BLOCK_BLOCK_OF_REDSTONE: return cpp14::make_unique(); case E_BLOCK_COMMAND_BLOCK: return cpp14::make_unique(); + case E_BLOCK_HOPPER: return cpp14::make_unique(); case E_BLOCK_NOTE_BLOCK: return cpp14::make_unique(); case E_BLOCK_REDSTONE_WIRE: return cpp14::make_unique(); case E_BLOCK_TNT: return cpp14::make_unique(); diff --git a/src/World.cpp b/src/World.cpp index d8655ce11..2d0dfca9f 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -1531,6 +1531,15 @@ bool cWorld::DoWithFurnaceAt(int a_BlockX, int a_BlockY, int a_BlockZ, cFurnaceC +bool cWorld::DoWithHopperAt(int a_BlockX, int a_BlockY, int a_BlockZ, cHopperCallback a_Callback) +{ + return m_ChunkMap->DoWithHopperAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); +} + + + + + bool cWorld::DoWithNoteBlockAt(int a_BlockX, int a_BlockY, int a_BlockZ, cNoteBlockCallback a_Callback) { return m_ChunkMap->DoWithNoteBlockAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); diff --git a/src/World.h b/src/World.h index 7f6b1871d..e1341c9cb 100644 --- a/src/World.h +++ b/src/World.h @@ -45,6 +45,7 @@ class cCuboid; class cDispenserEntity; class cFlowerPotEntity; class cFurnaceEntity; +class cHopperEntity; class cNoteEntity; class cMobHeadEntity; class cCompositeChat; @@ -769,6 +770,9 @@ public: /** Calls the callback for the furnace at the specified coords; returns false if there's no furnace at those coords or callback returns true, returns true if found */ bool DoWithFurnaceAt(int a_BlockX, int a_BlockY, int a_BlockZ, cFurnaceCallback a_Callback); // Exported in ManualBindings.cpp + /** Calls the callback for the hopper at the specified coords; returns false if there's no hopper at those coords or callback returns true, returns true if found */ + bool DoWithHopperAt(int a_BlockX, int a_BlockY, int a_BlockZ, cHopperCallback a_Callback); // Exported in ManualBindings.cpp + /** Calls the callback for the noteblock at the specified coords; returns false if there's no noteblock at those coords or callback returns true, returns true if found */ bool DoWithNoteBlockAt(int a_BlockX, int a_BlockY, int a_BlockZ, cNoteBlockCallback a_Callback); // Exported in ManualBindings.cpp -- cgit v1.2.3