104 lines
3.2 KiB
C#
Raw Normal View History

using HarmonyLib;
using OWML.Common;
2021-12-25 22:35:32 -08:00
using QSB.ItemSync.Messages;
using QSB.ItemSync.WorldObjects.Items;
2021-12-25 22:35:32 -08:00
using QSB.Messaging;
2021-02-25 13:52:49 +00:00
using QSB.Patches;
2021-10-30 16:14:38 +01:00
using QSB.Player;
2021-02-23 14:42:18 +00:00
using QSB.Utility;
2021-02-25 13:52:49 +00:00
using QSB.WorldSync;
2021-02-23 14:42:18 +00:00
using UnityEngine;
2022-03-02 19:46:33 -08:00
namespace QSB.ItemSync.Patches;
2022-03-23 16:05:46 -07:00
[HarmonyPatch(typeof(ItemTool))]
internal class ItemToolPatches : QSBPatch
2021-02-23 14:42:18 +00:00
{
2022-03-02 19:46:33 -08:00
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
[HarmonyPrefix]
2022-03-23 16:05:46 -07:00
[HarmonyPatch(nameof(ItemTool.MoveItemToCarrySocket))]
public static void MoveItemToCarrySocket(OWItem item)
2021-02-23 14:42:18 +00:00
{
2022-03-23 15:26:42 -07:00
var qsbItem = item.GetWorldObject<IQSBItem>();
QSBPlayerManager.LocalPlayer.HeldItem = qsbItem;
qsbItem.SendMessage(new MoveToCarryMessage());
2022-03-02 19:46:33 -08:00
}
2021-02-23 14:42:18 +00:00
[HarmonyPrefix]
2022-03-23 16:05:46 -07:00
[HarmonyPatch(nameof(ItemTool.SocketItem))]
public static void SocketItem(ItemTool __instance, OWItemSocket socket)
2022-03-02 19:46:33 -08:00
{
2022-03-23 15:26:42 -07:00
var item = __instance._heldItem;
2022-03-02 19:46:33 -08:00
QSBPlayerManager.LocalPlayer.HeldItem = null;
2022-03-23 15:26:42 -07:00
new SocketItemMessage(SocketMessageType.Socket, socket, item).Send();
2022-03-02 19:46:33 -08:00
}
2021-02-25 22:45:32 +00:00
[HarmonyPrefix]
2022-03-23 16:05:46 -07:00
[HarmonyPatch(nameof(ItemTool.StartUnsocketItem))]
public static void StartUnsocketItem(OWItemSocket socket)
2022-03-02 19:46:33 -08:00
{
2022-03-23 15:26:42 -07:00
var item = socket.GetSocketedItem();
var qsbItem = item.GetWorldObject<IQSBItem>();
QSBPlayerManager.LocalPlayer.HeldItem = qsbItem;
new SocketItemMessage(SocketMessageType.StartUnsocket, socket, item).Send();
2022-03-02 19:46:33 -08:00
}
2021-02-23 14:42:18 +00:00
[HarmonyPrefix]
2022-03-23 16:05:46 -07:00
[HarmonyPatch(nameof(ItemTool.CompleteUnsocketItem))]
public static void CompleteUnsocketItem(ItemTool __instance)
2022-03-02 19:46:33 -08:00
{
2022-03-23 15:26:42 -07:00
var item = __instance._heldItem;
new SocketItemMessage(SocketMessageType.CompleteUnsocket, null, item).Send();
2022-03-02 19:46:33 -08:00
}
2022-03-02 19:46:33 -08:00
[HarmonyPrefix]
2022-03-23 16:05:46 -07:00
[HarmonyPatch(nameof(ItemTool.DropItem))]
public static bool DropItem(ItemTool __instance, RaycastHit hit, OWRigidbody targetRigidbody, IItemDropTarget customDropTarget)
2022-03-02 19:46:33 -08:00
{
Locator.GetPlayerAudioController().PlayDropItem(__instance._heldItem.GetItemType());
var hitGameObject = hit.collider.gameObject;
var gameObject2 = hitGameObject;
var sectorGroup = gameObject2.GetComponent<ISectorGroup>();
Sector sector = null;
while (sectorGroup == null && gameObject2.transform.parent != null)
{
2022-03-02 19:46:33 -08:00
gameObject2 = gameObject2.transform.parent.gameObject;
sectorGroup = gameObject2.GetComponent<ISectorGroup>();
2021-02-23 14:42:18 +00:00
}
2022-03-02 19:46:33 -08:00
if (sectorGroup != null)
2021-02-23 14:42:18 +00:00
{
2022-03-02 19:46:33 -08:00
sector = sectorGroup.GetSector();
if (sector == null && sectorGroup is SectorCullGroup sectorCullGroup)
2021-02-23 14:42:18 +00:00
{
2022-03-02 19:46:33 -08:00
var controllingProxy = sectorCullGroup.GetControllingProxy();
if (controllingProxy != null)
2021-10-15 21:08:17 +01:00
{
2022-03-02 19:46:33 -08:00
sector = controllingProxy.GetSector();
2021-10-12 16:56:07 +01:00
}
2021-02-23 14:42:18 +00:00
}
2022-03-02 19:46:33 -08:00
}
2021-06-18 22:38:32 +01:00
2022-03-23 15:26:42 -07:00
var parent = customDropTarget == null
2022-03-02 19:46:33 -08:00
? targetRigidbody.transform
: customDropTarget.GetItemDropTargetTransform(hit.collider.gameObject);
var qsbItem = __instance._heldItem.GetWorldObject<IQSBItem>();
2022-03-02 19:46:33 -08:00
__instance._heldItem.DropItem(hit.point, hit.normal, parent, sector, customDropTarget);
__instance._heldItem = null;
QSBPlayerManager.LocalPlayer.HeldItem = null;
Locator.GetToolModeSwapper().UnequipTool();
var parentSector = parent.GetComponentInChildren<Sector>();
if (parentSector != null)
{
qsbItem.SendMessage(new DropItemMessage(hit.point, hit.normal, parentSector));
}
else
{
DebugLog.ToConsole($"Error - No sector found for rigidbody {targetRigidbody.name}!.", MessageType.Error);
2021-02-23 14:42:18 +00:00
}
2022-03-02 19:46:33 -08:00
return false;
2021-02-23 14:42:18 +00:00
}
2022-03-23 15:26:42 -07:00
}