mirror of
https://github.com/xivdev/Penumbra.git
synced 2025-12-12 18:27:24 +01:00
Move PlayerWatcher to own assembly and make appropriate changes for reuse.
This commit is contained in:
parent
ea40d5bc9c
commit
d99707f77e
9 changed files with 335 additions and 133 deletions
88
Penumbra.PlayerWatch/PlayerWatcher.cs
Normal file
88
Penumbra.PlayerWatch/PlayerWatcher.cs
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
using System;
|
||||
using Dalamud.Game.ClientState.Actors.Types;
|
||||
using Dalamud.Plugin;
|
||||
using Penumbra.GameData.Structs;
|
||||
|
||||
namespace Penumbra.PlayerWatch
|
||||
{
|
||||
public class PlayerWatcher : IDisposable, IPlayerWatcher
|
||||
{
|
||||
public int Version { get; } = 1;
|
||||
|
||||
private static PlayerWatchBase? _playerWatch;
|
||||
|
||||
public event ActorChange? ActorChanged;
|
||||
|
||||
public bool Active { get; set; } = true;
|
||||
|
||||
public bool Valid
|
||||
=> _playerWatch != null;
|
||||
|
||||
internal PlayerWatcher( DalamudPluginInterface pi )
|
||||
{
|
||||
_playerWatch ??= new PlayerWatchBase( pi );
|
||||
_playerWatch.RegisterWatcher( this );
|
||||
}
|
||||
|
||||
public void Enable()
|
||||
=> Active = Valid;
|
||||
|
||||
public void Disable()
|
||||
=> Active = false;
|
||||
|
||||
public void SetStatus( bool enabled )
|
||||
=> Active = enabled && Valid;
|
||||
|
||||
internal void Trigger( Actor actor )
|
||||
=> ActorChanged?.Invoke( actor );
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if( _playerWatch == null )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Active = false;
|
||||
ActorChanged = null;
|
||||
_playerWatch.UnregisterWatcher( this );
|
||||
if( _playerWatch.RegisteredWatchers.Count == 0 )
|
||||
{
|
||||
_playerWatch.Dispose();
|
||||
_playerWatch = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckValidity()
|
||||
{
|
||||
if( !Valid )
|
||||
{
|
||||
throw new Exception( $"PlayerWatch was already disposed." );
|
||||
}
|
||||
}
|
||||
|
||||
public void AddPlayerToWatch( string name )
|
||||
{
|
||||
CheckValidity();
|
||||
_playerWatch!.AddPlayerToWatch( name, this );
|
||||
}
|
||||
|
||||
public void RemovePlayerFromWatch( string playerName )
|
||||
{
|
||||
CheckValidity();
|
||||
_playerWatch!.RemovePlayerFromWatch( playerName, this );
|
||||
}
|
||||
|
||||
public CharEquipment UpdateActorWithoutEvent( Actor actor )
|
||||
{
|
||||
CheckValidity();
|
||||
return _playerWatch!.UpdateActorWithoutEvent( actor );
|
||||
}
|
||||
}
|
||||
|
||||
public static class PlayerWatchFactory
|
||||
{
|
||||
public static IPlayerWatcher Create( DalamudPluginInterface pi )
|
||||
=> new PlayerWatcher( pi );
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue