Skip to content

Instantly share code, notes, and snippets.

@brecert
Created June 27, 2022 20:42
Show Gist options
  • Save brecert/738665acc61e3a3eea5110464f138505 to your computer and use it in GitHub Desktop.
Save brecert/738665acc61e3a3eea5110464f138505 to your computer and use it in GitHub Desktop.
function nbtCompoundToObject(nbt) {
return Object.fromEntries(
Array.from(nbt.getKeys())
.map(key => [key, nbtToObject(nbt.get(key))])
)
}
function nbtListToArray(nbt) {
return Array(nbt.length())
.fill(null)
.map((_, i) => nbtToObject(nbt.get(i)))
}
function nbtNumberToNumberOrBool(nbt) {
switch (nbt.getType()) {
case 1: return nbt.asByte() != 0
case 2: return nbt.asShort()
case 3: return nbt.asInt()
case 4: return nbt.asLong()
case 5: return nbt.asFloat()
case 6: return nbt.asDouble()
default: return nbt.asFloat()
}
}
function nbtToObject(nbt) {
switch (true) {
case nbt == null: return undefined
case nbt.isNull(): return null
case nbt.isList(): return nbtListToArray(nbt)
case nbt.isNumber(): return nbtNumberToNumberOrBool(nbt)
case nbt.isString(): return nbt.asString()
case nbt.isCompound(): return nbtCompoundToObject(nbt)
default:
return nbt
}
}
function itemFromSlot(slot) {
return {
id: slot.getItemId(),
count: slot.getCount(),
category: slot.getCreativeTab(),
nbt: nbtToObject(slot.getNBT())
}
}
const shouldCopy = KeyBind.getPressedKeys().contains('key.keyboard.left.control')
if (shouldCopy) {
event.cancel = true
const slot = event.getInventory().getSlot(event.slot)
const message =
Chat.createTextBuilder()
.append(`Inspect: ${slot.toString()}`)
.withShowItemHover(slot)
.withClickEvent("copy_to_clipboard", JSON.stringify(itemFromSlot(slot)))
.build()
Chat.log(message)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment