PrismarineJS / node-minecraft-assets

Provide minecraft assets in node.js
17 stars 13 forks source link

Adds getTextureForItem #18

Closed u9g closed 2 years ago

u9g commented 2 years ago

Some items don't allow getting the texture like lapis etc, so I added this for much more fine grain control over which image you want

u9g commented 2 years ago

Adds async getTextureForItem(item): string which allows you to have much finer tune control over what image you get, even allowing you to make your own handler. That is, if mcAssets.customItemGetter is set to a function, that function is called before any item's image is found, allowing you to have your own return for things that mcAssets can't do, like skulls or enderchests. Here's an example of of a custom handler I made for skulls.

mcAssets.customItemGetter = async item => {
  if (item.name === 'skull') {
    const uuidAsB64 = item?.nbt?.value?.SkullOwner?.value?.Properties?.value?.textures?.value?.value[0]?.Value?.value
    if (typeof uuidAsB64 === 'string') {
      const textureURL = JSON.parse(Buffer.from(uuidAsB64, 'base64').toString('utf-8')).textures.SKIN.url.replace('http://textures.minecraft.net/texture/', '')
      const img = await canvas.loadImage(`https://mc-heads.net/head/${textureURL}/8`)
      const cvs = canvas.createCanvas(img.width, img.height)
      const ctx = cvs.getContext('2d')
      ctx.save()
      ctx.translate(0, 0)
      ctx.scale(0.4, 0.4)
      ctx.drawImage(img, 0, 0)
      ctx.restore()
      return cvs.toBuffer()
    }
  }
  return null
}
extremeheat commented 2 years ago

This seems like a hack, there’s most likely a mapping somewhere in the game assets you can use to pull this data off of.

rom1504 commented 2 years ago

https://github.com/PrismarineJS/minecraft-data/blob/master/data/pc/common/legacy.json#L2172 is the good solution

rom1504 commented 2 years ago

decompiled it, mapping for 1.12 item name + damage value to flattening is in https://github.com/extremeheat/extracted_minecraft_data/blob/client1.18.1/client/net/minecraft/util/datafix/fixes/ItemStackTheFlatteningFix.java , somewhere along the line they updated the textures paths via https://github.com/extremeheat/extracted_minecraft_data/blob/client1.18.1/client/net/minecraft/client/resources/LegacyPackResourcesAdapter.java , item model predicates are in client/assets/minecraft/models/item , eg for compass : https://github.com/extremeheat/extracted_minecraft_data/blob/client1.18.1/client/assets/minecraft/models/item/compass.json

rom1504 commented 2 years ago

closing, reopen if you think this is still useful