-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInventoryRandom.cs
84 lines (76 loc) · 2.96 KB
/
InventoryRandom.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
using System.Linq;
using HarmonyLib;
using Newtonsoft.Json.Linq;
namespace DarkwoodCustomizer;
internal class InventoryRandomizePatch
{
[HarmonyPatch(typeof(InventoryRandom), nameof(InventoryRandom.randomize))]
[HarmonyPostfix]
static void PatchRandomizedInventory(InventoryRandom __instance)
{
var customRandomInventories = Plugin.CustomRandomInventories;
if (!customRandomInventories.ContainsKey(__instance.name))
{
customRandomInventories[__instance.name] = new JObject
{
{ "presets", new JObject() }
};
Plugin.SaveRandomInventories = true;
}
for (var i = 0; i < __instance.presets.Count; i++)
{
if (__instance.presets[i] == null)
{
continue;
}
var token = customRandomInventories[__instance.name]["presets"][i.ToString()];
if (token == null || token.Type != JTokenType.Object)
{
customRandomInventories[__instance.name]["presets"][i.ToString()] = new JObject();
foreach (var item in __instance.presets[i].permittedItems)
{
if (item.type == null)
{
Plugin.Log.LogError($"[CustomRandomInventories] Preset {i} in {__instance.name} has an item ({item}) with no type, skipping");
continue;
}
customRandomInventories[__instance.name]["presets"][i.ToString()][item.type.name] = new JObject
{
{ "type", item.type.name },
{ "amountMin", item.amountMin },
{ "amountMax", item.amountMax },
{ "chance", item.chance }
};
}
Plugin.SaveRandomInventories = true;
}
if (!Plugin.RandomInventoriesModification.Value) return;
__instance.presets[i].permittedItems = [];
foreach (var item in (JObject)customRandomInventories[__instance.name]["presets"][i.ToString()])
{
var typeName = item.Value["type"]?.Value<string>();
if (typeName == null)
{
Plugin.Log.LogError($"[CustomRandomInventories] Preset {i} in {__instance.name} has an item with a null type, skipping");
continue;
}
var type = ItemsDatabase.Instance.getItem(typeName, false);
if (type == null)
{
Plugin.Log.LogError($"[CustomRandomInventories] Preset {i} in {__instance.name} has an item with an invalid type, skipping");
continue;
}
__instance.presets[i].permittedItems.Add(new PermittedItem
{
type = type,
amountMin = item.Value["amountMin"]?.Value<int>() ?? 0,
amountMax = item.Value["amountMax"]?.Value<int>() ?? 0,
chance = item.Value["chance"]?.Value<float>() ?? 0
});
}
__instance.excludeFromDifficultyRandomizer = true;
__instance.presets[i].allowedItems = [];
__instance.presets[i].allowedItems.AddRange(__instance.presets[i].permittedItems.Select(item => item.type));
}
}
}