Unicopia/src/main/java/com/minelittlepony/unicopia/util/RegistryIndexer.java

55 lines
1.2 KiB
Java
Raw Normal View History

2022-08-27 15:07:29 +02:00
package com.minelittlepony.unicopia.util;
import com.minelittlepony.common.client.gui.IField;
2022-12-18 22:07:24 +01:00
import net.minecraft.registry.Registry;
2022-08-27 15:07:29 +02:00
public class RegistryIndexer<T> {
public static <T> RegistryIndexer<T> of(Registry<T> registry) {
return new RegistryIndexer<>(registry);
}
private final Registry<T> values;
private RegistryIndexer(Registry<T> registry) {
values = registry;
}
public int size() {
2022-10-12 15:45:07 +02:00
return values.size() - 1;
2022-08-27 15:07:29 +02:00
}
public int indexOf(T value) {
return values.getRawId(value);
}
public T valueOf(int index) {
return values.get(wrapIndex(index));
}
public T valueOf(float index) {
return valueOf((int)index);
}
public T cycle(T value, int increment) {
return valueOf(indexOf(value) + increment);
}
public IField.IChangeCallback<Float> createSetter(IField.IChangeCallback<T> setter) {
return index -> {
int i = wrapIndex(index.intValue());
setter.perform(valueOf(i));
return (float)i;
};
}
private int wrapIndex(int index) {
2022-10-12 15:45:07 +02:00
int sz = values.size();
2022-08-27 15:07:29 +02:00
while (index < 0) {
index += sz;
}
return index % sz;
}
}