-
Notifications
You must be signed in to change notification settings - Fork 95
/
IdAwareComponentRegistry.java
42 lines (35 loc) · 1.16 KB
/
IdAwareComponentRegistry.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
package slimeknights.mantle.data.registry;
import net.minecraft.resources.ResourceLocation;
import slimeknights.mantle.registration.object.IdAwareObject;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
/**
* Generic registry of a {@link IdAwareObject}.
* @param <T> Type of the component being registered.
*/
public class IdAwareComponentRegistry<T extends IdAwareObject> extends AbstractNamedComponentRegistry<T> {
/** Registered box expansion types */
private final Map<ResourceLocation,T> values = new HashMap<>();
public IdAwareComponentRegistry(String errorText) {
super(errorText);
}
/** Registers the value with the given name */
public synchronized <V extends T> V register(V value) {
ResourceLocation name = value.getId();
if (values.putIfAbsent(name, value) != null) {
throw new IllegalArgumentException("Duplicate registration " + name);
}
return value;
}
/** Gets a value or null if missing */
@Override
@Nullable
public T getValue(ResourceLocation name) {
return values.get(name);
}
@Override
public ResourceLocation getKey(T object) {
return object.getId();
}
}