Skip to content

Commit 66b31ff

Browse files
committed
make RegistryKey, RegistryType & Tag implement Comparable
1 parent 5a4a755 commit 66b31ff

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
* @param <T> The type
3939
*/
40-
public interface RegistryKey<T> {
40+
public interface RegistryKey<T> extends Comparable<RegistryKey<?>> {
4141

4242
static <T> RegistryKey<T> of(final RegistryType<T> registry, final ResourceKey location) {
4343
return Sponge.game().factoryProvider().provide(Factory.class).of(Objects.requireNonNull(registry, "registry"),
@@ -59,6 +59,16 @@ static <T> RegistryKey<T> of(final RegistryType<T> registry, final ResourceKey l
5959
*/
6060
ResourceKey location();
6161

62+
@Override
63+
default int compareTo(final RegistryKey<?> registryKey) {
64+
final int registry = this.registry().compareTo(registryKey.registry());
65+
if (registry != 0) {
66+
return registry;
67+
}
68+
69+
return this.location().compareTo(registryKey.location());
70+
}
71+
6272
/**
6373
* Generates a utility {@link RegistryReference reference} used to assist in querying a value from this key
6474
*

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import java.util.Objects;
3131
import java.util.function.Supplier;
3232

33-
public interface RegistryType<T> {
33+
public interface RegistryType<T> extends Comparable<RegistryType<?>> {
3434

3535
static <T> RegistryType<T> of(final ResourceKey root, final ResourceKey location) {
3636
return Sponge.game().factoryProvider().provide(Factory.class).create(Objects.requireNonNull(root, "root"),
@@ -49,6 +49,16 @@ default RegistryReference<T> referenced(final ResourceKey key) {
4949
return RegistryKey.of(this, Objects.requireNonNull(key, "key")).asReference();
5050
}
5151

52+
@Override
53+
default int compareTo(final RegistryType<?> registryType) {
54+
final int root = this.root().compareTo(registryType.root());
55+
if (root != 0) {
56+
return root;
57+
}
58+
59+
return this.location().compareTo(registryType.location());
60+
}
61+
5262
/**
5363
* Gets a utility {@link DefaultedRegistryType defaulted type} for easier querying of the intention of where the registry is contained.
5464
*

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
* {@link BlockTypeTags#LOGS_THAT_BURN}</li>
5151
* </ul>
5252
*/
53-
public interface Tag<T> extends ResourceKeyed {
53+
public interface Tag<T> extends ResourceKeyed, Comparable<Tag<?>> {
5454

5555
static <T> Tag<T> of(final RegistryType<T> registryType, final ResourceKey key) {
5656
return Sponge.game().factoryProvider().provide(Tag.Factory.class).of(registryType, key);
@@ -63,6 +63,16 @@ static <T> Tag<T> of(final RegistryType<T> registryType, final ResourceKey key)
6363
*/
6464
RegistryType<T> registry();
6565

66+
@Override
67+
default int compareTo(final Tag<?> tag) {
68+
final int registry = this.registry().compareTo(tag.registry());
69+
if (registry != 0) {
70+
return registry;
71+
}
72+
73+
return this.key().compareTo(tag.key());
74+
}
75+
6676
DefaultedTag<T> asDefaultedTag(Supplier<RegistryHolder> holder);
6777

6878
DefaultedTag<T> asScopedTag();

0 commit comments

Comments
 (0)