-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.cs
137 lines (114 loc) · 4.82 KB
/
Plugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using MonoMod.RuntimeDetour;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.UIElements;
namespace MonsterDrops
{
[BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)]
public class Plugin : BaseUnityPlugin
{
Harmony _harmony = new Harmony(PluginInfo.PLUGIN_GUID);
public static ManualLogSource mls;
private void Awake()
{
mls = BepInEx.Logging.Logger.CreateLogSource("Monster Drops");
// Plugin startup logic
mls.LogInfo($"Plugin {PluginInfo.PLUGIN_GUID} is loaded!");
_harmony.PatchAll(typeof(Plugin));
var helperObject = new GameObject("CoroutineHelper");
helperObject.AddComponent<CoroutineHelper>();
}
public void DelayedSyncScrapValues(Action action, float delay)
{
StartCoroutine(DelayedActionCoroutine(action, delay));
}
private IEnumerator DelayedActionCoroutine(Action action, float delay)
{
yield return new WaitForSeconds(delay);
action();
}
[HarmonyPostfix]
[HarmonyPatch(typeof(EnemyAI), nameof(EnemyAI.KillEnemy))]
static void DropLoot(EnemyAI __instance)
{
mls.LogInfo($"Enemy {__instance.name} has been killed. Spawning scrap...");
// Find the RoundManager instance in the scene
RoundManager roundManager = GameObject.FindObjectOfType<RoundManager>();
if (roundManager == null)
{
mls.LogWarning("RoundManager instance not found in the scene!");
}
else
{
// Create the CoroutineHelper object if it doesn't exist
if (CoroutineHelper.Instance == null)
{
var helperObject = new GameObject("CoroutineHelper");
helperObject.AddComponent<CoroutineHelper>();
}
// Get the list of spawnable scrap items
List<SpawnableItemWithRarity> spawnableScrapItems = roundManager.currentLevel.spawnableScrap;
if (spawnableScrapItems == null || spawnableScrapItems.Count == 0)
{ // Get the list of spawnable scrap items
mls.LogWarning("No scrap items are configured to spawn.");
}
else
{
// Get a random scrap item from the list of spawnable scrap items
SpawnableItemWithRarity scrapSpawn = spawnableScrapItems[UnityEngine.Random.Range(0, spawnableScrapItems.Count)];
// Instantiate the loot item at the enemy's position
GameObject lootItem = UnityEngine.Object.Instantiate<GameObject>(scrapSpawn.spawnableItem.spawnPrefab, __instance.serverPosition, Quaternion.identity);
// Add any additional logic for initializing the loot item here
GrabbableObject component = lootItem.GetComponent<GrabbableObject>();
component.transform.rotation = Quaternion.Euler(component.itemProperties.restingRotation);
component.fallTime = 0f;
component.scrapValue = scrapSpawn.spawnableItem.maxValue;
NetworkObject component2 = lootItem.GetComponent<NetworkObject>();
component2.Spawn(false);
NetworkObjectReference[] networkArray = new NetworkObjectReference[1];
int[] intArray = new int[1];
networkArray[0] = component2;
intArray[0] = component.scrapValue;
CoroutineHelper.Instance.ExecuteAfterDelay(() =>
{
// The function you want to call after 11 seconds
roundManager.SyncScrapValuesClientRpc(networkArray, intArray);
}, 11f);
}
}
}
}
public class CoroutineHelper : MonoBehaviour
{
public static CoroutineHelper Instance { get; private set; }
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(this.gameObject);
}
else
{
Destroy(this.gameObject);
}
}
public void ExecuteAfterDelay(Action action, float delay)
{
StartCoroutine(DelayedExecution(action, delay));
}
private IEnumerator DelayedExecution(Action action, float delay)
{
yield return new WaitForSeconds(delay);
action.Invoke();
}
}
}