Skip to content

Commit

Permalink
fixed salvage and change the homes directory
Browse files Browse the repository at this point in the history
  • Loading branch information
RestoreMonarchy committed Apr 5, 2024
1 parent 7766655 commit dc91294
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 9 deletions.
2 changes: 1 addition & 1 deletion MoreHomes/Commands/DestroyHomeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void Execute(IRocketPlayer caller, string[] command)
UnturnedPlayer player = (UnturnedPlayer)caller;
string homeName = command.ElementAtOrDefault(0);

if (homeName == null)
if (string.IsNullOrEmpty(homeName))
{
UnturnedChat.Say(caller, pluginInstance.Translate("DestroyHomeFormat"), pluginInstance.MessageColor);
return;
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.8.1</Version>
<Version>1.9.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion MoreHomes/MoreHomesPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Rocket.Core;
using Rocket.Core.Plugins;
using Rocket.Unturned.Chat;
using SDG.Unturned;
using System;
using System.Collections.Generic;
using UnityEngine;
Expand Down
20 changes: 18 additions & 2 deletions MoreHomes/Patches/BarricadeManager_destroyBarricade_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,29 @@
using Rocket.Unturned.Chat;
using SDG.Unturned;
using Steamworks;
using UnityEngine;

namespace RestoreMonarchy.MoreHomes.Patches
{
[HarmonyPatch(typeof(BarricadeManager), "destroyBarricade",
typeof(BarricadeDrop), typeof(byte), typeof(byte), typeof(ushort))]
[HarmonyPatch(typeof(BarricadeManager))]
class BarricadeManager_destroyBarricade_Patch
{
[HarmonyPatch("salvageBarricade")]
[HarmonyPrefix]
static void salvageBarricade_Prefix(Transform transform)
{
InteractableBed interactableBed = transform.GetComponent<InteractableBed>();
if (interactableBed != null)
{
var home = HomesHelper.GetPlayerHome(interactableBed.owner, interactableBed);
if (home != null)
{
HomesHelper.RemoveHome(interactableBed.owner, home);
}
}
}

[HarmonyPatch("destroyBarricade", typeof(BarricadeDrop), typeof(byte), typeof(byte), typeof(ushort))]
[HarmonyPrefix]
static void destroyBarricade_Prefix(BarricadeDrop barricade, byte x, byte y, ushort plant)
{
Expand Down
52 changes: 48 additions & 4 deletions MoreHomes/Services/DataService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using RestoreMonarchy.MoreHomes.Models;
using RestoreMonarchy.MoreHomes.Storage;
using SDG.Unturned;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using Logger = Rocket.Core.Logging.Logger;

Expand All @@ -16,30 +18,72 @@ public class DataService : MonoBehaviour

void Awake()
{
PlayersDataStorage = new DataStorage<List<PlayerData>>(pluginInstance.Directory, "MoreHomesData.json");
string unturnedDirectory = UnturnedPaths.RootDirectory.FullName;
string mapLevelPath = Path.Combine(ServerSavedata.directory, Provider.serverID, "Level", Provider.map);

string directory = unturnedDirectory + mapLevelPath;

if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}

string filePath = Path.Combine(directory, "MoreHomesData.json");
if (!File.Exists(filePath))
{
string oldFilePath = Path.Combine(pluginInstance.Directory, "MoreHomesData.json");
if (File.Exists(oldFilePath))
{
File.Move(oldFilePath, filePath);
string migrationFilePath = Path.Combine(pluginInstance.Directory, "Migration.txt");

string[] lines =
[
"Old data file has been moved to the map level location!",
$"Old file path: {oldFilePath}",
$"New file path: {filePath}",
$"Timestamp: {DateTime.Now}"
];

using StreamWriter writer = File.CreateText(migrationFilePath);
foreach (string line in lines)
{
writer.WriteLine(line);
Logger.Log("[Migration] " + line, ConsoleColor.Yellow);
}
}
}

PlayersDataStorage = new DataStorage<List<PlayerData>>(directory, "MoreHomesData.json");
SaveManager.onPostSave += SaveData;
}

void Start()
{
if (Level.isLoaded)
{
ReloadData();
}
else
Level.onLevelLoaded += (i) => ReloadData();
{
Level.onLevelLoaded += ReloadData;
}
}

void OnDestroy()
{
Level.onLevelLoaded -= (i) => ReloadData();
Level.onLevelLoaded -= ReloadData;
SaveManager.onPostSave -= SaveData;
SaveData();
}

public void ReloadData()
public void ReloadData(int i = 0)
{
PlayersData = PlayersDataStorage.Read();
if (PlayersData == null)
{
PlayersData = new List<PlayerData>();
}

var interactableBeds = new List<InteractableBed>();

Expand Down
Binary file modified lib/Assembly-CSharp.dll
Binary file not shown.
Binary file modified lib/Newtonsoft.Json.dll
Binary file not shown.
Binary file modified lib/Rocket.API.dll
Binary file not shown.
Binary file modified lib/Rocket.Core.dll
Binary file not shown.
Binary file modified lib/Rocket.Unturned.dll
Binary file not shown.
Binary file modified lib/SDG.NetTransport.dll
Binary file not shown.
Binary file modified lib/UnityEngine.CoreModule.dll
Binary file not shown.
Binary file modified lib/UnityEngine.PhysicsModule.dll
Binary file not shown.
Binary file modified lib/UnityEngine.dll
Binary file not shown.
Binary file modified lib/com.rlabrecque.steamworks.net.dll
Binary file not shown.

0 comments on commit dc91294

Please sign in to comment.