mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-06-24 13:31:33 +00:00
refactor some stuff to use new errors
This commit is contained in:
+1
-1
@@ -105,7 +105,7 @@ public class RegistryArgument {
|
|||||||
.map(ArgumentParseResult::success)
|
.map(ArgumentParseResult::success)
|
||||||
.orJust(() ->
|
.orJust(() ->
|
||||||
registry.getByID(finalInput).collect(
|
registry.getByID(finalInput).collect(
|
||||||
left -> ArgumentParseResult.failure(new IllegalArgumentException(left)),
|
left -> ArgumentParseResult.failure(left.toIllegal()),
|
||||||
ArgumentParseResult::success
|
ArgumentParseResult::success
|
||||||
))
|
))
|
||||||
.get(() -> ArgumentParseResult.failure(new NoSuchEntryException("No such entry: " + finalInput)));
|
.get(() -> ArgumentParseResult.failure(new NoSuchEntryException("No such entry: " + finalInput)));
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.dfsek.terra.api.error;
|
||||||
|
|
||||||
|
public interface InvalidLookup extends Invalid {
|
||||||
|
record NoSuchElement(String message) implements InvalidLookup {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
record AmbiguousKey(String message) implements InvalidLookup {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,9 @@ package com.dfsek.terra.api.registry;
|
|||||||
|
|
||||||
import com.dfsek.tectonic.api.loader.type.TypeLoader;
|
import com.dfsek.tectonic.api.loader.type.TypeLoader;
|
||||||
|
|
||||||
|
import com.dfsek.terra.api.error.InvalidLookup;
|
||||||
|
import com.dfsek.terra.api.error.InvalidLookup.AmbiguousKey;
|
||||||
|
import com.dfsek.terra.api.error.InvalidLookup.NoSuchElement;
|
||||||
import com.dfsek.terra.api.util.generic.data.types.Either;
|
import com.dfsek.terra.api.util.generic.data.types.Either;
|
||||||
import com.dfsek.terra.api.util.generic.data.types.Maybe;
|
import com.dfsek.terra.api.util.generic.data.types.Maybe;
|
||||||
|
|
||||||
@@ -86,17 +89,17 @@ public interface Registry<T> extends TypeLoader<T> {
|
|||||||
return getType().getRawType();
|
return getType().getRawType();
|
||||||
}
|
}
|
||||||
|
|
||||||
default Either<String, T> getByID(String id) {
|
default Either<InvalidLookup, T> getByID(String id) {
|
||||||
return getByID(id, map -> {
|
return getByID(id, map -> {
|
||||||
if(map.isEmpty()) return Either.left("No such element.");
|
if(map.isEmpty()) return Either.left(new NoSuchElement("No such value + \"" + id + "\""));
|
||||||
if(map.size() == 1) {
|
if(map.size() == 1) {
|
||||||
return Either.right(map.values().stream().findFirst().get()); // only one value.
|
return Either.right(map.values().stream().findFirst().get()); // only one value.
|
||||||
}
|
}
|
||||||
return Either.<String, T>left("ID \"" + id + "\" is ambiguous; matches: " + map
|
return Either.left(new AmbiguousKey("ID \"" + id + "\" is ambiguous; matches: " + map
|
||||||
.keySet()
|
.keySet()
|
||||||
.stream()
|
.stream()
|
||||||
.map(RegistryKey::toString)
|
.map(RegistryKey::toString)
|
||||||
.reduce("", (a, b) -> a + "\n - " + b));
|
.reduce("", (a, b) -> a + "\n - " + b)));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,9 +109,9 @@ public interface Registry<T> extends TypeLoader<T> {
|
|||||||
|
|
||||||
Map<RegistryKey, T> getMatches(String id);
|
Map<RegistryKey, T> getMatches(String id);
|
||||||
|
|
||||||
default Either<String, T> getByID(String attempt, Function<Map<RegistryKey, T>, Either<String, T>> reduction) {
|
default Either<InvalidLookup, T> getByID(String attempt, Function<Map<RegistryKey, T>, Either<InvalidLookup, T>> reduction) {
|
||||||
if(attempt.contains(":")) {
|
if(attempt.contains(":")) {
|
||||||
return get(RegistryKey.parse(attempt)).toEither("No such value.");
|
return get(RegistryKey.parse(attempt)).toEither(new NoSuchElement("No such value + \"" + attempt + "\""));
|
||||||
}
|
}
|
||||||
return reduction.apply(getMatches(attempt));
|
return reduction.apply(getMatches(attempt));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,8 @@
|
|||||||
|
|
||||||
package registry;
|
package registry;
|
||||||
|
|
||||||
|
import com.dfsek.terra.api.error.Invalid;
|
||||||
|
import com.dfsek.terra.api.error.InvalidLookup;
|
||||||
import com.dfsek.terra.api.util.generic.data.types.Either;
|
import com.dfsek.terra.api.util.generic.data.types.Either;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@@ -78,7 +80,7 @@ public class RegistryTest {
|
|||||||
|
|
||||||
test.register(RegistryKey.parse("test:test"), "bazinga");
|
test.register(RegistryKey.parse("test:test"), "bazinga");
|
||||||
|
|
||||||
assertEquals(test.getByID("test").collectThrow(RuntimeException::new), "bazinga");
|
assertEquals(test.getByID("test").collectThrow(Invalid::toIllegal), "bazinga");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -88,7 +90,7 @@ public class RegistryTest {
|
|||||||
test.registerChecked(RegistryKey.parse("test:test"), "bazinga");
|
test.registerChecked(RegistryKey.parse("test:test"), "bazinga");
|
||||||
test.registerChecked(RegistryKey.parse("test2:test"), "bazinga");
|
test.registerChecked(RegistryKey.parse("test2:test"), "bazinga");
|
||||||
|
|
||||||
Either<String, String> result = test.getByID("test");
|
Either<InvalidLookup, String> result = test.getByID("test");
|
||||||
assertTrue(result.isLeft());
|
assertTrue(result.isLeft());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,8 @@
|
|||||||
|
|
||||||
package com.dfsek.terra.bukkit;
|
package com.dfsek.terra.bukkit;
|
||||||
|
|
||||||
|
import com.dfsek.terra.api.error.Invalid;
|
||||||
|
|
||||||
import io.papermc.paper.threadedregions.scheduler.AsyncScheduler;
|
import io.papermc.paper.threadedregions.scheduler.AsyncScheduler;
|
||||||
import io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler;
|
import io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
@@ -185,7 +187,7 @@ public class TerraBukkitPlugin extends JavaPlugin {
|
|||||||
ConfigPack pack = platform.getConfigRegistry().getByID(id).collectThrow(
|
ConfigPack pack = platform.getConfigRegistry().getByID(id).collectThrow(
|
||||||
left -> new IllegalArgumentException("No such config pack \"" + id + "\": " + left));
|
left -> new IllegalArgumentException("No such config pack \"" + id + "\": " + left));
|
||||||
return pack.getGeneratorProvider().newInstance(pack);
|
return pack.getGeneratorProvider().newInstance(pack);
|
||||||
}), platform.getRawConfigRegistry().getByID(id).collectThrow(RuntimeException::new), platform.getWorldHandle().air());
|
}), platform.getRawConfigRegistry().getByID(id).collectThrow(Invalid::toIllegal), platform.getWorldHandle().air());
|
||||||
}
|
}
|
||||||
|
|
||||||
public AsyncScheduler getAsyncScheduler() {
|
public AsyncScheduler getAsyncScheduler() {
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package com.dfsek.terra.cli;
|
package com.dfsek.terra.cli;
|
||||||
|
|
||||||
import com.dfsek.seismic.type.vector.Vector2Int;
|
import com.dfsek.seismic.type.vector.Vector2Int;
|
||||||
|
|
||||||
|
import com.dfsek.terra.api.error.Invalid;
|
||||||
|
|
||||||
import net.querz.mca.MCAUtil;
|
import net.querz.mca.MCAUtil;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -51,7 +54,7 @@ public final class TerraCLI implements Callable<Integer> {
|
|||||||
CLIPlatform platform = new CLIPlatform();
|
CLIPlatform platform = new CLIPlatform();
|
||||||
platform.getEventManager().callEvent(new PlatformInitializationEvent());
|
platform.getEventManager().callEvent(new PlatformInitializationEvent());
|
||||||
|
|
||||||
ConfigPack generate = platform.getConfigRegistry().getByID(pack).collectThrow(RuntimeException::new);
|
ConfigPack generate = platform.getConfigRegistry().getByID(pack).collectThrow(Invalid::toIllegal);
|
||||||
|
|
||||||
CLIWorld world = new CLIWorld(size, seed, maxHeight, minHeight, generate, noSave);
|
CLIWorld world = new CLIWorld(size, seed, maxHeight, minHeight, generate, noSave);
|
||||||
|
|
||||||
|
|||||||
+3
-1
@@ -1,5 +1,7 @@
|
|||||||
package com.dfsek.terra.minestom.api;
|
package com.dfsek.terra.minestom.api;
|
||||||
|
|
||||||
|
import com.dfsek.terra.api.error.Invalid;
|
||||||
|
|
||||||
import net.minestom.server.instance.Instance;
|
import net.minestom.server.instance.Instance;
|
||||||
import net.minestom.server.registry.RegistryKey;
|
import net.minestom.server.registry.RegistryKey;
|
||||||
import net.minestom.server.world.DimensionType;
|
import net.minestom.server.world.DimensionType;
|
||||||
@@ -39,7 +41,7 @@ public class TerraMinestomWorldBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public TerraMinestomWorldBuilder packById(String id) {
|
public TerraMinestomWorldBuilder packById(String id) {
|
||||||
this.pack = platform.getConfigRegistry().getByID(id).collectThrow(RuntimeException::new);
|
this.pack = platform.getConfigRegistry().getByID(id).collectThrow(Invalid::toIllegal);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user