Skip to content

Commit

Permalink
Refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
SnellarDev committed Nov 22, 2021
1 parent 8f615f6 commit 51281aa
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 73 deletions.
43 changes: 22 additions & 21 deletions AttachToPlayer/Attach.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ public override void OnApplicationStart()
MelonCoroutines.Start(AttachUI.WhereDaUI());
MelonCoroutines.Start(CheckNullPlayer());
}

private static void InitPatches()
{
MethodInfo[] array = (from m in typeof(NetworkManager).GetMethods()
where m.Name.Contains("Method_Public_Void_Player_") && !m.Name.Contains("PDM")
select m).ToArray();

try { Instance.Patch(AccessTools.Method(typeof(NetworkManager), array[1].Name, null, null), GetPatch("OnPlayerLeft")); } catch (Exception e) { MelonLogger.Error($"Error Patching OnPlayerLeft => {e.Message}"); }
}

Expand All @@ -43,16 +45,15 @@ private static bool OnPlayerLeft(ref VRC.Player __0)
catch { }
return true;
}

public static IEnumerator CheckNullPlayer()
{
for (; ; )
{
try
{
if (PlayerExtensions.IsInWorld() && AttachUI.GetSelectedPlayer() != null)
{
cachedselected = AttachUI.GetSelectedPlayer();
}
}
catch { }
yield return new WaitForSeconds(0.1f);
Expand All @@ -68,39 +69,39 @@ public override void OnUpdate()
AbreastAttachment = false;
CircularAttachment = false;
}
if (Target != null && PlayerExtensions.LocalVRCPlayer != null && !AbreastAttachment && !CircularAttachment)
{
PlayerExtensions.LocalPlayer.transform.position = new Vector3(Target.GetVRCPlayer().GetAnimator().GetBoneTransform(boneparts[BodyX]).position.x, Target.GetVRCPlayer().GetAnimator().GetBoneTransform(boneparts[BodyY]).position.y, Target.GetVRCPlayer().GetAnimator().GetBoneTransform(boneparts[BodyZ]).position.z);
}
//if (Target != null && PlayerExtensions.LocalVRCPlayer != null && AbreastAttachment)
//{
// PlayerExtensions.LocalPlayer.transform.position = new Vector3(Target.GetVRCPlayer().GetAnimator().GetBoneTransform(boneparts[BodyX]).position.x + PosX, PosY, Target.GetVRCPlayer().GetAnimator().GetBoneTransform(boneparts[BodyZ]).position.z + PosZ);
//}
//if (Target != null && PlayerExtensions.LocalVRCPlayer != null && !AbreastAttachment && CircularAttachment)
//{
// outset += Time.deltaTime * circularspeed;
// PlayerExtensions.LocalPlayer.transform.position = new Vector3(Target.transform.position.x + PosX * (float)System.Math.Cos((double)outset), Target.transform.position.y, Target.transform.position.z + PosZ * (float)System.Math.Sin((double)outset));
//}

if (Target != null && PlayerExtensions.LocalVRCPlayer != null && !AbreastAttachment)
PlayerExtensions.LocalPlayer.transform.position = new Vector3(Target.GetVRCPlayer().GetAnimator().GetBoneTransform(boneparts[(int)BodyVector3.x]).position.x, Target.GetVRCPlayer().GetAnimator().GetBoneTransform(boneparts[(int)BodyVector3.y]).position.y, Target.GetVRCPlayer().GetAnimator().GetBoneTransform(boneparts[(int)BodyVector3.z]).position.z);

if (Target != null && PlayerExtensions.LocalVRCPlayer != null && AbreastAttachment)
PlayerExtensions.LocalPlayer.transform.position = new Vector3(Target.GetVRCPlayer().GetAnimator().GetBoneTransform(boneparts[(int)BodyVector3.x]).position.x + PosVector3.x, PosVector3.y, Target.GetVRCPlayer().GetAnimator().GetBoneTransform(boneparts[(int)BodyVector3.z]).position.z + PosVector3.z);
}

private static HarmonyMethod GetPatch(string name)
{
return new HarmonyMethod(typeof(Attach).GetMethod(name, BindingFlags.Static | BindingFlags.NonPublic));
}

public static VRC.Player cachedselected;

public static HarmonyLib.Harmony Instance = new HarmonyLib.Harmony("Patches");

public static VRC.Player Target;

public static bool AbreastAttachment;

public static bool CircularAttachment;

public static float outset = 0f;

public static float circularspeed = 2f;
public static float PosX;
public static float PosY;
public static float PosZ;
public static int BodyX;
public static int BodyY;
public static int BodyZ;

public static Vector3 PosVector3;

public static Vector3 BodyVector3;

private readonly KeyCode[] keycodes = new KeyCode[] { KeyCode.Space, KeyCode.JoystickButton15 };

public HumanBodyBones[] boneparts = new HumanBodyBones[] { HumanBodyBones.Head, HumanBodyBones.Chest, HumanBodyBones.LeftHand, HumanBodyBones.RightHand, HumanBodyBones.Hips, HumanBodyBones.LeftLowerLeg, HumanBodyBones.RightLowerLeg, HumanBodyBones.RightLowerArm, HumanBodyBones.LeftLowerArm, HumanBodyBones.LeftToes, HumanBodyBones.RightToes, HumanBodyBones.Spine };
}
}
102 changes: 50 additions & 52 deletions AttachToPlayer/AttachUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,6 @@ namespace AttachToPlayer
{
public static class AttachUI
{
private static UiManager uiManager;
public static void BoneAttachValues(int BodyX, int BodyY, int BodyZ, bool isabreast = false, float PosX = 0, float PosY = 0, float PosZ = 0)
{
Player playercheck = Attach.cachedselected;
if (playercheck.GetVRCPlayer().GetAnimator().isHuman)
{
if (Attach.Target != null)
{
if(!isabreast)
{
Attach.Target = null;
Attach.AbreastAttachment = false;
Attach.CircularAttachment = false;
}
if(isabreast)
{
Attach.Target = null;
Attach.CircularAttachment = false;
}
}
Attach.BodyX = BodyX;
Attach.BodyY = BodyY;
Attach.BodyZ = BodyZ;
Attach.PosX = PosX;
Attach.PosY = PosY;
Attach.PosZ = PosZ;
Attach.Target = Attach.cachedselected;
PlayerExtensions.FreezeLocalPlayer(false);
}
else
MelonLogger.Msg("the player is not able to be attached to");
}
public static void AttachGUI()
{
uiManager = new UiManager("Target");
Expand All @@ -57,70 +25,97 @@ public static void AttachGUI()

page.AddButton("Attach To Player", "Attaches your player to the other player's head", delegate
{
BoneAttachValues(0, 0, 0);
BoneAttachValues(new Vector3(0, 0, 0));
});

page.AddMenuPage("Bone Attach", "Select the player's bones to attach to");
ReMenuPage bonepage = page.GetMenuPage("Bone Attach");

bonepage.AddButton("Chest", "Attaches your player to the other player's chest", delegate
{
BoneAttachValues(1, 1, 1);
BoneAttachValues(new Vector3(1, 1, 1));
});

bonepage.AddButton("Right Hand", "Attach to the right hand of the player", delegate
{
BoneAttachValues(2, 2, 2);
BoneAttachValues(new Vector3(2, 2, 2));
});

bonepage.AddButton("Left Hand", "Attach to the left hand of the player", delegate
{
BoneAttachValues(3, 3, 3);
BoneAttachValues(new Vector3(3, 3, 3));
});

bonepage.AddButton("Hips", "Attach to the hips of the player", delegate
{
BoneAttachValues(4, 4, 4);
BoneAttachValues(new Vector3(4, 4, 4));
});

bonepage.AddButton("Left Leg", "Attach to the left leg of the player", delegate
{
BoneAttachValues(5, 5, 5);
BoneAttachValues(new Vector3(5, 5, 5));
});

bonepage.AddButton("Right Leg", "Attach to the right leg of the player", delegate
{
BoneAttachValues(6, 6, 6);
BoneAttachValues(new Vector3(6, 6, 6));
});

bonepage.AddButton("Right Arm", "Attach to the right arm of the player", delegate
{
BoneAttachValues(7, 7, 7);
BoneAttachValues(new Vector3(7, 7, 7));
});

bonepage.AddButton("Left Arm", "Attach to the left arm of the player", delegate
{
BoneAttachValues(8, 8, 8);
BoneAttachValues(new Vector3(8, 8, 8));
});

bonepage.AddButton("Left Side", "Attach to the left side of the player", delegate
{
BoneAttachValues(9, 9, 9, true, 0.5f, 0f, 0f);
BoneAttachValues(new Vector3(9, 9, 9), new Vector3(0.5f, 0f, 0f), true);
});

bonepage.AddButton("Right Side", "Attach to the right side of the player", delegate
{
BoneAttachValues(10, 10, 10, true, -0.5f, 0f, 0f);
BoneAttachValues(new Vector3(10, 10, 10), new Vector3(-0.5f, 0f, 0f), true);
});
}

//bonepage.AddButton("Below", "Attach below the player", delegate
//{
// BoneAttachValues(11, 11, 11, true, 0f, -2f, 0f);
//});

//bonepage.AddButton("Above", "Attach above the player", delegate
//{
// BoneAttachValues(11, 11, 11, true, 0f, 1f, 0f);
//});
public static void BoneAttachValues(Vector3 bonevec, Vector3 posvec = default(Vector3), bool isabreast = false)
{
try
{
Player playercheck = Attach.cachedselected;
if (playercheck.GetVRCPlayer().GetAnimator().isHuman)
{
if (Attach.Target != null)
{
if (!isabreast)
{
Attach.Target = null;
Attach.AbreastAttachment = false;
Attach.CircularAttachment = false;
}
if (isabreast)
{
Attach.Target = null;
Attach.CircularAttachment = false;
}
}
Attach.BodyVector3.x = bonevec.x;
Attach.BodyVector3.y = bonevec.y;
Attach.BodyVector3.z = bonevec.z;
Attach.PosVector3.x = posvec.x;
Attach.PosVector3.y = posvec.y;
Attach.PosVector3.z = posvec.z;
Attach.Target = Attach.cachedselected;
PlayerExtensions.FreezeLocalPlayer(false);
}
else
MelonLogger.Error("The player is not able to be attached to");
}
catch { }
}

public static Player GetSelectedPlayer()
Expand All @@ -135,5 +130,8 @@ public static IEnumerator WhereDaUI()
AttachGUI();
yield break;
}

private static UiManager uiManager;

}
}

0 comments on commit 51281aa

Please sign in to comment.