refactor!: revert e71494ef83

This commit is contained in:
Oliver Booth 2024-06-11 17:08:45 +01:00
parent e71494ef83
commit c381e079c3
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
7 changed files with 26 additions and 15 deletions

View File

@ -13,6 +13,6 @@ public sealed class RequireBotOwnerAttribute : PreExecutionCheckAttribute
throw new ArgumentNullException(nameof(context)); throw new ArgumentNullException(nameof(context));
} }
return Task.FromResult(context.Avatar.User == context.Client.CurrentUser); return Task.FromResult(context.Avatar.UserId == context.Client.CurrentUser?.Id);
} }
} }

View File

@ -52,6 +52,6 @@ public sealed class RequireUserIdAttribute : PreExecutionCheckAttribute
throw new ArgumentNullException(nameof(context)); throw new ArgumentNullException(nameof(context));
} }
return Task.FromResult(UserIds.Any(i => context.Avatar.User.Id == i)); return Task.FromResult(UserIds.Contains(context.Avatar.UserId));
} }
} }

View File

@ -47,14 +47,14 @@ public sealed class RequireUserNameAttribute : PreExecutionCheckAttribute
public IReadOnlyList<string> Names { get; } public IReadOnlyList<string> Names { get; }
/// <inheritdoc /> /// <inheritdoc />
protected internal override Task<bool> PerformAsync(CommandContext context) protected internal override async Task<bool> PerformAsync(CommandContext context)
{ {
if (context is null) if (context is null)
{ {
throw new ArgumentNullException(nameof(context)); throw new ArgumentNullException(nameof(context));
} }
VirtualParadiseUser user = context.Avatar.User; VirtualParadiseUser user = await context.Avatar.GetUserAsync().ConfigureAwait(false);
return Task.FromResult(Names.Contains(user.Name)); return Names.Contains(user.Name);
} }
} }

View File

@ -13,6 +13,7 @@ namespace VpSharp.Entities;
public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar> public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
{ {
private readonly VirtualParadiseClient _client; private readonly VirtualParadiseClient _client;
private VirtualParadiseUser? _user;
internal VirtualParadiseAvatar(VirtualParadiseClient client, int session) internal VirtualParadiseAvatar(VirtualParadiseClient client, int session)
{ {
@ -60,10 +61,10 @@ public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
public int Type { get; internal set; } public int Type { get; internal set; }
/// <summary> /// <summary>
/// Gets the user associated with this avatar. /// Gets the user ID associated with this avatar.
/// </summary> /// </summary>
/// <value>The user.</value> /// <value>The user ID.</value>
public VirtualParadiseUser User { get; internal set; } = null!; public int UserId { get; internal set; }
/// <summary> /// <summary>
/// Determines if two <see cref="VirtualParadiseAvatar" /> instances are equal. /// Determines if two <see cref="VirtualParadiseAvatar" /> instances are equal.
@ -150,7 +151,7 @@ public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
return true; return true;
} }
return Session == other.Session && User.Equals(other.User); return Session == other.Session && UserId.Equals(other.UserId);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -163,10 +164,20 @@ public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
public override int GetHashCode() public override int GetHashCode()
{ {
// ReSharper disable NonReadonlyMemberInGetHashCode // ReSharper disable NonReadonlyMemberInGetHashCode
return HashCode.Combine(Session, User); return HashCode.Combine(Session, UserId);
// ReSharper restore NonReadonlyMemberInGetHashCode // ReSharper restore NonReadonlyMemberInGetHashCode
} }
/// <summary>
/// Gets the user associated with this avatar.
/// </summary>
/// <returns>The user.</returns>
public async Task<VirtualParadiseUser> GetUserAsync()
{
_user ??= await _client.GetUserAsync(UserId).ConfigureAwait(false);
return _user;
}
/// <summary> /// <summary>
/// Sends a console message to the avatar with no name. /// Sends a console message to the avatar with no name.
/// </summary> /// </summary>
@ -469,6 +480,6 @@ public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
/// <inheritdoc /> /// <inheritdoc />
public override string ToString() public override string ToString()
{ {
return $"Avatar #{Session}; {User.Name} ({Name})"; return $"Avatar #{Session}; User #{UserId} ({Name})";
} }
} }

View File

@ -36,7 +36,7 @@ public sealed partial class VirtualParadiseClient
existing.Location = avatar.Location; existing.Location = avatar.Location;
existing.Application = avatar.Application; existing.Application = avatar.Application;
existing.Type = avatar.Type; existing.Type = avatar.Type;
existing.User = avatar.User; existing.UserId = avatar.UserId;
return existing; return existing;
}); });
} }
@ -62,7 +62,8 @@ public sealed partial class VirtualParadiseClient
{ {
Name = vp_string(sender, StringAttribute.AvatarName), Name = vp_string(sender, StringAttribute.AvatarName),
Location = new Location(CurrentWorld!, position, rotation), Location = new Location(CurrentWorld!, position, rotation),
Application = new Application(applicationName, applicationVersion) Application = new Application(applicationName, applicationVersion),
UserId = vp_int(sender, IntegerAttribute.UserId)
}; };
} }
} }

View File

@ -89,7 +89,6 @@ public sealed partial class VirtualParadiseClient
private void OnAvatarAddNativeEvent(nint sender) private void OnAvatarAddNativeEvent(nint sender)
{ {
VirtualParadiseAvatar avatar = ExtractAvatar(sender); VirtualParadiseAvatar avatar = ExtractAvatar(sender);
avatar.User = GetUserAsync(vp_int(sender, IntegerAttribute.UserId)).ConfigureAwait(false).GetAwaiter().GetResult();
avatar = AddOrUpdateAvatar(avatar); avatar = AddOrUpdateAvatar(avatar);
_avatarJoined.OnNext(avatar); _avatarJoined.OnNext(avatar);
} }

View File

@ -408,7 +408,7 @@ public sealed partial class VirtualParadiseClient : IDisposable
Application = _configuration.Application!, Application = _configuration.Application!,
Name = $"[{_configuration.BotName}]", Name = $"[{_configuration.BotName}]",
Location = new Location(world, Vector3d.Zero, Rotation.None), Location = new Location(world, Vector3d.Zero, Rotation.None),
User = CurrentUser! UserId = CurrentUser!.Id
}; };
if (CurrentWorld is not null) if (CurrentWorld is not null)