Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Rxup committed Feb 14, 2024
1 parent 9b9ac02 commit 7f32820
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Content.Server/Backmen/Loadout/LoadoutItemPrototype.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Content.Shared.Roles;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;

namespace Content.Server.Backmen.Loadout;

[Prototype("loadout")]
public sealed class LoadoutItemPrototype : IPrototype
{
[IdDataField] public string ID { get; } = default!;

[DataField("entity", required: true)]
public ProtoId<EntityPrototype> EntityId { get; } = default!;

// Corvax-Sponsors-Start
[DataField("sponsorOnly")]
public bool SponsorOnly = false;
// Corvax-Sponsors-End

[DataField("whitelistJobs")]
public List<ProtoId<JobPrototype>>? WhitelistJobs { get; }

[DataField("blacklistJobs")]
public List<ProtoId<JobPrototype>>? BlacklistJobs { get; }

[DataField("speciesRestriction")]
public List<string>? SpeciesRestrictions { get; }
}
103 changes: 103 additions & 0 deletions Content.Server/Backmen/Loadout/LoadoutSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using Content.Corvax.Interfaces.Server;
using Content.Server.GameTicking;
using Content.Server.Hands.Systems;
using Content.Server.Storage.EntitySystems;
using Content.Shared.Clothing.Components;
using Content.Shared.Inventory;
using Robust.Shared.Prototypes;

namespace Content.Server.Backmen.Loadout;

public sealed class LoadoutSystem : EntitySystem
{
private const string BackpackSlotId = "back";

[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly InventorySystem _inventorySystem = default!;
[Dependency] private readonly HandsSystem _handsSystem = default!;
[Dependency] private readonly StorageSystem _storageSystem = default!;
[Dependency] private readonly IServerSponsorsManager _sponsorsManager = default!;

public override void Initialize()
{
SubscribeLocalEvent<PlayerSpawnCompleteEvent>(OnPlayerSpawned);
}

private void OnPlayerSpawned(PlayerSpawnCompleteEvent ev)
{
if (!_sponsorsManager.TryGetPrototypes(ev.Player.UserId, out var prototypes))
return;

foreach (var loadoutId in prototypes)
{
// NOTE: Now is easy to not extract method because event give all info we need
if (!_prototypeManager.TryIndex<LoadoutItemPrototype>(loadoutId, out var loadout))
continue;

var isSponsorOnly = loadout.SponsorOnly &&
!prototypes.Contains(loadoutId);
var isWhitelisted = ev.JobId != null &&
loadout.WhitelistJobs != null &&
!loadout.WhitelistJobs.Contains(ev.JobId);
var isBlacklisted = ev.JobId != null &&
loadout.BlacklistJobs != null &&
loadout.BlacklistJobs.Contains(ev.JobId);
var isSpeciesRestricted = loadout.SpeciesRestrictions != null &&
loadout.SpeciesRestrictions.Contains(ev.Profile.Species);

if (isSponsorOnly || isWhitelisted || isBlacklisted || isSpeciesRestricted)
continue;

var entity = Spawn(loadout.EntityId, Transform(ev.Mob).Coordinates);

// Take in hand if not clothes
if (!TryComp<ClothingComponent>(entity, out var clothing))
{
_handsSystem.TryPickup(ev.Mob, entity);
continue;
}

// Automatically search empty slot for clothes to equip
var firstSlotName = (string?)null;
var isEquiped = false;

if (!_inventorySystem.TryGetSlots(ev.Mob, out var slotDefinitions))
return;

foreach (var slot in slotDefinitions)
{
if (!clothing.Slots.HasFlag(slot.SlotFlags))
continue;

firstSlotName ??= slot.Name;

if (_inventorySystem.TryGetSlotEntity(ev.Mob, slot.Name, out var _))
continue;

if (!_inventorySystem.TryEquip(ev.Mob, entity, slot.Name, true))
continue;

isEquiped = true;
break;
}

if (isEquiped || firstSlotName == null)
continue;

// Force equip to first valid clothes slot
// Get occupied entity -> Insert to backpack -> Equip loadout entity
if (_inventorySystem.TryGetSlotEntity(ev.Mob, firstSlotName, out var slotEntity) &&
_inventorySystem.TryGetSlotEntity(ev.Mob, BackpackSlotId, out var backEntity) &&
_storageSystem.CanInsert(backEntity.Value, slotEntity.Value, out _))
{
if(_storageSystem.Insert(backEntity.Value, slotEntity.Value, out _, playSound: false))
continue;
}

if (!_inventorySystem.TryEquip(ev.Mob, entity, firstSlotName, true))
{
QueueDel(entity);
}
}
}
}
10 changes: 10 additions & 0 deletions Content.Server/Corvax/IServerLoadoutManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Diagnostics.CodeAnalysis;
using Content.Corvax.Interfaces.Shared;
using Robust.Shared.Network;

namespace Content.Corvax.Interfaces.Server;

public interface IServerLoadoutManager : ISharedSponsorsManager
{
public bool TryGetPrototypes(NetUserId userId, [NotNullWhen(true)] out List<string>? prototypes);
}
7 changes: 7 additions & 0 deletions Resources/Prototypes/Backmen/Catalog/Loadout/head.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- type: loadout
id: ClothingHeadHatFlowerCrownLoadout
entity: ClothingHeadHatFlowerCrown

- type: loadout
id: ClothingHeadHatHairflowerLoadout
entity: ClothingHeadHatHairflower

0 comments on commit 7f32820

Please sign in to comment.