This site has been archived and made available for preservation purposes. No edits can be made.

Command processor

This is an old revision of this page, as edited 08-25-2012, 11:06 PM by foxtacles (contribs). It may differ significantly from the current revision.
This script uses OnPlayerChat and processes chat input given by players if the first character is a forward slash.

[top]Code

Code cpp:
#include "vaultscript.h"
#include <cstdlib>
#include <cstring>
 
State VAULTSCRIPT OnPlayerChat(ID player, RawString message) noexcept
{
	// Player object
	Player pplayer(player);
 
	// it's a command if the first character is a /
	if (*message == '/')
	{
		// we can safely tokenize because we won't send the message
		std::vector<String> tokens;
		RawString cmd = std::strtok(message + 1, " ");
 
		// collecting tokens
		while (cmd)
		{
			tokens.emplace_back(cmd);
			cmd = std::strtok(nullptr, " ");
		}
 
		if (!tokens.empty())
		{
			if (!tokens[0].compare("additem"))
			{
				// baseID given?
				if (tokens.size() > 1)
				{
					// get the hexadecimal baseID of the item
					Base item = static_cast<Base>(std::strtoul(tokens[1].c_str(), nullptr, 16));
					// give item to player with notification
					pplayer.AddItem(item, 1, 100.0, False);
				}
			}
			// more commands...
			else
			{
				// send error message
				std::strncpy(message, String("Unrecognized command: " + tokens[0]).c_str(), static_cast<size_t>(Index::MAX_CHAT_LENGTH));
				return True;
			}
		}
 
		return False;
	}
 
	return True;
}