Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add SpawnTableOnUse #32620

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Content.Server/Storage/Components/SpawnTableOnUseComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Content.Server.Storage.EntitySystems;
using Content.Shared.EntityTable.EntitySelectors;

namespace Content.Server.Storage.Components;

/// <summary>
/// Spawns items from an entity table when used in hand.
/// </summary>
[RegisterComponent, Access(typeof(SpawnTableOnUseSystem))]
public sealed partial class SpawnTableOnUseComponent : Component
{
/// <summary>
/// The entity table to select entities from.
/// </summary>
[DataField(required: true)]
public EntityTableSelector Table = default!;
}
41 changes: 41 additions & 0 deletions Content.Server/Storage/EntitySystems/SpawnTableOnUseSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Content.Server.Administration.Logs;
using Content.Server.Storage.Components;
using Content.Shared.Database;
using Content.Shared.EntityTable;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Interaction.Events;

namespace Content.Server.Storage.EntitySystems;

public sealed class SpawnTableOnUseSystem : EntitySystem
{
[Dependency] private readonly EntityTableSystem _entityTable = default!;
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
[Dependency] private readonly SharedHandsSystem _hands = default!;

public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<SpawnTableOnUseComponent, UseInHandEvent>(OnUseInHand);
}

private void OnUseInHand(Entity<SpawnTableOnUseComponent> ent, ref UseInHandEvent args)
{
if (args.Handled)
return;

args.Handled = true;

var coords = Transform(ent).Coordinates;
var spawns = _entityTable.GetSpawns(ent.Comp.Table);
foreach (var id in spawns)
{
var spawned = Spawn(id, coords);
_adminLogger.Add(LogType.EntitySpawn, LogImpact.Low, $"{ToPrettyString(args.User):user} used {ToPrettyString(ent):spawner} which spawned {ToPrettyString(spawned)}");
_hands.TryPickupAnyHand(args.User, spawned);
}

Del(ent);
}
}
7 changes: 7 additions & 0 deletions Content.Shared/Sound/Components/BaseEmitSoundComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ public abstract partial class BaseEmitSoundComponent : Component
[ViewVariables(VVAccess.ReadWrite)]
[DataField(required: true)]
public SoundSpecifier? Sound;

/// <summary>
/// Play the sound at the position instead of parented to the source entity.
/// Useful if the entity is deleted after.
/// </summary>
[DataField]
public bool Positional;
}
18 changes: 13 additions & 5 deletions Content.Shared/Sound/SharedEmitSoundSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,22 @@ protected void TryEmitSound(EntityUid uid, BaseEmitSoundComponent component, Ent
if (component.Sound == null)
return;

if (predict)
if (component.Positional)
{
_audioSystem.PlayPredicted(component.Sound, uid, user);
var coords = Transform(uid).Coordinates;
if (predict)
_audioSystem.PlayPredicted(component.Sound, coords, user);
else if (_netMan.IsServer)
// don't predict sounds that client couldn't have played already
_audioSystem.PlayPvs(component.Sound, coords);
}
else if (_netMan.IsServer)
else
{
// don't predict sounds that client couldn't have played already
_audioSystem.PlayPvs(component.Sound, uid);
if (predict)
_audioSystem.PlayPredicted(component.Sound, uid, user);
else if (_netMan.IsServer)
// don't predict sounds that client couldn't have played already
_audioSystem.PlayPvs(component.Sound, uid);
}
}

Expand Down
Loading