Exemplo n.º 1
0
        void cmdChat(BasePlayer player, string command, string[] args)
        {
            if (!IsPlayerPermitted(player, PermissionName))
            {
                return;
            }

            int length = args.Length;

            chatHelper.Run(args, () =>
            {
                player.ChatMessage(Lang("HelpText", player));
            });

            chatHelper.OnCommand("add", () =>
            {
                if (CheckIsEditing(player) != null)
                {
                    player.ChatMessage(Lang("AlreadyEditing", player));
                    return;
                }

                if (chatHelper.NextExists())
                {
                    float height = 0.0f;

                    if (float.TryParse(chatHelper.Next(), out height))
                    {
                        if (chatHelper.NextExists())
                        {
                            if (chatHelper.Next().Equals("fixed", StringComparison.CurrentCultureIgnoreCase))
                            {
                                if (chatHelper.NextExists())                                // with height, "fixed" and name
                                {
                                    CommandAdd(player, height, chatHelper.Next(), true);
                                }
                                else                                 // with height and "fixed"
                                {
                                    CommandAdd(player, height, null, true);
                                }
                            }
                            else                             // with height and name
                            {
                                CommandAdd(player, height, chatHelper.Current());
                            }
                        }
                        else                         // with only height
                        {
                            CommandAdd(player, height);
                        }
                    }
                    else
                    {
                        player.ChatMessage(Lang("HeightMustBeNumber", player));
                    }
                }
                else
                {
                    player.ChatMessage(Lang("InvalidAddCommand", player));
                }
            });

            chatHelper.OnCommand("done", () =>
            {
                TemporaryStorage storage = CheckIsEditing(player);

                if (storage != null)
                {
                    if (storage.Zone.Vertices.Count < MinPoints || storage.Zone.Vertices.Count > MaxPoints)
                    {
                        player.ChatMessage(Lang("DonePointsCount", player));
                        return;
                    }

                    CommandDone(player);
                    player.ChatMessage(Lang("Done", player).Replace("{id}", storage.Zone.Id).Replace("{name}", storage.Zone.Name));
                }
            });

            chatHelper.OnCommand("undo", () =>
            {
                TemporaryStorage storage = CheckIsEditing(player);

                if (storage != null)
                {
                    if (storage.Zone.Vertices.Count != 0)
                    {
                        storage.Zone.Vertices.RemoveRange(storage.Zone.Vertices.Count - 2, 2);
                        player.ChatMessage(Lang("UndoComplete", player));
                    }
                }
            });

            chatHelper.OnCommand("remove", () =>
            {
                if (CheckIsEditing(player) != null)
                {
                    player.ChatMessage(Lang("AlreadyEditing", player));
                    return;
                }

                if (chatHelper.NextExists())
                {
                    if (IsDigitsOnly(chatHelper.Next()))
                    {
                        int removed = data.Zones.RemoveWhere(x => x.Id == chatHelper.Current());

                        if (removed <= 0)
                        {
                            player.ChatMessage(Lang("ZoneNotFound", player));
                            return;
                        }

                        currentZones.RemoveWhere((GameObject go) =>
                        {
                            RectZone rz = go.GetComponent <RectZone>();

                            if (rz.Definition.Id == chatHelper.Current())
                            {
                                GameObject.Destroy(go);
                                return(true);
                            }

                            return(false);
                        });

                        SaveData();

                        player.ChatMessage(Lang("Removed", player));
                    }
                    else
                    {
                        int removed = data.Zones.RemoveWhere((ZoneDefinition x) =>
                        {
                            if (x.Name == null)
                            {
                                return(false);
                            }

                            return(x.Name.Equals(chatHelper.Current(), StringComparison.CurrentCultureIgnoreCase));
                        });

                        if (removed <= 0)
                        {
                            player.ChatMessage(Lang("ZoneNotFound", player));
                            return;
                        }

                        currentZones.RemoveWhere((GameObject go) =>
                        {
                            RectZone rz = go.GetComponent <RectZone>();

                            if (rz.Definition.Name.Equals(chatHelper.Current(), StringComparison.CurrentCultureIgnoreCase))
                            {
                                GameObject.Destroy(go);
                                return(true);
                            }

                            return(false);
                        });

                        SaveData();

                        player.ChatMessage(Lang("Removed", player));
                    }
                }
                else
                {
                    player.ChatMessage(Lang("RemoveHelp", player));
                }
            });

            chatHelper.OnCommand("list", () =>
            {
                if (data.Zones.Count == 0)
                {
                    player.ChatMessage(Lang("Empty", player));
                    return;
                }

                string result = Lang("List", player);
                foreach (ZoneDefinition zd in data.Zones)
                {
                    result += zd.Id + (zd.Name != null ? (" (" + zd.Name + ")") : "") + ", ";
                }

                player.ChatMessage(result.Substring(0, result.Length - 2));
            });

            chatHelper.OnCommand("edit", () =>
            {
                if (CheckIsEditing(player) != null)
                {
                    player.ChatMessage(Lang("AlreadyEditing", player));
                    return;
                }

                if (args.Length > 1 && IsDigitsOnly(args[1]))
                {
                    // TODO: editing
                }
            });

            chatHelper.OnCommand("options", () =>
            {
                TemporaryStorage storage = CheckIsEditing(player);

                if (storage == null)
                {
                    if (chatHelper.NextExists())
                    {
                        string id = chatHelper.Next();

                        if (IsDigitsOnly(id))
                        {
                            ZoneDefinition definition = null;

                            foreach (ZoneDefinition d in data.Zones)
                            {
                                if (d.Id == id)
                                {
                                    definition = d;
                                    break;
                                }
                            }

                            if (definition == null)
                            {
                                player.ChatMessage(Lang("ZoneNotFound", player));
                                return;
                            }

                            if (definition.Options.Count == 0)
                            {
                                player.ChatMessage(Lang("NoOptions", player));
                                return;
                            }

                            string result = string.Empty;
                            foreach (KeyValuePair <string, string> option in definition.Options)
                            {
                                result += option.Key + (option.Value != null ? (" = " + option.Value) : "") + ", ";
                            }

                            player.ChatMessage(result.Substring(0, result.Length - 2));
                        }
                        else
                        {
                            player.ChatMessage(Lang("OptionsHelp", player));
                        }
                    }
                    else
                    {
                        string result = string.Empty;

                        foreach (KeyValuePair <string, string> option in availableOptions)
                        {
                            result += "<color=#ffa500ff>" + option.Key + "</color> - " + (option.Value ?? "No description") + "\n";
                        }

                        player.ChatMessage(result);
                    }
                }
                else
                {
                    if (chatHelper.NextExists())
                    {
                        if (chatHelper.Next() == "list")
                        {
                            string result = string.Empty;

                            foreach (KeyValuePair <string, string> option in availableOptions)
                            {
                                result += "<color=#ffa500ff>" + option.Key + "</color> - " + (option.Value ?? "No description") + "\n";
                            }

                            player.ChatMessage(result);

                            return;
                        }

                        for (int i = 1; i < args.Length; i++)
                        {
                            string[] option = args[i].Split(new char[] { '=' }, 2);

                            storage.Zone.Options.Add(option[0], (option.Length > 1 ? option[1] : null));
                        }
                    }
                    else
                    {
                        if (storage.Zone.Options.Count != 0)
                        {
                            string result = string.Empty;
                            foreach (KeyValuePair <string, string> option in storage.Zone.Options)
                            {
                                result += option.Key + " = " + option.Value + ", \n";
                            }

                            player.ChatMessage(result.Substring(0, result.Length - 2));
                        }
                        else
                        {
                            player.ChatMessage(Lang("NoOptions", player));
                        }
                    }
                }
            });

            chatHelper.OnCommand("show", () =>
            {
                if (chatHelper.NextExists() && IsDigitsOnly(chatHelper.Next()))
                {
                    foreach (GameObject zone in currentZones)
                    {
                        RectZone component = zone.GetComponent <RectZone>();

                        if (component.Definition.Id == chatHelper.Current())
                        {
                            component.ShowZone(player, 10f);
                            return;
                        }
                    }

                    player.ChatMessage(Lang("ZoneNotFound", player));
                }
                else if (chatHelper.Current().Equals("current", StringComparison.CurrentCultureIgnoreCase))
                {
                    RectZone zone = GetCurrentZoneForPlayer(player);
                    zone.ShowZone(player, 10f);
                }
                else
                {
                    if (currentZones.Count == 0)
                    {
                        player.ChatMessage(Lang("ShowEmpty", player));
                        return;
                    }

                    foreach (GameObject zone in currentZones)
                    {
                        zone.GetComponent <RectZone>().ShowZone(player, 10f);
                    }
                }
            });

            chatHelper.OnCommand("current", () =>
            {
                RectZone zone = GetCurrentZoneForPlayer(player);

                if (zone != null)
                {
                    player.ChatMessage(Lang("CurrentZone", player).Replace("{id}", zone.Definition.Id));
                }
                else
                {
                    player.ChatMessage(Lang("CurrentZoneNoZone", player));
                }
            });

            chatHelper.OnCommand("clear", () =>
            {
                foreach (GameObject zone in currentZones)
                {
                    GameObject.Destroy(zone);
                }

                currentZones.Clear();
                data.Zones.Clear();

                SaveData();

                player.ChatMessage(Lang("Cleared", player));
            });
        }
Exemplo n.º 2
0
        void Init()
        {
            LoadData();
            permission.RegisterPermission(PluginPermission, this);

            cmdr = new Commander(/*PlayerHasPermission*/);

            cmdr.Add(null, null, (player, args) =>
            {
                player.ChatMessage(Lang("HelpText", player));
            });

            cmdr.Add("add", null, (player, args) =>
            {
                if (CheckIsEditing(player) != null)
                {
                    player.ChatMessage(Lang("AlreadyEditing", player));
                    return;
                }

                if (args["height"].Value == null)
                {
                    player.ChatMessage(Lang("InvalidAddCommand", player));
                    return;
                }

                if (args["height"].IsInvalid)
                {
                    player.ChatMessage(Lang("HeightMustBeNumber", player));
                    return;
                }

                if (args.Count == 1)
                {
                    CommandAdd(player, args["height"].Get <float>());
                    return;
                }

                if (args["fixed"].String == "fixed")
                {
                    if (args["name"].Value == null)
                    {
                        CommandAdd(player, args["height"].Single, null, true);
                    }
                    else
                    {
                        CommandAdd(player, args["height"].Single, args["name"].String, true);
                    }
                }
                else
                {
                    CommandAdd(player, args["height"].Single, args["fixed"].String);
                }
            }).AddParam("height", Commander.ParamType.Float)
            .AddParam("fixed")
            .AddParam("name");

            cmdr.Add("finish", null, (player, args) =>
            {
                TemporaryStorage storage = CheckIsEditing(player);

                if (storage != null)
                {
                    if (storage.Zone.Vertices.Count < MinPoints || storage.Zone.Vertices.Count > MaxPoints)
                    {
                        player.ChatMessage(Lang("DonePointsCount", player));
                        return;
                    }

                    CommandFinish(player);
                    player.ChatMessage(Lang("Done", player).Replace("{id}", storage.Zone.Id).Replace("{name}", storage.Zone.Name));
                }
            });

            cmdr.Add("undo", null, (player, args) =>
            {
                TemporaryStorage storage = CheckIsEditing(player);

                if (storage != null)
                {
                    if (storage.Zone.Vertices.Count != 0)
                    {
                        storage.Zone.Vertices.RemoveRange(storage.Zone.Vertices.Count - 2, 2);
                        player.ChatMessage(Lang("UndoComplete", player));
                    }
                }
            });

            cmdr.Add("list", null, (player, args) =>
            {
                if (data.Zones.Count == 0)
                {
                    player.ChatMessage(Lang("Empty", player));
                    return;
                }

                var result = new StringBuilder(Lang("List", player).Length);
                result.Append(Lang("List", player));

                foreach (ZoneDefinition zd in data.Zones)
                {
                    result.Append(zd.Id + (zd.Name != null ? (" (" + zd.Name + ")") : "") + ", ");
                }

                player.ChatMessage(result.Remove(result.Length - 2, 2).ToString());
            });

            cmdr.Add("current", null, (player, args) =>
            {
                RectZone zone = GetCurrentZoneForPlayer(player);

                if (zone != null)
                {
                    player.ChatMessage(Lang("CurrentZone", player).Replace("{id}", zone.Definition.Id));
                }
                else
                {
                    player.ChatMessage(Lang("CurrentZoneNoZone", player));
                }
            });

            cmdr.Add("clear", null, (player, args) =>
            {
                foreach (GameObject zone in currentZones)
                {
                    GameObject.Destroy(zone);
                }

                currentZones.Clear();
                data.Zones.Clear();

                player.ChatMessage(Lang("Cleared", player));
            });

            cmdr.Add("show", null, (player, args) =>             // TODO show by name
            {
                if (args["id"].Value != null)
                {
                    if (args["id"].String.Equals("current", StringComparison.CurrentCultureIgnoreCase))
                    {
                        RectZone zone = GetCurrentZoneForPlayer(player);
                        zone.ShowZone(player, 10f);
                    }
                    else
                    {
                        var zone = currentZones.First((z) => z.GetComponent <RectZone>().Definition.Id.Equals(args["id"].String));

                        if (zone != null)
                        {
                            zone.GetComponent <RectZone>().ShowZone(player, 10f);
                        }
                        else
                        {
                            player.ChatMessage(Lang("ZoneNotFound", player));
                        }
                    }
                }
                else
                {
                    if (currentZones.Count == 0)
                    {
                        player.ChatMessage(Lang("ShowEmpty", player));
                        return;
                    }

                    foreach (GameObject zone in currentZones)
                    {
                        zone.GetComponent <RectZone>().ShowZone(player, 10f);
                    }
                }
            }).AddParam("id");

            cmdr.Add("remove", null, (player, args) =>
            {
                if (CheckIsEditing(player) != null)
                {
                    player.ChatMessage(Lang("AlreadyEditing", player));
                    return;
                }

                if (args["nameOrId"].Value != null)
                {
                    if (IsDigitsOnly(args["nameOrId"].String))
                    {
                        int removed = data.Zones.RemoveWhere(x => x.Id == args["nameOrId"].String);

                        if (removed <= 0)
                        {
                            player.ChatMessage(Lang("ZoneNotFound", player));
                            return;
                        }

                        currentZones.RemoveWhere((GameObject go) =>
                        {
                            RectZone rz = go.GetComponent <RectZone>();

                            if (rz.Definition.Id == args["nameOrId"].String)
                            {
                                GameObject.Destroy(go);
                                return(true);
                            }

                            return(false);
                        });

                        player.ChatMessage(Lang("Removed", player));
                    }
                    else
                    {
                        int removed = data.Zones.RemoveWhere((ZoneDefinition x) =>
                        {
                            if (x.Name == null)
                            {
                                return(false);
                            }

                            return(x.Name.Equals(args["nameOrId"].String, StringComparison.CurrentCultureIgnoreCase));
                        });

                        if (removed <= 0)
                        {
                            player.ChatMessage(Lang("ZoneNotFound", player));
                            return;
                        }

                        currentZones.RemoveWhere((GameObject go) =>
                        {
                            RectZone rz = go.GetComponent <RectZone>();

                            if (rz.Definition.Name.Equals(args["nameOrId"].String, StringComparison.CurrentCultureIgnoreCase))
                            {
                                GameObject.Destroy(go);
                                return(true);
                            }

                            return(false);
                        });

                        player.ChatMessage(Lang("Removed", player));
                    }
                }
                else
                {
                    player.ChatMessage(Lang("RemoveHelp", player));
                }
            }).AddParam("nameOrId");

            cmdr.Add("options", null, (player, args) =>
            {
                TemporaryStorage storage = CheckIsEditing(player);

                if (storage == null)
                {
                    if (args["command"].Value != null)
                    {
                        if (IsDigitsOnly(args["command"].String))
                        {
                            ZoneDefinition definition = null;

                            foreach (ZoneDefinition d in data.Zones)
                            {
                                if (d.Id == args["command"].String)
                                {
                                    definition = d;
                                    break;
                                }
                            }

                            if (definition == null)
                            {
                                player.ChatMessage(Lang("ZoneNotFound", player));
                                return;
                            }

                            if (definition.Options.Count == 0)
                            {
                                player.ChatMessage(Lang("NoOptions", player));
                                return;
                            }

                            string result = string.Empty;
                            foreach (KeyValuePair <string, string> option in definition.Options)
                            {
                                result += option.Key + (option.Value != null ? (" = " + option.Value) : "") + ", ";
                            }

                            player.ChatMessage(result.Substring(0, result.Length - 2));
                        }
                        else
                        {
                            player.ChatMessage(Lang("OptionsHelp", player));
                        }
                    }
                    else
                    {
                        string result = string.Empty;

                        foreach (KeyValuePair <string, string> option in availableOptions)
                        {
                            result += "<color=#ffa500ff>" + option.Key + "</color> - " + (option.Value ?? "No description") + "\n";
                        }

                        player.ChatMessage(result);
                    }
                }
                else
                {
                    if (args["command"].Value != null)
                    {
                        if (args["command"].String == "list")
                        {
                            string result = string.Empty;

                            foreach (KeyValuePair <string, string> option in availableOptions)
                            {
                                result += "<color=#ffa500ff>" + option.Key + "</color> - " + (option.Value ?? "") + "\n";
                            }

                            player.ChatMessage(result);

                            return;
                        }

                        string[] opts = args["command"].String.Split(' ');

                        for (int i = 1; i < opts.Length; i++)
                        {
                            string[] option = opts[i].Split(new char[] { '=' }, 2);

                            storage.Zone.Options.Add(option[0], (option.Length > 1 ? option[1] : null));
                        }
                    }
                    else
                    {
                        if (storage.Zone.Options.Count != 0)
                        {
                            string result = string.Empty;
                            foreach (KeyValuePair <string, string> option in storage.Zone.Options)
                            {
                                result += option.Key + " = " + option.Value + ", \n";
                            }

                            player.ChatMessage(result.Substring(0, result.Length - 2));
                        }
                        else
                        {
                            player.ChatMessage(Lang("NoOptions", player));
                        }
                    }
                }
            }).AddParam("command", Commander.ParamType.String, true);
        }