Advertisement
GenuineSounds

IMetal implementation

Feb 26th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.83 KB | None | 0 0
  1. package com.genuineflix.metal.registry;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import net.minecraft.block.Block;
  7. import net.minecraft.item.Item;
  8. import net.minecraft.item.ItemBlock;
  9. import net.minecraft.item.ItemStack;
  10. import net.minecraft.nbt.NBTTagCompound;
  11. import net.minecraft.nbt.NBTTagList;
  12.  
  13. import com.genuineflix.metal.api.IMetal;
  14.  
  15. class Metal implements IMetal {
  16.  
  17.     private String name;
  18.     private String displayName;
  19.     private Block ore;
  20.     private Block block;
  21.     private Item dust;
  22.     private Item ingot;
  23.     private Item nugget;
  24.     private Generation generation;
  25.     private List<Compound> compounds;
  26.  
  27.     Metal(final String name) {
  28.         this.name = name;
  29.         displayName = name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
  30.     }
  31.  
  32.     Metal(final String name, final Generation generation, final Compound... compounds) {
  33.         this.name = name;
  34.         displayName = name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
  35.         setGeneration(generation);
  36.         setCompounds(compounds);
  37.     }
  38.  
  39.     @Override
  40.     public String getName() {
  41.         return name;
  42.     }
  43.  
  44.     @Override
  45.     public String getDisplayName() {
  46.         return displayName;
  47.     }
  48.  
  49.     @Override
  50.     public List<Compound> getCompounds() {
  51.         return compounds;
  52.     }
  53.  
  54.     @Override
  55.     public Generation getGeneration() {
  56.         return generation;
  57.     }
  58.  
  59.     @Override
  60.     public boolean isComposite() {
  61.         return compounds != null && compounds.size() > 0;
  62.     }
  63.  
  64.     @Override
  65.     public void setCompounds(final Compound... compounds) {
  66.         this.compounds = new ArrayList<Compound>();
  67.         for (int i = 0; i < compounds.length; i++) {
  68.             final Compound compound = compounds[i];
  69.             if (!this.compounds.isEmpty() && this.compounds.contains(compound))
  70.                 this.compounds.get(this.compounds.indexOf(compound)).factor++;
  71.             else
  72.                 this.compounds.add(compound);
  73.         }
  74.     }
  75.  
  76.     @Override
  77.     public void setGeneration(final Generation generation) {
  78.         this.generation = generation;
  79.     }
  80.  
  81.     public void setCompounds(final List<Compound> compounds) {
  82.         this.compounds = compounds;
  83.     }
  84.  
  85.     void finallize() {
  86.         if (generation == null)
  87.             return;
  88.         ore.setHardness(generation.hardness);
  89.         ore.setResistance(generation.resistance);
  90.         block.setHardness(generation.hardness);
  91.         block.setResistance(generation.resistance);
  92.     }
  93.  
  94.     @Override
  95.     public Block getOre() {
  96.         return ore;
  97.     }
  98.  
  99.     @Override
  100.     public Block getBlock() {
  101.         return block;
  102.     }
  103.  
  104.     @Override
  105.     public Item getDust() {
  106.         return dust;
  107.     }
  108.  
  109.     @Override
  110.     public Item getIngot() {
  111.         return ingot;
  112.     }
  113.  
  114.     @Override
  115.     public Item getNugget() {
  116.         return nugget;
  117.     }
  118.  
  119.     void setOre(final Block ore) {
  120.         this.ore = ore;
  121.     }
  122.  
  123.     void setBlock(final Block block) {
  124.         this.block = block;
  125.     }
  126.  
  127.     void setDust(final Item dust) {
  128.         this.dust = dust;
  129.     }
  130.  
  131.     void setIngot(final Item ingot) {
  132.         this.ingot = ingot;
  133.     }
  134.  
  135.     void setNugget(final Item nugget) {
  136.         this.nugget = nugget;
  137.     }
  138.  
  139.     @Override
  140.     public NBTTagCompound save(final NBTTagCompound data) {
  141.         data.setString("class", this.getClass().getName());
  142.         data.setString("name", name);
  143.         data.setString("displayName", displayName);
  144.         data.setTag("ore", new ItemStack(ore).writeToNBT(new NBTTagCompound()));
  145.         data.setTag("block", new ItemStack(block).writeToNBT(new NBTTagCompound()));
  146.         data.setTag("dust", new ItemStack(dust).writeToNBT(new NBTTagCompound()));
  147.         data.setTag("ingot", new ItemStack(ingot).writeToNBT(new NBTTagCompound()));
  148.         data.setTag("nugget", new ItemStack(nugget).writeToNBT(new NBTTagCompound()));
  149.         if (generation != null)
  150.             data.setTag("generation", generation.save(new NBTTagCompound()));
  151.         if (compounds != null && !compounds.isEmpty()) {
  152.             final NBTTagList comps = new NBTTagList();
  153.             for (int i = 0; i < compounds.size(); i++)
  154.                 comps.appendTag(compounds.get(i).save(new NBTTagCompound()));
  155.             data.setTag("compounds", comps);
  156.         }
  157.         return data;
  158.     }
  159.  
  160.     @Override
  161.     public Metal load(final NBTTagCompound data) {
  162.         name = data.getString("name");
  163.         displayName = data.getString("displayName");
  164.         ore = ((ItemBlock) ItemStack.loadItemStackFromNBT(data.getCompoundTag("ore")).getItem()).field_150939_a;
  165.         block = ((ItemBlock) ItemStack.loadItemStackFromNBT(data.getCompoundTag("block")).getItem()).field_150939_a;
  166.         dust = ItemStack.loadItemStackFromNBT(data.getCompoundTag("dust")).getItem();
  167.         ingot = ItemStack.loadItemStackFromNBT(data.getCompoundTag("ingot")).getItem();
  168.         nugget = ItemStack.loadItemStackFromNBT(data.getCompoundTag("nugget")).getItem();
  169.         if (data.hasKey("generation"))
  170.             generation = new Generation().load(data.getCompoundTag("generation"));
  171.         if (data.hasKey("compounds")) {
  172.             final NBTTagList list = data.getTagList("compounds", 10);
  173.             final List<Compound> compounds = new ArrayList<Compound>();
  174.             for (int i = 0; i < list.tagCount(); i++)
  175.                 compounds.add(new Compound().load(list.getCompoundTagAt(i)));
  176.             this.compounds = compounds;
  177.         }
  178.         return this;
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement