Skip to content

Commit

Permalink
Add aliases for server settings for accessibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Extremelyd1 committed Oct 3, 2023
1 parent c5f195d commit 8fc83f5
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
28 changes: 21 additions & 7 deletions HKMP/Game/Command/Server/SettingsCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Reflection;
using Hkmp.Api.Command.Server;
using Hkmp.Game.Server;
Expand Down Expand Up @@ -41,34 +42,47 @@ public virtual void Execute(ICommandSender commandSender, string[] args) {
return;
}

var settingName = args[1];
var settingName = args[1].ToLower().Replace("_", "").Replace("-", "");

var propertyInfos = typeof(ServerSettings).GetProperties();

PropertyInfo settingProperty = null;
foreach (var prop in propertyInfos) {
if (prop.Name.Equals(settingName)) {
// Check if the property name in lower case equals the argument
if (prop.Name.ToLower().Equals(settingName)) {
settingProperty = prop;
break;
}

// Alternatively check for the alias attribute and all aliases
var aliasAttribute = prop.GetCustomAttribute<SettingAliasAttribute>();
if (aliasAttribute != null) {
if (aliasAttribute.Aliases.Contains(settingName)) {
settingProperty = prop;
break;
}
}
}

if (settingProperty == null || !settingProperty.CanRead) {
commandSender.SendMessage($"Could not find setting with name: {settingName}");
return;
}

var propName = settingProperty.Name;

if (args.Length < 3) {
// The user only supplied the name of the setting, so we print its value
var currentValue = settingProperty.GetValue(ServerSettings, null);

commandSender.SendMessage($"Setting '{settingName}' currently has value: {currentValue}");
commandSender.SendMessage($"Setting '{propName}' currently has value: {currentValue}");
return;
}

var newValueString = args[2];

if (!settingProperty.CanWrite) {
commandSender.SendMessage($"Could not change value of setting with name: {settingName} (non-writable)");
commandSender.SendMessage($"Could not change value of setting with name: {propName} (non-writable)");
return;
}

Expand All @@ -90,18 +104,18 @@ public virtual void Execute(ICommandSender commandSender, string[] args) {
newValueObject = newValueByte;
} else {
commandSender.SendMessage(
$"Could not change value of setting with name: {settingName} (unhandled type)");
$"Could not change value of setting with name: {propName} (unhandled type)");
return;
}

if (settingProperty.GetValue(ServerSettings).Equals(newValueObject)) {
commandSender.SendMessage($"Setting '{settingName}' already has value: {newValueObject}");
commandSender.SendMessage($"Setting '{propName}' already has value: {newValueObject}");
return;
}

settingProperty.SetValue(ServerSettings, newValueObject, null);

commandSender.SendMessage($"Changed setting '{settingName}' to: {newValueObject}");
commandSender.SendMessage($"Changed setting '{propName}' to: {newValueObject}");

_serverManager.OnUpdateServerSettings();
}
Expand Down
23 changes: 23 additions & 0 deletions HKMP/Game/Settings/ServerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,98 @@
using Hkmp.Api.Server;
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable StringLiteralTypo

namespace Hkmp.Game.Settings;

/// <inheritdoc cref="IServerSettings" />
public class ServerSettings : IServerSettings, IEquatable<ServerSettings> {
/// <inheritdoc />
[SettingAlias("pvp")]
public bool IsPvpEnabled { get; set; }

/// <inheritdoc />
[SettingAlias("bodydamage")]
public bool IsBodyDamageEnabled { get; set; } = true;

/// <inheritdoc />
[SettingAlias("globalmapicons")]
public bool AlwaysShowMapIcons { get; set; }

/// <inheritdoc />
[SettingAlias("compassicon", "compassicons", "waywardicon", "waywardicons")]
public bool OnlyBroadcastMapIconWithWaywardCompass { get; set; } = true;

/// <inheritdoc />
[SettingAlias("names")]
public bool DisplayNames { get; set; } = true;

/// <inheritdoc />
[SettingAlias("teams")]
public bool TeamsEnabled { get; set; }

/// <inheritdoc />
[SettingAlias("skins")]
public bool AllowSkins { get; set; } = true;

/// <inheritdoc />
[SettingAlias("parries")]
public bool AllowParries { get; set; } = true;

/// <inheritdoc />
[SettingAlias("naildmg")]
public byte NailDamage { get; set; } = 1;

/// <inheritdoc />
[SettingAlias("elegydmg")]
public byte GrubberflyElegyDamage { get; set; } = 1;

/// <inheritdoc />
[SettingAlias("vsdmg", "fireballdamage", "fireballdmg")]
public byte VengefulSpiritDamage { get; set; } = 1;

/// <inheritdoc />
[SettingAlias("shadesouldmg")]
public byte ShadeSoulDamage { get; set; } = 2;

/// <inheritdoc />
[SettingAlias("desolatedivedmg", "ddivedmg")]
public byte DesolateDiveDamage { get; set; } = 1;

/// <inheritdoc />
[SettingAlias("descendingdarkdmg", "ddarkdmg")]
public byte DescendingDarkDamage { get; set; } = 2;

/// <inheritdoc />
[SettingAlias("howlingwraithsdamage", "howlingwraithsdmg", "wraithsdmg")]
public byte HowlingWraithDamage { get; set; } = 1;

/// <inheritdoc />
[SettingAlias("abyssshriekdmg", "shriekdmg")]
public byte AbyssShriekDamage { get; set; } = 2;

/// <inheritdoc />
[SettingAlias("greatslashdmg")]
public byte GreatSlashDamage { get; set; } = 2;

/// <inheritdoc />
[SettingAlias("dashslashdmg")]
public byte DashSlashDamage { get; set; } = 2;

/// <inheritdoc />
[SettingAlias("cycloneslashdmg", "cyclonedmg")]
public byte CycloneSlashDamage { get; set; } = 1;

/// <inheritdoc />
[SettingAlias("sporeshroomdmg")]
public byte SporeShroomDamage { get; set; } = 1;

/// <inheritdoc />
[SettingAlias("sporedungshroomdmg", "dungshroomdmg")]
public byte SporeDungShroomDamage { get; set; } = 1;

/// <inheritdoc />
[SettingAlias("thornsofagonydamage", "thornsofagonydmg", "thornsdamage", "thornsdmg")]
public byte ThornOfAgonyDamage { get; set; } = 1;

/// <summary>
Expand Down
22 changes: 22 additions & 0 deletions HKMP/Game/Settings/SettingAliasAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace Hkmp.Game.Settings;

/// <summary>
/// Attribute to define aliases to settings properties/fields.
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class SettingAliasAttribute : Attribute {
/// <summary>
/// The string array containing the aliases.
/// </summary>
public string[] Aliases { get; private set; }

/// <summary>
/// Constructs the attribute with the given alias strings.
/// </summary>
/// <param name="aliases">One or more strings containing aliases.</param>
public SettingAliasAttribute(params string[] aliases) {
Aliases = aliases;
}
}

0 comments on commit 8fc83f5

Please sign in to comment.