Skip to content

Commit

Permalink
Merge pull request #42 from Soof4/master
Browse files Browse the repository at this point in the history
Fix Terraria commands' args showing up on Discord and add QS, Deerclops
  • Loading branch information
xNarnia authored May 8, 2024
2 parents f9df754 + 52c3045 commit adebfca
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions TCR.TShock/TerrariaChatRelayTShock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class TerrariaChatRelayTShock : TerrariaPlugin
public Version LatestVersion = new Version("0.0.0.0");

public string CommandPrefix;
public string SilentCommandPrefix;

// Really weird way to fix the double broadcasting issue. TShock's code simply will not allow any other way.
public List<Chatter> ChatHolder = new List<Chatter>();
Expand All @@ -42,7 +43,7 @@ public class Chatter
public string Text;
public Chatter()
{

}
}

Expand All @@ -54,7 +55,9 @@ public Chatter()
13, // Eater of Worlds
266, // Brain of Cthulu
35, // Skeletron
668, // Deerclops
113, // Wall of Flesh
657, // Queen Slime
125, // Retinazer
127, // Skeletron Prime
134, // The Destroyer
Expand All @@ -78,11 +81,12 @@ public override void Initialize()
Global.Config = new TCRConfig().GetOrCreateConfiguration();

CommandPrefix = TShock.Config.Settings.CommandSpecifier;
SilentCommandPrefix = TShock.Config.Settings.CommandSilentSpecifier;

ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

Global.Config = new TCRConfig().GetOrCreateConfiguration();

// Add subscribers to list
Core.Initialize(new TShockAdapter());
Core.ConnectClients();
Expand Down Expand Up @@ -122,31 +126,51 @@ private void OnReload(ReloadEventArgs reloadEventArgs)

private void OnChatReceived(ServerChatEventArgs args)
{
if (args.Text.StartsWith(CommandPrefix) || args.Text.StartsWith("."))
string text = args.Text;

// Terraria's client side commands remove the command prefix,
// which results in arguments of that command show up on the Discord.
// Thus, it needs to be reversed
foreach (var item in Terraria.UI.Chat.ChatManager.Commands._localizedCommands)
{
if (item.Value._name == args.CommandId._name)
{
if (!string.IsNullOrEmpty(text))
{
text = item.Key.Value + ' ' + text;
}
else
{
text = item.Key.Value;
}
break;
}
}

if (text.StartsWith(CommandPrefix) || text.StartsWith(SilentCommandPrefix))
return;

if (args.Text == "" || args.Text == null)
if (text == "" || text == null)
return;

if (TShock.Players[args.Who].mute == true)
return;

var snippets = ChatManager.ParseMessage(args.Text, Color.White);
var snippets = ChatManager.ParseMessage(text, Color.White);

string outmsg = "";
foreach (var snippet in snippets)
{
outmsg += snippet.Text;
}

ChatHolder.Add(new Chatter()
{
Player = Main.player[args.Who].ToTCRPlayer(args.Who),
ChatHolder.Add(new Chatter()
{
Player = Main.player[args.Who].ToTCRPlayer(args.Who),
Text = $"{outmsg}"
});


Core.RaiseTerrariaMessageReceived(this, Main.player[args.Who].ToTCRPlayer(args.Who), args.Text);
Core.RaiseTerrariaMessageReceived(this, Main.player[args.Who].ToTCRPlayer(args.Who), text);
}

private void OnPlayerGreet(GreetPlayerEventArgs args)
Expand Down Expand Up @@ -176,7 +200,7 @@ private void OnServerLeave(LeaveEventArgs args)
if (Main.player[args.Who].name != ""
&& Main.player[args.Who].name != " "
&& Main.player[args.Who].name != null
&& Main.player[args.Who].name.Replace("*" , "") != ""
&& Main.player[args.Who].name.Replace("*", "") != ""
&& Netplay.Clients[args.Who].State >= 3)
{
var player = Main.player[args.Who];
Expand All @@ -193,7 +217,7 @@ private void OnServerLeave(LeaveEventArgs args)
private void OnNPCSpawn(NpcSpawnEventArgs args)
{
NPC npc = Main.npc[args.NpcId];

if (BossIDs.Contains(npc.netID))
{
Core.RaiseTerrariaMessageReceived(this, TCRPlayer.Server, $"{npc.FullName} has awoken!");
Expand Down Expand Up @@ -221,7 +245,7 @@ private void OnServerBroadcast(ServerBroadcastEventArgs args)
literalText.EndsWith(" has joined.") || // User joined
literalText.EndsWith(" has left.") || // User left
literalText.EndsWith(" has awoken!") //|| // Boss Spawn
//Regex.IsMatch(literalText, @".*?:\s+.*") // Chat
//Regex.IsMatch(literalText, @".*?:\s+.*") // Chat
)
return;

Expand All @@ -232,7 +256,7 @@ private void OnServerBroadcast(ServerBroadcastEventArgs args)
return;
}

Core.RaiseTerrariaMessageReceived(this, TCRPlayer.Server, literalText);
Core.RaiseTerrariaMessageReceived(this, TCRPlayer.Server, literalText);
}

//private void OnBroadcastMessage(NetworkText text, ref Color color, ref int ignorePlayer)
Expand Down

0 comments on commit adebfca

Please sign in to comment.