85 lines
2.3 KiB
C#
85 lines
2.3 KiB
C#
using System.Collections.Generic;
|
|
using CommandHook;
|
|
using Vintagestory.API.Common;
|
|
using Vintagestory.API.Server;
|
|
|
|
namespace ClaimLink;
|
|
|
|
public class ClaimLinkModSystem : ModSystem
|
|
{
|
|
internal static ILandClaimAPI LandClaimAPI = null!;
|
|
internal static ILogger Logger = null!;
|
|
internal static ClaimLinkRegistry Registry = null!;
|
|
internal static IGroupManager Groups = null!;
|
|
internal static IWorldAccessor World = null!;
|
|
internal ClaimLinkCommandListener? cmdListener;
|
|
|
|
public override bool ShouldLoad(EnumAppSide forSide) => forSide == EnumAppSide.Server;
|
|
|
|
public override void StartServerSide(ICoreServerAPI api)
|
|
{
|
|
LandClaimAPI = api.World.Claims;
|
|
Logger = api.Logger;
|
|
Registry = new ClaimLinkRegistry(api.WorldManager.SaveGame);
|
|
Groups = api.Groups;
|
|
World = api.World;
|
|
|
|
cmdListener = new ClaimLinkCommandListener();
|
|
CommandHookModSystem.Register(cmdListener);
|
|
ClaimLinkChatCommand.Register(api);
|
|
|
|
api.Event.PlayerDisconnect += ClaimLinkCommandListener.OnPlayerDisconnect;
|
|
}
|
|
|
|
public override void Dispose()
|
|
{
|
|
if (cmdListener != null)
|
|
CommandHookModSystem.Unregister(cmdListener);
|
|
}
|
|
|
|
private static IEnumerable<(int claimIndex, LandClaim claim)> EnumerateOwnedClaims(
|
|
string ownerPlayerUid
|
|
)
|
|
{
|
|
int index = 0;
|
|
foreach (LandClaim c in LandClaimAPI.All)
|
|
{
|
|
if (c.OwnedByPlayerUid != ownerPlayerUid)
|
|
continue;
|
|
|
|
yield return (index, c);
|
|
index++;
|
|
}
|
|
}
|
|
|
|
internal static bool TryResolveOwnedClaim(string ownerPlayerUid, int claimIndex, out LandClaim? claim)
|
|
{
|
|
foreach ((int index, LandClaim c) in EnumerateOwnedClaims(ownerPlayerUid))
|
|
{
|
|
if (index == claimIndex)
|
|
{
|
|
claim = c;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
claim = null;
|
|
return false;
|
|
}
|
|
|
|
internal static bool TryResolveClaimIndex(string ownerPlayerUid, LandClaim target, out int claimIndex)
|
|
{
|
|
foreach ((int index, LandClaim c) in EnumerateOwnedClaims(ownerPlayerUid))
|
|
{
|
|
if (ReferenceEquals(c, target))
|
|
{
|
|
claimIndex = index;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
claimIndex = -1;
|
|
return false;
|
|
}
|
|
}
|