From bd690ee56cea3277b8637878f1dffe7d6e4879ae Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 8 Feb 2023 14:51:15 +0700 Subject: [PATCH] =?UTF-8?q?=D1=85=D0=B7,=20=D0=B2=D1=81=D1=91=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=BD=D0=BE=20=D0=BD=D0=B0=D0=B4=D0=BE=20=D1=87?= =?UTF-8?q?=D1=82=D0=BE=20=D1=82=D0=BE=20=D0=B4=D1=80=D1=83=D0=B3=D0=BE?= =?UTF-8?q?=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/dbotthepony/kstarbound/Starbound.kt | 4 +- .../kstarbound/defs/DefinitionBuilder.kt | 107 ++++++++++++++++++ .../kstarbound/defs/DirectAssetReference.kt | 8 ++ .../kstarbound/defs/MaterialReference.kt | 36 ------ .../defs/item/ArmorItemDefinition.kt | 35 ------ .../defs/item/ArmorItemPrototype.kt | 72 +++--------- .../defs/item/CurrencyItemDefinition.kt | 32 ------ .../defs/item/CurrencyItemPrototype.kt | 38 +------ .../defs/item/FlashlightDefinition.kt | 33 ------ .../defs/item/FlashlightPrototype.kt | 37 +----- .../defs/item/HarvestingToolDefinition.kt | 36 ------ .../defs/item/HarvestingToolPrototype.kt | 46 ++------ .../defs/item/IArmorItemDefinition.kt | 30 ++--- .../defs/item/ICurrencyItemDefinition.kt | 25 ---- .../kstarbound/defs/item/IItemDefinition.kt | 28 ++++- .../defs/item/ILeveledStatusEffect.kt | 15 +-- .../kstarbound/defs/item/ILiquidItem.kt | 4 +- .../kstarbound/defs/item/IMaterialItem.kt | 4 +- .../kstarbound/defs/item/ItemDefinition.kt | 27 ----- .../kstarbound/defs/item/ItemPrototype.kt | 76 ++++++------- .../defs/item/LiquidItemDefinition.kt | 27 ----- .../defs/item/LiquidItemPrototype.kt | 45 ++------ .../defs/item/MaterialItemDefinition.kt | 27 ----- .../defs/item/MaterialItemPrototype.kt | 45 ++------ .../io/json/builder/BuilderAdapter.kt | 8 +- .../kstarbound/util/INotNullDelegate.kt | 5 + .../dbotthepony/kstarbound/util/NotNullVar.kt | 4 +- .../kstarbound/world/entities/ItemEntity.kt | 1 - 28 files changed, 273 insertions(+), 582 deletions(-) create mode 100644 src/main/kotlin/ru/dbotthepony/kstarbound/defs/DefinitionBuilder.kt delete mode 100644 src/main/kotlin/ru/dbotthepony/kstarbound/defs/MaterialReference.kt delete mode 100644 src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ArmorItemDefinition.kt delete mode 100644 src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/CurrencyItemDefinition.kt delete mode 100644 src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/FlashlightDefinition.kt delete mode 100644 src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/HarvestingToolDefinition.kt delete mode 100644 src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ItemDefinition.kt delete mode 100644 src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/LiquidItemDefinition.kt delete mode 100644 src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/MaterialItemDefinition.kt create mode 100644 src/main/kotlin/ru/dbotthepony/kstarbound/util/INotNullDelegate.kt diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt index 80cc11fd..ef33aed3 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt @@ -162,8 +162,6 @@ class Starbound : ISBFileLocator { registerTypeAdapter(JsonFunction.Companion) // Общее - registerTypeAdapterFactory(LeveledStatusEffect.ADAPTER) - registerTypeAdapter(MaterialReference.Companion) registerTypeAdapterFactory(ThingDescription.Factory(stringInterner)) registerTypeAdapter(EnumAdapter(DamageType::class, default = DamageType.NORMAL)) @@ -172,7 +170,7 @@ class Starbound : ISBFileLocator { registerTypeAdapter(spriteRegistry) registerTypeAdapterFactory(IItemDefinition.InventoryIcon.Factory(pathStack, spriteRegistry)) - registerTypeAdapterFactory(IArmorItemDefinition.ArmorFrames.Factory(pathStack, this@Starbound::atlasRegistry)) + registerTypeAdapterFactory(IArmorItemDefinition.Frames.Factory(pathStack, this@Starbound::atlasRegistry)) registerTypeAdapterFactory(DirectAssetReferenceFactory(pathStack)) registerTypeAdapter(ImageReference.Adapter(pathStack, this@Starbound::atlasRegistry)) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/DefinitionBuilder.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/DefinitionBuilder.kt new file mode 100644 index 00000000..43052108 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/DefinitionBuilder.kt @@ -0,0 +1,107 @@ +package ru.dbotthepony.kstarbound.defs + +import ru.dbotthepony.kstarbound.io.json.builder.JsonIgnoreProperty +import ru.dbotthepony.kstarbound.util.INotNullDelegate +import kotlin.properties.ReadWriteProperty +import kotlin.reflect.KProperty + +abstract class DefinitionBuilder { + interface IProperty { + fun checkAndThrow() + } + + inner class Nullable(private var value: V? = null) : ReadWriteProperty, IProperty { + override fun getValue(thisRef: Any?, property: KProperty<*>): V? { + return value + } + + override fun setValue(thisRef: Any?, property: KProperty<*>, value: V?) { + if (isFrozen) throw IllegalStateException("$thisRef is frozen!") + this.value = value + } + + override fun checkAndThrow() { + // no op + } + + override fun equals(other: Any?): Boolean { + return other is Nullable<*> && other.value == value + } + + override fun hashCode(): Int { + return value?.hashCode() ?: 0 + } + + override fun toString(): String { + return "Nullable[$value]" + } + } + + inner class NotNull(private var value: V? = null) : ReadWriteProperty, IProperty, INotNullDelegate { + override val isPresent: Boolean + get() = value != null + + fun getNullable(): V? { + return value + } + + override fun getValue(thisRef: Any?, property: KProperty<*>): V { + return checkNotNull(value) {"${property.name} was not initialized"} + } + + override fun setValue(thisRef: Any?, property: KProperty<*>, value: V) { + if (isFrozen) throw IllegalStateException("$thisRef is frozen!") + this.value = value + } + + override fun checkAndThrow() { + if (value == null) { + throw NullPointerException("Value was not initialized") + } + } + + override fun equals(other: Any?): Boolean { + return other is NotNull<*> && other.value == value + } + + override fun hashCode(): Int { + return value?.hashCode() ?: 0 + } + + override fun toString(): String { + return "NotNull[$value]" + } + } + + @JsonIgnoreProperty + var isFrozen = false + private set + + @JsonIgnoreProperty + private val properties = ArrayList() + + override fun equals(other: Any?): Boolean { + if (other is DefinitionBuilder && other.javaClass === this.javaClass) + return other.properties == properties + + return false + } + + override fun hashCode(): Int { + return properties.hashCode() + } + + override fun toString(): String { + return "${this::class.simpleName}[frozen=$isFrozen,values={${properties.joinToString(",")}}]" + } + + protected open fun onFreeze() { + + } + + fun freeze() { + if (isFrozen) return + onFreeze() + isFrozen = true + } +} diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/DirectAssetReference.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/DirectAssetReference.kt index 5897a5ed..54db63f9 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/DirectAssetReference.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/DirectAssetReference.kt @@ -8,6 +8,11 @@ import com.google.gson.stream.JsonReader import com.google.gson.stream.JsonWriter import ru.dbotthepony.kstarbound.util.PathStack +/** + * Путь как указан в JSON + Абсолютный путь + * + * @see DirectAssetReference + */ class DirectAssetReferenceFactory(val remapper: PathStack) : TypeAdapterFactory { override fun create(gson: Gson, type: TypeToken): TypeAdapter? { if (type.rawType == DirectAssetReference::class.java) { @@ -33,4 +38,7 @@ class DirectAssetReferenceFactory(val remapper: PathStack) : TypeAdapterFactory } } +/** + * Путь как указан в JSON + Абсолютный путь + */ data class DirectAssetReference(val path: String, val fullPath: String) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/MaterialReference.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/MaterialReference.kt deleted file mode 100644 index a5e6415d..00000000 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/MaterialReference.kt +++ /dev/null @@ -1,36 +0,0 @@ -package ru.dbotthepony.kstarbound.defs - -import com.google.gson.JsonSyntaxException -import com.google.gson.TypeAdapter -import com.google.gson.stream.JsonReader -import com.google.gson.stream.JsonToken -import com.google.gson.stream.JsonWriter - -data class MaterialReference( - val id: Int?, - val name: String? -) { - init { - require(id != null || name != null) { "At least ID or material string ID should be not null" } - } - - companion object : TypeAdapter() { - override fun write(out: JsonWriter, value: MaterialReference) { - if (value.id != null) { - out.value(value.id) - } else { - out.value(value.name!!) - } - } - - override fun read(`in`: JsonReader): MaterialReference { - if (`in`.peek() == JsonToken.NUMBER) { - return MaterialReference(id = `in`.nextInt(), name = null) - } else if (`in`.peek() == JsonToken.STRING) { - return MaterialReference(id = null, name = `in`.nextString()) - } else { - throw JsonSyntaxException("Invalid material reference: ${`in`.peek()} (must be either an Integer or String), near ${`in`.path}") - } - } - } -} diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ArmorItemDefinition.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ArmorItemDefinition.kt deleted file mode 100644 index 210dee3c..00000000 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ArmorItemDefinition.kt +++ /dev/null @@ -1,35 +0,0 @@ -package ru.dbotthepony.kstarbound.defs.item - -import ru.dbotthepony.kstarbound.defs.DirectAssetReference -import ru.dbotthepony.kstarbound.defs.IThingWithDescription -import ru.dbotthepony.kstarbound.defs.ThingDescription - -data class ArmorItemDefinition( - override val itemName: String, - override val price: Long, - override val rarity: ItemRarity, - override val category: String?, - override val inventoryIcon: List?, - override val itemTags: List, - override val learnBlueprintsOnPickup: List, - override val maxStack: Long, - override val eventCategory: String?, - override val consumeOnPickup: Boolean, - override val pickupQuestTemplates: List, - override val scripts: List, - override val scriptDelta: Int, - override val tooltipKind: String, - override val twoHanded: Boolean, - override val radioMessagesOnPickup: List, - override val fuelAmount: Long?, - - override val colorOptions: List>, - override val maleFrames: IArmorItemDefinition.IArmorFrames, - override val femaleFrames: IArmorItemDefinition.IArmorFrames, - override val level: Double, - override val leveledStatusEffects: List, - - override val armorType: ArmorPieceType, - - val descriptionData: ThingDescription, -) : IArmorItemDefinition, IThingWithDescription by descriptionData diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ArmorItemPrototype.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ArmorItemPrototype.kt index 436f5332..0fcbbda5 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ArmorItemPrototype.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ArmorItemPrototype.kt @@ -2,85 +2,45 @@ package ru.dbotthepony.kstarbound.defs.item import com.google.common.collect.ImmutableList import ru.dbotthepony.kstarbound.defs.DirectAssetReference -import ru.dbotthepony.kstarbound.defs.util.enrollMap import ru.dbotthepony.kstarbound.io.json.builder.JsonBuilder import ru.dbotthepony.kstarbound.io.json.builder.JsonIgnoreProperty -import ru.dbotthepony.kstarbound.util.NotNullVar @JsonBuilder -open class ArmorItemPrototype : ItemPrototype(), IArmorItemDefinition { - override var colorOptions: ImmutableList> = ImmutableList.of() - override var maleFrames: IArmorItemDefinition.ArmorFrames by NotNullVar() - override var femaleFrames: IArmorItemDefinition.ArmorFrames by NotNullVar() - override var level: Double = 1.0 - override var leveledStatusEffects: ImmutableList = ImmutableList.of() +abstract class ArmorItemPrototype : ItemPrototype(), IArmorItemDefinition { + override var colorOptions: ImmutableList> by NotNull(ImmutableList.of()) + override var maleFrames: IArmorItemDefinition.Frames by NotNull() + override var femaleFrames: IArmorItemDefinition.Frames by NotNull() + override var level: Double by NotNull(1.0) + override var leveledStatusEffects: ImmutableList by NotNull(ImmutableList.of()) - override var scripts: ImmutableList = ImmutableList.of() - override var scriptDelta: Int = 1 - - @JsonIgnoreProperty - override var armorType: ArmorPieceType by NotNullVar() + override var scripts: ImmutableList by NotNull(ImmutableList.of()) + override var scriptDelta: Int by NotNull(1) init { maxStack = 1L } - - override fun assemble(): IItemDefinition { - return ArmorItemDefinition( - descriptionData = descriptionData, - itemName = itemName, - price = price, - rarity = rarity, - category = category, - inventoryIcon = inventoryIcon, - itemTags = itemTags, - learnBlueprintsOnPickup = learnBlueprintsOnPickup, - maxStack = maxStack, - eventCategory = eventCategory, - consumeOnPickup = consumeOnPickup, - pickupQuestTemplates = pickupQuestTemplates, - scripts = scripts, - scriptDelta = scriptDelta, - tooltipKind = tooltipKind, - twoHanded = twoHanded, - radioMessagesOnPickup = radioMessagesOnPickup, - fuelAmount = fuelAmount, - - colorOptions = colorOptions, - maleFrames = maleFrames, - femaleFrames = femaleFrames, - level = level, - leveledStatusEffects = leveledStatusEffects, - - armorType = armorType, - ) - } } @JsonBuilder class HeadArmorItemPrototype : ArmorItemPrototype() { - init { - armorType = ArmorPieceType.HEAD - } + @JsonIgnoreProperty + override val armorType: ArmorPieceType = ArmorPieceType.HEAD } @JsonBuilder class ChestArmorItemPrototype : ArmorItemPrototype() { - init { - armorType = ArmorPieceType.CHEST - } + @JsonIgnoreProperty + override val armorType: ArmorPieceType = ArmorPieceType.CHEST } @JsonBuilder class LegsArmorItemPrototype : ArmorItemPrototype() { - init { - armorType = ArmorPieceType.LEGS - } + @JsonIgnoreProperty + override val armorType: ArmorPieceType = ArmorPieceType.LEGS } @JsonBuilder class BackArmorItemPrototype : ArmorItemPrototype() { - init { - armorType = ArmorPieceType.BACK - } + @JsonIgnoreProperty + override val armorType: ArmorPieceType = ArmorPieceType.BACK } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/CurrencyItemDefinition.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/CurrencyItemDefinition.kt deleted file mode 100644 index f87f0836..00000000 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/CurrencyItemDefinition.kt +++ /dev/null @@ -1,32 +0,0 @@ -package ru.dbotthepony.kstarbound.defs.item - -import ru.dbotthepony.kstarbound.defs.IThingWithDescription -import ru.dbotthepony.kstarbound.defs.ThingDescription - -data class CurrencyItemDefinition( - override val itemName: String, - override val price: Long, - override val rarity: ItemRarity, - override val category: String?, - override val inventoryIcon: List?, - override val itemTags: List, - override val learnBlueprintsOnPickup: List, - override val maxStack: Long, - override val eventCategory: String?, - override val consumeOnPickup: Boolean, - override val pickupQuestTemplates: List, - override val tooltipKind: String, - override val twoHanded: Boolean, - override val radioMessagesOnPickup: List, - override val fuelAmount: Long?, - - override val pickupSoundsSmall: List, - override val pickupSoundsMedium: List, - override val pickupSoundsLarge: List, - override val smallStackLimit: Long, - override val mediumStackLimit: Long, - override val currency: String, - override val value: Long, - - val descriptionData: ThingDescription, -) : ICurrencyItemDefinition, IThingWithDescription by descriptionData diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/CurrencyItemPrototype.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/CurrencyItemPrototype.kt index d8c40d70..1821f927 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/CurrencyItemPrototype.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/CurrencyItemPrototype.kt @@ -8,44 +8,10 @@ import ru.dbotthepony.kstarbound.util.NotNullVar @JsonBuilder class CurrencyItemPrototype : ItemPrototype(), ICurrencyItemDefinition { - override var pickupSoundsSmall: ImmutableList = ImmutableList.of() - override var pickupSoundsMedium: ImmutableList = ImmutableList.of() - override var pickupSoundsLarge: ImmutableList = ImmutableList.of() - override var smallStackLimit: Long by NotNullVar() - override var mediumStackLimit: Long by NotNullVar() - override var currency: String by NotNullVar() - override var value: Long by NotNullVar() + override var currency: String by NotNull() + override var value: Long by NotNull() init { maxStack = 16777216L } - - override fun assemble(): IItemDefinition { - return CurrencyItemDefinition( - descriptionData = descriptionData, - itemName = itemName, - price = price, - rarity = rarity, - category = category, - inventoryIcon = inventoryIcon, - itemTags = itemTags, - learnBlueprintsOnPickup = learnBlueprintsOnPickup, - maxStack = maxStack, - eventCategory = eventCategory, - consumeOnPickup = consumeOnPickup, - pickupQuestTemplates = pickupQuestTemplates, - tooltipKind = tooltipKind, - twoHanded = twoHanded, - radioMessagesOnPickup = radioMessagesOnPickup, - fuelAmount = fuelAmount, - - pickupSoundsSmall = pickupSoundsSmall, - pickupSoundsMedium = pickupSoundsMedium, - pickupSoundsLarge = pickupSoundsLarge, - smallStackLimit = smallStackLimit, - mediumStackLimit = mediumStackLimit, - currency = currency, - value = value, - ) - } } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/FlashlightDefinition.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/FlashlightDefinition.kt deleted file mode 100644 index 8bc83675..00000000 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/FlashlightDefinition.kt +++ /dev/null @@ -1,33 +0,0 @@ -package ru.dbotthepony.kstarbound.defs.item - -import com.google.common.collect.ImmutableList -import ru.dbotthepony.kstarbound.defs.IThingWithDescription -import ru.dbotthepony.kstarbound.defs.ThingDescription -import ru.dbotthepony.kvector.vector.Color -import ru.dbotthepony.kvector.vector.ndouble.Vector2d - -data class FlashlightDefinition( - override val itemName: String, - override val price: Long, - override val rarity: ItemRarity, - override val category: String?, - override val inventoryIcon: ImmutableList?, - override val itemTags: ImmutableList, - override val learnBlueprintsOnPickup: ImmutableList, - override val maxStack: Long, - override val eventCategory: String?, - override val consumeOnPickup: Boolean, - override val pickupQuestTemplates: ImmutableList, - override val tooltipKind: String, - override val twoHanded: Boolean, - override val radioMessagesOnPickup: ImmutableList, - override val fuelAmount: Long?, - - override val lightPosition: Vector2d, - override val lightColor: Color, - override val beamLevel: Int, - override val beamAmbience: Double, - override val handPosition: Vector2d, - - val descriptionData: ThingDescription, -) : IFlashlightDefinition, IThingWithDescription by descriptionData diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/FlashlightPrototype.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/FlashlightPrototype.kt index b223d648..84fa7f4e 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/FlashlightPrototype.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/FlashlightPrototype.kt @@ -8,40 +8,13 @@ import ru.dbotthepony.kvector.vector.ndouble.Vector2d @JsonBuilder class FlashlightPrototype : ItemPrototype(), IFlashlightDefinition { - override var lightPosition: Vector2d by NotNullVar() - override var lightColor: Color by NotNullVar() - override var beamLevel: Int by NotNullVar() - override var beamAmbience: Double by NotNullVar() - override var handPosition: Vector2d by NotNullVar() + override var lightPosition: Vector2d by NotNull() + override var lightColor: Color by NotNull() + override var beamLevel: Int by NotNull() + override var beamAmbience: Double by NotNull() + override var handPosition: Vector2d by NotNull() init { maxStack = 1L } - - override fun assemble(): IItemDefinition { - return FlashlightDefinition( - descriptionData = descriptionData, - itemName = itemName, - price = price, - rarity = rarity, - category = category, - inventoryIcon = inventoryIcon, - itemTags = itemTags, - learnBlueprintsOnPickup = learnBlueprintsOnPickup, - maxStack = maxStack, - eventCategory = eventCategory, - consumeOnPickup = consumeOnPickup, - pickupQuestTemplates = pickupQuestTemplates, - tooltipKind = tooltipKind, - twoHanded = twoHanded, - radioMessagesOnPickup = radioMessagesOnPickup, - fuelAmount = fuelAmount, - - lightPosition = lightPosition, - lightColor = lightColor, - beamLevel = beamLevel, - beamAmbience = beamAmbience, - handPosition = handPosition, - ) - } } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/HarvestingToolDefinition.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/HarvestingToolDefinition.kt deleted file mode 100644 index 7397f2ef..00000000 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/HarvestingToolDefinition.kt +++ /dev/null @@ -1,36 +0,0 @@ -package ru.dbotthepony.kstarbound.defs.item - -import com.google.common.collect.ImmutableList -import ru.dbotthepony.kstarbound.defs.IThingWithDescription -import ru.dbotthepony.kstarbound.defs.ThingDescription -import ru.dbotthepony.kvector.vector.ndouble.Vector2d - -data class HarvestingToolDefinition( - override val itemName: String, - override val price: Long = 4L, - override val rarity: ItemRarity, - override val category: String?, - override val inventoryIcon: ImmutableList?, - override val itemTags: ImmutableList, - override val learnBlueprintsOnPickup: ImmutableList, - override val maxStack: Long, - override val eventCategory: String?, - override val consumeOnPickup: Boolean, - override val pickupQuestTemplates: ImmutableList, - override val tooltipKind: String, - override val twoHanded: Boolean, - override val radioMessagesOnPickup: ImmutableList, - override val fuelAmount: Long?, - - override val frames: Int, - override val animationCycle: Double, - override val blockRadius: Int, - override val altBlockRadius: Int, - override val idleSound: ImmutableList, - override val strikeSounds: ImmutableList, - - override val handPosition: Vector2d, - override val fireTime: Double, - - val descriptionData: ThingDescription, -) : IHarvestingToolDefinition, IThingWithDescription by descriptionData diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/HarvestingToolPrototype.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/HarvestingToolPrototype.kt index f79f8c62..334128fe 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/HarvestingToolPrototype.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/HarvestingToolPrototype.kt @@ -8,46 +8,16 @@ import ru.dbotthepony.kvector.vector.ndouble.Vector2d @JsonBuilder class HarvestingToolPrototype : ItemPrototype(), IHarvestingToolDefinition { - override var frames: Int by NotNullVar() - override var animationCycle: Double by NotNullVar() - override var blockRadius: Int by NotNullVar() - override var altBlockRadius: Int = 0 - override var idleSound: ImmutableList = ImmutableList.of() - override var strikeSounds: ImmutableList = ImmutableList.of() - override var handPosition: Vector2d by NotNullVar() - override var fireTime: Double by NotNullVar() + override var frames: Int by NotNull() + override var animationCycle: Double by NotNull() + override var blockRadius: Int by NotNull() + override var altBlockRadius: Int by NotNull(0) + override var idleSound: ImmutableList by NotNull(ImmutableList.of()) + override var strikeSounds: ImmutableList by NotNull(ImmutableList.of()) + override var handPosition: Vector2d by NotNull() + override var fireTime: Double by NotNull() init { maxStack = 1L } - - override fun assemble(): IItemDefinition { - return HarvestingToolDefinition( - descriptionData = descriptionData, - itemName = itemName, - price = price, - rarity = rarity, - category = category, - inventoryIcon = inventoryIcon, - itemTags = itemTags, - learnBlueprintsOnPickup = learnBlueprintsOnPickup, - maxStack = maxStack, - eventCategory = eventCategory, - consumeOnPickup = consumeOnPickup, - pickupQuestTemplates = pickupQuestTemplates, - tooltipKind = tooltipKind, - twoHanded = twoHanded, - radioMessagesOnPickup = radioMessagesOnPickup, - fuelAmount = fuelAmount, - - frames = frames, - animationCycle = animationCycle, - blockRadius = blockRadius, - altBlockRadius = altBlockRadius, - idleSound = idleSound, - strikeSounds = strikeSounds, - handPosition = handPosition, - fireTime = fireTime, - ) - } } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/IArmorItemDefinition.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/IArmorItemDefinition.kt index a0214132..19e8e8ad 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/IArmorItemDefinition.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/IArmorItemDefinition.kt @@ -27,50 +27,50 @@ interface IArmorItemDefinition : ILeveledItemDefinition, IScriptableItemDefiniti /** * Визуальные кадры анимации, когда надето на гуманоида мужского пола */ - val maleFrames: IArmorFrames + val maleFrames: IFrames /** * Визуальные кадры анимации, когда надето на гуманоида женского пола */ - val femaleFrames: IArmorFrames + val femaleFrames: IFrames - @JsonImplementation(ArmorFrames::class) - interface IArmorFrames { + @JsonImplementation(Frames::class) + interface IFrames { val body: ImageReference val backSleeve: ImageReference? val frontSleeve: ImageReference? } - data class ArmorFrames( + data class Frames( override val body: ImageReference, override val backSleeve: ImageReference? = null, override val frontSleeve: ImageReference? = null, - ) : IArmorFrames { + ) : IFrames { class Factory(private val remapper: PathStack, private val atlasRegistry: () -> AtlasConfiguration.Registry) : TypeAdapterFactory { override fun create(gson: Gson, type: TypeToken): TypeAdapter? { - if (type.rawType == ArmorFrames::class.java) { - return object : TypeAdapter() { + if (type.rawType == Frames::class.java) { + return object : TypeAdapter() { private val adapter = FactoryAdapter.Builder( - ArmorFrames::class, - ArmorFrames::body, - ArmorFrames::backSleeve, - ArmorFrames::frontSleeve, + Frames::class, + Frames::body, + Frames::backSleeve, + Frames::frontSleeve, ).build(gson) - override fun write(out: JsonWriter, value: ArmorFrames?) { + override fun write(out: JsonWriter, value: Frames?) { if (value == null) out.nullValue() else adapter.write(out, value) } - override fun read(`in`: JsonReader): ArmorFrames? { + override fun read(`in`: JsonReader): Frames? { if (`in`.peek() == JsonToken.NULL) return null if (`in`.peek() == JsonToken.STRING) { val path = remapper.remap(`in`.nextString()) - return ArmorFrames(ImageReference(path, atlasRegistry.invoke().get(path)), null, null) + return Frames(ImageReference(path, atlasRegistry.invoke().get(path)), null, null) } return adapter.read(`in`) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ICurrencyItemDefinition.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ICurrencyItemDefinition.kt index ebe991ca..f519030a 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ICurrencyItemDefinition.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ICurrencyItemDefinition.kt @@ -1,31 +1,6 @@ package ru.dbotthepony.kstarbound.defs.item interface ICurrencyItemDefinition : IItemDefinition { - /** - * Звуки при поднятии "малого" количества предметов. Не имеет никакого смысла без [smallStackLimit] - */ - val pickupSoundsSmall: List - - /** - * Звуки при поднятии "среднего" количества предметов. Не имеет никакого смысла без [mediumStackLimit] - */ - val pickupSoundsMedium: List - - /** - * Звуки при поднятии "большого" количества предметов. Не имеет никакого смысла без [smallStackLimit] и без [mediumStackLimit] - */ - val pickupSoundsLarge: List - - /** - * Количество предметов ниже или равному данному значению проиграет звук [pickupSoundsSmall] - */ - val smallStackLimit: Long - - /** - * Количество предметов ниже или равному данному значению (но не меньше [smallStackLimit]) проиграет звук [pickupSoundsMedium] - */ - val mediumStackLimit: Long - /** * ID валюты */ diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/IItemDefinition.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/IItemDefinition.kt index 142aae79..c15a2d5a 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/IItemDefinition.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/IItemDefinition.kt @@ -8,6 +8,7 @@ import com.google.gson.stream.JsonReader import com.google.gson.stream.JsonToken import com.google.gson.stream.JsonWriter import ru.dbotthepony.kstarbound.defs.IThingWithDescription +import ru.dbotthepony.kstarbound.defs.RegistryReference import ru.dbotthepony.kstarbound.defs.image.SpriteReference import ru.dbotthepony.kstarbound.io.json.builder.FactoryAdapter import ru.dbotthepony.kstarbound.io.json.builder.JsonImplementation @@ -90,7 +91,7 @@ interface IItemDefinition : IThingWithDescription { /** * При подборе предмета мгновенно заставляет игрока изучить эти рецепты крафта */ - val learnBlueprintsOnPickup: List + val learnBlueprintsOnPickup: List> /** * Максимальное количество предмета в стопке @@ -133,4 +134,29 @@ interface IItemDefinition : IThingWithDescription { * Топливо корабля */ val fuelAmount: Long? + + /** + * Звуки при поднятии "малого" количества предметов. Не имеет никакого смысла без [smallStackLimit] + */ + val pickupSoundsSmall: List + + /** + * Звуки при поднятии "среднего" количества предметов. Не имеет никакого смысла без [mediumStackLimit] + */ + val pickupSoundsMedium: List + + /** + * Звуки при поднятии "большого" количества предметов. Не имеет никакого смысла без [smallStackLimit] и без [mediumStackLimit] + */ + val pickupSoundsLarge: List + + /** + * Количество предметов ниже или равному данному значению проиграет звук [pickupSoundsSmall] + */ + val smallStackLimit: Long? + + /** + * Количество предметов ниже или равному данному значению (но не меньше [smallStackLimit]) проиграет звук [pickupSoundsMedium] + */ + val mediumStackLimit: Long? } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ILeveledStatusEffect.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ILeveledStatusEffect.kt index 8805bb71..46745666 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ILeveledStatusEffect.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ILeveledStatusEffect.kt @@ -1,7 +1,9 @@ package ru.dbotthepony.kstarbound.defs.item -import ru.dbotthepony.kstarbound.io.json.builder.FactoryAdapter +import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory +import ru.dbotthepony.kstarbound.io.json.builder.JsonImplementation +@JsonImplementation(LeveledStatusEffect::class) interface ILeveledStatusEffect { val levelFunction: String val stat: String @@ -9,17 +11,10 @@ interface ILeveledStatusEffect { val amount: Double } +@JsonFactory data class LeveledStatusEffect( override val levelFunction: String, override val stat: String, override val baseMultiplier: Double = 1.0, override val amount: Double = 0.0, -) : ILeveledStatusEffect { - companion object { - val ADAPTER = FactoryAdapter.Builder(LeveledStatusEffect::class, - LeveledStatusEffect::levelFunction, - LeveledStatusEffect::stat, - LeveledStatusEffect::baseMultiplier, - LeveledStatusEffect::amount) - } -} +) : ILeveledStatusEffect diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ILiquidItem.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ILiquidItem.kt index 2c37422b..12bcade2 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ILiquidItem.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ILiquidItem.kt @@ -1,10 +1,10 @@ package ru.dbotthepony.kstarbound.defs.item -import ru.dbotthepony.kstarbound.defs.MaterialReference +import ru.dbotthepony.kstarbound.util.Either interface ILiquidItem : IItemDefinition { /** * То, какую жидкость из себя представляет данный предмет */ - val liquid: MaterialReference + val liquid: Either } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/IMaterialItem.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/IMaterialItem.kt index 62d6413f..0bbf74a6 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/IMaterialItem.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/IMaterialItem.kt @@ -1,10 +1,10 @@ package ru.dbotthepony.kstarbound.defs.item -import ru.dbotthepony.kstarbound.defs.MaterialReference +import ru.dbotthepony.kstarbound.util.Either interface IMaterialItem : IItemDefinition { /** * То, какой материал (блок) из себя представляет данный предмет */ - val material: MaterialReference + val material: Either } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ItemDefinition.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ItemDefinition.kt deleted file mode 100644 index 14097042..00000000 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ItemDefinition.kt +++ /dev/null @@ -1,27 +0,0 @@ -package ru.dbotthepony.kstarbound.defs.item - -import com.google.common.collect.ImmutableList -import ru.dbotthepony.kstarbound.defs.IThingWithDescription -import ru.dbotthepony.kstarbound.defs.ThingDescription -import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory -import ru.dbotthepony.kstarbound.io.json.builder.JsonPropertyConfig - -data class ItemDefinition( - override val itemName: String, - override val price: Long = 4L, - override val rarity: ItemRarity, - override val category: String?, - override val inventoryIcon: ImmutableList?, - override val itemTags: ImmutableList, - override val learnBlueprintsOnPickup: ImmutableList, - override val maxStack: Long, - override val eventCategory: String?, - override val consumeOnPickup: Boolean, - override val pickupQuestTemplates: ImmutableList, - override val tooltipKind: String, - override val twoHanded: Boolean, - override val radioMessagesOnPickup: ImmutableList, - override val fuelAmount: Long?, - - val descriptionData: ThingDescription, -) : IItemDefinition, IThingWithDescription by descriptionData diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ItemPrototype.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ItemPrototype.kt index 496714df..3facb0ae 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ItemPrototype.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/ItemPrototype.kt @@ -1,6 +1,8 @@ package ru.dbotthepony.kstarbound.defs.item import com.google.common.collect.ImmutableList +import ru.dbotthepony.kstarbound.defs.DefinitionBuilder +import ru.dbotthepony.kstarbound.defs.RegistryReference import ru.dbotthepony.kstarbound.defs.ThingDescription import ru.dbotthepony.kstarbound.defs.util.enrollMap import ru.dbotthepony.kstarbound.io.json.builder.BuilderAdapter @@ -11,49 +13,45 @@ import ru.dbotthepony.kstarbound.io.json.builder.JsonIgnoreProperty import ru.dbotthepony.kstarbound.util.NotNullVar @JsonBuilder -open class ItemPrototype : IItemDefinition { +open class ItemPrototype : DefinitionBuilder(), IItemDefinition { @JsonIgnoreProperty - final override var shortdescription: String = "..." - @JsonIgnoreProperty - final override var description: String = "..." + final override val shortdescription: String + get() = descriptionData.shortdescription - final override var itemName: String by NotNullVar() - final override var price: Long = 0L - final override var rarity: ItemRarity = ItemRarity.COMMON - final override var category: String? = null - final override var inventoryIcon: ImmutableList? = null - final override var itemTags: ImmutableList = ImmutableList.of() - final override var learnBlueprintsOnPickup: ImmutableList = ImmutableList.of() - final override var maxStack: Long = 9999L - final override var eventCategory: String? = null - final override var consumeOnPickup: Boolean = false - final override var pickupQuestTemplates: ImmutableList = ImmutableList.of() - final override var tooltipKind: String = "normal" - final override var twoHanded: Boolean = false - final override var radioMessagesOnPickup: ImmutableList = ImmutableList.of() - final override var fuelAmount: Long? = null + @JsonIgnoreProperty + final override val description: String + get() = descriptionData.description + + @JsonIgnoreProperty + final override val racialDescription: Map + get() = descriptionData.racialDescription + + @JsonIgnoreProperty + final override val racialShortDescription: Map + get() = descriptionData.racialShortDescription @JsonPropertyConfig(isFlat = true) var descriptionData: ThingDescription by NotNullVar() - open fun assemble(): IItemDefinition { - return ItemDefinition( - descriptionData = descriptionData, - itemName = itemName, - price = price, - rarity = rarity, - category = category, - inventoryIcon = inventoryIcon, - itemTags = itemTags, - learnBlueprintsOnPickup = learnBlueprintsOnPickup, - maxStack = maxStack, - eventCategory = eventCategory, - consumeOnPickup = consumeOnPickup, - pickupQuestTemplates = pickupQuestTemplates, - tooltipKind = tooltipKind, - twoHanded = twoHanded, - radioMessagesOnPickup = radioMessagesOnPickup, - fuelAmount = fuelAmount, - ) - } + final override var itemName: String by NotNull() + final override var price: Long by NotNull(0L) + final override var rarity: ItemRarity by NotNull(ItemRarity.COMMON) + final override var category: String? by Nullable() + final override var inventoryIcon: ImmutableList? by Nullable() + final override var itemTags: ImmutableList by NotNull(ImmutableList.of()) + final override var learnBlueprintsOnPickup: ImmutableList> by NotNull(ImmutableList.of()) + final override var maxStack: Long by NotNull(9999L) + final override var eventCategory: String? by Nullable() + final override var consumeOnPickup: Boolean by NotNull(false) + final override var pickupQuestTemplates: ImmutableList by NotNull(ImmutableList.of()) + final override var tooltipKind: String by NotNull("normal") + final override var twoHanded: Boolean by NotNull(false) + final override var radioMessagesOnPickup: ImmutableList by NotNull(ImmutableList.of()) + final override var fuelAmount: Long? by Nullable() + + final override var pickupSoundsSmall: ImmutableList by NotNull(ImmutableList.of()) + final override var pickupSoundsMedium: ImmutableList by NotNull(ImmutableList.of()) + final override var pickupSoundsLarge: ImmutableList by NotNull(ImmutableList.of()) + final override var smallStackLimit: Long? by Nullable() + final override var mediumStackLimit: Long? by Nullable() } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/LiquidItemDefinition.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/LiquidItemDefinition.kt deleted file mode 100644 index 4843a888..00000000 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/LiquidItemDefinition.kt +++ /dev/null @@ -1,27 +0,0 @@ -package ru.dbotthepony.kstarbound.defs.item - -import ru.dbotthepony.kstarbound.defs.IThingWithDescription -import ru.dbotthepony.kstarbound.defs.MaterialReference -import ru.dbotthepony.kstarbound.defs.ThingDescription - -data class LiquidItemDefinition( - override val itemName: String, - override val price: Long, - override val rarity: ItemRarity, - override val category: String?, - override val inventoryIcon: List?, - override val itemTags: List, - override val learnBlueprintsOnPickup: List, - override val maxStack: Long, - override val eventCategory: String?, - override val consumeOnPickup: Boolean, - override val pickupQuestTemplates: List, - override val tooltipKind: String, - override val twoHanded: Boolean, - override val radioMessagesOnPickup: List, - override val fuelAmount: Long?, - - override val liquid: MaterialReference, - - val descriptionData: ThingDescription, -) : ILiquidItem, IThingWithDescription by descriptionData diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/LiquidItemPrototype.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/LiquidItemPrototype.kt index d703762f..2322cb5d 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/LiquidItemPrototype.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/LiquidItemPrototype.kt @@ -1,45 +1,24 @@ package ru.dbotthepony.kstarbound.defs.item -import ru.dbotthepony.kstarbound.defs.MaterialReference -import ru.dbotthepony.kstarbound.defs.util.enrollMap -import ru.dbotthepony.kstarbound.io.json.builder.BuilderAdapter import ru.dbotthepony.kstarbound.io.json.builder.JsonBuilder +import ru.dbotthepony.kstarbound.util.Either @JsonBuilder -class LiquidItemPrototype : ItemPrototype() { - var liquid: MaterialReference? = null +class LiquidItemPrototype : ItemPrototype(), ILiquidItem { + private val liquidDelegate = NotNull>() + override var liquid: Either by liquidDelegate var liquidId: Int? - get() = liquid?.id - set(value) { - if (liquid == null) - liquid = MaterialReference(id = value, name = null) - } + get() = liquidDelegate.getNullable()?.left + set(value) { if (liquidDelegate.getNullable() == null) liquid = Either.left(value!!) } var liquidName: String? - get() = liquid?.name - set(value) { liquid = MaterialReference(name = value, id = null) } + get() = liquidDelegate.getNullable()?.right + set(value) { liquid = Either.right(value!!) } - override fun assemble(): IItemDefinition { - return LiquidItemDefinition( - descriptionData = descriptionData, - itemName = itemName, - price = price, - rarity = rarity, - category = category, - inventoryIcon = inventoryIcon, - itemTags = itemTags, - learnBlueprintsOnPickup = learnBlueprintsOnPickup, - maxStack = maxStack, - eventCategory = eventCategory, - consumeOnPickup = consumeOnPickup, - pickupQuestTemplates = pickupQuestTemplates, - tooltipKind = tooltipKind, - twoHanded = twoHanded, - radioMessagesOnPickup = radioMessagesOnPickup, - fuelAmount = fuelAmount, - - liquid = checkNotNull(liquid) { "Liquid is null (either 'liquidId' or 'liquidName' should be present, or 'liquid' itself)" }, - ) + override fun onFreeze() { + if (liquidDelegate.getNullable() == null) { + throw NullPointerException("no liquidId nor liquidName was defined") + } } } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/MaterialItemDefinition.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/MaterialItemDefinition.kt deleted file mode 100644 index a9263752..00000000 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/MaterialItemDefinition.kt +++ /dev/null @@ -1,27 +0,0 @@ -package ru.dbotthepony.kstarbound.defs.item - -import ru.dbotthepony.kstarbound.defs.IThingWithDescription -import ru.dbotthepony.kstarbound.defs.MaterialReference -import ru.dbotthepony.kstarbound.defs.ThingDescription - -data class MaterialItemDefinition( - override val itemName: String, - override val price: Long, - override val rarity: ItemRarity, - override val category: String?, - override val inventoryIcon: List?, - override val itemTags: List, - override val learnBlueprintsOnPickup: List, - override val maxStack: Long, - override val eventCategory: String?, - override val consumeOnPickup: Boolean, - override val pickupQuestTemplates: List, - override val tooltipKind: String, - override val twoHanded: Boolean, - override val radioMessagesOnPickup: List, - override val fuelAmount: Long?, - - override val material: MaterialReference, - - val descriptionData: ThingDescription, -) : IMaterialItem, IThingWithDescription by descriptionData diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/MaterialItemPrototype.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/MaterialItemPrototype.kt index 778da48a..175494ad 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/MaterialItemPrototype.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/item/MaterialItemPrototype.kt @@ -1,45 +1,24 @@ package ru.dbotthepony.kstarbound.defs.item -import ru.dbotthepony.kstarbound.defs.MaterialReference -import ru.dbotthepony.kstarbound.defs.util.enrollMap -import ru.dbotthepony.kstarbound.io.json.builder.BuilderAdapter import ru.dbotthepony.kstarbound.io.json.builder.JsonBuilder +import ru.dbotthepony.kstarbound.util.Either @JsonBuilder -class MaterialItemPrototype : ItemPrototype() { - var material: MaterialReference? = null +class MaterialItemPrototype : ItemPrototype(), IMaterialItem { + private val materialDelegate = NotNull>() + override var material: Either by materialDelegate var materialId: Int? - get() = material?.id - set(value) { - if (material == null) - material = MaterialReference(id = value, name = null) - } + get() = materialDelegate.getNullable()?.left + set(value) { if (materialDelegate.getNullable() == null) material = Either.left(value!!) } var materialName: String? - get() = material?.name - set(value) { material = MaterialReference(name = value, id = null) } + get() = materialDelegate.getNullable()?.right + set(value) { material = Either.right(value!!) } - override fun assemble(): IItemDefinition { - return MaterialItemDefinition( - descriptionData = descriptionData, - itemName = itemName, - price = price, - rarity = rarity, - category = category, - inventoryIcon = inventoryIcon, - itemTags = itemTags, - learnBlueprintsOnPickup = learnBlueprintsOnPickup, - maxStack = maxStack, - eventCategory = eventCategory, - consumeOnPickup = consumeOnPickup, - pickupQuestTemplates = pickupQuestTemplates, - tooltipKind = tooltipKind, - twoHanded = twoHanded, - radioMessagesOnPickup = radioMessagesOnPickup, - fuelAmount = fuelAmount, - - material = checkNotNull(material) { "Material is null (either 'materialId' or 'materialName' should be present, or 'material' itself)" }, - ) + override fun onFreeze() { + if (material == null) { + throw NullPointerException("no materialId nor materialName was defined") + } } } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/io/json/builder/BuilderAdapter.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/io/json/builder/BuilderAdapter.kt index dedce642..4ef5cf86 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/io/json/builder/BuilderAdapter.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/io/json/builder/BuilderAdapter.kt @@ -19,8 +19,10 @@ import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap import org.apache.logging.log4j.LogManager +import ru.dbotthepony.kstarbound.defs.DefinitionBuilder import ru.dbotthepony.kstarbound.defs.util.flattenJsonElement import ru.dbotthepony.kstarbound.io.json.builder.BuilderAdapter.Builder +import ru.dbotthepony.kstarbound.util.INotNullDelegate import ru.dbotthepony.kstarbound.util.NotNullVar import kotlin.properties.Delegates import kotlin.reflect.KClass @@ -173,7 +175,7 @@ class BuilderAdapter private constructor( val delegate = property.property.getDelegate(instance) - if (delegate is NotNullVar<*> && !delegate.isInitialized) { + if (delegate is INotNullDelegate && !delegate.isPresent) { throw JsonSyntaxException("${property.name} in ${instance::class.qualifiedName} can not be null, but it is missing from JSON structure") } else { try { @@ -185,6 +187,10 @@ class BuilderAdapter private constructor( } } + if (instance is DefinitionBuilder) { + instance.freeze() + } + return instance } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/util/INotNullDelegate.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/util/INotNullDelegate.kt new file mode 100644 index 00000000..5683c8c0 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/util/INotNullDelegate.kt @@ -0,0 +1,5 @@ +package ru.dbotthepony.kstarbound.util + +interface INotNullDelegate { + val isPresent: Boolean +} diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/util/NotNullVar.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/util/NotNullVar.kt index c64c6c36..9eb7eb7b 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/util/NotNullVar.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/util/NotNullVar.kt @@ -7,13 +7,13 @@ import kotlin.properties.Delegates /** * Аналог [Delegates.notNull], но со свойством [isInitialized] */ -class NotNullVar : ReadWriteProperty { +class NotNullVar : ReadWriteProperty, INotNullDelegate { private var value: V? = null /** * Имеет ли данный делегат не-null значение */ - val isInitialized: Boolean + override val isPresent: Boolean get() = value != null override fun getValue(thisRef: Any?, property: KProperty<*>): V { diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/ItemEntity.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/ItemEntity.kt index 2699e51a..1531ab4f 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/ItemEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/ItemEntity.kt @@ -6,7 +6,6 @@ import ru.dbotthepony.kbox2d.api.Manifold import ru.dbotthepony.kbox2d.collision.shapes.PolygonShape import ru.dbotthepony.kbox2d.dynamics.contact.AbstractContact import ru.dbotthepony.kstarbound.defs.item.IItemDefinition -import ru.dbotthepony.kstarbound.defs.item.ItemDefinition import ru.dbotthepony.kstarbound.world.World class ItemEntity(world: World<*, *>, val def: IItemDefinition) : Entity(world) {