PDA

View Full Version : Plugin Handler


TheStan
06-23-2008, 06:24 AM
Recently, I've found myself working with Chompy on a special little project which will more than like be widely released for open use a wee bit later. However, in the midst of the project we found ourselves in need of the usage of plugins. Therefore, I created a handler to control plugins to objects. :)

First this is the plugin handler class:

/* Plugin Handler */
/*
Vars:
this.plugins: List of connected plugins
this.activeplugins: List of activated plugins

Server Vars:
serverr.plugins.<typename> = {plugin, plugin2}

Ex.:
serverr.plugins.TestWeapon = {testclass, testclass2}


Functions:

- addPlugin(plugins, typename)
|
- plugins: array or string of the plugin(s) needing added
|
- typname: basically the defining variable of what list of plugins to check in (serverr.plugins.<typename>)

- removePlugin(plugins)
|
- plugins: array or string of the plugin(s) needing added

- activatePlugin(plugins)
|
- plugins: array or string of the plugin(s) to activate

- deactivatePlugin(plugins)
|
- plugins: array or string of the plugin(s) to deactivate
*/
public function addPlugin(plugins, typename) {
// The "plugins" param is capable of taking one plugin
// (a string) or a list (an array).
switch (plugins.type()) {
// If it's a string
case 1:
// Check if the plugin is in the list of allowed
// plugins for that typename.
if (plugins in serverr.plugins.(@ typename)) {
// Join the plugin as a class and add to
// "this.plugins" array. Call the custom
// "classname_onCreated()" event on the plugin.
this.join(plugins);
this.plugins.add(plugins);
(@ plugins @ "_onCreated")();
}
break;

// If it's an array
case 3:
// Go through the plugins
for (p: plugins) {
// Check if the plugin is in the list of allowed
// plugins for that typename.
if (p in serverr.plugins.(@ typename)) {
// Join the plugin as a class and add to
// "this.plugins" array. Call the custom
// "classname_onCreated()" event on the plugin.
this.join(p);
this.plugins.add(p);
(@ p @ "_onCreated")();
}
}
break;
}
// Called function "activatePlugin()" to make sure all plugins
// are activated.
activatePlugin(plugins);
}
public function removePlugin(plugins) {
// Once again like in the "addPlugin()" function the "plugins"
// param can be either one (a string) or a list (an array).
// However, in this case the "typename" isn't really necessary
// the only necessity is that the plugin is already plugged in
// and added to the "this.plugins" array.
switch (plugins.type()) {
// If it's a string
case 1:
// Check if the plugin is listed in "this.plugins" array.
if (plugins in this.plugins) {
// Check if the plugin is active just in case it is.
if (plugins in this.activeplugins) {
// If so call the custom "classname_deactivate()"
// function. Then remove the plugin from
// "this.activeplugins" array.
(@ plugins @ "_deactivate")();
this.activeplugins.remove(plugins);
}
// Remove the class. "leave()" is retarded. :) And
// remove the plugin from "this.plugins" array.
this.joinedclasses.remove(plugins);
this.plugins.remove(plugins);
}
break;

// If it's an array
case 3:
// Go through each plugin
for (p: plugins) {
// Check if the plugin is in "this.plugins" array.
if (p in this.plugins) {
// Check if the plugin is in "this.activeplugins"
// array.
if (p in this.activeplugins) {
// If so call that infamous custom
// "classname_deactivate()" function. Then remove
// the plugin from "this.activeplugins" array.
(@ p @ "_deactivate")();
this.activeplugins.remove(p);
}
// Remove the class. And then remove the plugin
// from "this.plugins" array.
this.joinedclasses.remove(p);
this.plugins.remove(p);
}
}
break;
}
}
public function activatePlugin(plugins) {
// That ever so irritating check to see if "plugins" param
// is either a string or array.
switch (plugins.type()) {
// You should know what's going on right here by now.
// If you don't read one of the former switch statements.
// :)
case 1:
// Here I am checking to see if the plugin is in the
// "this.plugins array" and is NOT in the
// "this.activeplugins" array. Which makes it not active.
// :D
if (plugins in this.plugins && !(plugins in this.activeplugins)) {
// Time to active this plugin. To do this we call
// the un-infamous "classname_activate()" function
// on the plugin then add it to the "this.activeplugins"
// array.
(@ plugins @ "_activate")();
this.activeplugins.add(plugins);
}
break;

// Read above! Lmao. :)
case 3:
// Go through each plugin in the "plugin" param.
for (p: plugins) {
// Checking if it's in the "this.plugins" array but,
// is NOT in the "this.activeplugins" array.
if (p in this.plugins && !(p in this.activeplugins)) {
// Calling that frisky "classname_activate()"
// function then adding the plugin to the
// "this.activeplugins" array.
(@ p @ "_activate")();
this.activeplugins.add(p);
}
}
break;
}
}
public function deactivatePlugin(plugins) {
// You should know by now I'm checking whether or not
// the "plugins" param is a string or array. ;o
switch (plugins.type()) {
// Checking for string
case 1:
// Checking if the plugin is listed in the
// "this.plugins" array AND the "this.activeplugins"
// array.
if (plugins in this.plugins && plugins in this.activeplugins) {
// Call that famous "classname_deactivate()" function
// on the plugin then remove it from the
// "this.activeplugins" array.
(@ plugins @ "_deactivate")();
this.activeplugins.remove(plugins);
}
break;

// Checking for array
case 3:
// Of course going through the plugins.
for (p: plugins) {
// Checking if the plugin is listed in the
// "this.plugins" array AND the "this.activeplugins"
// array.
if (p in this.plugins && p in this.activeplugins) {
// Call that famous "classname_deactivate()" function
// on the plugin then remove it from the
// "this.activeplugins" array.
(@ plugins @ "_deactivate")();
this.activeplugins.remove(p);
}
}
break;
}
}


Secondly, I will show you an example of a "plugin" class:

public function classexample_onCreated() {
echo("This class example has been created!");
}
public function classexample_activate() {
echo("This class example has been activated!");
}
public function classexample_deactivate() {
echo("This class example has been deactivated!");
}

There isn't really much to explaining it because the comments have done well enough to explain the usage of every function and how it works. At least, I'd like to think so. :)

All questions welcome!

DrakilorP2P
06-23-2008, 03:51 PM
Why?

TheStan
06-23-2008, 07:39 PM
Why?

Why what Drak? :o

zokemon
06-24-2008, 03:18 AM
onCreated() is a special function when ti comes to classes in the sense that it will not replace the onCreated of the npc but rather will be called upon joining the class to the object. So you don't really need classexample_onCreated() :).

TheStan
06-24-2008, 07:34 AM
onCreated() is a special function when ti comes to classes in the sense that it will not replace the onCreated of the npc but rather will be called upon joining the class to the object. So you don't really need classexample_onCreated() :).

Lol, oh I know Zero! I just liked the way that looks!

Tigairius
06-24-2008, 08:32 AM
Ahhh tooo much commenting!!

Chompy
06-24-2008, 04:24 PM
Well, we'll release an updated version soon (with less commenting and more examples) ^^

cbk1994
06-24-2008, 06:12 PM
Ahhh tooo much commenting!!

Poor Tig :(