Ejemplo n.º 1
0
        private RegionFlag DeserializeFlag(SerializableFlag flag)
        {
            try
            {
                flag.Name = RegionFlag.GetPrimaryFlagName(flag.Name);

                var type = RegionFlag.GetFlagType(flag.Name);

                var deserializedFlag = (RegionFlag)Activator.CreateInstance(type);
                deserializedFlag.Region = this;
                deserializedFlag.Name   = RegionFlag.GetFlagName(type);
                deserializedFlag.Value  = flag.Value;

                for (var i = 0; i < flag.GroupValues.Count; i++)
                {
                    deserializedFlag.SetValue(flag.GroupValues[i].Value, GroupExtensions.GetGroup(flag.GroupValues[i].GroupName));
                }

                deserializedFlag.GroupValues = flag.GroupValues ?? new List <GroupValue>();
                _flags.Add(deserializedFlag);
                return(deserializedFlag);
            }
            catch (Exception) { /*ignored*/ }
            return(null);
        }
Ejemplo n.º 2
0
        public void SetFlag(string name, object value, List <GroupValue> groupValues, bool save = true)
        {
            name = RegionFlag.GetPrimaryFlagName(name);
            var flagType = RegionFlag.GetFlagType(name);

            if (flagType == null)
            {
                throw new ArgumentException("Unknown flag: " + name);
            }

            if (Flags == null)
            {
                Flags = new List <SerializableFlag>();
            }

            foreach (var f in Flags.Where(f => f.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)))
            {
                Flags.Remove(f);
                break;
            }

            var flag = new SerializableFlag
            {
                Name        = name,
                Value       = value,
                GroupValues = groupValues
            };

            Flags.Add(flag);
            if (save)
            {
                RegionsPlugin.Instance.Configuration.Save();
            }

            if (_flags == null)
            {
                _flags = new List <RegionFlag>();
            }
            foreach (RegionFlag f in ParsedFlags.Where(f => f.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)))
            {
                f.Value = value;
                return;
            }

            var deserializedFlag = DeserializeFlag(flag);

            if (deserializedFlag != null)
            {
                deserializedFlag.Value = value;
            }
        }
Ejemplo n.º 3
0
        public void Execute(IRocketPlayer caller, string[] command)
        {
            if (command.Length < 2)
            {
                this.SendUsage(caller);
                return;
            }
            var name   = command[0];
            var region = RegionsPlugin.Instance.GetRegion(name, true);

            if (region == null)
            {
                UnturnedChat.Say(caller, "Region \"" + name + "\" not found", Color.red);
                return;
            }

            if (!region.IsOwner(caller) && !PermissionUtil.HasPermission(caller, "flag.override"))
            {
                UnturnedChat.Say(caller, "You're not the owner of this region!", Color.red);
                return;
            }

            var flagName = command[1];

            var t = RegionFlag.GetFlagType(flagName);

            if (t == null)
            {
                UnturnedChat.Say(caller, "Unknown flag: \"" + flagName + "\"", Color.red);
                return;
            }

            var f = region.GetFlag(t);

            if (f == null)
            {
                f      = (RegionFlag)Activator.CreateInstance(t);
                f.Name = RegionFlag.GetFlagName(t);
            }

            var hasFlagPermission = PermissionUtil.HasPermission(caller, "flag." + flagName);
            var usage             = "Usage: /rflag " + name + " " + f.Name + " " + f.Usage + " [-g <group>]";

            if (command.Length == 3 && command[2].Equals("--help"))
            {
                var description = f.Description;
                var value       = f.GetValue <object>();

                UnturnedChat.Say(caller, "Flag: " + f.Name, Color.blue);
                UnturnedChat.Say(caller, "Description: " + description, Color.blue);
                if (!hasFlagPermission)
                {
                    return;
                }
                UnturnedChat.Say(caller, usage);
                UnturnedChat.Say(caller, "Value: " + (value ?? "null"), Color.blue);
                return;
            }

            if (!hasFlagPermission)
            {
                UnturnedChat.Say(caller, "You don't have access to this flag!", Color.red);
                return;
            }


            f.Region = region;

            Group         group    = Group.ALL;
            List <string> args     = new List <string>();
            bool          isValue  = false;
            bool          groupSet = false;

            foreach (string arg in command.Skip(2))
            {
                if (!groupSet)
                {
                    if (isValue)
                    {
                        if (f.SupportsGroups)
                        {
                            group = GroupExtensions.GetGroup(arg);
                        }
                        groupSet = true;
                        isValue  = false;
                        continue;
                    }
                    if (arg.ToLower().Equals("-g") || arg.ToLower().Equals("--group"))
                    {
                        if (!f.SupportsGroups)
                        {
                            UnturnedChat.Say(caller, "Warning: Flag does not support groups", Color.red);
                        }
                        isValue = true;
                        continue;
                    }
                }

                args.Add(arg);
            }

            string shownValue;

            if (isValue || !f.ParseValue(caller, region, args.ToArray(), out shownValue, group))
            {
                UnturnedChat.Say(caller, usage, Color.red);
                return;
            }

            region.SetFlag(f.Name, f.Value, f.GroupValues);

            if (shownValue != null)
            {
                string msg = $"Flag has been set to: {shownValue}";
                if (f.SupportsGroups)
                {
                    msg += $" for group {group}!";
                }
                UnturnedChat.Say(caller, msg, Color.green);
            }

            RegionsPlugin.Instance.Configuration.Save();
        }