Patch_KylockeStand_Awake.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using GadgetCore.API;
  2. using GadgetCore.Util;
  3. using HarmonyLib;
  4. using System.Collections;
  5. using System.Threading;
  6. using System.Windows.Threading;
  7. using UnityEngine;
  8. namespace MoreLights.Patches
  9. {
  10. [HarmonyPatch(typeof(KylockeStand))]
  11. [HarmonyPatch("Awake")]
  12. [HarmonyGadget("MoreLights")]
  13. public static class Patch_KylockeStand_Awake
  14. {
  15. [HarmonyPostfix]
  16. public static void Prefix(KylockeStand __instance)
  17. {
  18. __instance.StartCoroutine(StartUpdateLoop(__instance));
  19. }
  20. public static IEnumerator LightColorUpdateLoop(KylockeStand instance)
  21. {
  22. while (!Network.isServer && !Network.isClient)
  23. {
  24. yield return new WaitForSeconds(0f);
  25. }
  26. if (Network.isServer)
  27. {
  28. while (true)
  29. {
  30. yield return new WaitForSeconds(1f);
  31. if (instance.gameObject.activeSelf && Network.isServer)
  32. {
  33. instance.itemID = getItemIdFromIndex(colorIndex);
  34. instance.GetComponent<NetworkView>().RPC("Set", RPCMode.All, new object[]
  35. {
  36. new int[]{ instance.itemID, instance.cost }
  37. });
  38. }
  39. yield return new WaitForSeconds(9f);
  40. colorIndex = (colorIndex + 1) % 6;
  41. }
  42. }
  43. }
  44. private static IEnumerator StartUpdateLoop(KylockeStand instance)
  45. {
  46. if (instance.itemID == 2402)
  47. {
  48. instance.itemID = Core.itemWhiteLampId;
  49. foreach (var gadget in GadgetCore.API.Gadgets.ListAllEnabledGadgets())
  50. if (gadget.Info?.ModName == "ItemFrames")
  51. {
  52. instance.transform.localPosition += new Vector3(4, 0, 0);
  53. break;
  54. }
  55. }
  56. else if (instance.itemID == 2401)
  57. {
  58. colorIndex = Random.Range(0, 6);
  59. instance.StartCoroutine(LightColorUpdateLoop(instance));
  60. CoroutineManager newComponent = instance.gameObject.AddComponent<CoroutineManager>();
  61. newComponent.SetAction(new System.Action(() => { newComponent.StartCoroutine(LightColorUpdateLoop(instance)); }));
  62. }
  63. yield break;
  64. }
  65. private static int colorIndex = 0;
  66. private static int getItemIdFromIndex(int i)
  67. {
  68. switch (i)
  69. {
  70. case 0:
  71. return Core.itemYellowLampId;
  72. case 1:
  73. return Core.itemOrangeLampId;
  74. case 2:
  75. return 2402;
  76. case 3:
  77. return Core.itemPurpleLampId;
  78. case 4:
  79. return 2401;
  80. case 5:
  81. return Core.itemGreenLampId;
  82. }
  83. return 2401;
  84. }
  85. }
  86. }