Posted by
| Nick Gammon
Australia (22,975 posts) bio
Forum Administrator |
Message
| 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.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|