Jump to content
RAGE Multiplayer Community

Command&Event decorators [server-side/client-side] 1.1.4

   (1 review)

2 Screenshots

About This File

There is a server-side/client-side snippet which helps to registry any commands to Rage API with the simple interface by using decorators.

Install via npm/yarn:

$ npm i --save rage-decorators
// or
$ yarn add rage-decorators

Docs and examples: https://github.com/READYTOMASSACRE/rage-decorators#readme

Install (older version):

 

Скрытый текст

1. Install the reflect-metadata package.

$ npm i --save reflect-metadata
// or
$ yarn add reflect-metadata

2. Move the decorator.ts (or decorator.js, if you're using javascript instead typescript) file to your project folder

mKQnH2H.png

3. Import/require decorators in your file

Foo.ts

Скрытый текст

import { commandable, command } from './decorators'

// first we should flag our class is resolvable by decorator commanadble (for registy any commands in class)
@commandable()
class Foo {

  // then we flag class method as a command
  // added command "/helloworld"
  @command("helloworld")
  print(player: PlayerMp) {
    player.outputChatBox("hello world!")
  }
}

 

4. Make an instance class in other place in your project

App.ts

Скрытый текст

// we should add reflect-metadata first before decorators
// because they are using Reflect API in runtime
import 'reflect-metadata'

import { Foo } from './Foo'

// all commands in class Foo are created
const foo = new Foo()

 

5. Make sure you import/require reflect-metadata before decorators in your entry script

 

If you're using typescript, make sure there two options (experimentalDecorators, emitDecoratorMetadata) are true in your tsconfig.json:

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true
    },
}

Interface command decorator

/**
 * Decorator for adding commands to RAGE API
 *
 * @param {string | string[]} cmd - command(s) name, which will be added to mp.events.addCommand
 * @param {string | { group?: string, desc?: string }} params - additional params, add to group or add to description
 * 
 * Supports templates in the desc param:
 *  @template cmdName - name of command
 *  @template groupName - name of group (if added in the additional params)
 *
 * @example desc parameter template:
 * `Usage: /{{cmdName}} id`
 * `Usage: /{{groupName}} {{cmdName}} id`
 * 
 * decorator usage:
 * command("foo")
 * command(["bar", "baz"])
 * command("foo", "foogroup")
 * command("bar", { group: "foogroup", desc: "Custom description"})
 * 
 */
export const command = (cmd: string | string[], params?: string | { group?: string, desc?: string }): MethodDecorator

Interface event decorator

/**
 * Decorator for adding events into RAGE API
 * 
 * @param {string | string[]} eventName - event(s) name, which will be added to mp.events.add
 * 
 * @example
 * decorator usage:
 * event("playerJoin")
 * event(["playerDeath", "playerQuit"])
 */
export const event = (eventName: string | string[]): MethodDecorator

Forum discussion:

 


What's New in Version 1.0.0   See changelog

Released

No changelog available for this version.


User Feedback

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest

×
×
  • Create New...