Add cLuaWindow OnClicked Callback (#3901)
This commit is contained in:
@@ -875,6 +875,17 @@ void cLuaState::Push(const char * a_Value)
|
||||
|
||||
|
||||
|
||||
void cLuaState::Push(const cItem & a_Item)
|
||||
{
|
||||
ASSERT(IsValid());
|
||||
auto c = new cItem(a_Item);
|
||||
tolua_pushusertype_and_takeownership(m_LuaState, c, "cItem");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void cLuaState::Push(const cNil & a_Nil)
|
||||
{
|
||||
ASSERT(IsValid());
|
||||
|
||||
@@ -614,6 +614,7 @@ public:
|
||||
void Push(const AStringMap & a_Dictionary);
|
||||
void Push(const AStringVector & a_Vector);
|
||||
void Push(const char * a_Value);
|
||||
void Push(const cItem & a_Item);
|
||||
void Push(const cNil & a_Nil);
|
||||
void Push(const cRef & a_Ref);
|
||||
void Push(const Vector3d & a_Vector);
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#include "PluginLua.h"
|
||||
#include "Root.h"
|
||||
#include "lua/src/lauxlib.h" // Needed for LUA_REFNIL
|
||||
|
||||
#include "ClientHandle.h"
|
||||
|
||||
|
||||
|
||||
@@ -86,6 +86,19 @@ cLuaWindow::~cLuaWindow()
|
||||
|
||||
|
||||
|
||||
void cLuaWindow::SetOnClicked(cLuaState::cCallbackPtr && a_OnClicked)
|
||||
{
|
||||
// Only one Lua state can be a cLuaWindow object callback:
|
||||
ASSERT(a_OnClicked->IsSameLuaState(*m_LuaState));
|
||||
|
||||
// Store the new reference, releasing the old one if appropriate:
|
||||
m_OnClicked = std::move(a_OnClicked);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void cLuaWindow::SetOnClosing(cLuaState::cCallbackPtr && a_OnClosing)
|
||||
{
|
||||
// Only one Lua state can be a cLuaWindow object callback:
|
||||
@@ -206,3 +219,24 @@ void cLuaWindow::OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum)
|
||||
|
||||
|
||||
|
||||
|
||||
void cLuaWindow::Clicked(cPlayer & a_Player, int a_WindowID, short a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem)
|
||||
{
|
||||
if (m_OnClicked != nullptr)
|
||||
{
|
||||
// Plugin can stop a click
|
||||
if (m_OnClicked->Call(this, &a_Player, a_SlotNum, a_ClickAction, a_ClickedItem))
|
||||
{
|
||||
// Tell the client the actual state of the window
|
||||
a_Player.GetClientHandle()->SendInventorySlot(-1, -1, a_Player.GetDraggingItem());
|
||||
BroadcastWholeWindow();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
cWindow::Clicked(a_Player, a_WindowID, a_SlotNum, a_ClickAction, a_ClickedItem);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -49,6 +49,10 @@ public:
|
||||
|
||||
// tolua_end
|
||||
|
||||
/** Sets the Lua callback to call when the player clicks on the window.
|
||||
The window can stop the click from propogating. */
|
||||
void SetOnClicked(cLuaState::cCallbackPtr && a_OnClicked);
|
||||
|
||||
/** Sets the Lua callback function to call when the window is about to close */
|
||||
void SetOnClosing(cLuaState::cCallbackPtr && a_OnClosing);
|
||||
|
||||
@@ -63,6 +67,9 @@ protected:
|
||||
/** The canon Lua state that has opened the window and owns the m_LuaRef */
|
||||
cLuaState * m_LuaState;
|
||||
|
||||
/** The Lua callback to call when the player clicked on a slot */
|
||||
cLuaState::cCallbackPtr m_OnClicked;
|
||||
|
||||
/** The Lua callback to call when the window is closing for any player */
|
||||
cLuaState::cCallbackPtr m_OnClosing;
|
||||
|
||||
@@ -80,6 +87,11 @@ protected:
|
||||
|
||||
// cWindow overrides:
|
||||
virtual void OpenedByPlayer(cPlayer & a_Player) override;
|
||||
virtual void Clicked(
|
||||
cPlayer & a_Player, int a_WindowID,
|
||||
short a_SlotNum, eClickAction a_ClickAction,
|
||||
const cItem & a_ClickedItem
|
||||
) override;
|
||||
virtual bool ClosedByPlayer(cPlayer & a_Player, bool a_CanRefuse) override;
|
||||
virtual void Destroy(void) override;
|
||||
virtual void DistributeStack(cItem & a_ItemStack, int a_Slot, cPlayer & a_Player, cSlotArea * a_ClickedArea, bool a_ShouldApply) override;
|
||||
|
||||
@@ -3816,6 +3816,7 @@ void cManualBindings::Bind(lua_State * tolua_S)
|
||||
tolua_function(tolua_S, "new", tolua_cLuaWindow_new);
|
||||
tolua_function(tolua_S, "new_local", tolua_cLuaWindow_new_local);
|
||||
tolua_function(tolua_S, ".call", tolua_cLuaWindow_new_local);
|
||||
tolua_function(tolua_S, "SetOnClicked", tolua_SetObjectCallback<cLuaWindow, &cLuaWindow::SetOnClicked>);
|
||||
tolua_function(tolua_S, "SetOnClosing", tolua_SetObjectCallback<cLuaWindow, &cLuaWindow::SetOnClosing>);
|
||||
tolua_function(tolua_S, "SetOnSlotChanged", tolua_SetObjectCallback<cLuaWindow, &cLuaWindow::SetOnSlotChanged>);
|
||||
tolua_endmodule(tolua_S);
|
||||
|
||||
@@ -80,6 +80,7 @@ public:
|
||||
virtual bool OnPlayerJoined (cPlayer & a_Player) = 0;
|
||||
virtual bool OnPlayerLeftClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status) = 0;
|
||||
virtual bool OnPlayerMoving (cPlayer & a_Player, const Vector3d & a_OldPosition, const Vector3d & a_NewPosition) = 0;
|
||||
virtual bool OnPlayerOpeningWindow(cPlayer & a_Player, cWindow & a_Window) = 0;
|
||||
virtual bool OnPlayerPlacedBlock (cPlayer & a_Player, const sSetBlock & a_BlockChange) = 0;
|
||||
virtual bool OnPlayerPlacingBlock (cPlayer & a_Player, const sSetBlock & a_BlockChange) = 0;
|
||||
virtual bool OnPlayerRightClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) = 0;
|
||||
|
||||
@@ -659,6 +659,15 @@ bool cPluginLua::OnEntityTeleport(cEntity & a_Entity, const Vector3d & a_OldPosi
|
||||
|
||||
|
||||
|
||||
bool cPluginLua::OnPlayerOpeningWindow(cPlayer & a_Player, cWindow & a_Window)
|
||||
{
|
||||
return CallSimpleHooks(cPluginManager::HOOK_PLAYER_OPENING_WINDOW, &a_Player, &a_Window);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bool cPluginLua::OnPlayerPlacedBlock(cPlayer & a_Player, const sSetBlock & a_BlockChange)
|
||||
{
|
||||
return CallSimpleHooks(cPluginManager::HOOK_PLAYER_PLACED_BLOCK,
|
||||
@@ -1056,6 +1065,7 @@ const char * cPluginLua::GetHookFnName(int a_HookType)
|
||||
case cPluginManager::HOOK_PLAYER_JOINED: return "OnPlayerJoined";
|
||||
case cPluginManager::HOOK_PLAYER_LEFT_CLICK: return "OnPlayerLeftClick";
|
||||
case cPluginManager::HOOK_PLAYER_MOVING: return "OnPlayerMoving";
|
||||
case cPluginManager::HOOK_PLAYER_OPENING_WINDOW: return "OnPlayerOpeningWindow";
|
||||
case cPluginManager::HOOK_PLAYER_PLACED_BLOCK: return "OnPlayerPlacedBlock";
|
||||
case cPluginManager::HOOK_PLAYER_PLACING_BLOCK: return "OnPlayerPlacingBlock";
|
||||
case cPluginManager::HOOK_PLAYER_RIGHT_CLICK: return "OnPlayerRightClick";
|
||||
|
||||
@@ -101,6 +101,7 @@ public:
|
||||
virtual bool OnPlayerJoined (cPlayer & a_Player) override;
|
||||
virtual bool OnPlayerLeftClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status) override;
|
||||
virtual bool OnPlayerMoving (cPlayer & a_Player, const Vector3d & a_OldPosition, const Vector3d & a_NewPosition) override;
|
||||
virtual bool OnPlayerOpeningWindow(cPlayer & a_Player, cWindow & a_Window) override;
|
||||
virtual bool OnPlayerPlacedBlock (cPlayer & a_Player, const sSetBlock & a_BlockChange) override;
|
||||
virtual bool OnPlayerPlacingBlock (cPlayer & a_Player, const sSetBlock & a_BlockChange) override;
|
||||
virtual bool OnPlayerRightClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override;
|
||||
|
||||
@@ -999,6 +999,25 @@ bool cPluginManager::CallHookPlayerMoving(cPlayer & a_Player, const Vector3d & a
|
||||
|
||||
|
||||
|
||||
bool cPluginManager::CallHookPlayerOpeningWindow(cPlayer & a_Player, cWindow & a_Window)
|
||||
{
|
||||
FIND_HOOK(HOOK_PLAYER_OPENING_WINDOW);
|
||||
VERIFY_HOOK;
|
||||
|
||||
for (PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr)
|
||||
{
|
||||
if ((*itr)->OnPlayerOpeningWindow(a_Player, a_Window))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bool cPluginManager::CallHookPlayerPlacedBlock(cPlayer & a_Player, const sSetBlock & a_BlockChange)
|
||||
{
|
||||
FIND_HOOK(HOOK_PLAYER_PLACED_BLOCK);
|
||||
|
||||
@@ -24,6 +24,7 @@ class cPickup;
|
||||
class cPlayer;
|
||||
class cPlugin;
|
||||
class cProjectileEntity;
|
||||
class cWindow;
|
||||
class cWorld;
|
||||
class cSettingsRepositoryInterface;
|
||||
class cDeadlockDetect;
|
||||
@@ -111,6 +112,7 @@ public:
|
||||
HOOK_PLAYER_JOINED,
|
||||
HOOK_PLAYER_LEFT_CLICK,
|
||||
HOOK_PLAYER_MOVING,
|
||||
HOOK_PLAYER_OPENING_WINDOW,
|
||||
HOOK_PLAYER_PLACED_BLOCK,
|
||||
HOOK_PLAYER_PLACING_BLOCK,
|
||||
HOOK_PLAYER_RIGHT_CLICK,
|
||||
@@ -257,6 +259,7 @@ public:
|
||||
bool CallHookPlayerJoined (cPlayer & a_Player);
|
||||
bool CallHookPlayerLeftClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status);
|
||||
bool CallHookPlayerMoving (cPlayer & a_Player, const Vector3d & a_OldPosition, const Vector3d & a_NewPosition);
|
||||
bool CallHookPlayerOpeningWindow (cPlayer & a_Player, cWindow & a_Window);
|
||||
bool CallHookPlayerPlacedBlock (cPlayer & a_Player, const sSetBlock & a_BlockChange);
|
||||
bool CallHookPlayerPlacingBlock (cPlayer & a_Player, const sSetBlock & a_BlockChange);
|
||||
bool CallHookPlayerRightClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ);
|
||||
|
||||
@@ -1321,10 +1321,16 @@ cTeam * cPlayer::UpdateTeam(void)
|
||||
|
||||
void cPlayer::OpenWindow(cWindow & a_Window)
|
||||
{
|
||||
if (cRoot::Get()->GetPluginManager()->CallHookPlayerOpeningWindow(*this, a_Window))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (&a_Window != m_CurrentWindow)
|
||||
{
|
||||
CloseWindow(false);
|
||||
}
|
||||
|
||||
a_Window.OpenedByPlayer(*this);
|
||||
m_CurrentWindow = &a_Window;
|
||||
a_Window.SendWholeWindow(*GetClientHandle());
|
||||
|
||||
+1
-1
@@ -113,7 +113,7 @@ public:
|
||||
void GetSlots(cPlayer & a_Player, cItems & a_Slots) const;
|
||||
|
||||
/** Handles a click event from a player */
|
||||
void Clicked(
|
||||
virtual void Clicked(
|
||||
cPlayer & a_Player, int a_WindowID,
|
||||
short a_SlotNum, eClickAction a_ClickAction,
|
||||
const cItem & a_ClickedItem
|
||||
|
||||
Reference in New Issue
Block a user