Skip to content

Commit

Permalink
Small improvements to the code (optional update)
Browse files Browse the repository at this point in the history
  • Loading branch information
RestoreMonarchy committed Dec 20, 2024
1 parent 87a016a commit c7b324c
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 29 deletions.
16 changes: 16 additions & 0 deletions MoreHomes/Helpers/ReflectionHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using SDG.Unturned;
using Steamworks;
using System.Reflection;

namespace RestoreMonarchy.MoreHomes.Helpers
{
internal static class ReflectionHelper
{
private static MethodInfo BarricadeManager_ServerSetBedOwnerInternal = typeof(BarricadeManager).GetMethod("ServerSetBedOwnerInternal", BindingFlags.Static | BindingFlags.NonPublic);

internal static void ServerSetBedOwnerInternal(InteractableBed interactableBed, byte x, byte y, ushort plant, BarricadeRegion region, CSteamID owner)
{
BarricadeManager_ServerSetBedOwnerInternal.Invoke(null, new object[] { interactableBed, x, y, plant, region, owner });
}
}
}
29 changes: 19 additions & 10 deletions MoreHomes/Models/PlayerHome.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using RestoreMonarchy.MoreHomes.Helpers;
using SDG.Unturned;
using System.Reflection;
using UnityEngine;
Expand Down Expand Up @@ -36,39 +37,47 @@ public Vector3 LivePosition
public void Claim(Player player)
{
if (InteractableBed == null)
{
return;
}

if (!BarricadeManager.tryGetRegion(InteractableBed.transform, out byte x, out byte y, out ushort plant, out BarricadeRegion region))
{
return;
}

typeof(BarricadeManager).GetMethod("ServerSetBedOwnerInternal", BindingFlags.Static | BindingFlags.NonPublic)
.Invoke(null, new object[] {
InteractableBed,
x,
y,
plant,
region,
player.channel.owner.playerID.steamID
});
ReflectionHelper.ServerSetBedOwnerInternal(InteractableBed, x, y, plant, region, player.channel.owner.playerID.steamID);
}

public void Unclaim()
{
if (InteractableBed == null)
{
return;
}

BarricadeManager.ServerUnclaimBed(InteractableBed);
}

public void Destroy()
{
if (InteractableBed == null)
{
return;
}

BarricadeDrop drop = BarricadeManager.FindBarricadeByRootTransform(InteractableBed.transform);
if (drop == null)
{
return;

}

if (!BarricadeManager.tryGetRegion(InteractableBed.transform, out byte x, out byte y, out ushort plant, out _))
{
return;
}

Unclaim();
BarricadeManager.destroyBarricade(drop, x, y, plant);
}
}
Expand Down
4 changes: 2 additions & 2 deletions MoreHomes/MoreHomes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<TargetFramework>net48</TargetFramework>
<LangVersion>latest</LangVersion>
<RootNamespace>RestoreMonarchy.MoreHomes</RootNamespace>
<Version>1.10.2</Version>
<Version>1.10.3</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Lib.Harmony" Version="2.2.2" />
<PackageReference Include="RestoreMonarchy.RocketRedist" Version="3.24.6" ExcludeAssets="runtime" />
<PackageReference Include="RestoreMonarchy.RocketRedist" Version="3.24.7.1" ExcludeAssets="runtime" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions MoreHomes/MoreHomesPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected override void Load()
Instance = this;
MessageColor = UnturnedChat.GetColorFromName(Configuration.Instance.MessageColor, Color.green);

PlayerCooldowns = new Dictionary<string, DateTime>();
PlayerCooldowns = [];

HarmonyInstance = new Harmony(HarmonyInstanceId);
HarmonyInstance.PatchAll(Assembly);
Expand All @@ -56,7 +56,7 @@ protected override void Load()

protected override void Unload()
{
HarmonyInstance?.UnpatchAll(HarmonyInstanceId);
HarmonyInstance.UnpatchAll(HarmonyInstanceId);
HarmonyInstance = null;

Destroy(DataService);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
using HarmonyLib;
using RestoreMonarchy.MoreHomes.Helpers;
using Rocket.Unturned.Chat;
using Rocket.Unturned.Player;
using SDG.Unturned;
using Steamworks;
using UnityEngine;

namespace RestoreMonarchy.MoreHomes.Patches
{
[HarmonyPatch(typeof(BarricadeManager))]
class BarricadeManager_destroyBarricade_Patch
class BarricadeManagerPatches
{
[HarmonyPatch("salvageBarricade")]
[HarmonyPatch(nameof(BarricadeManager.salvageBarricade))]
[HarmonyPrefix]
static void salvageBarricade_Prefix(Transform transform)
static void salvageBarricadePrefix(Transform transform)
{
InteractableBed interactableBed = transform.GetComponent<InteractableBed>();
if (interactableBed != null)
Expand All @@ -28,7 +26,7 @@ static void salvageBarricade_Prefix(Transform transform)

[HarmonyPatch("destroyBarricade", typeof(BarricadeDrop), typeof(byte), typeof(byte), typeof(ushort))]
[HarmonyPrefix]
static void destroyBarricade_Prefix(BarricadeDrop barricade, byte x, byte y, ushort plant)
static void destroyBarricadePrefix(BarricadeDrop barricade, byte x, byte y, ushort plant)
{
InteractableBed interactableBed = barricade.interactable as InteractableBed;
if (interactableBed != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
using HarmonyLib;
using SDG.Unturned;
using Steamworks;
using System;
using RestoreMonarchy.MoreHomes.Helpers;
using RestoreMonarchy.MoreHomes.Models;
using Rocket.Unturned.Chat;
using Rocket.Unturned.Player;
using SDG.Unturned;
using Steamworks;

namespace RestoreMonarchy.MoreHomes.Patches
{
[HarmonyPatch(typeof(InteractableBed), "ReceiveClaimRequest")]
class InteractableBed_ReceiveClaimRequest_Patch
[HarmonyPatch(typeof(InteractableBed))]
class InteractableBedPatches
{
[HarmonyPatch(nameof(InteractableBed.ReceiveClaimRequest))]
[HarmonyPrefix]
static bool ReceiveClaimRequest_Prefix(InteractableBed __instance, in ServerInvocationContext context)
static bool ReceiveClaimRequestPrefix(InteractableBed __instance, in ServerInvocationContext context)
{
if (__instance == null)
{
return false;
}

byte x;
byte y;
ushort plant;
Expand All @@ -41,12 +45,15 @@ static bool ReceiveClaimRequest_Prefix(InteractableBed __instance, in ServerInvo

CSteamID steamID = player.channel.owner.playerID.steamID;

if (__instance != null && __instance.isClaimable && __instance.checkClaim(player.channel.owner.playerID.steamID))
if (__instance.isClaimable && __instance.checkClaim(steamID))
{
if (__instance.isClaimed)
{
PlayerHome home = HomesHelper.GetPlayerHome(steamID, __instance);
HomesHelper.RemoveHome(steamID, home);
if (home != null)
{
HomesHelper.RemoveHome(steamID, home);
}
home.Unclaim();
}
else
Expand Down

0 comments on commit c7b324c

Please sign in to comment.