-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBetterTaxes.cs
189 lines (165 loc) · 9.24 KB
/
BetterTaxes.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using Terraria;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
namespace BetterTaxes
{
public static class UsefulThings
{
public static string ValueToCoins(int num, string zeroString = "0 copper")
{
if (zeroString == "0 copper") zeroString = "0 " + Language.GetTextValue("LegacyInterface.18");
if (num < 1) return zeroString;
return Main.ValueToCoins(num);
}
public static string ValueToCoinsWithColor(double num, string zeroString = "0 copper")
{
if (zeroString == "0 copper") zeroString = "0 " + Language.GetTextValue("LegacyInterface.18");
if (double.IsPositiveInfinity(num)) return "[c/" + Colors.CoinPlatinum.Hex3() + ":" + Language.GetTextValue("Mods.BetterTaxes.Status.ALotOfMoney") + "]";
if (double.IsNegativeInfinity(num)) return "[c/" + Colors.CoinCopper.Hex3() + ":" + Language.GetTextValue("Mods.BetterTaxes.Status.NotALotOfMoney") + "]";
if (double.IsNaN(num)) return "[c/" + Colors.CoinGold.Hex3() + ":" + Language.GetTextValue("Mods.BetterTaxes.Status.Unknown") + "]";
return ValueToCoinsWithColor((int)num, zeroString);
}
public static string ValueToCoinsWithColor(int num, string zeroString = "0 copper")
{
if (zeroString == "0 copper") zeroString = "0 " + Language.GetTextValue("LegacyInterface.18");
string data = ValueToCoins(num, zeroString);
data = Regex.Replace(data, @"(\d+ " + Language.GetTextValue("LegacyInterface.15") + ")", "[c/" + Colors.CoinPlatinum.Hex3() + ":$1]");
data = Regex.Replace(data, @"(\d+ " + Language.GetTextValue("LegacyInterface.16") + ")", "[c/" + Colors.CoinGold.Hex3() + ":$1]");
data = Regex.Replace(data, @"(\d+ " + Language.GetTextValue("LegacyInterface.17") + ")", "[c/" + Colors.CoinSilver.Hex3() + ":$1]");
data = Regex.Replace(data, @"(\d+ " + Language.GetTextValue("LegacyInterface.18") + ")", "[c/" + Colors.CoinCopper.Hex3() + ":$1]");
return data;
}
public static string SecondsToHMS(int num, string zeroString = "0 seconds")
{
if (zeroString == "0 seconds") zeroString = "0 " + Language.GetTextValue("Mods.BetterTaxes.Config.Seconds");
if (num < 1) return zeroString;
string res = "";
int hours = num / 3600;
if (hours == 1) res += hours + " " + Language.GetTextValue("Mods.BetterTaxes.Config.Hour") + " ";
if (hours > 1) res += hours + " " + Language.GetTextValue("Mods.BetterTaxes.Config.Hours") + " ";
num %= 3600;
int minutes = num / 60;
if (minutes == 1) res += minutes + " " + Language.GetTextValue("Mods.BetterTaxes.Config.Minute") + " ";
if (minutes > 1) res += minutes + " " + Language.GetTextValue("Mods.BetterTaxes.Config.Minutes") + " ";
num %= 60;
if (num == 1) res += num + " " + Language.GetTextValue("Mods.BetterTaxes.Config.Second") + " ";
if (num > 1) res += num + " " + Language.GetTextValue("Mods.BetterTaxes.Config.Seconds") + " ";
return res.TrimEnd();
}
public static string SecondsToHMSCasual(int num, string zeroString = "1 tick")
{
if (zeroString == "1 tick") zeroString = "1 " + Language.GetTextValue("Mods.BetterTaxes.Config.Tick");
if (num < 1) return zeroString;
return SecondsToHMS(num, zeroString);
}
public static double GetTaxCollectorHappinessMultiplier()
{
if (!NPC.taxCollector) return 0;
int tcId = NPC.FindFirstNPC(NPCID.TaxCollector);
if (tcId < 0) return 0;
ShoppingSettings tcHappiness = Main.ShopHelper.GetShoppingSettings(new Player(), Main.npc[tcId]);
return 1 + (tcHappiness.PriceAdjustment - 1) * (1 - TaxWorld.serverConfig.HappinessBoost);
}
public static double GetTaxCollectorHappinessMultiplierInverse()
{
if (!NPC.taxCollector) return 0;
int tcId = NPC.FindFirstNPC(NPCID.TaxCollector);
if (tcId < 0) return 0;
ShoppingSettings tcHappiness = Main.ShopHelper.GetShoppingSettings(new Player(), Main.npc[tcId]);
return 1 / (1 + (tcHappiness.PriceAdjustment - 1) * (TaxWorld.serverConfig.HappinessBoost));
}
public static int CalculateNPCCount()
{
int npcCount = 0;
for (int i = 0; i < 200; i++)
{
if (Main.npc[i].active && !Main.npc[i].homeless && !NPCID.Sets.IsTownPet[Main.npc[i].type] && NPC.TypeToDefaultHeadIndex(Main.npc[i].type) > 0) npcCount++;
}
return npcCount;
}
}
public class BetterTaxes : Mod
{
public static string GithubUserName => "atenfyr";
public static string GithubProjectName => "bettertaxes";
internal static BetterTaxes Instance;
internal Mod herosMod;
internal static bool calamityLoaded;
internal static bool herosLoaded;
public BetterTaxes()
{
ContentAutoloadingEnabled = true;
GoreAutoloadingEnabled = true;
MusicAutoloadingEnabled = true;
BackgroundAutoloadingEnabled = true;
}
public override object Call(params object[] args)
{
if (!(args[0] is string)) throw new ModSupportException("First parameter must be a method name");
string given_method = (string)args[0];
object[] newArgs = args.Skip(1).ToArray();
MethodInfo func = typeof(BetterTaxesAPI).GetMethod(given_method, BindingFlags.Public | BindingFlags.Static, null, newArgs.Select(obj => obj.GetType()).ToArray(), null);
var attr = (ObsoleteAttribute[])func.GetCustomAttributes(typeof(ObsoleteAttribute), false);
if (attr.Length > 0) Logger.Warn("Deprecated method warning: " + attr[0].Message);
if (func != null) return func.Invoke(typeof(BetterTaxesAPI), newArgs);
throw new ModSupportException("Could not find method \"" + given_method + "\" with the arguments specified");
}
public override void Load()
{
Instance = this;
new ModHandler();
calamityLoaded = ModLoader.TryGetMod("CalamityMod", out Mod CalamityMod);
herosLoaded = ModLoader.TryGetMod("HEROsMod", out Mod HEROsMod);
}
public override void Unload()
{
Instance = null;
TaxWorld.serverConfig = null;
ModHandler.calamityMod = null;
herosMod = null;
ModHandler.parser = null;
ModHandler.delegates = new Dictionary<string, Dictionary<string, Func<bool>>>();
ModHandler.customStatements = new Dictionary<string, int>();
}
public override void PostSetupContent()
{
// Thorium support
if (ModLoader.TryGetMod("ThoriumMod", out Mod thoriumMod))
{
ModSystem thoriumWorld = thoriumMod.Find<ModSystem>("ThoriumWorld");
if (thoriumWorld != null)
{
Call("AddList", "Thorium");
Call("AddKey", "Thorium", "primordials", (Func<bool>)delegate () { return (bool)thoriumWorld.GetType().GetField("downedThePrimordials").GetValue(thoriumWorld); }, -1);
Call("AddKey", "Thorium", "ragnarok", (Func<bool>)delegate () { return (bool)thoriumWorld.GetType().GetField("downedThePrimordials").GetValue(thoriumWorld); }, -1);
Call("AddKey", "Thorium", "patchwerk", (Func<bool>)delegate () { return (bool)thoriumWorld.GetType().GetField("downedPatchwerk").GetValue(thoriumWorld); }, -1);
Call("AddKey", "Thorium", "bloom", (Func<bool>)delegate () { return (bool)thoriumWorld.GetType().GetField("downedBloom").GetValue(thoriumWorld); }, -1);
Call("AddKey", "Thorium", "strider", (Func<bool>)delegate () { return (bool)thoriumWorld.GetType().GetField("downedBoreanStrider").GetValue(thoriumWorld); }, -1);
Call("AddKey", "Thorium", "coznix", (Func<bool>)delegate () { return (bool)thoriumWorld.GetType().GetField("downedFallenBeholder").GetValue(thoriumWorld); }, -1);
Call("AddKey", "Thorium", "lich", (Func<bool>)delegate () { return (bool)thoriumWorld.GetType().GetField("downedLich").GetValue(thoriumWorld); }, -1);
Call("AddKey", "Thorium", "abyssion", (Func<bool>)delegate () { return (bool)thoriumWorld.GetType().GetField("downedForgottenOne").GetValue(thoriumWorld); }, -1);
}
}
// HERO's Mod support
if (herosLoaded)
try {
HerosIntegration(ModLoader.GetMod("HEROsMod"));
}
catch (Exception ex)
{
Logger.Warn("BetterTaxes.PostSetupContent() error: " + ex.StackTrace + ex.Message);
}
}
private void HerosIntegration(Mod herosMod)
{
herosMod.Call("AddPermission", "ModifyBTConfig", "Modify Better Taxes Config");
}
}
}