Guest User

Untitled

a guest
Feb 8th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.84 KB | None | 0 0
  1. package co.uk.silvania.cities.food.items.foods;
  2.  
  3. import java.util.List;
  4. import java.util.Random;
  5.  
  6. import net.minecraft.client.renderer.texture.IconRegister;
  7. import net.minecraft.entity.Entity;
  8. import net.minecraft.entity.player.EntityPlayer;
  9. import net.minecraft.item.Item;
  10. import net.minecraft.item.ItemFood;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.nbt.NBTTagCompound;
  13. import net.minecraft.potion.Potion;
  14. import net.minecraft.potion.PotionEffect;
  15. import net.minecraft.util.EnumChatFormatting;
  16. import net.minecraft.util.Icon;
  17. import net.minecraft.world.World;
  18. import co.uk.silvania.cities.api.GeneralUtils;
  19. import co.uk.silvania.cities.core.CityConfig;
  20. import co.uk.silvania.cities.food.FCF_Items;
  21. import co.uk.silvania.cities.food.FlenixCities_Food;
  22. import cpw.mods.fml.common.registry.LanguageRegistry;
  23. import cpw.mods.fml.relauncher.Side;
  24. import cpw.mods.fml.relauncher.SideOnly;
  25.  
  26. public class ItemFoodMeat extends ItemFood implements IFlenixFoods {
  27.    
  28.     public int feedValue;
  29.     public float satValue;
  30.     private int expiryTime;
  31.     private String meatName;
  32.     private float underCookedLevel;
  33.     private float cookedLevel;
  34.     private float burnedLevel;
  35.     private boolean poisonous;
  36.     private int boneSize;
  37.     private int boneQty;
  38.    
  39.  
  40.     public ItemFoodMeat(int id, int feed, float sat, boolean wolf, int time, String name, float uc, float c, float b, boolean poison, int bone, int boneq) {
  41.         super(id, feed, sat, wolf);
  42.         this.feedValue = feed;
  43.         this.satValue = sat;
  44.         this.expiryTime = time;
  45.         this.meatName = name;
  46.         this.underCookedLevel = uc;
  47.         this.cookedLevel = c;
  48.         this.burnedLevel = b;
  49.         this.poisonous = poison;
  50.         this.boneSize = bone;
  51.         this.boneQty = boneq;
  52.     }
  53.    
  54.     @Override
  55.     //Control the food; do things like tick it to slowly rot.
  56.     public void onUpdate(ItemStack item, World world, Entity entity, int par4, boolean par5) {
  57.         EntityPlayer player = (EntityPlayer) entity;
  58.         if (!world.isRemote) {
  59.             if (item.stackTagCompound == null) {
  60.                 item.stackTagCompound = new NBTTagCompound();
  61.                 item.stackTagCompound.setInteger("expiryTime", expiryTime);
  62.                 item.stackTagCompound.setInteger("livingTime", 0);
  63.                 item.stackTagCompound.setInteger("feedValue", feedValue);
  64.                 item.stackTagCompound.setInteger("temperature", 21000);
  65.                 item.stackTagCompound.setFloat("satValue", satValue);
  66.                 item.stackTagCompound.setFloat("cookedValue", 0);
  67.                 item.stackTagCompound.setBoolean("burned", false);
  68.                 item.stackTagCompound.setBoolean("mouldy", false);
  69.                 item.stackTagCompound.setFloat("underCookedLevel", underCookedLevel);
  70.                 item.stackTagCompound.setFloat("cookedLevel", cookedLevel);
  71.                 item.stackTagCompound.setFloat("burnedLevel", burnedLevel);
  72.             }
  73.             boolean mouldy = item.stackTagCompound.getBoolean("mouldy");
  74.             boolean burned = item.stackTagCompound.getBoolean("burned");
  75.             int expTime = item.stackTagCompound.getInteger("expiryTime");
  76.             int lvTime = item.stackTagCompound.getInteger("livingTime");
  77.             int temp = item.stackTagCompound.getInteger("temperature");
  78.             int roundTemp = Math.round(item.stackTagCompound.getInteger("temperature") / 1000);
  79.            
  80.             if (!mouldy) {
  81.                 item.stackTagCompound.setInteger("livingTime", (lvTime + 1));
  82.             }
  83.             if (lvTime >= expTime && !burned) {
  84.                 item.stackTagCompound.setBoolean("mouldy", true);
  85.                 item.stackTagCompound.setInteger("feedValue", 0);
  86.                 item.stackTagCompound.setFloat("satValue", 0);
  87.             }
  88.            
  89.             if ((item.stackTagCompound.getFloat("cookedValue")) >= (item.stackTagCompound.getFloat("cookedLevel")) && !burned) {
  90.                 if (roundTemp >= GeneralUtils.getPlayerTemperature(player)) {
  91.                     item.stackTagCompound.setInteger("temperature", temp - 1);
  92.                 } else if (roundTemp <= GeneralUtils.getPlayerTemperature(player)) {
  93.                     item.stackTagCompound.setInteger("temperature", temp + 1);
  94.                 }
  95.             }
  96.             if ((item.stackTagCompound.getFloat("cookedValue")) >= (item.stackTagCompound.getFloat("burnedLevel"))) {
  97.                 if (!world.isRemote) {
  98.                     item.stackTagCompound.setBoolean("burned", true);
  99.                     item.stackTagCompound.setInteger("feedValue", 0);
  100.                     item.stackTagCompound.setFloat("satValue", 0);
  101.                     item.stackTagCompound.setInteger("livingTime", item.stackTagCompound.getInteger("expiryTime"));
  102.                 }
  103.             }
  104.            
  105.             //Math for calculating how much to restore. Incomplete until I override food itself to have higher max values.
  106.             if (!mouldy && !burned) {
  107.                 float c = item.stackTagCompound.getFloat("cookedValue");
  108.                 float ucooked = item.stackTagCompound.getFloat("underCookedLevel");
  109.                 float cooked = item.stackTagCompound.getFloat("cookedLevel");
  110.                 float fBurned = item.stackTagCompound.getFloat("burnedLevel");
  111.                
  112.                 if (c < ucooked) { //raw
  113.                     item.stackTagCompound.setInteger("feedValue", feedValue);
  114.                     item.stackTagCompound.setFloat("satValue", satValue);
  115.                 } else if (c < cooked) { //undercooked
  116.                     item.stackTagCompound.setInteger("feedValue", feedValue + 1);
  117.                     item.stackTagCompound.setFloat("satValue", satValue);
  118.                 } else if (c < fBurned) { //cooked
  119.                     float fdFloat = (float) feedValue;
  120.                     int finalFeed = (int) Math.round((fdFloat + 1) * 2.5);
  121.                     float finalSat = satValue * 15;
  122.                     item.stackTagCompound.setInteger("feedValue", finalFeed);
  123.                     item.stackTagCompound.setFloat("satValue", finalSat);
  124.                 } else //burned
  125.                     item.stackTagCompound.setInteger("feedValue", 0);
  126.                     item.stackTagCompound.setFloat("satValue", 0);
  127.             }
  128.         }
  129.     }
  130.    
  131.     @Override //If the food is considered unsafe to eat raw, add a poison effect.
  132.     //Also give any bones back that were inside the food.
  133.     public ItemStack onEaten(ItemStack item, World world, EntityPlayer player) {
  134.         System.out.println("Eating!");
  135.         if (item.stackTagCompound.getFloat("cookedValue") > item.stackTagCompound.getFloat("underCookedLevel")) {
  136.             player.addPotionEffect((new PotionEffect(Potion.poison.getId(), 0, 1)));
  137.             player.addPotionEffect((new PotionEffect(Potion.confusion.getId(), 0, 1)));
  138.         }
  139.         if (this.poisonous) {
  140.             player.addPotionEffect((new PotionEffect(Potion.poison.getId(), 0, 1)));
  141.             player.addPotionEffect((new PotionEffect(Potion.confusion.getId(), 0, 1)));
  142.         }
  143.         --item.stackSize;
  144.         player.getFoodStats().addStats(this);
  145.         world.playSoundAtEntity(player, "random.burp", 0.5F, world.rand.nextFloat() * 0.1F + 0.9F);
  146.         this.onFoodEaten(item, world, player);
  147.         if (boneSize < 0) {
  148.             if (boneSize == 1) {
  149.                 //TODO Small bone
  150.             }
  151.             if (boneSize == 2) {
  152.                 //TODO Medium bone
  153.             }
  154.             if (boneSize == 3) {
  155.                 player.inventory.addItemStackToInventory(new ItemStack(Item.bone.itemID, boneQty, 1));
  156.             }
  157.             if (boneSize == 4) {
  158.                 //TODO Huge Bone
  159.             }
  160.             if (boneSize == 5) {
  161.                 player.inventory.addItemStackToInventory(new ItemStack(Item.bone.itemID, boneQty, 1));
  162.                 //player.inventory.addItemStackToInventory(new ItemStack(Item.bone.itemID, boneQty, 1)); //Small Bone
  163.             }
  164.             if (boneSize == 6) {
  165.                 player.inventory.addItemStackToInventory(new ItemStack(Item.bone.itemID, boneQty, 1));
  166.                 //player.inventory.addItemStackToInventory(new ItemStack(Item.bone.itemID, boneQty, 1)); //Medium Bone
  167.             }
  168.         }
  169.         return item;
  170.     }
  171.    
  172.     @Override
  173.     public ItemStack onItemRightClick(ItemStack item, World world, EntityPlayer player) {
  174.         if (player.canEat(this.alwaysEdible)) {
  175.             player.setItemInUse(item, this.getMaxItemUseDuration(item));
  176.         }
  177.         getItemUseAction(item);
  178.        
  179.         //Temporary cooking mechanism
  180.         if (!world.isRemote) {
  181.             if (player.isSneaking()) {
  182.                 System.out.println("Add to cooked value");
  183.                 item.stackTagCompound.setFloat("cookedValue", (item.stackTagCompound.getFloat("cookedValue") + 1));
  184.             }
  185.         }
  186.         return item;
  187.     }
  188.    
  189.     //****************************************************************
  190.     //****************************************************************
  191.     //
  192.     //Everything below here is graphical!!! Names, textures and so on.
  193.     //
  194.     //****************************************************************
  195.     //****************************************************************
  196.    
  197.     @Override //Alter name based on cooked state
  198.     public String getUnlocalizedName(ItemStack item) {
  199.         if (item.stackTagCompound != null) {
  200.             float c = item.stackTagCompound.getFloat("cookedValue");
  201.             float ucooked = item.stackTagCompound.getFloat("underCookedLevel");
  202.             float cooked = item.stackTagCompound.getFloat("cookedLevel");
  203.             float burned = item.stackTagCompound.getFloat("burnedLevel");
  204.             int cookedType = item.stackTagCompound.getInteger("cookedType");
  205.             if (c < 0) {
  206.                 return "item.frozen" + meatName;
  207.             } else if (c < ucooked) {
  208.                 return "item.raw" + meatName;
  209.             } else if (c < cooked) {
  210.                 return "item.underCooked" + meatName;
  211.             } else if (c < burned) {
  212.                 if (cookedType == 1) {
  213.                     return "item.roasted" + meatName;
  214.                 }
  215.                 if (cookedType == 2) {
  216.                     return "item.fried" + meatName;
  217.                 }
  218.                 if (cookedType == 3) {
  219.                     return "item.grilled" + meatName;
  220.                 }
  221.                 if (cookedType == 4) {
  222.                     return "item.steamed" + meatName;
  223.                 }
  224.                 return "item.cooked" + meatName;
  225.             } else
  226.                 return "item.burned" + meatName;
  227.         } else
  228.             return "item." + meatName;
  229.     }
  230.        
  231.     //Add nutritional info, best before date, and mouldy/burned stats if applicable
  232.     public void addInformation(ItemStack item, EntityPlayer player, List list, boolean bool) {
  233.         if (item.stackTagCompound != null) {
  234.             int expTime = item.stackTagCompound.getInteger("expiryTime");
  235.             int livingTime = item.stackTagCompound.getInteger("livingTime");
  236.             int feed = item.stackTagCompound.getInteger("feedValue");
  237.             float sat = item.stackTagCompound.getFloat("satValue");
  238.             boolean burn = item.stackTagCompound.getBoolean("burned");
  239.             boolean mouldy = item.stackTagCompound.getBoolean("mouldy");
  240.            
  241.             int remainingTime = expTime - livingTime;
  242.             int timeHours = Math.round(remainingTime / 1000);
  243.             int timeDays = 0;
  244.             while (timeHours >= 24) {
  245.                 timeDays = timeDays + 1;
  246.                 timeHours = timeHours - 24;
  247.             }
  248.             //list.add("expTime: " + expTime + ", livingtime: " + livingTime + ", remainingTime: " + remainingTime);
  249.             if (remainingTime > 0) {
  250.                 EnumChatFormatting colour = EnumChatFormatting.WHITE;
  251.                 if (remainingTime >= (expiryTime / 4) * 3) {
  252.                     colour = EnumChatFormatting.DARK_GREEN;
  253.                 } else if (remainingTime >= expiryTime / 2) {
  254.                     colour = EnumChatFormatting.GOLD;
  255.                 } else if (remainingTime >= expiryTime / 4) {
  256.                     colour = EnumChatFormatting.RED;
  257.                 }
  258.                 String day = " Days, ";
  259.                 String hour = " Hours.";
  260.                 if (timeDays == 1) {
  261.                     day = " Day, ";
  262.                 }
  263.                 if (timeHours == 1) {
  264.                     hour = " Hour.";
  265.                 }
  266.                 if (timeHours < 1) {
  267.                     hour = " Minutes.";
  268.                     list.add(colour + "Expires In: " + timeDays + day + (timeHours / 60) + hour);
  269.                     list.add("");
  270.                 } else
  271.                 list.add(colour + "Expires In: " + timeDays + day + timeHours + hour);
  272.                 list.add("");
  273.             } else
  274.                 if (!burn) {
  275.                     list.add(EnumChatFormatting.DARK_RED + "Expired");
  276.                     list.add("");
  277.                 }
  278.  
  279.             list.add("Nutritional Information:");
  280.            
  281.             EnumChatFormatting fdColour = EnumChatFormatting.WHITE;
  282.             if (feed == 1) {
  283.                 fdColour = EnumChatFormatting.DARK_RED;
  284.             } else if (feed == 2) {
  285.                 fdColour = EnumChatFormatting.RED;
  286.             } else if (feed == 3) {
  287.                 fdColour = EnumChatFormatting.GOLD;
  288.             } else if (feed == 4) {
  289.                 fdColour = EnumChatFormatting.DARK_GREEN;
  290.             } else if (feed > 4) {
  291.                 fdColour = EnumChatFormatting.GREEN;
  292.             }
  293.             list.add("Fill: " + fdColour + feed);
  294.            
  295.             EnumChatFormatting stColour = EnumChatFormatting.WHITE;
  296.             if (sat <= 0.1) {
  297.                 stColour = EnumChatFormatting.DARK_RED;
  298.             } else if (sat <= 0.2) {
  299.                 stColour = EnumChatFormatting.RED;
  300.             } else if (sat <= 0.3) {
  301.                 stColour = EnumChatFormatting.GOLD;
  302.             } else if (sat <= 0.5) {
  303.                 stColour = EnumChatFormatting.DARK_GREEN;
  304.             } else if (sat > 0.5) {
  305.                 stColour = EnumChatFormatting.GREEN;
  306.             }
  307.             list.add("Satiety: " + stColour + sat);
  308.             if (burn == true) {
  309.                 list.add("");
  310.                 list.add(EnumChatFormatting.DARK_GRAY + "Burned");
  311.             }
  312.         }
  313.     }
  314.    
  315.     @SideOnly(Side.CLIENT)
  316.     public Icon iconRaw;
  317.     public Icon iconUnderCooked;
  318.     public Icon iconCooked;
  319.     public Icon iconBurned;
  320.    
  321.     public void registerIcons(IconRegister iconRegister) {
  322.         iconRaw = iconRegister.registerIcon(FlenixCities_Food.modid + ":" + (this.getUnlocalizedName().toLowerCase().substring(5)) + "0");
  323.         iconUnderCooked = iconRegister.registerIcon(FlenixCities_Food.modid + ":" + (this.getUnlocalizedName().toLowerCase().substring(5)) + "1");
  324.         iconCooked = iconRegister.registerIcon(FlenixCities_Food.modid + ":" + (this.getUnlocalizedName().toLowerCase().substring(5)) + "2");
  325.         iconBurned = iconRegister.registerIcon(FlenixCities_Food.modid + ":" + (this.getUnlocalizedName().toLowerCase().substring(5)) + "3");
  326.     }
  327.    
  328.     @Override //Required for icon switching
  329.     public boolean requiresMultipleRenderPasses() {
  330.         return true;
  331.     }
  332.    
  333.     //Set the icon based on cooked state
  334.     public Icon getIcon(ItemStack item, int pass) {
  335.         if (item.stackTagCompound != null) {
  336.             float c = item.stackTagCompound.getFloat("cookedValue");
  337.             float ucooked = item.stackTagCompound.getFloat("underCookedLevel");
  338.             float cooked = item.stackTagCompound.getFloat("cookedLevel");
  339.             float burned = item.stackTagCompound.getFloat("burnedLevel");
  340.             if (c < ucooked) {
  341.                 return iconRaw;
  342.             } else if (c < cooked) {
  343.                 return iconUnderCooked;
  344.             } else if (c < burned) {
  345.                 return iconCooked;
  346.             } else
  347.             return iconBurned;
  348.         } else
  349.             return iconRaw;
  350.     }
  351. }
Advertisement
Add Comment
Please, Sign In to add comment