Skip to content

Commit

Permalink
Update default configuration and improve /commands command
Browse files Browse the repository at this point in the history
  • Loading branch information
RestoreMonarchy committed Nov 14, 2024
1 parent e8aecd4 commit 918ab2a
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 92 deletions.
35 changes: 22 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,46 @@
# Rich Message Announcer
Message announcer, text and web commands with rich text and icons.
Message announcer, text and web commands with rich text options and icons.

## Commands
- **/commands** - Display a list of permissions (works the same as `/p`)
- **/commands** - Display a list of commands the player has permission for

## Configuration
```xml
<?xml version="1.0" encoding="utf-8"?>
<RichMessageAnnouncerConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MessageColor>yellow</MessageColor>
<MessageInterval>180</MessageInterval>
<MessageColor>white</MessageColor>
<MessageIconUrl>https://i.imgur.com/tLPIfuf.png</MessageIconUrl>
<MessageInterval>300</MessageInterval>
<Messages>
<Message Text="Thank You for playing on our server!" IconUrl="https://i.imgur.com/pKphzxH.png" Color="yellow" />
<Message Text="Check out RestoreMonarchy.com!" IconUrl="https://i.imgur.com/pKphzxH.png" Color="yellow" />
<Message Text="Welcome! This server uses {b}RichMessageAnnouncer{/b}." IconUrl="https://i.imgur.com/tLPIfuf.png" Color="white" />
<Message Text="Use {color=#3498db}/commands{/color} to see available commands!" IconUrl="https://i.imgur.com/tLPIfuf.png" Color="white" />
<Message Text="{size=15}Tip: You can customize these messages in the config.{/size}" IconUrl="https://i.imgur.com/tLPIfuf.png" Color="white" />
<Message Text="Format examples: {b}Bold{/b}, {color=#e74c3c}Color{/color}, {size=20}Size{/size}" IconUrl="https://i.imgur.com/tLPIfuf.png" Color="white" />
</Messages>
<TextCommands>
<TextCommand>
<Name>rules</Name>
<Help>Shows rules</Help>
<Color>orange</Color>
<Message>There's no rules, just chill and have fun!</Message>
<IconUrl>https://i.imgur.com/pKphzxH.png</IconUrl>
<Color>white</Color>
<Message>Server Rules:{br}1. {color=#e74c3c}Be respectful{/color}{br}2. {color=#2ecc71}No cheating{/color}{br}3. {color=#f1c40f}Have fun!{/color}</Message>
<IconUrl>https://i.imgur.com/tLPIfuf.png</IconUrl>
</TextCommand>
</TextCommands>
<WebsiteCommands>
<WebCommand>
<Name>web</Name>
<Help>Shows web url</Help>
<Name>website</Name>
<Url>https://restoremonarchy.com</Url>
<Description>Restore Monarchy Website</Description>
</WebCommand>
</WebsiteCommands>
<EnableWelcomeMessage>true</EnableWelcomeMessage>
<WelcomeMessage Text="Welcome to the server!" IconUrl="https://i.imgur.com/pKphzxH.png" Color="yellow" />
<WelcomeMessage Text="{size=18}Welcome to the server!{/size}{br}Rules: {color=#e74c3c}Be respectful{/color} • {color=#2ecc71}No cheating{/color} • {color=#f1c40f}Have fun!{/color}{br}{color=#3498db}Type /commands for commands{/color}" IconUrl="https://i.imgur.com/tLPIfuf.png" Color="white" />
</RichMessageAnnouncerConfiguration>
```

## Translations
```xml
<?xml version="1.0" encoding="utf-8"?>
<Translations xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Translation Id="Commands" Value="[[b]]Your commands:[[/b]] {0}" />
</Translations>
```
41 changes: 34 additions & 7 deletions RichMessageAnnouncer/Commands/CommandsCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Rocket.API;
using Rocket.API.Serialisation;
using Rocket.Core;
using Rocket.Unturned.Chat;
using System;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -11,16 +12,42 @@ public class CommandsCommand : IRocketCommand
private RichMessageAnnouncerPlugin pluginInstance => RichMessageAnnouncerPlugin.Instance;
public AllowedCaller AllowedCaller => AllowedCaller.Both;
public string Name => "commands";
public string Help => "Shows all your available commands";
public string Help => "Shows all players available permissions";
public string Syntax => "";
public List<string> Aliases => new List<string>() { "cmds" };
public List<string> Permissions => new List<string>();
public List<string> Aliases => ["cmds"];
public List<string> Permissions => [];

public void Execute(IRocketPlayer caller, string[] command)
{
var permissions = R.Permissions.GetPermissions(caller);
UnturnedChat.Say(caller, pluginInstance.Translate("Commands", string.Join(", ", permissions.Select(x => x.Name))),
UnturnedChat.GetColorFromName(pluginInstance.Configuration.Instance.MessageColor, UnityEngine.Color.green));
List<Permission> permissions = R.Permissions.GetPermissions(caller);

List<string> commands = new();
foreach (IRocketCommand cmd in R.Commands.Commands)
{
foreach (Permission permission in permissions)
{
if (cmd.Permissions.Any(x => x.Equals(permission.Name, StringComparison.OrdinalIgnoreCase)))
{
commands.Add(cmd.Name);
break;
}

if (cmd.Aliases.Any(x => x.Equals(permission.Name, StringComparison.OrdinalIgnoreCase)))
{
commands.Add(permission.Name);
break;
}

if (cmd.Name.Equals(permission.Name, StringComparison.OrdinalIgnoreCase))
{
commands.Add(cmd.Name);
break;
}
}
}

string commandsString = string.Join(", ", commands.Select(x => x.ToLower()).Distinct());
pluginInstance.SendMessageToPlayer(caller, "Commands", [commandsString]);
}
}
}
15 changes: 1 addition & 14 deletions RichMessageAnnouncer/Commands/RocketTextCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public RocketTextCommand(TextCommand textCommand)
Color = textCommand.Color;
Message = textCommand.Message;
IconUrl = textCommand.IconUrl;

MessageColor = UnturnedChat.GetColorFromName(Color, UnityEngine.Color.green);
}

public AllowedCaller AllowedCaller => AllowedCaller.Both;
Expand All @@ -31,24 +29,13 @@ public RocketTextCommand(TextCommand textCommand)
public string Message { get; set; }
public string IconUrl { get; set; }

public UnityEngine.Color MessageColor { get; set; }

public string Syntax => "";
public List<string> Aliases => new List<string>();
public List<string> Permissions => new List<string>();

public void Execute(IRocketPlayer caller, string[] command)
{
UnturnedPlayer player = (UnturnedPlayer)caller;
string richMessage = Message.Replace('{', '<').Replace('}', '>');

if (player != null)
{
ChatManager.serverSendMessage(richMessage, MessageColor, null, player.SteamPlayer(), EChatMode.SAY, IconUrl, true);
} else
{
Logger.Log(richMessage);
}
pluginInstance.SendMessageToPlayer(caller, Message, null, IconUrl, Color);
}
}
}
3 changes: 2 additions & 1 deletion RichMessageAnnouncer/Commands/RocketWebsiteCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Rocket.API;
using Rocket.Core.Logging;
using Rocket.Unturned.Chat;
using Rocket.Unturned.Player;
using System.Collections.Generic;
Expand Down Expand Up @@ -34,7 +35,7 @@ public void Execute(IRocketPlayer caller, string[] command)
{
if (caller.Id == "Console")
{
UnturnedChat.Say(caller, $"{desc} {url}");
Logger.Log($"{desc} {url}");
} else
{
var player = (UnturnedPlayer)caller;
Expand Down
25 changes: 25 additions & 0 deletions RichMessageAnnouncer/Models/Message.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Xml.Serialization;

namespace RestoreMonarchy.RichMessageAnnouncer.Models
{
public sealed class Message
{
public Message(string text, string iconUrl, string color)
{
Text = text;
IconUrl = iconUrl;
Color = color;
}

public Message() { }

[XmlAttribute]
public string Text { get; set; }
[XmlAttribute]
public string IconUrl { get; set; }
public bool ShouldSerializeIconUrl() => !string.IsNullOrEmpty(IconUrl);
[XmlAttribute]
public string Color { get; set; }
public bool ShouldSerializeColor() => !string.IsNullOrEmpty(Color);
}
}
3 changes: 3 additions & 0 deletions RichMessageAnnouncer/Models/TextCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ public TextCommand(string name, string help, string color, string message, strin

public string Name { get; set; }
public string Help { get; set; }
public bool ShouldSerializeHelp() => !string.IsNullOrEmpty(Help);
public string Color { get; set; }
public bool ShouldSerializeColor() => !string.IsNullOrEmpty(Color);
public string Message { get; set; }
public string IconUrl { get; set; }
public bool ShouldSerializeIconUrl() => !string.IsNullOrEmpty(IconUrl);
}
}
4 changes: 3 additions & 1 deletion RichMessageAnnouncer/Models/WebsiteCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public WebsiteCommand(string name, string help, string url, string desc)
public WebsiteCommand() { }
public string Name { get; set; }
public string Help { get; set; }
public bool ShouldSerializeHelp() => !string.IsNullOrEmpty(Help);
public string Url { get; set; }
public string Description { get; set; }
public string Description { get; set; }
public bool ShouldSerializeDescription() => !string.IsNullOrEmpty(Description);
}
}
2 changes: 1 addition & 1 deletion RichMessageAnnouncer/RichMessageAnnouncer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net48</TargetFramework>
<LangVersion>latest</LangVersion>
<RootNamespace>RestoreMonarchy.RichMessageAnnouncer</RootNamespace>
<Version>1.0.0</Version>
<Version>1.1.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
57 changes: 21 additions & 36 deletions RichMessageAnnouncer/RichMessageAnnouncerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace RestoreMonarchy.RichMessageAnnouncer
public class RichMessageAnnouncerConfiguration : IRocketPluginConfiguration
{
public string MessageColor { get; set; }
public string MessageIconUrl { get; set; }
public double MessageInterval { get; set; }
[XmlArrayItem("Message")]
public List<Message> Messages { get; set; }
Expand All @@ -20,42 +21,26 @@ public class RichMessageAnnouncerConfiguration : IRocketPluginConfiguration

public void LoadDefaults()
{
MessageColor = "yellow";
MessageInterval = 180;
Messages = new List<Message>()
{
new Message("Thank You for playing on our server!", "https://i.imgur.com/pKphzxH.png", "yellow"),
new Message("Check out RestoreMonarchy.com", "https://i.imgur.com/pKphzxH.png", "yellow")
};
TextCommands = new List<TextCommand>()
{
new TextCommand("rules", "Shows rules", "orange", "There's no rules, just chill and have fun!", "https://i.imgur.com/pKphzxH.png")
};
WebsiteCommands = new List<WebsiteCommand>()
{
new WebsiteCommand("web", "Shows web url", "https://restoremonarchy.com", "Restore Monarchy Website")
};
MessageColor = "white";
MessageIconUrl = "https://i.imgur.com/tLPIfuf.png";
MessageInterval = 300;
Messages =
[
new("Welcome! This server uses {b}RichMessageAnnouncer{/b}.", "https://i.imgur.com/tLPIfuf.png", "white"),
new("Use {color=#3498db}/commands{/color} to see available commands!", "https://i.imgur.com/tLPIfuf.png", "white"),
new("{size=15}Tip: You can customize these messages in the config.{/size}", "https://i.imgur.com/tLPIfuf.png", "white"),
new("Format examples: {b}Bold{/b}, {color=#e74c3c}Color{/color}, {size=20}Size{/size}", "https://i.imgur.com/tLPIfuf.png", "white")
];
TextCommands =
[
new("rules", null, "white", "Server Rules:{br}1. {color=#e74c3c}Be respectful{/color}{br}2. {color=#2ecc71}No cheating{/color}{br}3. {color=#f1c40f}Have fun!{/color}", "https://i.imgur.com/tLPIfuf.png")
];
WebsiteCommands =
[
new("website", null, "https://restoremonarchy.com", "Restore Monarchy Website")
];
EnableWelcomeMessage = true;
WelcomeMessage = new Message("Welcome to the server!", "https://i.imgur.com/pKphzxH.png", "yellow");
WelcomeMessage = new Message("{size=18}Welcome to the server!{/size}{br}Rules: {color=#e74c3c}Be respectful{/color} • {color=#2ecc71}No cheating{/color} • {color=#f1c40f}Have fun!{/color}{br}{color=#3498db}Type /commands for commands{/color}", "https://i.imgur.com/tLPIfuf.png", "white");
}
}

public sealed class Message
{
public Message(string text, string iconUrl, string color)
{
Text = text;
IconUrl = iconUrl;
Color = color;
}

public Message() { }

[XmlAttribute]
public string Text { get; set; }
[XmlAttribute]
public string IconUrl { get; set; }
[XmlAttribute]
public string Color { get; set; }
}
}
}
Loading

0 comments on commit 918ab2a

Please sign in to comment.