Page 1 of 1

custom plugin events and working sqlite explorer

Posted: 04 Jan 2011, 02:40
by reaby
Custom plugin events ?
Well i managed to to this tonight after 3~4 hours of trial & error, as i'm totally noob with this new object orientated programming thingy. hopefully we get official support for plugin events for final version of manialive, as this can be ground breaking :)

for example i did a custom event for onCopperDonation...
you need to do new event class onCopperDonate.php

it has the class as following:

Code: Select all

namespace [define the namespace here for autoload]

class onCopperDonate extends \ManiaLive\Event\Event
{
	protected $login;
	protected $amount;
	
	function __construct($login, $amount)
	{
		$this->login = $login;
		$this->amount = $amount;
	}
	
	function fireDo($listener)
	{
		call_user_func_array(array($listener, 'onCopperDonate'), array($this->login, $this->amount));
	}
}

at CopperDonate.php you add this new class

Code: Select all

use ManiaLive\Event\Dispatcher;
use ManiaLivePlugins\MLEPP\DonateCoppers\Events\onCopperDonate;
and at the point, when you want the event to happen, command

Code: Select all

Dispatcher::dispatch(new onCopperDonate($login,$amount));
Then to the plugins where you want the event to happen...

Code: Select all

use ManiaLive\Event\Dispatcher;

function onInit() {
     if ($this->isPluginLoaded('MLEPP\DonateCoppers', 0.1)) {
	Dispatcher::register(\ManiaLivePlugins\MLEPP\DonateCoppers\Events\onCopperDonate::getClass(), $this);
      }	
}

function onCopperDonate($login, $amount) {
$this->sendChat("Hahaha, plugin interaction, and so $login gives $amount of coppers!");
}

Sqlite Browser for sqlite 2.1
The program version must be this 1.1, which is ancient, but it is freeware and works with manialive database.
http://mesh.dl.sourceforge.net/project/ ... .1-win.zip


now go, make new plugins,
-- Reaby

Re: custom plugin events and working sqlite explorer

Posted: 04 Jan 2011, 21:14
by oliverde8
Very nice thanks, I won't need to search to se how it works :)

Re: custom plugin events and working sqlite explorer

Posted: 05 Jan 2011, 10:08
by farfa
Congrats reaby it's almost this. you have understood how it works, but this is just a way to be more efficient ;)
Just something to optimize your code. With this Event, you have to create one class for each event you want to fire. It's just a bit painful (you'll see ;) ) What I recommend, is inspired of our usage of Event.
Create an Event class in your namespace with different constants, for example in \ManiaLive\ Data\Event we have:

Code: Select all

const ON_PLAYER_NEW_BEST_TIME = 1;
const ON_PLAYER_NEW_RANK = 2;
const ON_PLAYER_NEW_BEST_SCORE = 3;
Each constant is a particular event. To fire them, we use the following method

Code: Select all

function fireDo($listener)
{
	$method = null;
	
	switch($this->onWhat)
	{
		case self::ON_PLAYER_NEW_BEST_TIME: $method = 'onPlayerNewBestTime'; break;
		case self::ON_PLAYER_NEW_RANK: $method = 'onPlayerNewRank'; break;
		case self::ON_PLAYER_NEW_BEST_SCORE: $method = 'onPlayerNewBestScore'; break;
	}
	
	if ($method != null)
		call_user_func_array(array($listener, $method), $this->params);
}
For the full code, see https://code.google.com/p/manialive/sou ... /Event.php