From ca3f0d40d2c9b5ecb369629c4dbb5fb21f59747b Mon Sep 17 00:00:00 2001 From: FiLKoNd Date: Mon, 28 Oct 2024 01:38:01 +0300 Subject: [PATCH] + inventory api --- paperktlib-paper/build.gradle.kts | 0 .../com/filkond/paperktlib/paper/Inventory.kt | 75 +++++++++++++++++++ settings.gradle.kts | 3 +- 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 paperktlib-paper/build.gradle.kts create mode 100644 paperktlib-paper/src/main/kotlin/com/filkond/paperktlib/paper/Inventory.kt diff --git a/paperktlib-paper/build.gradle.kts b/paperktlib-paper/build.gradle.kts new file mode 100644 index 0000000..e69de29 diff --git a/paperktlib-paper/src/main/kotlin/com/filkond/paperktlib/paper/Inventory.kt b/paperktlib-paper/src/main/kotlin/com/filkond/paperktlib/paper/Inventory.kt new file mode 100644 index 0000000..a646b8f --- /dev/null +++ b/paperktlib-paper/src/main/kotlin/com/filkond/paperktlib/paper/Inventory.kt @@ -0,0 +1,75 @@ +package com.filkond.paperktlib.paper + +import org.bukkit.Bukkit +import org.bukkit.Material +import org.bukkit.inventory.Inventory +import org.bukkit.inventory.ItemStack +import org.bukkit.util.io.BukkitObjectInputStream +import org.bukkit.util.io.BukkitObjectOutputStream +import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream + +fun Inventory.removeForce(type: Material, amount: Int) { + var remainingAmount = amount + contents.forEach { + if (it?.type == type) { + val amountToRemove = minOf(remainingAmount, it.amount) + it.amount -= amountToRemove + remainingAmount -= amountToRemove + if (remainingAmount == 0) return + } + } +} + +fun Inventory.removeForce(stack: ItemStack) { + var remainingAmount = stack.amount + contents.forEach { + if (it?.isSimilar(stack) == true) { + val amountToRemove = minOf(remainingAmount, it.amount) + it.amount -= amountToRemove + remainingAmount -= amountToRemove + if (remainingAmount == 0) return + } + } +} + +fun Inventory.removeIfContains(stack: ItemStack): Boolean = containsAny(stack).also { + if (it) removeForce(stack) +} + +fun Inventory.removeIfContains(type: Material, amount: Int): Boolean = containsAny(type, amount).also { + if (it) removeForce(type, amount) +} + +fun Inventory.containsAny(type: Material, amount: Int): Boolean = all(type).values.map { it.amount }.sum() >= amount +fun Inventory.containsAny(item: ItemStack): Boolean = all(item).values.map { it.amount }.sum() >= item.amount + +fun Inventory.toBase64(): String { + val outputStream = ByteArrayOutputStream() + val dataOutput = BukkitObjectOutputStream(outputStream) + + // Write the size of the inventory + dataOutput.writeInt(size) + + // Save every element in the list + contents.forEach { dataOutput.writeObject(it) } + + // Serialize that array + dataOutput.close() + return Base64Coder.encodeLines(outputStream.toByteArray()) +} + +fun String.inventoryFromBase64(): Inventory { + val inputStream = ByteArrayInputStream(Base64Coder.decodeLines(this)) + val dataInput = BukkitObjectInputStream(inputStream) + val inventory = Bukkit.getServer().createInventory(null, dataInput.readInt()) + + // Read the serialized inventory + for (i in 0 until inventory.size) { + inventory.setItem(i, dataInput.readObject() as ItemStack?) + } + + dataInput.close() + return inventory +} \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index ecd0fb5..3583b1e 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -12,4 +12,5 @@ include( "paperktlib-towny", "paperktlib-adventure", "paperktlib-config" -) \ No newline at end of file +) +include("paperktlib-paper")