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

366 lines
No EOL
17 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GrandTheftMultiplayer.Server;
using GrandTheftMultiplayer.Shared;
using GrandTheftMultiplayer.Server.API;
using GrandTheftMultiplayer.Shared.Math;
using GrandTheftMultiplayer.Server.Elements;
using GrandTheftMultiplayer.Server.Constant;
using GrandTheftMultiplayer.Server.Managers;
using MySql.Data.MySqlClient;
using System.Data;
namespace Roleplay.Environment
{
class Weedplantage : Script
{
public static string myConnectionString = "SERVER=185.223.28.49;" + "DATABASE=gtanetwork;" + "UID=fivem;" + "PASSWORD=" + Main.databasepass + ";";
public static MySqlConnection connection;
public static MySqlCommand command;
public static MySqlDataReader reader;
public Weedplantage()
{
loadweed();
weedtimer();
API.onEntityEnterColShape += onCol;
API.onEntityExitColShape += onColEx;
API.onClientEventTrigger += onCallBack;
createWeedShop();
}
public int weedsamenprice = new Random().Next(1000,3000);
public int weedsellprice = new Random().Next(500,1000);
public const float wachstumsfaktor = 0.020F;
public void createWeedShop()
{
RAPI.Marker.createMarker(0,new Vector3(2482.446,3722.299,43.92),1,255,255,255,"weedshop",0,150,"",0);
API.createPed(PedHash.OmegaCutscene,new Vector3(-2078,2602.221,2.032635),20,0);
RAPI.Marker.createMarker(0,new Vector3(-2078.19,2602.665,1),1,0,155,0,"weedkauf",0,185,"",0);
}
public void weedtimer()
{
API.delay(60000*2,false,() => { // jede 2 Minuten
List<NetHandle> objects = API.getAllObjects();
foreach(NetHandle nh in objects){
if(nh != null){
GrandTheftMultiplayer.Server.Elements.Object obj = API.getEntityFromHandle<GrandTheftMultiplayer.Server.Elements.Object>(nh);
if(obj != null && API.hasEntityData(obj,"weedpercentage") == true){
int current = API.getEntityData(obj,"weedpercentage");
if(current != 100){
int percentage = API.getEntityData(obj,"weedpercentage") + 1;
int weedid = API.getEntityData(obj,"ID");
API.setEntityData(obj,"weedpercentage",percentage);
float x = obj.getData("x");
float y = obj.getData("y");
float z = obj.getData("z");
API.moveEntityPosition(obj,new Vector3(x,y,z).Add(new Vector3(0,0,(float)(percentage * wachstumsfaktor))),1000);
TextLabel tx = obj.getData("textlabel");
tx.text = "Fortschritt: ~y~" + percentage + "%";
updateWeed(weedid,percentage);
} else { // ~n~ ~y~/ernten ~w~zum abernten!
int percentage = API.getEntityData(obj,"weedpercentage");
float x = obj.getData("x");
float y = obj.getData("y");
float z = obj.getData("z");
TextLabel tx = obj.getData("textlabel");
API.moveEntityPosition(obj,new Vector3(x,y,z).Add(new Vector3(0,0,(float)(percentage * wachstumsfaktor))),1000);
tx.text = "Fortschritt: ~y~" + percentage + "% ~n~ ~y~/ernten ~w~zum abernten!";
}
}
}
}
});
}
public static void updateWeed(int id, int percentage)
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "UPDATE `weed` SET weedpercentage = " + percentage + " WHERE ID = " + id;
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
public const int CALLBACK = 894561852;
public void onCol(ColShape col, NetHandle nh )
{
if(col != null && nh != null){
if(col.hasData("weed") == true){
GrandTheftMultiplayer.Server.Elements.Object weed = col.getData("weed");
Client player = API.getPlayerFromHandle(nh);
if(weed != null && player != null){
player.setData("weed",weed);
if(Fraktionen.Fraktionsmanager.getFraktion(player) == 1){
API.sendChatMessageToPlayer(player,"Du musst das Weed verbrennen da du Polizist bist! /verbrennen");
}
}
} else if(col.hasData("weedshop") == true && col.getData("weedshop") == true){
Client player = API.getPlayerFromHandle(nh);
object[] argumentList = new object[14];
argumentList[0] = CALLBACK;
argumentList[1] = "Weedshop";
argumentList[2] = "Produkte:";
argumentList[3] = false;
int itemlength = 2;
argumentList[4] = itemlength;
argumentList[5] = "Schliessen";
argumentList[6] = "Samen kaufen";
argumentList[7] = "";
argumentList[8] = "Verkaufspreis: ~b~" + weedsamenprice;
API.triggerClientEvent(player, "menu_handler_create_menu", argumentList);
} else if(col.hasData("weedkauf") == true && col.getData("weedkauf") == true){
Client player = API.getPlayerFromHandle(nh);
object[] argumentList = new object[14];
argumentList[0] = CALLBACK + 98;
argumentList[1] = "Weedverkauf";
argumentList[2] = "Produkte:";
argumentList[3] = false;
int itemlength = 2;
argumentList[4] = itemlength;
argumentList[5] = "Schliessen";
argumentList[6] = "1 Joint verkaufen";
argumentList[7] = "";
argumentList[8] = "Verkaufspreis: ~b~$" + weedsellprice;
API.triggerClientEvent(player, "menu_handler_create_menu3", argumentList);
}
}
}
public void onCallBack(Client sender, string eventName, object[] args)
{
if(eventName == "menu_handler_select_item"){
int callback = (int) args[0];
int index = (int) args[1];
if(callback == CALLBACK){
if(index == 1){
if(Environment.MONEY.Money.hasBarMoney(sender,weedsamenprice) == true){
Environment.MONEY.Money.giveBarMoney(sender,-weedsamenprice,API);
Items.InventoryHolder invh = API.getEntityData(sender,"InventoryHolder");
invh.AddItemToInventory(Items.ItemByID(Items.ITEM_ID_WEED_SAMEN));
} else {
API.sendNotificationToPlayer(sender,"Du verschwendest meine Zeit! Verschwinde!!!");
}
}
} else if(callback == CALLBACK + 98){
if(index == 1){
Items.InventoryHolder invh = API.getEntityData(sender,"InventoryHolder");
if(invh.hasItemInInventory(Items.ItemByID(Items.ITEM_ID_WEED)) == true){
API.playPlayerScenario(sender,"WORLD_HUMAN_DRUG_DEALER");
API.sleep(1000*5);
invh.RemoveItemFromInventory(Items.ItemByID(Items.ITEM_ID_WEED));
API.stopPlayerAnimation(sender);
Environment.MONEY.Money.giveBarMoney(sender,weedsellprice,API,"");
} else {
API.sendNotificationToPlayer(sender,"Du hast keinen Joint in deinem Inventar!");
}
} else if(index == 0){
API.triggerClientEvent(sender,"menu_handler_close_menu");
}
}
}
}
public void onColEx(ColShape col, NetHandle nh )
{
if(col != null && nh != null){
if(col.hasData("weed")){
Client player = API.getPlayerFromHandle(nh);
if(player != null){
API.resetEntityData(player,"weed");
}
}
}
}
[Command("ernten")]
public void ernten(Client player)
{
if(player.hasData("weed") == true){
GrandTheftMultiplayer.Server.Elements.Object weed = player.getData("weed");
int percentage = API.getEntityData(weed,"weedpercentage");
if(percentage == 100){
API.shared.playPlayerScenario(player, "WORLD_HUMAN_GARDENER_PLANT");
API.sleep(1000*10);
API.shared.stopPlayerAnimation(player);
int id = weed.getData("ID");
string owner = weed.getData("owner");
ColShape col = weed.getData("collision");
API.deleteColShape(col);
TextLabel tx = weed.getData("textlabel");
tx.delete();
removeWeed(owner,id);
weed.delete();
Items.InventoryHolder ivh = API.getEntityData(player,"InventoryHolder");
ivh.AddItemToInventory(Items.ItemByID(Items.ITEM_ID_WEED));
ivh.AddItemToInventory(Items.ItemByID(Items.ITEM_ID_WEED));
ivh.AddItemToInventory(Items.ItemByID(Items.ITEM_ID_WEED));
} else {
API.sendNotificationToPlayer(player,"Die Hanfplanze ist noch nicht Fertig!");
}
} else {
API.sendNotificationToPlayer(player,"Es ist keine Hanfplanze in der nähe!");
}
}
[Command("verbrennen")]
public void verbrennen(Client player)
{
if(player.hasData("weed") == true){
GrandTheftMultiplayer.Server.Elements.Object weed = player.getData("weed");
int percentage = API.getEntityData(weed,"weedpercentage");
int id = weed.getData("ID");
ColShape col = weed.getData("collision");
API.deleteColShape(col);
TextLabel tx = weed.getData("textlabel");
String owner = weed.getData("owner");
tx.delete();
weed.delete();
removeWeed(owner,id);
API.sleep(1000*3);
API.sendNativeToAllPlayers(Hash.START_SCRIPT_FIRE,weed.position.X,weed.position.Y,weed.position.Z,25,false); // not working somehow
API.sendNativeToAllPlayers(Hash.START_ENTITY_FIRE,weed); // not working somehow
}
}
[Command("resolveweed")]
public void resolveweed(Client player){
if(Admin.admin_cmds.isAdmin(player,3) == true){
if(player.hasData("weed") == true){
GrandTheftMultiplayer.Server.Elements.Object weed = player.getData("weed");
API.sendNotificationToPlayer(player,"Weed Planter: " + weed.getData("owner"));
}
}
}
public static void removeWeed(String player, int id)
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "DELETE FROM `weed` WHERE owner = '" + player + "' AND ID = " + id;
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
public static int getWeedAnzahl(String playername)
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM `weed` WHERE owner = '" + playername + "'";
connection.Open();
reader = command.ExecuteReader();
DataTable results = new DataTable();
results.Load(reader);
int i = 0;
foreach (DataRow dr in results.Rows)
{
i++;
}
return i;
}
public static void insertWeed(Client player)
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
Vector3 weedpos = player.position.Subtract(new Vector3(0,0,3));
command.CommandText = "INSERT INTO `weed`(`owner`,`x`,`y`,`z`,`weedpercentage`) VALUES ('" + player.name + "', " + Convert.ToInt32(weedpos.X) + ", " + Convert.ToInt32(weedpos.Y) + ", " + Convert.ToInt32(weedpos.Z) + ", " + 0 + ")";
connection.Open();
command.ExecuteNonQuery();
connection.Close();
command.CommandText = "SELECT * FROM `weed` WHERE owner = '" + player.name + "'";
connection.Open();
reader = command.ExecuteReader();
int id = 0;
while(reader.Read())
{
id = reader.GetInt32("ID");
}
connection.Close();
API.shared.consoleOutput("Inserted Weed in Database!");
GrandTheftMultiplayer.Server.Elements.Object weed = API.shared.createObject(452618762, weedpos,new Vector3(0,0,0),0);
API.shared.setEntityData(weed,"weedpercentage",0);
API.shared.setEntityData(weed,"owner",player.name);
API.shared.setEntityData(weed,"ID",id);
API.shared.setEntityData(weed,"x",weedpos.X);
API.shared.setEntityData(weed,"y",weedpos.Y);
API.shared.setEntityData(weed,"z",weedpos.Z);
ColShape col = API.shared.createCylinderColShape(weedpos,4,10);
TextLabel tx = API.shared.createTextLabel("Fortschritt: ~y~" + 0 + "% ", weedpos.Add(new Vector3(0,0,3)),10,1,true,0);
API.shared.setEntityData(weed,"textlabel",tx);
API.shared.setEntityData(weed,"collision",col);
col.setData("weed",weed);
RAPI.Marker.textes.Add(tx);
}
public void loadweed()
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM weed";
connection.Open();
reader = command.ExecuteReader();
DataTable results = new DataTable();
results.Load(reader);
foreach (DataRow dr in results.Rows)
{
int id = Convert.ToInt32(dr["ID"]);
string owner = Convert.ToString(dr["owner"]);
int x = Convert.ToInt32(dr["x"]);
int y = Convert.ToInt32(dr["y"]);
int z = Convert.ToInt32(dr["z"]);
int weedpercentage = Convert.ToInt32(dr["weedpercentage"]);
GrandTheftMultiplayer.Server.Elements.Object weed = API.createObject(452618762, new Vector3(x,y,z),new Vector3(0,0,0),0);
API.setEntityData(weed,"ID",id);
API.setEntityData(weed,"owner",owner);
API.setEntityData(weed,"x",x);
API.setEntityData(weed,"y",x);
API.setEntityData(weed,"z",x);
API.setEntityData(weed,"weedpercentage",weedpercentage);
weed.position = new Vector3(x,y,z).Add(new Vector3(0,0,(weedpercentage * wachstumsfaktor)));
ColShape col = API.createCylinderColShape(new Vector3(x,y,z),4,10);
TextLabel tx = API.createTextLabel("Fortschritt: ~y~" + weedpercentage + "%", weed.position.Add(new Vector3(0,0,3)),10,1,true,0);
API.shared.setEntityData(weed,"textlabel",tx);
API.shared.setEntityData(weed,"collision",col);
col.setData("weed",weed);
RAPI.Marker.textes.Add(tx);
}
connection.Close();
}
public static void plantWeed(Client owner)
{
if(getWeedAnzahl(owner.name) < 5){
Client player = owner;
API.shared.playPlayerScenario(player, "WORLD_HUMAN_GARDENER_PLANT");
API.shared.sleep(1000*10);
API.shared.stopPlayerAnimation(owner);
insertWeed(player);
} else {
API.shared.sendNotificationToPlayer(owner,"Du kannst nicht mehr als 5 Pflanzen pflanzen.");
Items.InventoryHolder invh = API.shared.getEntityData(owner,"InventoryHolder");
invh.AddItemToInventory(Items.ItemByID(Items.ITEM_ID_WEED_SAMEN)); // ADDING THE ITEM BACK TO THE INVENTORY
}
}
}
}