Skip to content

Commit

Permalink
Add support for homes placed on vehicle
Browse files Browse the repository at this point in the history
  • Loading branch information
RestoreMonarchy committed Oct 29, 2024
1 parent f2e6ec5 commit f5f8492
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 27 deletions.
28 changes: 20 additions & 8 deletions MoreHomes/Helpers/HomesHelper.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion MoreHomes/MoreHomes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net48</TargetFramework>
<LangVersion>latest</LangVersion>
<RootNamespace>RestoreMonarchy.MoreHomes</RootNamespace>
<Version>1.10.0</Version>
<Version>1.10.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
67 changes: 49 additions & 18 deletions MoreHomes/Services/DataService.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -83,37 +86,65 @@ public void ReloadData(int i = 0)
if (PlayersData == null)
{
PlayersData = new List<PlayerData>();
}
}

var interactableBeds = new List<InteractableBed>();
List<InteractableBed> 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);
}
}
}

Expand Down

0 comments on commit f5f8492

Please sign in to comment.