1
0
Fork 0

Add cLuaWindow OnClicked Callback (#3901)

This commit is contained in:
Lane Kolbly 2017-08-17 09:27:43 -05:00 committed by Mattes D
parent 238f5bb338
commit 1ec85a2b2c
15 changed files with 156 additions and 3 deletions

View File

@ -8029,6 +8029,17 @@ This class is used by plugins wishing to display a custom window to the player,
},
Notes = "Returns the cItemGrid object representing the internal storage in this window",
},
SetOnClicked =
{
Params =
{
{
Name = "OnClickedCallback",
Type = "function",
},
},
Notes = "Sets the function that the window will call when it is about to process a click from a player. See {{#additionalinfo_1|below}} for the signature of the callback function.",
},
SetOnClosing =
{
Params =
@ -8060,6 +8071,17 @@ This class is used by plugins wishing to display a custom window to the player,
The object calls the following functions at the appropriate time:
]],
},
{
Header = "OnClicked Callback",
Contents = [[
This callback, settable via the SetOnClicked() function, will be called when the player clicks a slot in the window. The callback can cancel the click.</p>
<pre class="prettyprint lang-lua">
function OnWindowClicked(a_Window, a_Player, a_SlotNum, a_ClickAction, a_ClickedItem)
</pre>
<p>
The a_Window parameter is the cLuaWindow object representing the window, a_Player is the player who made the click, a_SlotNum is the slot the player clicked, a_ClickAction is the type of click the player made, and a_ClickedItem is the item the player clicked on, if applicable. If the function returns true, the click is cancelled (internally, the server resends the window slots to the player to keep the player in sync).
]],
},
{
Header = "OnClosing Callback",
Contents = [[
@ -8086,7 +8108,7 @@ function OnWindowSlotChanged(a_Window, a_SlotNum)
{
Header = "Example",
Contents = [[
This example is taken from the Debuggers plugin, used to test the API functionality. It opens a window and refuse to close it 3 times. It also logs slot changes to the server console.
This example is taken from the Debuggers plugin, used to test the API functionality. It opens a window and refuse to close it 3 times. It also logs slot changes to the server console and prevents shift-clicking in the window.
<pre class="prettyprint lang-lua">
-- Callback that refuses to close the window twice, then allows:
local Attempt = 1;
@ -8101,10 +8123,18 @@ local OnSlotChanged = function(Window, SlotNum)
LOG("Window \"" .. Window:GetWindowTitle() .. "\" slot " .. SlotNum .. " changed.");
end
-- Prevent shift-clicking:
local OnClicked = function(Window, ClickingPlayer, SlotNum, ClickAction, ClickedItem)
if ClickAction == caShiftLeftClick then
return true
end
end
-- Set window contents:
-- a_Player is a cPlayer object received from the outside of this code fragment
local Window = cLuaWindow(cWindow.wtHopper, 3, 3, "TestWnd");
Window:SetSlot(a_Player, 0, cItem(E_ITEM_DIAMOND, 64));
Window:SetOnClicked(OnClicked);
Window:SetOnClosing(OnClosing);
Window:SetOnSlotChanged(OnSlotChanged);

View File

@ -840,6 +840,10 @@ cPluginManager.AddHook(cPluginManager.HOOK_CHAT, OnChatMessage);
{
Notes = "Called when the player has moved and the movement is now being applied.",
},
HOOK_PLAYER_OPENING_WINDOW =
{
Notes = "Called when the player is about to open a window. The plugin can return true to cancel the window opening.",
},
HOOK_PLAYER_PLACED_BLOCK =
{
Notes = "Called when the player has just placed a block",

View File

@ -0,0 +1,20 @@
return
{
HOOK_PLAYER_OPENING_WINDOW =
{
CalledWhen = "Called when a player is about to open a window",
DefaultFnName = "OnPlayerOpeningWindow", -- also used as pagename
Desc = [[
This hook is called when a player is about to open a window, e.g. when they click on a chest or a furnace.
]],
Params =
{
{ Name = "Player", Type = "{{cPlayer}}", Notes = "The player who is opening the window" },
{ Name = "Window", Type = "{{cWindow}}", Notes = "The window that is being opened" },
},
Returns = [[
If the function returns false or no value, the next plugin's callback is called, and finally
Cuberite will process the opening window. If the function returns true, no other callback is called for this event.
]],
}, -- HOOK_PLAYER_OPENING_WINDOW
}

View File

@ -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());

View File

@ -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);

View File

@ -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);
}

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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";

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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());

View File

@ -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