Skip to content

Commit

Permalink
feat: Nearly working storage
Browse files Browse the repository at this point in the history
  • Loading branch information
CoasterFreakDE committed Jun 11, 2024
1 parent b062bb8 commit 40aeadd
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: <green>${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())
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -295,7 +297,7 @@ class TerminalGui : InventoryHolder, Listener {
}, 1L)
}

else -> return@with run { isCancelled = true }
else -> return@with
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,24 @@ 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.
*
* @param key the NamespacedKey used to identify the persistent data
* @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)
Expand Down
25 changes: 21 additions & 4 deletions src/main/kotlin/com/liamxsage/energeticstorage/model/Core.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
}

/**
Expand All @@ -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
Expand Down Expand Up @@ -199,7 +216,7 @@ data class Core(
*/
private fun removeExistingItemFromSystem(allDisks: List<Disk>, 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ESItem>() {

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)
}
}

0 comments on commit 40aeadd

Please sign in to comment.