ScrapYard.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using GadgetCore.API;
  2. using ScrapYard.API;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. namespace ScrapYard
  9. {
  10. [Gadget("ScrapYard", RequiredOnClients: true, GadgetVersionSpecificity: VersionSpecificity.NONBREAKING)]
  11. public class ScrapYard : Gadget<ScrapYard>
  12. {
  13. public const string MOD_VERSION = "1.6.1"; // Set this to the version of your mod.
  14. public const string CONFIG_VERSION = "1.0"; // Increment this whenever you change your mod's config file.
  15. protected override void LoadConfig()
  16. {
  17. Config.Load();
  18. string fileVersion = Config.ReadString("ConfigVersion", CONFIG_VERSION, comments: "The Config Version (not to be confused with mod version)");
  19. if (fileVersion != CONFIG_VERSION)
  20. {
  21. Config.Reset();
  22. Config.WriteString("ConfigVersion", CONFIG_VERSION, comments: "The Config Version (not to be confused with mod version)");
  23. }
  24. Config.Save();
  25. }
  26. public override string GetModDescription()
  27. {
  28. return "A mod that adds a scryp yard planet. Lite Version.";
  29. }
  30. protected override void Initialize()
  31. {
  32. Logger.Log("Scrap Yard Lite v" + Info.Mod.Version);
  33. Core.logger = Logger;
  34. Texture2D textureScrapTrophy = GadgetCoreAPI.LoadTexture2D("iScrapTrophy.png");
  35. ItemInfo item = new ItemInfo(ItemType.GENERIC, "Scrap Trophy", "A rusty trophy.\nUsed for purchasing\nspecial items.", textureScrapTrophy);
  36. int itemID = ItemRegistry.Singleton.Register(item, "Scrap Trophy");
  37. if (itemID == -1)
  38. throw new System.Exception("Could not register item " + "Scrap Trophy");
  39. { // Shop Stand
  40. var asset = GadgetCoreAPI.LoadAssetBundle("scrapyardstand");
  41. scrapYardStand = UnityEngine.Object.Instantiate((GameObject)asset.LoadAsset("assets/resources/scrapyardstand.prefab"));
  42. scrapYardStand.SetActive(false);
  43. scrapYardStand.AddComponent<ScrapYardShopStandScript>();
  44. scrapYardStand.name = "scrapyardstand";
  45. scrapYardStand.transform.GetChild(0).GetComponent<Renderer>().material = new Material(Shader.Find("Unlit/Transparent"))
  46. {
  47. mainTexture = GadgetCoreAPI.LoadTexture2D("scrapYardStand.png")
  48. };
  49. scrapYardStand.transform.GetChild(2).GetComponent<Renderer>().material = new Material(Shader.Find("Unlit/Transparent"))
  50. {
  51. mainTexture = GadgetCoreAPI.LoadTexture2D("scrapYardStandInfo.png")
  52. };
  53. GadgetCoreAPI.AddCustomResource("prop/ScrapYard/stand", scrapYardStand);
  54. ItemInfo tileItem = new ItemInfo(ItemType.GENERIC, "scrapyardstand", "Degug item.", null as Texture2D).Register();
  55. TileInfo tile = new TileInfo(TileType.INTERACTIVE, null as Texture2D, new GameObject("scrapyardstand"), tileItem);
  56. int tileID = TileRegistry.Singleton.Register(tile, "scrapyardstand");
  57. if (tileID == -1)
  58. throw new System.Exception("Could not register Tile " + "scrapyardstand");
  59. tile.OnInteract += OnInteractStand;
  60. }
  61. ShopPlatform.Reset();
  62. // Setting up default shop platforms
  63. new ShopPlatform("Default Objects", ShopPlatformSpacingType.Near).Register("DefaultObjects");
  64. new ShopPlatform("Default Blocks", ShopPlatformSpacingType.Near).Register("DefaultBlocks");
  65. new ShopPlatform("Default Walls", ShopPlatformSpacingType.Near).Register("DefaultWalls");
  66. SceneManager.sceneLoaded += OnSceneLoaded;
  67. }
  68. private static GameObject scrapYardStand;
  69. internal static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
  70. {
  71. if (scene.buildIndex == 1)
  72. {
  73. InstanceTracker.GameScript.StartCoroutine(CreateStands());
  74. try
  75. {
  76. var storageMenu = GameObject.Find("Ship").transform.Find("SHIPPLACES").transform.Find("NPCs").transform.Find("npcFlora").localPosition = new Vector3(-134.58f, 11.72f, -0.19f);
  77. }
  78. catch (System.Exception e) { Core.logger.LogError(e.Message); }
  79. }
  80. }
  81. private static IEnumerator CreateStands()
  82. {
  83. while (!Network.isServer && !Network.isClient)
  84. {
  85. yield return new WaitForSeconds(0.25f);
  86. }
  87. yield return new WaitForSeconds(0.75f);
  88. if (Network.isServer)
  89. {
  90. try
  91. {
  92. {
  93. var stand = (GameObject)Network.Instantiate((GameObject)Resources.Load("prop/ScrapYard/stand"), new Vector3(-250f + 4 * -5.5f, -5.99f - 2 + 0.5f, 0.19f), Quaternion.identity, 0);
  94. stand.GetComponent<ScrapYardShopStandScript>().GetComponent<ScrapYardShopStandScript>().StartCallSet(ItemRegistry.GetItemIDByRegistryName("ScrapYard:Scrap Trophy"), 9999, 1, 57);
  95. }
  96. List<ShopPlatformEntry> blocks = new List<ShopPlatformEntry>();
  97. List<ShopPlatformEntry> objects = new List<ShopPlatformEntry>();
  98. foreach (var p in ShopPlatform.GetShopPlatforms())
  99. if (p == ShopPlatform.DefaultBlocks || p == ShopPlatform.DefaultWalls)
  100. foreach (var e in p.Entries)
  101. blocks.Add(e);
  102. else
  103. foreach (var e in p.Entries)
  104. objects.Add(e);
  105. for (int i = 0; i < System.Math.Min(objects.Count, 3); i++)
  106. {
  107. var entry = objects[i];
  108. var stand = (GameObject)Network.Instantiate((GameObject)Resources.Load("prop/ScrapYard/stand"), new Vector3(-250f + 4 * i, -5.99f - 2 + 0.5f, 0.19f), Quaternion.identity, 0);
  109. stand.GetComponent<ScrapYardShopStandScript>().StartCallSet(entry.ItemID, entry.Price, entry.Quantity, entry.CurrencyItemID);
  110. }
  111. for (int i = 0; i < System.Math.Min(blocks.Count, 5); i++)
  112. {
  113. var entry = blocks[i];
  114. var stand = (GameObject)Network.Instantiate((GameObject)Resources.Load("prop/ScrapYard/stand"), new Vector3(-250f + 4 * 7 + 4 * i, -5.99f + 6 + 0.5f, 0.19f), Quaternion.identity, 0);
  115. stand.GetComponent<ScrapYardShopStandScript>().StartCallSet(entry.ItemID, entry.Price, entry.Quantity, entry.CurrencyItemID);
  116. }
  117. if (objects.Count > 3 || blocks.Count > 5)
  118. Core.logger.LogWarning("There are more stands than can be added to your ship. U should use the non Lite version of Scrap Yard");
  119. }
  120. catch (System.Exception e)
  121. {
  122. Core.logger.LogError(e);
  123. }
  124. }
  125. yield break;
  126. }
  127. public static readonly FieldInfo canInteractField = typeof(PlayerScript).GetField("canInteract", BindingFlags.NonPublic | BindingFlags.Instance);
  128. public static readonly FieldInfo interactingField = typeof(PlayerScript).GetField("interacting", BindingFlags.NonPublic | BindingFlags.Instance);
  129. private IEnumerator OnInteractStand()
  130. {
  131. try
  132. {
  133. PlayerScript.curInteractObj.SendMessage("Request");
  134. canInteractField.SetValue(InstanceTracker.PlayerScript, true);
  135. interactingField.SetValue(InstanceTracker.PlayerScript, false);
  136. InstanceTracker.PlayerScript.w.SetActive(true);
  137. }
  138. catch (System.Exception e)
  139. {
  140. Core.logger.Log(e);
  141. }
  142. yield break;
  143. }
  144. }
  145. }