-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathFargoUtils.cs
More file actions
122 lines (108 loc) · 4.82 KB
/
FargoUtils.cs
File metadata and controls
122 lines (108 loc) · 4.82 KB
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
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Terraria;
using Terraria.Audio;
using Terraria.Chat;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
namespace Fargowiltas
{
internal static class FargoUtils
{
public static readonly BindingFlags UniversalBindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public;
public static bool EternityMode => Fargowiltas.ModLoaded["FargowiltasSouls"] && (bool) ModLoader.GetMod("FargowiltasSouls").Call("EternityMode");
public static bool HasAnyItem(this Player player, params int[] itemIDs) => itemIDs.Any(itemID => player.HasItem(itemID));
public static bool ActuallyNight => !Main.dayTime || Main.remixWorld;
public static FargoPlayer GetFargoPlayer(this Player player) => player.GetModPlayer<FargoPlayer>();
public static void AddWithCondition<T>(this List<T> list, T type, bool condition)
{
if (condition)
{
list.Add(type);
}
}
public static void AddDebuffImmunities(this NPC npc, List<int> debuffs)
{
foreach (int buffType in debuffs)
{
NPCID.Sets.SpecificDebuffImmunity[npc.type][buffType] = true;
}
}
public static void TryDowned(string seller, Color color, params string[] names)
{
TryDowned(seller, color, true, names);
}
// condition is so that display text is hidden if the kill is done early, BUT the kill is still counted
// e.g. kill an enemy early, whose spawner is sold in hm, then get into hm, then spawner is unlocked
// however, text is hidden on that first kill so people don't think it's sold right away
public static void TryDowned(string seller, Color color, bool conditions, params string[] names)
{
bool update = false;
foreach (string name in names)
{
if (!FargoWorld.DownedBools[name])
{
FargoWorld.DownedBools[name] = true;
update = true;
}
}
if (update)
{
seller = Language.GetTextValue($"Mods.Fargowiltas.NPCs.{seller}.DisplayName");
string text = Language.GetTextValue("Mods.Fargowiltas.MessageInfo.NewItemUnlocked", seller);
if (Main.netMode == NetmodeID.SinglePlayer)
{
if (conditions)
Main.NewText(text, color);
}
else if (Main.netMode == NetmodeID.Server)
{
if (conditions)
ChatHelper.BroadcastChatMessage(NetworkText.FromLiteral(text), color);
NetMessage.SendData(MessageID.WorldData); //sync world
}
}
}
public static void PrintText(string text)
{
PrintText(text, Color.White);
}
public static void PrintText(string text, Color color)
{
if (Main.netMode == NetmodeID.SinglePlayer)
{
Main.NewText(text, color);
}
else if (Main.netMode == NetmodeID.Server)
{
ChatHelper.BroadcastChatMessage(NetworkText.FromLiteral(text), color);
}
}
public static void PrintText(string text, int r, int g, int b) => PrintText(text, new Color(r, g, b));
public static void PrintLocalization(string fargoKey, params object[] args) => PrintText(Language.GetTextValue($"Mods.Fargowiltas.{fargoKey}", args));
public static void PrintLocalization(string fargoKey, Color color, params object[] args) => PrintText(Language.GetTextValue($"Mods.Fargowiltas.{fargoKey}", args), color);
public static void SpawnBossNetcoded(Player player, int bossType)
{
if (player.whoAmI == Main.myPlayer)
{
// If the player using the item is the client
// (explicitely excluded serverside here)
SoundEngine.PlaySound(SoundID.Roar, player.position);
if (Main.netMode != NetmodeID.MultiplayerClient)
{
// If the player is not in multiplayer, spawn directly
NPC.SpawnOnPlayer(player.whoAmI, bossType);
}
else
{
// If the player is in multiplayer, request a spawn
// This will only work if NPCID.Sets.MPAllowedEnemies[type] is true, set in NPC code
NetMessage.SendData(MessageID.SpawnBossUseLicenseStartEvent, number: player.whoAmI, number2: bossType);
}
}
}
}
}