106 lines
3.3 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;
2022-04-01 19:08:03 +01:00
using System.Linq;
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());
2022-04-01 19:08:03 +01:00
GameObject gameObject = hit.collider.gameObject;
ISectorGroup component = gameObject.GetComponent<ISectorGroup>();
2022-03-02 19:46:33 -08:00
Sector sector = null;
2022-04-01 19:08:03 +01:00
while (component == null && gameObject.transform.parent != null)
{
2022-04-01 19:08:03 +01:00
gameObject = gameObject.transform.parent.gameObject;
component = gameObject.GetComponent<ISectorGroup>();
2021-02-23 14:42:18 +00:00
}
2022-04-01 19:08:03 +01:00
if (component != null)
2021-02-23 14:42:18 +00:00
{
2022-04-01 19:08:03 +01:00
sector = component.GetSector();
if (sector == null && component is SectorCullGroup)
2021-02-23 14:42:18 +00:00
{
2022-04-01 19:08:03 +01:00
SectorProxy controllingProxy = (component as SectorCullGroup).GetControllingProxy();
2022-03-02 19:46:33 -08:00
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-04-01 19:08:03 +01:00
Transform 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);
2022-04-01 19:08:03 +01:00
if (customDropTarget != null)
{
customDropTarget.AddDroppedItem(hit.collider.gameObject, __instance._heldItem);
}
2022-03-02 19:46:33 -08:00
__instance._heldItem = null;
QSBPlayerManager.LocalPlayer.HeldItem = null;
2022-04-01 19:08:03 +01:00
2022-03-02 19:46:33 -08:00
Locator.GetToolModeSwapper().UnequipTool();
2022-04-01 19:08:03 +01:00
var qsbDropItem = QSBWorldSync.GetWorldObjects<MonoBehaviourWorldObject>()
.FirstOrDefault(x => (IItemDropTarget)x.AttachedObject == customDropTarget);
qsbItem.SendMessage(new DropItemMessage(hit.point, hit.normal, parent, sector, qsbDropItem, targetRigidbody));
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
}