Skip to content

Commit

Permalink
feat: item lore & shulker insides
Browse files Browse the repository at this point in the history
  • Loading branch information
tristancamejo committed Mar 7, 2023
1 parent 4710a74 commit 8abe20d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
2 changes: 1 addition & 1 deletion clients/js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "elytra",
"version": "1.5.2",
"version": "1.6.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"author": "Tristan Camejo <contact@tristancamejo.com>",
Expand Down
14 changes: 13 additions & 1 deletion clients/js/src/schemas/ItemStack.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { z } from "zod";

export const ItemStackSchema = z.object({
const ItemStackNOTShulkerBoxSchema = z.object({
name: z.string().nullable(),
id: z.string(),
type: z.union([z.literal("item"), z.literal("block")]),
amount: z.number(),
durability: z.number(),
enchantments: z.record(z.number()),
b64: z.string(),
lore: z.array(z.string()),
});

export const ItemStackSchema = ItemStackNOTShulkerBoxSchema.extend({
itemsInside: z.array(
z.union([
ItemStackNOTShulkerBoxSchema.extend({
itemsInside: z.array(z.null()),
}),
z.null(),
])
),
});
15 changes: 15 additions & 0 deletions clients/js/src/structs/ItemStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ export class ItemStack extends Base {
return this.data.enchantments;
}

public get lore() {
return this.data.lore;
}

public get itemsInside(): (ItemStack | null)[] {
return this.data.itemsInside.map((i) =>
i === null
? null
: new ItemStack(this.client, {
...i,
itemsInside: [],
})
);
}

/**
* Serialized data of the item encoded in base64
*/
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/com/tristansmp/elytra/routes/Player.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import io.ktor.server.application.ApplicationCallPipeline.ApplicationPhase.Plugi
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.block.ShulkerBox
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.BlockStateMeta
import java.util.*


data class InventoryPOST(
val b64: String
)
Expand Down Expand Up @@ -56,6 +61,23 @@ fun Player.toJson(): Map<String, Any?> {
}

fun ItemStack.toJson(): Map<String, Any?> {

var itemLore: List<String> = listOf()

if (itemMeta.hasLore()) {
itemLore = itemMeta.lore()!!.map {
LegacyComponentSerializer.legacyAmpersand().serialize(it)
}
}

var itemsInside = listOf<Map<String, Any?>?>()

if (type == Material.SHULKER_BOX) {
val box = (itemMeta as BlockStateMeta).blockState as ShulkerBox

itemsInside = box.inventory.contents.map { it?.toJson() }
}

return mapOf(
"name" to itemMeta.getName(),
"id" to type.name,
Expand All @@ -64,6 +86,8 @@ fun ItemStack.toJson(): Map<String, Any?> {
"durability" to durability,
"enchantments" to enchantments.map { it.key.name to it.value }.toMap(),
"b64" to SerializeUtils.itemStackToBase64(this),
"lore" to itemLore,
"itemsInside" to itemsInside
)

}
Expand All @@ -76,7 +100,7 @@ fun Route.Player() {
call.respond(player!!.toJson())
}

get() {
get {
call.respond(Bukkit.getOnlinePlayers().map { it.toJson() })
}

Expand Down

0 comments on commit 8abe20d

Please sign in to comment.