Enable/Disable plugin's automatically.

Posted by LupusFatalis on Thu 23 Apr 2009 07:16 PM — 7 posts, 30,859 views.

#0
Ok, so I've been making a ton of little plugins for learning skills and so forth, to automate the process. MUSHclient doesn't seem to store the last state of the plugin, i.e. enabled/disabled. So what I'd like to do, is make all of my learning plugins disable themselves before I connect to the mud. This way I don't log in and immediately start trying to chop down trees, cast spells, and kill npcs all at once, lol... Any ideas on how to accomplish this?

As an aside, I imagine a response to this will also answer my question as to how to have a plugin disable itself once it completes a given task.

Thanks again folks.
Netherlands #1
You are probably looking at the wrong 'level' of things for what you want to do. Enabling and disabling plugins is meant for users, not for your script.

What you want to do is to give the relevant triggers, aliases and timers a group attribute, and then use EnableGroup to enable or disable them when you need to.

This could be in the OnPluginConnect() callback, for example:


function OnPluginConnect()
  EnableTrigger("woodcutting", false)  -- disable woodcutting.
end
Australia Forum Administrator #2
It doesn't remember the enabled state, but you can add that in, like this:


function OnPluginInstall ()

  -- other installation stuff here
  
  if GetVariable ("enabled") == "false" then
    ColourNote ("yellow", "", "Warning: Plugin " .. GetPluginName ().. " is currently disabled.")
    EnablePlugin (GetPluginID (), false)
    return
  end -- they didn't enable us last time
   

  -- if we didn't return (ie. we are enabled) then show windows etc.

end -- OnPluginInstall



function OnPluginSaveState ()

  -- remember if this plugin is disabled

  SetVariable ("enabled",     tostring (GetPluginInfo (GetPluginID (), 17)))

  -- other save state stuff here

end -- OnPluginSaveState


This saves in the state file whether or not you currently have it enabled, and next time it loads, it disables itself if necessary.

For this to work you need "save_state" to be "y" in the plugin header, like this:


<plugin
   name="Icon_Bar"
   author="Nick Gammon"
   id="ede0920fc1173d5a03140f0e"
   language="Lua"
   purpose="Shows a bar of buttons you can click on to do things"
   date_written="2009-02-26 09:00"
   date_modified="2009-04-02 12:15"
   requires="4.37"
   save_state="y"
   version="4.0"
   >

Amended on Fri 24 Apr 2009 04:50 AM by Nick Gammon
Australia Forum Administrator #3
If you just want a plugin to auto-disable on connect, then just do this:


function OnPluginConnect()
  EnablePlugin (GetPluginID (), false)  -- disable me
end

#4
Exactly what I needed Nick... I might tinker with the first one, but the second bit is all I want in this case... For anyone who cares--in python it looks like...
def OnPluginConnect():
	world.EnablePlugin (world.GetPluginID, 'false')
#5
I'm completely new to this whole MUSH/plug-in thing and i was just wondering where you would put that code in? On its own or add it to the end of the plug-in you want to disable?

Thanks!
Australia Forum Administrator #6
I'll do an example ...

Let's start with a simple, existing plugin that doesn't have the enable/disable code in it:


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Auto_Save"
   author="Nick Gammon"
   id="3ea0bfc4db127153568ff673"
   language="Lua"
   purpose="Saves the world file periodically"
   date_written="2009-02-25 17:50:10"
   requires="4.00"
   version="1.0"
   >
<description trim="y">
<![CDATA[
Saves the world file periodically.
Default is every 5 minutes.
]]>
</description>

</plugin>


<!--  Timers  -->

<timers>
  <timer 
    enabled="y" 
    minute="5" 
    send_to="12"
>
  <send>

if GetInfo (111) then  -- world file changed?
  if Save () == false then
    ColourNote ("green", "", "World file saved.")
  else
    ColourNote ("white", "red", "Couldn't save world file.")
  end -- if
end -- if world file changed

</send>

  </timer>
</timers>

</muclient>



This plugin (I think it ships with MUSHclient) automatically saves the world every 5 minutes.

This particular plugin does not have a <script> section (a lot will have), so we first need to add that in:


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Auto_Save"
   author="Nick Gammon"
   id="3ea0bfc4db127153568ff673"
   language="Lua"
   purpose="Saves the world file periodically"
   date_written="2009-02-25 17:50:10"
   requires="4.00"
   version="1.0"
   >
<description trim="y">
<![CDATA[
Saves the world file periodically.
Default is every 5 minutes.
]]>
</description>

</plugin>


<!--  Timers  -->

<timers>
  <timer 
    enabled="y" 
    minute="5" 
    send_to="12"
>
  <send>

if GetInfo (111) then  -- world file changed?
  if Save () == false then
    ColourNote ("green", "", "World file saved.")
  else
    ColourNote ("white", "red", "Couldn't save world file.")
  end -- if
end -- if world file changed

</send>

  </timer>
</timers>


<!--  Script  -->


<script>
<![CDATA[


]]>
</script>


</muclient>



A lot of plugins will already have a <script> section so in that case you would just find the <script> and </script> tags to find where it is (usually towards the bottom of the plugin).

Note that the <script> section goes before the final </muclient> tag.

Now to change the plugin so it disables itself when you connect (not when it is installed) add the lines from a couple of posts up:


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Auto_Save"
   author="Nick Gammon"
   id="3ea0bfc4db127153568ff673"
   language="Lua"
   purpose="Saves the world file periodically"
   date_written="2009-02-25 17:50:10"
   requires="4.00"
   version="1.0"
   >
<description trim="y">
<![CDATA[
Saves the world file periodically.
Default is every 5 minutes.
]]>
</description>

</plugin>


<!--  Timers  -->

<timers>
  <timer 
    enabled="y" 
    minute="5" 
    send_to="12"
>
  <send>

if GetInfo (111) then  -- world file changed?
  if Save () == false then
    ColourNote ("green", "", "World file saved.")
  else
    ColourNote ("white", "red", "Couldn't save world file.")
  end -- if
end -- if world file changed

</send>

  </timer>
</timers>

<!--  Script  -->


<script>
<![CDATA[


function OnPluginConnect()
  EnablePlugin (GetPluginID (), false)  -- disable me
end


]]>
</script>

</muclient>



The lines for the OnPluginConnect function are added before the </script> tag (in other words, inside the script section.

However to make the plugin "remember its enabled state" we need to do a bit more.



<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Auto_Save"
   author="Nick Gammon"
   id="3ea0bfc4db127153568ff673"
   language="Lua"
   purpose="Saves the world file periodically"
   date_written="2009-02-25 17:50:10"
   requires="4.00"
   save_state="y"
   version="1.0"
   >
<description trim="y">
<![CDATA[
Saves the world file periodically.
Default is every 5 minutes.
]]>
</description>

</plugin>


<!--  Timers  -->

<timers>
  <timer 
    enabled="y" 
    minute="5" 
    send_to="12"
>
  <send>

if GetInfo (111) then  -- world file changed?
  if Save () == false then
    ColourNote ("green", "", "World file saved.")
  else
    ColourNote ("white", "red", "Couldn't save world file.")
  end -- if
end -- if world file changed

</send>

  </timer>
</timers>

<!--  Script  -->


<script>
<![CDATA[


function OnPluginInstall ()

  -- other installation stuff here
  
  if GetVariable ("enabled") == "false" then
    ColourNote ("yellow", "", "Warning: Plugin " .. GetPluginName ().. " is currently disabled.")
    EnablePlugin (GetPluginID (), false)
    return
  end -- they didn't enable us last time
   

  -- if we didn't return (ie. we are enabled) then show windows etc.

end -- OnPluginInstall



function OnPluginSaveState ()

  -- remember if this plugin is disabled

  SetVariable ("enabled",     tostring (GetPluginInfo (GetPluginID (), 17)))

  -- other save state stuff here

end -- OnPluginSaveState


]]>
</script>

</muclient>



I have added the "save_state" line to the plugin header (near the top) and two extra functions instead of the one earlier. In OnPluginInstall we get the state from last time, and if it was disabled, we disable ourselves now. In OnPluginSaveState we remember whether we are currently disabled or not.

It is possible those functions themselves already exist (you need to check these things on a case-by-case basis). If so, the relevant lines inside the functions need to be added to the existing OnPluginInstall and OnPluginSaveState functions.