How to get user from SetOnSlotChanged
#2
OnSlotChanged() event needn't come from a user, so there's no user bound to it in the interface - for example Furnaces and Hoppers change their slots on their own. That's why you don't get one. If we wanted to give you the user, we'd also need to change all the API functions setting the slots in the various containers (cChestEntity:SetSlot()), to include the Player parameter, and you'd need to fill it each time. That's probably not what you'd want.

Windows can be shared among multiple players (such as Chest or Furnace windows), so MCServer doesn't provide a means to bind a specific window for a client. You can do that in your plugin, if you know that you'll be using the window for only one player. Don't store the cPlayer object, though, as the player may get kicked for whatever reason; store the player Name or UniqueID. You can use Lua Closures for this (functions can access variables defined outside of them):
function OnMyCommand(a_Split, a_Player)
  local PlayerName = a_Player:GetName()
  local PlayerWorld = a_Player:GetWorld()
  local NumChanges = 0
  local OnSlotChanged = function(a_Window, a_SlotNum)
    PlayerWorld:DoWithPlayer(PlayerName, DoSomething)  -- Note that you can access PlayerWorld and PlayerName here, even when OnMyCommand() finished executing!
    NumChanges = NumChanges + 1  -- You can even change the variables
  end
  local OnWindowClosing = function(a_Window, a_Player, a_CanRefuse)
    PlayerWorld:BroadcastChat("Player " .. PlayerName .. " closed the window after making " .. NumChanges .. " changes")  -- Again, you can access PlayerWorld and PlayerName
    -- Note that NumChanges is shared among these two functions, so even after OnMyCommand finishes executing, they still refer to the same variable.
  end
  ...
end

You need to take responsibility for not creating an infinite loop by using SetSlot() in OnSlotChanged(): Set a flag in the function whether the "Set" comes from you or not, and before doing the SetSlot(), set the flag. Then you get a second trigger of OnSlotChanged(), but because you test the flag at the very beginning, you won't do the second SetSlot() anymore.
Reply
Thanks given by:


Messages In This Thread
How to get user from SetOnSlotChanged - by ThuGie - 01-31-2014, 12:37 PM
RE: How to get user from SetOnSlotChanged - by xoft - 01-31-2014, 05:09 PM



Users browsing this thread: 5 Guest(s)