ItemColoredSoup.java 7.3 KB

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