/
RegistryContentType.java
49 lines (42 loc) · 1.38 KB
/
RegistryContentType.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package knightminer.inspirations.library.recipe.cauldron.contents;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.registries.IForgeRegistry;
import net.minecraftforge.registries.IForgeRegistryEntry;
import javax.annotation.Nullable;
import java.util.Objects;
/**
* Content type that mirrors a Forge registry
* @param <T> Value type
*/
public abstract class RegistryContentType<T extends IForgeRegistryEntry<T>> extends CauldronContentType<T> {
private final IForgeRegistry<T> registry;
/**
* Creates a new instance
* @param registry Forge registry instance for lookups
*/
public RegistryContentType(IForgeRegistry<T> registry) {
this.registry = registry;
}
@Override
public String getName(T value) {
return Objects.requireNonNull(value.getRegistryName()).toString();
}
@Nullable
@Override
public T getEntry(String name) {
ResourceLocation location = ResourceLocation.tryCreate(name);
if (location != null) {
// swap default value for null
T value = registry.getValue(location);
ResourceLocation defaultKey = registry.getDefaultKey();
if (value != null && defaultKey != null && !defaultKey.equals(value.getRegistryName())) {
return value;
}
}
return null;
}
@Override
public String getModId(T value) {
return Objects.requireNonNull(value.getRegistryName()).getNamespace();
}
}