mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-11-27 16:35:31 +00:00
feat: add environment attribute system
This commit is contained in:
1 parent
8a940f7e1f
commit
4e37c361ea
127 files changed
+9022
-19
No files matched your search
@@ -23,6 +23,8 @@ 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;
|
||||
import com.dfsek.terra.api.world.attribute.AttributeHandle;
|
||||
import com.dfsek.terra.api.world.attribute.AttributeParents;
|
||||
|
||||
|
||||
/**
|
||||
@@ -89,4 +91,17 @@ public interface Platform extends LoaderRegistrar {
|
||||
|
||||
@Contract(pure = true)
|
||||
int getGenerationThreads();
|
||||
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
AttributeHandle getAttributeHandle();
|
||||
|
||||
/**
|
||||
* Each pack reads these once, when it loads.
|
||||
*/
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
default AttributeParents getAttributeParents() {
|
||||
return AttributeParents.NONE;
|
||||
}
|
||||
}
|
||||
@@ -52,6 +52,14 @@ public class Context {
|
||||
return (T) list[key.key];
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlike {@link #get(PropertyKey)}, safe for keys created after this context last stored a value.
|
||||
*/
|
||||
public boolean has(PropertyKey<?> key) {
|
||||
Properties[] current = list;
|
||||
return key.key < current.length && current[key.key] != null;
|
||||
}
|
||||
|
||||
public <T extends Properties> boolean has(Class<T> test) {
|
||||
return map.containsKey(test);
|
||||
}
|
||||
|
||||
@@ -1,29 +1,31 @@
|
||||
package com.dfsek.terra.api.registry.key;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
||||
public final class RegistryKey implements StringIdentifiable, Namespaced {
|
||||
private static final Pattern ID_PATTERN = Pattern.compile("^[a-zA-Z0-9_-]*$");
|
||||
private static final Pattern NAMESPACE_PATTERN = Pattern.compile("^[a-zA-Z0-9_.-]*$");
|
||||
private static final Pattern ID_PATTERN = Pattern.compile("^[a-zA-Z0-9_./-]*$");
|
||||
private final String namespace;
|
||||
private final String id;
|
||||
private final int hash;
|
||||
|
||||
private RegistryKey(String namespace, String id) {
|
||||
if(!ID_PATTERN.matcher(namespace).matches()) {
|
||||
if(!NAMESPACE_PATTERN.matcher(namespace).matches()) {
|
||||
throw new IllegalArgumentException(
|
||||
"Namespace must only contain alphanumeric characters, hyphens, and underscores. \"" + namespace +
|
||||
"Namespace must only contain alphanumeric characters, hyphens, underscores, and periods. \"" + namespace +
|
||||
"\" is not a valid namespace.");
|
||||
}
|
||||
|
||||
if(!ID_PATTERN.matcher(id).matches()) {
|
||||
throw new IllegalArgumentException(
|
||||
"ID must only contain alphanumeric characters, hyphens, and underscores. \"" + id +
|
||||
"ID must only contain alphanumeric characters, hyphens, underscores, periods, and slashes. \"" + id +
|
||||
"\" is not a valid ID.");
|
||||
}
|
||||
|
||||
this.namespace = namespace;
|
||||
this.id = id;
|
||||
this.hash = 31 * (31 + namespace.hashCode()) + id.hashCode();
|
||||
}
|
||||
|
||||
public static RegistryKey parse(String key) {
|
||||
@@ -36,6 +38,37 @@ public final class RegistryKey implements StringIdentifiable, Namespaced {
|
||||
return new RegistryKey(namespace, id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses {@code namespace:id}, or {@code id} in {@code defaultNamespace}.
|
||||
*
|
||||
* @throws IllegalArgumentException if a part is empty or invalid, or the key holds more than one {@code :}
|
||||
*/
|
||||
public static RegistryKey parse(String key, String defaultNamespace) {
|
||||
// Shown to users in config warnings: no Java type names.
|
||||
if(key == null) throw new IllegalArgumentException("malformed ID: none given");
|
||||
int colon = key.indexOf(':');
|
||||
if(colon != key.lastIndexOf(':')) throw new IllegalArgumentException(malformed(key, "more than one ':'"));
|
||||
String namespace = colon < 0 ? defaultNamespace : key.substring(0, colon);
|
||||
String id = key.substring(colon + 1);
|
||||
if(namespace == null || namespace.isEmpty()) throw new IllegalArgumentException(malformed(key, "empty namespace"));
|
||||
if(id.isEmpty()) throw new IllegalArgumentException(malformed(key, "empty ID"));
|
||||
return new RegistryKey(namespace, id);
|
||||
}
|
||||
|
||||
private static String malformed(String key, String reason) {
|
||||
return "malformed ID \"" + key + "\": " + reason;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IllegalArgumentException if the key has no namespace or is otherwise invalid
|
||||
*/
|
||||
public static RegistryKey parseNamespaced(String key) {
|
||||
if(key != null && key.indexOf(':') < 0) {
|
||||
throw new IllegalArgumentException("a namespace is required, as in \"namespace:" + key + "\"");
|
||||
}
|
||||
return parse(key, "");
|
||||
}
|
||||
|
||||
public static RegistryKey of(String namespace, String id) {
|
||||
return new RegistryKey(namespace, id);
|
||||
}
|
||||
@@ -52,13 +85,14 @@ public final class RegistryKey implements StringIdentifiable, Namespaced {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(namespace, id);
|
||||
return hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if(this == obj) return true;
|
||||
if(obj instanceof RegistryKey that) {
|
||||
return this.id.equals(that.id) && this.namespace.equals(that.namespace);
|
||||
return this.hash == that.hash && this.id.equals(that.id) && this.namespace.equals(that.namespace);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.dfsek.terra.api.registry.key;
|
||||
|
||||
public final class TagKey {
|
||||
private final RegistryKey key;
|
||||
|
||||
private TagKey(RegistryKey key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public static TagKey of(RegistryKey key) {
|
||||
return new TagKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IllegalArgumentException if the string does not start with {@code #}, has no namespace, or is not a valid
|
||||
* {@link RegistryKey}
|
||||
*/
|
||||
public static TagKey parse(String tag) {
|
||||
String body = body(tag);
|
||||
try {
|
||||
return new TagKey(RegistryKey.parseNamespaced(body));
|
||||
} catch(IllegalArgumentException e) {
|
||||
if(body.indexOf(':') < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"invalid tag \"" + tag + "\": a namespace is required, as in \"#namespace:" + body + "\"", e);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses {@code #namespace:id}, or {@code #id} in {@code defaultNamespace}.
|
||||
*
|
||||
* @throws IllegalArgumentException if the string does not start with {@code #} or is not a valid {@link RegistryKey}
|
||||
*/
|
||||
public static TagKey parse(String tag, String defaultNamespace) {
|
||||
return new TagKey(RegistryKey.parse(body(tag), defaultNamespace));
|
||||
}
|
||||
|
||||
private static String body(String tag) {
|
||||
if(tag == null || !tag.startsWith("#")) throw new IllegalArgumentException("invalid tag (expected #namespace:id): \"" + tag + "\"");
|
||||
return tag.substring(1);
|
||||
}
|
||||
|
||||
public RegistryKey key() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return obj instanceof TagKey that && key.equals(that.key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return key.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "#" + key;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.dfsek.terra.api.world.attribute;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import com.dfsek.terra.api.registry.key.Keyed;
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
|
||||
|
||||
/**
|
||||
* Build each attribute once and register the same instance in every pack: every build takes a new {@link #index()}.
|
||||
*/
|
||||
public interface Attribute<T> extends Keyed<Attribute<T>> {
|
||||
TypeKey<Attribute<?>> REGISTRY_TYPE = new TypeKey<>() { };
|
||||
|
||||
AttributeKey<T> key();
|
||||
|
||||
AttributeType<T> type();
|
||||
|
||||
/**
|
||||
* {@code null} means no override.
|
||||
*/
|
||||
@Nullable
|
||||
T defaultValue();
|
||||
|
||||
AttributeScope scope();
|
||||
|
||||
/**
|
||||
* @throws DecodeException if the value is outside this attribute's range or not a required multiple
|
||||
*/
|
||||
T validate(T value) throws DecodeException;
|
||||
|
||||
/**
|
||||
* Clamps numeric values to this attribute's range; returns other values unchanged.
|
||||
*/
|
||||
T sanitize(T value);
|
||||
|
||||
/**
|
||||
* Dense, from 0, below {@link AttributeHandle#attributeCount()}.
|
||||
*/
|
||||
@ApiStatus.Internal
|
||||
int index();
|
||||
|
||||
@Override
|
||||
default RegistryKey getRegistryKey() {
|
||||
return key().getRegistryKey();
|
||||
}
|
||||
|
||||
|
||||
interface Builder<T> {
|
||||
/**
|
||||
* Values must lie in {@code [min, max]}; resolved values are clamped to it.
|
||||
*/
|
||||
Builder<T> range(Number min, Number max);
|
||||
|
||||
Builder<T> multipleOf(int multipleOf);
|
||||
|
||||
Builder<T> packOnly();
|
||||
|
||||
Attribute<T> build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.dfsek.terra.api.world.attribute;
|
||||
|
||||
public interface AttributeCodec<T> {
|
||||
/**
|
||||
* @param raw a number, string, boolean, list, string-keyed map, or {@code null}
|
||||
*/
|
||||
T decode(Object raw, DecodeContext context) throws DecodeException;
|
||||
|
||||
/**
|
||||
* Returns a number, boolean, string, list or string-keyed map of those, or {@code null}. {@link #decode} must read it
|
||||
* back to an equal value.
|
||||
*/
|
||||
Object encode(T value);
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package com.dfsek.terra.api.world.attribute;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
import com.dfsek.terra.api.registry.key.StringIdentifiable;
|
||||
import com.dfsek.terra.api.registry.key.TagKey;
|
||||
import com.dfsek.terra.api.util.generic.either.Either;
|
||||
|
||||
|
||||
public interface AttributeCodecs {
|
||||
@FunctionalInterface
|
||||
interface Decoder<T> {
|
||||
T decode(Object raw, DecodeContext context) throws DecodeException;
|
||||
}
|
||||
|
||||
|
||||
@FunctionalInterface
|
||||
interface Encoder<T> {
|
||||
Object encode(T value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Not thread safe.
|
||||
*/
|
||||
interface ObjectReader {
|
||||
/**
|
||||
* In config order.
|
||||
*/
|
||||
List<String> keys();
|
||||
|
||||
/**
|
||||
* @throws DecodeException if the key is missing or its value does not decode
|
||||
*/
|
||||
<V> V required(String key, AttributeCodec<V> codec) throws DecodeException;
|
||||
|
||||
<V> Optional<V> optional(String key, AttributeCodec<V> codec) throws DecodeException;
|
||||
|
||||
<V> V orDefault(String key, AttributeCodec<V> codec, V fallback) throws DecodeException;
|
||||
|
||||
/**
|
||||
* @throws DecodeException if the object has a key that was not read
|
||||
*/
|
||||
void finish() throws DecodeException;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Not thread safe. Keeps fields in the order written.
|
||||
*/
|
||||
interface ObjectWriter {
|
||||
<V> ObjectWriter put(String key, AttributeCodec<V> codec, V value);
|
||||
|
||||
<V> ObjectWriter optional(String key, AttributeCodec<V> codec, Optional<V> value);
|
||||
|
||||
Map<String, Object> map();
|
||||
}
|
||||
|
||||
<T> AttributeCodec<T> of(Decoder<T> decoder, Encoder<T> encoder);
|
||||
|
||||
/**
|
||||
* Names {@code value} for messages, such as {@code "text"}, {@code an object} or {@code nothing}.
|
||||
*/
|
||||
String describe(@Nullable Object value);
|
||||
|
||||
AttributeCodec<Boolean> booleanCodec();
|
||||
|
||||
AttributeCodec<Float> floatCodec();
|
||||
|
||||
AttributeCodec<Double> doubleCodec();
|
||||
|
||||
/**
|
||||
* Rejects fractions and values outside the {@code int} range.
|
||||
*/
|
||||
AttributeCodec<Integer> intCodec();
|
||||
|
||||
AttributeCodec<Integer> nonNegativeInt();
|
||||
|
||||
AttributeCodec<Float> unitFloat();
|
||||
|
||||
/**
|
||||
* @param expected the start of the message when the value is not a string, as in {@code expected a sound ID}
|
||||
*/
|
||||
AttributeCodec<String> string(String expected);
|
||||
|
||||
AttributeCodec<RawValue> raw();
|
||||
|
||||
<V> AttributeCodec<List<V>> list(AttributeCodec<V> element);
|
||||
|
||||
/**
|
||||
* Also reads a single element as a list of one. Always encodes a list.
|
||||
*/
|
||||
<V> AttributeCodec<List<V>> compactList(AttributeCodec<V> element);
|
||||
|
||||
/**
|
||||
* Reads constants by {@link StringIdentifiable#getID() ID}.
|
||||
*
|
||||
* @param name names the enum in messages, such as {@code moon phase}
|
||||
*
|
||||
* @throws IllegalArgumentException if two constants share an ID
|
||||
*/
|
||||
<E extends Enum<E> & StringIdentifiable> AttributeCodec<E> enumCodec(String name, Class<E> type);
|
||||
|
||||
/**
|
||||
* A bare ID takes {@code defaultNamespace}. Tags are rejected.
|
||||
*/
|
||||
AttributeCodec<RegistryKey> identifier(String defaultNamespace);
|
||||
|
||||
/**
|
||||
* A bare ID takes {@code defaultNamespace}.
|
||||
*/
|
||||
AttributeCodec<Either<RegistryKey, TagKey>> identifierOrTag(String defaultNamespace);
|
||||
|
||||
/**
|
||||
* @param what names the object in messages, such as {@code music}
|
||||
*
|
||||
* @throws DecodeException if {@code raw} is not an object, or has a key that is not a string
|
||||
*/
|
||||
ObjectReader reader(String what, Object raw, DecodeContext context) throws DecodeException;
|
||||
|
||||
ObjectWriter writer();
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.dfsek.terra.api.world.attribute;
|
||||
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
|
||||
public record AttributeEntry<T>(@NotNull Operation operation, @Nullable Object argument) {
|
||||
/**
|
||||
* @throws IllegalArgumentException if {@code argument} is {@code null} and {@code operation} is not {@link Operation#OVERRIDE}
|
||||
*/
|
||||
public AttributeEntry {
|
||||
Objects.requireNonNull(operation, "operation");
|
||||
if(argument == null && operation != Operation.OVERRIDE) {
|
||||
throw new IllegalArgumentException("modifier " + operation.getID() + " needs an argument");
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Contract("_ -> new")
|
||||
public static <T> AttributeEntry<T> override(@Nullable T value) {
|
||||
return new AttributeEntry<>(Operation.OVERRIDE, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IllegalArgumentException if {@code argument} is {@code null} and {@code op} is not {@link Operation#OVERRIDE}
|
||||
*/
|
||||
@NotNull
|
||||
@Contract("_, _ -> new")
|
||||
public static <T> AttributeEntry<T> of(@NotNull Operation op, @Nullable Object argument) {
|
||||
return new AttributeEntry<>(op, argument);
|
||||
}
|
||||
|
||||
public T apply(@NotNull AttributeType<T> type, T subject) {
|
||||
return type.apply(operation, subject, argument);
|
||||
}
|
||||
|
||||
public boolean isOverride() {
|
||||
return operation == Operation.OVERRIDE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package com.dfsek.terra.api.world.attribute;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
import com.dfsek.terra.api.world.attribute.schedule.AttributeSchedule;
|
||||
import com.dfsek.terra.api.world.attribute.schedule.ScheduleDecodeResult;
|
||||
|
||||
|
||||
public interface AttributeHandle {
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
AttributeTypes types();
|
||||
|
||||
/**
|
||||
* Applies no config preprocessing.
|
||||
*/
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
DecodeContext plainContext();
|
||||
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
AttributeCodecs codecs();
|
||||
|
||||
@NotNull
|
||||
@Contract("_, _ -> new")
|
||||
<T> AttributeType.Builder<T> typeBuilder(@NotNull String id, @NotNull AttributeCodec<T> codec);
|
||||
|
||||
@NotNull
|
||||
@Contract("_, _, _ -> new")
|
||||
<T> Attribute.Builder<T> attributeBuilder(@NotNull AttributeKey<T> key, @NotNull AttributeType<T> type, @Nullable T defaultValue);
|
||||
|
||||
/**
|
||||
* Only grows. Every {@link Attribute#index()} is below it.
|
||||
*/
|
||||
@ApiStatus.Internal
|
||||
int attributeCount();
|
||||
|
||||
/**
|
||||
* Applies each non-null layer, bottom first, to the default, then sanitizes. A {@code null} value only changes on an override.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Contract(pure = true)
|
||||
<T> @Nullable T resolve(@NotNull Attribute<T> attribute, AttributeEntry<T>... layersBottomToTop);
|
||||
|
||||
/**
|
||||
* Allocation-free {@link #resolve(Attribute, AttributeEntry[])}, bottom layer first.
|
||||
*/
|
||||
@Contract(pure = true)
|
||||
<T> @Nullable T resolve(@NotNull Attribute<T> attribute, @Nullable AttributeEntry<T> first, @Nullable AttributeEntry<T> second,
|
||||
@Nullable AttributeEntry<T> third);
|
||||
|
||||
/**
|
||||
* Allocation-free {@link #resolve(Attribute, AttributeEntry[])}, bottom layer first.
|
||||
*/
|
||||
@Contract(pure = true)
|
||||
<T> @Nullable T resolve(@NotNull Attribute<T> attribute, @Nullable AttributeEntry<T> first, @Nullable AttributeEntry<T> second,
|
||||
@Nullable AttributeEntry<T> third, @Nullable AttributeEntry<T> fourth);
|
||||
|
||||
/**
|
||||
* Composes two layers into one entry that applies like {@code lower} then {@code upper}. Empty when both are {@code null}
|
||||
* or they don't compose; fall back to an override of the {@link #resolve resolved} value. Float results may differ by rounding.
|
||||
*/
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
<T> Optional<AttributeEntry<T>> compose(@NotNull Attribute<T> attribute, @Nullable AttributeEntry<T> lower,
|
||||
@Nullable AttributeEntry<T> upper);
|
||||
|
||||
/**
|
||||
* Reads a bare value as an override, or a {@code modifier} and {@code argument} map. Resolves {@code raw} first.
|
||||
*/
|
||||
@NotNull
|
||||
<T> AttributeEntry<T> decodeEntry(@NotNull Attribute<T> attribute, @Nullable Object raw, @NotNull DecodeContext context)
|
||||
throws DecodeException;
|
||||
|
||||
/**
|
||||
* @throws IllegalArgumentException if {@code entry} is an override of {@code null}
|
||||
*/
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
<T> Object encodeEntry(@NotNull Attribute<T> attribute, @NotNull AttributeEntry<T> entry);
|
||||
|
||||
/**
|
||||
* {@code raw} must already be {@link DecodeContext#resolve resolved}. A bad track is dropped; other problems skip the schedule.
|
||||
*/
|
||||
@NotNull
|
||||
ScheduleDecodeResult decodeSchedule(@NotNull RegistryKey id, @NotNull Map<String, Object> raw, @NotNull AttributeLookup attributes,
|
||||
@NotNull DecodeContext context);
|
||||
|
||||
/**
|
||||
* @throws IllegalArgumentException if a keyframe's argument is {@code null}
|
||||
*/
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
Map<String, Object> encodeSchedule(@NotNull AttributeSchedule schedule);
|
||||
|
||||
/**
|
||||
* Empty unless the {@code config-attributes} addon loaded {@code pack}.
|
||||
*/
|
||||
@NotNull
|
||||
Optional<PackAttributes> pack(@NotNull ConfigPack pack);
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.dfsek.terra.api.world.attribute;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.registry.key.Keyed;
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
|
||||
|
||||
public final class AttributeKey<T> implements Keyed<AttributeKey<T>> {
|
||||
private final RegistryKey key;
|
||||
|
||||
private AttributeKey(RegistryKey key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static <T> AttributeKey<T> of(@NotNull RegistryKey key) {
|
||||
return new AttributeKey<>(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code T} is unchecked: {@link #lookup} returns empty for a key made here.
|
||||
*
|
||||
* @throws IllegalArgumentException if the key has no namespace or is not a valid {@link RegistryKey}
|
||||
*/
|
||||
@NotNull
|
||||
public static <T> AttributeKey<T> parse(@NotNull String key) {
|
||||
if(key == null) throw new IllegalArgumentException("invalid attribute key: null");
|
||||
try {
|
||||
return new AttributeKey<>(RegistryKey.parseNamespaced(key));
|
||||
} catch(IllegalArgumentException e) {
|
||||
if(key.indexOf(':') < 0) throw new IllegalArgumentException("invalid attribute key \"" + key + "\": " + e.getMessage(), e);
|
||||
throw new IllegalArgumentException("invalid attribute key: \"" + key + "\"", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Empty unless the registered attribute's {@link Attribute#key()} is this instance.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@NotNull
|
||||
public Optional<Attribute<T>> lookup(@NotNull Registry<Attribute<?>> registry) {
|
||||
Optional<Attribute<?>> attribute = registry.get(key);
|
||||
if(attribute.isEmpty() || attribute.get().key() != this) return Optional.empty();
|
||||
return Optional.of((Attribute<T>) attribute.get());
|
||||
}
|
||||
|
||||
@Override
|
||||
public RegistryKey getRegistryKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if(this == obj) return true;
|
||||
return obj instanceof AttributeKey<?> that && key.equals(that.key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return key.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return key.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.dfsek.terra.api.world.attribute;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface AttributeLookup {
|
||||
/**
|
||||
* @throws DecodeException if {@code key} names no usable attribute; the message is reported as the reason
|
||||
*/
|
||||
Attribute<?> find(String key) throws DecodeException;
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package com.dfsek.terra.api.world.attribute;
|
||||
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
public final class AttributeMap {
|
||||
private static final AttributeMap EMPTY = new AttributeMap(Map.of());
|
||||
|
||||
private final Map<AttributeKey<?>, AttributeEntry<?>> entries;
|
||||
|
||||
private AttributeMap(Map<AttributeKey<?>, AttributeEntry<?>> entries) {
|
||||
this.entries = entries;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
public static AttributeMap empty() {
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Contract("-> new")
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> Optional<AttributeEntry<T>> get(@NotNull AttributeKey<T> key) {
|
||||
return Optional.ofNullable((AttributeEntry<T>) entries.get(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* In insertion order. Unmodifiable.
|
||||
*/
|
||||
@NotNull
|
||||
public Set<AttributeKey<?>> keys() {
|
||||
return entries.keySet();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return entries.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code top}'s entries replace this map's per key.
|
||||
*/
|
||||
@NotNull
|
||||
public AttributeMap overlay(@NotNull AttributeMap top) {
|
||||
Objects.requireNonNull(top, "top");
|
||||
if(top.entries.isEmpty()) return this;
|
||||
if(entries.isEmpty()) return top;
|
||||
Map<AttributeKey<?>, AttributeEntry<?>> merged = new LinkedHashMap<>(entries);
|
||||
merged.putAll(top.entries);
|
||||
return new AttributeMap(Collections.unmodifiableMap(merged));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return obj instanceof AttributeMap that && entries.equals(that.entries);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return entries.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AttributeMap" + entries;
|
||||
}
|
||||
|
||||
|
||||
public static final class Builder {
|
||||
private final Map<AttributeKey<?>, AttributeEntry<?>> entries = new LinkedHashMap<>();
|
||||
|
||||
private Builder() {
|
||||
}
|
||||
|
||||
public <T> Builder put(AttributeKey<T> key, AttributeEntry<T> entry) {
|
||||
entries.put(Objects.requireNonNull(key, "key"), Objects.requireNonNull(entry, "entry"));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder remove(AttributeKey<?> key) {
|
||||
entries.remove(key);
|
||||
return this;
|
||||
}
|
||||
|
||||
public AttributeMap build() {
|
||||
if(entries.isEmpty()) return EMPTY;
|
||||
return new AttributeMap(Collections.unmodifiableMap(new LinkedHashMap<>(entries)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.dfsek.terra.api.world.attribute;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface AttributeModifier<T, A> {
|
||||
T apply(T subject, A argument);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.dfsek.terra.api.world.attribute;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
|
||||
|
||||
/**
|
||||
* Called on read paths: must not allocate.
|
||||
*/
|
||||
public interface AttributeParents {
|
||||
AttributeParents NONE = new AttributeParents() {
|
||||
@Override
|
||||
public <T> @Nullable AttributeEntry<T> biomeOrNull(Biome biome, Attribute<T> attribute) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> @Nullable AttributeEntry<T> packOrNull(PackAttributes pack, Attribute<T> attribute) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Sits between the pack entry and the biome's own entry.
|
||||
*/
|
||||
<T> @Nullable AttributeEntry<T> biomeOrNull(Biome biome, Attribute<T> attribute);
|
||||
|
||||
/**
|
||||
* Sits between the attribute's default and the pack entry.
|
||||
*/
|
||||
<T> @Nullable AttributeEntry<T> packOrNull(PackAttributes pack, Attribute<T> attribute);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.dfsek.terra.api.world.attribute;
|
||||
|
||||
public enum AttributeScope {
|
||||
ANYWHERE,
|
||||
PACK_ONLY
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.dfsek.terra.api.world.attribute;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.BinaryOperator;
|
||||
|
||||
|
||||
public interface AttributeType<T> {
|
||||
String id();
|
||||
|
||||
/**
|
||||
* Always includes {@link Operation#OVERRIDE}. Unmodifiable.
|
||||
*/
|
||||
Set<Operation> operations();
|
||||
|
||||
T decode(Object raw, DecodeContext context) throws DecodeException;
|
||||
|
||||
Object encode(T value);
|
||||
|
||||
/**
|
||||
* @throws DecodeException if {@code op} is not valid for this type, or the argument does not decode
|
||||
*/
|
||||
Object decodeArgument(Operation op, Object raw, DecodeContext context) throws DecodeException;
|
||||
|
||||
/**
|
||||
* @throws IllegalArgumentException if {@code op} is not valid for this type
|
||||
*/
|
||||
Object encodeArgument(Operation op, Object argument);
|
||||
|
||||
/**
|
||||
* @throws IllegalArgumentException if {@code op} is not valid for this type
|
||||
*/
|
||||
T apply(Operation op, T subject, Object argument);
|
||||
|
||||
/**
|
||||
* @return empty when the type has no composer for {@code op}
|
||||
*
|
||||
* @throws NullPointerException if the composer returns {@code null}
|
||||
*/
|
||||
Optional<Object> compose(Operation op, Object lower, Object upper);
|
||||
|
||||
|
||||
interface Builder<T> {
|
||||
/**
|
||||
* Replaces an earlier {@code op} and drops its composer.
|
||||
*
|
||||
* @throws IllegalArgumentException if {@code op} is {@link Operation#OVERRIDE}
|
||||
*/
|
||||
<A> Builder<T> op(Operation op, AttributeCodec<A> argumentCodec, AttributeModifier<T, A> modifier);
|
||||
|
||||
/**
|
||||
* {@code combine(lower, upper)} must apply like {@code lower} then {@code upper}. {@code A} must be the argument type
|
||||
* {@code op} was added with.
|
||||
*
|
||||
* @throws IllegalArgumentException if {@code op} is {@link Operation#OVERRIDE} or was not added first
|
||||
*/
|
||||
<A> Builder<T> composable(Operation op, BinaryOperator<A> combine);
|
||||
|
||||
AttributeType<T> build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.dfsek.terra.api.world.attribute;
|
||||
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
import com.dfsek.terra.api.registry.key.StringIdentifiable;
|
||||
import com.dfsek.terra.api.registry.key.TagKey;
|
||||
import com.dfsek.terra.api.util.generic.either.Either;
|
||||
import com.dfsek.terra.api.world.attribute.value.Color;
|
||||
import com.dfsek.terra.api.world.attribute.value.TriState;
|
||||
|
||||
|
||||
public interface AttributeTypes {
|
||||
/**
|
||||
* {@code and}, {@code nand}, {@code or}, {@code nor}, {@code xor}, {@code xnor}.
|
||||
*/
|
||||
AttributeType<Boolean> booleanType();
|
||||
|
||||
/**
|
||||
* Override only.
|
||||
*/
|
||||
AttributeType<TriState> triState();
|
||||
|
||||
/**
|
||||
* {@code alpha_blend}, {@code add}, {@code subtract}, {@code multiply}, {@code minimum}, {@code maximum}.
|
||||
*/
|
||||
AttributeType<Float> floatType();
|
||||
|
||||
/**
|
||||
* The {@link #floatType()} library.
|
||||
*/
|
||||
AttributeType<Float> angleDegrees();
|
||||
|
||||
/**
|
||||
* {@code alpha_blend}, {@code add}, {@code subtract}, {@code multiply}, {@code blend_to_gray}.
|
||||
*/
|
||||
AttributeType<Color> rgbColor();
|
||||
|
||||
/**
|
||||
* {@code alpha_blend}, {@code add}, {@code subtract}, {@code multiply}, {@code blend_to_gray}.
|
||||
*/
|
||||
AttributeType<Color> argbColor();
|
||||
|
||||
/**
|
||||
* {@code add}, {@code subtract}, {@code multiply}, {@code minimum}, {@code maximum}.
|
||||
*/
|
||||
AttributeType<Integer> integer();
|
||||
|
||||
/**
|
||||
* Override only.
|
||||
*/
|
||||
AttributeType<Integer> integerOverrideOnly();
|
||||
|
||||
/**
|
||||
* Any ID; a bare ID takes {@code defaultNamespace}. Tags are rejected. Override only.
|
||||
*/
|
||||
AttributeType<RegistryKey> identifier(String defaultNamespace);
|
||||
|
||||
/**
|
||||
* An ID or a {@code #tag}; a bare ID takes {@code defaultNamespace}. Override only.
|
||||
*/
|
||||
AttributeType<Either<RegistryKey, TagKey>> identifierOrTag(String defaultNamespace);
|
||||
|
||||
/**
|
||||
* An enum identified by its {@link StringIdentifiable#getID() ID}. Override only.
|
||||
*
|
||||
* @param type its constants' IDs must be distinct
|
||||
*/
|
||||
<E extends Enum<E> & StringIdentifiable> AttributeType<E> enumType(String id, Class<E> type);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.dfsek.terra.api.world.attribute;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
|
||||
public interface DecodeContext {
|
||||
/**
|
||||
* {@code type} is {@code Float}, {@code Integer}, {@code Double}, {@code Boolean}, {@code String}, {@code List<Object>},
|
||||
* {@code Map<String, Object>} or {@code Object}.
|
||||
*/
|
||||
<V> V load(Type type, Object raw) throws DecodeException;
|
||||
|
||||
/**
|
||||
* Call before checking a value's shape. Numeric expressions stay strings; {@link #load} evaluates them.
|
||||
*/
|
||||
Object resolve(Object raw) throws DecodeException;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.dfsek.terra.api.world.attribute;
|
||||
|
||||
/**
|
||||
* The message is shown to users in config warnings.
|
||||
*/
|
||||
public class DecodeException extends Exception {
|
||||
public DecodeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public DecodeException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.dfsek.terra.api.world.attribute;
|
||||
|
||||
import com.dfsek.terra.api.registry.key.StringIdentifiable;
|
||||
|
||||
|
||||
public enum Operation implements StringIdentifiable {
|
||||
OVERRIDE("override"),
|
||||
ALPHA_BLEND("alpha_blend"),
|
||||
ADD("add"),
|
||||
SUBTRACT("subtract"),
|
||||
MULTIPLY("multiply"),
|
||||
BLEND_TO_GRAY("blend_to_gray"),
|
||||
MINIMUM("minimum"),
|
||||
MAXIMUM("maximum"),
|
||||
AND("and"),
|
||||
NAND("nand"),
|
||||
OR("or"),
|
||||
NOR("nor"),
|
||||
XOR("xor"),
|
||||
XNOR("xnor"),
|
||||
APPEND("append"),
|
||||
OVERLAY("overlay");
|
||||
|
||||
private final String id;
|
||||
|
||||
Operation(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getID() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IllegalArgumentException if no operation has this {@link #getID() ID}
|
||||
*/
|
||||
public static Operation parse(String name) {
|
||||
for(Operation value : values()) {
|
||||
if(value.id.equals(name)) return value;
|
||||
}
|
||||
throw new IllegalArgumentException("unknown modifier \"" + name + "\"");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.dfsek.terra.api.world.attribute;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.dfsek.terra.api.properties.Properties;
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
import com.dfsek.terra.api.registry.key.TagKey;
|
||||
import com.dfsek.terra.api.util.generic.either.Either;
|
||||
import com.dfsek.terra.api.world.attribute.schedule.AttributeSchedule;
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
|
||||
|
||||
/**
|
||||
* Reads resolve the static layers only; schedules are not applied.
|
||||
*/
|
||||
public interface PackAttributes extends Properties {
|
||||
@NotNull
|
||||
AttributeMap entries();
|
||||
|
||||
/**
|
||||
* By config key, in config order. Unmodifiable.
|
||||
*/
|
||||
@NotNull
|
||||
Map<String, RegistryKey> parents();
|
||||
|
||||
/**
|
||||
* In config order.
|
||||
*/
|
||||
@NotNull
|
||||
List<Either<RegistryKey, TagKey>> scheduleRefs();
|
||||
|
||||
/**
|
||||
* By ID, in config order. Unmodifiable.
|
||||
*/
|
||||
@NotNull
|
||||
Map<RegistryKey, AttributeSchedule> scheduleDefinitions();
|
||||
|
||||
/**
|
||||
* {@code null} if the biome is not from this pack. Allocation-free.
|
||||
*/
|
||||
@Nullable
|
||||
<T> AttributeEntry<T> biomeEntryOrNull(@NotNull Attribute<T> attribute, @NotNull Biome biome);
|
||||
|
||||
/**
|
||||
* Merged across the biome's {@code extends} chain. Empty if the biome is not from this pack.
|
||||
*/
|
||||
@NotNull
|
||||
AttributeMap biomeEntries(@NotNull Biome biome);
|
||||
|
||||
/**
|
||||
* Layers, bottom first: default, {@link AttributeParents#packOrNull pack parent}, pack entry,
|
||||
* {@link AttributeParents#biomeOrNull biome parent}, biome entry.
|
||||
*/
|
||||
<T> @Nullable T resolve(@NotNull Attribute<T> attribute, @NotNull Biome biome);
|
||||
|
||||
/**
|
||||
* Layers, bottom first: default, {@link AttributeParents#packOrNull pack parent}, pack entry.
|
||||
*/
|
||||
<T> @Nullable T resolvePack(@NotNull Attribute<T> attribute);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.dfsek.terra.api.world.attribute;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
public record RawValue(Object value) {
|
||||
public RawValue {
|
||||
value = copy(value);
|
||||
}
|
||||
|
||||
private static Object copy(Object o) {
|
||||
if(o == null || o instanceof String || o instanceof Number || o instanceof Boolean) return o;
|
||||
if(o instanceof List<?> list) {
|
||||
List<Object> out = new ArrayList<>(list.size());
|
||||
for(Object e : list) out.add(copy(e));
|
||||
return Collections.unmodifiableList(out);
|
||||
}
|
||||
if(o instanceof Map<?, ?> map) {
|
||||
Map<String, Object> out = new LinkedHashMap<>();
|
||||
for(Map.Entry<?, ?> e : map.entrySet()) {
|
||||
if(!(e.getKey() instanceof String key)) {
|
||||
throw new IllegalArgumentException("unsupported value: map keys must be strings");
|
||||
}
|
||||
out.put(key, copy(e.getValue()));
|
||||
}
|
||||
return Collections.unmodifiableMap(out);
|
||||
}
|
||||
throw new IllegalArgumentException("unsupported value");
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.dfsek.terra.api.world.attribute.schedule;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.OptionalInt;
|
||||
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
import com.dfsek.terra.api.world.attribute.Attribute;
|
||||
|
||||
|
||||
/**
|
||||
* Both maps are unmodifiable copies in the given order.
|
||||
*/
|
||||
public record AttributeSchedule(RegistryKey clock, OptionalInt periodTicks, Map<Attribute<?>, ScheduleTrack<?>> tracks,
|
||||
Map<RegistryKey, TimeMarker> timeMarkers) {
|
||||
public AttributeSchedule {
|
||||
tracks = Collections.unmodifiableMap(new LinkedHashMap<>(tracks));
|
||||
timeMarkers = Collections.unmodifiableMap(new LinkedHashMap<>(timeMarkers));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.dfsek.terra.api.world.attribute.schedule;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
public sealed interface Easing {
|
||||
Set<String> NAMES = Set.of(
|
||||
"constant", "linear",
|
||||
"in_back", "in_bounce", "in_circ", "in_cubic", "in_elastic", "in_expo", "in_quad", "in_quart", "in_quint", "in_sine",
|
||||
"in_out_back", "in_out_bounce", "in_out_circ", "in_out_cubic", "in_out_elastic", "in_out_expo", "in_out_quad",
|
||||
"in_out_quart", "in_out_quint", "in_out_sine",
|
||||
"out_back", "out_bounce", "out_circ", "out_cubic", "out_elastic", "out_expo", "out_quad", "out_quart", "out_quint",
|
||||
"out_sine");
|
||||
|
||||
|
||||
record Named(String name) implements Easing {
|
||||
public Named {
|
||||
if(!NAMES.contains(name)) throw new IllegalArgumentException("unknown easing \"" + name + "\"");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
record CubicBezier(float x1, float y1, float x2, float y2) implements Easing {
|
||||
public CubicBezier {
|
||||
if(!(x1 >= 0f && x1 <= 1f)) throw new IllegalArgumentException("cubic_bezier x1 is outside [0, 1]");
|
||||
if(!(x2 >= 0f && x2 <= 1f)) throw new IllegalArgumentException("cubic_bezier x2 is outside [0, 1]");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.dfsek.terra.api.world.attribute.schedule;
|
||||
|
||||
/**
|
||||
* {@code argument} comes from {@link com.dfsek.terra.api.world.attribute.AttributeType#decodeArgument}.
|
||||
*/
|
||||
public record Keyframe(int ticks, Object argument) { }
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.dfsek.terra.api.world.attribute.schedule;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
public record ScheduleDecodeResult(Optional<AttributeSchedule> schedule, List<Problem> droppedTracks, Optional<Problem> skipped) {
|
||||
public ScheduleDecodeResult {
|
||||
Objects.requireNonNull(schedule, "schedule");
|
||||
droppedTracks = List.copyOf(droppedTracks);
|
||||
Objects.requireNonNull(skipped, "skipped");
|
||||
if(schedule.isPresent() == skipped.isPresent()) {
|
||||
throw new IllegalArgumentException("exactly one of schedule and skipped must be present");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public record Problem(String key, String reason) { }
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.dfsek.terra.api.world.attribute.schedule;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.api.world.attribute.Operation;
|
||||
|
||||
|
||||
public record ScheduleTrack<T>(Operation operation, List<Keyframe> keyframes, Easing ease) {
|
||||
public ScheduleTrack {
|
||||
keyframes = List.copyOf(keyframes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.dfsek.terra.api.world.attribute.schedule;
|
||||
|
||||
public record TimeMarker(int ticks, boolean showInCommands) { }
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.dfsek.terra.api.world.attribute.value;
|
||||
|
||||
/**
|
||||
* Both in [0, 1].
|
||||
*/
|
||||
public record BlendToGray(float brightness, float factor) { }
|
||||
@@ -0,0 +1,97 @@
|
||||
package com.dfsek.terra.api.world.attribute.value;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
||||
/**
|
||||
* Channels are in [0, 1].
|
||||
*/
|
||||
public record Color(float r, float g, float b, float a) {
|
||||
private static final Pattern HEX6 = Pattern.compile("#[0-9a-fA-F]{6}");
|
||||
private static final Pattern HEX8 = Pattern.compile("#[0-9a-fA-F]{8}");
|
||||
|
||||
public static Color rgb(int rgb) {
|
||||
return new Color(((rgb >> 16) & 0xFF) / 255f, ((rgb >> 8) & 0xFF) / 255f, (rgb & 0xFF) / 255f, 1f);
|
||||
}
|
||||
|
||||
public static Color argb(int argb) {
|
||||
return new Color(((argb >> 16) & 0xFF) / 255f, ((argb >> 8) & 0xFF) / 255f, (argb & 0xFF) / 255f, ((argb >>> 24) & 0xFF) / 255f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts {@code "#rrggbb"}, a list of 3 floats, or an integer {@code 0xRRGGBB} (alpha bits ignored).
|
||||
*/
|
||||
public static Color parseRgb(Object o) {
|
||||
if(o instanceof String s) {
|
||||
if(!HEX6.matcher(s).matches()) throw new IllegalArgumentException("invalid RGB color (expected #rrggbb): \"" + s + "\"");
|
||||
return rgb(Integer.parseInt(s.substring(1), 16));
|
||||
}
|
||||
if(o instanceof List<?> l) {
|
||||
if(l.size() != 3) throw new IllegalArgumentException("color list must have 3 elements, got " + l);
|
||||
return new Color(channel(l.get(0)), channel(l.get(1)), channel(l.get(2)), 1f);
|
||||
}
|
||||
if(o instanceof Number n) return rgb(integral(n, Integer.MAX_VALUE));
|
||||
throw new IllegalArgumentException("invalid RGB color: " + o);
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts {@code "#aarrggbb"}, a list of 4 floats (r, g, b, a), or an integer {@code 0xAARRGGBB}, signed or unsigned.
|
||||
*/
|
||||
public static Color parseArgb(Object o) {
|
||||
if(o instanceof String s) {
|
||||
if(!HEX8.matcher(s).matches()) throw new IllegalArgumentException("invalid ARGB color (expected #aarrggbb): \"" + s + "\"");
|
||||
return argb((int) Long.parseLong(s.substring(1), 16));
|
||||
}
|
||||
if(o instanceof List<?> l) {
|
||||
if(l.size() != 4) throw new IllegalArgumentException("color list must have 4 elements, got " + l);
|
||||
return new Color(channel(l.get(0)), channel(l.get(1)), channel(l.get(2)), channel(l.get(3)));
|
||||
}
|
||||
if(o instanceof Number n) return argb(integral(n, 0xFFFFFFFFL));
|
||||
throw new IllegalArgumentException("invalid ARGB color: " + o);
|
||||
}
|
||||
|
||||
private static int integral(Number n, long max) {
|
||||
BigInteger value;
|
||||
try {
|
||||
value = exact(n).toBigIntegerExact();
|
||||
} catch(ArithmeticException | NumberFormatException e) {
|
||||
throw new IllegalArgumentException("color integer must be integral: " + n);
|
||||
}
|
||||
if(value.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0 || value.compareTo(BigInteger.valueOf(max)) > 0) {
|
||||
throw new IllegalArgumentException("value " + n + " is outside [" + Integer.MIN_VALUE + ", " + max + "]");
|
||||
}
|
||||
return value.intValue();
|
||||
}
|
||||
|
||||
private static BigDecimal exact(Number n) {
|
||||
if(n instanceof Integer || n instanceof Long || n instanceof Short || n instanceof Byte) return BigDecimal.valueOf(n.longValue());
|
||||
if(n instanceof BigDecimal d) return d;
|
||||
if(n instanceof BigInteger i) return new BigDecimal(i);
|
||||
if(n instanceof Double || n instanceof Float) {
|
||||
double d = n.doubleValue();
|
||||
if(!Double.isFinite(d)) throw new NumberFormatException();
|
||||
return new BigDecimal(d);
|
||||
}
|
||||
return new BigDecimal(n.toString());
|
||||
}
|
||||
|
||||
private static float channel(Object o) {
|
||||
if(o instanceof Number n) return n.floatValue();
|
||||
throw new IllegalArgumentException("color channel must be a number: " + o);
|
||||
}
|
||||
|
||||
private static int to255(float c) {
|
||||
return Math.max(0, Math.min(255, Math.round(c * 255)));
|
||||
}
|
||||
|
||||
public int toRgbInt() {
|
||||
return (to255(r) << 16) | (to255(g) << 8) | to255(b);
|
||||
}
|
||||
|
||||
public int toArgbInt() {
|
||||
return (to255(a) << 24) | toRgbInt();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.dfsek.terra.api.world.attribute.value;
|
||||
|
||||
/**
|
||||
* {@code alpha} is in [0, 1].
|
||||
*/
|
||||
public record FloatWithAlpha(float value, float alpha) { }
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.dfsek.terra.api.world.attribute.value;
|
||||
|
||||
import com.dfsek.terra.api.registry.key.StringIdentifiable;
|
||||
|
||||
|
||||
public enum TriState implements StringIdentifiable {
|
||||
TRUE("true"),
|
||||
FALSE("false"),
|
||||
DEFAULT("default");
|
||||
|
||||
private final String id;
|
||||
|
||||
TriState(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getID() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package com.dfsek.terra.api.registry.key;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.NullSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
|
||||
public class RegistryKeyTest {
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"mypack:visual/fog_color, mypack, visual/fog_color",
|
||||
"mypack:ambient.cave, mypack, ambient.cave",
|
||||
"my.pack:thing, my.pack, thing",
|
||||
"terra:overworld_biome-1, terra, overworld_biome-1",
|
||||
"MyPack:Seasons, MyPack, Seasons",
|
||||
})
|
||||
void validKeysParse(String key, String namespace, String id) {
|
||||
RegistryKey parsed = RegistryKey.parse(key);
|
||||
assertEquals(RegistryKey.of(namespace, id), parsed);
|
||||
assertEquals(namespace, parsed.getNamespace());
|
||||
assertEquals(id, parsed.getID());
|
||||
assertEquals(key, parsed.toString());
|
||||
assertEquals(parsed, RegistryKey.parse(key, "other"));
|
||||
assertEquals(parsed, RegistryKey.parseNamespaced(key));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "a/b:thing", "bad key", "a:b:c", "a:b c", "a b:c" })
|
||||
void invalidKeysAreRejected(String key) {
|
||||
assertThrows(IllegalArgumentException.class, () -> RegistryKey.parse(key));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidCharacterMessage() {
|
||||
String id = assertThrows(IllegalArgumentException.class, () -> RegistryKey.of("a", "b c")).getMessage();
|
||||
assertTrue(id.contains("slashes") && id.contains("periods") && id.contains("\"b c\""), id);
|
||||
String ns = assertThrows(IllegalArgumentException.class, () -> RegistryKey.of("a b", "c")).getMessage();
|
||||
assertTrue(ns.contains("periods") && !ns.contains("slashes") && ns.contains("\"a b\""), ns);
|
||||
}
|
||||
|
||||
@Test
|
||||
void hashAndEquals() {
|
||||
RegistryKey key = RegistryKey.parse("mypack:visual/fog_color");
|
||||
RegistryKey same = RegistryKey.of("mypack", "visual/fog_color");
|
||||
assertEquals(Objects.hash("mypack", "visual/fog_color"), key.hashCode());
|
||||
assertEquals(key, same);
|
||||
assertEquals(key.hashCode(), same.hashCode());
|
||||
assertNotEquals(key, RegistryKey.of("mypack", "visual/sky_color"));
|
||||
assertNotEquals(key, RegistryKey.of("terra", "visual/fog_color"));
|
||||
assertNotEquals(RegistryKey.of("a", "bc"), RegistryKey.of("ab", "c"));
|
||||
assertNotEquals(key, "mypack:visual/fog_color");
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseWithDefaultNamespace() {
|
||||
assertEquals(RegistryKey.of("mypack", "fog"), RegistryKey.parse("fog", "mypack"));
|
||||
assertEquals(RegistryKey.of("my.pack", "a/b"), RegistryKey.parse("my.pack:a/b", "mypack"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(nullValues = "NULL", value = {
|
||||
":x, mypack, 'malformed ID \":x\": empty namespace'",
|
||||
"x:, mypack, 'malformed ID \"x:\": empty ID'",
|
||||
"'', mypack, 'malformed ID \"\": empty ID'",
|
||||
"NULL, mypack, 'malformed ID: none given'",
|
||||
"x, NULL, 'malformed ID \"x\": empty namespace'",
|
||||
"a:b:c, mypack, 'malformed ID \"a:b:c\": more than one '':'''",
|
||||
})
|
||||
void parseWithDefaultNamespaceRejects(String key, String defaultNamespace, String message) {
|
||||
assertEquals(message, assertThrows(IllegalArgumentException.class, () -> RegistryKey.parse(key, defaultNamespace)).getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseNamespacedWithoutNamespace() {
|
||||
assertEquals("a namespace is required, as in \"namespace:overworld\"",
|
||||
assertThrows(IllegalArgumentException.class, () -> RegistryKey.parseNamespaced("overworld")).getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@NullSource
|
||||
@ValueSource(strings = { "mypack:Bad Id", "#mypack:tag", "mypack:", ":overworld", "a:b:c" })
|
||||
void parseNamespacedRejects(String key) {
|
||||
assertThrows(IllegalArgumentException.class, () -> RegistryKey.parseNamespaced(key));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagKey() {
|
||||
TagKey tag = TagKey.parse("#mypack:infiniburn");
|
||||
assertEquals(RegistryKey.parse("mypack:infiniburn"), tag.key());
|
||||
assertEquals("#mypack:infiniburn", tag.toString());
|
||||
assertEquals(tag, TagKey.parse("#infiniburn", "mypack"));
|
||||
assertEquals(tag, TagKey.of(RegistryKey.parse("mypack:infiniburn")));
|
||||
assertEquals(RegistryKey.of("mypack", "Music.Game"), TagKey.parse("#mypack:Music.Game").key());
|
||||
assertEquals(RegistryKey.of("MyPack", "Logs"), TagKey.parse("#Logs", "MyPack").key());
|
||||
assertEquals("invalid tag \"#stone\": a namespace is required, as in \"#namespace:stone\"",
|
||||
assertThrows(IllegalArgumentException.class, () -> TagKey.parse("#stone")).getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "mypack:stone", "#mypack:", "#mypack:bad id" })
|
||||
void tagKeyRejects(String tag) {
|
||||
assertThrows(IllegalArgumentException.class, () -> TagKey.parse(tag));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.dfsek.terra.api.world.attribute;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
|
||||
class AttributeMapTest {
|
||||
private static final AttributeKey<Integer> A = AttributeKey.parse("test:a");
|
||||
private static final AttributeKey<Integer> B = AttributeKey.parse("test:b");
|
||||
|
||||
@Test
|
||||
void emptyIsShared() {
|
||||
assertSame(AttributeMap.empty(), AttributeMap.builder().build());
|
||||
assertSame(AttributeMap.empty(), AttributeMap.builder().put(A, AttributeEntry.override(1)).remove(A).build());
|
||||
assertTrue(AttributeMap.empty().get(A).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void overlayReplacesPerKey() {
|
||||
AttributeEntry<Integer> bottomA = AttributeEntry.override(1);
|
||||
AttributeMap bottom = AttributeMap.builder().put(A, bottomA).put(B, AttributeEntry.override(2)).build();
|
||||
AttributeMap top = AttributeMap.builder().put(B, AttributeEntry.override(3)).build();
|
||||
AttributeMap merged = bottom.overlay(top);
|
||||
assertEquals(List.of(A, B), List.copyOf(merged.keys()));
|
||||
assertSame(bottomA, merged.get(A).orElseThrow());
|
||||
assertEquals(AttributeEntry.override(3), merged.get(B).orElseThrow());
|
||||
assertEquals(AttributeEntry.override(2), bottom.get(B).orElseThrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void builderRemove() {
|
||||
AttributeMap map = AttributeMap.builder().put(A, AttributeEntry.override(1)).put(B, AttributeEntry.override(2)).remove(A).build();
|
||||
assertEquals(Set.of(B), map.keys());
|
||||
assertTrue(map.get(AttributeKey.<Integer>parse("test:b")).isPresent());
|
||||
assertThrows(UnsupportedOperationException.class, () -> map.keys().clear());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullsThrow() {
|
||||
AttributeMap.Builder builder = AttributeMap.builder();
|
||||
assertThrows(NullPointerException.class, () -> builder.put(null, AttributeEntry.override(1)));
|
||||
assertThrows(NullPointerException.class, () -> builder.put(A, null));
|
||||
assertEquals("top", assertThrows(NullPointerException.class, () -> AttributeMap.empty().overlay(null)).getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullArgument() {
|
||||
assertNull(AttributeEntry.override(null).argument());
|
||||
assertNull(AttributeEntry.of(Operation.OVERRIDE, null).argument());
|
||||
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> AttributeEntry.of(Operation.ADD, null));
|
||||
assertEquals("modifier add needs an argument", e.getMessage());
|
||||
assertThrows(IllegalArgumentException.class, () -> new AttributeEntry<Integer>(Operation.MULTIPLY, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void equalByContent() {
|
||||
AttributeMap one = AttributeMap.builder().put(A, AttributeEntry.override(1)).build();
|
||||
AttributeMap two = AttributeMap.builder().put(A, AttributeEntry.override(1)).build();
|
||||
assertEquals(one, two);
|
||||
assertEquals(one.hashCode(), two.hashCode());
|
||||
assertNotEquals(one, AttributeMap.builder().put(A, AttributeEntry.override(2)).build());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package com.dfsek.terra.api.world.attribute;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.NullSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
import com.dfsek.terra.api.world.attribute.schedule.Easing;
|
||||
import com.dfsek.terra.api.world.attribute.value.Color;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.params.provider.Arguments.arguments;
|
||||
|
||||
|
||||
public class ValueTypesTest {
|
||||
@Test
|
||||
public void attributeKey() {
|
||||
RegistryKey registryKey = RegistryKey.parse("my.pack:visual/Fog_Color");
|
||||
AttributeKey<Float> key = AttributeKey.parse("my.pack:visual/Fog_Color");
|
||||
assertEquals(registryKey, key.getRegistryKey());
|
||||
assertEquals("my.pack:visual/Fog_Color", key.toString());
|
||||
AttributeKey<Float> wrapped = AttributeKey.of(registryKey);
|
||||
assertSame(registryKey, wrapped.getRegistryKey());
|
||||
assertEquals(wrapped, key);
|
||||
assertEquals(registryKey.hashCode(), key.hashCode());
|
||||
assertNotEquals(key, AttributeKey.parse("my.pack:visual/other"));
|
||||
assertEquals("invalid attribute key \"visual/fog_color\": a namespace is required, as in \"namespace:visual/fog_color\"",
|
||||
assertThrows(IllegalArgumentException.class, () -> AttributeKey.parse("visual/fog_color")).getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@NullSource
|
||||
@ValueSource(strings = { "Bad Key", "mypack:", ":visual/fog_color", "a:b:c" })
|
||||
public void attributeKeyRejects(String key) {
|
||||
assertThrows(IllegalArgumentException.class, () -> AttributeKey.parse(key));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFailureMessages() {
|
||||
assertEquals("unknown modifier \"frobnicate\"",
|
||||
assertThrows(IllegalArgumentException.class, () -> Operation.parse("frobnicate")).getMessage());
|
||||
assertEquals("unknown easing \"x\"",
|
||||
assertThrows(IllegalArgumentException.class, () -> new Easing.Named("x")).getMessage());
|
||||
for(Operation op : Operation.values()) assertSame(op, Operation.parse(op.getID()));
|
||||
}
|
||||
|
||||
private static Arguments rgb(Object raw, int expected) {
|
||||
return arguments(raw, (Function<Object, Color>) Color::parseRgb, Color.rgb(expected));
|
||||
}
|
||||
|
||||
private static Arguments argb(Object raw, int expected) {
|
||||
return arguments(raw, (Function<Object, Color>) Color::parseArgb, Color.argb(expected));
|
||||
}
|
||||
|
||||
static Stream<Arguments> colors() {
|
||||
return Stream.of(
|
||||
rgb("#050533", 0x050533),
|
||||
arguments(List.of(1.0, 0.5, 0.0), (Function<Object, Color>) Color::parseRgb, new Color(1f, 0.5f, 0f, 1f)),
|
||||
rgb(16777215, 0xFFFFFF),
|
||||
rgb(16777215L, 0xFFFFFF),
|
||||
rgb(1.6777215E7, 0xFFFFFF),
|
||||
rgb(BigInteger.valueOf(0x102030), 0x102030),
|
||||
rgb(new BigDecimal("1056816.0"), 0x102030),
|
||||
rgb(0xFF102030, 0x102030),
|
||||
argb("#80ff0000", 0x80FF0000),
|
||||
argb(0xFF102030L, 0xFF102030),
|
||||
argb(0xFF102030, 0xFF102030),
|
||||
argb(BigInteger.valueOf(0xFFFFFFFFL), 0xFFFFFFFF));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
public void colors(Object raw, Function<Object, Color> parser, Color expected) {
|
||||
assertEquals(expected, parser.apply(raw));
|
||||
}
|
||||
|
||||
private static Arguments rejectRgb(Object raw, String message) {
|
||||
return arguments(raw, (Function<Object, Color>) Color::parseRgb, message);
|
||||
}
|
||||
|
||||
private static Arguments rejectArgb(Object raw, String message) {
|
||||
return arguments(raw, (Function<Object, Color>) Color::parseArgb, message);
|
||||
}
|
||||
|
||||
static Stream<Arguments> colorsRejected() {
|
||||
return Stream.of(
|
||||
rejectRgb("#12345", null),
|
||||
rejectRgb(0.5, null),
|
||||
rejectArgb(0.5, null),
|
||||
rejectRgb(new BigDecimal("1.5"), "color integer must be integral: 1.5"),
|
||||
rejectArgb(new BigDecimal("1.5"), null),
|
||||
rejectRgb(Double.NaN, null),
|
||||
rejectRgb(Double.POSITIVE_INFINITY, null),
|
||||
rejectRgb(0xFFFFFFFFL, "value 4294967295 is outside [-2147483648, 2147483647]"),
|
||||
rejectRgb(new BigInteger("100000000000"), null),
|
||||
rejectArgb(0x100000000L, "value 4294967296 is outside [-2147483648, 4294967295]"),
|
||||
rejectArgb(-2147483649L, null));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
public void colorsRejected(Object raw, Function<Object, Color> parser, String message) {
|
||||
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> parser.apply(raw));
|
||||
if(message != null) assertEquals(message, e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rawValueDeepCopy() {
|
||||
Map<String, Object> inner = new HashMap<>();
|
||||
inner.put("a", 1);
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("inner", inner);
|
||||
RawValue raw = new RawValue(map);
|
||||
map.put("b", 2);
|
||||
inner.put("c", 3);
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> copy = (Map<String, Object>) raw.value();
|
||||
assertEquals(1, copy.size());
|
||||
assertEquals(Map.of("a", 1), copy.get("inner"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user