-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecayLoot.cs
142 lines (132 loc) · 5.12 KB
/
DecayLoot.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
138
139
140
141
142
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Oxide.Plugins
{
[Info("DecayLoot", "decay.dev", "0.2.0")]
[Description("managed loot, https://decay.dev/loot")]
public class DecayLoot : RustPlugin
{
private ConfigData configData;
private bool init;
private void OnServerInitialized()
{
ItemManager.Initialize();
PopulateContainers();
}
private void PopulateContainers()
{
var populatedSlots = 0;
NextTick(() =>
{
foreach (var container in BaseNetworkable.serverEntities
.Where(p => p != null && p.GetComponent<BaseEntity>() != null && p is LootContainer)
.Cast<LootContainer>().ToList())
foreach (var prefab in configData.Prefabs)
if (container.PrefabName.Contains(prefab.id))
{
PopulateContainer(container, prefab);
populatedSlots++;
break;
}
Puts($"[DecayLoot]: {populatedSlots} container slots updated.");
init = true;
});
}
private bool PopulateContainer(LootContainer container, ConfigData.Prefab prefab)
{
if (container == null || prefab == null) return false;
int itemsCount;
var rand = new Random();
itemsCount = prefab.item_range[0] == prefab.item_range[1] ? prefab.item_range[1] : rand.Next(prefab.item_range[0], prefab.item_range[1]);
container.inventory.Clear();
ItemManager.DoRemoves();
container.scrapAmount = rand.Next(prefab.scrap_range[0], prefab.scrap_range[1]);
container.inventory.capacity = itemsCount;
var sample = new List<ConfigData.Prefab.Item>(prefab.items);
for (var i = 0; i < (itemsCount > 0 ? itemsCount : 1) - 1; i++)
{
var itemSpec = sample.GetRandom();
sample.Remove(itemSpec);
var itemDef = ItemManager.FindItemDefinition(itemSpec.shortname.Replace(".blueprint", ""));
var itemAmount = rand.Next(itemSpec.range[0], itemSpec.range[1]);
var ranged = new ItemAmountRanged(itemDef, itemAmount > 1 ? itemAmount : 1);
Item item;
if (ranged.itemDef.spawnAsBlueprint)
{
var blueprintBaseDef = ItemManager.FindItemDefinition("blueprintbase");
if (blueprintBaseDef == null)
{
PrintWarning("blueprintbase is null for item: {0}", itemSpec.shortname);
continue;
}
item = ItemManager.Create(blueprintBaseDef);
item.blueprintTarget = ranged.itemDef.itemid;
}
else
{
item = ItemManager.CreateByItemID(ranged.itemid, (int) ranged.GetAmount());
}
if (item == null) continue;
var allowStack = !new[]
{
ItemCategory.Weapon
}.Contains(itemDef.category);
if (!item.MoveToContainer(container.inventory, -1, allowStack))
{
if ((bool) container.inventory.playerOwner)
item.Drop(container.inventory.playerOwner.GetDropPosition(),
container.inventory.playerOwner.GetDropVelocity());
else
item.Remove();
}
}
container.GenerateScrap();
container.SendNetworkUpdate();
return true;
}
private object OnLootSpawn(LootContainer container)
{
if (!init || container == null) return null;
foreach (var prefab in configData.Prefabs)
if (container.PrefabName.Contains(prefab.id))
{
if (PopulateContainer(container, prefab))
{
ItemManager.DoRemoves();
return true;
}
return null;
}
return null;
}
protected override void LoadConfig()
{
base.LoadConfig();
configData = Config.ReadObject<ConfigData>();
Config.WriteObject(configData, true);
}
protected override void LoadDefaultConfig()
{
LoadConfig();
}
private class ConfigData
{
[JsonProperty(PropertyName = "prefabs")]
public Prefab[] Prefabs { get; set; }
public class Prefab
{
public string id;
public int[] item_range;
public List<Item> items;
public int[] scrap_range;
public class Item
{
public int[] range;
public string shortname;
}
}
}
}
}