use typetokens in RegistryHolder

This commit is contained in:
dfsek committed 2021-07-18 13:31:17 -07:00
1 parent d4e678fd65
commit cd208cbd18
3 files changed
+29 -9

No files matched your search

@@ -2,9 +2,28 @@ package com.dfsek.terra.api.registry.meta;
import com.dfsek.terra.api.registry.CheckedRegistry; import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.registry.Registry; import com.dfsek.terra.api.registry.Registry;
import com.dfsek.terra.api.util.TypeToken;
import java.lang.reflect.Type;
public interface RegistryHolder { 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;
} }
@@ -84,7 +84,7 @@ public class ConfigPackImpl implements ConfigPack {
private final SeededBiomeProvider seededBiomeProvider; 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; private final ConfigTypeRegistry configTypeRegistry;
@@ -220,7 +220,7 @@ public class ConfigPackImpl implements ConfigPack {
return this; return this;
} }
protected Map<Class<?>, ImmutablePair<OpenRegistry<?>, CheckedRegistry<?>>> getRegistryMap() { protected Map<Type, ImmutablePair<OpenRegistry<?>, CheckedRegistry<?>>> getRegistryMap() {
return registryMap; return registryMap;
} }
@@ -269,14 +269,14 @@ public class ConfigPackImpl implements ConfigPack {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> CheckedRegistry<T> getRegistry(Class<T> clazz) { public <T> CheckedRegistry<T> getRegistry(Type type) {
return (CheckedRegistry<T>) registryMap.getOrDefault(clazz, ImmutablePair.ofNull()).getRight(); return (CheckedRegistry<T>) registryMap.getOrDefault(type, ImmutablePair.ofNull()).getRight();
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public <T> CheckedRegistry<T> getCheckedRegistry(Class<T> clazz) throws IllegalStateException { public <T> CheckedRegistry<T> getCheckedRegistry(Type type) throws IllegalStateException {
return (CheckedRegistry<T>) registryMap.getOrDefault(clazz, ImmutablePair.ofNull()).getRight(); return (CheckedRegistry<T>) registryMap.getOrDefault(type, ImmutablePair.ofNull()).getRight();
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@@ -13,6 +13,7 @@ import com.dfsek.terra.registry.LockedRegistryImpl;
import com.dfsek.terra.registry.OpenRegistryImpl; import com.dfsek.terra.registry.OpenRegistryImpl;
import com.dfsek.terra.world.SamplerCacheImpl; import com.dfsek.terra.world.SamplerCacheImpl;
import java.lang.reflect.Type;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@@ -24,7 +25,7 @@ public class WorldConfigImpl implements WorldConfig {
private final TerraWorld world; private final TerraWorld world;
private final ConfigPackImpl pack; 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) { public WorldConfigImpl(TerraWorld world, ConfigPackImpl pack, TerraPlugin main) {
this.world = world; this.world = world;