fuck this

This commit is contained in:
Ed
2025-05-22 21:39:36 +03:00
parent 9a0905c53e
commit 066379e8ca
9 changed files with 182 additions and 69 deletions

View File

@@ -0,0 +1,7 @@
using Content.Shared._CP14.Workplace;
namespace Content.Client._CP14.Workplace;
public sealed partial class CP14ClientWorkplaceSystem : CP14SharedWorkplaceSystem
{
}

View File

@@ -14,12 +14,16 @@ public sealed partial class CP14WorkplaceWindow : DefaultWindow
[Dependency] private readonly IPrototypeManager _prototype = default!;
[Dependency] private readonly ILogManager _log = default!;
[Dependency] private readonly IEntityManager _e = default!;
private readonly CP14ClientWorkplaceSystem _workplace = default!;
public event Action<CP14WorkplaceRecipeEntry>? OnCraft;
private readonly Dictionary<int, LocId> _categories = new();
private CP14WorkplaceState? _cachedState;
private EntityUid? user;
private EntityUid? workplace;
private CP14WorkplaceRecipeEntry? _selectedEntry;
private string _searchFilter = string.Empty;
@@ -30,6 +34,7 @@ public sealed partial class CP14WorkplaceWindow : DefaultWindow
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
_workplace = _e.System<CP14ClientWorkplaceSystem>();
Sawmill = _log.GetSawmill("cp14_workplace_window");
SearchBar.OnTextChanged += OnSearchChanged;
@@ -77,6 +82,8 @@ public sealed partial class CP14WorkplaceWindow : DefaultWindow
public void UpdateState(CP14WorkplaceState state)
{
_cachedState = state;
user = _e.GetEntity(state.User);
workplace = _e.GetEntity(state.Workplace);
_categories.Clear();
OptionCategories.Clear();
@@ -178,7 +185,7 @@ public sealed partial class CP14WorkplaceWindow : DefaultWindow
// ItemRequirements.AddChild(new CP14WorkbenchRequirementControl(requirement));
//}
CraftButton.Disabled = !entry.Craftable;
CraftButton.Disabled = _workplace.CheckCraftable(entry.Recipe, workplace, user);
}
private void RecipeSelectNull()

View File

@@ -20,7 +20,7 @@ using Robust.Shared.Random;
namespace Content.Server._CP14.Workbench;
public sealed partial class CP14WorkbenchSystem : Shared._CP14.Workplace.CP14SharedWorkbenchSystem
public sealed partial class CP14WorkbenchSystem : CP14SharedWorkbenchSystem
{
[Dependency] private readonly AudioSystem _audio = default!;
[Dependency] private readonly EntityLookupSystem _lookup = default!;

View File

@@ -1,72 +1,12 @@
using Content.Shared._CP14.Workplace;
using Content.Shared.UserInterface;
using Robust.Server.GameObjects;
using Robust.Shared.Prototypes;
namespace Content.Server._CP14.Workplace;
public sealed partial class CP14WorkplaceSystem : CP14SharedWorkbenchSystem
public sealed partial class CP14WorkplaceSystem : CP14SharedWorkplaceSystem
{
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly UserInterfaceSystem _userInterface = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnPrototypeReload);
SubscribeLocalEvent<CP14WorkplaceComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<CP14WorkplaceComponent, BeforeActivatableUIOpenEvent>(OnBeforeUIOpen);
}
private void OnPrototypeReload(PrototypesReloadedEventArgs ev)
{
if (!ev.WasModified<EntityPrototype>())
return;
var query = EntityQueryEnumerator<CP14WorkplaceComponent>();
while (query.MoveNext(out var uid, out var workplace))
{
CacheWorkplaceRecipes((uid, workplace));
}
}
private void OnMapInit(Entity<CP14WorkplaceComponent> ent, ref MapInitEvent args)
{
CacheWorkplaceRecipes(ent);
}
private void OnBeforeUIOpen(Entity<CP14WorkplaceComponent> ent, ref BeforeActivatableUIOpenEvent args)
{
UpdateUIState(ent, args.User);
}
private void UpdateUIState(Entity<CP14WorkplaceComponent> entity, EntityUid user)
{
var recipes = new List<CP14WorkplaceRecipeEntry>();
foreach (var recipe in entity.Comp.CachedRecipes)
{
var entry = new CP14WorkplaceRecipeEntry(recipe, true);
recipes.Add(entry);
}
_userInterface.SetUiState(entity.Owner, CP14WorkplaceUiKey.Key, new CP14WorkplaceState(recipes));
}
private void CacheWorkplaceRecipes(Entity<CP14WorkplaceComponent> entity)
{
entity.Comp.CachedRecipes.Clear();
var allEnts = _proto.EnumeratePrototypes<EntityPrototype>();
foreach (var recipe in allEnts)
{
if (!recipe.Components.TryGetComponent(CP14WorkplaceRecipeComponent.CompName, out var compData) || compData is not CP14WorkplaceRecipeComponent recipeComp)
continue;
if (!entity.Comp.Tags.Contains(recipeComp.Tag))
continue;
entity.Comp.CachedRecipes.Add(recipe);
}
}
}

View File

@@ -1,5 +1,148 @@
using Content.Shared.UserInterface;
using Robust.Shared.Prototypes;
namespace Content.Shared._CP14.Workplace;
public abstract partial class CP14SharedWorkbenchSystem : EntitySystem
public abstract partial class CP14SharedWorkplaceSystem : EntitySystem
{
[Dependency] private readonly SharedUserInterfaceSystem _userInterface = default!;
[Dependency] private readonly IPrototypeManager _proto = default!;
/// <summary>
/// All recipes are stored here in the dictionary.
/// These are by design readonly entities for which events are called to collect information on them.
/// </summary>
private Dictionary<EntProtoId, Entity<CP14WorkplaceRecipeComponent>> _cachedRecipes = new();
public override void Initialize()
{
base.Initialize();
CacheAllRecipes();
SubscribeLocalEvent<CP14WorkplaceComponent, BeforeActivatableUIOpenEvent>(OnBeforeUIOpen);
SubscribeLocalEvent<CP14WorkplaceComponent, CP14WorkplaceCraftMessage>(OnCraftAttempt);
SubscribeLocalEvent<CP14WorkplaceComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnPrototypeReload);
}
private void OnBeforeUIOpen(Entity<CP14WorkplaceComponent> ent, ref BeforeActivatableUIOpenEvent args)
{
UpdateUIState(ent, args.User);
}
private void OnCraftAttempt(Entity<CP14WorkplaceComponent> ent, ref CP14WorkplaceCraftMessage args)
{
if (!_cachedRecipes.TryGetValue(args.Recipe, out var cachedRecipe))
return;
if (!ent.Comp.CachedRecipes.Contains(cachedRecipe))
return;
}
private void OnMapInit(Entity<CP14WorkplaceComponent> ent, ref MapInitEvent args)
{
CacheWorkplaceRecipes(ent);
}
private void OnPrototypeReload(PrototypesReloadedEventArgs ev)
{
if (!ev.WasModified<EntityPrototype>())
return;
CacheAllRecipes();
var query = EntityQueryEnumerator<CP14WorkplaceComponent>();
while (query.MoveNext(out var uid, out var workplace))
{
CacheWorkplaceRecipes((uid, workplace));
}
}
private void UpdateUIState(Entity<CP14WorkplaceComponent> entity, EntityUid user)
{
var recipes = new List<CP14WorkplaceRecipeEntry>();
foreach (var recipe in entity.Comp.CachedRecipes)
{
var proto = MetaData(recipe).EntityPrototype;
if (proto is null)
continue;
var entry = new CP14WorkplaceRecipeEntry(proto);
recipes.Add(entry);
}
_userInterface.SetUiState(entity.Owner, CP14WorkplaceUiKey.Key, new CP14WorkplaceState(GetNetEntity(user), GetNetEntity(entity), recipes));
}
public bool CheckCraftable(EntProtoId recipe, EntityUid? workplace, EntityUid? user)
{
if (!TryComp<CP14WorkplaceComponent>(workplace, out var workplaceComp))
return false;
if (!_cachedRecipes.TryGetValue(recipe, out var cachedRecipe))
return false;
return CheckCraftable(cachedRecipe, workplace, user);
}
public bool CheckCraftable(EntityUid recipe, EntityUid? workplace, EntityUid? user)
{
if (user is null || workplace is null)
return false;
var ev = new CP14WorkplaceRequirementsPass(user.Value, workplace.Value, recipe);
RaiseLocalEvent(recipe, ev);
if (ev.Cancelled)
return false;
return true;
}
private void CacheAllRecipes()
{
//Delete all old cached recipes entity
foreach (var recipe in _cachedRecipes.Values)
{
QueueDel(recipe);
}
var allEnts = _proto.EnumeratePrototypes<EntityPrototype>();
foreach (var recipe in allEnts)
{
if (!recipe.Components.TryGetComponent(CP14WorkplaceRecipeComponent.CompName, out var compData) || compData is not CP14WorkplaceRecipeComponent recipeComp)
continue;
if (_cachedRecipes.ContainsKey(recipe.ID))
continue;
var ent = Spawn(recipe.ID);
var entComp = EnsureComp<CP14WorkplaceRecipeComponent>(ent);
_cachedRecipes.Add(recipe.ID, (ent, entComp));
}
}
private void CacheWorkplaceRecipes(Entity<CP14WorkplaceComponent> entity)
{
entity.Comp.CachedRecipes.Clear();
foreach (var recipe in _cachedRecipes.Values)
{
if (!entity.Comp.Tags.Contains(recipe.Comp.Tag))
continue;
entity.Comp.CachedRecipes.Add(recipe);
}
}
}
public sealed class CP14WorkplaceRequirementsPass(EntityUid user, EntityUid workplace, EntityUid recipe)
: CancellableEntityEventArgs
{
public EntityUid User { get; } = user;
public EntityUid Workplace { get; } = workplace;
public EntityUid Recipe { get; } = recipe;
}

View File

@@ -20,5 +20,5 @@ public sealed partial class CP14WorkplaceComponent : Component
/// Cached when initializing a workstation or reloading prototypes
/// </summary>
[DataField]
public HashSet<EntProtoId> CachedRecipes = new();
public HashSet<EntityUid> CachedRecipes = new();
}

View File

@@ -16,13 +16,14 @@ public sealed class CP14WorkplaceCraftMessage(EntProtoId recipe) : BoundUserInte
}
[Serializable, NetSerializable]
public sealed class CP14WorkplaceState(List<CP14WorkplaceRecipeEntry> recipes) : BoundUserInterfaceState
public sealed class CP14WorkplaceState(NetEntity workplace, NetEntity user, List<CP14WorkplaceRecipeEntry> recipes) : BoundUserInterfaceState
{
public readonly NetEntity User = user;
public readonly NetEntity Workplace = workplace;
public readonly List<CP14WorkplaceRecipeEntry> Recipes = recipes;
}
[Serializable, NetSerializable]
public readonly record struct CP14WorkplaceRecipeEntry(
EntProtoId Recipe,
bool Craftable
EntProtoId Recipe
);

View File

@@ -26,4 +26,17 @@
- type: UserInterface
interfaces:
enum.CP14WorkplaceUiKey.Key:
type: CP14WorkplaceBoundUserInterface
type: CP14WorkplaceBoundUserInterface
- type: entity
parent: CP14BaseWorkplace
id: CP14BaseWorkplace2
name: workplace 2
components:
- type: Sprite
snapCardinals: true
sprite: _CP14/Structures/Furniture/workbench.rsi
state: cooking_table
- type: CP14Workplace
tags:
- CP14RecipeWorkbench

View File

@@ -67,6 +67,7 @@
- CP14ActionSpellBloodlust
components:
- type: CP14WorkplaceRecipe
tag: CP14RecipeWorkbench
category: cp14-recipe-category-tile
- type: entity
@@ -76,4 +77,5 @@
- CP14Lighter
components:
- type: CP14WorkplaceRecipe
tag: CP14RecipeWorkbench
category: cp14-recipe-category-tile