diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt index 835b98f6..997026f1 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt @@ -223,7 +223,7 @@ class Starbound : ISBFileLocator { registerTypeAdapterFactory(InventoryIcon.Factory(pathStack)) registerTypeAdapterFactory(IArmorItemDefinition.Frames.Factory) - registerTypeAdapterFactory(DirectAssetReferenceFactory(pathStack)) + registerTypeAdapterFactory(AssetPathFactory(pathStack)) registerTypeAdapterFactory(ImageReference.Factory({ atlasRegistry.get(it) }, pathStack)) registerTypeAdapterFactory(AssetReferenceFactory(pathStack, this@Starbound)) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/DirectAssetReference.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/AssetPath.kt similarity index 63% rename from src/main/kotlin/ru/dbotthepony/kstarbound/defs/DirectAssetReference.kt rename to src/main/kotlin/ru/dbotthepony/kstarbound/defs/AssetPath.kt index 54db63f9..ec26dd6b 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/DirectAssetReference.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/AssetPath.kt @@ -11,25 +11,25 @@ import ru.dbotthepony.kstarbound.util.PathStack /** * Путь как указан в JSON + Абсолютный путь * - * @see DirectAssetReference + * @see AssetPath */ -class DirectAssetReferenceFactory(val remapper: PathStack) : TypeAdapterFactory { +class AssetPathFactory(val remapper: PathStack) : TypeAdapterFactory { override fun create(gson: Gson, type: TypeToken): TypeAdapter? { - if (type.rawType == DirectAssetReference::class.java) { - return object : TypeAdapter() { + if (type.rawType == AssetPath::class.java) { + return object : TypeAdapter() { private val strings = gson.getAdapter(String::class.java) - override fun write(out: JsonWriter, value: DirectAssetReference?) { + override fun write(out: JsonWriter, value: AssetPath?) { if (value == null) out.nullValue() else out.value(value.fullPath) } - override fun read(`in`: JsonReader): DirectAssetReference? { + override fun read(`in`: JsonReader): AssetPath? { val path = strings.read(`in`) ?: return null if (path == "") return null - return DirectAssetReference(path, remapper.remap(path)) + return AssetPath(path, remapper.remap(path)) } } as TypeAdapter } @@ -41,4 +41,4 @@ class DirectAssetReferenceFactory(val remapper: PathStack) : TypeAdapterFactory /** * Путь как указан в JSON + Абсолютный путь */ -data class DirectAssetReference(val path: String, val fullPath: String) +data class AssetPath(val path: String, val fullPath: String) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/IScriptable.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/IScriptable.kt index 362782c0..d01117f7 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/IScriptable.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/IScriptable.kt @@ -9,7 +9,7 @@ interface IScriptable { /** * Lua скрипты для выполнения */ - val scripts: List + val scripts: List /** * Через какое количество тиков вызывать обновления скриптов @@ -18,7 +18,7 @@ interface IScriptable { @JsonFactory data class Impl( - override val scripts: ImmutableList = ImmutableList.of(), + override val scripts: ImmutableList = ImmutableList.of(), override val scriptDelta: Int = 1 ) : IScriptable } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/StatusEffectDefinition.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/StatusEffectDefinition.kt index 6d35cead..66624c43 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/StatusEffectDefinition.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/StatusEffectDefinition.kt @@ -12,7 +12,7 @@ data class StatusEffectDefinition( val blockingStat: String? = null, val label: String? = null, val icon: ImageReference? = null, - override val scripts: ImmutableList = ImmutableList.of(), + override val scripts: ImmutableList = ImmutableList.of(), override val scriptDelta: Int = 1, val animationConfig: AssetReference? = null, ) : IScriptable diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/image/ImageReference.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/image/ImageReference.kt index 110ea88a..2ec813f8 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/image/ImageReference.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/image/ImageReference.kt @@ -8,7 +8,7 @@ import com.google.gson.reflect.TypeToken import com.google.gson.stream.JsonReader import com.google.gson.stream.JsonToken import com.google.gson.stream.JsonWriter -import ru.dbotthepony.kstarbound.defs.DirectAssetReference +import ru.dbotthepony.kstarbound.defs.AssetPath import ru.dbotthepony.kstarbound.util.PathStack import ru.dbotthepony.kstarbound.util.SBPattern @@ -16,7 +16,7 @@ import ru.dbotthepony.kstarbound.util.SBPattern * @see [AtlasConfiguration.Registry.get] */ class ImageReference private constructor( - val raw: DirectAssetReference, + val raw: AssetPath, val imagePath: SBPattern, val spritePath: SBPattern?, val atlas: AtlasConfiguration?, @@ -111,9 +111,9 @@ class ImageReference private constructor( if (imagePath.isPlainString) { val remapped = remapper.remap(split[0]) - return ImageReference(DirectAssetReference(path, remapper.remap(path)), SBPattern.raw(remapped), spritePath, atlasLocator.invoke(remapped), atlasLocator) + return ImageReference(AssetPath(path, remapper.remap(path)), SBPattern.raw(remapped), spritePath, atlasLocator.invoke(remapped), atlasLocator) } else { - return ImageReference(DirectAssetReference(path, path), imagePath, spritePath, null, atlasLocator) + return ImageReference(AssetPath(path, path), imagePath, spritePath, null, atlasLocator) } } } as TypeAdapter @@ -124,6 +124,6 @@ class ImageReference private constructor( } companion object { - val NEVER = ImageReference(DirectAssetReference("", ""), SBPattern.EMPTY, null, null) { null } + val NEVER = ImageReference(AssetPath("", ""), SBPattern.EMPTY, null, null) { null } } } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/TreasurePoolDefinition.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/TreasurePoolDefinition.kt index a2125356..2dfbd7e0 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/TreasurePoolDefinition.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/TreasurePoolDefinition.kt @@ -65,10 +65,10 @@ class TreasurePoolDefinition(pieces: List) { for (round in 0 until rounds) { for (entry in fill) { - entry.run(left = { + entry.map({ val stack = it.makeStack() if (stack.isNotEmpty) result.add(stack) - }, right = { + }, { it.value?.value?.evaluate(random, actualLevel) }) } @@ -78,10 +78,10 @@ class TreasurePoolDefinition(pieces: List) { for (entry in pool) { if (chosen <= entry.weight) { - entry.treasure.run(left = { + entry.treasure.map({ val stack = it.makeStack() if (stack.isNotEmpty) result.add(stack) - }, right = { + }, { it.value?.value?.evaluate(random, actualLevel) }) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/impl/ArmorItemDefinition.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/impl/ArmorItemDefinition.kt index d8aa7a79..40a06b07 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/impl/ArmorItemDefinition.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/impl/ArmorItemDefinition.kt @@ -1,12 +1,10 @@ package ru.dbotthepony.kstarbound.defs.item.impl import com.google.common.collect.ImmutableList -import ru.dbotthepony.kstarbound.defs.DirectAssetReference import ru.dbotthepony.kstarbound.defs.IScriptable import ru.dbotthepony.kstarbound.defs.item.api.IArmorItemDefinition import ru.dbotthepony.kstarbound.defs.item.LeveledStatusEffect import ru.dbotthepony.kstarbound.defs.item.api.IItemDefinition -import ru.dbotthepony.kstarbound.io.json.builder.JsonBuilder import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory import ru.dbotthepony.kstarbound.io.json.builder.JsonFlat diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/monster/MonsterTypeDefinition.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/monster/MonsterTypeDefinition.kt index 0e705852..dda58afa 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/monster/MonsterTypeDefinition.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/monster/MonsterTypeDefinition.kt @@ -4,7 +4,6 @@ import com.google.common.collect.ImmutableList import com.google.common.collect.ImmutableMap import com.google.common.collect.ImmutableSet import ru.dbotthepony.kstarbound.defs.AssetReference -import ru.dbotthepony.kstarbound.defs.DirectAssetReference import ru.dbotthepony.kstarbound.defs.IScriptable import ru.dbotthepony.kstarbound.defs.IThingWithDescription import ru.dbotthepony.kstarbound.defs.MovementParameters diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/CompanionsConfig.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/CompanionsConfig.kt index a1b75cba..323ca27c 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/CompanionsConfig.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/CompanionsConfig.kt @@ -2,14 +2,14 @@ package ru.dbotthepony.kstarbound.defs.player import com.google.common.collect.ImmutableList import com.google.common.collect.ImmutableMap -import ru.dbotthepony.kstarbound.defs.DirectAssetReference +import ru.dbotthepony.kstarbound.defs.AssetPath import ru.dbotthepony.kstarbound.defs.IScriptable import ru.dbotthepony.kstarbound.util.SBPattern import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory @JsonFactory data class CompanionsConfig( - override val scripts: ImmutableList, + override val scripts: ImmutableList, override val scriptDelta: Int, val activePodLimit: Int, diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/DeploymentConfig.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/DeploymentConfig.kt index 0543b1e0..d6fdd86d 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/DeploymentConfig.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/DeploymentConfig.kt @@ -2,7 +2,7 @@ package ru.dbotthepony.kstarbound.defs.player import com.google.common.collect.ImmutableList import com.google.common.collect.ImmutableMap -import ru.dbotthepony.kstarbound.defs.DirectAssetReference +import ru.dbotthepony.kstarbound.defs.AssetPath import ru.dbotthepony.kstarbound.defs.IScriptable import ru.dbotthepony.kstarbound.defs.RegistryReference import ru.dbotthepony.kstarbound.defs.item.api.IItemDefinition @@ -10,7 +10,7 @@ import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory @JsonFactory data class DeploymentConfig( - override val scripts: ImmutableList, + override val scripts: ImmutableList, override val scriptDelta: Int, val starterMechSet: ImmutableMap>, diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/TechDefinition.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/TechDefinition.kt index bd5fe913..3a3e6d46 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/TechDefinition.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/TechDefinition.kt @@ -1,7 +1,7 @@ package ru.dbotthepony.kstarbound.defs.player import com.google.common.collect.ImmutableList -import ru.dbotthepony.kstarbound.defs.DirectAssetReference +import ru.dbotthepony.kstarbound.defs.AssetPath import ru.dbotthepony.kstarbound.defs.IScriptable import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory @@ -11,5 +11,5 @@ data class TechDefinition( val type: String, val chipCost: Int, override val scriptDelta: Int = 1, - override val scripts: ImmutableList + override val scripts: ImmutableList ) : IScriptable diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/quest/QuestTemplate.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/quest/QuestTemplate.kt index 9c4bb113..6efaa8ee 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/quest/QuestTemplate.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/quest/QuestTemplate.kt @@ -2,7 +2,7 @@ package ru.dbotthepony.kstarbound.defs.quest import com.google.common.collect.ImmutableSet import com.google.gson.JsonObject -import ru.dbotthepony.kstarbound.defs.DirectAssetReference +import ru.dbotthepony.kstarbound.defs.AssetPath import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory @JsonFactory @@ -10,7 +10,7 @@ data class QuestTemplate( val id: String, val prerequisites: ImmutableSet = ImmutableSet.of(), val requiredItems: ImmutableSet = ImmutableSet.of(), - val script: DirectAssetReference, + val script: AssetPath, val updateDelta: Int = 10, val moneyRange: LongRange, val scriptConfig: JsonObject = JsonObject() diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/io/json/EitherTypeAdapter.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/io/json/EitherTypeAdapter.kt index 79f83c19..b1446d32 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/io/json/EitherTypeAdapter.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/io/json/EitherTypeAdapter.kt @@ -32,7 +32,7 @@ object EitherTypeAdapter : TypeAdapterFactory { if (value == null) out.nullValue() else - value.run({ leftAdapter.write(out, it) }, { rightAdapter.write(out, it) }) + value.map({ leftAdapter.write(out, it) }, { rightAdapter.write(out, it) }) } override fun read(`in`: JsonReader): Either? { diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/util/Either.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/util/Either.kt index 0cf8051f..5af51a08 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/util/Either.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/util/Either.kt @@ -7,35 +7,81 @@ import ru.dbotthepony.kstarbound.io.json.EitherTypeAdapter * * JSON адаптер реализуется через [EitherTypeAdapter] */ -data class Either(val left: L?, val right: R?) { - init { - require(left != null || right != null) { "Both inputs are null" } - require(!(left != null && right != null)) { "Both inputs are not null" } +sealed class Either { + class Left(val value: L) : Either() { + override val isLeft: Boolean + get() = true + override val isRight: Boolean + get() = false + + override fun map(left: (L) -> T, right: (R) -> T): T { + return left.invoke(this.value) + } + + override fun left(): L { + return value + } + + override fun right(): R { + throw NoSuchElementException() + } } - inline fun run(left: (L) -> Unit, right: (R) -> Unit) { - if (this.left != null) - left.invoke(this.left) - else - right.invoke(this.right!!) + class Right(val value: R) : Either() { + override val isLeft: Boolean + get() = false + override val isRight: Boolean + get() = true + + override fun map(left: (L) -> T, right: (R) -> T): T { + return right.invoke(value) + } + + override fun left(): L { + throw NoSuchElementException() + } + + override fun right(): R { + return value + } } - inline fun reduce(left: (L) -> T, right: (R) -> T): T { - return if (this.left != null) - left.invoke(this.left) + abstract val isLeft: Boolean + abstract val isRight: Boolean + + abstract fun map(left: (L) -> T, right: (R) -> T): T + + /** + * @throws NoSuchElementException + */ + abstract fun left(): L + + /** + * @throws NoSuchElementException + */ + abstract fun right(): R + + inline fun leftOrElse(orElse: () -> L): L { + if (isLeft) + return left() else - right.invoke(this.right!!) + return orElse.invoke() + } + + inline fun rightOrElse(orElse: () -> R): R { + if (isRight) + return right() + else + return orElse.invoke() } companion object { - @JvmStatic - fun left(value: L): Either { - return Either(left = value, right = null) + fun left(value: L): Either { + return Left(value) } - @JvmStatic - fun right(value: R): Either { - return Either(left = null, right = value) + fun right(value: R): Either { + return Right(value) } } }