EntityUid sTagDummy = default;
TagComponent sTagComponent = null!;
+ Entity<TagComponent> sTagEntity = default;
await server.WaitPost(() =>
{
sTagDummy = sEntityManager.SpawnEntity(TagEntityId, MapCoordinates.Nullspace);
sTagComponent = sEntityManager.GetComponent<TagComponent>(sTagDummy);
+ sTagEntity = new Entity<TagComponent>(sTagDummy, sTagComponent);
});
await server.WaitAssertion(() =>
Assert.Multiple(() =>
{
// Cannot add the starting tag again
- Assert.That(tagSystem.AddTag(sTagDummy, sTagComponent, StartingTag), Is.False);
- Assert.That(tagSystem.AddTags(sTagDummy, sTagComponent, StartingTag, StartingTag), Is.False);
- Assert.That(tagSystem.AddTags(sTagDummy, sTagComponent, new List<string> { StartingTag, StartingTag }), Is.False);
+ Assert.That(tagSystem.AddTag(sTagEntity, StartingTag), Is.False);
+
+ Assert.That(tagSystem.AddTags(sTagEntity, StartingTag, StartingTag), Is.False);
+ Assert.That(tagSystem.AddTags(sTagEntity, new List<ProtoId<TagPrototype>> { StartingTag, StartingTag }), Is.False);
+ Assert.That(tagSystem.AddTags(sTagEntity, new HashSet<ProtoId<TagPrototype>> { StartingTag, StartingTag }), Is.False);
// Has the starting tag
Assert.That(tagSystem.HasTag(sTagComponent, StartingTag), Is.True);
+
Assert.That(tagSystem.HasAllTags(sTagComponent, StartingTag, StartingTag), Is.True);
- Assert.That(tagSystem.HasAllTags(sTagComponent, new List<string> { StartingTag, StartingTag }), Is.True);
+ Assert.That(tagSystem.HasAllTags(sTagComponent, new List<ProtoId<TagPrototype>> { StartingTag, StartingTag }), Is.True);
+ Assert.That(tagSystem.HasAllTags(sTagComponent, new HashSet<ProtoId<TagPrototype>> { StartingTag, StartingTag }), Is.True);
+
Assert.That(tagSystem.HasAnyTag(sTagComponent, StartingTag, StartingTag), Is.True);
- Assert.That(tagSystem.HasAnyTag(sTagComponent, new List<string> { StartingTag, StartingTag }), Is.True);
+ Assert.That(tagSystem.HasAnyTag(sTagComponent, new List<ProtoId<TagPrototype>> { StartingTag, StartingTag }), Is.True);
+ Assert.That(tagSystem.HasAnyTag(sTagComponent, new HashSet<ProtoId<TagPrototype>> { StartingTag, StartingTag }), Is.True);
// Does not have the added tag yet
Assert.That(tagSystem.HasTag(sTagComponent, AddedTag), Is.False);
+
Assert.That(tagSystem.HasAllTags(sTagComponent, AddedTag, AddedTag), Is.False);
- Assert.That(tagSystem.HasAllTags(sTagComponent, new List<string> { AddedTag, AddedTag }), Is.False);
+ Assert.That(tagSystem.HasAllTags(sTagComponent, new List<ProtoId<TagPrototype>> { AddedTag, AddedTag }), Is.False);
+ Assert.That(tagSystem.HasAllTags(sTagComponent, new HashSet<ProtoId<TagPrototype>> { AddedTag, AddedTag }), Is.False);
+
Assert.That(tagSystem.HasAnyTag(sTagComponent, AddedTag, AddedTag), Is.False);
- Assert.That(tagSystem.HasAnyTag(sTagComponent, new List<string> { AddedTag, AddedTag }), Is.False);
+ Assert.That(tagSystem.HasAnyTag(sTagComponent, new List<ProtoId<TagPrototype>> { AddedTag, AddedTag }), Is.False);
+ Assert.That(tagSystem.HasAnyTag(sTagComponent, new HashSet<ProtoId<TagPrototype>> { AddedTag, AddedTag }), Is.False);
// Has a combination of the two tags
Assert.That(tagSystem.HasAnyTag(sTagComponent, StartingTag, AddedTag), Is.True);
- Assert.That(tagSystem.HasAnyTag(sTagComponent, new List<string> { StartingTag, AddedTag }), Is.True);
+ Assert.That(tagSystem.HasAnyTag(sTagComponent, new List<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.True);
+ Assert.That(tagSystem.HasAnyTag(sTagComponent, new HashSet<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.True);
// Does not have both tags
Assert.That(tagSystem.HasAllTags(sTagComponent, StartingTag, AddedTag), Is.False);
- Assert.That(tagSystem.HasAllTags(sTagComponent, new List<string> { StartingTag, AddedTag }), Is.False);
+ Assert.That(tagSystem.HasAllTags(sTagComponent, new List<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.False);
+ Assert.That(tagSystem.HasAllTags(sTagComponent, new HashSet<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.False);
// Cannot remove a tag that does not exist
- Assert.That(tagSystem.RemoveTag(sTagDummy, sTagComponent, AddedTag), Is.False);
- Assert.That(tagSystem.RemoveTags(sTagDummy, sTagComponent, AddedTag, AddedTag), Is.False);
- Assert.That(tagSystem.RemoveTags(sTagDummy, sTagComponent, new List<string> { AddedTag, AddedTag }), Is.False);
+ Assert.That(tagSystem.RemoveTag(sTagEntity, AddedTag), Is.False);
+
+ Assert.That(tagSystem.RemoveTags(sTagEntity, AddedTag, AddedTag), Is.False);
+ Assert.That(tagSystem.RemoveTags(sTagEntity, new List<ProtoId<TagPrototype>> { AddedTag, AddedTag }), Is.False);
+ Assert.That(tagSystem.RemoveTags(sTagEntity, new HashSet<ProtoId<TagPrototype>> { AddedTag, AddedTag }), Is.False);
});
// Can add the new tag
- Assert.That(tagSystem.AddTag(sTagDummy, sTagComponent, AddedTag), Is.True);
+ Assert.That(tagSystem.AddTag(sTagEntity, AddedTag), Is.True);
Assert.Multiple(() =>
{
// Cannot add it twice
- Assert.That(tagSystem.AddTag(sTagDummy, sTagComponent, AddedTag), Is.False);
+ Assert.That(tagSystem.AddTag(sTagEntity, AddedTag), Is.False);
// Cannot add existing tags
- Assert.That(tagSystem.AddTags(sTagDummy, sTagComponent, StartingTag, AddedTag), Is.False);
- Assert.That(tagSystem.AddTags(sTagDummy, sTagComponent, new List<string> { StartingTag, AddedTag }), Is.False);
+ Assert.That(tagSystem.AddTags(sTagEntity, StartingTag, AddedTag), Is.False);
+ Assert.That(tagSystem.AddTags(sTagEntity, new List<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.False);
+ Assert.That(tagSystem.AddTags(sTagEntity, new HashSet<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.False);
// Now has two tags
Assert.That(sTagComponent.Tags, Has.Count.EqualTo(2));
// Has both tags
Assert.That(tagSystem.HasTag(sTagComponent, StartingTag), Is.True);
Assert.That(tagSystem.HasTag(sTagComponent, AddedTag), Is.True);
+
Assert.That(tagSystem.HasAllTags(sTagComponent, StartingTag, StartingTag), Is.True);
Assert.That(tagSystem.HasAllTags(sTagComponent, AddedTag, StartingTag), Is.True);
- Assert.That(tagSystem.HasAllTags(sTagComponent, new List<string> { StartingTag, AddedTag }), Is.True);
- Assert.That(tagSystem.HasAllTags(sTagComponent, new List<string> { AddedTag, StartingTag }), Is.True);
+ Assert.That(tagSystem.HasAllTags(sTagComponent, new List<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.True);
+ Assert.That(tagSystem.HasAllTags(sTagComponent, new List<ProtoId<TagPrototype>> { AddedTag, StartingTag }), Is.True);
+ Assert.That(tagSystem.HasAllTags(sTagComponent, new HashSet<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.True);
+ Assert.That(tagSystem.HasAllTags(sTagComponent, new HashSet<ProtoId<TagPrototype>> { AddedTag, StartingTag }), Is.True);
+
Assert.That(tagSystem.HasAnyTag(sTagComponent, StartingTag, AddedTag), Is.True);
Assert.That(tagSystem.HasAnyTag(sTagComponent, AddedTag, StartingTag), Is.True);
+ Assert.That(tagSystem.HasAnyTag(sTagComponent, new List<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.True);
+ Assert.That(tagSystem.HasAnyTag(sTagComponent, new List<ProtoId<TagPrototype>> { AddedTag, StartingTag }), Is.True);
+ Assert.That(tagSystem.HasAnyTag(sTagComponent, new HashSet<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.True);
+ Assert.That(tagSystem.HasAnyTag(sTagComponent, new HashSet<ProtoId<TagPrototype>> { AddedTag, StartingTag }), Is.True);
});
Assert.Multiple(() =>
{
// Remove the existing starting tag
- Assert.That(tagSystem.RemoveTag(sTagDummy, sTagComponent, StartingTag), Is.True);
+ Assert.That(tagSystem.RemoveTag(sTagEntity, StartingTag), Is.True);
// Remove the existing added tag
- Assert.That(tagSystem.RemoveTags(sTagDummy, sTagComponent, AddedTag, AddedTag), Is.True);
+ Assert.That(tagSystem.RemoveTags(sTagEntity, AddedTag, AddedTag), Is.True);
});
Assert.Multiple(() =>
{
// No tags left to remove
- Assert.That(tagSystem.RemoveTags(sTagDummy, sTagComponent, new List<string> { StartingTag, AddedTag }), Is.False);
+ Assert.That(tagSystem.RemoveTags(sTagEntity, new List<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.False);
// No tags left in the component
Assert.That(sTagComponent.Tags, Is.Empty);
});
-#if !DEBUG
- return;
+ // It is run only in DEBUG build,
+ // as the checks are performed only in DEBUG build.
+#if DEBUG
+ // Has single
+ Assert.Throws<DebugAssertException>(() => { tagSystem.HasTag(sTagDummy, UnregisteredTag); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.HasTag(sTagComponent, UnregisteredTag); });
+
+ // HasAny entityUid methods
+ Assert.Throws<DebugAssertException>(() => { tagSystem.HasAnyTag(sTagDummy, UnregisteredTag); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.HasAnyTag(sTagDummy, UnregisteredTag, UnregisteredTag); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.HasAnyTag(sTagDummy, new List<ProtoId<TagPrototype>> { UnregisteredTag }); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.HasAnyTag(sTagDummy, new HashSet<ProtoId<TagPrototype>> { UnregisteredTag }); });
+
+ // HasAny component methods
+ Assert.Throws<DebugAssertException>(() => { tagSystem.HasAnyTag(sTagComponent, UnregisteredTag); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.HasAnyTag(sTagComponent, UnregisteredTag, UnregisteredTag); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.HasAnyTag(sTagComponent, new List<ProtoId<TagPrototype>> { UnregisteredTag }); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.HasAnyTag(sTagComponent, new HashSet<ProtoId<TagPrototype>> { UnregisteredTag }); });
+
+ // HasAll entityUid methods
+ Assert.Throws<DebugAssertException>(() => { tagSystem.HasAllTags(sTagDummy, UnregisteredTag); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.HasAllTags(sTagDummy, UnregisteredTag, UnregisteredTag); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.HasAllTags(sTagDummy, new List<ProtoId<TagPrototype>> { UnregisteredTag }); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.HasAllTags(sTagDummy, new HashSet<ProtoId<TagPrototype>> { UnregisteredTag }); });
+
+ // HasAll component methods
+ Assert.Throws<DebugAssertException>(() => { tagSystem.HasAllTags(sTagComponent, UnregisteredTag); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.HasAllTags(sTagComponent, UnregisteredTag, UnregisteredTag); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.HasAllTags(sTagComponent, new List<ProtoId<TagPrototype>> { UnregisteredTag }); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.HasAllTags(sTagComponent, new HashSet<ProtoId<TagPrototype>> { UnregisteredTag }); });
+
+ // RemoveTag single
+ Assert.Throws<DebugAssertException>(() => { tagSystem.RemoveTag(sTagDummy, UnregisteredTag); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.RemoveTag(sTagEntity, UnregisteredTag); });
+
+ // RemoveTags entityUid methods
+ Assert.Throws<DebugAssertException>(() => { tagSystem.RemoveTags(sTagDummy, UnregisteredTag); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.RemoveTags(sTagDummy, UnregisteredTag, UnregisteredTag); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.RemoveTags(sTagDummy, new List<ProtoId<TagPrototype>> { UnregisteredTag }); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.RemoveTags(sTagDummy, new HashSet<ProtoId<TagPrototype>> { UnregisteredTag }); });
+
+ // RemoveTags entity methods
+ Assert.Throws<DebugAssertException>(() => { tagSystem.RemoveTags(sTagEntity, UnregisteredTag); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.RemoveTags(sTagEntity, UnregisteredTag, UnregisteredTag); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.RemoveTags(sTagEntity, new List<ProtoId<TagPrototype>> { UnregisteredTag }); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.RemoveTags(sTagEntity, new HashSet<ProtoId<TagPrototype>> { UnregisteredTag }); });
+
+ // AddTag single
+ Assert.Throws<DebugAssertException>(() => { tagSystem.AddTag(sTagDummy, UnregisteredTag); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.AddTag(sTagEntity, UnregisteredTag); });
+
+ // AddTags entityUid methods
+ Assert.Throws<DebugAssertException>(() => { tagSystem.AddTags(sTagDummy, UnregisteredTag); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.AddTags(sTagDummy, UnregisteredTag, UnregisteredTag); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.AddTags(sTagDummy, new List<ProtoId<TagPrototype>> { UnregisteredTag }); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.AddTags(sTagDummy, new HashSet<ProtoId<TagPrototype>> { UnregisteredTag }); });
+
+ // AddTags entity methods
+ Assert.Throws<DebugAssertException>(() => { tagSystem.AddTags(sTagEntity, UnregisteredTag); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.AddTags(sTagEntity, UnregisteredTag, UnregisteredTag); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.AddTags(sTagEntity, new List<ProtoId<TagPrototype>> { UnregisteredTag }); });
+ Assert.Throws<DebugAssertException>(() => { tagSystem.AddTags(sTagEntity, new HashSet<ProtoId<TagPrototype>> { UnregisteredTag }); });
#endif
-
- // Single
- Assert.Throws<DebugAssertException>(() =>
- {
- tagSystem.HasTag(sTagDummy, UnregisteredTag);
- });
- Assert.Throws<DebugAssertException>(() =>
- {
- tagSystem.HasTag(sTagComponent, UnregisteredTag);
- });
-
- // Any
- Assert.Throws<DebugAssertException>(() =>
- {
- tagSystem.HasAnyTag(sTagDummy, UnregisteredTag);
- });
- Assert.Throws<DebugAssertException>(() =>
- {
- tagSystem.HasAnyTag(sTagComponent, UnregisteredTag);
- });
-
- // All
- Assert.Throws<DebugAssertException>(() =>
- {
- tagSystem.HasAllTags(sTagDummy, UnregisteredTag);
- });
- Assert.Throws<DebugAssertException>(() =>
- {
- tagSystem.HasAllTags(sTagComponent, UnregisteredTag);
- });
});
await pair.CleanReturnAsync();
}
-using System.Diagnostics;
-using System.Linq;
-using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Shared.Tag;
+/// <summary>
+/// The system that is responsible for working with tags.
+/// Checking the existence of the <see cref="TagPrototype"/> only happens in DEBUG builds,
+/// to improve performance, so don't forget to check it.
+/// </summary>
+/// <summary>
+/// The methods to add or remove a list of tags have only an implementation with the <see cref="IEnumerable{T}"/> type,
+/// it's not much, but it takes away performance,
+/// if you need to use them often, it's better to make a proper implementation,
+/// you can read more <a href="https://github.com/space-wizards/space-station-14/pull/28272">HERE</a>.
+/// </summary>
public sealed class TagSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _proto = default!;
public override void Initialize()
{
base.Initialize();
+
_tagQuery = GetEntityQuery<TagComponent>();
- SubscribeLocalEvent<TagComponent, ComponentGetState>(OnTagGetState);
- SubscribeLocalEvent<TagComponent, ComponentHandleState>(OnTagHandleState);
#if DEBUG
SubscribeLocalEvent<TagComponent, ComponentInit>(OnTagInit);
+#endif
}
+#if DEBUG
private void OnTagInit(EntityUid uid, TagComponent component, ComponentInit args)
{
foreach (var tag in component.Tags)
{
AssertValidTag(tag);
}
-#endif
- }
-
-
- private void OnTagHandleState(EntityUid uid, TagComponent component, ref ComponentHandleState args)
- {
- if (args.Current is not TagComponentState state)
- return;
-
- component.Tags.Clear();
-
- foreach (var tag in state.Tags)
- {
- AssertValidTag(tag);
- component.Tags.Add(tag);
- }
- }
-
- private static void OnTagGetState(EntityUid uid, TagComponent component, ref ComponentGetState args)
- {
- var tags = new string[component.Tags.Count];
- var i = 0;
-
- foreach (var tag in component.Tags)
- {
- tags[i] = tag;
- i++;
- }
-
- args.State = new TagComponentState(tags);
- }
-
- private void AssertValidTag(string id)
- {
- DebugTools.Assert(_proto.HasIndex<TagPrototype>(id), $"Unknown tag: {id}");
}
+#endif
/// <summary>
- /// Tries to add a tag to an entity if the tag doesn't already exist.
+ /// Tries to add a tag to an entity if the tag doesn't already exist.
/// </summary>
- /// <param name="entity">The entity to add the tag to.</param>
- /// <param name="id">The tag to add.</param>
/// <returns>
- /// true if it was added, false otherwise even if it already existed.
+ /// true if it was added, false otherwise even if it already existed.
/// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if no <see cref="TagPrototype"/> exists with the given id.
+ /// Thrown if no <see cref="TagPrototype"/> exists with the given id.
/// </exception>
- public bool AddTag(EntityUid entity, string id)
+ public bool AddTag(EntityUid entityUid, ProtoId<TagPrototype> tag)
{
- return AddTag(entity, EnsureComp<TagComponent>(entity), id);
+ return AddTag((entityUid, EnsureComp<TagComponent>(entityUid)), tag);
}
/// <summary>
- /// Tries to add the given tags to an entity if the tags don't already exist.
+ /// Tries to add the given tags to an entity if the tags don't already exist.
/// </summary>
- /// <param name="entity">The entity to add the tag to.</param>
- /// <param name="ids">The tags to add.</param>
/// <returns>
- /// true if any tags were added, false otherwise even if they all already existed.
+ /// true if any tags were added, false otherwise even if they all already existed.
/// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool AddTags(EntityUid entity, params string[] ids)
+ public bool AddTags(EntityUid entityUid, params ProtoId<TagPrototype>[] tags)
{
- return AddTags(entity, EnsureComp<TagComponent>(entity), ids);
+ return AddTags(entityUid, (IEnumerable<ProtoId<TagPrototype>>)tags);
}
/// <summary>
- /// Tries to add the given tags to an entity if the tags don't already exist.
+ /// Tries to add the given tags to an entity if the tags don't already exist.
/// </summary>
- /// <param name="entity">The entity to add the tag to.</param>
- /// <param name="ids">The tags to add.</param>
/// <returns>
- /// true if any tags were added, false otherwise even if they all already existed.
+ /// true if any tags were added, false otherwise even if they all already existed.
/// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool AddTags(EntityUid entity, IEnumerable<string> ids)
+ public bool AddTags(EntityUid entityUid, IEnumerable<ProtoId<TagPrototype>> tags)
{
- return AddTags(entity, EnsureComp<TagComponent>(entity), ids);
+ return AddTags((entityUid, EnsureComp<TagComponent>(entityUid)), tags);
}
/// <summary>
- /// Tries to add a tag to an entity if it has a <see cref="TagComponent"/>
- /// and the tag doesn't already exist.
+ /// Tries to add a tag to an entity if it has a <see cref="TagComponent"/>
+ /// and the tag doesn't already exist.
/// </summary>
- /// <param name="entity">The entity to add the tag to.</param>
- /// <param name="id">The tag to add.</param>
/// <returns>
- /// true if it was added, false otherwise even if it already existed.
+ /// true if it was added, false otherwise even if it already existed.
/// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if no <see cref="TagPrototype"/> exists with the given id.
+ /// Thrown if no <see cref="TagPrototype"/> exists with the given id.
/// </exception>
- public bool TryAddTag(EntityUid entity, string id)
+ public bool TryAddTag(EntityUid entityUid, ProtoId<TagPrototype> tag)
{
- return _tagQuery.TryComp(entity, out var component) &&
- AddTag(entity, component, id);
+ return _tagQuery.TryComp(entityUid, out var component) &&
+ AddTag((entityUid, component), tag);
}
/// <summary>
- /// Tries to add the given tags to an entity if it has a
- /// <see cref="TagComponent"/> and the tags don't already exist.
+ /// Tries to add the given tags to an entity if it has a
+ /// <see cref="TagComponent"/> and the tags don't already exist.
/// </summary>
- /// <param name="entity">The entity to add the tag to.</param>
- /// <param name="ids">The tags to add.</param>
/// <returns>
- /// true if any tags were added, false otherwise even if they all already existed.
+ /// true if any tags were added, false otherwise even if they all already existed.
/// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool TryAddTags(EntityUid entity, params string[] ids)
+ public bool TryAddTags(EntityUid entityUid, params ProtoId<TagPrototype>[] tags)
{
- return _tagQuery.TryComp(entity, out var component) &&
- AddTags(entity, component, ids);
+ return TryAddTags(entityUid, (IEnumerable<ProtoId<TagPrototype>>)tags);
}
/// <summary>
- /// Tries to add the given tags to an entity if it has a
- /// <see cref="TagComponent"/> and the tags don't already exist.
+ /// Tries to add the given tags to an entity if it has a
+ /// <see cref="TagComponent"/> and the tags don't already exist.
/// </summary>
- /// <param name="entity">The entity to add the tag to.</param>
- /// <param name="ids">The tags to add.</param>
/// <returns>
- /// true if any tags were added, false otherwise even if they all already existed.
+ /// true if any tags were added, false otherwise even if they all already existed.
/// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool TryAddTags(EntityUid entity, IEnumerable<string> ids)
+ public bool TryAddTags(EntityUid entityUid, IEnumerable<ProtoId<TagPrototype>> tags)
{
- return _tagQuery.TryComp(entity, out var component) &&
- AddTags(entity, component, ids);
+ return _tagQuery.TryComp(entityUid, out var component) &&
+ AddTags((entityUid, component), tags);
}
/// <summary>
- /// Checks if a tag has been added to an entity.
+ /// Checks if a tag has been added to an entity.
/// </summary>
- /// <param name="entity">The entity to check.</param>
- /// <param name="id">The tag to check for.</param>
- /// <returns>true if it exists, false otherwise.</returns>
+ /// <returns>
+ /// true if it exists, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if no <see cref="TagPrototype"/> exists with the given id.
+ /// Thrown if no <see cref="TagPrototype"/> exists with the given id.
/// </exception>
- public bool HasTag(EntityUid entity, string id)
- {
- return _tagQuery.TryComp(entity, out var component) &&
- HasTag(component, id);
- }
-
- /// <summary>
- /// Checks if a tag has been added to an entity.
- /// </summary>
- [Obsolete]
- public bool HasTag(EntityUid entity, string id, EntityQuery<TagComponent> tagQuery)
+ public bool HasTag(EntityUid entityUid, ProtoId<TagPrototype> tag)
{
- return tagQuery.TryGetComponent(entity, out var component) &&
- HasTag(component, id);
+ return _tagQuery.TryComp(entityUid, out var component) &&
+ HasTag(component, tag);
}
/// <summary>
- /// Checks if all of the given tags have been added to an entity.
+ /// Checks if a tag has been added to an entity.
/// </summary>
- /// <param name="entity">The entity to check.</param>
- /// <param name="id">The tags to check for.</param>
- /// <returns>true if they all exist, false otherwise.</returns>
+ /// <returns>
+ /// true if it exists, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if no <see cref="TagPrototype"/> exists with the given id.
/// </exception>
- public bool HasAllTags(EntityUid entity, string id) => HasTag(entity, id);
+ public bool HasAllTags(EntityUid entityUid, ProtoId<TagPrototype> tag) =>
+ HasTag(entityUid, tag);
/// <summary>
- /// Checks if all of the given tags have been added to an entity.
+ /// Checks if all of the given tags have been added to an entity.
/// </summary>
- /// <param name="entity">The entity to check.</param>
- /// <param name="ids">The tags to check for.</param>
- /// <returns>true if they all exist, false otherwise.</returns>
+ /// <returns>
+ /// true if they all exist, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool HasAllTags(EntityUid entity, List<string> ids)
+ public bool HasAllTags(EntityUid entityUid, params ProtoId<TagPrototype>[] tags)
{
- return _tagQuery.TryComp(entity, out var component) &&
- HasAllTags(component, ids);
+ return _tagQuery.TryComp(entityUid, out var component) &&
+ HasAllTags(component, tags);
}
/// <summary>
- /// Checks if all of the given tags have been added to an entity.
+ /// Checks if all of the given tags have been added to an entity.
/// </summary>
- /// <param name="entity">The entity to check.</param>
- /// <param name="ids">The tags to check for.</param>
- /// <returns>true if they all exist, false otherwise.</returns>
+ /// <returns>
+ /// true if they all exist, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool HasAllTags(EntityUid entity, IEnumerable<string> ids)
+ public bool HasAllTags(EntityUid entityUid, HashSet<ProtoId<TagPrototype>> tags)
{
- return _tagQuery.TryComp(entity, out var component) &&
- HasAllTags(component, ids);
+ return _tagQuery.TryComp(entityUid, out var component) &&
+ HasAllTags(component, tags);
}
/// <summary>
- /// Checks if all of the given tags have been added to an entity.
+ /// Checks if all of the given tags have been added to an entity.
/// </summary>
- /// <param name="entity">The entity to check.</param>
- /// <param name="ids">The tags to check for.</param>
- /// <returns>true if they all exist, false otherwise.</returns>
+ /// <returns>
+ /// true if they all exist, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool HasAllTags(EntityUid entity, List<ProtoId<TagPrototype>> ids)
+ public bool HasAllTags(EntityUid entityUid, List<ProtoId<TagPrototype>> tags)
{
- return _tagQuery.TryComp(entity, out var component) &&
- HasAllTags(component, ids);
+ return _tagQuery.TryComp(entityUid, out var component) &&
+ HasAllTags(component, tags);
}
/// <summary>
- /// Checks if any of the given tags have been added to an entity.
+ /// Checks if all of the given tags have been added to an entity.
/// </summary>
- /// <param name="entity">The entity to check.</param>
- /// <param name="ids">The tags to check for.</param>
- /// <returns>true if any of them exist, false otherwise.</returns>
+ /// <returns>
+ /// true if they all exist, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool HasAnyTag(EntityUid entity, params string[] ids)
+ public bool HasAllTags(EntityUid entityUid, IEnumerable<ProtoId<TagPrototype>> tags)
{
- return _tagQuery.TryComp(entity, out var component) &&
- HasAnyTag(component, ids);
+ return _tagQuery.TryComp(entityUid, out var component) &&
+ HasAllTags(component, tags);
}
/// <summary>
- /// Checks if any of the given tags have been added to an entity.
+ /// Checks if a tag has been added to an entity.
/// </summary>
- /// <param name="entity">The entity to check.</param>
- /// <param name="id">The tag to check for.</param>
- /// <returns>true if any of them exist, false otherwise.</returns>
+ /// <returns>
+ /// true if it exists, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if no <see cref="TagPrototype"/> exists with the given id.
/// </exception>
- public bool HasAnyTag(EntityUid entity, string id) => HasTag(entity, id);
+ public bool HasAnyTag(EntityUid entityUid, ProtoId<TagPrototype> tag) =>
+ HasTag(entityUid, tag);
/// <summary>
- /// Checks if any of the given tags have been added to an entity.
+ /// Checks if any of the given tags have been added to an entity.
/// </summary>
- /// <param name="entity">The entity to check.</param>
- /// <param name="ids">The tags to check for.</param>
- /// <returns>true if any of them exist, false otherwise.</returns>
+ /// <returns>
+ /// true if any of them exist, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool HasAnyTag(EntityUid entity, List<string> ids)
+ public bool HasAnyTag(EntityUid entityUid, params ProtoId<TagPrototype>[] tags)
{
- return _tagQuery.TryComp(entity, out var component) &&
- HasAnyTag(component, ids);
+ return _tagQuery.TryComp(entityUid, out var component) &&
+ HasAnyTag(component, tags);
}
/// <summary>
- /// Checks if any of the given tags have been added to an entity.
+ /// Checks if any of the given tags have been added to an entity.
/// </summary>
- /// <param name="entity">The entity to check.</param>
- /// <param name="ids">The tags to check for.</param>
- /// <returns>true if any of them exist, false otherwise.</returns>
+ /// <returns>
+ /// true if any of them exist, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool HasAnyTag(EntityUid entity, List<ProtoId<TagPrototype>> ids)
+ public bool HasAnyTag(EntityUid entityUid, HashSet<ProtoId<TagPrototype>> tags)
{
- return TryComp<TagComponent>(entity, out var component) &&
- HasAnyTag(component, ids);
+ return _tagQuery.TryComp(entityUid, out var component) &&
+ HasAnyTag(component, tags);
}
/// <summary>
- /// Checks if any of the given tags have been added to an entity.
+ /// Checks if any of the given tags have been added to an entity.
/// </summary>
- /// <param name="entity">The entity to check.</param>
- /// <param name="ids">The tags to check for.</param>
- /// <returns>true if any of them exist, false otherwise.</returns>
+ /// <returns>
+ /// true if any of them exist, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool HasAnyTag(EntityUid entity, IEnumerable<string> ids)
+ public bool HasAnyTag(EntityUid entityUid, List<ProtoId<TagPrototype>> tags)
{
- return _tagQuery.TryComp(entity, out var component) &&
- HasAnyTag(component, ids);
+ return _tagQuery.TryComp(entityUid, out var component) &&
+ HasAnyTag(component, tags);
}
/// <summary>
- /// Tries to remove a tag from an entity if it exists.
+ /// Checks if any of the given tags have been added to an entity.
/// </summary>
- /// <param name="entity">The entity to remove the tag from.</param>
- /// <param name="id">The tag to remove.</param>
/// <returns>
- /// true if it was removed, false otherwise even if it didn't exist.
+ /// true if any of them exist, false otherwise.
/// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if no <see cref="TagPrototype"/> exists with the given id.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool RemoveTag(EntityUid entity, string id)
+ public bool HasAnyTag(EntityUid entityUid, IEnumerable<ProtoId<TagPrototype>> tags)
{
- return _tagQuery.TryComp(entity, out var component) &&
- RemoveTag(entity, component, id);
+ return _tagQuery.TryComp(entityUid, out var component) &&
+ HasAnyTag(component, tags);
}
/// <summary>
- /// Tries to remove a tag from an entity if it exists.
+ /// Checks if a tag has been added to an component.
/// </summary>
- /// <param name="entity">The entity to remove the tag from.</param>
- /// <param name="ids">The tag to remove.</param>
/// <returns>
- /// true if it was removed, false otherwise even if it didn't exist.
+ /// true if it exists, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if no <see cref="TagPrototype"/> exists with the given id.
/// </exception>
- /// </returns>
- public bool RemoveTags(EntityUid entity, params string[] ids)
+ public bool HasTag(TagComponent component, ProtoId<TagPrototype> tag)
{
- return _tagQuery.TryComp(entity, out var component) &&
- RemoveTags(entity, component, ids);
+#if DEBUG
+ AssertValidTag(tag);
+#endif
+ return component.Tags.Contains(tag);
}
/// <summary>
- /// Tries to remove a tag from an entity if it exists.
+ /// Checks if a tag has been added to an component.
/// </summary>
- /// <param name="entity">The entity to remove the tag from.</param>
- /// <param name="ids">The tag to remove.</param>
/// <returns>
- /// true if it was removed, false otherwise even if it didn't exist.
+ /// true if it exists, false otherwise.
/// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if no <see cref="TagPrototype"/> exists with the given id.
/// </exception>
- public bool RemoveTags(EntityUid entity, IEnumerable<string> ids)
- {
- return _tagQuery.TryComp(entity, out var component) &&
- RemoveTags(entity, component, ids);
- }
+ public bool HasAllTags(TagComponent component, ProtoId<TagPrototype> tag) =>
+ HasTag(component, tag);
/// <summary>
- /// Tries to add a tag if it doesn't already exist.
+ /// Checks if all of the given tags have been added to an component.
/// </summary>
- /// <param name="id">The tag to add.</param>
- /// <returns>true if it was added, false if it already existed.</returns>
+ /// <returns>
+ /// true if they all exist, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if no <see cref="TagPrototype"/> exists with the given id.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool AddTag(EntityUid uid, TagComponent component, string id)
+ public bool HasAllTags(TagComponent component, params ProtoId<TagPrototype>[] tags)
{
- AssertValidTag(id);
- var added = component.Tags.Add(id);
-
- if (added)
+ foreach (var tag in tags)
{
- Dirty(uid, component);
- return true;
+#if DEBUG
+ AssertValidTag(tag);
+#endif
+ if (!component.Tags.Contains(tag))
+ return false;
}
- return false;
+ return true;
}
/// <summary>
- /// Tries to add the given tags if they don't already exist.
+ /// Checks if all of the given tags have been added to an component.
/// </summary>
- /// <param name="ids">The tags to add.</param>
- /// <returns>true if any tags were added, false if they all already existed.</returns>
+ /// <returns>
+ /// true if they all exist, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool AddTags(EntityUid uid, TagComponent component, params string[] ids)
+ public bool HasAllTagsArray(TagComponent component, ProtoId<TagPrototype>[] tags)
{
- return AddTags(uid, component, ids.AsEnumerable());
+ foreach (var tag in tags)
+ {
+#if DEBUG
+ AssertValidTag(tag);
+#endif
+ if (!component.Tags.Contains(tag))
+ return false;
+ }
+
+ return true;
}
/// <summary>
- /// Tries to add the given tags if they don't already exist.
+ /// Checks if all of the given tags have been added to an component.
/// </summary>
- /// <param name="ids">The tags to add.</param>
- /// <returns>true if any tags were added, false if they all already existed.</returns>
+ /// <returns>
+ /// true if they all exist, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool AddTags(EntityUid uid, TagComponent component, IEnumerable<string> ids)
+ public bool HasAllTags(TagComponent component, List<ProtoId<TagPrototype>> tags)
{
- var count = component.Tags.Count;
-
- foreach (var id in ids)
- {
- AssertValidTag(id);
- component.Tags.Add(id);
- }
-
- if (component.Tags.Count > count)
+ foreach (var tag in tags)
{
- Dirty(uid, component);
- return true;
+#if DEBUG
+ AssertValidTag(tag);
+#endif
+ if (!component.Tags.Contains(tag))
+ return false;
}
- return false;
+ return true;
}
/// <summary>
- /// Checks if a tag has been added.
+ /// Checks if all of the given tags have been added to an component.
/// </summary>
- /// <param name="id">The tag to check for.</param>
- /// <returns>true if it exists, false otherwise.</returns>
+ /// <returns>
+ /// true if they all exist, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if no <see cref="TagPrototype"/> exists with the given id.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool HasTag(TagComponent component, string id)
+ public bool HasAllTags(TagComponent component, HashSet<ProtoId<TagPrototype>> tags)
{
- AssertValidTag(id);
- return component.Tags.Contains(id);
+ foreach (var tag in tags)
+ {
+#if DEBUG
+ AssertValidTag(tag);
+#endif
+ if (!component.Tags.Contains(tag))
+ return false;
+ }
+
+ return true;
}
/// <summary>
- /// Checks if all of the given tags have been added.
+ /// Checks if all of the given tags have been added to an component.
/// </summary>
- /// <param name="ids">The tags to check for.</param>
- /// <returns>true if they all exist, false otherwise.</returns>
+ /// <returns>
+ /// true if they all exist, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool HasAllTags(TagComponent component, params string[] ids)
+ public bool HasAllTags(TagComponent component, IEnumerable<ProtoId<TagPrototype>> tags)
{
- return HasAllTags(component, ids.AsEnumerable());
+ foreach (var tag in tags)
+ {
+#if DEBUG
+ AssertValidTag(tag);
+#endif
+ if (!component.Tags.Contains(tag))
+ return false;
+ }
+
+ return true;
}
/// <summary>
- /// Checks if all of the given tags have been added.
+ /// Checks if a tag has been added to an component.
/// </summary>
- /// <param name="id">The tag to check for.</param>
- /// <returns>true if they all exist, false otherwise.</returns>
+ /// <returns>
+ /// true if it exists, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if no <see cref="TagPrototype"/> exists with the given id.
/// </exception>
- public bool HasAllTags(TagComponent component, string id) => HasTag(component, id);
+ public bool HasAnyTag(TagComponent component, ProtoId<TagPrototype> tag) =>
+ HasTag(component, tag);
/// <summary>
- /// Checks if all of the given tags have been added.
+ /// Checks if any of the given tags have been added to an component.
/// </summary>
- /// <param name="ids">The tags to check for.</param>
- /// <returns>true if they all exist, false otherwise.</returns>
+ /// <returns>
+ /// true if any of them exist, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool HasAllTags(TagComponent component, List<string> ids)
+ public bool HasAnyTag(TagComponent component, params ProtoId<TagPrototype>[] tags)
{
- foreach (var id in ids)
+ foreach (var tag in tags)
{
- AssertValidTag(id);
-
- if (!component.Tags.Contains(id))
- return false;
+#if DEBUG
+ AssertValidTag(tag);
+#endif
+ if (component.Tags.Contains(tag))
+ return true;
}
- return true;
+ return false;
}
/// <summary>
- /// Checks if all of the given tags have been added.
+ /// Checks if any of the given tags have been added to an component.
/// </summary>
- /// <param name="ids">The tags to check for.</param>
- /// <returns>true if they all exist, false otherwise.</returns>
+ /// <returns>
+ /// true if any of them exist, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool HasAllTags(TagComponent component, IEnumerable<string> ids)
+ public bool HasAnyTag(TagComponent component, HashSet<ProtoId<TagPrototype>> tags)
{
- foreach (var id in ids)
+ foreach (var tag in tags)
{
- AssertValidTag(id);
-
- if (!component.Tags.Contains(id))
- return false;
-
+#if DEBUG
+ AssertValidTag(tag);
+#endif
+ if (component.Tags.Contains(tag))
+ return true;
}
- return true;
+ return false;
}
/// <summary>
- /// Checks if all of the given tags have been added.
+ /// Checks if any of the given tags have been added to an component.
/// </summary>
- /// <param name="ids">The tags to check for.</param>
- /// <returns>true if they all exist, false otherwise.</returns>
+ /// <returns>
+ /// true if any of them exist, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool HasAllTags(TagComponent component, List<ProtoId<TagPrototype>> ids)
+ public bool HasAnyTag(TagComponent component, List<ProtoId<TagPrototype>> tags)
{
- foreach (var id in ids)
+ foreach (var tag in tags)
{
- AssertValidTag(id);
-
- if (!component.Tags.Contains(id))
- return false;
-
+#if DEBUG
+ AssertValidTag(tag);
+#endif
+ if (component.Tags.Contains(tag))
+ return true;
}
- return true;
+ return false;
}
/// <summary>
- /// Checks if any of the given tags have been added.
+ /// Checks if any of the given tags have been added to an component.
/// </summary>
- /// <param name="ids">The tags to check for.</param>
- /// <returns>true if any of them exist, false otherwise.</returns>
+ /// <returns>
+ /// true if any of them exist, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool HasAnyTag(TagComponent component, params string[] ids)
+ public bool HasAnyTag(TagComponent component, IEnumerable<ProtoId<TagPrototype>> tags)
{
- foreach (var id in ids)
+ foreach (var tag in tags)
{
- AssertValidTag(id);
-
- if (component.Tags.Contains(id))
+#if DEBUG
+ AssertValidTag(tag);
+#endif
+ if (component.Tags.Contains(tag))
return true;
}
}
/// <summary>
- /// Checks if any of the given tags have been added.
+ /// Tries to remove a tag from an entity if it exists.
/// </summary>
- /// <param name="id">The tag to check for.</param>
- /// <returns>true if any of them exist, false otherwise.</returns>
+ /// <returns>
+ /// true if it was removed, false otherwise even if it didn't exist.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if no <see cref="TagPrototype"/> exists with the given id.
/// </exception>
- public bool HasAnyTag(TagComponent component, string id) => HasTag(component, id);
+ public bool RemoveTag(EntityUid entityUid, ProtoId<TagPrototype> tag)
+ {
+ return _tagQuery.TryComp(entityUid, out var component) &&
+ RemoveTag((entityUid, component), tag);
+ }
/// <summary>
- /// Checks if any of the given tags have been added.
+ /// Tries to remove a tag from an entity if it exists.
/// </summary>
- /// <param name="ids">The tags to check for.</param>
- /// <returns>true if any of them exist, false otherwise.</returns>
+ /// <returns>
+ /// true if it was removed, false otherwise even if it didn't exist.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool HasAnyTag(TagComponent component, List<string> ids)
+ public bool RemoveTags(EntityUid entityUid, params ProtoId<TagPrototype>[] tags)
{
- foreach (var id in ids)
- {
- AssertValidTag(id);
-
- if (component.Tags.Contains(id))
- {
- return true;
- }
- }
+ return RemoveTags(entityUid, (IEnumerable<ProtoId<TagPrototype>>)tags);
+ }
- return false;
+ /// <summary>
+ /// Tries to remove a tag from an entity if it exists.
+ /// </summary>
+ /// <returns>
+ /// true if it was removed, false otherwise even if it didn't exist.
+ /// </returns>
+ /// <exception cref="UnknownPrototypeException">
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// </exception>
+ public bool RemoveTags(EntityUid entityUid, IEnumerable<ProtoId<TagPrototype>> tags)
+ {
+ return _tagQuery.TryComp(entityUid, out var component) &&
+ RemoveTags((entityUid, component), tags);
}
/// <summary>
- /// Checks if any of the given tags have been added.
+ /// Tries to add a tag if it doesn't already exist.
/// </summary>
- /// <param name="ids">The tags to check for.</param>
- /// <returns>true if any of them exist, false otherwise.</returns>
+ /// <returns>
+ /// true if it was added, false if it already existed.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if no <see cref="TagPrototype"/> exists with the given id.
/// </exception>
- public bool HasAnyTag(TagComponent component, IEnumerable<string> ids)
+ public bool AddTag(Entity<TagComponent> entity, ProtoId<TagPrototype> tag)
{
- foreach (var id in ids)
- {
- AssertValidTag(id);
+#if DEBUG
+ AssertValidTag(tag);
+#endif
+ if (!entity.Comp.Tags.Add(tag))
+ return false;
- if (component.Tags.Contains(id))
- {
- return true;
- }
- }
+ Dirty(entity);
+ return true;
+ }
- return false;
+ /// <summary>
+ /// Tries to add the given tags if they don't already exist.
+ /// </summary>
+ /// <returns>
+ /// true if any tags were added, false if they all already existed.
+ /// </returns>
+ /// <exception cref="UnknownPrototypeException">
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// </exception>
+ public bool AddTags(Entity<TagComponent> entity, params ProtoId<TagPrototype>[] tags)
+ {
+ return AddTags(entity, (IEnumerable<ProtoId<TagPrototype>>)tags);
}
/// <summary>
- /// Checks if any of the given tags have been added.
+ /// Tries to add the given tags if they don't already exist.
/// </summary>
- /// <param name="ids">The tags to check for.</param>
- /// <returns>true if any of them exist, false otherwise.</returns>
+ /// <returns>
+ /// true if any tags were added, false if they all already existed.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool HasAnyTag(TagComponent comp, List<ProtoId<TagPrototype>> ids)
+ public bool AddTags(Entity<TagComponent> entity, IEnumerable<ProtoId<TagPrototype>> tags)
{
- foreach (var id in ids)
+ var update = false;
+ foreach (var tag in tags)
{
- AssertValidTag(id);
-
- if (comp.Tags.Contains(id))
- return true;
+#if DEBUG
+ AssertValidTag(tag);
+#endif
+ if (entity.Comp.Tags.Add(tag) && !update)
+ update = true;
}
- return false;
+ if (!update)
+ return false;
+
+ Dirty(entity);
+ return true;
}
/// <summary>
- /// Tries to remove a tag if it exists.
+ /// Tries to remove a tag if it exists.
/// </summary>
/// <returns>
- /// true if it was removed, false otherwise even if it didn't exist.
+ /// true if it was removed, false otherwise even if it didn't exist.
/// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if no <see cref="TagPrototype"/> exists with the given id.
+ /// Thrown if no <see cref="TagPrototype"/> exists with the given id.
/// </exception>
- public bool RemoveTag(EntityUid uid, TagComponent component, string id)
+ public bool RemoveTag(Entity<TagComponent> entity, ProtoId<TagPrototype> tag)
{
- AssertValidTag(id);
+#if DEBUG
+ AssertValidTag(tag);
+#endif
- if (component.Tags.Remove(id))
- {
- Dirty(uid, component);
- return true;
- }
+ if (!entity.Comp.Tags.Remove(tag))
+ return false;
- return false;
+ Dirty(entity);
+ return true;
}
/// <summary>
- /// Tries to remove all of the given tags if they exist.
+ /// Tries to remove all of the given tags if they exist.
/// </summary>
- /// <param name="ids">The tags to remove.</param>
/// <returns>
- /// true if it was removed, false otherwise even if they didn't exist.
+ /// true if any tag was removed, false otherwise.
/// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool RemoveTags(EntityUid uid, TagComponent component, params string[] ids)
+ public bool RemoveTags(Entity<TagComponent> entity, params ProtoId<TagPrototype>[] tags)
{
- return RemoveTags(uid, component, ids.AsEnumerable());
+ return RemoveTags(entity, (IEnumerable<ProtoId<TagPrototype>>)tags);
}
/// <summary>
- /// Tries to remove all of the given tags if they exist.
+ /// Tries to remove all of the given tags if they exist.
/// </summary>
- /// <param name="ids">The tags to remove.</param>
- /// <returns>true if any tag was removed, false otherwise.</returns>
+ /// <returns>
+ /// true if any tag was removed, false otherwise.
+ /// </returns>
/// <exception cref="UnknownPrototypeException">
- /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
+ /// Thrown if one of the ids represents an unregistered <see cref="TagPrototype"/>.
/// </exception>
- public bool RemoveTags(EntityUid uid, TagComponent component, IEnumerable<string> ids)
+ public bool RemoveTags(Entity<TagComponent> entity, IEnumerable<ProtoId<TagPrototype>> tags)
{
- var count = component.Tags.Count;
-
- foreach (var id in ids)
+ var update = false;
+ foreach (var tag in tags)
{
- AssertValidTag(id);
- component.Tags.Remove(id);
+#if DEBUG
+ AssertValidTag(tag);
+#endif
+ if (entity.Comp.Tags.Remove(tag) && !update)
+ update = true;
}
- if (component.Tags.Count < count)
- {
- Dirty(uid, component);
- return true;
- }
+ if (!update)
+ return false;
- return false;
+ Dirty(entity);
+ return true;
+ }
+
+ private void AssertValidTag(string id)
+ {
+ DebugTools.Assert(_proto.HasIndex<TagPrototype>(id), $"Unknown tag: {id}");
}
}