mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 10:30:42 +00:00
convert a bunch of stuff to new APIs
This commit is contained in:
@@ -27,6 +27,6 @@ public class ReplaceableBiomeLoader implements TypeLoader<ReplaceableBiome> {
|
||||
return biomeRegistry
|
||||
.getByID((String) c)
|
||||
.map(ReplaceableBiome::of)
|
||||
.orElseThrow(() -> new LoadException("No such biome: " + c, depthTracker));
|
||||
.collectThrow(left -> new LoadException("No such biome: " + c + ": " + left, depthTracker));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.dfsek.tectonic.api.loader.type.TypeLoader;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.dfsek.terra.addons.biome.pipeline.api.biome.PipelineBiome;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
@@ -27,6 +28,6 @@ public class PipelineBiomeLoader implements TypeLoader<PipelineBiome> {
|
||||
return biomeRegistry
|
||||
.getByID((String) c)
|
||||
.map(PipelineBiome::from)
|
||||
.orElseGet(() -> PipelineBiome.placeholder((String) c));
|
||||
.collect(left -> PipelineBiome.placeholder((String) c), Function.identity());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public class LocateCommandAddon implements AddonInitializer {
|
||||
private BaseAddon addon;
|
||||
|
||||
private static Registry<Biome> getBiomeRegistry(CommandContext<CommandSender> sender) {
|
||||
return sender.sender().getEntity().orElseThrow().world().getPack().getRegistry(Biome.class);
|
||||
return sender.sender().getEntity().orThrow().world().getPack().getRegistry(Biome.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -69,7 +69,7 @@ public class LocateCommandAddon implements AddonInitializer {
|
||||
.handler(context -> {
|
||||
// 1. Gather Context & Arguments
|
||||
Biome targetBiome = context.get("biome");
|
||||
Entity sender = context.sender().getEntity().orElseThrow(
|
||||
Entity sender = context.sender().getEntity().orThrow(
|
||||
() -> new Error("Only entities can run this command."));
|
||||
World world = sender.world();
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ public class StructureCommandAddon implements AddonInitializer {
|
||||
private BaseAddon addon;
|
||||
|
||||
private static Registry<Structure> getStructureRegistry(CommandContext<CommandSender> sender) {
|
||||
return sender.sender().getEntity().orElseThrow().world().getPack().getRegistry(Structure.class);
|
||||
return sender.sender().getEntity().orThrow().world().getPack().getRegistry(Structure.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -55,7 +55,7 @@ public class StructureCommandAddon implements AddonInitializer {
|
||||
.optional("rotation", EnumParser.enumParser(Rotation.class), DefaultValue.constant(Rotation.NONE))
|
||||
.handler(context -> {
|
||||
Structure structure = context.get("structure");
|
||||
Entity sender = context.sender().getEntity().orElseThrow();
|
||||
Entity sender = context.sender().getEntity().orThrow();
|
||||
structure.generate(
|
||||
sender.position().toInt(),
|
||||
sender.world(),
|
||||
|
||||
@@ -64,7 +64,7 @@ public class LootFunction implements Function<Void> {
|
||||
|
||||
|
||||
registry.get(RegistryKey.parse(id))
|
||||
.ifPresentOrElse(table -> {
|
||||
.consume(table -> {
|
||||
Vector3 apply = Vector3.of(FloatingPointFunctions.round(xz.getX()),
|
||||
y.apply(implementationArguments, scope)
|
||||
.intValue(),
|
||||
@@ -91,8 +91,8 @@ public class LootFunction implements Function<Void> {
|
||||
LOGGER.error("Could not apply loot at {}", apply, e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
},
|
||||
() -> LOGGER.error("No such loot table {}", id));
|
||||
}
|
||||
).ifNothing(() -> LOGGER.error("No such loot table {}", id));
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -91,10 +91,10 @@ public class StructureFunction implements Function<Boolean> {
|
||||
FloatingPointFunctions.round(xz.getZ())),
|
||||
arguments.getRandom(),
|
||||
arguments.getRotation().rotate(rotation1));
|
||||
}).orElseGet(() -> {
|
||||
}).collect(left -> {
|
||||
LOGGER.error("No such structure {}", app);
|
||||
return false;
|
||||
});
|
||||
}, java.util.function.Function.identity());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -7,17 +7,16 @@
|
||||
|
||||
package com.dfsek.terra.api.command;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import com.dfsek.terra.api.Handle;
|
||||
import com.dfsek.terra.api.entity.Entity;
|
||||
import com.dfsek.terra.api.entity.Player;
|
||||
import com.dfsek.terra.api.util.generic.data.types.Maybe;
|
||||
|
||||
|
||||
public interface CommandSender extends Handle {
|
||||
void sendMessage(String message);
|
||||
|
||||
Optional<Entity> getEntity();
|
||||
Maybe<Entity> getEntity();
|
||||
|
||||
Optional<Player> getPlayer();
|
||||
Maybe<Player> getPlayer();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.dfsek.terra.api.command.arguments;
|
||||
|
||||
import com.dfsek.terra.api.util.generic.data.types.Maybe;
|
||||
|
||||
import io.leangen.geantyref.TypeToken;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.incendo.cloud.component.CommandComponent;
|
||||
@@ -100,20 +102,16 @@ public class RegistryArgument {
|
||||
|
||||
Registry<R> registry = registryFunction.apply(commandContext);
|
||||
|
||||
Optional<R> result;
|
||||
try {
|
||||
result = registry.get(RegistryKey.parse(input));
|
||||
} catch(IllegalArgumentException e) {
|
||||
try {
|
||||
result = registry.getByID(input);
|
||||
} catch(IllegalArgumentException e1) {
|
||||
return ArgumentParseResult.failure(e1);
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
String finalInput = input;
|
||||
return registry.get(RegistryKey.parse(input))
|
||||
.map(ArgumentParseResult::success)
|
||||
.orElse(ArgumentParseResult.failure(new NoSuchEntryException("No such entry: " + input)));
|
||||
.orJust(() ->
|
||||
registry.getByID(finalInput).collect(
|
||||
left -> ArgumentParseResult.failure(new IllegalArgumentException(left)),
|
||||
ArgumentParseResult::success
|
||||
))
|
||||
.get(() -> ArgumentParseResult.failure(new NoSuchEntryException("No such entry: " + finalInput)));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -8,6 +8,10 @@
|
||||
package com.dfsek.terra.api.registry;
|
||||
|
||||
import com.dfsek.tectonic.api.loader.type.TypeLoader;
|
||||
|
||||
import com.dfsek.terra.api.util.generic.data.types.Either;
|
||||
import com.dfsek.terra.api.util.generic.data.types.Maybe;
|
||||
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -32,7 +36,7 @@ public interface Registry<T> extends TypeLoader<T> {
|
||||
* @return Value matching the identifier, {@code null} if no value is present.
|
||||
*/
|
||||
@Contract(pure = true)
|
||||
Optional<T> get(@NotNull RegistryKey key);
|
||||
Maybe<T> get(@NotNull RegistryKey key);
|
||||
|
||||
/**
|
||||
* Check if the registry contains a value.
|
||||
@@ -82,13 +86,13 @@ public interface Registry<T> extends TypeLoader<T> {
|
||||
return getType().getRawType();
|
||||
}
|
||||
|
||||
default Optional<T> getByID(String id) {
|
||||
default Either<String, T> getByID(String id) {
|
||||
return getByID(id, map -> {
|
||||
if(map.isEmpty()) return Optional.empty();
|
||||
if(map.isEmpty()) return Either.left("No such element.");
|
||||
if(map.size() == 1) {
|
||||
return map.values().stream().findFirst(); // only one value.
|
||||
return Either.right(map.values().stream().findFirst().get()); // only one value.
|
||||
}
|
||||
throw new IllegalArgumentException("ID \"" + id + "\" is ambiguous; matches: " + map
|
||||
return Either.<String, T>left("ID \"" + id + "\" is ambiguous; matches: " + map
|
||||
.keySet()
|
||||
.stream()
|
||||
.map(RegistryKey::toString)
|
||||
@@ -102,9 +106,9 @@ public interface Registry<T> extends TypeLoader<T> {
|
||||
|
||||
Map<RegistryKey, T> getMatches(String id);
|
||||
|
||||
default Optional<T> getByID(String attempt, Function<Map<RegistryKey, T>, Optional<T>> reduction) {
|
||||
default Either<String, T> getByID(String attempt, Function<Map<RegistryKey, T>, Either<String, T>> reduction) {
|
||||
if(attempt.contains(":")) {
|
||||
return get(RegistryKey.parse(attempt));
|
||||
return get(RegistryKey.parse(attempt)).toEither("No such value.");
|
||||
}
|
||||
return reduction.apply(getMatches(attempt));
|
||||
}
|
||||
|
||||
@@ -26,6 +26,15 @@ public final class FunctionUtils {
|
||||
return either.collect(Function.identity(), Function.identity());
|
||||
}
|
||||
|
||||
public static <T extends Throwable, U> U throw_(T e) throws T {
|
||||
throw e;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <E extends Throwable, U> U sneakyThrow(Throwable e) throws E {
|
||||
throw (E) e;
|
||||
}
|
||||
|
||||
public static <T, U> Function<T, Either<Exception, U>> liftTry(Function<T, U> f) {
|
||||
return s -> {
|
||||
try {
|
||||
|
||||
@@ -51,9 +51,9 @@ public interface Either<L, R> extends Monad<R, Either<?, ?>>, BiFunctor<L, R, Ei
|
||||
@Override
|
||||
<R1> Either<L, R1> mapRight(Function<R, R1> f);
|
||||
|
||||
Optional<L> getLeft();
|
||||
Maybe<L> getLeft();
|
||||
|
||||
Optional<R> getRight();
|
||||
Maybe<R> getRight();
|
||||
|
||||
boolean isLeft();
|
||||
|
||||
@@ -63,6 +63,11 @@ public interface Either<L, R> extends Monad<R, Either<?, ?>>, BiFunctor<L, R, Ei
|
||||
|
||||
<U> U collect(Function<L, U> left, Function<R, U> right);
|
||||
|
||||
@SuppressWarnings("Convert2MethodRef")
|
||||
default <T extends Throwable> R collectThrow(Function<L, T> left) throws T{
|
||||
return mapLeft(left).collect(l -> FunctionUtils.sneakyThrow(l), Function.identity());
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
@NotNull
|
||||
@Contract("_ -> new")
|
||||
@@ -87,13 +92,13 @@ public interface Either<L, R> extends Monad<R, Either<?, ?>>, BiFunctor<L, R, Ei
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<L> getLeft() {
|
||||
return Optional.of(value);
|
||||
public Maybe<L> getLeft() {
|
||||
return Maybe.just(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<R> getRight() {
|
||||
return Optional.empty();
|
||||
public Maybe<R> getRight() {
|
||||
return Maybe.nothing();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -142,13 +147,13 @@ public interface Either<L, R> extends Monad<R, Either<?, ?>>, BiFunctor<L, R, Ei
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<L> getLeft() {
|
||||
return Optional.empty();
|
||||
public Maybe<L> getLeft() {
|
||||
return Maybe.nothing();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<R> getRight() {
|
||||
return Optional.of(value);
|
||||
public Maybe<R> getRight() {
|
||||
return Maybe.just(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,7 +2,9 @@ package com.dfsek.terra.api.util.generic.data.types;
|
||||
|
||||
import com.dfsek.terra.api.util.generic.control.Monad;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
@@ -21,6 +23,7 @@ public interface Maybe<T> extends Monad<T, Maybe<?>> {
|
||||
Optional<T> toOptional();
|
||||
|
||||
<L> Either<L, T> toEither(L l);
|
||||
|
||||
T get(Supplier<T> def);
|
||||
|
||||
boolean isJust();
|
||||
@@ -32,12 +35,45 @@ public interface Maybe<T> extends Monad<T, Maybe<?>> {
|
||||
return get(() -> def);
|
||||
}
|
||||
|
||||
default Maybe<T> consume(Consumer<T> c) {
|
||||
return map(m -> {
|
||||
c.accept(m);
|
||||
return m;
|
||||
});
|
||||
}
|
||||
|
||||
default Maybe<T> ifNothing(Runnable r) {
|
||||
if(!isJust()) {
|
||||
r.run();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
default <T1> Maybe<T1> overwrite(Maybe<T1> m) {
|
||||
return bind(ignore -> m);
|
||||
}
|
||||
|
||||
Maybe<T> or(Supplier<Maybe<T>> or);
|
||||
|
||||
default Maybe<T> orJust(Supplier<T> or) {
|
||||
return or(() -> just(or.get()));
|
||||
}
|
||||
|
||||
|
||||
@Deprecated
|
||||
default <X extends Throwable> T orThrow() {
|
||||
return get(() -> { throw new NoSuchElementException("No value present."); });
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
default <X extends Throwable> T orThrow(Supplier<X> e) throws X {
|
||||
if(isJust()) {
|
||||
return orThrow();
|
||||
}
|
||||
throw e.get();
|
||||
}
|
||||
|
||||
|
||||
default Maybe<T> or(Maybe<T> or) {
|
||||
return or(() -> or);
|
||||
}
|
||||
@@ -54,6 +90,11 @@ public interface Maybe<T> extends Monad<T, Maybe<?>> {
|
||||
return op.map(Maybe::just).orElseGet(Maybe::nothing);
|
||||
}
|
||||
|
||||
static <T> Maybe<T> ofNullable(T t) {
|
||||
if(t == null) return nothing();
|
||||
return just(t);
|
||||
}
|
||||
|
||||
static <T1> Maybe<T1> just(T1 t) {
|
||||
record Just<T>(T value) implements Maybe<T> {
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
|
||||
@@ -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<>();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ public class AllayPlatform extends AbstractPlatform {
|
||||
boolean succeed = loadConfigPacks();
|
||||
|
||||
GENERATOR_WRAPPERS.forEach(wrapper -> {
|
||||
getConfigRegistry().get(wrapper.getConfigPack().getRegistryKey()).ifPresent(pack -> {
|
||||
getConfigRegistry().get(wrapper.getConfigPack().getRegistryKey()).consume(pack -> {
|
||||
wrapper.setConfigPack(pack);
|
||||
var dimension = wrapper.getAllayWorldGenerator().getDimension();
|
||||
TerraAllayPlugin.instance.getPluginLogger().info(
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.dfsek.terra.allay.generator;
|
||||
|
||||
import com.dfsek.terra.api.util.function.FunctionUtils;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import org.allaymc.api.utils.AllayStringUtils;
|
||||
import org.allaymc.api.world.biome.BiomeType;
|
||||
@@ -24,6 +26,8 @@ import com.dfsek.terra.api.world.chunk.generation.stage.GenerationStage;
|
||||
import com.dfsek.terra.api.world.chunk.generation.util.GeneratorWrapper;
|
||||
import com.dfsek.terra.api.world.info.WorldProperties;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
|
||||
/**
|
||||
* @author daoge_cmd
|
||||
@@ -91,14 +95,16 @@ public class AllayGeneratorWrapper implements GeneratorWrapper {
|
||||
return TerraAllayPlugin.platform
|
||||
.getConfigRegistry()
|
||||
.getByID(packId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Cant find terra config pack named " + packId));
|
||||
.collectThrow(
|
||||
left -> new IllegalArgumentException("Cant find terra config pack named " + packId + ": " + left));
|
||||
}
|
||||
|
||||
protected static ConfigPack getConfigPackByMeta(String metaPackId, DimensionInfo dimensionInfo) {
|
||||
return TerraAllayPlugin.platform
|
||||
.getMetaConfigRegistry()
|
||||
.getByID(metaPackId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Cant find terra meta pack named " + metaPackId))
|
||||
.collectThrow(
|
||||
left -> new IllegalArgumentException("Cant find terra meta pack named " + metaPackId + ": " + left))
|
||||
.packs()
|
||||
.get(Mapping.dimensionIdBeToJe(dimensionInfo.toString()));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.dfsek.terra.bukkit;
|
||||
|
||||
import com.dfsek.terra.api.util.generic.data.types.Maybe;
|
||||
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
@@ -24,19 +26,19 @@ public class CloudCommandSender implements CommandSender {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Entity> getEntity() {
|
||||
public Maybe<Entity> getEntity() {
|
||||
if(delegate instanceof org.bukkit.entity.Entity entity) {
|
||||
return Optional.of(BukkitAdapter.adapt(entity));
|
||||
return Maybe.just(BukkitAdapter.adapt(entity));
|
||||
}
|
||||
return Optional.empty();
|
||||
return Maybe.nothing();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Player> getPlayer() {
|
||||
public Maybe<Player> getPlayer() {
|
||||
if(delegate instanceof org.bukkit.entity.Player player) {
|
||||
return Optional.of(BukkitAdapter.adapt(player));
|
||||
return Maybe.just(BukkitAdapter.adapt(player));
|
||||
}
|
||||
return Optional.empty();
|
||||
return Maybe.nothing();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -73,7 +73,7 @@ public class PlatformImpl extends AbstractPlatform {
|
||||
|
||||
Bukkit.getWorlds().forEach(world -> {
|
||||
if(world.getGenerator() instanceof BukkitChunkGeneratorWrapper wrapper) {
|
||||
getConfigRegistry().get(wrapper.getPack().getRegistryKey()).ifPresent(pack -> {
|
||||
getConfigRegistry().get(wrapper.getPack().getRegistryKey()).consume(pack -> {
|
||||
wrapper.setPack(pack);
|
||||
LOGGER.info("Replaced pack in chunk generator for world {}", world);
|
||||
});
|
||||
|
||||
@@ -182,10 +182,10 @@ public class TerraBukkitPlugin extends JavaPlugin {
|
||||
ChunkGenerator getDefaultWorldGenerator(@NotNull String worldName, String id) {
|
||||
if(id == null || id.trim().isEmpty()) { return null; }
|
||||
return new BukkitChunkGeneratorWrapper(generatorMap.computeIfAbsent(worldName, name -> {
|
||||
ConfigPack pack = platform.getConfigRegistry().getByID(id).orElseThrow(
|
||||
() -> new IllegalArgumentException("No such config pack \"" + id + "\""));
|
||||
ConfigPack pack = platform.getConfigRegistry().getByID(id).collectThrow(
|
||||
left -> new IllegalArgumentException("No such config pack \"" + id + "\": " + left));
|
||||
return pack.getGeneratorProvider().newInstance(pack);
|
||||
}), platform.getRawConfigRegistry().getByID(id).orElseThrow(), platform.getWorldHandle().air());
|
||||
}), platform.getRawConfigRegistry().getByID(id).collectThrow(RuntimeException::new), platform.getWorldHandle().air());
|
||||
}
|
||||
|
||||
public AsyncScheduler getAsyncScheduler() {
|
||||
|
||||
@@ -80,7 +80,7 @@ public class CommonListener implements Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
ConfigPack pack = platform.getConfigRegistry().get(wrapper.getPack().getRegistryKey()).orElse(null);
|
||||
ConfigPack pack = platform.getConfigRegistry().get(wrapper.getPack().getRegistryKey()).get((ConfigPack) null);
|
||||
if(pack == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ public final class TerraCLI implements Callable<Integer> {
|
||||
CLIPlatform platform = new CLIPlatform();
|
||||
platform.getEventManager().callEvent(new PlatformInitializationEvent());
|
||||
|
||||
ConfigPack generate = platform.getConfigRegistry().getByID(pack).orElseThrow();
|
||||
ConfigPack generate = platform.getConfigRegistry().getByID(pack).collectThrow(RuntimeException::new);
|
||||
|
||||
CLIWorld world = new CLIWorld(size, seed, maxHeight, minHeight, generate, noSave);
|
||||
|
||||
|
||||
@@ -95,7 +95,7 @@ public final class TerraMinestomPlatform extends AbstractPlatform {
|
||||
|
||||
MinecraftServer.getInstanceManager().getInstances().forEach(world -> {
|
||||
if(world.generator() instanceof MinestomChunkGeneratorWrapper wrapper) {
|
||||
getConfigRegistry().get(wrapper.getPack().getRegistryKey()).ifPresent(pack -> {
|
||||
getConfigRegistry().get(wrapper.getPack().getRegistryKey()).consume(pack -> {
|
||||
wrapper.setPack(pack);
|
||||
LOGGER.info("Replaced pack in chunk generator for instance {}", world.getUuid());
|
||||
});
|
||||
|
||||
@@ -39,14 +39,14 @@ public class TerraMinestomWorldBuilder {
|
||||
}
|
||||
|
||||
public TerraMinestomWorldBuilder packById(String id) {
|
||||
this.pack = platform.getConfigRegistry().getByID(id).orElseThrow();
|
||||
this.pack = platform.getConfigRegistry().getByID(id).collectThrow(RuntimeException::new);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TerraMinestomWorldBuilder packByMeta(String metaPack, RegistryKey<@NonNull DimensionType> dimensionType) {
|
||||
this.pack = platform.getMetaConfigRegistry()
|
||||
.getByID(metaPack)
|
||||
.orElseThrow(() -> new RuntimeException("MetaPack " + metaPack + " could not be found"))
|
||||
.collectThrow(left -> new RuntimeException("MetaPack " + metaPack + " could not be found: " + left))
|
||||
.packs()
|
||||
.get(dimensionType.key().asString());
|
||||
return this;
|
||||
|
||||
@@ -31,7 +31,7 @@ public final class Codecs {
|
||||
.apply(config, config.stable(id -> CommonPlatform.get()
|
||||
.getConfigRegistry()
|
||||
.get(id)
|
||||
.orElseThrow(() -> new IllegalArgumentException(
|
||||
.orThrow(() -> new IllegalArgumentException(
|
||||
"No such config pack " +
|
||||
id)))));
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@ public abstract class LifecyclePlatform extends ModPlatform {
|
||||
}).join();
|
||||
server.getWorlds().forEach(world -> {
|
||||
if(world.getChunkManager().getChunkGenerator() instanceof MinecraftChunkGeneratorWrapper chunkGeneratorWrapper) {
|
||||
getConfigRegistry().get(chunkGeneratorWrapper.getPack().getRegistryKey()).ifPresent(pack -> {
|
||||
getConfigRegistry().get(chunkGeneratorWrapper.getPack().getRegistryKey()).consume(pack -> {
|
||||
chunkGeneratorWrapper.setPack(pack);
|
||||
LOGGER.info("Replaced pack in chunk generator for world {}", world);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user