mirror of
https://github.com/GTNewHorizons/Mantle.git
synced 2026-11-19 11:49:08 +00:00
Add EnumObject.Builder and use it in the registries
Gives me more control over implementation of the map
This commit is contained in:
1 parent
f6b1289ab9
commit
b8bcd95af3
3 files changed
+68
-21
No files matched your search
@@ -6,16 +6,13 @@ import net.minecraftforge.registries.IForgeRegistry;
|
||||
import net.minecraftforge.registries.IRegistryDelegate;
|
||||
import slimeknights.mantle.registration.object.EnumObject;
|
||||
|
||||
import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Adapter that allows certain registry types to register from a list of values
|
||||
* @param <T> Registry type
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
@SuppressWarnings({"unused", "WeakerAccess"})
|
||||
public class EnumRegistryAdapter<T extends ForgeRegistryEntry<T>> extends RegistryAdapter<T> {
|
||||
|
||||
public EnumRegistryAdapter(IForgeRegistry<T> registry) {
|
||||
@@ -39,13 +36,13 @@ public class EnumRegistryAdapter<T extends ForgeRegistryEntry<T>> extends Regist
|
||||
throw new IllegalArgumentException("Must have at least one value");
|
||||
}
|
||||
// note this cast only works because you cannot extend an enum
|
||||
Map<E,Supplier<? extends I>> map = new EnumMap<>((Class<E>)values[0].getClass());
|
||||
EnumObject.Builder<E,I> builder = new EnumObject.Builder<>(values[0].getDeclaringClass());
|
||||
for (E value : values) {
|
||||
// assuming the type will not sub for a different class
|
||||
IRegistryDelegate<T> delegate = register(mapper.apply(value), value.getString() + "_" + name).delegate;
|
||||
map.put(value, () -> (I) delegate.get());
|
||||
builder.put(value, () -> (I) delegate.get());
|
||||
}
|
||||
return new EnumObject<>(map);
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,12 +58,12 @@ public class EnumRegistryAdapter<T extends ForgeRegistryEntry<T>> extends Regist
|
||||
throw new IllegalArgumentException("Must have at least one value");
|
||||
}
|
||||
// note this cast only works because you cannot extend an enum
|
||||
Map<E,Supplier<? extends I>> map = new EnumMap<>((Class<E>)values[0].getClass());
|
||||
EnumObject.Builder<E,I> builder = new EnumObject.Builder<>(values[0].getDeclaringClass());
|
||||
for (E value : values) {
|
||||
// assuming the type will not sub for a different class
|
||||
IRegistryDelegate<T> delegate = register(mapper.apply(value), name + "_" + value.getString()).delegate;
|
||||
map.put(value, () -> (I) delegate.get());
|
||||
builder.put(value, () -> (I) delegate.get());
|
||||
}
|
||||
return new EnumObject<>(map);
|
||||
return builder.build();
|
||||
}
|
||||
}
|
||||
+6
-10
@@ -7,8 +7,6 @@ import net.minecraftforge.registries.IForgeRegistry;
|
||||
import net.minecraftforge.registries.IForgeRegistryEntry;
|
||||
import slimeknights.mantle.registration.object.EnumObject;
|
||||
|
||||
import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@@ -56,17 +54,16 @@ public abstract class DeferredRegisterWrapper<T extends IForgeRegistryEntry<T>>
|
||||
* @param register Function to register an entry
|
||||
* @return EnumObject mapping between different block types
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected static <E extends Enum<E> & IStringSerializable, V extends T, T extends IForgeRegistryEntry<T>> EnumObject<E,V> registerEnum(E[] values, String name, BiFunction<String,E,Supplier<? extends V>> register) {
|
||||
if (values.length == 0) {
|
||||
throw new IllegalArgumentException("Must have at least one value");
|
||||
}
|
||||
// note this cast only works because you cannot extend an enum
|
||||
Map<E,Supplier<? extends V>> map = new EnumMap<>((Class<E>)values[0].getClass());
|
||||
EnumObject.Builder<E,V> builder = new EnumObject.Builder<>(values[0].getDeclaringClass());
|
||||
for (E value : values) {
|
||||
map.put(value, register.apply(value.getString() + "_" + name, value));
|
||||
builder.put(value, register.apply(value.getString() + "_" + name, value));
|
||||
}
|
||||
return new EnumObject<>(map);
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,16 +73,15 @@ public abstract class DeferredRegisterWrapper<T extends IForgeRegistryEntry<T>>
|
||||
* @param register Function to register an entry
|
||||
* @return EnumObject mapping between different block types
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected static <E extends Enum<E> & IStringSerializable, V extends T, T extends IForgeRegistryEntry<T>> EnumObject<E,V> registerEnum(String name, E[] values, BiFunction<String,E,Supplier<? extends V>> register) {
|
||||
if (values.length == 0) {
|
||||
throw new IllegalArgumentException("Must have at least one value");
|
||||
}
|
||||
// note this cast only works because you cannot extend an enum
|
||||
Map<E,Supplier<? extends V>> map = new EnumMap<>((Class<E>)values[0].getClass());
|
||||
EnumObject.Builder<E,V> builder = new EnumObject.Builder<>(values[0].getDeclaringClass());
|
||||
for (E value : values) {
|
||||
map.put(value, register.apply(name + "_" + value.getString(), value));
|
||||
builder.put(value, register.apply(name + "_" + value.getString(), value));
|
||||
}
|
||||
return new EnumObject<>(map);
|
||||
return builder.build();
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
package slimeknights.mantle.registration.object;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import net.minecraftforge.registries.IForgeRegistryEntry;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.EnumMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
@@ -14,7 +16,7 @@ import java.util.stream.Collectors;
|
||||
* @param <T> Enum type
|
||||
* @param <I> Entry type
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@AllArgsConstructor(access = AccessLevel.PROTECTED)
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class EnumObject<T extends Enum<T>, I extends IForgeRegistryEntry<? super I>> {
|
||||
private final Map<T,Supplier<? extends I>> map;
|
||||
@@ -57,4 +59,56 @@ public class EnumObject<T extends Enum<T>, I extends IForgeRegistryEntry<? super
|
||||
public List<I> values() {
|
||||
return this.map.values().stream().map(Supplier::get).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Enum object builder, to more conveiently create it from items, a map, or another enum object
|
||||
* @param <T> Enum type
|
||||
* @param <I> Entry type
|
||||
*/
|
||||
@SuppressWarnings({"UnusedReturnValue", "unused"})
|
||||
public static class Builder<T extends Enum<T>, I extends IForgeRegistryEntry<? super I>> {
|
||||
private final Map<T, Supplier<? extends I>> map;
|
||||
public Builder(Class<T> clazz) {
|
||||
this.map = new EnumMap<>(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given key and value to the object
|
||||
* @param key Key
|
||||
* @param value Value
|
||||
* @return Builder instance
|
||||
*/
|
||||
public Builder<T,I> put(T key, Supplier<? extends I> value) {
|
||||
this.map.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds all values from the given map
|
||||
* @param map Map
|
||||
* @return Builder instance
|
||||
*/
|
||||
public Builder<T,I> putAll(Map<T, Supplier<? extends I>> map) {
|
||||
this.map.putAll(map);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds all values from the given enum object
|
||||
* @param object Enum object
|
||||
* @return Builder instance
|
||||
*/
|
||||
public Builder<T,I> putAll(EnumObject<T,I> object) {
|
||||
this.map.putAll(object.map);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the final enum object
|
||||
* @return Constructed enum object
|
||||
*/
|
||||
public EnumObject<T,I> build() {
|
||||
return new EnumObject<>(map);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user