Some renamings and make Either be able to handle nulls
This commit is contained in:
parent
6397637538
commit
528a2e6c59
@ -223,7 +223,7 @@ class Starbound : ISBFileLocator {
|
|||||||
registerTypeAdapterFactory(InventoryIcon.Factory(pathStack))
|
registerTypeAdapterFactory(InventoryIcon.Factory(pathStack))
|
||||||
|
|
||||||
registerTypeAdapterFactory(IArmorItemDefinition.Frames.Factory)
|
registerTypeAdapterFactory(IArmorItemDefinition.Frames.Factory)
|
||||||
registerTypeAdapterFactory(DirectAssetReferenceFactory(pathStack))
|
registerTypeAdapterFactory(AssetPathFactory(pathStack))
|
||||||
registerTypeAdapterFactory(ImageReference.Factory({ atlasRegistry.get(it) }, pathStack))
|
registerTypeAdapterFactory(ImageReference.Factory({ atlasRegistry.get(it) }, pathStack))
|
||||||
|
|
||||||
registerTypeAdapterFactory(AssetReferenceFactory(pathStack, this@Starbound))
|
registerTypeAdapterFactory(AssetReferenceFactory(pathStack, this@Starbound))
|
||||||
|
@ -11,25 +11,25 @@ import ru.dbotthepony.kstarbound.util.PathStack
|
|||||||
/**
|
/**
|
||||||
* Путь как указан в JSON + Абсолютный путь
|
* Путь как указан в JSON + Абсолютный путь
|
||||||
*
|
*
|
||||||
* @see DirectAssetReference
|
* @see AssetPath
|
||||||
*/
|
*/
|
||||||
class DirectAssetReferenceFactory(val remapper: PathStack) : TypeAdapterFactory {
|
class AssetPathFactory(val remapper: PathStack) : TypeAdapterFactory {
|
||||||
override fun <T : Any?> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
|
override fun <T : Any?> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
|
||||||
if (type.rawType == DirectAssetReference::class.java) {
|
if (type.rawType == AssetPath::class.java) {
|
||||||
return object : TypeAdapter<DirectAssetReference>() {
|
return object : TypeAdapter<AssetPath>() {
|
||||||
private val strings = gson.getAdapter(String::class.java)
|
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)
|
if (value == null)
|
||||||
out.nullValue()
|
out.nullValue()
|
||||||
else
|
else
|
||||||
out.value(value.fullPath)
|
out.value(value.fullPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun read(`in`: JsonReader): DirectAssetReference? {
|
override fun read(`in`: JsonReader): AssetPath? {
|
||||||
val path = strings.read(`in`) ?: return null
|
val path = strings.read(`in`) ?: return null
|
||||||
if (path == "") return null
|
if (path == "") return null
|
||||||
return DirectAssetReference(path, remapper.remap(path))
|
return AssetPath(path, remapper.remap(path))
|
||||||
}
|
}
|
||||||
} as TypeAdapter<T>
|
} as TypeAdapter<T>
|
||||||
}
|
}
|
||||||
@ -41,4 +41,4 @@ class DirectAssetReferenceFactory(val remapper: PathStack) : TypeAdapterFactory
|
|||||||
/**
|
/**
|
||||||
* Путь как указан в JSON + Абсолютный путь
|
* Путь как указан в JSON + Абсолютный путь
|
||||||
*/
|
*/
|
||||||
data class DirectAssetReference(val path: String, val fullPath: String)
|
data class AssetPath(val path: String, val fullPath: String)
|
@ -9,7 +9,7 @@ interface IScriptable {
|
|||||||
/**
|
/**
|
||||||
* Lua скрипты для выполнения
|
* Lua скрипты для выполнения
|
||||||
*/
|
*/
|
||||||
val scripts: List<DirectAssetReference>
|
val scripts: List<AssetPath>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Через какое количество тиков вызывать обновления скриптов
|
* Через какое количество тиков вызывать обновления скриптов
|
||||||
@ -18,7 +18,7 @@ interface IScriptable {
|
|||||||
|
|
||||||
@JsonFactory
|
@JsonFactory
|
||||||
data class Impl(
|
data class Impl(
|
||||||
override val scripts: ImmutableList<DirectAssetReference> = ImmutableList.of(),
|
override val scripts: ImmutableList<AssetPath> = ImmutableList.of(),
|
||||||
override val scriptDelta: Int = 1
|
override val scriptDelta: Int = 1
|
||||||
) : IScriptable
|
) : IScriptable
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ data class StatusEffectDefinition(
|
|||||||
val blockingStat: String? = null,
|
val blockingStat: String? = null,
|
||||||
val label: String? = null,
|
val label: String? = null,
|
||||||
val icon: ImageReference? = null,
|
val icon: ImageReference? = null,
|
||||||
override val scripts: ImmutableList<DirectAssetReference> = ImmutableList.of(),
|
override val scripts: ImmutableList<AssetPath> = ImmutableList.of(),
|
||||||
override val scriptDelta: Int = 1,
|
override val scriptDelta: Int = 1,
|
||||||
val animationConfig: AssetReference<AnimationDefinition>? = null,
|
val animationConfig: AssetReference<AnimationDefinition>? = null,
|
||||||
) : IScriptable
|
) : IScriptable
|
||||||
|
@ -8,7 +8,7 @@ import com.google.gson.reflect.TypeToken
|
|||||||
import com.google.gson.stream.JsonReader
|
import com.google.gson.stream.JsonReader
|
||||||
import com.google.gson.stream.JsonToken
|
import com.google.gson.stream.JsonToken
|
||||||
import com.google.gson.stream.JsonWriter
|
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.PathStack
|
||||||
import ru.dbotthepony.kstarbound.util.SBPattern
|
import ru.dbotthepony.kstarbound.util.SBPattern
|
||||||
|
|
||||||
@ -16,7 +16,7 @@ import ru.dbotthepony.kstarbound.util.SBPattern
|
|||||||
* @see [AtlasConfiguration.Registry.get]
|
* @see [AtlasConfiguration.Registry.get]
|
||||||
*/
|
*/
|
||||||
class ImageReference private constructor(
|
class ImageReference private constructor(
|
||||||
val raw: DirectAssetReference,
|
val raw: AssetPath,
|
||||||
val imagePath: SBPattern,
|
val imagePath: SBPattern,
|
||||||
val spritePath: SBPattern?,
|
val spritePath: SBPattern?,
|
||||||
val atlas: AtlasConfiguration?,
|
val atlas: AtlasConfiguration?,
|
||||||
@ -111,9 +111,9 @@ class ImageReference private constructor(
|
|||||||
|
|
||||||
if (imagePath.isPlainString) {
|
if (imagePath.isPlainString) {
|
||||||
val remapped = remapper.remap(split[0])
|
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 {
|
} else {
|
||||||
return ImageReference(DirectAssetReference(path, path), imagePath, spritePath, null, atlasLocator)
|
return ImageReference(AssetPath(path, path), imagePath, spritePath, null, atlasLocator)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} as TypeAdapter<T>
|
} as TypeAdapter<T>
|
||||||
@ -124,6 +124,6 @@ class ImageReference private constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val NEVER = ImageReference(DirectAssetReference("", ""), SBPattern.EMPTY, null, null) { null }
|
val NEVER = ImageReference(AssetPath("", ""), SBPattern.EMPTY, null, null) { null }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,10 +65,10 @@ class TreasurePoolDefinition(pieces: List<Piece>) {
|
|||||||
|
|
||||||
for (round in 0 until rounds) {
|
for (round in 0 until rounds) {
|
||||||
for (entry in fill) {
|
for (entry in fill) {
|
||||||
entry.run(left = {
|
entry.map({
|
||||||
val stack = it.makeStack()
|
val stack = it.makeStack()
|
||||||
if (stack.isNotEmpty) result.add(stack)
|
if (stack.isNotEmpty) result.add(stack)
|
||||||
}, right = {
|
}, {
|
||||||
it.value?.value?.evaluate(random, actualLevel)
|
it.value?.value?.evaluate(random, actualLevel)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -78,10 +78,10 @@ class TreasurePoolDefinition(pieces: List<Piece>) {
|
|||||||
|
|
||||||
for (entry in pool) {
|
for (entry in pool) {
|
||||||
if (chosen <= entry.weight) {
|
if (chosen <= entry.weight) {
|
||||||
entry.treasure.run(left = {
|
entry.treasure.map({
|
||||||
val stack = it.makeStack()
|
val stack = it.makeStack()
|
||||||
if (stack.isNotEmpty) result.add(stack)
|
if (stack.isNotEmpty) result.add(stack)
|
||||||
}, right = {
|
}, {
|
||||||
it.value?.value?.evaluate(random, actualLevel)
|
it.value?.value?.evaluate(random, actualLevel)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
package ru.dbotthepony.kstarbound.defs.item.impl
|
package ru.dbotthepony.kstarbound.defs.item.impl
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList
|
import com.google.common.collect.ImmutableList
|
||||||
import ru.dbotthepony.kstarbound.defs.DirectAssetReference
|
|
||||||
import ru.dbotthepony.kstarbound.defs.IScriptable
|
import ru.dbotthepony.kstarbound.defs.IScriptable
|
||||||
import ru.dbotthepony.kstarbound.defs.item.api.IArmorItemDefinition
|
import ru.dbotthepony.kstarbound.defs.item.api.IArmorItemDefinition
|
||||||
import ru.dbotthepony.kstarbound.defs.item.LeveledStatusEffect
|
import ru.dbotthepony.kstarbound.defs.item.LeveledStatusEffect
|
||||||
import ru.dbotthepony.kstarbound.defs.item.api.IItemDefinition
|
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.JsonFactory
|
||||||
import ru.dbotthepony.kstarbound.io.json.builder.JsonFlat
|
import ru.dbotthepony.kstarbound.io.json.builder.JsonFlat
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ import com.google.common.collect.ImmutableList
|
|||||||
import com.google.common.collect.ImmutableMap
|
import com.google.common.collect.ImmutableMap
|
||||||
import com.google.common.collect.ImmutableSet
|
import com.google.common.collect.ImmutableSet
|
||||||
import ru.dbotthepony.kstarbound.defs.AssetReference
|
import ru.dbotthepony.kstarbound.defs.AssetReference
|
||||||
import ru.dbotthepony.kstarbound.defs.DirectAssetReference
|
|
||||||
import ru.dbotthepony.kstarbound.defs.IScriptable
|
import ru.dbotthepony.kstarbound.defs.IScriptable
|
||||||
import ru.dbotthepony.kstarbound.defs.IThingWithDescription
|
import ru.dbotthepony.kstarbound.defs.IThingWithDescription
|
||||||
import ru.dbotthepony.kstarbound.defs.MovementParameters
|
import ru.dbotthepony.kstarbound.defs.MovementParameters
|
||||||
|
@ -2,14 +2,14 @@ package ru.dbotthepony.kstarbound.defs.player
|
|||||||
|
|
||||||
import com.google.common.collect.ImmutableList
|
import com.google.common.collect.ImmutableList
|
||||||
import com.google.common.collect.ImmutableMap
|
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.IScriptable
|
||||||
import ru.dbotthepony.kstarbound.util.SBPattern
|
import ru.dbotthepony.kstarbound.util.SBPattern
|
||||||
import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory
|
import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory
|
||||||
|
|
||||||
@JsonFactory
|
@JsonFactory
|
||||||
data class CompanionsConfig(
|
data class CompanionsConfig(
|
||||||
override val scripts: ImmutableList<DirectAssetReference>,
|
override val scripts: ImmutableList<AssetPath>,
|
||||||
override val scriptDelta: Int,
|
override val scriptDelta: Int,
|
||||||
|
|
||||||
val activePodLimit: Int,
|
val activePodLimit: Int,
|
||||||
|
@ -2,7 +2,7 @@ package ru.dbotthepony.kstarbound.defs.player
|
|||||||
|
|
||||||
import com.google.common.collect.ImmutableList
|
import com.google.common.collect.ImmutableList
|
||||||
import com.google.common.collect.ImmutableMap
|
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.IScriptable
|
||||||
import ru.dbotthepony.kstarbound.defs.RegistryReference
|
import ru.dbotthepony.kstarbound.defs.RegistryReference
|
||||||
import ru.dbotthepony.kstarbound.defs.item.api.IItemDefinition
|
import ru.dbotthepony.kstarbound.defs.item.api.IItemDefinition
|
||||||
@ -10,7 +10,7 @@ import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory
|
|||||||
|
|
||||||
@JsonFactory
|
@JsonFactory
|
||||||
data class DeploymentConfig(
|
data class DeploymentConfig(
|
||||||
override val scripts: ImmutableList<DirectAssetReference>,
|
override val scripts: ImmutableList<AssetPath>,
|
||||||
override val scriptDelta: Int,
|
override val scriptDelta: Int,
|
||||||
|
|
||||||
val starterMechSet: ImmutableMap<String, RegistryReference<IItemDefinition>>,
|
val starterMechSet: ImmutableMap<String, RegistryReference<IItemDefinition>>,
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package ru.dbotthepony.kstarbound.defs.player
|
package ru.dbotthepony.kstarbound.defs.player
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList
|
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.defs.IScriptable
|
||||||
import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory
|
import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory
|
||||||
|
|
||||||
@ -11,5 +11,5 @@ data class TechDefinition(
|
|||||||
val type: String,
|
val type: String,
|
||||||
val chipCost: Int,
|
val chipCost: Int,
|
||||||
override val scriptDelta: Int = 1,
|
override val scriptDelta: Int = 1,
|
||||||
override val scripts: ImmutableList<DirectAssetReference>
|
override val scripts: ImmutableList<AssetPath>
|
||||||
) : IScriptable
|
) : IScriptable
|
||||||
|
@ -2,7 +2,7 @@ package ru.dbotthepony.kstarbound.defs.quest
|
|||||||
|
|
||||||
import com.google.common.collect.ImmutableSet
|
import com.google.common.collect.ImmutableSet
|
||||||
import com.google.gson.JsonObject
|
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
|
import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory
|
||||||
|
|
||||||
@JsonFactory
|
@JsonFactory
|
||||||
@ -10,7 +10,7 @@ data class QuestTemplate(
|
|||||||
val id: String,
|
val id: String,
|
||||||
val prerequisites: ImmutableSet<String> = ImmutableSet.of(),
|
val prerequisites: ImmutableSet<String> = ImmutableSet.of(),
|
||||||
val requiredItems: ImmutableSet<String> = ImmutableSet.of(),
|
val requiredItems: ImmutableSet<String> = ImmutableSet.of(),
|
||||||
val script: DirectAssetReference,
|
val script: AssetPath,
|
||||||
val updateDelta: Int = 10,
|
val updateDelta: Int = 10,
|
||||||
val moneyRange: LongRange,
|
val moneyRange: LongRange,
|
||||||
val scriptConfig: JsonObject = JsonObject()
|
val scriptConfig: JsonObject = JsonObject()
|
||||||
|
@ -32,7 +32,7 @@ object EitherTypeAdapter : TypeAdapterFactory {
|
|||||||
if (value == null)
|
if (value == null)
|
||||||
out.nullValue()
|
out.nullValue()
|
||||||
else
|
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<Any, Any>? {
|
override fun read(`in`: JsonReader): Either<Any, Any>? {
|
||||||
|
@ -7,35 +7,81 @@ import ru.dbotthepony.kstarbound.io.json.EitherTypeAdapter
|
|||||||
*
|
*
|
||||||
* JSON адаптер реализуется через [EitherTypeAdapter]
|
* JSON адаптер реализуется через [EitherTypeAdapter]
|
||||||
*/
|
*/
|
||||||
data class Either<L : Any, R : Any>(val left: L?, val right: R?) {
|
sealed class Either<L, R> {
|
||||||
init {
|
class Left<L, R>(val value: L) : Either<L, R>() {
|
||||||
require(left != null || right != null) { "Both inputs are null" }
|
override val isLeft: Boolean
|
||||||
require(!(left != null && right != null)) { "Both inputs are not null" }
|
get() = true
|
||||||
|
override val isRight: Boolean
|
||||||
|
get() = false
|
||||||
|
|
||||||
|
override fun <T> 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) {
|
class Right<L, R>(val value: R) : Either<L, R>() {
|
||||||
if (this.left != null)
|
override val isLeft: Boolean
|
||||||
left.invoke(this.left)
|
get() = false
|
||||||
else
|
override val isRight: Boolean
|
||||||
right.invoke(this.right!!)
|
get() = true
|
||||||
|
|
||||||
|
override fun <T> 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 <T> reduce(left: (L) -> T, right: (R) -> T): T {
|
abstract val isLeft: Boolean
|
||||||
return if (this.left != null)
|
abstract val isRight: Boolean
|
||||||
left.invoke(this.left)
|
|
||||||
|
abstract fun <T> 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
|
else
|
||||||
right.invoke(this.right!!)
|
return orElse.invoke()
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun rightOrElse(orElse: () -> R): R {
|
||||||
|
if (isRight)
|
||||||
|
return right()
|
||||||
|
else
|
||||||
|
return orElse.invoke()
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@JvmStatic
|
fun <L, R> left(value: L): Either<L, R> {
|
||||||
fun <L : Any, R : Any> left(value: L): Either<L, R> {
|
return Left(value)
|
||||||
return Either(left = value, right = null)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
fun <L, R> right(value: R): Either<L, R> {
|
||||||
fun <L : Any, R : Any> right(value: R): Either<L, R> {
|
return Right(value)
|
||||||
return Either(left = null, right = value)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user