110 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
2021-02-23 14:42:18 +00:00
{
[HarmonyPatch]
internal class ItemPatches : QSBPatch
2021-02-23 14:42:18 +00:00
{
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
2021-02-23 14:42:18 +00:00
[HarmonyPrefix]
[HarmonyPatch(typeof(ItemTool), nameof(ItemTool.MoveItemToCarrySocket))]
public static bool ItemTool_MoveItemToCarrySocket(OWItem item)
{
var qsbObj = item.GetWorldObject<IQSBItem>();
QSBPlayerManager.LocalPlayer.HeldItem = qsbObj;
qsbObj.SendMessage(new MoveToCarryMessage());
return true;
}
2021-02-25 22:45:32 +00:00
[HarmonyPrefix]
[HarmonyPatch(typeof(ItemTool), nameof(ItemTool.SocketItem))]
public static bool ItemTool_SocketItem(ItemTool __instance, OWItemSocket socket)
{
var qsbObj = __instance._heldItem.GetWorldObject<IQSBItem>();
var socketId = socket.GetWorldObject<QSBItemSocket>().ObjectId;
var itemId = qsbObj.ObjectId;
QSBPlayerManager.LocalPlayer.HeldItem = null;
new SocketItemMessage(SocketMessageType.Socket, socketId, itemId).Send();
return true;
}
2021-02-23 14:42:18 +00:00
[HarmonyPrefix]
[HarmonyPatch(typeof(ItemTool), nameof(ItemTool.StartUnsocketItem))]
public static bool ItemTool_StartUnsocketItem(OWItemSocket socket)
{
var item = socket.GetSocketedItem().GetWorldObject<IQSBItem>();
QSBPlayerManager.LocalPlayer.HeldItem = item;
var socketId = socket.GetWorldObject<QSBItemSocket>().ObjectId;
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<IQSBItem>().ObjectId;
new SocketItemMessage(SocketMessageType.CompleteUnsocket, itemId: itemId).Send();
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());
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>();
}
if (sectorGroup != null)
2021-02-23 14:42:18 +00:00
{
sector = sectorGroup.GetSector();
if (sector == null && sectorGroup is SectorCullGroup sectorCullGroup)
2021-10-15 21:08:17 +01:00
{
var controllingProxy = sectorCullGroup.GetControllingProxy();
if (controllingProxy != null)
{
sector = controllingProxy.GetSector();
}
2021-10-12 16:56:07 +01:00
}
2021-02-23 14:42:18 +00:00
}
2021-06-18 22:38:32 +01:00
var parent = (customDropTarget == null)
? targetRigidbody.transform
: customDropTarget.GetItemDropTargetTransform(hit.collider.gameObject);
var IQSBItem = __instance._heldItem.GetWorldObject<IQSBItem>();
__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)
{
var localPos = parentSector.transform.InverseTransformPoint(hit.point);
IQSBItem.SendMessage(new DropItemMessage(localPos, hit.normal, parentSector));
return false;
}
DebugLog.ToConsole($"Error - No sector found for rigidbody {targetRigidbody.name}!.", MessageType.Error);
2021-02-23 14:42:18 +00:00
return false;
}
}
}