Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 06-23-2008, 05:24 AM
TheStan TheStan is offline
Stan?
Join Date: May 2008
Location: U.S.
Posts: 100
TheStan is on a distinguished road
Send a message via AIM to TheStan Send a message via MSN to TheStan
Plugin Handler

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:

PHP Code:
/* 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(pluginstypename) {
  
// 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 (pplugins) {
        
// 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);
          (@ 
"_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 (pplugins) {
        
// 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.
            
(@ "_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 (pplugins) {
        
// 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.
          
(@ "_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 (pplugins) {
        
// 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:

PHP Code:
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!
Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 10:18 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.