From 066379e8ca25523ea904978e4427bb6916fcdfcc Mon Sep 17 00:00:00 2001 From: Ed Date: Thu, 22 May 2025 21:39:36 +0300 Subject: [PATCH] fuck this --- .../Workplace/CP14ClientWorkplaceSystem.cs | 7 + .../Workplace/CP14WorkplaceWindow.xaml.cs | 9 +- .../_CP14/Workbench/CP14WorkbenchSystem.cs | 2 +- .../_CP14/Workplace/CP14WorkplaceSystem.cs | 62 +------- .../Workplace/CP14SharedWorkplaceSystem.cs | 145 +++++++++++++++++- .../_CP14/Workplace/CP14WorkplaceComponent.cs | 2 +- .../_CP14/Workplace/CP14WorkplaceUI.cs | 7 +- .../Structures/Furniture/workplaces.yml | 15 +- .../_CP14/Recipes/Workplace/debug.yml | 2 + 9 files changed, 182 insertions(+), 69 deletions(-) create mode 100644 Content.Client/_CP14/Workplace/CP14ClientWorkplaceSystem.cs diff --git a/Content.Client/_CP14/Workplace/CP14ClientWorkplaceSystem.cs b/Content.Client/_CP14/Workplace/CP14ClientWorkplaceSystem.cs new file mode 100644 index 0000000000..e0a0cc35ea --- /dev/null +++ b/Content.Client/_CP14/Workplace/CP14ClientWorkplaceSystem.cs @@ -0,0 +1,7 @@ +using Content.Shared._CP14.Workplace; + +namespace Content.Client._CP14.Workplace; + +public sealed partial class CP14ClientWorkplaceSystem : CP14SharedWorkplaceSystem +{ +} diff --git a/Content.Client/_CP14/Workplace/CP14WorkplaceWindow.xaml.cs b/Content.Client/_CP14/Workplace/CP14WorkplaceWindow.xaml.cs index 604cbb0bd4..7ba53b4b12 100644 --- a/Content.Client/_CP14/Workplace/CP14WorkplaceWindow.xaml.cs +++ b/Content.Client/_CP14/Workplace/CP14WorkplaceWindow.xaml.cs @@ -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? OnCraft; private readonly Dictionary _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(); 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() diff --git a/Content.Server/_CP14/Workbench/CP14WorkbenchSystem.cs b/Content.Server/_CP14/Workbench/CP14WorkbenchSystem.cs index 0b7fdf3b44..4ccc6dc517 100644 --- a/Content.Server/_CP14/Workbench/CP14WorkbenchSystem.cs +++ b/Content.Server/_CP14/Workbench/CP14WorkbenchSystem.cs @@ -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!; diff --git a/Content.Server/_CP14/Workplace/CP14WorkplaceSystem.cs b/Content.Server/_CP14/Workplace/CP14WorkplaceSystem.cs index 073c97ce38..1807ff6c1d 100644 --- a/Content.Server/_CP14/Workplace/CP14WorkplaceSystem.cs +++ b/Content.Server/_CP14/Workplace/CP14WorkplaceSystem.cs @@ -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(OnPrototypeReload); - SubscribeLocalEvent(OnMapInit); - SubscribeLocalEvent(OnBeforeUIOpen); - } - - private void OnPrototypeReload(PrototypesReloadedEventArgs ev) - { - if (!ev.WasModified()) - return; - - var query = EntityQueryEnumerator(); - while (query.MoveNext(out var uid, out var workplace)) - { - CacheWorkplaceRecipes((uid, workplace)); - } - } - - private void OnMapInit(Entity ent, ref MapInitEvent args) - { - CacheWorkplaceRecipes(ent); - } - - private void OnBeforeUIOpen(Entity ent, ref BeforeActivatableUIOpenEvent args) - { - UpdateUIState(ent, args.User); - } - - private void UpdateUIState(Entity entity, EntityUid user) - { - var recipes = new List(); - 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 entity) - { - entity.Comp.CachedRecipes.Clear(); - - var allEnts = _proto.EnumeratePrototypes(); - - 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); - } } } diff --git a/Content.Shared/_CP14/Workplace/CP14SharedWorkplaceSystem.cs b/Content.Shared/_CP14/Workplace/CP14SharedWorkplaceSystem.cs index 65fadc70d7..99d27d807e 100644 --- a/Content.Shared/_CP14/Workplace/CP14SharedWorkplaceSystem.cs +++ b/Content.Shared/_CP14/Workplace/CP14SharedWorkplaceSystem.cs @@ -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!; + + /// + /// All recipes are stored here in the dictionary. + /// These are by design readonly entities for which events are called to collect information on them. + /// + private Dictionary> _cachedRecipes = new(); + + public override void Initialize() + { + base.Initialize(); + + CacheAllRecipes(); + + SubscribeLocalEvent(OnBeforeUIOpen); + SubscribeLocalEvent(OnCraftAttempt); + + SubscribeLocalEvent(OnMapInit); + + SubscribeLocalEvent(OnPrototypeReload); + } + + private void OnBeforeUIOpen(Entity ent, ref BeforeActivatableUIOpenEvent args) + { + UpdateUIState(ent, args.User); + } + + private void OnCraftAttempt(Entity ent, ref CP14WorkplaceCraftMessage args) + { + if (!_cachedRecipes.TryGetValue(args.Recipe, out var cachedRecipe)) + return; + + if (!ent.Comp.CachedRecipes.Contains(cachedRecipe)) + return; + } + + private void OnMapInit(Entity ent, ref MapInitEvent args) + { + CacheWorkplaceRecipes(ent); + } + + private void OnPrototypeReload(PrototypesReloadedEventArgs ev) + { + if (!ev.WasModified()) + return; + + CacheAllRecipes(); + + var query = EntityQueryEnumerator(); + while (query.MoveNext(out var uid, out var workplace)) + { + CacheWorkplaceRecipes((uid, workplace)); + } + } + + private void UpdateUIState(Entity entity, EntityUid user) + { + var recipes = new List(); + 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(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(); + + 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(ent); + + _cachedRecipes.Add(recipe.ID, (ent, entComp)); + } + } + + private void CacheWorkplaceRecipes(Entity 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; } diff --git a/Content.Shared/_CP14/Workplace/CP14WorkplaceComponent.cs b/Content.Shared/_CP14/Workplace/CP14WorkplaceComponent.cs index fbd4186535..690718b256 100644 --- a/Content.Shared/_CP14/Workplace/CP14WorkplaceComponent.cs +++ b/Content.Shared/_CP14/Workplace/CP14WorkplaceComponent.cs @@ -20,5 +20,5 @@ public sealed partial class CP14WorkplaceComponent : Component /// Cached when initializing a workstation or reloading prototypes /// [DataField] - public HashSet CachedRecipes = new(); + public HashSet CachedRecipes = new(); } diff --git a/Content.Shared/_CP14/Workplace/CP14WorkplaceUI.cs b/Content.Shared/_CP14/Workplace/CP14WorkplaceUI.cs index 745fb62d36..d318c8ca20 100644 --- a/Content.Shared/_CP14/Workplace/CP14WorkplaceUI.cs +++ b/Content.Shared/_CP14/Workplace/CP14WorkplaceUI.cs @@ -16,13 +16,14 @@ public sealed class CP14WorkplaceCraftMessage(EntProtoId recipe) : BoundUserInte } [Serializable, NetSerializable] -public sealed class CP14WorkplaceState(List recipes) : BoundUserInterfaceState +public sealed class CP14WorkplaceState(NetEntity workplace, NetEntity user, List recipes) : BoundUserInterfaceState { + public readonly NetEntity User = user; + public readonly NetEntity Workplace = workplace; public readonly List Recipes = recipes; } [Serializable, NetSerializable] public readonly record struct CP14WorkplaceRecipeEntry( - EntProtoId Recipe, - bool Craftable + EntProtoId Recipe ); diff --git a/Resources/Prototypes/_CP14/Entities/Structures/Furniture/workplaces.yml b/Resources/Prototypes/_CP14/Entities/Structures/Furniture/workplaces.yml index ad94af3625..2c8d430ba2 100644 --- a/Resources/Prototypes/_CP14/Entities/Structures/Furniture/workplaces.yml +++ b/Resources/Prototypes/_CP14/Entities/Structures/Furniture/workplaces.yml @@ -26,4 +26,17 @@ - type: UserInterface interfaces: enum.CP14WorkplaceUiKey.Key: - type: CP14WorkplaceBoundUserInterface \ No newline at end of file + 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 \ No newline at end of file diff --git a/Resources/Prototypes/_CP14/Recipes/Workplace/debug.yml b/Resources/Prototypes/_CP14/Recipes/Workplace/debug.yml index 0c6b53036b..1f5fe6ef30 100644 --- a/Resources/Prototypes/_CP14/Recipes/Workplace/debug.yml +++ b/Resources/Prototypes/_CP14/Recipes/Workplace/debug.yml @@ -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 \ No newline at end of file