/enderking

/enderking

Postby Stuwie » 05 Jun 2013, 17:26

Just because I love Endermen and I like that fact that you can kill people I thought this would be a good command.
the command basicly kills everyone with saying a certain message, I think that would be cool especially if everyone
is arguing or won't listen to you. And it might just be a fun command. I have no idea how to code so I couldn't make
make this command but I'm sure one of you can, thanks :D!
Image
User avatar
Stuwie
 
Posts: 78
Joined: 27 Nov 2011, 23:49

Re: /enderking

Postby Conor » 05 Jun 2013, 17:41

Stuwie wrote:Just because I love Endermen and I like that fact that you can kill people I thought this would be a good command.
the command basicly kills everyone with saying a certain message, I think that would be cool especially if everyone
is arguing or won't listen to you. And it might just be a fun command. I have no idea how to code so I couldn't make
make this command but I'm sure one of you can, thanks :D!


Haven't tested it but it should compile fine. If not, post the errors.

To kill with " was killed by the enderking":
Code: Select all
using System;

namespace MCDzienny
{
    public class CmdEnderking : Command
    {
        public override string name { get { return "enderking"; } }
        public override string shortcut { get { return "ek"; } }
        public override string type { get { return "other"; } }
        public override bool museumUsable { get { return true; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Operator; } }
        public override bool ConsoleAccess { get { return false; } }

        public override void Help(Player p)
        {
            Player.SendMessage(p, "/enderking - kill every player on the server of a lower rank.");
        }

        public override void Use(Player p, string message)
        {
            Player.players.ForEach(delegate(Player pl)
            {
                if (pl.group.Permission < p.group.Permission)
                {
                    Command.all.Find("kill").Use(p, pl.PublicName + " was killed by the enderking.");
                }
            });
        }
    }
}


To kill with an optional message:

Code: Select all
using System;

namespace MCDzienny
{
    public class CmdEnderking : Command
    {
        public override string name { get { return "enderking"; } }
        public override string shortcut { get { return "ek"; } }
        public override string type { get { return "other"; } }
        public override bool museumUsable { get { return true; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Operator; } }
        public override bool ConsoleAccess { get { return false; } }

        public override void Help(Player p)
        {
            Player.SendMessage(p, "/enderking - kill every player on the server of a lower rank.");
        }

        public override void Use(Player p, string message)
        {
            Player.players.ForEach(delegate(Player pl)
            {
                if (pl.group.Permission < p.group.Permission)
                {
                    Command.all.Find("kill").Use(p, pl.PublicName + message);
                }
            });
        }
    }
}
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: /enderking

Postby Stuwie » 05 Jun 2013, 21:10

Nice!!, It also kills me. could you not change it to kill everyone but me?
Image
User avatar
Stuwie
 
Posts: 78
Joined: 27 Nov 2011, 23:49

Re: /enderking

Postby ismellike » 05 Jun 2013, 21:15

Here you go

Code: Select all
using System;

namespace MCDzienny
{
    public class CmdEnderking : Command
    {
        public override string name { get { return "enderking"; } }
        public override string shortcut { get { return "ek"; } }
        public override string type { get { return "other"; } }
        public override bool museumUsable { get { return true; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Operator; } }
        public override bool ConsoleAccess { get { return false; } }

        public override void Help(Player p)
        {
            Player.SendMessage(p, "/enderking - kill every player on the server of a lower rank.");
        }

        public override void Use(Player p, string message)
        {
            Player.players.ForEach(delegate(Player pl)
            {
                if (pl.group.Permission <= p.group.Permission)
                {
                   if(pl!=p){
                    Command.all.Find("kill").Use(p, pl.PublicName + " was killed by the enderking.");
                       }
                }
            });
        }
    }
}
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: /enderking

Postby Conor » 05 Jun 2013, 21:17

Stuwie wrote:Nice!!, It also kills me. could you not change it to kill everyone but me?


Ah, well, if it doesn't allow you to kill ranks of the same or equal rank, this problem wouldn't arise anyway.

I made a mistake and allowed you to kill players of the same rank, so by changing this you can't kill yourself.

Change the '<=' to a '<'.

I will edit my post to accustom this change anyway, so just re-copy the code.
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: /enderking

Postby Stuwie » 05 Jun 2013, 21:21

Thanks alot :D!
Image
User avatar
Stuwie
 
Posts: 78
Joined: 27 Nov 2011, 23:49

Re: /enderking

Postby dzienny » 05 Jun 2013, 21:42

I highly advise using Player.players.ForEachSync instead. It guarantees that all the players will be iterated over, while ForEach may occasionally skip some players ;)
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27

Re: /enderking

Postby Conor » 05 Jun 2013, 21:56

dzienny wrote:I highly advise using Player.players.ForEachSync instead. It guarantees that all the players will be iterated over, while ForEach may occasionally skip some players ;)


Okay, thanks.

But it doesn't recognise it when I use List.ForEachSync :(

Is it only supported in a certain version of the Net Framework? Maybe I need to add a seperate library other than System.Collections?

Also could you please give me an example? Unless its exactly the same but replacing 'ForEach' with 'ForEachSync'.
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: /enderking

Postby dzienny » 05 Jun 2013, 22:36

ForEachSync is a part of PlayerCollection class that inherits from List<Player>. It is not a List class method. So it will work on Player.players, but not on a List<player> collection. The difference between PlayerCollection.ForEach and PlayerCollectionForEachSync is that the latter guarantees that all the players will be iterated, because it internally uses locking to prevent any removal or addition of players to the list. ForEach method doesn't acquire the lock so it is possible and it happens that the collection is modified during the iteration causing unexpected behaviour such as for example skipping a player, or iterating twice over the same one. But because ForEachSync locks the collection it's not advised to use it with a code that may take unknown amount of time to finish, or very long. So if you are for example writing to a database you probably shouldn't use ForEachSync. But 99% of time you should use ForEachSync.
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27

Re: /enderking

Postby Conor » 05 Jun 2013, 23:18

dzienny wrote:ForEachSync is a part of PlayerCollection class that inherits from List<Player>. It is not a List class method. So it will work on Player.players, but not on a List<player> collection. The difference between PlayerCollection.ForEach and PlayerCollectionForEachSync is that the latter guarantees that all the players will be iterated, because it internally uses locking to prevent any removal or addition of players to the list. ForEach method doesn't acquire the lock so it is possible and it happens that the collection is modified during the iteration causing unexpected behaviour such as for example skipping a player, or iterating twice over the same one. But because ForEachSync locks the collection it's not advised to use it with a code that may take unknown amount of time to finish, or very long. So if you are for example writing to a database you probably shouldn't use ForEachSync. But 99% of time you should use ForEachSync.


Ahhh, my bad, I thought you were talking about a method within the List class :P

Sounds good, thanks for the advice and I shall adopt it in the future.
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor


Return to Requests for Addon

Who is online

Users browsing this forum: No registered users and 6 guests

cron