convert a bunch of stuff to new APIs

This commit is contained in:
dfsek
2025-12-29 21:11:11 -07:00
parent 16705057e0
commit 9a16336f53
29 changed files with 167 additions and 87 deletions

View File

@@ -23,10 +23,14 @@ import com.dfsek.tectonic.api.exception.LoadException;
import com.dfsek.tectonic.api.loader.ConfigLoader;
import com.dfsek.tectonic.api.loader.type.TypeLoader;
import com.dfsek.tectonic.impl.MapConfiguration;
import com.dfsek.terra.api.util.function.FunctionUtils;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.AnnotatedType;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
import com.dfsek.terra.api.registry.Registry;
@@ -46,7 +50,7 @@ public class GenericTemplateSupplierLoader<T> implements TypeLoader<T> {
String type = (String) map.get("type");
return loader
.load(registry.getByID(type)
.orElseThrow(() -> new LoadException("No such entry: " + map.get("type"), depthTracker))
.collect(left -> FunctionUtils.throw_(new LoadException("Failed to load entry " + map.get("type") + ": " + left, depthTracker)), Function.identity())
.get(), new MapConfiguration(map), depthTracker.intrinsic("With type \"" + type + "\"")).get();
}

View File

@@ -105,12 +105,11 @@ public class MetaPackImpl implements MetaPack {
template.getPacks().forEach((k, v) -> {
RegistryKey registryKey = RegistryKey.parse(v);
if(configRegistry.contains(registryKey)) {
packs.put(k, configRegistry.get(registryKey).get());
logger.info("Linked config pack \"{}\" to metapack \"{}:{}\".", v, namespace, id);
} else {
logger.warn("Failed to link config pack \"{}\" to metapack \"{}:{}\".", v, namespace, id);
}
configRegistry.get(registryKey).consume(pack -> {
packs.put(k, pack);
logger.info("Linked config pack \"{}\" to metapack \"{}:{}\".", v, namespace, id);
})
.ifNothing(() -> logger.warn("Failed to link config pack \"{}\" to metapack \"{}:{}\".", v, namespace, id));
});
HashSet<String> authors = new HashSet<>();

View File

@@ -20,6 +20,9 @@ package com.dfsek.terra.registry;
import com.dfsek.tectonic.api.depth.DepthTracker;
import com.dfsek.tectonic.api.exception.LoadException;
import com.dfsek.tectonic.api.loader.ConfigLoader;
import com.dfsek.terra.api.util.generic.data.types.Maybe;
import org.jetbrains.annotations.ApiStatus.Internal;
import org.jetbrains.annotations.NotNull;
@@ -61,7 +64,7 @@ public class CheckedRegistryImpl<T> implements CheckedRegistry<T> {
}
@Override
public Optional<T> get(@NotNull RegistryKey key) {
public Maybe<T> get(@NotNull RegistryKey key) {
return registry.get(key);
}

View File

@@ -20,6 +20,9 @@ package com.dfsek.terra.registry;
import com.dfsek.tectonic.api.depth.DepthTracker;
import com.dfsek.tectonic.api.exception.LoadException;
import com.dfsek.tectonic.api.loader.ConfigLoader;
import com.dfsek.terra.api.util.generic.data.types.Maybe;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.AnnotatedType;
@@ -48,7 +51,7 @@ public class LockedRegistryImpl<T> implements Registry<T> {
}
@Override
public Optional<T> get(@NotNull RegistryKey key) {
public Maybe<T> get(@NotNull RegistryKey key) {
return registry.get(key);
}

View File

@@ -20,6 +20,10 @@ package com.dfsek.terra.registry;
import com.dfsek.tectonic.api.depth.DepthTracker;
import com.dfsek.tectonic.api.exception.LoadException;
import com.dfsek.tectonic.api.loader.ConfigLoader;
import com.dfsek.terra.api.util.function.FunctionUtils;
import com.dfsek.terra.api.util.generic.data.types.Maybe;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimaps;
import org.jetbrains.annotations.NotNull;
@@ -35,6 +39,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.dfsek.terra.api.registry.OpenRegistry;
@@ -68,9 +73,11 @@ public class OpenRegistryImpl<T> implements OpenRegistry<T> {
@Override
public T load(@NotNull AnnotatedType type, @NotNull Object o, @NotNull ConfigLoader configLoader, DepthTracker depthTracker)
throws LoadException {
return getByID((String) o).orElseThrow(() -> new LoadException("No such " + type.getType().getTypeName() + " matching \"" + o +
"\" was found in this registry. Registry contains items: " +
getItemsFormatted(), depthTracker));
return getByID((String) o)
.collect(left -> FunctionUtils.throw_(new LoadException("Unable to retrieve " + type.getType().getTypeName() + " matching \"" + o +
"\" was found in this registry. Registry contains items: " +
getItemsFormatted() + "\n\nError:" + left, depthTracker)),
Function.identity());
}
private String getItemsFormatted() {
@@ -112,8 +119,8 @@ public class OpenRegistryImpl<T> implements OpenRegistry<T> {
@SuppressWarnings("unchecked")
@Override
public Optional<T> get(@NotNull RegistryKey key) {
return Optional.ofNullable(objects.getOrDefault(key, (Entry<T>) NULL).getValue());
public Maybe<T> get(@NotNull RegistryKey key) {
return Maybe.ofNullable(objects.getOrDefault(key, (Entry<T>) NULL).getValue());
}
@Override

View File

@@ -17,6 +17,8 @@
package registry;
import com.dfsek.terra.api.util.generic.data.types.Either;
import org.junit.jupiter.api.Test;
import com.dfsek.terra.api.registry.CheckedRegistry;
@@ -37,7 +39,7 @@ public class RegistryTest {
test.register(RegistryKey.parse("test:test"), "bazinga");
assertEquals(test.get(RegistryKey.parse("test:test")).orElseThrow(), "bazinga");
assertEquals(test.get(RegistryKey.parse("test:test")).orThrow(), "bazinga");
}
@Test
@@ -60,7 +62,7 @@ public class RegistryTest {
test.register(RegistryKey.parse("test:test"), "bazinga");
assertEquals(test.get(RegistryKey.parse("test:test")).orElseThrow(), "bazinga");
assertEquals(test.get(RegistryKey.parse("test:test")).orThrow(), "bazinga");
try {
test.register(RegistryKey.parse("test:test"), "bazinga2");
@@ -76,7 +78,7 @@ public class RegistryTest {
test.register(RegistryKey.parse("test:test"), "bazinga");
assertEquals(test.getByID("test").orElseThrow(), "bazinga");
assertEquals(test.getByID("test").collectThrow(RuntimeException::new), "bazinga");
}
@Test
@@ -86,11 +88,8 @@ public class RegistryTest {
test.registerChecked(RegistryKey.parse("test:test"), "bazinga");
test.registerChecked(RegistryKey.parse("test2:test"), "bazinga");
try {
test.getByID("test");
fail("Shouldn't be able to get with ambiguous ID!");
} catch(IllegalArgumentException ignore) {
Either<String, String> result = test.getByID("test");
assertTrue(result.isLeft());
}
}
}