Skip to content
This repository was archived by the owner on Jan 16, 2020. It is now read-only.

Commit bb389b8

Browse files
committed
Convert localized stuff to extension methods (to be DRY pattern conform)
1 parent 8208aca commit bb389b8

8 files changed

Lines changed: 58 additions & 44 deletions

File tree

Rocket.API/Chat/IChatManager.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,11 @@ public interface IChatManager
1515
/// <param name="bindings">The bindings for the message</param>
1616
void SendMessage(IOnlinePlayer player, string message, params object[] bindings);
1717

18-
/// <summary>
19-
/// Sends a localized message to the given player
20-
/// </summary>
21-
/// <param name="translationSource">The translation source</param>
22-
/// <param name="player">The receiver of the message</param>
23-
/// <param name="translationKey">The key of the translated message to send</param>
24-
/// <param name="bindings">The bindings for the message</param>
25-
void SendLocalizedMessage(ITranslationLocator translationSource, IOnlinePlayer player, string translationKey,
26-
params object[] bindings);
27-
2818
/// <summary>
2919
/// Broadcasts a message to all players
3020
/// </summary>
3121
/// <param name="message">The message to broadcast</param>
3222
/// <param name="bindings">The bindings for the message</param>
3323
void Broadcast(string message, params object[] bindings);
34-
35-
/// <summary>
36-
/// Broadcasts a localized message to all players
37-
/// </summary>
38-
/// <param name="translations">The translation soruce</param>
39-
/// <param name="translationKey">The key of the translated message to send</param>
40-
/// <param name="bindings">The bindings for the message</param>
41-
void BroadcastLocalized(ITranslationLocator translations, string translationKey, params object[] bindings);
4224
}
4325
}

Rocket.API/Commands/ICommandCaller.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using Rocket.API.Chat;
23
using Rocket.API.I18N;
34
using Rocket.API.Permissions;
5+
using Rocket.API.Player;
46

57
namespace Rocket.API.Commands
68
{
@@ -22,14 +24,5 @@ public interface ICommandCaller : IPermissible
2224
/// <param name="color">The color of the message. Depending on the caller implementation, it may not be used.</param>
2325
/// <param name="bindings">The bindings for the message. See <see cref="string.Format(string, object[])"/></param>
2426
void SendMessage(string message, ConsoleColor? color = null, params object[] bindings);
25-
26-
/// <summary>
27-
/// Sends a localized (translatable) message to the command caller.
28-
/// </summary>
29-
/// <param name="translations">The translations source.</param>
30-
/// <param name="translationKey">The translation key.</param>
31-
/// <param name="color">The color of the message. Depending on the caller implementation, it may not be used.</param>
32-
/// <param name="bindings">The bindings for the message. See <see cref="string.Format(string, object[])"/></param>
33-
void SendLocalizedMessage(ITranslationLocator translations, string translationKey, ConsoleColor? color = null, params object[] bindings);
3427
}
3528
}

Rocket.ConsoleImplementation/ConsoleCaller.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ public void SendMessage(string message, ConsoleColor? color = null, params objec
4141
Console.ForegroundColor = tmp;
4242
}
4343

44-
public void SendLocalizedMessage(ITranslationLocator translations, string translationKey, ConsoleColor? color = null,
45-
params object[] bindings)
46-
{
47-
SendMessage(translations.GetLocalizedMessage(translationKey), color, bindings);
48-
}
49-
5044
public string ToString(string format, IFormatProvider formatProvider)
5145
{
5246
return Id;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using Rocket.API.Chat;
3+
using Rocket.API.Commands;
4+
using Rocket.API.I18N;
5+
using Rocket.API.Player;
6+
7+
namespace Rocket.Core.Permissions
8+
{
9+
public static class LocalizationExtensions
10+
{
11+
/// <summary>
12+
/// Sends a localized (translatable) message to the command caller.
13+
/// </summary>
14+
/// <param name="commandCaller">The message receiver.</param>
15+
/// <param name="translations">The translations source.</param>
16+
/// <param name="translationKey">The translation key.</param>
17+
/// <param name="color">The color of the message. Depending on the caller implementation, it may not be used.</param>
18+
/// <param name="bindings">The bindings for the message. See <see cref="string.Format(string, object[])"/></param>
19+
public static void SendLocalizedMessage(this ICommandCaller commandCaller, ITranslationLocator translations, string translationKey,
20+
ConsoleColor? color = null, params object[] bindings)
21+
{
22+
commandCaller.SendMessage(translations.GetLocalizedMessage(translationKey, bindings), color);
23+
}
24+
25+
/// <summary>
26+
/// Sends a localized message to the given player
27+
/// </summary>
28+
/// <param name="chatManager">The chat manager.</param>
29+
/// <param name="translationSource">The translation source</param>
30+
/// <param name="player">The receiver of the message</param>
31+
/// <param name="translationKey">The translation key.</param>
32+
/// <param name="bindings">The bindings for the message</param>
33+
public static void SendLocalizedMessage(this IChatManager chatManager, ITranslationLocator translations,
34+
IOnlinePlayer player, string translationKey, params object[] bindings)
35+
{
36+
chatManager.SendMessage(player, translations.GetLocalizedMessage(translationKey, bindings));
37+
}
38+
39+
40+
41+
/// <summary>
42+
/// Broadcasts a localized message to all players
43+
/// </summary>
44+
/// <param name="chatManager">The chat manager.</param>
45+
/// <param name="translations">The translation soruce</param>
46+
/// <param name="translationKey">The key of the translated message to send</param>
47+
/// <param name="bindings">The bindings for the message</param>
48+
public static void BroadcastLocalized(this IChatManager chatManager, ITranslationLocator translations,
49+
string translationKey, params object[] bindings)
50+
{
51+
chatManager.Broadcast(translations.GetLocalizedMessage(translationKey, bindings));
52+
}
53+
}
54+
}

Rocket.Core/Player/BaseOnlinePlayer.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,5 @@ public override string ToString(string format, IFormatProvider formatProvider)
4747
public abstract TimeSpan SessionOnlineTime { get; }
4848
public string EntityTypeName => "Player";
4949
public abstract void SendMessage(string message, ConsoleColor? color = null, params object[] bindings);
50-
51-
public void SendLocalizedMessage(ITranslationLocator translations, string translationKey, ConsoleColor? color = null,
52-
params object[] bindings)
53-
{
54-
SendMessage(translations.GetLocalizedMessage(translationKey), color, bindings);
55-
}
5650
}
5751
}

Rocket.Core/Rocket.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<Compile Include="Commands\RocketCommands\CommandRocket.cs" />
6969
<Compile Include="Extensions\TypeConverterExtensions.cs" />
7070
<Compile Include="Logging\ProxyLogger.cs" />
71+
<Compile Include="Permissions\LocalizationExtensions.cs" />
7172
<Compile Include="Permissions\NotEnoughPermissionsException.cs" />
7273
<Compile Include="Configuration\ConfigurationNotLoadedException.cs" />
7374
<Compile Include="Configuration\JsonNetBase\JsonNetConfigurationBase.cs" />

Rocket.Tests/Mock/TestConsoleCaller.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ public void SendMessage(string message, ConsoleColor? color = null, params objec
1616
Console.WriteLine("[SendMessage] " + message, bindings);
1717
Console.ForegroundColor = tmp;
1818
}
19-
public void SendLocalizedMessage(ITranslationLocator translations, string translationKey, ConsoleColor? color = null,
20-
params object[] bindings)
21-
{
22-
SendMessage(translations.GetLocalizedMessage(translationKey), color, bindings);
23-
}
2419

2520
public int CompareTo(object obj)
2621
{

examples/commands/CommandsCollection.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Rocket.API.I18N;
44
using Rocket.API.Player;
55
using Rocket.Core.Commands;
6+
using Rocket.Core.Permissions;
67

78
namespace Rocket.Examples.CommandsPlugin
89
{

0 commit comments

Comments
 (0)