From 23bca008317e125659d4b97f29cf13da81a7aecd Mon Sep 17 00:00:00 2001 From: KrystilizeNevaDies <57762380+KrystilizeNevaDies@users.noreply.github.com> Date: Tue, 22 Sep 2020 01:31:45 +1000 Subject: Implement Dropspenser Hook (#4903) * Implement Dropspenser Hook Implemented a hook for dropspensing. HOOK_DROPSPENSE When plugin callback returns true then dropspense is cancelled * Update src/Bindings/PluginManager.h Co-authored-by: peterbell10 * Create OnDropSpense.lua * Fix indent * Forgot include Forgot to include the plugin manager. Although now im confused why it was working on my end without including the plugin manager * Update plugins.lua with dropspense * fix typos * haha notepad++ go brr Co-authored-by: peterbell10 --- src/Bindings/Plugin.h | 1 + src/Bindings/PluginLua.cpp | 9 +++++++++ src/Bindings/PluginLua.h | 1 + src/Bindings/PluginManager.cpp | 13 +++++++++++++ src/Bindings/PluginManager.h | 6 ++++-- src/BlockEntities/DropSpenserEntity.cpp | 11 ++++++++++- src/BlockEntities/HopperEntity.cpp | 2 +- 7 files changed, 39 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/Bindings/Plugin.h b/src/Bindings/Plugin.h index afc575b13..19947ff2e 100644 --- a/src/Bindings/Plugin.h +++ b/src/Bindings/Plugin.h @@ -66,6 +66,7 @@ public: virtual bool OnHandshake (cClientHandle & a_Client, const AString & a_Username) = 0; virtual bool OnHopperPullingItem (cWorld & a_World, cHopperEntity & a_Hopper, int a_DstSlotNum, cBlockEntityWithItems & a_SrcEntity, int a_SrcSlotNum) = 0; virtual bool OnHopperPushingItem (cWorld & a_World, cHopperEntity & a_Hopper, int a_SrcSlotNum, cBlockEntityWithItems & a_DstEntity, int a_DstSlotNum) = 0; + virtual bool OnDropSpense (cWorld & a_World, cDropSpenserEntity & a_DropSpenser, int a_SlotNum) = 0; virtual bool OnKilled (cEntity & a_Victim, TakeDamageInfo & a_TDI, AString & a_DeathMessage) = 0; virtual bool OnKilling (cEntity & a_Victim, cEntity * a_Killer, TakeDamageInfo & a_TDI) = 0; virtual bool OnLogin (cClientHandle & a_Client, UInt32 a_ProtocolVersion, const AString & a_Username) = 0; diff --git a/src/Bindings/PluginLua.cpp b/src/Bindings/PluginLua.cpp index abac8b9a8..f4cfd4a86 100644 --- a/src/Bindings/PluginLua.cpp +++ b/src/Bindings/PluginLua.cpp @@ -523,6 +523,15 @@ bool cPluginLua::OnHopperPushingItem(cWorld & a_World, cHopperEntity & a_Hopper, +bool cPluginLua::OnDropSpense(cWorld & a_World, cDropSpenserEntity & a_DropSpenser, int a_SlotNum) +{ + return CallSimpleHooks(cPluginManager::HOOK_DROPSPENSE, &a_World, &a_DropSpenser, a_SlotNum); +} + + + + + bool cPluginLua::OnKilled(cEntity & a_Victim, TakeDamageInfo & a_TDI, AString & a_DeathMessage) { cOperation op(*this); diff --git a/src/Bindings/PluginLua.h b/src/Bindings/PluginLua.h index a9e488074..352c444dc 100644 --- a/src/Bindings/PluginLua.h +++ b/src/Bindings/PluginLua.h @@ -87,6 +87,7 @@ public: virtual bool OnHandshake (cClientHandle & a_Client, const AString & a_Username) override; virtual bool OnHopperPullingItem (cWorld & a_World, cHopperEntity & a_Hopper, int a_DstSlotNum, cBlockEntityWithItems & a_SrcEntity, int a_SrcSlotNum) override; virtual bool OnHopperPushingItem (cWorld & a_World, cHopperEntity & a_Hopper, int a_SrcSlotNum, cBlockEntityWithItems & a_DstEntity, int a_DstSlotNum) override; + virtual bool OnDropSpense (cWorld & a_World, cDropSpenserEntity & a_DropSpenser, int a_SlotNum) override; virtual bool OnKilled (cEntity & a_Victim, TakeDamageInfo & a_TDI, AString & a_DeathMessage) override; virtual bool OnKilling (cEntity & a_Victim, cEntity * a_Killer, TakeDamageInfo & a_TDI) override; virtual bool OnLogin (cClientHandle & a_Client, UInt32 a_ProtocolVersion, const AString & a_Username) override; diff --git a/src/Bindings/PluginManager.cpp b/src/Bindings/PluginManager.cpp index 88bc82172..6593097dd 100644 --- a/src/Bindings/PluginManager.cpp +++ b/src/Bindings/PluginManager.cpp @@ -613,6 +613,19 @@ bool cPluginManager::CallHookHopperPushingItem(cWorld & a_World, cHopperEntity & +bool cPluginManager::CallHookDropSpense(cWorld & a_World, cDropSpenserEntity & a_DropSpenser, int a_SlotNum) +{ + return GenericCallHook(HOOK_DROPSPENSE, [&](cPlugin * a_Plugin) + { + return a_Plugin->OnDropSpense(a_World, a_DropSpenser, a_SlotNum); + } + ); +} + + + + + bool cPluginManager::CallHookKilled(cEntity & a_Victim, TakeDamageInfo & a_TDI, AString & a_DeathMessage) { return GenericCallHook(HOOK_KILLED, [&](cPlugin * a_Plugin) diff --git a/src/Bindings/PluginManager.h b/src/Bindings/PluginManager.h index 3b35a826a..353950f18 100644 --- a/src/Bindings/PluginManager.h +++ b/src/Bindings/PluginManager.h @@ -18,6 +18,7 @@ class cClientHandle; class cCommandOutputCallback; class cCraftingGrid; class cCraftingRecipe; +class cDropSpenserEntity; class cEntity; class cHopperEntity; class cItems; @@ -101,6 +102,7 @@ public: HOOK_HANDSHAKE, HOOK_HOPPER_PULLING_ITEM, HOOK_HOPPER_PUSHING_ITEM, + HOOK_DROPSPENSE, HOOK_KILLED, HOOK_KILLING, HOOK_LOGIN, @@ -249,7 +251,8 @@ 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 CallHookDropSpense (cWorld & a_World, cDropSpenserEntity & a_DropSpenser, int a_SlotNum); + 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 CallHookLoginForge (cClientHandle & a_Client, AStringMap & a_Mods); @@ -457,4 +460,3 @@ private: - diff --git a/src/BlockEntities/DropSpenserEntity.cpp b/src/BlockEntities/DropSpenserEntity.cpp index 97a67ff60..243ec8594 100644 --- a/src/BlockEntities/DropSpenserEntity.cpp +++ b/src/BlockEntities/DropSpenserEntity.cpp @@ -6,6 +6,7 @@ #include "Globals.h" #include "DropSpenserEntity.h" +#include "../Bindings/PluginManager.h" #include "../EffectID.h" #include "../Entities/Player.h" #include "../Chunk.h" @@ -80,8 +81,16 @@ void cDropSpenserEntity::DropSpense(cChunk & a_Chunk) int RandomSlot = m_World->GetTickRandomNumber(SlotsCnt - 1); + int SpenseSlot = OccupiedSlots[RandomSlot]; + + if (cPluginManager::Get()->CallHookDropSpense(*m_World, *this, SpenseSlot)) + { + // Plugin disagrees with the move + return; + } + // DropSpense the item, using the specialized behavior in the subclasses: - DropSpenseFromSlot(a_Chunk, OccupiedSlots[RandomSlot]); + DropSpenseFromSlot(a_Chunk, SpenseSlot); // Broadcast a smoke and click effects: NIBBLETYPE Meta = a_Chunk.GetMeta(GetRelPos()); diff --git a/src/BlockEntities/HopperEntity.cpp b/src/BlockEntities/HopperEntity.cpp index 9665b9f25..2e4264204 100644 --- a/src/BlockEntities/HopperEntity.cpp +++ b/src/BlockEntities/HopperEntity.cpp @@ -95,7 +95,7 @@ bool cHopperEntity::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) void cHopperEntity::SendTo(cClientHandle & a_Client) { // The hopper entity doesn't need anything sent to the client when it's created / gets in the viewdistance - // All the actual handling is in the cWindow UI code that gets called when the hopper is rclked + // All the actual handling is in the cWindow UI code that gets called when the hopper is right-clicked UNUSED(a_Client); } -- cgit v1.2.3