ItemColoredSoup.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. public ItemColoredSoup() {
  29. super(new Properties().group(ItemGroup.FOOD).food(new Food.Builder().build())
  30. .maxStackSize(1));
  31. }
  32. @Override
  33. public ItemStack onItemUseFinish(ItemStack stack, World w, LivingEntity liv) {
  34. super.onItemUseFinish(stack, w, liv);
  35. return new ItemStack(Items.BOWL);
  36. }
  37. @Override
  38. public ICapabilityProvider initCapabilities(ItemStack stack, CompoundNBT nbt) {
  39. getTagCompound(stack);
  40. return null;
  41. }
  42. @OnlyIn(Dist.CLIENT)
  43. @Override
  44. public void addInformation(ItemStack stack, World w, List<ITextComponent> lore,
  45. ITooltipFlag flag) {
  46. String text = getLocalizedIngredientList(stack);
  47. if(!text.isEmpty()) {
  48. lore.add(new StringTextComponent(text));
  49. }
  50. }
  51. private static CompoundNBT getTagCompound(ItemStack stack) {
  52. CompoundNBT com = stack.getTag();
  53. if(com == null) {
  54. com = new CompoundNBT();
  55. com.putInt("color", CookingPotColorMixer.DEFAULT.getRGB());
  56. com.put("ingredients", new ListNBT());
  57. com.putFloat("saturation", 0.0f);
  58. com.putInt("food", 0);
  59. stack.setTag(com);
  60. } else {
  61. if(!com.contains("color")) {
  62. com.putInt("color", CookingPotColorMixer.DEFAULT.getRGB());
  63. }
  64. if(!com.contains("ingredients")) {
  65. com.put("ingredients", new ListNBT());
  66. }
  67. if(!com.contains("saturation")) {
  68. com.putFloat("saturation", 0);
  69. }
  70. if(!com.contains("food")) {
  71. com.putInt("food", 0);
  72. }
  73. }
  74. return com;
  75. }
  76. @SuppressWarnings(value = "")
  77. public static void addIngredient(ItemStack stew, Iterable<ItemStack> stacks) {
  78. CompoundNBT com = stew.getTag();
  79. ListNBT list = com.getList("ingredients", 8);
  80. for(ItemStack stack : stacks) {
  81. list.add(StringNBT.valueOf(stack.getTranslationKey()));
  82. }
  83. }
  84. @OnlyIn(Dist.CLIENT)
  85. @SuppressWarnings(value = "")
  86. private String getLocalizedIngredientList(ItemStack stack) {
  87. CompoundNBT com = stack.getTag();
  88. if(com == null) {
  89. return "";
  90. }
  91. ListNBT list = com.getList("ingredients", 8);
  92. switch (list.size()) {
  93. case 0:
  94. return "";
  95. case 1:
  96. return TextFormatting.GRAY
  97. + LanguageMap.getInstance().func_230503_a_(list.getString(0));
  98. default:
  99. StringBuilder sb = new StringBuilder(TextFormatting.GRAY.toString());
  100. sb.append(LanguageMap.getInstance().func_230503_a_(list.getString(0)));
  101. for(int i = 1; i < list.size(); i++) {
  102. sb.append(", ");
  103. sb.append(LanguageMap.getInstance().func_230503_a_(list.getString(i)));
  104. }
  105. return sb.toString();
  106. }
  107. }
  108. public static void setColor(ItemStack stack, int color) {
  109. getTagCompound(stack).putInt("color", color);
  110. }
  111. public static int getColor(ItemStack stack) {
  112. return getTagCompound(stack).getInt("color");
  113. }
  114. @SuppressWarnings("deprecation")
  115. public static void setFoodStats(ItemStack soup, LinkedList<ItemStack> ingredients) {
  116. float saturation = 0.0f;
  117. int food = 0;
  118. for(ItemStack stack : ingredients) {
  119. Item item = stack.getItem();
  120. if(item == Item.getItemFromBlock(Blocks.PUMPKIN)) {
  121. food += 6;
  122. saturation += 0.3F;
  123. } else if(item == Items.APPLE) {
  124. food += 4;
  125. saturation += 0.3F;
  126. } else if(item == Items.GOLDEN_APPLE) {
  127. food += 4;
  128. saturation += 1.2F;
  129. } else if(item == Items.BEETROOT_SEEDS || item == Items.MELON_SEEDS
  130. || item == Items.PUMPKIN_SEEDS || item == Items.WHEAT_SEEDS) {
  131. food += 1;
  132. saturation += 0.1F;
  133. } else if(item == Items.BEEF || item == Items.PORKCHOP) {
  134. food += 8;
  135. saturation += 0.8F;
  136. } else if(item == Items.TROPICAL_FISH) {
  137. food += 1;
  138. saturation += 0.1F;
  139. } else if(item == Items.COD) {
  140. food += 5;
  141. saturation += 0.6F;
  142. } else if(item == Items.PUFFERFISH) {
  143. food -= 75;
  144. saturation -= 0.8F;
  145. } else if(item == Items.SALMON) {
  146. food += 6;
  147. saturation += 0.8F;
  148. } else if(item == Items.SUGAR) {
  149. food += 1;
  150. saturation += 0.1F;
  151. } else if(item == Items.MELON_SLICE || item == Items.GLISTERING_MELON_SLICE) {
  152. food += 2;
  153. saturation += 0.3F;
  154. } else if(item == Items.CHICKEN) {
  155. food += 6;
  156. saturation += 0.6F;
  157. } else if(item == Items.POISONOUS_POTATO || item == Items.SPIDER_EYE) {
  158. food += -25;
  159. saturation -= 0.2F;
  160. } else if(item == Items.RABBIT_FOOT || item == Items.NETHER_WART
  161. || item == Items.FERMENTED_SPIDER_EYE) {
  162. food += 1;
  163. saturation += 0.1F;
  164. } else if(item == Items.GOLDEN_CARROT || item == Items.CARROT) {
  165. food += 3;
  166. saturation += 0.6F;
  167. } else if(item == Items.RABBIT || item == Items.POTATO) {
  168. food += 5;
  169. saturation += 0.6F;
  170. } else if(item == Items.MUTTON) {
  171. food += 6;
  172. saturation += 0.8F;
  173. } else if(item == Items.BEETROOT) {
  174. food += 1;
  175. saturation += 0.6F;
  176. }
  177. }
  178. getTagCompound(soup).putFloat("saturation", saturation);
  179. getTagCompound(soup).putInt("food", food);
  180. }
  181. @Override
  182. public void onFoodEaten(ItemStack stack, World w, LivingEntity liv) {
  183. int food = getTagCompound(stack).getInt("food");
  184. if(food < 0 && !w.isRemote) {
  185. liv.addPotionEffect(new EffectInstance(Effects.POISON, 100, -food / 25));
  186. food = 0;
  187. }
  188. float saturation = getTagCompound(stack).getFloat("saturation");
  189. if(liv instanceof PlayerEntity && !w.isRemote) {
  190. ((PlayerEntity) liv).getFoodStats().addStats(food, saturation);
  191. }
  192. }
  193. }