HOOK_PLUGINS_LOADED and Aliases
#1
[strike]Is HOOK_PLUGINS_LOADED called both when all plugins are loaded during initial startup AND when there are plugins loaded later on by command, or only during the former time?[/strike]

If it is only triggered after initial loading, what is the best way to get other plugins to realize that an API plugin has loaded if it doesn't load at startup? I'm thinking that the API plugin will loop through all plugins when it is initialized and execute a specifically named function on all of them, but that seems like it might be glitchy.

Edit: I just checked, and it is only triggered after initial loading, or reloads. Using load and unload from the console doesn't trigger it.

So, in that case. A function that gets called every few ticks could work. It would initially be called during load, then it would be called using cWorld:ScheduleTask() from within itself. In my attempts to test where to go from here, I kept encountering errors.

Code:
[spoiler]
function Initialize(Plugin)
	Plugin:SetName( "HookLoadedTest" )
	Plugin:SetVersion( 15 )

	cPluginManager:AddHook(cPluginManager.HOOK_PLUGINS_LOADED,OnPluginsLoaded)

	LOG("Initialised " .. Plugin:GetName() .. " v." .. Plugin:GetVersion())
  cRoot:Get():GetPluginManager():BindConsoleCommand("doallplugins",doAllPlugins,"")

	return true
end

function OnDisable()
	LOG( "Disabled Core!" )
end

function OnPluginsLoaded()
  LOG("Plugins loaded!asdfasdrfqwerasdfqwer")
end

function doAllPlugins(split)
  local PLMan = cRoot:Get():GetPluginManager()
  PLMan:ForEachPlugin(function(plugin)
      PLMan:CallPlugin(plugin:GetName(),mySpecialFunction)
    end
  )
end
[/spoiler]
Error:
[spoiler]
Code:
[21:03:28] LUA: Plugins/HookLoadedTest/main.lua:24: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[21:03:28] Stack trace:
[21:03:28]   [C](-1): CallPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(24): (no name)
[21:03:28]   [C](-1): ForEachPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(23): (no name)
[21:03:28] Stack trace end
[21:03:28] Error in <attached> calling function <callback>()
[21:03:28] LUA: Plugins/HookLoadedTest/main.lua:24: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[21:03:28] Stack trace:
[21:03:28]   [C](-1): CallPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(24): (no name)
[21:03:28]   [C](-1): ForEachPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(23): (no name)
[21:03:28] Stack trace end
[21:03:28] Error in <attached> calling function <callback>()
[21:03:28] LUA: Plugins/HookLoadedTest/main.lua:24: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[21:03:28] Stack trace:
[21:03:28]   [C](-1): CallPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(24): (no name)
[21:03:28]   [C](-1): ForEachPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(23): (no name)
[21:03:28] Stack trace end
[21:03:28] Error in <attached> calling function <callback>()
[21:03:28] LUA: Plugins/HookLoadedTest/main.lua:24: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[21:03:28] Stack trace:
[21:03:28]   [C](-1): CallPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(24): (no name)
[21:03:28]   [C](-1): ForEachPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(23): (no name)
[21:03:28] Stack trace end
[21:03:28] Error in <attached> calling function <callback>()
[21:03:28] LUA: Plugins/HookLoadedTest/main.lua:24: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[21:03:28] Stack trace:
[21:03:28]   [C](-1): CallPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(24): (no name)
[21:03:28]   [C](-1): ForEachPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(23): (no name)
[21:03:28] Stack trace end
[21:03:28] Error in <attached> calling function <callback>()
[21:03:28] LUA: Plugins/HookLoadedTest/main.lua:24: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[21:03:28] Stack trace:
[21:03:28]   [C](-1): CallPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(24): (no name)
[21:03:28]   [C](-1): ForEachPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(23): (no name)
[21:03:28] Stack trace end
[21:03:28] Error in <attached> calling function <callback>()
[21:03:28] LUA: Plugins/HookLoadedTest/main.lua:24: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[21:03:28] Stack trace:
[21:03:28]   [C](-1): CallPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(24): (no name)
[21:03:28]   [C](-1): ForEachPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(23): (no name)
[21:03:28] Stack trace end
[21:03:28] Error in <attached> calling function <callback>()
[21:03:28] LUA: Plugins/HookLoadedTest/main.lua:24: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[21:03:28] Stack trace:
[21:03:28]   [C](-1): CallPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(24): (no name)
[21:03:28]   [C](-1): ForEachPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(23): (no name)
[21:03:28] Stack trace end
[21:03:28] Error in <attached> calling function <callback>()
[21:03:28] LUA: Plugins/HookLoadedTest/main.lua:24: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[21:03:28] Stack trace:
[21:03:28]   [C](-1): CallPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(24): (no name)
[21:03:28]   [C](-1): ForEachPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(23): (no name)
[21:03:28] Stack trace end
[21:03:28] Error in <attached> calling function <callback>()
[21:03:28] LUA: Plugins/HookLoadedTest/main.lua:24: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[21:03:28] Stack trace:
[21:03:28]   [C](-1): CallPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(24): (no name)
[21:03:28]   [C](-1): ForEachPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(23): (no name)
[21:03:28] Stack trace end
[21:03:28] Error in <attached> calling function <callback>()
[21:03:28] LUA: Plugins/HookLoadedTest/main.lua:24: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[21:03:28] Stack trace:
[21:03:28]   [C](-1): CallPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(24): (no name)
[21:03:28]   [C](-1): ForEachPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(23): (no name)
[21:03:28] Stack trace end
[21:03:28] Error in <attached> calling function <callback>()
[21:03:28] LUA: Plugins/HookLoadedTest/main.lua:24: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[21:03:28] Stack trace:
[21:03:28]   [C](-1): CallPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(24): (no name)
[21:03:28]   [C](-1): ForEachPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(23): (no name)
[21:03:28] Stack trace end
[21:03:28] Error in <attached> calling function <callback>()
[21:03:28] LUA: Plugins/HookLoadedTest/main.lua:24: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[21:03:28] Stack trace:
[21:03:28]   [C](-1): CallPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(24): (no name)
[21:03:28]   [C](-1): ForEachPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(23): (no name)
[21:03:28] Stack trace end
[21:03:28] Error in <attached> calling function <callback>()
[21:03:28] LUA: Plugins/HookLoadedTest/main.lua:24: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[21:03:28] Stack trace:
[21:03:28]   [C](-1): CallPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(24): (no name)
[21:03:28]   [C](-1): ForEachPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(23): (no name)
[21:03:28] Stack trace end
[21:03:28] Error in <attached> calling function <callback>()
[21:03:28] LUA: Plugins/HookLoadedTest/main.lua:24: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[21:03:28] Stack trace:
[21:03:28]   [C](-1): CallPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(24): (no name)
[21:03:28]   [C](-1): ForEachPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(23): (no name)
[21:03:28] Stack trace end
[21:03:28] Error in <attached> calling function <callback>()
[21:03:28] LUA: Plugins/HookLoadedTest/main.lua:24: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[21:03:28] Stack trace:
[21:03:28]   [C](-1): CallPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(24): (no name)
[21:03:28]   [C](-1): ForEachPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(23): (no name)
[21:03:28] Stack trace end
[21:03:28] Error in <attached> calling function <callback>()
[21:03:28] LUA: Plugins/HookLoadedTest/main.lua:24: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[21:03:28] Stack trace:
[21:03:28]   [C](-1): CallPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(24): (no name)
[21:03:28]   [C](-1): ForEachPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(23): (no name)
[21:03:28] Stack trace end
[21:03:28] Error in <attached> calling function <callback>()
[21:03:28] LUA: Plugins/HookLoadedTest/main.lua:24: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[21:03:28] Stack trace:
[21:03:28]   [C](-1): CallPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(24): (no name)
[21:03:28]   [C](-1): ForEachPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(23): (no name)
[21:03:28] Stack trace end
[21:03:28] Error in <attached> calling function <callback>()
[21:03:28] LUA: Plugins/HookLoadedTest/main.lua:24: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[21:03:28] Stack trace:
[21:03:28]   [C](-1): CallPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(24): (no name)
[21:03:28]   [C](-1): ForEachPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(23): (no name)
[21:03:28] Stack trace end
[21:03:28] Error in <attached> calling function <callback>()
[21:03:28] LUA: Plugins/HookLoadedTest/main.lua:24: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[21:03:28] Stack trace:
[21:03:28]   [C](-1): CallPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(24): (no name)
[21:03:28]   [C](-1): ForEachPlugin
[21:03:28]   Plugins/HookLoadedTest/main.lua(23): (no name)
[21:03:28] Stack trace end
[21:03:28] Error in <attached> calling function <callback>()
[/spoiler]
Attempted with and without :GetName() and using the full cRoot:Get... instead of PLMan. So explanation of what I've done wrong would be great.


Now onto command Aliases (as defined in Info.lua). I know that things go wrong if two plugins share the same base name for a function (like both using /ext). What about if Plugin A registers their command as "/command1" but Plugin B registers it as "/asdfasdfcommand1" with alias "/command1"? Are aliases handled differently then names, or are they all stored in the same way?
Reply
Thanks given by:
#2
what happened with your code?

("Plugins loaded!asdfasdrfqwerasdfqwer")
PLMan:CallPlugin(plugin:GetName(),mySpecialFunction)

and I thought you can't Get() the cRoot because only one exists
Reply
Thanks given by:
#3
(12-03-2015, 04:04 PM)Schwertspize Wrote: what happened with your code?

("Plugins loaded!asdfasdrfqwerasdfqwer")
PLMan:CallPlugin(plugin:GetName(),mySpecialFunction)

and I thought you can't Get() the cRoot because only one exists

Not sure what you mean in the first part. The asdfqwe... string is properly logged whenever all plugins load at start or after reload. The second strip of code is 'completely' unrelated.

Second part:
Quote:There is one instance of cPluginManager in Cuberite, to get it, call either cRoot:Get():GetPluginManager() or cPluginManager:Get() function.
Right from the API docs.

cPluginManager:Get() gives the error

doallplugins
[00:26:13] Executing console command: "doallplugins"
[00:26:13] LUA: Plugins/HookLoadedTest/main.lua:22: Attempt to call a non-callable object.
[00:26:13] Stack trace:
[00:26:13] [C](-1): cPluginManager
[00:26:13] Plugins/HookLoadedTest/main.lua(22): (no name)
[00:26:13] Stack trace end
[00:26:13] Error in plugin HookLoadedTest calling function <callback>()

cRoot:Get():GetPluginManager():Get() gives the error

doallplugins
[00:27:34] Executing console command: "doallplugins"
[00:27:34] LUA: Plugins/HookLoadedTest/main.lua:22: error in function 'Get'.
argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[00:27:34] Stack trace:
[00:27:34] [C](-1): Get
[00:27:34] Plugins/HookLoadedTest/main.lua(22): (no name)
[00:27:34] Stack trace end
[00:27:34] Error in plugin HookLoadedTest calling function <callback>()
[00:27:34] Unknown command, type 'help' for all commands.

Edit: I JUST SAW THE STATIC THING. Testing to make sure I fixed my idiot move.
Edit2: Nope. . syntax just yelled at me for the rather obvious reason.
Reply
Thanks given by:
#4
Try this:
cPluginManager:Get():ForEachPlugin(
	function(a_Plugin)
		cPluginManager:CallPlugin(a_Plugin:GetName(), mySpecialFunction)
	end
)
Also, are you sure a function exists in all plugins with the content of "mySpecialFunction"?
Reply
Thanks given by:
#5
(12-03-2015, 05:29 PM)NiLSPACE Wrote: Try this:
cPluginManager:Get():ForEachPlugin(
	function(a_Plugin)
		cPluginManager:CallPlugin(a_Plugin:GetName(), mySpecialFunction)
	end
)
Also, are you sure a function exists in all plugins with the content of "mySpecialFunction"?


Ignore this:

Swapped to a server where my plugin is the ONLY plugin.

[ spoiler="The new code"]
function Initialize(Plugin)
	Plugin:SetName( "HookLoadedTest" )
	Plugin:SetVersion( 15 )

	cPluginManager:AddHook(cPluginManager.HOOK_PLUGINS_LOADED,OnPluginsLoaded)

	LOG("Initialised " .. Plugin:GetName() .. " v." .. Plugin:GetVersion())
  cRoot:Get():GetPluginManager():BindConsoleCommand("doallplugins",doAllPlugins,"")

	return true
end

function OnDisable()
	LOG( "Disabled Core!" )
end

function OnPluginsLoaded()
  LOG("Plugins loaded!asdfasdrfqwerasdfqwer")
end

function doAllPlugins(split)
--[[
  local PLMan = cRoot:Get():GetPluginManager()
  PLMan:ForEachPlugin(testfunction)
--]]
cPluginManager:Get():ForEachPlugin(
    function(a_Plugin)
        cPluginManager:CallPlugin(a_Plugin:GetName(), "mySpecialFunction")
    end
)
end

function mySpecialFunction()
  return true
end

function testfunction(plugin)
  cRoot:Get():GetPluginManager():CallPlugin(plugin:GetName(),"mySpecialFunction")
end
[ /spoiler]

And the new... interesting error:

Code:
doallplugins
[01:49:18] Executing console command: "doallplugins"
[01:49:18] cPluginManager::CallPlugin(): Calling self is not implemented (why would it?)
[01:49:18] Stack trace:
[01:49:18]   [C](-1): CallPlugin
[01:49:18]   Plugins/HookLoadedTest/main.lua(28): (no name)
[01:49:18]   [C](-1): ForEachPlugin
[01:49:18]   Plugins/HookLoadedTest/main.lua(26): (no name)
[01:49:18] Stack trace end
[01:49:18] Unknown command, type 'help' for all commands.

Same error if I copy the same code to a new plugin (and remove the command binding from the second one). Adding :Get() in the second part results in this:

[ spoiler]
Code:
doallplugins
[01:54:56] Executing console command: "doallplugins"
[01:54:56] LUA: Plugins/HookLoadedTest/main.lua:28: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[01:54:56] Stack trace:
[01:54:56]   [C](-1): CallPlugin
[01:54:56]   Plugins/HookLoadedTest/main.lua(28): (no name)
[01:54:56]   [C](-1): ForEachPlugin
[01:54:56]   Plugins/HookLoadedTest/main.lua(26): (no name)
[01:54:56] Stack trace end
[01:54:56] Error in <attached> calling function <callback>()
[01:54:56] LUA: Plugins/HookLoadedTest/main.lua:28: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[01:54:56] Stack trace:
[01:54:56]   [C](-1): CallPlugin
[01:54:56]   Plugins/HookLoadedTest/main.lua(28): (no name)
[01:54:56]   [C](-1): ForEachPlugin
[01:54:56]   Plugins/HookLoadedTest/main.lua(26): (no name)
[01:54:56] Stack trace end
[01:54:56] Error in <attached> calling function <callback>()
[01:54:56] LUA: Plugins/HookLoadedTest/main.lua:28: error in function 'CallPlugin'.
     argument #1 is 'cPluginManager'; 'cPluginManager' expected.

[01:54:56] Stack trace:
[01:54:56]   [C](-1): CallPlugin
[01:54:56]   Plugins/HookLoadedTest/main.lua(28): (no name)
[01:54:56]   [C](-1): ForEachPlugin
[01:54:56]   Plugins/HookLoadedTest/main.lua(26): (no name)
[01:54:56] Stack trace end
[01:54:56] Error in <attached> calling function <callback>()
[01:54:56] Unknown command, type 'help' for all commands.
[ /spoiler]

Moving the callback to a named function only affects the related line numbers in the errors.

Edit: The interesting error is the obvious error! I think it actually works. Doing one last test to verify.

Edit2: yeah I was being a derp in the second part. The "interesting" error was just saying "I won't let you call a function within your own code" which makes sense. Doing more testing, but I think this will work.

Final Edit: It continues to work even on a server with a number of plugins that don't have "mySpecialFunction", although it does spam console with errors. Is there a way to suppress console error messages?
Reply
Thanks given by:
#6
The error message about wanting a cPluginManager instead of a cPluginManager is super-confusing, we should try to edit ToLua++ to produce a better diagnostic.
Reply
Thanks given by:
#7
(12-03-2015, 06:08 PM)xoft Wrote: The error message about wanting a cPluginManager instead of a cPluginManager is super-confusing, we should try to edit ToLua++ to produce a better diagnostic.

Hopefully that can happen. But at least now that I've seen this here, I can hopefully figure this sort of thing out in the future if it ever happens.
Reply
Thanks given by:




Users browsing this thread: 5 Guest(s)