Skip to content

Commit 1c5ae48

Browse files
committed
add DefaultedTag
1 parent 356e1fa commit 1c5ae48

File tree

5 files changed

+124
-37
lines changed

5 files changed

+124
-37
lines changed

src/main/java/org/spongepowered/api/event/cause/entity/damage/DamageType.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.spongepowered.api.entity.Entity;
3030
import org.spongepowered.api.event.cause.entity.damage.source.DamageSource;
3131
import org.spongepowered.api.registry.DefaultedRegistryValue;
32-
import org.spongepowered.api.tag.Tag;
3332
import org.spongepowered.api.tag.Taggable;
3433
import org.spongepowered.api.util.CopyableBuilder;
3534
import org.spongepowered.api.util.Nameable;
@@ -65,15 +64,6 @@ static Builder builder() {
6564
*/
6665
double exhaustion();
6766

68-
/**
69-
* Checks whether this damage types matches a damage type tag.
70-
*
71-
* @param tag The tag to check.
72-
* @return true if this damage type matches the damage type tag.
73-
*/
74-
@Override
75-
boolean is(Tag<DamageType> tag);
76-
7767
/**
7868
* Returns the damage scaling.
7969
*

src/main/java/org/spongepowered/api/registry/Registry.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
import java.util.Objects;
3232
import java.util.Optional;
33-
import java.util.Set;
3433
import java.util.stream.Stream;
3534

3635
/**
@@ -128,7 +127,7 @@ default <V extends T> Optional<V> findValue(final RegistryKey<T> key) {
128127
*
129128
* @return The registered types associated with given tag
130129
*/
131-
<V extends T> Set<V> taggedValues(Tag<T> tag);
130+
<V extends T> Stream<V> taggedValues(Tag<T> tag);
132131

133132
/**
134133
* Gets the tags associated with this registry.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.api.tag;
26+
27+
import org.spongepowered.api.ResourceKey;
28+
import org.spongepowered.api.registry.DefaultedRegistryType;
29+
import org.spongepowered.api.registry.RegistryHolder;
30+
import org.spongepowered.api.registry.ValueNotFoundException;
31+
32+
import java.util.Objects;
33+
import java.util.function.Supplier;
34+
import java.util.stream.Stream;
35+
36+
/**
37+
* A {@link Tag} paired with the {@link RegistryHolder}.
38+
*/
39+
public interface DefaultedTag<T> extends Tag<T> {
40+
41+
static <T> DefaultedTag<T> of(final DefaultedRegistryType<T> registryType, final ResourceKey location) {
42+
return Tag.of(registryType, location).asDefaultedTag(registryType.defaultHolder());
43+
}
44+
45+
Supplier<RegistryHolder> defaultHolder();
46+
47+
/**
48+
* Returns the {@link Stream} of values tagged by this tag in the {@link #defaultHolder()}.
49+
*
50+
* <p>Great care needs to be made in calling this method with any uncertainty as to
51+
* if the {@link #registry()} will exist in the {@link #defaultHolder()}. Should the
52+
* key lack a value, a {@link ValueNotFoundException} will be thrown. Therefore, it
53+
* is advised to call {@link #findValues()} instead.</p>
54+
*
55+
* @return The {@link Stream} of values
56+
*/
57+
default Stream<T> values() {
58+
return this.defaultHolder().get().registry(this.registry()).taggedValues(this);
59+
}
60+
61+
/**
62+
* Returns the {@link Stream} of values tagged by this tag in the {@link #defaultHolder()}
63+
* if it contains this tag's {@link #registry()}, or {@link Stream#empty()} otherwise.
64+
*
65+
* @return The {@link Stream} of values
66+
*/
67+
default Stream<T> findValues() {
68+
return this.defaultHolder().get().findRegistry(this.registry()).map(r -> r.taggedValues(this)).orElseGet(Stream::empty);
69+
}
70+
71+
/**
72+
* Returns whether this tag is associated with the given value in the {@link #defaultHolder()}.
73+
*
74+
* @param value The value
75+
* @return true if this tag is associated with the given value in the {@link #defaultHolder()}
76+
*/
77+
default boolean contains(final T value) {
78+
return this.findValues().anyMatch(Objects.requireNonNull(value, "value")::equals);
79+
}
80+
}

src/main/java/org/spongepowered/api/tag/Tag.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
import org.spongepowered.api.ResourceKeyed;
3030
import org.spongepowered.api.Sponge;
3131
import org.spongepowered.api.block.BlockType;
32-
import org.spongepowered.api.registry.DefaultedRegistryType;
32+
import org.spongepowered.api.registry.RegistryHolder;
3333
import org.spongepowered.api.registry.RegistryType;
3434

35+
import java.util.function.Supplier;
36+
3537
/**
3638
* A {@link ResourceKey resource keyed} collection of {@link Taggable} values
3739
* (of type {@code T}).
@@ -50,26 +52,22 @@
5052
*/
5153
public interface Tag<T> extends ResourceKeyed {
5254

55+
static <T> Tag<T> of(final RegistryType<T> registryType, final ResourceKey key) {
56+
return Sponge.game().factoryProvider().provide(Tag.Factory.class).of(registryType, key);
57+
}
58+
5359
/**
5460
* Gets the {@link RegistryType location} defining the parent registry.
5561
*
5662
* @return The location
5763
*/
5864
RegistryType<T> registry();
5965

60-
interface Factory {
61-
default <T> Tag<T> of(DefaultedRegistryType<T> registryType, ResourceKey key) {
62-
return this.of((RegistryType<T>) registryType, key);
63-
}
66+
DefaultedTag<T> asDefaultedTag(Supplier<RegistryHolder> holder);
6467

65-
<T> Tag<T> of(RegistryType<T> registryType, ResourceKey key);
66-
}
68+
DefaultedTag<T> asScopedTag();
6769

68-
static <T> Tag<T> of(DefaultedRegistryType<T> registryType, ResourceKey key) {
69-
return Sponge.game().factoryProvider().provide(Tag.Factory.class).of(registryType, key);
70-
}
71-
72-
static <T> Tag<T> of(RegistryType<T> registryType, ResourceKey key) {
73-
return Sponge.game().factoryProvider().provide(Tag.Factory.class).of(registryType, key);
70+
interface Factory {
71+
<T> Tag<T> of(RegistryType<T> registryType, ResourceKey key);
7472
}
7573
}

src/main/java/org/spongepowered/api/tag/Taggable.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,55 @@
2626

2727
import org.spongepowered.api.registry.DefaultedRegistryType;
2828
import org.spongepowered.api.registry.DefaultedRegistryValue;
29-
import org.spongepowered.api.registry.RegistryType;
29+
import org.spongepowered.api.registry.Registry;
30+
import org.spongepowered.api.registry.RegistryHolder;
3031

31-
import java.util.Collection;
32+
import java.util.Objects;
33+
import java.util.stream.Stream;
3234

3335
/**
3436
* A type that may be included in one or more {@link Tag} collections.
3537
*/
38+
@SuppressWarnings("unchecked")
3639
public interface Taggable<T extends Taggable<T>> extends DefaultedRegistryValue {
3740

3841
/**
39-
* Gets the {@link RegistryType} that holds the types of {@link Tag tags}
40-
* that can be associated with this object.
42+
* Gets all {@link Tag}s that have been associated with this object in the given registry.
4143
*
42-
* @return The {@link RegistryType}
44+
* @return The {@link Stream} of {@link Tag}s.
4345
*/
44-
DefaultedRegistryType<T> registryType();
46+
default Stream<Tag<T>> tags(final DefaultedRegistryType<T> registryType) {
47+
final Registry<T> registry = Objects.requireNonNull(registryType, "registryType").get();
48+
return registry.tags().filter(tag -> this.is(registry, tag));
49+
}
4550

4651
/**
47-
* Gets all {@link Tag tags} that have been associated with this object.
52+
* Returns whether the given tag is associated with this object in the given registry holder.
4853
*
49-
* @return The {@link Collection} of {@link Tag}s.
54+
* @param tag The tag
55+
* @return true if the given tag is associated with this object in the given registry holder
5056
*/
51-
Collection<Tag<T>> tags();
57+
default boolean is(final RegistryHolder registryHolder, final Tag<T> tag) {
58+
return registryHolder.findRegistry(tag.registry()).map(r -> this.is(r, tag)).orElse(false);
59+
}
5260

5361
/**
54-
* Returns true when given tag is associated with this object
62+
* Returns whether the given tag is associated with this object in the given registry.
63+
*
5564
* @param tag The tag
56-
* @return true when given tag is associated with this object
65+
* @return true if the given tag is associated with this object in the given registry
5766
*/
58-
boolean is(Tag<T> tag);
67+
default boolean is(final Registry<T> registry, final Tag<T> tag) {
68+
return registry.taggedValues(tag).anyMatch(this::equals);
69+
}
5970

71+
/**
72+
* Returns whether the given tag is associated with this object.
73+
*
74+
* @param tag The tag
75+
* @return true if the given tag is associated with this object
76+
*/
77+
default boolean is(final DefaultedTag<T> tag) {
78+
return tag.contains((T) this);
79+
}
6080
}

0 commit comments

Comments
 (0)