mineflayer icon indicating copy to clipboard operation
mineflayer copied to clipboard

Items getter for Shulker-Box-Items

Open Moosbee opened this issue 2 years ago • 0 comments

Is your feature request related to a problem? Please describe.

I'm dealing with Shulker Boxes(Storage Bot) and need to know what Items are in the Boxes without Placing them.

Describe the solution you'd like

A getter on the Item Class named items just like enchants outputting a array of readonly Item instances from the nbt.

Describe alternatives you've considered

I currently use my own function

Additional context

The Function(should not be implemented as is)


function isShulkerBox(item: Item | any): boolean {
  return typeof item?.name == 'string'
    ? item.name.includes('shulker_box')
    : false;
}

interface MinecraftItem {
  Slot: {
    type: 'byte';
    value: number;
  };
  id: {
    type: 'string';
    value: string;
  };
  tag?: {
    type: 'compound';
    value: {
      RepairCost?: {
        type: 'int';
        value: number;
      };
      Enchantments?: {
        type: 'list';
        value: {
          type: 'compound';
          value: {
            id: {
              type: 'string';
              value: string;
            };
            lvl: {
              type: 'short';
              value: number;
            };
          }[];
        };
      };
      Damage?: {
        type: 'int';
        value: number;
      };
    };
  };
  Count: {
    type: 'byte';
    value: number;
  };
}

function shulkerContent(shulkerBox: Item) {
  let items: Item[] = [];
  let nbt = shulkerBox.nbt;
  // console.log("shulkerBoxI", shulkerBox);
  if (nbt?.type == TagType.Compound) {
    if (nbt.value.BlockEntityTag?.type == TagType.Compound) {
      if (nbt.value.BlockEntityTag.value.Items) {
        if (nbt.value.BlockEntityTag.value.Items.type == TagType.List) {
          if (
            nbt.value.BlockEntityTag.value.Items.value.type == TagType.Compound
          ) {
            let itemsANY: any =
              nbt.value.BlockEntityTag.value.Items.value.value;
            // console.log(JSON.stringify(itemsNBT, null, 2));
            // console.log(itemsNBT);
            let itemsNBT: MinecraftItem[] = itemsANY;
            for (let i = 0; i < itemsNBT.length; i++) {
              const itemNBT = itemsNBT[i];
              const itemNames = itemNBT.id.value.split(':');
              const id =
                getBot().registry.itemsByName[itemNames[itemNames.length - 1]]
                  .id;
              const itme: Item = new ItemI(
                id,
                itemNBT.Count.value,
                undefined,
                itemNBT.tag,
              );

              items.push(itme);

              // const items = new Item(
              //   id,
              //   itemNBT.Count.value,
              //   undefined,
              //   itemNBT.tag
              // );
              // console.log("itme", itme);
            }
          }
        }
      }
    }
  }
  return items;
}

Moosbee avatar Sep 28 '23 10:09 Moosbee