Move API to one package

This commit is contained in:
dfsek
2021-12-23 13:51:21 -07:00
parent 3d9d0d46fb
commit 0f50b29c4e
155 changed files with 10 additions and 51 deletions

View File

@@ -0,0 +1,20 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api;
/**
* An interface that contains a platform delegate.
*/
public interface Handle {
/**
* Gets the delegate object.
*
* @return Delegate
*/
Object getHandle();
}

View File

@@ -0,0 +1,84 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import com.dfsek.terra.api.addon.BaseAddon;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.config.PluginConfig;
import com.dfsek.terra.api.event.EventManager;
import com.dfsek.terra.api.handle.ItemHandle;
import com.dfsek.terra.api.handle.WorldHandle;
import com.dfsek.terra.api.profiler.Profiler;
import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.registry.Registry;
import com.dfsek.terra.api.tectonic.LoaderRegistrar;
/**
* Represents a Terra mod/plugin instance.
*/
public interface Platform extends LoaderRegistrar {
boolean reload();
@NotNull
@Contract(pure = true)
String platformName();
/**
* Runs a task that may or may not be thread safe, depending on platform.
* <p>
* Allows platforms to define what code is safe to be run asynchronously.
*
* @param task Task to be run.
*/
default void runPossiblyUnsafeTask(@NotNull Runnable task) {
task.run();
}
@NotNull
@Contract(pure = true)
WorldHandle getWorldHandle();
@NotNull
@Contract(pure = true)
PluginConfig getTerraConfig();
@NotNull
@Contract(pure = true)
File getDataFolder();
@NotNull
@Contract(pure = true)
CheckedRegistry<ConfigPack> getConfigRegistry();
@NotNull
@Contract(pure = true)
Registry<BaseAddon> getAddons();
@NotNull
@Contract(pure = true)
ItemHandle getItemHandle();
@NotNull
@Contract(pure = true)
EventManager getEventManager();
@Contract(pure = true)
default @NotNull String getVersion() {
return "@VERSION@";
}
@NotNull
@Contract(pure = true)
Profiler getProfiler();
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.addon;
import ca.solostudios.strata.version.Version;
import ca.solostudios.strata.version.VersionRange;
import java.util.Collections;
import java.util.Map;
import com.dfsek.terra.api.util.StringIdentifiable;
public interface BaseAddon extends StringIdentifiable {
default void initialize() { }
default Map<String, VersionRange> getDependencies() {
return Collections.emptyMap();
}
Version getVersion();
}

View File

@@ -0,0 +1,25 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.addon.bootstrap;
import java.nio.file.Path;
import com.dfsek.terra.api.addon.BaseAddon;
public interface BootstrapBaseAddon<T extends BaseAddon> extends BaseAddon {
/**
* Load all the relevant addons in the specified path.
*
* @param addonsFolder Path containing addons.
* @param parent parent class loader
*
* @return Loaded addons
*/
Iterable<T> loadAddons(Path addonsFolder, ClassLoader parent);
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.block;
import com.dfsek.terra.api.Handle;
import com.dfsek.terra.api.block.state.BlockState;
public interface BlockType extends Handle {
BlockState getDefaultState();
boolean isSolid();
boolean isWater();
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.block.entity;
import com.dfsek.terra.api.Handle;
import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.util.vector.Vector3;
public interface BlockEntity extends Handle {
boolean update(boolean applyPhysics);
default void applyState(String state) {
// Do nothing by default.
}
Vector3 getPosition();
int getX();
int getY();
int getZ();
BlockState getBlockState();
}

View File

@@ -0,0 +1,14 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.block.entity;
import com.dfsek.terra.api.inventory.BlockInventoryHolder;
public interface Container extends BlockEntity, BlockInventoryHolder {
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.block.entity;
import org.jetbrains.annotations.NotNull;
import com.dfsek.terra.api.entity.EntityType;
public interface MobSpawner extends BlockEntity {
EntityType getSpawnedType();
void setSpawnedType(@NotNull EntityType creatureType);
int getDelay();
void setDelay(int delay);
int getMinSpawnDelay();
void setMinSpawnDelay(int delay);
int getMaxSpawnDelay();
void setMaxSpawnDelay(int delay);
int getSpawnCount();
void setSpawnCount(int spawnCount);
int getMaxNearbyEntities();
void setMaxNearbyEntities(int maxNearbyEntities);
int getRequiredPlayerRange();
void setRequiredPlayerRange(int requiredPlayerRange);
int getSpawnRange();
void setSpawnRange(int spawnRange);
}

View File

@@ -0,0 +1,103 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.block.entity;
import java.util.HashMap;
import java.util.Map;
public class SerialState {
protected final Map<String, Property<?>> properties = new HashMap<>();
public SerialState() {
}
public static Map<String, String> parse(String props) {
String[] sep = props.split(",");
Map<String, String> map = new HashMap<>();
for(String item : sep) {
map.put(item.substring(0, item.indexOf('=')), item.substring(item.indexOf('=') + 1));
}
return map;
}
@SuppressWarnings("unchecked")
public <T> T get(String id, Class<T> clazz) {
checkExists(id);
Property<?> prop = properties.get(id);
checkType(clazz, prop.getValue(), id);
return (T) prop.getValue();
}
private void checkExists(String prop) {
if(!properties.containsKey(prop)) throw new IllegalArgumentException("No such property \"" + prop + "\"");
}
private void checkType(Class<?> clazz, Object o, String id) {
if(!clazz.isInstance(o))
throw new IllegalArgumentException("Invalid data for property " + id + ": " + o);
}
public void setProperty(String id, Object value) {
checkExists(id);
Property<?> prop = properties.get(id);
checkType(prop.getValueClass(), value, id);
prop.setValue(value);
}
public int getInteger(String id) {
checkExists(id);
Property<?> prop = properties.get(id);
checkType(Integer.class, prop.getValue(), id);
return (Integer) prop.getValue();
}
public String getString(String id) {
checkExists(id);
Property<?> prop = properties.get(id);
checkType(String.class, prop.getValue(), id);
return (String) prop.getValue();
}
public long getLong(String id) {
checkExists(id);
Property<?> prop = properties.get(id);
checkType(Long.class, prop.getValue(), id);
return (Long) prop.getValue();
}
public boolean getBoolean(String id) {
checkExists(id);
Property<?> prop = properties.get(id);
checkType(Boolean.class, prop.getValue(), id);
return (Boolean) prop.getValue();
}
protected static class Property<T> {
private final Class<T> clazz;
private Object value;
public Property(Class<T> clazz) {
this.clazz = clazz;
}
public Class<T> getValueClass() {
return clazz;
}
@SuppressWarnings("unchecked")
public T getValue() {
return (T) value;
}
public void setValue(Object value) {
this.value = value;
}
}
}

View File

@@ -0,0 +1,19 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.block.entity;
import org.jetbrains.annotations.NotNull;
public interface Sign extends BlockEntity {
void setLine(int index, @NotNull String line) throws IndexOutOfBoundsException;
@NotNull String[] getLines();
@NotNull String getLine(int index) throws IndexOutOfBoundsException;
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.block.state;
import java.util.function.Consumer;
import com.dfsek.terra.api.Handle;
import com.dfsek.terra.api.block.BlockType;
import com.dfsek.terra.api.block.state.properties.Property;
public interface BlockState extends Handle {
boolean matches(BlockState other);
<T extends Comparable<T>> boolean has(Property<T> property);
<T extends Comparable<T>> T get(Property<T> property);
<T extends Comparable<T>> BlockState set(Property<T> property, T value);
default <T extends Comparable<T>> BlockState ifProperty(Property<T> property, Consumer<BlockState> action) {
if(has(property)) action.accept(this);
return this;
}
default <T extends Comparable<T>> BlockState setIfPresent(Property<T> property, T value) {
if(has(property)) set(property, value);
return this;
}
BlockType getBlockType();
default String getAsString() {
return getAsString(true);
}
String getAsString(boolean properties);
boolean isAir();
}

View File

@@ -0,0 +1,19 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.block.state.properties;
import java.util.Collection;
public interface Property<T> {
Collection<T> values();
Class<T> getType();
String getName();
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.block.state.properties.base;
import java.util.Arrays;
import java.util.Collection;
import com.dfsek.terra.api.block.state.properties.Property;
public interface BooleanProperty extends Property<Boolean> {
static BooleanProperty of(String name) {
return new BooleanProperty() {
private static final Collection<Boolean> BOOLEANS = Arrays.asList(true, false);
@Override
public Collection<Boolean> values() {
return BOOLEANS;
}
@Override
public String getName() {
return name;
}
};
}
@Override
default Class<Boolean> getType() {
return Boolean.class;
}
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.block.state.properties.base;
import java.util.Arrays;
import java.util.Collection;
import com.dfsek.terra.api.block.state.properties.Property;
import com.dfsek.terra.api.util.generic.Lazy;
public interface EnumProperty<T extends Enum<T>> extends Property<T> {
static <T extends Enum<T>> EnumProperty<T> of(String name, Class<T> clazz) {
return new EnumProperty<>() {
private final Lazy<Collection<T>> constants = Lazy.lazy(() -> Arrays.asList(clazz.getEnumConstants()));
@Override
public Collection<T> values() {
return constants.value();
}
@Override
public Class<T> getType() {
return clazz;
}
@Override
public String getName() {
return name;
}
};
}
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.block.state.properties.base;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.dfsek.terra.api.block.state.properties.Property;
import com.dfsek.terra.api.util.generic.Construct;
public interface IntProperty extends Property<Integer> {
static IntProperty of(String name, int min, int max) {
return new IntProperty() {
private final Collection<Integer> collection = Construct.construct(() -> {
List<Integer> ints = new ArrayList<>();
for(int i = min; i <= max; i++) {
ints.add(i);
}
return ints;
});
@Override
public Collection<Integer> values() {
return collection;
}
@Override
public String getName() {
return name;
}
};
}
@Override
default Class<Integer> getType() {
return Integer.class;
}
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.block.state.properties.base;
import com.dfsek.terra.api.block.state.properties.enums.Axis;
import com.dfsek.terra.api.block.state.properties.enums.Direction;
import com.dfsek.terra.api.block.state.properties.enums.Half;
import com.dfsek.terra.api.block.state.properties.enums.RailShape;
import com.dfsek.terra.api.block.state.properties.enums.RedstoneConnection;
import com.dfsek.terra.api.block.state.properties.enums.WallHeight;
@Deprecated
public final class Properties {
public static final EnumProperty<Direction> DIRECTION = EnumProperty.of("facing", Direction.class);
public static final EnumProperty<Axis> AXIS = EnumProperty.of("axis", Axis.class);
public static final BooleanProperty NORTH = BooleanProperty.of("north");
public static final BooleanProperty SOUTH = BooleanProperty.of("south");
public static final BooleanProperty EAST = BooleanProperty.of("east");
public static final BooleanProperty WEST = BooleanProperty.of("west");
public static final EnumProperty<WallHeight> NORTH_HEIGHT = EnumProperty.of("north", WallHeight.class);
public static final EnumProperty<WallHeight> SOUTH_HEIGHT = EnumProperty.of("south", WallHeight.class);
public static final EnumProperty<WallHeight> EAST_HEIGHT = EnumProperty.of("east", WallHeight.class);
public static final EnumProperty<WallHeight> WEST_HEIGHT = EnumProperty.of("west", WallHeight.class);
public static final EnumProperty<RedstoneConnection> NORTH_CONNECTION = EnumProperty.of("north", RedstoneConnection.class);
public static final EnumProperty<RedstoneConnection> SOUTH_CONNECTION = EnumProperty.of("south", RedstoneConnection.class);
public static final EnumProperty<RedstoneConnection> EAST_CONNECTION = EnumProperty.of("east", RedstoneConnection.class);
public static final EnumProperty<RedstoneConnection> WEST_CONNECTION = EnumProperty.of("west", RedstoneConnection.class);
public static final EnumProperty<RailShape> RAIL_SHAPE = EnumProperty.of("shape", RailShape.class);
public static final EnumProperty<Half> HALF = EnumProperty.of("half", Half.class);
public static final IntProperty ROTATION = IntProperty.of("rotation", 0, 15);
public static final BooleanProperty WATERLOGGED = BooleanProperty.of("waterlogged");
}

View File

@@ -0,0 +1,14 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.block.state.properties.enums;
public enum Axis {
X,
Y,
Z
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.block.state.properties.enums;
import com.dfsek.terra.api.util.Rotation;
import com.dfsek.terra.api.util.generic.Construct;
public enum Direction {
NORTH(0, 0, 0, -1),
EAST(1, 1, 0, 0),
SOUTH(2, 0, 0, 1),
WEST(3, -1, 0, 0),
UP(-1, 0, 1, 0),
DOWN(-1, 0, -1, 0);
private static final Direction[] rotations = Construct.construct(() -> new Direction[]{ NORTH, SOUTH, EAST, WEST });
private final int rotation;
private final int modX;
private final int modY;
private final int modZ;
Direction(int rotation, int modX, int modY, int modZ) {
this.rotation = rotation;
this.modX = modX;
this.modY = modY;
this.modZ = modZ;
}
public Direction rotate(Rotation rotation) {
return switch(this) {
case UP, DOWN -> this;
default -> rotations[(this.rotation + rotation.getDegrees() / 90) % 4];
};
}
public Direction opposite() {
return switch(this) {
case DOWN -> UP;
case UP -> DOWN;
case EAST -> WEST;
case WEST -> EAST;
case NORTH -> SOUTH;
case SOUTH -> NORTH;
};
}
public int getModX() {
return modX;
}
public int getModY() {
return modY;
}
public int getModZ() {
return modZ;
}
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.block.state.properties.enums;
public enum Half {
/**
* The top half of the block, normally with the higher y coordinate.
*/
TOP,
/**
* The bottom half of the block, normally with the lower y coordinate.
*/
BOTTOM,
/**
* Some blocks, e.g. slabs, can occupy both halves.
*/
DOUBLE
}

View File

@@ -0,0 +1,21 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.block.state.properties.enums;
public enum RailShape {
ASCENDING_EAST,
ASCENDING_NORTH,
ASCENDING_SOUTH,
ASCENDING_WEST,
EAST_WEST,
NORTH_EAST,
NORTH_SOUTH,
NORTH_WEST,
SOUTH_EAST,
SOUTH_WEST
}

View File

@@ -0,0 +1,14 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.block.state.properties.enums;
public enum RedstoneConnection {
NONE,
SIDE,
UP
}

View File

@@ -0,0 +1,14 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.block.state.properties.enums;
public enum WallHeight {
LOW,
NONE,
TALL
}

View File

@@ -0,0 +1,103 @@
package com.dfsek.terra.api.command.arguments;
import cloud.commandframework.ArgumentDescription;
import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.arguments.parser.ArgumentParser;
import cloud.commandframework.context.CommandContext;
import com.dfsek.terra.api.registry.Registry;
import com.dfsek.terra.api.registry.exception.NoSuchEntryException;
import io.leangen.geantyref.TypeToken;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.List;
import java.util.Queue;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
public class RegistryArgument<T, R> extends CommandArgument<T, R> {
@SuppressWarnings("unchecked")
private RegistryArgument(
final boolean required,
final @NonNull String name,
final Registry<R> registry,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<T>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider,
final @NonNull ArgumentDescription description
) {
super(required,
name,
new RegistryArgumentParser<>(registry),
defaultValue,
(TypeToken<R>) TypeToken.get(registry.getType().getType()),
suggestionsProvider,
description);
}
public static <T, R> Builder<T, R> builder(String name, Registry<R> registry) {
return new Builder<>(name, registry);
}
public static <T, R> CommandArgument<T, R> of(String name, Registry<R> registry) {
return RegistryArgument.<T, R>builder(name, registry).build();
}
public static <T, R> CommandArgument<T, R> optional(String name, Registry<R> registry) {
return RegistryArgument.<T, R>builder(name, registry).asOptional().build();
}
public static <T, R> CommandArgument<T, R> optional(String name, Registry<R> registry, String defaultKey) {
return RegistryArgument.<T, R>builder(name, registry).asOptionalWithDefault(defaultKey).build();
}
public static final class Builder<T, R> extends CommandArgument.Builder<T, R> {
private final Registry<R> registry;
@SuppressWarnings("unchecked")
private Builder(@NonNull String name, Registry<R> registry) {
super((TypeToken<R>) TypeToken.get(registry.getType().getType()), name);
this.registry = registry;
}
@Override
public @NonNull RegistryArgument<T, R> build() {
return new RegistryArgument<>(
isRequired(),
getName(),
registry,
getDefaultValue(),
getSuggestionsProvider(),
getDefaultDescription()
);
}
}
private static final class RegistryArgumentParser<T, R> implements ArgumentParser<T, R> {
private final Registry<R> registry;
private RegistryArgumentParser(Registry<R> registry) {
this.registry = registry;
}
@Override
public @NonNull ArgumentParseResult<@NonNull R> parse(@NonNull CommandContext<@NonNull T> commandContext,
@NonNull Queue<@NonNull String> inputQueue) {
String input = inputQueue.remove();
return registry.get(input).map(ArgumentParseResult::success).orElse(ArgumentParseResult.failure(new NoSuchEntryException("No such entry: " + input)));
}
@Override
public @NonNull List<@NonNull String> suggestions(@NonNull CommandContext<T> commandContext, @NonNull String input) {
return registry.keys().stream().sorted().collect(Collectors.toList());
}
}
}

View File

@@ -0,0 +1,16 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.config;
import com.dfsek.tectonic.api.config.template.ConfigTemplate;
import com.dfsek.terra.api.util.StringIdentifiable;
public interface AbstractableTemplate extends ConfigTemplate, StringIdentifiable {
}

View File

@@ -0,0 +1,18 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.config;
import com.dfsek.tectonic.api.config.template.ConfigTemplate;
import com.dfsek.tectonic.api.exception.LoadException;
import com.dfsek.terra.api.Platform;
public interface ConfigFactory<C extends ConfigTemplate, O> {
O build(C config, Platform platform) throws LoadException;
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.config;
import ca.solostudios.strata.version.Version;
import ca.solostudios.strata.version.VersionRange;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import com.dfsek.terra.api.addon.BaseAddon;
import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.registry.meta.RegistryHolder;
import com.dfsek.terra.api.tectonic.ConfigLoadingDelegate;
import com.dfsek.terra.api.tectonic.LoaderRegistrar;
import com.dfsek.terra.api.tectonic.ShortcutLoader;
import com.dfsek.terra.api.util.StringIdentifiable;
import com.dfsek.terra.api.util.reflection.TypeKey;
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
import com.dfsek.terra.api.world.chunk.generation.stage.GenerationStage;
import com.dfsek.terra.api.world.chunk.generation.util.provider.ChunkGeneratorProvider;
public interface ConfigPack extends LoaderRegistrar, ConfigLoadingDelegate, RegistryHolder, StringIdentifiable {
ConfigPack registerConfigType(ConfigType<?, ?> type, String id, int priority);
Map<BaseAddon, VersionRange> addons();
BiomeProvider getBiomeProvider();
default <T> CheckedRegistry<T> getOrCreateRegistry(Class<T> clazz) {
return getOrCreateRegistry(TypeKey.of(clazz));
}
<T> CheckedRegistry<T> getOrCreateRegistry(TypeKey<T> type);
List<GenerationStage> getStages();
Loader getLoader();
String getAuthor();
Version getVersion();
<T> ConfigPack registerShortcut(TypeKey<T> clazz, String shortcut, ShortcutLoader<T> loader);
default <T> ConfigPack registerShortcut(Class<T> clazz, String shortcut, ShortcutLoader<T> loader) {
return registerShortcut(TypeKey.of(clazz), shortcut, loader);
}
ChunkGeneratorProvider getGeneratorProvider();
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.config;
import java.util.function.Supplier;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.registry.OpenRegistry;
import com.dfsek.terra.api.util.reflection.TypeKey;
public interface ConfigType<T extends AbstractableTemplate, R> {
T getTemplate(ConfigPack pack, Platform platform);
ConfigFactory<T, R> getFactory();
TypeKey<R> getTypeKey();
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.config;
import com.dfsek.tectonic.api.exception.ConfigException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
public interface Loader {
Loader thenNames(Consumer<List<String>> consumer) throws ConfigException;
Loader thenEntries(Consumer<Set<Map.Entry<String, InputStream>>> consumer) throws ConfigException;
/**
* Get a single file from this Loader.
*
* @param singleFile File to get
*
* @return InputStream from file.
*/
InputStream get(String singleFile) throws IOException;
/**
* Open a subdirectory.
*
* @param directory Directory to open
* @param extension File extension
*/
Loader open(String directory, String extension);
/**
* Close all InputStreams opened.
*/
Loader close();
}

View File

@@ -0,0 +1,33 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.config;
import com.dfsek.terra.api.Platform;
public interface PluginConfig {
void load(Platform platform);
boolean dumpDefaultConfig();
boolean isDebugCommands();
boolean isDebugProfiler();
boolean isDebugScript();
int getBiomeSearchResolution();
int getStructureCache();
int getSamplerCache();
int getMaxRecursion();
int getProviderCache();
}

View File

@@ -0,0 +1,19 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.config.meta;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
public @interface Meta {
}

View File

@@ -0,0 +1,15 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.entity;
import com.dfsek.terra.api.Handle;
public interface CommandSender extends Handle {
void sendMessage(String message);
}

View File

@@ -0,0 +1,22 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.entity;
import com.dfsek.terra.api.util.vector.Vector3;
import com.dfsek.terra.api.world.ServerWorld;
public interface Entity extends CommandSender {
Vector3 position();
void position(Vector3 position);
void world(ServerWorld world);
ServerWorld world();
}

View File

@@ -0,0 +1,14 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.entity;
import com.dfsek.terra.api.Handle;
public interface EntityType extends Handle {
}

View File

@@ -0,0 +1,11 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.entity;
public interface Player extends Entity {
}

View File

@@ -0,0 +1,15 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.event;
import com.dfsek.terra.api.event.events.Event;
public interface EventHandler {
void handle(Event event);
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.event;
import com.dfsek.terra.api.event.events.Event;
/**
* Manages event registration and triggering.
*/
public interface EventManager {
/**
* Call an event, and return the execution status.
*
* @param event Event to pass to all registered EventListeners.
*/
<T extends Event> T callEvent(T event);
<T extends EventHandler> void registerHandler(Class<T> clazz, T handler);
<T extends EventHandler> T getHandler(Class<T> clazz);
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.event.events;
import com.dfsek.terra.api.util.mutable.MutableBoolean;
/**
* Abstract class containing basic {@link Cancellable} implementation.
*/
public abstract class AbstractCancellable implements Cancellable {
private final MutableBoolean cancelled = new MutableBoolean(false);
@Override
public boolean isCancelled() {
return cancelled.get();
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled.set(cancelled);
}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.event.events;
/**
* Events that implement this interface may be cancelled.
* <p>
* Cancelling an event is assumed to stop the execution of whatever action triggered the event.
*/
public interface Cancellable extends Event {
/**
* Get the cancellation status of the event.
*
* @return Whether event is cancelled.
*/
boolean isCancelled();
/**
* Set the cancellation status of the event.
*
* @param cancelled Whether event is cancelled.
*/
void setCancelled(boolean cancelled);
}

View File

@@ -0,0 +1,14 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.event.events;
/**
* An event that addons may listen to.
*/
public interface Event {
}

View File

@@ -0,0 +1,15 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.event.events;
/**
* An event which (optionally) passes exceptions thrown by listeners to
* the event caller.
*/
public interface FailThroughEvent extends Event {
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.event.events;
import com.dfsek.terra.api.config.ConfigPack;
/**
* An event with functionality directly linked to a {@link ConfigPack}.
* <p>
* PackEvents are only invoked when the pack specifies the addon in its
* {@code addon} key (or when the listener is global).
*/
@SuppressWarnings("InterfaceMayBeAnnotatedFunctional")
public interface PackEvent extends Event {
/**
* Get the {@link ConfigPack} associated with this event.
*
* @return ConfigPack associated with the event.
*/
ConfigPack getPack();
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.event.events.config;
import com.dfsek.tectonic.api.config.Configuration;
import java.util.function.BiConsumer;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.config.Loader;
import com.dfsek.terra.api.event.events.FailThroughEvent;
import com.dfsek.terra.api.event.events.PackEvent;
/**
* Fired when a pack is searched for {@link Configuration}s.
* <p>
* Addons should listen to this event if they wish to add
* another configuration format.
*/
public class ConfigurationDiscoveryEvent implements PackEvent, FailThroughEvent {
private final ConfigPack pack;
private final Loader loader;
private final BiConsumer<String, Configuration> consumer;
public ConfigurationDiscoveryEvent(ConfigPack pack, Loader loader, BiConsumer<String, Configuration> consumer) {
this.pack = pack;
this.loader = loader;
this.consumer = consumer;
}
public void register(String identifier, Configuration config) {
consumer.accept(identifier, config);
}
@Override
public ConfigPack getPack() {
return pack;
}
public Loader getLoader() {
return loader;
}
}

View File

@@ -0,0 +1,80 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.event.events.config;
import com.dfsek.tectonic.api.config.template.ConfigTemplate;
import com.dfsek.tectonic.impl.abstraction.AbstractConfiguration;
import java.util.function.Consumer;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.config.ConfigType;
import com.dfsek.terra.api.event.events.FailThroughEvent;
import com.dfsek.terra.api.event.events.PackEvent;
import com.dfsek.terra.api.util.reflection.ReflectionUtil;
/**
* Fired when each individual configuration is loaded.
* <p>
* Addons should listen to this event if they wish to add
* config values to existing {@link ConfigType}s.
*/
public class ConfigurationLoadEvent implements PackEvent, FailThroughEvent {
private final ConfigPack pack;
private final AbstractConfiguration configuration;
private final Consumer<ConfigTemplate> loader;
private final ConfigType<?, ?> type;
private final Object loaded;
public ConfigurationLoadEvent(ConfigPack pack, AbstractConfiguration configuration, Consumer<ConfigTemplate> loader,
ConfigType<?, ?> type, Object loaded) {
this.pack = pack;
this.configuration = configuration;
this.loader = loader;
this.type = type;
this.loaded = loaded;
}
public <T extends ConfigTemplate> T load(T template) {
loader.accept(template);
return template;
}
public boolean is(Class<?> clazz) {
return clazz.isAssignableFrom(type.getTypeKey().getRawType());
}
@Override
public ConfigPack getPack() {
return pack;
}
public AbstractConfiguration getConfiguration() {
return configuration;
}
public ConfigType<?, ?> getType() {
return type;
}
@SuppressWarnings("unchecked")
public <T> T getLoadedObject(Class<T> clazz) {
if(!clazz.isAssignableFrom(type.getTypeKey().getRawType()))
throw new ClassCastException(
"Cannot assign object from loader of type " + ReflectionUtil.typeToString(type.getTypeKey().getType()) + " to class " +
clazz.getCanonicalName());
return (T) loaded;
}
@SuppressWarnings("unchecked")
public <T> T getLoadedObject() {
return (T) loaded;
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.event.events.config.pack;
import com.dfsek.tectonic.api.config.template.ConfigTemplate;
import com.dfsek.tectonic.api.exception.ConfigException;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.event.events.FailThroughEvent;
import com.dfsek.terra.api.event.events.PackEvent;
/**
* An event related to the loading process of config packs.
*/
public abstract class ConfigPackLoadEvent implements PackEvent, FailThroughEvent {
private final ConfigPack pack;
private final ExceptionalConsumer<ConfigTemplate> configLoader;
public ConfigPackLoadEvent(ConfigPack pack, ExceptionalConsumer<ConfigTemplate> configLoader) {
this.pack = pack;
this.configLoader = configLoader;
}
/**
* Load a custom {@link ConfigTemplate} using the pack manifest.
*
* @param template Template to register.
*/
public <T extends ConfigTemplate> T loadTemplate(T template) throws ConfigException {
configLoader.accept(template);
return template;
}
@Override
public ConfigPack getPack() {
return pack;
}
public interface ExceptionalConsumer<T extends ConfigTemplate> {
void accept(T value) throws ConfigException;
}
}

View File

@@ -0,0 +1,22 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.event.events.config.pack;
import com.dfsek.tectonic.api.config.template.ConfigTemplate;
import com.dfsek.terra.api.config.ConfigPack;
/**
* Called when a config pack has finished loading.
*/
public class ConfigPackPostLoadEvent extends ConfigPackLoadEvent {
public ConfigPackPostLoadEvent(ConfigPack pack, ExceptionalConsumer<ConfigTemplate> loader) {
super(pack, loader);
}
}

View File

@@ -0,0 +1,22 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.event.events.config.pack;
import com.dfsek.tectonic.api.config.template.ConfigTemplate;
import com.dfsek.terra.api.config.ConfigPack;
/**
* Called before a config pack's registries are filled.
*/
public class ConfigPackPreLoadEvent extends ConfigPackLoadEvent {
public ConfigPackPreLoadEvent(ConfigPack pack, ExceptionalConsumer<ConfigTemplate> configLoader) {
super(pack, configLoader);
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.event.events.config.type;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.config.ConfigType;
import com.dfsek.terra.api.event.events.FailThroughEvent;
import com.dfsek.terra.api.event.events.PackEvent;
import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.util.reflection.ReflectionUtil;
public abstract class ConfigTypeLoadEvent implements PackEvent, FailThroughEvent {
private final ConfigType<?, ?> type;
private final CheckedRegistry<?> registry;
private final ConfigPack pack;
public ConfigTypeLoadEvent(ConfigType<?, ?> type, CheckedRegistry<?> registry, ConfigPack pack) {
this.type = type;
this.registry = registry;
this.pack = pack;
}
public boolean is(Class<?> clazz) {
return clazz.isAssignableFrom(type.getTypeKey().getRawType());
}
@Override
public ConfigPack getPack() {
return pack;
}
@SuppressWarnings("unchecked")
public <T> CheckedRegistry<T> getRegistry(Class<T> clazz) {
if(!clazz.isAssignableFrom(type.getTypeKey().getRawType()))
throw new ClassCastException(
"Cannot assign object from loader of type " + ReflectionUtil.typeToString(type.getTypeKey().getType()) + " to class " +
clazz.getCanonicalName());
return (CheckedRegistry<T>) registry;
}
}

View File

@@ -0,0 +1,19 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.event.events.config.type;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.config.ConfigType;
import com.dfsek.terra.api.registry.CheckedRegistry;
public class ConfigTypePostLoadEvent extends ConfigTypeLoadEvent {
public ConfigTypePostLoadEvent(ConfigType<?, ?> type, CheckedRegistry<?> registry, ConfigPack pack) {
super(type, registry, pack);
}
}

View File

@@ -0,0 +1,19 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.event.events.config.type;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.config.ConfigType;
import com.dfsek.terra.api.registry.CheckedRegistry;
public class ConfigTypePreLoadEvent extends ConfigTypeLoadEvent {
public ConfigTypePreLoadEvent(ConfigType<?, ?> type, CheckedRegistry<?> registry, ConfigPack pack) {
super(type, registry, pack);
}
}

View File

@@ -0,0 +1,19 @@
package com.dfsek.terra.api.event.events.platform;
import cloud.commandframework.CommandManager;
import com.dfsek.terra.api.entity.CommandSender;
import com.dfsek.terra.api.event.events.Event;
public class CommandRegistrationEvent implements Event {
private final CommandManager<CommandSender> commandManager;
public CommandRegistrationEvent(CommandManager<CommandSender> commandManager) {
this.commandManager = commandManager;
}
public CommandManager<CommandSender> getCommandManager() {
return commandManager;
}
}

View File

@@ -0,0 +1,17 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.event.events.platform;
import com.dfsek.terra.api.event.events.Event;
/**
* Called when the platform is initialized.
*/
public class PlatformInitializationEvent implements Event {
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.event.events.world.generation;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.entity.Entity;
import com.dfsek.terra.api.event.events.PackEvent;
/**
* Called when an entity is spawned.
*/
public class EntitySpawnEvent implements PackEvent {
private final ConfigPack pack;
private final Entity entity;
public EntitySpawnEvent(ConfigPack pack, Entity entity) {
this.pack = pack;
this.entity = entity;
}
@Override
public ConfigPack getPack() {
return pack;
}
/**
* Get the entity that triggered the event.
*
* @return The entity.
*/
public Entity getEntity() {
return entity;
}
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.event.events.world.generation;
import org.jetbrains.annotations.NotNull;
import com.dfsek.terra.api.block.entity.Container;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.event.events.AbstractCancellable;
import com.dfsek.terra.api.event.events.PackEvent;
import com.dfsek.terra.api.structure.LootTable;
import com.dfsek.terra.api.structure.Structure;
import com.dfsek.terra.api.util.vector.Vector3;
/**
* Called when loot is populated.
*/
public class LootPopulateEvent extends AbstractCancellable implements PackEvent {
private final Container container;
private final ConfigPack pack;
private final Structure structure;
private LootTable table;
public LootPopulateEvent(Container container, LootTable table, ConfigPack pack, Structure structure) {
this.container = container;
this.table = table;
this.pack = pack;
this.structure = structure;
}
@Override
public ConfigPack getPack() {
return pack;
}
public Vector3 getPosition() {
return container.getPosition();
}
/**
* Get the {@link Container} representing the inventory.
*
* @return Inventory recieving loot.
*/
public Container getContainer() {
return container;
}
/**
* Get the loot table to be populated.
*
* @return Loot table.
*/
public LootTable getTable() {
return table;
}
/**
* Set the loot table to be populated.
*
* @param table New loot table.
*/
public void setTable(@NotNull LootTable table) {
this.table = table;
}
/**
* Get the script used to generate the structure.
*
* @return Structure script.
*/
public Structure getStructure() {
return structure;
}
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.event.functional;
import java.util.function.Consumer;
import com.dfsek.terra.api.event.events.Event;
public interface EventContext<T extends Event> {
EventContext<T> then(Consumer<T> action);
EventContext<T> priority(int priority);
EventContext<T> failThrough();
EventContext<T> global();
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.event.functional;
import com.dfsek.terra.api.addon.BaseAddon;
import com.dfsek.terra.api.event.EventHandler;
import com.dfsek.terra.api.event.events.Event;
import com.dfsek.terra.api.util.reflection.TypeKey;
public interface FunctionalEventHandler extends EventHandler {
<T extends Event> EventContext<T> register(BaseAddon addon, Class<T> clazz);
<T extends Event> EventContext<T> register(BaseAddon addon, TypeKey<T> clazz);
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.handle;
import java.util.Set;
import com.dfsek.terra.api.inventory.Item;
import com.dfsek.terra.api.inventory.item.Enchantment;
public interface ItemHandle {
Item createItem(String data);
Enchantment getEnchantment(String id);
Set<Enchantment> getEnchantments();
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.handle;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.entity.EntityType;
/**
* Interface to be implemented for world manipulation.
*/
public interface WorldHandle {
@NotNull
@Contract("_ -> new")
BlockState createBlockState(@NotNull String data);
@NotNull
@Contract(pure = true)
BlockState air();
@NotNull
EntityType getEntity(@NotNull String id);
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.inject;
import com.dfsek.terra.api.inject.annotations.Inject;
import com.dfsek.terra.api.inject.exception.InjectionException;
import com.dfsek.terra.api.inject.impl.InjectorImpl;
/**
* Dynamic dependency injector.
* <p>
* Stores an object to inject, and injects it into objects passed to {@link #inject(Object)}.
*
* @param <T> Type of object to inject.
*/
public interface Injector<T> {
static <T1> Injector<T1> get(T1 value) {
return new InjectorImpl<>(value);
}
/**
* Add an explicit class as a target. Useful for applications where subclasses may cause issues with DI.
*
* @param target Target class type.
*/
void addExplicitTarget(Class<? extends T> target);
/**
* Inject the stored object into an object.
* <p>
* Injects the stored object into any non-static, non-final fields
* annotated with {@link Inject},
* with type matching the stored object or any explicit targets
* ({@link #addExplicitTarget(Class)}.
*
* @param object Object to inject into
*
* @throws InjectionException If:
* <ul>
* <li>Matching field annotated with {@link Inject} is final</li>
* <li>Matching field annotated with {@link Inject} is static</li>
* <li>A reflective access exception occurs</li>
* </ul>
*/
void inject(Object object) throws InjectionException;
}

View File

@@ -0,0 +1,22 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.inject.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Specifies that a field is a target for dependency injection.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Inject {
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.inject.exception;
import java.io.Serial;
import com.dfsek.terra.api.inject.Injector;
/**
* Thrown when dynamic dependency injection cannot be completed by an {@link Injector}.
*/
public class InjectionException extends RuntimeException {
@Serial
private static final long serialVersionUID = -6929631447064215387L;
public InjectionException(String message) {
super(message);
}
public InjectionException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.inject.impl;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashSet;
import java.util.Set;
import com.dfsek.terra.api.inject.Injector;
import com.dfsek.terra.api.inject.annotations.Inject;
import com.dfsek.terra.api.inject.exception.InjectionException;
import com.dfsek.terra.api.util.reflection.ReflectionUtil;
public class InjectorImpl<T> implements Injector<T> {
private final T value;
private final Set<Class<? extends T>> targets = new HashSet<>();
/**
* Instantiate an Injector with a value to inject
*
* @param value Value to inject
*/
public InjectorImpl(T value) {
this.value = value;
}
@Override
public void addExplicitTarget(Class<? extends T> target) {
targets.add(target);
}
@Override
public void inject(Object object) throws InjectionException {
for(Field field : ReflectionUtil.getFields(object.getClass())) {
Inject inject = field.getAnnotation(Inject.class);
if(inject == null) continue;
if(value.getClass().equals(field.getType()) || targets.contains(field.getType())) {
int mod = field.getModifiers();
if(Modifier.isFinal(mod)) {
throw new InjectionException("Attempted to inject final field: " + field);
}
if(Modifier.isStatic(mod)) {
throw new InjectionException("Attempted to inject static field: " + field);
}
field.setAccessible(true);
try {
field.set(object, value);
} catch(IllegalAccessException e) {
throw new InjectionException("Failed to inject field: " + field, e);
}
}
}
}
}

View File

@@ -0,0 +1,15 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.inventory;
import com.dfsek.terra.api.util.vector.Vector3;
public interface BlockInventoryHolder extends InventoryHolder {
Vector3 getPosition();
}

View File

@@ -0,0 +1,19 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.inventory;
import com.dfsek.terra.api.Handle;
public interface Inventory extends Handle {
void setItem(int slot, ItemStack newStack);
int getSize();
ItemStack getItem(int slot);
}

View File

@@ -0,0 +1,15 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.inventory;
import com.dfsek.terra.api.Handle;
public interface InventoryHolder extends Handle {
Inventory getInventory();
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.inventory;
import com.dfsek.terra.api.Handle;
/**
* An inventory item.
*/
public interface Item extends Handle {
ItemStack newItemStack(int amount);
double getMaxDurability();
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.inventory;
import com.dfsek.terra.api.Handle;
import com.dfsek.terra.api.inventory.item.Damageable;
import com.dfsek.terra.api.inventory.item.ItemMeta;
public interface ItemStack extends Handle {
int getAmount();
void setAmount(int i);
Item getType();
ItemMeta getItemMeta();
void setItemMeta(ItemMeta meta);
default boolean isDamageable() {
return getItemMeta() instanceof Damageable;
}
}

View File

@@ -0,0 +1,19 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.inventory.item;
import com.dfsek.terra.api.Handle;
public interface Damageable extends Handle {
int getDamage();
void setDamage(int damage);
boolean hasDamage();
}

View File

@@ -0,0 +1,22 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.inventory.item;
import com.dfsek.terra.api.Handle;
import com.dfsek.terra.api.inventory.ItemStack;
public interface Enchantment extends Handle {
boolean canEnchantItem(ItemStack itemStack);
boolean conflictsWith(Enchantment other);
String getID();
int getMaxLevel();
}

View File

@@ -0,0 +1,19 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.inventory.item;
import java.util.Map;
import com.dfsek.terra.api.Handle;
public interface ItemMeta extends Handle {
void addEnchantment(Enchantment enchantment, int level);
Map<Enchantment, Integer> getEnchantments();
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.noise;
import com.dfsek.terra.api.util.vector.Vector2;
import com.dfsek.terra.api.util.vector.Vector3;
import com.dfsek.terra.api.util.vector.Vector2Int;
import com.dfsek.terra.api.util.vector.Vector3Int;
public interface NoiseSampler {
static NoiseSampler zero() {
return new NoiseSampler() {
@Override
public double noise(long seed, double x, double y) {
return 0;
}
@Override
public double noise(long seed, double x, double y, double z) {
return 0;
}
};
}
default double noise(Vector3 vector3, long seed) {
return noise(seed, vector3.getX(), vector3.getY(), vector3.getZ());
}
default double noise(Vector3Int vector3, long seed) {
return noise(seed, vector3.getX(), vector3.getY(), vector3.getZ());
}
default double noise(Vector2 vector2, long seed) {
return noise(seed, vector2.getX(), vector2.getZ());
}
default double noise(Vector2Int vector2, long seed) {
return noise(seed, vector2.getX(), vector2.getZ());
}
double noise(long seed, double x, double y);
double noise(long seed, double x, double y, double z);
}

View File

@@ -0,0 +1,21 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.profiler;
public class ProfileFrame implements AutoCloseable {
private final Runnable action;
public ProfileFrame(Runnable action) {
this.action = action;
}
@Override
public void close() {
action.run();
}
}

View File

@@ -0,0 +1,65 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.profiler;
import java.util.Map;
public interface Profiler {
/**
* Push a frame to this profiler.
*
* @param frame ID of frame.
*/
void push(String frame);
/**
* Pop a frame from this profiler.
*
* @param frame ID of frame. Must match ID
* at the top of the profiler stack.
*/
void pop(String frame);
/**
* Start profiling.
*/
void start();
/**
* Stop profiling.
*/
void stop();
/**
* Return a {@link AutoCloseable} implementation that
* may be used in a try-with-resources statement for
* more intuitive profiling, with auto-push/pop.
*
* @param frame ID of frame.
*
* @return {@link AutoCloseable} implementation for use
* in try-with-resources.
*/
default ProfileFrame profile(String frame) {
push(frame);
return new ProfileFrame(() -> pop(frame));
}
/**
* Clear the profiler data.
*/
void reset();
/**
* Get the profiler data.
*
* @return Profiler data.
*/
Map<String, Timings> getTimings();
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.profiler;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Timings {
private final Map<String, Timings> subItems = new HashMap<>();
private final List<Long> timings = new ArrayList<>();
public void addTime(long time) {
timings.add(time);
}
public double average() {
return (double) timings.stream().reduce(0L, Long::sum) / timings.size();
}
public long max() {
return timings.stream().mapToLong(Long::longValue).max().orElse(0L);
}
public long min() {
return timings.stream().mapToLong(Long::longValue).min().orElse(0L);
}
public double sum() {
return timings.stream().mapToDouble(Long::doubleValue).sum();
}
@Override
public String toString() {
return toString(1, this, Collections.emptySet());
}
private String toString(int indent, Timings parent, Set<Integer> branches) {
StringBuilder builder = new StringBuilder();
builder.append((double) min() / 1000000).append("ms min / ").append(average() / 1000000).append("ms avg / ")
.append((double) max() / 1000000).append("ms max (").append(timings.size()).append(" samples, ")
.append((sum() / parent.sum()) * 100).append("% of parent)");
List<String> frames = new ArrayList<>();
Set<Integer> newBranches = new HashSet<>(branches);
newBranches.add(indent);
subItems.forEach((id, timings) -> frames.add(id + ": " + timings.toString(indent + 1, this, newBranches)));
for(int i = 0; i < frames.size(); i++) {
builder.append('\n');
for(int j = 0; j < indent; j++) {
if(branches.contains(j)) builder.append("");
else builder.append(" ");
}
if(i == frames.size() - 1 && !frames.get(i).contains("\n")) builder.append("└───");
else builder.append("├───");
builder.append(frames.get(i));
}
return builder.toString();
}
public List<Long> getTimings() {
return timings;
}
public Timings getSubItem(String id) {
return subItems.computeIfAbsent(id, s -> new Timings());
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.properties;
import java.util.HashMap;
import java.util.Map;
public class Context {
private final Map<Class<? extends Properties>, Properties> map = new HashMap<>();
@SuppressWarnings("unchecked")
public <T extends Properties> T get(Class<T> clazz) {
return (T) map.computeIfAbsent(clazz, k -> {
throw new IllegalArgumentException("No properties registered for class " + clazz.getCanonicalName());
});
}
public Context put(Properties properties) {
if(map.containsKey(properties.getClass())) throw new IllegalArgumentException(
"Property for class " + properties.getClass().getCanonicalName() + " already registered.");
map.put(properties.getClass(), properties);
return this;
}
}

View File

@@ -0,0 +1,11 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.properties;
public interface Properties {
}

View File

@@ -0,0 +1,12 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.properties;
public interface PropertyHolder {
Context getContext();
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.properties.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import com.dfsek.terra.api.properties.PropertyHolder;
/**
* Specifies that this property holder shares properties
* with the {@link #value()} holder.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
public @interface Linked {
Class<? extends PropertyHolder> value();
}

View File

@@ -0,0 +1,25 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.registry;
import org.jetbrains.annotations.NotNull;
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
public interface CheckedRegistry<T> extends Registry<T> {
/**
* Add a value to this registry, checking whether it is present first.
*
* @param identifier Identifier to assign value.
* @param value Value to register.
*
* @throws DuplicateEntryException If an entry with the same identifier is already present.
*/
void register(@NotNull String identifier, @NotNull T value) throws DuplicateEntryException;
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.registry;
import org.jetbrains.annotations.NotNull;
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
public interface OpenRegistry<T> extends Registry<T> {
/**
* Add a value to this registry.
*
* @param identifier Identifier to assign value.
* @param value Value to register.
*/
boolean register(@NotNull String identifier, @NotNull T value);
/**
* Add a value to this registry, checking whether it is present first.
*
* @param identifier Identifier to assign value.
* @param value Value to register.
*
* @throws DuplicateEntryException If an entry with the same identifier is already present.
*/
void registerChecked(@NotNull String identifier, @NotNull T value) throws DuplicateEntryException;
/**
* Clears all entries from the registry.
*/
void clear();
}

View File

@@ -0,0 +1,79 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.registry;
import com.dfsek.tectonic.api.loader.type.TypeLoader;
import com.dfsek.terra.api.util.reflection.TypeKey;
import com.google.common.reflect.TypeToken;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
public interface Registry<T> extends TypeLoader<T> {
/**
* Get a value from the registry.
*
* @param identifier Identifier of value.
*
* @return Value matching the identifier, {@code null} if no value is present.
*/
@Contract(pure = true)
Optional<T> get(@NotNull String identifier);
/**
* Check if the registry contains a value.
*
* @param identifier Identifier of value.
*
* @return Whether the registry contains the value.
*/
@Contract(pure = true)
boolean contains(@NotNull String identifier);
/**
* Perform the given action for every value in the registry.
*
* @param consumer Action to perform on value.
*/
void forEach(@NotNull Consumer<T> consumer);
/**
* Perform an action for every key-value pair in the registry.
*
* @param consumer Action to perform on pair.
*/
void forEach(@NotNull BiConsumer<String, T> consumer);
/**
* Get the entries of this registry as a {@link Set}.
*
* @return Set containing all entries.
*/
@NotNull
@Contract(pure = true)
Collection<T> entries();
/**
* Get all the keys in this registry.
*
* @return Keys in registry
*/
@NotNull
@Contract(pure = true)
Set<String> keys();
TypeKey<T> getType();
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.registry.exception;
import java.io.Serial;
/**
* Thrown when a duplicate entry is found in a registry.
*/
public class DuplicateEntryException extends RuntimeException {
@Serial
private static final long serialVersionUID = -7199021672428288780L;
public DuplicateEntryException(String message) {
super(message);
}
public DuplicateEntryException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -0,0 +1,8 @@
package com.dfsek.terra.api.registry.exception;
public class NoSuchEntryException extends RuntimeException {
public NoSuchEntryException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.registry.meta;
import java.lang.reflect.Type;
import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.registry.Registry;
import com.dfsek.terra.api.util.reflection.TypeKey;
public interface RegistryHolder {
default <T> Registry<T> getRegistry(Class<T> clazz) {
return getRegistry((Type) clazz);
}
default <T> Registry<T> getRegistry(TypeKey<T> type) {
return getRegistry(type.getType());
}
<T> Registry<T> getRegistry(Type type);
default <T> CheckedRegistry<T> getCheckedRegistry(Class<T> clazz) throws IllegalStateException {
return getCheckedRegistry((Type) clazz);
}
default <T> CheckedRegistry<T> getCheckedRegistry(TypeKey<T> type) throws IllegalStateException {
return getCheckedRegistry(type.getType());
}
default <T> CheckedRegistry<T> getCheckedRegistry(Type type) throws IllegalStateException {
throw new UnsupportedOperationException("Cannot get checked registry.");
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.structure;
import org.jetbrains.annotations.ApiStatus.Experimental;
import java.util.List;
import java.util.Random;
import com.dfsek.terra.api.inventory.Inventory;
import com.dfsek.terra.api.inventory.ItemStack;
@Experimental
public interface LootTable {
/**
* Fills an Inventory with loot.
*
* @param i The Inventory to fill.
* @param r The The Random instance to use.
*/
void fillInventory(Inventory i, Random r);
/**
* Fetches a list of ItemStacks from the loot table using the given Random instance.
*
* @param r The Random instance to use.
*
* @return List&lt;ItemStack&gt; - The list of loot fetched.
*/
List<ItemStack> getLoot(Random r);
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.structure;
import java.util.Random;
import com.dfsek.terra.api.util.Rotation;
import com.dfsek.terra.api.util.StringIdentifiable;
import com.dfsek.terra.api.util.vector.Vector3Int;
import com.dfsek.terra.api.world.WritableWorld;
public interface Structure extends StringIdentifiable {
boolean generate(Vector3Int location, WritableWorld world, Random random, Rotation rotation);
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.structure;
import org.jetbrains.annotations.ApiStatus.Experimental;
import com.dfsek.terra.api.util.vector.Vector3;
@Experimental
public interface StructureSpawn {
/**
* Get nearest spawn point
*
* @param x X coordinate
* @param z Z coordinate
* @param seed Seed for RNG
*
* @return Vector representing nearest spawnpoint
*/
Vector3 getNearestSpawn(int x, int z, long seed);
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.structure.configured;
import org.jetbrains.annotations.ApiStatus.Experimental;
import com.dfsek.terra.api.structure.Structure;
import com.dfsek.terra.api.structure.StructureSpawn;
import com.dfsek.terra.api.util.Range;
import com.dfsek.terra.api.util.StringIdentifiable;
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
@Experimental
public interface ConfiguredStructure extends StringIdentifiable {
ProbabilityCollection<Structure> getStructure();
Range getSpawnStart();
StructureSpawn getSpawn();
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.structure.feature;
import java.util.function.IntConsumer;
public class BinaryColumn {
private final boolean[] data;
private final int minY;
public BinaryColumn(int minY, int maxY) {
this.minY = minY;
if(maxY <= minY) throw new IllegalArgumentException("Max y must be greater than min y");
this.data = new boolean[maxY - minY];
}
public void set(int y) {
data[y - minY] = true;
}
public boolean get(int y) {
return data[y - minY];
}
public void forEach(IntConsumer consumer) {
for(int y = 0; y < data.length; y++) {
if(data[y]) {
consumer.accept(y + minY);
}
}
}
public BinaryColumn and(BinaryColumn that) {
if(that.minY != this.minY) throw new IllegalArgumentException("Must share same min Y");
if(that.data.length != this.data.length) throw new IllegalArgumentException("Must share same max Y");
BinaryColumn next = new BinaryColumn(minY, data.length - minY);
for(int i = 0; i < this.data.length; i++) {
next.data[i] = this.data[i] && that.data[i];
}
return next;
}
public BinaryColumn or(BinaryColumn that) {
if(that.minY != this.minY) throw new IllegalArgumentException("Must share same min Y");
if(that.data.length != this.data.length) throw new IllegalArgumentException("Must share same max Y");
BinaryColumn next = new BinaryColumn(minY, data.length - minY);
for(int i = 0; i < this.data.length; i++) {
next.data[i] = this.data[i] || that.data[i];
}
return next;
}
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.structure.feature;
public interface Distributor {
static Distributor yes() {
return (x, z, seed) -> true;
}
static Distributor no() {
return (x, z, seed) -> false;
}
boolean matches(int x, int z, long seed);
default Distributor and(Distributor other) {
return (x, z, seed) -> this.matches(x, z, seed) && other.matches(x, z, seed);
}
default Distributor or(Distributor other) {
return (x, z, seed) -> this.matches(x, z, seed) || other.matches(x, z, seed);
}
}

View File

@@ -0,0 +1,21 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.structure.feature;
import com.dfsek.terra.api.structure.Structure;
import com.dfsek.terra.api.util.StringIdentifiable;
import com.dfsek.terra.api.world.WritableWorld;
public interface Feature extends StringIdentifiable {
Structure getStructure(WritableWorld world, int x, int y, int z);
Distributor getDistributor();
Locator getLocator();
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.structure.feature;
import com.dfsek.terra.api.world.chunk.generation.util.Column;
public interface Locator {
default Locator and(Locator that) {
return column -> this.getSuitableCoordinates(column).and(that.getSuitableCoordinates(column));
}
default Locator or(Locator that) {
return column -> this.getSuitableCoordinates(column).or(that.getSuitableCoordinates(column));
}
BinaryColumn getSuitableCoordinates(Column<?> column);
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.tectonic;
import com.dfsek.tectonic.api.config.template.object.ObjectTemplate;
import com.dfsek.tectonic.api.loader.type.TypeLoader;
import java.lang.reflect.Type;
import java.util.function.Supplier;
public interface ConfigLoadingDelegate {
<T> ConfigLoadingDelegate applyLoader(Type type, TypeLoader<T> loader);
default <T> ConfigLoadingDelegate applyLoader(Class<? extends T> type, TypeLoader<T> loader) {
return applyLoader((Type) type, loader);
}
<T> ConfigLoadingDelegate applyLoader(Type type, Supplier<ObjectTemplate<T>> loader);
default <T> ConfigLoadingDelegate applyLoader(Class<? extends T> type, Supplier<ObjectTemplate<T>> loader) {
return applyLoader((Type) type, loader);
}
}

View File

@@ -0,0 +1,15 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.tectonic;
import com.dfsek.tectonic.api.TypeRegistry;
public interface LoaderRegistrar {
void register(TypeRegistry registry);
}

View File

@@ -0,0 +1,8 @@
package com.dfsek.terra.api.tectonic;
import com.dfsek.tectonic.api.loader.ConfigLoader;
public interface ShortcutLoader<T> {
T load(ConfigLoader configLoader, String input);
}

View File

@@ -0,0 +1,19 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.transform;
import com.dfsek.terra.api.transform.exception.TransformException;
/**
* Interface to transform data from one type to another.
*/
@FunctionalInterface
public interface Transform<F, T> {
T transform(F input) throws TransformException;
}

View File

@@ -0,0 +1,19 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.transform;
public interface Transformer<F, T> {
/**
* Translate data from {@code from} type to {@code to} type.
*
* @param from Data to translate
*
* @return Result
*/
T translate(F from);
}

View File

@@ -0,0 +1,22 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.transform;
import java.util.Objects;
import com.dfsek.terra.api.transform.exception.TransformException;
public interface Validator<T> {
static <T> Validator<T> notNull() {
return Objects::nonNull;
}
boolean validate(T value) throws TransformException;
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.transform.exception;
import java.io.Serial;
import java.util.ArrayList;
import java.util.List;
public class AttemptsFailedException extends RuntimeException {
@Serial
private static final long serialVersionUID = -1160459550006067137L;
private final List<Throwable> causes;
public AttemptsFailedException(String message, List<Throwable> causes) {
super(message);
this.causes = causes;
}
public List<Throwable> getCauses() {
return new ArrayList<>(causes);
}
}

View File

@@ -0,0 +1,32 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.transform.exception;
import java.io.Serial;
public class TransformException extends Exception {
@Serial
private static final long serialVersionUID = -6661338369581162084L;
public TransformException() {
super();
}
public TransformException(String message) {
super(message);
}
public TransformException(String message, Throwable cause) {
super(message, cause);
}
public TransformException(Throwable cause) {
super(cause);
}
}

View File

@@ -0,0 +1,144 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.util;
import net.jafama.FastMath;
import org.jetbrains.annotations.NotNull;
import java.util.Iterator;
import java.util.Random;
public class ConstantRange implements Range {
private int min;
private int max;
public ConstantRange(int min, int max) {
if(min > max) throw new IllegalArgumentException("Minimum must not be grater than maximum!");
this.max = max;
this.min = min;
}
@Override
public Range multiply(int mult) {
min *= mult;
max *= mult;
return this;
}
@Override
public Range reflect(int pt) {
return new ConstantRange(2 * pt - this.getMax(), 2 * pt - this.getMin());
}
@Override
public int get(Random r) {
return r.nextInt((max - min) + 1) + min;
}
@Override
public Range intersects(Range other) {
try {
return new ConstantRange(FastMath.max(this.getMin(), other.getMin()), FastMath.min(this.getMax(), other.getMax()));
} catch(IllegalArgumentException e) {
return null;
}
}
@Override
public Range add(int add) {
this.min += add;
this.max += add;
return this;
}
@Override
public Range sub(int sub) {
this.min -= sub;
this.max -= sub;
return this;
}
@NotNull
@Override
public Iterator<Integer> iterator() {
return new RangeIterator(this);
}
@Override
public boolean isInRange(int test) {
return test >= min && test < max;
}
@Override
public int getMax() {
return max;
}
@Override
public Range setMax(int max) {
this.max = max;
return this;
}
@Override
public int getMin() {
return min;
}
@Override
public Range setMin(int min) {
this.min = min;
return this;
}
@Override
public int getRange() {
return max - min;
}
@Override
public int hashCode() {
return min * 31 + max;
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof ConstantRange)) return false;
Range other = (Range) obj;
return other.getMin() == this.getMin() && other.getMax() == this.getMax();
}
@Override
public String toString() {
return "Min: " + getMin() + ", Max:" + getMax();
}
private static class RangeIterator implements Iterator<Integer> {
private final Range m;
private Integer current;
public RangeIterator(Range m) {
this.m = m;
current = m.getMin();
}
@Override
public boolean hasNext() {
return current < m.getMax();
}
@Override
public Integer next() {
current++;
return current - 1;
}
}
}

Some files were not shown because too many files have changed in this diff Show More