Example #1
0
        private void OnCreateSpeedZone([FromSource] Player player, byte[] data)
        {
            if (!IsConfigurationEnabled("use_speed_zones"))
            {
                return;
            }

            if (!IsPlayerAllowed(player))
            {
                return;
            }

            SpeedZone.Create(data);
        }
Example #2
0
        public static void Create(int radius, float speed)
        {
            SpeedZone zone = new SpeedZone()
            {
                Position = GetEntityCoords(PlayerPedId(), true),
                Radius   = radius,
                Speed    = speed
            };

            using MemoryStream stream = new MemoryStream();
            using BinaryWriter writer = new BinaryWriter(stream);
            writer.Write(zone);

            BaseScript.TriggerServerEvent("TrafficManager:CreateSpeedZone", stream.ToArray());
        }
        public static void Create(byte[] data)
        {
            using MemoryStream stream = new MemoryStream(data);
            using BinaryReader reader = new BinaryReader(stream);

            SpeedZone zone = reader.Read <SpeedZone>();

            zone.Id = Utility.GetNextIdentifier();
            List.Add(zone);

            stream.Position           = 0;
            using BinaryWriter writer = new BinaryWriter(stream);
            writer.Write(zone);

            BaseScript.TriggerClientEvent("TrafficManager:CreateSpeedZone", stream.ToArray());
        }
Example #4
0
        private void OnRemoveSpeedZone([FromSource] Player player, int id)
        {
            if (!IsConfigurationEnabled("use_speed_zones"))
            {
                return;
            }

            if (!IsPlayerAllowed(player))
            {
                return;
            }

            SpeedZone zone = SpeedZone.List.FirstOrDefault(z => z.Id == id);

            if (zone == null)
            {
                return;
            }

            SpeedZone.List.Remove(zone);
            TriggerClientEvent("TrafficManager:RemoveSpeedZone", id);
        }
Example #5
0
        public Main()
        {
            EventHandlers["TrafficManager:CreateSpeedZone"] += new Action <byte[]>((data) =>
            {
                using MemoryStream stream = new MemoryStream(data);
                using BinaryReader reader = new BinaryReader(stream);

                SpeedZone zone = reader.Read <SpeedZone>();
                zone.Enable();
                SpeedZone.List.Add(zone);
            });

            EventHandlers["TrafficManager:RemoveSpeedZone"] += new Action <int>((id) =>
            {
                SpeedZone zone = SpeedZone.List.FirstOrDefault(z => z.Id == id);
                if (zone == null)
                {
                    return;
                }

                zone.Disable();
                SpeedZone.List.Remove(zone);
            });

            EventHandlers["TrafficManager:CreateSecureZone"] += new Action <byte[]>((data) =>
            {
                using MemoryStream stream = new MemoryStream(data);
                using BinaryReader reader = new BinaryReader(stream);

                SecureZone zone = reader.Read <SecureZone>();
                zone.Enable();
                SecureZone.List.Add(zone);

                Tick += SecureZoneThread;
            });

            EventHandlers["TrafficManager:RemoveSecureZone"] += new Action <int>((id) =>
            {
                SecureZone zone = SecureZone.List.FirstOrDefault(z => z.Id == id);
                if (zone == null)
                {
                    return;
                }

                zone.Disable();
                SecureZone.List.Remove(zone);

                if (SecureZone.List.Count == 0)
                {
                    Tick -= SecureZoneThread;
                }
            });

            EventHandlers["TrafficManager:EditNode"] += new Action <byte[]>((data) =>
            {
                using MemoryStream stream = new MemoryStream(data);
                using BinaryReader reader = new BinaryReader(stream);

                Node node = reader.Read <Node>();
                node.Enable();
                Node.List.Add(node);
            });

            EventHandlers["TrafficManager:RemoveNode"] += new Action <int>((id) =>
            {
                Node node = Node.List.FirstOrDefault(n => n.Id == id);
                if (node == null)
                {
                    return;
                }

                node.Disable();
                Node.List.Remove(node);
            });

            EventHandlers["TrafficManager:FullSync"] += new Action <byte[]>((data) =>
            {
                using MemoryStream stream = new MemoryStream(data);
                using BinaryReader reader = new BinaryReader(stream);

                List <SpeedZone> speedZones = reader.ReadList <SpeedZone>();
                speedZones.ForEach(s => s.Enable());
                SpeedZone.List.AddRange(speedZones);

                List <SecureZone> secureZones = reader.ReadList <SecureZone>();
                secureZones.ForEach(s => s.Enable());
                SecureZone.List.AddRange(secureZones);

                if (SecureZone.List.Count > 0)
                {
                    Tick += SecureZoneThread;
                }

                List <Node> nodes = reader.ReadList <Node>();
                nodes.ForEach(n => n.Enable());
                Node.List.AddRange(nodes);
            });

            EventHandlers["TrafficManager:ToggleMenu"] += new Action(() =>
            {
                Menu.Instance.Toggle();
            });

            // This would be registered on the server, but for some unbewnost reason RegisterKeyMapping fails to work with it on the server.
            // This method is just as safe.
            RegisterCommand("trafficmenu", new Action <int, List <object>, string>((source, args, raw) =>
            {
                TriggerServerEvent("TrafficManager:ToggleMenu");
            }), false);

            string defaultKey = GetResourceMetadata(GetCurrentResourceName(), "default_menu_button", 0) ?? "F5";

            RegisterKeyMapping("trafficmenu", "Traffic", "keyboard", defaultKey);

            Menu.ForceLoad();

            TriggerServerEvent("TrafficManager:PlayerConnected");
        }
Example #6
0
        private void AddSpeedZoneMenu()
        {
            UIMenu submenu = menuPool.AddSubMenu(mainMenu, "Speed Zones");

            speedZoneMenu = submenu; // Used in ForceLoad, serves no other purpose.
            submenu.MouseControlsEnabled = false;

            List <dynamic> radiusList = new List <dynamic>()
            {
                5, 10, 15, 20, 25, 30, 40, 50, 75, 100
            };
            UIMenuListItem radius = new UIMenuListItem("Radius", radiusList, 0);

            submenu.AddItem(radius);

            string speedType = GetResourceMetadata(GetCurrentResourceName(), "speed_type", 0) ?? "MPH";

            List <dynamic> speedList = new List <dynamic>()
            {
                0, 5, 10, 15, 20, 25, 30, 40, 50, 60, 70
            };
            UIMenuListItem speed = new UIMenuListItem($"Speed ({speedType.ToUpper()})", speedList, 0);

            submenu.AddItem(speed);

            UIMenuItem create = new UIMenuItem("~b~Create");

            submenu.AddItem(create);

            UIMenuItem remove = new UIMenuItem("~r~Remove Closest");

            submenu.AddItem(remove);

            submenu.OnItemSelect += (sender, item, index) =>
            {
                if (item == create)
                {
                    float speedDiv = speedType.ToLower() == "mph" ? 2.237f : 3.59f;

                    SpeedZone.Create(radiusList[radius.Index], speedList[speed.Index] / speedDiv);
                }
                else if (item == remove)
                {
                    Vector3 myPos = GetEntityCoords(PlayerPedId(), true);

                    SpeedZone closest         = null;
                    float     closestDistance = 100f;
                    foreach (SpeedZone zone in SpeedZone.List)
                    {
                        float distance = Vector3.Distance(myPos, zone.Position);
                        if (distance < closestDistance)
                        {
                            closest         = zone;
                            closestDistance = distance;
                        }
                    }

                    if (closest != null)
                    {
                        BaseScript.TriggerServerEvent("TrafficManager:RemoveSpeedZone", closest.Id);
                    }
                }
            };
        }