Чертежи
This commit is contained in:
@@ -70,7 +70,7 @@ namespace Content.Shared.Lathe
|
||||
|
||||
public bool getUnavailable;
|
||||
|
||||
public List<ProtoId<LatheRecipePrototype>> Recipes = new();
|
||||
public HashSet<ProtoId<LatheRecipePrototype>> Recipes = new(); // WD Ahead of wizden
|
||||
|
||||
public LatheGetRecipesEvent(EntityUid lathe, bool forced)
|
||||
{
|
||||
|
||||
19
Content.Shared/Research/Components/BlueprintComponent.cs
Normal file
19
Content.Shared/Research/Components/BlueprintComponent.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Content.Shared.Research.Prototypes;
|
||||
using Content.Shared.Research.Systems;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Shared.Research.Components;
|
||||
|
||||
/// <summary>
|
||||
/// This is used for an item that is inserted directly into a given lathe to provide it with a recipe.
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent, Access(typeof(BlueprintSystem))]
|
||||
public sealed partial class BlueprintComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// The recipes that this blueprint provides.
|
||||
/// </summary>
|
||||
[DataField(required: true)]
|
||||
public HashSet<ProtoId<LatheRecipePrototype>> ProvidedRecipes = new();
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using Content.Shared.Research.Systems;
|
||||
using Content.Shared.Whitelist;
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Shared.Research.Components;
|
||||
|
||||
/// <summary>
|
||||
/// This is used for a lathe that can utilize <see cref="BlueprintComponent"/>s to gain more recipes.
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent, Access(typeof(BlueprintSystem))]
|
||||
public sealed partial class BlueprintReceiverComponent : Component
|
||||
{
|
||||
[DataField]
|
||||
public string ContainerId = "blueprint";
|
||||
|
||||
[DataField(required: true)]
|
||||
public EntityWhitelist Whitelist = new();
|
||||
}
|
||||
114
Content.Shared/Research/Systems/BlueprintSystem.cs
Normal file
114
Content.Shared/Research/Systems/BlueprintSystem.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using Content.Shared.IdentityManagement;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Lathe;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Research.Components;
|
||||
using Content.Shared.Research.Prototypes;
|
||||
using Content.Shared.Whitelist;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Shared.Research.Systems;
|
||||
|
||||
public sealed class BlueprintSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly SharedContainerSystem _container = default!;
|
||||
[Dependency] private readonly EntityWhitelistSystem _entityWhitelist = default!;
|
||||
[Dependency] private readonly SharedPopupSystem _popup = default!;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Initialize()
|
||||
{
|
||||
SubscribeLocalEvent<BlueprintReceiverComponent, ComponentStartup>(OnStartup);
|
||||
SubscribeLocalEvent<BlueprintReceiverComponent, AfterInteractUsingEvent>(OnAfterInteract);
|
||||
SubscribeLocalEvent<BlueprintReceiverComponent, LatheGetRecipesEvent>(OnGetRecipes);
|
||||
}
|
||||
|
||||
private void OnStartup(Entity<BlueprintReceiverComponent> ent, ref ComponentStartup args)
|
||||
{
|
||||
_container.EnsureContainer<Container>(ent, ent.Comp.ContainerId);
|
||||
}
|
||||
|
||||
private void OnAfterInteract(Entity<BlueprintReceiverComponent> ent, ref AfterInteractUsingEvent args)
|
||||
{
|
||||
if (args.Handled || !args.CanReach || !TryComp<BlueprintComponent>(args.Used, out var blueprintComponent))
|
||||
return;
|
||||
args.Handled = TryInsertBlueprint(ent, (args.Used, blueprintComponent), args.User);
|
||||
}
|
||||
|
||||
private void OnGetRecipes(Entity<BlueprintReceiverComponent> ent, ref LatheGetRecipesEvent args)
|
||||
{
|
||||
var recipes = GetBlueprintRecipes(ent);
|
||||
foreach (var recipe in recipes)
|
||||
{
|
||||
args.Recipes.Add(recipe);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryInsertBlueprint(Entity<BlueprintReceiverComponent> ent, Entity<BlueprintComponent> blueprint, EntityUid? user)
|
||||
{
|
||||
if (!CanInsertBlueprint(ent, blueprint, user))
|
||||
return false;
|
||||
|
||||
if (user is not null)
|
||||
{
|
||||
var userId = Identity.Entity(user.Value, EntityManager);
|
||||
var bpId = Identity.Entity(blueprint, EntityManager);
|
||||
var machineId = Identity.Entity(ent, EntityManager);
|
||||
var msg = Loc.GetString("blueprint-receiver-popup-insert",
|
||||
("user", userId),
|
||||
("blueprint", bpId),
|
||||
("receiver", machineId));
|
||||
_popup.PopupPredicted(msg, ent, user);
|
||||
}
|
||||
|
||||
_container.Insert(blueprint.Owner, _container.GetContainer(ent, ent.Comp.ContainerId));
|
||||
|
||||
var ev = new TechnologyDatabaseModifiedEvent();
|
||||
RaiseLocalEvent(ent, ref ev);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool CanInsertBlueprint(Entity<BlueprintReceiverComponent> ent, Entity<BlueprintComponent> blueprint, EntityUid? user)
|
||||
{
|
||||
if (_entityWhitelist.IsWhitelistFail(ent.Comp.Whitelist, blueprint))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (blueprint.Comp.ProvidedRecipes.Count == 0)
|
||||
{
|
||||
Log.Error($"Attempted to insert blueprint {ToPrettyString(blueprint)} with no recipes.");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Don't add new blueprints if there are no new recipes.
|
||||
var currentRecipes = GetBlueprintRecipes(ent);
|
||||
if (currentRecipes.Count != 0 && currentRecipes.IsSupersetOf(blueprint.Comp.ProvidedRecipes))
|
||||
{
|
||||
_popup.PopupPredicted(Loc.GetString("blueprint-receiver-popup-recipe-exists"), ent, user);
|
||||
return false;
|
||||
}
|
||||
|
||||
return _container.CanInsert(blueprint, _container.GetContainer(ent, ent.Comp.ContainerId));
|
||||
}
|
||||
|
||||
public HashSet<ProtoId<LatheRecipePrototype>> GetBlueprintRecipes(Entity<BlueprintReceiverComponent> ent)
|
||||
{
|
||||
var contained = _container.GetContainer(ent, ent.Comp.ContainerId);
|
||||
|
||||
var recipes = new HashSet<ProtoId<LatheRecipePrototype>>();
|
||||
foreach (var blueprint in contained.ContainedEntities)
|
||||
{
|
||||
if (!TryComp<BlueprintComponent>(blueprint, out var blueprintComponent))
|
||||
continue;
|
||||
|
||||
foreach (var provided in blueprintComponent.ProvidedRecipes)
|
||||
{
|
||||
recipes.Add(provided);
|
||||
}
|
||||
}
|
||||
|
||||
return recipes;
|
||||
}
|
||||
}
|
||||
@@ -81,4 +81,17 @@ public sealed class EntityWhitelistSystem : EntitySystem
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// WD Ahead of wizden.
|
||||
/// Helper function to determine if Whitelist is not null and entity is not on the list
|
||||
/// </summary>
|
||||
public bool IsWhitelistFail(EntityWhitelist? whitelist, EntityUid uid)
|
||||
{
|
||||
if (whitelist == null)
|
||||
return false;
|
||||
|
||||
return !IsValid(whitelist, uid);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user