1- using HarmonyLib ;
1+ using HarmonyLib ;
22using RestoreMonarchy . AnimalManager . Configurations ;
33using RestoreMonarchy . AnimalManager . Helpers ;
44using RestoreMonarchy . AnimalManager . Models ;
99using Rocket . Unturned . Chat ;
1010using SDG . Unturned ;
1111using System ;
12+ using System . Collections . Generic ;
13+ using System . Reflection ;
1214using UnityEngine ;
1315using AnimalSpawn = RestoreMonarchy . AnimalManager . Models . AnimalSpawn ;
1416using Logger = Rocket . Core . Logging . Logger ;
@@ -18,6 +20,8 @@ namespace RestoreMonarchy.AnimalManager
1820 public class AnimalManagerPlugin : RocketPlugin < AnimalManagerConfiguration >
1921 {
2022 public static AnimalManagerPlugin Instance { get ; private set ; }
23+
24+ public static event Action < List < uint > > OnAnimalLootDropped ;
2125 public Color MessageColor { get ; private set ; }
2226
2327 public AnimalSpawnsXmlConfiguration AnimalSpawnsConfiguration { get ; private set ; }
@@ -39,7 +43,8 @@ protected override void Load()
3943 if ( Level . isLoaded )
4044 {
4145 OnPostLevelLoaded ( 0 ) ;
42- } else
46+ }
47+ else
4348 {
4449 Level . onPostLevelLoaded += OnPostLevelLoaded ;
4550 }
@@ -67,7 +72,7 @@ protected override void Unload()
6772 { "NoAnimalSpawnsFound" , "No animal spawns found in radius of {0}." } ,
6873 { "AnimalSpawnRemoved" , "Found and removed {0} animal spawns in {1}m radius." } ,
6974 { "AnimalsNone" , "There isn't any alive animals on the map." } ,
70- { "AnimalsNoneSpecific" , "There isn't any alive {0} animals on the map." } ,
75+ { "AnimalsNoneSpecific" , "There isn't any alive {0} animals on the map." } ,
7176 { "AnimalTeleported" , "You have been teleported to {0} animal." }
7277 } ;
7378
@@ -80,13 +85,13 @@ internal void LogDebug(string message)
8085 }
8186
8287 private void OnPostLevelLoaded ( int level )
83- {
88+ {
8489 AnimalSpawnsConfiguration . Load ( ) ;
8590 if ( Configuration . Instance . CustomSpawns . Enabled )
8691 {
8792 AnimalHelper . ResetAnimalManager ( ) ;
8893 AnimalSpawnService = gameObject . AddComponent < AnimalSpawnService > ( ) ;
89- }
94+ }
9095 }
9196
9297 public float GetRadius ( AnimalSpawn animalSpawn )
@@ -109,6 +114,12 @@ public float GetRespawnTime(AnimalSpawn animalSpawn)
109114 return Configuration . Instance . CustomSpawns . DefaultRespawnTime ;
110115 }
111116
117+ private static readonly FieldInfo InstanceCountField = typeof ( ItemManager ) . GetField ( "instanceCount" , BindingFlags . Static | BindingFlags . NonPublic ) ;
118+
119+ /// <summary>
120+ /// Handles custom loot dropping for animals.
121+ /// </summary>
122+ /// <returns>true if custom loot was dropped, false otherwise.</returns>
112123 public bool DropLoot ( Animal animal )
113124 {
114125 AnimalConfig animalConfig = Configuration . Instance . GetAnimalById ( animal . asset . id ) ;
@@ -122,6 +133,8 @@ public bool DropLoot(Animal animal)
122133 return false ;
123134 }
124135
136+ uint startInstanceCount = ( uint ) InstanceCountField . GetValue ( null ) ;
137+
125138 Vector3 position = animal . transform . position ;
126139 foreach ( LootItem lootItem in animalConfig . LootItems )
127140 {
@@ -140,7 +153,22 @@ public bool DropLoot(Animal animal)
140153 }
141154 }
142155
156+ if ( OnAnimalLootDropped != null )
157+ {
158+ uint endInstanceCount = ( uint ) InstanceCountField . GetValue ( null ) ;
159+ List < uint > instanceIds = new ( ) ;
160+ for ( uint i = startInstanceCount + 1 ; i <= endInstanceCount ; i ++ )
161+ {
162+ instanceIds . Add ( i ) ;
163+ }
164+
165+ if ( instanceIds . Count > 0 )
166+ {
167+ OnAnimalLootDropped . Invoke ( instanceIds ) ;
168+ }
169+ }
170+
143171 return true ;
144172 }
145173 }
146- }
174+ }
0 commit comments