Ragemp-roleplay-skript/Fahrzeuganmeldestelle.cs
2019-04-16 13:59:07 +02:00

248 lines
9.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GrandTheftMultiplayer.Server.API;
using GrandTheftMultiplayer.Server.Constant;
using GrandTheftMultiplayer.Server.Elements;
using GrandTheftMultiplayer.Server.Managers;
using GrandTheftMultiplayer.Shared;
using GrandTheftMultiplayer.Shared.Math;
namespace Roleplay.Environment
{
class Fahrzeuganmeldestelle : Script
{
public Fahrzeuganmeldestelle()
{
API.onEntityEnterColShape += onEnter;
API.onClientEventTrigger += onCallbacK;
populateAnmeldeStellen();
API.delay(1000*15,true,() => {setPlates();});
}
public int fahrzeuge = 0;
public void setPlates()
{
API.consoleOutput("Loading Vehicle plates after 15 Seconds");
List<NetHandle> vehicles = API.getAllVehicles();
foreach(NetHandle vehicle in vehicles){
if(vehicle != null){
Vehicle veh = API.getEntityFromHandle<Vehicle>(vehicle);
if(API.hasEntityData(veh,"numberplate") == true){
string numberplate = API.getEntityData(veh,"numberplate");
API.setVehicleNumberPlate(veh,numberplate);
fahrzeuge = fahrzeuge +1;
}
}
}
API.consoleOutput("Loaded " + fahrzeuge + " Numberplates!");
}
public const int ANMELDESTELLECALLBACK = 645994561;
public List<Vehicle> getTempPrivates(Client player)
{
List<NetHandle> vehs = API.getAllVehicles();
List<Vehicle> main = new List<Vehicle>();
foreach(NetHandle nh in vehs)
{
if(nh != null){
Vehicle veh = API.getEntityFromHandle<Vehicle>(nh);
if(veh != null){
if(veh.hasData("besitzer") == true && veh.getData("besitzer") == player.name){
main.Add(veh);
}
}
}
}
return main;
}
private void onEnter(ColShape colshape, NetHandle entity)
{
if(colshape != null && entity != null)
{
if(colshape.hasData("anmeldestelle") && colshape.getData("anmeldestelle") == true)
{
Client player = API.getPlayerFromHandle(entity);
List<Vehicle> privates = getTempPrivates(player);
object[] argslist = new object[7 + privates.Count * 2];
argslist[0] = ANMELDESTELLECALLBACK;
argslist[1] = "Fahrzeuge";
argslist[2] = "Optionen:";
argslist[3] = true;
argslist[4] = privates.Count + 1;
argslist[5] = "~r~Verlassen";
var i = 0;
foreach (Vehicle s in privates)
{
string x = "Unbekannt";
if(hasKennzeichen(s) == true)
{
x = "~g~" + s.displayName;
}
else
{
x = "~r~" + s.displayName;
}
argslist[6 + i] = x;
i++;
}
API.triggerClientEvent(player, "menu_handler_create_menu", argslist);
}
}
}
private void onCallbacK(Client sender, string eventName, object[] arguments)
{
if (eventName == "menu_handler_select_item")
{
int callback = (int)arguments[0];
if (callback == ANMELDESTELLECALLBACK)
{
int index = (int)arguments[1];
if (index != 0)
{
List<Vehicle> privates = getTempPrivates(sender);
Vehicle selectedVehicle = privates[index - 1];
if (hasKennzeichen(selectedVehicle) == false)
{
API.triggerClientEvent(sender, "get_user_input", ANMELDESTELLECALLBACK + 1, "", 7, selectedVehicle);
}
else
{
API.sendNotificationToPlayer(sender, "Das Fahrzeug ist schon angemeldet!");
}
}
else
{
API.triggerClientEvent(sender, "menu_handler_close_menu");
}
}
}
else if (eventName == "menu_handler_user_input")
{
int callback = (int)arguments[0];
if (callback == ANMELDESTELLECALLBACK + 1)
{
string text = (string)arguments[1];
Vehicle selectedVehicle = API.getEntityFromHandle<Vehicle>((NetHandle)arguments[2]);
if (doesKennzeichenAlreadyExist(text) == false)
{
setNewKennzeichen(sender, selectedVehicle, text);
API.setEntityData(selectedVehicle,"numberplate",text);
API.setVehicleNumberPlate(selectedVehicle,text);
API.sendNotificationToPlayer(sender, "~r~Lade...");
API.delay(1000 * 2, true, () => {
setKennzeichenToVehicleFromDatabase(selectedVehicle);
API.sendNotificationToPlayer(sender, "~g~Kennzeichen übertragen!");
});
}
else
{
API.sendNotificationToPlayer(sender, "Dieses Kennzeichen existiert schon!");
}
}
}
}
public void populateAnmeldeStellen()
{
Blip anmeldestelle = API.createBlip(new Vector3(238,-413,48));
anmeldestelle.sprite = 498;
RAPI.Marker.createMarker(0, anmeldestelle.position, 1, 255, 255, 255, "anmeldestelle", 0, 185, "Fahrzeug Anmeldestelle", 0);
}
// WWED new Vector3(1051.491, -3196.536, -39.14842);
public static void setKennzeichenToVehicleFromDatabase(Vehicle veh)
{
String numberplate = Vehiclesystem.VehicleMain.getDataBaseString(veh,"numberplate");
veh.numberPlate = numberplate;
API.shared.setVehicleNumberPlate(veh, numberplate);
API.shared.setEntityData(veh, "numberplate", numberplate);
}
public static bool hasKennzeichen(Client player, int slot){
List<Vehicle> vehlist = Vehiclesystem.VehicleMain.getPrivateVehicles(player);
bool state = false;
foreach(Vehicle veh in vehlist){
if(veh != null){
if(veh.hasData("slot") == true && veh.getData("slot") == slot){
state = true;
}
}
}
return state;
}
public static String getKennzeichenOwner(string plate)
{
List<NetHandle> vehicles = API.shared.getAllVehicles();
foreach(NetHandle nh in vehicles)
{
if(nh != null)
{
Vehicle veh = API.shared.getEntityFromHandle<Vehicle>(nh);
if(API.shared.hasEntityData(veh,"numberplate") == true && API.shared.hasEntityData(veh,"besitzer") == true)
{
String owner = API.shared.getEntityData(veh, "besitzer");
return owner;
}
}
}
return "";
}
public static string getKennzeichen(Vehicle veh)
{
if(API.shared.hasEntityData(veh,"numberplate") == true)
{
String numberplate = API.shared.getEntityData(veh, "numberplate");
return numberplate;
}
else
{
return "";
}
}
public static bool doesKennzeichenAlreadyExist(String numberplate)
{
bool state = false;
foreach(NetHandle nh in API.shared.getAllVehicles())
{
if(nh != null){
Vehicle veh = API.shared.getEntityFromHandle<Vehicle>(nh);
if(veh.numberPlate == numberplate){
state = true;
}
}
}
return state;
}
public static bool hasKennzeichen(Vehicle veh)
{
if(getKennzeichen(veh) != "")
{
return true;
}
return false; ;
}
public static void setNewKennzeichen(Client player,Vehicle veh, String str)
{
Vehiclesystem.VehicleMain.setDatabaseString(player, veh, "numberplate", str);
}
}
}