start working on error handling stuff

This commit is contained in:
dfsek
2025-12-29 22:18:44 -07:00
parent 9a16336f53
commit cb08401536
76 changed files with 212 additions and 165 deletions

View File

@@ -31,7 +31,7 @@ public interface BaseAddon extends StringIdentifiable, Namespaced {
*
* @return Map of dependency ID to {@link VersionRange} of dependency
*/
default Map<String, VersionRange> getDependencies() {
default Map<String, VersionRange> dependencies() {
return Collections.emptyMap();
}
@@ -40,9 +40,9 @@ public interface BaseAddon extends StringIdentifiable, Namespaced {
*
* @return Version of addon
*/
Version getVersion();
Version version();
default String getNamespace() {
default String namespace() {
return getID();
}
}

View File

@@ -20,19 +20,19 @@ public interface BlockType extends Handle {
*
* @return Default block state
*/
BlockState getDefaultState();
BlockState defaultState();
/**
* Get whether this block is solid.
*
* @return Whether this block is solid.
*/
boolean isSolid();
boolean solid();
/**
* Get whether this block is water.
*
* @return Whether this block is water.
*/
boolean isWater();
boolean water();
}

View File

@@ -23,7 +23,7 @@ public interface BlockState extends Handle, Extendable {
/**
* Whether this {@link BlockState} matches another.
* <p>
* "matches" is defined as this {@link BlockState} holding a matching {@link #getBlockType()}.
* "matches" is defined as this {@link BlockState} holding a matching {@link #blockType()}.
*
* @param other Other {@link BlockState}
*
@@ -90,15 +90,15 @@ public interface BlockState extends Handle, Extendable {
*
* @return Block type.
*/
BlockType getBlockType();
BlockType blockType();
/**
* Get this state and its properties as a String
*
* @return String representation of this state
*/
default String getAsString() {
return getAsString(true);
default String asString() {
return asString(true);
}
/**
@@ -108,12 +108,12 @@ public interface BlockState extends Handle, Extendable {
*
* @return String representation of this state
*/
String getAsString(boolean properties);
String asString(boolean properties);
/**
* Get whether this BlockState is air
*
* @return Whether this state is air
*/
boolean isAir();
boolean air();
}

View File

@@ -9,7 +9,7 @@ public interface BlockStateExtended extends BlockState {
*
* @return BlockData of this BlockStateExtended
*/
ExtendedData getData();
ExtendedData data();
/**
* Sets the BlockData.
@@ -25,8 +25,8 @@ public interface BlockStateExtended extends BlockState {
*
* @return Raw BlockState of this BlockStateExtended
*/
BlockState getState();
BlockState state();
@Override
default boolean isExtended() { return true; }
default boolean extended() { return true; }
}

View File

@@ -16,7 +16,7 @@ import com.dfsek.terra.api.util.generic.data.types.Maybe;
public interface CommandSender extends Handle {
void sendMessage(String message);
Maybe<Entity> getEntity();
Maybe<Entity> entity();
Maybe<Player> getPlayer();
Maybe<Player> player();
}

View File

@@ -1,7 +1,5 @@
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;
@@ -14,7 +12,6 @@ import org.incendo.cloud.parser.ParserDescriptor;
import org.incendo.cloud.suggestion.Suggestion;
import org.incendo.cloud.suggestion.SuggestionProvider;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -103,15 +100,19 @@ public class RegistryArgument {
Registry<R> registry = registryFunction.apply(commandContext);
String finalInput = input;
return registry.get(RegistryKey.parse(input))
.map(ArgumentParseResult::success)
.orJust(() ->
registry.getByID(finalInput).collect(
left -> ArgumentParseResult.failure(new IllegalArgumentException(left)),
ArgumentParseResult::success
))
.get(() -> ArgumentParseResult.failure(new NoSuchEntryException("No such entry: " + finalInput)));
try {
return registry.get(RegistryKey.parse(input))
.map(ArgumentParseResult::success)
.orJust(() ->
registry.getByID(finalInput).collect(
left -> ArgumentParseResult.failure(new IllegalArgumentException(left)),
ArgumentParseResult::success
))
.get(() -> ArgumentParseResult.failure(new NoSuchEntryException("No such entry: " + finalInput)));
} catch(IllegalArgumentException e) {
return ArgumentParseResult.failure(e);
}
}
@Override

View File

@@ -7,5 +7,5 @@ public interface Extendable {
*
* @return Whether this state is extended.
*/
default boolean isExtended() { return false; }
default boolean extended() { return false; }
}

View File

@@ -28,5 +28,5 @@ public interface EntityTypeExtended extends EntityType {
EntityType getType();
@Override
default boolean isExtended() { return true; }
default boolean extended() { return true; }
}

View File

@@ -0,0 +1,12 @@
package com.dfsek.terra.api.error;
import com.dfsek.terra.api.util.generic.data.types.Either;
public interface Invalid {
String message();
default <T> Either<Invalid, T> left() {
return Either.left(this);
}
}

View File

@@ -0,0 +1,8 @@
package com.dfsek.terra.api.error;
public record InvalidBlockStateError(Exception exception) implements Invalid {
@Override
public String message() {
return exception.getMessage();
}
}

View File

@@ -7,6 +7,9 @@
package com.dfsek.terra.api.handle;
import com.dfsek.terra.api.error.Invalid;
import com.dfsek.terra.api.util.generic.data.types.Either;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
@@ -20,7 +23,7 @@ import com.dfsek.terra.api.entity.EntityType;
public interface WorldHandle {
@NotNull
@Contract("_ -> new")
BlockState createBlockState(@NotNull String data);
Either<Invalid, BlockState> createBlockState(@NotNull String data);
@NotNull
@Contract(pure = true)

View File

@@ -5,8 +5,8 @@ public interface Keyed<T extends Keyed<T>> extends Namespaced, StringIdentifiabl
RegistryKey getRegistryKey();
@Override
default String getNamespace() {
return getRegistryKey().getNamespace();
default String namespace() {
return getRegistryKey().namespace();
}
@Override

View File

@@ -1,9 +1,9 @@
package com.dfsek.terra.api.registry.key;
public interface Namespaced {
String getNamespace();
String namespace();
default RegistryKey key(String id) {
return RegistryKey.of(getNamespace(), id);
return RegistryKey.of(namespace(), id);
}
}

View File

@@ -41,7 +41,7 @@ public final class RegistryKey implements StringIdentifiable, Namespaced {
}
@Override
public String getNamespace() {
public String namespace() {
return namespace;
}

View File

@@ -46,7 +46,7 @@ public class BlockStateSet extends HashSet<BlockType> {
}
private void add(BlockState data) {
add(data.getBlockType());
add(data.blockType());
}
private static final class Singleton extends BlockStateSet {

View File

@@ -49,10 +49,10 @@ public interface Maybe<T> extends Monad<T, Maybe<?>> {
return this;
}
default <T1> Maybe<T1> overwrite(Maybe<T1> m) {
return bind(ignore -> m);
}
/**
* Project a new value into this Maybe if it is Nothing.
*/
Maybe<T> or(Supplier<Maybe<T>> or);
default Maybe<T> orJust(Supplier<T> or) {