hanf continuation
This commit is contained in:
@@ -88,6 +88,8 @@ namespace ReallifeGamemode.Server.Events
|
||||
NAPI.World.RequestIpl("ferris_finale_Anim"); // Riesenrad
|
||||
NAPI.World.RequestIpl("Carwash_with_spinners"); // Carwash
|
||||
|
||||
HanfManager.UpdateHanfForPlayer(player);
|
||||
|
||||
TimeSpan currentTime = TimeManager.CurrentTime;
|
||||
bool disableLightMode = currentTime > LightModeTimeFrom && currentTime < LightModeTimeTo;
|
||||
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using GTANetworkAPI;
|
||||
using ReallifeGamemode.Database.Entities;
|
||||
using ReallifeGamemode.Database.Models;
|
||||
using ReallifeGamemode.Server.Extensions;
|
||||
using ReallifeGamemode.Server.Managers;
|
||||
|
||||
namespace ReallifeGamemode.Server.Inventory.Items
|
||||
{
|
||||
public class CannabisSeeds : BaseItem
|
||||
public class CannabisSeeds : UseItem
|
||||
{
|
||||
public override int Id { get; } = 109;
|
||||
public override string Name { get; } = "Cannabis Samen";
|
||||
@@ -13,5 +18,18 @@ namespace ReallifeGamemode.Server.Inventory.Items
|
||||
public override string Einheit { get; } = "g";
|
||||
public override int Price { get; } = 0;
|
||||
public override bool Legal => false;
|
||||
|
||||
public override uint Object { get; }
|
||||
public override bool RemoveWhenUsed { get; } = false;
|
||||
|
||||
public override bool Use(Player player, User user, DatabaseContext databaseContext)
|
||||
{
|
||||
if(!player.IsInVehicle)
|
||||
{
|
||||
HanfManager.StartCannabisPlanting(player);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Timers;
|
||||
using GTANetworkAPI;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using ReallifeGamemode.Database.Entities;
|
||||
using ReallifeGamemode.Database.Models;
|
||||
using ReallifeGamemode.Server.Extensions;
|
||||
@@ -47,8 +48,18 @@ namespace ReallifeGamemode.Server.Managers
|
||||
/// </summary>
|
||||
private const string _manufacturerAnim = "manufacturJoint";
|
||||
|
||||
/// <summary>
|
||||
/// Data-Key, ob der Spieler grade eine Pflanze anpflanzt
|
||||
/// </summary>
|
||||
private const string PLAYER_CURRENTLY_PLANTING_DATA_KEY = "isPlantingCannabis";
|
||||
|
||||
/// <summary>
|
||||
/// Timer der den Status des Verarbeiters zurücksetzt
|
||||
/// </summary>
|
||||
private static Timer _manufacturerDoneTimer = new Timer(TimeSpan.FromSeconds(10).TotalMilliseconds);
|
||||
|
||||
private static List<CannabisData> _currentCannabisData = new List<CannabisData>();
|
||||
|
||||
static HanfManager()
|
||||
{
|
||||
_manufacturerDoneTimer.Elapsed += ManufacturerDoneTimerCallback;
|
||||
@@ -72,11 +83,29 @@ namespace ReallifeGamemode.Server.Managers
|
||||
colShape = NAPI.ColShape.CreateSphereColShape(buyPoint, 20.0f);
|
||||
colShape.OnEntityEnterColShape += OnSeedBuyRangeColShapeEnter;
|
||||
}
|
||||
|
||||
UpdateHanfWorldData(new DatabaseContext());
|
||||
|
||||
Timer updateHanfDataTimer = new Timer(TimeSpan.FromMinutes(1).TotalMilliseconds);
|
||||
updateHanfDataTimer.Elapsed += UpdateHanfDataTimer_Elapsed;
|
||||
updateHanfDataTimer.Start();
|
||||
}
|
||||
|
||||
private static void UpdateHanfDataTimer_Elapsed(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
using var dbContext = new DatabaseContext();
|
||||
UpdateHanfWorldData(dbContext);
|
||||
}
|
||||
|
||||
private static void OnSeedBuyRangeColShapeEnter(ColShape colShape, Player player)
|
||||
{
|
||||
ChatService.SendMessage(player, $"Fremder sagt: Pssst.. Willst paar Samen erwerben?");
|
||||
var user = player.GetUser();
|
||||
if (user.Faction.StateOwned)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ChatService.SendMessage(player, $"Fremder sagt: Pssst.. Willst du Samen kaufen?");
|
||||
}
|
||||
|
||||
internal static bool IsPlayerNearSeedBuyPoint(Player player)
|
||||
@@ -89,6 +118,95 @@ namespace ReallifeGamemode.Server.Managers
|
||||
return _jointManufacturerPoint.DistanceTo(player.Position) <= 2.5f;
|
||||
}
|
||||
|
||||
internal static void StartCannabisPlanting(Player player)
|
||||
{
|
||||
player.ToggleInventory(InventoryToggleOption.HIDE);
|
||||
|
||||
if (!player.IsAlive())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var dbContext = new DatabaseContext();
|
||||
var user = player.GetUser(dbContext);
|
||||
if (user.Faction?.StateOwned ?? false)
|
||||
{
|
||||
player.SendNotification("~r~Du darfst keine Hanfsamen einfplanzen");
|
||||
return;
|
||||
}
|
||||
|
||||
if (player.HasData(PLAYER_CURRENTLY_PLANTING_DATA_KEY) && player.GetData<bool>(PLAYER_CURRENTLY_PLANTING_DATA_KEY))
|
||||
{
|
||||
player.SendNotification("~r~Du pflanzt aktuell schon einen Hanfsamen ein");
|
||||
return;
|
||||
}
|
||||
|
||||
player.SetData(PLAYER_CURRENTLY_PLANTING_DATA_KEY, true);
|
||||
|
||||
player.TriggerEvent("SERVER:Hanf_StartPlanting");
|
||||
}
|
||||
|
||||
[RemoteEvent("CLIENT:Hanf_PlantHanf")]
|
||||
public void HanfManagerPlantHanf(Player player, float x, float y, float z)
|
||||
{
|
||||
player.ResetData(PLAYER_CURRENTLY_PLANTING_DATA_KEY);
|
||||
|
||||
IItem cannabisSeedItem = InventoryManager.GetItem<CannabisSeeds>();
|
||||
UserItem userCannabisSeedsItem = InventoryManager.UserHasThisItem(player, cannabisSeedItem.Id);
|
||||
if (userCannabisSeedsItem == null)
|
||||
{
|
||||
player.SendNotification("~r~Du hast keine Samen mehr");
|
||||
return;
|
||||
}
|
||||
|
||||
logger.LogInformation("Player {0} planted a cannabis plant at x: {1}, y: {2}, z: {3}", player.Name, x, y, z);
|
||||
|
||||
using var dbContext = new DatabaseContext();
|
||||
var user = player.GetUser(dbContext);
|
||||
|
||||
InventoryManager.RemoveUserItem(user, userCannabisSeedsItem, 1);
|
||||
|
||||
CannabisPlant newPlant = new CannabisPlant()
|
||||
{
|
||||
X = x,
|
||||
Y = y,
|
||||
Z = z,
|
||||
PlantedBy = user,
|
||||
PlantDate = DateTime.Now,
|
||||
};
|
||||
|
||||
dbContext.CannabisPlants.Add(newPlant);
|
||||
dbContext.SaveChanges();
|
||||
|
||||
UpdateHanfWorldData(dbContext);
|
||||
}
|
||||
|
||||
public static void UpdateHanfWorldData(DatabaseContext dbContext)
|
||||
{
|
||||
var activePlants = dbContext.CannabisPlants.Where(p => !p.Harvested).Select(p => new CannabisData()
|
||||
{
|
||||
Id = p.Id,
|
||||
Time = p.PlantDate,
|
||||
X = p.X,
|
||||
Y = p.Y,
|
||||
Z = p.Z
|
||||
}).ToList();
|
||||
|
||||
_currentCannabisData = activePlants;
|
||||
|
||||
NAPI.Pools.GetAllPlayers().ForEach(p =>
|
||||
{
|
||||
UpdateHanfForPlayer(p, activePlants);
|
||||
});
|
||||
}
|
||||
|
||||
public async static void UpdateHanfForPlayer(Player player, List<CannabisData> cannabisData = null)
|
||||
{
|
||||
cannabisData ??= _currentCannabisData;
|
||||
var x = await NAPI.Task.WaitForMainThread();
|
||||
player.TriggerEvent("SERVER:Hanf_UpdateHanfData", JsonConvert.SerializeObject(cannabisData));
|
||||
}
|
||||
|
||||
[RemoteEvent("CLIENT:Hanf_BuySeeds")]
|
||||
public void HanfManagerBuySeeds(Player player, int amount)
|
||||
{
|
||||
@@ -129,7 +247,7 @@ namespace ReallifeGamemode.Server.Managers
|
||||
|
||||
internal static void BuildJointsFromCannabis(Player player)
|
||||
{
|
||||
if(player.HasAnimation(_manufacturerAnim) || _manufacturerCurrentlyUsed)
|
||||
if (player.HasAnimation(_manufacturerAnim) || _manufacturerCurrentlyUsed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -563,10 +563,9 @@ namespace ReallifeGamemode.Server.Managers
|
||||
if (usableItemObj.RemoveWhenUsed)
|
||||
{
|
||||
RemoveUserItem(user, fItem, 1);
|
||||
SetBackpackItems(player);
|
||||
player.TriggerEvent("aproveUse", 1, iItem.Name);
|
||||
}
|
||||
|
||||
SetBackpackItems(player);
|
||||
player.TriggerEvent("aproveUse", 1, iItem.Name);
|
||||
}
|
||||
}
|
||||
else player.TriggerEvent("Error", "Du kannst dieses Item nicht benutzen.");
|
||||
|
||||
19
ReallifeGamemode.Server/Util/CannabisData.cs
Normal file
19
ReallifeGamemode.Server/Util/CannabisData.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace ReallifeGamemode.Server.Util
|
||||
{
|
||||
public class CannabisData
|
||||
{
|
||||
public long Id { get; set; }
|
||||
|
||||
public float X { get; set; }
|
||||
|
||||
public float Y { get; set; }
|
||||
|
||||
public float Z { get; set; }
|
||||
|
||||
public DateTime Time { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user