Skip to content

Commit c996a0e

Browse files
committed
Refactored chat handling
1 parent abc5e78 commit c996a0e

File tree

4 files changed

+194
-157
lines changed

4 files changed

+194
-157
lines changed

GriefClientPro/ChatManager.cs

Lines changed: 106 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,44 @@ public class ChatManager
1313
// Special default prefix for Hellsing: http://steamcommunity.com/id/singhell/
1414
public static string DefaultPrefix => SteamUser.GetSteamID().m_SteamID == 76561198008774571 ? "Ⓗ " : "<ツ> ";
1515

16+
public enum ChatAs
17+
{
18+
Self,
19+
Random,
20+
Selected
21+
}
22+
23+
public enum ChatAsInvisible
24+
{
25+
Random,
26+
Selected
27+
}
28+
29+
public static readonly string[] ChatAsNames = Enum.GetNames(typeof(ChatAs));
30+
public static readonly string[] ChatAsInvisibleNames = Enum.GetNames(typeof(ChatAsInvisible));
31+
32+
public static readonly int ChatAsCount = ChatAsNames.Length;
33+
public static readonly int ChatAsInvisibleCount = ChatAsInvisibleNames.Length;
34+
35+
public class ChatEventArgs : EventArgs
36+
{
37+
public bool CustomHandler { get; set; }
38+
public string Text { get; set; }
39+
public NetworkId SenderId { get; set; }
40+
public GlobalTargets Target { get; set; } = GlobalTargets.OnlyServer;
41+
}
42+
1643
private GriefClientPro Instance { get; set; }
1744

18-
public bool UsePrefix = true;
45+
public bool UsePrefixWhenVisible;
46+
public bool UsePrefixWhenInvisible = true;
1947
public string Prefix = DefaultPrefix;
2048

21-
public bool ChatAsSelf = true;
22-
public bool ChatAsRandom;
23-
public bool ChatAsSelected;
24-
public bool ChatInvisibleAsRandom = true;
25-
public bool ChatInvisibleAsSelected;
49+
public int ChatAsValue = (int) ChatAs.Self;
50+
public int ChatAsInvisibleValue = (int) ChatAsInvisible.Random;
51+
52+
public ChatAs CurrentChatAs => (ChatAs) ChatAsValue;
53+
public ChatAsInvisible CurrentChatAsInvisible => (ChatAsInvisible) ChatAsInvisibleValue;
2654

2755
public Player LastChattedAs { get; set; }
2856

@@ -47,73 +75,93 @@ public void RandomizePlayer()
4775
LastChattedAs = GriefClientPro.PlayerManager.Players.Shuffle()[0];
4876
}
4977
}
50-
}
5178

52-
public class ChatBoxEx : ChatBox
53-
{
54-
public override void OnSubmit()
79+
public void OnSubmit(ChatEventArgs chatEvent)
5580
{
56-
if (GriefClientPro.ChatManager == null)
57-
{
58-
base.OnSubmit();
59-
}
60-
else
81+
if (!string.IsNullOrEmpty(chatEvent.Text))
6182
{
62-
if (!string.IsNullOrEmpty(_input.value))
83+
try
6384
{
64-
try
85+
if (!BoltNetwork.isRunning)
6586
{
66-
if (!BoltNetwork.isRunning)
67-
{
68-
return;
69-
}
87+
return;
88+
}
7089

71-
// Check if player is detached
72-
var attached = LocalPlayer.GameObject?.GetComponent<BoltEntity>()?.isAttached;
90+
// Check if player is detached
91+
var isAttached = LocalPlayer.Entity?.isAttached ?? false;
7392

74-
// Validate player
75-
GriefClientPro.ChatManager.ValidatePlayer();
93+
// Validate player
94+
ValidatePlayer();
95+
96+
// Check if we need to randomize
97+
if (CurrentChatAs == ChatAs.Random || (CurrentChatAs == ChatAs.Self && !isAttached && CurrentChatAsInvisible == ChatAsInvisible.Random))
98+
{
99+
RandomizePlayer();
76100

77-
// Check if we need to randomize
78-
if (GriefClientPro.ChatManager.ChatAsRandom ||
79-
(GriefClientPro.ChatManager.ChatAsSelf && attached.HasValue && !attached.Value && GriefClientPro.ChatManager.ChatInvisibleAsRandom))
101+
// Validate
102+
if (LastChattedAs == null)
103+
{
104+
Logger.Warning("Player still null even after refresh (all invisible?)");
105+
return;
106+
}
107+
if (!LastChattedAs.Entity.isAttached)
80108
{
81-
GriefClientPro.ChatManager.RandomizePlayer();
82-
83-
// Validate
84-
if (GriefClientPro.ChatManager.LastChattedAs == null)
85-
{
86-
Logger.Warning("Player still null even after refresh (all invisible?)");
87-
return;
88-
}
89-
if (!GriefClientPro.ChatManager.LastChattedAs.Entity.isAttached)
90-
{
91-
Logger.Warning("Random player is not attached (disconnecting?)");
92-
return;
93-
}
109+
Logger.Warning("Random player is not attached (disconnecting?)");
110+
return;
94111
}
112+
}
113+
114+
// Apply values
115+
if (!isAttached)
116+
{
117+
chatEvent.Target = GlobalTargets.Everyone;
118+
}
119+
chatEvent.CustomHandler = true;
120+
chatEvent.SenderId = CurrentChatAs == ChatAs.Self && isAttached ? LocalPlayer.Entity.networkId : LastChattedAs.NetworkId;
121+
chatEvent.Text = ((isAttached && UsePrefixWhenVisible) || (!isAttached && UsePrefixWhenInvisible) ? GriefClientPro.ChatManager.Prefix : string.Empty) + chatEvent.Text;
122+
}
123+
catch (Exception e)
124+
{
125+
Logger.Exception("Exception while processing chat message to send!", e);
126+
}
127+
}
128+
}
129+
}
130+
131+
public class ChatBoxEx : ChatBox
132+
{
133+
public override void OnSubmit()
134+
{
135+
if (GriefClientPro.ChatManager == null)
136+
{
137+
base.OnSubmit();
138+
return;
139+
}
95140

96-
// Get the player to chat as
97-
var player = GriefClientPro.ChatManager.LastChattedAs;
98-
var senderNetworkId = GriefClientPro.ChatManager.ChatAsSelf && attached.HasValue && attached.Value ? LocalPlayer.Entity.networkId : player.NetworkId;
141+
// Create local event
142+
var chatEvent = new ChatManager.ChatEventArgs
143+
{
144+
Text = _input.value
145+
};
99146

100-
// Create the chat event
101-
var chatEvent = ChatEvent.Create(GlobalTargets.OnlyServer);
102-
chatEvent.Message = (GriefClientPro.ChatManager.UsePrefix ? GriefClientPro.ChatManager.Prefix : "") + _input.value;
147+
// Process the event
148+
GriefClientPro.ChatManager.OnSubmit(chatEvent);
103149

104-
// Define the sender
105-
chatEvent.Sender = senderNetworkId;
150+
if (!chatEvent.CustomHandler || string.IsNullOrEmpty(chatEvent.Text))
151+
{
152+
base.OnSubmit();
153+
}
154+
else
155+
{
156+
// Create the chat event
157+
var @event = ChatEvent.Create(chatEvent.Target);
158+
@event.Message = chatEvent.Text;
159+
@event.Sender = chatEvent.SenderId;
106160

107-
// Send the message
108-
PacketQueue.Add(chatEvent);
109-
}
110-
catch (Exception e)
111-
{
112-
Logger.Exception("Exception while processing chat message to send!", e);
113-
}
161+
// Send the message
162+
PacketQueue.Add(@event);
114163

115-
_input.value = null;
116-
}
164+
_input.value = null;
117165
_mustClose = true;
118166
_lastInteractionTime = Time.time;
119167
}

0 commit comments

Comments
 (0)