ItemColoredSoup.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package me.km.items;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4. import me.km.blocks.cookingpot.CookingPotColorMixer;
  5. import net.minecraft.client.util.ITooltipFlag;
  6. import net.minecraft.entity.LivingEntity;
  7. import net.minecraft.entity.player.PlayerEntity;
  8. import net.minecraft.block.Blocks;
  9. import net.minecraft.item.Items;
  10. import net.minecraft.item.Item;
  11. import net.minecraft.item.Food;
  12. import net.minecraft.item.ItemGroup;
  13. import net.minecraft.item.ItemStack;
  14. import net.minecraft.nbt.CompoundNBT;
  15. import net.minecraft.nbt.ListNBT;
  16. import net.minecraft.nbt.StringNBT;
  17. import net.minecraft.potion.EffectInstance;
  18. import net.minecraft.potion.Effects;
  19. import net.minecraft.util.text.ITextComponent;
  20. import net.minecraft.util.text.StringTextComponent;
  21. import net.minecraft.util.text.TextFormatting;
  22. import net.minecraft.util.text.LanguageMap;
  23. import net.minecraft.world.World;
  24. import net.minecraftforge.common.capabilities.ICapabilityProvider;
  25. import net.minecraftforge.api.distmarker.Dist;
  26. import net.minecraftforge.api.distmarker.OnlyIn;
  27. public class ItemColoredSoup extends ItemFood
  28. {
  29. public ItemColoredSoup()
  30. {
  31. super(new Properties().group(ItemGroup.FOOD).food(new Food.Builder().build()).maxStackSize(1));
  32. }
  33. @Override
  34. public ItemStack onItemUseFinish(ItemStack stack, World w, LivingEntity liv)
  35. {
  36. super.onItemUseFinish(stack, w, liv);
  37. return new ItemStack(Items.BOWL);
  38. }
  39. @Override
  40. public ICapabilityProvider initCapabilities(ItemStack stack, CompoundNBT nbt)
  41. {
  42. getTagCompound(stack);
  43. return null;
  44. }
  45. @OnlyIn(Dist.CLIENT)
  46. @Override
  47. public void addInformation(ItemStack stack, World w, List<ITextComponent> lore, ITooltipFlag flag)
  48. {
  49. String text = getLocalizedIngredientList(stack);
  50. if(!text.isEmpty())
  51. {
  52. lore.add(new StringTextComponent(text));
  53. }
  54. }
  55. private static CompoundNBT getTagCompound(ItemStack stack)
  56. {
  57. CompoundNBT com = stack.getTag();
  58. if(com == null)
  59. {
  60. com = new CompoundNBT();
  61. com.putInt("color", CookingPotColorMixer.DEFAULT.getRGB());
  62. com.put("ingredients", new ListNBT());
  63. com.putFloat("saturation", 0.0f);
  64. com.putInt("food", 0);
  65. stack.setTag(com);
  66. }
  67. else
  68. {
  69. if(!com.contains("color"))
  70. {
  71. com.putInt("color", CookingPotColorMixer.DEFAULT.getRGB());
  72. }
  73. if(!com.contains("ingredients"))
  74. {
  75. com.put("ingredients", new ListNBT());
  76. }
  77. if(!com.contains("saturation"))
  78. {
  79. com.putFloat("saturation", 0);
  80. }
  81. if(!com.contains("food"))
  82. {
  83. com.putInt("food", 0);
  84. }
  85. }
  86. return com;
  87. }
  88. @SuppressWarnings(value = "")
  89. public static void addIngredient(ItemStack stew, Iterable<ItemStack> stacks)
  90. {
  91. CompoundNBT com = stew.getTag();
  92. ListNBT list = com.getList("ingredients", 8);
  93. for(ItemStack stack : stacks)
  94. {
  95. list.add(StringNBT.valueOf(stack.getTranslationKey()));
  96. }
  97. }
  98. @OnlyIn(Dist.CLIENT)
  99. @SuppressWarnings(value = "")
  100. private String getLocalizedIngredientList(ItemStack stack)
  101. {
  102. CompoundNBT com = stack.getTag();
  103. ListNBT list = com.getList("ingredients", 8);
  104. switch(list.size())
  105. {
  106. case 0: return "";
  107. case 1: return TextFormatting.GRAY + LanguageMap.getInstance().translateKey(list.getString(0));
  108. default:
  109. StringBuilder sb = new StringBuilder(TextFormatting.GRAY.toString());
  110. sb.append(LanguageMap.getInstance().translateKey(list.getString(0)));
  111. for(int i = 1; i < list.size(); i++)
  112. {
  113. sb.append(", ");
  114. sb.append(LanguageMap.getInstance().translateKey(list.getString(i)));
  115. }
  116. return sb.toString();
  117. }
  118. }
  119. public static void setColor(ItemStack stack, int color)
  120. {
  121. getTagCompound(stack).putInt("color", color);
  122. }
  123. public static int getColor(ItemStack stack)
  124. {
  125. return getTagCompound(stack).getInt("color");
  126. }
  127. public static void setFoodStats(ItemStack soup, LinkedList<ItemStack> ingredients)
  128. {
  129. float saturation = 0.0f;
  130. int food = 0;
  131. for(ItemStack stack : ingredients)
  132. {
  133. Item item = stack.getItem();
  134. if(item == Item.getItemFromBlock(Blocks.PUMPKIN))
  135. {
  136. food += 6;
  137. saturation += 0.3F;
  138. }
  139. else if(item == Items.APPLE)
  140. {
  141. food += 4;
  142. saturation += 0.3F;
  143. }
  144. else if(item == Items.GOLDEN_APPLE)
  145. {
  146. food += 4;
  147. saturation += 1.2F;
  148. }
  149. else if(item == Items.BEETROOT_SEEDS || item == Items.MELON_SEEDS || item == Items.PUMPKIN_SEEDS || item == Items.WHEAT_SEEDS)
  150. {
  151. food += 1;
  152. saturation += 0.1F;
  153. }
  154. else if(item == Items.BEEF || item == Items.PORKCHOP)
  155. {
  156. food += 8;
  157. saturation += 0.8F;
  158. }
  159. else if(item == Items.TROPICAL_FISH)
  160. {
  161. food += 1;
  162. saturation += 0.1F;
  163. }
  164. else if(item == Items.COD)
  165. {
  166. food += 5;
  167. saturation += 0.6F;
  168. }
  169. else if(item == Items.PUFFERFISH)
  170. {
  171. food -= 75;
  172. saturation -= 0.8F;
  173. }
  174. else if(item == Items.SALMON)
  175. {
  176. food += 6;
  177. saturation += 0.8F;
  178. }
  179. else if(item == Items.SUGAR)
  180. {
  181. food += 1;
  182. saturation += 0.1F;
  183. }
  184. else if(item == Items.MELON_SLICE || item == Items.GLISTERING_MELON_SLICE)
  185. {
  186. food += 2;
  187. saturation += 0.3F;
  188. }
  189. else if(item == Items.CHICKEN)
  190. {
  191. food += 6;
  192. saturation += 0.6F;
  193. }
  194. else if(item == Items.POISONOUS_POTATO || item == Items.SPIDER_EYE)
  195. {
  196. food += -25;
  197. saturation -= 0.2F;
  198. }
  199. else if(item == Items.RABBIT_FOOT || item == Items.NETHER_WART || item == Items.FERMENTED_SPIDER_EYE)
  200. {
  201. food += 1;
  202. saturation += 0.1F;
  203. }
  204. else if(item == Items.GOLDEN_CARROT || item == Items.CARROT)
  205. {
  206. food += 3;
  207. saturation += 0.6F;
  208. }
  209. else if(item == Items.RABBIT || item == Items.POTATO)
  210. {
  211. food += 5;
  212. saturation += 0.6F;
  213. }
  214. else if(item == Items.MUTTON)
  215. {
  216. food += 6;
  217. saturation += 0.8F;
  218. }
  219. else if(item == Items.BEETROOT)
  220. {
  221. food += 1;
  222. saturation += 0.6F;
  223. }
  224. }
  225. getTagCompound(soup).putFloat("saturation", saturation);
  226. getTagCompound(soup).putInt("food", food);
  227. }
  228. @Override
  229. public void onFoodEaten(ItemStack stack, World w, LivingEntity liv)
  230. {
  231. int food = getTagCompound(stack).getInt("food");
  232. if(food < 0 && !w.isRemote)
  233. {
  234. liv.addPotionEffect(new EffectInstance(Effects.POISON, 100, -food / 25));
  235. food = 0;
  236. }
  237. float saturation = getTagCompound(stack).getFloat("saturation");
  238. if(liv instanceof PlayerEntity && !w.isRemote)
  239. {
  240. ((PlayerEntity) liv).getFoodStats().addStats(food, saturation);
  241. }
  242. }
  243. }