From 40aeadda274d8126affdf3cc659080c5d53bfe80 Mon Sep 17 00:00:00 2001 From: CoasterFreakDE Date: Tue, 11 Jun 2024 22:43:06 +0200 Subject: [PATCH] feat: Nearly working storage --- .../energeticstorage/EnergeticStorage.kt | 7 ++- .../energeticstorage/database/DiskTable.kt | 8 ++-- .../energeticstorage/gui/TerminalGui.kt | 14 +++--- .../energeticstorage/items/ItemBuilder.kt | 12 +++++- .../liamxsage/energeticstorage/model/Core.kt | 25 +++++++++-- .../serialization/ESItemAdapter.kt | 43 +++++++++++++++++++ 6 files changed, 90 insertions(+), 19 deletions(-) create mode 100644 src/main/kotlin/com/liamxsage/energeticstorage/serialization/ESItemAdapter.kt diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/EnergeticStorage.kt b/src/main/kotlin/com/liamxsage/energeticstorage/EnergeticStorage.kt index 5c18bab..6a73d5f 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/EnergeticStorage.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/EnergeticStorage.kt @@ -3,10 +3,8 @@ package com.liamxsage.energeticstorage import com.google.gson.GsonBuilder import com.liamxsage.energeticstorage.database.DatabaseConnection import com.liamxsage.energeticstorage.managers.RegisterManager -import com.liamxsage.energeticstorage.serialization.ItemStackAdapter -import com.liamxsage.energeticstorage.serialization.LocationAdapter -import com.liamxsage.energeticstorage.serialization.OptionalTypeAdapter -import com.liamxsage.energeticstorage.serialization.UUIDAdapter +import com.liamxsage.energeticstorage.model.ESItem +import com.liamxsage.energeticstorage.serialization.* import org.bukkit.Location import org.bukkit.inventory.ItemStack import org.bukkit.plugin.java.JavaPlugin @@ -31,6 +29,7 @@ class EnergeticStorage : JavaPlugin() { * @see ItemStackAdapter */ val gson = GsonBuilder() + .registerTypeAdapter(ESItem::class.java, ESItemAdapter()) .registerTypeAdapter(ItemStack::class.java, ItemStackAdapter()) .registerTypeAdapter(UUID::class.java, UUIDAdapter()) .registerTypeAdapter(Location::class.java, LocationAdapter()) diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/database/DiskTable.kt b/src/main/kotlin/com/liamxsage/energeticstorage/database/DiskTable.kt index c75f493..a216196 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/database/DiskTable.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/database/DiskTable.kt @@ -31,11 +31,11 @@ object DiskTable : UUIDTable("energeticstorage_disks") { fun Disk.saveToDB() = transaction { DiskTable.replace { it[DiskTable.id] = this@saveToDB.uuid - it[DiskTable.diskDrive] = this@saveToDB.diskDriveUUID - it[DiskTable.size] = this@saveToDB.size - it[DiskTable.items] = + it[diskDrive] = this@saveToDB.diskDriveUUID + it[size] = this@saveToDB.size + it[items] = ExposedBlob(EnergeticStorage.instance.gson.toJson(this@saveToDB.items.toTypedArray()).toByteArray()) - it[DiskTable.updatedAt] = Calendar.now().javaInstant + it[updatedAt] = Calendar.now().javaInstant } } diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/gui/TerminalGui.kt b/src/main/kotlin/com/liamxsage/energeticstorage/gui/TerminalGui.kt index b88c1ed..2435000 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/gui/TerminalGui.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/gui/TerminalGui.kt @@ -2,11 +2,13 @@ package com.liamxsage.energeticstorage.gui import com.liamxsage.energeticstorage.* import com.liamxsage.energeticstorage.database.saveToDB +import com.liamxsage.energeticstorage.extensions.getLogger import com.liamxsage.energeticstorage.extensions.hasKey import com.liamxsage.energeticstorage.extensions.sendOpenSound import com.liamxsage.energeticstorage.extensions.toItemBuilder import com.liamxsage.energeticstorage.model.Core import com.liamxsage.energeticstorage.model.SortOrder +import dev.fruxz.stacked.extension.asPlainString import dev.fruxz.stacked.text import org.bukkit.Bukkit import org.bukkit.Material @@ -117,11 +119,8 @@ class TerminalGui : InventoryHolder, Listener { asAmount(min(item.amount, item.itemStackAsSingle.maxStackSize.toLong()).toInt()) addPersistentData(ITEM_AMOUNT_NAMESPACE, PersistentDataType.LONG, item.amount) lore( + *item.itemStackAsSingle.lore()?.map { it.asPlainString }?.toTypedArray() ?: emptyArray(), "${TEXT_GRAY}Amount: ${item.amount}", - "", - "${TEXT_GRAY}Left-Click to get one,", - "${TEXT_GRAY}Right-Click to get half (up to 32).", - "${TEXT_GRAY}Shift-Click to get a full stack." ) }.build()) } @@ -272,6 +271,8 @@ class TerminalGui : InventoryHolder, Listener { toRemoveStack.amount -= leftOver?.amount ?: 0 } + if (toRemoveStack.amount <= 0) return@with run { isCancelled = true } + core.removeItemFromSystem(toRemoveStack) Bukkit.getScheduler().runTaskLater(EnergeticStorage.instance, Runnable { @@ -282,7 +283,8 @@ class TerminalGui : InventoryHolder, Listener { in setOf(ClickType.OUT, ClickType.OUT_HALF) -> { val takingItem = clickedItem.clone() takingItem.amount = - if ((clickType == ClickType.OUT_HALF && clickedItem.amount / 2 > 0)) clickedItem.amount / 2 else clickedItem.maxStackSize + if ((clickType == ClickType.OUT_HALF && clickedItem.amount / 2 > 0)) clickedItem.amount / 2 else min(takingItem.amount, clickedItem.maxStackSize) + val addingESItem = core.getFirstMatchingItem(clickedItem.clone()) ?: return@with run { isCancelled = true } val addingItem = addingESItem.itemStackAsSingle.asQuantity(takingItem.amount) @@ -295,7 +297,7 @@ class TerminalGui : InventoryHolder, Listener { }, 1L) } - else -> return@with run { isCancelled = true } + else -> return@with } } diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/items/ItemBuilder.kt b/src/main/kotlin/com/liamxsage/energeticstorage/items/ItemBuilder.kt index 796c8f4..e084700 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/items/ItemBuilder.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/items/ItemBuilder.kt @@ -150,6 +150,16 @@ class ItemBuilder(material: Material, count: Int = 1, dsl: ItemBuilder.() -> Uni return this } + /** + * Removes persistent data associated with the given key. + * + * @param key The namespaced key representing the data to be removed. + * @return An instance of ItemBuilder. + */ + fun removePersistentData(key: NamespacedKey): ItemBuilder { + return removePersistentDataIf(key, true) + } + /** * Removes persistent data from the item if the condition is true. * @@ -157,7 +167,7 @@ class ItemBuilder(material: Material, count: Int = 1, dsl: ItemBuilder.() -> Uni * @param condition the condition to check before removing the data (defaults to false) * @return the ItemBuilder with the persistent data removed (or unchanged if the condition is false) */ - fun removePersistantDataIf(key: NamespacedKey, condition: Boolean = false): ItemBuilder { + fun removePersistentDataIf(key: NamespacedKey, condition: Boolean = false): ItemBuilder { if (condition) { val meta = itemStack.itemMeta meta.persistentDataContainer.remove(key) diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/model/Core.kt b/src/main/kotlin/com/liamxsage/energeticstorage/model/Core.kt index 268f920..46b4225 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/model/Core.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/model/Core.kt @@ -1,5 +1,6 @@ package com.liamxsage.energeticstorage.model +import com.liamxsage.energeticstorage.ITEM_AMOUNT_NAMESPACE import com.liamxsage.energeticstorage.NETWORK_INTERFACE_ID_NAMESPACE import com.liamxsage.energeticstorage.NETWORK_INTERFACE_NAMESPACE import com.liamxsage.energeticstorage.TEXT_GRAY @@ -8,6 +9,7 @@ import com.liamxsage.energeticstorage.extensions.persistentDataContainer import com.liamxsage.energeticstorage.extensions.toItemBuilder import com.liamxsage.energeticstorage.network.NetworkInterface import com.liamxsage.energeticstorage.network.NetworkInterfaceType +import dev.fruxz.stacked.extension.asPlainString import org.bukkit.block.Block import org.bukkit.inventory.ItemFlag import org.bukkit.inventory.ItemStack @@ -110,7 +112,22 @@ data class Core( * @return The first matching [ESItem] object representing the item, or null if no matching item is found. */ fun getFirstMatchingItem(item: ItemStack): ESItem? { - return getAllDisksInSystem().flatMap { it.items }.find { it.itemStackAsSingle.isSimilar(item) } + return getAllDisksInSystem().flatMap { it.items }.find { it.itemStackAsSingle.isSimilar(getItemStackWithOutGuiModifications(item)) } + } + + /** + * Returns an ItemStack without the amount identifier. + * + * @param item The input ItemStack. + * @return The modified ItemStack without the amount identifier. + */ + private fun getItemStackWithOutGuiModifications(item: ItemStack): ItemStack { + return item.clone().toItemBuilder { + removePersistentData(ITEM_AMOUNT_NAMESPACE) + lore( + *item.lore()?.dropLast(1)?.map { it.asPlainString }?.toTypedArray() ?: emptyArray() + ) + }.build() } /** @@ -127,8 +144,8 @@ data class Core( if (remainingCapacity <= 0) continue val newItemStack = createNewItemStack(item) - disk.items.add(ESItem(newItemStack, remainingCapacity)) - subtractItemAmount(item, remainingCapacity.toInt()) + disk.items.add(ESItem(newItemStack, item.amount.toLong())) + subtractItemAmount(item, item.amount) if (item.amount <= 0) return true } return false @@ -199,7 +216,7 @@ data class Core( */ private fun removeExistingItemFromSystem(allDisks: List, item: ItemStack): Boolean { for (disk in allDisks) { - val existingItem = disk.items.find { it.itemStackAsSingle.isSimilar(item) } ?: continue + val existingItem = disk.items.find { it.itemStackAsSingle.isSimilar(getItemStackWithOutGuiModifications(item)) } ?: continue if (existingItem.amount >= item.amount) { existingItem.amount -= item.amount if (existingItem.amount == 0L) { diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/serialization/ESItemAdapter.kt b/src/main/kotlin/com/liamxsage/energeticstorage/serialization/ESItemAdapter.kt new file mode 100644 index 0000000..db9be22 --- /dev/null +++ b/src/main/kotlin/com/liamxsage/energeticstorage/serialization/ESItemAdapter.kt @@ -0,0 +1,43 @@ +package com.liamxsage.energeticstorage.serialization + +import com.google.gson.TypeAdapter +import com.google.gson.stream.JsonReader +import com.google.gson.stream.JsonToken +import com.google.gson.stream.JsonWriter +import com.liamxsage.energeticstorage.model.ESItem +import org.bukkit.inventory.ItemStack + +class ESItemAdapter : TypeAdapter() { + + override fun write(output: JsonWriter, esItem: ESItem?) { + if (esItem == null) { + output.nullValue() + return + } + output.beginObject() + output.name("amount").value(esItem.amount) + output.name("itemStack").value(ItemStackConverter.itemStackToBase64(esItem.itemStackAsSingle)) + output.endObject() + } + + override fun read(input: JsonReader): ESItem { + var amount: Long = 0 + var itemStack: ItemStack? = null + + input.beginObject() + while (input.hasNext()) { + when (input.peek()) { + JsonToken.NAME -> { + when (input.nextName()) { + "amount" -> amount = input.nextLong() + "itemStack" -> itemStack = ItemStackConverter.itemStackFromBase64(input.nextString()) + else -> input.skipValue() + } + } + else -> input.skipValue() + } + } + input.endObject() + return ESItem(itemStack ?: ItemStack.empty(), amount) + } +}