2626
2727import org .spongepowered .api .ResourceKey ;
2828
29+ import java .util .Objects ;
2930import 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}
0 commit comments