How to override core command?

Re: How to override core command?

Postby Ultima » 17 Dec 2011, 01:19

And where is that changelog? :mrgreen:
Current release only has the 7.14 version.
User avatar
Ultima
 
Posts: 953
Joined: 19 Aug 2011, 23:45

Re: How to override core command?

Postby Ultima » 24 Jan 2012, 16:22

dzienny wrote:There's no clean way to do it, but there is one that should work.

You have to create a custom command that will remove core command from the commands pool.

For example, if you want to remove /alive command do this in the command:
Code: Select all
Command.all.Remove(new CmdAlive());

After you remove it you can load a custom command that is named CmdAlive2 and it will replace the core command. Still it will work till you reset a server.


I noticed that u cant remove a command if you are tyring it with a other command.
E.g using /override cant remove CmdAlive() and load a custom CmdAlive().
User avatar
Ultima
 
Posts: 953
Joined: 19 Aug 2011, 23:45

Re: How to override core command?

Postby Ultima » 21 Feb 2012, 22:13

When autoload this:

Code: Select all
using System;

namespace MCDzienny
{
   public class CmdOverride : Command
   {
      //Don't change it
      public override string name { get { return ""; } }
      public override string shortcut { get { return ""; } }
      public override string type { get { return ""; } }
      public override bool museumUsable { get { return false; } }
      public override LevelPermission defaultRank { get { return LevelPermission.Banned; } }

      // Init is executed when the code is loaded
      public override void Init()
      {
      Command.all.Remove(new Topten());
      }

      // Don't change it
      public override void Help(Player p)
      { }
      public override void Use(Player p, string message)
      { }
   }
}


And i autoload this after it:

Code: Select all
using System;

namespace MCDzienny
{
    class CmdTopten : Command
    {
        public override string name { get { return "topten2"; } }
        public override string shortcut { get { return ""; } }
        public override string type { get { return "information"; } }
        public override bool museumUsable { get { return false; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Banned; } }
        public CmdTopten() { }

        public override void Use(Player p, string message)
        {
            Player.SendMessage(p, "Top 10 users");
        }

        public override void Help(Player p)
        {
            Player.SendMessage(p, "/topten - Display custom topten");
        }
    }
}


It doesnt work.
If i type /topten it will show the old topten.
and /topten2 doesnt work.

What do do wrong?
Note that topten2 is just a example.
User avatar
Ultima
 
Posts: 953
Joined: 19 Aug 2011, 23:45

Re: How to override core command?

Postby dzienny » 21 Feb 2012, 22:42

The first code seems to be fine, but the second one isn't.
By using "Command.all.Remove(new CmdTopten());" you remove CmdTopten from the list of commands, not from the assembly. It means that there is still such a class ("CmdTopten"), and when you try to add your "CmdTopten" there is an error because there can't be two classes with the same name in the same place! But good thing is that the class name has no importance, it just have to have a unique name, for example "CmdTopten2" (if it was "Potato" it wouldn't matter, but for readability sake use "CmdToptenCustom"). What really matters is "public override string name { get { return "topten"; } }" because it defines the command in-game name.

This code should work:

Code: Select all
using System;

namespace MCDzienny
{
    class CmdToptenCustom : Command
    {
        public override string name { get { return "topten"; } }
        public override string shortcut { get { return ""; } }
        public override string type { get { return "information"; } }
        public override bool museumUsable { get { return false; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Banned; } }
        public CmdToptenCustom() { }

        public override void Use(Player p, string message)
        {
            Player.SendMessage(p, "Top 10 users");
        }

        public override void Help(Player p)
        {
            Player.SendMessage(p, "/topten - Display custom topten");
        }
    }
}
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27

Re: How to override core command?

Postby Ultima » 21 Feb 2012, 23:08

Thanks :D
I knew it was something stupid :P
User avatar
Ultima
 
Posts: 953
Joined: 19 Aug 2011, 23:45

Re: How to override core command?

Postby Ultima » 24 Feb 2012, 20:09

I give up, i dont know what i am doing wrong.

Code: Select all
using System;

namespace MCDzienny
{
    public class CmdOverride : Command
    {
        //Don't change it
        public override string name { get { return ""; } }
        public override string shortcut { get { return ""; } }
        public override string type { get { return ""; } }
        public override bool museumUsable { get { return false; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Guest; } }

        // Init is executed when the code is loaded
        public override void Init()
        {
            Command.all.Remove(new CmdTopten());
        }

        // Don't change it
        public override void Help(Player p)
        { }
        public override void Use(Player p, string message)
        { }
    }
}


Code: Select all
using System;
using System.Data;
using MCDzienny;


namespace MCDzienny
{
    class CmdToptenCustom : Command
    {
        public override string name { get { return "topten"; } }
        public override string shortcut { get { return ""; } }
        public override string type { get { return "information"; } }
        public override bool museumUsable { get { return false; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Banned; } }
        public CmdToptenCustom() { }

        public override void Use(Player p, string message)
        {
           Player.SendMessage(p, "test");
        }
        public override void Help(Player p)
        {
            Player.SendMessage(p, "/topten test");
        }
    }
}


Cmdautload:
    override
    topten

I also tried with autoload "toptencustom" but with no success.
It keeps saying "Elite of the server".
User avatar
Ultima
 
Posts: 953
Joined: 19 Aug 2011, 23:45

Re: How to override core command?

Postby dzienny » 25 Feb 2012, 17:58

That's strange hmm, use Command.all.Remove(Command.all.Find("topten")); instead of Command.all.Remove(new CmdTopten());. It has to work.
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27

Re: How to override core command?

Postby Ultima » 26 Feb 2012, 18:58

That worked :D
User avatar
Ultima
 
Posts: 953
Joined: 19 Aug 2011, 23:45

Previous

Return to Help in Coding

Who is online

Users browsing this forum: No registered users and 1 guest

cron