Need help with directing a command/message to another player

Need help with directing a command/message to another player

Postby Leeizazombie » 12 Jul 2013, 12:38

Okay today I don't have alot of time on the computer so while I look into system.timers I love to also be able to understand (with notes) on how it works to send a message or direct a command on them from /mycommand (player) kinda thing.
Owner of:
LeeIzaZombie Freebuild and Lava Survival V2 (Shut Down and updated)
LeeIzaZombie Survival (Comming back soon)

Contact:
Skype: leeizazombie
IRC: irc.geekshed.net, #leeizazombie, #mcclassichosting
User avatar
Leeizazombie
 
Posts: 536
Joined: 10 Jun 2013, 17:45
Location: Ireland.

Re: Need help with directing a command/message to another pl

Postby Conor » 12 Jul 2013, 12:44

I recently helped somebody else with this. Maybe these notes will help you. If you want some more support then just ask.

Conor wrote:....

You can make a player use a command, or you can use a command directed at a player. There is a difference and I'm not sure which you're talking about. So I'll show you both.

To make a player use a command, you would do the following.
Code: Select all
public override void Use(Player p, string message)
{
   if (String.IsNullOrEmpty(message) || message.IndexOf(' ') != -1) { Help(p); return; }

   Player who = Player.Find(message);
   
   if (who == null || who.hidden)
   {
       Player.SendMessage(p, "Player could not be found.");
       return;
   }

   string commandName = "me";
   string commandMessage = "is a smelly monkey.";

   Command.all.Find(commandName).Use(who, commandMessage);
}


To use a command, directed at another player (e.g. /kill [other player]) you would do the following.
Code: Select all
public override void Use(Player p, string message)
{
   if (String.IsNullOrEmpty(message) || message.IndexOf(' ') != -1) { Help(p); return; }

   Player who = Player.Find(message);
   
   if (who == null || who.hidden)
   {
       Player.SendMessage(p, "Player could not be found.");
       return;
   }

   string commandName = "kill";
   string commandMessage = " was eaten alive by monkeys";

   Command.all.Find(commandName).Use(p, who.PublicName + commandMessage);
}


To get the parameters from the command when the player uses the command, instead of having them as stored string variables, you'd have to split the message up using String.Split(' '), or just use the 'message' variable.
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Need help with directing a command/message to another pl

Postby Leeizazombie » 12 Jul 2013, 13:08

Okay so when I use the following is tells me that I cant find the player specified:

Code: Select all
namespace MCDzienny
{
    public class Cmdkicktban : Command
    {
        public override string name { get { return "kicktempban"; } }
        public override string shortcut { get { return "kt"; } }
        public override string type { get { return "mod"; } }
        public override bool museumUsable { get { return true; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Operator; } }

        public override void Use(Player p, string message)
        {
            if (String.IsNullOrEmpty(message) || message.IndexOf(' ') != -1) { Help(p); return; }

            Player who = Player.Find(message);

            if (who == null || who.hidden)
            {
                Player.SendMessage(p, "Player could not be found.");
                return;
            }

            string kick = "kick";
            string kickMessage = who + " was kicked and is being temp-banned";
            string tban = "tempban";
            string tbanMessage = who + " is now temp-banned";
   
           
            Command.all.Find(kick).Use(p, who.PublicName + kickMessage);
            Command.all.Find(tban).Use(p, who.PublicName + tbanMessage);
        }

I tested it with a dummy player when I logged him in with port connector in xWoM
Owner of:
LeeIzaZombie Freebuild and Lava Survival V2 (Shut Down and updated)
LeeIzaZombie Survival (Comming back soon)

Contact:
Skype: leeizazombie
IRC: irc.geekshed.net, #leeizazombie, #mcclassichosting
User avatar
Leeizazombie
 
Posts: 536
Joined: 10 Jun 2013, 17:45
Location: Ireland.

Re: Need help with directing a command/message to another pl

Postby Conor » 12 Jul 2013, 13:55

This is due to you missing out a vital space in the message when you are making the player use the command.

Code: Select all
string kickMessage = who + " was kicked and is being temp-banned";
Command.all.Find(kick).Use(p, who.PublicName + kickMessage);


First off you are meant to use who.PublicName there, not who, you must have made a little mistake. Also, you have put the who.PublicName inside the Command.Use method, so you don't need it in the kick message. So lets change that:

Code: Select all
string kickMessage = "was kicked and is being temp-banned";
Command.all.Find(kick).Use(p, who.PublicName + kickMessage);


Now, this is what the command is being called as, where 'conanza121' is the player who.

/kick conanza121was kicked and is being temp-banned.


See the problem? :) So make sure you include a space in the messages. You could either put it in the string variable at the beginning, or add + " " + into the method like below.

Code: Select all
string kickMessage = "was kicked and is being temp-banned";
Command.all.Find(kick).Use(p, who.PublicName + " " + kickMessage);
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Need help with directing a command/message to another pl

Postby Leeizazombie » 12 Jul 2013, 16:37

Ohh wow I can't believe it was such an easy mistake xD thanks again :3
Owner of:
LeeIzaZombie Freebuild and Lava Survival V2 (Shut Down and updated)
LeeIzaZombie Survival (Comming back soon)

Contact:
Skype: leeizazombie
IRC: irc.geekshed.net, #leeizazombie, #mcclassichosting
User avatar
Leeizazombie
 
Posts: 536
Joined: 10 Jun 2013, 17:45
Location: Ireland.

Re: Need help with directing a command/message to another pl

Postby Conor » 12 Jul 2013, 16:40

Leeizazombie wrote:Ohh wow I can't believe it was such an easy mistake xD thanks again :3


No problem, ask for help anytime :)

I think you are going to enjoy coding, and become great. You understand things really easily. Good luck learning <ok>
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Need help with directing a command/message to another pl

Postby Leeizazombie » 13 Jul 2013, 09:13

Thanks! that means a lot :)
Owner of:
LeeIzaZombie Freebuild and Lava Survival V2 (Shut Down and updated)
LeeIzaZombie Survival (Comming back soon)

Contact:
Skype: leeizazombie
IRC: irc.geekshed.net, #leeizazombie, #mcclassichosting
User avatar
Leeizazombie
 
Posts: 536
Joined: 10 Jun 2013, 17:45
Location: Ireland.


Return to Help in Coding

Who is online

Users browsing this forum: No registered users and 1 guest

cron