use TypeToken for registries

This commit is contained in:
dfsek
2021-07-18 13:55:35 -07:00
parent ebc81b196a
commit d5601229ac
13 changed files with 43 additions and 26 deletions

View File

@@ -7,6 +7,7 @@ import com.dfsek.terra.api.config.ConfigFactory;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.config.ConfigType;
import com.dfsek.terra.api.registry.OpenRegistry;
import com.dfsek.terra.api.util.TypeToken;
import com.dfsek.terra.api.util.seeded.SeededTerraBiome;
import java.util.function.Supplier;
@@ -15,6 +16,8 @@ public class BiomeConfigType implements ConfigType<BiomeTemplate, SeededTerraBio
private final ConfigPack pack;
private final BiomeFactory factory;
public static final TypeToken<SeededTerraBiome> BIOME_TYPE_TOKEN = new TypeToken<>() {};
public BiomeConfigType(ConfigPack pack) {
this.pack = pack;
this.factory = new BiomeFactory(pack);
@@ -31,8 +34,8 @@ public class BiomeConfigType implements ConfigType<BiomeTemplate, SeededTerraBio
}
@Override
public Class<SeededTerraBiome> getTypeClass() {
return SeededTerraBiome.class;
public TypeToken<SeededTerraBiome> getTypeClass() {
return BIOME_TYPE_TOKEN;
}
@Override

View File

@@ -5,6 +5,7 @@ import com.dfsek.terra.api.config.ConfigFactory;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.config.ConfigType;
import com.dfsek.terra.api.registry.OpenRegistry;
import com.dfsek.terra.api.util.TypeToken;
import com.dfsek.terra.api.world.Flora;
import java.util.function.Supplier;
@@ -13,6 +14,8 @@ public class FloraConfigType implements ConfigType<FloraTemplate, Flora> {
private final FloraFactory factory = new FloraFactory();
private final ConfigPack pack;
public static final TypeToken<Flora> FLORA_TYPE_TOKEN = new TypeToken<>(){};
public FloraConfigType(ConfigPack pack) {
this.pack = pack;
}
@@ -28,8 +31,8 @@ public class FloraConfigType implements ConfigType<FloraTemplate, Flora> {
}
@Override
public Class<Flora> getTypeClass() {
return Flora.class;
public TypeToken<Flora> getTypeClass() {
return FLORA_TYPE_TOKEN;
}
@Override

View File

@@ -6,12 +6,14 @@ import com.dfsek.terra.api.config.ConfigFactory;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.config.ConfigType;
import com.dfsek.terra.api.registry.OpenRegistry;
import com.dfsek.terra.api.util.TypeToken;
import java.util.function.Supplier;
public class OreConfigType implements ConfigType<OreTemplate, Ore> {
private final OreFactory factory = new OreFactory();
private final ConfigPack pack;
public static final TypeToken<Ore> ORE_TYPE_TOKEN = new TypeToken<>(){};
public OreConfigType(ConfigPack pack) {
this.pack = pack;
@@ -28,8 +30,8 @@ public class OreConfigType implements ConfigType<OreTemplate, Ore> {
}
@Override
public Class<Ore> getTypeClass() {
return Ore.class;
public TypeToken<Ore> getTypeClass() {
return ORE_TYPE_TOKEN;
}
@Override

View File

@@ -8,6 +8,7 @@ import com.dfsek.terra.api.config.ConfigFactory;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.config.ConfigType;
import com.dfsek.terra.api.registry.OpenRegistry;
import com.dfsek.terra.api.util.TypeToken;
import com.dfsek.terra.api.world.generator.Palette;
import java.util.function.Supplier;
@@ -17,6 +18,8 @@ public class PaletteConfigType implements ConfigType<PaletteTemplate, Palette> {
private final ConfigPack pack;
private final TerraPlugin main;
public static final TypeToken<Palette> PALETTE_TYPE_TOKEN = new TypeToken<>(){};
public PaletteConfigType(ConfigPack pack, TerraPlugin main) {
this.pack = pack;
this.main = main;
@@ -33,8 +36,8 @@ public class PaletteConfigType implements ConfigType<PaletteTemplate, Palette> {
}
@Override
public Class<Palette> getTypeClass() {
return Palette.class;
public TypeToken<Palette> getTypeClass() {
return PALETTE_TYPE_TOKEN;
}
@Override

View File

@@ -5,6 +5,7 @@ import com.dfsek.terra.api.config.ConfigFactory;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.config.ConfigType;
import com.dfsek.terra.api.registry.OpenRegistry;
import com.dfsek.terra.api.util.TypeToken;
import com.dfsek.terra.api.world.Tree;
import java.util.function.Supplier;
@@ -13,6 +14,7 @@ public class TreeConfigType implements ConfigType<TreeTemplate, Tree> {
private final TreeFactory factory = new TreeFactory();
private final ConfigPack pack;
public static final TypeToken<Tree> TREE_TYPE_TOKEN = new TypeToken<>(){};
public TreeConfigType(ConfigPack pack) {
this.pack = pack;
}
@@ -28,8 +30,8 @@ public class TreeConfigType implements ConfigType<TreeTemplate, Tree> {
}
@Override
public Class<Tree> getTypeClass() {
return Tree.class;
public TypeToken<Tree> getTypeClass() {
return TREE_TYPE_TOKEN;
}
@Override

View File

@@ -30,7 +30,7 @@ public class FeatureGenerationAddon extends TerraAddon implements EventListener
}
public void onBiomeLoad(ConfigurationLoadEvent event) {
if(SeededTerraBiome.class.isAssignableFrom(event.getType().getTypeClass())) {
if(event.is(SeededTerraBiome.class)) {
event.getLoadedObject(SeededTerraBiome.class).getContext().put(event.load(new BiomeFeaturesTemplate()).get());
}
}

View File

@@ -31,7 +31,7 @@ public class FloraGenerationAddon extends TerraAddon implements EventListener {
}
public void onBiomeLoad(ConfigurationLoadEvent event) {
if(SeededTerraBiome.class.isAssignableFrom(event.getType().getTypeClass())) {
if(event.is(SeededTerraBiome.class)) {
event.getLoadedObject(SeededTerraBiome.class).getContext().put(event.load(new BiomeFloraTemplate()).get());
}
}

View File

@@ -31,7 +31,7 @@ public class TreeGenerationAddon extends TerraAddon implements EventListener {
}
public void onBiomeLoad(ConfigurationLoadEvent event) {
if(SeededTerraBiome.class.isAssignableFrom(event.getType().getTypeClass())) {
if(event.is(SeededTerraBiome.class)) {
event.getLoadedObject(SeededTerraBiome.class).getContext().put(event.load(new BiomeTreeTemplate()).get());
}
}

View File

@@ -2,6 +2,7 @@ package com.dfsek.terra.api.config;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.registry.OpenRegistry;
import com.dfsek.terra.api.util.TypeToken;
import java.util.function.Supplier;
@@ -10,7 +11,7 @@ public interface ConfigType<T extends AbstractableTemplate, R> {
ConfigFactory<T, R> getFactory();
Class<R> getTypeClass();
TypeToken<R> getTypeClass();
Supplier<OpenRegistry<R>> registrySupplier();
}

View File

@@ -5,6 +5,7 @@ import com.dfsek.tectonic.config.ConfigTemplate;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.config.ConfigType;
import com.dfsek.terra.api.event.events.PackEvent;
import com.dfsek.terra.api.util.ReflectionUtil;
import java.util.function.Consumer;
@@ -49,13 +50,13 @@ public class ConfigurationLoadEvent implements PackEvent {
}
public boolean is(Class<?> clazz) {
return clazz.isAssignableFrom(type.getTypeClass());
return clazz.isAssignableFrom(type.getTypeClass().getRawType());
}
@SuppressWarnings("unchecked")
public <T> T getLoadedObject(Class<T> clazz) {
if(!clazz.isAssignableFrom(type.getTypeClass()))
throw new ClassCastException("Cannot assign object from loader of type " + type.getTypeClass().getCanonicalName() + " to class " + clazz.getCanonicalName());
if(!clazz.isAssignableFrom(type.getTypeClass().getRawType()))
throw new ClassCastException("Cannot assign object from loader of type " + ReflectionUtil.typeToString(type.getTypeClass().getType()) + " to class " + clazz.getCanonicalName());
return (T) loaded;
}

View File

@@ -4,6 +4,7 @@ import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.config.ConfigType;
import com.dfsek.terra.api.event.events.PackEvent;
import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.util.ReflectionUtil;
public abstract class ConfigTypeLoadEvent implements PackEvent {
private final ConfigType<?, ?> type;
@@ -23,13 +24,13 @@ public abstract class ConfigTypeLoadEvent implements PackEvent {
}
public boolean is(Class<?> clazz) {
return clazz.isAssignableFrom(type.getTypeClass());
return clazz.isAssignableFrom(type.getTypeClass().getRawType());
}
@SuppressWarnings("unchecked")
public <T> CheckedRegistry<T> getRegistry(Class<T> clazz) {
if(!clazz.isAssignableFrom(type.getTypeClass()))
throw new ClassCastException("Cannot assign object from loader of type " + type.getTypeClass().getCanonicalName() + " to class " + clazz.getCanonicalName());
if(!clazz.isAssignableFrom(type.getTypeClass().getRawType()))
throw new ClassCastException("Cannot assign object from loader of type " + ReflectionUtil.typeToString(type.getTypeClass().getType()) + " to class " + clazz.getCanonicalName());
return (CheckedRegistry<T>) registry;
}
}

View File

@@ -194,12 +194,12 @@ public class ConfigPackImpl implements ConfigPack {
private ConfigTypeRegistry createRegistry() {
return new ConfigTypeRegistry(main, (id, configType) -> {
OpenRegistry<?> openRegistry = configType.registrySupplier().get();
if(registryMap.containsKey(configType.getTypeClass())) { // Someone already registered something; we need to copy things to the new registry.
registryMap.get(configType.getTypeClass()).getLeft().forEach(((OpenRegistry<Object>) openRegistry)::register);
if(registryMap.containsKey(configType.getTypeClass().getType())) { // Someone already registered something; we need to copy things to the new registry.
registryMap.get(configType.getTypeClass().getType()).getLeft().forEach(((OpenRegistry<Object>) openRegistry)::register);
}
selfLoader.registerLoader(configType.getTypeClass(), openRegistry);
abstractConfigLoader.registerLoader(configType.getTypeClass(), openRegistry);
registryMap.put(configType.getTypeClass(), ImmutablePair.of(openRegistry, new CheckedRegistryImpl<>(openRegistry)));
selfLoader.registerLoader(configType.getTypeClass().getType(), openRegistry);
abstractConfigLoader.registerLoader(configType.getTypeClass().getType(), openRegistry);
registryMap.put(configType.getTypeClass().getType(), ImmutablePair.of(openRegistry, new CheckedRegistryImpl<>(openRegistry)));
});
}

View File

@@ -2,6 +2,7 @@ package com.dfsek.terra.registry.config;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.config.ConfigType;
import com.dfsek.terra.api.util.ReflectionUtil;
import com.dfsek.terra.registry.OpenRegistryImpl;
import java.util.LinkedHashMap;
@@ -21,7 +22,7 @@ public class ConfigTypeRegistry extends OpenRegistryImpl<ConfigType<?, ?>> {
@Override
public boolean register(String identifier, Entry<ConfigType<?, ?>> value) {
callback.accept(identifier, value.getValue());
main.getDebugLogger().info("Registered config registry with ID " + identifier + " to class " + value.getValue().getTypeClass().getCanonicalName());
main.getDebugLogger().info("Registered config registry with ID " + identifier + " to type " + ReflectionUtil.typeToString(value.getValue().getTypeClass().getType()));
return super.register(identifier, value);
}
}