Exemple #1
0
        public static void StartRefill(Client client, OwnedVehicle ownedVehicle, FuelType fuelType)
        {
            if (!client.hasData("player"))
            {
                return;
            }
            Player player = client.getData("player");

            if (ownedVehicle.ActiveHandle.engineStatus)
            {
                API.shared.sendNotificationToPlayer(client, "~r~You must first turn off the engine before you start refuel.");
                return;
            }
            GasStation gasStation = GasStationList.FirstOrDefault(x => x.Position.DistanceTo(client.position) <= 20);

            client.setData("gasStation", gasStation);
            if (gasStation == null)
            {
                return;
            }

            if (VehicleService.GetVehicleInfo(ownedVehicle.ModelName).Fuel != fuelType)
            {
                API.shared.sendNotificationToPlayer(client, "~o~You don't wont fill " + fuelType.ToString() + " in your " + ownedVehicle.ModelName + "(" +
                                                    VehicleService.GetVehicleInfo(ownedVehicle.ModelName).Fuel.ToString() + ").. ~r~~n~Remember that..");
                return;
            }

            int    maxFuel   = VehicleService.GetVehicleInfo(ownedVehicle.ModelName).MaxFuel;
            int    startFuel = ownedVehicle.Fuel;
            double price     = 0;

            switch (fuelType)
            {
            case FuelType.Petrol:
                price = gasStation.StationFuelPrices.Petrol;
                break;

            case FuelType.Diesel:
                price = gasStation.StationFuelPrices.Diesel;
                break;

            case FuelType.Gas:
                price = gasStation.StationFuelPrices.Gas;
                break;

            case FuelType.Electricity:
                price = gasStation.StationFuelPrices.Electricity;
                break;

            case FuelType.Kerosene:
                price = gasStation.StationFuelPrices.Kerosene;
                break;
            }

            API.shared.triggerClientEvent(client, "ShowRefuelProgressbar", ownedVehicle.Fuel, maxFuel);
            client.setData("fuelStartPosition", client.position);
            client.setSyncedData("currentfuel", ownedVehicle.Fuel);
            client.setData("fuelcar", ownedVehicle);
            client.setData("fuelcarstart", ownedVehicle.Fuel);
            client.setData("fueltype", fuelType);
            Timer timer = API.shared.startTimer(1000, false, () => {
                if (ownedVehicle.Fuel < VehicleService.GetVehicleInfo(ownedVehicle.ModelName).MaxFuel)
                {
                    Vector3 startPositon = client.getData("fuelStartPosition");
                    if (startPositon.DistanceTo(client.position) > 4f)
                    {
                        StopRefill(client);
                        return;
                    }

                    switch (fuelType)
                    {
                    case FuelType.Petrol:
                        if (gasStation.Storage.Petrol > 0)
                        {
                            gasStation.Storage.Petrol--;
                        }
                        else
                        {
                            StopRefill(client);
                            API.shared.sendNotificationToPlayer(client, "~o~This gas station has no more ~b~Petrol~o~ in stock..");
                            return;
                        }
                        break;

                    case FuelType.Diesel:
                        if (gasStation.Storage.Diesel > 0)
                        {
                            gasStation.Storage.Diesel--;
                        }
                        else
                        {
                            StopRefill(client);
                            API.shared.sendNotificationToPlayer(client, "~o~This gas station has no more ~b~Diesel~o~ in stock..");
                            return;
                        }
                        break;

                    case FuelType.Gas:
                        if (gasStation.Storage.Gas > 0)
                        {
                            gasStation.Storage.Gas--;
                        }
                        else
                        {
                            StopRefill(client);
                            API.shared.sendNotificationToPlayer(client, "~o~This gas station has no more ~b~Gas~o~ in stock..");
                            return;
                        }
                        break;
                    }

                    if (player.Character.Bank < GetFuelPrice(gasStation, fuelType))
                    {
                        StopRefill(client);
                        API.shared.sendNotificationToPlayer(client, "~r~You don't have enough Money to continue the Refuel..");
                        return;
                    }


                    ownedVehicle.Fuel++;
                    player.Character.Bank   -= price;
                    gasStation.MoneyStorage += price;
                    ownedVehicle.ActiveHandle.setSyncedData("fuel", ownedVehicle.Fuel);
                    client.setSyncedData("currentfuel", ownedVehicle.Fuel);
                }
                else
                {
                    StopRefill(client);
                }
            });

            client.setData("refuelTimer", timer);
        }