mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-20 15:20:25 +00:00
start working on error handling stuff
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -7,5 +7,5 @@ public interface Extendable {
|
||||
*
|
||||
* @return Whether this state is extended.
|
||||
*/
|
||||
default boolean isExtended() { return false; }
|
||||
default boolean extended() { return false; }
|
||||
}
|
||||
|
||||
@@ -28,5 +28,5 @@ public interface EntityTypeExtended extends EntityType {
|
||||
EntityType getType();
|
||||
|
||||
@Override
|
||||
default boolean isExtended() { return true; }
|
||||
default boolean extended() { return true; }
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.dfsek.terra.api.error;
|
||||
|
||||
public record InvalidBlockStateError(Exception exception) implements Invalid {
|
||||
@Override
|
||||
public String message() {
|
||||
return exception.getMessage();
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public final class RegistryKey implements StringIdentifiable, Namespaced {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNamespace() {
|
||||
public String namespace() {
|
||||
return namespace;
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user