mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 02:20:57 +00:00
add more api helpers
This commit is contained in:
@@ -62,9 +62,10 @@ public class LootFunction implements Function<Void> {
|
||||
|
||||
String id = data.apply(implementationArguments, scope);
|
||||
|
||||
|
||||
registry.get(RegistryKey.parse(id))
|
||||
.consume(table -> {
|
||||
RegistryKey.parse(id)
|
||||
.bind(registry::getEither)
|
||||
.consume(left -> LOGGER.error("No such loot table {}", id),
|
||||
table -> {
|
||||
Vector3 apply = Vector3.of(FloatingPointFunctions.round(xz.getX()),
|
||||
y.apply(implementationArguments, scope)
|
||||
.intValue(),
|
||||
@@ -92,7 +93,7 @@ public class LootFunction implements Function<Void> {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
).ifNothing(() -> LOGGER.error("No such loot table {}", id));
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.dfsek.terra.api.error;
|
||||
|
||||
public record InvalidKey(String message) implements Invalid {
|
||||
}
|
||||
@@ -9,6 +9,7 @@ package com.dfsek.terra.api.registry;
|
||||
|
||||
import com.dfsek.tectonic.api.loader.type.TypeLoader;
|
||||
|
||||
import com.dfsek.terra.api.error.Invalid;
|
||||
import com.dfsek.terra.api.error.InvalidLookup;
|
||||
import com.dfsek.terra.api.error.InvalidLookup.AmbiguousKey;
|
||||
import com.dfsek.terra.api.error.InvalidLookup.NoSuchElement;
|
||||
@@ -41,6 +42,10 @@ public interface Registry<T> extends TypeLoader<T> {
|
||||
@Contract(pure = true)
|
||||
Maybe<T> get(@NotNull RegistryKey key);
|
||||
|
||||
default Either<Invalid, T> getEither(@NotNull RegistryKey key) {
|
||||
return get(key).toEither(new NoSuchElement("No such element " + key.toString()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the registry contains a value.
|
||||
*
|
||||
@@ -89,7 +94,7 @@ public interface Registry<T> extends TypeLoader<T> {
|
||||
return getType().getRawType();
|
||||
}
|
||||
|
||||
default Either<InvalidLookup, T> getByID(String id) {
|
||||
default Either<Invalid, T> getByID(String id) {
|
||||
return getByID(id, map -> {
|
||||
if(map.isEmpty()) return Either.left(new NoSuchElement("No such value + \"" + id + "\""));
|
||||
if(map.size() == 1) {
|
||||
@@ -109,9 +114,10 @@ public interface Registry<T> extends TypeLoader<T> {
|
||||
|
||||
Map<RegistryKey, T> getMatches(String id);
|
||||
|
||||
default Either<InvalidLookup, T> getByID(String attempt, Function<Map<RegistryKey, T>, Either<InvalidLookup, T>> reduction) {
|
||||
default Either<Invalid, T> getByID(String attempt, Function<Map<RegistryKey, T>, Either<Invalid, T>> reduction) {
|
||||
if(attempt.contains(":")) {
|
||||
return get(RegistryKey.parse(attempt)).toEither(new NoSuchElement("No such value + \"" + attempt + "\""));
|
||||
return RegistryKey.parse(attempt)
|
||||
.bind(this::getEither);
|
||||
}
|
||||
return reduction.apply(getMatches(attempt));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package com.dfsek.terra.api.registry.key;
|
||||
|
||||
import com.dfsek.terra.api.error.Invalid;
|
||||
import com.dfsek.terra.api.error.InvalidKey;
|
||||
import com.dfsek.terra.api.util.generic.data.types.Either;
|
||||
import com.dfsek.terra.api.util.generic.data.types.Maybe;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -26,14 +31,14 @@ public final class RegistryKey implements StringIdentifiable, Namespaced {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public static RegistryKey parse(String key) {
|
||||
public static Either<Invalid, RegistryKey> parse(String key) {
|
||||
if(key.chars().filter(c -> c == ':').count() != 1) {
|
||||
throw new IllegalArgumentException("Malformed RegistryKey: " + key);
|
||||
return Either.left(new InvalidKey("Malformed RegistryKey: " + key));
|
||||
}
|
||||
String namespace = key.substring(0, key.indexOf(":"));
|
||||
String id = key.substring(key.indexOf(":") + 1);
|
||||
|
||||
return new RegistryKey(namespace, id);
|
||||
return Either.right(new RegistryKey(namespace, id));
|
||||
}
|
||||
|
||||
public static RegistryKey of(String namespace, String id) {
|
||||
|
||||
@@ -17,7 +17,6 @@ import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
@@ -63,6 +62,16 @@ public interface Either<L, R> extends Monad<R, Either<?, ?>>, BiFunctor<L, R, Ei
|
||||
|
||||
<U> U collect(Function<L, U> left, Function<R, U> right);
|
||||
|
||||
default Either<L, R> consume(Consumer<L> left, Consumer<R> right) {
|
||||
return mapLeft(l -> {
|
||||
left.accept(l);
|
||||
return l;
|
||||
}).mapRight(r -> {
|
||||
right.accept(r);
|
||||
return r;
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("Convert2MethodRef")
|
||||
default <T extends Throwable> R collectThrow(Function<L, T> left) throws T {
|
||||
return mapLeft(left).collect(l -> FunctionUtils.sneakyThrow(l), Function.identity());
|
||||
|
||||
@@ -104,12 +104,13 @@ public class MetaPackImpl implements MetaPack {
|
||||
logger.info("Loading metapack \"{}:{}\"", id, namespace);
|
||||
|
||||
template.getPacks().forEach((k, v) -> {
|
||||
RegistryKey registryKey = RegistryKey.parse(v);
|
||||
configRegistry.get(registryKey).consume(pack -> {
|
||||
packs.put(k, pack);
|
||||
RegistryKey.parse(v)
|
||||
.bind(configRegistry::getEither)
|
||||
.consume(left -> logger.warn("Failed to link config pack \"{}\" to metapack \"{}:{}\".", v, namespace, id),
|
||||
right -> {
|
||||
packs.put(k, right);
|
||||
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<>();
|
||||
|
||||
Reference in New Issue
Block a user