Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #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
  #2  
Old 06-23-2008, 02:51 PM
DrakilorP2P DrakilorP2P is offline
Registered User
DrakilorP2P's Avatar
Join Date: Apr 2006
Posts: 755
DrakilorP2P is just really niceDrakilorP2P is just really nice
Why?
Reply With Quote
  #3  
Old 06-23-2008, 06:39 PM
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
Quote:
Originally Posted by DrakilorP2P View Post
Why?
Why what Drak? :o
Reply With Quote
  #4  
Old 06-24-2008, 02:18 AM
zokemon zokemon is offline
That one guy...
zokemon's Avatar
Join Date: Mar 2001
Location: Sonoma County, California
Posts: 2,925
zokemon is a jewel in the roughzokemon is a jewel in the rough
Send a message via ICQ to zokemon Send a message via AIM to zokemon Send a message via MSN to zokemon Send a message via Yahoo to zokemon
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() .
__________________
Do it with a DON!
Reply With Quote
  #5  
Old 06-24-2008, 06:34 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
Quote:
Originally Posted by zokemon View Post
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!
Reply With Quote
  #6  
Old 06-24-2008, 07:32 AM
Tigairius Tigairius is offline
The Cat
Tigairius's Avatar
Join Date: Jan 2007
Location: Missouri, USA
Posts: 4,240
Tigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant future
Ahhh tooo much commenting!!
__________________


“Shoot for the moon. Even if you miss, you'll land among the stars.”
Reply With Quote
  #7  
Old 06-24-2008, 05:12 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Tigairius View Post
Ahhh tooo much commenting!!
Poor Tig
__________________
Reply With Quote
  #8  
Old 06-24-2008, 03:24 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Well, we'll release an updated version soon (with less commenting and more examples)
__________________
Reply With Quote
Reply


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:20 AM.


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