summaryrefslogtreecommitdiff
path: root/src/plugins/sample.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/sample.c')
-rw-r--r--src/plugins/sample.c96
1 files changed, 73 insertions, 23 deletions
diff --git a/src/plugins/sample.c b/src/plugins/sample.c
index fea25514b..1ddecdc66 100644
--- a/src/plugins/sample.c
+++ b/src/plugins/sample.c
@@ -1,6 +1,24 @@
-// Copyright (c) Hercules Dev Team, licensed under GNU GPL.
-// See the LICENSE file
-// Sample Hercules Plugin
+/**
+ * This file is part of Hercules.
+ * http://herc.ws - http://github.com/HerculesWS/Hercules
+ *
+ * Copyright (C) 2013-2015 Hercules Dev Team
+ *
+ * Hercules is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/// Sample Hercules Plugin
#include "common/hercules.h" /* Should always be the first Hercules file included! (if you don't make it first, you won't be able to use interfaces) */
#include "common/memmgr.h"
@@ -40,6 +58,8 @@ struct sample_data_struct {
unsigned int someNumber;
};
+int my_setting;
+
/* sample packet implementation */
/* cmd 0xf3 - it is a client-server existent id, for clif_parse_GlobalMessage */
/* in this sample we do nothing and simply redirect */
@@ -111,10 +131,31 @@ int my_pc_dropitem_post(int retVal, struct map_session_data *sd,int *n,int *amou
}
return 1;
}
-void parse_my_setting(const char *val) {
- ShowDebug("Received 'my_setting:%s'\n",val);
+/*
+* Key is the setting name in our example it's 'my_setting' while val is the value of it.
+* this way you can manage more than one setting in one function instead of define multiable ones
+*/
+
+void parse_my_setting(const char *key, const char *val) {
+ ShowDebug("Received '%s:%s'\n",key,val);
/* do anything with the var e.g. config_switch(val) */
+ /* for our example we will save it in global variable */
+
+ /* please note, battle settings can be only returned as int for scripts and other usage */
+ if (strcmpi(key,"my_setting") == 0)
+ my_setting = atoi(val);
+}
+
+/* Battle Config Settings need to have return function in-case scripts need to read it */
+int return_my_setting(const char *key)
+{
+ /* first we check the key to know what setting we need to return with strcmpi then return it */
+ if (strcmpi(key, "my_setting") == 0)
+ return my_setting;
+
+ return 0;
}
+
/* run when server starts */
HPExport void plugin_init (void) {
ShowInfo("Server type is ");
@@ -127,13 +168,19 @@ HPExport void plugin_init (void) {
ShowInfo("I'm being run from the '%s' filename\n", SERVER_NAME);
- /* addAtcommand("command-key",command-function) tells map server to call ACMD(sample) when "sample" command is used */
- /* - it will print a warning when used on a non-map-server plugin */
- addAtcommand("sample",sample);//link our '@sample' command
+ // Atcommands only make sense on the map server
+ if (SERVER_TYPE == SERVER_TYPE_MAP) {
+ /* addAtcommand("command-key",command-function) tells map server to call ACMD(sample) when "sample" command is used */
+ /* - it will print a warning when used on a non-map-server plugin */
+ addAtcommand("sample",sample);//link our '@sample' command
+ }
- /* addScriptCommand("script-command-name","script-command-params-info",script-function) tells map server to call BUILDIN(sample) for the "sample(i)" command */
- /* - it will print a warning when used on a non-map-server plugin */
- addScriptCommand("sample","i",sample);
+ // Script commands only make sense on the map server
+ if (SERVER_TYPE == SERVER_TYPE_MAP) {
+ /* addScriptCommand("script-command-name","script-command-params-info",script-function) tells map server to call BUILDIN(sample) for the "sample(i)" command */
+ /* - it will print a warning when used on a non-map-server plugin */
+ addScriptCommand("sample","i",sample);
+ }
/* addCPCommand("console-command-name",command-function) tells server to call CPCMD(sample) for the 'this is a sample <optional-args>' console call */
/* in "console-command-name" usage of ':' indicates a category, for example 'this:is:a:sample' translates to 'this is a sample',
@@ -147,23 +194,26 @@ HPExport void plugin_init (void) {
* to trigger packetFunction in the packetIncomingPoint section ( available points listed in enum HPluginPacketHookingPoints within src/common/HPMi.h ) */
addPacket(0xf3,-1,sample_packet0f3,hpClif_Parse);
- /* in this sample we add a PreHook to pc->dropitem */
- /* to identify whether the item being dropped is on amount higher than 1 */
- /* if so, it stores the amount on a variable (my_pc_dropitem_storage) and changes the amount to 1 */
- addHookPre("pc->dropitem",my_pc_dropitem_pre);
-
- /* in this sample we add a PostHook to pc->dropitem */
- /* if the original pc->dropitem was successful and the amount stored on my_pc_dropitem_storage is higher than 1, */
- /* our posthook will display a message to the user about the cap */
- /* - by checking whether it was successful (retVal value) it allows for the originals conditions to take place */
- addHookPost("pc->dropitem",my_pc_dropitem_post);
+ // The following hooks would show an error message where pc->dropitem doesn't exist (login or char server)
+ if (SERVER_TYPE == SERVER_TYPE_MAP) {
+ /* in this sample we add a PreHook to pc->dropitem */
+ /* to identify whether the item being dropped is on amount higher than 1 */
+ /* if so, it stores the amount on a variable (my_pc_dropitem_storage) and changes the amount to 1 */
+ addHookPre("pc->dropitem",my_pc_dropitem_pre);
+
+ /* in this sample we add a PostHook to pc->dropitem */
+ /* if the original pc->dropitem was successful and the amount stored on my_pc_dropitem_storage is higher than 1, */
+ /* our posthook will display a message to the user about the cap */
+ /* - by checking whether it was successful (retVal value) it allows for the originals conditions to take place */
+ addHookPost("pc->dropitem",my_pc_dropitem_post);
+ }
}
/* triggered when server starts loading, before any server-specific data is set */
HPExport void server_preinit (void) {
/* makes map server listen to mysetting:value in any "battleconf" file (including imported or custom ones) */
/* value is not limited to numbers, its passed to our plugins handler (parse_my_setting) as const char *,
- * and thus can be manipulated at will */
- addBattleConf("my_setting",parse_my_setting);
+ * however for battle config to be returned to our script engine we need it to be number (int) so keep use it as int only */
+ addBattleConf("my_setting",parse_my_setting,return_my_setting);
}
/* run when server is ready (online) */
HPExport void server_online (void) {