Skip to content

Commit 24fe544

Browse files
committed
nuke RegistryValue, add overloads that take RegistrtHolder
1 parent 090b0d0 commit 24fe544

File tree

4 files changed

+81
-62
lines changed

4 files changed

+81
-62
lines changed

src/main/java/org/spongepowered/api/data/Keys.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@
252252
import org.spongepowered.api.projectile.source.ProjectileSource;
253253
import org.spongepowered.api.raid.Raid;
254254
import org.spongepowered.api.raid.RaidWave;
255-
import org.spongepowered.api.registry.DefaultedRegistryType;
256255
import org.spongepowered.api.statistic.Statistic;
257256
import org.spongepowered.api.tag.Tag;
258257
import org.spongepowered.api.util.Axis;
@@ -809,7 +808,7 @@ public final class Keys {
809808
* a {@link Player} uses the item. Can be used in tandem with the {@link #COOLDOWN} key
810809
* to apply a cooldown to said group. A group will differentiate a cooldown from the default
811810
* {@link ItemStackLike ItemStack} cooldown based on the
812-
* {@link ItemType#key(DefaultedRegistryType) ResourceKey}.
811+
* {@link ItemType#registryKey() ResourceKey}.
813812
*/
814813
public static final Key<Value<ResourceKey>> COOLDOWN_GROUP = Keys.key(ResourceKey.sponge("cooldown_group"), ResourceKey.class);
815814

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

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@
2626

2727
import org.spongepowered.api.ResourceKey;
2828

29+
import java.util.Objects;
2930
import java.util.Optional;
3031

3132
/**
32-
* A {@link RegistryValue} that usually resides in a single {@link RegistryType}
33+
* A Utility marker that assists in getting a {@link ResourceKey} for values
34+
* that generally can be within a single {@link DefaultedRegistryType defaulted registry}
3335
* and therefore this registry can be considered as "default".
3436
*/
35-
public interface DefaultedRegistryValue<T extends DefaultedRegistryValue<T>> extends RegistryValue<T> {
37+
@SuppressWarnings("unchecked")
38+
public interface DefaultedRegistryValue<T extends DefaultedRegistryValue<T>> {
3639

3740
/**
3841
* Gets the default {@link RegistryType} for
@@ -50,7 +53,20 @@ public interface DefaultedRegistryValue<T extends DefaultedRegistryValue<T>> ext
5053
* this object in the default {@link #registryType()}
5154
*/
5255
default ResourceKey registryKey() {
53-
return this.key(this.registryType());
56+
return this.registryType().get().valueKey((T) this);
57+
}
58+
59+
/**
60+
* Returns the {@link ResourceKey} associated with
61+
* this object in the default {@link #registryType()}
62+
* for the given {@link RegistryHolder}.
63+
*
64+
* @return The {@link ResourceKey} associated with
65+
* this object in the default {@link #registryType()}
66+
* for the given {@link RegistryHolder}
67+
*/
68+
default ResourceKey registryKey(final RegistryHolder holder) {
69+
return Objects.requireNonNull(holder, "holder").registry(this.registryType()).valueKey((T) this);
5470
}
5571

5672
/**
@@ -61,7 +77,20 @@ default ResourceKey registryKey() {
6177
* this object in the default {@link #registryType()}, if found
6278
*/
6379
default Optional<ResourceKey> findRegistryKey() {
64-
return this.findKey(this.registryType());
80+
return this.registryType().find().flatMap(r -> r.findValueKey((T) this));
81+
}
82+
83+
/**
84+
* Returns the {@link ResourceKey} associated with
85+
* this object in the default {@link #registryType()}.
86+
* for the given {@link RegistryHolder}, if found.
87+
*
88+
* @return The {@link ResourceKey} associated with
89+
* this object in the default {@link #registryType()}
90+
* for the given {@link RegistryHolder}, if found
91+
*/
92+
default Optional<ResourceKey> findRegistryKey(final RegistryHolder holder) {
93+
return Objects.requireNonNull(holder, "holder").findRegistry(this.registryType()).flatMap(r -> r.findValueKey((T) this));
6594
}
6695

6796
/**
@@ -72,6 +101,20 @@ default Optional<ResourceKey> findRegistryKey() {
72101
* this object in the default {@link #registryType()}
73102
*/
74103
default DefaultedRegistryReference<T> asDefaultedReference() {
75-
return this.asDefaultedReference(this.registryType());
104+
return RegistryKey.of(this.registryType(), this.registryKey()).asDefaultedReference(this.registryType().defaultHolder());
105+
}
106+
107+
/**
108+
* Returns the {@link DefaultedRegistryReference} for
109+
* this object in the default {@link #registryType()}
110+
* for the given {@link RegistryHolder}.
111+
*
112+
* @return The {@link DefaultedRegistryReference} for
113+
* this object in the default {@link #registryType()}
114+
* for the given {@link RegistryHolder}
115+
*/
116+
default DefaultedRegistryReference<T> asDefaultedReference(final RegistryHolder holder) {
117+
Objects.requireNonNull(holder, "holder");
118+
return RegistryKey.of(this.registryType(), this.registryKey(holder)).asDefaultedReference(() -> holder);
76119
}
77120
}

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

Lines changed: 0 additions & 50 deletions
This file was deleted.

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

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
package org.spongepowered.api.tag;
2626

2727
import org.spongepowered.api.registry.DefaultedRegistryValue;
28+
import org.spongepowered.api.registry.RegistryHolder;
2829

29-
import java.util.Collection;
30+
import java.util.Objects;
31+
import java.util.stream.Stream;
3032

3133
/**
32-
* A {@link DefaultedRegistryValue} that may be included in one or more {@link Tag} collections.
34+
* A type that may be included in one or more {@link Tag} collections.
3335
*/
3436
@SuppressWarnings("unchecked")
3537
public interface Taggable<T extends Taggable<T>> extends DefaultedRegistryValue<T> {
@@ -38,10 +40,21 @@ public interface Taggable<T extends Taggable<T>> extends DefaultedRegistryValue<
3840
* Gets all {@link Tag}s that have been associated
3941
* with this object in the default {@link #registryType()}.
4042
*
41-
* @return THe {@link Collection} of {@link Tag}s
43+
* @return THe {@link Stream} of {@link Tag}s
4244
*/
43-
default Collection<Tag<T>> tags() {
44-
return this.registryType().get().tags().filter(this::is).toList();
45+
default Stream<Tag<T>> tags() {
46+
return this.registryType().get().tags().filter(this::is);
47+
}
48+
49+
/**
50+
* Gets all {@link Tag}s that have been associated
51+
* with this object in the default {@link #registryType()}
52+
* for the given {@link RegistryHolder}.
53+
*
54+
* @return THe {@link Stream} of {@link Tag}s
55+
*/
56+
default Stream<Tag<T>> tags(final RegistryHolder holder) {
57+
return Objects.requireNonNull(holder, "holder").registry(this.registryType()).tags().filter(tag -> this.is(holder, tag));
4558
}
4659

4760
/**
@@ -55,4 +68,18 @@ default Collection<Tag<T>> tags() {
5568
default boolean is(final Tag<T> tag) {
5669
return this.registryType().get().taggedValues(tag).contains((T) this);
5770
}
71+
72+
/**
73+
* Returns whether the given {@link Tag} is associated with
74+
* this object in the default {@link #registryType()}
75+
* for the given {@link RegistryHolder}.
76+
*
77+
* @param tag The tag
78+
* @return true if the given {@link Tag} is associated with
79+
* this object in the default {@link #registryType()}
80+
* for the given {@link RegistryHolder}
81+
*/
82+
default boolean is(final RegistryHolder holder, final Tag<T> tag) {
83+
return Objects.requireNonNull(holder, "holder").registry(this.registryType()).taggedValues(tag).contains((T) this);
84+
}
5885
}

0 commit comments

Comments
 (0)