Clean up RefiningSystem (#27904)

cleanup RefiningSystem
This commit is contained in:
Nemanja
2024-05-29 14:06:50 -04:00
committed by GitHub
parent a90cb25ef7
commit 5d79f897b9
5 changed files with 79 additions and 65 deletions

View File

@@ -1,50 +1,51 @@
using Content.Server.Construction.Components;
using Content.Server.Stack;
using Content.Shared.Construction;
using Content.Shared.Interaction;
using Content.Shared.Stacks;
using SharedToolSystem = Content.Shared.Tools.Systems.SharedToolSystem;
using Content.Shared.Storage;
using Content.Shared.Tools.Systems;
using Robust.Shared.Random;
namespace Content.Server.Construction
namespace Content.Server.Construction;
public sealed class RefiningSystem : EntitySystem
{
public sealed class RefiningSystem : EntitySystem
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly SharedToolSystem _toolSystem = default!;
public override void Initialize()
{
[Dependency] private readonly SharedToolSystem _toolSystem = default!;
[Dependency] private readonly StackSystem _stackSystem = default!;
public override void Initialize()
base.Initialize();
SubscribeLocalEvent<WelderRefinableComponent, InteractUsingEvent>(OnInteractUsing);
SubscribeLocalEvent<WelderRefinableComponent, WelderRefineDoAfterEvent>(OnDoAfter);
}
private void OnInteractUsing(EntityUid uid, WelderRefinableComponent component, InteractUsingEvent args)
{
if (args.Handled)
return;
args.Handled = _toolSystem.UseTool(
args.Used,
args.User,
uid,
component.RefineTime,
component.QualityNeeded,
new WelderRefineDoAfterEvent(),
fuel: component.RefineFuel);
}
private void OnDoAfter(EntityUid uid, WelderRefinableComponent component, WelderRefineDoAfterEvent args)
{
if (args.Cancelled)
return;
var xform = Transform(uid);
var spawns = EntitySpawnCollection.GetSpawns(component.RefineResult, _random);
foreach (var spawn in spawns)
{
base.Initialize();
SubscribeLocalEvent<WelderRefinableComponent, InteractUsingEvent>(OnInteractUsing);
SubscribeLocalEvent<WelderRefinableComponent, WelderRefineDoAfterEvent>(OnDoAfter);
SpawnNextToOrDrop(spawn, uid, xform);
}
private void OnInteractUsing(EntityUid uid, WelderRefinableComponent component, InteractUsingEvent args)
{
if (args.Handled)
return;
args.Handled = _toolSystem.UseTool(args.Used, args.User, uid, component.RefineTime, component.QualityNeeded, new WelderRefineDoAfterEvent(), fuel: component.RefineFuel);
}
private void OnDoAfter(EntityUid uid, WelderRefinableComponent component, WelderRefineDoAfterEvent args)
{
if (args.Cancelled)
return;
// get last owner coordinates and delete it
var resultPosition = Transform(uid).Coordinates;
EntityManager.DeleteEntity(uid);
// spawn each result after refine
foreach (var result in component.RefineResult!)
{
var droppedEnt = Spawn(result, resultPosition);
// TODO: If something has a stack... Just use a prototype with a single thing in the stack.
// This is not a good way to do it.
if (TryComp<StackComponent>(droppedEnt, out var stack))
_stackSystem.SetCount(droppedEnt, 1, stack);
}
}
Del(uid);
}
}