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

@@ -15,7 +15,7 @@ public class ApiAddon implements BaseAddon {
}
@Override
public Version getVersion() {
public Version version() {
return version;
}

View File

@@ -33,7 +33,7 @@ public class ApiAddonLoader implements BootstrapBaseAddon<BaseAddon> {
}
@Override
public Version getVersion() {
public Version version() {
return VERSION;
}
}

View File

@@ -39,7 +39,7 @@ public class AddonsCommandAddon implements AddonInitializer {
.append(" - ")
.append(addon.getID())
.append('@')
.append(addon.getVersion().getFormatted())
.append(addon.version().getFormatted())
.append('\n'));
context.sender().sendMessage(addons.toString());
})
@@ -52,10 +52,10 @@ public class AddonsCommandAddon implements AddonInitializer {
BaseAddon addon = context.get("addon");
StringBuilder addonInfo = new StringBuilder("Addon ").append(addon.getID()).append('\n');
addonInfo.append("Version: ").append(addon.getVersion().getFormatted()).append('\n');
addonInfo.append("Version: ").append(addon.version().getFormatted()).append('\n');
addonInfo.append("Dependencies:\n");
addon.getDependencies().forEach((id, versions) -> addonInfo
addon.dependencies().forEach((id, versions) -> addonInfo
.append(" - ")
.append(id)
.append('@')

View File

@@ -4,7 +4,6 @@ package com.dfsek.terra.addons.commands.locate;
import com.dfsek.seismic.type.vector.Vector2Int;
import com.dfsek.seismic.type.vector.Vector3Int;
import com.dfsek.terra.api.util.function.FunctionUtils;
import com.dfsek.terra.api.util.generic.data.types.Maybe;
import org.incendo.cloud.CommandManager;
@@ -13,8 +12,6 @@ import org.incendo.cloud.context.CommandContext;
import org.incendo.cloud.description.Description;
import org.incendo.cloud.parser.standard.IntegerParser;
import java.util.Optional;
import com.dfsek.terra.addons.manifest.api.AddonInitializer;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.addon.BaseAddon;
@@ -41,7 +38,7 @@ public class LocateCommandAddon implements AddonInitializer {
private BaseAddon addon;
private static Registry<Biome> getBiomeRegistry(CommandContext<CommandSender> sender) {
return sender.sender().getEntity().orThrow().world().getPack().getRegistry(Biome.class);
return sender.sender().entity().orThrow().world().getPack().getRegistry(Biome.class);
}
@Override
@@ -69,7 +66,7 @@ public class LocateCommandAddon implements AddonInitializer {
.handler(context -> {
// 1. Gather Context & Arguments
Biome targetBiome = context.get("biome");
Entity sender = context.sender().getEntity().orThrow(
Entity sender = context.sender().entity().orThrow(
() -> new Error("Only entities can run this command."));
World world = sender.world();

View File

@@ -34,7 +34,7 @@ public class StructureCommandAddon implements AddonInitializer {
private BaseAddon addon;
private static Registry<Structure> getStructureRegistry(CommandContext<CommandSender> sender) {
return sender.sender().getEntity().orThrow().world().getPack().getRegistry(Structure.class);
return sender.sender().entity().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().orThrow();
Entity sender = context.sender().entity().orThrow();
structure.generate(
sender.position().toInt(),
sender.world(),

View File

@@ -55,7 +55,7 @@ public class TerraFlora implements Structure {
private void test(EnumSet<Direction> faces, Direction f, Vector3Int b, WritableWorld world) {
if(testRotation.contains(
world.getBlockState(b.getX() + f.getModX(), b.getY() + f.getModY(), b.getZ() + f.getModZ()).getBlockType()))
world.getBlockState(b.getX() + f.getModX(), b.getY() + f.getModY(), b.getZ() + f.getModZ()).blockType()))
faces.add(f);
}

View File

@@ -24,6 +24,6 @@ public class AirMatchPatternTemplate implements ObjectTemplate<Pattern> {
@Override
public Pattern get() {
return new MatchPattern(offset, BlockState::isAir);
return new MatchPattern(offset, BlockState::air);
}
}

View File

@@ -26,6 +26,6 @@ public class BlockSetMatchPatternTemplate implements ObjectTemplate<Pattern> {
@Override
public Pattern get() {
return new MatchPattern(offset, blockState -> blocks.contains(blockState.getBlockType()));
return new MatchPattern(offset, blockState -> blocks.contains(blockState.blockType()));
}
}

View File

@@ -21,6 +21,6 @@ public class SolidMatchPatternTemplate implements ObjectTemplate<Pattern> {
@Override
public Pattern get() {
return new MatchPattern(offset, blockState -> blockState.getBlockType().isSolid());
return new MatchPattern(offset, blockState -> blockState.blockType().solid());
}
}

View File

@@ -28,7 +28,7 @@ public class SurfaceLocator implements Locator {
int min = Math.max(search.getMin(), column.getMinY());
if(min >= max) return builder.build();
for(int y = min; y < max; y++) {
if(column.getBlock(y).isAir() && !column.getBlock(y - 1).isAir()) {
if(column.getBlock(y).air() && !column.getBlock(y - 1).air()) {
builder.set(y);
}
}

View File

@@ -23,7 +23,7 @@ public class TopLocator implements Locator {
@Override
public BinaryColumn getSuitableCoordinates(Column<?> column) {
for(int y = search.getMax(); y >= search.getMin(); y--) {
if(column.getBlock(y).isAir() && !column.getBlock(y - 1).isAir()) {
if(column.getBlock(y).air() && !column.getBlock(y - 1).air()) {
return new BinaryColumn(y, y + 1, yi -> true);
}
}

View File

@@ -149,7 +149,7 @@ public class VanillaOre implements Structure {
if(!visited.get(index)) { // Skip blocks that have already been visited
visited.set(index);
BlockType block = world.getBlockState(xi, yi, zi).getBlockType();
BlockType block = world.getBlockState(xi, yi, zi).blockType();
if(shouldPlace(getReplaceable(), block, exposed, random, world, xi, yi, zi)) {
world.setBlockState(xi, yi, zi, getMaterial(block), isApplyGravity());
++blockCount;

View File

@@ -31,7 +31,7 @@ public class VanillaScatteredOre extends VanillaOre {
for(int j = 0; j < i; ++j) {
this.setPos(mutable, random, location, Math.min(j, spread));
BlockType block = world.getBlockState(mutable).getBlockType();
BlockType block = world.getBlockState(mutable).blockType();
if(shouldPlace(getReplaceable(), block, exposed, random, world, mutable.getX(), mutable.getY(), mutable.getZ())) {
world.setBlockState(mutable, getMaterial(block), isApplyGravity());
}

View File

@@ -21,12 +21,12 @@ public class VanillaOreUtils {
if(!replaceable.contains(type)) return false;
if(shouldExpose(random, exposedChance)) return true; // Exposed blocks can be placed regardless of adjacency to air
// Adjacency is checked after determining not exposed rather than vice-versa, assuming block checks are more expensive
boolean adjacentAir = world.getBlockState(x, y, z - 1).isAir() ||
world.getBlockState(x, y, z + 1).isAir() ||
world.getBlockState(x, y - 1, z).isAir() ||
world.getBlockState(x, y + 1, z).isAir() ||
world.getBlockState(x - 1, y, z).isAir() ||
world.getBlockState(x + 1, y, z).isAir();
boolean adjacentAir = world.getBlockState(x, y, z - 1).air() ||
world.getBlockState(x, y, z + 1).air() ||
world.getBlockState(x, y - 1, z).air() ||
world.getBlockState(x, y + 1, z).air() ||
world.getBlockState(x - 1, y, z).air() ||
world.getBlockState(x + 1, y, z).air();
return !adjacentAir; // Exposed check did not pass earlier so only blocks not adjacent air should place
}
}

View File

@@ -66,12 +66,12 @@ public class ManifestAddon implements BaseAddon {
}
@Override
public Map<String, VersionRange> getDependencies() {
public Map<String, VersionRange> dependencies() {
return manifest.getDependencies();
}
@Override
public Version getVersion() {
public Version version() {
return manifest.getVersion();
}
}

View File

@@ -132,7 +132,7 @@ public class ManifestAddonLoader implements BootstrapBaseAddon<ManifestAddon> {
}
@Override
public Version getVersion() {
public Version version() {
return VERSION;
}
}

View File

@@ -4,7 +4,7 @@ contributors:
id: shortcut-blockstate-fallback
version: @VERSION@
entrypoints:
- "com.dfsek.terra.addons.shortcut.blockstate.ShortcutBlockstateFallbackAddon"
- "com.dfsek.terra.addons.shortcut.blockstate.ShortcutBlockStateFallbackAddon"
website:
issues: https://github.com/PolyhedralDev/Terra/issues
source: https://github.com/PolyhedralDev/Terra

View File

@@ -76,7 +76,7 @@ public class BlockFunction implements Function<Void> {
y.apply(implementationArguments, scope).doubleValue(),
FloatingPointFunctions.round(xz.getZ())).mutable().add(arguments.getOrigin().toFloat());
BlockState current = arguments.getWorld().getBlockState(set);
if(overwrite.apply(implementationArguments, scope) || current.isAir()) {
if(overwrite.apply(implementationArguments, scope) || current.air()) {
arguments.getWorld().setBlockState(set, rot, physics.apply(implementationArguments, scope));
}
} catch(RuntimeException e) {

View File

@@ -45,7 +45,7 @@ public class CheckBlockFunction implements Function<String> {
.add(Vector3.of(FloatingPointFunctions.round(xz.getX()),
y.apply(implementationArguments, scope)
.doubleValue(), FloatingPointFunctions.round(xz.getZ()))))
.getAsString();
.asString();
if(data.contains("[")) return data.substring(0, data.indexOf('[')); // Strip properties
else return data;
}

View File

@@ -48,7 +48,7 @@ public class PullFunction implements Function<Void> {
Vector3.Mutable mutable = Vector3.of(FloatingPointFunctions.round(xz.getX()), y.apply(implementationArguments, scope).intValue(),
FloatingPointFunctions.round(xz.getZ())).mutable().add(arguments.getOrigin().toFloat());
while(mutable.getY() > arguments.getWorld().getMinHeight()) {
if(!arguments.getWorld().getBlockState(mutable).isAir()) {
if(!arguments.getWorld().getBlockState(mutable).air()) {
arguments.getWorld().setBlockState(mutable, data);
break;
}

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) {

View File

@@ -274,7 +274,7 @@ public abstract class AbstractPlatform implements Platform {
.append("- ")
.append(addon.getID())
.append("@")
.append(addon.getVersion().getFormatted());
.append(addon.version().getFormatted());
}
logger.info(builder.toString());

View File

@@ -41,13 +41,13 @@ public class DependencySorter {
}
private void sortDependencies(BaseAddon addon, List<BaseAddon> sort) {
addon.getDependencies().forEach((id, range) -> {
addon.dependencies().forEach((id, range) -> {
BaseAddon dependency = get(id, addon);
if(!range.isSatisfiedBy(dependency.getVersion())) {
if(!range.isSatisfiedBy(dependency.version())) {
throw new DependencyVersionException(
"Addon " + addon.getID() + " specifies dependency on " + id + ", versions " + range.getFormatted() +
", but non-matching version " + dependency.getVersion().getFormatted() + " is installed.");
", but non-matching version " + dependency.version().getFormatted() + " is installed.");
}
if(!visited.get(dependency.getID())) { // if we've not visited it yet
@@ -63,14 +63,14 @@ public class DependencySorter {
private BaseAddon get(String id, BaseAddon addon) {
if(!addons.containsKey(id)) {
throw new DependencyException("Addon " + addon.getID() + " specifies dependency on " + id + ", versions " +
addon.getDependencies().get(id).getFormatted() +
addon.dependencies().get(id).getFormatted() +
", but no such addon is installed.");
}
return addons.get(id);
}
private void checkDependencies(BaseAddon base, BaseAddon current) {
current.getDependencies().forEach((id, range) -> {
current.dependencies().forEach((id, range) -> {
BaseAddon dependency = get(id, current);
if(dependency.getID().equals(base.getID())) {
throw new CircularDependencyException(

View File

@@ -15,7 +15,7 @@ public class EphemeralAddon implements BaseAddon {
}
@Override
public Version getVersion() {
public Version version() {
return version;
}

View File

@@ -36,7 +36,7 @@ public class InternalAddon implements BaseAddon {
}
@Override
public Version getVersion() {
public Version version() {
return VERSION;
}
}

View File

@@ -61,7 +61,7 @@ public class GenericLoaders implements LoaderRegistrar {
if(platform != null) {
registry.registerLoader(BaseAddon.class, platform.getAddons())
.registerLoader(BlockType.class, (type, object, configLoader, depthTracker) -> platform
.getWorldHandle().createBlockState((String) object).getBlockType())
.getWorldHandle().createBlockState((String) object).blockType())
.registerLoader(BlockState.class, (type, object, configLoader, depthTracker) -> platform
.getWorldHandle().createBlockState((String) object));
}

View File

@@ -59,7 +59,7 @@ public class FunctionalEventHandlerImpl implements FunctionalEventHandler {
throw e; // Rethrow if it's fail-through.
// else warn
logger.warn("Exception occurred during event handling. Report this to the maintainers of {}@{}",
context.getAddon().getID(), context.getAddon().getVersion().getFormatted(), e);
context.getAddon().getID(), context.getAddon().version().getFormatted(), e);
}
});
}

View File

@@ -63,7 +63,7 @@ public class BootstrapAddonLoader implements BootstrapBaseAddon<BootstrapBaseAdd
}
logger.debug("Loaded bootstrap addon {}@{} with entry point {}",
addon.getID(), addon.getVersion().getFormatted(), addonObject.getClass());
addon.getID(), addon.version().getFormatted(), addonObject.getClass());
return addon;
} catch(InvocationTargetException e) {
throw new AddonLoadException("Exception occurred while instantiating addon", e);
@@ -103,7 +103,7 @@ public class BootstrapAddonLoader implements BootstrapBaseAddon<BootstrapBaseAdd
}
@Override
public Version getVersion() {
public Version version() {
return VERSION;
}
}

View File

@@ -48,17 +48,17 @@ public final class AllayBlockState implements com.dfsek.terra.api.block.state.Bl
}
@Override
public BlockType getBlockType() {
public BlockType blockType() {
return new AllayBlockType(allayBlockState.getBlockType());
}
@Override
public String getAsString(boolean properties) {
public String asString(boolean properties) {
return jeBlockState.toString(properties);
}
@Override
public boolean isAir() {
public boolean air() {
return allayBlockState.getBlockType() == BlockTypes.AIR;
}

View File

@@ -12,17 +12,17 @@ import com.dfsek.terra.api.block.state.BlockState;
*/
public record AllayBlockType(BlockType<?> allayBlockType) implements com.dfsek.terra.api.block.BlockType {
@Override
public BlockState getDefaultState() {
public BlockState defaultState() {
return new AllayBlockState(allayBlockType.getDefaultState(), Mapping.blockStateBeToJe(allayBlockType.getDefaultState()));
}
@Override
public boolean isSolid() {
public boolean solid() {
return allayBlockType.getDefaultState().getBlockStateData().isSolid();
}
@Override
public boolean isWater() {
public boolean water() {
return allayBlockType.hasBlockTag(BlockTags.WATER);
}

View File

@@ -1,5 +1,8 @@
package com.dfsek.terra.allay.handle;
import com.dfsek.terra.api.error.Invalid;
import com.dfsek.terra.api.error.InvalidBlockStateError;
import com.dfsek.terra.api.util.generic.data.types.Either;
import org.jetbrains.annotations.NotNull;
import com.dfsek.terra.allay.JeBlockState;
@@ -16,9 +19,13 @@ import com.dfsek.terra.api.handle.WorldHandle;
public class AllayWorldHandle implements WorldHandle {
@Override
public @NotNull BlockState createBlockState(@NotNull String data) {
JeBlockState jeBlockState = JeBlockState.fromString(data);
return new AllayBlockState(Mapping.blockStateJeToBe(jeBlockState), jeBlockState);
public @NotNull Either<Invalid, BlockState> createBlockState(@NotNull String data) {
try {
JeBlockState jeBlockState = JeBlockState.fromString(data);
return Either.right(new AllayBlockState(Mapping.blockStateJeToBe(jeBlockState), jeBlockState));
} catch(Exception e) {
return new InvalidBlockStateError(e).left();
}
}
@Override

View File

@@ -28,7 +28,7 @@ public class BukkitAddon implements BaseAddon {
}
@Override
public Version getVersion() {
public Version version() {
return VERSION;
}

View File

@@ -26,7 +26,7 @@ public class CloudCommandSender implements CommandSender {
}
@Override
public Maybe<Entity> getEntity() {
public Maybe<Entity> entity() {
if(delegate instanceof org.bukkit.entity.Entity entity) {
return Maybe.just(BukkitAdapter.adapt(entity));
}
@@ -34,7 +34,7 @@ public class CloudCommandSender implements CommandSender {
}
@Override
public Maybe<Player> getPlayer() {
public Maybe<Player> player() {
if(delegate instanceof org.bukkit.entity.Player player) {
return Maybe.just(BukkitAdapter.adapt(player));
}

View File

@@ -17,6 +17,9 @@
package com.dfsek.terra.bukkit.handles;
import com.dfsek.terra.api.error.Invalid;
import com.dfsek.terra.api.error.InvalidBlockStateError;
import com.dfsek.terra.api.util.generic.data.types.Either;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.jetbrains.annotations.NotNull;
@@ -39,10 +42,14 @@ public class BukkitWorldHandle implements WorldHandle {
}
@Override
public synchronized @NotNull BlockState createBlockState(@NotNull String data) {
org.bukkit.block.data.BlockData bukkitData = Bukkit.createBlockData(
data); // somehow bukkit managed to make this not thread safe! :)
return BukkitBlockState.newInstance(bukkitData);
public synchronized @NotNull Either<Invalid, BlockState> createBlockState(@NotNull String data) {
try {
org.bukkit.block.data.BlockData bukkitData = Bukkit.createBlockData(
data); // somehow bukkit managed to make this not thread safe! :)
return Either.right(BukkitBlockState.newInstance(bukkitData));
} catch(Exception e) {
return new InvalidBlockStateError(e).left();
}
}
@Override

View File

@@ -39,17 +39,17 @@ public class BukkitBlockTypeAndItem implements BlockType, Item {
}
@Override
public BlockState getDefaultState() {
public BlockState defaultState() {
return BukkitAdapter.adapt(delegate.createBlockData());
}
@Override
public boolean isSolid() {
public boolean solid() {
return delegate.isOccluding();
}
@Override
public boolean isWater() {
public boolean water() {
return delegate == Material.WATER;
}

View File

@@ -61,17 +61,17 @@ public class BukkitBlockState implements BlockState {
}
@Override
public BlockType getBlockType() {
public BlockType blockType() {
return BukkitAdapter.adapt(delegate.getMaterial());
}
@Override
public String getAsString(boolean properties) {
public String asString(boolean properties) {
return delegate.getAsString(!properties);
}
@Override
public boolean isAir() {
public boolean air() {
return delegate.getMaterial().isAir();
}
}

View File

@@ -98,6 +98,6 @@ public class NMSBiomeInjector {
public static String createBiomeID(ConfigPack pack, com.dfsek.terra.api.registry.key.RegistryKey biomeID) {
return pack.getID()
.toLowerCase() + "/" + biomeID.getNamespace().toLowerCase(Locale.ROOT) + "/" + biomeID.getID().toLowerCase(Locale.ROOT);
.toLowerCase() + "/" + biomeID.namespace().toLowerCase(Locale.ROOT) + "/" + biomeID.getID().toLowerCase(Locale.ROOT);
}
}

View File

@@ -64,17 +64,17 @@ public class CLIBlockState implements BlockState {
}
@Override
public BlockType getBlockType() {
public BlockType blockType() {
return type;
}
@Override
public String getAsString(boolean properties) {
public String asString(boolean properties) {
return value;
}
@Override
public boolean isAir() {
public boolean air() {
return isAir;
}

View File

@@ -25,17 +25,17 @@ public class CLIBlockType implements BlockType {
}
@Override
public BlockState getDefaultState() {
public BlockState defaultState() {
return defaultState.value();
}
@Override
public boolean isSolid() {
public boolean solid() {
return solid;
}
@Override
public boolean isWater() {
public boolean water() {
return water;
}
}

View File

@@ -1,5 +1,7 @@
package com.dfsek.terra.cli.handle;
import com.dfsek.terra.api.error.Invalid;
import com.dfsek.terra.api.util.generic.data.types.Either;
import org.jetbrains.annotations.NotNull;
import com.dfsek.terra.api.block.state.BlockState;
@@ -16,8 +18,8 @@ public class CLIWorldHandle implements WorldHandle {
}
@Override
public @NotNull BlockState createBlockState(@NotNull String data) {
return new CLIBlockState(data);
public @NotNull Either<Invalid, BlockState> createBlockState(@NotNull String data) {
return Either.right(new CLIBlockState(data));
}
@Override

View File

@@ -36,7 +36,7 @@ public class MinestomAddon implements BaseAddon {
}
@Override
public Version getVersion() { return VERSION; }
public Version version() { return VERSION; }
@Override
public String getID() { return "terra-minestom"; }

View File

@@ -114,12 +114,12 @@ public record MinestomBlockState(Block block) implements BlockState {
}
@Override
public BlockType getBlockType() {
public BlockType blockType() {
return new MinestomBlockType(block);
}
@Override
public String getAsString(boolean properties) {
public String asString(boolean properties) {
String name = block.key().asString();
if(!properties || block.properties().isEmpty()) {
return name;
@@ -132,7 +132,7 @@ public record MinestomBlockState(Block block) implements BlockState {
}
@Override
public boolean isAir() {
public boolean air() {
return block.isAir();
}

View File

@@ -14,17 +14,17 @@ public class MinestomBlockType implements BlockType {
}
@Override
public BlockState getDefaultState() {
public BlockState defaultState() {
return new MinestomBlockState(block);
}
@Override
public boolean isSolid() {
public boolean solid() {
return block.isSolid();
}
@Override
public boolean isWater() {
public boolean water() {
return block.isLiquid();
}

View File

@@ -1,5 +1,8 @@
package com.dfsek.terra.minestom.world;
import com.dfsek.terra.api.error.Invalid;
import com.dfsek.terra.api.error.InvalidBlockStateError;
import com.dfsek.terra.api.util.generic.data.types.Either;
import net.minestom.server.instance.block.Block;
import org.jetbrains.annotations.NotNull;
@@ -14,8 +17,12 @@ public class MinestomWorldHandle implements WorldHandle {
private static final MinestomBlockState AIR = new MinestomBlockState(Block.AIR);
@Override
public @NotNull BlockState createBlockState(@NotNull String data) {
return MinestomBlockState.fromStateId(data);
public @NotNull Either<Invalid, BlockState> createBlockState(@NotNull String data) {
try {
return Either.right(MinestomBlockState.fromStateId(data));
} catch(Exception e) {
return new InvalidBlockStateError(e).left();
}
}
@Override

View File

@@ -76,7 +76,7 @@ public abstract class MinecraftAddon implements BaseAddon {
}
@Override
public Version getVersion() {
public Version version() {
return VERSION;
}
}

View File

@@ -18,7 +18,7 @@ public final class Codecs {
public static final Codec<RegistryKey> TERRA_REGISTRY_KEY = RecordCodecBuilder
.create(registryKey -> registryKey.group(Codec.STRING.fieldOf("namespace")
.stable()
.forGetter(RegistryKey::getNamespace),
.forGetter(RegistryKey::namespace),
Codec.STRING.fieldOf("id")
.stable()
.forGetter(RegistryKey::getID))

View File

@@ -138,11 +138,11 @@ public class MinecraftChunkGeneratorWrapper extends net.minecraft.world.gen.chun
com.dfsek.terra.api.block.state.BlockState data = delegate.getPalette(x + xi, y, z + zi, world, biomeProvider).get(
depth, x + xi, y, z + zi, world.getSeed());
BlockPos blockPos = new BlockPos(x, y, z);
boolean isExtended = data.isExtended() && data.getClass().equals(BlockStateArgument.class);
boolean isExtended = data.extended() && data.getClass().equals(BlockStateArgument.class);
if(isExtended) {
BlockStateExtended blockStateExtended = (BlockStateExtended) data;
net.minecraft.block.BlockState blockState = (net.minecraft.block.BlockState) blockStateExtended.getState();
net.minecraft.block.BlockState blockState = (net.minecraft.block.BlockState) blockStateExtended.state();
chunk.setBlockState(blockPos, blockState, 0);
} else {
chunk.setBlockState(blockPos, (net.minecraft.block.BlockState) data, 0);
@@ -196,7 +196,7 @@ public class MinecraftChunkGeneratorWrapper extends net.minecraft.world.gen.chun
for(int y = height.getTopYInclusive() - 1; y >= min; y--) {
com.dfsek.terra.api.block.state.BlockState terraBlockState = delegate.getBlock(properties, x, y, z, biomeProvider);
BlockState blockState =
(BlockState) (terraBlockState.isExtended() ? ((BlockStateExtended) terraBlockState).getState() : terraBlockState);
(BlockState) (terraBlockState.extended() ? ((BlockStateExtended) terraBlockState).state() : terraBlockState);
if(heightmap
.getBlockPredicate()
.test(blockState)) return y + 1;
@@ -212,7 +212,7 @@ public class MinecraftChunkGeneratorWrapper extends net.minecraft.world.gen.chun
for(int y = height.getTopYInclusive() - 1; y >= height.getBottomY(); y--) {
com.dfsek.terra.api.block.state.BlockState terraBlockState = delegate.getBlock(properties, x, y, z, biomeProvider);
BlockState blockState =
(BlockState) (terraBlockState.isExtended() ? ((BlockStateExtended) terraBlockState).getState() : terraBlockState);
(BlockState) (terraBlockState.extended() ? ((BlockStateExtended) terraBlockState).state() : terraBlockState);
array[y - height.getBottomY()] = blockState;
}
return new VerticalBlockSample(height.getBottomY(), array);

View File

@@ -17,6 +17,9 @@
package com.dfsek.terra.mod.handle;
import com.dfsek.terra.api.error.Invalid;
import com.dfsek.terra.api.error.InvalidBlockStateError;
import com.dfsek.terra.api.util.generic.data.types.Either;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.block.BlockEntityProvider;
@@ -51,7 +54,7 @@ public class MinecraftWorldHandle implements WorldHandle {
@SuppressWarnings("DataFlowIssue")
@Override
public @NotNull BlockState createBlockState(@NotNull String data) {
public @NotNull Either<Invalid, BlockState> createBlockState(@NotNull String data) {
try {
BlockResult blockResult = BlockArgumentParser.block(Registries.BLOCK, data, true);
BlockState blockState;
@@ -76,10 +79,10 @@ public class MinecraftWorldHandle implements WorldHandle {
blockState = (BlockState) blockResult.blockState();
}
if(blockState == null) throw new IllegalArgumentException("Invalid data: " + data);
return blockState;
if(blockState == null) return new InvalidBlockStateError(new IllegalArgumentException("Invalid data: " + data)).left();
return Either.right(blockState);
} catch(CommandSyntaxException e) {
throw new IllegalArgumentException(e);
return new InvalidBlockStateError(e).left();
}
}

View File

@@ -29,16 +29,16 @@ import com.dfsek.terra.api.block.BlockType;
@Mixin(Block.class)
@Implements(@Interface(iface = BlockType.class, prefix = "terra$"))
public abstract class BlockMixin {
public com.dfsek.terra.api.block.state.BlockState terra$getDefaultState() {
public com.dfsek.terra.api.block.state.BlockState terra$defaultState() {
return (com.dfsek.terra.api.block.state.BlockState) ((Block) (Object) this).getDefaultState();
}
public boolean terra$isSolid() {
public boolean terra$solid() {
return ((Block) (Object) this).getDefaultState().isOpaque();
}
@SuppressWarnings("ConstantConditions")
public boolean terra$isWater() {
public boolean terra$water() {
return ((Object) this) == Blocks.WATER;
}
}

View File

@@ -68,7 +68,7 @@ public abstract class MobSpawnerBlockEntityMixin extends BlockEntity {
rand = Random.create();
}
net.minecraft.entity.EntityType<?> entityType =
(((net.minecraft.entity.EntityType<?>) (creatureType.isExtended() && creatureType.getClass().equals(
(((net.minecraft.entity.EntityType<?>) (creatureType.extended() && creatureType.getClass().equals(
MinecraftEntityTypeExtended.class) ? ((MinecraftEntityTypeExtended) creatureType).getType() : creatureType)));
setEntityType(entityType, rand);
}

View File

@@ -57,17 +57,17 @@ public abstract class BlockStateArgumentMixin implements Predicate<CachedBlockPo
@Intrinsic
public BlockType terra$getBlockType() {
return ((com.dfsek.terra.api.block.state.BlockState) getBlockState()).getBlockType();
return ((com.dfsek.terra.api.block.state.BlockState) getBlockState()).blockType();
}
@Intrinsic
public String terra$getAsString(boolean properties) {
return ((com.dfsek.terra.api.block.state.BlockState) getBlockState()).getAsString(properties);
return ((com.dfsek.terra.api.block.state.BlockState) getBlockState()).asString(properties);
}
@Intrinsic
public boolean terra$isAir() {
return ((com.dfsek.terra.api.block.state.BlockState) getBlockState()).isAir();
return ((com.dfsek.terra.api.block.state.BlockState) getBlockState()).air();
}
@SuppressWarnings({ "ConstantValue", "DataFlowIssue", "EqualsBetweenInconvertibleTypes" })
@@ -79,12 +79,12 @@ public abstract class BlockStateArgumentMixin implements Predicate<CachedBlockPo
@SuppressWarnings("DataFlowIssue")
@Intrinsic
public ExtendedData terra$getData() {
public ExtendedData terra$data() {
return ((ExtendedData) ((Object) data));
}
@Intrinsic
public com.dfsek.terra.api.block.state.BlockState terra$getState() {
public com.dfsek.terra.api.block.state.BlockState terra$state() {
return (com.dfsek.terra.api.block.state.BlockState) getBlockState();
}

View File

@@ -60,12 +60,12 @@ public abstract class BlockStateMixin extends State<Block, net.minecraft.block.B
}
@Intrinsic
public BlockType terra$getBlockType() {
public BlockType terra$blockType() {
return (BlockType) getBlock();
}
@Intrinsic
public String terra$getAsString(boolean properties) {
public String terra$asString(boolean properties) {
StringBuilder data = new StringBuilder(Registries.BLOCK.getId(getBlock()).toString());
if(properties && !getEntries().isEmpty()) {
data.append('[');
@@ -77,7 +77,7 @@ public abstract class BlockStateMixin extends State<Block, net.minecraft.block.B
}
@Intrinsic
public boolean terra$isAir() {
public boolean terra$air() {
return isAir();
}
}

View File

@@ -83,7 +83,7 @@ public abstract class ChunkRegionMixin implements StructureWorldAccess {
state = arg.getBlockState();
setBlockState(blockPos, state, 0, 512);
net.minecraft.world.chunk.Chunk chunk = getChunk(blockPos);
NbtCompound nbt = ((NbtCompound) (Object) ((BlockStateExtended) data).getData());
NbtCompound nbt = ((NbtCompound) (Object) ((BlockStateExtended) data).data());
MinecraftUtil.loadBlockEntity(chunk, world, blockPos, state, nbt);
} else {
state = (net.minecraft.block.BlockState) data;

View File

@@ -66,7 +66,7 @@ public abstract class WorldChunkMixin {
BlockStateArgument arg = ((BlockStateArgument) data);
state = arg.getBlockState();
setBlockState(blockPos, state, 0);
loadBlockEntity(blockPos, ((NbtCompound) (Object) ((BlockStateExtended) data).getData()));
loadBlockEntity(blockPos, ((NbtCompound) (Object) ((BlockStateExtended) data).data()));
} else {
state = (net.minecraft.block.BlockState) data;
setBlockState(blockPos, state, 0);

View File

@@ -54,11 +54,11 @@ public abstract class ProtoChunkMixin extends Chunk {
public void terra$setBlock(int x, int y, int z, @NotNull BlockState data) {
BlockPos blockPos = new BlockPos(x, y, z);
boolean isExtended = data.isExtended() && data.getClass().equals(BlockStateArgument.class);
boolean isExtended = data.extended() && data.getClass().equals(BlockStateArgument.class);
if(isExtended) {
BlockStateExtended blockStateExtended = (BlockStateExtended) data;
net.minecraft.block.BlockState blockState = (net.minecraft.block.BlockState) blockStateExtended.getState();
net.minecraft.block.BlockState blockState = (net.minecraft.block.BlockState) blockStateExtended.state();
this.setBlockState(blockPos, blockState, 0);
} else {
this.setBlockState(blockPos, (net.minecraft.block.BlockState) data, 0);

View File

@@ -17,6 +17,8 @@
package com.dfsek.terra.mod.mixin.implementations.terra.entity;
import com.dfsek.terra.api.util.generic.data.types.Maybe;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
@@ -27,8 +29,6 @@ import org.spongepowered.asm.mixin.Interface;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import java.util.Optional;
import com.dfsek.terra.api.command.CommandSender;
import com.dfsek.terra.api.entity.Entity;
import com.dfsek.terra.api.entity.Player;
@@ -52,15 +52,15 @@ public abstract class ServerCommandSourceMixin {
}
@Nullable
public Optional<Entity> terra$getEntity() {
return Optional.ofNullable((Entity) getEntity());
public Maybe<Entity> terra$entity() {
return Maybe.ofNullable((Entity) getEntity());
}
public Optional<Player> terra$getPlayer() {
public Maybe<Player> terra$player() {
try {
return Optional.ofNullable((Player) getPlayer());
return Maybe.ofNullable((Player) getPlayer());
} catch(CommandSyntaxException e) {
return Optional.empty();
return Maybe.nothing();
}
}
}

View File

@@ -102,7 +102,7 @@ public abstract class ChunkRegionMixin implements StructureWorldAccess {
state = arg.getBlockState();
setBlockState(blockPos, state, flags);
net.minecraft.world.chunk.Chunk chunk = getChunk(blockPos);
NbtCompound nbt = ((NbtCompound) (Object) ((BlockStateExtended) data).getData());
NbtCompound nbt = ((NbtCompound) (Object) ((BlockStateExtended) data).data());
MinecraftUtil.loadBlockEntity(chunk, world, blockPos, state, nbt);
} else {
state = (net.minecraft.block.BlockState) data;

View File

@@ -101,7 +101,7 @@ public abstract class ServerWorldMixin extends World {
state = arg.getBlockState();
setBlockState(blockPos, state, flags);
net.minecraft.world.chunk.Chunk chunk = getWorldChunk(blockPos);
((WorldChunkAccessor) chunk).invokeLoadBlockEntity(blockPos, ((NbtCompound) (Object) ((BlockStateExtended) data).getData()));
((WorldChunkAccessor) chunk).invokeLoadBlockEntity(blockPos, ((NbtCompound) (Object) ((BlockStateExtended) data).data()));
} else {
state = (net.minecraft.block.BlockState) data;
setBlockState(blockPos, state, flags);

View File

@@ -103,7 +103,7 @@ public class BiomeUtil {
public static String createBiomeID(ConfigPack pack, com.dfsek.terra.api.registry.key.RegistryKey biomeID) {
return pack.getID()
.toLowerCase() + "/" + biomeID.getNamespace().toLowerCase(Locale.ROOT) + "/" + biomeID.getID().toLowerCase(Locale.ROOT);
.toLowerCase() + "/" + biomeID.namespace().toLowerCase(Locale.ROOT) + "/" + biomeID.getID().toLowerCase(Locale.ROOT);
}
public static Map<Identifier, List<Identifier>> getTerraBiomeMap() {

View File

@@ -80,7 +80,7 @@ public final class MinecraftUtil {
}
public static boolean isCompatibleBlockStateExtended(com.dfsek.terra.api.block.state.BlockState blockState) {
return blockState.isExtended() && BlockStateArgument.class.isAssignableFrom(blockState.getClass());
return blockState.extended() && BlockStateArgument.class.isAssignableFrom(blockState.getClass());
}
//[Vanilla Copy]
@@ -103,7 +103,7 @@ public final class MinecraftUtil {
}
public static boolean isCompatibleEntityTypeExtended(EntityType entityType) {
return entityType.isExtended() && MinecraftEntityTypeExtended.class.isAssignableFrom(entityType.getClass());
return entityType.extended() && MinecraftEntityTypeExtended.class.isAssignableFrom(entityType.getClass());
}
public static void registerIntProviderTypes() {

View File

@@ -49,7 +49,7 @@ public class PresetUtil {
Identifier generatorID = Identifier.tryParse(
"terra:" + pack.getID().toLowerCase(Locale.ROOT) + "/" + pack.getNamespace().toLowerCase(
"terra:" + pack.getID().toLowerCase(Locale.ROOT) + "/" + pack.namespace().toLowerCase(
Locale.ROOT));
PRESETS.add(Pair.of(generatorID, extended));
@@ -74,7 +74,7 @@ public class PresetUtil {
platform.multiNoiseBiomeSourceParameterListRegistry();
Identifier generatorID = Identifier.of("terra",
metaPack.getID().toLowerCase(Locale.ROOT) + "/" + metaPack.getNamespace().toLowerCase(
metaPack.getID().toLowerCase(Locale.ROOT) + "/" + metaPack.namespace().toLowerCase(
Locale.ROOT));
PRESETS.add(Pair.of(generatorID, extended));