Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit d40676a

Browse files
committed
getRegistryName code deduplication and (hopefully) dummy proofing
1 parent 9457fe7 commit d40676a

File tree

6 files changed

+72
-24
lines changed

6 files changed

+72
-24
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.patchworkmc.impl.registries;
2+
3+
import javax.annotation.Nullable;
4+
import javax.annotation.ParametersAreNonnullByDefault;
5+
6+
import net.minecraft.util.Identifier;
7+
import net.minecraft.util.registry.DefaultedRegistry;
8+
import net.minecraft.util.registry.Registry;
9+
10+
@ParametersAreNonnullByDefault
11+
public class Identifiers {
12+
private Identifiers() {
13+
}
14+
15+
/**
16+
* Gets the current {@link Identifier} for the object in the registry if it exists. If the object does not exist in
17+
* the provided registry, {@code fallback} will be returned instead.
18+
*
19+
* @param registry the registry to query. Must NOT be an instance of {@link DefaultedRegistry}, or else this method
20+
* will throw an {@link IllegalArgumentException}.
21+
* @return an {@link Identifier} if the instance is registered or if the fallback is non null, otherwise null
22+
*/
23+
@Nullable
24+
public static <T> Identifier getOrFallback(Registry<T> registry, T instance, @Nullable Identifier fallback) {
25+
if (registry instanceof DefaultedRegistry) {
26+
// While we could just cast here, I want to catch these cases where they come up and fix them in the mixin.
27+
throw new IllegalArgumentException("Used the non-defaulted getOrFallback method with a DefaultedRegistry");
28+
}
29+
30+
Identifier current = registry.getId(instance);
31+
32+
if (current == null) {
33+
return fallback;
34+
} else {
35+
return current;
36+
}
37+
}
38+
39+
/**
40+
* Gets the current {@link Identifier} for the object in the registry if it exists. If the object does not exist in
41+
* the provided registry, {@code fallback} will be returned instead.
42+
*
43+
* @param registry the registry to query. Must be an instance of {@link DefaultedRegistry}.
44+
* @return an {@link Identifier} if the instance is registered or if the fallback is non null, otherwise null
45+
*/
46+
@Nullable
47+
public static <T> Identifier getOrFallback(DefaultedRegistry<T> registry, T instance, @Nullable Identifier fallback) {
48+
Identifier current = registry.getId(instance);
49+
50+
if (current.equals(registry.getDefaultId())) {
51+
return fallback;
52+
} else {
53+
return current;
54+
}
55+
}
56+
}

patchwork-registries/src/main/java/com/patchworkmc/mixin/registries/MixinBiome.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import net.minecraft.world.biome.Biome;
2929

3030
import com.patchworkmc.impl.registries.ExtendedForgeRegistryEntry;
31+
import com.patchworkmc.impl.registries.Identifiers;
3132

3233
@Mixin(Biome.class)
3334
public class MixinBiome implements ExtendedForgeRegistryEntry<Biome> {
@@ -42,10 +43,9 @@ public IForgeRegistryEntry setRegistryName(Identifier name) {
4243
}
4344

4445
public Identifier getRegistryName() {
45-
Identifier current = Registry.BIOME.getId((Biome) (Object) this);
46-
Identifier set = registryName;
46+
Biome biome = (Biome) (Object) this;
4747

48-
return current != null ? current : set;
48+
return Identifiers.getOrFallback(Registry.BIOME, biome, registryName);
4949
}
5050

5151
public Class<Biome> getRegistryType() {

patchwork-registries/src/main/java/com/patchworkmc/mixin/registries/MixinBlock.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,24 @@
2828
import net.minecraft.util.registry.Registry;
2929

3030
import com.patchworkmc.impl.registries.ExtendedForgeRegistryEntry;
31+
import com.patchworkmc.impl.registries.Identifiers;
3132

3233
@Mixin(Block.class)
3334
public class MixinBlock implements ExtendedForgeRegistryEntry<Block> {
3435
@Unique
3536
private Identifier registryName;
3637

3738
@Override
38-
public IForgeRegistryEntry setRegistryName(Identifier name) {
39+
public IForgeRegistryEntry<Block> setRegistryName(Identifier name) {
3940
this.registryName = name;
4041

4142
return this;
4243
}
4344

4445
public Identifier getRegistryName() {
45-
Identifier current = Registry.BLOCK.getId((Block) (Object) this);
46-
Identifier set = registryName;
46+
Block block = (Block) (Object) this;
4747

48-
if (set == null) {
49-
set = Registry.BLOCK.getDefaultId();
50-
}
51-
52-
return current != Registry.BLOCK.getDefaultId() ? current : set;
48+
return Identifiers.getOrFallback(Registry.BLOCK, block, registryName);
5349
}
5450

5551
public Class<Block> getRegistryType() {

patchwork-registries/src/main/java/com/patchworkmc/mixin/registries/MixinFeature.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import net.minecraft.world.gen.feature.Feature;
2929

3030
import com.patchworkmc.impl.registries.ExtendedForgeRegistryEntry;
31+
import com.patchworkmc.impl.registries.Identifiers;
3132

3233
@Mixin(Feature.class)
3334
public class MixinFeature implements ExtendedForgeRegistryEntry<Feature> {
@@ -42,10 +43,9 @@ public IForgeRegistryEntry setRegistryName(Identifier name) {
4243
}
4344

4445
public Identifier getRegistryName() {
45-
Identifier current = Registry.FEATURE.getId((Feature) (Object) this);
46-
Identifier set = registryName;
46+
Feature<?> feature = (Feature<?>) (Object) this;
4747

48-
return current != null ? current : set;
48+
return Identifiers.getOrFallback(Registry.FEATURE, feature, registryName);
4949
}
5050

5151
public Class<Feature> getRegistryType() {

patchwork-registries/src/main/java/com/patchworkmc/mixin/registries/MixinItem.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import net.minecraft.util.registry.Registry;
2929

3030
import com.patchworkmc.impl.registries.ExtendedForgeRegistryEntry;
31+
import com.patchworkmc.impl.registries.Identifiers;
3132

3233
@Mixin(Item.class)
3334
public class MixinItem implements ExtendedForgeRegistryEntry<Item> {
@@ -42,14 +43,9 @@ public IForgeRegistryEntry setRegistryName(Identifier name) {
4243
}
4344

4445
public Identifier getRegistryName() {
45-
Identifier current = Registry.ITEM.getId((Item) (Object) this);
46-
Identifier set = registryName;
46+
Item item = (Item) (Object) this;
4747

48-
if (set == null) {
49-
set = Registry.ITEM.getDefaultId();
50-
}
51-
52-
return current != Registry.ITEM.getDefaultId() ? current : set;
48+
return Identifiers.getOrFallback(Registry.ITEM, item, registryName);
5349
}
5450

5551
public Class<Item> getRegistryType() {

patchwork-registries/src/main/java/com/patchworkmc/mixin/registries/MixinSurfaceBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import net.minecraft.world.gen.surfacebuilder.SurfaceBuilder;
2929

3030
import com.patchworkmc.impl.registries.ExtendedForgeRegistryEntry;
31+
import com.patchworkmc.impl.registries.Identifiers;
3132

3233
@Mixin(SurfaceBuilder.class)
3334
public class MixinSurfaceBuilder implements ExtendedForgeRegistryEntry<SurfaceBuilder> {
@@ -42,10 +43,9 @@ public IForgeRegistryEntry setRegistryName(Identifier name) {
4243
}
4344

4445
public Identifier getRegistryName() {
45-
Identifier current = Registry.SURFACE_BUILDER.getId((SurfaceBuilder) (Object) this);
46-
Identifier set = registryName;
46+
SurfaceBuilder<?> surfaceBuilder = (SurfaceBuilder<?>) (Object) this;
4747

48-
return current != null ? current : set;
48+
return Identifiers.getOrFallback(Registry.SURFACE_BUILDER, surfaceBuilder, registryName);
4949
}
5050

5151
public Class<SurfaceBuilder> getRegistryType() {

0 commit comments

Comments
 (0)