Skip to content

Commit

Permalink
Ftlkeys (#4)
Browse files Browse the repository at this point in the history
* ftl-key-base

* fix-tag-adding-bug

* maps-update

* fix-bugs

* fixes

* Update "Prison" ftlkey name
  • Loading branch information
Fragoler authored Mar 30, 2024
1 parent 7ef87d9 commit 9726cfa
Show file tree
Hide file tree
Showing 87 changed files with 3,543,697 additions and 24 deletions.
68 changes: 68 additions & 0 deletions Content.Server/Exodus/FTLKeys/Commands/SetFTLWhitelist.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Linq;
using Content.Server.Administration;
using Content.Server.Shuttles.Systems;
using Content.Shared.Administration;
using Content.Shared.Whitelist;
using Robust.Shared.Console;
using Robust.Shared.Map;

namespace Content.Server.Exodus.FTLKey.Commands
{
[AdminCommand(AdminFlags.Admin)]
public sealed class SetFTLWhiteList : IConsoleCommand
{
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IEntityManager _entity = default!;
public string Command => "setftlwhitelist";
public string Description => "Create White List for current FTL point";
public string Help => "setftlwhitelist <Map Uid> <FTL Tag 1> <...>";

public void Execute(IConsoleShell shell, string argStr, string[] args)
{
var _shuttle = _entity.System<ShuttleSystem>();

if (shell.Player is not { } player)
{
shell.WriteLine("shell-server-cannot");
return;
}

if (args.Length == 0)
{
shell.WriteLine(Loc.GetString("shell-wrong-arguments-number"));
return;
}

if (!int.TryParse(args[0], out var map))
{
shell.WriteLine(Loc.GetString("shell-argument-must-be-number"));
return;
}

var mapId = new MapId(map);

if (!_mapManager.MapExists(mapId))
{
shell.WriteLine(Loc.GetString("shell-invalid-map-id"));
return;
}

if (args.Length == 1)
{
if (_shuttle.TryAddFTLDestination(mapId, true, out var ftl))
_shuttle.SetFTLWhitelist((_mapManager.GetMapEntityId(mapId), ftl), null);
return;
}

if (_shuttle.TryAddFTLDestination(mapId, true, out var ftlComp))
{
var whitelist = new EntityWhitelist()
{
Tags = new List<string>(args.Skip(1))
};

_shuttle.SetFTLWhitelist((_mapManager.GetMapEntityId(mapId), ftlComp), whitelist);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Content.Shared.Containers.ItemSlots;

namespace Content.Server.Exodus.FTLKey;

[RegisterComponent]
public sealed partial class FTLAccessConsoleComponent : Component
{

[DataField("slots")]
public Dictionary<string, ItemSlot> Slots = [];
}
8 changes: 8 additions & 0 deletions Content.Server/Exodus/FTLKeys/Components/FTLKeyComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Content.Server.Exodus.FTLKey;

[RegisterComponent]
public sealed partial class FTLKeyComponent : Component
{
[DataField("access"), ViewVariables(VVAccess.ReadWrite)]
public List<string>? FTLKeys = [];
}
17 changes: 17 additions & 0 deletions Content.Server/Exodus/FTLKeys/Components/FTLMarkerComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Content.Shared.Whitelist;

namespace Content.Server.Exodus.FTLKey;

/// <summary>
/// Give grid FTLDestination component with settings
/// </summary>

[RegisterComponent]
public sealed partial class FTLMarkerComponent : Component
{
[DataField("enabled"), ViewVariables(VVAccess.ReadWrite)]
public bool Enabled = true;

[DataField("whitelist"), ViewVariables(VVAccess.ReadWrite)]
public EntityWhitelist Whitelist = new();
}
151 changes: 151 additions & 0 deletions Content.Server/Exodus/FTLKeys/FTLAccessConsoleSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
using Content.Shared.Containers.ItemSlots;
using Robust.Shared.Containers;
using Content.Shared.Tag;
using Content.Server.Shuttles.Systems;
using Content.Shared.Lock;

namespace Content.Server.Exodus.FTLKey
{
public sealed class FTLAccessConsoleSystem : EntitySystem
{
[Dependency] private readonly ItemSlotsSystem _itemSlotsSystem = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly TagSystem _tagSystem = default!;
[Dependency] private readonly ShuttleConsoleSystem _shuttleConsoleSystem = default!;

public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<FTLAccessConsoleComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<FTLAccessConsoleComponent, ComponentRemove>(OnComponentRemove);

SubscribeLocalEvent<FTLAccessConsoleComponent, EntInsertedIntoContainerMessage>(OnItemInserted);
SubscribeLocalEvent<FTLAccessConsoleComponent, EntRemovedFromContainerMessage>(OnItemRemoved);
SubscribeLocalEvent<FTLAccessConsoleComponent, AnchorStateChangedEvent>(OnAnchorChange);

//Locking
SubscribeLocalEvent<FTLAccessConsoleComponent, LockToggledEvent>(OnLockToggled);
}

private void OnComponentInit(EntityUid uid, FTLAccessConsoleComponent consl, ComponentInit args)
{
foreach (var slot in consl.Slots)
{
_itemSlotsSystem.AddItemSlot(uid, slot.Key, slot.Value);
}

UpdateAccess(uid, consl);
}

private void OnComponentRemove(EntityUid uid, FTLAccessConsoleComponent consl, ComponentRemove args)
{
RemoveAccess(uid, consl);

foreach (var slot in consl.Slots.Values)
{
_itemSlotsSystem.TryEject(uid, slot, null, out var _);
_itemSlotsSystem.RemoveItemSlot(uid, slot);
}
}

private void OnItemInserted(EntityUid uid, FTLAccessConsoleComponent consl, EntInsertedIntoContainerMessage args)
{
var xform = Transform(uid);
if (!xform.Anchored) return;

UpdateAccess(uid, consl);
}

private void OnItemRemoved(EntityUid uid, FTLAccessConsoleComponent consl, EntRemovedFromContainerMessage args)
{
var xform = Transform(uid);
if (!xform.Anchored) return;

RemoveCurrentAccess(uid, args.Entity);
UpdateAccess(uid, consl);
}

private void OnAnchorChange(EntityUid uid, FTLAccessConsoleComponent consl, AnchorStateChangedEvent args)
{
if (args.Anchored) UpdateAccess(uid, consl);
else RemoveAccess(uid, consl);
}

private void OnLockToggled(EntityUid uid, FTLAccessConsoleComponent consl, LockToggledEvent args)
{
if (args.Locked)
{
foreach (var slot in consl.Slots.Values)
{
_itemSlotsSystem.SetLock(uid, slot, true);
}
}
else
{
foreach (var slot in consl.Slots.Values)
{
_itemSlotsSystem.SetLock(uid, slot, false);
}
}
}

/// <summary>
/// Sets Access from inserted Keys
/// </summary>
private void UpdateAccess(EntityUid uid, FTLAccessConsoleComponent consl)
{
foreach (var slot in consl.Slots.Values)
{
if (slot.ContainerSlot is not null && slot.ContainerSlot.ContainedEntity is not null)
AddCurrentAccess(uid, slot.ContainerSlot.ContainedEntity.Value);
}

UpdateConsole(uid);
}

/// <summary>
/// Remove access of all inserted keys
/// </summary>

private void RemoveAccess(EntityUid uid, FTLAccessConsoleComponent consl)
{
foreach (var slot in consl.Slots.Values)
{
if (slot.ContainerSlot is not null && slot.ContainerSlot.ContainedEntity is not null)
RemoveCurrentAccess(uid, slot.ContainerSlot.ContainedEntity.Value);
}

UpdateConsole(uid);
}

/// <summary>
/// Add access of current Key
/// </summary>
private void AddCurrentAccess(EntityUid uid, EntityUid added)
{
var xform = Transform(uid);
if (xform.GridUid is null) return;

if (!TryComp<FTLKeyComponent>(added, out var keyComp) || keyComp.FTLKeys is null) return;
_tagSystem.AddTags(xform.GridUid.Value, keyComp.FTLKeys);
}

/// <summary>
/// Remove access of current Key
/// </summary>
private void RemoveCurrentAccess(EntityUid uid, EntityUid removed)
{
var xform = Transform(uid);
if (xform.GridUid is null) return;

if (!TryComp<FTLKeyComponent>(removed, out var keyComp) || keyComp.FTLKeys is null) return;
_tagSystem.RemoveTags(xform.GridUid.Value, keyComp.FTLKeys);
}

private void UpdateConsole(EntityUid uid)
{
_shuttleConsoleSystem.RefreshShuttleConsoles(uid);
}
}
}
26 changes: 26 additions & 0 deletions Content.Server/Exodus/FTLKeys/FTLMarkerSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Content.Server.Shuttles.Systems;
using Content.Shared.Shuttles.Components;

namespace Content.Server.Exodus.FTLKey;

public class FTLMarkerSystem : EntitySystem
{
[Dependency] private readonly ShuttleSystem _shuttle = default!;

public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<FTLMarkerComponent, ComponentInit>(OnComponentInit);
}

private void OnComponentInit(EntityUid uid, FTLMarkerComponent component, ComponentInit args)
{
var xform = Transform(uid);

if (_shuttle.TryAddFTLDestination(xform.MapID, component.Enabled, out var ftlComp))
{
_shuttle.SetFTLWhitelist((xform.MapUid!.Value, ftlComp), component.Whitelist);
}
}
}
47 changes: 47 additions & 0 deletions Content.Server/Exodus/Shuttle/MapSpawnComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;

namespace Content.Server.Shuttles.Systems;

/// <summary>
/// Similar to <see cref="MapSpawnComponent"/> except spawns the grid on new map
/// </summary>
[RegisterComponent, Access(typeof(ShuttleSystem))]
public sealed partial class MapSpawnComponent : Component
{
/// <summary>
/// Dictionary of groups where each group will have entries selected.
/// String is just an identifier to make yaml easier.
/// </summary>
[DataField("maps", required: true)] public List<MapSpawnGroup> Groups = [];
}

[DataRecord]
public record struct MapSpawnGroup
{
public string MapName = "map";

public List<ResPath> Paths = [];

public int MinCount = 1;

public int MaxCount = 1;

/// <summary>
/// Components to be added to map.
/// </summary>
public ComponentRegistry AddComponents = new();

/// <summary>
/// Hide the IFF label of the grid.
/// </summary>
public bool Hide = false;

/// <summary>
/// Should we set the metadata name of a grid. Useful for admin purposes.
/// </summary>

public MapSpawnGroup()
{
}
}
Loading

0 comments on commit 9726cfa

Please sign in to comment.