use typetokens in RegistryHolder

This commit is contained in:
dfsek
2021-07-18 13:31:17 -07:00
parent d4e678fd65
commit cd208cbd18
3 changed files with 29 additions and 9 deletions

View File

@@ -2,9 +2,28 @@ package com.dfsek.terra.api.registry.meta;
import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.registry.Registry;
import com.dfsek.terra.api.util.TypeToken;
import java.lang.reflect.Type;
public interface RegistryHolder {
<T> Registry<T> getRegistry(Class<T> clazz);
default <T> Registry<T> getRegistry(Class<T> clazz) {
return getRegistry((Type) clazz);
}
<T> CheckedRegistry<T> getCheckedRegistry(Class<T> clazz) throws IllegalStateException;
default <T> Registry<T> getRegistry(TypeToken<T> type) {
return getRegistry(type.getType());
}
<T> Registry<T> getRegistry(Type type);
default <T> CheckedRegistry<T> getCheckedRegistry(Class<T> clazz) throws IllegalStateException {
return getCheckedRegistry((Type) clazz);
}
default <T> CheckedRegistry<T> getCheckedRegistry(TypeToken<T> type) {
return getCheckedRegistry(type.getType());
}
<T> CheckedRegistry<T> getCheckedRegistry(Type type) throws IllegalStateException;
}

View File

@@ -84,7 +84,7 @@ public class ConfigPackImpl implements ConfigPack {
private final SeededBiomeProvider seededBiomeProvider;
private final Map<Class<?>, ImmutablePair<OpenRegistry<?>, CheckedRegistry<?>>> registryMap = new HashMap<>();
private final Map<Type, ImmutablePair<OpenRegistry<?>, CheckedRegistry<?>>> registryMap = new HashMap<>();
private final ConfigTypeRegistry configTypeRegistry;
@@ -220,7 +220,7 @@ public class ConfigPackImpl implements ConfigPack {
return this;
}
protected Map<Class<?>, ImmutablePair<OpenRegistry<?>, CheckedRegistry<?>>> getRegistryMap() {
protected Map<Type, ImmutablePair<OpenRegistry<?>, CheckedRegistry<?>>> getRegistryMap() {
return registryMap;
}
@@ -269,14 +269,14 @@ public class ConfigPackImpl implements ConfigPack {
@Override
@SuppressWarnings("unchecked")
public <T> CheckedRegistry<T> getRegistry(Class<T> clazz) {
return (CheckedRegistry<T>) registryMap.getOrDefault(clazz, ImmutablePair.ofNull()).getRight();
public <T> CheckedRegistry<T> getRegistry(Type type) {
return (CheckedRegistry<T>) registryMap.getOrDefault(type, ImmutablePair.ofNull()).getRight();
}
@SuppressWarnings("unchecked")
@Override
public <T> CheckedRegistry<T> getCheckedRegistry(Class<T> clazz) throws IllegalStateException {
return (CheckedRegistry<T>) registryMap.getOrDefault(clazz, ImmutablePair.ofNull()).getRight();
public <T> CheckedRegistry<T> getCheckedRegistry(Type type) throws IllegalStateException {
return (CheckedRegistry<T>) registryMap.getOrDefault(type, ImmutablePair.ofNull()).getRight();
}
@SuppressWarnings("unchecked")

View File

@@ -13,6 +13,7 @@ import com.dfsek.terra.registry.LockedRegistryImpl;
import com.dfsek.terra.registry.OpenRegistryImpl;
import com.dfsek.terra.world.SamplerCacheImpl;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
@@ -24,7 +25,7 @@ public class WorldConfigImpl implements WorldConfig {
private final TerraWorld world;
private final ConfigPackImpl pack;
private final Map<Class<?>, Registry<?>> registryMap = new HashMap<>();
private final Map<Type, Registry<?>> registryMap = new HashMap<>();
public WorldConfigImpl(TerraWorld world, ConfigPackImpl pack, TerraPlugin main) {
this.world = world;