Exemplo n.º 1
0
        public Target FillTargets(OpenPlayer commander, OpenLevel level, string source)
        {
            Target target = ParseTarget(source);

            if (target.Selector == "closestPlayer" && target.Rules == null)
            {
                target.Players = new[] { commander };
            }
            else if (target.Selector == "closestPlayer" && target.Rules != null)
            {
                string username = target.Rules.First().Value;
                var    players  = level.GetAllPlayers().Where(p => p.Username == username);
                target.Players = players.ToArray();
            }
            else if (target.Selector == "allPlayers")
            {
                target.Players = level.GetAllPlayers();
            }
            else if (target.Selector == "allEntities")
            {
                target.Entities = level.GetEntites();
            }
            else if (target.Selector == "randomPlayer")
            {
                MiNET.Player[] players = level.GetAllPlayers();
                target.Players = new[] { players[new Random().Next(players.Length)] };
            }


            return(target);
        }
Exemplo n.º 2
0
        public static Target ParseTarget(string source)
        {
            Target target = new Target();

            if (!source.StartsWith("@"))
            {
                target.Selector = "closestPlayer";
                target.Rules    = new[]
                {
                    new Target.Rule()
                    {
                        Name  = "name",
                        Value = source
                    }
                };
            }
            else
            {
                var matches  = Regex.Matches(source, @"^(?<selector>@[aeprs])(\[((?<args>(c|dx|dy|dz|l|lm|m|name|r|rm|rx|rxm|rym|type|x|y|z)=.*?)(,*?))*\])*$");
                var selector = matches[0].Groups["selector"].Captures[0].Value;
                switch (selector)
                {
                case "@a":
                    selector = "allPlayers";
                    break;

                case "@e":
                    selector = "allEntities";
                    break;

                case "@p":
                    selector = "closestPlayer";
                    break;

                case "@r":
                    selector = "randomPlayer";
                    break;

                case "@s":
                    selector = "yourself";
                    break;
                }
                target.Selector = selector;
                List <Target.Rule> rules = new List <Target.Rule>();
                foreach (Capture arg in matches[0].Groups["args"].Captures)
                {
                    string[] split = arg.Value.Split('=');
                    string   name  = split[0];
                    string   value = split[1];

                    Target.Rule rule = new Target.Rule();
                    rule.Name = name;
                    if (value.StartsWith("!"))
                    {
                        rule.Inverted = true;
                        rule.Value    = value.Substring(1);
                    }
                    else
                    {
                        rule.Value = value;
                    }

                    rules.Add(rule);
                }


                if (rules.Count != 0)
                {
                    target.Rules = rules.ToArray();
                }
            }

            return(target);
        }