Code Monkey home page Code Monkey logo

samp-command-handler's Introduction

SAMP Command Handler

A command handler for SAMP in C++.

๐Ÿ”ฝ Installation

Use PACC package manager.

Add to dependencies

Add [email protected] to dependencies field of your cpackage.json:

"dependencies": [
	"[email protected]"
]

Install packages

Run the following shell command:

pacc install

๐ŸŽฏ Usage

1. Initial configuration

Include the header file:

#include <SAMPCommandHandler/Everything.hpp>

You probably want to add the following aliases:

namespace samp = samp_cpp; // typical samp_cpp alias

using samp_cmd::CommandArgs;
using samp_cmd::CommandHandler;

Add CommandHandler instance to your game mode (the singleton is simply for the example purpose):

// In "GameMode.hpp" - the declaration:
class GameMode
{
public:
	CommandHandler cmds;
};

GameMode& gameMode();

// In "GameMode.cpp" - the definition:
GameMode& gameMode()
{
	static GameMode instance;
	return instance;
}

Then inside OnPlayerCommandText handle the commands:

PLUGIN_EXPORT bool PLUGIN_CALL OnPlayerCommandText(int playerid, const char *cmdtext_)
{										
	std::string_view cmdText = cmdtext_;

	auto& gm = gameMode();

	if (!gm.cmds.handleCommandText(playerid, cmdText))
		return false;

	return true;
}

2. Adding commands

Once you've configured the handler, you can add a command:

// Declared before:

/// No additional parameters
void spawnInfernus(samp::Player player_);

/// With additional parameters
void sendPrivMsg(samp::Player player_, CommandArgs args_);

// The class definition:
class GameMode
{
public:
	GameMode() {
		this->setupCommands();
	}

	void setupCommands()
	{
		cmds.add( "infernus", 	spawnInfernus );
		cmds.add( "pm", 		sendPrivMsg );
	}

	CommandHandler cmds;
};

Then provide the command definitions:

// For convenience:
using samp::colored;
using samp::colors::red;
using samp::colors::white;
using samp::colors::yellow;
using samp::colors::orange;

////////////////////////////////
void spawnInfernus(Player player_)
{
	if (player_.isInAnyVehicle())
	{
		player_.getVehicle().destroy();
	}

	auto veh = Vehicle::create(Vehicle::Infernus, player_.pos(), player_.rot(), 3, 4, 5000);

	player_.putInVehicle(veh, 0);
	player_.msg(colored(yellow,"You've spawned  ",white,"{}",yellow," for free!"), veh.prettyName());
}

////////////////////////////////
void sendPrivMsg(samp::Player player_, CommandArgs args_)
{
	if (args_.tryParse(1) != 1)
		return player_.msg(red,"Usage: /pm [id] [message content]");

	auto id = args_.get<int>(1); // returns optional<int>
	if (!id.has_value())
		return player_.msg(red,"Usage: /pm [id] [message content]");

	samp::Player other{*id};
	if (!other.valid())
		return player_.msg(colored(red,"Player ",white,"{}",red," is not connected"), *id);


	// Message other player:
	{
		other.msg(colored(yellow,"PRIVATE MESSAGE from ",white,"{} (id: {})"), player_.name(), player_.id());
		// tail() returns rest of the argument string
		// without spaces at the beginning
		other.msg(colored(white,"{}"), args_.tail());
	}

	// Inform self:
	{
		player_.msg(colored(orange,"PRIVATE MESSAGE to ",white,"{} (id: {})"), other.name(), other.id());
		player_.msg(colored(white,"{}"), args_.tail());
	}


	// PLEASE NOTE:
	// This command function is only an example.
	// A couple more checks should be performed.
}

Commands from lambda functions:

cmds.add( "healme",
	[](samp::Player p) {
		p.setHealth(100.f);
		p.msg(samp::colors::green, "Healed");
	}
);

๐Ÿ‘ฑโ€โ™‚๏ธ Authors

Paweล‚ Syska

samp-command-handler's People

Contributors

poetakodu avatar

Watchers

 avatar  avatar

Forkers

pacc-repo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.