Adding Player-Specific Variables

Adding Player-Specific Variables

Postby Conor » 15 Jul 2013, 15:07

Hey.

So a common problem many of us coders face with custom commands is the fact that we are limited to the variables in the Player class, we cannot add our own. Dzienny has been helpful in giving us the Player.ExtraData dictionary; however this doesn't allow us to add all kinds of variables/objects to our Player class, for example a timer.

A way around this is to create a new class, which binds to a Player object, inside this class we can add any variables we like, and then access them. You can also add your own methods. Below is the class, and below that I show you a little pointless example. Hopefully this may help people who are coding more advanced things :)

Code: Select all
using System;
using System.Collections.Generic;

namespace MCDzienny
{
    public class CmdPlayerExtraPlugin : Command
    {
        public override string name { get { return "playerextraplugin"; } }
        public override string shortcut { get { return ""; } }
        public override string type { get { return "mod"; } }
        public override bool museumUsable { get { return true; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Nobody; } }

        public override void Init()
        {
            Player.Joined += (object sender, PlayerEventArgs e) =>
            {
                PlayerExtra addPlayer = new PlayerExtra(e.Player);
            };
        }

        public override void Help(Player p)
        {
            Player.SendMessage(p, "This is a plugin, the command does nothing.");
        }

        public override void Use(Player p, string message)
        {
            Help(p);
        }
    }

    public partial class PlayerExtra
    {
        public static List<PlayerExtra> all = new List<PlayerExtra>();
        public Player player;

        public PlayerExtra(Player player)
        {
            this.player = player;

            if (!all.Contains(this))
            {
                all.Add(this);
            }
        }

        public static PlayerExtra Find(Player player)
        {
            PlayerExtra toReturn = null;

            all.ForEach(delegate(PlayerExtra p)
            {
                if (p.player == player)
                {
                    toReturn = p;
                }
            });

            if (toReturn != null)
            {
                return toReturn;
            }

            if (player != null && !player.disconnected)
            {
                PlayerExtra newPlayer = new PlayerExtra(player);
                return newPlayer;
            }
            return null;
        }
    }

    public partial class PlayerExtra
    {
        // Add your own variables/methods inside here.

        public int myInt = 0;
        public string myString = "hello";
        public System.Timers.Timer playerTimer = new System.Timers.Timer(1000);

        public void myMethod()
        {
            player.flipHead = true;
        }
    }
}


Here is an example of how you can access the variables and methods.

Code: Select all
public override void Use(Player p, string message)
{
   PlayerExtra pe = PlayerExtra.Find(p);
   
   pe.myMethod(); 
 
   for (int i = 0; i < 10; i++)
   {
       Thread.Sleep(1000);
       pe.myInt++;       
   }
   
   Player.SendMessage(p, pe.myInt.ToString());
}
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Adding Player-Specific Variables

Postby ismellike » 15 Jul 2013, 16:24

Awesome, this will definitely be helpful.
I actually need something like this for a minigame I'm making ;P.
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas


Return to Knowledge Base

Who is online

Users browsing this forum: No registered users and 1 guest

cron