Advertisement
PaleoCrafter

SubItem system - ItemBase

Jun 21st, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. package de.paleocrafter.pmfw.item;
  2.  
  3. import java.util.HashMap;
  4. import java.util.List;
  5.  
  6. import cpw.mods.fml.relauncher.Side;
  7. import cpw.mods.fml.relauncher.SideOnly;
  8.  
  9. import net.minecraft.client.renderer.texture.IconRegister;
  10. import net.minecraft.creativetab.CreativeTabs;
  11. import net.minecraft.entity.player.EntityPlayer;
  12. import net.minecraft.item.Item;
  13. import net.minecraft.item.ItemStack;
  14. import net.minecraft.nbt.NBTTagCompound;
  15. import net.minecraft.util.Icon;
  16. import net.minecraft.world.World;
  17.  
  18. /**
  19.  *
  20.  * PaleoMachineFramework
  21.  *
  22.  * ItemBase
  23.  *
  24.  * @author PaleoCrafter
  25.  * @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
  26.  *
  27.  */
  28. public class ItemBase extends Item {
  29.  
  30.     private static HashMap<String, SubItem> subItems;
  31.  
  32.     static {
  33.         subItems = new HashMap<String, SubItem>();
  34.     }
  35.  
  36.     public ItemBase(int id) {
  37.         super(id);
  38.     }
  39.  
  40.     public static void addSubItem(String name, SubItem subItem) {
  41.         subItems.put(name, subItem);
  42.     }
  43.  
  44.     public static SubItem getSubItem(String name) {
  45.         return subItems.get(name);
  46.     }
  47.  
  48.     @Override
  49.     public String getUnlocalizedName(ItemStack is) {
  50.         defaultSubItem(is);
  51.         String name = getSubItem(is.getTagCompound().getString("SubItemName"))
  52.                 .getUnlocalizedName();
  53.         return this.getUnlocalizedName() + "." + name;
  54.     }
  55.  
  56.     public void registerRecipes() {
  57.         ItemStack output = new ItemStack(this, 1, 0);
  58.         output.setTagCompound(new NBTTagCompound());
  59.  
  60.         for (String name : subItems.keySet()) {
  61.             output.getTagCompound().setString("SubItemName", name);
  62.             subItems.get(name).registerRecipe(output);
  63.         }
  64.     }
  65.  
  66.     @Override
  67.     @SideOnly(Side.CLIENT)
  68.     public void registerIcons(IconRegister iconRegister) {
  69.         for (SubItem item : subItems.values()) {
  70.             item.registerIcons(iconRegister);
  71.         }
  72.     }
  73.  
  74.     @Override
  75.     public Icon getIcon(ItemStack stack, int pass) {
  76.         defaultSubItem(stack);
  77.         return null;
  78.     }
  79.  
  80.     @SuppressWarnings({ "rawtypes", "unchecked" })
  81.     @Override
  82.     @SideOnly(Side.CLIENT)
  83.     public void getSubItems(int id, CreativeTabs tabs, List list) {
  84.         for (String name : subItems.keySet()) {
  85.             NBTTagCompound tag = new NBTTagCompound();
  86.             tag.setString("SubItemName", name);
  87.             ItemStack is = new ItemStack(this, 1);
  88.             is.setTagCompound(tag);
  89.             list.add(is);
  90.         }
  91.     }
  92.  
  93.     @Override
  94.     @SideOnly(Side.CLIENT)
  95.     public boolean requiresMultipleRenderPasses() {
  96.         return true;
  97.     }
  98.  
  99.     @Override
  100.     public int getRenderPasses(int damage) {
  101.         return 1;
  102.     }
  103.  
  104.     @Override
  105.     public void onCreated(ItemStack stack, World world, EntityPlayer player) {
  106.         defaultSubItem(stack);
  107.         subItems.get(stack.getTagCompound().getString("SubItemName"))
  108.                 .onCreated(stack, world, player);
  109.     }
  110.  
  111.     public void defaultSubItem(ItemStack stack) {
  112.         if (!stack.hasTagCompound()) {
  113.             NBTTagCompound tag = new NBTTagCompound();
  114.             if (subItems.isEmpty())
  115.                 throw new IllegalArgumentException(
  116.                         "There has to be at least one SubItem added to "
  117.                                 + this.getClass().getName());
  118.             tag.setString("SubItemName", subItems.keySet().iterator().next());
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement