From b47abdced4e57d97c57f6b008d5e541bf933348e Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Tue, 28 Mar 2023 20:54:06 +0700 Subject: [PATCH] =?UTF-8?q?ItemDescriptor=20=D0=B2=20ItemStack=20=D0=B8?= =?UTF-8?q?=D0=B1=D0=BE=20=D1=81=D1=82=D1=80=D0=B0=D0=BD=D0=BD=D0=BE=20?= =?UTF-8?q?=D0=BD=D0=B0=D0=B7=D1=8B=D0=B2=D0=B0=D1=82=D1=8C=20=D0=B5=D0=B3?= =?UTF-8?q?=D0=BE=20=D1=82=D0=B0=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/dbotthepony/kstarbound/Starbound.kt | 30 ++++++------- .../kstarbound/defs/ItemReference.kt | 8 ++-- .../kstarbound/defs/player/BagFilterConfig.kt | 6 +-- .../dbotthepony/kstarbound/player/Avatar.kt | 44 +++++++++---------- .../kstarbound/player/AvatarBag.kt | 14 +++--- .../kstarbound/player/QuestInstance.kt | 4 +- .../util/{ItemDescriptor.kt => ItemStack.kt} | 24 +++++----- 7 files changed, 65 insertions(+), 65 deletions(-) rename src/main/kotlin/ru/dbotthepony/kstarbound/util/{ItemDescriptor.kt => ItemStack.kt} (80%) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt index ec132c5a..b8fe0cab 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt @@ -63,7 +63,7 @@ import ru.dbotthepony.kstarbound.io.json.factory.ImmutableCollectionAdapterFacto import ru.dbotthepony.kstarbound.lua.LuaState import ru.dbotthepony.kstarbound.lua.loadInternalScript import ru.dbotthepony.kstarbound.math.* -import ru.dbotthepony.kstarbound.util.ItemDescriptor +import ru.dbotthepony.kstarbound.util.ItemStack import ru.dbotthepony.kstarbound.util.PathStack import ru.dbotthepony.kstarbound.util.SBPattern import ru.dbotthepony.kstarbound.util.WriteOnce @@ -202,7 +202,7 @@ class Starbound : ISBFileLocator { registerTypeAdapterFactory(AssetReferenceFactory(pathStack, this@Starbound)) - registerTypeAdapter(ItemDescriptor.Adapter(this@Starbound)) + registerTypeAdapter(ItemStack.Adapter(this@Starbound)) registerTypeAdapterFactory(ItemReference.Factory(STRINGS)) @@ -229,39 +229,39 @@ class Starbound : ISBFileLocator { .expireAfterAccess(Duration.ofMinutes(10)) .build() - fun item(name: String): ItemDescriptor { - return ItemDescriptor(items[name] ?: return ItemDescriptor.EMPTY) + fun item(name: String): ItemStack { + return ItemStack(items[name] ?: return ItemStack.EMPTY) } - fun item(name: String, count: Long): ItemDescriptor { + fun item(name: String, count: Long): ItemStack { if (count <= 0L) - return ItemDescriptor.EMPTY + return ItemStack.EMPTY - return ItemDescriptor(items[name] ?: return ItemDescriptor.EMPTY, count = count) + return ItemStack(items[name] ?: return ItemStack.EMPTY, count = count) } - fun item(name: String, count: Long, parameters: JsonObject): ItemDescriptor { + fun item(name: String, count: Long, parameters: JsonObject): ItemStack { if (count <= 0L) - return ItemDescriptor.EMPTY + return ItemStack.EMPTY - return ItemDescriptor(items[name] ?: return ItemDescriptor.EMPTY, count = count, parameters = parameters) + return ItemStack(items[name] ?: return ItemStack.EMPTY, count = count, parameters = parameters) } - fun item(descriptor: JsonObject): ItemDescriptor { + fun item(descriptor: JsonObject): ItemStack { return item( - (descriptor["name"] as? JsonPrimitive)?.asString ?: return ItemDescriptor.EMPTY, - descriptor["count"]?.asLong ?: return ItemDescriptor.EMPTY, + (descriptor["name"] as? JsonPrimitive)?.asString ?: return ItemStack.EMPTY, + descriptor["count"]?.asLong ?: return ItemStack.EMPTY, (descriptor["parameters"] as? JsonObject)?.deepCopy() ?: JsonObject() ) } - fun item(descriptor: JsonElement?): ItemDescriptor { + fun item(descriptor: JsonElement?): ItemStack { if (descriptor is JsonPrimitive) { return item(descriptor.asString) } else if (descriptor is JsonObject) { return item(descriptor) } else { - return ItemDescriptor.EMPTY + return ItemStack.EMPTY } } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/ItemReference.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/ItemReference.kt index d235bb3c..0f3dd000 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/ItemReference.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/ItemReference.kt @@ -13,18 +13,18 @@ import ru.dbotthepony.kstarbound.defs.item.IItemDefinition import ru.dbotthepony.kstarbound.io.json.builder.FactoryAdapter import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory import ru.dbotthepony.kstarbound.io.json.consumeNull -import ru.dbotthepony.kstarbound.util.ItemDescriptor +import ru.dbotthepony.kstarbound.util.ItemStack /** - * Прототип [ItemDescriptor] в JSON файлах + * Прототип [ItemStack] в JSON файлах */ data class ItemReference( val item: RegistryReference, val count: Long = 1, val parameters: JsonObject = JsonObject() ) { - fun makeStack(): ItemDescriptor { - return ItemDescriptor(item.value ?: return ItemDescriptor.EMPTY, count, parameters) + fun makeStack(): ItemStack { + return ItemStack(item.value ?: return ItemStack.EMPTY, count, parameters) } class Factory(val stringInterner: Interner = Interner { it }) : TypeAdapterFactory { diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/BagFilterConfig.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/BagFilterConfig.kt index aa4f5443..66320fdc 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/BagFilterConfig.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/player/BagFilterConfig.kt @@ -2,7 +2,7 @@ package ru.dbotthepony.kstarbound.defs.player import com.google.common.collect.ImmutableSet import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory -import ru.dbotthepony.kstarbound.util.ItemDescriptor +import ru.dbotthepony.kstarbound.util.ItemStack import java.util.function.Predicate @JsonFactory @@ -11,8 +11,8 @@ data class BagFilterConfig( val typeWhitelist: ImmutableSet? = null, val tagBlacklist: ImmutableSet? = null, val categoryBlacklist: ImmutableSet? = null, -) : Predicate { - override fun test(t: ItemDescriptor): Boolean { +) : Predicate { + override fun test(t: ItemStack): Boolean { if (t.isEmpty) return false diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/player/Avatar.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/player/Avatar.kt index a4e7bb50..dcc5c34a 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/player/Avatar.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/player/Avatar.kt @@ -11,7 +11,7 @@ import ru.dbotthepony.kstarbound.Starbound import ru.dbotthepony.kstarbound.defs.player.TechDefinition import ru.dbotthepony.kstarbound.lua.LuaState import ru.dbotthepony.kstarbound.lua.loadInternalScript -import ru.dbotthepony.kstarbound.util.ItemDescriptor +import ru.dbotthepony.kstarbound.util.ItemStack import ru.dbotthepony.kstarbound.util.immutableMap import java.util.* import kotlin.collections.ArrayList @@ -40,20 +40,20 @@ class Avatar(val starbound: Starbound, val uniqueId: UUID) { BACK_COSMETIC; } - private val essentialSlots = EnumMap(EssentialSlot::class.java) - private val equipmentSlots = EnumMap(EquipmentSlot::class.java) + private val essentialSlots = EnumMap(EssentialSlot::class.java) + private val equipmentSlots = EnumMap(EquipmentSlot::class.java) private val bags = ArrayList() private val quests = Object2ObjectOpenHashMap() - var cursorItem = ItemDescriptor.EMPTY + var cursorItem = ItemStack.EMPTY private val availableTechs = ObjectOpenHashSet>() private val enabledTechs = ObjectOpenHashSet>() private val equippedTechs = Object2ObjectOpenHashMap>() - private val knownBlueprints = ObjectOpenHashSet() + private val knownBlueprints = ObjectOpenHashSet() // С подписью NEW - private val newBlueprints = ObjectOpenHashSet() + private val newBlueprints = ObjectOpenHashSet() private val currencies = Object2LongOpenHashMap() @@ -95,7 +95,7 @@ class Avatar(val starbound: Starbound, val uniqueId: UUID) { * Teaches the player any recipes which can be used to craft the specified item. */ private fun giveBlueprint(name: JsonElement): Boolean { - val item: ItemDescriptor + val item: ItemStack if (name is JsonPrimitive) { item = starbound.item(name.asString).conciseToNull() ?: return false @@ -202,36 +202,36 @@ class Avatar(val starbound: Starbound, val uniqueId: UUID) { /** * Adds the specified item to the player's inventory. */ - fun giveItem(descriptor: ItemDescriptor) { + fun giveItem(descriptor: ItemStack) { TODO() } /** * Returns `true` if the player's inventory contains an item matching the specified descriptor and `false` otherwise. If exactMatch is `true` then parameters as well as item name must match. */ - fun hasItem(descriptor: ItemDescriptor, exactMatch: Boolean = false): Boolean { + fun hasItem(descriptor: ItemStack, exactMatch: Boolean = false): Boolean { return false } /** * Returns the total number of items in the player's inventory matching the specified descriptor. If exactMatch is `true` then parameters as well as item name must match. */ - fun hasCountOfItem(descriptor: ItemDescriptor, exactMatch: Boolean = false): Long { + fun hasCountOfItem(descriptor: ItemStack, exactMatch: Boolean = false): Long { return 0L } /** * Attempts to consume the specified item from the player's inventory and returns the item consumed if successful. If consumePartial is `true`, matching stacks totalling fewer items than the requested count may be consumed, otherwise the operation will only be performed if the full count can be consumed. If exactMatch is `true` then parameters as well as item name must match. */ - fun consumeItem(descriptor: ItemDescriptor, allowPartial: Boolean = false, exactMatch: Boolean = false): ItemDescriptor { - return ItemDescriptor.EMPTY + fun consumeItem(descriptor: ItemStack, allowPartial: Boolean = false, exactMatch: Boolean = false): ItemStack { + return ItemStack.EMPTY } fun inventoryTags(): Map { return mapOf() } - fun itemsWithTag(): List { + fun itemsWithTag(): List { return listOf() } @@ -247,18 +247,18 @@ class Avatar(val starbound: Starbound, val uniqueId: UUID) { return 0L } - fun getItemWithParameter(name: String, value: JsonElement): ItemDescriptor { - return ItemDescriptor.EMPTY + fun getItemWithParameter(name: String, value: JsonElement): ItemStack { + return ItemStack.EMPTY } - var primaryHandItem: ItemDescriptor? = null - var altHandItem: ItemDescriptor? = null + var primaryHandItem: ItemStack? = null + var altHandItem: ItemStack? = null - fun essentialItem(slotName: EssentialSlot): ItemDescriptor? { + fun essentialItem(slotName: EssentialSlot): ItemStack? { return essentialSlots[slotName]?.conciseToNull() } - fun giveEssentialItem(slotName: EssentialSlot, item: ItemDescriptor) { + fun giveEssentialItem(slotName: EssentialSlot, item: ItemStack) { } @@ -266,11 +266,11 @@ class Avatar(val starbound: Starbound, val uniqueId: UUID) { } - fun equippedItem(slotName: EquipmentSlot): ItemDescriptor { - return equipmentSlots[slotName] ?: ItemDescriptor.EMPTY + fun equippedItem(slotName: EquipmentSlot): ItemStack { + return equipmentSlots[slotName] ?: ItemStack.EMPTY } - fun setEquippedItem(slotName: EquipmentSlot, item: ItemDescriptor) { + fun setEquippedItem(slotName: EquipmentSlot, item: ItemStack) { } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/player/AvatarBag.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/player/AvatarBag.kt index 2bfd6389..0da0e32a 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/player/AvatarBag.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/player/AvatarBag.kt @@ -2,10 +2,10 @@ package ru.dbotthepony.kstarbound.player import com.google.common.collect.ImmutableList import ru.dbotthepony.kstarbound.defs.player.InventoryConfig -import ru.dbotthepony.kstarbound.util.ItemDescriptor +import ru.dbotthepony.kstarbound.util.ItemStack import java.util.function.Predicate -class AvatarBag(val avatar: Avatar, val config: InventoryConfig.Bag, val filter: Predicate) { +class AvatarBag(val avatar: Avatar, val config: InventoryConfig.Bag, val filter: Predicate) { private val slots = ImmutableList.builder().let { for (i in 0 until config.size) { it.add(Slot()) @@ -15,9 +15,9 @@ class AvatarBag(val avatar: Avatar, val config: InventoryConfig.Bag, val filter: } private class Slot { - var item: ItemDescriptor? = null + var item: ItemStack? = null - fun mergeFrom(value: ItemDescriptor, simulate: Boolean) { + fun mergeFrom(value: ItemStack, simulate: Boolean) { if (item == null) { if (!simulate) { item = value.copy().also { it.count = value.count.coerceAtMost(value.item!!.value.maxStack) } @@ -30,15 +30,15 @@ class AvatarBag(val avatar: Avatar, val config: InventoryConfig.Bag, val filter: } } - operator fun set(index: Int, value: ItemDescriptor?) { + operator fun set(index: Int, value: ItemStack?) { slots[index].item = value } - operator fun get(index: Int): ItemDescriptor? { + operator fun get(index: Int): ItemStack? { return slots[index].item } - fun put(item: ItemDescriptor, simulate: Boolean): ItemDescriptor { + fun put(item: ItemStack, simulate: Boolean): ItemStack { if (!filter.test(item)) return item diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/player/QuestInstance.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/player/QuestInstance.kt index 165a91ac..a2b028d2 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/player/QuestInstance.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/player/QuestInstance.kt @@ -9,7 +9,7 @@ import ru.dbotthepony.kstarbound.defs.quest.QuestTemplate import ru.dbotthepony.kstarbound.lua.LuaState import ru.dbotthepony.kstarbound.lua.MessageHandler import ru.dbotthepony.kstarbound.lua.exposeConfig -import ru.dbotthepony.kstarbound.util.ItemDescriptor +import ru.dbotthepony.kstarbound.util.ItemStack import ru.dbotthepony.kstarbound.util.set import java.util.UUID @@ -55,7 +55,7 @@ class QuestInstance( private var calledStart = false private var successfulStart = false - private val rewards = ArrayList() + private val rewards = ArrayList() fun complete() { diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/util/ItemDescriptor.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/util/ItemStack.kt similarity index 80% rename from src/main/kotlin/ru/dbotthepony/kstarbound/util/ItemDescriptor.kt rename to src/main/kotlin/ru/dbotthepony/kstarbound/util/ItemStack.kt index 15a9fa84..48b692e8 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/util/ItemDescriptor.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/util/ItemStack.kt @@ -11,7 +11,7 @@ import ru.dbotthepony.kstarbound.RegistryObject import ru.dbotthepony.kstarbound.Starbound import ru.dbotthepony.kstarbound.defs.item.IItemDefinition -class ItemDescriptor private constructor(item: RegistryObject?, count: Long, val parameters: JsonObject, marker: Unit) { +class ItemStack private constructor(item: RegistryObject?, count: Long, val parameters: JsonObject, marker: Unit) { constructor(item: RegistryObject, count: Long = 1L, parameters: JsonObject = JsonObject()) : this(item, count, parameters, Unit) var item: RegistryObject? = item @@ -46,7 +46,7 @@ class ItemDescriptor private constructor(item: RegistryObject?, /** * Возвращает null если этот предмет пуст */ - fun conciseToNull(): ItemDescriptor? { + fun conciseToNull(): ItemStack? { if (isEmpty) { return null } else { @@ -54,7 +54,7 @@ class ItemDescriptor private constructor(item: RegistryObject?, } } - fun mergeFrom(other: ItemDescriptor, simulate: Boolean) { + fun mergeFrom(other: ItemStack, simulate: Boolean) { if (isStackable(other)) { val newCount = (count + other.count).coerceAtMost(item!!.value.maxStack) val diff = newCount - count @@ -66,7 +66,7 @@ class ItemDescriptor private constructor(item: RegistryObject?, } fun lenientEquals(other: Any?): Boolean { - if (other !is ItemDescriptor) + if (other !is ItemStack) return false if (isEmpty) @@ -75,12 +75,12 @@ class ItemDescriptor private constructor(item: RegistryObject?, return other.count == count && other.item == item } - fun isStackable(other: ItemDescriptor): Boolean { + fun isStackable(other: ItemStack): Boolean { return count != 0L && other.count != 0L && item!!.value.maxStack < count && other.item == item && other.parameters == parameters } override fun equals(other: Any?): Boolean { - if (other !is ItemDescriptor) + if (other !is ItemStack) return false if (isEmpty) @@ -101,11 +101,11 @@ class ItemDescriptor private constructor(item: RegistryObject?, return "ItemDescriptor[${item!!.value.itemName}, count = $count, params = $parameters]" } - fun copy(): ItemDescriptor { + fun copy(): ItemStack { if (isEmpty) return this - return ItemDescriptor(item, count, parameters.deepCopy(), Unit) + return ItemStack(item, count, parameters.deepCopy(), Unit) } fun toJson(): JsonObject? { @@ -120,8 +120,8 @@ class ItemDescriptor private constructor(item: RegistryObject?, } } - class Adapter(val starbound: Starbound) : TypeAdapter() { - override fun write(out: JsonWriter, value: ItemDescriptor?) { + class Adapter(val starbound: Starbound) : TypeAdapter() { + override fun write(out: JsonWriter, value: ItemStack?) { val json = value?.toJson() if (json == null) @@ -130,7 +130,7 @@ class ItemDescriptor private constructor(item: RegistryObject?, TypeAdapters.JSON_ELEMENT.write(out, json) } - override fun read(`in`: JsonReader): ItemDescriptor { + override fun read(`in`: JsonReader): ItemStack { if (`in`.peek() == JsonToken.NULL) return EMPTY @@ -140,6 +140,6 @@ class ItemDescriptor private constructor(item: RegistryObject?, companion object { @JvmField - val EMPTY = ItemDescriptor(null, 0L, JsonObject(), Unit) + val EMPTY = ItemStack(null, 0L, JsonObject(), Unit) } }