Clean up RefiningSystem (#27904)

cleanup RefiningSystem
This commit is contained in:
Nemanja 2024-05-29 14:06:50 -04:00 committed by null
parent bf59f7dbc6
commit 0a686ec58e
No known key found for this signature in database
GPG Key ID: 212F05528FD678BE
5 changed files with 79 additions and 65 deletions

View File

@ -1,3 +1,4 @@
using Content.Shared.Storage;
using Content.Shared.Tools;
using Robust.Shared.Prototypes;
@ -7,18 +8,30 @@ namespace Content.Server.Construction.Components;
/// Used for something that can be refined by welder.
/// For example, glass shard can be refined to glass sheet.
/// </summary>
[RegisterComponent]
[RegisterComponent, Access(typeof(RefiningSystem))]
public sealed partial class WelderRefinableComponent : Component
{
[DataField]
public HashSet<EntProtoId>? RefineResult = new();
/// <summary>
/// The items created when the item is refined.
/// </summary>
[DataField(required: true)]
public List<EntitySpawnEntry> RefineResult = new();
/// <summary>
/// The amount of time it takes to refine a given item.
/// </summary>
[DataField]
public float RefineTime = 2f;
/// <summary>
/// The amount of fuel it takes to refine a given item.
/// </summary>
[DataField]
public float RefineFuel;
/// <summary>
/// The tool type needed in order to refine this item.
/// </summary>
[DataField]
public ProtoId<ToolQualityPrototype> QualityNeeded = "Welding";
}

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);
}
}

View File

@ -87,7 +87,7 @@
color: "#bbeeff"
- type: WelderRefinable
refineResult:
- SheetGlass1
- id: SheetGlass1
- type: DamageUserOnTrigger
damage:
types:
@ -122,8 +122,8 @@
Slash: 4.5
- type: WelderRefinable
refineResult:
- SheetGlass1
- PartRodMetal1
- id: SheetGlass1
- id: PartRodMetal1
- type: DamageUserOnTrigger
damage:
types:
@ -158,8 +158,8 @@
Slash: 5.5
- type: WelderRefinable
refineResult:
- SheetGlass1
- SheetPlasma1
- id: SheetGlass1
- id: SheetPlasma1
- type: DamageUserOnTrigger
damage:
types:
@ -197,8 +197,8 @@
Radiation: 2
- type: WelderRefinable
refineResult:
- SheetGlass1
- SheetUranium1
- id: SheetGlass1
- id: SheetUranium1
- type: DamageUserOnTrigger
damage:
types:
@ -232,8 +232,8 @@
color: "#e0aa36"
- type: WelderRefinable
refineResult:
- SheetGlass1
- SheetBrass1
- id: SheetGlass1
- id: SheetBrass1
- type: DamageUserOnTrigger
damage:
types:

View File

@ -28,4 +28,4 @@
- type: SpaceGarbage
- type: WelderRefinable
refineResult:
- SheetGlass1
- id: SheetGlass1

View File

@ -68,7 +68,7 @@
- type: SpaceGarbage
- type: WelderRefinable
refineResult:
- SheetGlass1
- id: SheetGlass1
- type: entity
parent: BaseLightbulb
@ -276,8 +276,8 @@
node: icon
- type: WelderRefinable
refineResult:
- SheetGlass1
- ShardCrystalCyan
- id: SheetGlass1
- id: ShardCrystalCyan
- type: entity
parent: LightTubeCrystalCyan
@ -296,8 +296,8 @@
node: icon
- type: WelderRefinable
refineResult:
- SheetGlass1
- ShardCrystalBlue
- id: SheetGlass1
- id: ShardCrystalBlue
- type: entity
parent: LightTubeCrystalCyan
@ -316,8 +316,8 @@
node: icon
- type: WelderRefinable
refineResult:
- SheetGlass1
- ShardCrystalPink
- id: SheetGlass1
- id: ShardCrystalPink
- type: entity
parent: LightTubeCrystalCyan
@ -336,8 +336,8 @@
node: icon
- type: WelderRefinable
refineResult:
- SheetGlass1
- ShardCrystalOrange
- id: SheetGlass1
- id: ShardCrystalOrange
- type: entity
parent: LightTubeCrystalCyan
@ -356,8 +356,8 @@
node: icon
- type: WelderRefinable
refineResult:
- SheetGlass1
- ShardCrystalRed
- id: SheetGlass1
- id: ShardCrystalRed
- type: entity
parent: LightTubeCrystalCyan
@ -376,5 +376,5 @@
node: icon
- type: WelderRefinable
refineResult:
- SheetGlass1
- ShardCrystalGreen
- id: SheetGlass1
- id: ShardCrystalGreen