-
Notifications
You must be signed in to change notification settings - Fork 101
/
MagicStorageMod.cs
200 lines (152 loc) · 5.66 KB
/
MagicStorageMod.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using MagicStorage.Common.Systems;
using MagicStorage.Common.Systems.RecurrentRecipes;
using MagicStorage.CrossMod;
using MagicStorage.CrossMod.Control;
using MagicStorage.Items;
using MagicStorage.NPCs;
using MagicStorage.Stations;
using SerousCommonLib.API;
using SerousCommonLib.API.Helpers;
using System;
using System.IO;
using Terraria;
using Terraria.GameContent.ItemDropRules;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
namespace MagicStorage {
public class MagicStorageMod : Mod {
public static MagicStorageMod Instance => ModContent.GetInstance<MagicStorageMod>();
internal static bool UsingPrivateBeta { get; private set; } //Make sure to add the "NETPLAY" define when setting this to true for beta builds! -- absoluteAquarian
// Integration with ModHelpers
public static string GithubUserName => "blushiemagic";
public static string GithubProjectName => "MagicStorage";
public static readonly Condition HasCampfire = new(Language.GetText("Mods.MagicStorage.CookedMarshmallowCondition"), () => CraftingGUI.Campfire);
public UIOptionConfigurationManager optionsConfig;
public MagicStorageMod() {
PreJITFilter = new CheckModBuildVersionBeforeJIT();
CheckModBuildVersionBeforeJIT.Mod = this;
}
internal const string build144Version = "2023.8";
public override void Load()
{
UsingPrivateBeta = DisplayName.Contains("BETA");
LocalizationHelper.ForceLoadModHJsonLocalization(this);
InterfaceHelper.Initialize();
//Sorting options
SortingOptionLoader.Load();
//Filtering options
FilteringOptionLoader.Load();
}
public override void Unload()
{
StorageGUI.Unload();
CraftingGUI.Unload();
EnvironmentGUI.Unload();
DecraftingGUI.Unload();
EnvironmentModuleLoader.Unload();
Obsolete_Unload();
SortingOptionLoader.Unload();
FilteringOptionLoader.Unload();
optionsConfig = null;
CheckModBuildVersionBeforeJIT.Mod = null;
CheckModBuildVersionBeforeJIT.versionChecked = false;
}
[Obsolete]
private static void Obsolete_Unload() {
ItemCombining.NextID = 0;
}
public override void PostSetupContent() {
if (!Main.dedServ) {
optionsConfig = new();
optionsConfig.Initialize();
}
SortingOptionLoader.InitializeOrder();
FilteringOptionLoader.InitializeOrder();
}
public override void HandlePacket(BinaryReader reader, int whoAmI) {
NetHelper.HandlePacket(reader, whoAmI);
}
public override object Call(params object[] args) {
if (args.Length < 1)
throw new ArgumentException("Call requires at least one argument");
string function = "";
void TryParseAs<T>(int arg, out T value) {
if (args.Length < arg + 1)
throw new ArgumentException($"Call \"{function}\" requires at least {arg} arguments");
if (args[arg] is T v)
value = v;
else
throw new ArgumentException($"Call requires argument #{arg + 1} to be of type {typeof(T).GetSimplifiedGenericTypeName()}");
}
void ThrowWithMessage(string message, int argument = -1) {
if (argument < 0)
throw new ArgumentException($"Call \"{function}\" could not be performed.\nReason: {message}");
else
throw new ArgumentException($"Call \"{function}\" had an invalid value for argument #{argument}\nReason: {message}");
}
TryParseAs(0, out function);
switch (function) {
case "Prevent Shadow Diamond Drop":
if (args.Length != 2)
ThrowWithMessage("Expected 2 arguments");
TryParseAs(1, out int npcID);
if (npcID < 0)
ThrowWithMessage("NPC ID must be positive", 1);
else if (npcID < NPCID.Count)
ThrowWithMessage("NPC ID must refer to a modded NPC ID", 1);
StorageWorld.disallowDropModded.Add(npcID);
break;
case "Set Shadow Diamond Drop Rule":
if (args.Length != 3)
ThrowWithMessage("Expected 3 arguments");
TryParseAs(1, out npcID);
TryParseAs(2, out IItemDropRule rule);
if (npcID < 0)
ThrowWithMessage("NPC ID must be positive", 1);
else if (npcID < NPCID.Count)
ThrowWithMessage("NPC ID must refer to a modded NPC ID", 1);
StorageWorld.moddedDiamondDropRulesByType.Add(npcID, rule);
break;
case "Get Shadow Diamond Drop Rule":
if (args.Length < 2 || args.Length > 3)
ThrowWithMessage("Expected 2 or 3 arguments");
TryParseAs(1, out int dropNormal);
int dropExpert = -1;
if (args.Length == 3)
TryParseAs(2, out dropExpert);
if (dropNormal < 1)
ThrowWithMessage("Normal mode drop stack must be positive", 1);
return ShadowDiamondDrop.DropDiamond(dropNormal, dropExpert);
case "Get Campfire Condition":
if (args.Length != 1)
ThrowWithMessage("Expected 1 argument");
return HasCampfire;
case "Simulating Crafts":
if (args.Length != 1)
ThrowWithMessage("Expected 1 argument");
return CraftingGUI.SimulatingCrafts;
case "Add Extra Craft Drop":
if (args.Length != 3)
ThrowWithMessage("Expected 3 arguments");
TryParseAs(2, out IItemDropRule extraDropRule);
if (args[1] is Recipe recipe)
ExtraCraftItemsSystem.RegisterDrop(recipe, extraDropRule);
else if (args[1] is Func<Recipe, bool> condition)
ExtraCraftItemsSystem.RegisterDrop(condition, extraDropRule);
else
ThrowWithMessage("First argument must be a \"Recipe\" or \"Func<Recipe, bool>\" object", 1);
return true;
case "Block Recursion":
if (args.Length != 2)
ThrowWithMessage("Expected 2 arguments");
TryParseAs(1, out Recipe recipeToBlock);
MagicCache.BlockRecipeRecursionFor(recipeToBlock);
return true;
default:
throw new ArgumentException("Call does not support the function \"" + function + "\"");
}
return null;
}
}
}