quantum-space-buddies/QSB/ItemSync/Patches/ItemToolPatches.cs

113 lines
3.8 KiB
C#
Raw Normal View History

using HarmonyLib;
2021-12-26 06:35:32 +00:00
using QSB.ItemSync.Messages;
using QSB.ItemSync.WorldObjects.Items;
2021-12-26 06:35:32 +00:00
using QSB.Messaging;
2021-02-25 13:52:49 +00:00
using QSB.Patches;
2021-10-30 15:14:38 +00:00
using QSB.Player;
2021-02-25 13:52:49 +00:00
using QSB.WorldSync;
2021-02-23 14:42:18 +00:00
using UnityEngine;
2022-03-03 03:46:33 +00:00
namespace QSB.ItemSync.Patches;
2022-03-23 23:05:46 +00:00
[HarmonyPatch(typeof(ItemTool))]
internal class ItemToolPatches : QSBPatch
2021-02-23 14:42:18 +00:00
{
2022-03-03 03:46:33 +00:00
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
[HarmonyPrefix]
2022-03-23 23:05:46 +00:00
[HarmonyPatch(nameof(ItemTool.MoveItemToCarrySocket))]
public static void MoveItemToCarrySocket(OWItem item)
2021-02-23 14:42:18 +00:00
{
2022-03-23 22:26:42 +00:00
var qsbItem = item.GetWorldObject<IQSBItem>();
QSBPlayerManager.LocalPlayer.HeldItem = qsbItem;
qsbItem.ItemState.HasBeenInteractedWith = true;
2022-07-25 12:09:51 +00:00
qsbItem.ItemState.State = ItemStateType.Held;
qsbItem.ItemState.HoldingPlayer = QSBPlayerManager.LocalPlayer;
qsbItem.SendMessage(new MoveToCarryMessage(QSBPlayerManager.LocalPlayer.PlayerId));
2022-03-03 03:46:33 +00:00
}
2021-02-23 14:42:18 +00:00
[HarmonyPrefix]
2022-03-23 23:05:46 +00:00
[HarmonyPatch(nameof(ItemTool.SocketItem))]
public static void SocketItem(ItemTool __instance, OWItemSocket socket)
2022-03-03 03:46:33 +00:00
{
2022-03-23 22:26:42 +00:00
var item = __instance._heldItem;
2022-03-03 03:46:33 +00:00
QSBPlayerManager.LocalPlayer.HeldItem = null;
2022-07-25 12:09:51 +00:00
var qsbItem = item.GetWorldObject<IQSBItem>();
qsbItem.ItemState.State = ItemStateType.Socketed;
qsbItem.ItemState.Socket = socket;
2022-03-23 22:26:42 +00:00
new SocketItemMessage(SocketMessageType.Socket, socket, item).Send();
2022-03-03 03:46:33 +00:00
}
2021-02-25 22:45:32 +00:00
[HarmonyPrefix]
2022-03-23 23:05:46 +00:00
[HarmonyPatch(nameof(ItemTool.StartUnsocketItem))]
public static void StartUnsocketItem(OWItemSocket socket)
2022-03-03 03:46:33 +00:00
{
2022-03-23 22:26:42 +00:00
var item = socket.GetSocketedItem();
var qsbItem = item.GetWorldObject<IQSBItem>();
qsbItem.ItemState.HasBeenInteractedWith = true;
2022-03-23 22:26:42 +00:00
QSBPlayerManager.LocalPlayer.HeldItem = qsbItem;
new SocketItemMessage(SocketMessageType.StartUnsocket, socket, item).Send();
2022-03-03 03:46:33 +00:00
}
2021-02-23 14:42:18 +00:00
[HarmonyPrefix]
2022-03-23 23:05:46 +00:00
[HarmonyPatch(nameof(ItemTool.CompleteUnsocketItem))]
public static void CompleteUnsocketItem(ItemTool __instance)
2022-03-03 03:46:33 +00:00
{
2022-03-23 22:26:42 +00:00
var item = __instance._heldItem;
new SocketItemMessage(SocketMessageType.CompleteUnsocket, null, item).Send();
2022-03-03 03:46:33 +00:00
}
2022-03-03 03:46:33 +00:00
[HarmonyPrefix]
2022-03-23 23:05:46 +00:00
[HarmonyPatch(nameof(ItemTool.DropItem))]
public static bool DropItem(ItemTool __instance, RaycastHit hit, OWRigidbody targetRigidbody, IItemDropTarget customDropTarget)
2022-03-03 03:46:33 +00:00
{
Locator.GetPlayerAudioController().PlayDropItem(__instance._heldItem.GetItemType());
2022-04-01 20:30:36 +00:00
var gameObject = hit.collider.gameObject;
var component = gameObject.GetComponent<ISectorGroup>();
2022-03-03 03:46:33 +00:00
Sector sector = null;
2022-04-01 18:08:03 +00:00
while (component == null && gameObject.transform.parent != null)
{
2022-04-01 18:08:03 +00:00
gameObject = gameObject.transform.parent.gameObject;
component = gameObject.GetComponent<ISectorGroup>();
2021-02-23 14:42:18 +00:00
}
2022-04-01 18:08:03 +00:00
if (component != null)
2021-02-23 14:42:18 +00:00
{
2022-04-01 18:08:03 +00:00
sector = component.GetSector();
2022-04-01 20:30:36 +00:00
if (sector == null && component is SectorCullGroup sectorCullGroup)
2021-02-23 14:42:18 +00:00
{
2022-04-01 20:30:36 +00:00
var controllingProxy = sectorCullGroup.GetControllingProxy();
2022-03-03 03:46:33 +00:00
if (controllingProxy != null)
2021-10-15 20:08:17 +00:00
{
2022-03-03 03:46:33 +00:00
sector = controllingProxy.GetSector();
2021-10-12 15:56:07 +00:00
}
2021-02-23 14:42:18 +00:00
}
2022-03-03 03:46:33 +00:00
}
2021-06-18 21:38:32 +00:00
2022-04-01 20:30:36 +00:00
var parent = customDropTarget == null
2022-03-03 03:46:33 +00:00
? targetRigidbody.transform
: customDropTarget.GetItemDropTargetTransform(hit.collider.gameObject);
var qsbItem = __instance._heldItem.GetWorldObject<IQSBItem>();
2022-03-03 03:46:33 +00:00
__instance._heldItem.DropItem(hit.point, hit.normal, parent, sector, customDropTarget);
2022-04-01 20:30:36 +00:00
customDropTarget?.AddDroppedItem(hit.collider.gameObject, __instance._heldItem);
2022-03-03 03:46:33 +00:00
__instance._heldItem = null;
QSBPlayerManager.LocalPlayer.HeldItem = null;
2022-04-01 18:08:03 +00:00
2022-03-03 03:46:33 +00:00
Locator.GetToolModeSwapper().UnequipTool();
2022-04-01 18:08:03 +00:00
2022-04-01 20:42:20 +00:00
qsbItem.SendMessage(new DropItemMessage(hit.point, hit.normal, parent, sector, customDropTarget, targetRigidbody));
2022-03-03 03:46:33 +00:00
2022-07-25 12:09:51 +00:00
qsbItem.ItemState.State = ItemStateType.OnGround;
qsbItem.ItemState.Parent = parent;
qsbItem.ItemState.LocalPosition = parent.InverseTransformPoint(hit.point);
qsbItem.ItemState.LocalNormal = parent.InverseTransformDirection(hit.normal);
qsbItem.ItemState.Sector = sector;
qsbItem.ItemState.CustomDropTarget = customDropTarget;
qsbItem.ItemState.Rigidbody = targetRigidbody;
2022-03-03 03:46:33 +00:00
return false;
2021-02-23 14:42:18 +00:00
}
2022-03-23 22:26:42 +00:00
}