ItemDescriptor в ItemStack ибо странно называть его так

This commit is contained in:
DBotThePony 2023-03-28 20:54:06 +07:00
parent 89180a6664
commit b47abdced4
Signed by: DBot
GPG Key ID: DCC23B5715498507
7 changed files with 65 additions and 65 deletions

View File

@ -63,7 +63,7 @@ import ru.dbotthepony.kstarbound.io.json.factory.ImmutableCollectionAdapterFacto
import ru.dbotthepony.kstarbound.lua.LuaState import ru.dbotthepony.kstarbound.lua.LuaState
import ru.dbotthepony.kstarbound.lua.loadInternalScript import ru.dbotthepony.kstarbound.lua.loadInternalScript
import ru.dbotthepony.kstarbound.math.* 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.PathStack
import ru.dbotthepony.kstarbound.util.SBPattern import ru.dbotthepony.kstarbound.util.SBPattern
import ru.dbotthepony.kstarbound.util.WriteOnce import ru.dbotthepony.kstarbound.util.WriteOnce
@ -202,7 +202,7 @@ class Starbound : ISBFileLocator {
registerTypeAdapterFactory(AssetReferenceFactory(pathStack, this@Starbound)) registerTypeAdapterFactory(AssetReferenceFactory(pathStack, this@Starbound))
registerTypeAdapter(ItemDescriptor.Adapter(this@Starbound)) registerTypeAdapter(ItemStack.Adapter(this@Starbound))
registerTypeAdapterFactory(ItemReference.Factory(STRINGS)) registerTypeAdapterFactory(ItemReference.Factory(STRINGS))
@ -229,39 +229,39 @@ class Starbound : ISBFileLocator {
.expireAfterAccess(Duration.ofMinutes(10)) .expireAfterAccess(Duration.ofMinutes(10))
.build<String, ImageData>() .build<String, ImageData>()
fun item(name: String): ItemDescriptor { fun item(name: String): ItemStack {
return ItemDescriptor(items[name] ?: return ItemDescriptor.EMPTY) return ItemStack(items[name] ?: return ItemStack.EMPTY)
} }
fun item(name: String, count: Long): ItemDescriptor { fun item(name: String, count: Long): ItemStack {
if (count <= 0L) 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) 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( return item(
(descriptor["name"] as? JsonPrimitive)?.asString ?: return ItemDescriptor.EMPTY, (descriptor["name"] as? JsonPrimitive)?.asString ?: return ItemStack.EMPTY,
descriptor["count"]?.asLong ?: return ItemDescriptor.EMPTY, descriptor["count"]?.asLong ?: return ItemStack.EMPTY,
(descriptor["parameters"] as? JsonObject)?.deepCopy() ?: JsonObject() (descriptor["parameters"] as? JsonObject)?.deepCopy() ?: JsonObject()
) )
} }
fun item(descriptor: JsonElement?): ItemDescriptor { fun item(descriptor: JsonElement?): ItemStack {
if (descriptor is JsonPrimitive) { if (descriptor is JsonPrimitive) {
return item(descriptor.asString) return item(descriptor.asString)
} else if (descriptor is JsonObject) { } else if (descriptor is JsonObject) {
return item(descriptor) return item(descriptor)
} else { } else {
return ItemDescriptor.EMPTY return ItemStack.EMPTY
} }
} }

View File

@ -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.FactoryAdapter
import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory
import ru.dbotthepony.kstarbound.io.json.consumeNull 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( data class ItemReference(
val item: RegistryReference<IItemDefinition>, val item: RegistryReference<IItemDefinition>,
val count: Long = 1, val count: Long = 1,
val parameters: JsonObject = JsonObject() val parameters: JsonObject = JsonObject()
) { ) {
fun makeStack(): ItemDescriptor { fun makeStack(): ItemStack {
return ItemDescriptor(item.value ?: return ItemDescriptor.EMPTY, count, parameters) return ItemStack(item.value ?: return ItemStack.EMPTY, count, parameters)
} }
class Factory(val stringInterner: Interner<String> = Interner { it }) : TypeAdapterFactory { class Factory(val stringInterner: Interner<String> = Interner { it }) : TypeAdapterFactory {

View File

@ -2,7 +2,7 @@ package ru.dbotthepony.kstarbound.defs.player
import com.google.common.collect.ImmutableSet import com.google.common.collect.ImmutableSet
import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory 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 import java.util.function.Predicate
@JsonFactory @JsonFactory
@ -11,8 +11,8 @@ data class BagFilterConfig(
val typeWhitelist: ImmutableSet<String>? = null, val typeWhitelist: ImmutableSet<String>? = null,
val tagBlacklist: ImmutableSet<String>? = null, val tagBlacklist: ImmutableSet<String>? = null,
val categoryBlacklist: ImmutableSet<String>? = null, val categoryBlacklist: ImmutableSet<String>? = null,
) : Predicate<ItemDescriptor> { ) : Predicate<ItemStack> {
override fun test(t: ItemDescriptor): Boolean { override fun test(t: ItemStack): Boolean {
if (t.isEmpty) if (t.isEmpty)
return false return false

View File

@ -11,7 +11,7 @@ import ru.dbotthepony.kstarbound.Starbound
import ru.dbotthepony.kstarbound.defs.player.TechDefinition import ru.dbotthepony.kstarbound.defs.player.TechDefinition
import ru.dbotthepony.kstarbound.lua.LuaState import ru.dbotthepony.kstarbound.lua.LuaState
import ru.dbotthepony.kstarbound.lua.loadInternalScript 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 ru.dbotthepony.kstarbound.util.immutableMap
import java.util.* import java.util.*
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
@ -40,20 +40,20 @@ class Avatar(val starbound: Starbound, val uniqueId: UUID) {
BACK_COSMETIC; BACK_COSMETIC;
} }
private val essentialSlots = EnumMap<EssentialSlot, ItemDescriptor>(EssentialSlot::class.java) private val essentialSlots = EnumMap<EssentialSlot, ItemStack>(EssentialSlot::class.java)
private val equipmentSlots = EnumMap<EquipmentSlot, ItemDescriptor>(EquipmentSlot::class.java) private val equipmentSlots = EnumMap<EquipmentSlot, ItemStack>(EquipmentSlot::class.java)
private val bags = ArrayList<AvatarBag>() private val bags = ArrayList<AvatarBag>()
private val quests = Object2ObjectOpenHashMap<String, QuestInstance>() private val quests = Object2ObjectOpenHashMap<String, QuestInstance>()
var cursorItem = ItemDescriptor.EMPTY var cursorItem = ItemStack.EMPTY
private val availableTechs = ObjectOpenHashSet<RegistryObject<TechDefinition>>() private val availableTechs = ObjectOpenHashSet<RegistryObject<TechDefinition>>()
private val enabledTechs = ObjectOpenHashSet<RegistryObject<TechDefinition>>() private val enabledTechs = ObjectOpenHashSet<RegistryObject<TechDefinition>>()
private val equippedTechs = Object2ObjectOpenHashMap<String, RegistryObject<TechDefinition>>() private val equippedTechs = Object2ObjectOpenHashMap<String, RegistryObject<TechDefinition>>()
private val knownBlueprints = ObjectOpenHashSet<ItemDescriptor>() private val knownBlueprints = ObjectOpenHashSet<ItemStack>()
// С подписью NEW // С подписью NEW
private val newBlueprints = ObjectOpenHashSet<ItemDescriptor>() private val newBlueprints = ObjectOpenHashSet<ItemStack>()
private val currencies = Object2LongOpenHashMap<String>() private val currencies = Object2LongOpenHashMap<String>()
@ -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. * Teaches the player any recipes which can be used to craft the specified item.
*/ */
private fun giveBlueprint(name: JsonElement): Boolean { private fun giveBlueprint(name: JsonElement): Boolean {
val item: ItemDescriptor val item: ItemStack
if (name is JsonPrimitive) { if (name is JsonPrimitive) {
item = starbound.item(name.asString).conciseToNull() ?: return false 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. * Adds the specified item to the player's inventory.
*/ */
fun giveItem(descriptor: ItemDescriptor) { fun giveItem(descriptor: ItemStack) {
TODO() 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. * 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 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. * 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 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. * 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 { fun consumeItem(descriptor: ItemStack, allowPartial: Boolean = false, exactMatch: Boolean = false): ItemStack {
return ItemDescriptor.EMPTY return ItemStack.EMPTY
} }
fun inventoryTags(): Map<String, Long> { fun inventoryTags(): Map<String, Long> {
return mapOf() return mapOf()
} }
fun itemsWithTag(): List<ItemDescriptor> { fun itemsWithTag(): List<ItemStack> {
return listOf() return listOf()
} }
@ -247,18 +247,18 @@ class Avatar(val starbound: Starbound, val uniqueId: UUID) {
return 0L return 0L
} }
fun getItemWithParameter(name: String, value: JsonElement): ItemDescriptor { fun getItemWithParameter(name: String, value: JsonElement): ItemStack {
return ItemDescriptor.EMPTY return ItemStack.EMPTY
} }
var primaryHandItem: ItemDescriptor? = null var primaryHandItem: ItemStack? = null
var altHandItem: ItemDescriptor? = null var altHandItem: ItemStack? = null
fun essentialItem(slotName: EssentialSlot): ItemDescriptor? { fun essentialItem(slotName: EssentialSlot): ItemStack? {
return essentialSlots[slotName]?.conciseToNull() 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 { fun equippedItem(slotName: EquipmentSlot): ItemStack {
return equipmentSlots[slotName] ?: ItemDescriptor.EMPTY return equipmentSlots[slotName] ?: ItemStack.EMPTY
} }
fun setEquippedItem(slotName: EquipmentSlot, item: ItemDescriptor) { fun setEquippedItem(slotName: EquipmentSlot, item: ItemStack) {
} }

View File

@ -2,10 +2,10 @@ package ru.dbotthepony.kstarbound.player
import com.google.common.collect.ImmutableList import com.google.common.collect.ImmutableList
import ru.dbotthepony.kstarbound.defs.player.InventoryConfig import ru.dbotthepony.kstarbound.defs.player.InventoryConfig
import ru.dbotthepony.kstarbound.util.ItemDescriptor import ru.dbotthepony.kstarbound.util.ItemStack
import java.util.function.Predicate import java.util.function.Predicate
class AvatarBag(val avatar: Avatar, val config: InventoryConfig.Bag, val filter: Predicate<ItemDescriptor>) { class AvatarBag(val avatar: Avatar, val config: InventoryConfig.Bag, val filter: Predicate<ItemStack>) {
private val slots = ImmutableList.builder<Slot>().let { private val slots = ImmutableList.builder<Slot>().let {
for (i in 0 until config.size) { for (i in 0 until config.size) {
it.add(Slot()) it.add(Slot())
@ -15,9 +15,9 @@ class AvatarBag(val avatar: Avatar, val config: InventoryConfig.Bag, val filter:
} }
private class Slot { 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 (item == null) {
if (!simulate) { if (!simulate) {
item = value.copy().also { it.count = value.count.coerceAtMost(value.item!!.value.maxStack) } 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 slots[index].item = value
} }
operator fun get(index: Int): ItemDescriptor? { operator fun get(index: Int): ItemStack? {
return slots[index].item return slots[index].item
} }
fun put(item: ItemDescriptor, simulate: Boolean): ItemDescriptor { fun put(item: ItemStack, simulate: Boolean): ItemStack {
if (!filter.test(item)) if (!filter.test(item))
return item return item

View File

@ -9,7 +9,7 @@ import ru.dbotthepony.kstarbound.defs.quest.QuestTemplate
import ru.dbotthepony.kstarbound.lua.LuaState import ru.dbotthepony.kstarbound.lua.LuaState
import ru.dbotthepony.kstarbound.lua.MessageHandler import ru.dbotthepony.kstarbound.lua.MessageHandler
import ru.dbotthepony.kstarbound.lua.exposeConfig 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 ru.dbotthepony.kstarbound.util.set
import java.util.UUID import java.util.UUID
@ -55,7 +55,7 @@ class QuestInstance(
private var calledStart = false private var calledStart = false
private var successfulStart = false private var successfulStart = false
private val rewards = ArrayList<ItemDescriptor>() private val rewards = ArrayList<ItemStack>()
fun complete() { fun complete() {

View File

@ -11,7 +11,7 @@ import ru.dbotthepony.kstarbound.RegistryObject
import ru.dbotthepony.kstarbound.Starbound import ru.dbotthepony.kstarbound.Starbound
import ru.dbotthepony.kstarbound.defs.item.IItemDefinition import ru.dbotthepony.kstarbound.defs.item.IItemDefinition
class ItemDescriptor private constructor(item: RegistryObject<IItemDefinition>?, count: Long, val parameters: JsonObject, marker: Unit) { class ItemStack private constructor(item: RegistryObject<IItemDefinition>?, count: Long, val parameters: JsonObject, marker: Unit) {
constructor(item: RegistryObject<IItemDefinition>, count: Long = 1L, parameters: JsonObject = JsonObject()) : this(item, count, parameters, Unit) constructor(item: RegistryObject<IItemDefinition>, count: Long = 1L, parameters: JsonObject = JsonObject()) : this(item, count, parameters, Unit)
var item: RegistryObject<IItemDefinition>? = item var item: RegistryObject<IItemDefinition>? = item
@ -46,7 +46,7 @@ class ItemDescriptor private constructor(item: RegistryObject<IItemDefinition>?,
/** /**
* Возвращает null если этот предмет пуст * Возвращает null если этот предмет пуст
*/ */
fun conciseToNull(): ItemDescriptor? { fun conciseToNull(): ItemStack? {
if (isEmpty) { if (isEmpty) {
return null return null
} else { } else {
@ -54,7 +54,7 @@ class ItemDescriptor private constructor(item: RegistryObject<IItemDefinition>?,
} }
} }
fun mergeFrom(other: ItemDescriptor, simulate: Boolean) { fun mergeFrom(other: ItemStack, simulate: Boolean) {
if (isStackable(other)) { if (isStackable(other)) {
val newCount = (count + other.count).coerceAtMost(item!!.value.maxStack) val newCount = (count + other.count).coerceAtMost(item!!.value.maxStack)
val diff = newCount - count val diff = newCount - count
@ -66,7 +66,7 @@ class ItemDescriptor private constructor(item: RegistryObject<IItemDefinition>?,
} }
fun lenientEquals(other: Any?): Boolean { fun lenientEquals(other: Any?): Boolean {
if (other !is ItemDescriptor) if (other !is ItemStack)
return false return false
if (isEmpty) if (isEmpty)
@ -75,12 +75,12 @@ class ItemDescriptor private constructor(item: RegistryObject<IItemDefinition>?,
return other.count == count && other.item == item 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 return count != 0L && other.count != 0L && item!!.value.maxStack < count && other.item == item && other.parameters == parameters
} }
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (other !is ItemDescriptor) if (other !is ItemStack)
return false return false
if (isEmpty) if (isEmpty)
@ -101,11 +101,11 @@ class ItemDescriptor private constructor(item: RegistryObject<IItemDefinition>?,
return "ItemDescriptor[${item!!.value.itemName}, count = $count, params = $parameters]" return "ItemDescriptor[${item!!.value.itemName}, count = $count, params = $parameters]"
} }
fun copy(): ItemDescriptor { fun copy(): ItemStack {
if (isEmpty) if (isEmpty)
return this return this
return ItemDescriptor(item, count, parameters.deepCopy(), Unit) return ItemStack(item, count, parameters.deepCopy(), Unit)
} }
fun toJson(): JsonObject? { fun toJson(): JsonObject? {
@ -120,8 +120,8 @@ class ItemDescriptor private constructor(item: RegistryObject<IItemDefinition>?,
} }
} }
class Adapter(val starbound: Starbound) : TypeAdapter<ItemDescriptor>() { class Adapter(val starbound: Starbound) : TypeAdapter<ItemStack>() {
override fun write(out: JsonWriter, value: ItemDescriptor?) { override fun write(out: JsonWriter, value: ItemStack?) {
val json = value?.toJson() val json = value?.toJson()
if (json == null) if (json == null)
@ -130,7 +130,7 @@ class ItemDescriptor private constructor(item: RegistryObject<IItemDefinition>?,
TypeAdapters.JSON_ELEMENT.write(out, json) TypeAdapters.JSON_ELEMENT.write(out, json)
} }
override fun read(`in`: JsonReader): ItemDescriptor { override fun read(`in`: JsonReader): ItemStack {
if (`in`.peek() == JsonToken.NULL) if (`in`.peek() == JsonToken.NULL)
return EMPTY return EMPTY
@ -140,6 +140,6 @@ class ItemDescriptor private constructor(item: RegistryObject<IItemDefinition>?,
companion object { companion object {
@JvmField @JvmField
val EMPTY = ItemDescriptor(null, 0L, JsonObject(), Unit) val EMPTY = ItemStack(null, 0L, JsonObject(), Unit)
} }
} }