Give Item spell (#4241)
* Cherry-picked summonspell * Renamed the script for clarity * Fixed Spell type error in yaml * Fixed Sound issues and increased the cooldown * Newline * Major script change * Fixed Namespace * Validation fixed, TryGet replaced * Typo again * Allowed for proper localisation * Typo fixed Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> * Adressed changes * Review cleanup Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
75
Content.Server/Actions/Spells/GiveItemSpell.cs
Normal file
75
Content.Server/Actions/Spells/GiveItemSpell.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using Content.Server.Hands.Components;
|
||||
using Content.Server.Items;
|
||||
using Content.Server.Notification;
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Actions.Behaviors;
|
||||
using Content.Shared.Actions.Components;
|
||||
using Content.Shared.Cooldown;
|
||||
using Content.Shared.Notification.Managers;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Actions.Spells
|
||||
{
|
||||
[UsedImplicitly]
|
||||
[DataDefinition]
|
||||
public class GiveItemSpell : IInstantAction
|
||||
{ //TODO: Needs to be an EntityPrototype for proper validation
|
||||
[ViewVariables] [DataField("castMessage")] public string? CastMessage { get; set; } = default!;
|
||||
[ViewVariables] [DataField("cooldown")] public float CoolDown { get; set; } = 1f;
|
||||
[ViewVariables] [DataField("spellItem")] public string ItemProto { get; set; } = default!;
|
||||
|
||||
[ViewVariables] [DataField("castSound")] public string? CastSound { get; set; } = default!;
|
||||
|
||||
//Rubber-band snapping items into player's hands, originally was a workaround, later found it works quite well with stuns
|
||||
//Not sure if needs fixing
|
||||
|
||||
public void DoInstantAction(InstantActionEventArgs args)
|
||||
{
|
||||
var caster = args.Performer;
|
||||
|
||||
if (!caster.TryGetComponent(out HandsComponent? handsComponent))
|
||||
{
|
||||
caster.PopupMessage(Loc.GetString("spell-fail-no-hands"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!EntitySystem.Get<ActionBlockerSystem>().CanInteract(caster)) return;
|
||||
|
||||
// TODO: Nix when we get EntityPrototype serializers
|
||||
if (!IoCManager.Resolve<IPrototypeManager>().HasIndex<EntityPrototype>(ItemProto))
|
||||
{
|
||||
Logger.Error($"Invalid prototype {ItemProto} supplied for {nameof(GiveItemSpell)}");
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Look this is shitty and ideally a test would do it
|
||||
var spawnedProto = caster.EntityManager.SpawnEntity(ItemProto, caster.Transform.MapPosition);
|
||||
|
||||
if (!spawnedProto.TryGetComponent(out ItemComponent? itemComponent))
|
||||
{
|
||||
Logger.Error($"Tried to use {nameof(GiveItemSpell)} but prototype has no {nameof(ItemComponent)}?");
|
||||
spawnedProto.Delete();
|
||||
return;
|
||||
}
|
||||
|
||||
args.PerformerActions?.Cooldown(args.ActionType, Cooldowns.SecondsFromNow(CoolDown));
|
||||
|
||||
if (CastMessage != null)
|
||||
caster.PopupMessageEveryone(CastMessage);
|
||||
|
||||
handsComponent.PutInHandOrDrop(itemComponent);
|
||||
|
||||
if (CastSound != null)
|
||||
SoundSystem.Play(Filter.Pvs(caster), CastSound, caster);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user