StackSystem uses EntityUid for Split and Spawn

This commit is contained in:
Vera Aguilera Puerto
2021-11-09 15:34:40 +01:00
parent 6f2cc733e7
commit 8b57bafcd1
13 changed files with 60 additions and 44 deletions

View File

@@ -30,10 +30,10 @@ namespace Content.Server.Construction
return;
// If the used entity doesn't have a tool, return early.
if (!EntityManager.TryGetComponent(args.Used.Uid, out ToolComponent? usedTool))
if (!EntityManager.TryGetComponent(args.UsedUid, out ToolComponent? usedTool))
return;
args.Handled = await TryToggleAnchor(uid, args.User.Uid, args.Used.Uid, anchorable, usingTool:usedTool);
args.Handled = await TryToggleAnchor(uid, args.UserUid, args.UsedUid, anchorable, usingTool:usedTool);
}
/// <summary>

View File

@@ -91,7 +91,7 @@ namespace Content.Server.Construction.Components
if (stack == null)
throw new Exception($"Couldn't spawn stack of type {stackType}!");
if (!partContainer.Insert(stack))
if (!partContainer.Insert(Owner.EntityManager.GetEntity(stack)))
throw new Exception($"Couldn't insert machine material of type {stackType} to machine with prototype {Owner.Prototype?.ID ?? "N/A"}");
}

View File

@@ -319,7 +319,7 @@ namespace Content.Server.Construction.Components
if (splitStack == null)
return false;
if(!_partContainer.Insert(splitStack))
if(!_partContainer.Insert(Owner.EntityManager.GetEntity(splitStack.Value)))
return false;
_materialProgress[type] += needed;

View File

@@ -171,10 +171,10 @@ namespace Content.Server.Construction
if (string.IsNullOrEmpty(materialStep.Store))
{
if (!container.Insert(splitStack))
if (!container.Insert(EntityManager.GetEntity(splitStack.Value)))
continue;
}
else if (!GetContainer(materialStep.Store).Insert(splitStack))
else if (!GetContainer(materialStep.Store).Insert(EntityManager.GetEntity(splitStack.Value)))
continue;
handled = true;
@@ -186,7 +186,7 @@ namespace Content.Server.Construction
case ArbitraryInsertConstructionGraphStep arbitraryStep:
foreach (var entity in EnumerateNearby(user))
{
if (!arbitraryStep.EntityValid(entity))
if (!arbitraryStep.EntityValid(entity.Uid, EntityManager))
continue;
if (string.IsNullOrEmpty(arbitraryStep.Store))
@@ -430,7 +430,7 @@ namespace Content.Server.Construction
switch (step)
{
case EntityInsertConstructionGraphStep entityInsert:
if (entityInsert.EntityValid(holding))
if (entityInsert.EntityValid(holding.Uid, EntityManager))
valid = true;
break;
case ToolConstructionGraphStep _:

View File

@@ -273,11 +273,11 @@ namespace Content.Server.Construction
if (doAfterState == DoAfterState.Cancelled)
return HandleResult.False;
var insert = interactUsing.Used;
var insert = interactUsing.UsedUid;
// Since many things inherit this step, we delegate the "is this entity valid?" logic to them.
// While this is very OOP and I find it icky, I must admit that it simplifies the code here a lot.
if(!insertStep.EntityValid(insert))
if(!insertStep.EntityValid(insert, EntityManager))
return HandleResult.False;
// If we're only testing whether this step would be handled by the given event, then we're done.
@@ -312,7 +312,7 @@ namespace Content.Server.Construction
// we split the stack in two and insert the split stack.
if (insertStep is MaterialConstructionGraphStep materialInsertStep)
{
if (_stackSystem.Split(insert.Uid, materialInsertStep.Amount, interactUsing.User.Transform.Coordinates) is not { } stack)
if (_stackSystem.Split(insert, materialInsertStep.Amount, EntityManager.GetComponent<TransformComponent>(interactUsing.UserUid).Coordinates) is not {} stack)
return HandleResult.False;
insert = stack;
@@ -330,12 +330,12 @@ namespace Content.Server.Construction
// The container doesn't necessarily need to exist, so we ensure it.
_containerSystem.EnsureContainer<Container>(uid, store)
.Insert(insert);
.Insert(EntityManager.GetEntity(insert));
}
else
{
// If we don't store the item in a container on the entity, we just delete it right away.
insert.Delete();
EntityManager.DeleteEntity(insert);
}
// Step has been handled correctly, so we signal this.

View File

@@ -245,7 +245,7 @@ namespace Content.Server.Hands.Systems
if (splitStack == null)
return false;
throwEnt = splitStack;
throwEnt = EntityManager.GetEntity(splitStack.Value);
}
else if (!hands.Drop(throwEnt))
return false;

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using Content.Server.Hands.Components;
using Content.Server.Items;
using Content.Server.Popups;
@@ -28,7 +29,7 @@ namespace Content.Server.Stack
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
public static readonly List<int> DefaultSplitAmounts = new() { 1, 5, 10, 20, 30, 50 };
public static readonly int[] DefaultSplitAmounts = { 1, 5, 10, 20, 30, 50 };
public override void Initialize()
{
@@ -41,7 +42,7 @@ namespace Content.Server.Stack
/// <summary>
/// Try to split this stack into two. Returns a non-null <see cref="IEntity"/> if successful.
/// </summary>
public IEntity? Split(EntityUid uid, int amount, EntityCoordinates spawnPosition, SharedStackComponent? stack = null)
public EntityUid? Split(EntityUid uid, int amount, EntityCoordinates spawnPosition, SharedStackComponent? stack = null)
{
if (!Resolve(uid, ref stack))
return null;
@@ -56,12 +57,12 @@ namespace Content.Server.Stack
return null;
// Set the output parameter in the event instance to the newly split stack.
var entity = EntityManager.SpawnEntity(prototype, spawnPosition);
var entity = EntityManager.SpawnEntity(prototype, spawnPosition).Uid;
if (EntityManager.TryGetComponent(entity.Uid, out SharedStackComponent? stackComp))
if (EntityManager.TryGetComponent(entity, out SharedStackComponent? stackComp))
{
// Set the split stack's count.
SetCount(entity.Uid, amount, stackComp);
SetCount(entity, amount, stackComp);
// Don't let people dupe unlimited stacks
stackComp.Unlimited = false;
}
@@ -72,14 +73,14 @@ namespace Content.Server.Stack
/// <summary>
/// Spawns a stack of a certain stack type. See <see cref="StackPrototype"/>.
/// </summary>
public IEntity Spawn(int amount, StackPrototype prototype, EntityCoordinates spawnPosition)
public EntityUid Spawn(int amount, StackPrototype prototype, EntityCoordinates spawnPosition)
{
// Set the output result parameter to the new stack entity...
var entity = EntityManager.SpawnEntity(prototype.Spawn, spawnPosition);
var stack = EntityManager.GetComponent<StackComponent>(entity.Uid);
var entity = EntityManager.SpawnEntity(prototype.Spawn, spawnPosition).Uid;
var stack = EntityManager.GetComponent<StackComponent>(entity);
// And finally, set the correct amount!
SetCount(entity.Uid, amount, stack);
SetCount(entity, amount, stack);
return entity;
}
@@ -136,7 +137,7 @@ namespace Content.Server.Stack
Verb halve = new();
halve.Text = Loc.GetString("comp-stack-split-halve");
halve.Category = VerbCategory.Split;
halve.Act = () => UserSplit(args.User, stack, stack.Count / 2);
halve.Act = () => UserSplit(uid, args.User.Uid, stack.Count / 2, stack);
halve.Priority = 1;
args.Verbs.Add(halve);
@@ -149,7 +150,7 @@ namespace Content.Server.Stack
Verb verb = new();
verb.Text = amount.ToString();
verb.Category = VerbCategory.Split;
verb.Act = () => UserSplit(args.User, stack, amount);
verb.Act = () => UserSplit(uid, args.User.Uid, amount, stack);
// we want to sort by size, not alphabetically by the verb text.
verb.Priority = priority;
@@ -159,15 +160,23 @@ namespace Content.Server.Stack
}
}
private void UserSplit(IEntity user, StackComponent stack, int amount)
private void UserSplit(EntityUid uid, EntityUid userUid, int amount,
StackComponent? stack = null,
TransformComponent? userTransform = null)
{
if (!Resolve(uid, ref stack))
return;
if (!Resolve(userUid, ref userTransform))
return;
if (amount <= 0)
{
user.PopupMessage(Loc.GetString("comp-stack-split-too-small"));
_popupSystem.PopupCursor(Loc.GetString("comp-stack-split-too-small"), Filter.Entities(userUid));
return;
}
if (user.TryGetComponent<HandsComponent>(out var hands))
if (EntityManager.TryGetComponent<HandsComponent>(userUid, out var hands))
{
if (hands.TryGetActiveHeldEntity(out var heldItem) && heldItem != stack.Owner)
{
@@ -179,12 +188,15 @@ namespace Content.Server.Stack
return;
}
var secondStack = Split(stack.Owner.Uid, amount, user.Transform.Coordinates, stack);
user.PopupMessage(Loc.GetString("comp-stack-split"));
if (secondStack is not null && secondStack.TryGetComponent<ItemComponent>(out var itemComponent))
if(Split(uid, amount, userTransform.Coordinates, stack) is not {} splitStack)
return;
if (EntityManager.TryGetComponent<ItemComponent>(splitStack, out var item))
{
hands.PutInHandOrDrop(itemComponent);
hands.PutInHandOrDrop(item);
}
_popupSystem.PopupCursor(Loc.GetString("comp-stack-split"), Filter.Entities(userUid));
}
}
}

View File

@@ -10,9 +10,9 @@ namespace Content.Shared.Construction.Steps
{
[DataField("component")] public string Component { get; } = string.Empty;
public override bool EntityValid(IEntity entity)
public override bool EntityValid(EntityUid uid, IEntityManager entityManager)
{
foreach (var component in entity.GetAllComponents())
foreach (var component in entityManager.GetComponents(uid))
{
if (component.Name == Component)
return true;

View File

@@ -8,6 +8,6 @@ namespace Content.Shared.Construction.Steps
{
[DataField("store")] public string Store { get; } = string.Empty;
public abstract bool EntityValid(IEntity entity);
public abstract bool EntityValid(EntityUid uid, IEntityManager entityManager);
}
}

View File

@@ -27,9 +27,9 @@ namespace Content.Shared.Construction.Steps
examinedEvent.Message.AddMarkup(Loc.GetString("construction-insert-material-entity", ("amount", Amount), ("materialName", material.Name)));
}
public override bool EntityValid(IEntity entity)
public override bool EntityValid(EntityUid uid, IEntityManager entityManager)
{
return entity.TryGetComponent(out SharedStackComponent? stack) && stack.StackTypeId.Equals(MaterialPrototypeId) && stack.Count >= Amount;
return entityManager.TryGetComponent(uid, out SharedStackComponent? stack) && stack.StackTypeId.Equals(MaterialPrototypeId) && stack.Count >= Amount;
}
public bool EntityValid(IEntity entity, [NotNullWhen(true)] out SharedStackComponent? stack)

View File

@@ -18,16 +18,20 @@ namespace Content.Shared.Construction.Steps
return list == null || list.Count == 0;
}
public override bool EntityValid(IEntity entity)
public override bool EntityValid(EntityUid uid, IEntityManager entityManager)
{
// This step can only happen if either list has tags.
if (IsNullOrEmpty(_allTags) && IsNullOrEmpty(_anyTags))
return false; // Step is somehow invalid, we return.
if (_allTags != null && !entity.HasAllTags(_allTags))
// No tags at all.
if (!entityManager.TryGetComponent(uid, out TagComponent? tags))
return false;
if (_allTags != null && !tags.HasAllTags(_allTags))
return false; // We don't have all the tags needed.
if (_anyTags != null && !entity.HasAnyTag(_anyTags))
if (_anyTags != null && !tags.HasAnyTag(_anyTags))
return false; // We don't have any of the tags needed.
// This entity is valid!

View File

@@ -10,9 +10,9 @@ namespace Content.Shared.Construction.Steps
{
[DataField("prototype")] public string Prototype { get; } = string.Empty;
public override bool EntityValid(IEntity entity)
public override bool EntityValid(EntityUid uid, IEntityManager entityManager)
{
return entity.Prototype?.ID == Prototype;
return entityManager.GetComponent<MetaDataComponent>(uid).EntityPrototype?.ID == Prototype;
}
public override void DoExamine(ExaminedEvent examinedEvent)

View File

@@ -10,9 +10,9 @@ namespace Content.Shared.Construction.Steps
[DataField("tag")]
private string? _tag = null;
public override bool EntityValid(IEntity entity)
public override bool EntityValid(EntityUid uid, IEntityManager entityManager)
{
return !string.IsNullOrEmpty(_tag) && entity.HasTag(_tag);
return !string.IsNullOrEmpty(_tag) && entityManager.TryGetComponent(uid, out TagComponent? tags) && tags.HasTag(_tag);
}
}
}