TheGreyGhost / MinecraftByExample

Working sample code for the basic concepts in Minecraft and Forge.
Other
1.24k stars 187 forks source link

Bitwise operation value in mbe11_item_variants > ItemsVariants #36

Closed gottsch closed 6 years ago

gottsch commented 6 years ago

Hi. I was looking at your ItemsVariants class for the bitwise operations to get/set metadata and I noticed that you were using 0x07 for the upper 2 bits of data (fullnessBits):

  @Override
  public String getItemStackDisplayName(ItemStack stack)
  {
    String s = ("" + I18n.translateToLocal(this.getUnlocalizedName(stack) + ".name")).trim();  // this is depecrated, but I don't know what replaces it...
    int metadata = stack.getMetadata();
    int fullnessBits = (metadata >> 2) & 0x07;
    EnumBottleFullness fullness = EnumBottleFullness.byMetadata(fullnessBits);
    s += "(" + fullness.getDescription() + ")";
    return s;
  }

But this is unnecessary, as you could use 0x03 as you did for the lower 2 bits in the getUnlocalizedName() method, since you right-shifted the bits:

  public String getUnlocalizedName(ItemStack stack)
  {
    int metadata = stack.getMetadata();
    int contentsBits = metadata & 0x03;
    int fullnessBits = (metadata >> 2) & 0x07;

    EnumBottleContents contents = EnumBottleContents.byMetadata(contentsBits);
    return super.getUnlocalizedName() + "." + contents.getName();
  }
TheGreyGhost commented 6 years ago

Hi Hmm yeah it looks like you're right. Both of those should be int fullnessBits = (metadata >> 2); or more robustly (i.e. in case metadata is > 0x0f, for some reason) either int fullnessBits = (metadata >> 2) & 0x03; or int fullnessBits = (metadata & 0x0c) >> 2;

Thanks for that, I'll update them next time I'm working on the mod.

Cheers TGG

TheGreyGhost commented 6 years ago

Hi again I just looked at the code and I remembered that I'm using 5 bits in total, not 4, i.e. fullness can be 0, 1, 2, 3, or 4. The metadata for items is allowed to be more than 4 bits, unlike blocks. Cheers TGG