Skip to content

Commit bb1310e

Browse files
Fixed custom loot dropping alongside original loot and added Zombie Loot Manager compatibility
1 parent 293f8e6 commit bb1310e

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

AnimalManager/AnimalManager.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
<TargetFramework>net48</TargetFramework>
55
<LangVersion>latest</LangVersion>
66
<RootNamespace>RestoreMonarchy.AnimalManager</RootNamespace>
7-
<Version>1.0.1</Version>
7+
<Version>1.0.2</Version>
88
</PropertyGroup>
99

1010
<ItemGroup>
1111
<PackageReference Include="Lib.Harmony" Version="2.3.3" />
1212
<PackageReference Include="RestoreMonarchy.RocketRedist" Version="3.*" ExcludeAssets="runtime" />
1313
</ItemGroup>
1414

15-
</Project>
15+
</Project>

AnimalManager/AnimalManagerPlugin.cs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using HarmonyLib;
1+
using HarmonyLib;
22
using RestoreMonarchy.AnimalManager.Configurations;
33
using RestoreMonarchy.AnimalManager.Helpers;
44
using RestoreMonarchy.AnimalManager.Models;
@@ -9,6 +9,8 @@
99
using Rocket.Unturned.Chat;
1010
using SDG.Unturned;
1111
using System;
12+
using System.Collections.Generic;
13+
using System.Reflection;
1214
using UnityEngine;
1315
using AnimalSpawn = RestoreMonarchy.AnimalManager.Models.AnimalSpawn;
1416
using 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+
}

AnimalManager/Patches/AnimalManagerPatches.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using HarmonyLib;
1+
using HarmonyLib;
22
using SDG.Unturned;
33

44
namespace RestoreMonarchy.AnimalManager.Patches
@@ -12,7 +12,8 @@ static bool dropLoot_Prefix(Animal animal)
1212
{
1313
AnimalManagerPlugin pluginInstance = AnimalManagerPlugin.Instance;
1414

15-
return pluginInstance.DropLoot(animal);
15+
// If custom loot was dropped, skip the original method
16+
return !pluginInstance.DropLoot(animal);
1617
}
1718

1819
[HarmonyPatch("respawnAnimals")]

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Animal Manager
22
Set up custom spawns points and modify the loot from animals.
33

4+
## Compatibility
5+
This plugin is compatible with [Zombie Loot Manager](https://restoremonarchy.com/plugins/150) - custom animal loot will be properly tracked for despawning.
6+
47
## Features
58
- Modify the loot dropped by animals on death
69
- Set up custom spawn points for animals

0 commit comments

Comments
 (0)