-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpluginAcme.php
More file actions
executable file
·168 lines (143 loc) · 6.68 KB
/
pluginAcme.php
File metadata and controls
executable file
·168 lines (143 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace ManiaLivePlugins\pluginAcme;
use ManiaLivePlugins\eXpansion\Core\types\BasicPlugin;
/**
* This is a development plugin to learn how to create plugins for expansion.
*
* @see https://github.com/eXpansionPluginPack/eXpansion/wiki/Developpers-:-First%20plugin
*
* Class Acme
* @package ManiaLivePlugins\eXpansion\Acme
*/
class pluginAcme extends BasicPlugin
{
/**
* @var Config
*/
private $config;
/**
* This method is called when the plugin is first initialized. Very early in the loading process.
*/
public function exp_onInit()
{
// Define a version : This is will be displayed to the user in the plugin list.
$this->setVersion('0.1.0');
// Get the configuration object. You might as well get it whanever you need it.
$this->config = Config::getInstance();
}
/**
* Once the plugin initialized it will be loaded. This plugin is ready but other plugins might not be.
*/
public function exp_onLoad()
{
}
/**
* Once all plugins are loaded and the system is ready.
*/
public function exp_onReady()
{
// When all is ready we can register a chat command :
/**
* The command the user will need to use is /hello, it will call the sayHello method of this class.
*
* The command can take an unlimited amount of parameters(0 will mean no parameters,
* 1 that it needs one parameter ...)
*
* And finally the login of the user that used the command will be sent to the method.
*
* @see sayHello
*/
$this->registerChatCommand('hello_acme', "sayHello", -1, true);
// We will also send a widget to all players.
//First we need to create our Acme Widget. We use the static method Create of our window for that.
$widget = Gui\Widgets\acmeWidget::Create();
// We also need to set a position, to display it at.
$widget->setPosition(0,70);
/*
* Finally we will show it. We have not precised to whom we are showing the widget, we show it to 'null'
* that means it is going to be sent to everyone without exception
*/
$widget->show();
/*
* If we wished to send it to a specific player we should have passed in parameter the login of the player :
* $widget->show('player_login');
*
* We can also send it to multiple players at the same time using an array
* $widget->show(array('player_login', 'player_login2');
*/
}
/**
* Method call when an user uses the hello chat command.
*
* @param string $login
* The login of the user that used the command.
*
* @param string $params
* All the parameters passed to the hello command. for hello toto tata we will have 'toto tata' string)
*/
public function sayHello($login, $params = "") {
// The user might wish to say hello to everyone, and not one person.
if (empty($params)) {
/**
* We will send the message to everyone, so to null. But this won't work well with translations. The method
* will search for translations but the string here is dynamic and might change
* depending on the configuration.
*/
// $this->exp_chatSendServerMessage('Hello ' . $this->config->who, null);
/**
* That is why we will use this method to send the chat. Here we are using one string with a "token" (%1$s)
* which means the first element in the array will replace it. So the end text will be "Hello world" but
* when being translated it will try to translate "Hello %1$s". That will be translated in french as
* "Bonjour %1$s" and when the token is processes it will become "Bonjour world".
*
* @see https://github.com/eXpansionPluginPack/eXpansion/wiki/Developpers-:-i18n-support
*/
$this->exp_chatSendServerMessage('Hello %1$s', null, array($this->config->who));
} else {
// Break the params into multiple parts.
$args = explode(' ', $params);
// We are sending one message to each player
foreach ($args as $playerLogin) {
// Check if the player exists.
$player = $this->storage->getPlayerObject($playerLogin);
if (!is_null($player)) {
/**
* The player exist. We will use the same method as before. But this time we send it to the player
* so not to "null" and we use the player nickname instead of the configuration value.
*/
$this->exp_chatSendServerMessage('Hello %1$s', $playerLogin, array($player->nickName));
} else {
/**
* The player can't be found, we need to send an error to the user that used the chat command.
* Have you noticed how dull our messages has been? So this code will work it won't look nice.
* We need to add some color.
*/
//$this->exp_chatSendServerMessage('Player %1$s isn\'t on the server. Can\'t say hello', $login, array($playerLogin));
/**
* For that we won't use color codes but some different tokens, that starts and end with #
* We are using #error# which is basically red text & #variable# that is white to differentiate the
* login of the non existing player.
*
* All color codes are defined here :
* @see ManiaLivePlugins\eXpansion\Core\Config
* They are prefixed by Colors_ and can be configured in game. Check the wiki for more information :
* @see https://github.com/eXpansionPluginPack/eXpansion/wiki/Developpers-:-Chat-Colors
*/
$this->exp_chatSendServerMessage('#error#Player #variable#%1$s #error#isn\'t on the server. Can\'t say hello', $login, array($playerLogin));
}
}
}
}
/**
* When the plugin is uninstalled. It is important to destroy all windows that the plugin might have opened
* when it was running in order to handle memory. Keep in mind that plugins can be started & stopped quite often
* and it the most abvious place for memory leaks.
*
* Some eXpansion plugins don't do it properly because they are part of the core and can't be unloaded anyway.
*/
public function exp_onUnload()
{
Gui\Widgets\acmeWidget::EraseAll();
}
}
?>