Cuberite Forum
Need help with making a "Clear Inventory on join" plugin - Printable Version

+- Cuberite Forum (https://forum.cuberite.org)
+-- Forum: Plugins (https://forum.cuberite.org/forum-1.html)
+--- Forum: Plugin Discussion (https://forum.cuberite.org/forum-8.html)
+--- Thread: Need help with making a "Clear Inventory on join" plugin (/thread-1615.html)



Need help with making a "Clear Inventory on join" plugin - Mathias - 10-07-2014

Hi!
I'm currently trying to learn lua, and I tried to make a simple plugin that clears the player's inventory when joining the server. However, I would need some help on making this plugin work (sorry for my noobishness).

Here's what I've done so far:
Code:
function Initialize(Plugin)
        PLUGIN = Plugin
        Plugin:SetName("ClearInv")
        Plugin:SetVersion(1)

        cPluginManager:AddHook(cPluginManager.HOOK_PLAYER_JOINED, MyOnPlayerJoined);

        LOG("Initialized ClearInv")
        return true
end

function OnPlayerJoined(Player)
        Player:GetInventory():Clear()
        return true
end
Thanks for your help! I really want to learn more about lua.


RE: Need help with making a "Clear Inventory on join" plugin - NiLSPACE - 10-07-2014

Change MyOnPlayerJoined to OnPlayerJoined.


RE: Need help with making a "Clear Inventory on join" plugin - Mathias - 10-07-2014

(10-07-2014, 02:26 AM)STR_Warrior Wrote: Change MyOnPlayerJoined to OnPlayerJoined.
Thank you! It worked.


RE: Need help with making a "Clear Inventory on join" plugin - Seadragon91 - 10-07-2014

(10-07-2014, 02:26 AM)STR_Warrior Wrote: Change MyOnPlayerJoined to OnPlayerJoined.

Had this problem long time ago, too. Every example for adding a hook has a My at start and the function starts without My at start, I find that confusingConfused


RE: Need help with making a "Clear Inventory on join" plugin - xoft - 10-07-2014

The "OnPlayerJoined" is the default name for the hook - it is used when you call the register function like this (old, deprecated but still working way):
cPluginManager.AddHook(cPluginManager.HOOK_PLAYER_JOINED)
This is from the times when plugins could only register a single hook callback.

When multiple callbacks were introduced, the functions were added as the parameter:
local function AnyNameReally(a_Player)
  a_Player:SendMessage("Hello")
end

cPluginManager.AddHook(cPluginManager.HOOK_PLAYER_JOINED, AnyNameReally)

You could even do this:
cPluginManager.AddHook(cPluginManager.HOOK_PLAYER_JOINED, 
  function (a_Player)
    a_Player:SendMessage("Hello")
  end
)