Files
zzz_REBIRTH__Utils/Harmony/Harmony_EntityItem.cs
2025-06-04 16:44:53 +09:30

108 lines
6.1 KiB
C#

namespace Harmony.EntityItemPatches
{
[HarmonyPatch(typeof(EntityItem))]
[HarmonyPatch("createMesh")]
public class createMeshPatch
{
public static bool Prefix(EntityItem __instance)
{
if (__instance.itemStack.itemValue.type == 0 || __instance.itemClass == null)
{
Log.Error(string.Format("Could not create item with id {0}", (object)__instance.itemStack.itemValue.type));
__instance.SetDead();
}
else
{
if ((UnityEngine.Object)__instance.meshGameObject != (UnityEngine.Object)null && __instance.lastCachedItemStack.itemValue.type != 0)
{
UnityEngine.Object.Destroy((UnityEngine.Object)__instance.meshGameObject);
__instance.meshGameObject = (GameObject)null;
}
__instance.itemTransform = (Transform)null;
float num = 0.0f;
Vector3 zero = Vector3.zero;
if (__instance.itemClass.IsBlock())
{
BlockValue blockValue = __instance.itemStack.itemValue.ToBlockValue();
if ((UnityEngine.Object)__instance.itemTransform == (UnityEngine.Object)null)
__instance.itemTransform = __instance.itemClass.CloneModel(__instance.meshGameObject, __instance.world, blockValue, (Vector3[])null, Vector3.zero, __instance.transform, BlockShape.MeshPurpose.Drop);
Block block = blockValue.Block;
if (block.Properties.Values.ContainsKey("DropScale"))
num = StringParsers.ParseFloat(block.Properties.Values["DropScale"]);
}
else
{
if ((UnityEngine.Object)__instance.itemTransform == (UnityEngine.Object)null)
{
__instance.itemTransform = __instance.itemClass.CloneModel(__instance.world, __instance.itemStack.itemValue, Vector3.zero, __instance.transform, BlockShape.MeshPurpose.Drop);
}
bool isArrow = __instance.itemStack.itemValue.ItemClass.GetItemName().ToLower().Contains("ammoarrow") ||
__instance.itemStack.itemValue.ItemClass.GetItemName().ToLower().Contains("ammocrossbowbolt");
if (isArrow)
{
//Log.Out("EntityItemPatches-createMesh __instance.itemStack.count: " + __instance.itemStack.count);
//Log.Out("EntityItemPatches-createMesh __instance.itemStack.itemValue: " + __instance.itemStack.itemValue.ItemClass.GetItemName());
//Log.Out("EntityItemPatches-createMesh OwnerId: " + __instance.OwnerId);
if (__instance.OwnerId != -1)
{
EntityPlayer player = (EntityPlayer)__instance.world.GetEntity(__instance.OwnerId);
if (player != null)
{
//Log.Out("EntityItemPatches-createMesh player != null");
if (SingletonMonoBehaviour<ConnectionManager>.Instance.IsServer && !SingletonMonoBehaviour<ConnectionManager>.Instance.IsSinglePlayer)
{
SingletonMonoBehaviour<ConnectionManager>.Instance.SendPackage((NetPackage)NetPackageManager.GetPackage<NetPackageAddPlayerItem>().Setup(__instance.itemStack.itemValue.ItemClass.GetItemName(), player.entityId, 1, "item_pickup"), false, player.entityId);
}
else
{
RebirthUtilities.addToPlayerBag(__instance.itemStack.itemValue, player, __instance.itemStack.count, "item_pickup");
}
__instance.SetDead();
return false;
}
}
}
if (__instance.itemClass.Properties.Values.ContainsKey("DropScale"))
num = StringParsers.ParseFloat(__instance.itemClass.Properties.Values["DropScale"]);
}
if ((double)num != 0.0)
__instance.itemTransform.localScale = new Vector3(num, num, num);
__instance.itemTransform.localEulerAngles = __instance.itemClass.GetDroppedCorrectionRotation();
__instance.itemTransform.localPosition = zero;
bool flag = true;
Collider[] componentsInChildren = __instance.itemTransform.GetComponentsInChildren<Collider>();
for (int index = 0; componentsInChildren != null && index < componentsInChildren.Length; ++index)
{
Collider collider = componentsInChildren[index];
Rigidbody component = collider.gameObject.GetComponent<Rigidbody>();
if ((bool)(UnityEngine.Object)component && component.isKinematic || collider is MeshCollider && !((MeshCollider)collider).convex)
{
collider.enabled = false;
}
else
{
collider.gameObject.layer = 13;
collider.enabled = true;
flag = false;
collider.gameObject.AddMissingComponent<RootTransformRefEntity>();
}
}
__instance.transform.GetComponent<Collider>().enabled = flag;
__instance.meshGameObject = __instance.itemTransform.gameObject;
__instance.meshGameObject.SetActive(true);
if (__instance.itemWorldData != null)
__instance.itemClass.OnMeshCreated(__instance.itemWorldData);
__instance.bMeshCreated = true;
__instance.meshRenderers = __instance.itemTransform.GetComponentsInChildren<Renderer>(true);
__instance.VisiblityCheck(0.0f, false);
}
return false;
}
}
}