Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

PlayerChatCommand(const char* user,const char* cmd, int argc, char** …

…argv) callback
  • Loading branch information...
commit 13651da9874ba85806d9cccd6bea6fb06ee5c735 1 parent 9ac4089
Marko Viitanen authored January 16, 2011
120  plugintest/src/command.cpp
@@ -69,40 +69,6 @@ std::string dtos(double n)
69 69
   return result.str();
70 70
 }
71 71
 
72  
-std::deque<std::string> parseCmd(std::string cmd)
73  
-{
74  
-  int del;
75  
-  std::deque<std::string> temp;
76  
-
77  
-  while(cmd.length() > 0)
78  
-  {
79  
-    while(cmd[0] == ' ')
80  
-    {
81  
-      cmd = cmd.substr(1);
82  
-    }
83  
-
84  
-    del = cmd.find(' ');
85  
-
86  
-    if(del > -1)
87  
-    {
88  
-      temp.push_back(cmd.substr(0, del));
89  
-      cmd = cmd.substr(del+1);
90  
-    }
91  
-    else
92  
-    {
93  
-      temp.push_back(cmd);
94  
-      break;
95  
-    }
96  
-  }
97  
-
98  
-  if(temp.empty())
99  
-  {
100  
-    temp.push_back("empty");
101  
-  }
102  
-
103  
-  return temp;
104  
-}
105  
-
106 72
 typedef void (*CommandCallback)(std::string nick, std::string, std::deque<std::string>);
107 73
 
108 74
 struct Command
@@ -131,44 +97,29 @@ void registerCommand(Command* command)
131 97
 }
132 98
 
133 99
 #define LOG_INFO 6
134  
-bool chatPreFunction(const char* userIn,time_t timestamp, const char* msgIn)
  100
+bool chatCommandFunction(const char* userIn,const char* cmdIn, int argc, char** argv)
135 101
 {
136 102
   std::string user(userIn);
137  
-  std::string msg(msgIn);
  103
+  std::string command(cmdIn);
  104
+  std::deque<std::string> cmd(argv, argv+argc);
138 105
 
139  
-  if(msg.size() == 0)
  106
+  if(command.size() == 0)
140 107
   {
141 108
     return false;
142 109
   }
143 110
 
144  
-  char prefix = msg[0];
145 111
 
146  
-  std::string logMsg = "Command Plugin got from "+user+": " + msg;
  112
+  std::string logMsg = "Command Plugin got from "+user+": " + command;
147 113
   mineserver->logger.log(LOG_INFO, "plugin.command", logMsg.c_str());
148 114
 
149  
-  if (prefix == CHATCMDPREFIX)
  115
+  // User commands
  116
+  CommandList::iterator iter;
  117
+  if((iter = m_Commands.find(command)) != m_Commands.end())
150 118
   {
151  
-    // Timestamp
152  
-    time_t rawTime = timestamp;
153  
-    struct tm* Tm  = localtime(&rawTime);
154  
-    std::string timeStamp (asctime(Tm));
155  
-    timeStamp = timeStamp.substr(11, 5);
156  
-
157  
-    std::deque<std::string> cmd = parseCmd(msg.substr(1));
158  
-
159  
-    std::string command = cmd[0];
160  
-    cmd.pop_front();
161  
-
162  
-    // User commands
163  
-    CommandList::iterator iter;
164  
-    if((iter = m_Commands.find(command)) != m_Commands.end())
165  
-    {
166  
-      iter->second->callback(user, command, cmd);
167  
-      return false;
168  
-    }
  119
+    iter->second->callback(user, command, cmd);
  120
+    return false;
169 121
   }
170 122
 
171  
-  return true;
172 123
 }
173 124
 
174 125
 bool isValidItem(int id)
@@ -434,13 +385,13 @@ bool translateDirection(int32_t *x, int8_t *y, int32_t *z, int8_t direction)
434 385
 {
435 386
     switch(direction)
436 387
     {
437  
-    case BLOCK_BOTTOM: (*y)--;  break;
438  
-    case BLOCK_TOP:    (*y)++;  break;
439  
-    case BLOCK_NORTH:  (*x)++;  break;
440  
-    case BLOCK_SOUTH:  (*x)--;  break;
441  
-    case BLOCK_EAST:   (*z)++;  break;
442  
-    case BLOCK_WEST:   (*z)--;  break;
443  
-    default:                  break;
  388
+      case BLOCK_BOTTOM: (*y)--;  break;
  389
+      case BLOCK_TOP:    (*y)++;  break;
  390
+      case BLOCK_NORTH:  (*x)++;  break;
  391
+      case BLOCK_SOUTH:  (*x)--;  break;
  392
+      case BLOCK_EAST:   (*z)++;  break;
  393
+      case BLOCK_WEST:   (*z)--;  break;
  394
+      default:                    break;
444 395
     }
445 396
   return true;
446 397
 }
@@ -548,7 +499,7 @@ PLUGIN_API_EXPORT void CALLCONVERSION command_init(mineserver_pointer_struct* mi
548 499
 
549 500
   mineserver->plugin.setPluginVersion(pluginName.c_str(), PLUGIN_COMMAND_VERSION);
550 501
 
551  
-  mineserver->plugin.addCallback("PlayerChatPre", (void *)chatPreFunction);
  502
+  mineserver->plugin.addCallback("PlayerChatCommand", (void *)chatCommandFunction);
552 503
   mineserver->plugin.addCallback("BlockPlacePre", (void *)blockPlacePreFunction);
553 504
 
554 505
   registerCommand(new Command(parseCmd("igive i"), "<id/alias> [count]", "Gives self [count] pieces of <id/alias>. By default [count] = 1", giveItemsSelf));
@@ -570,3 +521,38 @@ PLUGIN_API_EXPORT void CALLCONVERSION command_shutdown(void)
570 521
     return;
571 522
   }
572 523
 }
  524
+
  525
+
  526
+std::deque<std::string> parseCmd(std::string cmd)
  527
+{
  528
+  int del;
  529
+  std::deque<std::string> temp;
  530
+
  531
+  while(cmd.length() > 0)
  532
+  {
  533
+    while(cmd[0] == ' ')
  534
+    {
  535
+      cmd = cmd.substr(1);
  536
+    }
  537
+
  538
+    del = cmd.find(' ');
  539
+
  540
+    if(del > -1)
  541
+    {
  542
+      temp.push_back(cmd.substr(0, del));
  543
+      cmd = cmd.substr(del+1);
  544
+    }
  545
+    else
  546
+    {
  547
+      temp.push_back(cmd);
  548
+      break;
  549
+    }
  550
+  }
  551
+
  552
+  if(temp.empty())
  553
+  {
  554
+    temp.push_back("empty");
  555
+  }
  556
+
  557
+  return temp;
  558
+}
1  plugintest/src/command.h
@@ -3,6 +3,7 @@
3 3
 #ifndef _PLUGIN_COMMAND_H
4 4
 #define _PLUGIN_COMMAND_H
5 5
 
  6
+std::deque<std::string> parseCmd(std::string cmd);
6 7
 
7 8
 
8 9
 #endif
29  src/chat.cpp
@@ -158,6 +158,9 @@ bool Chat::handleMsg(User* user, std::string msg)
158 158
       }
159 159
       break;
160 160
 
  161
+    case CHATCMDPREFIX:
  162
+      handleCommand(user, msg, timeStamp);
  163
+      break;
161 164
     // Normal chat message
162 165
     default:
163 166
       handleChatMsg(user, msg, timeStamp);
@@ -167,6 +170,32 @@ bool Chat::handleMsg(User* user, std::string msg)
167 170
   return true;
168 171
 }
169 172
 
  173
+void Chat::handleCommand(User* user, std::string msg, const std::string& timeStamp)
  174
+{
  175
+  std::deque<std::string> cmd = parseCmd(msg.substr(1));
  176
+
  177
+  if(!cmd.size() || !cmd[0].size())
  178
+  {
  179
+    return;
  180
+  }
  181
+
  182
+  std::string command = cmd[0];
  183
+  cmd.pop_front();
  184
+
  185
+  char **param = new char *[cmd.size()];
  186
+
  187
+  for(uint32_t i = 0; i < cmd.size(); i++)
  188
+  {
  189
+    param[i] = (char *)cmd[i].c_str();
  190
+  }
  191
+
  192
+  (static_cast<Hook4<bool,const char*,const char*,int,const char**>*>(Mineserver::get()->plugin()->getHook("PlayerChatCommand")))->doAll(user->nick.c_str(), command.c_str(), cmd.size(), (const char **)param);
  193
+
  194
+  delete [] param;
  195
+
  196
+}
  197
+
  198
+
170 199
 void Chat::handleServerMsg(User* user, std::string msg, const std::string& timeStamp)
171 200
 {
172 201
   // Decorate server message
2  src/chat.h
@@ -59,6 +59,8 @@ class Chat
59 59
   bool checkMotd(const std::string& motdFile);
60 60
   void sendHelp(User* user, std::deque<std::string> args);
61 61
 
  62
+  void handleCommand(User* user, std::string msg, const std::string& timeStamp);
  63
+
62 64
 private:
63 65
   std::deque<std::string> parseCmd(std::string cmd);
64 66
 };
1  src/plugin.h
@@ -191,6 +191,7 @@ class Plugin
191 191
     setHook("BlockReplacePost", new Hook6<bool,const char*,int32_t,int8_t,int32_t,int16_t,int16_t>);
192 192
     setHook("BlockNeighbourReplace", new Hook9<bool,const char*,int32_t,int8_t,int32_t,int32_t,int8_t,int32_t,int16_t,int16_t>);
193 193
     setHook("LogPost", new Hook3<bool,int,const char*,const char*>);
  194
+    setHook("PlayerChatCommand", new Hook4<bool,const char*,const char*,int,const char**>);
194 195
 
195 196
     init();
196 197
   }

0 notes on commit 13651da

Please sign in to comment.
Something went wrong with that request. Please try again.