111 lines
3.8 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;
using QSB.ItemSync.WorldObjects.Sockets;
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;
namespace QSB.ItemSync.Patches
{
[HarmonyPatch]
2021-02-24 10:45:25 +00:00
internal class ItemPatches : QSBPatch
2021-02-23 14:42:18 +00:00
{
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
[HarmonyPrefix]
[HarmonyPatch(typeof(ItemTool), nameof(ItemTool.MoveItemToCarrySocket))]
2021-02-25 22:45:32 +00:00
public static bool ItemTool_MoveItemToCarrySocket(OWItem item)
{
var qsbObj = item.GetWorldObject<IQSBOWItem>();
2021-10-30 16:14:38 +01:00
QSBPlayerManager.LocalPlayer.HeldItem = qsbObj;
2021-12-25 22:35:32 -08:00
qsbObj.SendMessage(new MoveToCarryMessage());
2021-02-25 22:45:32 +00:00
return true;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(ItemTool), nameof(ItemTool.SocketItem))]
public static bool ItemTool_SocketItem(ItemTool __instance, OWItemSocket socket)
2021-02-23 14:42:18 +00:00
{
var qsbObj = __instance._heldItem.GetWorldObject<IQSBOWItem>();
2022-01-25 21:38:38 -08:00
var socketId = socket.GetWorldObject<QSBItemSocket>().ObjectId;
2021-12-11 21:36:41 -08:00
var itemId = qsbObj.ObjectId;
2021-10-30 16:14:38 +01:00
QSBPlayerManager.LocalPlayer.HeldItem = null;
2021-12-25 22:35:32 -08:00
new SocketItemMessage(SocketMessageType.Socket, socketId, itemId).Send();
2021-02-23 14:42:18 +00:00
return true;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(ItemTool), nameof(ItemTool.StartUnsocketItem))]
2021-02-25 22:45:32 +00:00
public static bool ItemTool_StartUnsocketItem(OWItemSocket socket)
2021-02-23 14:42:18 +00:00
{
var item = socket.GetSocketedItem().GetWorldObject<IQSBOWItem>();
2021-10-30 16:14:38 +01:00
QSBPlayerManager.LocalPlayer.HeldItem = item;
2022-01-25 21:38:38 -08:00
var socketId = socket.GetWorldObject<QSBItemSocket>().ObjectId;
2021-12-25 22:35:32 -08:00
new SocketItemMessage(SocketMessageType.StartUnsocket, socketId).Send();
return true;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(ItemTool), nameof(ItemTool.CompleteUnsocketItem))]
public static bool ItemTool_CompleteUnsocketItem(ItemTool __instance)
{
var itemId = __instance._heldItem.GetWorldObject<IQSBOWItem>().ObjectId;
2021-12-25 22:35:32 -08:00
new SocketItemMessage(SocketMessageType.CompleteUnsocket, itemId: itemId).Send();
2021-02-25 22:45:32 +00:00
return true;
2021-02-23 14:42:18 +00:00
}
[HarmonyPrefix]
[HarmonyPatch(typeof(ItemTool), nameof(ItemTool.DropItem))]
public static bool ItemTool_DropItem(ItemTool __instance, RaycastHit hit, OWRigidbody targetRigidbody, IItemDropTarget customDropTarget)
2021-02-23 14:42:18 +00:00
{
Locator.GetPlayerAudioController().PlayDropItem(__instance._heldItem.GetItemType());
2021-02-23 14:42:18 +00:00
var hitGameObject = hit.collider.gameObject;
var gameObject2 = hitGameObject;
var sectorGroup = gameObject2.GetComponent<ISectorGroup>();
Sector sector = null;
while (sectorGroup == null && gameObject2.transform.parent != null)
{
gameObject2 = gameObject2.transform.parent.gameObject;
sectorGroup = gameObject2.GetComponent<ISectorGroup>();
}
2021-06-18 22:38:32 +01:00
2021-02-23 14:42:18 +00:00
if (sectorGroup != null)
{
sector = sectorGroup.GetSector();
2021-12-25 22:35:32 -08:00
if (sector == null && sectorGroup is SectorCullGroup sectorCullGroup)
2021-10-15 21:08:17 +01:00
{
2021-12-25 22:35:32 -08:00
var controllingProxy = sectorCullGroup.GetControllingProxy();
2021-10-12 16:56:07 +01:00
if (controllingProxy != null)
{
sector = controllingProxy.GetSector();
}
}
2021-02-23 14:42:18 +00:00
}
2021-06-18 22:38:32 +01:00
2021-10-12 16:56:07 +01:00
var parent = (customDropTarget == null)
? targetRigidbody.transform
: customDropTarget.GetItemDropTargetTransform(hit.collider.gameObject);
var qsbItem = __instance._heldItem.GetWorldObject<IQSBOWItem>();
__instance._heldItem.DropItem(hit.point, hit.normal, parent, sector, customDropTarget);
__instance._heldItem = null;
2021-10-30 16:14:38 +01:00
QSBPlayerManager.LocalPlayer.HeldItem = null;
2021-02-23 14:42:18 +00:00
Locator.GetToolModeSwapper().UnequipTool();
2021-02-25 13:52:49 +00:00
var parentSector = parent.GetComponentInChildren<Sector>();
if (parentSector != null)
{
var localPos = parentSector.transform.InverseTransformPoint(hit.point);
2021-12-25 22:35:32 -08:00
qsbItem.SendMessage(new DropItemMessage(localPos, hit.normal, parentSector));
2021-02-25 13:52:49 +00:00
return false;
}
2021-06-18 22:38:32 +01:00
2021-03-12 21:46:02 +00:00
DebugLog.ToConsole($"Error - No sector found for rigidbody {targetRigidbody.name}!.", MessageType.Error);
2021-02-23 14:42:18 +00:00
return false;
}
}
}