Skip to content

Commit e11241e

Browse files
committed
cleanup
1 parent 59b4711 commit e11241e

28 files changed

Lines changed: 424 additions & 463 deletions

src/Api/Command/CommandExtensions.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
*/
2222
#endregion
2323

24+
using System;
2425
using UnityEngine;
2526

2627
namespace Essentials.Api.Command {
2728

2829
public static class CommandExtensions {
2930

3031
/// <summary>
31-
/// Try get a vector3 from 3 arguments, starting in <paramref name="initialIndex"/>
32+
/// Try to parse a Vector3 from 3 arguments, starting in <paramref name="initialIndex"/>
3233
/// </summary>
3334
/// <param name="src">Source</param>
3435
/// <param name="initialIndex"> Initial index </param>
@@ -60,6 +61,38 @@ public static bool IsInRange(this ICommandArgument src, int minInclusive, int ma
6061
return val >= minInclusive && val <= maxInclusive;
6162
}
6263

64+
/// <summary>
65+
/// Try to convert the argument to byte.
66+
/// </summary>
67+
/// <param name="value">the converted value</param>
68+
/// <param name="error">
69+
/// CommandResult.LangError("NUMBER_BETWEEN", byte.MinValue, byte.MaxValue) if out of range;
70+
// CommandResult.LangError("INVALID_NUMBER", src.ToString()) if invalid.
71+
/// </param>
72+
/// <returns>true if sucessfull, otherwise false</returns>
73+
public static bool TryConvertToByte(this ICommandArgument src, out byte value, out CommandResult error) {
74+
value = 0;
75+
error = null;
76+
try {
77+
value = byte.Parse(src.ToString());
78+
return true;
79+
} catch (OverflowException) {
80+
error = CommandResult.LangError("NUMBER_BETWEEN", byte.MinValue, byte.MaxValue);
81+
} catch (FormatException) {
82+
error = CommandResult.LangError("INVALID_NUMBER", src.ToString());
83+
}
84+
return false;
85+
}
86+
87+
public static bool TryConvertToByte(this ICommandArgument src, out byte? value, out CommandResult error) {
88+
byte result;
89+
if (src.TryConvertToByte(out result, out error)) {
90+
value = result;
91+
return true;
92+
}
93+
value = null;
94+
return false;
95+
}
6396
}
6497

6598
}

src/Api/Command/Source/AllowedSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public enum AllowedSource {
3131
CONSOLE,
3232

3333
/// <summary>
34-
/// Represents an Unturned player
34+
/// Represents a player
3535
/// </summary>
3636
PLAYER,
3737

src/Api/Command/Source/ICommandSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public interface ICommandSource : IPermissible {
6161
/// <summary>
6262
/// Dispatch a command
6363
/// </summary>
64-
/// <param name="command"> Command that you want to dispatch </param>
64+
/// <param name="command"> the command you want to dispatch </param>
6565
void DispatchCommand(string command);
6666

6767
}

src/Api/Task/Task.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ public Builder Async() {
109109
return this;
110110
}
111111

112-
// TODO: Better naming?
113112
public Builder UseIntervalAsDelay() {
114113
_task.Delay = _task.Interval;
115114
return this;

src/Api/UEssentials.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static class UEssentials {
5353
public static string ROCKET_VERSION => EssCore.ROCKET_VERSION;
5454

5555
/// <summary>
56-
/// Instance of CommandManager
56+
/// Instance of EventManager
5757
/// </summary>
5858
public static IEventManager EventManager => Core.EventManager;
5959

@@ -107,7 +107,7 @@ public static class UEssentials {
107107
public static string TranslationFolder => Core.TranslationFolder;
108108

109109
/// <summary>
110-
/// Translation folder path
110+
/// Modules folder path
111111
/// </summary>
112112
public static string ModulesFolder => Core.ModulesFolder;
113113

src/Api/Unturned/UPlayer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ public static bool TryGet(Player unturnedPlayer, Action<UPlayer> callback) {
428428
/// </summary>
429429
/// <param name="cmdArg"></param>
430430
/// <param name="callback"></param>
431+
[Obsolete("Use TryGet(string name, out UPlayer player) instead.")]
431432
public static bool TryGet(ICommandArgument cmdArg, Action<UPlayer> callback) {
432433
var player = From(cmdArg.ToString());
433434

@@ -439,6 +440,11 @@ public static bool TryGet(ICommandArgument cmdArg, Action<UPlayer> callback) {
439440
return false;
440441
}
441442

443+
public static bool TryGet(string name, out UPlayer player) {
444+
player = From(name);
445+
return player != null;
446+
}
447+
442448
public override string ToString() {
443449
return DisplayName;
444450
}

src/Api/Unturned/USkill.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222
#endregion
2323

24+
using System;
2425
using System.Linq;
2526
using Essentials.Common;
2627

@@ -90,19 +91,23 @@ private USkill(byte specialityIndex, byte skillIndex, string name) {
9091
/// <summary>
9192
/// Get skill from name.
9293
/// </summary>
93-
/// <param name="input">Skill name</param>
94+
/// <param name="name">Skill name</param>
9495
/// <returns>
9596
/// <see cref="Optional{USkill}.Empty"/> if not found,
9697
/// otherwise return a <see cref="Optional{USkill}"/> containing the skill.
9798
/// </returns>
98-
public static Optional<USkill> FromName(string input) {
99-
var skill = Skills
100-
.FirstOrDefault(sk => sk.Name.EqualsIgnoreCase(input))
101-
?? Skills.FirstOrDefault(sk => sk.Name.ContainsIgnoreCase(input));
102-
99+
[Obsolete("Use FromName(string name, out USkill skill) instead.")]
100+
public static Optional<USkill> FromName(string name) {
101+
var skill = Skills.FirstOrDefault(sk => sk.Name.EqualsIgnoreCase(name)) ??
102+
Skills.FirstOrDefault(sk => sk.Name.ContainsIgnoreCase(name));
103103
return Optional<USkill>.OfNullable(skill);
104104
}
105105

106-
};
106+
public static bool FromName(string name, out USkill skill) {
107+
skill = Skills.FirstOrDefault(sk => sk.Name.EqualsIgnoreCase(name)) ??
108+
Skills.FirstOrDefault(sk => sk.Name.ContainsIgnoreCase(name));
109+
return skill != null;
110+
}
111+
}
107112

108113
}

src/Commands/CommandBoom.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,10 @@ public override CommandResult OnExecute(ICommandSource src, ICommandArgs args) {
6767

6868
UServer.Players.Where(p => p != caller).ForEach(p => Explode(p.Position));
6969
} else {
70-
var found = UPlayer.TryGet(args[0], player => Explode(player.Position));
71-
72-
if (!found) {
70+
if (!UPlayer.TryGet(args[0].ToString(), out var player)) {
7371
return CommandResult.LangError("PLAYER_NOT_FOUND", args[0]);
7472
}
73+
Explode(player.Position);
7574
}
7675
break;
7776

src/Commands/CommandExperience.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ private void GiveExp(UPlayer player, int amount) {
101101
else
102102
playerExp += (uint) amount;
103103
} else {
104-
playerExp += (uint) amount;
104+
if ((playerExp + amount) > int.MaxValue)
105+
playerExp = int.MaxValue;
106+
else
107+
playerExp += (uint) amount;
105108
}
106109

107110
if (amount >= 0) {

src/Commands/CommandFreeze.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,17 @@ public override CommandResult OnExecute(ICommandSource src, ICommandArgs args) {
5353

5454
EssLang.Send(src, "FROZEN_ALL");
5555
} else {
56-
var found = UPlayer.TryGet(args[0], player => {
57-
if (player.HasComponent<FrozenPlayer>()) {
58-
EssLang.Send(src, "ALREADY_FROZEN", player.DisplayName);
59-
} else {
60-
player.AddComponent<FrozenPlayer>();
56+
if (!UPlayer.TryGet(args[0].ToString(), out var player)) {
57+
return CommandResult.LangError("PLAYER_NOT_FOUND", args[0]);
58+
}
6159

62-
EssLang.Send(src, "FROZEN_SENDER", player.DisplayName);
63-
EssLang.Send(player, "FROZEN_PLAYER", src.DisplayName);
64-
}
65-
});
60+
if (player.HasComponent<FrozenPlayer>()) {
61+
EssLang.Send(src, "ALREADY_FROZEN", player.DisplayName);
62+
} else {
63+
player.AddComponent<FrozenPlayer>();
6664

67-
if (!found) {
68-
return CommandResult.LangError("PLAYER_NOT_FOUND", args[0]);
65+
EssLang.Send(src, "FROZEN_SENDER", player.DisplayName);
66+
EssLang.Send(player, "FROZEN_PLAYER", src.DisplayName);
6967
}
7068
}
7169

0 commit comments

Comments
 (0)