Sign in to follow this  
Gross

tutorial Add Command Line to server console

Recommended Posts

Create file 'commandline.js' to dir 'packages/keker' with the following code:

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

function ParseCmd(s){
	var args = s.split(" ");
	var cmd = args[0].toLowerCase();
	var res = '';
	switch(cmd){
		case "ban":
			mp.players.forEach(player => { 
				if(player.name.toLowerCase() == args[1].toLowerCase()){
					player.ban("Console");
					res = " Player "+player.name+" was banned!!!";
				} 
				});
			break;
		case "kick":
			mp.players.forEach(player => { 
				if(player.name.toLowerCase() == args[1].toLowerCase()){
					player.kick("Console");
					res = " Player "+player.name+" was kicked!!!";
				} 
				});
			break;
		case "tppos":
			if(args.length>4){
				mp.players.forEach(player => { 
				if(player.name.toLowerCase() == args[1].toLowerCase()){
					player.position = new mp.Vector3(parseFloat(args[2]), parseFloat(args[3]), parseFloat(args[4]));
					res = " Player "+player.name+" was teleported to X:"+parseFloat(args[2])+" Y:"+parseFloat(args[3])+" Z:"+parseFloat(args[4])+"!!!";
				} 
				});
			} else {
				res = " Invalid arguments!!!"
			}
			break;
		case "give.weapons":
			if(args.length>3){
				mp.players.forEach(player => { 
				if(player.name.toLowerCase() == args[1].toLowerCase()){
					player.giveWeapon(mp.joaat(args[2]), parseInt(args[3]));
					res = " Player "+player.name+" received weapons!!!";
				} 
				});
			} else {
				res = " Invalid arguments!!!"
			}
			break;
		case "status":
			res = "\n Players: "+mp.players.length+"/"+mp.players.size+"\n Vehicles: "+mp.vehicles.length+"\n Objects: "+mp.objects.length+"\n Wheather: "+mp.environment.weather+"\n Game Time: "+mp.environment.time.hour+"h\n Uptime: "+process.uptime()+"\n";
			break;
		case "online":
			res = "\n Online: "+mp.players.length+"/"+mp.players.size+"\n ";
			mp.players.forEach(player => {
				res += player.name+" | "+player.ip+" | "+player.ping+"\n ";
			});
			break;
		default:
			res = " Unknown command!!!";
			break;
	}
	return res;
}

rl.on('line', (s) => {
	var res = ParseCmd(s);
	console.log(res);
});

After that,  append to file 'packages/keker/index.js' the following line:

require("./commandline");

Then you can execute commands on the server console.

The list of available commands as seen from the code:

ban <PlayerName> -> Kiked player
kick <PlayerName> -> Baned player
tppos <PlayerName> <X> <Y> <Z> -> Teleport player to coordinates
give.weapons <PlayerName> <WeaponName> <Amount> -> Give weapon to player
status -> Show server info
online -> Show players list with info(Name,IP,Ping)

 

  • Like 3
  • Mask 1

Share this post


Link to post
Share on other sites
20 minutes ago, alex_r said:

Isn't it better to make separate package?

Why, for quick installation through NPM?

Share this post


Link to post
Share on other sites

I made some changes on that script. I fixed status command and added support of "Test test" syntax.

Example

kick "Ivan Ivanov"
tppos "Ivan Ivanov" 20 20 20

 

/* global mp */

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

function ParseCmd(s) {
    var args = s.split(" ");
    var cmd = args[0].toLowerCase();
    var params = [];
    var joinMode = false;
    for (var i = 0; i < args.length; i++) {
        if (args[i].charAt(0) === '"') {
            var param = args[i].replace('"', '');
            params.push(param);
            joinMode = true;
        } else if (args[i].charAt(args[i].length - 1) === '"') {
            var param = args[i].replace('"', '');
            params[params.length - 1] += " " + param;
            joinMode = false;
        } else if (joinMode) {
            params[params.length - 1] += " " + param;
        } else {
            params.push(args[i]);
        }
    }
    var res = '';
    switch (cmd) {
        case "ban":
            mp.players.forEach(player => {
                if (player.name.toLowerCase() == params[1].toLowerCase()) {
                    player.ban("Console");
                    res = " Player " + player.name + " was banned!!!";
                }
            });
            break;
        case "kick":
            mp.players.forEach(player => {
                if (player.name.toLowerCase() == params[1].toLowerCase()) {
                    player.kick("Console");
                    res = " Player " + player.name + " was kicked!!!";
                }
            });
            break;
        case "tppos":
            if (params.length > 3) {
                mp.players.forEach(player => {
                    if (player.name.toLowerCase() == params[1].toLowerCase()) {
                        player.position = new mp.Vector3(parseFloat(params[2]), parseFloat(params[3]), parseFloat(params[4]));
                        res = " Player " + player.name + " was teleported to X:" + parseFloat(params[2]) + " Y:" + parseFloat(params[3]) + " Z:" + parseFloat(params[4]) + "!!!";
                    }
                });
            } else {
                res = " Invalid arguments!!!"
            }
            break;
        case "give.weapons":
            if (args.length > 3) {
                mp.players.forEach(player => {
                    if (player.name.toLowerCase() == params[1].toLowerCase()) {
                        player.giveWeapon(mp.joaat(params[2]), parseInt(params[3]));
                        res = " Player " + player.name + " received weapons!!!";
                    }
                });
            } else {
                res = " Invalid arguments!!!"
            }
            break;
        case "status":
            res = "\n Players: " + mp.players.length + "/" + mp.players.size + "\n Vehicles: " + mp.vehicles.length + "\n Objects: " + mp.objects.length + "\n Wheather: " + mp.world.weather + "\n Game Time: " + mp.world.time.hour + ":" + mp.world.time.minute + "\n Uptime: " + process.uptime() + "\n";
            break;
        case "online":
            res = "\n Online: " + mp.players.length + "/" + mp.players.size + "\n ";
            mp.players.forEach(player => {
                res += player.name + " | " + player.ip + " | " + player.ping + "\n ";
            });
            break;
        default:
            res = " Unknown command!!!";
            break;
    }
    return res;
}

rl.on('line', (s) => {
    var res = ParseCmd(s);
    console.log(res);
});

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.