Advertisement
kotoroshinoto

Possible Metallurgy Thing

Oct 1st, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. package TFC.API.Crafting;
  2.  
  3. import java.util.HashMap;
  4. import java.util.List;
  5.  
  6. import TFC.API.Metal;
  7.  
  8. public class AlloyComponentMetal {
  9.     public AlloyComponentMetal(Metal metaltype, float units){
  10.         this.metaltype=metaltype;
  11.         this.units=units;
  12.         this.subcomponents=null;
  13.     }
  14.     public AlloyComponentMetal(Metal metaltype, float units,HashMap<Metal,AlloyComponentMetal> subcomponents){
  15.         this.metaltype=metaltype;
  16.         this.units=units;
  17.         this.subcomponents=subcomponents;
  18.     }
  19.     Metal metaltype;
  20.     float units;
  21.     HashMap<Metal,AlloyComponentMetal> subcomponents;
  22.     public boolean hasSubcomponent(Metal metaltype){
  23.         if (this.subcomponents == null){return false;}
  24.         return this.subcomponents.containsKey(metaltype);
  25.     }
  26.     public boolean canCombine(AlloyComponentMetal other){
  27.         if(this.metaltype != other.metaltype){return false;}
  28.         if(this.subcomponents != null && other.subcomponents != null){
  29.             java.util.Iterator<Metal> sub=this.subcomponents.keySet().iterator();
  30.             while(sub.hasNext()){
  31.                 Metal type=sub.next();
  32.                 if(!(other.subcomponents.containsKey(type))){
  33.                     //same alloy, different subcomponents, cannot combine
  34.                     return false;
  35.                 }
  36.                 AlloyComponentMetal subA,subB;
  37.                 subA=this.subcomponents.get(type);
  38.                 subB=other.subcomponents.get(type);
  39.                 if(!subA.canCombine(subB)){
  40.                     return false;
  41.                 }
  42.             }
  43.         }
  44.         return true;
  45.     }
  46.     public AlloyComponentMetal combine(AlloyComponentMetal other){
  47.         if(this.canCombine(other)){
  48.             AlloyComponentMetal combined=new AlloyComponentMetal(this.metaltype,this.units+other.units);
  49.             if(this.subcomponents != null && other.subcomponents != null){
  50.                 java.util.Iterator<Metal> sub=this.subcomponents.keySet().iterator();
  51.                 while(sub.hasNext()){
  52.                     Metal type=sub.next();
  53.                     if(!(other.subcomponents.containsKey(type))){
  54.                         //same alloy, different subcomponents, cannot combine
  55.                         return null;
  56.                     }
  57.                     AlloyComponentMetal subA,subB;
  58.                     subA=this.subcomponents.get(type);
  59.                     subB=other.subcomponents.get(type);
  60.                     this.subcomponents.put(type, subA.combine(subB));
  61.                 }
  62.             } else if(!(this.subcomponents == null && other.subcomponents == null )){
  63.                 //mismatched subcomponents
  64.                 return null;
  65.             }
  66.             return combined;
  67.         } else {
  68.             return null;
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement