Xyberviri

getStringForItemStack

Aug 16th, 2019
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. /**
  2.  * Borrowed this from forestry
  3.  * @param itemStack
  4.  * @return
  5.  */
  6.     @Nullable
  7.     public static String getStringForItemStack(ItemStack itemStack) {
  8.         if (itemStack.isEmpty()) {
  9.             return null;
  10.         }
  11.  
  12.         Item item = itemStack.getItem();
  13.         String itemStackString = getItemNameFromRegistryAsString(item);
  14.         if (itemStackString == null) {
  15.             return null;
  16.         }
  17.  
  18.         int meta = itemStack.getItemDamage();
  19.         if (meta != OreDictionary.WILDCARD_VALUE) {
  20.             return itemStackString + ':' + meta;
  21.         } else {
  22.             return itemStackString;
  23.         }
  24.     }
  25.     /**
  26.      * @return The registry name of the item as {@link String}
  27.      */
  28.     @Nullable
  29.     public static String getItemNameFromRegistryAsString(Item item) {
  30.         ResourceLocation rl = item.getRegistryName();
  31.         return rl == null ? null : rl.toString();
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment