From f5f84925be095c02bbed2473394717fab0c40e4e Mon Sep 17 00:00:00 2001 From: MCrow Date: Tue, 29 Oct 2024 20:35:40 +0100 Subject: [PATCH] Add support for homes placed on vehicle --- MoreHomes/Helpers/HomesHelper.cs | 28 +++++++++---- MoreHomes/MoreHomes.csproj | 2 +- MoreHomes/Services/DataService.cs | 67 ++++++++++++++++++++++--------- 3 files changed, 70 insertions(+), 27 deletions(-) diff --git a/MoreHomes/Helpers/HomesHelper.cs b/MoreHomes/Helpers/HomesHelper.cs index 2bc90f3..5f4f58b 100644 --- a/MoreHomes/Helpers/HomesHelper.cs +++ b/MoreHomes/Helpers/HomesHelper.cs @@ -1,12 +1,9 @@ using RestoreMonarchy.MoreHomes.Models; -using Rocket.Unturned.Chat; using SDG.Unturned; using Steamworks; using System; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; using UnityEngine; namespace RestoreMonarchy.MoreHomes.Helpers @@ -18,30 +15,45 @@ public class HomesHelper public static PlayerData GetOrCreatePlayer(CSteamID steamID) { - var player = playersData.FirstOrDefault(x => x.PlayerId == steamID.m_SteamID); + PlayerData player = playersData.FirstOrDefault(x => x.PlayerId == steamID.m_SteamID); if (player == null) { player = new PlayerData(steamID.m_SteamID); playersData.Add(player); } + return player; } public static PlayerHome GetPlayerHome(CSteamID steamID, string name = null) { - var player = GetOrCreatePlayer(steamID); + PlayerData player = GetOrCreatePlayer(steamID); + return player.Homes.FirstOrDefault(x => name == null || x.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); } public static PlayerHome GetPlayerHome(CSteamID steamID, InteractableBed interactableBed) { - var player = GetOrCreatePlayer(steamID); + PlayerData player = GetOrCreatePlayer(steamID); + return player.Homes.FirstOrDefault(x => x.InteractableBed == interactableBed); - } + } + + public static PlayerHome GetPlayerHome(CSteamID steamID, Vector3 position) + { + PlayerData player = GetOrCreatePlayer(steamID); + + return player.Homes.FirstOrDefault(x => + Math.Abs(x.Position.X - position.x) <= 1 && + Math.Abs(x.Position.Y - position.y) <= 1 && + Math.Abs(x.Position.Z - position.z) <= 1 + ); + } public static bool RemoveHome(CSteamID steamID, PlayerHome playerHome) { - var player = GetOrCreatePlayer(steamID); + PlayerData player = GetOrCreatePlayer(steamID); + return player.Homes.Remove(playerHome); } diff --git a/MoreHomes/MoreHomes.csproj b/MoreHomes/MoreHomes.csproj index 3087bce..167a305 100644 --- a/MoreHomes/MoreHomes.csproj +++ b/MoreHomes/MoreHomes.csproj @@ -4,7 +4,7 @@ net48 latest RestoreMonarchy.MoreHomes - 1.10.0 + 1.10.1 diff --git a/MoreHomes/Services/DataService.cs b/MoreHomes/Services/DataService.cs index f7d019e..51a2740 100644 --- a/MoreHomes/Services/DataService.cs +++ b/MoreHomes/Services/DataService.cs @@ -1,9 +1,12 @@ -using RestoreMonarchy.MoreHomes.Models; +using RestoreMonarchy.MoreHomes.Helpers; +using RestoreMonarchy.MoreHomes.Models; using RestoreMonarchy.MoreHomes.Storage; using SDG.Unturned; +using Steamworks; using System; using System.Collections.Generic; using System.IO; +using System.Linq; using UnityEngine; using Logger = Rocket.Core.Logging.Logger; @@ -83,37 +86,65 @@ public void ReloadData(int i = 0) if (PlayersData == null) { PlayersData = new List(); - } + } - var interactableBeds = new List(); + List interactableBeds = []; - foreach (var region in BarricadeManager.regions) + foreach (BarricadeRegion region in BarricadeManager.regions) { - foreach (var drop in region.drops) + foreach (BarricadeDrop drop in region.drops) { - if (drop.interactable as InteractableBed != null) + if (drop.interactable is InteractableBed interactableBed) { - interactableBeds.Add(drop.interactable as InteractableBed); + interactableBeds.Add(interactableBed); } } } - foreach (var player in PlayersData) + foreach (VehicleBarricadeRegion region in BarricadeManager.vehicleRegions) { - foreach (var home in player.Homes) + foreach (BarricadeDrop drop in region.drops) + { + if (drop.interactable is InteractableBed interactableBed) + { + interactableBeds.Add(interactableBed); + } + } + } + + foreach (InteractableBed interactableBed in interactableBeds) + { + if (interactableBed.owner == CSteamID.Nil) { - foreach (var interactableBed in interactableBeds) + continue; + } + + PlayerHome home = HomesHelper.GetPlayerHome(interactableBed.owner, interactableBed.transform.position); + if (home != null) + { + home.InteractableBed = interactableBed; + } else + { + PlayerData player = HomesHelper.GetOrCreatePlayer(interactableBed.owner); + home = new PlayerHome(player.GetUniqueHomeName(), interactableBed); + player.Homes.Add(home); + } + } + + foreach (PlayerData player in PlayersData.ToList()) + { + foreach (PlayerHome home in player.Homes.ToList()) + { + if (home.InteractableBed == null) { - if (interactableBed.transform.position.x == home.Position.X - && interactableBed.transform.position.y == home.Position.Y - && interactableBed.transform.position.z == home.Position.Z) - { - home.InteractableBed = interactableBed; - interactableBeds.Remove(interactableBed); - break; - } + player.Homes.Remove(home); } } + + if (player.Homes.Count == 0) + { + PlayersData.Remove(player); + } } }