Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ org.jetbrains.dokka.experimental.gradle.pluginMode=V2Enabled
javaVersion=25
mcVersion=26.1.2
group=dev.slne.surf.api
version=3.16.0
version=3.17.0
relocationPrefix=dev.slne.surf.api.libs
snapshot=false
16 changes: 16 additions & 0 deletions surf-api-core/surf-api-core/api/surf-api-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,22 @@ public final class dev/slne/surf/api/core/config/type/DurationOrDisabled {
public final class dev/slne/surf/api/core/config/type/DurationOrDisabled$Companion {
}

public final class dev/slne/surf/api/core/config/type/StringOrDefault {
public static final field Companion Ldev/slne/surf/api/core/config/type/StringOrDefault$Companion;
public synthetic fun <init> (Ljava/lang/String;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Ljava/lang/String;
public fun equals (Ljava/lang/Object;)Z
public final fun getValue ()Ljava/lang/String;
public fun hashCode ()I
public final fun or (Ljava/lang/String;)Ljava/lang/String;
public fun toString ()Ljava/lang/String;
}

public final class dev/slne/surf/api/core/config/type/StringOrDefault$Companion {
public final fun getUSE_DEFAULT ()Ldev/slne/surf/api/core/config/type/StringOrDefault;
public final fun of (Ljava/lang/String;)Ldev/slne/surf/api/core/config/type/StringOrDefault;
}

public abstract interface annotation class dev/slne/surf/api/core/config/type/number/BelowZeroToEmpty : java/lang/annotation/Annotation {
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.slne.surf.api.core.config.constraints

import dev.slne.surf.api.core.config.type.StringOrDefault
import org.spongepowered.configurate.objectmapping.meta.Constraint
import org.spongepowered.configurate.serialize.SerializationException
import java.lang.reflect.Type
Expand All @@ -23,5 +24,14 @@ annotation class Contains(val value: String) {
}
}
}

internal object FactoryStringOrDefault : Constraint.Factory<Contains, StringOrDefault?> {
override fun make(data: Contains, type: Type): Constraint<StringOrDefault?> = { valueOrDefault ->
val value = valueOrDefault?.value
if (value != null && !value.contains(data.value)) {
throw SerializationException("String must contain '${data.value}'")
}
}
Comment thread
twisti-dev marked this conversation as resolved.
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.slne.surf.api.core.config.constraints

import dev.slne.surf.api.core.config.type.StringOrDefault
import org.spongepowered.configurate.objectmapping.meta.Constraint
import org.spongepowered.configurate.serialize.SerializationException
import java.lang.reflect.Type
Expand All @@ -23,5 +24,14 @@ annotation class EndsWith(val suffix: String) {
}
}
}

internal object FactoryStringOrDefault : Constraint.Factory<EndsWith, StringOrDefault?> {
override fun make(data: EndsWith, type: Type): Constraint<StringOrDefault?> = { stringOrDefault ->
val value = stringOrDefault?.value
if (value != null && !value.endsWith(data.suffix)) {
throw SerializationException("String must end with '${data.suffix}'")
}
}
Comment thread
twisti-dev marked this conversation as resolved.
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.slne.surf.api.core.config.constraints

import dev.slne.surf.api.core.config.type.StringOrDefault
import org.spongepowered.configurate.objectmapping.meta.Constraint
import org.spongepowered.configurate.serialize.SerializationException
import java.io.File
Expand Down Expand Up @@ -41,12 +42,13 @@ annotation class ExistingFile {
}
}

internal fun Any?.asPathOrNull(): Path? {
internal tailrec fun Any?.asPathOrNull(): Path? {
return when (this) {
null -> null
is Path -> this
is File -> toPath()
is String -> runCatching { Path.of(this) }.getOrNull()
is StringOrDefault -> value?.asPathOrNull()
Comment thread
twisti-dev marked this conversation as resolved.
Comment thread
twisti-dev marked this conversation as resolved.
else -> null
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.slne.surf.api.core.config.constraints

import dev.slne.surf.api.core.config.type.ConfigDuration
import dev.slne.surf.api.core.config.type.DurationOrDisabled
import org.spongepowered.configurate.objectmapping.meta.Constraint
import org.spongepowered.configurate.serialize.SerializationException
import java.lang.reflect.Type
Expand All @@ -25,5 +26,15 @@ annotation class MaxDuration(val seconds: Long) {
}
}
}

internal object FactoryDurationOrDisabled : Constraint.Factory<MaxDuration, DurationOrDisabled?> {
override fun make(data: MaxDuration, type: Type): Constraint<DurationOrDisabled?> = { durationOrDisabled ->
val value = durationOrDisabled?.value

if (value != null && value.inWholeSeconds > data.seconds) {
throw SerializationException("Duration is too long: ${value}, expected <= ${data.seconds}s")
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.slne.surf.api.core.config.constraints

import dev.slne.surf.api.core.config.type.StringOrDefault
import org.spongepowered.configurate.objectmapping.meta.Constraint
import org.spongepowered.configurate.serialize.SerializationException
import java.lang.reflect.Type
Expand All @@ -24,5 +25,14 @@ annotation class MaxLength(val max: Int) {
}
}
}

internal object FactoryStringOrDefault : Constraint.Factory<MaxLength, StringOrDefault?> {
override fun make(data: MaxLength, type: Type): Constraint<StringOrDefault?> = { stringOrDefault ->
val value = stringOrDefault?.value
if (value != null && value.length > data.max) {
throw SerializationException("String is too long: ${value.length}, expected <= ${data.max}")
}
}
Comment thread
twisti-dev marked this conversation as resolved.
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.slne.surf.api.core.config.constraints

import dev.slne.surf.api.core.config.type.number.DoubleOr
import dev.slne.surf.api.core.config.type.number.IntOr
import org.spongepowered.configurate.objectmapping.meta.Constraint
import org.spongepowered.configurate.serialize.SerializationException
import java.lang.reflect.Type
Expand Down Expand Up @@ -35,5 +37,25 @@ annotation class MaxNumber(val max: Double) {
}
}
}

internal object FactoryIntOr : Constraint.Factory<MaxNumber, IntOr?> {
override fun make(data: MaxNumber, type: Type): Constraint<IntOr?> = { intOr ->
val number = intOr?.value

if (number != null && number > data.max) {
Comment thread
twisti-dev marked this conversation as resolved.
throw SerializationException(type, "Number is too big: $number, expected <= ${data.max}")
}
}
}

internal object FactoryDoubleOr : Constraint.Factory<MaxNumber, DoubleOr?> {
override fun make(data: MaxNumber, type: Type): Constraint<DoubleOr?> = { doubleOr ->
val number = doubleOr?.value

if (number != null && number > data.max) {
throw SerializationException(type, "Number is too big: $number, expected <= ${data.max}")
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.slne.surf.api.core.config.constraints

import dev.slne.surf.api.core.config.type.ConfigDuration
import dev.slne.surf.api.core.config.type.DurationOrDisabled
import org.spongepowered.configurate.objectmapping.meta.Constraint
import org.spongepowered.configurate.serialize.SerializationException
import java.lang.reflect.Type
Expand All @@ -25,5 +26,15 @@ annotation class MinDuration(val seconds: Long) {
}
}
}

internal object FactoryDurationOrDisabled : Constraint.Factory<MinDuration, DurationOrDisabled?> {
override fun make(data: MinDuration, type: Type): Constraint<DurationOrDisabled?> = { durationOrDisabled ->
val value = durationOrDisabled?.value

if (value != null && value.inWholeSeconds < data.seconds) {
throw SerializationException("Duration is too short: ${value}, expected >= ${data.seconds}s")
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.slne.surf.api.core.config.constraints

import dev.slne.surf.api.core.config.type.StringOrDefault
import org.spongepowered.configurate.objectmapping.meta.Constraint
import org.spongepowered.configurate.serialize.SerializationException
import java.lang.reflect.Type
Expand All @@ -23,5 +24,15 @@ annotation class MinLength(val min: Int) {
}
}
}

internal object FactoryStringOrDefault : Constraint.Factory<MinLength, StringOrDefault?> {
override fun make(data: MinLength, type: Type): Constraint<StringOrDefault?> = { stringOrDefault ->
val value = stringOrDefault?.value

if (value != null && value.length < data.min) {
throw SerializationException("String is too short: ${value.length}, expected >= ${data.min}")
}
}
Comment thread
twisti-dev marked this conversation as resolved.
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.slne.surf.api.core.config.constraints

import dev.slne.surf.api.core.config.type.number.DoubleOr
import dev.slne.surf.api.core.config.type.number.IntOr
import org.spongepowered.configurate.objectmapping.meta.Constraint
import org.spongepowered.configurate.serialize.SerializationException
import java.lang.reflect.Type
Expand Down Expand Up @@ -35,6 +37,26 @@ annotation class MinNumber(val min: Double) {
}
}
}

internal object FactoryIntOr : Constraint.Factory<MinNumber, IntOr?> {
override fun make(data: MinNumber, type: Type): Constraint<IntOr?> = { intOr ->
val number = intOr?.value

if (number != null && number < data.min) {
Comment thread
twisti-dev marked this conversation as resolved.
throw SerializationException(type, "Number is too small: $number, expected >= ${data.min}")
}
}
}

internal object FactoryDoubleOr : Constraint.Factory<MinNumber, DoubleOr?> {
override fun make(data: MinNumber, type: Type): Constraint<DoubleOr?> = { doubleOr ->
val number = doubleOr?.value

if (number != null && number < data.min) {
throw SerializationException(type, "Number is too small: $number, expected >= ${data.min}")
}
}
}
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.slne.surf.api.core.config.constraints

import dev.slne.surf.api.core.config.type.number.DoubleOr
import dev.slne.surf.api.core.config.type.number.IntOr
import org.spongepowered.configurate.objectmapping.meta.Constraint
import org.spongepowered.configurate.serialize.SerializationException
import java.lang.reflect.Type
Expand All @@ -21,5 +23,25 @@ annotation class NegativeNumber {
}
}
}

internal object FactoryIntOr : Constraint.Factory<NegativeNumber, IntOr?> {
override fun make(data: NegativeNumber, type: Type): Constraint<IntOr?> = { intOr ->
val value = intOr?.value

if (value != null && value >= 0) {
throw SerializationException("Number must be negative: $value, expected < 0")
}
}
}

internal object FactoryDoubleOr : Constraint.Factory<NegativeNumber, DoubleOr?> {
override fun make(data: NegativeNumber, type: Type): Constraint<DoubleOr?> = { doubleOr ->
val value = doubleOr?.value

if (value != null && value >= 0.0) {
throw SerializationException("Number must be negative: $value, expected < 0")
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.slne.surf.api.core.config.constraints

import dev.slne.surf.api.core.config.type.number.DoubleOr
import dev.slne.surf.api.core.config.type.number.IntOr
import org.spongepowered.configurate.objectmapping.meta.Constraint
import org.spongepowered.configurate.serialize.SerializationException
import java.lang.reflect.Type
Expand Down Expand Up @@ -29,7 +31,27 @@ annotation class PositiveNumber {
*/
internal object Factory : Constraint.Factory<PositiveNumber, Number?> {
override fun make(data: PositiveNumber, type: Type): Constraint<Number?> = { number ->
if (number != null && number.toDouble() <= 0) {
if (number != null && number.toDouble() <= 0.0) {
throw SerializationException("Number is not positive: $number, expected > 0")
}
}
}

internal object FactoryIntOr : Constraint.Factory<PositiveNumber, IntOr?> {
override fun make(data: PositiveNumber, type: Type): Constraint<IntOr?> = { intOr ->
val number = intOr?.value

if (number != null && number <= 0) {
throw SerializationException("Number is not positive: $number, expected > 0")
}
}
}

internal object FactoryDoubleOr : Constraint.Factory<PositiveNumber, DoubleOr?> {
override fun make(data: PositiveNumber, type: Type): Constraint<DoubleOr?> = { doubleOr ->
val number = doubleOr?.value

if (number != null && number <= 0.0) {
throw SerializationException("Number is not positive: $number, expected > 0")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.slne.surf.api.core.config.constraints

import dev.slne.surf.api.core.config.type.number.DoubleOr
import dev.slne.surf.api.core.config.type.number.IntOr
import org.spongepowered.configurate.objectmapping.meta.Constraint
import org.spongepowered.configurate.serialize.SerializationException
import java.lang.reflect.Type
Expand Down Expand Up @@ -27,5 +29,30 @@ annotation class Range(val min: Double, val max: Double) {
}
}
}

internal object FactoryIntOr : Constraint.Factory<Range, IntOr?> {
override fun make(data: Range, type: Type): Constraint<IntOr?> = { intOr ->
val value = intOr?.value

if (value != null) {
val double = value.toDouble()
if (double < data.min || double > data.max) {
throw SerializationException("Number is out of range: $value, expected ${data.min}..${data.max}")
}
}
}
}

internal object FactoryDoubleOr : Constraint.Factory<Range, DoubleOr?> {
override fun make(data: Range, type: Type): Constraint<DoubleOr?> = { doubleOr ->
val value = doubleOr?.value

if (value != null) {
if (value < data.min || value > data.max) {
throw SerializationException("Number is out of range: $value, expected ${data.min}..${data.max}")
}
}
}
}
}
}
Loading
Loading